rust: build failure after NON_DEPENDENT_EXPR removal [PR111899]
[official-gcc.git] / gcc / match.pd
bloba56838fb388a42eb111a86fc103d9e21b00db24a
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 {value_range vr0, vr1;}
930      (if (INTEGRAL_TYPE_P (type)
931           && get_range_query (cfun)->range_of_expr (vr0, @0)
932           && get_range_query (cfun)->range_of_expr (vr1, @1)
933           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
934       @0))
935 #endif
936    ))))
938 #if GIMPLE
939 (for div (trunc_div exact_div)
940  /* Simplify (X + M*N) / N -> X / N + M.  */
941  (simplify
942   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
943   (with {value_range vr0, vr1, vr2, vr3, vr4;}
944   (if (INTEGRAL_TYPE_P (type)
945        && get_range_query (cfun)->range_of_expr (vr1, @1)
946        && get_range_query (cfun)->range_of_expr (vr2, @2)
947        /* "N*M" doesn't overflow.  */
948        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
949        && get_range_query (cfun)->range_of_expr (vr0, @0)
950        && get_range_query (cfun)->range_of_expr (vr3, @3)
951        /* "X+(N*M)" doesn't overflow.  */
952        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
953        && get_range_query (cfun)->range_of_expr (vr4, @4)
954        && !vr4.undefined_p ()
955        /* "X+N*M" is not with opposite sign as "X".  */
956        && (TYPE_UNSIGNED (type)
957            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
958            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
959   (plus (div @0 @2) @1))))
961  /* Simplify (X - M*N) / N -> X / N - M.  */
962  (simplify
963   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
964   (with {value_range vr0, vr1, vr2, vr3, vr4;}
965   (if (INTEGRAL_TYPE_P (type)
966        && get_range_query (cfun)->range_of_expr (vr1, @1)
967        && get_range_query (cfun)->range_of_expr (vr2, @2)
968        /* "N * M" doesn't overflow.  */
969        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
970        && get_range_query (cfun)->range_of_expr (vr0, @0)
971        && get_range_query (cfun)->range_of_expr (vr3, @3)
972        /* "X - (N*M)" doesn't overflow.  */
973        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
974        && get_range_query (cfun)->range_of_expr (vr4, @4)
975        && !vr4.undefined_p ()
976        /* "X-N*M" is not with opposite sign as "X".  */
977        && (TYPE_UNSIGNED (type)
978            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
979            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
980   (minus (div @0 @2) @1)))))
982 /* Simplify
983    (X + C) / N -> X / N + C / N where C is multiple of N.
984    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
985 (for op (trunc_div exact_div rshift)
986  (simplify
987   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
988    (with
989     {
990       wide_int c = wi::to_wide (@1);
991       wide_int n = wi::to_wide (@2);
992       bool shift = op == RSHIFT_EXPR;
993 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
994                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
995 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
996                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
997       value_range vr0, vr1, vr3;
998     }
999     (if (INTEGRAL_TYPE_P (type)
1000          && get_range_query (cfun)->range_of_expr (vr0, @0))
1001      (if (exact_mod (c)
1002           && get_range_query (cfun)->range_of_expr (vr1, @1)
1003           /* "X+C" doesn't overflow.  */
1004           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1005           && get_range_query (cfun)->range_of_expr (vr3, @3)
1006           && !vr3.undefined_p ()
1007           /* "X+C" and "X" are not of opposite sign.  */
1008           && (TYPE_UNSIGNED (type)
1009               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1010               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1011        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1012        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1013             && exact_mod (-c)
1014             /* unsigned "X-(-C)" doesn't underflow.  */
1015             && wi::geu_p (vr0.lower_bound (), -c))
1016          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1017 #undef plus_op1
1018 #undef exact_mod
1019 #endif
1021 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1022    if var is smaller in precision.
1023    This is always safe for both doing the negative in signed or unsigned
1024    as the value for undefined will not show up.  */
1025 (simplify
1026  (convert (negate:s@1 (convert:s @0)))
1027  (if (INTEGRAL_TYPE_P (type)
1028       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1029       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1030     (negate (convert @0))))
1032 (for op (negate abs)
1033  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1034  (for coss (COS COSH)
1035   (simplify
1036    (coss (op @0))
1037     (coss @0)))
1038  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1039  (for pows (POW)
1040   (simplify
1041    (pows (op @0) REAL_CST@1)
1042    (with { HOST_WIDE_INT n; }
1043     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1044      (pows @0 @1)))))
1045  /* Likewise for powi.  */
1046  (for pows (POWI)
1047   (simplify
1048    (pows (op @0) INTEGER_CST@1)
1049    (if ((wi::to_wide (@1) & 1) == 0)
1050     (pows @0 @1))))
1051  /* Strip negate and abs from both operands of hypot.  */
1052  (for hypots (HYPOT)
1053   (simplify
1054    (hypots (op @0) @1)
1055    (hypots @0 @1))
1056   (simplify
1057    (hypots @0 (op @1))
1058    (hypots @0 @1)))
1059  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1060  (for copysigns (COPYSIGN_ALL)
1061   (simplify
1062    (copysigns (op @0) @1)
1063    (copysigns @0 @1))))
1065 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1066 (simplify
1067  (mult (abs@1 @0) @1)
1068  (mult @0 @0))
1070 /* Convert absu(x)*absu(x) -> x*x.  */
1071 (simplify
1072  (mult (absu@1 @0) @1)
1073  (mult (convert@2 @0) @2))
1075 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1076 (for coss (COS COSH)
1077      copysigns (COPYSIGN)
1078  (simplify
1079   (coss (copysigns @0 @1))
1080    (coss @0)))
1082 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1083 (for pows (POW)
1084      copysigns (COPYSIGN)
1085  (simplify
1086   (pows (copysigns @0 @2) REAL_CST@1)
1087   (with { HOST_WIDE_INT n; }
1088    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1089     (pows @0 @1)))))
1090 /* Likewise for powi.  */
1091 (for pows (POWI)
1092      copysigns (COPYSIGN)
1093  (simplify
1094   (pows (copysigns @0 @2) INTEGER_CST@1)
1095   (if ((wi::to_wide (@1) & 1) == 0)
1096    (pows @0 @1))))
1098 (for hypots (HYPOT)
1099      copysigns (COPYSIGN)
1100  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1101  (simplify
1102   (hypots (copysigns @0 @1) @2)
1103   (hypots @0 @2))
1104  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1105  (simplify
1106   (hypots @0 (copysigns @1 @2))
1107   (hypots @0 @1)))
1109 /* copysign(x, CST) -> [-]abs (x).  */
1110 (for copysigns (COPYSIGN_ALL)
1111  (simplify
1112   (copysigns @0 REAL_CST@1)
1113   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1114    (negate (abs @0))
1115    (abs @0))))
1117 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1118 (for copysigns (COPYSIGN_ALL)
1119  (simplify
1120   (copysigns (copysigns @0 @1) @2)
1121   (copysigns @0 @2)))
1123 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1124 (for copysigns (COPYSIGN_ALL)
1125  (simplify
1126   (mult (copysigns@2 @0 @1) @2)
1127   (mult @0 @0)))
1129 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1130 (for ccoss (CCOS CCOSH)
1131  (simplify
1132   (ccoss (negate @0))
1133    (ccoss @0)))
1135 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1136 (for ops (conj negate)
1137  (for cabss (CABS)
1138   (simplify
1139    (cabss (ops @0))
1140    (cabss @0))))
1142 /* Fold (a * (1 << b)) into (a << b)  */
1143 (simplify
1144  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1145   (if (! FLOAT_TYPE_P (type)
1146        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1147    (lshift @0 @2)))
1149 /* Shifts by precision or greater result in zero.  */
1150 (for shift (lshift rshift)
1151  (simplify
1152   (shift @0 uniform_integer_cst_p@1)
1153   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1154        /* Leave arithmetic right shifts of possibly negative values alone.  */
1155        && (TYPE_UNSIGNED (type)
1156            || shift == LSHIFT_EXPR
1157            || tree_expr_nonnegative_p (@0))
1158        /* Use a signed compare to leave negative shift counts alone.  */
1159        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1160                      element_precision (type)))
1161    { build_zero_cst (type); })))
1163 /* Shifts by constants distribute over several binary operations,
1164    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1165 (for op (plus minus)
1166   (simplify
1167     (op (lshift:s @0 @1) (lshift:s @2 @1))
1168     (if (INTEGRAL_TYPE_P (type)
1169          && TYPE_OVERFLOW_WRAPS (type)
1170          && !TYPE_SATURATING (type))
1171       (lshift (op @0 @2) @1))))
1173 (for op (bit_and bit_ior bit_xor)
1174   (simplify
1175     (op (lshift:s @0 @1) (lshift:s @2 @1))
1176     (if (INTEGRAL_TYPE_P (type))
1177       (lshift (op @0 @2) @1)))
1178   (simplify
1179     (op (rshift:s @0 @1) (rshift:s @2 @1))
1180     (if (INTEGRAL_TYPE_P (type))
1181       (rshift (op @0 @2) @1))))
1183 /* Fold (1 << (C - x)) where C = precision(type) - 1
1184    into ((1 << C) >> x). */
1185 (simplify
1186  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1187   (if (INTEGRAL_TYPE_P (type)
1188        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1189        && single_use (@1))
1190    (if (TYPE_UNSIGNED (type))
1191      (rshift (lshift @0 @2) @3)
1192    (with
1193     { tree utype = unsigned_type_for (type); }
1194     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1196 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1197 (simplify
1198  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1199  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1200       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1201   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1202    (bit_and (convert @0)
1203             { wide_int_to_tree (type,
1204                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1206 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1207 (for cst (INTEGER_CST VECTOR_CST)
1208  (simplify
1209   (rshift (negate:s @0) cst@1)
1210    (if (!TYPE_UNSIGNED (type)
1211         && TYPE_OVERFLOW_UNDEFINED (type))
1212     (with { tree stype = TREE_TYPE (@1);
1213             tree bt = truth_type_for (type);
1214             tree zeros = build_zero_cst (type);
1215             tree cst = NULL_TREE; }
1216      (switch
1217       /* Handle scalar case.  */
1218       (if (INTEGRAL_TYPE_P (type)
1219            /* If we apply the rule to the scalar type before vectorization
1220               we will enforce the result of the comparison being a bool
1221               which will require an extra AND on the result that will be
1222               indistinguishable from when the user did actually want 0
1223               or 1 as the result so it can't be removed.  */
1224            && canonicalize_math_after_vectorization_p ()
1225            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1226        (negate (convert (gt @0 { zeros; }))))
1227       /* Handle vector case.  */
1228       (if (VECTOR_INTEGER_TYPE_P (type)
1229            /* First check whether the target has the same mode for vector
1230               comparison results as it's operands do.  */
1231            && TYPE_MODE (bt) == TYPE_MODE (type)
1232            /* Then check to see if the target is able to expand the comparison
1233               with the given type later on, otherwise we may ICE.  */
1234            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1235            && (cst = uniform_integer_cst_p (@1)) != NULL
1236            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1237        (view_convert (gt:bt @0 { zeros; }))))))))
1239 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1240 (simplify
1241  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1242   (if (flag_associative_math
1243        && single_use (@3))
1244    (with
1245     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1246     (if (tem)
1247      (rdiv { tem; } @1)))))
1249 /* Simplify ~X & X as zero.  */
1250 (simplify
1251  (bit_and (convert? @0) (convert? @1))
1252  (with { bool wascmp; }
1253   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1254        && bitwise_inverted_equal_p (@0, @1, wascmp))
1255    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1257 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1258 (simplify
1259   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1260   (if (TYPE_UNSIGNED (type))
1261     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1263 (for bitop (bit_and bit_ior)
1264      cmp (eq ne)
1265  /* PR35691: Transform
1266     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1267     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1268  (simplify
1269   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1270    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1271         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1272         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1273     (cmp (bit_ior @0 (convert @1)) @2)))
1274  /* Transform:
1275     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1276     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1277  (simplify
1278   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1279    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1280         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1281         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1282     (cmp (bit_and @0 (convert @1)) @2))))
1284 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1285 (simplify
1286  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1287   (minus (bit_xor @0 @1) @1))
1288 (simplify
1289  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1290  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1291   (minus (bit_xor @0 @1) @1)))
1293 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1294 (simplify
1295  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1296   (minus @1 (bit_xor @0 @1)))
1298 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1299 (for op (bit_ior bit_xor plus)
1300  (simplify
1301   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1302   (with { bool wascmp0, wascmp1; }
1303    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1304         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1305         && ((!wascmp0 && !wascmp1)
1306             || element_precision (type) == 1))
1307    (bit_xor @0 @1)))))
1309 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1310 (simplify
1311   (bit_ior:c (bit_xor:c @0 @1) @0)
1312   (bit_ior @0 @1))
1314 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1315 (simplify
1316  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1317  @2)
1319 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1320 (simplify
1321  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1322  (bit_not (bit_and @0 @1)))
1324 /* (~a & b) ^ a  -->   (a | b)   */
1325 (simplify
1326  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1327  (bit_ior @0 @1))
1329 /* (a | b) & ~(a ^ b)  -->  a & b  */
1330 (simplify
1331  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1332  (bit_and @0 @1))
1334 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1335 (simplify
1336  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1337  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1338       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1339   (bit_and @0 @1)))
1341 /* a | ~(a ^ b)  -->  a | ~b  */
1342 (simplify
1343  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1344  (bit_ior @0 (bit_not @1)))
1346 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1347 (simplify
1348  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1349  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1350       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1351   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1353 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1354 (simplify
1355  (bit_ior:c @0 (bit_xor:cs @1 @2))
1356  (with { bool wascmp; }
1357  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1358       && (!wascmp || element_precision (type) == 1))
1359   (bit_ior @0 (bit_not @2)))))
1361 /* a & ~(a ^ b)  -->  a & b  */
1362 (simplify
1363  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1364  (bit_and @0 @1))
1366 /* a & (a == b)  -->  a & b (boolean version of the above). */
1367 (simplify
1368  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1369  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1370       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1371   (bit_and @0 @1)))
1373 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1374 (simplify
1375  (bit_and:c @0 (bit_xor:c @1 @2))
1376  (with { bool wascmp; }
1377  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1378       && (!wascmp || element_precision (type) == 1))
1379   (bit_and @0 @2))))
1381 /* (a | b) | (a &^ b)  -->  a | b  */
1382 (for op (bit_and bit_xor)
1383  (simplify
1384   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1385   @2))
1387 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1388 (simplify
1389  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1390  @2)
1392 /* (a & b) | (a == b)  -->  a == b  */
1393 (simplify
1394  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1395  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1396       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1397   @2))
1399 /* ~(~a & b)  -->  a | ~b  */
1400 (simplify
1401  (bit_not (bit_and:cs (bit_not @0) @1))
1402  (bit_ior @0 (bit_not @1)))
1404 /* ~(~a | b) --> a & ~b */
1405 (simplify
1406  (bit_not (bit_ior:cs (bit_not @0) @1))
1407  (bit_and @0 (bit_not @1)))
1409 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1410 (simplify
1411  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1412  (bit_and @3 (bit_not @2)))
1414 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1415 (simplify
1416  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1417  (bit_ior @3 @2))
1419 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1420 (simplify
1421  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1422   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1424 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1425 (simplify
1426  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1427   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1429 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1430 (simplify
1431  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1432  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1433       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1434   (bit_xor @0 @1)))
1436 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1437    ((A & N) + B) & M -> (A + B) & M
1438    Similarly if (N & M) == 0,
1439    ((A | N) + B) & M -> (A + B) & M
1440    and for - instead of + (or unary - instead of +)
1441    and/or ^ instead of |.
1442    If B is constant and (B & M) == 0, fold into A & M.  */
1443 (for op (plus minus)
1444  (for bitop (bit_and bit_ior bit_xor)
1445   (simplify
1446    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1447     (with
1448      { tree pmop[2];
1449        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1450                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1451                                        NULL_TREE, pmop); }
1452      (if (utype)
1453       (convert (bit_and (op (convert:utype { pmop[0]; })
1454                             (convert:utype { pmop[1]; }))
1455                         (convert:utype @2))))))
1456   (simplify
1457    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1458     (with
1459      { tree pmop[2];
1460        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1461                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1462                                        @4, pmop); }
1463      (if (utype)
1464       (convert (bit_and (op (convert:utype { pmop[0]; })
1465                             (convert:utype { pmop[1]; }))
1466                         (convert:utype @2)))))))
1467  (simplify
1468   (bit_and (op:s @0 @1) INTEGER_CST@2)
1469    (with
1470     { tree pmop[2];
1471       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1472                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1473                                       NULL_TREE, NULL_TREE, pmop); }
1474     (if (utype)
1475      (convert (bit_and (op (convert:utype { pmop[0]; })
1476                            (convert:utype { pmop[1]; }))
1477                        (convert:utype @2)))))))
1478 (for bitop (bit_and bit_ior bit_xor)
1479  (simplify
1480   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1481    (with
1482     { tree pmop[2];
1483       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1484                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1485                                       NULL_TREE, NULL_TREE, pmop); }
1486     (if (utype)
1487      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1488                        (convert:utype @1)))))))
1490 /* X % Y is smaller than Y.  */
1491 (for cmp (lt ge)
1492  (simplify
1493   (cmp:c (trunc_mod @0 @1) @1)
1494   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1495    { constant_boolean_node (cmp == LT_EXPR, type); })))
1497 /* x | ~0 -> ~0  */
1498 (simplify
1499  (bit_ior @0 integer_all_onesp@1)
1500  @1)
1502 /* x | 0 -> x  */
1503 (simplify
1504  (bit_ior @0 integer_zerop)
1505  @0)
1507 /* x & 0 -> 0  */
1508 (simplify
1509  (bit_and @0 integer_zerop@1)
1510  @1)
1512 /* ~x | x -> -1 */
1513 /* ~x ^ x -> -1 */
1514 (for op (bit_ior bit_xor)
1515  (simplify
1516   (op (convert? @0) (convert? @1))
1517   (with { bool wascmp; }
1518    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1519         && bitwise_inverted_equal_p (@0, @1, wascmp))
1520     (convert
1521      { wascmp
1522         ? constant_boolean_node (true, type)
1523         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1525 /* x ^ x -> 0 */
1526 (simplify
1527   (bit_xor @0 @0)
1528   { build_zero_cst (type); })
1530 /* Canonicalize X ^ ~0 to ~X.  */
1531 (simplify
1532   (bit_xor @0 integer_all_onesp@1)
1533   (bit_not @0))
1535 /* x & ~0 -> x  */
1536 (simplify
1537  (bit_and @0 integer_all_onesp)
1538   (non_lvalue @0))
1540 /* x & x -> x,  x | x -> x  */
1541 (for bitop (bit_and bit_ior)
1542  (simplify
1543   (bitop @0 @0)
1544   (non_lvalue @0)))
1546 /* x & C -> x if we know that x & ~C == 0.  */
1547 #if GIMPLE
1548 (simplify
1549  (bit_and SSA_NAME@0 INTEGER_CST@1)
1550  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1551       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1552   @0))
1554 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1555 (simplify
1556  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1557  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1558       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1559   @0))
1561 /* x | C -> C if we know that x & ~C == 0.  */
1562 (simplify
1563  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1564  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1565       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1566   @1))
1567 #endif
1569 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1570 (simplify
1571  (bit_not (minus (bit_not @0) @1))
1572  (plus @0 @1))
1573 (simplify
1574  (bit_not (plus:c (bit_not @0) @1))
1575  (minus @0 @1))
1576 /* (~X - ~Y) -> Y - X.  */
1577 (simplify
1578  (minus (bit_not @0) (bit_not @1))
1579   (if (!TYPE_OVERFLOW_SANITIZED (type))
1580    (with { tree utype = unsigned_type_for (type); }
1581     (convert (minus (convert:utype @1) (convert:utype @0))))))
1583 /* ~(X - Y) -> ~X + Y.  */
1584 (simplify
1585  (bit_not (minus:s @0 @1))
1586  (plus (bit_not @0) @1))
1587 (simplify
1588  (bit_not (plus:s @0 INTEGER_CST@1))
1589  (if ((INTEGRAL_TYPE_P (type)
1590        && TYPE_UNSIGNED (type))
1591       || (!TYPE_OVERFLOW_SANITIZED (type)
1592           && may_negate_without_overflow_p (@1)))
1593   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1595 #if GIMPLE
1596 /* ~X + Y -> (Y - X) - 1.  */
1597 (simplify
1598  (plus:c (bit_not @0) @1)
1599   (if (ANY_INTEGRAL_TYPE_P (type)
1600        && TYPE_OVERFLOW_WRAPS (type)
1601        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1602        && !integer_all_onesp (@1))
1603    (plus (minus @1 @0) { build_minus_one_cst (type); })
1604    (if (INTEGRAL_TYPE_P (type)
1605         && TREE_CODE (@1) == INTEGER_CST
1606         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1607                                               SIGNED))
1608     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1609 #endif
1611 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1612 (simplify
1613  (bit_not (rshift:s @0 @1))
1614   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1615    (rshift (bit_not! @0) @1)
1616    /* For logical right shifts, this is possible only if @0 doesn't
1617       have MSB set and the logical right shift is changed into
1618       arithmetic shift.  */
1619    (if (INTEGRAL_TYPE_P (type)
1620         && !wi::neg_p (tree_nonzero_bits (@0)))
1621     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1622      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1624 /* x + (x & 1) -> (x + 1) & ~1 */
1625 (simplify
1626  (plus:c @0 (bit_and:s @0 integer_onep@1))
1627  (bit_and (plus @0 @1) (bit_not @1)))
1629 /* x & ~(x & y) -> x & ~y */
1630 /* x | ~(x | y) -> x | ~y  */
1631 (for bitop (bit_and bit_ior)
1632  (simplify
1633   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1634   (bitop @0 (bit_not @1))))
1636 /* (~x & y) | ~(x | y) -> ~x */
1637 (simplify
1638  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1639  @2)
1641 /* (x | y) ^ (x | ~y) -> ~x */
1642 (simplify
1643  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1644  (bit_not @0))
1646 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1647 (simplify
1648  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1649  (bit_not (bit_xor @0 @1)))
1651 /* (~x | y) ^ (x ^ y) -> x | ~y */
1652 (simplify
1653  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1654  (bit_ior @0 (bit_not @1)))
1656 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1657 (simplify
1658  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1659  (bit_not (bit_and @0 @1)))
1661 /* (x & y) ^ (x | y) -> x ^ y */
1662 (simplify
1663  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1664  (bit_xor @0 @1))
1666 /* (x ^ y) ^ (x | y) -> x & y */
1667 (simplify
1668  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1669  (bit_and @0 @1))
1671 /* (x & y) + (x ^ y) -> x | y */
1672 /* (x & y) | (x ^ y) -> x | y */
1673 /* (x & y) ^ (x ^ y) -> x | y */
1674 (for op (plus bit_ior bit_xor)
1675  (simplify
1676   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1677   (bit_ior @0 @1)))
1679 /* (x & y) + (x | y) -> x + y */
1680 (simplify
1681  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1682  (plus @0 @1))
1684 /* (x + y) - (x | y) -> x & y */
1685 (simplify
1686  (minus (plus @0 @1) (bit_ior @0 @1))
1687  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1688       && !TYPE_SATURATING (type))
1689   (bit_and @0 @1)))
1691 /* (x + y) - (x & y) -> x | y */
1692 (simplify
1693  (minus (plus @0 @1) (bit_and @0 @1))
1694  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1695       && !TYPE_SATURATING (type))
1696   (bit_ior @0 @1)))
1698 /* (x | y) - y -> (x & ~y) */
1699 (simplify
1700  (minus (bit_ior:cs @0 @1) @1)
1701  (bit_and @0 (bit_not @1)))
1703 /* (x | y) - (x ^ y) -> x & y */
1704 (simplify
1705  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1706  (bit_and @0 @1))
1708 /* (x | y) - (x & y) -> x ^ y */
1709 (simplify
1710  (minus (bit_ior @0 @1) (bit_and @0 @1))
1711  (bit_xor @0 @1))
1713 /* (x | y) & ~(x & y) -> x ^ y */
1714 (simplify
1715  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1716  (bit_xor @0 @1))
1718 /* (x | y) & (~x ^ y) -> x & y */
1719 (simplify
1720  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1721  (with { bool wascmp; }
1722   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1723        && (!wascmp || element_precision (type) == 1))
1724    (bit_and @0 @1))))
1726 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1727 (simplify
1728  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1729  (bit_not (bit_xor @0 @1)))
1731 /* (~x | y) ^ (x | ~y) -> x ^ y */
1732 (simplify
1733  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1734  (bit_xor @0 @1))
1736 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1737 (simplify
1738  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1739                               (nop_convert2? (bit_ior @0 @1))))
1740        integer_all_onesp)
1741  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1742       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1743       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1744       && !TYPE_SATURATING (TREE_TYPE (@2)))
1745  (bit_not (convert (bit_xor @0 @1)))))
1746 (simplify
1747  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1748                                integer_all_onesp))
1749        (nop_convert3? (bit_ior @0 @1)))
1750  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1751       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1752       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1753       && !TYPE_SATURATING (TREE_TYPE (@2)))
1754  (bit_not (convert (bit_xor @0 @1)))))
1755 (simplify
1756  (minus (nop_convert1? (bit_and @0 @1))
1757        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1758                                integer_onep)))
1759  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1760       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1761       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1762       && !TYPE_SATURATING (TREE_TYPE (@2)))
1763  (bit_not (convert (bit_xor @0 @1)))))
1765 /* ~x & ~y -> ~(x | y)
1766    ~x | ~y -> ~(x & y) */
1767 (for op (bit_and bit_ior)
1768      rop (bit_ior bit_and)
1769  (simplify
1770   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1771   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1772        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1773    (bit_not (rop (convert @0) (convert @1))))))
1775 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1776    with a constant, and the two constants have no bits in common,
1777    we should treat this as a BIT_IOR_EXPR since this may produce more
1778    simplifications.  */
1779 (for op (bit_xor plus)
1780  (simplify
1781   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1782       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1783   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1784        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1785        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1786    (bit_ior (convert @4) (convert @5)))))
1788 /* (X | Y) ^ X -> Y & ~ X*/
1789 (simplify
1790  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1791  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1792   (convert (bit_and @1 (bit_not @0)))))
1794 /* (~X | Y) ^ X -> ~(X & Y).  */
1795 (simplify
1796  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1797  (if (bitwise_equal_p (@0, @2))
1798   (convert (bit_not (bit_and @0 (convert @1))))))
1800 /* Convert ~X ^ ~Y to X ^ Y.  */
1801 (simplify
1802  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1803  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1804       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1805   (bit_xor (convert @0) (convert @1))))
1807 /* Convert ~X ^ C to X ^ ~C.  */
1808 (simplify
1809  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1810  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1811   (bit_xor (convert @0) (bit_not @1))))
1813 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1814 (for opo (bit_and bit_xor)
1815      opi (bit_xor bit_and)
1816  (simplify
1817   (opo:c (opi:cs @0 @1) @1)
1818   (bit_and (bit_not @0) @1)))
1820 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1821    operands are another bit-wise operation with a common input.  If so,
1822    distribute the bit operations to save an operation and possibly two if
1823    constants are involved.  For example, convert
1824      (A | B) & (A | C) into A | (B & C)
1825    Further simplification will occur if B and C are constants.  */
1826 (for op (bit_and bit_ior bit_xor)
1827      rop (bit_ior bit_and bit_and)
1828  (simplify
1829   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1830   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1831        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1832    (rop (convert @0) (op (convert @1) (convert @2))))))
1834 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1835 /* (X & Y) & Y -> X & Y
1836    (X | Y) | Y -> X | Y  */
1837 (for op (bit_and bit_ior)
1838  (simplify
1839   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1840   @2))
1841 /* (X ^ Y) ^ Y -> X  */
1842 (simplify
1843  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1844  (convert @0))
1846 /* (X & ~Y) & Y -> 0 */
1847 (simplify
1848  (bit_and:c (bit_and @0 @1) @2)
1849  (with { bool wascmp; }
1850   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1851        || bitwise_inverted_equal_p (@1, @2, wascmp))
1852    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1853 /* (X | ~Y) | Y -> -1 */
1854 (simplify
1855  (bit_ior:c (bit_ior @0 @1) @2)
1856  (with { bool wascmp; }
1857   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1858         || bitwise_inverted_equal_p (@1, @2, wascmp))
1859        && (!wascmp || element_precision (type) == 1))
1860    { build_all_ones_cst (TREE_TYPE (@0)); })))
1862 /* (X & Y) & (X & Z) -> (X & Y) & Z
1863    (X | Y) | (X | Z) -> (X | Y) | Z  */
1864 (for op (bit_and bit_ior)
1865  (simplify
1866   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1867   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1868        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1869    (if (single_use (@5) && single_use (@6))
1870     (op @3 (convert @2))
1871     (if (single_use (@3) && single_use (@4))
1872      (op (convert @1) @5))))))
1873 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1874 (simplify
1875  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1876  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1877       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1878   (bit_xor (convert @1) (convert @2))))
1880 /* Convert abs (abs (X)) into abs (X).
1881    also absu (absu (X)) into absu (X).  */
1882 (simplify
1883  (abs (abs@1 @0))
1884  @1)
1886 (simplify
1887  (absu (convert@2 (absu@1 @0)))
1888  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1889   @1))
1891 /* Convert abs[u] (-X) -> abs[u] (X).  */
1892 (simplify
1893  (abs (negate @0))
1894  (abs @0))
1896 (simplify
1897  (absu (negate @0))
1898  (absu @0))
1900 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1901 (simplify
1902  (abs tree_expr_nonnegative_p@0)
1903  @0)
1905 (simplify
1906  (absu tree_expr_nonnegative_p@0)
1907  (convert @0))
1909 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1910 (simplify
1911  (mult:c (nop_convert1?
1912           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1913                     integer_onep))
1914          (nop_convert3? @0))
1915  (if (INTEGRAL_TYPE_P (type)
1916       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1917       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1918   (if (TYPE_UNSIGNED (type))
1919    (absu @0)
1920    (abs @0)
1921   )
1925 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1926 (match negate_expr_p
1927  INTEGER_CST
1928  (if ((INTEGRAL_TYPE_P (type)
1929        && TYPE_UNSIGNED (type))
1930       || (!TYPE_OVERFLOW_SANITIZED (type)
1931           && may_negate_without_overflow_p (t)))))
1932 (match negate_expr_p
1933  FIXED_CST)
1934 (match negate_expr_p
1935  (negate @0)
1936  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1937 (match negate_expr_p
1938  REAL_CST
1939  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1940 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1941    ways.  */
1942 (match negate_expr_p
1943  VECTOR_CST
1944  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1945 (match negate_expr_p
1946  (minus @0 @1)
1947  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1948       || (FLOAT_TYPE_P (type)
1949           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1950           && !HONOR_SIGNED_ZEROS (type)))))
1952 /* (-A) * (-B) -> A * B  */
1953 (simplify
1954  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1955   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1956        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1957    (mult (convert @0) (convert (negate @1)))))
1959 /* -(A + B) -> (-B) - A.  */
1960 (simplify
1961  (negate (plus:c @0 negate_expr_p@1))
1962  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1963       && !HONOR_SIGNED_ZEROS (type))
1964   (minus (negate @1) @0)))
1966 /* -(A - B) -> B - A.  */
1967 (simplify
1968  (negate (minus @0 @1))
1969  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1970       || (FLOAT_TYPE_P (type)
1971           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1972           && !HONOR_SIGNED_ZEROS (type)))
1973   (minus @1 @0)))
1974 (simplify
1975  (negate (pointer_diff @0 @1))
1976  (if (TYPE_OVERFLOW_UNDEFINED (type))
1977   (pointer_diff @1 @0)))
1979 /* A - B -> A + (-B) if B is easily negatable.  */
1980 (simplify
1981  (minus @0 negate_expr_p@1)
1982  (if (!FIXED_POINT_TYPE_P (type))
1983  (plus @0 (negate @1))))
1985 /* 1 - a is a ^ 1 if a had a bool range. */
1986 /* This is only enabled for gimple as sometimes
1987    cfun is not set for the function which contains
1988    the SSA_NAME (e.g. while IPA passes are happening,
1989    fold might be called).  */
1990 (simplify
1991  (minus integer_onep@0 SSA_NAME@1)
1992   (if (INTEGRAL_TYPE_P (type)
1993        && ssa_name_has_boolean_range (@1))
1994    (bit_xor @1 @0)))
1996 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1997 (simplify
1998  (negate (mult:c@0 @1 negate_expr_p@2))
1999  (if (! TYPE_UNSIGNED (type)
2000       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2001       && single_use (@0))
2002   (mult @1 (negate @2))))
2004 (simplify
2005  (negate (rdiv@0 @1 negate_expr_p@2))
2006  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2007       && single_use (@0))
2008   (rdiv @1 (negate @2))))
2010 (simplify
2011  (negate (rdiv@0 negate_expr_p@1 @2))
2012  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2013       && single_use (@0))
2014   (rdiv (negate @1) @2)))
2016 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2017 (simplify
2018  (negate (convert? (rshift @0 INTEGER_CST@1)))
2019  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2020       && wi::to_wide (@1) == element_precision (type) - 1)
2021   (with { tree stype = TREE_TYPE (@0);
2022           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2023                                              : unsigned_type_for (stype); }
2024    (if (VECTOR_TYPE_P (type))
2025     (view_convert (rshift (view_convert:ntype @0) @1))
2026     (convert (rshift (convert:ntype @0) @1))))))
2028 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2029    when profitable.
2030    For bitwise binary operations apply operand conversions to the
2031    binary operation result instead of to the operands.  This allows
2032    to combine successive conversions and bitwise binary operations.
2033    We combine the above two cases by using a conditional convert.  */
2034 (for bitop (bit_and bit_ior bit_xor)
2035  (simplify
2036   (bitop (convert@2 @0) (convert?@3 @1))
2037   (if (((TREE_CODE (@1) == INTEGER_CST
2038          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2039          && (int_fits_type_p (@1, TREE_TYPE (@0))
2040              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2041         || types_match (@0, @1))
2042        && !POINTER_TYPE_P (TREE_TYPE (@0))
2043        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2044        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2045        /* ???  This transform conflicts with fold-const.cc doing
2046           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2047           constants (if x has signed type, the sign bit cannot be set
2048           in c).  This folds extension into the BIT_AND_EXPR.
2049           Restrict it to GIMPLE to avoid endless recursions.  */
2050        && (bitop != BIT_AND_EXPR || GIMPLE)
2051        && (/* That's a good idea if the conversion widens the operand, thus
2052               after hoisting the conversion the operation will be narrower.
2053               It is also a good if the conversion is a nop as moves the
2054               conversion to one side; allowing for combining of the conversions.  */
2055            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2056            /* The conversion check for being a nop can only be done at the gimple
2057               level as fold_binary has some re-association code which can conflict
2058               with this if there is a "constant" which is not a full INTEGER_CST.  */
2059            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2060            /* It's also a good idea if the conversion is to a non-integer
2061               mode.  */
2062            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2063            /* Or if the precision of TO is not the same as the precision
2064               of its mode.  */
2065            || !type_has_mode_precision_p (type)
2066            /* In GIMPLE, getting rid of 2 conversions for one new results
2067               in smaller IL.  */
2068            || (GIMPLE
2069                && TREE_CODE (@1) != INTEGER_CST
2070                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2071                && single_use (@2)
2072                && single_use (@3))))
2073    (convert (bitop @0 (convert @1)))))
2074  /* In GIMPLE, getting rid of 2 conversions for one new results
2075     in smaller IL.  */
2076  (simplify
2077   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2078   (if (GIMPLE
2079        && TREE_CODE (@1) != INTEGER_CST
2080        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2081        && types_match (type, @0)
2082        && !POINTER_TYPE_P (TREE_TYPE (@0))
2083        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2084    (bitop @0 (convert @1)))))
2086 (for bitop (bit_and bit_ior)
2087      rbitop (bit_ior bit_and)
2088   /* (x | y) & x -> x */
2089   /* (x & y) | x -> x */
2090  (simplify
2091   (bitop:c (rbitop:c @0 @1) @0)
2092   @0)
2093  /* (~x | y) & x -> x & y */
2094  /* (~x & y) | x -> x | y */
2095  (simplify
2096   (bitop:c (rbitop:c @2 @1) @0)
2097   (with { bool wascmp; }
2098    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2099         && (!wascmp || element_precision (type) == 1))
2100     (bitop @0 @1))))
2101   /* (x | y) & (x & z) -> (x & z) */
2102   /* (x & y) | (x | z) -> (x | z) */
2103  (simplify
2104   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2105   @3)
2106  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2107  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2108  (simplify
2109   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2110   (bitop @0 @3))
2111  /* x & ~(y | x) -> 0 */
2112  /* x | ~(y & x) -> -1 */
2113  (simplify
2114   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2115   (if (bitop == BIT_AND_EXPR)
2116    { build_zero_cst (type); }
2117    { build_minus_one_cst (type); })))
2119 /* ((x | y) & z) | x -> (z & y) | x
2120    ((x ^ y) & z) | x -> (z & y) | x  */
2121 (for op (bit_ior bit_xor)
2122  (simplify
2123   (bit_ior:c (nop_convert1?:s
2124                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2125   (if (bitwise_equal_p (@0, @3))
2126    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2128 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2129 (simplify
2130   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2131   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2133 /* Combine successive equal operations with constants.  */
2134 (for bitop (bit_and bit_ior bit_xor)
2135  (simplify
2136   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2137   (if (!CONSTANT_CLASS_P (@0))
2138    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2139       folded to a constant.  */
2140    (bitop @0 (bitop! @1 @2))
2141    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2142       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2143       the values involved are such that the operation can't be decided at
2144       compile time.  Try folding one of @0 or @1 with @2 to see whether
2145       that combination can be decided at compile time.
2147       Keep the existing form if both folds fail, to avoid endless
2148       oscillation.  */
2149    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2150     (if (cst1)
2151      (bitop @1 { cst1; })
2152      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2153       (if (cst2)
2154        (bitop @0 { cst2; }))))))))
2156 /* Try simple folding for X op !X, and X op X with the help
2157    of the truth_valued_p and logical_inverted_value predicates.  */
2158 (match truth_valued_p
2159  @0
2160  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2161 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2162  (match truth_valued_p
2163   (op @0 @1)))
2164 (match truth_valued_p
2165   (truth_not @0))
2167 (match (logical_inverted_value @0)
2168  (truth_not @0))
2169 (match (logical_inverted_value @0)
2170  (bit_not truth_valued_p@0))
2171 (match (logical_inverted_value @0)
2172  (eq @0 integer_zerop))
2173 (match (logical_inverted_value @0)
2174  (ne truth_valued_p@0 integer_truep))
2175 (match (logical_inverted_value @0)
2176  (bit_xor truth_valued_p@0 integer_truep))
2178 /* X & !X -> 0.  */
2179 (simplify
2180  (bit_and:c @0 (logical_inverted_value @0))
2181  { build_zero_cst (type); })
2182 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2183 (for op (bit_ior bit_xor)
2184  (simplify
2185   (op:c truth_valued_p@0 (logical_inverted_value @0))
2186   { constant_boolean_node (true, type); }))
2187 /* X ==/!= !X is false/true.  */
2188 (for op (eq ne)
2189  (simplify
2190   (op:c truth_valued_p@0 (logical_inverted_value @0))
2191   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2193 /* ~~x -> x */
2194 (simplify
2195   (bit_not (bit_not @0))
2196   @0)
2198 /* zero_one_valued_p will match when a value is known to be either
2199    0 or 1 including constants 0 or 1.
2200    Signed 1-bits includes -1 so they cannot match here. */
2201 (match zero_one_valued_p
2202  @0
2203  (if (INTEGRAL_TYPE_P (type)
2204       && (TYPE_UNSIGNED (type)
2205           || TYPE_PRECISION (type) > 1)
2206       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2207 (match zero_one_valued_p
2208  truth_valued_p@0
2209  (if (INTEGRAL_TYPE_P (type)
2210       && (TYPE_UNSIGNED (type)
2211           || TYPE_PRECISION (type) > 1))))
2213 /* (a&1) is always [0,1] too. This is useful again when
2214    the range is not known. */
2215 /* Note this can't be recursive due to VN handling of equivalents,
2216    VN and would cause an infinite recursion. */
2217 (match zero_one_valued_p
2218  (bit_and:c@0 @1 integer_onep)
2219  (if (INTEGRAL_TYPE_P (type))))
2221 /* A conversion from an zero_one_valued_p is still a [0,1].
2222    This is useful when the range of a variable is not known */
2223 /* Note this matches can't be recursive because of the way VN handles
2224    nop conversions being equivalent and then recursive between them. */
2225 (match zero_one_valued_p
2226  (convert@0 @1)
2227  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2228       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2229           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2230       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2232 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2233 (simplify
2234  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2235  (if (INTEGRAL_TYPE_P (type))
2236   (bit_and @0 @1)))
2238 (for cmp (tcc_comparison)
2239      icmp (inverted_tcc_comparison)
2240  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2241  (simplify
2242   (bit_ior
2243    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2244    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2245     (if (INTEGRAL_TYPE_P (type)
2246          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2247          /* The scalar version has to be canonicalized after vectorization
2248             because it makes unconditional loads conditional ones, which
2249             means we lose vectorization because the loads may trap.  */
2250          && canonicalize_math_after_vectorization_p ())
2251      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2253  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2254     canonicalized further and we recognize the conditional form:
2255     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2256  (simplify
2257   (bit_ior
2258    (cond (cmp@0  @01 @02) @3 zerop)
2259    (cond (icmp@4 @01 @02) @5 zerop))
2260     (if (INTEGRAL_TYPE_P (type)
2261          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2262          /* The scalar version has to be canonicalized after vectorization
2263             because it makes unconditional loads conditional ones, which
2264             means we lose vectorization because the loads may trap.  */
2265          && canonicalize_math_after_vectorization_p ())
2266     (cond @0 @3 @5)))
2268  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2269     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2270  (simplify
2271   (bit_ior
2272    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2273    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2274     (if (integer_zerop (@5)
2275          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2276      (switch
2277       (if (integer_onep (@4))
2278        (bit_and (vec_cond @0 @2 @3) @4))
2279         (if (integer_minus_onep (@4))
2280          (vec_cond @0 @2 @3)))
2281     (if (integer_zerop (@4)
2282          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2283      (switch
2284       (if (integer_onep (@5))
2285        (bit_and (vec_cond @0 @3 @2) @5))
2286       (if (integer_minus_onep (@5))
2287        (vec_cond @0 @3 @2))))))
2289  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2290     into a < b ? d : c.  */
2291  (simplify
2292   (bit_ior
2293    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2294    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2295   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2296    (vec_cond @0 @2 @3))))
2298 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2299 (simplify
2300  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2301  (if (INTEGRAL_TYPE_P (type)
2302       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2303       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2304       /* Sign extending of the neg or a truncation of the neg
2305          is needed. */
2306       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2307           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2308   (mult (convert @0) @1)))
2310 /* Narrow integer multiplication by a zero_one_valued_p operand.
2311    Multiplication by [0,1] is guaranteed not to overflow.  */
2312 (simplify
2313  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2314  (if (INTEGRAL_TYPE_P (type)
2315       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2316       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2317   (mult (convert @1) (convert @2))))
2319 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2320    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2321    as some targets (such as x86's SSE) may return zero for larger C.  */
2322 (simplify
2323   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2324   (if (tree_fits_shwi_p (@1)
2325        && tree_to_shwi (@1) > 0
2326        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2327     (convert @0)))
2329 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2330    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2331    as some targets (such as x86's SSE) may return zero for larger C.  */
2332 (simplify
2333   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2334   (if (tree_fits_shwi_p (@1)
2335        && tree_to_shwi (@1) > 0
2336        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2337     (eq @0 @2)))
2339 /* Convert ~ (-A) to A - 1.  */
2340 (simplify
2341  (bit_not (convert? (negate @0)))
2342  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2343       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2344   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2346 /* Convert - (~A) to A + 1.  */
2347 (simplify
2348  (negate (nop_convert? (bit_not @0)))
2349  (plus (view_convert @0) { build_each_one_cst (type); }))
2351 /* (a & b) ^ (a == b) -> !(a | b) */
2352 /* (a & b) == (a ^ b) -> !(a | b) */
2353 (for first_op (bit_xor eq)
2354      second_op (eq bit_xor)
2355  (simplify
2356   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2357     (bit_not (bit_ior @0 @1))))
2359 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2360 (simplify
2361  (bit_not (convert? (minus @0 integer_each_onep)))
2362  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2363       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2364   (convert (negate @0))))
2365 (simplify
2366  (bit_not (convert? (plus @0 integer_all_onesp)))
2367  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2368       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2369   (convert (negate @0))))
2371 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2372 (simplify
2373  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2374  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2375   (convert (bit_xor @0 (bit_not @1)))))
2376 (simplify
2377  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2378  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2379   (convert (bit_xor @0 @1))))
2381 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2382 (simplify
2383  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2384  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2385   (bit_not (bit_xor (view_convert @0) @1))))
2387 /* ~(a ^ b) is a == b for truth valued a and b.  */
2388 (simplify
2389  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2390  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2391       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2392   (convert (eq @0 @1))))
2394 /* (~a) == b is a ^ b for truth valued a and b.  */
2395 (simplify
2396  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2397  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2398       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2399   (convert (bit_xor @0 @1))))
2401 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2402 (simplify
2403  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2404  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2406 /* Fold A - (A & B) into ~B & A.  */
2407 (simplify
2408  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2409  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2410       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2411   (convert (bit_and (bit_not @1) @0))))
2413 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2414 (if (!canonicalize_math_p ())
2415  (for cmp (tcc_comparison)
2416   (simplify
2417    (mult:c (convert (cmp@0 @1 @2)) @3)
2418    (if (INTEGRAL_TYPE_P (type)
2419         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2420      (cond @0 @3 { build_zero_cst (type); })))
2421 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2422   (simplify
2423    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2424    (if (INTEGRAL_TYPE_P (type)
2425         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2426      (cond @0 @3 { build_zero_cst (type); })))
2430 /* For integral types with undefined overflow and C != 0 fold
2431    x * C EQ/NE y * C into x EQ/NE y.  */
2432 (for cmp (eq ne)
2433  (simplify
2434   (cmp (mult:c @0 @1) (mult:c @2 @1))
2435   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2436        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2437        && tree_expr_nonzero_p (@1))
2438    (cmp @0 @2))))
2440 /* For integral types with wrapping overflow and C odd fold
2441    x * C EQ/NE y * C into x EQ/NE y.  */
2442 (for cmp (eq ne)
2443  (simplify
2444   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2445   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2446        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2447        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2448    (cmp @0 @2))))
2450 /* For integral types with undefined overflow and C != 0 fold
2451    x * C RELOP y * C into:
2453    x RELOP y for nonnegative C
2454    y RELOP x for negative C  */
2455 (for cmp (lt gt le ge)
2456  (simplify
2457   (cmp (mult:c @0 @1) (mult:c @2 @1))
2458   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2459        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2460    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2461     (cmp @0 @2)
2462    (if (TREE_CODE (@1) == INTEGER_CST
2463         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2464     (cmp @2 @0))))))
2466 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2467 (for cmp (le gt)
2468      icmp (gt le)
2469  (simplify
2470   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2471    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2472         && TYPE_UNSIGNED (TREE_TYPE (@0))
2473         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2474         && (wi::to_wide (@2)
2475             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2476     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2477      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2479 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2480 (for cmp (simple_comparison)
2481  (simplify
2482   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2483   (if (element_precision (@3) >= element_precision (@0)
2484        && types_match (@0, @1))
2485    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2486     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2487      (cmp @1 @0)
2488      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2489       (with
2490        {
2491         tree utype = unsigned_type_for (TREE_TYPE (@0));
2492        }
2493        (cmp (convert:utype @1) (convert:utype @0)))))
2494     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2495      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2496       (cmp @0 @1)
2497       (with
2498        {
2499         tree utype = unsigned_type_for (TREE_TYPE (@0));
2500        }
2501        (cmp (convert:utype @0) (convert:utype @1)))))))))
2503 /* X / C1 op C2 into a simple range test.  */
2504 (for cmp (simple_comparison)
2505  (simplify
2506   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2507   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2508        && integer_nonzerop (@1)
2509        && !TREE_OVERFLOW (@1)
2510        && !TREE_OVERFLOW (@2))
2511    (with { tree lo, hi; bool neg_overflow;
2512            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2513                                                    &neg_overflow); }
2514     (switch
2515      (if (code == LT_EXPR || code == GE_EXPR)
2516        (if (TREE_OVERFLOW (lo))
2517         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2518         (if (code == LT_EXPR)
2519          (lt @0 { lo; })
2520          (ge @0 { lo; }))))
2521      (if (code == LE_EXPR || code == GT_EXPR)
2522        (if (TREE_OVERFLOW (hi))
2523         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2524         (if (code == LE_EXPR)
2525          (le @0 { hi; })
2526          (gt @0 { hi; }))))
2527      (if (!lo && !hi)
2528       { build_int_cst (type, code == NE_EXPR); })
2529      (if (code == EQ_EXPR && !hi)
2530       (ge @0 { lo; }))
2531      (if (code == EQ_EXPR && !lo)
2532       (le @0 { hi; }))
2533      (if (code == NE_EXPR && !hi)
2534       (lt @0 { lo; }))
2535      (if (code == NE_EXPR && !lo)
2536       (gt @0 { hi; }))
2537      (if (GENERIC)
2538       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2539                            lo, hi); })
2540      (with
2541       {
2542         tree etype = range_check_type (TREE_TYPE (@0));
2543         if (etype)
2544           {
2545             hi = fold_convert (etype, hi);
2546             lo = fold_convert (etype, lo);
2547             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2548           }
2549       }
2550       (if (etype && hi && !TREE_OVERFLOW (hi))
2551        (if (code == EQ_EXPR)
2552         (le (minus (convert:etype @0) { lo; }) { hi; })
2553         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
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 (plus:c @0 @2) (plus:c @1 @2))
2559   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2560        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2561    (op @0 @1))))
2563 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2564    when C is an unsigned integer constant with only the MSB set, and X and
2565    Y have types of equal or lower integer conversion rank than C's.  */
2566 (for op (lt le ge gt)
2567  (simplify
2568   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2569   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2570        && TYPE_UNSIGNED (TREE_TYPE (@0))
2571        && wi::only_sign_bit_p (wi::to_wide (@0)))
2572    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2573     (op (convert:stype @1) (convert:stype @2))))))
2575 /* For equality and subtraction, this is also true with wrapping overflow.  */
2576 (for op (eq ne minus)
2577  (simplify
2578   (op (plus:c @0 @2) (plus:c @1 @2))
2579   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2580        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2581            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2582    (op @0 @1))))
2584 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2585 (for op (lt le ge gt)
2586  (simplify
2587   (op (minus @0 @2) (minus @1 @2))
2588   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2589        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2590    (op @0 @1))))
2591 /* For equality and subtraction, this is also true with wrapping overflow.  */
2592 (for op (eq ne minus)
2593  (simplify
2594   (op (minus @0 @2) (minus @1 @2))
2595   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2596        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2597            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2598    (op @0 @1))))
2599 /* And for pointers...  */
2600 (for op (simple_comparison)
2601  (simplify
2602   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2603   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2604    (op @0 @1))))
2605 (simplify
2606  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2607  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2608       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2609   (pointer_diff @0 @1)))
2611 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2612 (for op (lt le ge gt)
2613  (simplify
2614   (op (minus @2 @0) (minus @2 @1))
2615   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2616        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2617    (op @1 @0))))
2618 /* For equality and subtraction, this is also true with wrapping overflow.  */
2619 (for op (eq ne minus)
2620  (simplify
2621   (op (minus @2 @0) (minus @2 @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    (op @1 @0))))
2626 /* And for pointers...  */
2627 (for op (simple_comparison)
2628  (simplify
2629   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2630   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2631    (op @1 @0))))
2632 (simplify
2633  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2634  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2635       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2636   (pointer_diff @1 @0)))
2638 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2639 (for op (lt le gt ge)
2640  (simplify
2641   (op:c (plus:c@2 @0 @1) @1)
2642   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2643        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2644        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2645        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2646    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2647 /* For equality, this is also true with wrapping overflow.  */
2648 (for op (eq ne)
2649  (simplify
2650   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2651   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2652        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2653            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2654        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2655        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2656        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2657    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2658  (simplify
2659   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2660   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2661        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2662        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2663    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2665 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2666 (for neeq (ne eq)
2667  (simplify
2668   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2669    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2670     (if (ptr_difference_const (@0, @2, &diff))
2671      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2672  (simplify
2673   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2674    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2675     (if (ptr_difference_const (@0, @2, &diff))
2676      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2678 /* X - Y < X is the same as Y > 0 when there is no overflow.
2679    For equality, this is also true with wrapping overflow.  */
2680 (for op (simple_comparison)
2681  (simplify
2682   (op:c @0 (minus@2 @0 @1))
2683   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2684        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2685            || ((op == EQ_EXPR || op == NE_EXPR)
2686                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2687        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2688    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2690 /* Transform:
2691    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2692    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2693 (for cmp (eq ne)
2694      ocmp (lt ge)
2695  (simplify
2696   (cmp (trunc_div @0 @1) integer_zerop)
2697   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2698        /* Complex ==/!= is allowed, but not </>=.  */
2699        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2700        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2701    (ocmp @0 @1))))
2703 /* X == C - X can never be true if C is odd.  */
2704 (for cmp (eq ne)
2705  (simplify
2706   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2707   (if (TREE_INT_CST_LOW (@1) & 1)
2708    { constant_boolean_node (cmp == NE_EXPR, type); })))
2710 /* Arguments on which one can call get_nonzero_bits to get the bits
2711    possibly set.  */
2712 (match with_possible_nonzero_bits
2713  INTEGER_CST@0)
2714 (match with_possible_nonzero_bits
2715  SSA_NAME@0
2716  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2717 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2718 (match (with_possible_nonzero_bits2 @0)
2719  with_possible_nonzero_bits@0)
2720 (match (with_possible_nonzero_bits2 @0)
2721  (bit_and:c with_possible_nonzero_bits@0 @2))
2723 /* Same for bits that are known to be set, but we do not have
2724    an equivalent to get_nonzero_bits yet.  */
2725 (match (with_certain_nonzero_bits2 @0)
2726  INTEGER_CST@0)
2727 (match (with_certain_nonzero_bits2 @0)
2728  (bit_ior @1 INTEGER_CST@0))
2730 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2731 (for cmp (eq ne)
2732  (simplify
2733   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2734   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2735    { constant_boolean_node (cmp == NE_EXPR, type); })))
2737 /* ((X inner_op C0) outer_op C1)
2738    With X being a tree where value_range has reasoned certain bits to always be
2739    zero throughout its computed value range,
2740    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2741    where zero_mask has 1's for all bits that are sure to be 0 in
2742    and 0's otherwise.
2743    if (inner_op == '^') C0 &= ~C1;
2744    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2745    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2747 (for inner_op (bit_ior bit_xor)
2748      outer_op (bit_xor bit_ior)
2749 (simplify
2750  (outer_op
2751   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2752  (with
2753   {
2754     bool fail = false;
2755     wide_int zero_mask_not;
2756     wide_int C0;
2757     wide_int cst_emit;
2759     if (TREE_CODE (@2) == SSA_NAME)
2760       zero_mask_not = get_nonzero_bits (@2);
2761     else
2762       fail = true;
2764     if (inner_op == BIT_XOR_EXPR)
2765       {
2766         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2767         cst_emit = C0 | wi::to_wide (@1);
2768       }
2769     else
2770       {
2771         C0 = wi::to_wide (@0);
2772         cst_emit = C0 ^ wi::to_wide (@1);
2773       }
2774   }
2775   (if (!fail && (C0 & zero_mask_not) == 0)
2776    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2777    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2778     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2780 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2781 (simplify
2782   (pointer_plus (pointer_plus:s @0 @1) @3)
2783   (pointer_plus @0 (plus @1 @3)))
2784 #if GENERIC
2785 (simplify
2786   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2787   (convert:type (pointer_plus @0 (plus @1 @3))))
2788 #endif
2790 /* Pattern match
2791      tem1 = (long) ptr1;
2792      tem2 = (long) ptr2;
2793      tem3 = tem2 - tem1;
2794      tem4 = (unsigned long) tem3;
2795      tem5 = ptr1 + tem4;
2796    and produce
2797      tem5 = ptr2;  */
2798 (simplify
2799   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2800   /* Conditionally look through a sign-changing conversion.  */
2801   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2802        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2803             || (GENERIC && type == TREE_TYPE (@1))))
2804    @1))
2805 (simplify
2806   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2807   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2808    (convert @1)))
2810 /* Pattern match
2811      tem = (sizetype) ptr;
2812      tem = tem & algn;
2813      tem = -tem;
2814      ... = ptr p+ tem;
2815    and produce the simpler and easier to analyze with respect to alignment
2816      ... = ptr & ~algn;  */
2817 (simplify
2818   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2819   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2820    (bit_and @0 { algn; })))
2822 /* Try folding difference of addresses.  */
2823 (simplify
2824  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2825  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2826   (with { poly_int64 diff; }
2827    (if (ptr_difference_const (@0, @1, &diff))
2828     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2829 (simplify
2830  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2831  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2832   (with { poly_int64 diff; }
2833    (if (ptr_difference_const (@0, @1, &diff))
2834     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2835 (simplify
2836  (minus (convert ADDR_EXPR@0) (convert @1))
2837  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2838   (with { poly_int64 diff; }
2839    (if (ptr_difference_const (@0, @1, &diff))
2840     { build_int_cst_type (type, diff); }))))
2841 (simplify
2842  (minus (convert @0) (convert ADDR_EXPR@1))
2843  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2844   (with { poly_int64 diff; }
2845    (if (ptr_difference_const (@0, @1, &diff))
2846     { build_int_cst_type (type, diff); }))))
2847 (simplify
2848  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2849  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2850       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2851   (with { poly_int64 diff; }
2852    (if (ptr_difference_const (@0, @1, &diff))
2853     { build_int_cst_type (type, diff); }))))
2854 (simplify
2855  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2856  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2857       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2858   (with { poly_int64 diff; }
2859    (if (ptr_difference_const (@0, @1, &diff))
2860     { build_int_cst_type (type, diff); }))))
2862 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2863 (simplify
2864  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2865  (with { poly_int64 diff; }
2866    (if (ptr_difference_const (@0, @2, &diff))
2867     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2868 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2869 (simplify
2870  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2871  (with { poly_int64 diff; }
2872    (if (ptr_difference_const (@0, @2, &diff))
2873     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2874 (simplify
2875  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2876  (with { poly_int64 diff; }
2877    (if (ptr_difference_const (@0, @1, &diff))
2878     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2880 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2881 (simplify
2882  (convert (pointer_diff @0 INTEGER_CST@1))
2883  (if (POINTER_TYPE_P (type))
2884   { build_fold_addr_expr_with_type
2885       (build2 (MEM_REF, char_type_node, @0,
2886                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2887                type); }))
2889 /* If arg0 is derived from the address of an object or function, we may
2890    be able to fold this expression using the object or function's
2891    alignment.  */
2892 (simplify
2893  (bit_and (convert? @0) INTEGER_CST@1)
2894  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2895       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2896   (with
2897    {
2898      unsigned int align;
2899      unsigned HOST_WIDE_INT bitpos;
2900      get_pointer_alignment_1 (@0, &align, &bitpos);
2901    }
2902    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2903     { wide_int_to_tree (type, (wi::to_wide (@1)
2904                                & (bitpos / BITS_PER_UNIT))); }))))
2906 (match min_value
2907  uniform_integer_cst_p
2908  (with {
2909    tree int_cst = uniform_integer_cst_p (t);
2910    tree inner_type = TREE_TYPE (int_cst);
2911   }
2912   (if ((INTEGRAL_TYPE_P (inner_type)
2913         || POINTER_TYPE_P (inner_type))
2914        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2916 (match max_value
2917  uniform_integer_cst_p
2918  (with {
2919    tree int_cst = uniform_integer_cst_p (t);
2920    tree itype = TREE_TYPE (int_cst);
2921   }
2922  (if ((INTEGRAL_TYPE_P (itype)
2923        || POINTER_TYPE_P (itype))
2924       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2926 /* x >  y  &&  x != XXX_MIN  -->  x > y
2927    x >  y  &&  x == XXX_MIN  -->  false . */
2928 (for eqne (eq ne)
2929  (simplify
2930   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2931    (switch
2932     (if (eqne == EQ_EXPR)
2933      { constant_boolean_node (false, type); })
2934     (if (eqne == NE_EXPR)
2935      @2)
2936     )))
2938 /* x <  y  &&  x != XXX_MAX  -->  x < y
2939    x <  y  &&  x == XXX_MAX  -->  false.  */
2940 (for eqne (eq ne)
2941  (simplify
2942   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2943    (switch
2944     (if (eqne == EQ_EXPR)
2945      { constant_boolean_node (false, type); })
2946     (if (eqne == NE_EXPR)
2947      @2)
2948     )))
2950 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2951 (simplify
2952  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2953   @2)
2955 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2956 (simplify
2957  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2958   @2)
2960 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2961 (simplify
2962  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2963   @2)
2965 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2966 (simplify
2967  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2968   { constant_boolean_node (true, type); })
2970 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2971 (simplify
2972  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2973   @2)
2975 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2976 (simplify
2977  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2978   @2)
2980 /* x >=  y  ||  x != XXX_MAX   -->  true
2981    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2982 (for eqne (eq ne)
2983  (simplify
2984   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2985    (switch
2986     (if (eqne == EQ_EXPR)
2987      @2)
2988     (if (eqne == NE_EXPR)
2989      { constant_boolean_node (true, type); }))))
2991 /* y == XXX_MIN || x < y --> x <= y - 1 */
2992 (simplify
2993  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2994   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2995        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2996   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2998 /* y != XXX_MIN && x >= y --> x > y - 1 */
2999 (simplify
3000  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3001   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3002        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3003   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3005 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3006    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3007 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3008    Similarly for (X != Y).  */
3010 (for code1 (eq ne)
3011  (for code2 (eq ne lt gt le ge)
3012   (simplify
3013    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3014    (if ((TREE_CODE (@1) == INTEGER_CST
3015          && TREE_CODE (@2) == INTEGER_CST)
3016         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3017              || POINTER_TYPE_P (TREE_TYPE (@1)))
3018             && bitwise_equal_p (@1, @2)))
3019     (with
3020      {
3021       bool one_before = false;
3022       bool one_after = false;
3023       int cmp = 0;
3024       bool allbits = true;
3025       if (TREE_CODE (@1) == INTEGER_CST
3026           && TREE_CODE (@2) == INTEGER_CST)
3027         {
3028           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3029           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3030           auto t2 = wi::to_wide (@2);
3031           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3032           if (cmp < 0
3033               && t1 == t2 - 1)
3034             one_before = true;
3035           if (cmp > 0
3036               && t1 == t2 + 1)
3037             one_after = true;
3038         }
3039       bool val;
3040       switch (code2)
3041          {
3042         case EQ_EXPR: val = (cmp == 0); break;
3043         case NE_EXPR: val = (cmp != 0); break;
3044         case LT_EXPR: val = (cmp < 0); break;
3045         case GT_EXPR: val = (cmp > 0); break;
3046         case LE_EXPR: val = (cmp <= 0); break;
3047         case GE_EXPR: val = (cmp >= 0); break;
3048         default: gcc_unreachable ();
3049         }
3050      }
3051      (switch
3052       (if (code1 == EQ_EXPR && val) @3)
3053       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3054       (if (code1 == NE_EXPR && !val && allbits) @4)
3055       (if (code1 == NE_EXPR
3056            && code2 == GE_EXPR
3057            && cmp == 0
3058            && allbits)
3059        (gt @c0 (convert @1)))
3060       (if (code1 == NE_EXPR
3061            && code2 == LE_EXPR
3062            && cmp == 0
3063            && allbits)
3064        (lt @c0 (convert @1)))
3065       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3066       (if (code1 == NE_EXPR
3067            && code2 == GT_EXPR
3068            && one_after
3069            && allbits)
3070        (gt @c0 (convert @1)))
3071       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3072       (if (code1 == NE_EXPR
3073            && code2 == LT_EXPR
3074            && one_before
3075            && allbits)
3076        (lt @c0 (convert @1)))
3077      )
3078     )
3079    )
3080   )
3084 /* Convert (X OP1 CST1) && (X OP2 CST2).
3085    Convert (X OP1 Y) && (X OP2 Y).  */
3087 (for code1 (lt le gt ge)
3088  (for code2 (lt le gt ge)
3089   (simplify
3090   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3091   (if ((TREE_CODE (@1) == INTEGER_CST
3092         && TREE_CODE (@2) == INTEGER_CST)
3093        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3094             || POINTER_TYPE_P (TREE_TYPE (@1)))
3095            && operand_equal_p (@1, @2)))
3096    (with
3097     {
3098      int cmp = 0;
3099      if (TREE_CODE (@1) == INTEGER_CST
3100          && TREE_CODE (@2) == INTEGER_CST)
3101        cmp = tree_int_cst_compare (@1, @2);
3102     }
3103     (switch
3104      /* Choose the more restrictive of two < or <= comparisons.  */
3105      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3106           && (code2 == LT_EXPR || code2 == LE_EXPR))
3107       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3108        @3
3109        @4))
3110      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3111      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3112           && (code2 == GT_EXPR || code2 == GE_EXPR))
3113       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3114        @3
3115        @4))
3116      /* Check for singleton ranges.  */
3117      (if (cmp == 0
3118           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3119             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3120       (eq @0 @1))
3121      /* Check for disjoint ranges.  */
3122      (if (cmp <= 0
3123           && (code1 == LT_EXPR || code1 == LE_EXPR)
3124           && (code2 == GT_EXPR || code2 == GE_EXPR))
3125       { constant_boolean_node (false, type); })
3126      (if (cmp >= 0
3127           && (code1 == GT_EXPR || code1 == GE_EXPR)
3128           && (code2 == LT_EXPR || code2 == LE_EXPR))
3129       { constant_boolean_node (false, type); })
3130      ))))))
3132 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3133    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3134 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3135    Similarly for (X != Y).  */
3137 (for code1 (eq ne)
3138  (for code2 (eq ne lt gt le ge)
3139   (simplify
3140    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3141    (if ((TREE_CODE (@1) == INTEGER_CST
3142          && TREE_CODE (@2) == INTEGER_CST)
3143         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3144             || POINTER_TYPE_P (TREE_TYPE (@1)))
3145             && bitwise_equal_p (@1, @2)))
3146     (with
3147      {
3148       bool one_before = false;
3149       bool one_after = false;
3150       int cmp = 0;
3151       bool allbits = true;
3152       if (TREE_CODE (@1) == INTEGER_CST
3153           && TREE_CODE (@2) == INTEGER_CST)
3154         {
3155           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3156           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3157           auto t2 = wi::to_wide (@2);
3158           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3159           if (cmp < 0
3160               && t1 == t2 - 1)
3161             one_before = true;
3162           if (cmp > 0
3163               && t1 == t2 + 1)
3164             one_after = true;
3165         }
3166       bool val;
3167       switch (code2)
3168         {
3169         case EQ_EXPR: val = (cmp == 0); break;
3170         case NE_EXPR: val = (cmp != 0); break;
3171         case LT_EXPR: val = (cmp < 0); break;
3172         case GT_EXPR: val = (cmp > 0); break;
3173         case LE_EXPR: val = (cmp <= 0); break;
3174         case GE_EXPR: val = (cmp >= 0); break;
3175         default: gcc_unreachable ();
3176         }
3177      }
3178      (switch
3179       (if (code1 == EQ_EXPR && val) @4)
3180       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3181       (if (code1 == NE_EXPR && !val && allbits) @3)
3182       (if (code1 == EQ_EXPR
3183            && code2 == GT_EXPR
3184            && cmp == 0
3185            && allbits)
3186        (ge @c0 @2))
3187       (if (code1 == EQ_EXPR
3188            && code2 == LT_EXPR
3189            && cmp == 0
3190            && allbits)
3191        (le @c0 @2))
3192       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3193       (if (code1 == EQ_EXPR
3194            && code2 == GE_EXPR
3195            && one_before
3196            && allbits)
3197        (ge @c0 (convert @1)))
3198       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3199       (if (code1 == EQ_EXPR
3200            && code2 == LE_EXPR
3201            && one_after
3202            && allbits)
3203        (le @c0 (convert @1)))
3204      )
3205     )
3206    )
3207   )
3211 /* Convert (X OP1 CST1) || (X OP2 CST2).
3212    Convert (X OP1 Y)    || (X OP2 Y).  */
3214 (for code1 (lt le gt ge)
3215  (for code2 (lt le gt ge)
3216   (simplify
3217   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3218   (if ((TREE_CODE (@1) == INTEGER_CST
3219         && TREE_CODE (@2) == INTEGER_CST)
3220        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3221             || POINTER_TYPE_P (TREE_TYPE (@1)))
3222            && operand_equal_p (@1, @2)))
3223    (with
3224     {
3225      int cmp = 0;
3226      if (TREE_CODE (@1) == INTEGER_CST
3227          && TREE_CODE (@2) == INTEGER_CST)
3228        cmp = tree_int_cst_compare (@1, @2);
3229     }
3230     (switch
3231      /* Choose the more restrictive of two < or <= comparisons.  */
3232      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3233           && (code2 == LT_EXPR || code2 == LE_EXPR))
3234       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3235        @4
3236        @3))
3237      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3238      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3239           && (code2 == GT_EXPR || code2 == GE_EXPR))
3240       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3241        @4
3242        @3))
3243      /* Check for singleton ranges.  */
3244      (if (cmp == 0
3245           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3246               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3247       (ne @0 @2))
3248      /* Check for disjoint ranges.  */
3249      (if (cmp >= 0
3250           && (code1 == LT_EXPR || code1 == LE_EXPR)
3251           && (code2 == GT_EXPR || code2 == GE_EXPR))
3252       { constant_boolean_node (true, type); })
3253      (if (cmp <= 0
3254           && (code1 == GT_EXPR || code1 == GE_EXPR)
3255           && (code2 == LT_EXPR || code2 == LE_EXPR))
3256       { constant_boolean_node (true, type); })
3257      ))))))
3259 /* Optimize (a CMP b) ^ (a CMP b)  */
3260 /* Optimize (a CMP b) != (a CMP b)  */
3261 (for op (bit_xor ne)
3262  (for cmp1 (lt lt lt le le le)
3263       cmp2 (gt eq ne ge eq ne)
3264       rcmp (ne le gt ne lt ge)
3265   (simplify
3266    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3267    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3268     (rcmp @0 @1)))))
3270 /* Optimize (a CMP b) == (a CMP b)  */
3271 (for cmp1 (lt lt lt le le le)
3272      cmp2 (gt eq ne ge eq ne)
3273      rcmp (eq gt le eq ge lt)
3274  (simplify
3275   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3276   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3277     (rcmp @0 @1))))
3279 /* We can't reassociate at all for saturating types.  */
3280 (if (!TYPE_SATURATING (type))
3282  /* Contract negates.  */
3283  /* A + (-B) -> A - B */
3284  (simplify
3285   (plus:c @0 (convert? (negate @1)))
3286   /* Apply STRIP_NOPS on the negate.  */
3287   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3288        && !TYPE_OVERFLOW_SANITIZED (type))
3289    (with
3290     {
3291      tree t1 = type;
3292      if (INTEGRAL_TYPE_P (type)
3293          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3294        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3295     }
3296     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3297  /* A - (-B) -> A + B */
3298  (simplify
3299   (minus @0 (convert? (negate @1)))
3300   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3301        && !TYPE_OVERFLOW_SANITIZED (type))
3302    (with
3303     {
3304      tree t1 = type;
3305      if (INTEGRAL_TYPE_P (type)
3306          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3307        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3308     }
3309     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3310  /* -(T)(-A) -> (T)A
3311     Sign-extension is ok except for INT_MIN, which thankfully cannot
3312     happen without overflow.  */
3313  (simplify
3314   (negate (convert (negate @1)))
3315   (if (INTEGRAL_TYPE_P (type)
3316        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3317            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3318                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3319        && !TYPE_OVERFLOW_SANITIZED (type)
3320        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3321    (convert @1)))
3322  (simplify
3323   (negate (convert negate_expr_p@1))
3324   (if (SCALAR_FLOAT_TYPE_P (type)
3325        && ((DECIMAL_FLOAT_TYPE_P (type)
3326             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3327             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3328            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3329    (convert (negate @1))))
3330  (simplify
3331   (negate (nop_convert? (negate @1)))
3332   (if (!TYPE_OVERFLOW_SANITIZED (type)
3333        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3334    (view_convert @1)))
3336  /* We can't reassociate floating-point unless -fassociative-math
3337     or fixed-point plus or minus because of saturation to +-Inf.  */
3338  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3339       && !FIXED_POINT_TYPE_P (type))
3341   /* Match patterns that allow contracting a plus-minus pair
3342      irrespective of overflow issues.  */
3343   /* (A +- B) - A       ->  +- B */
3344   /* (A +- B) -+ B      ->  A */
3345   /* A - (A +- B)       -> -+ B */
3346   /* A +- (B -+ A)      ->  +- B */
3347   (simplify
3348    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3349    (view_convert @1))
3350   (simplify
3351    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3352    (if (!ANY_INTEGRAL_TYPE_P (type)
3353         || TYPE_OVERFLOW_WRAPS (type))
3354    (negate (view_convert @1))
3355    (view_convert (negate @1))))
3356   (simplify
3357    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3358    (view_convert @0))
3359   (simplify
3360    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3361     (if (!ANY_INTEGRAL_TYPE_P (type)
3362          || TYPE_OVERFLOW_WRAPS (type))
3363      (negate (view_convert @1))
3364      (view_convert (negate @1))))
3365   (simplify
3366    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3367    (view_convert @1))
3368   /* (A +- B) + (C - A)   -> C +- B */
3369   /* (A +  B) - (A - C)   -> B + C */
3370   /* More cases are handled with comparisons.  */
3371   (simplify
3372    (plus:c (plus:c @0 @1) (minus @2 @0))
3373    (plus @2 @1))
3374   (simplify
3375    (plus:c (minus @0 @1) (minus @2 @0))
3376    (minus @2 @1))
3377   (simplify
3378    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3379    (if (TYPE_OVERFLOW_UNDEFINED (type)
3380         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3381     (pointer_diff @2 @1)))
3382   (simplify
3383    (minus (plus:c @0 @1) (minus @0 @2))
3384    (plus @1 @2))
3386   /* (A +- CST1) +- CST2 -> A + CST3
3387      Use view_convert because it is safe for vectors and equivalent for
3388      scalars.  */
3389   (for outer_op (plus minus)
3390    (for inner_op (plus minus)
3391         neg_inner_op (minus plus)
3392     (simplify
3393      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3394                CONSTANT_CLASS_P@2)
3395      /* If one of the types wraps, use that one.  */
3396      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3397       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3398          forever if something doesn't simplify into a constant.  */
3399       (if (!CONSTANT_CLASS_P (@0))
3400        (if (outer_op == PLUS_EXPR)
3401         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3402         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3403       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3404            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3405        (if (outer_op == PLUS_EXPR)
3406         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3407         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3408        /* If the constant operation overflows we cannot do the transform
3409           directly as we would introduce undefined overflow, for example
3410           with (a - 1) + INT_MIN.  */
3411        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3412         (with { tree cst = const_binop (outer_op == inner_op
3413                                         ? PLUS_EXPR : MINUS_EXPR,
3414                                         type, @1, @2); }
3415          (if (cst)
3416           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3417            (inner_op @0 { cst; } )
3418            /* X+INT_MAX+1 is X-INT_MIN.  */
3419            (if (INTEGRAL_TYPE_P (type)
3420                 && wi::to_wide (cst) == wi::min_value (type))
3421             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3422             /* Last resort, use some unsigned type.  */
3423             (with { tree utype = unsigned_type_for (type); }
3424              (if (utype)
3425               (view_convert (inner_op
3426                              (view_convert:utype @0)
3427                              (view_convert:utype
3428                               { TREE_OVERFLOW (cst)
3429                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3431   /* (CST1 - A) +- CST2 -> CST3 - A  */
3432   (for outer_op (plus minus)
3433    (simplify
3434     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3435     /* If one of the types wraps, use that one.  */
3436     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3437      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3438         forever if something doesn't simplify into a constant.  */
3439      (if (!CONSTANT_CLASS_P (@0))
3440       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3441      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3442           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3443       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3444       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3445        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3446         (if (cst && !TREE_OVERFLOW (cst))
3447          (minus { cst; } @0))))))))
3449   /* CST1 - (CST2 - A) -> CST3 + A
3450      Use view_convert because it is safe for vectors and equivalent for
3451      scalars.  */
3452   (simplify
3453    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3454    /* If one of the types wraps, use that one.  */
3455    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3456     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3457       forever if something doesn't simplify into a constant.  */
3458     (if (!CONSTANT_CLASS_P (@0))
3459      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3460     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3461          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3462      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3463      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3464       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3465        (if (cst && !TREE_OVERFLOW (cst))
3466         (plus { cst; } @0)))))))
3468 /* ((T)(A)) + CST -> (T)(A + CST)  */
3469 #if GIMPLE
3470   (simplify
3471    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3472     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3473          && TREE_CODE (type) == INTEGER_TYPE
3474          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3475          && int_fits_type_p (@1, TREE_TYPE (@0)))
3476      /* Perform binary operation inside the cast if the constant fits
3477         and (A + CST)'s range does not overflow.  */
3478      (with
3479       {
3480         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3481                           max_ovf = wi::OVF_OVERFLOW;
3482         tree inner_type = TREE_TYPE (@0);
3484         wide_int w1
3485           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3486                             TYPE_SIGN (inner_type));
3488         value_range vr;
3489         if (get_global_range_query ()->range_of_expr (vr, @0)
3490             && !vr.varying_p () && !vr.undefined_p ())
3491           {
3492             wide_int wmin0 = vr.lower_bound ();
3493             wide_int wmax0 = vr.upper_bound ();
3494             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3495             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3496           }
3497       }
3498      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3499       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3500      )))
3501 #endif
3503 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3504 #if GIMPLE
3505   (for op (plus minus)
3506    (simplify
3507     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3508      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3509           && TREE_CODE (type) == INTEGER_TYPE
3510           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3511           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3512           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3513           && TYPE_OVERFLOW_WRAPS (type))
3514        (plus (convert @0) (op @2 (convert @1))))))
3515 #endif
3517 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3518    to a simple value.  */
3519   (for op (plus minus)
3520    (simplify
3521     (op (convert @0) (convert @1))
3522      (if (INTEGRAL_TYPE_P (type)
3523           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3524           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3525           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3526           && !TYPE_OVERFLOW_TRAPS (type)
3527           && !TYPE_OVERFLOW_SANITIZED (type))
3528       (convert (op! @0 @1)))))
3530   /* ~A + A -> -1 */
3531   (simplify
3532    (plus:c (convert? (bit_not @0)) (convert? @0))
3533    (if (!TYPE_OVERFLOW_TRAPS (type))
3534     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3536   /* ~A + 1 -> -A */
3537   (simplify
3538    (plus (convert? (bit_not @0)) integer_each_onep)
3539    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3540     (negate (convert @0))))
3542   /* -A - 1 -> ~A */
3543   (simplify
3544    (minus (convert? (negate @0)) integer_each_onep)
3545    (if (!TYPE_OVERFLOW_TRAPS (type)
3546         && TREE_CODE (type) != COMPLEX_TYPE
3547         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3548     (bit_not (convert @0))))
3550   /* -1 - A -> ~A */
3551   (simplify
3552    (minus integer_all_onesp @0)
3553    (if (TREE_CODE (type) != COMPLEX_TYPE)
3554     (bit_not @0)))
3556   /* (T)(P + A) - (T)P -> (T) A */
3557   (simplify
3558    (minus (convert (plus:c @@0 @1))
3559     (convert? @0))
3560    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3561         /* For integer types, if A has a smaller type
3562            than T the result depends on the possible
3563            overflow in P + A.
3564            E.g. T=size_t, A=(unsigned)429497295, P>0.
3565            However, if an overflow in P + A would cause
3566            undefined behavior, we can assume that there
3567            is no overflow.  */
3568         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3569             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3570     (convert @1)))
3571   (simplify
3572    (minus (convert (pointer_plus @@0 @1))
3573     (convert @0))
3574    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3575         /* For pointer types, if the conversion of A to the
3576            final type requires a sign- or zero-extension,
3577            then we have to punt - it is not defined which
3578            one is correct.  */
3579         || (POINTER_TYPE_P (TREE_TYPE (@0))
3580             && TREE_CODE (@1) == INTEGER_CST
3581             && tree_int_cst_sign_bit (@1) == 0))
3582     (convert @1)))
3583    (simplify
3584     (pointer_diff (pointer_plus @@0 @1) @0)
3585     /* The second argument of pointer_plus must be interpreted as signed, and
3586        thus sign-extended if necessary.  */
3587     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3588      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3589         second arg is unsigned even when we need to consider it as signed,
3590         we don't want to diagnose overflow here.  */
3591      (convert (view_convert:stype @1))))
3593   /* (T)P - (T)(P + A) -> -(T) A */
3594   (simplify
3595    (minus (convert? @0)
3596     (convert (plus:c @@0 @1)))
3597    (if (INTEGRAL_TYPE_P (type)
3598         && TYPE_OVERFLOW_UNDEFINED (type)
3599         /* For integer literals, using an intermediate unsigned type to avoid
3600            an overflow at run time is counter-productive because it introduces
3601            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3602            the result, which may be problematic in GENERIC for some front-ends:
3603              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3604            so we use the direct path for them.  */
3605         && TREE_CODE (@1) != INTEGER_CST
3606         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3607     (with { tree utype = unsigned_type_for (type); }
3608      (convert (negate (convert:utype @1))))
3609     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3610          /* For integer types, if A has a smaller type
3611             than T the result depends on the possible
3612             overflow in P + A.
3613             E.g. T=size_t, A=(unsigned)429497295, P>0.
3614             However, if an overflow in P + A would cause
3615             undefined behavior, we can assume that there
3616             is no overflow.  */
3617          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3618              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3619      (negate (convert @1)))))
3620   (simplify
3621    (minus (convert @0)
3622     (convert (pointer_plus @@0 @1)))
3623    (if (INTEGRAL_TYPE_P (type)
3624         && TYPE_OVERFLOW_UNDEFINED (type)
3625         /* See above the rationale for this condition.  */
3626         && TREE_CODE (@1) != INTEGER_CST
3627         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3628     (with { tree utype = unsigned_type_for (type); }
3629      (convert (negate (convert:utype @1))))
3630     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3631          /* For pointer types, if the conversion of A to the
3632             final type requires a sign- or zero-extension,
3633             then we have to punt - it is not defined which
3634             one is correct.  */
3635          || (POINTER_TYPE_P (TREE_TYPE (@0))
3636              && TREE_CODE (@1) == INTEGER_CST
3637              && tree_int_cst_sign_bit (@1) == 0))
3638      (negate (convert @1)))))
3639    (simplify
3640     (pointer_diff @0 (pointer_plus @@0 @1))
3641     /* The second argument of pointer_plus must be interpreted as signed, and
3642        thus sign-extended if necessary.  */
3643     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3644      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3645         second arg is unsigned even when we need to consider it as signed,
3646         we don't want to diagnose overflow here.  */
3647      (negate (convert (view_convert:stype @1)))))
3649   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3650   (simplify
3651    (minus (convert (plus:c @@0 @1))
3652     (convert (plus:c @0 @2)))
3653    (if (INTEGRAL_TYPE_P (type)
3654         && TYPE_OVERFLOW_UNDEFINED (type)
3655         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3656         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3657     (with { tree utype = unsigned_type_for (type); }
3658      (convert (minus (convert:utype @1) (convert:utype @2))))
3659     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3660           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3661          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3662              /* For integer types, if A has a smaller type
3663                 than T the result depends on the possible
3664                 overflow in P + A.
3665                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3666                 However, if an overflow in P + A would cause
3667                 undefined behavior, we can assume that there
3668                 is no overflow.  */
3669              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3670                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3671                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3672                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3673      (minus (convert @1) (convert @2)))))
3674   (simplify
3675    (minus (convert (pointer_plus @@0 @1))
3676     (convert (pointer_plus @0 @2)))
3677    (if (INTEGRAL_TYPE_P (type)
3678         && TYPE_OVERFLOW_UNDEFINED (type)
3679         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3680     (with { tree utype = unsigned_type_for (type); }
3681      (convert (minus (convert:utype @1) (convert:utype @2))))
3682     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3683          /* For pointer types, if the conversion of A to the
3684             final type requires a sign- or zero-extension,
3685             then we have to punt - it is not defined which
3686             one is correct.  */
3687          || (POINTER_TYPE_P (TREE_TYPE (@0))
3688              && TREE_CODE (@1) == INTEGER_CST
3689              && tree_int_cst_sign_bit (@1) == 0
3690              && TREE_CODE (@2) == INTEGER_CST
3691              && tree_int_cst_sign_bit (@2) == 0))
3692      (minus (convert @1) (convert @2)))))
3693    (simplify
3694     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3695      (pointer_diff @0 @1))
3696    (simplify
3697     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3698     /* The second argument of pointer_plus must be interpreted as signed, and
3699        thus sign-extended if necessary.  */
3700     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3701      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3702         second arg is unsigned even when we need to consider it as signed,
3703         we don't want to diagnose overflow here.  */
3704      (minus (convert (view_convert:stype @1))
3705             (convert (view_convert:stype @2)))))))
3707 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3708     Modeled after fold_plusminus_mult_expr.  */
3709 (if (!TYPE_SATURATING (type)
3710      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3711  (for plusminus (plus minus)
3712   (simplify
3713    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3714    (if (!ANY_INTEGRAL_TYPE_P (type)
3715         || TYPE_OVERFLOW_WRAPS (type)
3716         || (INTEGRAL_TYPE_P (type)
3717             && tree_expr_nonzero_p (@0)
3718             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3719     (if (single_use (@3) || single_use (@4))
3720      /* If @1 +- @2 is constant require a hard single-use on either
3721         original operand (but not on both).  */
3722      (mult (plusminus @1 @2) @0)
3723      (mult! (plusminus @1 @2) @0)
3724   )))
3725   /* We cannot generate constant 1 for fract.  */
3726   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3727    (simplify
3728     (plusminus @0 (mult:c@3 @0 @2))
3729     (if ((!ANY_INTEGRAL_TYPE_P (type)
3730           || TYPE_OVERFLOW_WRAPS (type)
3731           /* For @0 + @0*@2 this transformation would introduce UB
3732              (where there was none before) for @0 in [-1,0] and @2 max.
3733              For @0 - @0*@2 this transformation would introduce UB
3734              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3735           || (INTEGRAL_TYPE_P (type)
3736               && ((tree_expr_nonzero_p (@0)
3737                    && expr_not_equal_to (@0,
3738                                 wi::minus_one (TYPE_PRECISION (type))))
3739                   || (plusminus == PLUS_EXPR
3740                       ? expr_not_equal_to (@2,
3741                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3742                       /* Let's ignore the @0 -1 and @2 min case.  */
3743                       : (expr_not_equal_to (@2,
3744                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3745                          && expr_not_equal_to (@2,
3746                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3747                                 + 1))))))
3748          && single_use (@3))
3749      (mult (plusminus { build_one_cst (type); } @2) @0)))
3750    (simplify
3751     (plusminus (mult:c@3 @0 @2) @0)
3752     (if ((!ANY_INTEGRAL_TYPE_P (type)
3753           || TYPE_OVERFLOW_WRAPS (type)
3754           /* For @0*@2 + @0 this transformation would introduce UB
3755              (where there was none before) for @0 in [-1,0] and @2 max.
3756              For @0*@2 - @0 this transformation would introduce UB
3757              for @0 0 and @2 min.  */
3758           || (INTEGRAL_TYPE_P (type)
3759               && ((tree_expr_nonzero_p (@0)
3760                    && (plusminus == MINUS_EXPR
3761                        || expr_not_equal_to (@0,
3762                                 wi::minus_one (TYPE_PRECISION (type)))))
3763                   || expr_not_equal_to (@2,
3764                         (plusminus == PLUS_EXPR
3765                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3766                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3767          && single_use (@3))
3768      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3770 #if GIMPLE
3771 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3772    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3773 (simplify
3774  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3775   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3776        && tree_fits_uhwi_p (@1)
3777        && tree_to_uhwi (@1) < element_precision (type)
3778        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3779            || optab_handler (smul_optab,
3780                              TYPE_MODE (type)) != CODE_FOR_nothing))
3781    (with { tree t = type;
3782            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3783            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3784                                              element_precision (type));
3785            w += 1;
3786            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3787                                         : t, w);
3788            cst = build_uniform_cst (t, cst); }
3789     (convert (mult (convert:t @0) { cst; })))))
3790 (simplify
3791  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3792   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3793        && tree_fits_uhwi_p (@1)
3794        && tree_to_uhwi (@1) < element_precision (type)
3795        && tree_fits_uhwi_p (@2)
3796        && tree_to_uhwi (@2) < element_precision (type)
3797        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3798            || optab_handler (smul_optab,
3799                              TYPE_MODE (type)) != CODE_FOR_nothing))
3800    (with { tree t = type;
3801            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3802            unsigned int prec = element_precision (type);
3803            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3804            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3805            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3806                                         : t, w);
3807            cst = build_uniform_cst (t, cst); }
3808     (convert (mult (convert:t @0) { cst; })))))
3809 #endif
3811 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3812    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3813    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3814 (for op (bit_ior bit_xor)
3815  (simplify
3816   (op (mult:s@0 @1 INTEGER_CST@2)
3817       (mult:s@3 @1 INTEGER_CST@4))
3818   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3819        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3820    (mult @1
3821          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3822  (simplify
3823   (op:c (mult:s@0 @1 INTEGER_CST@2)
3824         (lshift:s@3 @1 INTEGER_CST@4))
3825   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3826        && tree_int_cst_sgn (@4) > 0
3827        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3828    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3829            wide_int c = wi::add (wi::to_wide (@2),
3830                                  wi::lshift (wone, wi::to_wide (@4))); }
3831     (mult @1 { wide_int_to_tree (type, c); }))))
3832  (simplify
3833   (op:c (mult:s@0 @1 INTEGER_CST@2)
3834         @1)
3835   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3836        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3837    (mult @1
3838          { wide_int_to_tree (type,
3839                              wi::add (wi::to_wide (@2), 1)); })))
3840  (simplify
3841   (op (lshift:s@0 @1 INTEGER_CST@2)
3842       (lshift:s@3 @1 INTEGER_CST@4))
3843   (if (INTEGRAL_TYPE_P (type)
3844        && tree_int_cst_sgn (@2) > 0
3845        && tree_int_cst_sgn (@4) > 0
3846        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3847    (with { tree t = type;
3848            if (!TYPE_OVERFLOW_WRAPS (t))
3849              t = unsigned_type_for (t);
3850            wide_int wone = wi::one (TYPE_PRECISION (t));
3851            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3852                                  wi::lshift (wone, wi::to_wide (@4))); }
3853     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3854  (simplify
3855   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3856         @1)
3857   (if (INTEGRAL_TYPE_P (type)
3858        && tree_int_cst_sgn (@2) > 0
3859        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3860    (with { tree t = type;
3861            if (!TYPE_OVERFLOW_WRAPS (t))
3862              t = unsigned_type_for (t);
3863            wide_int wone = wi::one (TYPE_PRECISION (t));
3864            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3865     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3867 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3869 (for minmax (min max)
3870  (simplify
3871   (minmax @0 @0)
3872   @0)
3873 /* max(max(x,y),x) -> max(x,y)  */
3874  (simplify
3875   (minmax:c (minmax:c@2 @0 @1) @0)
3876   @2))
3877 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3878 (for minmax (FMIN_ALL FMAX_ALL)
3879  (simplify
3880   (minmax @0 @0)
3881   (if (!tree_expr_maybe_signaling_nan_p (@0))
3882     @0)))
3883 /* min(max(x,y),y) -> y.  */
3884 (simplify
3885  (min:c (max:c @0 @1) @1)
3886  @1)
3887 /* max(min(x,y),y) -> y.  */
3888 (simplify
3889  (max:c (min:c @0 @1) @1)
3890  @1)
3891 /* max(a,-a) -> abs(a).  */
3892 (simplify
3893  (max:c @0 (negate @0))
3894  (if (TREE_CODE (type) != COMPLEX_TYPE
3895       && (! ANY_INTEGRAL_TYPE_P (type)
3896           || TYPE_OVERFLOW_UNDEFINED (type)))
3897   (abs @0)))
3898 /* min(a,-a) -> -abs(a).  */
3899 (simplify
3900  (min:c @0 (negate @0))
3901  (if (TREE_CODE (type) != COMPLEX_TYPE
3902       && (! ANY_INTEGRAL_TYPE_P (type)
3903           || TYPE_OVERFLOW_UNDEFINED (type)))
3904   (negate (abs @0))))
3905 (simplify
3906  (min @0 @1)
3907  (switch
3908   (if (INTEGRAL_TYPE_P (type)
3909        && TYPE_MIN_VALUE (type)
3910        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3911    @1)
3912   (if (INTEGRAL_TYPE_P (type)
3913        && TYPE_MAX_VALUE (type)
3914        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3915    @0)))
3916 (simplify
3917  (max @0 @1)
3918  (switch
3919   (if (INTEGRAL_TYPE_P (type)
3920        && TYPE_MAX_VALUE (type)
3921        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3922    @1)
3923   (if (INTEGRAL_TYPE_P (type)
3924        && TYPE_MIN_VALUE (type)
3925        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3926    @0)))
3928 /* max (a, a + CST) -> a + CST where CST is positive.  */
3929 /* max (a, a + CST) -> a where CST is negative.  */
3930 (simplify
3931  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3932   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3933    (if (tree_int_cst_sgn (@1) > 0)
3934     @2
3935     @0)))
3937 /* min (a, a + CST) -> a where CST is positive.  */
3938 /* min (a, a + CST) -> a + CST where CST is negative. */
3939 (simplify
3940  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3941   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3942    (if (tree_int_cst_sgn (@1) > 0)
3943     @0
3944     @2)))
3946 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3947    the addresses are known to be less, equal or greater.  */
3948 (for minmax (min max)
3949      cmp (lt gt)
3950  (simplify
3951   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3952   (with
3953    {
3954      poly_int64 off0, off1;
3955      tree base0, base1;
3956      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3957                                   off0, off1, GENERIC);
3958    }
3959    (if (equal == 1)
3960     (if (minmax == MIN_EXPR)
3961      (if (known_le (off0, off1))
3962       @2
3963       (if (known_gt (off0, off1))
3964        @3))
3965      (if (known_ge (off0, off1))
3966       @2
3967       (if (known_lt (off0, off1))
3968        @3)))))))
3970 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3971    and the outer convert demotes the expression back to x's type.  */
3972 (for minmax (min max)
3973  (simplify
3974   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3975   (if (INTEGRAL_TYPE_P (type)
3976        && types_match (@1, type) && int_fits_type_p (@2, type)
3977        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3978        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3979    (minmax @1 (convert @2)))))
3981 (for minmax (FMIN_ALL FMAX_ALL)
3982  /* If either argument is NaN and other one is not sNaN, return the other
3983     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3984  (simplify
3985   (minmax:c @0 REAL_CST@1)
3986    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3987        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3988        && !tree_expr_maybe_signaling_nan_p (@0))
3989    @0)))
3990 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3991    functions to return the numeric arg if the other one is NaN.
3992    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3993    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3994    worry about it either.  */
3995 (if (flag_finite_math_only)
3996  (simplify
3997   (FMIN_ALL @0 @1)
3998   (min @0 @1))
3999  (simplify
4000   (FMAX_ALL @0 @1)
4001   (max @0 @1)))
4002 /* min (-A, -B) -> -max (A, B)  */
4003 (for minmax (min max FMIN_ALL FMAX_ALL)
4004      maxmin (max min FMAX_ALL FMIN_ALL)
4005  (simplify
4006   (minmax (negate:s@2 @0) (negate:s@3 @1))
4007   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4008        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4009            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4010    (negate (maxmin @0 @1)))))
4011 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4012    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4013 (for minmax (min max)
4014  maxmin (max min)
4015  (simplify
4016   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4017   (bit_not (maxmin @0 @1)))
4018 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4019 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4020  (simplify
4021   (bit_not (minmax:cs (bit_not @0) @1))
4022   (maxmin @0 (bit_not @1))))
4024 /* MIN (X, Y) == X -> X <= Y  */
4025 /* MIN (X, Y) < X -> X > Y  */
4026 /* MIN (X, Y) >= X -> X <= Y  */
4027 (for minmax (min min min min max max max max)
4028      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4029      out    (le  gt  gt  le  ge  lt  lt  ge )
4030  (simplify
4031   (cmp:c (minmax:c @0 @1) @0)
4032   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4033    (out @0 @1))))
4034 /* MIN (X, 5) == 0 -> X == 0
4035    MIN (X, 5) == 7 -> false  */
4036 (for cmp (eq ne)
4037  (simplify
4038   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4039   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4040                  TYPE_SIGN (TREE_TYPE (@0))))
4041    { constant_boolean_node (cmp == NE_EXPR, type); }
4042    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4043                   TYPE_SIGN (TREE_TYPE (@0))))
4044     (cmp @0 @2)))))
4045 (for cmp (eq ne)
4046  (simplify
4047   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4048   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4049                  TYPE_SIGN (TREE_TYPE (@0))))
4050    { constant_boolean_node (cmp == NE_EXPR, type); }
4051    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4052                   TYPE_SIGN (TREE_TYPE (@0))))
4053     (cmp @0 @2)))))
4055 /* X <= MAX(X, Y) -> true
4056    X > MAX(X, Y) -> false 
4057    X >= MIN(X, Y) -> true
4058    X < MIN(X, Y) -> false */
4059 (for minmax (min     min     max     max     )
4060      cmp    (ge      lt      le      gt      )
4061  (simplify
4062   (cmp:c @0 (minmax:c @0 @1))
4063   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4065 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4066 (for minmax (min     min     max     max     min     min     max     max    )
4067      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4068      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4069  (simplify
4070   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4071   (comb (cmp @0 @2) (cmp @1 @2))))
4073 /* Undo fancy ways of writing max/min or other ?: expressions, like
4074    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4075    People normally use ?: and that is what we actually try to optimize.  */
4076 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4077 (simplify
4078  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4079  (if (INTEGRAL_TYPE_P (type)
4080       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4081   (cond (convert:boolean_type_node @2) @1 @0)))
4082 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4083 (simplify
4084  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4085  (if (INTEGRAL_TYPE_P (type)
4086       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4087   (cond (convert:boolean_type_node @2) @1 @0)))
4088 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4089 (simplify
4090  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4091  (if (INTEGRAL_TYPE_P (type)
4092       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4093   (cond (convert:boolean_type_node @2) @1 @0)))
4095 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4096 (simplify
4097   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4098   (max @2 @1))
4100 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4101 (for op (bit_xor bit_ior plus)
4102  (simplify
4103   (cond (eq zero_one_valued_p@0
4104             integer_zerop)
4105         @1
4106         (op:c @2 @1))
4107   (if (INTEGRAL_TYPE_P (type)
4108        && TYPE_PRECISION (type) > 1
4109        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4110        (op (mult (convert:type @0) @2) @1))))
4112 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4113 (for op (bit_xor bit_ior plus)
4114  (simplify
4115   (cond (ne zero_one_valued_p@0
4116             integer_zerop)
4117        (op:c @2 @1)
4118         @1)
4119   (if (INTEGRAL_TYPE_P (type)
4120        && TYPE_PRECISION (type) > 1
4121        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4122        (op (mult (convert:type @0) @2) @1))))
4124 /* Simplifications of shift and rotates.  */
4126 (for rotate (lrotate rrotate)
4127  (simplify
4128   (rotate integer_all_onesp@0 @1)
4129   @0))
4131 /* Optimize -1 >> x for arithmetic right shifts.  */
4132 (simplify
4133  (rshift integer_all_onesp@0 @1)
4134  (if (!TYPE_UNSIGNED (type))
4135   @0))
4137 /* Optimize (x >> c) << c into x & (-1<<c).  */
4138 (simplify
4139  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4140  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4141   /* It doesn't matter if the right shift is arithmetic or logical.  */
4142   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4144 (simplify
4145  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4146  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4147       /* Allow intermediate conversion to integral type with whatever sign, as
4148          long as the low TYPE_PRECISION (type)
4149          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4150       && INTEGRAL_TYPE_P (type)
4151       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4152       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4153       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4154       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4155           || wi::geu_p (wi::to_wide (@1),
4156                         TYPE_PRECISION (type)
4157                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4158   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4160 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4161    unsigned x OR truncate into the precision(type) - c lowest bits
4162    of signed x (if they have mode precision or a precision of 1).  */
4163 (simplify
4164  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4165  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4166   (if (TYPE_UNSIGNED (type))
4167    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4168    (if (INTEGRAL_TYPE_P (type))
4169     (with {
4170       int width = element_precision (type) - tree_to_uhwi (@1);
4171       tree stype = NULL_TREE;
4172       if (width <= MAX_FIXED_MODE_SIZE)
4173         stype = build_nonstandard_integer_type (width, 0);
4174      }
4175      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4176       (convert (convert:stype @0))))))))
4178 /* Optimize x >> x into 0 */
4179 (simplify
4180  (rshift @0 @0)
4181   { build_zero_cst (type); })
4183 (for shiftrotate (lrotate rrotate lshift rshift)
4184  (simplify
4185   (shiftrotate @0 integer_zerop)
4186   (non_lvalue @0))
4187  (simplify
4188   (shiftrotate integer_zerop@0 @1)
4189   @0)
4190  /* Prefer vector1 << scalar to vector1 << vector2
4191     if vector2 is uniform.  */
4192  (for vec (VECTOR_CST CONSTRUCTOR)
4193   (simplify
4194    (shiftrotate @0 vec@1)
4195    (with { tree tem = uniform_vector_p (@1); }
4196     (if (tem)
4197      (shiftrotate @0 { tem; }))))))
4199 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4200    Y is 0.  Similarly for X >> Y.  */
4201 #if GIMPLE
4202 (for shift (lshift rshift)
4203  (simplify
4204   (shift @0 SSA_NAME@1)
4205    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4206     (with {
4207       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4208       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4209      }
4210      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4211       @0)))))
4212 #endif
4214 /* Rewrite an LROTATE_EXPR by a constant into an
4215    RROTATE_EXPR by a new constant.  */
4216 (simplify
4217  (lrotate @0 INTEGER_CST@1)
4218  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4219                             build_int_cst (TREE_TYPE (@1),
4220                                            element_precision (type)), @1); }))
4222 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4223 (for op (lrotate rrotate rshift lshift)
4224  (simplify
4225   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4226   (with { unsigned int prec = element_precision (type); }
4227    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4228         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4229         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4230         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4231     (with { unsigned int low = (tree_to_uhwi (@1)
4232                                 + tree_to_uhwi (@2)); }
4233      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4234         being well defined.  */
4235      (if (low >= prec)
4236       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4237        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4238        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4239         { build_zero_cst (type); }
4240         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4241       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4244 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4245 (simplify
4246  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4247   (if ((wi::to_wide (@1) & 1) != 0)
4248    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4249    { build_zero_cst (type); }))
4251 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4252    either to false if D is smaller (unsigned comparison) than C, or to
4253    x == log2 (D) - log2 (C).  Similarly for right shifts.
4254    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4255 (for cmp (ne eq)
4256      icmp (eq ne)
4257  (simplify
4258   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4259    (with { int c1 = wi::clz (wi::to_wide (@1));
4260            int c2 = wi::clz (wi::to_wide (@2)); }
4261     (if (c1 < c2)
4262      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4263      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4264  (simplify
4265   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4266    (if (tree_int_cst_sgn (@1) > 0)
4267     (with { int c1 = wi::clz (wi::to_wide (@1));
4268             int c2 = wi::clz (wi::to_wide (@2)); }
4269      (if (c1 > c2)
4270       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4271       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4272  /* `(1 >> X) != 0` -> `X == 0` */
4273  /* `(1 >> X) == 0` -> `X != 0` */
4274  (simplify
4275   (cmp (rshift integer_onep@1 @0) integer_zerop)
4276    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4277     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4279 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4280    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4281    if CST2 != 0.  */
4282 (for cmp (ne eq)
4283  (simplify
4284   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4285   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4286    (if (cand < 0
4287         || (!integer_zerop (@2)
4288             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4289     { constant_boolean_node (cmp == NE_EXPR, type); }
4290     (if (!integer_zerop (@2)
4291          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4292      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4294 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4295         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4296 (for cmp (ne eq)
4297  (simplify
4298   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4299   (if (tree_fits_shwi_p (@1)
4300        && tree_to_shwi (@1) > 0
4301        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4302     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4303       { constant_boolean_node (cmp == NE_EXPR, type); }
4304       (with { wide_int c1 = wi::to_wide (@1);
4305               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4306               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4307         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4308              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4309  (simplify
4310   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4311   (if (tree_fits_shwi_p (@1)
4312        && tree_to_shwi (@1) > 0
4313        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4314     (with { tree t0 = TREE_TYPE (@0);
4315             unsigned int prec = TYPE_PRECISION (t0);
4316             wide_int c1 = wi::to_wide (@1);
4317             wide_int c2 = wi::to_wide (@2);
4318             wide_int c3 = wi::to_wide (@3);
4319             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4320       (if ((c2 & c3) != c3)
4321         { constant_boolean_node (cmp == NE_EXPR, type); }
4322         (if (TYPE_UNSIGNED (t0))
4323           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4324             { constant_boolean_node (cmp == NE_EXPR, type); }
4325             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4326                  { wide_int_to_tree (t0, c3 << c1); }))
4327           (with { wide_int smask = wi::arshift (sb, c1); }
4328             (switch
4329               (if ((c2 & smask) == 0)
4330                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4331                      { wide_int_to_tree (t0, c3 << c1); }))
4332               (if ((c3 & smask) == 0)
4333                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4334                      { wide_int_to_tree (t0, c3 << c1); }))
4335               (if ((c2 & smask) != (c3 & smask))
4336                 { constant_boolean_node (cmp == NE_EXPR, type); })
4337               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4338                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4340 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4341         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4342    if the new mask might be further optimized.  */
4343 (for shift (lshift rshift)
4344  (simplify
4345   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4346            INTEGER_CST@2)
4347    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4348         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4349         && tree_fits_uhwi_p (@1)
4350         && tree_to_uhwi (@1) > 0
4351         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4352     (with
4353      {
4354        unsigned int shiftc = tree_to_uhwi (@1);
4355        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4356        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4357        tree shift_type = TREE_TYPE (@3);
4358        unsigned int prec;
4360        if (shift == LSHIFT_EXPR)
4361          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4362        else if (shift == RSHIFT_EXPR
4363                 && type_has_mode_precision_p (shift_type))
4364          {
4365            prec = TYPE_PRECISION (TREE_TYPE (@3));
4366            tree arg00 = @0;
4367            /* See if more bits can be proven as zero because of
4368               zero extension.  */
4369            if (@3 != @0
4370                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4371              {
4372                tree inner_type = TREE_TYPE (@0);
4373                if (type_has_mode_precision_p (inner_type)
4374                    && TYPE_PRECISION (inner_type) < prec)
4375                  {
4376                    prec = TYPE_PRECISION (inner_type);
4377                    /* See if we can shorten the right shift.  */
4378                    if (shiftc < prec)
4379                      shift_type = inner_type;
4380                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4381                       it into (X, 0) later on by making sure zerobits
4382                       is all ones.  */
4383                  }
4384              }
4385            zerobits = HOST_WIDE_INT_M1U;
4386            if (shiftc < prec)
4387              {
4388                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4389                zerobits <<= prec - shiftc;
4390              }
4391            /* For arithmetic shift if sign bit could be set, zerobits
4392               can contain actually sign bits, so no transformation is
4393               possible, unless MASK masks them all away.  In that
4394               case the shift needs to be converted into logical shift.  */
4395            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4396                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4397              {
4398                if ((mask & zerobits) == 0)
4399                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4400                else
4401                  zerobits = 0;
4402              }
4403          }
4404      }
4405      /* ((X << 16) & 0xff00) is (X, 0).  */
4406      (if ((mask & zerobits) == mask)
4407       { build_int_cst (type, 0); }
4408       (with { newmask = mask | zerobits; }
4409        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4410         (with
4411          {
4412            /* Only do the transformation if NEWMASK is some integer
4413               mode's mask.  */
4414            for (prec = BITS_PER_UNIT;
4415                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4416              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4417                break;
4418          }
4419          (if (prec < HOST_BITS_PER_WIDE_INT
4420               || newmask == HOST_WIDE_INT_M1U)
4421           (with
4422            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4423            (if (!tree_int_cst_equal (newmaskt, @2))
4424             (if (shift_type != TREE_TYPE (@3))
4425              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4426              (bit_and @4 { newmaskt; })))))))))))))
4428 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4429 (for cmp (ne eq)
4430        icmp (eq ne)
4431  (simplify
4432   (cmp
4433    (bit_and
4434     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4435   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4436    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4437                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4439 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4440    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4441 (for shift (lshift rshift)
4442  (for bit_op (bit_and bit_xor bit_ior)
4443   (simplify
4444    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4445    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4446     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4447      (if (mask)
4448       (bit_op (shift (convert @0) @1) { mask; })))))))
4450 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4451 (simplify
4452  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4453   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4454        && (element_precision (TREE_TYPE (@0))
4455            <= element_precision (TREE_TYPE (@1))
4456            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4457    (with
4458     { tree shift_type = TREE_TYPE (@0); }
4459      (convert (rshift (convert:shift_type @1) @2)))))
4461 /* ~(~X >>r Y) -> X >>r Y
4462    ~(~X <<r Y) -> X <<r Y */
4463 (for rotate (lrotate rrotate)
4464  (simplify
4465   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4466    (if ((element_precision (TREE_TYPE (@0))
4467          <= element_precision (TREE_TYPE (@1))
4468          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4469         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4470             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4471     (with
4472      { tree rotate_type = TREE_TYPE (@0); }
4473       (convert (rotate (convert:rotate_type @1) @2))))))
4475 (for cmp (eq ne)
4476  (for rotate (lrotate rrotate)
4477       invrot (rrotate lrotate)
4478   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4479   (simplify
4480    (cmp (rotate @1 @0) (rotate @2 @0))
4481    (cmp @1 @2))
4482   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4483   (simplify
4484    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4485    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4486   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4487   (simplify
4488    (cmp (rotate @0 @1) INTEGER_CST@2)
4489     (if (integer_zerop (@2) || integer_all_onesp (@2))
4490      (cmp @0 @2)))))
4492 /* Narrow a lshift by constant.  */
4493 (simplify
4494  (convert (lshift:s@0 @1 INTEGER_CST@2))
4495  (if (INTEGRAL_TYPE_P (type)
4496       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4497       && !integer_zerop (@2)
4498       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4499   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4500        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4501    (lshift (convert @1) @2)
4502    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4503     { build_zero_cst (type); }))))
4505 /* Simplifications of conversions.  */
4507 /* Basic strip-useless-type-conversions / strip_nops.  */
4508 (for cvt (convert view_convert float fix_trunc)
4509  (simplify
4510   (cvt @0)
4511   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4512        || (GENERIC && type == TREE_TYPE (@0)))
4513    @0)))
4515 /* Contract view-conversions.  */
4516 (simplify
4517   (view_convert (view_convert @0))
4518   (view_convert @0))
4520 /* For integral conversions with the same precision or pointer
4521    conversions use a NOP_EXPR instead.  */
4522 (simplify
4523   (view_convert @0)
4524   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4525        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4526        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4527    (convert @0)))
4529 /* Strip inner integral conversions that do not change precision or size, or
4530    zero-extend while keeping the same size (for bool-to-char).  */
4531 (simplify
4532   (view_convert (convert@0 @1))
4533   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4534        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4535        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4536        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4537            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4538                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4539    (view_convert @1)))
4541 /* Simplify a view-converted empty or single-element constructor.  */
4542 (simplify
4543   (view_convert CONSTRUCTOR@0)
4544   (with
4545    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4546                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4547    (switch
4548     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4549      { build_zero_cst (type); })
4550     (if (CONSTRUCTOR_NELTS (ctor) == 1
4551          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4552          && operand_equal_p (TYPE_SIZE (type),
4553                              TYPE_SIZE (TREE_TYPE
4554                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4555      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4557 /* Re-association barriers around constants and other re-association
4558    barriers can be removed.  */
4559 (simplify
4560  (paren CONSTANT_CLASS_P@0)
4561  @0)
4562 (simplify
4563  (paren (paren@1 @0))
4564  @1)
4566 /* Handle cases of two conversions in a row.  */
4567 (for ocvt (convert float fix_trunc)
4568  (for icvt (convert float)
4569   (simplify
4570    (ocvt (icvt@1 @0))
4571    (with
4572     {
4573       tree inside_type = TREE_TYPE (@0);
4574       tree inter_type = TREE_TYPE (@1);
4575       int inside_int = INTEGRAL_TYPE_P (inside_type);
4576       int inside_ptr = POINTER_TYPE_P (inside_type);
4577       int inside_float = FLOAT_TYPE_P (inside_type);
4578       int inside_vec = VECTOR_TYPE_P (inside_type);
4579       unsigned int inside_prec = element_precision (inside_type);
4580       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4581       int inter_int = INTEGRAL_TYPE_P (inter_type);
4582       int inter_ptr = POINTER_TYPE_P (inter_type);
4583       int inter_float = FLOAT_TYPE_P (inter_type);
4584       int inter_vec = VECTOR_TYPE_P (inter_type);
4585       unsigned int inter_prec = element_precision (inter_type);
4586       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4587       int final_int = INTEGRAL_TYPE_P (type);
4588       int final_ptr = POINTER_TYPE_P (type);
4589       int final_float = FLOAT_TYPE_P (type);
4590       int final_vec = VECTOR_TYPE_P (type);
4591       unsigned int final_prec = element_precision (type);
4592       int final_unsignedp = TYPE_UNSIGNED (type);
4593     }
4594    (switch
4595     /* In addition to the cases of two conversions in a row
4596        handled below, if we are converting something to its own
4597        type via an object of identical or wider precision, neither
4598        conversion is needed.  */
4599     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4600           || (GENERIC
4601               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4602          && (((inter_int || inter_ptr) && final_int)
4603              || (inter_float && final_float))
4604          && inter_prec >= final_prec)
4605      (ocvt @0))
4607     /* Likewise, if the intermediate and initial types are either both
4608        float or both integer, we don't need the middle conversion if the
4609        former is wider than the latter and doesn't change the signedness
4610        (for integers).  Avoid this if the final type is a pointer since
4611        then we sometimes need the middle conversion.  */
4612     (if (((inter_int && inside_int) || (inter_float && inside_float))
4613          && (final_int || final_float)
4614          && inter_prec >= inside_prec
4615          && (inter_float || inter_unsignedp == inside_unsignedp))
4616      (ocvt @0))
4618     /* If we have a sign-extension of a zero-extended value, we can
4619        replace that by a single zero-extension.  Likewise if the
4620        final conversion does not change precision we can drop the
4621        intermediate conversion.  */
4622     (if (inside_int && inter_int && final_int
4623          && ((inside_prec < inter_prec && inter_prec < final_prec
4624               && inside_unsignedp && !inter_unsignedp)
4625              || final_prec == inter_prec))
4626      (ocvt @0))
4628     /* Two conversions in a row are not needed unless:
4629         - some conversion is floating-point (overstrict for now), or
4630         - some conversion is a vector (overstrict for now), or
4631         - the intermediate type is narrower than both initial and
4632           final, or
4633         - the intermediate type and innermost type differ in signedness,
4634           and the outermost type is wider than the intermediate, or
4635         - the initial type is a pointer type and the precisions of the
4636           intermediate and final types differ, or
4637         - the final type is a pointer type and the precisions of the
4638           initial and intermediate types differ.  */
4639     (if (! inside_float && ! inter_float && ! final_float
4640          && ! inside_vec && ! inter_vec && ! final_vec
4641          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4642          && ! (inside_int && inter_int
4643                && inter_unsignedp != inside_unsignedp
4644                && inter_prec < final_prec)
4645          && ((inter_unsignedp && inter_prec > inside_prec)
4646              == (final_unsignedp && final_prec > inter_prec))
4647          && ! (inside_ptr && inter_prec != final_prec)
4648          && ! (final_ptr && inside_prec != inter_prec))
4649      (ocvt @0))
4651    /* `(outer:M)(inter:N) a:O`
4652       can be converted to `(outer:M) a`
4653       if M <= O && N >= O. No matter what signedness of the casts,
4654       as the final is either a truncation from the original or just
4655       a sign change of the type. */
4656    (if (inside_int && inter_int && final_int
4657         && final_prec <= inside_prec
4658         && inter_prec >= inside_prec)
4659     (convert @0))
4661     /* A truncation to an unsigned type (a zero-extension) should be
4662        canonicalized as bitwise and of a mask.  */
4663     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4664          && final_int && inter_int && inside_int
4665          && final_prec == inside_prec
4666          && final_prec > inter_prec
4667          && inter_unsignedp)
4668      (convert (bit_and @0 { wide_int_to_tree
4669                               (inside_type,
4670                                wi::mask (inter_prec, false,
4671                                          TYPE_PRECISION (inside_type))); })))
4673     /* If we are converting an integer to a floating-point that can
4674        represent it exactly and back to an integer, we can skip the
4675        floating-point conversion.  */
4676     (if (GIMPLE /* PR66211 */
4677          && inside_int && inter_float && final_int &&
4678          (unsigned) significand_size (TYPE_MODE (inter_type))
4679          >= inside_prec - !inside_unsignedp)
4680      (convert @0)))))))
4682 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4683    float_type.  Only do the transformation if we do not need to preserve
4684    trapping behaviour, so require !flag_trapping_math. */
4685 #if GIMPLE
4686 (simplify
4687    (float (fix_trunc @0))
4688    (if (!flag_trapping_math
4689         && types_match (type, TREE_TYPE (@0))
4690         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4691                                           OPTIMIZE_FOR_BOTH))
4692       (IFN_TRUNC @0)))
4693 #endif
4695 /* If we have a narrowing conversion to an integral type that is fed by a
4696    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4697    masks off bits outside the final type (and nothing else).  */
4698 (simplify
4699   (convert (bit_and @0 INTEGER_CST@1))
4700   (if (INTEGRAL_TYPE_P (type)
4701        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4702        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4703        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4704                                                     TYPE_PRECISION (type)), 0))
4705    (convert @0)))
4708 /* (X /[ex] A) * A -> X.  */
4709 (simplify
4710   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4711   (convert @0))
4713 /* Simplify (A / B) * B + (A % B) -> A.  */
4714 (for div (trunc_div ceil_div floor_div round_div)
4715      mod (trunc_mod ceil_mod floor_mod round_mod)
4716   (simplify
4717    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4718    @0))
4720 /* x / y * y == x -> x % y == 0.  */
4721 (simplify
4722   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4723   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4724     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4726 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4727 (for op (plus minus)
4728  (simplify
4729   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4730   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4731        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4732    (with
4733      {
4734        wi::overflow_type overflow;
4735        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4736                                TYPE_SIGN (type), &overflow);
4737      }
4738      (if (types_match (type, TREE_TYPE (@2))
4739          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4740       (op @0 { wide_int_to_tree (type, mul); })
4741       (with { tree utype = unsigned_type_for (type); }
4742        (convert (op (convert:utype @0)
4743                     (mult (convert:utype @1) (convert:utype @2))))))))))
4745 /* Canonicalization of binary operations.  */
4747 /* Convert X + -C into X - C.  */
4748 (simplify
4749  (plus @0 REAL_CST@1)
4750  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4751   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4752    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4753     (minus @0 { tem; })))))
4755 /* Convert x+x into x*2.  */
4756 (simplify
4757  (plus @0 @0)
4758  (if (SCALAR_FLOAT_TYPE_P (type))
4759   (mult @0 { build_real (type, dconst2); })
4760   (if (INTEGRAL_TYPE_P (type))
4761    (mult @0 { build_int_cst (type, 2); }))))
4763 /* 0 - X  ->  -X.  */
4764 (simplify
4765  (minus integer_zerop @1)
4766  (negate @1))
4767 (simplify
4768  (pointer_diff integer_zerop @1)
4769  (negate (convert @1)))
4771 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4772    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4773    (-ARG1 + ARG0) reduces to -ARG1.  */
4774 (simplify
4775  (minus real_zerop@0 @1)
4776  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4777   (negate @1)))
4779 /* Transform x * -1 into -x.  */
4780 (simplify
4781  (mult @0 integer_minus_onep)
4782  (negate @0))
4784 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4785    signed overflow for CST != 0 && CST != -1.  */
4786 (simplify
4787  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4788  (if (TREE_CODE (@2) != INTEGER_CST
4789       && single_use (@3)
4790       && !integer_zerop (@1) && !integer_minus_onep (@1))
4791   (mult (mult @0 @2) @1)))
4793 /* True if we can easily extract the real and imaginary parts of a complex
4794    number.  */
4795 (match compositional_complex
4796  (convert? (complex @0 @1)))
4798 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4799 (simplify
4800  (complex (realpart @0) (imagpart @0))
4801  @0)
4802 (simplify
4803  (realpart (complex @0 @1))
4804  @0)
4805 (simplify
4806  (imagpart (complex @0 @1))
4807  @1)
4809 /* Sometimes we only care about half of a complex expression.  */
4810 (simplify
4811  (realpart (convert?:s (conj:s @0)))
4812  (convert (realpart @0)))
4813 (simplify
4814  (imagpart (convert?:s (conj:s @0)))
4815  (convert (negate (imagpart @0))))
4816 (for part (realpart imagpart)
4817  (for op (plus minus)
4818   (simplify
4819    (part (convert?:s@2 (op:s @0 @1)))
4820    (convert (op (part @0) (part @1))))))
4821 (simplify
4822  (realpart (convert?:s (CEXPI:s @0)))
4823  (convert (COS @0)))
4824 (simplify
4825  (imagpart (convert?:s (CEXPI:s @0)))
4826  (convert (SIN @0)))
4828 /* conj(conj(x)) -> x  */
4829 (simplify
4830  (conj (convert? (conj @0)))
4831  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4832   (convert @0)))
4834 /* conj({x,y}) -> {x,-y}  */
4835 (simplify
4836  (conj (convert?:s (complex:s @0 @1)))
4837  (with { tree itype = TREE_TYPE (type); }
4838   (complex (convert:itype @0) (negate (convert:itype @1)))))
4840 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4841 (for bswap (BSWAP)
4842  (simplify
4843   (bswap (bswap @0))
4844   @0)
4845  (simplify
4846   (bswap (bit_not (bswap @0)))
4847   (bit_not @0))
4848  (for bitop (bit_xor bit_ior bit_and)
4849   (simplify
4850    (bswap (bitop:c (bswap @0) @1))
4851    (bitop @0 (bswap @1))))
4852  (for cmp (eq ne)
4853   (simplify
4854    (cmp (bswap@2 @0) (bswap @1))
4855    (with { tree ctype = TREE_TYPE (@2); }
4856     (cmp (convert:ctype @0) (convert:ctype @1))))
4857   (simplify
4858    (cmp (bswap @0) INTEGER_CST@1)
4859    (with { tree ctype = TREE_TYPE (@1); }
4860     (cmp (convert:ctype @0) (bswap! @1)))))
4861  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4862  (simplify
4863   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4864            INTEGER_CST@3)
4865    (if (BITS_PER_UNIT == 8
4866         && tree_fits_uhwi_p (@2)
4867         && tree_fits_uhwi_p (@3))
4868     (with
4869      {
4870       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4871       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4872       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4873       unsigned HOST_WIDE_INT lo = bits & 7;
4874       unsigned HOST_WIDE_INT hi = bits - lo;
4875      }
4876      (if (bits < prec
4877           && mask < (256u>>lo)
4878           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4879       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4880        (if (ns == 0)
4881         (bit_and (convert @1) @3)
4882         (with
4883          {
4884           tree utype = unsigned_type_for (TREE_TYPE (@1));
4885           tree nst = build_int_cst (integer_type_node, ns);
4886          }
4887          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4888  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4889  (simplify
4890   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4891    (if (BITS_PER_UNIT == 8
4892         && CHAR_TYPE_SIZE == 8
4893         && tree_fits_uhwi_p (@1))
4894     (with
4895      {
4896       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4897       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4898       /* If the bswap was extended before the original shift, this
4899          byte (shift) has the sign of the extension, not the sign of
4900          the original shift.  */
4901       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4902      }
4903      /* Special case: logical right shift of sign-extended bswap.
4904         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4905      (if (TYPE_PRECISION (type) > prec
4906           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4907           && TYPE_UNSIGNED (type)
4908           && bits < prec && bits + 8 >= prec)
4909       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4910        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4911       (if (bits + 8 == prec)
4912        (if (TYPE_UNSIGNED (st))
4913         (convert (convert:unsigned_char_type_node @0))
4914         (convert (convert:signed_char_type_node @0)))
4915        (if (bits < prec && bits + 8 > prec)
4916         (with 
4917          {
4918           tree nst = build_int_cst (integer_type_node, bits & 7);
4919           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4920                                        : signed_char_type_node;
4921          }
4922          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4923  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4924  (simplify
4925   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4926    (if (BITS_PER_UNIT == 8
4927         && tree_fits_uhwi_p (@1)
4928         && tree_to_uhwi (@1) < 256)
4929     (with
4930      {
4931       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4932       tree utype = unsigned_type_for (TREE_TYPE (@0));
4933       tree nst = build_int_cst (integer_type_node, prec - 8);
4934      }
4935      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4938 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4940 /* Simplify constant conditions.
4941    Only optimize constant conditions when the selected branch
4942    has the same type as the COND_EXPR.  This avoids optimizing
4943    away "c ? x : throw", where the throw has a void type.
4944    Note that we cannot throw away the fold-const.cc variant nor
4945    this one as we depend on doing this transform before possibly
4946    A ? B : B -> B triggers and the fold-const.cc one can optimize
4947    0 ? A : B to B even if A has side-effects.  Something
4948    genmatch cannot handle.  */
4949 (simplify
4950  (cond INTEGER_CST@0 @1 @2)
4951  (if (integer_zerop (@0))
4952   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4953    @2)
4954   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4955    @1)))
4956 (simplify
4957  (vec_cond VECTOR_CST@0 @1 @2)
4958  (if (integer_all_onesp (@0))
4959   @1
4960   (if (integer_zerop (@0))
4961    @2)))
4963 /* Sink unary operations to branches, but only if we do fold both.  */
4964 (for op (negate bit_not abs absu)
4965  (simplify
4966   (op (vec_cond:s @0 @1 @2))
4967   (vec_cond @0 (op! @1) (op! @2))))
4969 /* Sink unary conversions to branches, but only if we do fold both
4970    and the target's truth type is the same as we already have.  */
4971 (simplify
4972  (convert (vec_cond:s @0 @1 @2))
4973  (if (VECTOR_TYPE_P (type)
4974       && types_match (TREE_TYPE (@0), truth_type_for (type)))
4975   (vec_cond @0 (convert! @1) (convert! @2))))
4977 /* Likewise for view_convert of nop_conversions. */
4978 (simplify
4979  (view_convert (vec_cond:s @0 @1 @2))
4980  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4981       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4982                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4983       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4984   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4986 /* Sink binary operation to branches, but only if we can fold it.  */
4987 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4988          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4989          trunc_mod ceil_mod floor_mod round_mod min max)
4990 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4991  (simplify
4992   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4993   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4995 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4996  (simplify
4997   (op (vec_cond:s @0 @1 @2) @3)
4998   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4999  (simplify
5000   (op @3 (vec_cond:s @0 @1 @2))
5001   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5003 #if GIMPLE
5004 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5005  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5006            INTEGER_CST@1)
5007  (with {
5008          int ibit = tree_log2 (@0);
5009          int ibit2 = tree_log2 (@1);
5010        }
5011   (if (ibit == ibit2
5012       && ibit >= 0
5013       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5015 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5016  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5017           INTEGER_CST@1)
5018  (with {
5019          int ibit = tree_log2 (@0);
5020          int ibit2 = tree_log2 (@1);
5021        }
5022   (if (ibit == ibit2
5023       && ibit >= 0
5024       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5026 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5027  (bit_and:c
5028   (convert1?@4
5029    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5030   (convert2? @0))
5031  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5033 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5034  (bit_and:c
5035   (convert1?@4
5036    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5037   (convert2? @0))
5038  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5040 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5041  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5042             INTEGER_CST@1)
5043  (with {
5044          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5045                                               TYPE_PRECISION(type)));
5046          int ibit2 = tree_log2 (@1);
5047        }
5048   (if (ibit == ibit2
5049       && ibit >= 0
5050       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5052 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5053  (bit_and@4
5054   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5055   INTEGER_CST@1)
5056  (with {
5057          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5058                                               TYPE_PRECISION(type)));
5059          int ibit2 = tree_log2 (@1);
5060        }
5061   (if (ibit == ibit2
5062       && ibit >= 0
5063       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5065 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5066  (bit_and:c
5067   (convert1?@3
5068    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5069   (convert2? @0))
5070  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5072 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5073  (bit_and:c
5074   (convert1?@3
5075    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5076   (convert2? @0))
5077   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5079 #endif
5081 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5082    Currently disabled after pass lvec because ARM understands
5083    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5084 #if GIMPLE
5085 /* These can only be done in gimple as fold likes to convert:
5086    (CMP) & N into (CMP) ? N : 0
5087    and we try to match the same pattern again and again. */
5088 (simplify
5089  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5090  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5091   (vec_cond (bit_and @0 @3) @1 @2)))
5092 (simplify
5093  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5094  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5095   (vec_cond (bit_ior @0 @3) @1 @2)))
5096 (simplify
5097  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5098  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5099   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5100 (simplify
5101  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5102  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5103   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5105 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5106 (simplify
5107  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5108  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5109   (vec_cond (bit_and @0 @1) @2 @3)))
5110 (simplify
5111  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5112  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5113   (vec_cond (bit_ior @0 @1) @2 @3)))
5114 (simplify
5115  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5116  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5117   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5118 (simplify
5119  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5120  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5121   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5122 #endif
5124 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5125    types are compatible.  */
5126 (simplify
5127  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5128  (if (VECTOR_BOOLEAN_TYPE_P (type)
5129       && types_match (type, TREE_TYPE (@0)))
5130   (if (integer_zerop (@1) && integer_all_onesp (@2))
5131    (bit_not @0)
5132    (if (integer_all_onesp (@1) && integer_zerop (@2))
5133     @0))))
5135 /* A few simplifications of "a ? CST1 : CST2". */
5136 /* NOTE: Only do this on gimple as the if-chain-to-switch
5137    optimization depends on the gimple to have if statements in it. */
5138 #if GIMPLE
5139 (simplify
5140  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5141  (switch
5142   (if (integer_zerop (@2))
5143    (switch
5144     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5145     (if (integer_onep (@1))
5146      (convert (convert:boolean_type_node @0)))
5147     /* a ? -1 : 0 -> -a.  */
5148     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5149      (if (TYPE_PRECISION (type) == 1)
5150       /* For signed 1-bit precision just cast bool to the type.  */
5151       (convert (convert:boolean_type_node @0))
5152       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5153        (with {
5154           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5155                                                       TYPE_UNSIGNED (type));
5156         }
5157         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5158        (negate (convert:type (convert:boolean_type_node @0))))))
5159     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5160     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5161      (with {
5162        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5163       }
5164       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5165   (if (integer_zerop (@1))
5166    (switch
5167     /* a ? 0 : 1 -> !a.  */
5168     (if (integer_onep (@2))
5169      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5170     /* a ? 0 : -1 -> -(!a).  */
5171     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5172      (if (TYPE_PRECISION (type) == 1)
5173       /* For signed 1-bit precision just cast bool to the type.  */
5174       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5175       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5176        (with {
5177           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5178                                                       TYPE_UNSIGNED (type));
5179         }
5180         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5181                                                 { boolean_true_node; })))))
5182        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5183                                       { boolean_true_node; }))))))
5184     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5185     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5186      (with {
5187        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5188       }
5189       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5190                                 { boolean_true_node; })) { shift; })))))))
5192 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5193    for unsigned types. */
5194 (simplify
5195  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5196  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5197       && bitwise_equal_p (@0, @2))
5198   (convert (eq @0 @1))
5202 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5203    for unsigned types. */
5204 (simplify
5205  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5206  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5207       && bitwise_equal_p (@0, @2))
5208   (convert (eq @0 @1))
5212 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5213    on the first bit of the CST.  */
5214 (simplify
5215  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5216  (if ((wi::to_wide (@1) & 1) != 0)
5217   @2
5218   { build_zero_cst (type); }))
5220 /* Optimize
5221    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5222    x_5 == cstN ? cst4 : cst3
5223    # op is == or != and N is 1 or 2
5224    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5225    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5226    of cst3 and cst4 is smaller.
5227    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5228 (for eqne (ne eq)
5229  (simplify
5230   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5231   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5232        && INTEGRAL_TYPE_P (type)
5233        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5234            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5235    (with {
5236      value_range r;
5237      get_range_query (cfun)->range_of_expr (r, @0);
5238      if (r.undefined_p ())
5239        r.set_varying (TREE_TYPE (@0));
5241      wide_int min = r.lower_bound ();
5242      wide_int max = r.upper_bound ();
5243     }
5244     (if (min + 1 == max
5245          && (wi::to_wide (@1) == min
5246              || wi::to_wide (@1) == max))
5247      (with {
5248        tree arg0 = @2, arg1 = @3;
5249        tree type1;
5250        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5251          std::swap (arg0, arg1);
5252        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5253          type1 = TREE_TYPE (@0);
5254        else
5255          type1 = type;
5256        auto prec = TYPE_PRECISION (type1);
5257        auto unsign = TYPE_UNSIGNED (type1);
5258        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5259         type1 = build_nonstandard_integer_type (prec, unsign);
5260        min = wide_int::from (min, prec,
5261                              TYPE_SIGN (TREE_TYPE (@0)));
5262        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5263                                     TYPE_SIGN (type));
5264        enum tree_code code;
5265        wi::overflow_type ovf;
5266        if (tree_int_cst_lt (arg0, arg1))
5267          {
5268            code = PLUS_EXPR;
5269            a -= min;
5270            if (!unsign)
5271              {
5272                /* lhs is known to be in range [min, min+1] and we want to add a
5273                   to it.  Check if that operation can overflow for those 2 values
5274                   and if yes, force unsigned type.  */
5275                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5276                if (ovf)
5277                  type1 = unsigned_type_for (type1);
5278              }
5279          }
5280        else
5281          {
5282            code = MINUS_EXPR;
5283            a += min;
5284            if (!unsign)
5285              {
5286                /* lhs is known to be in range [min, min+1] and we want to subtract
5287                   it from a.  Check if that operation can overflow for those 2
5288                   values and if yes, force unsigned type.  */
5289                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5290                if (ovf)
5291                 type1 = unsigned_type_for (type1);
5292              }
5293          }
5294        tree arg = wide_int_to_tree (type1, a);
5295       }
5296       (if (code == PLUS_EXPR)
5297        (convert (plus (convert:type1 @0) { arg; }))
5298        (convert (minus { arg; } (convert:type1 @0))))))))))
5299 #endif
5301 (simplify
5302  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5303  (if (INTEGRAL_TYPE_P (type)
5304       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5305   (cond @1 (convert @2) (convert @3))))
5307 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5308    be extended.  */
5309 /* This pattern implements two kinds simplification:
5311    Case 1)
5312    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5313      1) Conversions are type widening from smaller type.
5314      2) Const c1 equals to c2 after canonicalizing comparison.
5315      3) Comparison has tree code LT, LE, GT or GE.
5316    This specific pattern is needed when (cmp (convert x) c) may not
5317    be simplified by comparison patterns because of multiple uses of
5318    x.  It also makes sense here because simplifying across multiple
5319    referred var is always benefitial for complicated cases.
5321    Case 2)
5322    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5323 (for cmp (lt le gt ge eq ne)
5324  (simplify
5325   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5326   (with
5327    {
5328      tree from_type = TREE_TYPE (@1);
5329      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5330      enum tree_code code = ERROR_MARK;
5332      if (INTEGRAL_TYPE_P (from_type)
5333          && int_fits_type_p (@2, from_type)
5334          && (types_match (c1_type, from_type)
5335              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5336                  && (TYPE_UNSIGNED (from_type)
5337                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5338          && (types_match (c2_type, from_type)
5339              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5340                  && (TYPE_UNSIGNED (from_type)
5341                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5342        {
5343          if (cmp != EQ_EXPR)
5344            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5345          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5346          else if (int_fits_type_p (@3, from_type))
5347            code = EQ_EXPR;
5348        }
5349    }
5350    (if (code == MAX_EXPR)
5351     (convert (max @1 (convert @2)))
5352     (if (code == MIN_EXPR)
5353      (convert (min @1 (convert @2)))
5354      (if (code == EQ_EXPR)
5355       (convert (cond (eq @1 (convert @3))
5356                      (convert:from_type @3) (convert:from_type @2)))))))))
5358 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5360      1) OP is PLUS or MINUS.
5361      2) CMP is LT, LE, GT or GE.
5362      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5364    This pattern also handles special cases like:
5366      A) Operand x is a unsigned to signed type conversion and c1 is
5367         integer zero.  In this case,
5368           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5369           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5370      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5371         check equality for (c1+1) and (c1-1) by adjusting comparison
5372         code.
5374    TODO: Though signed type is handled by this pattern, it cannot be
5375    simplified at the moment because C standard requires additional
5376    type promotion.  In order to match&simplify it here, the IR needs
5377    to be cleaned up by other optimizers, i.e, VRP.  */
5378 (for op (plus minus)
5379  (for cmp (lt le gt ge)
5380   (simplify
5381    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5382    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5383     (if (types_match (from_type, to_type)
5384          /* Check if it is special case A).  */
5385          || (TYPE_UNSIGNED (from_type)
5386              && !TYPE_UNSIGNED (to_type)
5387              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5388              && integer_zerop (@1)
5389              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5390      (with
5391       {
5392         wi::overflow_type overflow = wi::OVF_NONE;
5393         enum tree_code code, cmp_code = cmp;
5394         wide_int real_c1;
5395         wide_int c1 = wi::to_wide (@1);
5396         wide_int c2 = wi::to_wide (@2);
5397         wide_int c3 = wi::to_wide (@3);
5398         signop sgn = TYPE_SIGN (from_type);
5400         /* Handle special case A), given x of unsigned type:
5401             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5402             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5403         if (!types_match (from_type, to_type))
5404           {
5405             if (cmp_code == LT_EXPR)
5406               cmp_code = GT_EXPR;
5407             if (cmp_code == GE_EXPR)
5408               cmp_code = LE_EXPR;
5409             c1 = wi::max_value (to_type);
5410           }
5411         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5412            compute (c3 op' c2) and check if it equals to c1 with op' being
5413            the inverted operator of op.  Make sure overflow doesn't happen
5414            if it is undefined.  */
5415         if (op == PLUS_EXPR)
5416           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5417         else
5418           real_c1 = wi::add (c3, c2, sgn, &overflow);
5420         code = cmp_code;
5421         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5422           {
5423             /* Check if c1 equals to real_c1.  Boundary condition is handled
5424                by adjusting comparison operation if necessary.  */
5425             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5426                 && !overflow)
5427               {
5428                 /* X <= Y - 1 equals to X < Y.  */
5429                 if (cmp_code == LE_EXPR)
5430                   code = LT_EXPR;
5431                 /* X > Y - 1 equals to X >= Y.  */
5432                 if (cmp_code == GT_EXPR)
5433                   code = GE_EXPR;
5434               }
5435             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5436                 && !overflow)
5437               {
5438                 /* X < Y + 1 equals to X <= Y.  */
5439                 if (cmp_code == LT_EXPR)
5440                   code = LE_EXPR;
5441                 /* X >= Y + 1 equals to X > Y.  */
5442                 if (cmp_code == GE_EXPR)
5443                   code = GT_EXPR;
5444               }
5445             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5446               {
5447                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5448                   code = MIN_EXPR;
5449                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5450                   code = MAX_EXPR;
5451               }
5452           }
5453       }
5454       (if (code == MAX_EXPR)
5455        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5456            { wide_int_to_tree (from_type, c2); })
5457        (if (code == MIN_EXPR)
5458         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5459             { wide_int_to_tree (from_type, c2); })))))))))
5461 #if GIMPLE
5462 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5463    in fold_cond_expr_with_comparison for GENERIC folding with
5464    some extra constraints.  */
5465 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5466  (simplify
5467   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5468         (convert3? @0) (convert4? @1))
5469   (if (!HONOR_SIGNED_ZEROS (type)
5470        && (/* Allow widening conversions of the compare operands as data.  */
5471            (INTEGRAL_TYPE_P (type)
5472             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5473             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5474             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5475             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5476            /* Or sign conversions for the comparison.  */
5477            || (types_match (type, TREE_TYPE (@0))
5478                && types_match (type, TREE_TYPE (@1)))))
5479    (switch
5480     (if (cmp == EQ_EXPR)
5481      (if (VECTOR_TYPE_P (type))
5482       (view_convert @c1)
5483       (convert @c1)))
5484     (if (cmp == NE_EXPR)
5485      (if (VECTOR_TYPE_P (type))
5486       (view_convert @c0)
5487       (convert @c0)))
5488     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5489      (if (!HONOR_NANS (type))
5490       (if (VECTOR_TYPE_P (type))
5491        (view_convert (min @c0 @c1))
5492        (convert (min @c0 @c1)))))
5493     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5494      (if (!HONOR_NANS (type))
5495       (if (VECTOR_TYPE_P (type))
5496        (view_convert (max @c0 @c1))
5497        (convert (max @c0 @c1)))))
5498     (if (cmp == UNEQ_EXPR)
5499      (if (!HONOR_NANS (type))
5500       (if (VECTOR_TYPE_P (type))
5501        (view_convert @c1)
5502        (convert @c1))))
5503     (if (cmp == LTGT_EXPR)
5504      (if (!HONOR_NANS (type))
5505       (if (VECTOR_TYPE_P (type))
5506        (view_convert @c0)
5507        (convert @c0))))))))
5508 #endif
5510 (for cnd (cond vec_cond)
5511  /* (a != b) ? (a - b) : 0 -> (a - b) */
5512  (simplify
5513   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5514   @2)
5515  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5516  (simplify
5517   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5518   @2)
5519  /* (a != b) ? (a & b) : a -> (a & b) */
5520  /* (a != b) ? (a | b) : a -> (a | b) */
5521  /* (a != b) ? min(a,b) : a -> min(a,b) */
5522  /* (a != b) ? max(a,b) : a -> max(a,b) */
5523  (for op (bit_and bit_ior min max)
5524   (simplify
5525    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5526    @2))
5527  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5528  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5529  (for op (mult plus)
5530   (simplify
5531    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5532    (if (ANY_INTEGRAL_TYPE_P (type))
5533     @2)))
5534  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5535  (simplify
5536   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5537   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5538    @2))
5541 /* These was part of minmax phiopt.  */
5542 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5543    to minmax<min/max<a, b>, c> */
5544 (for minmax (min max)
5545  (for cmp (lt le gt ge ne)
5546   (simplify
5547    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5548    (with
5549     {
5550       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5551     }
5552     (if (code == MIN_EXPR)
5553      (minmax (min @1 @2) @4)
5554      (if (code == MAX_EXPR)
5555       (minmax (max @1 @2) @4)))))))
5557 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5558 (for cmp    (gt  ge  lt  le)
5559      minmax (min min max max)
5560  (simplify
5561   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5562    (with
5563     {
5564       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5565     }
5566     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5567          && code == MIN_EXPR
5568          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5569      (min @2 @4)
5570      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5571           && code == MAX_EXPR
5572           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5573       (max @2 @4))))))
5575 #if GIMPLE
5576 /* These patterns should be after min/max detection as simplifications
5577    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5578    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5579    Even without those, reaching min/max/and/ior faster is better.  */
5580 (simplify
5581  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5582  (switch
5583   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5584   (if (integer_zerop (@2))
5585    (bit_and (convert @0) @1))
5586   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5587   (if (integer_zerop (@1))
5588    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5589   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5590   (if (integer_onep (@1))
5591    (bit_ior (convert @0) @2))
5592   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5593   (if (integer_onep (@2))
5594    (bit_ior (bit_xor (convert @0) @2) @1))
5597 #endif
5599 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5600 (simplify
5601  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5602  (if (!TYPE_SATURATING (type)
5603       && (TYPE_OVERFLOW_WRAPS (type)
5604           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5605       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5606   @3))
5608 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5609 (simplify
5610  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5611  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5612   @3))
5614 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5615    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5616 (simplify
5617  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5618  (if (TYPE_UNSIGNED (type))
5619   (cond (ge @0 @1) (negate @0) @2)))
5621 (for cnd (cond vec_cond)
5622  /* A ? B : (A ? X : C) -> A ? B : C.  */
5623  (simplify
5624   (cnd @0 (cnd @0 @1 @2) @3)
5625   (cnd @0 @1 @3))
5626  (simplify
5627   (cnd @0 @1 (cnd @0 @2 @3))
5628   (cnd @0 @1 @3))
5629  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5630  /* ???  This matches embedded conditions open-coded because genmatch
5631     would generate matching code for conditions in separate stmts only.
5632     The following is still important to merge then and else arm cases
5633     from if-conversion.  */
5634  (simplify
5635   (cnd @0 @1 (cnd @2 @3 @4))
5636   (if (inverse_conditions_p (@0, @2))
5637    (cnd @0 @1 @3)))
5638  (simplify
5639   (cnd @0 (cnd @1 @2 @3) @4)
5640   (if (inverse_conditions_p (@0, @1))
5641    (cnd @0 @3 @4)))
5643  /* A ? B : B -> B.  */
5644  (simplify
5645   (cnd @0 @1 @1)
5646   @1)
5648  /* !A ? B : C -> A ? C : B.  */
5649  (simplify
5650   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5651   (cnd @0 @2 @1)))
5653 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5654    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5655    Need to handle UN* comparisons.
5657    None of these transformations work for modes with signed
5658    zeros.  If A is +/-0, the first two transformations will
5659    change the sign of the result (from +0 to -0, or vice
5660    versa).  The last four will fix the sign of the result,
5661    even though the original expressions could be positive or
5662    negative, depending on the sign of A.
5664    Note that all these transformations are correct if A is
5665    NaN, since the two alternatives (A and -A) are also NaNs.  */
5667 (for cnd (cond vec_cond)
5668  /* A == 0 ? A : -A    same as -A */
5669  (for cmp (eq uneq)
5670   (simplify
5671    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5672     (if (!HONOR_SIGNED_ZEROS (type)
5673          && bitwise_equal_p (@0, @2))
5674      @1))
5675   (simplify
5676    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5677     (if (!HONOR_SIGNED_ZEROS (type)
5678          && bitwise_equal_p (@0, @2))
5679      @1))
5681  /* A != 0 ? A : -A    same as A */
5682  (for cmp (ne ltgt)
5683   (simplify
5684    (cnd (cmp @0 zerop) @1 (negate @1))
5685     (if (!HONOR_SIGNED_ZEROS (type)
5686          && bitwise_equal_p (@0, @1))
5687      @1))
5688   (simplify
5689    (cnd (cmp @0 zerop) @1 integer_zerop)
5690     (if (!HONOR_SIGNED_ZEROS (type)
5691          && bitwise_equal_p (@0, @1))
5692      @1))
5694  /* A >=/> 0 ? A : -A    same as abs (A) */
5695  (for cmp (ge gt)
5696   (simplify
5697    (cnd (cmp @0 zerop) @1 (negate @1))
5698     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5699          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5700          && bitwise_equal_p (@0, @1))
5701      (if (TYPE_UNSIGNED (type))
5702       (absu:type @0)
5703       (abs @0)))))
5704  /* A <=/< 0 ? A : -A    same as -abs (A) */
5705  (for cmp (le lt)
5706   (simplify
5707    (cnd (cmp @0 zerop) @1 (negate @1))
5708     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5709          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5710          && bitwise_equal_p (@0, @1))
5711      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5712            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5713           || TYPE_UNSIGNED (type))
5714       (with {
5715         tree utype = unsigned_type_for (TREE_TYPE(@0));
5716        }
5717        (convert (negate (absu:utype @0))))
5718        (negate (abs @0)))))
5722 /* -(type)!A -> (type)A - 1.  */
5723 (simplify
5724  (negate (convert?:s (logical_inverted_value:s @0)))
5725  (if (INTEGRAL_TYPE_P (type)
5726       && TREE_CODE (type) != BOOLEAN_TYPE
5727       && TYPE_PRECISION (type) > 1
5728       && TREE_CODE (@0) == SSA_NAME
5729       && ssa_name_has_boolean_range (@0))
5730   (plus (convert:type @0) { build_all_ones_cst (type); })))
5732 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5733    return all -1 or all 0 results.  */
5734 /* ??? We could instead convert all instances of the vec_cond to negate,
5735    but that isn't necessarily a win on its own.  */
5736 (simplify
5737  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5738  (if (VECTOR_TYPE_P (type)
5739       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5740                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5741       && (TYPE_MODE (TREE_TYPE (type))
5742           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5743   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5745 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5746 (simplify
5747  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5748  (if (VECTOR_TYPE_P (type)
5749       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5750                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5751       && (TYPE_MODE (TREE_TYPE (type))
5752           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5753   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5756 /* Simplifications of comparisons.  */
5758 /* See if we can reduce the magnitude of a constant involved in a
5759    comparison by changing the comparison code.  This is a canonicalization
5760    formerly done by maybe_canonicalize_comparison_1.  */
5761 (for cmp  (le gt)
5762      acmp (lt ge)
5763  (simplify
5764   (cmp @0 uniform_integer_cst_p@1)
5765   (with { tree cst = uniform_integer_cst_p (@1); }
5766    (if (tree_int_cst_sgn (cst) == -1)
5767      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5768                                    wide_int_to_tree (TREE_TYPE (cst),
5769                                                      wi::to_wide (cst)
5770                                                      + 1)); })))))
5771 (for cmp  (ge lt)
5772      acmp (gt le)
5773  (simplify
5774   (cmp @0 uniform_integer_cst_p@1)
5775   (with { tree cst = uniform_integer_cst_p (@1); }
5776    (if (tree_int_cst_sgn (cst) == 1)
5777     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5778                                   wide_int_to_tree (TREE_TYPE (cst),
5779                                   wi::to_wide (cst) - 1)); })))))
5781 /* We can simplify a logical negation of a comparison to the
5782    inverted comparison.  As we cannot compute an expression
5783    operator using invert_tree_comparison we have to simulate
5784    that with expression code iteration.  */
5785 (for cmp (tcc_comparison)
5786      icmp (inverted_tcc_comparison)
5787      ncmp (inverted_tcc_comparison_with_nans)
5788  /* Ideally we'd like to combine the following two patterns
5789     and handle some more cases by using
5790       (logical_inverted_value (cmp @0 @1))
5791     here but for that genmatch would need to "inline" that.
5792     For now implement what forward_propagate_comparison did.  */
5793  (simplify
5794   (bit_not (cmp @0 @1))
5795   (if (VECTOR_TYPE_P (type)
5796        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5797    /* Comparison inversion may be impossible for trapping math,
5798       invert_tree_comparison will tell us.  But we can't use
5799       a computed operator in the replacement tree thus we have
5800       to play the trick below.  */
5801    (with { enum tree_code ic = invert_tree_comparison
5802              (cmp, HONOR_NANS (@0)); }
5803     (if (ic == icmp)
5804      (icmp @0 @1)
5805      (if (ic == ncmp)
5806       (ncmp @0 @1))))))
5807  (simplify
5808   (bit_xor (cmp @0 @1) integer_truep)
5809   (with { enum tree_code ic = invert_tree_comparison
5810             (cmp, HONOR_NANS (@0)); }
5811    (if (ic == icmp)
5812     (icmp @0 @1)
5813     (if (ic == ncmp)
5814      (ncmp @0 @1)))))
5815  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5816  (simplify
5817   (ne (cmp@2 @0 @1) integer_zerop)
5818   (if (types_match (type, TREE_TYPE (@2)))
5819    (cmp @0 @1)))
5820  (simplify
5821   (eq (cmp@2 @0 @1) integer_truep)
5822   (if (types_match (type, TREE_TYPE (@2)))
5823    (cmp @0 @1)))
5824  (simplify
5825   (ne (cmp@2 @0 @1) integer_truep)
5826   (if (types_match (type, TREE_TYPE (@2)))
5827    (with { enum tree_code ic = invert_tree_comparison
5828              (cmp, HONOR_NANS (@0)); }
5829     (if (ic == icmp)
5830      (icmp @0 @1)
5831      (if (ic == ncmp)
5832       (ncmp @0 @1))))))
5833  (simplify
5834   (eq (cmp@2 @0 @1) integer_zerop)
5835   (if (types_match (type, TREE_TYPE (@2)))
5836    (with { enum tree_code ic = invert_tree_comparison
5837              (cmp, HONOR_NANS (@0)); }
5838     (if (ic == icmp)
5839      (icmp @0 @1)
5840      (if (ic == ncmp)
5841       (ncmp @0 @1)))))))
5843 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5844    ??? The transformation is valid for the other operators if overflow
5845    is undefined for the type, but performing it here badly interacts
5846    with the transformation in fold_cond_expr_with_comparison which
5847    attempts to synthetize ABS_EXPR.  */
5848 (for cmp (eq ne)
5849  (for sub (minus pointer_diff)
5850   (simplify
5851    (cmp (sub@2 @0 @1) integer_zerop)
5852    (if (single_use (@2))
5853     (cmp @0 @1)))))
5855 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5856    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5857 (for cmp (lt ge)
5858  (simplify
5859   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5860    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5861         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5862         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5863     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5864 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5865    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5866 (simplify
5867  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5868   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5869        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5870        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5871    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5873 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5874    signed arithmetic case.  That form is created by the compiler
5875    often enough for folding it to be of value.  One example is in
5876    computing loop trip counts after Operator Strength Reduction.  */
5877 (for cmp (simple_comparison)
5878      scmp (swapped_simple_comparison)
5879  (simplify
5880   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5881   /* Handle unfolded multiplication by zero.  */
5882   (if (integer_zerop (@1))
5883    (cmp @1 @2)
5884    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5885         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5886         && single_use (@3))
5887     /* If @1 is negative we swap the sense of the comparison.  */
5888     (if (tree_int_cst_sgn (@1) < 0)
5889      (scmp @0 @2)
5890      (cmp @0 @2))))))
5892 /* For integral types with undefined overflow fold
5893    x * C1 == C2 into x == C2 / C1 or false.
5894    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5895    Z / 2^n Z.  */
5896 (for cmp (eq ne)
5897  (simplify
5898   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5899   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5900        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5901        && wi::to_wide (@1) != 0)
5902    (with { widest_int quot; }
5903     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5904                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5905      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5906      { constant_boolean_node (cmp == NE_EXPR, type); }))
5907    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5908         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5909         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5910     (cmp @0
5911      {
5912        tree itype = TREE_TYPE (@0);
5913        int p = TYPE_PRECISION (itype);
5914        wide_int m = wi::one (p + 1) << p;
5915        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5916        wide_int i = wide_int::from (wi::mod_inv (a, m),
5917                                     p, TYPE_SIGN (itype));
5918        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5919      })))))
5921 /* Simplify comparison of something with itself.  For IEEE
5922    floating-point, we can only do some of these simplifications.  */
5923 (for cmp (eq ge le)
5924  (simplify
5925   (cmp @0 @0)
5926   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5927        || ! tree_expr_maybe_nan_p (@0))
5928    { constant_boolean_node (true, type); }
5929    (if (cmp != EQ_EXPR
5930         /* With -ftrapping-math conversion to EQ loses an exception.  */
5931         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5932             || ! flag_trapping_math))
5933     (eq @0 @0)))))
5934 (for cmp (ne gt lt)
5935  (simplify
5936   (cmp @0 @0)
5937   (if (cmp != NE_EXPR
5938        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5939        || ! tree_expr_maybe_nan_p (@0))
5940    { constant_boolean_node (false, type); })))
5941 (for cmp (unle unge uneq)
5942  (simplify
5943   (cmp @0 @0)
5944   { constant_boolean_node (true, type); }))
5945 (for cmp (unlt ungt)
5946  (simplify
5947   (cmp @0 @0)
5948   (unordered @0 @0)))
5949 (simplify
5950  (ltgt @0 @0)
5951  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5952   { constant_boolean_node (false, type); }))
5954 /* x == ~x -> false */
5955 /* x != ~x -> true */
5956 (for cmp (eq ne)
5957  (simplify
5958   (cmp:c @0 (bit_not @0))
5959   { constant_boolean_node (cmp == NE_EXPR, type); }))
5961 /* Fold ~X op ~Y as Y op X.  */
5962 (for cmp (simple_comparison)
5963  (simplify
5964   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
5965   (if (single_use (@2) && single_use (@3))
5966    (with { tree otype = TREE_TYPE (@4); }
5967     (cmp (convert:otype @1) (convert:otype @0))))))
5969 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5970 (for cmp (simple_comparison)
5971      scmp (swapped_simple_comparison)
5972  (simplify
5973   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
5974   (if (single_use (@2)
5975        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5976    (with { tree otype = TREE_TYPE (@1); }
5977     (scmp (convert:otype @0) (bit_not @1))))))
5979 (for cmp (simple_comparison)
5980  (simplify
5981   (cmp @0 REAL_CST@1)
5982   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5983   (switch
5984    /* a CMP (-0) -> a CMP 0  */
5985    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5986     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5987    /* (-0) CMP b -> 0 CMP b.  */
5988    (if (TREE_CODE (@0) == REAL_CST
5989         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5990     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5991    /* x != NaN is always true, other ops are always false.  */
5992    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5993         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5994         && !tree_expr_signaling_nan_p (@1)
5995         && !tree_expr_maybe_signaling_nan_p (@0))
5996     { constant_boolean_node (cmp == NE_EXPR, type); })
5997    /* NaN != y is always true, other ops are always false.  */
5998    (if (TREE_CODE (@0) == REAL_CST
5999         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6000         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6001         && !tree_expr_signaling_nan_p (@0)
6002         && !tree_expr_signaling_nan_p (@1))
6003     { constant_boolean_node (cmp == NE_EXPR, type); })
6004    /* Fold comparisons against infinity.  */
6005    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6006         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6007     (with
6008      {
6009        REAL_VALUE_TYPE max;
6010        enum tree_code code = cmp;
6011        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6012        if (neg)
6013          code = swap_tree_comparison (code);
6014      }
6015      (switch
6016       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6017       (if (code == GT_EXPR
6018            && !(HONOR_NANS (@0) && flag_trapping_math))
6019        { constant_boolean_node (false, type); })
6020       (if (code == LE_EXPR)
6021        /* x <= +Inf is always true, if we don't care about NaNs.  */
6022        (if (! HONOR_NANS (@0))
6023         { constant_boolean_node (true, type); }
6024         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6025            an "invalid" exception.  */
6026         (if (!flag_trapping_math)
6027          (eq @0 @0))))
6028       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6029          for == this introduces an exception for x a NaN.  */
6030       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6031            || code == GE_EXPR)
6032        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6033         (if (neg)
6034          (lt @0 { build_real (TREE_TYPE (@0), max); })
6035          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6036       /* x < +Inf is always equal to x <= DBL_MAX.  */
6037       (if (code == LT_EXPR)
6038        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6039         (if (neg)
6040          (ge @0 { build_real (TREE_TYPE (@0), max); })
6041          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6042       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6043          an exception for x a NaN so use an unordered comparison.  */
6044       (if (code == NE_EXPR)
6045        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6046         (if (! HONOR_NANS (@0))
6047          (if (neg)
6048           (ge @0 { build_real (TREE_TYPE (@0), max); })
6049           (le @0 { build_real (TREE_TYPE (@0), max); }))
6050          (if (neg)
6051           (unge @0 { build_real (TREE_TYPE (@0), max); })
6052           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6054  /* If this is a comparison of a real constant with a PLUS_EXPR
6055     or a MINUS_EXPR of a real constant, we can convert it into a
6056     comparison with a revised real constant as long as no overflow
6057     occurs when unsafe_math_optimizations are enabled.  */
6058  (if (flag_unsafe_math_optimizations)
6059   (for op (plus minus)
6060    (simplify
6061     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6062     (with
6063      {
6064        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6065                                TREE_TYPE (@1), @2, @1);
6066      }
6067      (if (tem && !TREE_OVERFLOW (tem))
6068       (cmp @0 { tem; }))))))
6070  /* Likewise, we can simplify a comparison of a real constant with
6071     a MINUS_EXPR whose first operand is also a real constant, i.e.
6072     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6073     floating-point types only if -fassociative-math is set.  */
6074  (if (flag_associative_math)
6075   (simplify
6076    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6077    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6078     (if (tem && !TREE_OVERFLOW (tem))
6079      (cmp { tem; } @1)))))
6081  /* Fold comparisons against built-in math functions.  */
6082  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6083   (for sq (SQRT)
6084    (simplify
6085     (cmp (sq @0) REAL_CST@1)
6086     (switch
6087      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6088       (switch
6089        /* sqrt(x) < y is always false, if y is negative.  */
6090        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6091         { constant_boolean_node (false, type); })
6092        /* sqrt(x) > y is always true, if y is negative and we
6093           don't care about NaNs, i.e. negative values of x.  */
6094        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6095         { constant_boolean_node (true, type); })
6096        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6097        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6098      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6099       (switch
6100        /* sqrt(x) < 0 is always false.  */
6101        (if (cmp == LT_EXPR)
6102         { constant_boolean_node (false, type); })
6103        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6104        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6105         { constant_boolean_node (true, type); })
6106        /* sqrt(x) <= 0 -> x == 0.  */
6107        (if (cmp == LE_EXPR)
6108         (eq @0 @1))
6109        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6110           == or !=.  In the last case:
6112             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6114           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6115           the results for other x follow from natural arithmetic.  */
6116        (cmp @0 @1)))
6117      (if ((cmp == LT_EXPR
6118            || cmp == LE_EXPR
6119            || cmp == GT_EXPR
6120            || cmp == GE_EXPR)
6121           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6122           /* Give up for -frounding-math.  */
6123           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6124       (with
6125        {
6126          REAL_VALUE_TYPE c2;
6127          enum tree_code ncmp = cmp;
6128          const real_format *fmt
6129            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6130          real_arithmetic (&c2, MULT_EXPR,
6131                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6132          real_convert (&c2, fmt, &c2);
6133          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6134             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6135          if (!REAL_VALUE_ISINF (c2))
6136            {
6137              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6138                                         build_real (TREE_TYPE (@0), c2));
6139              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6140                ncmp = ERROR_MARK;
6141              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6142                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6143                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6144              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6145                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6146                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6147              else
6148                {
6149                  /* With rounding to even, sqrt of up to 3 different values
6150                     gives the same normal result, so in some cases c2 needs
6151                     to be adjusted.  */
6152                  REAL_VALUE_TYPE c2alt, tow;
6153                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6154                    tow = dconst0;
6155                  else
6156                    tow = dconstinf;
6157                  real_nextafter (&c2alt, fmt, &c2, &tow);
6158                  real_convert (&c2alt, fmt, &c2alt);
6159                  if (REAL_VALUE_ISINF (c2alt))
6160                    ncmp = ERROR_MARK;
6161                  else
6162                    {
6163                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6164                                            build_real (TREE_TYPE (@0), c2alt));
6165                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6166                        ncmp = ERROR_MARK;
6167                      else if (real_equal (&TREE_REAL_CST (c3),
6168                                           &TREE_REAL_CST (@1)))
6169                        c2 = c2alt;
6170                    }
6171                }
6172            }
6173        }
6174        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6175         (if (REAL_VALUE_ISINF (c2))
6176          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6177          (if (HONOR_INFINITIES (@0))
6178           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6179           { constant_boolean_node (false, type); })
6180          /* sqrt(x) > c is the same as x > c*c.  */
6181          (if (ncmp != ERROR_MARK)
6182           (if (ncmp == GE_EXPR)
6183            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6184            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6185         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6186         (if (REAL_VALUE_ISINF (c2))
6187          (switch
6188           /* sqrt(x) < y is always true, when y is a very large
6189              value and we don't care about NaNs or Infinities.  */
6190           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6191            { constant_boolean_node (true, type); })
6192           /* sqrt(x) < y is x != +Inf when y is very large and we
6193              don't care about NaNs.  */
6194           (if (! HONOR_NANS (@0))
6195            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6196           /* sqrt(x) < y is x >= 0 when y is very large and we
6197              don't care about Infinities.  */
6198           (if (! HONOR_INFINITIES (@0))
6199            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6200           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6201           (if (GENERIC)
6202            (truth_andif
6203             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6204             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6205          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6206          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6207           (if (ncmp == LT_EXPR)
6208            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6209            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6210           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6211           (if (ncmp != ERROR_MARK && GENERIC)
6212            (if (ncmp == LT_EXPR)
6213             (truth_andif
6214              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6215              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6216             (truth_andif
6217              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6218              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6219    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6220    (simplify
6221     (cmp (sq @0) (sq @1))
6222       (if (! HONOR_NANS (@0))
6223         (cmp @0 @1))))))
6225 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6226 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6227      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6228  (simplify
6229   (cmp (float@0 @1) (float @2))
6230    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6231         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6232     (with
6233      {
6234        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6235        tree type1 = TREE_TYPE (@1);
6236        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6237        tree type2 = TREE_TYPE (@2);
6238        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6239      }
6240      (if (fmt.can_represent_integral_type_p (type1)
6241           && fmt.can_represent_integral_type_p (type2))
6242       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6243        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6244        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6245             && type1_signed_p >= type2_signed_p)
6246         (icmp @1 (convert @2))
6247         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6248              && type1_signed_p <= type2_signed_p)
6249          (icmp (convert:type2 @1) @2)
6250          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6251               && type1_signed_p == type2_signed_p)
6252           (icmp @1 @2))))))))))
6254 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6255 (for cmp  (lt le eq ne ge gt)
6256      icmp (le le eq ne ge ge)
6257  (simplify
6258   (cmp (float @0) REAL_CST@1)
6259    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6260         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6261     (with
6262      {
6263        tree itype = TREE_TYPE (@0);
6264        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6265        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6266        /* Be careful to preserve any potential exceptions due to
6267           NaNs.  qNaNs are ok in == or != context.
6268           TODO: relax under -fno-trapping-math or
6269           -fno-signaling-nans.  */
6270        bool exception_p
6271          = real_isnan (cst) && (cst->signalling
6272                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6273      }
6274      /* TODO: allow non-fitting itype and SNaNs when
6275         -fno-trapping-math.  */
6276      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6277       (with
6278        {
6279          signop isign = TYPE_SIGN (itype);
6280          REAL_VALUE_TYPE imin, imax;
6281          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6282          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6284          REAL_VALUE_TYPE icst;
6285          if (cmp == GT_EXPR || cmp == GE_EXPR)
6286            real_ceil (&icst, fmt, cst);
6287          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6288            real_floor (&icst, fmt, cst);
6289          else
6290            real_trunc (&icst, fmt, cst);
6292          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6294          bool overflow_p = false;
6295          wide_int icst_val
6296            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6297        }
6298        (switch
6299         /* Optimize cases when CST is outside of ITYPE's range.  */
6300         (if (real_compare (LT_EXPR, cst, &imin))
6301          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6302                                   type); })
6303         (if (real_compare (GT_EXPR, cst, &imax))
6304          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6305                                   type); })
6306         /* Remove cast if CST is an integer representable by ITYPE.  */
6307         (if (cst_int_p)
6308          (cmp @0 { gcc_assert (!overflow_p);
6309                    wide_int_to_tree (itype, icst_val); })
6310         )
6311         /* When CST is fractional, optimize
6312             (FTYPE) N == CST -> 0
6313             (FTYPE) N != CST -> 1.  */
6314         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6315          { constant_boolean_node (cmp == NE_EXPR, type); })
6316         /* Otherwise replace with sensible integer constant.  */
6317         (with
6318          {
6319            gcc_checking_assert (!overflow_p);
6320          }
6321          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6323 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6324 (for cmp (eq ne)
6325  (simplify
6326   (cmp (exact_div @0 @1) INTEGER_CST@2)
6327   (if (!integer_zerop (@1))
6328    (if (wi::to_wide (@2) == 0)
6329     (cmp @0 @2)
6330     (if (TREE_CODE (@1) == INTEGER_CST)
6331      (with
6332       {
6333         wi::overflow_type ovf;
6334         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6335                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6336       }
6337       (if (ovf)
6338        { constant_boolean_node (cmp == NE_EXPR, type); }
6339        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6340 (for cmp (lt le gt ge)
6341  (simplify
6342   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6343   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6344    (with
6345     {
6346       wi::overflow_type ovf;
6347       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6348                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6349     }
6350     (if (ovf)
6351      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6352                                         TYPE_SIGN (TREE_TYPE (@2)))
6353                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6354      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6356 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6358    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6359    For large C (more than min/B+2^size), this is also true, with the
6360    multiplication computed modulo 2^size.
6361    For intermediate C, this just tests the sign of A.  */
6362 (for cmp  (lt le gt ge)
6363      cmp2 (ge ge lt lt)
6364  (simplify
6365   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6366   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6367        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6368        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6369    (with
6370     {
6371       tree utype = TREE_TYPE (@2);
6372       wide_int denom = wi::to_wide (@1);
6373       wide_int right = wi::to_wide (@2);
6374       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6375       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6376       bool small = wi::leu_p (right, smax);
6377       bool large = wi::geu_p (right, smin);
6378     }
6379     (if (small || large)
6380      (cmp (convert:utype @0) (mult @2 (convert @1)))
6381      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6383 /* Unordered tests if either argument is a NaN.  */
6384 (simplify
6385  (bit_ior (unordered @0 @0) (unordered @1 @1))
6386  (if (types_match (@0, @1))
6387   (unordered @0 @1)))
6388 (simplify
6389  (bit_and (ordered @0 @0) (ordered @1 @1))
6390  (if (types_match (@0, @1))
6391   (ordered @0 @1)))
6392 (simplify
6393  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6394  @2)
6395 (simplify
6396  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6397  @2)
6399 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6400    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6402    Note that comparisons
6403      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6404      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6405    will be canonicalized to above so there's no need to
6406    consider them here.
6407  */
6409 (for cmp (le gt)
6410      eqcmp (eq ne)
6411  (simplify
6412   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6413   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6414    (with
6415     {
6416      tree ty = TREE_TYPE (@0);
6417      unsigned prec = TYPE_PRECISION (ty);
6418      wide_int mask = wi::to_wide (@2, prec);
6419      wide_int rhs = wi::to_wide (@3, prec);
6420      signop sgn = TYPE_SIGN (ty);
6421     }
6422     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6423          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6424       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6425              { build_zero_cst (ty); }))))))
6427 /* -A CMP -B -> B CMP A.  */
6428 (for cmp (tcc_comparison)
6429      scmp (swapped_tcc_comparison)
6430  (simplify
6431   (cmp (negate @0) (negate @1))
6432   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6433        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6434            && (cmp == EQ_EXPR
6435                || cmp == NE_EXPR
6436                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6437    (scmp @0 @1)))
6438  (simplify
6439   (cmp (negate @0) CONSTANT_CLASS_P@1)
6440   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6441        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6442            && (cmp == EQ_EXPR
6443                || cmp == NE_EXPR
6444                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6445    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6446     (if (tem && !TREE_OVERFLOW (tem))
6447      (scmp @0 { tem; }))))))
6449 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6450 (for op (abs absu)
6451  (for eqne (eq ne)
6452   (simplify
6453    (eqne (op @0) zerop@1)
6454    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6456 /* From fold_sign_changed_comparison and fold_widened_comparison.
6457    FIXME: the lack of symmetry is disturbing.  */
6458 (for cmp (simple_comparison)
6459  (simplify
6460   (cmp (convert@0 @00) (convert?@1 @10))
6461   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6462        /* Disable this optimization if we're casting a function pointer
6463           type on targets that require function pointer canonicalization.  */
6464        && !(targetm.have_canonicalize_funcptr_for_compare ()
6465             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6466                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6467                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6468                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6469        && single_use (@0))
6470    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6471         && (TREE_CODE (@10) == INTEGER_CST
6472             || @1 != @10)
6473         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6474             || cmp == NE_EXPR
6475             || cmp == EQ_EXPR)
6476         && !POINTER_TYPE_P (TREE_TYPE (@00))
6477         /* (int)bool:32 != (int)uint is not the same as
6478            bool:32 != (bool:32)uint since boolean types only have two valid
6479            values independent of their precision.  */
6480         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6481             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6482     /* ???  The special-casing of INTEGER_CST conversion was in the original
6483        code and here to avoid a spurious overflow flag on the resulting
6484        constant which fold_convert produces.  */
6485     (if (TREE_CODE (@1) == INTEGER_CST)
6486      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6487                                 wide_int::from (wi::to_wide (@1),
6488                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6489                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6490                                                 TYPE_SIGN (TREE_TYPE (@1))),
6491                                 0, TREE_OVERFLOW (@1)); })
6492      (cmp @00 (convert @1)))
6494     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6495      /* If possible, express the comparison in the shorter mode.  */
6496      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6497            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6498            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6499                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6500           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6501               || ((TYPE_PRECISION (TREE_TYPE (@00))
6502                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6503                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6504                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6505               || (TREE_CODE (@10) == INTEGER_CST
6506                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6507                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6508       (cmp @00 (convert @10))
6509       (if (TREE_CODE (@10) == INTEGER_CST
6510            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6511            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6512        (with
6513         {
6514           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6515           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6516           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6517           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6518         }
6519         (if (above || below)
6520          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6521           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6522           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6523            { constant_boolean_node (above ? true : false, type); }
6524            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6525             { constant_boolean_node (above ? false : true, type); })))))))))
6526    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6527    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6528         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6529             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6530         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6531             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6532     (with
6533      {
6534        tree type1 = TREE_TYPE (@10);
6535        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6536          {
6537            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6538            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6539                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6540              type1 = float_type_node;
6541            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6542                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6543              type1 = double_type_node;
6544          }
6545       tree newtype
6546         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6547            ? TREE_TYPE (@00) : type1);
6548      }
6549      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6550       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6553 (for cmp (eq ne)
6554  (simplify
6555   /* SSA names are canonicalized to 2nd place.  */
6556   (cmp addr@0 SSA_NAME@1)
6557   (with
6558    {
6559      poly_int64 off; tree base;
6560      tree addr = (TREE_CODE (@0) == SSA_NAME
6561                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6562    }
6563    /* A local variable can never be pointed to by
6564       the default SSA name of an incoming parameter.  */
6565    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6566         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6567         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6568         && TREE_CODE (base) == VAR_DECL
6569         && auto_var_in_fn_p (base, current_function_decl))
6570     (if (cmp == NE_EXPR)
6571      { constant_boolean_node (true, type); }
6572      { constant_boolean_node (false, type); })
6573     /* If the address is based on @1 decide using the offset.  */
6574     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6575          && TREE_CODE (base) == MEM_REF
6576          && TREE_OPERAND (base, 0) == @1)
6577      (with { off += mem_ref_offset (base).force_shwi (); }
6578       (if (known_ne (off, 0))
6579        { constant_boolean_node (cmp == NE_EXPR, type); }
6580        (if (known_eq (off, 0))
6581         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6583 /* Equality compare simplifications from fold_binary  */
6584 (for cmp (eq ne)
6586  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6587     Similarly for NE_EXPR.  */
6588  (simplify
6589   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6590   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6591        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6592    { constant_boolean_node (cmp == NE_EXPR, type); }))
6594  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6595  (simplify
6596   (cmp (bit_xor @0 @1) integer_zerop)
6597   (cmp @0 @1))
6599  /* (X ^ Y) == Y becomes X == 0.
6600     Likewise (X ^ Y) == X becomes Y == 0.  */
6601  (simplify
6602   (cmp:c (bit_xor:c @0 @1) @0)
6603   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6605  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6606  (simplify
6607   (cmp:c (bit_and:c @0 @1) @0)
6608   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6609  (simplify
6610   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6611   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6612        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6613        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6614        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6615        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6616        && !wi::neg_p (wi::to_wide (@1)))
6617    (cmp (bit_and @0 (convert (bit_not @1)))
6618         { build_zero_cst (TREE_TYPE (@0)); })))
6620  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6621  (simplify
6622   (cmp:c (bit_ior:c @0 @1) @1)
6623   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6625  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6626  (simplify
6627   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6628   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6629    (cmp @0 (bit_xor @1 (convert @2)))))
6631  (simplify
6632   (cmp (nop_convert? @0) integer_zerop)
6633   (if (tree_expr_nonzero_p (@0))
6634    { constant_boolean_node (cmp == NE_EXPR, type); }))
6636  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6637  (simplify
6638   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6639   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6641 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6642    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6643    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6644    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6645 (for cmp (eq ne)
6646      ncmp (ge lt)
6647  (for sgncmp (ge lt)
6648   (simplify
6649    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6650    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6651         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6652         && types_match (@0, @1))
6653     (ncmp (bit_xor @0 @1) @2)))))
6654 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6655    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6656 (for cmp (eq ne)
6657      ncmp (lt ge)
6658  (simplify
6659   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6660    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6661         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6662         && types_match (@0, @1))
6663     (ncmp (bit_xor @0 @1) @2))))
6665 /* If we have (A & C) == C where C is a power of 2, convert this into
6666    (A & C) != 0.  Similarly for NE_EXPR.  */
6667 (for cmp (eq ne)
6668      icmp (ne eq)
6669  (simplify
6670   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6671   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6673 #if GIMPLE
6674 /* From fold_binary_op_with_conditional_arg handle the case of
6675    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6676    compares simplify.  */
6677 (for cmp (simple_comparison)
6678  (simplify
6679   (cmp:c (cond @0 @1 @2) @3)
6680   /* Do not move possibly trapping operations into the conditional as this
6681      pessimizes code and causes gimplification issues when applied late.  */
6682   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6683        || !operation_could_trap_p (cmp, true, false, @3))
6684    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6685 #endif
6687 (for cmp (ge lt)
6688 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6689 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6690  (simplify
6691   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6692    (if (INTEGRAL_TYPE_P (type)
6693         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6694         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6695         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6696     (with
6697      {
6698        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6699      }
6700     (if (cmp == LT_EXPR)
6701      (bit_xor (convert (rshift @0 {shifter;})) @1)
6702      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6703 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6704 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6705  (simplify
6706   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6707    (if (INTEGRAL_TYPE_P (type)
6708         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6709         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6710         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6711     (with
6712      {
6713        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6714      }
6715     (if (cmp == GE_EXPR)
6716      (bit_xor (convert (rshift @0 {shifter;})) @1)
6717      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6719 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6720    convert this into a shift followed by ANDing with D.  */
6721 (simplify
6722  (cond
6723   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6724   INTEGER_CST@2 integer_zerop)
6725  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6726   (with {
6727      int shift = (wi::exact_log2 (wi::to_wide (@2))
6728                   - wi::exact_log2 (wi::to_wide (@1)));
6729    }
6730    (if (shift > 0)
6731     (bit_and
6732      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6733     (bit_and
6734      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6735      @2)))))
6737 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6738    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6739 (for cmp (eq ne)
6740      ncmp (ge lt)
6741  (simplify
6742   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6743   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6744        && type_has_mode_precision_p (TREE_TYPE (@0))
6745        && element_precision (@2) >= element_precision (@0)
6746        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6747    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6748     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6750 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6751    this into a right shift or sign extension followed by ANDing with C.  */
6752 (simplify
6753  (cond
6754   (lt @0 integer_zerop)
6755   INTEGER_CST@1 integer_zerop)
6756  (if (integer_pow2p (@1)
6757       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6758   (with {
6759     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6760    }
6761    (if (shift >= 0)
6762     (bit_and
6763      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6764      @1)
6765     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6766        sign extension followed by AND with C will achieve the effect.  */
6767     (bit_and (convert @0) @1)))))
6769 /* When the addresses are not directly of decls compare base and offset.
6770    This implements some remaining parts of fold_comparison address
6771    comparisons but still no complete part of it.  Still it is good
6772    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6773 (for cmp (simple_comparison)
6774  (simplify
6775   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6776   (with
6777    {
6778      poly_int64 off0, off1;
6779      tree base0, base1;
6780      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6781                                   off0, off1, GENERIC);
6782    }
6783    (if (equal == 1)
6784     (switch
6785      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6786       { constant_boolean_node (known_eq (off0, off1), type); })
6787      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6788       { constant_boolean_node (known_ne (off0, off1), type); })
6789      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6790       { constant_boolean_node (known_lt (off0, off1), type); })
6791      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6792       { constant_boolean_node (known_le (off0, off1), type); })
6793      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6794       { constant_boolean_node (known_ge (off0, off1), type); })
6795      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6796       { constant_boolean_node (known_gt (off0, off1), type); }))
6797     (if (equal == 0)
6798      (switch
6799       (if (cmp == EQ_EXPR)
6800        { constant_boolean_node (false, type); })
6801       (if (cmp == NE_EXPR)
6802        { constant_boolean_node (true, type); })))))))
6804 #if GIMPLE
6805 /* a?~t:t -> (-(a))^t */
6806 (simplify
6807  (cond @0 @1 @2)
6808  (with { bool wascmp; }
6809   (if (INTEGRAL_TYPE_P (type)
6810        && bitwise_inverted_equal_p (@1, @2, wascmp)
6811        && (!wascmp || TYPE_PRECISION (type) == 1))
6812    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6813         || TYPE_PRECISION (type) == 1)
6814     (bit_xor (convert:type @0) @2)
6815     (bit_xor (negate (convert:type @0)) @2)))))
6816 #endif
6818 /* Simplify pointer equality compares using PTA.  */
6819 (for neeq (ne eq)
6820  (simplify
6821   (neeq @0 @1)
6822   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6823        && ptrs_compare_unequal (@0, @1))
6824    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6826 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6827    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6828    Disable the transform if either operand is pointer to function.
6829    This broke pr22051-2.c for arm where function pointer
6830    canonicalizaion is not wanted.  */
6832 (for cmp (ne eq)
6833  (simplify
6834   (cmp (convert @0) INTEGER_CST@1)
6835   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6836          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6837          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6838          /* Don't perform this optimization in GENERIC if @0 has reference
6839             type when sanitizing.  See PR101210.  */
6840          && !(GENERIC
6841               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6842               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6843         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6844             && POINTER_TYPE_P (TREE_TYPE (@1))
6845             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6846        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6847    (cmp @0 (convert @1)))))
6849 /* Non-equality compare simplifications from fold_binary  */
6850 (for cmp (lt gt le ge)
6851  /* Comparisons with the highest or lowest possible integer of
6852     the specified precision will have known values.  */
6853  (simplify
6854   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6855   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6856         || POINTER_TYPE_P (TREE_TYPE (@1))
6857         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6858        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6859    (with
6860     {
6861       tree cst = uniform_integer_cst_p (@1);
6862       tree arg1_type = TREE_TYPE (cst);
6863       unsigned int prec = TYPE_PRECISION (arg1_type);
6864       wide_int max = wi::max_value (arg1_type);
6865       wide_int signed_max = wi::max_value (prec, SIGNED);
6866       wide_int min = wi::min_value (arg1_type);
6867     }
6868     (switch
6869      (if (wi::to_wide (cst) == max)
6870       (switch
6871        (if (cmp == GT_EXPR)
6872         { constant_boolean_node (false, type); })
6873        (if (cmp == GE_EXPR)
6874         (eq @2 @1))
6875        (if (cmp == LE_EXPR)
6876         { constant_boolean_node (true, type); })
6877        (if (cmp == LT_EXPR)
6878         (ne @2 @1))))
6879      (if (wi::to_wide (cst) == min)
6880       (switch
6881        (if (cmp == LT_EXPR)
6882         { constant_boolean_node (false, type); })
6883        (if (cmp == LE_EXPR)
6884         (eq @2 @1))
6885        (if (cmp == GE_EXPR)
6886         { constant_boolean_node (true, type); })
6887        (if (cmp == GT_EXPR)
6888         (ne @2 @1))))
6889      (if (wi::to_wide (cst) == max - 1)
6890       (switch
6891        (if (cmp == GT_EXPR)
6892         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6893                                     wide_int_to_tree (TREE_TYPE (cst),
6894                                                       wi::to_wide (cst)
6895                                                       + 1)); }))
6896        (if (cmp == LE_EXPR)
6897         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6898                                     wide_int_to_tree (TREE_TYPE (cst),
6899                                                       wi::to_wide (cst)
6900                                                       + 1)); }))))
6901      (if (wi::to_wide (cst) == min + 1)
6902       (switch
6903        (if (cmp == GE_EXPR)
6904         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6905                                     wide_int_to_tree (TREE_TYPE (cst),
6906                                                       wi::to_wide (cst)
6907                                                       - 1)); }))
6908        (if (cmp == LT_EXPR)
6909         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6910                                     wide_int_to_tree (TREE_TYPE (cst),
6911                                                       wi::to_wide (cst)
6912                                                       - 1)); }))))
6913      (if (wi::to_wide (cst) == signed_max
6914           && TYPE_UNSIGNED (arg1_type)
6915           && TYPE_MODE (arg1_type) != BLKmode
6916           /* We will flip the signedness of the comparison operator
6917              associated with the mode of @1, so the sign bit is
6918              specified by this mode.  Check that @1 is the signed
6919              max associated with this sign bit.  */
6920           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6921           /* signed_type does not work on pointer types.  */
6922           && INTEGRAL_TYPE_P (arg1_type))
6923       /* The following case also applies to X < signed_max+1
6924          and X >= signed_max+1 because previous transformations.  */
6925       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6926        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6927         (switch
6928          (if (cst == @1 && cmp == LE_EXPR)
6929           (ge (convert:st @0) { build_zero_cst (st); }))
6930          (if (cst == @1 && cmp == GT_EXPR)
6931           (lt (convert:st @0) { build_zero_cst (st); }))
6932          (if (cmp == LE_EXPR)
6933           (ge (view_convert:st @0) { build_zero_cst (st); }))
6934          (if (cmp == GT_EXPR)
6935           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6937 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
6938 (simplify
6939  (lt:c @0 (convert (ne @0 integer_zerop)))
6940  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6941   { constant_boolean_node (false, type); }))
6943 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6944 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6945 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6946 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6947 (for outer (ne eq)
6948  (for inner (ne eq)
6949   (simplify
6950    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6951    (with {
6952      bool cst1 = integer_onep (@1);
6953      bool cst0 = integer_zerop (@1);
6954      bool innereq = inner == EQ_EXPR;
6955      bool outereq = outer == EQ_EXPR;
6956     }
6957     (switch
6958      (if (innereq ? cst0 : cst1)
6959       { constant_boolean_node (!outereq, type); })
6960      (if (innereq ? cst1 : cst0)
6961       (with {
6962         tree utype = unsigned_type_for (TREE_TYPE (@0));
6963         tree ucst1 = build_one_cst (utype);
6964        }
6965        (if (!outereq)
6966         (gt (convert:utype @0) { ucst1; })
6967         (le (convert:utype @0) { ucst1; })
6968        )
6969       )
6970      )
6971      (with {
6972        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6973       }
6974       (if (outereq)
6975        (eq @0 { value; })
6976        (ne @0 { value; })
6977       )
6978      )
6979     )
6980    )
6981   )
6985 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6986  /* If the second operand is NaN, the result is constant.  */
6987  (simplify
6988   (cmp @0 REAL_CST@1)
6989   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6990        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6991    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6992                             ? false : true, type); })))
6994 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6995 (simplify
6996   (unordered @0 @1)
6997   (switch
6998     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6999         { constant_boolean_node (true, type); })
7000     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7001         { constant_boolean_node (false, type); })))
7003 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7004 (simplify
7005   (ordered @0 @1)
7006   (switch
7007     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7008         { constant_boolean_node (false, type); })
7009     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7010         { constant_boolean_node (true, type); })))
7012 /* bool_var != 0 becomes bool_var.  */
7013 (simplify
7014  (ne @0 integer_zerop)
7015  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7016       && types_match (type, TREE_TYPE (@0)))
7017   (non_lvalue @0)))
7018 /* bool_var == 1 becomes bool_var.  */
7019 (simplify
7020  (eq @0 integer_onep)
7021  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7022       && types_match (type, TREE_TYPE (@0)))
7023   (non_lvalue @0)))
7024 /* Do not handle
7025    bool_var == 0 becomes !bool_var or
7026    bool_var != 1 becomes !bool_var
7027    here because that only is good in assignment context as long
7028    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7029    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7030    clearly less optimal and which we'll transform again in forwprop.  */
7032 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7033    where ~Y + 1 == pow2 and Z = ~Y.  */
7034 (for cst (VECTOR_CST INTEGER_CST)
7035  (for cmp (eq ne)
7036       icmp (le gt)
7037   (simplify
7038    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7039     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7040      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7041       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7042                          ? optab_vector : optab_default;
7043               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7044        (if (target_supports_op_p (utype, icmp, optab)
7045             || (optimize_vectors_before_lowering_p ()
7046                 && (!target_supports_op_p (type, cmp, optab)
7047                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7048         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7049          (icmp @0 { csts; })
7050          (icmp (view_convert:utype @0) { csts; })))))))))
7052 /* When one argument is a constant, overflow detection can be simplified.
7053    Currently restricted to single use so as not to interfere too much with
7054    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7055    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7056 (for cmp (lt le ge gt)
7057      out (gt gt le le)
7058  (simplify
7059   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7060   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7061        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7062        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7063        && wi::to_wide (@1) != 0
7064        && single_use (@2))
7065    (with {
7066      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7067      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7068     }
7069     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7070                                 wi::max_value (prec, sign)
7071                                 - wi::to_wide (@1)); })))))
7073 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7074    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7075    expects the long form, so we restrict the transformation for now.  */
7076 (for cmp (gt le)
7077  (simplify
7078   (cmp:c (minus@2 @0 @1) @0)
7079   (if (single_use (@2)
7080        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7081        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7082    (cmp @1 @0))))
7084 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7085 (for cmp (ge lt)
7086  (simplify
7087   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7088    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7089         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7090     (cmp @1 @0))))
7092 /* Testing for overflow is unnecessary if we already know the result.  */
7093 /* A - B > A  */
7094 (for cmp (gt le)
7095      out (ne eq)
7096  (simplify
7097   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7098   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7099        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7100    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7101 /* A + B < A  */
7102 (for cmp (lt ge)
7103      out (ne eq)
7104  (simplify
7105   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7106   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7107        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7108    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7110 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7111    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7112 (for cmp (lt ge)
7113      out (ne eq)
7114  (simplify
7115   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7116   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7117    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7118     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7120 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7121    is at least twice as wide as type of A and B, simplify to
7122    __builtin_mul_overflow (A, B, <unused>).  */
7123 (for cmp (eq ne)
7124  (simplify
7125   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7126        integer_zerop)
7127   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7128        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7129        && TYPE_UNSIGNED (TREE_TYPE (@0))
7130        && (TYPE_PRECISION (TREE_TYPE (@3))
7131            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7132        && tree_fits_uhwi_p (@2)
7133        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7134        && types_match (@0, @1)
7135        && type_has_mode_precision_p (TREE_TYPE (@0))
7136        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7137            != CODE_FOR_nothing))
7138    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7139     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7141 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7142 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7143  (simplify
7144   (ovf (convert@2 @0) @1)
7145   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7146        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7147        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7148        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7149    (ovf @0 @1)))
7150  (simplify
7151   (ovf @1 (convert@2 @0))
7152   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7153        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7154        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7155        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7156    (ovf @1 @0))))
7158 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7159    are unsigned to x > (umax / cst).  Similarly for signed type, but
7160    in that case it needs to be outside of a range.  */
7161 (simplify
7162  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7163   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7164        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7165        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7166        && int_fits_type_p (@1, TREE_TYPE (@0)))
7167    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7168     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7169     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7170      (if (integer_minus_onep (@1))
7171       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7172       (with
7173        {
7174          tree div = fold_convert (TREE_TYPE (@0), @1);
7175          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7176                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7177          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7178                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7179          tree etype = range_check_type (TREE_TYPE (@0));
7180          if (etype)
7181            {
7182              if (wi::neg_p (wi::to_wide (div)))
7183                std::swap (lo, hi);
7184              lo = fold_convert (etype, lo);
7185              hi = fold_convert (etype, hi);
7186              hi = int_const_binop (MINUS_EXPR, hi, lo);
7187            }
7188        }
7189        (if (etype)
7190         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7192 /* Simplification of math builtins.  These rules must all be optimizations
7193    as well as IL simplifications.  If there is a possibility that the new
7194    form could be a pessimization, the rule should go in the canonicalization
7195    section that follows this one.
7197    Rules can generally go in this section if they satisfy one of
7198    the following:
7200    - the rule describes an identity
7202    - the rule replaces calls with something as simple as addition or
7203      multiplication
7205    - the rule contains unary calls only and simplifies the surrounding
7206      arithmetic.  (The idea here is to exclude non-unary calls in which
7207      one operand is constant and in which the call is known to be cheap
7208      when the operand has that value.)  */
7210 (if (flag_unsafe_math_optimizations)
7211  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7212  (simplify
7213   (mult (SQRT_ALL@1 @0) @1)
7214   (if (!tree_expr_maybe_signaling_nan_p (@0))
7215    @0))
7217  (for op (plus minus)
7218   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7219   (simplify
7220    (op (rdiv @0 @1)
7221        (rdiv @2 @1))
7222    (rdiv (op @0 @2) @1)))
7224  (for cmp (lt le gt ge)
7225       neg_cmp (gt ge lt le)
7226   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7227   (simplify
7228    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7229    (with
7230     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7231     (if (tem
7232          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7233               || (real_zerop (tem) && !real_zerop (@1))))
7234      (switch
7235       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7236        (cmp @0 { tem; }))
7237       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7238        (neg_cmp @0 { tem; })))))))
7240  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7241  (for root (SQRT CBRT)
7242   (simplify
7243    (mult (root:s @0) (root:s @1))
7244     (root (mult @0 @1))))
7246  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7247  (for exps (EXP EXP2 EXP10 POW10)
7248   (simplify
7249    (mult (exps:s @0) (exps:s @1))
7250     (exps (plus @0 @1))))
7252  /* Simplify a/root(b/c) into a*root(c/b).  */
7253  (for root (SQRT CBRT)
7254   (simplify
7255    (rdiv @0 (root:s (rdiv:s @1 @2)))
7256     (mult @0 (root (rdiv @2 @1)))))
7258  /* Simplify x/expN(y) into x*expN(-y).  */
7259  (for exps (EXP EXP2 EXP10 POW10)
7260   (simplify
7261    (rdiv @0 (exps:s @1))
7262     (mult @0 (exps (negate @1)))))
7264  (for logs (LOG LOG2 LOG10 LOG10)
7265       exps (EXP EXP2 EXP10 POW10)
7266   /* logN(expN(x)) -> x.  */
7267   (simplify
7268    (logs (exps @0))
7269    @0)
7270   /* expN(logN(x)) -> x.  */
7271   (simplify
7272    (exps (logs @0))
7273    @0))
7275  /* Optimize logN(func()) for various exponential functions.  We
7276     want to determine the value "x" and the power "exponent" in
7277     order to transform logN(x**exponent) into exponent*logN(x).  */
7278  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7279       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7280   (simplify
7281    (logs (exps @0))
7282    (if (SCALAR_FLOAT_TYPE_P (type))
7283     (with {
7284       tree x;
7285       switch (exps)
7286         {
7287         CASE_CFN_EXP:
7288           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7289           x = build_real_truncate (type, dconst_e ());
7290           break;
7291         CASE_CFN_EXP2:
7292           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7293           x = build_real (type, dconst2);
7294           break;
7295         CASE_CFN_EXP10:
7296         CASE_CFN_POW10:
7297           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7298           {
7299             REAL_VALUE_TYPE dconst10;
7300             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7301             x = build_real (type, dconst10);
7302           }
7303           break;
7304         default:
7305           gcc_unreachable ();
7306         }
7307       }
7308      (mult (logs { x; }) @0)))))
7310  (for logs (LOG LOG
7311             LOG2 LOG2
7312             LOG10 LOG10)
7313       exps (SQRT CBRT)
7314   (simplify
7315    (logs (exps @0))
7316    (if (SCALAR_FLOAT_TYPE_P (type))
7317     (with {
7318       tree x;
7319       switch (exps)
7320         {
7321         CASE_CFN_SQRT:
7322           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7323           x = build_real (type, dconsthalf);
7324           break;
7325         CASE_CFN_CBRT:
7326           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7327           x = build_real_truncate (type, dconst_third ());
7328           break;
7329         default:
7330           gcc_unreachable ();
7331         }
7332       }
7333      (mult { x; } (logs @0))))))
7335  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7336  (for logs (LOG LOG2 LOG10)
7337       pows (POW)
7338   (simplify
7339    (logs (pows @0 @1))
7340    (mult @1 (logs @0))))
7342  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7343     or if C is a positive power of 2,
7344     pow(C,x) -> exp2(log2(C)*x).  */
7345 #if GIMPLE
7346  (for pows (POW)
7347       exps (EXP)
7348       logs (LOG)
7349       exp2s (EXP2)
7350       log2s (LOG2)
7351   (simplify
7352    (pows REAL_CST@0 @1)
7353    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7354         && real_isfinite (TREE_REAL_CST_PTR (@0))
7355         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7356            the use_exp2 case until after vectorization.  It seems actually
7357            beneficial for all constants to postpone this until later,
7358            because exp(log(C)*x), while faster, will have worse precision
7359            and if x folds into a constant too, that is unnecessary
7360            pessimization.  */
7361         && canonicalize_math_after_vectorization_p ())
7362     (with {
7363        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7364        bool use_exp2 = false;
7365        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7366            && value->cl == rvc_normal)
7367          {
7368            REAL_VALUE_TYPE frac_rvt = *value;
7369            SET_REAL_EXP (&frac_rvt, 1);
7370            if (real_equal (&frac_rvt, &dconst1))
7371              use_exp2 = true;
7372          }
7373      }
7374      (if (!use_exp2)
7375       (if (optimize_pow_to_exp (@0, @1))
7376        (exps (mult (logs @0) @1)))
7377       (exp2s (mult (log2s @0) @1)))))))
7378 #endif
7380  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7381  (for pows (POW)
7382       exps (EXP EXP2 EXP10 POW10)
7383       logs (LOG LOG2 LOG10 LOG10)
7384   (simplify
7385    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7386    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7387         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7388     (exps (plus (mult (logs @0) @1) @2)))))
7390  (for sqrts (SQRT)
7391       cbrts (CBRT)
7392       pows (POW)
7393       exps (EXP EXP2 EXP10 POW10)
7394   /* sqrt(expN(x)) -> expN(x*0.5).  */
7395   (simplify
7396    (sqrts (exps @0))
7397    (exps (mult @0 { build_real (type, dconsthalf); })))
7398   /* cbrt(expN(x)) -> expN(x/3).  */
7399   (simplify
7400    (cbrts (exps @0))
7401    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7402   /* pow(expN(x), y) -> expN(x*y).  */
7403   (simplify
7404    (pows (exps @0) @1)
7405    (exps (mult @0 @1))))
7407  /* tan(atan(x)) -> x.  */
7408  (for tans (TAN)
7409       atans (ATAN)
7410   (simplify
7411    (tans (atans @0))
7412    @0)))
7414  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7415  (for sins (SIN)
7416       atans (ATAN)
7417       sqrts (SQRT)
7418       copysigns (COPYSIGN)
7419   (simplify
7420    (sins (atans:s @0))
7421    (with
7422      {
7423       REAL_VALUE_TYPE r_cst;
7424       build_sinatan_real (&r_cst, type);
7425       tree t_cst = build_real (type, r_cst);
7426       tree t_one = build_one_cst (type);
7427      }
7428     (if (SCALAR_FLOAT_TYPE_P (type))
7429      (cond (lt (abs @0) { t_cst; })
7430       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7431       (copysigns { t_one; } @0))))))
7433 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7434  (for coss (COS)
7435       atans (ATAN)
7436       sqrts (SQRT)
7437       copysigns (COPYSIGN)
7438   (simplify
7439    (coss (atans:s @0))
7440    (with
7441      {
7442       REAL_VALUE_TYPE r_cst;
7443       build_sinatan_real (&r_cst, type);
7444       tree t_cst = build_real (type, r_cst);
7445       tree t_one = build_one_cst (type);
7446       tree t_zero = build_zero_cst (type);
7447      }
7448     (if (SCALAR_FLOAT_TYPE_P (type))
7449      (cond (lt (abs @0) { t_cst; })
7450       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7451       (copysigns { t_zero; } @0))))))
7453  (if (!flag_errno_math)
7454   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7455   (for sinhs (SINH)
7456        atanhs (ATANH)
7457        sqrts (SQRT)
7458    (simplify
7459     (sinhs (atanhs:s @0))
7460     (with { tree t_one = build_one_cst (type); }
7461     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7463   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7464   (for coshs (COSH)
7465        atanhs (ATANH)
7466        sqrts (SQRT)
7467    (simplify
7468     (coshs (atanhs:s @0))
7469     (with { tree t_one = build_one_cst (type); }
7470     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7472 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7473 (simplify
7474  (CABS (complex:C @0 real_zerop@1))
7475  (abs @0))
7477 /* trunc(trunc(x)) -> trunc(x), etc.  */
7478 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7479  (simplify
7480   (fns (fns @0))
7481   (fns @0)))
7482 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7483 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7484  (simplify
7485   (fns integer_valued_real_p@0)
7486   @0))
7488 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7489 (simplify
7490  (HYPOT:c @0 real_zerop@1)
7491  (abs @0))
7493 /* pow(1,x) -> 1.  */
7494 (simplify
7495  (POW real_onep@0 @1)
7496  @0)
7498 (simplify
7499  /* copysign(x,x) -> x.  */
7500  (COPYSIGN_ALL @0 @0)
7501  @0)
7503 (simplify
7504  /* copysign(x,-x) -> -x.  */
7505  (COPYSIGN_ALL @0 (negate@1 @0))
7506  @1)
7508 (simplify
7509  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7510  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7511  (abs @0))
7513 (simplify
7514  /* fabs (copysign(x, y)) -> fabs (x).  */
7515  (abs (COPYSIGN_ALL @0 @1))
7516  (abs @0))
7518 (for scale (LDEXP SCALBN SCALBLN)
7519  /* ldexp(0, x) -> 0.  */
7520  (simplify
7521   (scale real_zerop@0 @1)
7522   @0)
7523  /* ldexp(x, 0) -> x.  */
7524  (simplify
7525   (scale @0 integer_zerop@1)
7526   @0)
7527  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7528  (simplify
7529   (scale REAL_CST@0 @1)
7530   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7531    @0)))
7533 /* Canonicalization of sequences of math builtins.  These rules represent
7534    IL simplifications but are not necessarily optimizations.
7536    The sincos pass is responsible for picking "optimal" implementations
7537    of math builtins, which may be more complicated and can sometimes go
7538    the other way, e.g. converting pow into a sequence of sqrts.
7539    We only want to do these canonicalizations before the pass has run.  */
7541 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7542  /* Simplify tan(x) * cos(x) -> sin(x). */
7543  (simplify
7544   (mult:c (TAN:s @0) (COS:s @0))
7545    (SIN @0))
7547  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7548  (simplify
7549   (mult:c @0 (POW:s @0 REAL_CST@1))
7550   (if (!TREE_OVERFLOW (@1))
7551    (POW @0 (plus @1 { build_one_cst (type); }))))
7553  /* Simplify sin(x) / cos(x) -> tan(x). */
7554  (simplify
7555   (rdiv (SIN:s @0) (COS:s @0))
7556    (TAN @0))
7558  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7559  (simplify
7560   (rdiv (SINH:s @0) (COSH:s @0))
7561    (TANH @0))
7563  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7564  (simplify
7565    (rdiv (TANH:s @0) (SINH:s @0))
7566    (rdiv {build_one_cst (type);} (COSH @0)))
7568  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7569  (simplify
7570   (rdiv (COS:s @0) (SIN:s @0))
7571    (rdiv { build_one_cst (type); } (TAN @0)))
7573  /* Simplify sin(x) / tan(x) -> cos(x). */
7574  (simplify
7575   (rdiv (SIN:s @0) (TAN:s @0))
7576   (if (! HONOR_NANS (@0)
7577        && ! HONOR_INFINITIES (@0))
7578    (COS @0)))
7580  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7581  (simplify
7582   (rdiv (TAN:s @0) (SIN:s @0))
7583   (if (! HONOR_NANS (@0)
7584        && ! HONOR_INFINITIES (@0))
7585    (rdiv { build_one_cst (type); } (COS @0))))
7587  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7588  (simplify
7589   (mult (POW:s @0 @1) (POW:s @0 @2))
7590    (POW @0 (plus @1 @2)))
7592  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7593  (simplify
7594   (mult (POW:s @0 @1) (POW:s @2 @1))
7595    (POW (mult @0 @2) @1))
7597  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7598  (simplify
7599   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7600    (POWI (mult @0 @2) @1))
7602  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7603  (simplify
7604   (rdiv (POW:s @0 REAL_CST@1) @0)
7605   (if (!TREE_OVERFLOW (@1))
7606    (POW @0 (minus @1 { build_one_cst (type); }))))
7608  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7609  (simplify
7610   (rdiv @0 (POW:s @1 @2))
7611    (mult @0 (POW @1 (negate @2))))
7613  (for sqrts (SQRT)
7614       cbrts (CBRT)
7615       pows (POW)
7616   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7617   (simplify
7618    (sqrts (sqrts @0))
7619    (pows @0 { build_real (type, dconst_quarter ()); }))
7620   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7621   (simplify
7622    (sqrts (cbrts @0))
7623    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7624   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7625   (simplify
7626    (cbrts (sqrts @0))
7627    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7628   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7629   (simplify
7630    (cbrts (cbrts tree_expr_nonnegative_p@0))
7631    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7632   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7633   (simplify
7634    (sqrts (pows @0 @1))
7635    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7636   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7637   (simplify
7638    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7639    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7640   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7641   (simplify
7642    (pows (sqrts @0) @1)
7643    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7644   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7645   (simplify
7646    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7647    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7648   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7649   (simplify
7650    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7651    (pows @0 (mult @1 @2))))
7653  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7654  (simplify
7655   (CABS (complex @0 @0))
7656   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7658  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7659  (simplify
7660   (HYPOT @0 @0)
7661   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7663  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7664  (for cexps (CEXP)
7665       exps (EXP)
7666       cexpis (CEXPI)
7667   (simplify
7668    (cexps compositional_complex@0)
7669    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7670     (complex
7671      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7672      (mult @1 (imagpart @2)))))))
7674 (if (canonicalize_math_p ())
7675  /* floor(x) -> trunc(x) if x is nonnegative.  */
7676  (for floors (FLOOR_ALL)
7677       truncs (TRUNC_ALL)
7678   (simplify
7679    (floors tree_expr_nonnegative_p@0)
7680    (truncs @0))))
7682 (match double_value_p
7683  @0
7684  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7685 (for froms (BUILT_IN_TRUNCL
7686             BUILT_IN_FLOORL
7687             BUILT_IN_CEILL
7688             BUILT_IN_ROUNDL
7689             BUILT_IN_NEARBYINTL
7690             BUILT_IN_RINTL)
7691      tos (BUILT_IN_TRUNC
7692           BUILT_IN_FLOOR
7693           BUILT_IN_CEIL
7694           BUILT_IN_ROUND
7695           BUILT_IN_NEARBYINT
7696           BUILT_IN_RINT)
7697  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7698  (if (optimize && canonicalize_math_p ())
7699   (simplify
7700    (froms (convert double_value_p@0))
7701    (convert (tos @0)))))
7703 (match float_value_p
7704  @0
7705  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7706 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7707             BUILT_IN_FLOORL BUILT_IN_FLOOR
7708             BUILT_IN_CEILL BUILT_IN_CEIL
7709             BUILT_IN_ROUNDL BUILT_IN_ROUND
7710             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7711             BUILT_IN_RINTL BUILT_IN_RINT)
7712      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7713           BUILT_IN_FLOORF BUILT_IN_FLOORF
7714           BUILT_IN_CEILF BUILT_IN_CEILF
7715           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7716           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7717           BUILT_IN_RINTF BUILT_IN_RINTF)
7718  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7719     if x is a float.  */
7720  (if (optimize && canonicalize_math_p ()
7721       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7722   (simplify
7723    (froms (convert float_value_p@0))
7724    (convert (tos @0)))))
7726 #if GIMPLE
7727 (match float16_value_p
7728  @0
7729  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7730 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7731             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7732             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7733             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7734             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7735             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7736             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7737             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7738      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7739           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7740           IFN_CEIL IFN_CEIL IFN_CEIL
7741           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7742           IFN_ROUND IFN_ROUND IFN_ROUND
7743           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7744           IFN_RINT IFN_RINT IFN_RINT
7745           IFN_SQRT IFN_SQRT IFN_SQRT)
7746  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7747     if x is a _Float16.  */
7748  (simplify
7749    (convert (froms (convert float16_value_p@0)))
7750      (if (optimize
7751           && types_match (type, TREE_TYPE (@0))
7752           && direct_internal_fn_supported_p (as_internal_fn (tos),
7753                                              type, OPTIMIZE_FOR_BOTH))
7754        (tos @0))))
7756 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7757    x,y is float value, similar for _Float16/double.  */
7758 (for copysigns (COPYSIGN_ALL)
7759  (simplify
7760   (convert (copysigns (convert@2 @0) (convert @1)))
7761    (if (optimize
7762        && !HONOR_SNANS (@2)
7763        && types_match (type, TREE_TYPE (@0))
7764        && types_match (type, TREE_TYPE (@1))
7765        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7766        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7767                                           type, OPTIMIZE_FOR_BOTH))
7768     (IFN_COPYSIGN @0 @1))))
7770 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7771      tos (IFN_FMA IFN_FMA IFN_FMA)
7772  (simplify
7773   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7774    (if (flag_unsafe_math_optimizations
7775        && optimize
7776        && FLOAT_TYPE_P (type)
7777        && FLOAT_TYPE_P (TREE_TYPE (@3))
7778        && types_match (type, TREE_TYPE (@0))
7779        && types_match (type, TREE_TYPE (@1))
7780        && types_match (type, TREE_TYPE (@2))
7781        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7782        && direct_internal_fn_supported_p (as_internal_fn (tos),
7783                                           type, OPTIMIZE_FOR_BOTH))
7784     (tos @0 @1 @2))))
7786 (for maxmin (max min)
7787  (simplify
7788   (convert (maxmin (convert@2 @0) (convert @1)))
7789    (if (optimize
7790        && FLOAT_TYPE_P (type)
7791        && FLOAT_TYPE_P (TREE_TYPE (@2))
7792        && types_match (type, TREE_TYPE (@0))
7793        && types_match (type, TREE_TYPE (@1))
7794        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7795     (maxmin @0 @1))))
7796 #endif
7798 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7799      tos (XFLOOR XCEIL XROUND XRINT)
7800  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7801  (if (optimize && canonicalize_math_p ())
7802   (simplify
7803    (froms (convert double_value_p@0))
7804    (tos @0))))
7806 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7807             XFLOOR XCEIL XROUND XRINT)
7808      tos (XFLOORF XCEILF XROUNDF XRINTF)
7809  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7810     if x is a float.  */
7811  (if (optimize && canonicalize_math_p ())
7812   (simplify
7813    (froms (convert float_value_p@0))
7814    (tos @0))))
7816 (if (canonicalize_math_p ())
7817  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7818  (for floors (IFLOOR LFLOOR LLFLOOR)
7819   (simplify
7820    (floors tree_expr_nonnegative_p@0)
7821    (fix_trunc @0))))
7823 (if (canonicalize_math_p ())
7824  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7825  (for fns (IFLOOR LFLOOR LLFLOOR
7826            ICEIL LCEIL LLCEIL
7827            IROUND LROUND LLROUND)
7828   (simplify
7829    (fns integer_valued_real_p@0)
7830    (fix_trunc @0)))
7831  (if (!flag_errno_math)
7832   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7833   (for rints (IRINT LRINT LLRINT)
7834    (simplify
7835     (rints integer_valued_real_p@0)
7836     (fix_trunc @0)))))
7838 (if (canonicalize_math_p ())
7839  (for ifn (IFLOOR ICEIL IROUND IRINT)
7840       lfn (LFLOOR LCEIL LROUND LRINT)
7841       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7842   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7843      sizeof (int) == sizeof (long).  */
7844   (if (TYPE_PRECISION (integer_type_node)
7845        == TYPE_PRECISION (long_integer_type_node))
7846    (simplify
7847     (ifn @0)
7848     (lfn:long_integer_type_node @0)))
7849   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7850      sizeof (long long) == sizeof (long).  */
7851   (if (TYPE_PRECISION (long_long_integer_type_node)
7852        == TYPE_PRECISION (long_integer_type_node))
7853    (simplify
7854     (llfn @0)
7855     (lfn:long_integer_type_node @0)))))
7857 /* cproj(x) -> x if we're ignoring infinities.  */
7858 (simplify
7859  (CPROJ @0)
7860  (if (!HONOR_INFINITIES (type))
7861    @0))
7863 /* If the real part is inf and the imag part is known to be
7864    nonnegative, return (inf + 0i).  */
7865 (simplify
7866  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7867  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7868   { build_complex_inf (type, false); }))
7870 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7871 (simplify
7872  (CPROJ (complex @0 REAL_CST@1))
7873  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7874   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7876 (for pows (POW)
7877      sqrts (SQRT)
7878      cbrts (CBRT)
7879  (simplify
7880   (pows @0 REAL_CST@1)
7881   (with {
7882     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7883     REAL_VALUE_TYPE tmp;
7884    }
7885    (switch
7886     /* pow(x,0) -> 1.  */
7887     (if (real_equal (value, &dconst0))
7888      { build_real (type, dconst1); })
7889     /* pow(x,1) -> x.  */
7890     (if (real_equal (value, &dconst1))
7891      @0)
7892     /* pow(x,-1) -> 1/x.  */
7893     (if (real_equal (value, &dconstm1))
7894      (rdiv { build_real (type, dconst1); } @0))
7895     /* pow(x,0.5) -> sqrt(x).  */
7896     (if (flag_unsafe_math_optimizations
7897          && canonicalize_math_p ()
7898          && real_equal (value, &dconsthalf))
7899      (sqrts @0))
7900     /* pow(x,1/3) -> cbrt(x).  */
7901     (if (flag_unsafe_math_optimizations
7902          && canonicalize_math_p ()
7903          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7904              real_equal (value, &tmp)))
7905      (cbrts @0))))))
7907 /* powi(1,x) -> 1.  */
7908 (simplify
7909  (POWI real_onep@0 @1)
7910  @0)
7912 (simplify
7913  (POWI @0 INTEGER_CST@1)
7914  (switch
7915   /* powi(x,0) -> 1.  */
7916   (if (wi::to_wide (@1) == 0)
7917    { build_real (type, dconst1); })
7918   /* powi(x,1) -> x.  */
7919   (if (wi::to_wide (@1) == 1)
7920    @0)
7921   /* powi(x,-1) -> 1/x.  */
7922   (if (wi::to_wide (@1) == -1)
7923    (rdiv { build_real (type, dconst1); } @0))))
7925 /* Narrowing of arithmetic and logical operations.
7927    These are conceptually similar to the transformations performed for
7928    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7929    term we want to move all that code out of the front-ends into here.  */
7931 /* Convert (outertype)((innertype0)a+(innertype1)b)
7932    into ((newtype)a+(newtype)b) where newtype
7933    is the widest mode from all of these.  */
7934 (for op (plus minus mult rdiv)
7935  (simplify
7936    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7937    /* If we have a narrowing conversion of an arithmetic operation where
7938       both operands are widening conversions from the same type as the outer
7939       narrowing conversion.  Then convert the innermost operands to a
7940       suitable unsigned type (to avoid introducing undefined behavior),
7941       perform the operation and convert the result to the desired type.  */
7942    (if (INTEGRAL_TYPE_P (type)
7943         && op != MULT_EXPR
7944         && op != RDIV_EXPR
7945         /* We check for type compatibility between @0 and @1 below,
7946            so there's no need to check that @2/@4 are integral types.  */
7947         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7948         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7949         /* The precision of the type of each operand must match the
7950            precision of the mode of each operand, similarly for the
7951            result.  */
7952         && type_has_mode_precision_p (TREE_TYPE (@1))
7953         && type_has_mode_precision_p (TREE_TYPE (@2))
7954         && type_has_mode_precision_p (type)
7955         /* The inner conversion must be a widening conversion.  */
7956         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7957         && types_match (@1, type)
7958         && (types_match (@1, @2)
7959             /* Or the second operand is const integer or converted const
7960                integer from valueize.  */
7961             || poly_int_tree_p (@4)))
7962      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7963        (op @1 (convert @2))
7964        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7965         (convert (op (convert:utype @1)
7966                      (convert:utype @2)))))
7967      (if (FLOAT_TYPE_P (type)
7968           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7969                == DECIMAL_FLOAT_TYPE_P (type))
7970       (with { tree arg0 = strip_float_extensions (@1);
7971               tree arg1 = strip_float_extensions (@2);
7972               tree itype = TREE_TYPE (@0);
7973               tree ty1 = TREE_TYPE (arg0);
7974               tree ty2 = TREE_TYPE (arg1);
7975               enum tree_code code = TREE_CODE (itype); }
7976         (if (FLOAT_TYPE_P (ty1)
7977              && FLOAT_TYPE_P (ty2))
7978          (with { tree newtype = type;
7979                  if (TYPE_MODE (ty1) == SDmode
7980                      || TYPE_MODE (ty2) == SDmode
7981                      || TYPE_MODE (type) == SDmode)
7982                    newtype = dfloat32_type_node;
7983                  if (TYPE_MODE (ty1) == DDmode
7984                      || TYPE_MODE (ty2) == DDmode
7985                      || TYPE_MODE (type) == DDmode)
7986                    newtype = dfloat64_type_node;
7987                  if (TYPE_MODE (ty1) == TDmode
7988                      || TYPE_MODE (ty2) == TDmode
7989                      || TYPE_MODE (type) == TDmode)
7990                    newtype = dfloat128_type_node; }
7991           (if ((newtype == dfloat32_type_node
7992                 || newtype == dfloat64_type_node
7993                 || newtype == dfloat128_type_node)
7994               && newtype == type
7995               && types_match (newtype, type))
7996             (op (convert:newtype @1) (convert:newtype @2))
7997             (with { if (element_precision (ty1) > element_precision (newtype))
7998                       newtype = ty1;
7999                     if (element_precision (ty2) > element_precision (newtype))
8000                       newtype = ty2; }
8001                /* Sometimes this transformation is safe (cannot
8002                   change results through affecting double rounding
8003                   cases) and sometimes it is not.  If NEWTYPE is
8004                   wider than TYPE, e.g. (float)((long double)double
8005                   + (long double)double) converted to
8006                   (float)(double + double), the transformation is
8007                   unsafe regardless of the details of the types
8008                   involved; double rounding can arise if the result
8009                   of NEWTYPE arithmetic is a NEWTYPE value half way
8010                   between two representable TYPE values but the
8011                   exact value is sufficiently different (in the
8012                   right direction) for this difference to be
8013                   visible in ITYPE arithmetic.  If NEWTYPE is the
8014                   same as TYPE, however, the transformation may be
8015                   safe depending on the types involved: it is safe
8016                   if the ITYPE has strictly more than twice as many
8017                   mantissa bits as TYPE, can represent infinities
8018                   and NaNs if the TYPE can, and has sufficient
8019                   exponent range for the product or ratio of two
8020                   values representable in the TYPE to be within the
8021                   range of normal values of ITYPE.  */
8022               (if (element_precision (newtype) < element_precision (itype)
8023                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8024                        || target_supports_op_p (newtype, op, optab_default))
8025                    && (flag_unsafe_math_optimizations
8026                        || (element_precision (newtype) == element_precision (type)
8027                            && real_can_shorten_arithmetic (element_mode (itype),
8028                                                            element_mode (type))
8029                            && !excess_precision_type (newtype)))
8030                    && !types_match (itype, newtype))
8031                  (convert:type (op (convert:newtype @1)
8032                                    (convert:newtype @2)))
8033          )))) )
8034    ))
8037 /* This is another case of narrowing, specifically when there's an outer
8038    BIT_AND_EXPR which masks off bits outside the type of the innermost
8039    operands.   Like the previous case we have to convert the operands
8040    to unsigned types to avoid introducing undefined behavior for the
8041    arithmetic operation.  */
8042 (for op (minus plus)
8043  (simplify
8044   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8045   (if (INTEGRAL_TYPE_P (type)
8046        /* We check for type compatibility between @0 and @1 below,
8047           so there's no need to check that @1/@3 are integral types.  */
8048        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8049        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8050        /* The precision of the type of each operand must match the
8051           precision of the mode of each operand, similarly for the
8052           result.  */
8053        && type_has_mode_precision_p (TREE_TYPE (@0))
8054        && type_has_mode_precision_p (TREE_TYPE (@1))
8055        && type_has_mode_precision_p (type)
8056        /* The inner conversion must be a widening conversion.  */
8057        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8058        && types_match (@0, @1)
8059        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8060            <= TYPE_PRECISION (TREE_TYPE (@0)))
8061        && (wi::to_wide (@4)
8062            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8063                        true, TYPE_PRECISION (type))) == 0)
8064    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8065     (with { tree ntype = TREE_TYPE (@0); }
8066      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8067     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8068      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8069                (convert:utype @4))))))))
8071 /* Transform (@0 < @1 and @0 < @2) to use min,
8072    (@0 > @1 and @0 > @2) to use max */
8073 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8074      op    (lt      le      gt      ge      lt      le      gt      ge     )
8075      ext   (min     min     max     max     max     max     min     min    )
8076  (simplify
8077   (logic (op:cs @0 @1) (op:cs @0 @2))
8078   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8079        && TREE_CODE (@0) != INTEGER_CST)
8080    (op @0 (ext @1 @2)))))
8082 /* Max<bool0, bool1> -> bool0 | bool1
8083    Min<bool0, bool1> -> bool0 & bool1 */
8084 (for op    (max     min)
8085      logic (bit_ior bit_and)
8086  (simplify
8087   (op zero_one_valued_p@0 zero_one_valued_p@1)
8088   (logic @0 @1)))
8090 /* signbit(x) != 0 ? -x : x -> abs(x)
8091    signbit(x) == 0 ? -x : x -> -abs(x) */
8092 (for sign (SIGNBIT)
8093  (for neeq (ne eq)
8094   (simplify
8095    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8096     (if (neeq == NE_EXPR)
8097      (abs @0)
8098      (negate (abs @0))))))
8100 (simplify
8101  /* signbit(x) -> 0 if x is nonnegative.  */
8102  (SIGNBIT tree_expr_nonnegative_p@0)
8103  { integer_zero_node; })
8105 (simplify
8106  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8107  (SIGNBIT @0)
8108  (if (!HONOR_SIGNED_ZEROS (@0))
8109   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8111 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8112 (for cmp (eq ne)
8113  (for op (plus minus)
8114       rop (minus plus)
8115   (simplify
8116    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8117    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8118         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8119         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8120         && !TYPE_SATURATING (TREE_TYPE (@0)))
8121     (with { tree res = int_const_binop (rop, @2, @1); }
8122      (if (TREE_OVERFLOW (res)
8123           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8124       { constant_boolean_node (cmp == NE_EXPR, type); }
8125       (if (single_use (@3))
8126        (cmp @0 { TREE_OVERFLOW (res)
8127                  ? drop_tree_overflow (res) : res; }))))))))
8128 (for cmp (lt le gt ge)
8129  (for op (plus minus)
8130       rop (minus plus)
8131   (simplify
8132    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8133    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8134         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8135     (with { tree res = int_const_binop (rop, @2, @1); }
8136      (if (TREE_OVERFLOW (res))
8137       {
8138         fold_overflow_warning (("assuming signed overflow does not occur "
8139                                 "when simplifying conditional to constant"),
8140                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8141         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8142         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8143         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8144                                   TYPE_SIGN (TREE_TYPE (@1)))
8145                         != (op == MINUS_EXPR);
8146         constant_boolean_node (less == ovf_high, type);
8147       }
8148       (if (single_use (@3))
8149        (with
8150         {
8151           fold_overflow_warning (("assuming signed overflow does not occur "
8152                                   "when changing X +- C1 cmp C2 to "
8153                                   "X cmp C2 -+ C1"),
8154                                  WARN_STRICT_OVERFLOW_COMPARISON);
8155         }
8156         (cmp @0 { res; })))))))))
8158 /* Canonicalizations of BIT_FIELD_REFs.  */
8160 (simplify
8161  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8162  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8164 (simplify
8165  (BIT_FIELD_REF (view_convert @0) @1 @2)
8166  (BIT_FIELD_REF @0 @1 @2))
8168 (simplify
8169  (BIT_FIELD_REF @0 @1 integer_zerop)
8170  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8171   (view_convert @0)))
8173 (simplify
8174  (BIT_FIELD_REF @0 @1 @2)
8175  (switch
8176   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8177        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8178    (switch
8179     (if (integer_zerop (@2))
8180      (view_convert (realpart @0)))
8181     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8182      (view_convert (imagpart @0)))))
8183   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8184        && INTEGRAL_TYPE_P (type)
8185        /* On GIMPLE this should only apply to register arguments.  */
8186        && (! GIMPLE || is_gimple_reg (@0))
8187        /* A bit-field-ref that referenced the full argument can be stripped.  */
8188        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8189             && integer_zerop (@2))
8190            /* Low-parts can be reduced to integral conversions.
8191               ???  The following doesn't work for PDP endian.  */
8192            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8193                /* But only do this after vectorization.  */
8194                && canonicalize_math_after_vectorization_p ()
8195                /* Don't even think about BITS_BIG_ENDIAN.  */
8196                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8197                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8198                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8199                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8200                                             - TYPE_PRECISION (type))
8201                                          : 0)) == 0)))
8202    (convert @0))))
8204 /* Simplify vector extracts.  */
8206 (simplify
8207  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8208  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8209       && tree_fits_uhwi_p (TYPE_SIZE (type))
8210       && ((tree_to_uhwi (TYPE_SIZE (type))
8211            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8212           || (VECTOR_TYPE_P (type)
8213               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8214                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8215   (with
8216    {
8217      tree ctor = (TREE_CODE (@0) == SSA_NAME
8218                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8219      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8220      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8221      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8222      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8223    }
8224    (if (n != 0
8225         && (idx % width) == 0
8226         && (n % width) == 0
8227         && known_le ((idx + n) / width,
8228                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8229     (with
8230      {
8231        idx = idx / width;
8232        n = n / width;
8233        /* Constructor elements can be subvectors.  */
8234        poly_uint64 k = 1;
8235        if (CONSTRUCTOR_NELTS (ctor) != 0)
8236          {
8237            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8238            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8239              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8240          }
8241        unsigned HOST_WIDE_INT elt, count, const_k;
8242      }
8243      (switch
8244       /* We keep an exact subset of the constructor elements.  */
8245       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8246        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8247         { build_zero_cst (type); }
8248         (if (count == 1)
8249          (if (elt < CONSTRUCTOR_NELTS (ctor))
8250           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8251           { build_zero_cst (type); })
8252          /* We don't want to emit new CTORs unless the old one goes away.
8253             ???  Eventually allow this if the CTOR ends up constant or
8254             uniform.  */
8255          (if (single_use (@0))
8256           (with
8257             {
8258               vec<constructor_elt, va_gc> *vals;
8259               vec_alloc (vals, count);
8260               bool constant_p = true;
8261               tree res;
8262               for (unsigned i = 0;
8263                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8264                 {
8265                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8266                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8267                   if (!CONSTANT_CLASS_P (e))
8268                     constant_p = false;
8269                 }
8270               tree evtype = (types_match (TREE_TYPE (type),
8271                                           TREE_TYPE (TREE_TYPE (ctor)))
8272                              ? type
8273                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8274                                                   count * k));
8275               /* We used to build a CTOR in the non-constant case here
8276                  but that's not a GIMPLE value.  We'd have to expose this
8277                  operation somehow so the code generation can properly
8278                  split it out to a separate stmt.  */
8279               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8280                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8281             }
8282             (if (res)
8283              (view_convert { res; })))))))
8284       /* The bitfield references a single constructor element.  */
8285       (if (k.is_constant (&const_k)
8286            && idx + n <= (idx / const_k + 1) * const_k)
8287        (switch
8288         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8289          { build_zero_cst (type); })
8290         (if (n == const_k)
8291          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8292         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8293                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8295 /* Simplify a bit extraction from a bit insertion for the cases with
8296    the inserted element fully covering the extraction or the insertion
8297    not touching the extraction.  */
8298 (simplify
8299  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8300  (with
8301   {
8302     unsigned HOST_WIDE_INT isize;
8303     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8304       isize = TYPE_PRECISION (TREE_TYPE (@1));
8305     else
8306       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8307   }
8308   (switch
8309    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8310          || type_has_mode_precision_p (TREE_TYPE (@1)))
8311         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8312         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8313                       wi::to_wide (@ipos) + isize))
8314     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8315                                                  wi::to_wide (@rpos)
8316                                                  - wi::to_wide (@ipos)); }))
8317    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8318         && compare_tree_int (@rsize, isize) == 0)
8319     (convert @1))
8320    (if (wi::geu_p (wi::to_wide (@ipos),
8321                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8322         || wi::geu_p (wi::to_wide (@rpos),
8323                       wi::to_wide (@ipos) + isize))
8324     (BIT_FIELD_REF @0 @rsize @rpos)))))
8326 /* Simplify vector inserts of other vector extracts to a permute.  */
8327 (simplify
8328  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8329  (if (VECTOR_TYPE_P (type)
8330       && types_match (@0, @1)
8331       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8332       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8333   (with
8334    {
8335      unsigned HOST_WIDE_INT elsz
8336        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8337      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8338      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8339      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8340      vec_perm_builder builder;
8341      builder.new_vector (nunits, nunits, 1);
8342      for (unsigned i = 0; i < nunits; ++i)
8343        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8344      vec_perm_indices sel (builder, 2, nunits);
8345    }
8346    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8347         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8348     (vec_perm @0 @1 { vec_perm_indices_to_tree
8349                         (build_vector_type (ssizetype, nunits), sel); })))))
8351 (if (canonicalize_math_after_vectorization_p ())
8352  (for fmas (FMA)
8353   (simplify
8354    (fmas:c (negate @0) @1 @2)
8355    (IFN_FNMA @0 @1 @2))
8356   (simplify
8357    (fmas @0 @1 (negate @2))
8358    (IFN_FMS @0 @1 @2))
8359   (simplify
8360    (fmas:c (negate @0) @1 (negate @2))
8361    (IFN_FNMS @0 @1 @2))
8362   (simplify
8363    (negate (fmas@3 @0 @1 @2))
8364    (if (single_use (@3))
8365     (IFN_FNMS @0 @1 @2))))
8367  (simplify
8368   (IFN_FMS:c (negate @0) @1 @2)
8369   (IFN_FNMS @0 @1 @2))
8370  (simplify
8371   (IFN_FMS @0 @1 (negate @2))
8372   (IFN_FMA @0 @1 @2))
8373  (simplify
8374   (IFN_FMS:c (negate @0) @1 (negate @2))
8375   (IFN_FNMA @0 @1 @2))
8376  (simplify
8377   (negate (IFN_FMS@3 @0 @1 @2))
8378    (if (single_use (@3))
8379     (IFN_FNMA @0 @1 @2)))
8381  (simplify
8382   (IFN_FNMA:c (negate @0) @1 @2)
8383   (IFN_FMA @0 @1 @2))
8384  (simplify
8385   (IFN_FNMA @0 @1 (negate @2))
8386   (IFN_FNMS @0 @1 @2))
8387  (simplify
8388   (IFN_FNMA:c (negate @0) @1 (negate @2))
8389   (IFN_FMS @0 @1 @2))
8390  (simplify
8391   (negate (IFN_FNMA@3 @0 @1 @2))
8392   (if (single_use (@3))
8393    (IFN_FMS @0 @1 @2)))
8395  (simplify
8396   (IFN_FNMS:c (negate @0) @1 @2)
8397   (IFN_FMS @0 @1 @2))
8398  (simplify
8399   (IFN_FNMS @0 @1 (negate @2))
8400   (IFN_FNMA @0 @1 @2))
8401  (simplify
8402   (IFN_FNMS:c (negate @0) @1 (negate @2))
8403   (IFN_FMA @0 @1 @2))
8404  (simplify
8405   (negate (IFN_FNMS@3 @0 @1 @2))
8406   (if (single_use (@3))
8407    (IFN_FMA @0 @1 @2))))
8409 /* CLZ simplifications.  */
8410 (for clz (CLZ)
8411  (for op (eq ne)
8412       cmp (lt ge)
8413   (simplify
8414    (op (clz:s@2 @0) INTEGER_CST@1)
8415    (if (integer_zerop (@1) && single_use (@2))
8416     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8417     (with { tree type0 = TREE_TYPE (@0);
8418             tree stype = signed_type_for (type0);
8419             HOST_WIDE_INT val = 0;
8420             /* Punt on hypothetical weird targets.  */
8421             if (clz == CFN_CLZ
8422                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8423                                               val) == 2
8424                 && val == 0)
8425               stype = NULL_TREE;
8426           }
8427      (if (stype)
8428       (cmp (convert:stype @0) { build_zero_cst (stype); })))
8429     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8430     (with { bool ok = true;
8431             HOST_WIDE_INT val = 0;
8432             tree type0 = TREE_TYPE (@0);
8433             /* Punt on hypothetical weird targets.  */
8434             if (clz == CFN_CLZ
8435                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8436                                               val) == 2
8437                 && val == TYPE_PRECISION (type0) - 1)
8438               ok = false;
8439           }
8440      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8441       (op @0 { build_one_cst (type0); })))))))
8443 /* CTZ simplifications.  */
8444 (for ctz (CTZ)
8445  (for op (ge gt le lt)
8446       cmp (eq eq ne ne)
8447   (simplify
8448    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8449    (op (ctz:s @0) INTEGER_CST@1)
8450     (with { bool ok = true;
8451             HOST_WIDE_INT val = 0;
8452             if (!tree_fits_shwi_p (@1))
8453               ok = false;
8454             else
8455               {
8456                 val = tree_to_shwi (@1);
8457                 /* Canonicalize to >= or <.  */
8458                 if (op == GT_EXPR || op == LE_EXPR)
8459                   {
8460                     if (val == HOST_WIDE_INT_MAX)
8461                       ok = false;
8462                     else
8463                       val++;
8464                   }
8465               }
8466             bool zero_res = false;
8467             HOST_WIDE_INT zero_val = 0;
8468             tree type0 = TREE_TYPE (@0);
8469             int prec = TYPE_PRECISION (type0);
8470             if (ctz == CFN_CTZ
8471                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8472                                               zero_val) == 2)
8473               zero_res = true;
8474           }
8475      (if (val <= 0)
8476       (if (ok && (!zero_res || zero_val >= val))
8477        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8478       (if (val >= prec)
8479        (if (ok && (!zero_res || zero_val < val))
8480         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8481        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8482         (cmp (bit_and @0 { wide_int_to_tree (type0,
8483                                              wi::mask (val, false, prec)); })
8484              { build_zero_cst (type0); })))))))
8485  (for op (eq ne)
8486   (simplify
8487    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8488    (op (ctz:s @0) INTEGER_CST@1)
8489     (with { bool zero_res = false;
8490             HOST_WIDE_INT zero_val = 0;
8491             tree type0 = TREE_TYPE (@0);
8492             int prec = TYPE_PRECISION (type0);
8493             if (ctz == CFN_CTZ
8494                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8495                                               zero_val) == 2)
8496               zero_res = true;
8497           }
8498      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8499       (if (!zero_res || zero_val != wi::to_widest (@1))
8500        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8501       (if (!zero_res || zero_val < 0 || zero_val >= prec)
8502        (op (bit_and @0 { wide_int_to_tree (type0,
8503                                            wi::mask (tree_to_uhwi (@1) + 1,
8504                                                      false, prec)); })
8505            { wide_int_to_tree (type0,
8506                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8507                                                  false, prec)); })))))))
8509 /* POPCOUNT simplifications.  */
8510 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8511 (simplify
8512   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8513   (if (INTEGRAL_TYPE_P (type)
8514        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8515     (POPCOUNT (bit_ior @0 @1))))
8517 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8518 (for popcount (POPCOUNT)
8519   (for cmp (le eq ne gt)
8520        rep (eq eq ne ne)
8521     (simplify
8522       (cmp (popcount @0) integer_zerop)
8523       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8525 /* popcount(bswap(x)) is popcount(x).  */
8526 (for popcount (POPCOUNT)
8527   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8528               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8529     (simplify
8530       (popcount (convert?@0 (bswap:s@1 @2)))
8531       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8532            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8533         (with { tree type0 = TREE_TYPE (@0);
8534                 tree type1 = TREE_TYPE (@1);
8535                 unsigned int prec0 = TYPE_PRECISION (type0);
8536                 unsigned int prec1 = TYPE_PRECISION (type1); }
8537           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8538             (popcount (convert:type0 (convert:type1 @2)))))))))
8540 /* popcount(rotate(X Y)) is popcount(X).  */
8541 (for popcount (POPCOUNT)
8542   (for rot (lrotate rrotate)
8543     (simplify
8544       (popcount (convert?@0 (rot:s@1 @2 @3)))
8545       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8546            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8547            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8548         (with { tree type0 = TREE_TYPE (@0);
8549                 tree type1 = TREE_TYPE (@1);
8550                 unsigned int prec0 = TYPE_PRECISION (type0);
8551                 unsigned int prec1 = TYPE_PRECISION (type1); }
8552           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8553             (popcount (convert:type0 @2))))))))
8555 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8556 (simplify
8557   (bit_and (POPCOUNT @0) integer_onep)
8558   (PARITY @0))
8560 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8561 (simplify
8562   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8563   (plus (POPCOUNT @0) (POPCOUNT @1)))
8565 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8566 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8567 (for popcount (POPCOUNT)
8568   (for log1 (bit_and bit_ior)
8569        log2 (bit_ior bit_and)
8570     (simplify
8571       (minus (plus:s (popcount:s @0) (popcount:s @1))
8572              (popcount:s (log1:cs @0 @1)))
8573       (popcount (log2 @0 @1)))
8574     (simplify
8575       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8576               (popcount:s @1))
8577       (popcount (log2 @0 @1)))))
8579 /* PARITY simplifications.  */
8580 /* parity(~X) is parity(X).  */
8581 (simplify
8582   (PARITY (bit_not @0))
8583   (PARITY @0))
8585 /* parity(bswap(x)) is parity(x).  */
8586 (for parity (PARITY)
8587   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8588               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8589     (simplify
8590       (parity (convert?@0 (bswap:s@1 @2)))
8591       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8592            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8593            && TYPE_PRECISION (TREE_TYPE (@0))
8594               >= TYPE_PRECISION (TREE_TYPE (@1)))
8595         (with { tree type0 = TREE_TYPE (@0);
8596                 tree type1 = TREE_TYPE (@1); }
8597           (parity (convert:type0 (convert:type1 @2))))))))
8599 /* parity(rotate(X Y)) is parity(X).  */
8600 (for parity (PARITY)
8601   (for rot (lrotate rrotate)
8602     (simplify
8603       (parity (convert?@0 (rot:s@1 @2 @3)))
8604       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8605            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8606            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8607            && TYPE_PRECISION (TREE_TYPE (@0))
8608               >= TYPE_PRECISION (TREE_TYPE (@1)))
8609         (with { tree type0 = TREE_TYPE (@0); }
8610           (parity (convert:type0 @2)))))))
8612 /* parity(X)^parity(Y) is parity(X^Y).  */
8613 (simplify
8614   (bit_xor (PARITY:s @0) (PARITY:s @1))
8615   (PARITY (bit_xor @0 @1)))
8617 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8618 (for func (POPCOUNT BSWAP FFS PARITY)
8619  (simplify
8620   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8621   @3))
8623 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8624    where CST is precision-1. */
8625 (for func (CLRSB)
8626  (simplify
8627   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8628   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8629    @4)))
8631 #if GIMPLE
8632 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8633 (for func (CLZ)
8634  (simplify
8635   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8636   (with { int val;
8637           internal_fn ifn = IFN_LAST;
8638           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8639               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8640                                             val) == 2)
8641             ifn = IFN_CLZ;
8642         }
8643    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8644     (IFN_CLZ @3)))))
8646 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8647 (for func (CTZ)
8648  (simplify
8649   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8650   (with { int val;
8651           internal_fn ifn = IFN_LAST;
8652           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8653               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8654                                             val) == 2)
8655             ifn = IFN_CTZ;
8656         }
8657    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8658     (IFN_CTZ @3)))))
8659 #endif
8661 /* Common POPCOUNT/PARITY simplifications.  */
8662 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8663 (for pfun (POPCOUNT PARITY)
8664   (simplify
8665     (pfun @0)
8666     (if (INTEGRAL_TYPE_P (type))
8667      (with { wide_int nz = tree_nonzero_bits (@0); }
8668        (switch
8669          (if (nz == 1)
8670            (convert @0))
8671          (if (wi::popcount (nz) == 1)
8672            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8673              (convert (rshift:utype (convert:utype @0)
8674                                     { build_int_cst (integer_type_node,
8675                                                      wi::ctz (nz)); })))))))))
8677 #if GIMPLE
8678 /* 64- and 32-bits branchless implementations of popcount are detected:
8680    int popcount64c (uint64_t x)
8681    {
8682      x -= (x >> 1) & 0x5555555555555555ULL;
8683      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8684      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8685      return (x * 0x0101010101010101ULL) >> 56;
8686    }
8688    int popcount32c (uint32_t x)
8689    {
8690      x -= (x >> 1) & 0x55555555;
8691      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8692      x = (x + (x >> 4)) & 0x0f0f0f0f;
8693      return (x * 0x01010101) >> 24;
8694    }  */
8695 (simplify
8696  (rshift
8697   (mult
8698    (bit_and
8699     (plus:c
8700      (rshift @8 INTEGER_CST@5)
8701       (plus:c@8
8702        (bit_and @6 INTEGER_CST@7)
8703         (bit_and
8704          (rshift
8705           (minus@6 @0
8706            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8707           INTEGER_CST@10)
8708          INTEGER_CST@9)))
8709     INTEGER_CST@3)
8710    INTEGER_CST@2)
8711   INTEGER_CST@1)
8712   /* Check constants and optab.  */
8713   (with { unsigned prec = TYPE_PRECISION (type);
8714           int shift = (64 - prec) & 63;
8715           unsigned HOST_WIDE_INT c1
8716             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8717           unsigned HOST_WIDE_INT c2
8718             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8719           unsigned HOST_WIDE_INT c3
8720             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8721           unsigned HOST_WIDE_INT c4
8722             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8723    }
8724    (if (prec >= 16
8725         && prec <= 64
8726         && pow2p_hwi (prec)
8727         && TYPE_UNSIGNED (type)
8728         && integer_onep (@4)
8729         && wi::to_widest (@10) == 2
8730         && wi::to_widest (@5) == 4
8731         && wi::to_widest (@1) == prec - 8
8732         && tree_to_uhwi (@2) == c1
8733         && tree_to_uhwi (@3) == c2
8734         && tree_to_uhwi (@9) == c3
8735         && tree_to_uhwi (@7) == c3
8736         && tree_to_uhwi (@11) == c4)
8737     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8738                                          OPTIMIZE_FOR_BOTH))
8739      (convert (IFN_POPCOUNT:type @0))
8740      /* Try to do popcount in two halves.  PREC must be at least
8741         five bits for this to work without extension before adding.  */
8742      (with {
8743        tree half_type = NULL_TREE;
8744        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8745        int half_prec = 8;
8746        if (m.exists ()
8747            && m.require () != TYPE_MODE (type))
8748          {
8749            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8750            half_type = build_nonstandard_integer_type (half_prec, 1);
8751          }
8752        gcc_assert (half_prec > 2);
8753       }
8754       (if (half_type != NULL_TREE
8755            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8756                                               OPTIMIZE_FOR_BOTH))
8757        (convert (plus
8758          (IFN_POPCOUNT:half_type (convert @0))
8759          (IFN_POPCOUNT:half_type (convert (rshift @0
8760             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8762 /* __builtin_ffs needs to deal on many targets with the possible zero
8763    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8764    should lead to better code.  */
8765 (simplify
8766  (FFS tree_expr_nonzero_p@0)
8767  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8768       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8769                                          OPTIMIZE_FOR_SPEED))
8770   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8771    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8772 #endif
8774 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8775           BUILT_IN_FFSIMAX)
8776  /* __builtin_ffs (X) == 0 -> X == 0.
8777     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8778  (for cmp (eq ne)
8779   (simplify
8780    (cmp (ffs@2 @0) INTEGER_CST@1)
8781     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8782      (switch
8783       (if (integer_zerop (@1))
8784        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8785       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8786        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8787       (if (single_use (@2))
8788        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8789                                             wi::mask (tree_to_uhwi (@1),
8790                                                       false, prec)); })
8791             { wide_int_to_tree (TREE_TYPE (@0),
8792                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8793                                                   false, prec)); }))))))
8795  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8796  (for cmp (gt le)
8797       cmp2 (ne eq)
8798       cmp3 (eq ne)
8799       bit_op (bit_and bit_ior)
8800   (simplify
8801    (cmp (ffs@2 @0) INTEGER_CST@1)
8802     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8803      (switch
8804       (if (integer_zerop (@1))
8805        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8806       (if (tree_int_cst_sgn (@1) < 0)
8807        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8808       (if (wi::to_widest (@1) >= prec)
8809        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8810       (if (wi::to_widest (@1) == prec - 1)
8811        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8812                                     wi::shifted_mask (prec - 1, 1,
8813                                                       false, prec)); }))
8814       (if (single_use (@2))
8815        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8816                (cmp3 (bit_and @0
8817                               { wide_int_to_tree (TREE_TYPE (@0),
8818                                                   wi::mask (tree_to_uhwi (@1),
8819                                                   false, prec)); })
8820                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8822 #if GIMPLE
8824 /* Simplify:
8825      a = op a1
8826      r = cond ? a : b
8827      --> r = .COND_FN (cond, a, b)
8828 and,
8829     a = op a1
8830     r = cond ? b : a
8831     --> r = .COND_FN (~cond, b, a).  */
8833 (for uncond_op (UNCOND_UNARY)
8834      cond_op (COND_UNARY)
8835  (simplify
8836   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8837    (with { tree op_type = TREE_TYPE (@3); }
8838     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8839         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8840      (cond_op @0 @1 @2))))
8841  (simplify
8842   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8843    (with { tree op_type = TREE_TYPE (@3); }
8844     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8845         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8846      (cond_op (bit_not @0) @2 @1)))))
8848 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
8849 (simplify
8850  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8851  (if (canonicalize_math_after_vectorization_p ()
8852       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8853       && is_truth_type_for (type, TREE_TYPE (@0)))
8854  (if (integer_all_onesp (@1) && integer_zerop (@2))
8855   (IFN_COND_NOT @0 @3 @3))
8856   (if (integer_all_onesp (@2) && integer_zerop (@1))
8857    (IFN_COND_NOT (bit_not @0) @3 @3))))
8859 /* Simplify:
8861      a = a1 op a2
8862      r = c ? a : b;
8864    to:
8866      r = c ? a1 op a2 : b;
8868    if the target can do it in one go.  This makes the operation conditional
8869    on c, so could drop potentially-trapping arithmetic, but that's a valid
8870    simplification if the result of the operation isn't needed.
8872    Avoid speculatively generating a stand-alone vector comparison
8873    on targets that might not support them.  Any target implementing
8874    conditional internal functions must support the same comparisons
8875    inside and outside a VEC_COND_EXPR.  */
8877 (for uncond_op (UNCOND_BINARY)
8878      cond_op (COND_BINARY)
8879  (simplify
8880   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8881   (with { tree op_type = TREE_TYPE (@4); }
8882    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8883         && is_truth_type_for (op_type, TREE_TYPE (@0))
8884         && single_use (@4))
8885     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8886  (simplify
8887   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8888   (with { tree op_type = TREE_TYPE (@4); }
8889    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8890         && is_truth_type_for (op_type, TREE_TYPE (@0))
8891         && single_use (@4))
8892     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8894 /* Same for ternary operations.  */
8895 (for uncond_op (UNCOND_TERNARY)
8896      cond_op (COND_TERNARY)
8897  (simplify
8898   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8899   (with { tree op_type = TREE_TYPE (@5); }
8900    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8901         && is_truth_type_for (op_type, TREE_TYPE (@0))
8902         && single_use (@5))
8903     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8904  (simplify
8905   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8906   (with { tree op_type = TREE_TYPE (@5); }
8907    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8908         && is_truth_type_for (op_type, TREE_TYPE (@0))
8909         && single_use (@5))
8910     (view_convert (cond_op (bit_not @0) @2 @3 @4
8911                   (view_convert:op_type @1)))))))
8912 #endif
8914 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8915    "else" value of an IFN_COND_*.  */
8916 (for cond_op (COND_BINARY)
8917  (simplify
8918   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8919   (with { tree op_type = TREE_TYPE (@3); }
8920    (if (element_precision (type) == element_precision (op_type))
8921     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8922  (simplify
8923   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8924   (with { tree op_type = TREE_TYPE (@5); }
8925    (if (inverse_conditions_p (@0, @2)
8926         && element_precision (type) == element_precision (op_type))
8927     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8929 /* Same for ternary operations.  */
8930 (for cond_op (COND_TERNARY)
8931  (simplify
8932   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8933   (with { tree op_type = TREE_TYPE (@4); }
8934    (if (element_precision (type) == element_precision (op_type))
8935     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8936  (simplify
8937   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8938   (with { tree op_type = TREE_TYPE (@6); }
8939    (if (inverse_conditions_p (@0, @2)
8940         && element_precision (type) == element_precision (op_type))
8941     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8943 /* Detect simplication for a conditional reduction where
8945    a = mask1 ? b : 0
8946    c = mask2 ? d + a : d
8948    is turned into
8950    c = mask1 && mask2 ? d + b : d.  */
8951 (simplify
8952   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
8953    (if (ANY_INTEGRAL_TYPE_P (type)
8954         || (FLOAT_TYPE_P (type)
8955             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
8956    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
8958 /* Detect simplication for a conditional length reduction where
8960    a = mask ? b : 0
8961    c = i < len + bias ? d + a : d
8963    is turned into
8965    c = mask && i < len + bias ? d + b : d.  */
8966 (simplify
8967   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
8968    (if (ANY_INTEGRAL_TYPE_P (type)
8969         || (FLOAT_TYPE_P (type)
8970             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
8971     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
8973 /* Detect simplification for vector condition folding where
8975   c = mask1 ? (masked_op mask2 a b) : b
8977   into
8979   c = masked_op (mask1 & mask2) a b
8981   where the operation can be partially applied to one operand. */
8983 (for cond_op (COND_BINARY)
8984  (simplify
8985   (vec_cond @0
8986    (cond_op:s @1 @2 @3 @4) @3)
8987   (cond_op (bit_and @1 @0) @2 @3 @4)))
8989 /* And same for ternary expressions.  */
8991 (for cond_op (COND_TERNARY)
8992  (simplify
8993   (vec_cond @0
8994    (cond_op:s @1 @2 @3 @4 @5) @4)
8995   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
8997 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8998    expressions like:
9000    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9001    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9003    If pointers are known not to wrap, B checks whether @1 bytes starting
9004    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9005    bytes.  A is more efficiently tested as:
9007    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9009    The equivalent expression for B is given by replacing @1 with @1 - 1:
9011    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9013    @0 and @2 can be swapped in both expressions without changing the result.
9015    The folds rely on sizetype's being unsigned (which is always true)
9016    and on its being the same width as the pointer (which we have to check).
9018    The fold replaces two pointer_plus expressions, two comparisons and
9019    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9020    the best case it's a saving of two operations.  The A fold retains one
9021    of the original pointer_pluses, so is a win even if both pointer_pluses
9022    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9023    used elsewhere, since all we end up doing is replacing a comparison with
9024    a pointer_plus.  We do still apply the fold under those circumstances
9025    though, in case applying it to other conditions eventually makes one of the
9026    pointer_pluses dead.  */
9027 (for ior (truth_orif truth_or bit_ior)
9028  (for cmp (le lt)
9029   (simplify
9030    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9031         (cmp:cs (pointer_plus@4 @2 @1) @0))
9032    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9033         && TYPE_OVERFLOW_WRAPS (sizetype)
9034         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9035     /* Calculate the rhs constant.  */
9036     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9037             offset_int rhs = off * 2; }
9038      /* Always fails for negative values.  */
9039      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9040       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9041          pick a canonical order.  This increases the chances of using the
9042          same pointer_plus in multiple checks.  */
9043       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9044               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9045        (if (cmp == LT_EXPR)
9046         (gt (convert:sizetype
9047              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9048                                      { swap_p ? @0 : @2; }))
9049             { rhs_tree; })
9050         (gt (convert:sizetype
9051              (pointer_diff:ssizetype
9052               (pointer_plus { swap_p ? @2 : @0; }
9053                             { wide_int_to_tree (sizetype, off); })
9054               { swap_p ? @0 : @2; }))
9055             { rhs_tree; })))))))))
9057 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9058    element of @1.  */
9059 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9060  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9061   (with { int i = single_nonzero_element (@1); }
9062    (if (i >= 0)
9063     (with { tree elt = vector_cst_elt (@1, i);
9064             tree elt_type = TREE_TYPE (elt);
9065             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9066             tree size = bitsize_int (elt_bits);
9067             tree pos = bitsize_int (elt_bits * i); }
9068      (view_convert
9069       (bit_and:elt_type
9070        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9071        { elt; })))))))
9073 /* Fold reduction of a single nonzero element constructor.  */
9074 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9075   (simplify (reduc (CONSTRUCTOR@0))
9076     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9077                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9078             tree elt = ctor_single_nonzero_element (ctor); }
9079       (if (elt
9080            && !HONOR_SNANS (type)
9081            && !HONOR_SIGNED_ZEROS (type))
9082         { elt; }))))
9084 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9085 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9086             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9087      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9088   (simplify (reduc (op @0 VECTOR_CST@1))
9089     (op (reduc:type @0) (reduc:type @1))))
9091 /* Simplify vector floating point operations of alternating sub/add pairs
9092    into using an fneg of a wider element type followed by a normal add.
9093    under IEEE 754 the fneg of the wider type will negate every even entry
9094    and when doing an add we get a sub of the even and add of every odd
9095    elements.  */
9096 (for plusminus (plus minus)
9097      minusplus (minus plus)
9098  (simplify
9099   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9100    (if (!VECTOR_INTEGER_TYPE_P (type)
9101         && !FLOAT_WORDS_BIG_ENDIAN
9102         /* plus is commutative, while minus is not, so :c can't be used.
9103            Do equality comparisons by hand and at the end pick the operands
9104            from the minus.  */
9105         && (operand_equal_p (@0, @2, 0)
9106             ? operand_equal_p (@1, @3, 0)
9107             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9108    (with
9109     {
9110       /* Build a vector of integers from the tree mask.  */
9111       vec_perm_builder builder;
9112     }
9113     (if (tree_to_vec_perm_builder (&builder, @4))
9114      (with
9115       {
9116         /* Create a vec_perm_indices for the integer vector.  */
9117         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9118         vec_perm_indices sel (builder, 2, nelts);
9119         machine_mode vec_mode = TYPE_MODE (type);
9120         machine_mode wide_mode;
9121         scalar_mode wide_elt_mode;
9122         poly_uint64 wide_nunits;
9123         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9124       }
9125       (if (VECTOR_MODE_P (vec_mode)
9126            && sel.series_p (0, 2, 0, 2)
9127            && sel.series_p (1, 2, nelts + 1, 2)
9128            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9129            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9130            && related_vector_mode (vec_mode, wide_elt_mode,
9131                                    wide_nunits).exists (&wide_mode))
9132        (with
9133         {
9134           tree stype
9135             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9136                                               TYPE_UNSIGNED (type));
9137           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9139           /* The format has to be a non-extended ieee format.  */
9140           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9141           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9142         }
9143         (if (TYPE_MODE (stype) != BLKmode
9144              && VECTOR_TYPE_P (ntype)
9145              && fmt_old != NULL
9146              && fmt_new != NULL)
9147          (with
9148           {
9149             /* If the target doesn't support v1xx vectors, try using
9150                scalar mode xx instead.  */
9151             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9152                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9153               ntype = stype;
9154           }
9155           (if (fmt_new->signbit_rw
9156                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9157                && fmt_new->signbit_rw == fmt_new->signbit_ro
9158                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9159                                                  TYPE_MODE (type), ALL_REGS)
9160                && ((optimize_vectors_before_lowering_p ()
9161                     && VECTOR_TYPE_P (ntype))
9162                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9163            (if (plusminus == PLUS_EXPR)
9164             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9165             (minus @0 (view_convert:type
9166                         (negate (view_convert:ntype @1))))))))))))))))
9168 (simplify
9169  (vec_perm @0 @1 VECTOR_CST@2)
9170  (with
9171   {
9172     tree op0 = @0, op1 = @1, op2 = @2;
9173     machine_mode result_mode = TYPE_MODE (type);
9174     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9176     /* Build a vector of integers from the tree mask.  */
9177     vec_perm_builder builder;
9178   }
9179   (if (tree_to_vec_perm_builder (&builder, op2))
9180    (with
9181     {
9182       /* Create a vec_perm_indices for the integer vector.  */
9183       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9184       bool single_arg = (op0 == op1);
9185       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9186     }
9187     (if (sel.series_p (0, 1, 0, 1))
9188      { op0; }
9189      (if (sel.series_p (0, 1, nelts, 1))
9190       { op1; }
9191       (with
9192        {
9193          if (!single_arg)
9194            {
9195              if (sel.all_from_input_p (0))
9196                op1 = op0;
9197              else if (sel.all_from_input_p (1))
9198                {
9199                  op0 = op1;
9200                  sel.rotate_inputs (1);
9201                }
9202              else if (known_ge (poly_uint64 (sel[0]), nelts))
9203                {
9204                  std::swap (op0, op1);
9205                  sel.rotate_inputs (1);
9206                }
9207            }
9208          gassign *def;
9209          tree cop0 = op0, cop1 = op1;
9210          if (TREE_CODE (op0) == SSA_NAME
9211              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9212              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9213            cop0 = gimple_assign_rhs1 (def);
9214          if (TREE_CODE (op1) == SSA_NAME
9215              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9216              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9217            cop1 = gimple_assign_rhs1 (def);
9218          tree t;
9219        }
9220        (if ((TREE_CODE (cop0) == VECTOR_CST
9221              || TREE_CODE (cop0) == CONSTRUCTOR)
9222             && (TREE_CODE (cop1) == VECTOR_CST
9223                 || TREE_CODE (cop1) == CONSTRUCTOR)
9224             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9225         { t; }
9226         (with
9227          {
9228            bool changed = (op0 == op1 && !single_arg);
9229            tree ins = NULL_TREE;
9230            unsigned at = 0;
9232            /* See if the permutation is performing a single element
9233               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9234               in that case.  But only if the vector mode is supported,
9235               otherwise this is invalid GIMPLE.  */
9236            if (op_mode != BLKmode
9237                && (TREE_CODE (cop0) == VECTOR_CST
9238                    || TREE_CODE (cop0) == CONSTRUCTOR
9239                    || TREE_CODE (cop1) == VECTOR_CST
9240                    || TREE_CODE (cop1) == CONSTRUCTOR))
9241              {
9242                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9243                if (insert_first_p)
9244                  {
9245                    /* After canonicalizing the first elt to come from the
9246                       first vector we only can insert the first elt from
9247                       the first vector.  */
9248                    at = 0;
9249                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9250                      op0 = op1;
9251                  }
9252                /* The above can fail for two-element vectors which always
9253                   appear to insert the first element, so try inserting
9254                   into the second lane as well.  For more than two
9255                   elements that's wasted time.  */
9256                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9257                  {
9258                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9259                    for (at = 0; at < encoded_nelts; ++at)
9260                      if (maybe_ne (sel[at], at))
9261                        break;
9262                    if (at < encoded_nelts
9263                        && (known_eq (at + 1, nelts)
9264                            || sel.series_p (at + 1, 1, at + 1, 1)))
9265                      {
9266                        if (known_lt (poly_uint64 (sel[at]), nelts))
9267                          ins = fold_read_from_vector (cop0, sel[at]);
9268                        else
9269                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9270                      }
9271                  }
9272              }
9274            /* Generate a canonical form of the selector.  */
9275            if (!ins && sel.encoding () != builder)
9276              {
9277                /* Some targets are deficient and fail to expand a single
9278                   argument permutation while still allowing an equivalent
9279                   2-argument version.  */
9280                tree oldop2 = op2;
9281                if (sel.ninputs () == 2
9282                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9283                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9284                else
9285                  {
9286                    vec_perm_indices sel2 (builder, 2, nelts);
9287                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9288                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9289                    else
9290                      /* Not directly supported with either encoding,
9291                         so use the preferred form.  */
9292                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9293                  }
9294                if (!operand_equal_p (op2, oldop2, 0))
9295                  changed = true;
9296              }
9297          }
9298          (if (ins)
9299           (bit_insert { op0; } { ins; }
9300            { bitsize_int (at * vector_element_bits (type)); })
9301           (if (changed)
9302            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9304 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9306 (match vec_same_elem_p
9307  (vec_duplicate @0))
9309 (match vec_same_elem_p
9310  CONSTRUCTOR@0
9311  (if (TREE_CODE (@0) == SSA_NAME
9312       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9314 (match vec_same_elem_p
9315  @0
9316  (if (uniform_vector_p (@0))))
9319 (simplify
9320  (vec_perm vec_same_elem_p@0 @0 @1)
9321  (if (types_match (type, TREE_TYPE (@0)))
9322   @0
9323   (with
9324    {
9325      tree elem = uniform_vector_p (@0);
9326    }
9327    (if (elem)
9328     { build_vector_from_val (type, elem); }))))
9330 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9331 (simplify
9332  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9333  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9334   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9335 (simplify
9336  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9337  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9338   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9341 /* Merge
9342      c = VEC_PERM_EXPR <a, b, VCST0>;
9343      d = VEC_PERM_EXPR <c, c, VCST1>;
9344    to
9345      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9347 (simplify
9348  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9349  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9350   (with
9351    {
9352      machine_mode result_mode = TYPE_MODE (type);
9353      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9354      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9355      vec_perm_builder builder0;
9356      vec_perm_builder builder1;
9357      vec_perm_builder builder2 (nelts, nelts, 1);
9358    }
9359    (if (tree_to_vec_perm_builder (&builder0, @3)
9360         && tree_to_vec_perm_builder (&builder1, @4))
9361     (with
9362      {
9363        vec_perm_indices sel0 (builder0, 2, nelts);
9364        vec_perm_indices sel1 (builder1, 1, nelts);
9366        for (int i = 0; i < nelts; i++)
9367          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9369        vec_perm_indices sel2 (builder2, 2, nelts);
9371        tree op0 = NULL_TREE;
9372        /* If the new VEC_PERM_EXPR can't be handled but both
9373           original VEC_PERM_EXPRs can, punt.
9374           If one or both of the original VEC_PERM_EXPRs can't be
9375           handled and the new one can't be either, don't increase
9376           number of VEC_PERM_EXPRs that can't be handled.  */
9377        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9378            || (single_use (@0)
9379                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9380                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9381                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9382          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9383      }
9384      (if (op0)
9385       (vec_perm @1 @2 { op0; })))))))
9387 /* Merge
9388      c = VEC_PERM_EXPR <a, b, VCST0>;
9389      d = VEC_PERM_EXPR <x, c, VCST1>;
9390    to
9391      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9392    when all elements from a or b are replaced by the later
9393    permutation.  */
9395 (simplify
9396  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9397  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9398   (with
9399    {
9400      machine_mode result_mode = TYPE_MODE (type);
9401      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9402      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9403      vec_perm_builder builder0;
9404      vec_perm_builder builder1;
9405      vec_perm_builder builder2 (nelts, nelts, 2);
9406    }
9407    (if (tree_to_vec_perm_builder (&builder0, @3)
9408         && tree_to_vec_perm_builder (&builder1, @4))
9409     (with
9410      {
9411        vec_perm_indices sel0 (builder0, 2, nelts);
9412        vec_perm_indices sel1 (builder1, 2, nelts);
9413        bool use_1 = false, use_2 = false;
9415        for (int i = 0; i < nelts; i++)
9416          {
9417            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9418              builder2.quick_push (sel1[i]);
9419            else
9420              {
9421                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9422                                     .to_constant ()];
9423                if (known_lt (j, sel0.nelts_per_input ()))
9424                  use_1 = true;
9425                else
9426                  {
9427                    use_2 = true;
9428                    j -= sel0.nelts_per_input ();
9429                  }
9430                builder2.quick_push (j + sel1.nelts_per_input ());
9431              }
9432          }
9433      }
9434      (if (use_1 ^ use_2)
9435       (with
9436        {
9437          vec_perm_indices sel2 (builder2, 2, nelts);
9438          tree op0 = NULL_TREE;
9439          /* If the new VEC_PERM_EXPR can't be handled but both
9440             original VEC_PERM_EXPRs can, punt.
9441             If one or both of the original VEC_PERM_EXPRs can't be
9442             handled and the new one can't be either, don't increase
9443             number of VEC_PERM_EXPRs that can't be handled.  */
9444          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9445              || (single_use (@0)
9446                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9447                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9448                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9449            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9450        }
9451        (if (op0)
9452         (switch
9453          (if (use_1)
9454           (vec_perm @5 @1 { op0; }))
9455          (if (use_2)
9456           (vec_perm @5 @2 { op0; })))))))))))
9458 /* And the case with swapped outer permute sources.  */
9460 (simplify
9461  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9462  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9463   (with
9464    {
9465      machine_mode result_mode = TYPE_MODE (type);
9466      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9467      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9468      vec_perm_builder builder0;
9469      vec_perm_builder builder1;
9470      vec_perm_builder builder2 (nelts, nelts, 2);
9471    }
9472    (if (tree_to_vec_perm_builder (&builder0, @3)
9473         && tree_to_vec_perm_builder (&builder1, @4))
9474     (with
9475      {
9476        vec_perm_indices sel0 (builder0, 2, nelts);
9477        vec_perm_indices sel1 (builder1, 2, nelts);
9478        bool use_1 = false, use_2 = false;
9480        for (int i = 0; i < nelts; i++)
9481          {
9482            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9483              builder2.quick_push (sel1[i]);
9484            else
9485              {
9486                poly_uint64 j = sel0[sel1[i].to_constant ()];
9487                if (known_lt (j, sel0.nelts_per_input ()))
9488                  use_1 = true;
9489                else
9490                  {
9491                    use_2 = true;
9492                    j -= sel0.nelts_per_input ();
9493                  }
9494                builder2.quick_push (j);
9495              }
9496          }
9497      }
9498      (if (use_1 ^ use_2)
9499       (with
9500        {
9501          vec_perm_indices sel2 (builder2, 2, nelts);
9502          tree op0 = NULL_TREE;
9503          /* If the new VEC_PERM_EXPR can't be handled but both
9504             original VEC_PERM_EXPRs can, punt.
9505             If one or both of the original VEC_PERM_EXPRs can't be
9506             handled and the new one can't be either, don't increase
9507             number of VEC_PERM_EXPRs that can't be handled.  */
9508          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9509              || (single_use (@0)
9510                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9511                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9512                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9513            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9514        }
9515        (if (op0)
9516         (switch
9517          (if (use_1)
9518           (vec_perm @1 @5 { op0; }))
9519          (if (use_2)
9520           (vec_perm @2 @5 { op0; })))))))))))
9523 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9524    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9525    constant which when multiplied by a power of 2 contains a unique value
9526    in the top 5 or 6 bits.  This is then indexed into a table which maps it
9527    to the number of trailing zeroes.  */
9528 (match (ctz_table_index @1 @2 @3)
9529   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9531 (match (cond_expr_convert_p @0 @2 @3 @6)
9532  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9533   (if (INTEGRAL_TYPE_P (type)
9534        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9535        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9536        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9537        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9538        && TYPE_PRECISION (TREE_TYPE (@0))
9539           == TYPE_PRECISION (TREE_TYPE (@2))
9540        && TYPE_PRECISION (TREE_TYPE (@0))
9541           == TYPE_PRECISION (TREE_TYPE (@3))
9542        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9543           signess when convert is truncation, but not ok for extension since
9544           it's sign_extend vs zero_extend.  */
9545        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9546            || (TYPE_UNSIGNED (TREE_TYPE (@2))
9547                == TYPE_UNSIGNED (TREE_TYPE (@3))))
9548        && single_use (@4)
9549        && single_use (@5))))
9551 (for bit_op (bit_and bit_ior bit_xor)
9552  (match (bitwise_induction_p @0 @2 @3)
9553   (bit_op:c
9554    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9555    @3)))
9557 (match (bitwise_induction_p @0 @2 @3)
9558  (bit_not
9559   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9561 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
9562    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
9563 (simplify
9564   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9565   (with { auto i = wi::neg (wi::to_wide (@2)); }
9566   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
9567     (if (wi::popcount (i) == 1
9568          && (wi::to_wide (@1)) == (i - 1))
9569       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9570         (bit_and @0 @1)
9571       (cond (le @0 @1) @0 (bit_and @0 @1))))))
9573 /* -x & 1 -> x & 1.  */
9574 (simplify 
9575  (bit_and (negate @0) integer_onep@1)
9576  (if (!TYPE_OVERFLOW_SANITIZED (type))
9577   (bit_and @0 @1)))
9579 /* `-a` is just `a` if the type is 1bit wide or when converting
9580    to a 1bit type; similar to the above transformation of `(-x)&1`.
9581    This is used mostly with the transformation of
9582    `a ? ~b : b` into `(-a)^b`.
9583    It also can show up with bitfields.  */
9584 (simplify
9585  (convert? (negate @0))
9586  (if (INTEGRAL_TYPE_P (type)
9587       && TYPE_PRECISION (type) == 1
9588       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9589   (convert @0)))
9591 /* Optimize
9592    c1 = VEC_PERM_EXPR (a, a, mask)
9593    c2 = VEC_PERM_EXPR (b, b, mask)
9594    c3 = c1 op c2
9595    -->
9596    c = a op b
9597    c3 = VEC_PERM_EXPR (c, c, mask)
9598    For all integer non-div operations.  */
9599 (for op (plus minus mult bit_and bit_ior bit_xor
9600          lshift rshift)
9601  (simplify
9602   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9603    (if (VECTOR_INTEGER_TYPE_P (type))
9604     (vec_perm (op@3 @0 @1) @3 @2))))
9606 /* Similar for float arithmetic when permutation constant covers
9607    all vector elements.  */
9608 (for op (plus minus mult)
9609  (simplify
9610   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9611    (if (VECTOR_FLOAT_TYPE_P (type)
9612         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9613     (with
9614      {
9615        tree perm_cst = @2;
9616        vec_perm_builder builder;
9617        bool full_perm_p = false;
9618        if (tree_to_vec_perm_builder (&builder, perm_cst))
9619          {
9620            unsigned HOST_WIDE_INT nelts;
9622            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9623            /* Create a vec_perm_indices for the VECTOR_CST.  */
9624            vec_perm_indices sel (builder, 1, nelts);
9626            /* Check if perm indices covers all vector elements.  */
9627            if (sel.encoding ().encoded_full_vector_p ())
9628              {
9629                auto_sbitmap seen (nelts);
9630                bitmap_clear (seen);
9632                unsigned HOST_WIDE_INT count = 0, i;
9634                for (i = 0; i < nelts; i++)
9635                  {
9636                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9637                     break;
9638                    count++;
9639                  }
9640                full_perm_p = count == nelts;
9641              }
9642          }
9643       }
9644       (if (full_perm_p)
9645         (vec_perm (op@3 @0 @1) @3 @2))))))