ada: Fix internal error on instantiation with private component type
[official-gcc.git] / gcc / match.pd
blobc6df4230b5f7fd99bec48cdc9713cc45d1db03cc
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2023 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
91 /* Binary operations and their associated IFN_COND_* function.  */
92 (define_operator_list UNCOND_BINARY
93   plus minus
94   mult trunc_div trunc_mod rdiv
95   min max
96   IFN_FMIN IFN_FMAX
97   bit_and bit_ior bit_xor
98   lshift rshift)
99 (define_operator_list COND_BINARY
100   IFN_COND_ADD IFN_COND_SUB
101   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102   IFN_COND_MIN IFN_COND_MAX
103   IFN_COND_FMIN IFN_COND_FMAX
104   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105   IFN_COND_SHL IFN_COND_SHR)
107 /* Same for ternary operations.  */
108 (define_operator_list UNCOND_TERNARY
109   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117   BUILT_IN_ATOMIC_FETCH_OR_16
118   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120   BUILT_IN_ATOMIC_FETCH_XOR_16
121   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123   BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128   BUILT_IN_SYNC_FETCH_AND_OR_16
129   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131   BUILT_IN_SYNC_FETCH_AND_XOR_16
132   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134   BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*.  */
136 (define_operator_list ATOMIC_FETCH_AND_N
137   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139   BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*.  */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144   BUILT_IN_SYNC_FETCH_AND_AND_16)
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147    plus conditionalize on tree_nop_conversion_p conversions.  */
148 (match (nop_convert @0)
149  (convert @0)
150  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152  (view_convert @0)
153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
158 #if GIMPLE
159 /* These are used by gimple_bitwise_inverted_equal_p to simplify
160    detection of BIT_NOT and comparisons. */
161 (match (bit_not_with_nop @0)
162  (bit_not @0))
163 (match (bit_not_with_nop @0)
164  (convert (bit_not @0))
165  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
166 (for cmp (tcc_comparison)
167  (match (maybe_cmp @0)
168   (cmp@0 @1 @2))
169  (match (maybe_cmp @0)
170   (convert (cmp@0 @1 @2))
171    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
173 #endif
175 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
176    ABSU_EXPR returns unsigned absolute value of the operand and the operand
177    of the ABSU_EXPR will have the corresponding signed type.  */
178 (simplify (abs (convert @0))
179  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
180       && !TYPE_UNSIGNED (TREE_TYPE (@0))
181       && element_precision (type) > element_precision (TREE_TYPE (@0)))
182   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
183    (convert (absu:utype @0)))))
185 #if GIMPLE
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
187 (simplify
188  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
189  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
190       && !TYPE_UNSIGNED (TREE_TYPE (@0))
191       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
192   (abs @0)))
193 #endif
195 /* Simplifications of operations with one constant operand and
196    simplifications to constants or single values.  */
198 (for op (plus pointer_plus minus bit_ior bit_xor)
199   (simplify
200     (op @0 integer_zerop)
201     (non_lvalue @0)))
203 /* 0 +p index -> (type)index */
204 (simplify
205  (pointer_plus integer_zerop @1)
206  (non_lvalue (convert @1)))
208 /* ptr - 0 -> (type)ptr */
209 (simplify
210  (pointer_diff @0 integer_zerop)
211  (convert @0))
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214    Likewise if the operands are reversed.  */
215 (simplify
216  (plus:c @0 real_zerop@1)
217  (if (fold_real_zero_addition_p (type, @0, @1, 0))
218   (non_lvalue @0)))
220 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
221 (simplify
222  (minus @0 real_zerop@1)
223  (if (fold_real_zero_addition_p (type, @0, @1, 1))
224   (non_lvalue @0)))
226 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
227    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
228    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
229    if not -frounding-math.  For sNaNs the first operation would raise
230    exceptions but turn the result into qNan, so the second operation
231    would not raise it.   */
232 (for inner_op (plus minus)
233  (for outer_op (plus minus)
234   (simplify
235    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
236     (if (real_zerop (@1)
237          && real_zerop (@2)
238          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
239      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
240                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
241              bool outer_plus
242                = ((outer_op == PLUS_EXPR)
243                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244       (if (outer_plus && !inner_plus)
245        (outer_op @0 @2)
246        @3))))))
248 /* Simplify x - x.
249    This is unsafe for certain floats even in non-IEEE formats.
250    In IEEE, it is unsafe because it does wrong for NaNs.
251    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
252    Also note that operand_equal_p is always false if an operand
253    is volatile.  */
254 (simplify
255  (minus @0 @0)
256  (if (!FLOAT_TYPE_P (type)
257       || (!tree_expr_maybe_nan_p (@0)
258           && !tree_expr_maybe_infinite_p (@0)
259           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
260               || !HONOR_SIGNED_ZEROS (type))))
261   { build_zero_cst (type); }))
262 (simplify
263  (pointer_diff @@0 @0)
264  { build_zero_cst (type); })
266 (simplify
267  (mult @0 integer_zerop@1)
268  @1)
270 /* -x == x -> x == 0 */
271 (for cmp (eq ne)
272  (simplify
273   (cmp:c @0 (negate @0))
274    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
275         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
276     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
278 /* Maybe fold x * 0 to 0.  The expressions aren't the same
279    when x is NaN, since x * 0 is also NaN.  Nor are they the
280    same in modes with signed zeros, since multiplying a
281    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
282    since x * 0 is NaN.  */
283 (simplify
284  (mult @0 real_zerop@1)
285  (if (!tree_expr_maybe_nan_p (@0)
286       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
287       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
288   @1))
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291    Likewise for complex arithmetic with signed zeros.  */
292 (simplify
293  (mult @0 real_onep)
294  (if (!tree_expr_maybe_signaling_nan_p (@0)
295       && (!HONOR_SIGNED_ZEROS (type)
296           || !COMPLEX_FLOAT_TYPE_P (type)))
297   (non_lvalue @0)))
299 /* Transform x * -1.0 into -x.  */
300 (simplify
301  (mult @0 real_minus_onep)
302   (if (!tree_expr_maybe_signaling_nan_p (@0)
303        && (!HONOR_SIGNED_ZEROS (type)
304            || !COMPLEX_FLOAT_TYPE_P (type)))
305    (negate @0)))
307 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
308    unless the target has native support for the former but not the latter.  */
309 (simplify
310  (mult @0 VECTOR_CST@1)
311  (if (initializer_each_zero_or_onep (@1)
312       && !HONOR_SNANS (type)
313       && !HONOR_SIGNED_ZEROS (type))
314   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
315    (if (itype
316         && (!VECTOR_MODE_P (TYPE_MODE (type))
317             || (VECTOR_MODE_P (TYPE_MODE (itype))
318                 && optab_handler (and_optab,
319                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
320     (view_convert (bit_and:itype (view_convert @0)
321                                  (ne @1 { build_zero_cst (type); })))))))
323 /* In SWAR (SIMD within a register) code a signed comparison of packed data
324    can be constructed with a particular combination of shift, bitwise and,
325    and multiplication by constants.  If that code is vectorized we can
326    convert this pattern into a more efficient vector comparison.  */
327 (simplify
328  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329             uniform_integer_cst_p@2)
330     uniform_integer_cst_p@3)
331  (with {
332    tree rshift_cst = uniform_integer_cst_p (@1);
333    tree bit_and_cst = uniform_integer_cst_p (@2);
334    tree mult_cst = uniform_integer_cst_p (@3);
335   }
336   /* Make sure we're working with vectors and uniform vector constants.  */
337   (if (VECTOR_TYPE_P (type)
338        && tree_fits_uhwi_p (rshift_cst)
339        && tree_fits_uhwi_p (mult_cst)
340        && tree_fits_uhwi_p (bit_and_cst))
341    /* Compute what constants would be needed for this to represent a packed
342       comparison based on the shift amount denoted by RSHIFT_CST.  */
343    (with {
344      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
345      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
346      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
347      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
348      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
349      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
350      mult_i = tree_to_uhwi (mult_cst);
351      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
352      bit_and_i = tree_to_uhwi (bit_and_cst);
353      target_bit_and_i = 0;
355      /* The bit pattern in BIT_AND_I should be a mask for the least
356         significant bit of each packed element that is CMP_BITS wide.  */
357      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
358        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
359     }
360     (if ((exact_log2 (cmp_bits_i)) >= 0
361          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
362          && multiple_p (vec_bits, cmp_bits_i)
363          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
364          && target_mult_i == mult_i
365          && target_bit_and_i == bit_and_i)
366      /* Compute the vector shape for the comparison and check if the target is
367         able to expand the comparison with that type.  */
368      (with {
369        /* We're doing a signed comparison.  */
370        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
371        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
372        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
373        tree vec_truth_type = truth_type_for (vec_cmp_type);
374        tree zeros = build_zero_cst (vec_cmp_type);
375        tree ones = build_all_ones_cst (vec_cmp_type);
376       }
377       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
378            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
379        (view_convert:type (vec_cond (lt:vec_truth_type
380                                      (view_convert:vec_cmp_type @0)
381                                      { zeros; })
382                            { ones; } { zeros; })))))))))
384 (for cmp (gt ge lt le)
385      outp (convert convert negate negate)
386      outn (negate negate convert convert)
387  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
388  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
389  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
390  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
391  (simplify
392   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
393   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
394    (outp (abs @0))))
395  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
396  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
397  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
398  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
399  (simplify
400   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
401   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
402    (outn (abs @0)))))
404 /* Transform X * copysign (1.0, X) into abs(X). */
405 (simplify
406  (mult:c @0 (COPYSIGN_ALL real_onep @0))
407  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
408   (abs @0)))
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
411 (simplify
412  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414   (negate (abs @0))))
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
417 (simplify
418  (COPYSIGN_ALL REAL_CST@0 @1)
419  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420   (COPYSIGN_ALL (negate @0) @1)))
422 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
423    tree-ssa-math-opts.cc does the corresponding optimization for
424    unconditional multiplications (via xorsign).  */
425 (simplify
426  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427  (with { tree signs = sign_mask_for (type); }
428   (if (signs)
429    (with { tree inttype = TREE_TYPE (signs); }
430     (view_convert:type
431      (IFN_COND_XOR:inttype @0
432       (view_convert:inttype @1)
433       (bit_and (view_convert:inttype @2) { signs; })
434       (view_convert:inttype @3)))))))
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
437 (simplify
438   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
439   (abs @0))
441 /* X * 1, X / 1 -> X.  */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
443   (simplify
444     (op @0 integer_onep)
445     (non_lvalue @0)))
447 /* (A / (1 << B)) -> (A >> B).
448    Only for unsigned A.  For signed A, this would not preserve rounding
449    toward zero.
450    For example: (-1 / ( 1 << B)) !=  -1 >> B.
451    Also handle widening conversions, like:
452    (A / (unsigned long long) (1U << B)) -> (A >> B)
453    or
454    (A / (unsigned long long) (1 << B)) -> (A >> B).
455    If the left shift is signed, it can be done only if the upper bits
456    of A starting from shift's type sign bit are zero, as
457    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
458    so it is valid only if A >> 31 is zero.  */
459 (simplify
460  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
461  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
462       && (!VECTOR_TYPE_P (type)
463           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
464           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
465       && (useless_type_conversion_p (type, TREE_TYPE (@1))
466           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
467               && (TYPE_UNSIGNED (TREE_TYPE (@1))
468                   || (element_precision (type)
469                       == element_precision (TREE_TYPE (@1)))
470                   || (INTEGRAL_TYPE_P (type)
471                       && (tree_nonzero_bits (@0)
472                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
473                                       true,
474                                       element_precision (type))) == 0)))))
475    (if (!VECTOR_TYPE_P (type)
476         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
477         && element_precision (TREE_TYPE (@3)) < element_precision (type))
478     (convert (rshift @3 @2))
479     (rshift @0 @2))))
481 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
482    undefined behavior in constexpr evaluation, and assuming that the division
483    traps enables better optimizations than these anyway.  */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
485  /* 0 / X is always zero.  */
486  (simplify
487   (div integer_zerop@0 @1)
488   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
489   (if (!integer_zerop (@1))
490    @0))
491  /* X / -1 is -X.  */
492  (simplify
493   (div @0 integer_minus_onep@1)
494   (if (!TYPE_UNSIGNED (type))
495    (negate @0)))
496  /* X / bool_range_Y is X.  */ 
497  (simplify
498   (div @0 SSA_NAME@1)
499   (if (INTEGRAL_TYPE_P (type)
500        && ssa_name_has_boolean_range (@1)
501        && !flag_non_call_exceptions)
502    @0))
503  /* X / X is one.  */
504  (simplify
505   (div @0 @0)
506   /* But not for 0 / 0 so that we can get the proper warnings and errors.
507      And not for _Fract types where we can't build 1.  */
508   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
509        && !integer_zerop (@0)
510        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511    { build_one_cst (type); }))
512  /* X / abs (X) is X < 0 ? -1 : 1.  */
513  (simplify
514    (div:C @0 (abs @0))
515    (if (INTEGRAL_TYPE_P (type)
516         && TYPE_OVERFLOW_UNDEFINED (type)
517         && !integer_zerop (@0)
518         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
519     (cond (lt @0 { build_zero_cst (type); })
520           { build_minus_one_cst (type); } { build_one_cst (type); })))
521  /* X / -X is -1.  */
522  (simplify
523    (div:C @0 (negate @0))
524    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
525         && TYPE_OVERFLOW_UNDEFINED (type)
526         && !integer_zerop (@0)
527         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
528     { build_minus_one_cst (type); })))
530 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
531    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
532    for MOD instead of DIV.  */
533 (for floor_divmod (floor_div floor_mod)
534      trunc_divmod (trunc_div trunc_mod)
535  (simplify
536   (floor_divmod @0 @1)
537   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538        && TYPE_UNSIGNED (type))
539    (trunc_divmod @0 @1))))
541 /* 1 / X -> X == 1 for unsigned integer X.
542    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
543    But not for 1 / 0 so that we can get proper warnings and errors,
544    and not for 1-bit integers as they are edge cases better handled
545    elsewhere.  */
546 (simplify
547  (trunc_div integer_onep@0 @1)
548  (if (INTEGRAL_TYPE_P (type)
549       && TYPE_PRECISION (type) > 1
550       && !integer_zerop (@1)
551       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
552   (if (TYPE_UNSIGNED (type))
553    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
554    (with { tree utype = unsigned_type_for (type); }
555     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
556               { build_int_cst (utype, 2); })
557      @1 { build_zero_cst (type); })))))
559 /* Combine two successive divisions.  Note that combining ceil_div
560    and floor_div is trickier and combining round_div even more so.  */
561 (for div (trunc_div exact_div)
562  (simplify
563   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
564   (with {
565     wi::overflow_type overflow;
566     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567                             TYPE_SIGN (type), &overflow);
568    }
569    (if (div == EXACT_DIV_EXPR
570         || optimize_successive_divisions_p (@2, @3))
571     (if (!overflow)
572      (div @0 { wide_int_to_tree (type, mul); })
573      (if (TYPE_UNSIGNED (type)
574           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
575       { build_zero_cst (type); }))))))
577 /* Combine successive multiplications.  Similar to above, but handling
578    overflow is different.  */
579 (simplify
580  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
581  (with {
582    wi::overflow_type overflow;
583    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584                            TYPE_SIGN (type), &overflow);
585   }
586   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
587      otherwise undefined overflow implies that @0 must be zero.  */
588   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
589    (mult @0 { wide_int_to_tree (type, mul); }))))
591 /* Similar to above, but there could be an extra add/sub between
592    successive multuiplications.  */
593 (simplify
594  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
595  (with {
596    bool overflowed = true;
597    wi::overflow_type ovf1, ovf2;
598    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
599                            TYPE_SIGN (type), &ovf1);
600    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
601                            TYPE_SIGN (type), &ovf2);
602   if (TYPE_OVERFLOW_UNDEFINED (type))
603     {
604 #if GIMPLE
605       value_range vr0;
606       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
607           && get_global_range_query ()->range_of_expr (vr0, @4)
608           && !vr0.varying_p () && !vr0.undefined_p ())
609         {
610           wide_int wmin0 = vr0.lower_bound ();
611           wide_int wmax0 = vr0.upper_bound ();
612           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
613           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
614           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
615             {
616               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
617               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
618               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
619                 overflowed = false;
620             }
621         }
622 #endif
623     }
624   else
625    overflowed = false;
627   /* Skip folding on overflow.  */
628   (if (!overflowed)
629    (plus (mult @0 { wide_int_to_tree (type, mul); })
630          { wide_int_to_tree (type, add); }))))
632 /* Similar to above, but a multiplication between successive additions.  */
633 (simplify
634  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
635  (with {
636    bool overflowed = true;
637    wi::overflow_type ovf1;
638    wi::overflow_type ovf2;
639    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
640                            TYPE_SIGN (type), &ovf1);
641    wide_int add = wi::add (mul, wi::to_wide (@3),
642                            TYPE_SIGN (type), &ovf2);
643   if (TYPE_OVERFLOW_UNDEFINED (type))
644     {
645 #if GIMPLE
646       value_range vr0;
647       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
648           && get_global_range_query ()->range_of_expr (vr0, @0)
649           && !vr0.varying_p () && !vr0.undefined_p ())
650         {
651           wide_int wmin0 = vr0.lower_bound ();
652           wide_int wmax0 = vr0.upper_bound ();
653           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
654           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
655           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
656             {
657               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
658               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
659               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
660                 overflowed = false;
661             }
662         }
663 #endif
664     }
665   else
666    overflowed = false;
668   /* Skip folding on overflow.  */
669   (if (!overflowed)
670    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
672 /* Optimize A / A to 1.0 if we don't care about
673    NaNs or Infinities.  */
674 (simplify
675  (rdiv @0 @0)
676  (if (FLOAT_TYPE_P (type)
677       && ! HONOR_NANS (type)
678       && ! HONOR_INFINITIES (type))
679   { build_one_cst (type); }))
681 /* Optimize -A / A to -1.0 if we don't care about
682    NaNs or Infinities.  */
683 (simplify
684  (rdiv:C @0 (negate @0))
685  (if (FLOAT_TYPE_P (type)
686       && ! HONOR_NANS (type)
687       && ! HONOR_INFINITIES (type))
688   { build_minus_one_cst (type); }))
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
691 (simplify
692  (rdiv:C (convert? @0) (convert? (abs @0)))
693   (if (SCALAR_FLOAT_TYPE_P (type)
694        && ! HONOR_NANS (type)
695        && ! HONOR_INFINITIES (type))
696    (switch
697     (if (types_match (type, float_type_node))
698      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
699     (if (types_match (type, double_type_node))
700      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
701     (if (types_match (type, long_double_type_node))
702      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
704 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
705 (simplify
706  (rdiv @0 real_onep)
707  (if (!tree_expr_maybe_signaling_nan_p (@0))
708   (non_lvalue @0)))
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
711 (simplify
712  (rdiv @0 real_minus_onep)
713  (if (!tree_expr_maybe_signaling_nan_p (@0))
714   (negate @0)))
716 (if (flag_reciprocal_math)
717  /* Convert (A/B)/C to A/(B*C). */
718  (simplify
719   (rdiv (rdiv:s @0 @1) @2)
720   (rdiv @0 (mult @1 @2)))
722  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
723  (simplify
724   (rdiv @0 (mult:s @1 REAL_CST@2))
725   (with
726    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
727    (if (tem)
728     (rdiv (mult @0 { tem; } ) @1))))
730  /* Convert A/(B/C) to (A/B)*C  */
731  (simplify
732   (rdiv @0 (rdiv:s @1 @2))
733    (mult (rdiv @0 @1) @2)))
735 /* Simplify x / (- y) to -x / y.  */
736 (simplify
737  (rdiv @0 (negate @1))
738  (rdiv (negate @0) @1))
740 (if (flag_unsafe_math_optimizations)
741  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
742     Since C / x may underflow to zero, do this only for unsafe math.  */
743  (for op (lt le gt ge)
744       neg_op (gt ge lt le)
745   (simplify
746    (op (rdiv REAL_CST@0 @1) real_zerop@2)
747    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
748     (switch
749      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
750       (op @1 @2))
751      /* For C < 0, use the inverted operator.  */
752      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
753       (neg_op @1 @2)))))))
755 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
756 (for div (trunc_div ceil_div floor_div round_div exact_div)
757  (simplify
758   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
759   (if (integer_pow2p (@2)
760        && tree_int_cst_sgn (@2) > 0
761        && tree_nop_conversion_p (type, TREE_TYPE (@0))
762        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
763    (rshift (convert @0)
764            { build_int_cst (integer_type_node,
765                             wi::exact_log2 (wi::to_wide (@2))); }))))
767 /* If ARG1 is a constant, we can convert this to a multiply by the
768    reciprocal.  This does not have the same rounding properties,
769    so only do this if -freciprocal-math.  We can actually
770    always safely do it if ARG1 is a power of two, but it's hard to
771    tell if it is or not in a portable manner.  */
772 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
773  (simplify
774   (rdiv @0 cst@1)
775   (if (optimize)
776    (if (flag_reciprocal_math
777         && !real_zerop (@1))
778     (with
779      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
780      (if (tem)
781       (mult @0 { tem; } )))
782     (if (cst != COMPLEX_CST)
783      (with { tree inverse = exact_inverse (type, @1); }
784       (if (inverse)
785        (mult @0 { inverse; } ))))))))
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788  /* 0 % X is always zero.  */
789  (simplify
790   (mod integer_zerop@0 @1)
791   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
792   (if (!integer_zerop (@1))
793    @0))
794  /* X % 1 is always zero.  */
795  (simplify
796   (mod @0 integer_onep)
797   { build_zero_cst (type); })
798  /* X % -1 is zero.  */
799  (simplify
800   (mod @0 integer_minus_onep@1)
801   (if (!TYPE_UNSIGNED (type))
802    { build_zero_cst (type); }))
803  /* X % X is zero.  */
804  (simplify
805   (mod @0 @0)
806   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
807   (if (!integer_zerop (@0))
808    { build_zero_cst (type); }))
809  /* (X % Y) % Y is just X % Y.  */
810  (simplify
811   (mod (mod@2 @0 @1) @1)
812   @2)
813  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
814  (simplify
815   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
816   (if (ANY_INTEGRAL_TYPE_P (type)
817        && TYPE_OVERFLOW_UNDEFINED (type)
818        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
819                              TYPE_SIGN (type)))
820    { build_zero_cst (type); }))
821  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
822     modulo and comparison, since it is simpler and equivalent.  */
823  (for cmp (eq ne)
824   (simplify
825    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
826    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
827     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
828      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
830 /* X % -C is the same as X % C.  */
831 (simplify
832  (trunc_mod @0 INTEGER_CST@1)
833   (if (TYPE_SIGN (type) == SIGNED
834        && !TREE_OVERFLOW (@1)
835        && wi::neg_p (wi::to_wide (@1))
836        && !TYPE_OVERFLOW_TRAPS (type)
837        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
838        && !sign_bit_p (@1, @1))
839    (trunc_mod @0 (negate @1))))
841 /* X % -Y is the same as X % Y.  */
842 (simplify
843  (trunc_mod @0 (convert? (negate @1)))
844  (if (INTEGRAL_TYPE_P (type)
845       && !TYPE_UNSIGNED (type)
846       && !TYPE_OVERFLOW_TRAPS (type)
847       && tree_nop_conversion_p (type, TREE_TYPE (@1))
848       /* Avoid this transformation if X might be INT_MIN or
849          Y might be -1, because we would then change valid
850          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
851       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
852           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
853                                                         (TREE_TYPE (@1))))))
854   (trunc_mod @0 (convert @1))))
856 /* X - (X / Y) * Y is the same as X % Y.  */
857 (simplify
858  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
859  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
860   (convert (trunc_mod @0 @1))))
862 /* x * (1 + y / x) - y -> x - y % x */
863 (simplify
864  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
865  (if (INTEGRAL_TYPE_P (type))
866   (minus @0 (trunc_mod @1 @0))))
868 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
869    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
870    Also optimize A % (C << N)  where C is a power of 2,
871    to A & ((C << N) - 1).
872    Also optimize "A shift (B % C)", if C is a power of 2, to
873    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
874    and assume (B % C) is nonnegative as shifts negative values would
875    be UB.  */
876 (match (power_of_two_cand @1)
877  INTEGER_CST@1)
878 (match (power_of_two_cand @1)
879  (lshift INTEGER_CST@1 @2))
880 (for mod (trunc_mod floor_mod)
881  (for shift (lshift rshift)
882   (simplify
883    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
884    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
885     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
886                                                       1); }))))))
887  (simplify
888   (mod @0 (convert? (power_of_two_cand@1 @2)))
889   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
890        /* Allow any integral conversions of the divisor, except
891           conversion from narrower signed to wider unsigned type
892           where if @1 would be negative power of two, the divisor
893           would not be a power of two.  */
894        && INTEGRAL_TYPE_P (type)
895        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
896        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
897            || TYPE_UNSIGNED (TREE_TYPE (@1))
898            || !TYPE_UNSIGNED (type))
899        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
900    (with { tree utype = TREE_TYPE (@1);
901            if (!TYPE_OVERFLOW_WRAPS (utype))
902              utype = unsigned_type_for (utype); }
903     (bit_and @0 (convert (minus (convert:utype @1)
904                                 { build_one_cst (utype); })))))))
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
907 (simplify
908  (trunc_div (mult @0 integer_pow2p@1) @1)
909  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
910   (bit_and @0 { wide_int_to_tree
911                 (type, wi::mask (TYPE_PRECISION (type)
912                                  - wi::exact_log2 (wi::to_wide (@1)),
913                                  false, TYPE_PRECISION (type))); })))
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
916 (simplify
917  (mult (trunc_div @0 integer_pow2p@1) @1)
918  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
919   (bit_and @0 (negate @1))))
921 /* Simplify (t * 2) / 2) -> t.  */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
923  (simplify
924   (div (mult:c @0 @1) @1)
925   (if (ANY_INTEGRAL_TYPE_P (type))
926    (if (TYPE_OVERFLOW_UNDEFINED (type))
927     @0
928 #if GIMPLE
929     (with
930      {
931        bool overflowed = true;
932        value_range vr0, vr1;
933        if (INTEGRAL_TYPE_P (type)
934            && get_global_range_query ()->range_of_expr (vr0, @0)
935            && get_global_range_query ()->range_of_expr (vr1, @1)
936            && !vr0.varying_p () && !vr0.undefined_p ()
937            && !vr1.varying_p () && !vr1.undefined_p ())
938          {
939            wide_int wmin0 = vr0.lower_bound ();
940            wide_int wmax0 = vr0.upper_bound ();
941            wide_int wmin1 = vr1.lower_bound ();
942            wide_int wmax1 = vr1.upper_bound ();
943            /* If the multiplication can't overflow/wrap around, then
944               it can be optimized too.  */
945            wi::overflow_type min_ovf, max_ovf;
946            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
947            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
948            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
949              {
950                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
951                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
952                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
953                  overflowed = false;
954              }
955          }
956      }
957     (if (!overflowed)
958      @0))
959 #endif
960    ))))
962 #if GIMPLE
963 (for div (trunc_div exact_div)
964  /* Simplify (X + M*N) / N -> X / N + M.  */
965  (simplify
966   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
967   (with {value_range vr0, vr1, vr2, vr3, vr4;}
968   (if (INTEGRAL_TYPE_P (type)
969        && get_range_query (cfun)->range_of_expr (vr1, @1)
970        && get_range_query (cfun)->range_of_expr (vr2, @2)
971        /* "N*M" doesn't overflow.  */
972        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
973        && get_range_query (cfun)->range_of_expr (vr0, @0)
974        && get_range_query (cfun)->range_of_expr (vr3, @3)
975        /* "X+(N*M)" doesn't overflow.  */
976        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
977        && get_range_query (cfun)->range_of_expr (vr4, @4)
978        /* "X+N*M" is not with opposite sign as "X".  */
979        && (TYPE_UNSIGNED (type)
980            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
981            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
982   (plus (div @0 @2) @1))))
984  /* Simplify (X - M*N) / N -> X / N - M.  */
985  (simplify
986   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
987   (with {value_range vr0, vr1, vr2, vr3, vr4;}
988   (if (INTEGRAL_TYPE_P (type)
989        && get_range_query (cfun)->range_of_expr (vr1, @1)
990        && get_range_query (cfun)->range_of_expr (vr2, @2)
991        /* "N * M" doesn't overflow.  */
992        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
993        && get_range_query (cfun)->range_of_expr (vr0, @0)
994        && get_range_query (cfun)->range_of_expr (vr3, @3)
995        /* "X - (N*M)" doesn't overflow.  */
996        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
997        && get_range_query (cfun)->range_of_expr (vr4, @4)
998        /* "X-N*M" is not with opposite sign as "X".  */
999        && (TYPE_UNSIGNED (type)
1000            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1001            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1002   (minus (div @0 @2) @1)))))
1004 /* Simplify
1005    (X + C) / N -> X / N + C / N where C is multiple of N.
1006    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
1007 (for op (trunc_div exact_div rshift)
1008  (simplify
1009   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1010    (with
1011     {
1012       wide_int c = wi::to_wide (@1);
1013       wide_int n = wi::to_wide (@2);
1014       bool shift = op == RSHIFT_EXPR;
1015 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1016                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1017 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1018                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1019       value_range vr0, vr1, vr3;
1020     }
1021     (if (INTEGRAL_TYPE_P (type)
1022          && get_range_query (cfun)->range_of_expr (vr0, @0))
1023      (if (exact_mod (c)
1024           && get_range_query (cfun)->range_of_expr (vr1, @1)
1025           /* "X+C" doesn't overflow.  */
1026           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1027           && get_range_query (cfun)->range_of_expr (vr3, @3)
1028           /* "X+C" and "X" are not of opposite sign.  */
1029           && (TYPE_UNSIGNED (type)
1030               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1031               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1032        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1033        (if (TYPE_UNSIGNED (type) && c.sign_mask () < 0
1034             && exact_mod (-c)
1035             /* unsigned "X-(-C)" doesn't underflow.  */
1036             && wi::geu_p (vr0.lower_bound (), -c))
1037          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1038 #undef plus_op1
1039 #undef exact_mod
1040 #endif
1042 (for op (negate abs)
1043  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1044  (for coss (COS COSH)
1045   (simplify
1046    (coss (op @0))
1047     (coss @0)))
1048  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1049  (for pows (POW)
1050   (simplify
1051    (pows (op @0) REAL_CST@1)
1052    (with { HOST_WIDE_INT n; }
1053     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1054      (pows @0 @1)))))
1055  /* Likewise for powi.  */
1056  (for pows (POWI)
1057   (simplify
1058    (pows (op @0) INTEGER_CST@1)
1059    (if ((wi::to_wide (@1) & 1) == 0)
1060     (pows @0 @1))))
1061  /* Strip negate and abs from both operands of hypot.  */
1062  (for hypots (HYPOT)
1063   (simplify
1064    (hypots (op @0) @1)
1065    (hypots @0 @1))
1066   (simplify
1067    (hypots @0 (op @1))
1068    (hypots @0 @1)))
1069  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1070  (for copysigns (COPYSIGN_ALL)
1071   (simplify
1072    (copysigns (op @0) @1)
1073    (copysigns @0 @1))))
1075 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1076 (simplify
1077  (mult (abs@1 @0) @1)
1078  (mult @0 @0))
1080 /* Convert absu(x)*absu(x) -> x*x.  */
1081 (simplify
1082  (mult (absu@1 @0) @1)
1083  (mult (convert@2 @0) @2))
1085 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1086 (for coss (COS COSH)
1087      copysigns (COPYSIGN)
1088  (simplify
1089   (coss (copysigns @0 @1))
1090    (coss @0)))
1092 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1093 (for pows (POW)
1094      copysigns (COPYSIGN)
1095  (simplify
1096   (pows (copysigns @0 @2) REAL_CST@1)
1097   (with { HOST_WIDE_INT n; }
1098    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1099     (pows @0 @1)))))
1100 /* Likewise for powi.  */
1101 (for pows (POWI)
1102      copysigns (COPYSIGN)
1103  (simplify
1104   (pows (copysigns @0 @2) INTEGER_CST@1)
1105   (if ((wi::to_wide (@1) & 1) == 0)
1106    (pows @0 @1))))
1108 (for hypots (HYPOT)
1109      copysigns (COPYSIGN)
1110  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1111  (simplify
1112   (hypots (copysigns @0 @1) @2)
1113   (hypots @0 @2))
1114  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1115  (simplify
1116   (hypots @0 (copysigns @1 @2))
1117   (hypots @0 @1)))
1119 /* copysign(x, CST) -> [-]abs (x).  */
1120 (for copysigns (COPYSIGN_ALL)
1121  (simplify
1122   (copysigns @0 REAL_CST@1)
1123   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1124    (negate (abs @0))
1125    (abs @0))))
1127 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1128 (for copysigns (COPYSIGN_ALL)
1129  (simplify
1130   (copysigns (copysigns @0 @1) @2)
1131   (copysigns @0 @2)))
1133 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1134 (for copysigns (COPYSIGN_ALL)
1135  (simplify
1136   (mult (copysigns@2 @0 @1) @2)
1137   (mult @0 @0)))
1139 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1140 (for ccoss (CCOS CCOSH)
1141  (simplify
1142   (ccoss (negate @0))
1143    (ccoss @0)))
1145 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1146 (for ops (conj negate)
1147  (for cabss (CABS)
1148   (simplify
1149    (cabss (ops @0))
1150    (cabss @0))))
1152 /* Fold (a * (1 << b)) into (a << b)  */
1153 (simplify
1154  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1155   (if (! FLOAT_TYPE_P (type)
1156        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1157    (lshift @0 @2)))
1159 /* Shifts by precision or greater result in zero.  */
1160 (for shift (lshift rshift)
1161  (simplify
1162   (shift @0 uniform_integer_cst_p@1)
1163   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1164        /* Leave arithmetic right shifts of possibly negative values alone.  */
1165        && (TYPE_UNSIGNED (type)
1166            || shift == LSHIFT_EXPR
1167            || tree_expr_nonnegative_p (@0))
1168        /* Use a signed compare to leave negative shift counts alone.  */
1169        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1170                      element_precision (type)))
1171    { build_zero_cst (type); })))
1173 /* Shifts by constants distribute over several binary operations,
1174    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1175 (for op (plus minus)
1176   (simplify
1177     (op (lshift:s @0 @1) (lshift:s @2 @1))
1178     (if (INTEGRAL_TYPE_P (type)
1179          && TYPE_OVERFLOW_WRAPS (type)
1180          && !TYPE_SATURATING (type))
1181       (lshift (op @0 @2) @1))))
1183 (for op (bit_and bit_ior bit_xor)
1184   (simplify
1185     (op (lshift:s @0 @1) (lshift:s @2 @1))
1186     (if (INTEGRAL_TYPE_P (type))
1187       (lshift (op @0 @2) @1)))
1188   (simplify
1189     (op (rshift:s @0 @1) (rshift:s @2 @1))
1190     (if (INTEGRAL_TYPE_P (type))
1191       (rshift (op @0 @2) @1))))
1193 /* Fold (1 << (C - x)) where C = precision(type) - 1
1194    into ((1 << C) >> x). */
1195 (simplify
1196  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1197   (if (INTEGRAL_TYPE_P (type)
1198        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1199        && single_use (@1))
1200    (if (TYPE_UNSIGNED (type))
1201      (rshift (lshift @0 @2) @3)
1202    (with
1203     { tree utype = unsigned_type_for (type); }
1204     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1206 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1207 (simplify
1208  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1209  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1210       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1211   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1212    (bit_and (convert @0)
1213             { wide_int_to_tree (type,
1214                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1216 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1217 (for cst (INTEGER_CST VECTOR_CST)
1218  (simplify
1219   (rshift (negate:s @0) cst@1)
1220    (if (!TYPE_UNSIGNED (type)
1221         && TYPE_OVERFLOW_UNDEFINED (type))
1222     (with { tree stype = TREE_TYPE (@1);
1223             tree bt = truth_type_for (type);
1224             tree zeros = build_zero_cst (type);
1225             tree cst = NULL_TREE; }
1226      (switch
1227       /* Handle scalar case.  */
1228       (if (INTEGRAL_TYPE_P (type)
1229            /* If we apply the rule to the scalar type before vectorization
1230               we will enforce the result of the comparison being a bool
1231               which will require an extra AND on the result that will be
1232               indistinguishable from when the user did actually want 0
1233               or 1 as the result so it can't be removed.  */
1234            && canonicalize_math_after_vectorization_p ()
1235            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1236        (negate (convert (gt @0 { zeros; }))))
1237       /* Handle vector case.  */
1238       (if (VECTOR_INTEGER_TYPE_P (type)
1239            /* First check whether the target has the same mode for vector
1240               comparison results as it's operands do.  */
1241            && TYPE_MODE (bt) == TYPE_MODE (type)
1242            /* Then check to see if the target is able to expand the comparison
1243               with the given type later on, otherwise we may ICE.  */
1244            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1245            && (cst = uniform_integer_cst_p (@1)) != NULL
1246            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1247        (view_convert (gt:bt @0 { zeros; }))))))))
1249 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1250 (simplify
1251  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1252   (if (flag_associative_math
1253        && single_use (@3))
1254    (with
1255     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1256     (if (tem)
1257      (rdiv { tem; } @1)))))
1259 /* Simplify ~X & X as zero.  */
1260 (simplify
1261  (bit_and (convert? @0) (convert? @1))
1262  (with { bool wascmp; }
1263   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1264        && bitwise_inverted_equal_p (@0, @1, wascmp))
1265    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1267 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1268 (simplify
1269   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1270   (if (TYPE_UNSIGNED (type))
1271     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1273 (for bitop (bit_and bit_ior)
1274      cmp (eq ne)
1275  /* PR35691: Transform
1276     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1277     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1278  (simplify
1279   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1280    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1281         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1282         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1283     (cmp (bit_ior @0 (convert @1)) @2)))
1284  /* Transform:
1285     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1286     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1287  (simplify
1288   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1289    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1290         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1291         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1292     (cmp (bit_and @0 (convert @1)) @2))))
1294 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1295 (simplify
1296  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1297   (minus (bit_xor @0 @1) @1))
1298 (simplify
1299  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1300  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1301   (minus (bit_xor @0 @1) @1)))
1303 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1304 (simplify
1305  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1306   (minus @1 (bit_xor @0 @1)))
1308 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1309 (for op (bit_ior bit_xor plus)
1310  (simplify
1311   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1312   (with { bool wascmp0, wascmp1; }
1313    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1314         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1315         && ((!wascmp0 && !wascmp1)
1316             || element_precision (type) == 1))
1317    (bit_xor @0 @1)))))
1319 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1320 (simplify
1321   (bit_ior:c (bit_xor:c @0 @1) @0)
1322   (bit_ior @0 @1))
1324 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1325 (simplify
1326  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1327  @2)
1329 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1330 (simplify
1331  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1332  (bit_not (bit_and @0 @1)))
1334 /* (~a & b) ^ a  -->   (a | b)   */
1335 (simplify
1336  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1337  (bit_ior @0 @1))
1339 /* (a | b) & ~(a ^ b)  -->  a & b  */
1340 (simplify
1341  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1342  (bit_and @0 @1))
1344 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1345 (simplify
1346  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1347  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1348       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1349   (bit_and @0 @1)))
1351 /* a | ~(a ^ b)  -->  a | ~b  */
1352 (simplify
1353  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1354  (bit_ior @0 (bit_not @1)))
1356 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1357 (simplify
1358  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1359  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1360       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1361   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1363 /* (a | b) | (a &^ b)  -->  a | b  */
1364 (for op (bit_and bit_xor)
1365  (simplify
1366   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1367   @2))
1369 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1370 (simplify
1371  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1372  @2)
1374 /* (a & b) | (a == b)  -->  a == b  */
1375 (simplify
1376  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1377  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1378       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1379   @2))
1381 /* ~(~a & b)  -->  a | ~b  */
1382 (simplify
1383  (bit_not (bit_and:cs (bit_not @0) @1))
1384  (bit_ior @0 (bit_not @1)))
1386 /* ~(~a | b) --> a & ~b */
1387 (simplify
1388  (bit_not (bit_ior:cs (bit_not @0) @1))
1389  (bit_and @0 (bit_not @1)))
1391 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1392 (simplify
1393  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1394  (bit_and @3 (bit_not @2)))
1396 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1397 (simplify
1398  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1399  (bit_ior @3 @2))
1401 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1402 (simplify
1403  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1404   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1406 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1407 (simplify
1408  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1409   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1411 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1412 (simplify
1413  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1414  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1415       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1416   (bit_xor @0 @1)))
1418 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1419    ((A & N) + B) & M -> (A + B) & M
1420    Similarly if (N & M) == 0,
1421    ((A | N) + B) & M -> (A + B) & M
1422    and for - instead of + (or unary - instead of +)
1423    and/or ^ instead of |.
1424    If B is constant and (B & M) == 0, fold into A & M.  */
1425 (for op (plus minus)
1426  (for bitop (bit_and bit_ior bit_xor)
1427   (simplify
1428    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1429     (with
1430      { tree pmop[2];
1431        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1432                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1433                                        NULL_TREE, pmop); }
1434      (if (utype)
1435       (convert (bit_and (op (convert:utype { pmop[0]; })
1436                             (convert:utype { pmop[1]; }))
1437                         (convert:utype @2))))))
1438   (simplify
1439    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1440     (with
1441      { tree pmop[2];
1442        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1443                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1444                                        @4, pmop); }
1445      (if (utype)
1446       (convert (bit_and (op (convert:utype { pmop[0]; })
1447                             (convert:utype { pmop[1]; }))
1448                         (convert:utype @2)))))))
1449  (simplify
1450   (bit_and (op:s @0 @1) INTEGER_CST@2)
1451    (with
1452     { tree pmop[2];
1453       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1454                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1455                                       NULL_TREE, NULL_TREE, pmop); }
1456     (if (utype)
1457      (convert (bit_and (op (convert:utype { pmop[0]; })
1458                            (convert:utype { pmop[1]; }))
1459                        (convert:utype @2)))))))
1460 (for bitop (bit_and bit_ior bit_xor)
1461  (simplify
1462   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1463    (with
1464     { tree pmop[2];
1465       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1466                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1467                                       NULL_TREE, NULL_TREE, pmop); }
1468     (if (utype)
1469      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1470                        (convert:utype @1)))))))
1472 /* X % Y is smaller than Y.  */
1473 (for cmp (lt ge)
1474  (simplify
1475   (cmp (trunc_mod @0 @1) @1)
1476   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1477    { constant_boolean_node (cmp == LT_EXPR, type); })))
1478 (for cmp (gt le)
1479  (simplify
1480   (cmp @1 (trunc_mod @0 @1))
1481   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1482    { constant_boolean_node (cmp == GT_EXPR, type); })))
1484 /* x | ~0 -> ~0  */
1485 (simplify
1486  (bit_ior @0 integer_all_onesp@1)
1487  @1)
1489 /* x | 0 -> x  */
1490 (simplify
1491  (bit_ior @0 integer_zerop)
1492  @0)
1494 /* x & 0 -> 0  */
1495 (simplify
1496  (bit_and @0 integer_zerop@1)
1497  @1)
1499 /* ~x | x -> -1 */
1500 /* ~x ^ x -> -1 */
1501 (for op (bit_ior bit_xor)
1502  (simplify
1503   (op (convert? @0) (convert? @1))
1504   (with { bool wascmp; }
1505    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1506         && bitwise_inverted_equal_p (@0, @1, wascmp))
1507     (convert
1508      { wascmp
1509         ? constant_boolean_node (true, type)
1510         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1512 /* x ^ x -> 0 */
1513 (simplify
1514   (bit_xor @0 @0)
1515   { build_zero_cst (type); })
1517 /* Canonicalize X ^ ~0 to ~X.  */
1518 (simplify
1519   (bit_xor @0 integer_all_onesp@1)
1520   (bit_not @0))
1522 /* x & ~0 -> x  */
1523 (simplify
1524  (bit_and @0 integer_all_onesp)
1525   (non_lvalue @0))
1527 /* x & x -> x,  x | x -> x  */
1528 (for bitop (bit_and bit_ior)
1529  (simplify
1530   (bitop @0 @0)
1531   (non_lvalue @0)))
1533 /* x & C -> x if we know that x & ~C == 0.  */
1534 #if GIMPLE
1535 (simplify
1536  (bit_and SSA_NAME@0 INTEGER_CST@1)
1537  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1538       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1539   @0))
1540 /* x | C -> C if we know that x & ~C == 0.  */
1541 (simplify
1542  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1543  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1544       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1545   @1))
1546 #endif
1548 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1549 (simplify
1550  (bit_not (minus (bit_not @0) @1))
1551  (plus @0 @1))
1552 (simplify
1553  (bit_not (plus:c (bit_not @0) @1))
1554  (minus @0 @1))
1555 /* (~X - ~Y) -> Y - X.  */
1556 (simplify
1557  (minus (bit_not @0) (bit_not @1))
1558   (if (!TYPE_OVERFLOW_SANITIZED (type))
1559    (with { tree utype = unsigned_type_for (type); }
1560     (convert (minus (convert:utype @1) (convert:utype @0))))))
1562 /* ~(X - Y) -> ~X + Y.  */
1563 (simplify
1564  (bit_not (minus:s @0 @1))
1565  (plus (bit_not @0) @1))
1566 (simplify
1567  (bit_not (plus:s @0 INTEGER_CST@1))
1568  (if ((INTEGRAL_TYPE_P (type)
1569        && TYPE_UNSIGNED (type))
1570       || (!TYPE_OVERFLOW_SANITIZED (type)
1571           && may_negate_without_overflow_p (@1)))
1572   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1574 #if GIMPLE
1575 /* ~X + Y -> (Y - X) - 1.  */
1576 (simplify
1577  (plus:c (bit_not @0) @1)
1578   (if (ANY_INTEGRAL_TYPE_P (type)
1579        && TYPE_OVERFLOW_WRAPS (type)
1580        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1581        && !integer_all_onesp (@1))
1582    (plus (minus @1 @0) { build_minus_one_cst (type); })
1583    (if (INTEGRAL_TYPE_P (type)
1584         && TREE_CODE (@1) == INTEGER_CST
1585         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1586                                               SIGNED))
1587     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1588 #endif
1590 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1591 (simplify
1592  (bit_not (rshift:s @0 @1))
1593   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1594    (rshift (bit_not! @0) @1)
1595    /* For logical right shifts, this is possible only if @0 doesn't
1596       have MSB set and the logical right shift is changed into
1597       arithmetic shift.  */
1598    (if (INTEGRAL_TYPE_P (type)
1599         && !wi::neg_p (tree_nonzero_bits (@0)))
1600     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1601      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1603 /* x + (x & 1) -> (x + 1) & ~1 */
1604 (simplify
1605  (plus:c @0 (bit_and:s @0 integer_onep@1))
1606  (bit_and (plus @0 @1) (bit_not @1)))
1608 /* x & ~(x & y) -> x & ~y */
1609 /* x | ~(x | y) -> x | ~y  */
1610 (for bitop (bit_and bit_ior)
1611  (simplify
1612   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1613   (bitop @0 (bit_not @1))))
1615 /* (~x & y) | ~(x | y) -> ~x */
1616 (simplify
1617  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1618  @2)
1620 /* (x | y) ^ (x | ~y) -> ~x */
1621 (simplify
1622  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1623  (bit_not @0))
1625 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1626 (simplify
1627  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1628  (bit_not (bit_xor @0 @1)))
1630 /* (~x | y) ^ (x ^ y) -> x | ~y */
1631 (simplify
1632  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1633  (bit_ior @0 (bit_not @1)))
1635 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1636 (simplify
1637  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1638  (bit_not (bit_and @0 @1)))
1640 /* (x & y) ^ (x | y) -> x ^ y */
1641 (simplify
1642  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1643  (bit_xor @0 @1))
1645 /* (x ^ y) ^ (x | y) -> x & y */
1646 (simplify
1647  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1648  (bit_and @0 @1))
1650 /* (x & y) + (x ^ y) -> x | y */
1651 /* (x & y) | (x ^ y) -> x | y */
1652 /* (x & y) ^ (x ^ y) -> x | y */
1653 (for op (plus bit_ior bit_xor)
1654  (simplify
1655   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1656   (bit_ior @0 @1)))
1658 /* (x & y) + (x | y) -> x + y */
1659 (simplify
1660  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1661  (plus @0 @1))
1663 /* (x + y) - (x | y) -> x & y */
1664 (simplify
1665  (minus (plus @0 @1) (bit_ior @0 @1))
1666  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1667       && !TYPE_SATURATING (type))
1668   (bit_and @0 @1)))
1670 /* (x + y) - (x & y) -> x | y */
1671 (simplify
1672  (minus (plus @0 @1) (bit_and @0 @1))
1673  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1674       && !TYPE_SATURATING (type))
1675   (bit_ior @0 @1)))
1677 /* (x | y) - y -> (x & ~y) */
1678 (simplify
1679  (minus (bit_ior:cs @0 @1) @1)
1680  (bit_and @0 (bit_not @1)))
1682 /* (x | y) - (x ^ y) -> x & y */
1683 (simplify
1684  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1685  (bit_and @0 @1))
1687 /* (x | y) - (x & y) -> x ^ y */
1688 (simplify
1689  (minus (bit_ior @0 @1) (bit_and @0 @1))
1690  (bit_xor @0 @1))
1692 /* (x | y) & ~(x & y) -> x ^ y */
1693 (simplify
1694  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1695  (bit_xor @0 @1))
1697 /* (x | y) & (~x ^ y) -> x & y */
1698 (simplify
1699  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1700  (with { bool wascmp; }
1701   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1702        && (!wascmp || element_precision (type) == 1))
1703    (bit_and @0 @1))))
1705 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1706 (simplify
1707  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1708  (bit_not (bit_xor @0 @1)))
1710 /* (~x | y) ^ (x | ~y) -> x ^ y */
1711 (simplify
1712  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1713  (bit_xor @0 @1))
1715 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1716 (simplify
1717  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1718                               (nop_convert2? (bit_ior @0 @1))))
1719        integer_all_onesp)
1720  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1721       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1722       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1723       && !TYPE_SATURATING (TREE_TYPE (@2)))
1724  (bit_not (convert (bit_xor @0 @1)))))
1725 (simplify
1726  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1727                                integer_all_onesp))
1728        (nop_convert3? (bit_ior @0 @1)))
1729  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1730       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1731       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1732       && !TYPE_SATURATING (TREE_TYPE (@2)))
1733  (bit_not (convert (bit_xor @0 @1)))))
1734 (simplify
1735  (minus (nop_convert1? (bit_and @0 @1))
1736        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1737                                integer_onep)))
1738  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1739       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1740       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1741       && !TYPE_SATURATING (TREE_TYPE (@2)))
1742  (bit_not (convert (bit_xor @0 @1)))))
1744 /* ~x & ~y -> ~(x | y)
1745    ~x | ~y -> ~(x & y) */
1746 (for op (bit_and bit_ior)
1747      rop (bit_ior bit_and)
1748  (simplify
1749   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1750   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1751        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1752    (bit_not (rop (convert @0) (convert @1))))))
1754 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1755    with a constant, and the two constants have no bits in common,
1756    we should treat this as a BIT_IOR_EXPR since this may produce more
1757    simplifications.  */
1758 (for op (bit_xor plus)
1759  (simplify
1760   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1761       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1762   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1763        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1764        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1765    (bit_ior (convert @4) (convert @5)))))
1767 /* (X | Y) ^ X -> Y & ~ X*/
1768 (simplify
1769  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1770  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1771   (convert (bit_and @1 (bit_not @0)))))
1773 /* (~X | Y) ^ X -> ~(X & Y).  */
1774 (simplify
1775  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1776  (if (bitwise_equal_p (@0, @2))
1777   (convert (bit_not (bit_and @0 (convert @1))))))
1779 /* Convert ~X ^ ~Y to X ^ Y.  */
1780 (simplify
1781  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1782  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1783       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1784   (bit_xor (convert @0) (convert @1))))
1786 /* Convert ~X ^ C to X ^ ~C.  */
1787 (simplify
1788  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1789  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1790   (bit_xor (convert @0) (bit_not @1))))
1792 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1793 (for opo (bit_and bit_xor)
1794      opi (bit_xor bit_and)
1795  (simplify
1796   (opo:c (opi:cs @0 @1) @1)
1797   (bit_and (bit_not @0) @1)))
1799 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1800    operands are another bit-wise operation with a common input.  If so,
1801    distribute the bit operations to save an operation and possibly two if
1802    constants are involved.  For example, convert
1803      (A | B) & (A | C) into A | (B & C)
1804    Further simplification will occur if B and C are constants.  */
1805 (for op (bit_and bit_ior bit_xor)
1806      rop (bit_ior bit_and bit_and)
1807  (simplify
1808   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1809   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1810        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1811    (rop (convert @0) (op (convert @1) (convert @2))))))
1813 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1814 /* (X & Y) & Y -> X & Y
1815    (X | Y) | Y -> X | Y  */
1816 (for op (bit_and bit_ior)
1817  (simplify
1818   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1819   @2))
1820 /* (X ^ Y) ^ Y -> X  */
1821 (simplify
1822  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1823  (convert @0))
1824 /* (X & Y) & (X & Z) -> (X & Y) & Z
1825    (X | Y) | (X | Z) -> (X | Y) | Z  */
1826 (for op (bit_and bit_ior)
1827  (simplify
1828   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1829   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1830        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1831    (if (single_use (@5) && single_use (@6))
1832     (op @3 (convert @2))
1833     (if (single_use (@3) && single_use (@4))
1834      (op (convert @1) @5))))))
1835 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1836 (simplify
1837  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1838  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1839       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1840   (bit_xor (convert @1) (convert @2))))
1842 /* Convert abs (abs (X)) into abs (X).
1843    also absu (absu (X)) into absu (X).  */
1844 (simplify
1845  (abs (abs@1 @0))
1846  @1)
1848 (simplify
1849  (absu (convert@2 (absu@1 @0)))
1850  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1851   @1))
1853 /* Convert abs[u] (-X) -> abs[u] (X).  */
1854 (simplify
1855  (abs (negate @0))
1856  (abs @0))
1858 (simplify
1859  (absu (negate @0))
1860  (absu @0))
1862 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1863 (simplify
1864  (abs tree_expr_nonnegative_p@0)
1865  @0)
1867 (simplify
1868  (absu tree_expr_nonnegative_p@0)
1869  (convert @0))
1871 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1872 (simplify
1873  (mult:c (nop_convert1?
1874           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1875                     integer_onep))
1876          (nop_convert3? @0))
1877  (if (INTEGRAL_TYPE_P (type)
1878       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1879       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1880   (if (TYPE_UNSIGNED (type))
1881    (absu @0)
1882    (abs @0)
1883   )
1887 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1888 (match negate_expr_p
1889  INTEGER_CST
1890  (if ((INTEGRAL_TYPE_P (type)
1891        && TYPE_UNSIGNED (type))
1892       || (!TYPE_OVERFLOW_SANITIZED (type)
1893           && may_negate_without_overflow_p (t)))))
1894 (match negate_expr_p
1895  FIXED_CST)
1896 (match negate_expr_p
1897  (negate @0)
1898  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1899 (match negate_expr_p
1900  REAL_CST
1901  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1902 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1903    ways.  */
1904 (match negate_expr_p
1905  VECTOR_CST
1906  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1907 (match negate_expr_p
1908  (minus @0 @1)
1909  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1910       || (FLOAT_TYPE_P (type)
1911           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1912           && !HONOR_SIGNED_ZEROS (type)))))
1914 /* (-A) * (-B) -> A * B  */
1915 (simplify
1916  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1917   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1918        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1919    (mult (convert @0) (convert (negate @1)))))
1921 /* -(A + B) -> (-B) - A.  */
1922 (simplify
1923  (negate (plus:c @0 negate_expr_p@1))
1924  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1925       && !HONOR_SIGNED_ZEROS (type))
1926   (minus (negate @1) @0)))
1928 /* -(A - B) -> B - A.  */
1929 (simplify
1930  (negate (minus @0 @1))
1931  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1932       || (FLOAT_TYPE_P (type)
1933           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1934           && !HONOR_SIGNED_ZEROS (type)))
1935   (minus @1 @0)))
1936 (simplify
1937  (negate (pointer_diff @0 @1))
1938  (if (TYPE_OVERFLOW_UNDEFINED (type))
1939   (pointer_diff @1 @0)))
1941 /* A - B -> A + (-B) if B is easily negatable.  */
1942 (simplify
1943  (minus @0 negate_expr_p@1)
1944  (if (!FIXED_POINT_TYPE_P (type))
1945  (plus @0 (negate @1))))
1947 /* 1 - a is a ^ 1 if a had a bool range. */
1948 /* This is only enabled for gimple as sometimes
1949    cfun is not set for the function which contains
1950    the SSA_NAME (e.g. while IPA passes are happening,
1951    fold might be called).  */
1952 (simplify
1953  (minus integer_onep@0 SSA_NAME@1)
1954   (if (INTEGRAL_TYPE_P (type)
1955        && ssa_name_has_boolean_range (@1))
1956    (bit_xor @1 @0)))
1958 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1959 (simplify
1960  (negate (mult:c@0 @1 negate_expr_p@2))
1961  (if (! TYPE_UNSIGNED (type)
1962       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1963       && single_use (@0))
1964   (mult @1 (negate @2))))
1966 (simplify
1967  (negate (rdiv@0 @1 negate_expr_p@2))
1968  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1969       && single_use (@0))
1970   (rdiv @1 (negate @2))))
1972 (simplify
1973  (negate (rdiv@0 negate_expr_p@1 @2))
1974  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1975       && single_use (@0))
1976   (rdiv (negate @1) @2)))
1978 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1979 (simplify
1980  (negate (convert? (rshift @0 INTEGER_CST@1)))
1981  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1982       && wi::to_wide (@1) == element_precision (type) - 1)
1983   (with { tree stype = TREE_TYPE (@0);
1984           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1985                                              : unsigned_type_for (stype); }
1986    (if (VECTOR_TYPE_P (type))
1987     (view_convert (rshift (view_convert:ntype @0) @1))
1988     (convert (rshift (convert:ntype @0) @1))))))
1990 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1991    when profitable.
1992    For bitwise binary operations apply operand conversions to the
1993    binary operation result instead of to the operands.  This allows
1994    to combine successive conversions and bitwise binary operations.
1995    We combine the above two cases by using a conditional convert.  */
1996 (for bitop (bit_and bit_ior bit_xor)
1997  (simplify
1998   (bitop (convert@2 @0) (convert?@3 @1))
1999   (if (((TREE_CODE (@1) == INTEGER_CST
2000          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2001          && (int_fits_type_p (@1, TREE_TYPE (@0))
2002              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2003         || types_match (@0, @1))
2004        && !POINTER_TYPE_P (TREE_TYPE (@0))
2005        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2006        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2007        /* ???  This transform conflicts with fold-const.cc doing
2008           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2009           constants (if x has signed type, the sign bit cannot be set
2010           in c).  This folds extension into the BIT_AND_EXPR.
2011           Restrict it to GIMPLE to avoid endless recursions.  */
2012        && (bitop != BIT_AND_EXPR || GIMPLE)
2013        && (/* That's a good idea if the conversion widens the operand, thus
2014               after hoisting the conversion the operation will be narrower.
2015               It is also a good if the conversion is a nop as moves the
2016               conversion to one side; allowing for combining of the conversions.  */
2017            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2018            /* The conversion check for being a nop can only be done at the gimple
2019               level as fold_binary has some re-association code which can conflict
2020               with this if there is a "constant" which is not a full INTEGER_CST.  */
2021            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2022            /* It's also a good idea if the conversion is to a non-integer
2023               mode.  */
2024            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2025            /* Or if the precision of TO is not the same as the precision
2026               of its mode.  */
2027            || !type_has_mode_precision_p (type)
2028            /* In GIMPLE, getting rid of 2 conversions for one new results
2029               in smaller IL.  */
2030            || (GIMPLE
2031                && TREE_CODE (@1) != INTEGER_CST
2032                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2033                && single_use (@2)
2034                && single_use (@3))))
2035    (convert (bitop @0 (convert @1)))))
2036  /* In GIMPLE, getting rid of 2 conversions for one new results
2037     in smaller IL.  */
2038  (simplify
2039   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2040   (if (GIMPLE
2041        && TREE_CODE (@1) != INTEGER_CST
2042        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2043        && types_match (type, @0)
2044        && !POINTER_TYPE_P (TREE_TYPE (@0))
2045        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2046    (bitop @0 (convert @1)))))
2048 (for bitop (bit_and bit_ior)
2049      rbitop (bit_ior bit_and)
2050   /* (x | y) & x -> x */
2051   /* (x & y) | x -> x */
2052  (simplify
2053   (bitop:c (rbitop:c @0 @1) @0)
2054   @0)
2055  /* (~x | y) & x -> x & y */
2056  /* (~x & y) | x -> x | y */
2057  (simplify
2058   (bitop:c (rbitop:c @2 @1) @0)
2059   (with { bool wascmp; }
2060    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2061         && (!wascmp || element_precision (type) == 1))
2062     (bitop @0 @1)))))
2064 /* ((x | y) & z) | x -> (z & y) | x
2065    ((x ^ y) & z) | x -> (z & y) | x  */
2066 (for op (bit_ior bit_xor)
2067  (simplify
2068   (bit_ior:c (nop_convert1?:s
2069                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2070   (if (bitwise_equal_p (@0, @3))
2071    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2073 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2074 (simplify
2075   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2076   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2078 /* Combine successive equal operations with constants.  */
2079 (for bitop (bit_and bit_ior bit_xor)
2080  (simplify
2081   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2082   (if (!CONSTANT_CLASS_P (@0))
2083    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2084       folded to a constant.  */
2085    (bitop @0 (bitop! @1 @2))
2086    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2087       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2088       the values involved are such that the operation can't be decided at
2089       compile time.  Try folding one of @0 or @1 with @2 to see whether
2090       that combination can be decided at compile time.
2092       Keep the existing form if both folds fail, to avoid endless
2093       oscillation.  */
2094    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2095     (if (cst1)
2096      (bitop @1 { cst1; })
2097      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2098       (if (cst2)
2099        (bitop @0 { cst2; }))))))))
2101 /* Try simple folding for X op !X, and X op X with the help
2102    of the truth_valued_p and logical_inverted_value predicates.  */
2103 (match truth_valued_p
2104  @0
2105  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2106 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2107  (match truth_valued_p
2108   (op @0 @1)))
2109 (match truth_valued_p
2110   (truth_not @0))
2112 (match (logical_inverted_value @0)
2113  (truth_not @0))
2114 (match (logical_inverted_value @0)
2115  (bit_not truth_valued_p@0))
2116 (match (logical_inverted_value @0)
2117  (eq @0 integer_zerop))
2118 (match (logical_inverted_value @0)
2119  (ne truth_valued_p@0 integer_truep))
2120 (match (logical_inverted_value @0)
2121  (bit_xor truth_valued_p@0 integer_truep))
2123 /* X & !X -> 0.  */
2124 (simplify
2125  (bit_and:c @0 (logical_inverted_value @0))
2126  { build_zero_cst (type); })
2127 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2128 (for op (bit_ior bit_xor)
2129  (simplify
2130   (op:c truth_valued_p@0 (logical_inverted_value @0))
2131   { constant_boolean_node (true, type); }))
2132 /* X ==/!= !X is false/true.  */
2133 (for op (eq ne)
2134  (simplify
2135   (op:c truth_valued_p@0 (logical_inverted_value @0))
2136   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2138 /* ~~x -> x */
2139 (simplify
2140   (bit_not (bit_not @0))
2141   @0)
2143 /* zero_one_valued_p will match when a value is known to be either
2144    0 or 1 including constants 0 or 1.
2145    Signed 1-bits includes -1 so they cannot match here. */
2146 (match zero_one_valued_p
2147  @0
2148  (if (INTEGRAL_TYPE_P (type)
2149       && (TYPE_UNSIGNED (type)
2150           || TYPE_PRECISION (type) > 1)
2151       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2152 (match zero_one_valued_p
2153  truth_valued_p@0
2154  (if (INTEGRAL_TYPE_P (type)
2155       && (TYPE_UNSIGNED (type)
2156           || TYPE_PRECISION (type) > 1))))
2158 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2159 (simplify
2160  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2161  (if (INTEGRAL_TYPE_P (type))
2162   (bit_and @0 @1)))
2164 (for cmp (tcc_comparison)
2165      icmp (inverted_tcc_comparison)
2166  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2167  (simplify
2168   (bit_ior
2169    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2170    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2171     (if (INTEGRAL_TYPE_P (type)
2172          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2173          /* The scalar version has to be canonicalized after vectorization
2174             because it makes unconditional loads conditional ones, which
2175             means we lose vectorization because the loads may trap.  */
2176          && canonicalize_math_after_vectorization_p ())
2177      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2179  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2180     canonicalized further and we recognize the conditional form:
2181     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2182  (simplify
2183   (bit_ior
2184    (cond (cmp@0  @01 @02) @3 zerop)
2185    (cond (icmp@4 @01 @02) @5 zerop))
2186     (if (INTEGRAL_TYPE_P (type)
2187          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2188          /* The scalar version has to be canonicalized after vectorization
2189             because it makes unconditional loads conditional ones, which
2190             means we lose vectorization because the loads may trap.  */
2191          && canonicalize_math_after_vectorization_p ())
2192     (cond @0 @3 @5)))
2194  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2195     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2196  (simplify
2197   (bit_ior
2198    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2199    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2200     (if (integer_zerop (@5)
2201          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2202      (switch
2203       (if (integer_onep (@4))
2204        (bit_and (vec_cond @0 @2 @3) @4))
2205         (if (integer_minus_onep (@4))
2206          (vec_cond @0 @2 @3)))
2207     (if (integer_zerop (@4)
2208          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2209      (switch
2210       (if (integer_onep (@5))
2211        (bit_and (vec_cond @0 @3 @2) @5))
2212       (if (integer_minus_onep (@5))
2213        (vec_cond @0 @3 @2))))))
2215  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2216     into a < b ? d : c.  */
2217  (simplify
2218   (bit_ior
2219    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2220    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2221   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2222    (vec_cond @0 @2 @3))))
2224 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2225 (simplify
2226  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2227  (if (INTEGRAL_TYPE_P (type)
2228       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2229       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2230       /* Sign extending of the neg or a truncation of the neg
2231          is needed. */
2232       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2233           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2234   (mult (convert @0) @1)))
2236 /* Narrow integer multiplication by a zero_one_valued_p operand.
2237    Multiplication by [0,1] is guaranteed not to overflow.  */
2238 (simplify
2239  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2240  (if (INTEGRAL_TYPE_P (type)
2241       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2242       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2243   (mult (convert @1) (convert @2))))
2245 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2246    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2247    as some targets (such as x86's SSE) may return zero for larger C.  */
2248 (simplify
2249   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2250   (if (tree_fits_shwi_p (@1)
2251        && tree_to_shwi (@1) > 0
2252        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2253     (convert @0)))
2255 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2256    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2257    as some targets (such as x86's SSE) may return zero for larger C.  */
2258 (simplify
2259   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2260   (if (tree_fits_shwi_p (@1)
2261        && tree_to_shwi (@1) > 0
2262        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2263     (eq @0 @2)))
2265 /* Convert ~ (-A) to A - 1.  */
2266 (simplify
2267  (bit_not (convert? (negate @0)))
2268  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2269       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2270   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2272 /* Convert - (~A) to A + 1.  */
2273 (simplify
2274  (negate (nop_convert? (bit_not @0)))
2275  (plus (view_convert @0) { build_each_one_cst (type); }))
2277 /* (a & b) ^ (a == b) -> !(a | b) */
2278 /* (a & b) == (a ^ b) -> !(a | b) */
2279 (for first_op (bit_xor eq)
2280      second_op (eq bit_xor)
2281  (simplify
2282   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2283     (bit_not (bit_ior @0 @1))))
2285 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2286 (simplify
2287  (bit_not (convert? (minus @0 integer_each_onep)))
2288  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2289       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2290   (convert (negate @0))))
2291 (simplify
2292  (bit_not (convert? (plus @0 integer_all_onesp)))
2293  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2294       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2295   (convert (negate @0))))
2297 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2298 (simplify
2299  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2300  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2301   (convert (bit_xor @0 (bit_not @1)))))
2302 (simplify
2303  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2304  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2305   (convert (bit_xor @0 @1))))
2307 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2308 (simplify
2309  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2310  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2311   (bit_not (bit_xor (view_convert @0) @1))))
2313 /* ~(a ^ b) is a == b for truth valued a and b.  */
2314 (simplify
2315  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2316  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2317       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2318   (convert (eq @0 @1))))
2320 /* (~a) == b is a ^ b for truth valued a and b.  */
2321 (simplify
2322  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2323  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2324       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2325   (convert (bit_xor @0 @1))))
2327 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2328 (simplify
2329  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2330  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2332 /* Fold A - (A & B) into ~B & A.  */
2333 (simplify
2334  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2335  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2336       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2337   (convert (bit_and (bit_not @1) @0))))
2339 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2340 (if (!canonicalize_math_p ())
2341  (for cmp (tcc_comparison)
2342   (simplify
2343    (mult:c (convert (cmp@0 @1 @2)) @3)
2344    (if (INTEGRAL_TYPE_P (type)
2345         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2346      (cond @0 @3 { build_zero_cst (type); })))
2347 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2348   (simplify
2349    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2350    (if (INTEGRAL_TYPE_P (type)
2351         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2352      (cond @0 @3 { build_zero_cst (type); })))
2356 /* For integral types with undefined overflow and C != 0 fold
2357    x * C EQ/NE y * C into x EQ/NE y.  */
2358 (for cmp (eq ne)
2359  (simplify
2360   (cmp (mult:c @0 @1) (mult:c @2 @1))
2361   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2362        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2363        && tree_expr_nonzero_p (@1))
2364    (cmp @0 @2))))
2366 /* For integral types with wrapping overflow and C odd fold
2367    x * C EQ/NE y * C into x EQ/NE y.  */
2368 (for cmp (eq ne)
2369  (simplify
2370   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2371   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2372        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2373        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2374    (cmp @0 @2))))
2376 /* For integral types with undefined overflow and C != 0 fold
2377    x * C RELOP y * C into:
2379    x RELOP y for nonnegative C
2380    y RELOP x for negative C  */
2381 (for cmp (lt gt le ge)
2382  (simplify
2383   (cmp (mult:c @0 @1) (mult:c @2 @1))
2384   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2385        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2386    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2387     (cmp @0 @2)
2388    (if (TREE_CODE (@1) == INTEGER_CST
2389         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2390     (cmp @2 @0))))))
2392 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2393 (for cmp (le gt)
2394      icmp (gt le)
2395  (simplify
2396   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2397    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2398         && TYPE_UNSIGNED (TREE_TYPE (@0))
2399         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2400         && (wi::to_wide (@2)
2401             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2402     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2403      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2405 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2406 (for cmp (simple_comparison)
2407  (simplify
2408   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2409   (if (element_precision (@3) >= element_precision (@0)
2410        && types_match (@0, @1))
2411    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2412     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2413      (cmp @1 @0)
2414      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2415       (with
2416        {
2417         tree utype = unsigned_type_for (TREE_TYPE (@0));
2418        }
2419        (cmp (convert:utype @1) (convert:utype @0)))))
2420     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2421      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2422       (cmp @0 @1)
2423       (with
2424        {
2425         tree utype = unsigned_type_for (TREE_TYPE (@0));
2426        }
2427        (cmp (convert:utype @0) (convert:utype @1)))))))))
2429 /* X / C1 op C2 into a simple range test.  */
2430 (for cmp (simple_comparison)
2431  (simplify
2432   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2433   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2434        && integer_nonzerop (@1)
2435        && !TREE_OVERFLOW (@1)
2436        && !TREE_OVERFLOW (@2))
2437    (with { tree lo, hi; bool neg_overflow;
2438            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2439                                                    &neg_overflow); }
2440     (switch
2441      (if (code == LT_EXPR || code == GE_EXPR)
2442        (if (TREE_OVERFLOW (lo))
2443         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2444         (if (code == LT_EXPR)
2445          (lt @0 { lo; })
2446          (ge @0 { lo; }))))
2447      (if (code == LE_EXPR || code == GT_EXPR)
2448        (if (TREE_OVERFLOW (hi))
2449         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2450         (if (code == LE_EXPR)
2451          (le @0 { hi; })
2452          (gt @0 { hi; }))))
2453      (if (!lo && !hi)
2454       { build_int_cst (type, code == NE_EXPR); })
2455      (if (code == EQ_EXPR && !hi)
2456       (ge @0 { lo; }))
2457      (if (code == EQ_EXPR && !lo)
2458       (le @0 { hi; }))
2459      (if (code == NE_EXPR && !hi)
2460       (lt @0 { lo; }))
2461      (if (code == NE_EXPR && !lo)
2462       (gt @0 { hi; }))
2463      (if (GENERIC)
2464       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2465                            lo, hi); })
2466      (with
2467       {
2468         tree etype = range_check_type (TREE_TYPE (@0));
2469         if (etype)
2470           {
2471             hi = fold_convert (etype, hi);
2472             lo = fold_convert (etype, lo);
2473             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2474           }
2475       }
2476       (if (etype && hi && !TREE_OVERFLOW (hi))
2477        (if (code == EQ_EXPR)
2478         (le (minus (convert:etype @0) { lo; }) { hi; })
2479         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2481 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2482 (for op (lt le ge gt)
2483  (simplify
2484   (op (plus:c @0 @2) (plus:c @1 @2))
2485   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2486        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2487    (op @0 @1))))
2489 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2490    when C is an unsigned integer constant with only the MSB set, and X and
2491    Y have types of equal or lower integer conversion rank than C's.  */
2492 (for op (lt le ge gt)
2493  (simplify
2494   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2495   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2496        && TYPE_UNSIGNED (TREE_TYPE (@0))
2497        && wi::only_sign_bit_p (wi::to_wide (@0)))
2498    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2499     (op (convert:stype @1) (convert:stype @2))))))
2501 /* For equality and subtraction, this is also true with wrapping overflow.  */
2502 (for op (eq ne minus)
2503  (simplify
2504   (op (plus:c @0 @2) (plus:c @1 @2))
2505   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2506        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2507            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2508    (op @0 @1))))
2510 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2511 (for op (lt le ge gt)
2512  (simplify
2513   (op (minus @0 @2) (minus @1 @2))
2514   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2515        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2516    (op @0 @1))))
2517 /* For equality and subtraction, this is also true with wrapping overflow.  */
2518 (for op (eq ne minus)
2519  (simplify
2520   (op (minus @0 @2) (minus @1 @2))
2521   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2522        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2523            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2524    (op @0 @1))))
2525 /* And for pointers...  */
2526 (for op (simple_comparison)
2527  (simplify
2528   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2529   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2530    (op @0 @1))))
2531 (simplify
2532  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2533  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2534       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2535   (pointer_diff @0 @1)))
2537 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2538 (for op (lt le ge gt)
2539  (simplify
2540   (op (minus @2 @0) (minus @2 @1))
2541   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2542        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2543    (op @1 @0))))
2544 /* For equality and subtraction, this is also true with wrapping overflow.  */
2545 (for op (eq ne minus)
2546  (simplify
2547   (op (minus @2 @0) (minus @2 @1))
2548   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2549        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2550            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2551    (op @1 @0))))
2552 /* And for pointers...  */
2553 (for op (simple_comparison)
2554  (simplify
2555   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2556   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2557    (op @1 @0))))
2558 (simplify
2559  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2560  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2561       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2562   (pointer_diff @1 @0)))
2564 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2565 (for op (lt le gt ge)
2566  (simplify
2567   (op:c (plus:c@2 @0 @1) @1)
2568   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2569        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2570        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2571        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2572    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2573 /* For equality, this is also true with wrapping overflow.  */
2574 (for op (eq ne)
2575  (simplify
2576   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2577   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2578        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2579            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2580        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2581        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2582        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2583    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2584  (simplify
2585   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2586   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2587        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2588        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2589    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2591 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2592 (for neeq (ne eq)
2593  (simplify
2594   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2595    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2596     (if (ptr_difference_const (@0, @2, &diff))
2597      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2598  (simplify
2599   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2600    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2601     (if (ptr_difference_const (@0, @2, &diff))
2602      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2604 /* X - Y < X is the same as Y > 0 when there is no overflow.
2605    For equality, this is also true with wrapping overflow.  */
2606 (for op (simple_comparison)
2607  (simplify
2608   (op:c @0 (minus@2 @0 @1))
2609   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2610        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2611            || ((op == EQ_EXPR || op == NE_EXPR)
2612                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2613        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2614    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2616 /* Transform:
2617    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2618    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2619 (for cmp (eq ne)
2620      ocmp (lt ge)
2621  (simplify
2622   (cmp (trunc_div @0 @1) integer_zerop)
2623   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2624        /* Complex ==/!= is allowed, but not </>=.  */
2625        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2626        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2627    (ocmp @0 @1))))
2629 /* X == C - X can never be true if C is odd.  */
2630 (for cmp (eq ne)
2631  (simplify
2632   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2633   (if (TREE_INT_CST_LOW (@1) & 1)
2634    { constant_boolean_node (cmp == NE_EXPR, type); })))
2636 /* Arguments on which one can call get_nonzero_bits to get the bits
2637    possibly set.  */
2638 (match with_possible_nonzero_bits
2639  INTEGER_CST@0)
2640 (match with_possible_nonzero_bits
2641  SSA_NAME@0
2642  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2643 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2644 (match (with_possible_nonzero_bits2 @0)
2645  with_possible_nonzero_bits@0)
2646 (match (with_possible_nonzero_bits2 @0)
2647  (bit_and:c with_possible_nonzero_bits@0 @2))
2649 /* Same for bits that are known to be set, but we do not have
2650    an equivalent to get_nonzero_bits yet.  */
2651 (match (with_certain_nonzero_bits2 @0)
2652  INTEGER_CST@0)
2653 (match (with_certain_nonzero_bits2 @0)
2654  (bit_ior @1 INTEGER_CST@0))
2656 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2657 (for cmp (eq ne)
2658  (simplify
2659   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2660   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2661    { constant_boolean_node (cmp == NE_EXPR, type); })))
2663 /* ((X inner_op C0) outer_op C1)
2664    With X being a tree where value_range has reasoned certain bits to always be
2665    zero throughout its computed value range,
2666    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2667    where zero_mask has 1's for all bits that are sure to be 0 in
2668    and 0's otherwise.
2669    if (inner_op == '^') C0 &= ~C1;
2670    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2671    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2673 (for inner_op (bit_ior bit_xor)
2674      outer_op (bit_xor bit_ior)
2675 (simplify
2676  (outer_op
2677   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2678  (with
2679   {
2680     bool fail = false;
2681     wide_int zero_mask_not;
2682     wide_int C0;
2683     wide_int cst_emit;
2685     if (TREE_CODE (@2) == SSA_NAME)
2686       zero_mask_not = get_nonzero_bits (@2);
2687     else
2688       fail = true;
2690     if (inner_op == BIT_XOR_EXPR)
2691       {
2692         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2693         cst_emit = C0 | wi::to_wide (@1);
2694       }
2695     else
2696       {
2697         C0 = wi::to_wide (@0);
2698         cst_emit = C0 ^ wi::to_wide (@1);
2699       }
2700   }
2701   (if (!fail && (C0 & zero_mask_not) == 0)
2702    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2703    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2704     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2706 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2707 (simplify
2708   (pointer_plus (pointer_plus:s @0 @1) @3)
2709   (pointer_plus @0 (plus @1 @3)))
2710 #if GENERIC
2711 (simplify
2712   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2713   (convert:type (pointer_plus @0 (plus @1 @3))))
2714 #endif
2716 /* Pattern match
2717      tem1 = (long) ptr1;
2718      tem2 = (long) ptr2;
2719      tem3 = tem2 - tem1;
2720      tem4 = (unsigned long) tem3;
2721      tem5 = ptr1 + tem4;
2722    and produce
2723      tem5 = ptr2;  */
2724 (simplify
2725   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2726   /* Conditionally look through a sign-changing conversion.  */
2727   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2728        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2729             || (GENERIC && type == TREE_TYPE (@1))))
2730    @1))
2731 (simplify
2732   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2733   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2734    (convert @1)))
2736 /* Pattern match
2737      tem = (sizetype) ptr;
2738      tem = tem & algn;
2739      tem = -tem;
2740      ... = ptr p+ tem;
2741    and produce the simpler and easier to analyze with respect to alignment
2742      ... = ptr & ~algn;  */
2743 (simplify
2744   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2745   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2746    (bit_and @0 { algn; })))
2748 /* Try folding difference of addresses.  */
2749 (simplify
2750  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2751  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2752   (with { poly_int64 diff; }
2753    (if (ptr_difference_const (@0, @1, &diff))
2754     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2755 (simplify
2756  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2757  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2758   (with { poly_int64 diff; }
2759    (if (ptr_difference_const (@0, @1, &diff))
2760     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2761 (simplify
2762  (minus (convert ADDR_EXPR@0) (convert @1))
2763  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2764   (with { poly_int64 diff; }
2765    (if (ptr_difference_const (@0, @1, &diff))
2766     { build_int_cst_type (type, diff); }))))
2767 (simplify
2768  (minus (convert @0) (convert ADDR_EXPR@1))
2769  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2770   (with { poly_int64 diff; }
2771    (if (ptr_difference_const (@0, @1, &diff))
2772     { build_int_cst_type (type, diff); }))))
2773 (simplify
2774  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2775  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2776       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2777   (with { poly_int64 diff; }
2778    (if (ptr_difference_const (@0, @1, &diff))
2779     { build_int_cst_type (type, diff); }))))
2780 (simplify
2781  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2782  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2783       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2784   (with { poly_int64 diff; }
2785    (if (ptr_difference_const (@0, @1, &diff))
2786     { build_int_cst_type (type, diff); }))))
2788 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2789 (simplify
2790  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2791  (with { poly_int64 diff; }
2792    (if (ptr_difference_const (@0, @2, &diff))
2793     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2794 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2795 (simplify
2796  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2797  (with { poly_int64 diff; }
2798    (if (ptr_difference_const (@0, @2, &diff))
2799     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2800 (simplify
2801  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2802  (with { poly_int64 diff; }
2803    (if (ptr_difference_const (@0, @1, &diff))
2804     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2806 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2807 (simplify
2808  (convert (pointer_diff @0 INTEGER_CST@1))
2809  (if (POINTER_TYPE_P (type))
2810   { build_fold_addr_expr_with_type
2811       (build2 (MEM_REF, char_type_node, @0,
2812                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2813                type); }))
2815 /* If arg0 is derived from the address of an object or function, we may
2816    be able to fold this expression using the object or function's
2817    alignment.  */
2818 (simplify
2819  (bit_and (convert? @0) INTEGER_CST@1)
2820  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2821       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2822   (with
2823    {
2824      unsigned int align;
2825      unsigned HOST_WIDE_INT bitpos;
2826      get_pointer_alignment_1 (@0, &align, &bitpos);
2827    }
2828    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2829     { wide_int_to_tree (type, (wi::to_wide (@1)
2830                                & (bitpos / BITS_PER_UNIT))); }))))
2832 (match min_value
2833  uniform_integer_cst_p
2834  (with {
2835    tree int_cst = uniform_integer_cst_p (t);
2836    tree inner_type = TREE_TYPE (int_cst);
2837   }
2838   (if ((INTEGRAL_TYPE_P (inner_type)
2839         || POINTER_TYPE_P (inner_type))
2840        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2842 (match max_value
2843  uniform_integer_cst_p
2844  (with {
2845    tree int_cst = uniform_integer_cst_p (t);
2846    tree itype = TREE_TYPE (int_cst);
2847   }
2848  (if ((INTEGRAL_TYPE_P (itype)
2849        || POINTER_TYPE_P (itype))
2850       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2852 /* x >  y  &&  x != XXX_MIN  -->  x > y
2853    x >  y  &&  x == XXX_MIN  -->  false . */
2854 (for eqne (eq ne)
2855  (simplify
2856   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2857    (switch
2858     (if (eqne == EQ_EXPR)
2859      { constant_boolean_node (false, type); })
2860     (if (eqne == NE_EXPR)
2861      @2)
2862     )))
2864 /* x <  y  &&  x != XXX_MAX  -->  x < y
2865    x <  y  &&  x == XXX_MAX  -->  false.  */
2866 (for eqne (eq ne)
2867  (simplify
2868   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2869    (switch
2870     (if (eqne == EQ_EXPR)
2871      { constant_boolean_node (false, type); })
2872     (if (eqne == NE_EXPR)
2873      @2)
2874     )))
2876 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2877 (simplify
2878  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2879   @2)
2881 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2882 (simplify
2883  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2884   @2)
2886 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2887 (simplify
2888  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2889   @2)
2891 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2892 (simplify
2893  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2894   { constant_boolean_node (true, type); })
2896 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2897 (simplify
2898  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2899   @2)
2901 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2902 (simplify
2903  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2904   @2)
2906 /* x >=  y  ||  x != XXX_MAX   -->  true
2907    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2908 (for eqne (eq ne)
2909  (simplify
2910   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2911    (switch
2912     (if (eqne == EQ_EXPR)
2913      @2)
2914     (if (eqne == NE_EXPR)
2915      { constant_boolean_node (true, type); }))))
2917 /* y == XXX_MIN || x < y --> x <= y - 1 */
2918 (simplify
2919  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2920   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2921        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2922   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2924 /* y != XXX_MIN && x >= y --> x > y - 1 */
2925 (simplify
2926  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2927   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2928        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2929   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2931 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2932    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2933 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2934    Similarly for (X != Y).  */
2936 (for code1 (eq ne)
2937  (for code2 (eq ne lt gt le ge)
2938   (simplify
2939    (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
2940    (if ((TREE_CODE (@1) == INTEGER_CST
2941          && TREE_CODE (@2) == INTEGER_CST)
2942         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2943              || POINTER_TYPE_P (TREE_TYPE (@1)))
2944             && operand_equal_p (@1, @2)))
2945     (with
2946      {
2947       int cmp = 0;
2948       if (TREE_CODE (@1) == INTEGER_CST
2949           && TREE_CODE (@2) == INTEGER_CST)
2950         cmp = tree_int_cst_compare (@1, @2);
2951       bool val;
2952       switch (code2)
2953          {
2954         case EQ_EXPR: val = (cmp == 0); break;
2955         case NE_EXPR: val = (cmp != 0); break;
2956         case LT_EXPR: val = (cmp < 0); break;
2957         case GT_EXPR: val = (cmp > 0); break;
2958         case LE_EXPR: val = (cmp <= 0); break;
2959         case GE_EXPR: val = (cmp >= 0); break;
2960         default: gcc_unreachable ();
2961         }
2962      }
2963      (switch
2964       (if (code1 == EQ_EXPR && val) @3)
2965       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2966       (if (code1 == NE_EXPR && !val) @4)
2967       (if (code1 == NE_EXPR
2968            && code2 == GE_EXPR
2969            && cmp == 0)
2970        (gt @0 @1))
2971       (if (code1 == NE_EXPR
2972            && code2 == LE_EXPR
2973            && cmp == 0)
2974        (lt @0 @1))
2975      )
2976     )
2977    )
2978   )
2982 /* Convert (X OP1 CST1) && (X OP2 CST2).
2983    Convert (X OP1 Y) && (X OP2 Y).  */
2985 (for code1 (lt le gt ge)
2986  (for code2 (lt le gt ge)
2987   (simplify
2988   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
2989   (if ((TREE_CODE (@1) == INTEGER_CST
2990         && TREE_CODE (@2) == INTEGER_CST)
2991        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2992             || POINTER_TYPE_P (TREE_TYPE (@1)))
2993            && operand_equal_p (@1, @2)))
2994    (with
2995     {
2996      int cmp = 0;
2997      if (TREE_CODE (@1) == INTEGER_CST
2998          && TREE_CODE (@2) == INTEGER_CST)
2999        cmp = tree_int_cst_compare (@1, @2);
3000     }
3001     (switch
3002      /* Choose the more restrictive of two < or <= comparisons.  */
3003      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3004           && (code2 == LT_EXPR || code2 == LE_EXPR))
3005       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3006        @3
3007        @4))
3008      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3009      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3010           && (code2 == GT_EXPR || code2 == GE_EXPR))
3011       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3012        @3
3013        @4))
3014      /* Check for singleton ranges.  */
3015      (if (cmp == 0
3016           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3017             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3018       (eq @0 @1))
3019      /* Check for disjoint ranges.  */
3020      (if (cmp <= 0
3021           && (code1 == LT_EXPR || code1 == LE_EXPR)
3022           && (code2 == GT_EXPR || code2 == GE_EXPR))
3023       { constant_boolean_node (false, type); })
3024      (if (cmp >= 0
3025           && (code1 == GT_EXPR || code1 == GE_EXPR)
3026           && (code2 == LT_EXPR || code2 == LE_EXPR))
3027       { constant_boolean_node (false, type); })
3028      ))))))
3030 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3031    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3032 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3033    Similarly for (X != Y).  */
3035 (for code1 (eq ne)
3036  (for code2 (eq ne lt gt le ge)
3037   (simplify
3038    (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
3039    (if ((TREE_CODE (@1) == INTEGER_CST
3040          && TREE_CODE (@2) == INTEGER_CST)
3041         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3042             || POINTER_TYPE_P (TREE_TYPE (@1)))
3043             && operand_equal_p (@1, @2)))
3044     (with
3045      {
3046       int cmp = 0;
3047       if (TREE_CODE (@1) == INTEGER_CST
3048           && TREE_CODE (@2) == INTEGER_CST)
3049         cmp = tree_int_cst_compare (@1, @2);
3050       bool val;
3051       switch (code2)
3052         {
3053         case EQ_EXPR: val = (cmp == 0); break;
3054         case NE_EXPR: val = (cmp != 0); break;
3055         case LT_EXPR: val = (cmp < 0); break;
3056         case GT_EXPR: val = (cmp > 0); break;
3057         case LE_EXPR: val = (cmp <= 0); break;
3058         case GE_EXPR: val = (cmp >= 0); break;
3059         default: gcc_unreachable ();
3060         }
3061      }
3062      (switch
3063       (if (code1 == EQ_EXPR && val) @4)
3064       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
3065       (if (code1 == NE_EXPR && !val) @3)
3066       (if (code1 == EQ_EXPR
3067            && code2 == GT_EXPR
3068            && cmp == 0)
3069        (ge @0 @1))
3070       (if (code1 == EQ_EXPR
3071            && code2 == LT_EXPR
3072            && cmp == 0)
3073        (le @0 @1))
3074      )
3075     )
3076    )
3077   )
3081 /* Convert (X OP1 CST1) || (X OP2 CST2).
3082    Convert (X OP1 Y)    || (X OP2 Y).  */
3084 (for code1 (lt le gt ge)
3085  (for code2 (lt le gt ge)
3086   (simplify
3087   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3088   (if ((TREE_CODE (@1) == INTEGER_CST
3089         && TREE_CODE (@2) == INTEGER_CST)
3090        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3091             || POINTER_TYPE_P (TREE_TYPE (@1)))
3092            && operand_equal_p (@1, @2)))
3093    (with
3094     {
3095      int cmp = 0;
3096      if (TREE_CODE (@1) == INTEGER_CST
3097          && TREE_CODE (@2) == INTEGER_CST)
3098        cmp = tree_int_cst_compare (@1, @2);
3099     }
3100     (switch
3101      /* Choose the more restrictive of two < or <= comparisons.  */
3102      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3103           && (code2 == LT_EXPR || code2 == LE_EXPR))
3104       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3105        @4
3106        @3))
3107      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3108      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3109           && (code2 == GT_EXPR || code2 == GE_EXPR))
3110       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3111        @4
3112        @3))
3113      /* Check for singleton ranges.  */
3114      (if (cmp == 0
3115           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3116               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3117       (ne @0 @2))
3118      /* Check for disjoint ranges.  */
3119      (if (cmp >= 0
3120           && (code1 == LT_EXPR || code1 == LE_EXPR)
3121           && (code2 == GT_EXPR || code2 == GE_EXPR))
3122       { constant_boolean_node (true, type); })
3123      (if (cmp <= 0
3124           && (code1 == GT_EXPR || code1 == GE_EXPR)
3125           && (code2 == LT_EXPR || code2 == LE_EXPR))
3126       { constant_boolean_node (true, type); })
3127      ))))))
3129 /* We can't reassociate at all for saturating types.  */
3130 (if (!TYPE_SATURATING (type))
3132  /* Contract negates.  */
3133  /* A + (-B) -> A - B */
3134  (simplify
3135   (plus:c @0 (convert? (negate @1)))
3136   /* Apply STRIP_NOPS on the negate.  */
3137   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3138        && !TYPE_OVERFLOW_SANITIZED (type))
3139    (with
3140     {
3141      tree t1 = type;
3142      if (INTEGRAL_TYPE_P (type)
3143          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3144        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3145     }
3146     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3147  /* A - (-B) -> A + B */
3148  (simplify
3149   (minus @0 (convert? (negate @1)))
3150   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3151        && !TYPE_OVERFLOW_SANITIZED (type))
3152    (with
3153     {
3154      tree t1 = type;
3155      if (INTEGRAL_TYPE_P (type)
3156          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3157        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3158     }
3159     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3160  /* -(T)(-A) -> (T)A
3161     Sign-extension is ok except for INT_MIN, which thankfully cannot
3162     happen without overflow.  */
3163  (simplify
3164   (negate (convert (negate @1)))
3165   (if (INTEGRAL_TYPE_P (type)
3166        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3167            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3168                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3169        && !TYPE_OVERFLOW_SANITIZED (type)
3170        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3171    (convert @1)))
3172  (simplify
3173   (negate (convert negate_expr_p@1))
3174   (if (SCALAR_FLOAT_TYPE_P (type)
3175        && ((DECIMAL_FLOAT_TYPE_P (type)
3176             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3177             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3178            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3179    (convert (negate @1))))
3180  (simplify
3181   (negate (nop_convert? (negate @1)))
3182   (if (!TYPE_OVERFLOW_SANITIZED (type)
3183        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3184    (view_convert @1)))
3186  /* We can't reassociate floating-point unless -fassociative-math
3187     or fixed-point plus or minus because of saturation to +-Inf.  */
3188  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3189       && !FIXED_POINT_TYPE_P (type))
3191   /* Match patterns that allow contracting a plus-minus pair
3192      irrespective of overflow issues.  */
3193   /* (A +- B) - A       ->  +- B */
3194   /* (A +- B) -+ B      ->  A */
3195   /* A - (A +- B)       -> -+ B */
3196   /* A +- (B -+ A)      ->  +- B */
3197   (simplify
3198    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3199    (view_convert @1))
3200   (simplify
3201    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3202    (if (!ANY_INTEGRAL_TYPE_P (type)
3203         || TYPE_OVERFLOW_WRAPS (type))
3204    (negate (view_convert @1))
3205    (view_convert (negate @1))))
3206   (simplify
3207    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3208    (view_convert @0))
3209   (simplify
3210    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3211     (if (!ANY_INTEGRAL_TYPE_P (type)
3212          || TYPE_OVERFLOW_WRAPS (type))
3213      (negate (view_convert @1))
3214      (view_convert (negate @1))))
3215   (simplify
3216    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3217    (view_convert @1))
3218   /* (A +- B) + (C - A)   -> C +- B */
3219   /* (A +  B) - (A - C)   -> B + C */
3220   /* More cases are handled with comparisons.  */
3221   (simplify
3222    (plus:c (plus:c @0 @1) (minus @2 @0))
3223    (plus @2 @1))
3224   (simplify
3225    (plus:c (minus @0 @1) (minus @2 @0))
3226    (minus @2 @1))
3227   (simplify
3228    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3229    (if (TYPE_OVERFLOW_UNDEFINED (type)
3230         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3231     (pointer_diff @2 @1)))
3232   (simplify
3233    (minus (plus:c @0 @1) (minus @0 @2))
3234    (plus @1 @2))
3236   /* (A +- CST1) +- CST2 -> A + CST3
3237      Use view_convert because it is safe for vectors and equivalent for
3238      scalars.  */
3239   (for outer_op (plus minus)
3240    (for inner_op (plus minus)
3241         neg_inner_op (minus plus)
3242     (simplify
3243      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3244                CONSTANT_CLASS_P@2)
3245      /* If one of the types wraps, use that one.  */
3246      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3247       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3248          forever if something doesn't simplify into a constant.  */
3249       (if (!CONSTANT_CLASS_P (@0))
3250        (if (outer_op == PLUS_EXPR)
3251         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3252         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3253       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3254            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3255        (if (outer_op == PLUS_EXPR)
3256         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3257         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3258        /* If the constant operation overflows we cannot do the transform
3259           directly as we would introduce undefined overflow, for example
3260           with (a - 1) + INT_MIN.  */
3261        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3262         (with { tree cst = const_binop (outer_op == inner_op
3263                                         ? PLUS_EXPR : MINUS_EXPR,
3264                                         type, @1, @2); }
3265          (if (cst)
3266           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3267            (inner_op @0 { cst; } )
3268            /* X+INT_MAX+1 is X-INT_MIN.  */
3269            (if (INTEGRAL_TYPE_P (type)
3270                 && wi::to_wide (cst) == wi::min_value (type))
3271             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3272             /* Last resort, use some unsigned type.  */
3273             (with { tree utype = unsigned_type_for (type); }
3274              (if (utype)
3275               (view_convert (inner_op
3276                              (view_convert:utype @0)
3277                              (view_convert:utype
3278                               { TREE_OVERFLOW (cst)
3279                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3281   /* (CST1 - A) +- CST2 -> CST3 - A  */
3282   (for outer_op (plus minus)
3283    (simplify
3284     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3285     /* If one of the types wraps, use that one.  */
3286     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3287      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3288         forever if something doesn't simplify into a constant.  */
3289      (if (!CONSTANT_CLASS_P (@0))
3290       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3291      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3292           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3293       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3294       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3295        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3296         (if (cst && !TREE_OVERFLOW (cst))
3297          (minus { cst; } @0))))))))
3299   /* CST1 - (CST2 - A) -> CST3 + A
3300      Use view_convert because it is safe for vectors and equivalent for
3301      scalars.  */
3302   (simplify
3303    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3304    /* If one of the types wraps, use that one.  */
3305    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3306     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3307       forever if something doesn't simplify into a constant.  */
3308     (if (!CONSTANT_CLASS_P (@0))
3309      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3310     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3311          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3312      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3313      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3314       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3315        (if (cst && !TREE_OVERFLOW (cst))
3316         (plus { cst; } @0)))))))
3318 /* ((T)(A)) + CST -> (T)(A + CST)  */
3319 #if GIMPLE
3320   (simplify
3321    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3322     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3323          && TREE_CODE (type) == INTEGER_TYPE
3324          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3325          && int_fits_type_p (@1, TREE_TYPE (@0)))
3326      /* Perform binary operation inside the cast if the constant fits
3327         and (A + CST)'s range does not overflow.  */
3328      (with
3329       {
3330         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3331                           max_ovf = wi::OVF_OVERFLOW;
3332         tree inner_type = TREE_TYPE (@0);
3334         wide_int w1
3335           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3336                             TYPE_SIGN (inner_type));
3338         value_range vr;
3339         if (get_global_range_query ()->range_of_expr (vr, @0)
3340             && !vr.varying_p () && !vr.undefined_p ())
3341           {
3342             wide_int wmin0 = vr.lower_bound ();
3343             wide_int wmax0 = vr.upper_bound ();
3344             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3345             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3346           }
3347       }
3348      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3349       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3350      )))
3351 #endif
3353 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3354 #if GIMPLE
3355   (for op (plus minus)
3356    (simplify
3357     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3358      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3359           && TREE_CODE (type) == INTEGER_TYPE
3360           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3361           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3362           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3363           && TYPE_OVERFLOW_WRAPS (type))
3364        (plus (convert @0) (op @2 (convert @1))))))
3365 #endif
3367 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3368    to a simple value.  */
3369   (for op (plus minus)
3370    (simplify
3371     (op (convert @0) (convert @1))
3372      (if (INTEGRAL_TYPE_P (type)
3373           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3374           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3375           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3376           && !TYPE_OVERFLOW_TRAPS (type)
3377           && !TYPE_OVERFLOW_SANITIZED (type))
3378       (convert (op! @0 @1)))))
3380   /* ~A + A -> -1 */
3381   (simplify
3382    (plus:c (convert? (bit_not @0)) (convert? @0))
3383    (if (!TYPE_OVERFLOW_TRAPS (type))
3384     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3386   /* ~A + 1 -> -A */
3387   (simplify
3388    (plus (convert? (bit_not @0)) integer_each_onep)
3389    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3390     (negate (convert @0))))
3392   /* -A - 1 -> ~A */
3393   (simplify
3394    (minus (convert? (negate @0)) integer_each_onep)
3395    (if (!TYPE_OVERFLOW_TRAPS (type)
3396         && TREE_CODE (type) != COMPLEX_TYPE
3397         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3398     (bit_not (convert @0))))
3400   /* -1 - A -> ~A */
3401   (simplify
3402    (minus integer_all_onesp @0)
3403    (if (TREE_CODE (type) != COMPLEX_TYPE)
3404     (bit_not @0)))
3406   /* (T)(P + A) - (T)P -> (T) A */
3407   (simplify
3408    (minus (convert (plus:c @@0 @1))
3409     (convert? @0))
3410    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3411         /* For integer types, if A has a smaller type
3412            than T the result depends on the possible
3413            overflow in P + A.
3414            E.g. T=size_t, A=(unsigned)429497295, P>0.
3415            However, if an overflow in P + A would cause
3416            undefined behavior, we can assume that there
3417            is no overflow.  */
3418         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3419             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3420     (convert @1)))
3421   (simplify
3422    (minus (convert (pointer_plus @@0 @1))
3423     (convert @0))
3424    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3425         /* For pointer types, if the conversion of A to the
3426            final type requires a sign- or zero-extension,
3427            then we have to punt - it is not defined which
3428            one is correct.  */
3429         || (POINTER_TYPE_P (TREE_TYPE (@0))
3430             && TREE_CODE (@1) == INTEGER_CST
3431             && tree_int_cst_sign_bit (@1) == 0))
3432     (convert @1)))
3433    (simplify
3434     (pointer_diff (pointer_plus @@0 @1) @0)
3435     /* The second argument of pointer_plus must be interpreted as signed, and
3436        thus sign-extended if necessary.  */
3437     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3438      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3439         second arg is unsigned even when we need to consider it as signed,
3440         we don't want to diagnose overflow here.  */
3441      (convert (view_convert:stype @1))))
3443   /* (T)P - (T)(P + A) -> -(T) A */
3444   (simplify
3445    (minus (convert? @0)
3446     (convert (plus:c @@0 @1)))
3447    (if (INTEGRAL_TYPE_P (type)
3448         && TYPE_OVERFLOW_UNDEFINED (type)
3449         /* For integer literals, using an intermediate unsigned type to avoid
3450            an overflow at run time is counter-productive because it introduces
3451            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3452            the result, which may be problematic in GENERIC for some front-ends:
3453              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3454            so we use the direct path for them.  */
3455         && TREE_CODE (@1) != INTEGER_CST
3456         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3457     (with { tree utype = unsigned_type_for (type); }
3458      (convert (negate (convert:utype @1))))
3459     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3460          /* For integer types, if A has a smaller type
3461             than T the result depends on the possible
3462             overflow in P + A.
3463             E.g. T=size_t, A=(unsigned)429497295, P>0.
3464             However, if an overflow in P + A would cause
3465             undefined behavior, we can assume that there
3466             is no overflow.  */
3467          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3468              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3469      (negate (convert @1)))))
3470   (simplify
3471    (minus (convert @0)
3472     (convert (pointer_plus @@0 @1)))
3473    (if (INTEGRAL_TYPE_P (type)
3474         && TYPE_OVERFLOW_UNDEFINED (type)
3475         /* See above the rationale for this condition.  */
3476         && TREE_CODE (@1) != INTEGER_CST
3477         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3478     (with { tree utype = unsigned_type_for (type); }
3479      (convert (negate (convert:utype @1))))
3480     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3481          /* For pointer types, if the conversion of A to the
3482             final type requires a sign- or zero-extension,
3483             then we have to punt - it is not defined which
3484             one is correct.  */
3485          || (POINTER_TYPE_P (TREE_TYPE (@0))
3486              && TREE_CODE (@1) == INTEGER_CST
3487              && tree_int_cst_sign_bit (@1) == 0))
3488      (negate (convert @1)))))
3489    (simplify
3490     (pointer_diff @0 (pointer_plus @@0 @1))
3491     /* The second argument of pointer_plus must be interpreted as signed, and
3492        thus sign-extended if necessary.  */
3493     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3494      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3495         second arg is unsigned even when we need to consider it as signed,
3496         we don't want to diagnose overflow here.  */
3497      (negate (convert (view_convert:stype @1)))))
3499   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3500   (simplify
3501    (minus (convert (plus:c @@0 @1))
3502     (convert (plus:c @0 @2)))
3503    (if (INTEGRAL_TYPE_P (type)
3504         && TYPE_OVERFLOW_UNDEFINED (type)
3505         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3506         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3507     (with { tree utype = unsigned_type_for (type); }
3508      (convert (minus (convert:utype @1) (convert:utype @2))))
3509     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3510           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3511          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3512              /* For integer types, if A has a smaller type
3513                 than T the result depends on the possible
3514                 overflow in P + A.
3515                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3516                 However, if an overflow in P + A would cause
3517                 undefined behavior, we can assume that there
3518                 is no overflow.  */
3519              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3520                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3521                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3522                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3523      (minus (convert @1) (convert @2)))))
3524   (simplify
3525    (minus (convert (pointer_plus @@0 @1))
3526     (convert (pointer_plus @0 @2)))
3527    (if (INTEGRAL_TYPE_P (type)
3528         && TYPE_OVERFLOW_UNDEFINED (type)
3529         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3530     (with { tree utype = unsigned_type_for (type); }
3531      (convert (minus (convert:utype @1) (convert:utype @2))))
3532     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3533          /* For pointer types, if the conversion of A to the
3534             final type requires a sign- or zero-extension,
3535             then we have to punt - it is not defined which
3536             one is correct.  */
3537          || (POINTER_TYPE_P (TREE_TYPE (@0))
3538              && TREE_CODE (@1) == INTEGER_CST
3539              && tree_int_cst_sign_bit (@1) == 0
3540              && TREE_CODE (@2) == INTEGER_CST
3541              && tree_int_cst_sign_bit (@2) == 0))
3542      (minus (convert @1) (convert @2)))))
3543    (simplify
3544     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3545      (pointer_diff @0 @1))
3546    (simplify
3547     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3548     /* The second argument of pointer_plus must be interpreted as signed, and
3549        thus sign-extended if necessary.  */
3550     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3551      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3552         second arg is unsigned even when we need to consider it as signed,
3553         we don't want to diagnose overflow here.  */
3554      (minus (convert (view_convert:stype @1))
3555             (convert (view_convert:stype @2)))))))
3557 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3558     Modeled after fold_plusminus_mult_expr.  */
3559 (if (!TYPE_SATURATING (type)
3560      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3561  (for plusminus (plus minus)
3562   (simplify
3563    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3564    (if (!ANY_INTEGRAL_TYPE_P (type)
3565         || TYPE_OVERFLOW_WRAPS (type)
3566         || (INTEGRAL_TYPE_P (type)
3567             && tree_expr_nonzero_p (@0)
3568             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3569     (if (single_use (@3) || single_use (@4))
3570      /* If @1 +- @2 is constant require a hard single-use on either
3571         original operand (but not on both).  */
3572      (mult (plusminus @1 @2) @0)
3573      (mult! (plusminus @1 @2) @0)
3574   )))
3575   /* We cannot generate constant 1 for fract.  */
3576   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3577    (simplify
3578     (plusminus @0 (mult:c@3 @0 @2))
3579     (if ((!ANY_INTEGRAL_TYPE_P (type)
3580           || TYPE_OVERFLOW_WRAPS (type)
3581           /* For @0 + @0*@2 this transformation would introduce UB
3582              (where there was none before) for @0 in [-1,0] and @2 max.
3583              For @0 - @0*@2 this transformation would introduce UB
3584              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3585           || (INTEGRAL_TYPE_P (type)
3586               && ((tree_expr_nonzero_p (@0)
3587                    && expr_not_equal_to (@0,
3588                                 wi::minus_one (TYPE_PRECISION (type))))
3589                   || (plusminus == PLUS_EXPR
3590                       ? expr_not_equal_to (@2,
3591                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3592                       /* Let's ignore the @0 -1 and @2 min case.  */
3593                       : (expr_not_equal_to (@2,
3594                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3595                          && expr_not_equal_to (@2,
3596                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3597                                 + 1))))))
3598          && single_use (@3))
3599      (mult (plusminus { build_one_cst (type); } @2) @0)))
3600    (simplify
3601     (plusminus (mult:c@3 @0 @2) @0)
3602     (if ((!ANY_INTEGRAL_TYPE_P (type)
3603           || TYPE_OVERFLOW_WRAPS (type)
3604           /* For @0*@2 + @0 this transformation would introduce UB
3605              (where there was none before) for @0 in [-1,0] and @2 max.
3606              For @0*@2 - @0 this transformation would introduce UB
3607              for @0 0 and @2 min.  */
3608           || (INTEGRAL_TYPE_P (type)
3609               && ((tree_expr_nonzero_p (@0)
3610                    && (plusminus == MINUS_EXPR
3611                        || expr_not_equal_to (@0,
3612                                 wi::minus_one (TYPE_PRECISION (type)))))
3613                   || expr_not_equal_to (@2,
3614                         (plusminus == PLUS_EXPR
3615                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3616                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3617          && single_use (@3))
3618      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3620 #if GIMPLE
3621 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3622    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3623 (simplify
3624  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3625   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3626        && tree_fits_uhwi_p (@1)
3627        && tree_to_uhwi (@1) < element_precision (type)
3628        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3629            || optab_handler (smul_optab,
3630                              TYPE_MODE (type)) != CODE_FOR_nothing))
3631    (with { tree t = type;
3632            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3633            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3634                                              element_precision (type));
3635            w += 1;
3636            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3637                                         : t, w);
3638            cst = build_uniform_cst (t, cst); }
3639     (convert (mult (convert:t @0) { cst; })))))
3640 (simplify
3641  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3642   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3643        && tree_fits_uhwi_p (@1)
3644        && tree_to_uhwi (@1) < element_precision (type)
3645        && tree_fits_uhwi_p (@2)
3646        && tree_to_uhwi (@2) < element_precision (type)
3647        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3648            || optab_handler (smul_optab,
3649                              TYPE_MODE (type)) != CODE_FOR_nothing))
3650    (with { tree t = type;
3651            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3652            unsigned int prec = element_precision (type);
3653            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3654            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3655            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3656                                         : t, w);
3657            cst = build_uniform_cst (t, cst); }
3658     (convert (mult (convert:t @0) { cst; })))))
3659 #endif
3661 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3662    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3663    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3664 (for op (bit_ior bit_xor)
3665  (simplify
3666   (op (mult:s@0 @1 INTEGER_CST@2)
3667       (mult:s@3 @1 INTEGER_CST@4))
3668   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3669        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3670    (mult @1
3671          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3672  (simplify
3673   (op:c (mult:s@0 @1 INTEGER_CST@2)
3674         (lshift:s@3 @1 INTEGER_CST@4))
3675   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3676        && tree_int_cst_sgn (@4) > 0
3677        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3678    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3679            wide_int c = wi::add (wi::to_wide (@2),
3680                                  wi::lshift (wone, wi::to_wide (@4))); }
3681     (mult @1 { wide_int_to_tree (type, c); }))))
3682  (simplify
3683   (op:c (mult:s@0 @1 INTEGER_CST@2)
3684         @1)
3685   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3686        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3687    (mult @1
3688          { wide_int_to_tree (type,
3689                              wi::add (wi::to_wide (@2), 1)); })))
3690  (simplify
3691   (op (lshift:s@0 @1 INTEGER_CST@2)
3692       (lshift:s@3 @1 INTEGER_CST@4))
3693   (if (INTEGRAL_TYPE_P (type)
3694        && tree_int_cst_sgn (@2) > 0
3695        && tree_int_cst_sgn (@4) > 0
3696        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3697    (with { tree t = type;
3698            if (!TYPE_OVERFLOW_WRAPS (t))
3699              t = unsigned_type_for (t);
3700            wide_int wone = wi::one (TYPE_PRECISION (t));
3701            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3702                                  wi::lshift (wone, wi::to_wide (@4))); }
3703     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3704  (simplify
3705   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3706         @1)
3707   (if (INTEGRAL_TYPE_P (type)
3708        && tree_int_cst_sgn (@2) > 0
3709        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3710    (with { tree t = type;
3711            if (!TYPE_OVERFLOW_WRAPS (t))
3712              t = unsigned_type_for (t);
3713            wide_int wone = wi::one (TYPE_PRECISION (t));
3714            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3715     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3717 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3719 (for minmax (min max)
3720  (simplify
3721   (minmax @0 @0)
3722   @0)
3723 /* max(max(x,y),x) -> max(x,y)  */
3724  (simplify
3725   (minmax:c (minmax:c@2 @0 @1) @0)
3726   @2))
3727 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3728 (for minmax (FMIN_ALL FMAX_ALL)
3729  (simplify
3730   (minmax @0 @0)
3731   (if (!tree_expr_maybe_signaling_nan_p (@0))
3732     @0)))
3733 /* min(max(x,y),y) -> y.  */
3734 (simplify
3735  (min:c (max:c @0 @1) @1)
3736  @1)
3737 /* max(min(x,y),y) -> y.  */
3738 (simplify
3739  (max:c (min:c @0 @1) @1)
3740  @1)
3741 /* max(a,-a) -> abs(a).  */
3742 (simplify
3743  (max:c @0 (negate @0))
3744  (if (TREE_CODE (type) != COMPLEX_TYPE
3745       && (! ANY_INTEGRAL_TYPE_P (type)
3746           || TYPE_OVERFLOW_UNDEFINED (type)))
3747   (abs @0)))
3748 /* min(a,-a) -> -abs(a).  */
3749 (simplify
3750  (min:c @0 (negate @0))
3751  (if (TREE_CODE (type) != COMPLEX_TYPE
3752       && (! ANY_INTEGRAL_TYPE_P (type)
3753           || TYPE_OVERFLOW_UNDEFINED (type)))
3754   (negate (abs @0))))
3755 (simplify
3756  (min @0 @1)
3757  (switch
3758   (if (INTEGRAL_TYPE_P (type)
3759        && TYPE_MIN_VALUE (type)
3760        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3761    @1)
3762   (if (INTEGRAL_TYPE_P (type)
3763        && TYPE_MAX_VALUE (type)
3764        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3765    @0)))
3766 (simplify
3767  (max @0 @1)
3768  (switch
3769   (if (INTEGRAL_TYPE_P (type)
3770        && TYPE_MAX_VALUE (type)
3771        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3772    @1)
3773   (if (INTEGRAL_TYPE_P (type)
3774        && TYPE_MIN_VALUE (type)
3775        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3776    @0)))
3778 /* max (a, a + CST) -> a + CST where CST is positive.  */
3779 /* max (a, a + CST) -> a where CST is negative.  */
3780 (simplify
3781  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3782   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3783    (if (tree_int_cst_sgn (@1) > 0)
3784     @2
3785     @0)))
3787 /* min (a, a + CST) -> a where CST is positive.  */
3788 /* min (a, a + CST) -> a + CST where CST is negative. */
3789 (simplify
3790  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3791   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3792    (if (tree_int_cst_sgn (@1) > 0)
3793     @0
3794     @2)))
3796 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3797    the addresses are known to be less, equal or greater.  */
3798 (for minmax (min max)
3799      cmp (lt gt)
3800  (simplify
3801   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3802   (with
3803    {
3804      poly_int64 off0, off1;
3805      tree base0, base1;
3806      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3807                                   off0, off1, GENERIC);
3808    }
3809    (if (equal == 1)
3810     (if (minmax == MIN_EXPR)
3811      (if (known_le (off0, off1))
3812       @2
3813       (if (known_gt (off0, off1))
3814        @3))
3815      (if (known_ge (off0, off1))
3816       @2
3817       (if (known_lt (off0, off1))
3818        @3)))))))
3820 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3821    and the outer convert demotes the expression back to x's type.  */
3822 (for minmax (min max)
3823  (simplify
3824   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3825   (if (INTEGRAL_TYPE_P (type)
3826        && types_match (@1, type) && int_fits_type_p (@2, type)
3827        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3828        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3829    (minmax @1 (convert @2)))))
3831 (for minmax (FMIN_ALL FMAX_ALL)
3832  /* If either argument is NaN and other one is not sNaN, return the other
3833     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3834  (simplify
3835   (minmax:c @0 REAL_CST@1)
3836    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3837        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3838        && !tree_expr_maybe_signaling_nan_p (@0))
3839    @0)))
3840 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3841    functions to return the numeric arg if the other one is NaN.
3842    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3843    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3844    worry about it either.  */
3845 (if (flag_finite_math_only)
3846  (simplify
3847   (FMIN_ALL @0 @1)
3848   (min @0 @1))
3849  (simplify
3850   (FMAX_ALL @0 @1)
3851   (max @0 @1)))
3852 /* min (-A, -B) -> -max (A, B)  */
3853 (for minmax (min max FMIN_ALL FMAX_ALL)
3854      maxmin (max min FMAX_ALL FMIN_ALL)
3855  (simplify
3856   (minmax (negate:s@2 @0) (negate:s@3 @1))
3857   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3858        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3859            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3860    (negate (maxmin @0 @1)))))
3861 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3862    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3863 (for minmax (min max)
3864  maxmin (max min)
3865  (simplify
3866   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3867   (bit_not (maxmin @0 @1))))
3869 /* MIN (X, Y) == X -> X <= Y  */
3870 (for minmax (min min max max)
3871      cmp    (eq  ne  eq  ne )
3872      out    (le  gt  ge  lt )
3873  (simplify
3874   (cmp:c (minmax:c @0 @1) @0)
3875   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3876    (out @0 @1))))
3877 /* MIN (X, 5) == 0 -> X == 0
3878    MIN (X, 5) == 7 -> false  */
3879 (for cmp (eq ne)
3880  (simplify
3881   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3882   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3883                  TYPE_SIGN (TREE_TYPE (@0))))
3884    { constant_boolean_node (cmp == NE_EXPR, type); }
3885    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3886                   TYPE_SIGN (TREE_TYPE (@0))))
3887     (cmp @0 @2)))))
3888 (for cmp (eq ne)
3889  (simplify
3890   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3891   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3892                  TYPE_SIGN (TREE_TYPE (@0))))
3893    { constant_boolean_node (cmp == NE_EXPR, type); }
3894    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3895                   TYPE_SIGN (TREE_TYPE (@0))))
3896     (cmp @0 @2)))))
3897 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
3898 (for minmax (min     min     max     max     min     min     max     max    )
3899      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
3900      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3901  (simplify
3902   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3903   (comb (cmp @0 @2) (cmp @1 @2))))
3905 /* X <= MAX(X, Y) -> true
3906    X > MAX(X, Y) -> false 
3907    X >= MIN(X, Y) -> true
3908    X < MIN(X, Y) -> false */
3909 (for minmax (min     min     max     max     )
3910      cmp    (ge      lt      le      gt      )
3911  (simplify
3912   (cmp @0 (minmax:c @0 @1))
3913   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3915 /* Undo fancy ways of writing max/min or other ?: expressions, like
3916    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
3917    People normally use ?: and that is what we actually try to optimize.  */
3918 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
3919 (simplify
3920  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3921  (if (INTEGRAL_TYPE_P (type)
3922       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3923   (cond (convert:boolean_type_node @2) @1 @0)))
3924 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
3925 (simplify
3926  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3927  (if (INTEGRAL_TYPE_P (type)
3928       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3929   (cond (convert:boolean_type_node @2) @1 @0)))
3930 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
3931 (simplify
3932  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3933  (if (INTEGRAL_TYPE_P (type)
3934       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3935   (cond (convert:boolean_type_node @2) @1 @0)))
3937 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
3938 (simplify
3939   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3940   (max @2 @1))
3942 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
3943 (for op (bit_xor bit_ior plus)
3944  (simplify
3945   (cond (eq zero_one_valued_p@0
3946             integer_zerop)
3947         @1
3948         (op:c @2 @1))
3949   (if (INTEGRAL_TYPE_P (type)
3950        && TYPE_PRECISION (type) > 1
3951        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3952        (op (mult (convert:type @0) @2) @1))))
3954 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
3955 (for op (bit_xor bit_ior plus)
3956  (simplify
3957   (cond (ne zero_one_valued_p@0
3958             integer_zerop)
3959        (op:c @2 @1)
3960         @1)
3961   (if (INTEGRAL_TYPE_P (type)
3962        && TYPE_PRECISION (type) > 1
3963        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3964        (op (mult (convert:type @0) @2) @1))))
3966 /* Simplifications of shift and rotates.  */
3968 (for rotate (lrotate rrotate)
3969  (simplify
3970   (rotate integer_all_onesp@0 @1)
3971   @0))
3973 /* Optimize -1 >> x for arithmetic right shifts.  */
3974 (simplify
3975  (rshift integer_all_onesp@0 @1)
3976  (if (!TYPE_UNSIGNED (type))
3977   @0))
3979 /* Optimize (x >> c) << c into x & (-1<<c).  */
3980 (simplify
3981  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3982  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3983   /* It doesn't matter if the right shift is arithmetic or logical.  */
3984   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3986 (simplify
3987  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3988  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3989       /* Allow intermediate conversion to integral type with whatever sign, as
3990          long as the low TYPE_PRECISION (type)
3991          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3992       && INTEGRAL_TYPE_P (type)
3993       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3994       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3995       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3996       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3997           || wi::geu_p (wi::to_wide (@1),
3998                         TYPE_PRECISION (type)
3999                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4000   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4002 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4003    unsigned x OR truncate into the precision(type) - c lowest bits
4004    of signed x (if they have mode precision or a precision of 1).  */
4005 (simplify
4006  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4007  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4008   (if (TYPE_UNSIGNED (type))
4009    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4010    (if (INTEGRAL_TYPE_P (type))
4011     (with {
4012       int width = element_precision (type) - tree_to_uhwi (@1);
4013       tree stype = build_nonstandard_integer_type (width, 0);
4014      }
4015      (if (width == 1 || type_has_mode_precision_p (stype))
4016       (convert (convert:stype @0))))))))
4018 /* Optimize x >> x into 0 */
4019 (simplify
4020  (rshift @0 @0)
4021   { build_zero_cst (type); })
4023 (for shiftrotate (lrotate rrotate lshift rshift)
4024  (simplify
4025   (shiftrotate @0 integer_zerop)
4026   (non_lvalue @0))
4027  (simplify
4028   (shiftrotate integer_zerop@0 @1)
4029   @0)
4030  /* Prefer vector1 << scalar to vector1 << vector2
4031     if vector2 is uniform.  */
4032  (for vec (VECTOR_CST CONSTRUCTOR)
4033   (simplify
4034    (shiftrotate @0 vec@1)
4035    (with { tree tem = uniform_vector_p (@1); }
4036     (if (tem)
4037      (shiftrotate @0 { tem; }))))))
4039 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4040    Y is 0.  Similarly for X >> Y.  */
4041 #if GIMPLE
4042 (for shift (lshift rshift)
4043  (simplify
4044   (shift @0 SSA_NAME@1)
4045    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4046     (with {
4047       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4048       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4049      }
4050      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4051       @0)))))
4052 #endif
4054 /* Rewrite an LROTATE_EXPR by a constant into an
4055    RROTATE_EXPR by a new constant.  */
4056 (simplify
4057  (lrotate @0 INTEGER_CST@1)
4058  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4059                             build_int_cst (TREE_TYPE (@1),
4060                                            element_precision (type)), @1); }))
4062 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4063 (for op (lrotate rrotate rshift lshift)
4064  (simplify
4065   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4066   (with { unsigned int prec = element_precision (type); }
4067    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4068         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4069         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4070         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4071     (with { unsigned int low = (tree_to_uhwi (@1)
4072                                 + tree_to_uhwi (@2)); }
4073      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4074         being well defined.  */
4075      (if (low >= prec)
4076       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4077        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4078        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4079         { build_zero_cst (type); }
4080         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4081       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4084 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4085 (simplify
4086  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4087   (if ((wi::to_wide (@1) & 1) != 0)
4088    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4089    { build_zero_cst (type); }))
4091 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4092    either to false if D is smaller (unsigned comparison) than C, or to
4093    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
4094 (for cmp (ne eq)
4095      icmp (eq ne)
4096  (simplify
4097   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4098    (with { int c1 = wi::clz (wi::to_wide (@1));
4099            int c2 = wi::clz (wi::to_wide (@2)); }
4100     (if (c1 < c2)
4101      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4102      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4103  (simplify
4104   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4105    (if (tree_int_cst_sgn (@1) > 0)
4106     (with { int c1 = wi::clz (wi::to_wide (@1));
4107             int c2 = wi::clz (wi::to_wide (@2)); }
4108      (if (c1 > c2)
4109       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4110       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
4112 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4113    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4114    if CST2 != 0.  */
4115 (for cmp (ne eq)
4116  (simplify
4117   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4118   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4119    (if (cand < 0
4120         || (!integer_zerop (@2)
4121             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4122     { constant_boolean_node (cmp == NE_EXPR, type); }
4123     (if (!integer_zerop (@2)
4124          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4125      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4127 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4128         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4129 (for cmp (ne eq)
4130  (simplify
4131   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4132   (if (tree_fits_shwi_p (@1)
4133        && tree_to_shwi (@1) > 0
4134        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4135     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4136       { constant_boolean_node (cmp == NE_EXPR, type); }
4137       (with { wide_int c1 = wi::to_wide (@1);
4138               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4139               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4140         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4141              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4142  (simplify
4143   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4144   (if (tree_fits_shwi_p (@1)
4145        && tree_to_shwi (@1) > 0
4146        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4147     (with { tree t0 = TREE_TYPE (@0);
4148             unsigned int prec = TYPE_PRECISION (t0);
4149             wide_int c1 = wi::to_wide (@1);
4150             wide_int c2 = wi::to_wide (@2);
4151             wide_int c3 = wi::to_wide (@3);
4152             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4153       (if ((c2 & c3) != c3)
4154         { constant_boolean_node (cmp == NE_EXPR, type); }
4155         (if (TYPE_UNSIGNED (t0))
4156           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4157             { constant_boolean_node (cmp == NE_EXPR, type); }
4158             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4159                  { wide_int_to_tree (t0, c3 << c1); }))
4160           (with { wide_int smask = wi::arshift (sb, c1); }
4161             (switch
4162               (if ((c2 & smask) == 0)
4163                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4164                      { wide_int_to_tree (t0, c3 << c1); }))
4165               (if ((c3 & smask) == 0)
4166                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4167                      { wide_int_to_tree (t0, c3 << c1); }))
4168               (if ((c2 & smask) != (c3 & smask))
4169                 { constant_boolean_node (cmp == NE_EXPR, type); })
4170               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4171                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4173 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4174         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4175    if the new mask might be further optimized.  */
4176 (for shift (lshift rshift)
4177  (simplify
4178   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4179            INTEGER_CST@2)
4180    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4181         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4182         && tree_fits_uhwi_p (@1)
4183         && tree_to_uhwi (@1) > 0
4184         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4185     (with
4186      {
4187        unsigned int shiftc = tree_to_uhwi (@1);
4188        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4189        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4190        tree shift_type = TREE_TYPE (@3);
4191        unsigned int prec;
4193        if (shift == LSHIFT_EXPR)
4194          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4195        else if (shift == RSHIFT_EXPR
4196                 && type_has_mode_precision_p (shift_type))
4197          {
4198            prec = TYPE_PRECISION (TREE_TYPE (@3));
4199            tree arg00 = @0;
4200            /* See if more bits can be proven as zero because of
4201               zero extension.  */
4202            if (@3 != @0
4203                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4204              {
4205                tree inner_type = TREE_TYPE (@0);
4206                if (type_has_mode_precision_p (inner_type)
4207                    && TYPE_PRECISION (inner_type) < prec)
4208                  {
4209                    prec = TYPE_PRECISION (inner_type);
4210                    /* See if we can shorten the right shift.  */
4211                    if (shiftc < prec)
4212                      shift_type = inner_type;
4213                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4214                       it into (X, 0) later on by making sure zerobits
4215                       is all ones.  */
4216                  }
4217              }
4218            zerobits = HOST_WIDE_INT_M1U;
4219            if (shiftc < prec)
4220              {
4221                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4222                zerobits <<= prec - shiftc;
4223              }
4224            /* For arithmetic shift if sign bit could be set, zerobits
4225               can contain actually sign bits, so no transformation is
4226               possible, unless MASK masks them all away.  In that
4227               case the shift needs to be converted into logical shift.  */
4228            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4229                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4230              {
4231                if ((mask & zerobits) == 0)
4232                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4233                else
4234                  zerobits = 0;
4235              }
4236          }
4237      }
4238      /* ((X << 16) & 0xff00) is (X, 0).  */
4239      (if ((mask & zerobits) == mask)
4240       { build_int_cst (type, 0); }
4241       (with { newmask = mask | zerobits; }
4242        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4243         (with
4244          {
4245            /* Only do the transformation if NEWMASK is some integer
4246               mode's mask.  */
4247            for (prec = BITS_PER_UNIT;
4248                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4249              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4250                break;
4251          }
4252          (if (prec < HOST_BITS_PER_WIDE_INT
4253               || newmask == HOST_WIDE_INT_M1U)
4254           (with
4255            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4256            (if (!tree_int_cst_equal (newmaskt, @2))
4257             (if (shift_type != TREE_TYPE (@3))
4258              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4259              (bit_and @4 { newmaskt; })))))))))))))
4261 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4262 (for cmp (ne eq)
4263        icmp (eq ne)
4264  (simplify
4265   (cmp
4266    (bit_and
4267     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4268   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4269    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4270                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4272 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4273    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4274 (for shift (lshift rshift)
4275  (for bit_op (bit_and bit_xor bit_ior)
4276   (simplify
4277    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4278    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4279     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4280      (if (mask)
4281       (bit_op (shift (convert @0) @1) { mask; })))))))
4283 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4284 (simplify
4285  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4286   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4287        && (element_precision (TREE_TYPE (@0))
4288            <= element_precision (TREE_TYPE (@1))
4289            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4290    (with
4291     { tree shift_type = TREE_TYPE (@0); }
4292      (convert (rshift (convert:shift_type @1) @2)))))
4294 /* ~(~X >>r Y) -> X >>r Y
4295    ~(~X <<r Y) -> X <<r Y */
4296 (for rotate (lrotate rrotate)
4297  (simplify
4298   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4299    (if ((element_precision (TREE_TYPE (@0))
4300          <= element_precision (TREE_TYPE (@1))
4301          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4302         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4303             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4304     (with
4305      { tree rotate_type = TREE_TYPE (@0); }
4306       (convert (rotate (convert:rotate_type @1) @2))))))
4308 (for cmp (eq ne)
4309  (for rotate (lrotate rrotate)
4310       invrot (rrotate lrotate)
4311   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4312   (simplify
4313    (cmp (rotate @1 @0) (rotate @2 @0))
4314    (cmp @1 @2))
4315   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4316   (simplify
4317    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4318    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4319   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4320   (simplify
4321    (cmp (rotate @0 @1) INTEGER_CST@2)
4322     (if (integer_zerop (@2) || integer_all_onesp (@2))
4323      (cmp @0 @2)))))
4325 /* Narrow a lshift by constant.  */
4326 (simplify
4327  (convert (lshift:s@0 @1 INTEGER_CST@2))
4328  (if (INTEGRAL_TYPE_P (type)
4329       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4330       && !integer_zerop (@2)
4331       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4332   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4333        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4334    (lshift (convert @1) @2)
4335    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4336     { build_zero_cst (type); }))))
4338 /* Simplifications of conversions.  */
4340 /* Basic strip-useless-type-conversions / strip_nops.  */
4341 (for cvt (convert view_convert float fix_trunc)
4342  (simplify
4343   (cvt @0)
4344   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4345        || (GENERIC && type == TREE_TYPE (@0)))
4346    @0)))
4348 /* Contract view-conversions.  */
4349 (simplify
4350   (view_convert (view_convert @0))
4351   (view_convert @0))
4353 /* For integral conversions with the same precision or pointer
4354    conversions use a NOP_EXPR instead.  */
4355 (simplify
4356   (view_convert @0)
4357   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4358        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4359        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4360    (convert @0)))
4362 /* Strip inner integral conversions that do not change precision or size, or
4363    zero-extend while keeping the same size (for bool-to-char).  */
4364 (simplify
4365   (view_convert (convert@0 @1))
4366   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4367        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4368        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4369        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4370            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4371                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4372    (view_convert @1)))
4374 /* Simplify a view-converted empty or single-element constructor.  */
4375 (simplify
4376   (view_convert CONSTRUCTOR@0)
4377   (with
4378    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4379                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4380    (switch
4381     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4382      { build_zero_cst (type); })
4383     (if (CONSTRUCTOR_NELTS (ctor) == 1
4384          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4385          && operand_equal_p (TYPE_SIZE (type),
4386                              TYPE_SIZE (TREE_TYPE
4387                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4388      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4390 /* Re-association barriers around constants and other re-association
4391    barriers can be removed.  */
4392 (simplify
4393  (paren CONSTANT_CLASS_P@0)
4394  @0)
4395 (simplify
4396  (paren (paren@1 @0))
4397  @1)
4399 /* Handle cases of two conversions in a row.  */
4400 (for ocvt (convert float fix_trunc)
4401  (for icvt (convert float)
4402   (simplify
4403    (ocvt (icvt@1 @0))
4404    (with
4405     {
4406       tree inside_type = TREE_TYPE (@0);
4407       tree inter_type = TREE_TYPE (@1);
4408       int inside_int = INTEGRAL_TYPE_P (inside_type);
4409       int inside_ptr = POINTER_TYPE_P (inside_type);
4410       int inside_float = FLOAT_TYPE_P (inside_type);
4411       int inside_vec = VECTOR_TYPE_P (inside_type);
4412       unsigned int inside_prec = element_precision (inside_type);
4413       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4414       int inter_int = INTEGRAL_TYPE_P (inter_type);
4415       int inter_ptr = POINTER_TYPE_P (inter_type);
4416       int inter_float = FLOAT_TYPE_P (inter_type);
4417       int inter_vec = VECTOR_TYPE_P (inter_type);
4418       unsigned int inter_prec = element_precision (inter_type);
4419       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4420       int final_int = INTEGRAL_TYPE_P (type);
4421       int final_ptr = POINTER_TYPE_P (type);
4422       int final_float = FLOAT_TYPE_P (type);
4423       int final_vec = VECTOR_TYPE_P (type);
4424       unsigned int final_prec = element_precision (type);
4425       int final_unsignedp = TYPE_UNSIGNED (type);
4426     }
4427    (switch
4428     /* In addition to the cases of two conversions in a row
4429        handled below, if we are converting something to its own
4430        type via an object of identical or wider precision, neither
4431        conversion is needed.  */
4432     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4433           || (GENERIC
4434               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4435          && (((inter_int || inter_ptr) && final_int)
4436              || (inter_float && final_float))
4437          && inter_prec >= final_prec)
4438      (ocvt @0))
4440     /* Likewise, if the intermediate and initial types are either both
4441        float or both integer, we don't need the middle conversion if the
4442        former is wider than the latter and doesn't change the signedness
4443        (for integers).  Avoid this if the final type is a pointer since
4444        then we sometimes need the middle conversion.  */
4445     (if (((inter_int && inside_int) || (inter_float && inside_float))
4446          && (final_int || final_float)
4447          && inter_prec >= inside_prec
4448          && (inter_float || inter_unsignedp == inside_unsignedp))
4449      (ocvt @0))
4451     /* If we have a sign-extension of a zero-extended value, we can
4452        replace that by a single zero-extension.  Likewise if the
4453        final conversion does not change precision we can drop the
4454        intermediate conversion.  */
4455     (if (inside_int && inter_int && final_int
4456          && ((inside_prec < inter_prec && inter_prec < final_prec
4457               && inside_unsignedp && !inter_unsignedp)
4458              || final_prec == inter_prec))
4459      (ocvt @0))
4461     /* Two conversions in a row are not needed unless:
4462         - some conversion is floating-point (overstrict for now), or
4463         - some conversion is a vector (overstrict for now), or
4464         - the intermediate type is narrower than both initial and
4465           final, or
4466         - the intermediate type and innermost type differ in signedness,
4467           and the outermost type is wider than the intermediate, or
4468         - the initial type is a pointer type and the precisions of the
4469           intermediate and final types differ, or
4470         - the final type is a pointer type and the precisions of the
4471           initial and intermediate types differ.  */
4472     (if (! inside_float && ! inter_float && ! final_float
4473          && ! inside_vec && ! inter_vec && ! final_vec
4474          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4475          && ! (inside_int && inter_int
4476                && inter_unsignedp != inside_unsignedp
4477                && inter_prec < final_prec)
4478          && ((inter_unsignedp && inter_prec > inside_prec)
4479              == (final_unsignedp && final_prec > inter_prec))
4480          && ! (inside_ptr && inter_prec != final_prec)
4481          && ! (final_ptr && inside_prec != inter_prec))
4482      (ocvt @0))
4484    /* `(outer:M)(inter:N) a:O`
4485       can be converted to `(outer:M) a`
4486       if M <= O && N >= O. No matter what signedness of the casts,
4487       as the final is either a truncation from the original or just
4488       a sign change of the type. */
4489    (if (inside_int && inter_int && final_int
4490         && final_prec <= inside_prec
4491         && inter_prec >= inside_prec)
4492     (convert @0))
4494     /* A truncation to an unsigned type (a zero-extension) should be
4495        canonicalized as bitwise and of a mask.  */
4496     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4497          && final_int && inter_int && inside_int
4498          && final_prec == inside_prec
4499          && final_prec > inter_prec
4500          && inter_unsignedp)
4501      (convert (bit_and @0 { wide_int_to_tree
4502                               (inside_type,
4503                                wi::mask (inter_prec, false,
4504                                          TYPE_PRECISION (inside_type))); })))
4506     /* If we are converting an integer to a floating-point that can
4507        represent it exactly and back to an integer, we can skip the
4508        floating-point conversion.  */
4509     (if (GIMPLE /* PR66211 */
4510          && inside_int && inter_float && final_int &&
4511          (unsigned) significand_size (TYPE_MODE (inter_type))
4512          >= inside_prec - !inside_unsignedp)
4513      (convert @0)))))))
4515 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4516    float_type.  Only do the transformation if we do not need to preserve
4517    trapping behaviour, so require !flag_trapping_math. */
4518 #if GIMPLE
4519 (simplify
4520    (float (fix_trunc @0))
4521    (if (!flag_trapping_math
4522         && types_match (type, TREE_TYPE (@0))
4523         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4524                                           OPTIMIZE_FOR_BOTH))
4525       (IFN_TRUNC @0)))
4526 #endif
4528 /* If we have a narrowing conversion to an integral type that is fed by a
4529    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4530    masks off bits outside the final type (and nothing else).  */
4531 (simplify
4532   (convert (bit_and @0 INTEGER_CST@1))
4533   (if (INTEGRAL_TYPE_P (type)
4534        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4535        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4536        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4537                                                     TYPE_PRECISION (type)), 0))
4538    (convert @0)))
4541 /* (X /[ex] A) * A -> X.  */
4542 (simplify
4543   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4544   (convert @0))
4546 /* Simplify (A / B) * B + (A % B) -> A.  */
4547 (for div (trunc_div ceil_div floor_div round_div)
4548      mod (trunc_mod ceil_mod floor_mod round_mod)
4549   (simplify
4550    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4551    @0))
4553 /* x / y * y == x -> x % y == 0.  */
4554 (simplify
4555   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4556   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4557     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4559 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4560 (for op (plus minus)
4561  (simplify
4562   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4563   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4564        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4565    (with
4566      {
4567        wi::overflow_type overflow;
4568        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4569                                TYPE_SIGN (type), &overflow);
4570      }
4571      (if (types_match (type, TREE_TYPE (@2))
4572          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4573       (op @0 { wide_int_to_tree (type, mul); })
4574       (with { tree utype = unsigned_type_for (type); }
4575        (convert (op (convert:utype @0)
4576                     (mult (convert:utype @1) (convert:utype @2))))))))))
4578 /* Canonicalization of binary operations.  */
4580 /* Convert X + -C into X - C.  */
4581 (simplify
4582  (plus @0 REAL_CST@1)
4583  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4584   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4585    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4586     (minus @0 { tem; })))))
4588 /* Convert x+x into x*2.  */
4589 (simplify
4590  (plus @0 @0)
4591  (if (SCALAR_FLOAT_TYPE_P (type))
4592   (mult @0 { build_real (type, dconst2); })
4593   (if (INTEGRAL_TYPE_P (type))
4594    (mult @0 { build_int_cst (type, 2); }))))
4596 /* 0 - X  ->  -X.  */
4597 (simplify
4598  (minus integer_zerop @1)
4599  (negate @1))
4600 (simplify
4601  (pointer_diff integer_zerop @1)
4602  (negate (convert @1)))
4604 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4605    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4606    (-ARG1 + ARG0) reduces to -ARG1.  */
4607 (simplify
4608  (minus real_zerop@0 @1)
4609  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4610   (negate @1)))
4612 /* Transform x * -1 into -x.  */
4613 (simplify
4614  (mult @0 integer_minus_onep)
4615  (negate @0))
4617 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4618    signed overflow for CST != 0 && CST != -1.  */
4619 (simplify
4620  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4621  (if (TREE_CODE (@2) != INTEGER_CST
4622       && single_use (@3)
4623       && !integer_zerop (@1) && !integer_minus_onep (@1))
4624   (mult (mult @0 @2) @1)))
4626 /* True if we can easily extract the real and imaginary parts of a complex
4627    number.  */
4628 (match compositional_complex
4629  (convert? (complex @0 @1)))
4631 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4632 (simplify
4633  (complex (realpart @0) (imagpart @0))
4634  @0)
4635 (simplify
4636  (realpart (complex @0 @1))
4637  @0)
4638 (simplify
4639  (imagpart (complex @0 @1))
4640  @1)
4642 /* Sometimes we only care about half of a complex expression.  */
4643 (simplify
4644  (realpart (convert?:s (conj:s @0)))
4645  (convert (realpart @0)))
4646 (simplify
4647  (imagpart (convert?:s (conj:s @0)))
4648  (convert (negate (imagpart @0))))
4649 (for part (realpart imagpart)
4650  (for op (plus minus)
4651   (simplify
4652    (part (convert?:s@2 (op:s @0 @1)))
4653    (convert (op (part @0) (part @1))))))
4654 (simplify
4655  (realpart (convert?:s (CEXPI:s @0)))
4656  (convert (COS @0)))
4657 (simplify
4658  (imagpart (convert?:s (CEXPI:s @0)))
4659  (convert (SIN @0)))
4661 /* conj(conj(x)) -> x  */
4662 (simplify
4663  (conj (convert? (conj @0)))
4664  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4665   (convert @0)))
4667 /* conj({x,y}) -> {x,-y}  */
4668 (simplify
4669  (conj (convert?:s (complex:s @0 @1)))
4670  (with { tree itype = TREE_TYPE (type); }
4671   (complex (convert:itype @0) (negate (convert:itype @1)))))
4673 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4674 (for bswap (BSWAP)
4675  (simplify
4676   (bswap (bswap @0))
4677   @0)
4678  (simplify
4679   (bswap (bit_not (bswap @0)))
4680   (bit_not @0))
4681  (for bitop (bit_xor bit_ior bit_and)
4682   (simplify
4683    (bswap (bitop:c (bswap @0) @1))
4684    (bitop @0 (bswap @1))))
4685  (for cmp (eq ne)
4686   (simplify
4687    (cmp (bswap@2 @0) (bswap @1))
4688    (with { tree ctype = TREE_TYPE (@2); }
4689     (cmp (convert:ctype @0) (convert:ctype @1))))
4690   (simplify
4691    (cmp (bswap @0) INTEGER_CST@1)
4692    (with { tree ctype = TREE_TYPE (@1); }
4693     (cmp (convert:ctype @0) (bswap! @1)))))
4694  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4695  (simplify
4696   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4697            INTEGER_CST@3)
4698    (if (BITS_PER_UNIT == 8
4699         && tree_fits_uhwi_p (@2)
4700         && tree_fits_uhwi_p (@3))
4701     (with
4702      {
4703       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4704       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4705       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4706       unsigned HOST_WIDE_INT lo = bits & 7;
4707       unsigned HOST_WIDE_INT hi = bits - lo;
4708      }
4709      (if (bits < prec
4710           && mask < (256u>>lo)
4711           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4712       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4713        (if (ns == 0)
4714         (bit_and (convert @1) @3)
4715         (with
4716          {
4717           tree utype = unsigned_type_for (TREE_TYPE (@1));
4718           tree nst = build_int_cst (integer_type_node, ns);
4719          }
4720          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4721  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4722  (simplify
4723   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4724    (if (BITS_PER_UNIT == 8
4725         && CHAR_TYPE_SIZE == 8
4726         && tree_fits_uhwi_p (@1))
4727     (with
4728      {
4729       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4730       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4731       /* If the bswap was extended before the original shift, this
4732          byte (shift) has the sign of the extension, not the sign of
4733          the original shift.  */
4734       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4735      }
4736      /* Special case: logical right shift of sign-extended bswap.
4737         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4738      (if (TYPE_PRECISION (type) > prec
4739           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4740           && TYPE_UNSIGNED (type)
4741           && bits < prec && bits + 8 >= prec)
4742       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4743        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4744       (if (bits + 8 == prec)
4745        (if (TYPE_UNSIGNED (st))
4746         (convert (convert:unsigned_char_type_node @0))
4747         (convert (convert:signed_char_type_node @0)))
4748        (if (bits < prec && bits + 8 > prec)
4749         (with 
4750          {
4751           tree nst = build_int_cst (integer_type_node, bits & 7);
4752           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4753                                        : signed_char_type_node;
4754          }
4755          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4756  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4757  (simplify
4758   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4759    (if (BITS_PER_UNIT == 8
4760         && tree_fits_uhwi_p (@1)
4761         && tree_to_uhwi (@1) < 256)
4762     (with
4763      {
4764       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4765       tree utype = unsigned_type_for (TREE_TYPE (@0));
4766       tree nst = build_int_cst (integer_type_node, prec - 8);
4767      }
4768      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4771 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4773 /* Simplify constant conditions.
4774    Only optimize constant conditions when the selected branch
4775    has the same type as the COND_EXPR.  This avoids optimizing
4776    away "c ? x : throw", where the throw has a void type.
4777    Note that we cannot throw away the fold-const.cc variant nor
4778    this one as we depend on doing this transform before possibly
4779    A ? B : B -> B triggers and the fold-const.cc one can optimize
4780    0 ? A : B to B even if A has side-effects.  Something
4781    genmatch cannot handle.  */
4782 (simplify
4783  (cond INTEGER_CST@0 @1 @2)
4784  (if (integer_zerop (@0))
4785   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4786    @2)
4787   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4788    @1)))
4789 (simplify
4790  (vec_cond VECTOR_CST@0 @1 @2)
4791  (if (integer_all_onesp (@0))
4792   @1
4793   (if (integer_zerop (@0))
4794    @2)))
4796 /* Sink unary operations to branches, but only if we do fold both.  */
4797 (for op (negate bit_not abs absu)
4798  (simplify
4799   (op (vec_cond:s @0 @1 @2))
4800   (vec_cond @0 (op! @1) (op! @2))))
4802 /* Sink unary conversions to branches, but only if we do fold both
4803    and the target's truth type is the same as we already have.  */
4804 (simplify
4805  (convert (vec_cond:s @0 @1 @2))
4806  (if (VECTOR_TYPE_P (type)
4807       && types_match (TREE_TYPE (@0), truth_type_for (type)))
4808   (vec_cond @0 (convert! @1) (convert! @2))))
4810 /* Likewise for view_convert of nop_conversions. */
4811 (simplify
4812  (view_convert (vec_cond:s @0 @1 @2))
4813  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4814       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4815                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4816       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4817   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4819 /* Sink binary operation to branches, but only if we can fold it.  */
4820 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4821          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4822          trunc_mod ceil_mod floor_mod round_mod min max)
4823 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4824  (simplify
4825   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4826   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4828 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4829  (simplify
4830   (op (vec_cond:s @0 @1 @2) @3)
4831   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4832  (simplify
4833   (op @3 (vec_cond:s @0 @1 @2))
4834   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4836 #if GIMPLE
4837 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4838  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4839            INTEGER_CST@1)
4840  (with {
4841          int ibit = tree_log2 (@0);
4842          int ibit2 = tree_log2 (@1);
4843        }
4844   (if (ibit == ibit2
4845       && ibit >= 0
4846       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4848 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4849  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4850           INTEGER_CST@1)
4851  (with {
4852          int ibit = tree_log2 (@0);
4853          int ibit2 = tree_log2 (@1);
4854        }
4855   (if (ibit == ibit2
4856       && ibit >= 0
4857       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4859 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4860  (bit_and:c
4861   (convert1?@4
4862    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4863   (convert2? @0))
4864  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4866 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4867  (bit_and:c
4868   (convert1?@4
4869    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4870   (convert2? @0))
4871  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4873 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4874  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4875             INTEGER_CST@1)
4876  (with {
4877          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4878                                               TYPE_PRECISION(type)));
4879          int ibit2 = tree_log2 (@1);
4880        }
4881   (if (ibit == ibit2
4882       && ibit >= 0
4883       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4885 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4886  (bit_and@4
4887   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4888   INTEGER_CST@1)
4889  (with {
4890          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4891                                               TYPE_PRECISION(type)));
4892          int ibit2 = tree_log2 (@1);
4893        }
4894   (if (ibit == ibit2
4895       && ibit >= 0
4896       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4898 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4899  (bit_and:c
4900   (convert1?@3
4901    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4902   (convert2? @0))
4903  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4905 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4906  (bit_and:c
4907   (convert1?@3
4908    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4909   (convert2? @0))
4910   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4912 #endif
4914 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4915    Currently disabled after pass lvec because ARM understands
4916    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
4917 (simplify
4918  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4919  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4920   (vec_cond (bit_and @0 @3) @1 @2)))
4921 (simplify
4922  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4923  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4924   (vec_cond (bit_ior @0 @3) @1 @2)))
4925 (simplify
4926  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4927  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4928   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4929 (simplify
4930  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4931  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4932   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4934 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
4935 (simplify
4936  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4937  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4938   (vec_cond (bit_and @0 @1) @2 @3)))
4939 (simplify
4940  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4941  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4942   (vec_cond (bit_ior @0 @1) @2 @3)))
4943 (simplify
4944  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4945  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4946   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4947 (simplify
4948  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4949  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4950   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4952 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4953    types are compatible.  */
4954 (simplify
4955  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4956  (if (VECTOR_BOOLEAN_TYPE_P (type)
4957       && types_match (type, TREE_TYPE (@0)))
4958   (if (integer_zerop (@1) && integer_all_onesp (@2))
4959    (bit_not @0)
4960    (if (integer_all_onesp (@1) && integer_zerop (@2))
4961     @0))))
4963 /* A few simplifications of "a ? CST1 : CST2". */
4964 /* NOTE: Only do this on gimple as the if-chain-to-switch
4965    optimization depends on the gimple to have if statements in it. */
4966 #if GIMPLE
4967 (simplify
4968  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4969  (switch
4970   (if (integer_zerop (@2))
4971    (switch
4972     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4973     (if (integer_onep (@1))
4974      (convert (convert:boolean_type_node @0)))
4975     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4976     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4977      (with {
4978        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4979       }
4980       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4981     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
4982        here as the powerof2cst case above will handle that case correctly.  */
4983     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4984      (with {
4985        auto prec = TYPE_PRECISION (type);
4986        auto unsign = TYPE_UNSIGNED (type);
4987        tree inttype = build_nonstandard_integer_type (prec, unsign);
4988       }
4989       (convert (negate (convert:inttype (convert:boolean_type_node @0))))))))
4990   (if (integer_zerop (@1))
4991    (with {
4992       tree booltrue = constant_boolean_node (true, boolean_type_node);
4993     }
4994     (switch
4995      /* a ? 0 : 1 -> !a. */
4996      (if (integer_onep (@2))
4997       (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4998      /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4999      (if (INTEGRAL_TYPE_P (type) &&  integer_pow2p (@2))
5000       (with {
5001         tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5002        }
5003        (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
5004         { shift; })))
5005      /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
5006        here as the powerof2cst case above will handle that case correctly.  */
5007      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5008       (with {
5009         auto prec = TYPE_PRECISION (type);
5010         auto unsign = TYPE_UNSIGNED (type);
5011         tree inttype = build_nonstandard_integer_type (prec, unsign);
5012        }
5013        (convert
5014         (negate
5015          (convert:inttype
5016           (bit_xor (convert:boolean_type_node @0) { booltrue; } )
5017          )
5018         )
5019        )
5020       )
5021      )
5022     )
5023    )
5024   )
5028 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5029    for unsigned types. */
5030 (simplify
5031  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5032  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5033       && bitwise_equal_p (@0, @2))
5034   (convert (eq @0 @1))
5038 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5039    for unsigned types. */
5040 (simplify
5041  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5042  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5043       && bitwise_equal_p (@0, @2))
5044   (convert (eq @0 @1))
5048 /* Optimize
5049    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5050    x_5 ? cstN ? cst4 : cst3
5051    # op is == or != and N is 1 or 2
5052    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5053    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5054    of cst3 and cst4 is smaller.
5055    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5056 (for eqne (ne eq)
5057  (simplify
5058   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5059   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5060        && INTEGRAL_TYPE_P (type)
5061        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5062            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5063    (with {
5064      value_range r;
5065      get_range_query (cfun)->range_of_expr (r, @0);
5066      if (r.undefined_p ())
5067        r.set_varying (TREE_TYPE (@0));
5069      wide_int min = r.lower_bound ();
5070      wide_int max = r.upper_bound ();
5071     }
5072     (if (min + 1 == max
5073          && (wi::to_wide (@1) == min
5074              || wi::to_wide (@1) == max))
5075      (with {
5076        tree arg0 = @2, arg1 = @3;
5077        tree type1;
5078        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5079          std::swap (arg0, arg1);
5080        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5081          type1 = TREE_TYPE (@0);
5082        else
5083          type1 = type;
5084        auto prec = TYPE_PRECISION (type1);
5085        auto unsign = TYPE_UNSIGNED (type1);
5086        type1 = build_nonstandard_integer_type (prec, unsign);
5087        min = wide_int::from (min, prec,
5088                              TYPE_SIGN (TREE_TYPE (@0)));
5089        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5090                                     TYPE_SIGN (type));
5091        enum tree_code code;
5092        wi::overflow_type ovf;
5093        if (tree_int_cst_lt (arg0, arg1))
5094          {
5095            code = PLUS_EXPR;
5096            a -= min;
5097            if (!unsign)
5098              {
5099                /* lhs is known to be in range [min, min+1] and we want to add a
5100                   to it.  Check if that operation can overflow for those 2 values
5101                   and if yes, force unsigned type.  */
5102                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5103                if (ovf)
5104                  type1 = unsigned_type_for (type1);
5105              }
5106          }
5107        else
5108          {
5109            code = MINUS_EXPR;
5110            a += min;
5111            if (!unsign)
5112              {
5113                /* lhs is known to be in range [min, min+1] and we want to subtract
5114                   it from a.  Check if that operation can overflow for those 2
5115                   values and if yes, force unsigned type.  */
5116                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5117                if (ovf)
5118                 type1 = unsigned_type_for (type1);
5119              }
5120          }
5121        tree arg = wide_int_to_tree (type1, a);
5122       }
5123       (if (code == PLUS_EXPR)
5124        (convert (plus (convert:type1 @0) { arg; }))
5125        (convert (minus { arg; } (convert:type1 @0)))
5126       )
5127      )
5128     )
5129    )
5130   )
5133 #endif
5135 (simplify
5136  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5137  (if (INTEGRAL_TYPE_P (type)
5138       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5139   (cond @1 (convert @2) (convert @3))))
5141 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5142    be extended.  */
5143 /* This pattern implements two kinds simplification:
5145    Case 1)
5146    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5147      1) Conversions are type widening from smaller type.
5148      2) Const c1 equals to c2 after canonicalizing comparison.
5149      3) Comparison has tree code LT, LE, GT or GE.
5150    This specific pattern is needed when (cmp (convert x) c) may not
5151    be simplified by comparison patterns because of multiple uses of
5152    x.  It also makes sense here because simplifying across multiple
5153    referred var is always benefitial for complicated cases.
5155    Case 2)
5156    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5157 (for cmp (lt le gt ge eq ne)
5158  (simplify
5159   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5160   (with
5161    {
5162      tree from_type = TREE_TYPE (@1);
5163      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5164      enum tree_code code = ERROR_MARK;
5166      if (INTEGRAL_TYPE_P (from_type)
5167          && int_fits_type_p (@2, from_type)
5168          && (types_match (c1_type, from_type)
5169              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5170                  && (TYPE_UNSIGNED (from_type)
5171                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5172          && (types_match (c2_type, from_type)
5173              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5174                  && (TYPE_UNSIGNED (from_type)
5175                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5176        {
5177          if (cmp != EQ_EXPR)
5178            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5179          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5180          else if (int_fits_type_p (@3, from_type))
5181            code = EQ_EXPR;
5182        }
5183    }
5184    (if (code == MAX_EXPR)
5185     (convert (max @1 (convert @2)))
5186     (if (code == MIN_EXPR)
5187      (convert (min @1 (convert @2)))
5188      (if (code == EQ_EXPR)
5189       (convert (cond (eq @1 (convert @3))
5190                      (convert:from_type @3) (convert:from_type @2)))))))))
5192 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5194      1) OP is PLUS or MINUS.
5195      2) CMP is LT, LE, GT or GE.
5196      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5198    This pattern also handles special cases like:
5200      A) Operand x is a unsigned to signed type conversion and c1 is
5201         integer zero.  In this case,
5202           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5203           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5204      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5205         check equality for (c1+1) and (c1-1) by adjusting comparison
5206         code.
5208    TODO: Though signed type is handled by this pattern, it cannot be
5209    simplified at the moment because C standard requires additional
5210    type promotion.  In order to match&simplify it here, the IR needs
5211    to be cleaned up by other optimizers, i.e, VRP.  */
5212 (for op (plus minus)
5213  (for cmp (lt le gt ge)
5214   (simplify
5215    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5216    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5217     (if (types_match (from_type, to_type)
5218          /* Check if it is special case A).  */
5219          || (TYPE_UNSIGNED (from_type)
5220              && !TYPE_UNSIGNED (to_type)
5221              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5222              && integer_zerop (@1)
5223              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5224      (with
5225       {
5226         wi::overflow_type overflow = wi::OVF_NONE;
5227         enum tree_code code, cmp_code = cmp;
5228         wide_int real_c1;
5229         wide_int c1 = wi::to_wide (@1);
5230         wide_int c2 = wi::to_wide (@2);
5231         wide_int c3 = wi::to_wide (@3);
5232         signop sgn = TYPE_SIGN (from_type);
5234         /* Handle special case A), given x of unsigned type:
5235             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5236             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5237         if (!types_match (from_type, to_type))
5238           {
5239             if (cmp_code == LT_EXPR)
5240               cmp_code = GT_EXPR;
5241             if (cmp_code == GE_EXPR)
5242               cmp_code = LE_EXPR;
5243             c1 = wi::max_value (to_type);
5244           }
5245         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5246            compute (c3 op' c2) and check if it equals to c1 with op' being
5247            the inverted operator of op.  Make sure overflow doesn't happen
5248            if it is undefined.  */
5249         if (op == PLUS_EXPR)
5250           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5251         else
5252           real_c1 = wi::add (c3, c2, sgn, &overflow);
5254         code = cmp_code;
5255         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5256           {
5257             /* Check if c1 equals to real_c1.  Boundary condition is handled
5258                by adjusting comparison operation if necessary.  */
5259             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5260                 && !overflow)
5261               {
5262                 /* X <= Y - 1 equals to X < Y.  */
5263                 if (cmp_code == LE_EXPR)
5264                   code = LT_EXPR;
5265                 /* X > Y - 1 equals to X >= Y.  */
5266                 if (cmp_code == GT_EXPR)
5267                   code = GE_EXPR;
5268               }
5269             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5270                 && !overflow)
5271               {
5272                 /* X < Y + 1 equals to X <= Y.  */
5273                 if (cmp_code == LT_EXPR)
5274                   code = LE_EXPR;
5275                 /* X >= Y + 1 equals to X > Y.  */
5276                 if (cmp_code == GE_EXPR)
5277                   code = GT_EXPR;
5278               }
5279             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5280               {
5281                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5282                   code = MIN_EXPR;
5283                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5284                   code = MAX_EXPR;
5285               }
5286           }
5287       }
5288       (if (code == MAX_EXPR)
5289        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5290            { wide_int_to_tree (from_type, c2); })
5291        (if (code == MIN_EXPR)
5292         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5293             { wide_int_to_tree (from_type, c2); })))))))))
5295 #if GIMPLE
5296 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5297    in fold_cond_expr_with_comparison for GENERIC folding with
5298    some extra constraints.  */
5299 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5300  (simplify
5301   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5302         (convert3? @0) (convert4? @1))
5303   (if (!HONOR_SIGNED_ZEROS (type)
5304        && (/* Allow widening conversions of the compare operands as data.  */
5305            (INTEGRAL_TYPE_P (type)
5306             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5307             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5308             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5309             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5310            /* Or sign conversions for the comparison.  */
5311            || (types_match (type, TREE_TYPE (@0))
5312                && types_match (type, TREE_TYPE (@1)))))
5313    (switch
5314     (if (cmp == EQ_EXPR)
5315      (if (VECTOR_TYPE_P (type))
5316       (view_convert @c1)
5317       (convert @c1)))
5318     (if (cmp == NE_EXPR)
5319      (if (VECTOR_TYPE_P (type))
5320       (view_convert @c0)
5321       (convert @c0)))
5322     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5323      (if (!HONOR_NANS (type))
5324       (if (VECTOR_TYPE_P (type))
5325        (view_convert (min @c0 @c1))
5326        (convert (min @c0 @c1)))))
5327     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5328      (if (!HONOR_NANS (type))
5329       (if (VECTOR_TYPE_P (type))
5330        (view_convert (max @c0 @c1))
5331        (convert (max @c0 @c1)))))
5332     (if (cmp == UNEQ_EXPR)
5333      (if (!HONOR_NANS (type))
5334       (if (VECTOR_TYPE_P (type))
5335        (view_convert @c1)
5336        (convert @c1))))
5337     (if (cmp == LTGT_EXPR)
5338      (if (!HONOR_NANS (type))
5339       (if (VECTOR_TYPE_P (type))
5340        (view_convert @c0)
5341        (convert @c0))))))))
5342 #endif
5344 (for cnd (cond vec_cond)
5345  /* (a != b) ? (a - b) : 0 -> (a - b) */
5346  (simplify
5347   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5348   @2)
5349  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5350  (simplify
5351   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5352   @2)
5353  /* (a != b) ? (a & b) : a -> (a & b) */
5354  /* (a != b) ? (a | b) : a -> (a | b) */
5355  /* (a != b) ? min(a,b) : a -> min(a,b) */
5356  /* (a != b) ? max(a,b) : a -> max(a,b) */
5357  (for op (bit_and bit_ior min max)
5358   (simplify
5359    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5360    @2))
5361  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5362  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5363  (for op (mult plus)
5364   (simplify
5365    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5366    (if (ANY_INTEGRAL_TYPE_P (type))
5367     @2)))
5368  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5369  (simplify
5370   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5371   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5372    @2))
5375 /* These was part of minmax phiopt.  */
5376 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5377    to minmax<min/max<a, b>, c> */
5378 (for minmax (min max)
5379  (for cmp (lt le gt ge ne)
5380   (simplify
5381    (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5382    (with
5383     {
5384       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5385     }
5386     (if (code == MIN_EXPR)
5387      (minmax (min @1 @2) @4)
5388      (if (code == MAX_EXPR)
5389       (minmax (max @1 @2) @4)))))))
5391 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5392 (for cmp    (gt  ge  lt  le)
5393      minmax (min min max max)
5394  (simplify
5395   (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4)
5396    (with
5397     {
5398       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5399     }
5400     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5401          && code == MIN_EXPR
5402          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @1)))
5403      (min @2 @4)
5404      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5405           && code == MAX_EXPR
5406           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1)))
5407       (max @2 @4))))))
5409 #if GIMPLE
5410 /* These patterns should be after min/max detection as simplifications
5411    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5412    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5413    Even without those, reaching min/max/and/ior faster is better.  */
5414 (simplify
5415  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5416  (switch
5417   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5418   (if (integer_zerop (@2))
5419    (bit_and (convert @0) @1))
5420   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5421   (if (integer_zerop (@1))
5422    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5423   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5424   (if (integer_onep (@1))
5425    (bit_ior (convert @0) @2))
5426   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5427   (if (integer_onep (@2))
5428    (bit_ior (bit_xor (convert @0) @2) @1))
5431 #endif
5433 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5434 (simplify
5435  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5436  (if (!TYPE_SATURATING (type)
5437       && (TYPE_OVERFLOW_WRAPS (type)
5438           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5439       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5440   @3))
5442 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5443 (simplify
5444  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5445  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5446   @3))
5448 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5449    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5450 (simplify
5451  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5452  (if (TYPE_UNSIGNED (type))
5453   (cond (ge @0 @1) (negate @0) @2)))
5455 (for cnd (cond vec_cond)
5456  /* A ? B : (A ? X : C) -> A ? B : C.  */
5457  (simplify
5458   (cnd @0 (cnd @0 @1 @2) @3)
5459   (cnd @0 @1 @3))
5460  (simplify
5461   (cnd @0 @1 (cnd @0 @2 @3))
5462   (cnd @0 @1 @3))
5463  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5464  /* ???  This matches embedded conditions open-coded because genmatch
5465     would generate matching code for conditions in separate stmts only.
5466     The following is still important to merge then and else arm cases
5467     from if-conversion.  */
5468  (simplify
5469   (cnd @0 @1 (cnd @2 @3 @4))
5470   (if (inverse_conditions_p (@0, @2))
5471    (cnd @0 @1 @3)))
5472  (simplify
5473   (cnd @0 (cnd @1 @2 @3) @4)
5474   (if (inverse_conditions_p (@0, @1))
5475    (cnd @0 @3 @4)))
5477  /* A ? B : B -> B.  */
5478  (simplify
5479   (cnd @0 @1 @1)
5480   @1)
5482  /* !A ? B : C -> A ? C : B.  */
5483  (simplify
5484   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5485   (cnd @0 @2 @1)))
5487 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5488    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5489    Need to handle UN* comparisons.
5491    None of these transformations work for modes with signed
5492    zeros.  If A is +/-0, the first two transformations will
5493    change the sign of the result (from +0 to -0, or vice
5494    versa).  The last four will fix the sign of the result,
5495    even though the original expressions could be positive or
5496    negative, depending on the sign of A.
5498    Note that all these transformations are correct if A is
5499    NaN, since the two alternatives (A and -A) are also NaNs.  */
5501 (for cnd (cond vec_cond)
5502  /* A == 0 ? A : -A    same as -A */
5503  (for cmp (eq uneq)
5504   (simplify
5505    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5506     (if (!HONOR_SIGNED_ZEROS (type))
5507      @1))
5508   (simplify
5509    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5510     (if (!HONOR_SIGNED_ZEROS (type))
5511      @1))
5513  /* A != 0 ? A : -A    same as A */
5514  (for cmp (ne ltgt)
5515   (simplify
5516    (cnd (cmp @0 zerop) @0 (negate @0))
5517     (if (!HONOR_SIGNED_ZEROS (type))
5518      @0))
5519   (simplify
5520    (cnd (cmp @0 zerop) @0 integer_zerop)
5521     (if (!HONOR_SIGNED_ZEROS (type))
5522      @0))
5524  /* A >=/> 0 ? A : -A    same as abs (A) */
5525  (for cmp (ge gt)
5526   (simplify
5527    (cnd (cmp @0 zerop) @0 (negate @0))
5528     (if (!HONOR_SIGNED_ZEROS (type)
5529          && !TYPE_UNSIGNED (type))
5530      (abs @0))))
5531  /* A <=/< 0 ? A : -A    same as -abs (A) */
5532  (for cmp (le lt)
5533   (simplify
5534    (cnd (cmp @0 zerop) @0 (negate @0))
5535     (if (!HONOR_SIGNED_ZEROS (type)
5536          && !TYPE_UNSIGNED (type))
5537      (if (ANY_INTEGRAL_TYPE_P (type)
5538           && !TYPE_OVERFLOW_WRAPS (type))
5539       (with {
5540         tree utype = unsigned_type_for (type);
5541        }
5542        (convert (negate (absu:utype @0))))
5543        (negate (abs @0)))))
5547 /* -(type)!A -> (type)A - 1.  */
5548 (simplify
5549  (negate (convert?:s (logical_inverted_value:s @0)))
5550  (if (INTEGRAL_TYPE_P (type)
5551       && TREE_CODE (type) != BOOLEAN_TYPE
5552       && TYPE_PRECISION (type) > 1
5553       && TREE_CODE (@0) == SSA_NAME
5554       && ssa_name_has_boolean_range (@0))
5555   (plus (convert:type @0) { build_all_ones_cst (type); })))
5557 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5558    return all -1 or all 0 results.  */
5559 /* ??? We could instead convert all instances of the vec_cond to negate,
5560    but that isn't necessarily a win on its own.  */
5561 (simplify
5562  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5563  (if (VECTOR_TYPE_P (type)
5564       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5565                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5566       && (TYPE_MODE (TREE_TYPE (type))
5567           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5568   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5570 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5571 (simplify
5572  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5573  (if (VECTOR_TYPE_P (type)
5574       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5575                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5576       && (TYPE_MODE (TREE_TYPE (type))
5577           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5578   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5581 /* Simplifications of comparisons.  */
5583 /* See if we can reduce the magnitude of a constant involved in a
5584    comparison by changing the comparison code.  This is a canonicalization
5585    formerly done by maybe_canonicalize_comparison_1.  */
5586 (for cmp  (le gt)
5587      acmp (lt ge)
5588  (simplify
5589   (cmp @0 uniform_integer_cst_p@1)
5590   (with { tree cst = uniform_integer_cst_p (@1); }
5591    (if (tree_int_cst_sgn (cst) == -1)
5592      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5593                                    wide_int_to_tree (TREE_TYPE (cst),
5594                                                      wi::to_wide (cst)
5595                                                      + 1)); })))))
5596 (for cmp  (ge lt)
5597      acmp (gt le)
5598  (simplify
5599   (cmp @0 uniform_integer_cst_p@1)
5600   (with { tree cst = uniform_integer_cst_p (@1); }
5601    (if (tree_int_cst_sgn (cst) == 1)
5602     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5603                                   wide_int_to_tree (TREE_TYPE (cst),
5604                                   wi::to_wide (cst) - 1)); })))))
5606 /* We can simplify a logical negation of a comparison to the
5607    inverted comparison.  As we cannot compute an expression
5608    operator using invert_tree_comparison we have to simulate
5609    that with expression code iteration.  */
5610 (for cmp (tcc_comparison)
5611      icmp (inverted_tcc_comparison)
5612      ncmp (inverted_tcc_comparison_with_nans)
5613  /* Ideally we'd like to combine the following two patterns
5614     and handle some more cases by using
5615       (logical_inverted_value (cmp @0 @1))
5616     here but for that genmatch would need to "inline" that.
5617     For now implement what forward_propagate_comparison did.  */
5618  (simplify
5619   (bit_not (cmp @0 @1))
5620   (if (VECTOR_TYPE_P (type)
5621        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5622    /* Comparison inversion may be impossible for trapping math,
5623       invert_tree_comparison will tell us.  But we can't use
5624       a computed operator in the replacement tree thus we have
5625       to play the trick below.  */
5626    (with { enum tree_code ic = invert_tree_comparison
5627              (cmp, HONOR_NANS (@0)); }
5628     (if (ic == icmp)
5629      (icmp @0 @1)
5630      (if (ic == ncmp)
5631       (ncmp @0 @1))))))
5632  (simplify
5633   (bit_xor (cmp @0 @1) integer_truep)
5634   (with { enum tree_code ic = invert_tree_comparison
5635             (cmp, HONOR_NANS (@0)); }
5636    (if (ic == icmp)
5637     (icmp @0 @1)
5638     (if (ic == ncmp)
5639      (ncmp @0 @1)))))
5640  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5641  (simplify
5642   (ne (cmp@2 @0 @1) integer_zerop)
5643   (if (types_match (type, TREE_TYPE (@2)))
5644    (cmp @0 @1)))
5645  (simplify
5646   (eq (cmp@2 @0 @1) integer_truep)
5647   (if (types_match (type, TREE_TYPE (@2)))
5648    (cmp @0 @1)))
5649  (simplify
5650   (ne (cmp@2 @0 @1) integer_truep)
5651   (if (types_match (type, TREE_TYPE (@2)))
5652    (with { enum tree_code ic = invert_tree_comparison
5653              (cmp, HONOR_NANS (@0)); }
5654     (if (ic == icmp)
5655      (icmp @0 @1)
5656      (if (ic == ncmp)
5657       (ncmp @0 @1))))))
5658  (simplify
5659   (eq (cmp@2 @0 @1) integer_zerop)
5660   (if (types_match (type, TREE_TYPE (@2)))
5661    (with { enum tree_code ic = invert_tree_comparison
5662              (cmp, HONOR_NANS (@0)); }
5663     (if (ic == icmp)
5664      (icmp @0 @1)
5665      (if (ic == ncmp)
5666       (ncmp @0 @1)))))))
5668 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5669    ??? The transformation is valid for the other operators if overflow
5670    is undefined for the type, but performing it here badly interacts
5671    with the transformation in fold_cond_expr_with_comparison which
5672    attempts to synthetize ABS_EXPR.  */
5673 (for cmp (eq ne)
5674  (for sub (minus pointer_diff)
5675   (simplify
5676    (cmp (sub@2 @0 @1) integer_zerop)
5677    (if (single_use (@2))
5678     (cmp @0 @1)))))
5680 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5681    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5682 (for cmp (lt ge)
5683  (simplify
5684   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5685    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5686         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5687         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5688     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5689 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5690    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5691 (simplify
5692  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5693   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5694        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5695        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5696    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5698 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5699    signed arithmetic case.  That form is created by the compiler
5700    often enough for folding it to be of value.  One example is in
5701    computing loop trip counts after Operator Strength Reduction.  */
5702 (for cmp (simple_comparison)
5703      scmp (swapped_simple_comparison)
5704  (simplify
5705   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5706   /* Handle unfolded multiplication by zero.  */
5707   (if (integer_zerop (@1))
5708    (cmp @1 @2)
5709    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5710         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5711         && single_use (@3))
5712     /* If @1 is negative we swap the sense of the comparison.  */
5713     (if (tree_int_cst_sgn (@1) < 0)
5714      (scmp @0 @2)
5715      (cmp @0 @2))))))
5717 /* For integral types with undefined overflow fold
5718    x * C1 == C2 into x == C2 / C1 or false.
5719    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5720    Z / 2^n Z.  */
5721 (for cmp (eq ne)
5722  (simplify
5723   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5724   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5725        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5726        && wi::to_wide (@1) != 0)
5727    (with { widest_int quot; }
5728     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5729                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5730      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5731      { constant_boolean_node (cmp == NE_EXPR, type); }))
5732    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5733         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5734         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5735     (cmp @0
5736      {
5737        tree itype = TREE_TYPE (@0);
5738        int p = TYPE_PRECISION (itype);
5739        wide_int m = wi::one (p + 1) << p;
5740        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5741        wide_int i = wide_int::from (wi::mod_inv (a, m),
5742                                     p, TYPE_SIGN (itype));
5743        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5744      })))))
5746 /* Simplify comparison of something with itself.  For IEEE
5747    floating-point, we can only do some of these simplifications.  */
5748 (for cmp (eq ge le)
5749  (simplify
5750   (cmp @0 @0)
5751   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5752        || ! tree_expr_maybe_nan_p (@0))
5753    { constant_boolean_node (true, type); }
5754    (if (cmp != EQ_EXPR
5755         /* With -ftrapping-math conversion to EQ loses an exception.  */
5756         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5757             || ! flag_trapping_math))
5758     (eq @0 @0)))))
5759 (for cmp (ne gt lt)
5760  (simplify
5761   (cmp @0 @0)
5762   (if (cmp != NE_EXPR
5763        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5764        || ! tree_expr_maybe_nan_p (@0))
5765    { constant_boolean_node (false, type); })))
5766 (for cmp (unle unge uneq)
5767  (simplify
5768   (cmp @0 @0)
5769   { constant_boolean_node (true, type); }))
5770 (for cmp (unlt ungt)
5771  (simplify
5772   (cmp @0 @0)
5773   (unordered @0 @0)))
5774 (simplify
5775  (ltgt @0 @0)
5776  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5777   { constant_boolean_node (false, type); }))
5779 /* x == ~x -> false */
5780 /* x != ~x -> true */
5781 (for cmp (eq ne)
5782  (simplify
5783   (cmp:c @0 (bit_not @0))
5784   { constant_boolean_node (cmp == NE_EXPR, type); }))
5786 /* Fold ~X op ~Y as Y op X.  */
5787 (for cmp (simple_comparison)
5788  (simplify
5789   (cmp (bit_not@2 @0) (bit_not@3 @1))
5790   (if (single_use (@2) && single_use (@3))
5791    (cmp @1 @0))))
5793 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5794 (for cmp (simple_comparison)
5795      scmp (swapped_simple_comparison)
5796  (simplify
5797   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5798   (if (single_use (@2)
5799        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5800    (scmp @0 (bit_not @1)))))
5802 (for cmp (simple_comparison)
5803  (simplify
5804   (cmp @0 REAL_CST@1)
5805   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5806   (switch
5807    /* a CMP (-0) -> a CMP 0  */
5808    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5809     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5810    /* (-0) CMP b -> 0 CMP b.  */
5811    (if (TREE_CODE (@0) == REAL_CST
5812         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5813     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5814    /* x != NaN is always true, other ops are always false.  */
5815    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5816         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5817         && !tree_expr_signaling_nan_p (@1)
5818         && !tree_expr_maybe_signaling_nan_p (@0))
5819     { constant_boolean_node (cmp == NE_EXPR, type); })
5820    /* NaN != y is always true, other ops are always false.  */
5821    (if (TREE_CODE (@0) == REAL_CST
5822         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5823         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5824         && !tree_expr_signaling_nan_p (@0)
5825         && !tree_expr_signaling_nan_p (@1))
5826     { constant_boolean_node (cmp == NE_EXPR, type); })
5827    /* Fold comparisons against infinity.  */
5828    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5829         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5830     (with
5831      {
5832        REAL_VALUE_TYPE max;
5833        enum tree_code code = cmp;
5834        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5835        if (neg)
5836          code = swap_tree_comparison (code);
5837      }
5838      (switch
5839       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5840       (if (code == GT_EXPR
5841            && !(HONOR_NANS (@0) && flag_trapping_math))
5842        { constant_boolean_node (false, type); })
5843       (if (code == LE_EXPR)
5844        /* x <= +Inf is always true, if we don't care about NaNs.  */
5845        (if (! HONOR_NANS (@0))
5846         { constant_boolean_node (true, type); }
5847         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5848            an "invalid" exception.  */
5849         (if (!flag_trapping_math)
5850          (eq @0 @0))))
5851       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5852          for == this introduces an exception for x a NaN.  */
5853       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5854            || code == GE_EXPR)
5855        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5856         (if (neg)
5857          (lt @0 { build_real (TREE_TYPE (@0), max); })
5858          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5859       /* x < +Inf is always equal to x <= DBL_MAX.  */
5860       (if (code == LT_EXPR)
5861        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5862         (if (neg)
5863          (ge @0 { build_real (TREE_TYPE (@0), max); })
5864          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5865       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5866          an exception for x a NaN so use an unordered comparison.  */
5867       (if (code == NE_EXPR)
5868        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5869         (if (! HONOR_NANS (@0))
5870          (if (neg)
5871           (ge @0 { build_real (TREE_TYPE (@0), max); })
5872           (le @0 { build_real (TREE_TYPE (@0), max); }))
5873          (if (neg)
5874           (unge @0 { build_real (TREE_TYPE (@0), max); })
5875           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5877  /* If this is a comparison of a real constant with a PLUS_EXPR
5878     or a MINUS_EXPR of a real constant, we can convert it into a
5879     comparison with a revised real constant as long as no overflow
5880     occurs when unsafe_math_optimizations are enabled.  */
5881  (if (flag_unsafe_math_optimizations)
5882   (for op (plus minus)
5883    (simplify
5884     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5885     (with
5886      {
5887        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5888                                TREE_TYPE (@1), @2, @1);
5889      }
5890      (if (tem && !TREE_OVERFLOW (tem))
5891       (cmp @0 { tem; }))))))
5893  /* Likewise, we can simplify a comparison of a real constant with
5894     a MINUS_EXPR whose first operand is also a real constant, i.e.
5895     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
5896     floating-point types only if -fassociative-math is set.  */
5897  (if (flag_associative_math)
5898   (simplify
5899    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5900    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5901     (if (tem && !TREE_OVERFLOW (tem))
5902      (cmp { tem; } @1)))))
5904  /* Fold comparisons against built-in math functions.  */
5905  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5906   (for sq (SQRT)
5907    (simplify
5908     (cmp (sq @0) REAL_CST@1)
5909     (switch
5910      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5911       (switch
5912        /* sqrt(x) < y is always false, if y is negative.  */
5913        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5914         { constant_boolean_node (false, type); })
5915        /* sqrt(x) > y is always true, if y is negative and we
5916           don't care about NaNs, i.e. negative values of x.  */
5917        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5918         { constant_boolean_node (true, type); })
5919        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5920        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5921      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5922       (switch
5923        /* sqrt(x) < 0 is always false.  */
5924        (if (cmp == LT_EXPR)
5925         { constant_boolean_node (false, type); })
5926        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
5927        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5928         { constant_boolean_node (true, type); })
5929        /* sqrt(x) <= 0 -> x == 0.  */
5930        (if (cmp == LE_EXPR)
5931         (eq @0 @1))
5932        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
5933           == or !=.  In the last case:
5935             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5937           if x is negative or NaN.  Due to -funsafe-math-optimizations,
5938           the results for other x follow from natural arithmetic.  */
5939        (cmp @0 @1)))
5940      (if ((cmp == LT_EXPR
5941            || cmp == LE_EXPR
5942            || cmp == GT_EXPR
5943            || cmp == GE_EXPR)
5944           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5945           /* Give up for -frounding-math.  */
5946           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5947       (with
5948        {
5949          REAL_VALUE_TYPE c2;
5950          enum tree_code ncmp = cmp;
5951          const real_format *fmt
5952            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5953          real_arithmetic (&c2, MULT_EXPR,
5954                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5955          real_convert (&c2, fmt, &c2);
5956          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5957             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
5958          if (!REAL_VALUE_ISINF (c2))
5959            {
5960              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5961                                         build_real (TREE_TYPE (@0), c2));
5962              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5963                ncmp = ERROR_MARK;
5964              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5965                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5966                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5967              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5968                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5969                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5970              else
5971                {
5972                  /* With rounding to even, sqrt of up to 3 different values
5973                     gives the same normal result, so in some cases c2 needs
5974                     to be adjusted.  */
5975                  REAL_VALUE_TYPE c2alt, tow;
5976                  if (cmp == LT_EXPR || cmp == GE_EXPR)
5977                    tow = dconst0;
5978                  else
5979                    tow = dconstinf;
5980                  real_nextafter (&c2alt, fmt, &c2, &tow);
5981                  real_convert (&c2alt, fmt, &c2alt);
5982                  if (REAL_VALUE_ISINF (c2alt))
5983                    ncmp = ERROR_MARK;
5984                  else
5985                    {
5986                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5987                                            build_real (TREE_TYPE (@0), c2alt));
5988                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5989                        ncmp = ERROR_MARK;
5990                      else if (real_equal (&TREE_REAL_CST (c3),
5991                                           &TREE_REAL_CST (@1)))
5992                        c2 = c2alt;
5993                    }
5994                }
5995            }
5996        }
5997        (if (cmp == GT_EXPR || cmp == GE_EXPR)
5998         (if (REAL_VALUE_ISINF (c2))
5999          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6000          (if (HONOR_INFINITIES (@0))
6001           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6002           { constant_boolean_node (false, type); })
6003          /* sqrt(x) > c is the same as x > c*c.  */
6004          (if (ncmp != ERROR_MARK)
6005           (if (ncmp == GE_EXPR)
6006            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6007            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6008         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6009         (if (REAL_VALUE_ISINF (c2))
6010          (switch
6011           /* sqrt(x) < y is always true, when y is a very large
6012              value and we don't care about NaNs or Infinities.  */
6013           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6014            { constant_boolean_node (true, type); })
6015           /* sqrt(x) < y is x != +Inf when y is very large and we
6016              don't care about NaNs.  */
6017           (if (! HONOR_NANS (@0))
6018            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6019           /* sqrt(x) < y is x >= 0 when y is very large and we
6020              don't care about Infinities.  */
6021           (if (! HONOR_INFINITIES (@0))
6022            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6023           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6024           (if (GENERIC)
6025            (truth_andif
6026             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6027             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6028          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6029          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6030           (if (ncmp == LT_EXPR)
6031            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6032            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6033           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6034           (if (ncmp != ERROR_MARK && GENERIC)
6035            (if (ncmp == LT_EXPR)
6036             (truth_andif
6037              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6038              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6039             (truth_andif
6040              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6041              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6042    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6043    (simplify
6044     (cmp (sq @0) (sq @1))
6045       (if (! HONOR_NANS (@0))
6046         (cmp @0 @1))))))
6048 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6049 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6050      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6051  (simplify
6052   (cmp (float@0 @1) (float @2))
6053    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6054         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6055     (with
6056      {
6057        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6058        tree type1 = TREE_TYPE (@1);
6059        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6060        tree type2 = TREE_TYPE (@2);
6061        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6062      }
6063      (if (fmt.can_represent_integral_type_p (type1)
6064           && fmt.can_represent_integral_type_p (type2))
6065       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6066        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6067        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6068             && type1_signed_p >= type2_signed_p)
6069         (icmp @1 (convert @2))
6070         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6071              && type1_signed_p <= type2_signed_p)
6072          (icmp (convert:type2 @1) @2)
6073          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6074               && type1_signed_p == type2_signed_p)
6075           (icmp @1 @2))))))))))
6077 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6078 (for cmp  (lt le eq ne ge gt)
6079      icmp (le le eq ne ge ge)
6080  (simplify
6081   (cmp (float @0) REAL_CST@1)
6082    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6083         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6084     (with
6085      {
6086        tree itype = TREE_TYPE (@0);
6087        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6088        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6089        /* Be careful to preserve any potential exceptions due to
6090           NaNs.  qNaNs are ok in == or != context.
6091           TODO: relax under -fno-trapping-math or
6092           -fno-signaling-nans.  */
6093        bool exception_p
6094          = real_isnan (cst) && (cst->signalling
6095                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6096      }
6097      /* TODO: allow non-fitting itype and SNaNs when
6098         -fno-trapping-math.  */
6099      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6100       (with
6101        {
6102          signop isign = TYPE_SIGN (itype);
6103          REAL_VALUE_TYPE imin, imax;
6104          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6105          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6107          REAL_VALUE_TYPE icst;
6108          if (cmp == GT_EXPR || cmp == GE_EXPR)
6109            real_ceil (&icst, fmt, cst);
6110          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6111            real_floor (&icst, fmt, cst);
6112          else
6113            real_trunc (&icst, fmt, cst);
6115          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6117          bool overflow_p = false;
6118          wide_int icst_val
6119            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6120        }
6121        (switch
6122         /* Optimize cases when CST is outside of ITYPE's range.  */
6123         (if (real_compare (LT_EXPR, cst, &imin))
6124          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6125                                   type); })
6126         (if (real_compare (GT_EXPR, cst, &imax))
6127          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6128                                   type); })
6129         /* Remove cast if CST is an integer representable by ITYPE.  */
6130         (if (cst_int_p)
6131          (cmp @0 { gcc_assert (!overflow_p);
6132                    wide_int_to_tree (itype, icst_val); })
6133         )
6134         /* When CST is fractional, optimize
6135             (FTYPE) N == CST -> 0
6136             (FTYPE) N != CST -> 1.  */
6137         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6138          { constant_boolean_node (cmp == NE_EXPR, type); })
6139         /* Otherwise replace with sensible integer constant.  */
6140         (with
6141          {
6142            gcc_checking_assert (!overflow_p);
6143          }
6144          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6146 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6147 (for cmp (eq ne)
6148  (simplify
6149   (cmp (exact_div @0 @1) INTEGER_CST@2)
6150   (if (!integer_zerop (@1))
6151    (if (wi::to_wide (@2) == 0)
6152     (cmp @0 @2)
6153     (if (TREE_CODE (@1) == INTEGER_CST)
6154      (with
6155       {
6156         wi::overflow_type ovf;
6157         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6158                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6159       }
6160       (if (ovf)
6161        { constant_boolean_node (cmp == NE_EXPR, type); }
6162        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6163 (for cmp (lt le gt ge)
6164  (simplify
6165   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6166   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6167    (with
6168     {
6169       wi::overflow_type ovf;
6170       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6171                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6172     }
6173     (if (ovf)
6174      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6175                                         TYPE_SIGN (TREE_TYPE (@2)))
6176                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6177      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6179 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6181    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6182    For large C (more than min/B+2^size), this is also true, with the
6183    multiplication computed modulo 2^size.
6184    For intermediate C, this just tests the sign of A.  */
6185 (for cmp  (lt le gt ge)
6186      cmp2 (ge ge lt lt)
6187  (simplify
6188   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6189   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6190        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6191        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6192    (with
6193     {
6194       tree utype = TREE_TYPE (@2);
6195       wide_int denom = wi::to_wide (@1);
6196       wide_int right = wi::to_wide (@2);
6197       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6198       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6199       bool small = wi::leu_p (right, smax);
6200       bool large = wi::geu_p (right, smin);
6201     }
6202     (if (small || large)
6203      (cmp (convert:utype @0) (mult @2 (convert @1)))
6204      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6206 /* Unordered tests if either argument is a NaN.  */
6207 (simplify
6208  (bit_ior (unordered @0 @0) (unordered @1 @1))
6209  (if (types_match (@0, @1))
6210   (unordered @0 @1)))
6211 (simplify
6212  (bit_and (ordered @0 @0) (ordered @1 @1))
6213  (if (types_match (@0, @1))
6214   (ordered @0 @1)))
6215 (simplify
6216  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6217  @2)
6218 (simplify
6219  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6220  @2)
6222 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6223    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6225    Note that comparisons
6226      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6227      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6228    will be canonicalized to above so there's no need to
6229    consider them here.
6230  */
6232 (for cmp (le gt)
6233      eqcmp (eq ne)
6234  (simplify
6235   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6236   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6237    (with
6238     {
6239      tree ty = TREE_TYPE (@0);
6240      unsigned prec = TYPE_PRECISION (ty);
6241      wide_int mask = wi::to_wide (@2, prec);
6242      wide_int rhs = wi::to_wide (@3, prec);
6243      signop sgn = TYPE_SIGN (ty);
6244     }
6245     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6246          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6247       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6248              { build_zero_cst (ty); }))))))
6250 /* -A CMP -B -> B CMP A.  */
6251 (for cmp (tcc_comparison)
6252      scmp (swapped_tcc_comparison)
6253  (simplify
6254   (cmp (negate @0) (negate @1))
6255   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6256        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6257            && (cmp == EQ_EXPR
6258                || cmp == NE_EXPR
6259                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6260    (scmp @0 @1)))
6261  (simplify
6262   (cmp (negate @0) CONSTANT_CLASS_P@1)
6263   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6264        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6265            && (cmp == EQ_EXPR
6266                || cmp == NE_EXPR
6267                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6268    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6269     (if (tem && !TREE_OVERFLOW (tem))
6270      (scmp @0 { tem; }))))))
6272 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6273 (for op (abs absu)
6274  (for eqne (eq ne)
6275   (simplify
6276    (eqne (op @0) zerop@1)
6277    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6279 /* From fold_sign_changed_comparison and fold_widened_comparison.
6280    FIXME: the lack of symmetry is disturbing.  */
6281 (for cmp (simple_comparison)
6282  (simplify
6283   (cmp (convert@0 @00) (convert?@1 @10))
6284   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6285        /* Disable this optimization if we're casting a function pointer
6286           type on targets that require function pointer canonicalization.  */
6287        && !(targetm.have_canonicalize_funcptr_for_compare ()
6288             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6289                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6290                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6291                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6292        && single_use (@0))
6293    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6294         && (TREE_CODE (@10) == INTEGER_CST
6295             || @1 != @10)
6296         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6297             || cmp == NE_EXPR
6298             || cmp == EQ_EXPR)
6299         && !POINTER_TYPE_P (TREE_TYPE (@00))
6300         /* (int)bool:32 != (int)uint is not the same as
6301            bool:32 != (bool:32)uint since boolean types only have two valid
6302            values independent of their precision.  */
6303         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6304             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6305     /* ???  The special-casing of INTEGER_CST conversion was in the original
6306        code and here to avoid a spurious overflow flag on the resulting
6307        constant which fold_convert produces.  */
6308     (if (TREE_CODE (@1) == INTEGER_CST)
6309      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6310                                 TREE_OVERFLOW (@1)); })
6311      (cmp @00 (convert @1)))
6313     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6314      /* If possible, express the comparison in the shorter mode.  */
6315      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6316            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6317            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6318                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6319           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6320               || ((TYPE_PRECISION (TREE_TYPE (@00))
6321                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6322                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6323                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6324               || (TREE_CODE (@10) == INTEGER_CST
6325                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6326                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6327       (cmp @00 (convert @10))
6328       (if (TREE_CODE (@10) == INTEGER_CST
6329            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6330            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6331        (with
6332         {
6333           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6334           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6335           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6336           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6337         }
6338         (if (above || below)
6339          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6340           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6341           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6342            { constant_boolean_node (above ? true : false, type); }
6343            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6344             { constant_boolean_node (above ? false : true, type); })))))))))
6345    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6346    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6347         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6348             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6349         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6350             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6351     (with
6352      {
6353        tree type1 = TREE_TYPE (@10);
6354        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6355          {
6356            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6357            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6358                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6359              type1 = float_type_node;
6360            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6361                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6362              type1 = double_type_node;
6363          }
6364       tree newtype
6365         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6366            ? TREE_TYPE (@00) : type1);
6367      }
6368      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6369       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6372 (for cmp (eq ne)
6373  (simplify
6374   /* SSA names are canonicalized to 2nd place.  */
6375   (cmp addr@0 SSA_NAME@1)
6376   (with
6377    {
6378      poly_int64 off; tree base;
6379      tree addr = (TREE_CODE (@0) == SSA_NAME
6380                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6381    }
6382    /* A local variable can never be pointed to by
6383       the default SSA name of an incoming parameter.  */
6384    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6385         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6386         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6387         && TREE_CODE (base) == VAR_DECL
6388         && auto_var_in_fn_p (base, current_function_decl))
6389     (if (cmp == NE_EXPR)
6390      { constant_boolean_node (true, type); }
6391      { constant_boolean_node (false, type); })
6392     /* If the address is based on @1 decide using the offset.  */
6393     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6394          && TREE_CODE (base) == MEM_REF
6395          && TREE_OPERAND (base, 0) == @1)
6396      (with { off += mem_ref_offset (base).force_shwi (); }
6397       (if (known_ne (off, 0))
6398        { constant_boolean_node (cmp == NE_EXPR, type); }
6399        (if (known_eq (off, 0))
6400         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6402 /* Equality compare simplifications from fold_binary  */
6403 (for cmp (eq ne)
6405  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6406     Similarly for NE_EXPR.  */
6407  (simplify
6408   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6409   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6410        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6411    { constant_boolean_node (cmp == NE_EXPR, type); }))
6413  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6414  (simplify
6415   (cmp (bit_xor @0 @1) integer_zerop)
6416   (cmp @0 @1))
6418  /* (X ^ Y) == Y becomes X == 0.
6419     Likewise (X ^ Y) == X becomes Y == 0.  */
6420  (simplify
6421   (cmp:c (bit_xor:c @0 @1) @0)
6422   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6424  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6425  (simplify
6426   (cmp:c (bit_and:c @0 @1) @0)
6427   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6428  (simplify
6429   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6430   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6431        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6432        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6433        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6434        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6435        && !wi::neg_p (wi::to_wide (@1)))
6436    (cmp (bit_and @0 (convert (bit_not @1)))
6437         { build_zero_cst (TREE_TYPE (@0)); })))
6439  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6440  (simplify
6441   (cmp:c (bit_ior:c @0 @1) @1)
6442   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6444  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6445  (simplify
6446   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6447   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6448    (cmp @0 (bit_xor @1 (convert @2)))))
6450  (simplify
6451   (cmp (nop_convert? @0) integer_zerop)
6452   (if (tree_expr_nonzero_p (@0))
6453    { constant_boolean_node (cmp == NE_EXPR, type); }))
6455  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6456  (simplify
6457   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6458   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6460 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6461    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6462    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6463    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6464 (for cmp (eq ne)
6465      ncmp (ge lt)
6466  (for sgncmp (ge lt)
6467   (simplify
6468    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6469    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6470         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6471         && types_match (@0, @1))
6472     (ncmp (bit_xor @0 @1) @2)))))
6473 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6474    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6475 (for cmp (eq ne)
6476      ncmp (lt ge)
6477  (simplify
6478   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6479    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6480         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6481         && types_match (@0, @1))
6482     (ncmp (bit_xor @0 @1) @2))))
6484 /* If we have (A & C) == C where C is a power of 2, convert this into
6485    (A & C) != 0.  Similarly for NE_EXPR.  */
6486 (for cmp (eq ne)
6487      icmp (ne eq)
6488  (simplify
6489   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6490   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6492 #if GIMPLE
6493 /* From fold_binary_op_with_conditional_arg handle the case of
6494    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6495    compares simplify.  */
6496 (for cmp (simple_comparison)
6497  (simplify
6498   (cmp:c (cond @0 @1 @2) @3)
6499   /* Do not move possibly trapping operations into the conditional as this
6500      pessimizes code and causes gimplification issues when applied late.  */
6501   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6502        || !operation_could_trap_p (cmp, true, false, @3))
6503    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6504 #endif
6506 (for cmp (ge lt)
6507 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6508 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6509  (simplify
6510   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6511    (if (INTEGRAL_TYPE_P (type)
6512         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6513         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6514         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6515     (with
6516      {
6517        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6518      }
6519     (if (cmp == LT_EXPR)
6520      (bit_xor (convert (rshift @0 {shifter;})) @1)
6521      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6522 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6523 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6524  (simplify
6525   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6526    (if (INTEGRAL_TYPE_P (type)
6527         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6528         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6529         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6530     (with
6531      {
6532        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6533      }
6534     (if (cmp == GE_EXPR)
6535      (bit_xor (convert (rshift @0 {shifter;})) @1)
6536      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6538 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6539    convert this into a shift followed by ANDing with D.  */
6540 (simplify
6541  (cond
6542   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6543   INTEGER_CST@2 integer_zerop)
6544  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6545   (with {
6546      int shift = (wi::exact_log2 (wi::to_wide (@2))
6547                   - wi::exact_log2 (wi::to_wide (@1)));
6548    }
6549    (if (shift > 0)
6550     (bit_and
6551      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6552     (bit_and
6553      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6554      @2)))))
6556 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6557    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6558 (for cmp (eq ne)
6559      ncmp (ge lt)
6560  (simplify
6561   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6562   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6563        && type_has_mode_precision_p (TREE_TYPE (@0))
6564        && element_precision (@2) >= element_precision (@0)
6565        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6566    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6567     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6569 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6570    this into a right shift or sign extension followed by ANDing with C.  */
6571 (simplify
6572  (cond
6573   (lt @0 integer_zerop)
6574   INTEGER_CST@1 integer_zerop)
6575  (if (integer_pow2p (@1)
6576       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6577   (with {
6578     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6579    }
6580    (if (shift >= 0)
6581     (bit_and
6582      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6583      @1)
6584     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6585        sign extension followed by AND with C will achieve the effect.  */
6586     (bit_and (convert @0) @1)))))
6588 /* When the addresses are not directly of decls compare base and offset.
6589    This implements some remaining parts of fold_comparison address
6590    comparisons but still no complete part of it.  Still it is good
6591    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6592 (for cmp (simple_comparison)
6593  (simplify
6594   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6595   (with
6596    {
6597      poly_int64 off0, off1;
6598      tree base0, base1;
6599      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6600                                   off0, off1, GENERIC);
6601    }
6602    (if (equal == 1)
6603     (switch
6604      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6605       { constant_boolean_node (known_eq (off0, off1), type); })
6606      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6607       { constant_boolean_node (known_ne (off0, off1), type); })
6608      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6609       { constant_boolean_node (known_lt (off0, off1), type); })
6610      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6611       { constant_boolean_node (known_le (off0, off1), type); })
6612      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6613       { constant_boolean_node (known_ge (off0, off1), type); })
6614      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6615       { constant_boolean_node (known_gt (off0, off1), type); }))
6616     (if (equal == 0)
6617      (switch
6618       (if (cmp == EQ_EXPR)
6619        { constant_boolean_node (false, type); })
6620       (if (cmp == NE_EXPR)
6621        { constant_boolean_node (true, type); })))))))
6623 #if GIMPLE
6624 /* a?~t:t -> (-(a))^t */
6625 (simplify
6626  (cond @0 @1 @2)
6627  (with { bool wascmp; }
6628   (if (INTEGRAL_TYPE_P (type)
6629        && bitwise_inverted_equal_p (@1, @2, wascmp)
6630        && (!wascmp || element_precision (type) == 1))
6631    (with {
6632      auto prec = TYPE_PRECISION (type);
6633      auto unsign = TYPE_UNSIGNED (type);
6634      tree inttype = build_nonstandard_integer_type (prec, unsign);
6635     }
6636     (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
6637 #endif
6639 /* Simplify pointer equality compares using PTA.  */
6640 (for neeq (ne eq)
6641  (simplify
6642   (neeq @0 @1)
6643   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6644        && ptrs_compare_unequal (@0, @1))
6645    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6647 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6648    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6649    Disable the transform if either operand is pointer to function.
6650    This broke pr22051-2.c for arm where function pointer
6651    canonicalizaion is not wanted.  */
6653 (for cmp (ne eq)
6654  (simplify
6655   (cmp (convert @0) INTEGER_CST@1)
6656   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6657          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6658          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6659          /* Don't perform this optimization in GENERIC if @0 has reference
6660             type when sanitizing.  See PR101210.  */
6661          && !(GENERIC
6662               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6663               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6664         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6665             && POINTER_TYPE_P (TREE_TYPE (@1))
6666             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6667        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6668    (cmp @0 (convert @1)))))
6670 /* Non-equality compare simplifications from fold_binary  */
6671 (for cmp (lt gt le ge)
6672  /* Comparisons with the highest or lowest possible integer of
6673     the specified precision will have known values.  */
6674  (simplify
6675   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6676   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6677         || POINTER_TYPE_P (TREE_TYPE (@1))
6678         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6679        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6680    (with
6681     {
6682       tree cst = uniform_integer_cst_p (@1);
6683       tree arg1_type = TREE_TYPE (cst);
6684       unsigned int prec = TYPE_PRECISION (arg1_type);
6685       wide_int max = wi::max_value (arg1_type);
6686       wide_int signed_max = wi::max_value (prec, SIGNED);
6687       wide_int min = wi::min_value (arg1_type);
6688     }
6689     (switch
6690      (if (wi::to_wide (cst) == max)
6691       (switch
6692        (if (cmp == GT_EXPR)
6693         { constant_boolean_node (false, type); })
6694        (if (cmp == GE_EXPR)
6695         (eq @2 @1))
6696        (if (cmp == LE_EXPR)
6697         { constant_boolean_node (true, type); })
6698        (if (cmp == LT_EXPR)
6699         (ne @2 @1))))
6700      (if (wi::to_wide (cst) == min)
6701       (switch
6702        (if (cmp == LT_EXPR)
6703         { constant_boolean_node (false, type); })
6704        (if (cmp == LE_EXPR)
6705         (eq @2 @1))
6706        (if (cmp == GE_EXPR)
6707         { constant_boolean_node (true, type); })
6708        (if (cmp == GT_EXPR)
6709         (ne @2 @1))))
6710      (if (wi::to_wide (cst) == max - 1)
6711       (switch
6712        (if (cmp == GT_EXPR)
6713         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6714                                     wide_int_to_tree (TREE_TYPE (cst),
6715                                                       wi::to_wide (cst)
6716                                                       + 1)); }))
6717        (if (cmp == LE_EXPR)
6718         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6719                                     wide_int_to_tree (TREE_TYPE (cst),
6720                                                       wi::to_wide (cst)
6721                                                       + 1)); }))))
6722      (if (wi::to_wide (cst) == min + 1)
6723       (switch
6724        (if (cmp == GE_EXPR)
6725         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6726                                     wide_int_to_tree (TREE_TYPE (cst),
6727                                                       wi::to_wide (cst)
6728                                                       - 1)); }))
6729        (if (cmp == LT_EXPR)
6730         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6731                                     wide_int_to_tree (TREE_TYPE (cst),
6732                                                       wi::to_wide (cst)
6733                                                       - 1)); }))))
6734      (if (wi::to_wide (cst) == signed_max
6735           && TYPE_UNSIGNED (arg1_type)
6736           /* We will flip the signedness of the comparison operator
6737              associated with the mode of @1, so the sign bit is
6738              specified by this mode.  Check that @1 is the signed
6739              max associated with this sign bit.  */
6740           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6741           /* signed_type does not work on pointer types.  */
6742           && INTEGRAL_TYPE_P (arg1_type))
6743       /* The following case also applies to X < signed_max+1
6744          and X >= signed_max+1 because previous transformations.  */
6745       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6746        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6747         (switch
6748          (if (cst == @1 && cmp == LE_EXPR)
6749           (ge (convert:st @0) { build_zero_cst (st); }))
6750          (if (cst == @1 && cmp == GT_EXPR)
6751           (lt (convert:st @0) { build_zero_cst (st); }))
6752          (if (cmp == LE_EXPR)
6753           (ge (view_convert:st @0) { build_zero_cst (st); }))
6754          (if (cmp == GT_EXPR)
6755           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6757 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
6758 (simplify
6759  (lt:c @0 (convert (ne @0 integer_zerop)))
6760  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6761   { constant_boolean_node (false, type); }))
6763 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6764 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6765 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6766 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6767 (for outer (ne eq)
6768  (for inner (ne eq)
6769   (simplify
6770    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6771    (with {
6772      bool cst1 = integer_onep (@1);
6773      bool cst0 = integer_zerop (@1);
6774      bool innereq = inner == EQ_EXPR;
6775      bool outereq = outer == EQ_EXPR;
6776     }
6777     (switch
6778      (if (innereq ? cst0 : cst1)
6779       { constant_boolean_node (!outereq, type); })
6780      (if (innereq ? cst1 : cst0)
6781       (with {
6782         tree utype = unsigned_type_for (TREE_TYPE (@0));
6783         tree ucst1 = build_one_cst (utype);
6784        }
6785        (if (!outereq)
6786         (gt (convert:utype @0) { ucst1; })
6787         (le (convert:utype @0) { ucst1; })
6788        )
6789       )
6790      )
6791      (with {
6792        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6793       }
6794       (if (outereq)
6795        (eq @0 { value; })
6796        (ne @0 { value; })
6797       )
6798      )
6799     )
6800    )
6801   )
6805 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6806  /* If the second operand is NaN, the result is constant.  */
6807  (simplify
6808   (cmp @0 REAL_CST@1)
6809   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6810        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6811    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6812                             ? false : true, type); })))
6814 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6815 (simplify
6816   (unordered @0 @1)
6817   (switch
6818     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6819         { constant_boolean_node (true, type); })
6820     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6821         { constant_boolean_node (false, type); })))
6823 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6824 (simplify
6825   (ordered @0 @1)
6826   (switch
6827     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6828         { constant_boolean_node (false, type); })
6829     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6830         { constant_boolean_node (true, type); })))
6832 /* bool_var != 0 becomes bool_var.  */
6833 (simplify
6834  (ne @0 integer_zerop)
6835  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6836       && types_match (type, TREE_TYPE (@0)))
6837   (non_lvalue @0)))
6838 /* bool_var == 1 becomes bool_var.  */
6839 (simplify
6840  (eq @0 integer_onep)
6841  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6842       && types_match (type, TREE_TYPE (@0)))
6843   (non_lvalue @0)))
6844 /* Do not handle
6845    bool_var == 0 becomes !bool_var or
6846    bool_var != 1 becomes !bool_var
6847    here because that only is good in assignment context as long
6848    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6849    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6850    clearly less optimal and which we'll transform again in forwprop.  */
6852 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6853    where ~Y + 1 == pow2 and Z = ~Y.  */
6854 (for cst (VECTOR_CST INTEGER_CST)
6855  (for cmp (eq ne)
6856       icmp (le gt)
6857   (simplify
6858    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6859     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6860      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6861       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6862                          ? optab_vector : optab_default;
6863               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6864        (if (target_supports_op_p (utype, icmp, optab)
6865             || (optimize_vectors_before_lowering_p ()
6866                 && (!target_supports_op_p (type, cmp, optab)
6867                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6868         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6869          (icmp @0 { csts; })
6870          (icmp (view_convert:utype @0) { csts; })))))))))
6872 /* When one argument is a constant, overflow detection can be simplified.
6873    Currently restricted to single use so as not to interfere too much with
6874    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6875    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6876 (for cmp (lt le ge gt)
6877      out (gt gt le le)
6878  (simplify
6879   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6880   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6881        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6882        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6883        && wi::to_wide (@1) != 0
6884        && single_use (@2))
6885    (with {
6886      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6887      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6888     }
6889     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6890                                 wi::max_value (prec, sign)
6891                                 - wi::to_wide (@1)); })))))
6893 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6894    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6895    expects the long form, so we restrict the transformation for now.  */
6896 (for cmp (gt le)
6897  (simplify
6898   (cmp:c (minus@2 @0 @1) @0)
6899   (if (single_use (@2)
6900        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6901        && TYPE_UNSIGNED (TREE_TYPE (@0)))
6902    (cmp @1 @0))))
6904 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
6905 (for cmp (ge lt)
6906  (simplify
6907   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6908    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6909         && TYPE_UNSIGNED (TREE_TYPE (@0)))
6910     (cmp @1 @0))))
6912 /* Testing for overflow is unnecessary if we already know the result.  */
6913 /* A - B > A  */
6914 (for cmp (gt le)
6915      out (ne eq)
6916  (simplify
6917   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6918   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6919        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6920    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6921 /* A + B < A  */
6922 (for cmp (lt ge)
6923      out (ne eq)
6924  (simplify
6925   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6926   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6927        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6928    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6930 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6931    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
6932 (for cmp (lt ge)
6933      out (ne eq)
6934  (simplify
6935   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6936   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6937    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6938     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6940 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6941    is at least twice as wide as type of A and B, simplify to
6942    __builtin_mul_overflow (A, B, <unused>).  */
6943 (for cmp (eq ne)
6944  (simplify
6945   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6946        integer_zerop)
6947   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6948        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6949        && TYPE_UNSIGNED (TREE_TYPE (@0))
6950        && (TYPE_PRECISION (TREE_TYPE (@3))
6951            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6952        && tree_fits_uhwi_p (@2)
6953        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6954        && types_match (@0, @1)
6955        && type_has_mode_precision_p (TREE_TYPE (@0))
6956        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6957            != CODE_FOR_nothing))
6958    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6959     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6961 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
6962 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6963  (simplify
6964   (ovf (convert@2 @0) @1)
6965   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6966        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6967        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6968        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6969    (ovf @0 @1)))
6970  (simplify
6971   (ovf @1 (convert@2 @0))
6972   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6973        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6974        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6975        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6976    (ovf @1 @0))))
6978 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6979    are unsigned to x > (umax / cst).  Similarly for signed type, but
6980    in that case it needs to be outside of a range.  */
6981 (simplify
6982  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6983   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6984        && TYPE_MAX_VALUE (TREE_TYPE (@0))
6985        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6986        && int_fits_type_p (@1, TREE_TYPE (@0)))
6987    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6988     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6989     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6990      (if (integer_minus_onep (@1))
6991       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6992       (with
6993        {
6994          tree div = fold_convert (TREE_TYPE (@0), @1);
6995          tree lo = int_const_binop (TRUNC_DIV_EXPR,
6996                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6997          tree hi = int_const_binop (TRUNC_DIV_EXPR,
6998                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6999          tree etype = range_check_type (TREE_TYPE (@0));
7000          if (etype)
7001            {
7002              if (wi::neg_p (wi::to_wide (div)))
7003                std::swap (lo, hi);
7004              lo = fold_convert (etype, lo);
7005              hi = fold_convert (etype, hi);
7006              hi = int_const_binop (MINUS_EXPR, hi, lo);
7007            }
7008        }
7009        (if (etype)
7010         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7012 /* Simplification of math builtins.  These rules must all be optimizations
7013    as well as IL simplifications.  If there is a possibility that the new
7014    form could be a pessimization, the rule should go in the canonicalization
7015    section that follows this one.
7017    Rules can generally go in this section if they satisfy one of
7018    the following:
7020    - the rule describes an identity
7022    - the rule replaces calls with something as simple as addition or
7023      multiplication
7025    - the rule contains unary calls only and simplifies the surrounding
7026      arithmetic.  (The idea here is to exclude non-unary calls in which
7027      one operand is constant and in which the call is known to be cheap
7028      when the operand has that value.)  */
7030 (if (flag_unsafe_math_optimizations)
7031  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7032  (simplify
7033   (mult (SQRT_ALL@1 @0) @1)
7034   (if (!tree_expr_maybe_signaling_nan_p (@0))
7035    @0))
7037  (for op (plus minus)
7038   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7039   (simplify
7040    (op (rdiv @0 @1)
7041        (rdiv @2 @1))
7042    (rdiv (op @0 @2) @1)))
7044  (for cmp (lt le gt ge)
7045       neg_cmp (gt ge lt le)
7046   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7047   (simplify
7048    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7049    (with
7050     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7051     (if (tem
7052          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7053               || (real_zerop (tem) && !real_zerop (@1))))
7054      (switch
7055       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7056        (cmp @0 { tem; }))
7057       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7058        (neg_cmp @0 { tem; })))))))
7060  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7061  (for root (SQRT CBRT)
7062   (simplify
7063    (mult (root:s @0) (root:s @1))
7064     (root (mult @0 @1))))
7066  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7067  (for exps (EXP EXP2 EXP10 POW10)
7068   (simplify
7069    (mult (exps:s @0) (exps:s @1))
7070     (exps (plus @0 @1))))
7072  /* Simplify a/root(b/c) into a*root(c/b).  */
7073  (for root (SQRT CBRT)
7074   (simplify
7075    (rdiv @0 (root:s (rdiv:s @1 @2)))
7076     (mult @0 (root (rdiv @2 @1)))))
7078  /* Simplify x/expN(y) into x*expN(-y).  */
7079  (for exps (EXP EXP2 EXP10 POW10)
7080   (simplify
7081    (rdiv @0 (exps:s @1))
7082     (mult @0 (exps (negate @1)))))
7084  (for logs (LOG LOG2 LOG10 LOG10)
7085       exps (EXP EXP2 EXP10 POW10)
7086   /* logN(expN(x)) -> x.  */
7087   (simplify
7088    (logs (exps @0))
7089    @0)
7090   /* expN(logN(x)) -> x.  */
7091   (simplify
7092    (exps (logs @0))
7093    @0))
7095  /* Optimize logN(func()) for various exponential functions.  We
7096     want to determine the value "x" and the power "exponent" in
7097     order to transform logN(x**exponent) into exponent*logN(x).  */
7098  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7099       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7100   (simplify
7101    (logs (exps @0))
7102    (if (SCALAR_FLOAT_TYPE_P (type))
7103     (with {
7104       tree x;
7105       switch (exps)
7106         {
7107         CASE_CFN_EXP:
7108           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7109           x = build_real_truncate (type, dconst_e ());
7110           break;
7111         CASE_CFN_EXP2:
7112           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7113           x = build_real (type, dconst2);
7114           break;
7115         CASE_CFN_EXP10:
7116         CASE_CFN_POW10:
7117           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7118           {
7119             REAL_VALUE_TYPE dconst10;
7120             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7121             x = build_real (type, dconst10);
7122           }
7123           break;
7124         default:
7125           gcc_unreachable ();
7126         }
7127       }
7128      (mult (logs { x; }) @0)))))
7130  (for logs (LOG LOG
7131             LOG2 LOG2
7132             LOG10 LOG10)
7133       exps (SQRT CBRT)
7134   (simplify
7135    (logs (exps @0))
7136    (if (SCALAR_FLOAT_TYPE_P (type))
7137     (with {
7138       tree x;
7139       switch (exps)
7140         {
7141         CASE_CFN_SQRT:
7142           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7143           x = build_real (type, dconsthalf);
7144           break;
7145         CASE_CFN_CBRT:
7146           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7147           x = build_real_truncate (type, dconst_third ());
7148           break;
7149         default:
7150           gcc_unreachable ();
7151         }
7152       }
7153      (mult { x; } (logs @0))))))
7155  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7156  (for logs (LOG LOG2 LOG10)
7157       pows (POW)
7158   (simplify
7159    (logs (pows @0 @1))
7160    (mult @1 (logs @0))))
7162  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7163     or if C is a positive power of 2,
7164     pow(C,x) -> exp2(log2(C)*x).  */
7165 #if GIMPLE
7166  (for pows (POW)
7167       exps (EXP)
7168       logs (LOG)
7169       exp2s (EXP2)
7170       log2s (LOG2)
7171   (simplify
7172    (pows REAL_CST@0 @1)
7173    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7174         && real_isfinite (TREE_REAL_CST_PTR (@0))
7175         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7176            the use_exp2 case until after vectorization.  It seems actually
7177            beneficial for all constants to postpone this until later,
7178            because exp(log(C)*x), while faster, will have worse precision
7179            and if x folds into a constant too, that is unnecessary
7180            pessimization.  */
7181         && canonicalize_math_after_vectorization_p ())
7182     (with {
7183        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7184        bool use_exp2 = false;
7185        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7186            && value->cl == rvc_normal)
7187          {
7188            REAL_VALUE_TYPE frac_rvt = *value;
7189            SET_REAL_EXP (&frac_rvt, 1);
7190            if (real_equal (&frac_rvt, &dconst1))
7191              use_exp2 = true;
7192          }
7193      }
7194      (if (!use_exp2)
7195       (if (optimize_pow_to_exp (@0, @1))
7196        (exps (mult (logs @0) @1)))
7197       (exp2s (mult (log2s @0) @1)))))))
7198 #endif
7200  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7201  (for pows (POW)
7202       exps (EXP EXP2 EXP10 POW10)
7203       logs (LOG LOG2 LOG10 LOG10)
7204   (simplify
7205    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7206    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7207         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7208     (exps (plus (mult (logs @0) @1) @2)))))
7210  (for sqrts (SQRT)
7211       cbrts (CBRT)
7212       pows (POW)
7213       exps (EXP EXP2 EXP10 POW10)
7214   /* sqrt(expN(x)) -> expN(x*0.5).  */
7215   (simplify
7216    (sqrts (exps @0))
7217    (exps (mult @0 { build_real (type, dconsthalf); })))
7218   /* cbrt(expN(x)) -> expN(x/3).  */
7219   (simplify
7220    (cbrts (exps @0))
7221    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7222   /* pow(expN(x), y) -> expN(x*y).  */
7223   (simplify
7224    (pows (exps @0) @1)
7225    (exps (mult @0 @1))))
7227  /* tan(atan(x)) -> x.  */
7228  (for tans (TAN)
7229       atans (ATAN)
7230   (simplify
7231    (tans (atans @0))
7232    @0)))
7234  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7235  (for sins (SIN)
7236       atans (ATAN)
7237       sqrts (SQRT)
7238       copysigns (COPYSIGN)
7239   (simplify
7240    (sins (atans:s @0))
7241    (with
7242      {
7243       REAL_VALUE_TYPE r_cst;
7244       build_sinatan_real (&r_cst, type);
7245       tree t_cst = build_real (type, r_cst);
7246       tree t_one = build_one_cst (type);
7247      }
7248     (if (SCALAR_FLOAT_TYPE_P (type))
7249      (cond (lt (abs @0) { t_cst; })
7250       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7251       (copysigns { t_one; } @0))))))
7253 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7254  (for coss (COS)
7255       atans (ATAN)
7256       sqrts (SQRT)
7257       copysigns (COPYSIGN)
7258   (simplify
7259    (coss (atans:s @0))
7260    (with
7261      {
7262       REAL_VALUE_TYPE r_cst;
7263       build_sinatan_real (&r_cst, type);
7264       tree t_cst = build_real (type, r_cst);
7265       tree t_one = build_one_cst (type);
7266       tree t_zero = build_zero_cst (type);
7267      }
7268     (if (SCALAR_FLOAT_TYPE_P (type))
7269      (cond (lt (abs @0) { t_cst; })
7270       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7271       (copysigns { t_zero; } @0))))))
7273  (if (!flag_errno_math)
7274   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7275   (for sinhs (SINH)
7276        atanhs (ATANH)
7277        sqrts (SQRT)
7278    (simplify
7279     (sinhs (atanhs:s @0))
7280     (with { tree t_one = build_one_cst (type); }
7281     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7283   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7284   (for coshs (COSH)
7285        atanhs (ATANH)
7286        sqrts (SQRT)
7287    (simplify
7288     (coshs (atanhs:s @0))
7289     (with { tree t_one = build_one_cst (type); }
7290     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7292 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7293 (simplify
7294  (CABS (complex:C @0 real_zerop@1))
7295  (abs @0))
7297 /* trunc(trunc(x)) -> trunc(x), etc.  */
7298 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7299  (simplify
7300   (fns (fns @0))
7301   (fns @0)))
7302 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7303 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7304  (simplify
7305   (fns integer_valued_real_p@0)
7306   @0))
7308 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7309 (simplify
7310  (HYPOT:c @0 real_zerop@1)
7311  (abs @0))
7313 /* pow(1,x) -> 1.  */
7314 (simplify
7315  (POW real_onep@0 @1)
7316  @0)
7318 (simplify
7319  /* copysign(x,x) -> x.  */
7320  (COPYSIGN_ALL @0 @0)
7321  @0)
7323 (simplify
7324  /* copysign(x,-x) -> -x.  */
7325  (COPYSIGN_ALL @0 (negate@1 @0))
7326  @1)
7328 (simplify
7329  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7330  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7331  (abs @0))
7333 (for scale (LDEXP SCALBN SCALBLN)
7334  /* ldexp(0, x) -> 0.  */
7335  (simplify
7336   (scale real_zerop@0 @1)
7337   @0)
7338  /* ldexp(x, 0) -> x.  */
7339  (simplify
7340   (scale @0 integer_zerop@1)
7341   @0)
7342  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7343  (simplify
7344   (scale REAL_CST@0 @1)
7345   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7346    @0)))
7348 /* Canonicalization of sequences of math builtins.  These rules represent
7349    IL simplifications but are not necessarily optimizations.
7351    The sincos pass is responsible for picking "optimal" implementations
7352    of math builtins, which may be more complicated and can sometimes go
7353    the other way, e.g. converting pow into a sequence of sqrts.
7354    We only want to do these canonicalizations before the pass has run.  */
7356 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7357  /* Simplify tan(x) * cos(x) -> sin(x). */
7358  (simplify
7359   (mult:c (TAN:s @0) (COS:s @0))
7360    (SIN @0))
7362  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7363  (simplify
7364   (mult:c @0 (POW:s @0 REAL_CST@1))
7365   (if (!TREE_OVERFLOW (@1))
7366    (POW @0 (plus @1 { build_one_cst (type); }))))
7368  /* Simplify sin(x) / cos(x) -> tan(x). */
7369  (simplify
7370   (rdiv (SIN:s @0) (COS:s @0))
7371    (TAN @0))
7373  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7374  (simplify
7375   (rdiv (SINH:s @0) (COSH:s @0))
7376    (TANH @0))
7378  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7379  (simplify
7380    (rdiv (TANH:s @0) (SINH:s @0))
7381    (rdiv {build_one_cst (type);} (COSH @0)))
7383  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7384  (simplify
7385   (rdiv (COS:s @0) (SIN:s @0))
7386    (rdiv { build_one_cst (type); } (TAN @0)))
7388  /* Simplify sin(x) / tan(x) -> cos(x). */
7389  (simplify
7390   (rdiv (SIN:s @0) (TAN:s @0))
7391   (if (! HONOR_NANS (@0)
7392        && ! HONOR_INFINITIES (@0))
7393    (COS @0)))
7395  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7396  (simplify
7397   (rdiv (TAN:s @0) (SIN:s @0))
7398   (if (! HONOR_NANS (@0)
7399        && ! HONOR_INFINITIES (@0))
7400    (rdiv { build_one_cst (type); } (COS @0))))
7402  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7403  (simplify
7404   (mult (POW:s @0 @1) (POW:s @0 @2))
7405    (POW @0 (plus @1 @2)))
7407  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7408  (simplify
7409   (mult (POW:s @0 @1) (POW:s @2 @1))
7410    (POW (mult @0 @2) @1))
7412  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7413  (simplify
7414   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7415    (POWI (mult @0 @2) @1))
7417  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7418  (simplify
7419   (rdiv (POW:s @0 REAL_CST@1) @0)
7420   (if (!TREE_OVERFLOW (@1))
7421    (POW @0 (minus @1 { build_one_cst (type); }))))
7423  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7424  (simplify
7425   (rdiv @0 (POW:s @1 @2))
7426    (mult @0 (POW @1 (negate @2))))
7428  (for sqrts (SQRT)
7429       cbrts (CBRT)
7430       pows (POW)
7431   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7432   (simplify
7433    (sqrts (sqrts @0))
7434    (pows @0 { build_real (type, dconst_quarter ()); }))
7435   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7436   (simplify
7437    (sqrts (cbrts @0))
7438    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7439   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7440   (simplify
7441    (cbrts (sqrts @0))
7442    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7443   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7444   (simplify
7445    (cbrts (cbrts tree_expr_nonnegative_p@0))
7446    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7447   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7448   (simplify
7449    (sqrts (pows @0 @1))
7450    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7451   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7452   (simplify
7453    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7454    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7455   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7456   (simplify
7457    (pows (sqrts @0) @1)
7458    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7459   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7460   (simplify
7461    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7462    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7463   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7464   (simplify
7465    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7466    (pows @0 (mult @1 @2))))
7468  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7469  (simplify
7470   (CABS (complex @0 @0))
7471   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7473  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7474  (simplify
7475   (HYPOT @0 @0)
7476   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7478  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7479  (for cexps (CEXP)
7480       exps (EXP)
7481       cexpis (CEXPI)
7482   (simplify
7483    (cexps compositional_complex@0)
7484    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7485     (complex
7486      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7487      (mult @1 (imagpart @2)))))))
7489 (if (canonicalize_math_p ())
7490  /* floor(x) -> trunc(x) if x is nonnegative.  */
7491  (for floors (FLOOR_ALL)
7492       truncs (TRUNC_ALL)
7493   (simplify
7494    (floors tree_expr_nonnegative_p@0)
7495    (truncs @0))))
7497 (match double_value_p
7498  @0
7499  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7500 (for froms (BUILT_IN_TRUNCL
7501             BUILT_IN_FLOORL
7502             BUILT_IN_CEILL
7503             BUILT_IN_ROUNDL
7504             BUILT_IN_NEARBYINTL
7505             BUILT_IN_RINTL)
7506      tos (BUILT_IN_TRUNC
7507           BUILT_IN_FLOOR
7508           BUILT_IN_CEIL
7509           BUILT_IN_ROUND
7510           BUILT_IN_NEARBYINT
7511           BUILT_IN_RINT)
7512  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7513  (if (optimize && canonicalize_math_p ())
7514   (simplify
7515    (froms (convert double_value_p@0))
7516    (convert (tos @0)))))
7518 (match float_value_p
7519  @0
7520  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7521 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7522             BUILT_IN_FLOORL BUILT_IN_FLOOR
7523             BUILT_IN_CEILL BUILT_IN_CEIL
7524             BUILT_IN_ROUNDL BUILT_IN_ROUND
7525             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7526             BUILT_IN_RINTL BUILT_IN_RINT)
7527      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7528           BUILT_IN_FLOORF BUILT_IN_FLOORF
7529           BUILT_IN_CEILF BUILT_IN_CEILF
7530           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7531           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7532           BUILT_IN_RINTF BUILT_IN_RINTF)
7533  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7534     if x is a float.  */
7535  (if (optimize && canonicalize_math_p ()
7536       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7537   (simplify
7538    (froms (convert float_value_p@0))
7539    (convert (tos @0)))))
7541 #if GIMPLE
7542 (match float16_value_p
7543  @0
7544  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7545 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7546             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7547             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7548             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7549             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7550             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7551             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7552             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7553      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7554           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7555           IFN_CEIL IFN_CEIL IFN_CEIL
7556           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7557           IFN_ROUND IFN_ROUND IFN_ROUND
7558           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7559           IFN_RINT IFN_RINT IFN_RINT
7560           IFN_SQRT IFN_SQRT IFN_SQRT)
7561  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7562     if x is a _Float16.  */
7563  (simplify
7564    (convert (froms (convert float16_value_p@0)))
7565      (if (optimize
7566           && types_match (type, TREE_TYPE (@0))
7567           && direct_internal_fn_supported_p (as_internal_fn (tos),
7568                                              type, OPTIMIZE_FOR_BOTH))
7569        (tos @0))))
7571 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7572    x,y is float value, similar for _Float16/double.  */
7573 (for copysigns (COPYSIGN_ALL)
7574  (simplify
7575   (convert (copysigns (convert@2 @0) (convert @1)))
7576    (if (optimize
7577        && !HONOR_SNANS (@2)
7578        && types_match (type, TREE_TYPE (@0))
7579        && types_match (type, TREE_TYPE (@1))
7580        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7581        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7582                                           type, OPTIMIZE_FOR_BOTH))
7583     (IFN_COPYSIGN @0 @1))))
7585 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7586      tos (IFN_FMA IFN_FMA IFN_FMA)
7587  (simplify
7588   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7589    (if (flag_unsafe_math_optimizations
7590        && optimize
7591        && FLOAT_TYPE_P (type)
7592        && FLOAT_TYPE_P (TREE_TYPE (@3))
7593        && types_match (type, TREE_TYPE (@0))
7594        && types_match (type, TREE_TYPE (@1))
7595        && types_match (type, TREE_TYPE (@2))
7596        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7597        && direct_internal_fn_supported_p (as_internal_fn (tos),
7598                                           type, OPTIMIZE_FOR_BOTH))
7599     (tos @0 @1 @2))))
7601 (for maxmin (max min)
7602  (simplify
7603   (convert (maxmin (convert@2 @0) (convert @1)))
7604    (if (optimize
7605        && FLOAT_TYPE_P (type)
7606        && FLOAT_TYPE_P (TREE_TYPE (@2))
7607        && types_match (type, TREE_TYPE (@0))
7608        && types_match (type, TREE_TYPE (@1))
7609        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7610     (maxmin @0 @1))))
7611 #endif
7613 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7614      tos (XFLOOR XCEIL XROUND XRINT)
7615  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7616  (if (optimize && canonicalize_math_p ())
7617   (simplify
7618    (froms (convert double_value_p@0))
7619    (tos @0))))
7621 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7622             XFLOOR XCEIL XROUND XRINT)
7623      tos (XFLOORF XCEILF XROUNDF XRINTF)
7624  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7625     if x is a float.  */
7626  (if (optimize && canonicalize_math_p ())
7627   (simplify
7628    (froms (convert float_value_p@0))
7629    (tos @0))))
7631 (if (canonicalize_math_p ())
7632  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7633  (for floors (IFLOOR LFLOOR LLFLOOR)
7634   (simplify
7635    (floors tree_expr_nonnegative_p@0)
7636    (fix_trunc @0))))
7638 (if (canonicalize_math_p ())
7639  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7640  (for fns (IFLOOR LFLOOR LLFLOOR
7641            ICEIL LCEIL LLCEIL
7642            IROUND LROUND LLROUND)
7643   (simplify
7644    (fns integer_valued_real_p@0)
7645    (fix_trunc @0)))
7646  (if (!flag_errno_math)
7647   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7648   (for rints (IRINT LRINT LLRINT)
7649    (simplify
7650     (rints integer_valued_real_p@0)
7651     (fix_trunc @0)))))
7653 (if (canonicalize_math_p ())
7654  (for ifn (IFLOOR ICEIL IROUND IRINT)
7655       lfn (LFLOOR LCEIL LROUND LRINT)
7656       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7657   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7658      sizeof (int) == sizeof (long).  */
7659   (if (TYPE_PRECISION (integer_type_node)
7660        == TYPE_PRECISION (long_integer_type_node))
7661    (simplify
7662     (ifn @0)
7663     (lfn:long_integer_type_node @0)))
7664   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7665      sizeof (long long) == sizeof (long).  */
7666   (if (TYPE_PRECISION (long_long_integer_type_node)
7667        == TYPE_PRECISION (long_integer_type_node))
7668    (simplify
7669     (llfn @0)
7670     (lfn:long_integer_type_node @0)))))
7672 /* cproj(x) -> x if we're ignoring infinities.  */
7673 (simplify
7674  (CPROJ @0)
7675  (if (!HONOR_INFINITIES (type))
7676    @0))
7678 /* If the real part is inf and the imag part is known to be
7679    nonnegative, return (inf + 0i).  */
7680 (simplify
7681  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7682  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7683   { build_complex_inf (type, false); }))
7685 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7686 (simplify
7687  (CPROJ (complex @0 REAL_CST@1))
7688  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7689   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7691 (for pows (POW)
7692      sqrts (SQRT)
7693      cbrts (CBRT)
7694  (simplify
7695   (pows @0 REAL_CST@1)
7696   (with {
7697     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7698     REAL_VALUE_TYPE tmp;
7699    }
7700    (switch
7701     /* pow(x,0) -> 1.  */
7702     (if (real_equal (value, &dconst0))
7703      { build_real (type, dconst1); })
7704     /* pow(x,1) -> x.  */
7705     (if (real_equal (value, &dconst1))
7706      @0)
7707     /* pow(x,-1) -> 1/x.  */
7708     (if (real_equal (value, &dconstm1))
7709      (rdiv { build_real (type, dconst1); } @0))
7710     /* pow(x,0.5) -> sqrt(x).  */
7711     (if (flag_unsafe_math_optimizations
7712          && canonicalize_math_p ()
7713          && real_equal (value, &dconsthalf))
7714      (sqrts @0))
7715     /* pow(x,1/3) -> cbrt(x).  */
7716     (if (flag_unsafe_math_optimizations
7717          && canonicalize_math_p ()
7718          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7719              real_equal (value, &tmp)))
7720      (cbrts @0))))))
7722 /* powi(1,x) -> 1.  */
7723 (simplify
7724  (POWI real_onep@0 @1)
7725  @0)
7727 (simplify
7728  (POWI @0 INTEGER_CST@1)
7729  (switch
7730   /* powi(x,0) -> 1.  */
7731   (if (wi::to_wide (@1) == 0)
7732    { build_real (type, dconst1); })
7733   /* powi(x,1) -> x.  */
7734   (if (wi::to_wide (@1) == 1)
7735    @0)
7736   /* powi(x,-1) -> 1/x.  */
7737   (if (wi::to_wide (@1) == -1)
7738    (rdiv { build_real (type, dconst1); } @0))))
7740 /* Narrowing of arithmetic and logical operations.
7742    These are conceptually similar to the transformations performed for
7743    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7744    term we want to move all that code out of the front-ends into here.  */
7746 /* Convert (outertype)((innertype0)a+(innertype1)b)
7747    into ((newtype)a+(newtype)b) where newtype
7748    is the widest mode from all of these.  */
7749 (for op (plus minus mult rdiv)
7750  (simplify
7751    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7752    /* If we have a narrowing conversion of an arithmetic operation where
7753       both operands are widening conversions from the same type as the outer
7754       narrowing conversion.  Then convert the innermost operands to a
7755       suitable unsigned type (to avoid introducing undefined behavior),
7756       perform the operation and convert the result to the desired type.  */
7757    (if (INTEGRAL_TYPE_P (type)
7758         && op != MULT_EXPR
7759         && op != RDIV_EXPR
7760         /* We check for type compatibility between @0 and @1 below,
7761            so there's no need to check that @2/@4 are integral types.  */
7762         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7763         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7764         /* The precision of the type of each operand must match the
7765            precision of the mode of each operand, similarly for the
7766            result.  */
7767         && type_has_mode_precision_p (TREE_TYPE (@1))
7768         && type_has_mode_precision_p (TREE_TYPE (@2))
7769         && type_has_mode_precision_p (type)
7770         /* The inner conversion must be a widening conversion.  */
7771         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7772         && types_match (@1, type)
7773         && (types_match (@1, @2)
7774             /* Or the second operand is const integer or converted const
7775                integer from valueize.  */
7776             || poly_int_tree_p (@4)))
7777      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7778        (op @1 (convert @2))
7779        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7780         (convert (op (convert:utype @1)
7781                      (convert:utype @2)))))
7782      (if (FLOAT_TYPE_P (type)
7783           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7784                == DECIMAL_FLOAT_TYPE_P (type))
7785       (with { tree arg0 = strip_float_extensions (@1);
7786               tree arg1 = strip_float_extensions (@2);
7787               tree itype = TREE_TYPE (@0);
7788               tree ty1 = TREE_TYPE (arg0);
7789               tree ty2 = TREE_TYPE (arg1);
7790               enum tree_code code = TREE_CODE (itype); }
7791         (if (FLOAT_TYPE_P (ty1)
7792              && FLOAT_TYPE_P (ty2))
7793          (with { tree newtype = type;
7794                  if (TYPE_MODE (ty1) == SDmode
7795                      || TYPE_MODE (ty2) == SDmode
7796                      || TYPE_MODE (type) == SDmode)
7797                    newtype = dfloat32_type_node;
7798                  if (TYPE_MODE (ty1) == DDmode
7799                      || TYPE_MODE (ty2) == DDmode
7800                      || TYPE_MODE (type) == DDmode)
7801                    newtype = dfloat64_type_node;
7802                  if (TYPE_MODE (ty1) == TDmode
7803                      || TYPE_MODE (ty2) == TDmode
7804                      || TYPE_MODE (type) == TDmode)
7805                    newtype = dfloat128_type_node; }
7806           (if ((newtype == dfloat32_type_node
7807                 || newtype == dfloat64_type_node
7808                 || newtype == dfloat128_type_node)
7809               && newtype == type
7810               && types_match (newtype, type))
7811             (op (convert:newtype @1) (convert:newtype @2))
7812             (with { if (element_precision (ty1) > element_precision (newtype))
7813                       newtype = ty1;
7814                     if (element_precision (ty2) > element_precision (newtype))
7815                       newtype = ty2; }
7816                /* Sometimes this transformation is safe (cannot
7817                   change results through affecting double rounding
7818                   cases) and sometimes it is not.  If NEWTYPE is
7819                   wider than TYPE, e.g. (float)((long double)double
7820                   + (long double)double) converted to
7821                   (float)(double + double), the transformation is
7822                   unsafe regardless of the details of the types
7823                   involved; double rounding can arise if the result
7824                   of NEWTYPE arithmetic is a NEWTYPE value half way
7825                   between two representable TYPE values but the
7826                   exact value is sufficiently different (in the
7827                   right direction) for this difference to be
7828                   visible in ITYPE arithmetic.  If NEWTYPE is the
7829                   same as TYPE, however, the transformation may be
7830                   safe depending on the types involved: it is safe
7831                   if the ITYPE has strictly more than twice as many
7832                   mantissa bits as TYPE, can represent infinities
7833                   and NaNs if the TYPE can, and has sufficient
7834                   exponent range for the product or ratio of two
7835                   values representable in the TYPE to be within the
7836                   range of normal values of ITYPE.  */
7837               (if (element_precision (newtype) < element_precision (itype)
7838                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7839                        || target_supports_op_p (newtype, op, optab_default))
7840                    && (flag_unsafe_math_optimizations
7841                        || (element_precision (newtype) == element_precision (type)
7842                            && real_can_shorten_arithmetic (element_mode (itype),
7843                                                            element_mode (type))
7844                            && !excess_precision_type (newtype)))
7845                    && !types_match (itype, newtype))
7846                  (convert:type (op (convert:newtype @1)
7847                                    (convert:newtype @2)))
7848          )))) )
7849    ))
7852 /* This is another case of narrowing, specifically when there's an outer
7853    BIT_AND_EXPR which masks off bits outside the type of the innermost
7854    operands.   Like the previous case we have to convert the operands
7855    to unsigned types to avoid introducing undefined behavior for the
7856    arithmetic operation.  */
7857 (for op (minus plus)
7858  (simplify
7859   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7860   (if (INTEGRAL_TYPE_P (type)
7861        /* We check for type compatibility between @0 and @1 below,
7862           so there's no need to check that @1/@3 are integral types.  */
7863        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7864        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7865        /* The precision of the type of each operand must match the
7866           precision of the mode of each operand, similarly for the
7867           result.  */
7868        && type_has_mode_precision_p (TREE_TYPE (@0))
7869        && type_has_mode_precision_p (TREE_TYPE (@1))
7870        && type_has_mode_precision_p (type)
7871        /* The inner conversion must be a widening conversion.  */
7872        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7873        && types_match (@0, @1)
7874        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7875            <= TYPE_PRECISION (TREE_TYPE (@0)))
7876        && (wi::to_wide (@4)
7877            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7878                        true, TYPE_PRECISION (type))) == 0)
7879    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7880     (with { tree ntype = TREE_TYPE (@0); }
7881      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7882     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7883      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7884                (convert:utype @4))))))))
7886 /* Transform (@0 < @1 and @0 < @2) to use min,
7887    (@0 > @1 and @0 > @2) to use max */
7888 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7889      op    (lt      le      gt      ge      lt      le      gt      ge     )
7890      ext   (min     min     max     max     max     max     min     min    )
7891  (simplify
7892   (logic (op:cs @0 @1) (op:cs @0 @2))
7893   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7894        && TREE_CODE (@0) != INTEGER_CST)
7895    (op @0 (ext @1 @2)))))
7897 /* Max<bool0, bool1> -> bool0 | bool1
7898    Min<bool0, bool1> -> bool0 & bool1 */
7899 (for op    (max     min)
7900      logic (bit_ior bit_and)
7901  (simplify
7902   (op zero_one_valued_p@0 zero_one_valued_p@1)
7903   (logic @0 @1)))
7905 /* signbit(x) != 0 ? -x : x -> abs(x)
7906    signbit(x) == 0 ? -x : x -> -abs(x) */
7907 (for sign (SIGNBIT)
7908  (for neeq (ne eq)
7909   (simplify
7910    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
7911     (if (neeq == NE_EXPR)
7912      (abs @0)
7913      (negate (abs @0))))))
7915 (simplify
7916  /* signbit(x) -> 0 if x is nonnegative.  */
7917  (SIGNBIT tree_expr_nonnegative_p@0)
7918  { integer_zero_node; })
7920 (simplify
7921  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
7922  (SIGNBIT @0)
7923  (if (!HONOR_SIGNED_ZEROS (@0))
7924   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7926 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
7927 (for cmp (eq ne)
7928  (for op (plus minus)
7929       rop (minus plus)
7930   (simplify
7931    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7932    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7933         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7934         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7935         && !TYPE_SATURATING (TREE_TYPE (@0)))
7936     (with { tree res = int_const_binop (rop, @2, @1); }
7937      (if (TREE_OVERFLOW (res)
7938           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7939       { constant_boolean_node (cmp == NE_EXPR, type); }
7940       (if (single_use (@3))
7941        (cmp @0 { TREE_OVERFLOW (res)
7942                  ? drop_tree_overflow (res) : res; }))))))))
7943 (for cmp (lt le gt ge)
7944  (for op (plus minus)
7945       rop (minus plus)
7946   (simplify
7947    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7948    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7949         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7950     (with { tree res = int_const_binop (rop, @2, @1); }
7951      (if (TREE_OVERFLOW (res))
7952       {
7953         fold_overflow_warning (("assuming signed overflow does not occur "
7954                                 "when simplifying conditional to constant"),
7955                                WARN_STRICT_OVERFLOW_CONDITIONAL);
7956         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7957         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
7958         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7959                                   TYPE_SIGN (TREE_TYPE (@1)))
7960                         != (op == MINUS_EXPR);
7961         constant_boolean_node (less == ovf_high, type);
7962       }
7963       (if (single_use (@3))
7964        (with
7965         {
7966           fold_overflow_warning (("assuming signed overflow does not occur "
7967                                   "when changing X +- C1 cmp C2 to "
7968                                   "X cmp C2 -+ C1"),
7969                                  WARN_STRICT_OVERFLOW_COMPARISON);
7970         }
7971         (cmp @0 { res; })))))))))
7973 /* Canonicalizations of BIT_FIELD_REFs.  */
7975 (simplify
7976  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7977  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7979 (simplify
7980  (BIT_FIELD_REF (view_convert @0) @1 @2)
7981  (BIT_FIELD_REF @0 @1 @2))
7983 (simplify
7984  (BIT_FIELD_REF @0 @1 integer_zerop)
7985  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7986   (view_convert @0)))
7988 (simplify
7989  (BIT_FIELD_REF @0 @1 @2)
7990  (switch
7991   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7992        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7993    (switch
7994     (if (integer_zerop (@2))
7995      (view_convert (realpart @0)))
7996     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7997      (view_convert (imagpart @0)))))
7998   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7999        && INTEGRAL_TYPE_P (type)
8000        /* On GIMPLE this should only apply to register arguments.  */
8001        && (! GIMPLE || is_gimple_reg (@0))
8002        /* A bit-field-ref that referenced the full argument can be stripped.  */
8003        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8004             && integer_zerop (@2))
8005            /* Low-parts can be reduced to integral conversions.
8006               ???  The following doesn't work for PDP endian.  */
8007            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8008                /* But only do this after vectorization.  */
8009                && canonicalize_math_after_vectorization_p ()
8010                /* Don't even think about BITS_BIG_ENDIAN.  */
8011                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8012                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8013                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8014                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8015                                             - TYPE_PRECISION (type))
8016                                          : 0)) == 0)))
8017    (convert @0))))
8019 /* Simplify vector extracts.  */
8021 (simplify
8022  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8023  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8024       && tree_fits_uhwi_p (TYPE_SIZE (type))
8025       && ((tree_to_uhwi (TYPE_SIZE (type))
8026            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8027           || (VECTOR_TYPE_P (type)
8028               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8029                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8030   (with
8031    {
8032      tree ctor = (TREE_CODE (@0) == SSA_NAME
8033                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8034      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8035      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8036      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8037      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8038    }
8039    (if (n != 0
8040         && (idx % width) == 0
8041         && (n % width) == 0
8042         && known_le ((idx + n) / width,
8043                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8044     (with
8045      {
8046        idx = idx / width;
8047        n = n / width;
8048        /* Constructor elements can be subvectors.  */
8049        poly_uint64 k = 1;
8050        if (CONSTRUCTOR_NELTS (ctor) != 0)
8051          {
8052            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8053            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8054              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8055          }
8056        unsigned HOST_WIDE_INT elt, count, const_k;
8057      }
8058      (switch
8059       /* We keep an exact subset of the constructor elements.  */
8060       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8061        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8062         { build_zero_cst (type); }
8063         (if (count == 1)
8064          (if (elt < CONSTRUCTOR_NELTS (ctor))
8065           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8066           { build_zero_cst (type); })
8067          /* We don't want to emit new CTORs unless the old one goes away.
8068             ???  Eventually allow this if the CTOR ends up constant or
8069             uniform.  */
8070          (if (single_use (@0))
8071           (with
8072             {
8073               vec<constructor_elt, va_gc> *vals;
8074               vec_alloc (vals, count);
8075               bool constant_p = true;
8076               tree res;
8077               for (unsigned i = 0;
8078                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8079                 {
8080                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8081                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8082                   if (!CONSTANT_CLASS_P (e))
8083                     constant_p = false;
8084                 }
8085               tree evtype = (types_match (TREE_TYPE (type),
8086                                           TREE_TYPE (TREE_TYPE (ctor)))
8087                              ? type
8088                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8089                                                   count * k));
8090               /* We used to build a CTOR in the non-constant case here
8091                  but that's not a GIMPLE value.  We'd have to expose this
8092                  operation somehow so the code generation can properly
8093                  split it out to a separate stmt.  */
8094               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8095                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8096             }
8097             (if (res)
8098              (view_convert { res; })))))))
8099       /* The bitfield references a single constructor element.  */
8100       (if (k.is_constant (&const_k)
8101            && idx + n <= (idx / const_k + 1) * const_k)
8102        (switch
8103         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8104          { build_zero_cst (type); })
8105         (if (n == const_k)
8106          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8107         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8108                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8110 /* Simplify a bit extraction from a bit insertion for the cases with
8111    the inserted element fully covering the extraction or the insertion
8112    not touching the extraction.  */
8113 (simplify
8114  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8115  (with
8116   {
8117     unsigned HOST_WIDE_INT isize;
8118     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8119       isize = TYPE_PRECISION (TREE_TYPE (@1));
8120     else
8121       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8122   }
8123   (switch
8124    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8125          || type_has_mode_precision_p (TREE_TYPE (@1)))
8126         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8127         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8128                       wi::to_wide (@ipos) + isize))
8129     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8130                                                  wi::to_wide (@rpos)
8131                                                  - wi::to_wide (@ipos)); }))
8132    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8133         && compare_tree_int (@rsize, isize) == 0)
8134     (convert @1))
8135    (if (wi::geu_p (wi::to_wide (@ipos),
8136                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8137         || wi::geu_p (wi::to_wide (@rpos),
8138                       wi::to_wide (@ipos) + isize))
8139     (BIT_FIELD_REF @0 @rsize @rpos)))))
8141 /* Simplify vector inserts of other vector extracts to a permute.  */
8142 (simplify
8143  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8144  (if (VECTOR_TYPE_P (type)
8145       && types_match (@0, @1)
8146       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8147       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8148   (with
8149    {
8150      unsigned HOST_WIDE_INT elsz
8151        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8152      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8153      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8154      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8155      vec_perm_builder builder;
8156      builder.new_vector (nunits, nunits, 1);
8157      for (unsigned i = 0; i < nunits; ++i)
8158        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8159      vec_perm_indices sel (builder, 2, nunits);
8160    }
8161    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8162         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8163     (vec_perm @0 @1 { vec_perm_indices_to_tree
8164                         (build_vector_type (ssizetype, nunits), sel); })))))
8166 (if (canonicalize_math_after_vectorization_p ())
8167  (for fmas (FMA)
8168   (simplify
8169    (fmas:c (negate @0) @1 @2)
8170    (IFN_FNMA @0 @1 @2))
8171   (simplify
8172    (fmas @0 @1 (negate @2))
8173    (IFN_FMS @0 @1 @2))
8174   (simplify
8175    (fmas:c (negate @0) @1 (negate @2))
8176    (IFN_FNMS @0 @1 @2))
8177   (simplify
8178    (negate (fmas@3 @0 @1 @2))
8179    (if (single_use (@3))
8180     (IFN_FNMS @0 @1 @2))))
8182  (simplify
8183   (IFN_FMS:c (negate @0) @1 @2)
8184   (IFN_FNMS @0 @1 @2))
8185  (simplify
8186   (IFN_FMS @0 @1 (negate @2))
8187   (IFN_FMA @0 @1 @2))
8188  (simplify
8189   (IFN_FMS:c (negate @0) @1 (negate @2))
8190   (IFN_FNMA @0 @1 @2))
8191  (simplify
8192   (negate (IFN_FMS@3 @0 @1 @2))
8193    (if (single_use (@3))
8194     (IFN_FNMA @0 @1 @2)))
8196  (simplify
8197   (IFN_FNMA:c (negate @0) @1 @2)
8198   (IFN_FMA @0 @1 @2))
8199  (simplify
8200   (IFN_FNMA @0 @1 (negate @2))
8201   (IFN_FNMS @0 @1 @2))
8202  (simplify
8203   (IFN_FNMA:c (negate @0) @1 (negate @2))
8204   (IFN_FMS @0 @1 @2))
8205  (simplify
8206   (negate (IFN_FNMA@3 @0 @1 @2))
8207   (if (single_use (@3))
8208    (IFN_FMS @0 @1 @2)))
8210  (simplify
8211   (IFN_FNMS:c (negate @0) @1 @2)
8212   (IFN_FMS @0 @1 @2))
8213  (simplify
8214   (IFN_FNMS @0 @1 (negate @2))
8215   (IFN_FNMA @0 @1 @2))
8216  (simplify
8217   (IFN_FNMS:c (negate @0) @1 (negate @2))
8218   (IFN_FMA @0 @1 @2))
8219  (simplify
8220   (negate (IFN_FNMS@3 @0 @1 @2))
8221   (if (single_use (@3))
8222    (IFN_FMA @0 @1 @2))))
8224 /* CLZ simplifications.  */
8225 (for clz (CLZ)
8226  (for op (eq ne)
8227       cmp (lt ge)
8228   (simplify
8229    (op (clz:s@2 @0) INTEGER_CST@1)
8230    (if (integer_zerop (@1) && single_use (@2))
8231     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8232     (with { tree type0 = TREE_TYPE (@0);
8233             tree stype = signed_type_for (type0);
8234             HOST_WIDE_INT val = 0;
8235             /* Punt on hypothetical weird targets.  */
8236             if (clz == CFN_CLZ
8237                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8238                                               val) == 2
8239                 && val == 0)
8240               stype = NULL_TREE;
8241           }
8242      (if (stype)
8243       (cmp (convert:stype @0) { build_zero_cst (stype); })))
8244     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8245     (with { bool ok = true;
8246             HOST_WIDE_INT val = 0;
8247             tree type0 = TREE_TYPE (@0);
8248             /* Punt on hypothetical weird targets.  */
8249             if (clz == CFN_CLZ
8250                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8251                                               val) == 2
8252                 && val == TYPE_PRECISION (type0) - 1)
8253               ok = false;
8254           }
8255      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8256       (op @0 { build_one_cst (type0); })))))))
8258 /* CTZ simplifications.  */
8259 (for ctz (CTZ)
8260  (for op (ge gt le lt)
8261       cmp (eq eq ne ne)
8262   (simplify
8263    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8264    (op (ctz:s @0) INTEGER_CST@1)
8265     (with { bool ok = true;
8266             HOST_WIDE_INT val = 0;
8267             if (!tree_fits_shwi_p (@1))
8268               ok = false;
8269             else
8270               {
8271                 val = tree_to_shwi (@1);
8272                 /* Canonicalize to >= or <.  */
8273                 if (op == GT_EXPR || op == LE_EXPR)
8274                   {
8275                     if (val == HOST_WIDE_INT_MAX)
8276                       ok = false;
8277                     else
8278                       val++;
8279                   }
8280               }
8281             bool zero_res = false;
8282             HOST_WIDE_INT zero_val = 0;
8283             tree type0 = TREE_TYPE (@0);
8284             int prec = TYPE_PRECISION (type0);
8285             if (ctz == CFN_CTZ
8286                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8287                                               zero_val) == 2)
8288               zero_res = true;
8289           }
8290      (if (val <= 0)
8291       (if (ok && (!zero_res || zero_val >= val))
8292        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8293       (if (val >= prec)
8294        (if (ok && (!zero_res || zero_val < val))
8295         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8296        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8297         (cmp (bit_and @0 { wide_int_to_tree (type0,
8298                                              wi::mask (val, false, prec)); })
8299              { build_zero_cst (type0); })))))))
8300  (for op (eq ne)
8301   (simplify
8302    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8303    (op (ctz:s @0) INTEGER_CST@1)
8304     (with { bool zero_res = false;
8305             HOST_WIDE_INT zero_val = 0;
8306             tree type0 = TREE_TYPE (@0);
8307             int prec = TYPE_PRECISION (type0);
8308             if (ctz == CFN_CTZ
8309                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8310                                               zero_val) == 2)
8311               zero_res = true;
8312           }
8313      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8314       (if (!zero_res || zero_val != wi::to_widest (@1))
8315        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8316       (if (!zero_res || zero_val < 0 || zero_val >= prec)
8317        (op (bit_and @0 { wide_int_to_tree (type0,
8318                                            wi::mask (tree_to_uhwi (@1) + 1,
8319                                                      false, prec)); })
8320            { wide_int_to_tree (type0,
8321                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8322                                                  false, prec)); })))))))
8324 /* POPCOUNT simplifications.  */
8325 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8326 (simplify
8327   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8328   (if (INTEGRAL_TYPE_P (type)
8329        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8330     (POPCOUNT (bit_ior @0 @1))))
8332 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8333 (for popcount (POPCOUNT)
8334   (for cmp (le eq ne gt)
8335        rep (eq eq ne ne)
8336     (simplify
8337       (cmp (popcount @0) integer_zerop)
8338       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8340 /* popcount(bswap(x)) is popcount(x).  */
8341 (for popcount (POPCOUNT)
8342   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8343               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8344     (simplify
8345       (popcount (convert?@0 (bswap:s@1 @2)))
8346       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8347            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8348         (with { tree type0 = TREE_TYPE (@0);
8349                 tree type1 = TREE_TYPE (@1);
8350                 unsigned int prec0 = TYPE_PRECISION (type0);
8351                 unsigned int prec1 = TYPE_PRECISION (type1); }
8352           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8353             (popcount (convert:type0 (convert:type1 @2)))))))))
8355 /* popcount(rotate(X Y)) is popcount(X).  */
8356 (for popcount (POPCOUNT)
8357   (for rot (lrotate rrotate)
8358     (simplify
8359       (popcount (convert?@0 (rot:s@1 @2 @3)))
8360       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8361            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8362            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8363         (with { tree type0 = TREE_TYPE (@0);
8364                 tree type1 = TREE_TYPE (@1);
8365                 unsigned int prec0 = TYPE_PRECISION (type0);
8366                 unsigned int prec1 = TYPE_PRECISION (type1); }
8367           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8368             (popcount (convert:type0 @2))))))))
8370 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8371 (simplify
8372   (bit_and (POPCOUNT @0) integer_onep)
8373   (PARITY @0))
8375 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8376 (simplify
8377   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8378   (plus (POPCOUNT @0) (POPCOUNT @1)))
8380 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8381 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8382 (for popcount (POPCOUNT)
8383   (for log1 (bit_and bit_ior)
8384        log2 (bit_ior bit_and)
8385     (simplify
8386       (minus (plus:s (popcount:s @0) (popcount:s @1))
8387              (popcount:s (log1:cs @0 @1)))
8388       (popcount (log2 @0 @1)))
8389     (simplify
8390       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8391               (popcount:s @1))
8392       (popcount (log2 @0 @1)))))
8394 /* PARITY simplifications.  */
8395 /* parity(~X) is parity(X).  */
8396 (simplify
8397   (PARITY (bit_not @0))
8398   (PARITY @0))
8400 /* parity(bswap(x)) is parity(x).  */
8401 (for parity (PARITY)
8402   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8403               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8404     (simplify
8405       (parity (convert?@0 (bswap:s@1 @2)))
8406       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8407            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8408            && TYPE_PRECISION (TREE_TYPE (@0))
8409               >= TYPE_PRECISION (TREE_TYPE (@1)))
8410         (with { tree type0 = TREE_TYPE (@0);
8411                 tree type1 = TREE_TYPE (@1); }
8412           (parity (convert:type0 (convert:type1 @2))))))))
8414 /* parity(rotate(X Y)) is parity(X).  */
8415 (for parity (PARITY)
8416   (for rot (lrotate rrotate)
8417     (simplify
8418       (parity (convert?@0 (rot:s@1 @2 @3)))
8419       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8420            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8421            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8422            && TYPE_PRECISION (TREE_TYPE (@0))
8423               >= TYPE_PRECISION (TREE_TYPE (@1)))
8424         (with { tree type0 = TREE_TYPE (@0); }
8425           (parity (convert:type0 @2)))))))
8427 /* parity(X)^parity(Y) is parity(X^Y).  */
8428 (simplify
8429   (bit_xor (PARITY:s @0) (PARITY:s @1))
8430   (PARITY (bit_xor @0 @1)))
8432 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8433 (for func (POPCOUNT BSWAP FFS PARITY)
8434  (simplify
8435   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8436   @3))
8438 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8439    where CST is precision-1. */
8440 (for func (CLRSB)
8441  (simplify
8442   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8443   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8444    @4)))
8446 #if GIMPLE
8447 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8448 (for func (CLZ)
8449  (simplify
8450   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8451   (with { int val;
8452           internal_fn ifn = IFN_LAST;
8453           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8454               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8455                                             val) == 2)
8456             ifn = IFN_CLZ;
8457         }
8458    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8459     (IFN_CLZ @3)))))
8461 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8462 (for func (CTZ)
8463  (simplify
8464   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8465   (with { int val;
8466           internal_fn ifn = IFN_LAST;
8467           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8468               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8469                                             val) == 2)
8470             ifn = IFN_CTZ;
8471         }
8472    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8473     (IFN_CTZ @3)))))
8474 #endif
8476 /* Common POPCOUNT/PARITY simplifications.  */
8477 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8478 (for pfun (POPCOUNT PARITY)
8479   (simplify
8480     (pfun @0)
8481     (if (INTEGRAL_TYPE_P (type))
8482      (with { wide_int nz = tree_nonzero_bits (@0); }
8483        (switch
8484          (if (nz == 1)
8485            (convert @0))
8486          (if (wi::popcount (nz) == 1)
8487            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8488              (convert (rshift:utype (convert:utype @0)
8489                                     { build_int_cst (integer_type_node,
8490                                                      wi::ctz (nz)); })))))))))
8492 #if GIMPLE
8493 /* 64- and 32-bits branchless implementations of popcount are detected:
8495    int popcount64c (uint64_t x)
8496    {
8497      x -= (x >> 1) & 0x5555555555555555ULL;
8498      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8499      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8500      return (x * 0x0101010101010101ULL) >> 56;
8501    }
8503    int popcount32c (uint32_t x)
8504    {
8505      x -= (x >> 1) & 0x55555555;
8506      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8507      x = (x + (x >> 4)) & 0x0f0f0f0f;
8508      return (x * 0x01010101) >> 24;
8509    }  */
8510 (simplify
8511  (rshift
8512   (mult
8513    (bit_and
8514     (plus:c
8515      (rshift @8 INTEGER_CST@5)
8516       (plus:c@8
8517        (bit_and @6 INTEGER_CST@7)
8518         (bit_and
8519          (rshift
8520           (minus@6 @0
8521            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8522           INTEGER_CST@10)
8523          INTEGER_CST@9)))
8524     INTEGER_CST@3)
8525    INTEGER_CST@2)
8526   INTEGER_CST@1)
8527   /* Check constants and optab.  */
8528   (with { unsigned prec = TYPE_PRECISION (type);
8529           int shift = (64 - prec) & 63;
8530           unsigned HOST_WIDE_INT c1
8531             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8532           unsigned HOST_WIDE_INT c2
8533             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8534           unsigned HOST_WIDE_INT c3
8535             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8536           unsigned HOST_WIDE_INT c4
8537             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8538    }
8539    (if (prec >= 16
8540         && prec <= 64
8541         && pow2p_hwi (prec)
8542         && TYPE_UNSIGNED (type)
8543         && integer_onep (@4)
8544         && wi::to_widest (@10) == 2
8545         && wi::to_widest (@5) == 4
8546         && wi::to_widest (@1) == prec - 8
8547         && tree_to_uhwi (@2) == c1
8548         && tree_to_uhwi (@3) == c2
8549         && tree_to_uhwi (@9) == c3
8550         && tree_to_uhwi (@7) == c3
8551         && tree_to_uhwi (@11) == c4)
8552     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8553                                          OPTIMIZE_FOR_BOTH))
8554      (convert (IFN_POPCOUNT:type @0))
8555      /* Try to do popcount in two halves.  PREC must be at least
8556         five bits for this to work without extension before adding.  */
8557      (with {
8558        tree half_type = NULL_TREE;
8559        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8560        int half_prec = 8;
8561        if (m.exists ()
8562            && m.require () != TYPE_MODE (type))
8563          {
8564            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8565            half_type = build_nonstandard_integer_type (half_prec, 1);
8566          }
8567        gcc_assert (half_prec > 2);
8568       }
8569       (if (half_type != NULL_TREE
8570            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8571                                               OPTIMIZE_FOR_BOTH))
8572        (convert (plus
8573          (IFN_POPCOUNT:half_type (convert @0))
8574          (IFN_POPCOUNT:half_type (convert (rshift @0
8575             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8577 /* __builtin_ffs needs to deal on many targets with the possible zero
8578    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8579    should lead to better code.  */
8580 (simplify
8581  (FFS tree_expr_nonzero_p@0)
8582  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8583       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8584                                          OPTIMIZE_FOR_SPEED))
8585   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8586    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8587 #endif
8589 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8590           BUILT_IN_FFSIMAX)
8591  /* __builtin_ffs (X) == 0 -> X == 0.
8592     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8593  (for cmp (eq ne)
8594   (simplify
8595    (cmp (ffs@2 @0) INTEGER_CST@1)
8596     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8597      (switch
8598       (if (integer_zerop (@1))
8599        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8600       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8601        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8602       (if (single_use (@2))
8603        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8604                                             wi::mask (tree_to_uhwi (@1),
8605                                                       false, prec)); })
8606             { wide_int_to_tree (TREE_TYPE (@0),
8607                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8608                                                   false, prec)); }))))))
8610  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8611  (for cmp (gt le)
8612       cmp2 (ne eq)
8613       cmp3 (eq ne)
8614       bit_op (bit_and bit_ior)
8615   (simplify
8616    (cmp (ffs@2 @0) INTEGER_CST@1)
8617     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8618      (switch
8619       (if (integer_zerop (@1))
8620        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8621       (if (tree_int_cst_sgn (@1) < 0)
8622        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8623       (if (wi::to_widest (@1) >= prec)
8624        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8625       (if (wi::to_widest (@1) == prec - 1)
8626        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8627                                     wi::shifted_mask (prec - 1, 1,
8628                                                       false, prec)); }))
8629       (if (single_use (@2))
8630        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8631                (cmp3 (bit_and @0
8632                               { wide_int_to_tree (TREE_TYPE (@0),
8633                                                   wi::mask (tree_to_uhwi (@1),
8634                                                   false, prec)); })
8635                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8637 #if GIMPLE
8639 /* Simplify:
8640      a = op a1
8641      r = cond ? a : b
8642      --> r = .COND_FN (cond, a, b)
8643 and,
8644     a = op a1
8645     r = cond ? b : a
8646     --> r = .COND_FN (~cond, b, a).  */
8648 (for uncond_op (UNCOND_UNARY)
8649      cond_op (COND_UNARY)
8650  (simplify
8651   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8652    (with { tree op_type = TREE_TYPE (@3); }
8653     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8654         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8655      (cond_op @0 @1 @2))))
8656  (simplify
8657   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8658    (with { tree op_type = TREE_TYPE (@3); }
8659     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8660         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8661      (cond_op (bit_not @0) @2 @1)))))
8663 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
8664 (simplify
8665  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8666  (if (canonicalize_math_after_vectorization_p ()
8667       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8668       && is_truth_type_for (type, TREE_TYPE (@0)))
8669  (if (integer_all_onesp (@1) && integer_zerop (@2))
8670   (IFN_COND_NOT @0 @3 @3))
8671   (if (integer_all_onesp (@2) && integer_zerop (@1))
8672    (IFN_COND_NOT (bit_not @0) @3 @3))))
8674 /* Simplify:
8676      a = a1 op a2
8677      r = c ? a : b;
8679    to:
8681      r = c ? a1 op a2 : b;
8683    if the target can do it in one go.  This makes the operation conditional
8684    on c, so could drop potentially-trapping arithmetic, but that's a valid
8685    simplification if the result of the operation isn't needed.
8687    Avoid speculatively generating a stand-alone vector comparison
8688    on targets that might not support them.  Any target implementing
8689    conditional internal functions must support the same comparisons
8690    inside and outside a VEC_COND_EXPR.  */
8692 (for uncond_op (UNCOND_BINARY)
8693      cond_op (COND_BINARY)
8694  (simplify
8695   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8696   (with { tree op_type = TREE_TYPE (@4); }
8697    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8698         && is_truth_type_for (op_type, TREE_TYPE (@0))
8699         && single_use (@4))
8700     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8701  (simplify
8702   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8703   (with { tree op_type = TREE_TYPE (@4); }
8704    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8705         && is_truth_type_for (op_type, TREE_TYPE (@0))
8706         && single_use (@4))
8707     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8709 /* Same for ternary operations.  */
8710 (for uncond_op (UNCOND_TERNARY)
8711      cond_op (COND_TERNARY)
8712  (simplify
8713   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8714   (with { tree op_type = TREE_TYPE (@5); }
8715    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8716         && is_truth_type_for (op_type, TREE_TYPE (@0))
8717         && single_use (@5))
8718     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8719  (simplify
8720   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8721   (with { tree op_type = TREE_TYPE (@5); }
8722    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8723         && is_truth_type_for (op_type, TREE_TYPE (@0))
8724         && single_use (@5))
8725     (view_convert (cond_op (bit_not @0) @2 @3 @4
8726                   (view_convert:op_type @1)))))))
8727 #endif
8729 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8730    "else" value of an IFN_COND_*.  */
8731 (for cond_op (COND_BINARY)
8732  (simplify
8733   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8734   (with { tree op_type = TREE_TYPE (@3); }
8735    (if (element_precision (type) == element_precision (op_type))
8736     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8737  (simplify
8738   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8739   (with { tree op_type = TREE_TYPE (@5); }
8740    (if (inverse_conditions_p (@0, @2)
8741         && element_precision (type) == element_precision (op_type))
8742     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8744 /* Same for ternary operations.  */
8745 (for cond_op (COND_TERNARY)
8746  (simplify
8747   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8748   (with { tree op_type = TREE_TYPE (@4); }
8749    (if (element_precision (type) == element_precision (op_type))
8750     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8751  (simplify
8752   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8753   (with { tree op_type = TREE_TYPE (@6); }
8754    (if (inverse_conditions_p (@0, @2)
8755         && element_precision (type) == element_precision (op_type))
8756     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8758 /* Detect simplication for a conditional reduction where
8760    a = mask1 ? b : 0
8761    c = mask2 ? d + a : d
8763    is turned into
8765    c = mask1 && mask2 ? d + b : d.  */
8766 (simplify
8767   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8768    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8770 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8771    expressions like:
8773    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8774    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8776    If pointers are known not to wrap, B checks whether @1 bytes starting
8777    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8778    bytes.  A is more efficiently tested as:
8780    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8782    The equivalent expression for B is given by replacing @1 with @1 - 1:
8784    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8786    @0 and @2 can be swapped in both expressions without changing the result.
8788    The folds rely on sizetype's being unsigned (which is always true)
8789    and on its being the same width as the pointer (which we have to check).
8791    The fold replaces two pointer_plus expressions, two comparisons and
8792    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8793    the best case it's a saving of two operations.  The A fold retains one
8794    of the original pointer_pluses, so is a win even if both pointer_pluses
8795    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8796    used elsewhere, since all we end up doing is replacing a comparison with
8797    a pointer_plus.  We do still apply the fold under those circumstances
8798    though, in case applying it to other conditions eventually makes one of the
8799    pointer_pluses dead.  */
8800 (for ior (truth_orif truth_or bit_ior)
8801  (for cmp (le lt)
8802   (simplify
8803    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8804         (cmp:cs (pointer_plus@4 @2 @1) @0))
8805    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8806         && TYPE_OVERFLOW_WRAPS (sizetype)
8807         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8808     /* Calculate the rhs constant.  */
8809     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8810             offset_int rhs = off * 2; }
8811      /* Always fails for negative values.  */
8812      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8813       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8814          pick a canonical order.  This increases the chances of using the
8815          same pointer_plus in multiple checks.  */
8816       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8817               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8818        (if (cmp == LT_EXPR)
8819         (gt (convert:sizetype
8820              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8821                                      { swap_p ? @0 : @2; }))
8822             { rhs_tree; })
8823         (gt (convert:sizetype
8824              (pointer_diff:ssizetype
8825               (pointer_plus { swap_p ? @2 : @0; }
8826                             { wide_int_to_tree (sizetype, off); })
8827               { swap_p ? @0 : @2; }))
8828             { rhs_tree; })))))))))
8830 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8831    element of @1.  */
8832 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8833  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8834   (with { int i = single_nonzero_element (@1); }
8835    (if (i >= 0)
8836     (with { tree elt = vector_cst_elt (@1, i);
8837             tree elt_type = TREE_TYPE (elt);
8838             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8839             tree size = bitsize_int (elt_bits);
8840             tree pos = bitsize_int (elt_bits * i); }
8841      (view_convert
8842       (bit_and:elt_type
8843        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8844        { elt; })))))))
8846 /* Fold reduction of a single nonzero element constructor.  */
8847 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8848   (simplify (reduc (CONSTRUCTOR@0))
8849     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8850                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8851             tree elt = ctor_single_nonzero_element (ctor); }
8852       (if (elt
8853            && !HONOR_SNANS (type)
8854            && !HONOR_SIGNED_ZEROS (type))
8855         { elt; }))))
8857 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8858 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8859             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8860      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8861   (simplify (reduc (op @0 VECTOR_CST@1))
8862     (op (reduc:type @0) (reduc:type @1))))
8864 /* Simplify vector floating point operations of alternating sub/add pairs
8865    into using an fneg of a wider element type followed by a normal add.
8866    under IEEE 754 the fneg of the wider type will negate every even entry
8867    and when doing an add we get a sub of the even and add of every odd
8868    elements.  */
8869 (for plusminus (plus minus)
8870      minusplus (minus plus)
8871  (simplify
8872   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8873    (if (!VECTOR_INTEGER_TYPE_P (type)
8874         && !FLOAT_WORDS_BIG_ENDIAN
8875         /* plus is commutative, while minus is not, so :c can't be used.
8876            Do equality comparisons by hand and at the end pick the operands
8877            from the minus.  */
8878         && (operand_equal_p (@0, @2, 0)
8879             ? operand_equal_p (@1, @3, 0)
8880             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8881    (with
8882     {
8883       /* Build a vector of integers from the tree mask.  */
8884       vec_perm_builder builder;
8885     }
8886     (if (tree_to_vec_perm_builder (&builder, @4))
8887      (with
8888       {
8889         /* Create a vec_perm_indices for the integer vector.  */
8890         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8891         vec_perm_indices sel (builder, 2, nelts);
8892         machine_mode vec_mode = TYPE_MODE (type);
8893         machine_mode wide_mode;
8894         scalar_mode wide_elt_mode;
8895         poly_uint64 wide_nunits;
8896         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8897       }
8898       (if (VECTOR_MODE_P (vec_mode)
8899            && sel.series_p (0, 2, 0, 2)
8900            && sel.series_p (1, 2, nelts + 1, 2)
8901            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
8902            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
8903            && related_vector_mode (vec_mode, wide_elt_mode,
8904                                    wide_nunits).exists (&wide_mode))
8905        (with
8906         {
8907           tree stype
8908             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
8909                                               TYPE_UNSIGNED (type));
8910           tree ntype = build_vector_type_for_mode (stype, wide_mode);
8912           /* The format has to be a non-extended ieee format.  */
8913           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
8914           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
8915         }
8916         (if (TYPE_MODE (stype) != BLKmode
8917              && VECTOR_TYPE_P (ntype)
8918              && fmt_old != NULL
8919              && fmt_new != NULL)
8920          (with
8921           {
8922             /* If the target doesn't support v1xx vectors, try using
8923                scalar mode xx instead.  */
8924             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
8925                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
8926               ntype = stype;
8927           }
8928           (if (fmt_new->signbit_rw
8929                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
8930                && fmt_new->signbit_rw == fmt_new->signbit_ro
8931                && targetm.can_change_mode_class (TYPE_MODE (ntype),
8932                                                  TYPE_MODE (type), ALL_REGS)
8933                && ((optimize_vectors_before_lowering_p ()
8934                     && VECTOR_TYPE_P (ntype))
8935                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
8936            (if (plusminus == PLUS_EXPR)
8937             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
8938             (minus @0 (view_convert:type
8939                         (negate (view_convert:ntype @1))))))))))))))))
8941 (simplify
8942  (vec_perm @0 @1 VECTOR_CST@2)
8943  (with
8944   {
8945     tree op0 = @0, op1 = @1, op2 = @2;
8946     machine_mode result_mode = TYPE_MODE (type);
8947     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
8949     /* Build a vector of integers from the tree mask.  */
8950     vec_perm_builder builder;
8951   }
8952   (if (tree_to_vec_perm_builder (&builder, op2))
8953    (with
8954     {
8955       /* Create a vec_perm_indices for the integer vector.  */
8956       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8957       bool single_arg = (op0 == op1);
8958       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8959     }
8960     (if (sel.series_p (0, 1, 0, 1))
8961      { op0; }
8962      (if (sel.series_p (0, 1, nelts, 1))
8963       { op1; }
8964       (with
8965        {
8966          if (!single_arg)
8967            {
8968              if (sel.all_from_input_p (0))
8969                op1 = op0;
8970              else if (sel.all_from_input_p (1))
8971                {
8972                  op0 = op1;
8973                  sel.rotate_inputs (1);
8974                }
8975              else if (known_ge (poly_uint64 (sel[0]), nelts))
8976                {
8977                  std::swap (op0, op1);
8978                  sel.rotate_inputs (1);
8979                }
8980            }
8981          gassign *def;
8982          tree cop0 = op0, cop1 = op1;
8983          if (TREE_CODE (op0) == SSA_NAME
8984              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
8985              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8986            cop0 = gimple_assign_rhs1 (def);
8987          if (TREE_CODE (op1) == SSA_NAME
8988              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
8989              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8990            cop1 = gimple_assign_rhs1 (def);
8991          tree t;
8992        }
8993        (if ((TREE_CODE (cop0) == VECTOR_CST
8994              || TREE_CODE (cop0) == CONSTRUCTOR)
8995             && (TREE_CODE (cop1) == VECTOR_CST
8996                 || TREE_CODE (cop1) == CONSTRUCTOR)
8997             && (t = fold_vec_perm (type, cop0, cop1, sel)))
8998         { t; }
8999         (with
9000          {
9001            bool changed = (op0 == op1 && !single_arg);
9002            tree ins = NULL_TREE;
9003            unsigned at = 0;
9005            /* See if the permutation is performing a single element
9006               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9007               in that case.  But only if the vector mode is supported,
9008               otherwise this is invalid GIMPLE.  */
9009            if (op_mode != BLKmode
9010                && (TREE_CODE (cop0) == VECTOR_CST
9011                    || TREE_CODE (cop0) == CONSTRUCTOR
9012                    || TREE_CODE (cop1) == VECTOR_CST
9013                    || TREE_CODE (cop1) == CONSTRUCTOR))
9014              {
9015                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9016                if (insert_first_p)
9017                  {
9018                    /* After canonicalizing the first elt to come from the
9019                       first vector we only can insert the first elt from
9020                       the first vector.  */
9021                    at = 0;
9022                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9023                      op0 = op1;
9024                  }
9025                /* The above can fail for two-element vectors which always
9026                   appear to insert the first element, so try inserting
9027                   into the second lane as well.  For more than two
9028                   elements that's wasted time.  */
9029                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9030                  {
9031                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9032                    for (at = 0; at < encoded_nelts; ++at)
9033                      if (maybe_ne (sel[at], at))
9034                        break;
9035                    if (at < encoded_nelts
9036                        && (known_eq (at + 1, nelts)
9037                            || sel.series_p (at + 1, 1, at + 1, 1)))
9038                      {
9039                        if (known_lt (poly_uint64 (sel[at]), nelts))
9040                          ins = fold_read_from_vector (cop0, sel[at]);
9041                        else
9042                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9043                      }
9044                  }
9045              }
9047            /* Generate a canonical form of the selector.  */
9048            if (!ins && sel.encoding () != builder)
9049              {
9050                /* Some targets are deficient and fail to expand a single
9051                   argument permutation while still allowing an equivalent
9052                   2-argument version.  */
9053                tree oldop2 = op2;
9054                if (sel.ninputs () == 2
9055                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9056                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9057                else
9058                  {
9059                    vec_perm_indices sel2 (builder, 2, nelts);
9060                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9061                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9062                    else
9063                      /* Not directly supported with either encoding,
9064                         so use the preferred form.  */
9065                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9066                  }
9067                if (!operand_equal_p (op2, oldop2, 0))
9068                  changed = true;
9069              }
9070          }
9071          (if (ins)
9072           (bit_insert { op0; } { ins; }
9073            { bitsize_int (at * vector_element_bits (type)); })
9074           (if (changed)
9075            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9077 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9079 (match vec_same_elem_p
9080  (vec_duplicate @0))
9082 (match vec_same_elem_p
9083  CONSTRUCTOR@0
9084  (if (TREE_CODE (@0) == SSA_NAME
9085       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9087 (match vec_same_elem_p
9088  @0
9089  (if (uniform_vector_p (@0))))
9092 (simplify
9093  (vec_perm vec_same_elem_p@0 @0 @1)
9094  (if (types_match (type, TREE_TYPE (@0)))
9095   @0
9096   (with
9097    {
9098      tree elem = uniform_vector_p (@0);
9099    }
9100    (if (elem)
9101     { build_vector_from_val (type, elem); }))))
9103 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9104 (simplify
9105  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9106  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9107   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9108 (simplify
9109  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9110  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9111   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9114 /* Merge
9115      c = VEC_PERM_EXPR <a, b, VCST0>;
9116      d = VEC_PERM_EXPR <c, c, VCST1>;
9117    to
9118      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9120 (simplify
9121  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9122  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9123   (with
9124    {
9125      machine_mode result_mode = TYPE_MODE (type);
9126      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9127      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9128      vec_perm_builder builder0;
9129      vec_perm_builder builder1;
9130      vec_perm_builder builder2 (nelts, nelts, 1);
9131    }
9132    (if (tree_to_vec_perm_builder (&builder0, @3)
9133         && tree_to_vec_perm_builder (&builder1, @4))
9134     (with
9135      {
9136        vec_perm_indices sel0 (builder0, 2, nelts);
9137        vec_perm_indices sel1 (builder1, 1, nelts);
9139        for (int i = 0; i < nelts; i++)
9140          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9142        vec_perm_indices sel2 (builder2, 2, nelts);
9144        tree op0 = NULL_TREE;
9145        /* If the new VEC_PERM_EXPR can't be handled but both
9146           original VEC_PERM_EXPRs can, punt.
9147           If one or both of the original VEC_PERM_EXPRs can't be
9148           handled and the new one can't be either, don't increase
9149           number of VEC_PERM_EXPRs that can't be handled.  */
9150        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9151            || (single_use (@0)
9152                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9153                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9154                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9155          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9156      }
9157      (if (op0)
9158       (vec_perm @1 @2 { op0; })))))))
9160 /* Merge
9161      c = VEC_PERM_EXPR <a, b, VCST0>;
9162      d = VEC_PERM_EXPR <x, c, VCST1>;
9163    to
9164      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9165    when all elements from a or b are replaced by the later
9166    permutation.  */
9168 (simplify
9169  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9170  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9171   (with
9172    {
9173      machine_mode result_mode = TYPE_MODE (type);
9174      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9175      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9176      vec_perm_builder builder0;
9177      vec_perm_builder builder1;
9178      vec_perm_builder builder2 (nelts, nelts, 2);
9179    }
9180    (if (tree_to_vec_perm_builder (&builder0, @3)
9181         && tree_to_vec_perm_builder (&builder1, @4))
9182     (with
9183      {
9184        vec_perm_indices sel0 (builder0, 2, nelts);
9185        vec_perm_indices sel1 (builder1, 2, nelts);
9186        bool use_1 = false, use_2 = false;
9188        for (int i = 0; i < nelts; i++)
9189          {
9190            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9191              builder2.quick_push (sel1[i]);
9192            else
9193              {
9194                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9195                                     .to_constant ()];
9196                if (known_lt (j, sel0.nelts_per_input ()))
9197                  use_1 = true;
9198                else
9199                  {
9200                    use_2 = true;
9201                    j -= sel0.nelts_per_input ();
9202                  }
9203                builder2.quick_push (j + sel1.nelts_per_input ());
9204              }
9205          }
9206      }
9207      (if (use_1 ^ use_2)
9208       (with
9209        {
9210          vec_perm_indices sel2 (builder2, 2, nelts);
9211          tree op0 = NULL_TREE;
9212          /* If the new VEC_PERM_EXPR can't be handled but both
9213             original VEC_PERM_EXPRs can, punt.
9214             If one or both of the original VEC_PERM_EXPRs can't be
9215             handled and the new one can't be either, don't increase
9216             number of VEC_PERM_EXPRs that can't be handled.  */
9217          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9218              || (single_use (@0)
9219                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9220                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9221                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9222            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9223        }
9224        (if (op0)
9225         (switch
9226          (if (use_1)
9227           (vec_perm @5 @1 { op0; }))
9228          (if (use_2)
9229           (vec_perm @5 @2 { op0; })))))))))))
9231 /* And the case with swapped outer permute sources.  */
9233 (simplify
9234  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9235  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9236   (with
9237    {
9238      machine_mode result_mode = TYPE_MODE (type);
9239      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9240      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9241      vec_perm_builder builder0;
9242      vec_perm_builder builder1;
9243      vec_perm_builder builder2 (nelts, nelts, 2);
9244    }
9245    (if (tree_to_vec_perm_builder (&builder0, @3)
9246         && tree_to_vec_perm_builder (&builder1, @4))
9247     (with
9248      {
9249        vec_perm_indices sel0 (builder0, 2, nelts);
9250        vec_perm_indices sel1 (builder1, 2, nelts);
9251        bool use_1 = false, use_2 = false;
9253        for (int i = 0; i < nelts; i++)
9254          {
9255            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9256              builder2.quick_push (sel1[i]);
9257            else
9258              {
9259                poly_uint64 j = sel0[sel1[i].to_constant ()];
9260                if (known_lt (j, sel0.nelts_per_input ()))
9261                  use_1 = true;
9262                else
9263                  {
9264                    use_2 = true;
9265                    j -= sel0.nelts_per_input ();
9266                  }
9267                builder2.quick_push (j);
9268              }
9269          }
9270      }
9271      (if (use_1 ^ use_2)
9272       (with
9273        {
9274          vec_perm_indices sel2 (builder2, 2, nelts);
9275          tree op0 = NULL_TREE;
9276          /* If the new VEC_PERM_EXPR can't be handled but both
9277             original VEC_PERM_EXPRs can, punt.
9278             If one or both of the original VEC_PERM_EXPRs can't be
9279             handled and the new one can't be either, don't increase
9280             number of VEC_PERM_EXPRs that can't be handled.  */
9281          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9282              || (single_use (@0)
9283                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9284                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9285                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9286            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9287        }
9288        (if (op0)
9289         (switch
9290          (if (use_1)
9291           (vec_perm @1 @5 { op0; }))
9292          (if (use_2)
9293           (vec_perm @2 @5 { op0; })))))))))))
9296 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9297    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9298    constant which when multiplied by a power of 2 contains a unique value
9299    in the top 5 or 6 bits.  This is then indexed into a table which maps it
9300    to the number of trailing zeroes.  */
9301 (match (ctz_table_index @1 @2 @3)
9302   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9304 (match (cond_expr_convert_p @0 @2 @3 @6)
9305  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9306   (if (INTEGRAL_TYPE_P (type)
9307        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9308        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9309        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9310        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9311        && TYPE_PRECISION (TREE_TYPE (@0))
9312           == TYPE_PRECISION (TREE_TYPE (@2))
9313        && TYPE_PRECISION (TREE_TYPE (@0))
9314           == TYPE_PRECISION (TREE_TYPE (@3))
9315        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9316           signess when convert is truncation, but not ok for extension since
9317           it's sign_extend vs zero_extend.  */
9318        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9319            || (TYPE_UNSIGNED (TREE_TYPE (@2))
9320                == TYPE_UNSIGNED (TREE_TYPE (@3))))
9321        && single_use (@4)
9322        && single_use (@5))))
9324 (for bit_op (bit_and bit_ior bit_xor)
9325  (match (bitwise_induction_p @0 @2 @3)
9326   (bit_op:c
9327    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9328    @3)))
9330 (match (bitwise_induction_p @0 @2 @3)
9331  (bit_not
9332   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9334 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
9335    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
9336 (simplify
9337   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9338   (with { auto i = wi::neg (wi::to_wide (@2)); }
9339   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
9340     (if (wi::popcount (i) == 1
9341          && (wi::to_wide (@1)) == (i - 1))
9342       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9343         (bit_and @0 @1)
9344       (cond (le @0 @1) @0 (bit_and @0 @1))))))
9346 /* -x & 1 -> x & 1.  */
9347 (simplify 
9348  (bit_and (negate @0) integer_onep@1)
9349  (if (!TYPE_OVERFLOW_SANITIZED (type))
9350   (bit_and @0 @1)))
9352 /* `-a` is just `a` if the type is 1bit wide or when converting
9353    to a 1bit type; similar to the above transformation of `(-x)&1`.
9354    This is used mostly with the transformation of
9355    `a ? ~b : b` into `(-a)^b`.
9356    It also can show up with bitfields.  */
9357 (simplify
9358  (convert? (negate @0))
9359  (if (INTEGRAL_TYPE_P (type)
9360       && TYPE_PRECISION (type) == 1
9361       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9362   (convert @0)))
9364 /* Optimize
9365    c1 = VEC_PERM_EXPR (a, a, mask)
9366    c2 = VEC_PERM_EXPR (b, b, mask)
9367    c3 = c1 op c2
9368    -->
9369    c = a op b
9370    c3 = VEC_PERM_EXPR (c, c, mask)
9371    For all integer non-div operations.  */
9372 (for op (plus minus mult bit_and bit_ior bit_xor
9373          lshift rshift)
9374  (simplify
9375   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9376    (if (VECTOR_INTEGER_TYPE_P (type))
9377     (vec_perm (op@3 @0 @1) @3 @2))))
9379 /* Similar for float arithmetic when permutation constant covers
9380    all vector elements.  */
9381 (for op (plus minus mult)
9382  (simplify
9383   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9384    (if (VECTOR_FLOAT_TYPE_P (type)
9385         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9386     (with
9387      {
9388        tree perm_cst = @2;
9389        vec_perm_builder builder;
9390        bool full_perm_p = false;
9391        if (tree_to_vec_perm_builder (&builder, perm_cst))
9392          {
9393            unsigned HOST_WIDE_INT nelts;
9395            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9396            /* Create a vec_perm_indices for the VECTOR_CST.  */
9397            vec_perm_indices sel (builder, 1, nelts);
9399            /* Check if perm indices covers all vector elements.  */
9400            if (sel.encoding ().encoded_full_vector_p ())
9401              {
9402                auto_sbitmap seen (nelts);
9403                bitmap_clear (seen);
9405                unsigned HOST_WIDE_INT count = 0, i;
9407                for (i = 0; i < nelts; i++)
9408                  {
9409                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9410                     break;
9411                    count++;
9412                  }
9413                full_perm_p = count == nelts;
9414              }
9415          }
9416       }
9417       (if (full_perm_p)
9418         (vec_perm (op@3 @0 @1) @3 @2))))))