ada: Update personality function for CHERI purecap
[official-gcc.git] / gcc / match.pd
bloba17778fbaa616399f4cdc0a91e75055a2be2d4e9
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2023 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
91 /* Binary operations and their associated IFN_COND_* function.  */
92 (define_operator_list UNCOND_BINARY
93   plus minus
94   mult trunc_div trunc_mod rdiv
95   min max
96   IFN_FMIN IFN_FMAX
97   bit_and bit_ior bit_xor
98   lshift rshift)
99 (define_operator_list COND_BINARY
100   IFN_COND_ADD IFN_COND_SUB
101   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102   IFN_COND_MIN IFN_COND_MAX
103   IFN_COND_FMIN IFN_COND_FMAX
104   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105   IFN_COND_SHL IFN_COND_SHR)
107 /* Same for ternary operations.  */
108 (define_operator_list UNCOND_TERNARY
109   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117   BUILT_IN_ATOMIC_FETCH_OR_16
118   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120   BUILT_IN_ATOMIC_FETCH_XOR_16
121   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123   BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128   BUILT_IN_SYNC_FETCH_AND_OR_16
129   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131   BUILT_IN_SYNC_FETCH_AND_XOR_16
132   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134   BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*.  */
136 (define_operator_list ATOMIC_FETCH_AND_N
137   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139   BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*.  */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144   BUILT_IN_SYNC_FETCH_AND_AND_16)
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147    plus conditionalize on tree_nop_conversion_p conversions.  */
148 (match (nop_convert @0)
149  (convert @0)
150  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152  (view_convert @0)
153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
158 #if GIMPLE
159 /* These are used by gimple_bitwise_inverted_equal_p to simplify
160    detection of BIT_NOT and comparisons. */
161 (match (bit_not_with_nop @0)
162  (bit_not @0))
163 (match (bit_not_with_nop @0)
164  (convert (bit_not @0))
165  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
166 (for cmp (tcc_comparison)
167  (match (maybe_cmp @0)
168   (cmp@0 @1 @2))
169  (match (maybe_cmp @0)
170   (convert (cmp@0 @1 @2))
171    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
173 #endif
175 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
176    ABSU_EXPR returns unsigned absolute value of the operand and the operand
177    of the ABSU_EXPR will have the corresponding signed type.  */
178 (simplify (abs (convert @0))
179  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
180       && !TYPE_UNSIGNED (TREE_TYPE (@0))
181       && element_precision (type) > element_precision (TREE_TYPE (@0)))
182   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
183    (convert (absu:utype @0)))))
185 #if GIMPLE
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
187 (simplify
188  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
189  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
190       && !TYPE_UNSIGNED (TREE_TYPE (@0))
191       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
192   (abs @0)))
193 #endif
195 /* Simplifications of operations with one constant operand and
196    simplifications to constants or single values.  */
198 (for op (plus pointer_plus minus bit_ior bit_xor)
199   (simplify
200     (op @0 integer_zerop)
201     (non_lvalue @0)))
203 /* 0 +p index -> (type)index */
204 (simplify
205  (pointer_plus integer_zerop @1)
206  (non_lvalue (convert @1)))
208 /* ptr - 0 -> (type)ptr */
209 (simplify
210  (pointer_diff @0 integer_zerop)
211  (convert @0))
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214    Likewise if the operands are reversed.  */
215 (simplify
216  (plus:c @0 real_zerop@1)
217  (if (fold_real_zero_addition_p (type, @0, @1, 0))
218   (non_lvalue @0)))
220 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
221 (simplify
222  (minus @0 real_zerop@1)
223  (if (fold_real_zero_addition_p (type, @0, @1, 1))
224   (non_lvalue @0)))
226 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
227    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
228    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
229    if not -frounding-math.  For sNaNs the first operation would raise
230    exceptions but turn the result into qNan, so the second operation
231    would not raise it.   */
232 (for inner_op (plus minus)
233  (for outer_op (plus minus)
234   (simplify
235    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
236     (if (real_zerop (@1)
237          && real_zerop (@2)
238          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
239      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
240                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
241              bool outer_plus
242                = ((outer_op == PLUS_EXPR)
243                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244       (if (outer_plus && !inner_plus)
245        (outer_op @0 @2)
246        @3))))))
248 /* Simplify x - x.
249    This is unsafe for certain floats even in non-IEEE formats.
250    In IEEE, it is unsafe because it does wrong for NaNs.
251    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
252    Also note that operand_equal_p is always false if an operand
253    is volatile.  */
254 (simplify
255  (minus @0 @0)
256  (if (!FLOAT_TYPE_P (type)
257       || (!tree_expr_maybe_nan_p (@0)
258           && !tree_expr_maybe_infinite_p (@0)
259           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
260               || !HONOR_SIGNED_ZEROS (type))))
261   { build_zero_cst (type); }))
262 (simplify
263  (pointer_diff @@0 @0)
264  { build_zero_cst (type); })
266 (simplify
267  (mult @0 integer_zerop@1)
268  @1)
270 /* -x == x -> x == 0 */
271 (for cmp (eq ne)
272  (simplify
273   (cmp:c @0 (negate @0))
274    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
275         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
276     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
278 /* Maybe fold x * 0 to 0.  The expressions aren't the same
279    when x is NaN, since x * 0 is also NaN.  Nor are they the
280    same in modes with signed zeros, since multiplying a
281    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
282    since x * 0 is NaN.  */
283 (simplify
284  (mult @0 real_zerop@1)
285  (if (!tree_expr_maybe_nan_p (@0)
286       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
287       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
288   @1))
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291    Likewise for complex arithmetic with signed zeros.  */
292 (simplify
293  (mult @0 real_onep)
294  (if (!tree_expr_maybe_signaling_nan_p (@0)
295       && (!HONOR_SIGNED_ZEROS (type)
296           || !COMPLEX_FLOAT_TYPE_P (type)))
297   (non_lvalue @0)))
299 /* Transform x * -1.0 into -x.  */
300 (simplify
301  (mult @0 real_minus_onep)
302   (if (!tree_expr_maybe_signaling_nan_p (@0)
303        && (!HONOR_SIGNED_ZEROS (type)
304            || !COMPLEX_FLOAT_TYPE_P (type)))
305    (negate @0)))
307 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
308    unless the target has native support for the former but not the latter.  */
309 (simplify
310  (mult @0 VECTOR_CST@1)
311  (if (initializer_each_zero_or_onep (@1)
312       && !HONOR_SNANS (type)
313       && !HONOR_SIGNED_ZEROS (type))
314   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
315    (if (itype
316         && (!VECTOR_MODE_P (TYPE_MODE (type))
317             || (VECTOR_MODE_P (TYPE_MODE (itype))
318                 && optab_handler (and_optab,
319                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
320     (view_convert (bit_and:itype (view_convert @0)
321                                  (ne @1 { build_zero_cst (type); })))))))
323 /* In SWAR (SIMD within a register) code a signed comparison of packed data
324    can be constructed with a particular combination of shift, bitwise and,
325    and multiplication by constants.  If that code is vectorized we can
326    convert this pattern into a more efficient vector comparison.  */
327 (simplify
328  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329             uniform_integer_cst_p@2)
330     uniform_integer_cst_p@3)
331  (with {
332    tree rshift_cst = uniform_integer_cst_p (@1);
333    tree bit_and_cst = uniform_integer_cst_p (@2);
334    tree mult_cst = uniform_integer_cst_p (@3);
335   }
336   /* Make sure we're working with vectors and uniform vector constants.  */
337   (if (VECTOR_TYPE_P (type)
338        && tree_fits_uhwi_p (rshift_cst)
339        && tree_fits_uhwi_p (mult_cst)
340        && tree_fits_uhwi_p (bit_and_cst))
341    /* Compute what constants would be needed for this to represent a packed
342       comparison based on the shift amount denoted by RSHIFT_CST.  */
343    (with {
344      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
345      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
346      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
347      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
348      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
349      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
350      mult_i = tree_to_uhwi (mult_cst);
351      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
352      bit_and_i = tree_to_uhwi (bit_and_cst);
353      target_bit_and_i = 0;
355      /* The bit pattern in BIT_AND_I should be a mask for the least
356         significant bit of each packed element that is CMP_BITS wide.  */
357      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
358        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
359     }
360     (if ((exact_log2 (cmp_bits_i)) >= 0
361          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
362          && multiple_p (vec_bits, cmp_bits_i)
363          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
364          && target_mult_i == mult_i
365          && target_bit_and_i == bit_and_i)
366      /* Compute the vector shape for the comparison and check if the target is
367         able to expand the comparison with that type.  */
368      (with {
369        /* We're doing a signed comparison.  */
370        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
371        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
372        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
373        tree vec_truth_type = truth_type_for (vec_cmp_type);
374        tree zeros = build_zero_cst (vec_cmp_type);
375        tree ones = build_all_ones_cst (vec_cmp_type);
376       }
377       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
378            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
379        (view_convert:type (vec_cond (lt:vec_truth_type
380                                      (view_convert:vec_cmp_type @0)
381                                      { zeros; })
382                            { ones; } { zeros; })))))))))
384 (for cmp (gt ge lt le)
385      outp (convert convert negate negate)
386      outn (negate negate convert convert)
387  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
388  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
389  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
390  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
391  (simplify
392   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
393   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
394    (outp (abs @0))))
395  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
396  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
397  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
398  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
399  (simplify
400   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
401   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
402    (outn (abs @0)))))
404 /* Transform X * copysign (1.0, X) into abs(X). */
405 (simplify
406  (mult:c @0 (COPYSIGN_ALL real_onep @0))
407  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
408   (abs @0)))
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
411 (simplify
412  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414   (negate (abs @0))))
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
417 (simplify
418  (COPYSIGN_ALL REAL_CST@0 @1)
419  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420   (COPYSIGN_ALL (negate @0) @1)))
422 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
423    tree-ssa-math-opts.cc does the corresponding optimization for
424    unconditional multiplications (via xorsign).  */
425 (simplify
426  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427  (with { tree signs = sign_mask_for (type); }
428   (if (signs)
429    (with { tree inttype = TREE_TYPE (signs); }
430     (view_convert:type
431      (IFN_COND_XOR:inttype @0
432       (view_convert:inttype @1)
433       (bit_and (view_convert:inttype @2) { signs; })
434       (view_convert:inttype @3)))))))
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
437 (simplify
438   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
439   (abs @0))
441 /* X * 1, X / 1 -> X.  */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
443   (simplify
444     (op @0 integer_onep)
445     (non_lvalue @0)))
447 /* (A / (1 << B)) -> (A >> B).
448    Only for unsigned A.  For signed A, this would not preserve rounding
449    toward zero.
450    For example: (-1 / ( 1 << B)) !=  -1 >> B.
451    Also handle widening conversions, like:
452    (A / (unsigned long long) (1U << B)) -> (A >> B)
453    or
454    (A / (unsigned long long) (1 << B)) -> (A >> B).
455    If the left shift is signed, it can be done only if the upper bits
456    of A starting from shift's type sign bit are zero, as
457    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
458    so it is valid only if A >> 31 is zero.  */
459 (simplify
460  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
461  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
462       && (!VECTOR_TYPE_P (type)
463           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
464           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
465       && (useless_type_conversion_p (type, TREE_TYPE (@1))
466           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
467               && (TYPE_UNSIGNED (TREE_TYPE (@1))
468                   || (element_precision (type)
469                       == element_precision (TREE_TYPE (@1)))
470                   || (INTEGRAL_TYPE_P (type)
471                       && (tree_nonzero_bits (@0)
472                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
473                                       true,
474                                       element_precision (type))) == 0)))))
475    (if (!VECTOR_TYPE_P (type)
476         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
477         && element_precision (TREE_TYPE (@3)) < element_precision (type))
478     (convert (rshift @3 @2))
479     (rshift @0 @2))))
481 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
482    undefined behavior in constexpr evaluation, and assuming that the division
483    traps enables better optimizations than these anyway.  */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
485  /* 0 / X is always zero.  */
486  (simplify
487   (div integer_zerop@0 @1)
488   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
489   (if (!integer_zerop (@1))
490    @0))
491  /* X / -1 is -X.  */
492  (simplify
493   (div @0 integer_minus_onep@1)
494   (if (!TYPE_UNSIGNED (type))
495    (negate @0)))
496  /* X / bool_range_Y is X.  */ 
497  (simplify
498   (div @0 SSA_NAME@1)
499   (if (INTEGRAL_TYPE_P (type)
500        && ssa_name_has_boolean_range (@1)
501        && !flag_non_call_exceptions)
502    @0))
503  /* X / X is one.  */
504  (simplify
505   (div @0 @0)
506   /* But not for 0 / 0 so that we can get the proper warnings and errors.
507      And not for _Fract types where we can't build 1.  */
508   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
509        && !integer_zerop (@0)
510        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511    { build_one_cst (type); }))
512  /* X / abs (X) is X < 0 ? -1 : 1.  */
513  (simplify
514    (div:C @0 (abs @0))
515    (if (INTEGRAL_TYPE_P (type)
516         && TYPE_OVERFLOW_UNDEFINED (type)
517         && !integer_zerop (@0)
518         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
519     (cond (lt @0 { build_zero_cst (type); })
520           { build_minus_one_cst (type); } { build_one_cst (type); })))
521  /* X / -X is -1.  */
522  (simplify
523    (div:C @0 (negate @0))
524    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
525         && TYPE_OVERFLOW_UNDEFINED (type)
526         && !integer_zerop (@0)
527         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
528     { build_minus_one_cst (type); })))
530 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
531    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
532    for MOD instead of DIV.  */
533 (for floor_divmod (floor_div floor_mod)
534      trunc_divmod (trunc_div trunc_mod)
535  (simplify
536   (floor_divmod @0 @1)
537   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538        && TYPE_UNSIGNED (type))
539    (trunc_divmod @0 @1))))
541 /* 1 / X -> X == 1 for unsigned integer X.
542    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
543    But not for 1 / 0 so that we can get proper warnings and errors,
544    and not for 1-bit integers as they are edge cases better handled
545    elsewhere.  */
546 (simplify
547  (trunc_div integer_onep@0 @1)
548  (if (INTEGRAL_TYPE_P (type)
549       && TYPE_PRECISION (type) > 1
550       && !integer_zerop (@1)
551       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
552   (if (TYPE_UNSIGNED (type))
553    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
554    (with { tree utype = unsigned_type_for (type); }
555     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
556               { build_int_cst (utype, 2); })
557      @1 { build_zero_cst (type); })))))
559 /* Combine two successive divisions.  Note that combining ceil_div
560    and floor_div is trickier and combining round_div even more so.  */
561 (for div (trunc_div exact_div)
562  (simplify
563   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
564   (with {
565     wi::overflow_type overflow;
566     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567                             TYPE_SIGN (type), &overflow);
568    }
569    (if (div == EXACT_DIV_EXPR
570         || optimize_successive_divisions_p (@2, @3))
571     (if (!overflow)
572      (div @0 { wide_int_to_tree (type, mul); })
573      (if (TYPE_UNSIGNED (type)
574           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
575       { build_zero_cst (type); }))))))
577 /* Combine successive multiplications.  Similar to above, but handling
578    overflow is different.  */
579 (simplify
580  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
581  (with {
582    wi::overflow_type overflow;
583    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584                            TYPE_SIGN (type), &overflow);
585   }
586   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
587      otherwise undefined overflow implies that @0 must be zero.  */
588   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
589    (mult @0 { wide_int_to_tree (type, mul); }))))
591 /* Similar to above, but there could be an extra add/sub between
592    successive multuiplications.  */
593 (simplify
594  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
595  (with {
596    bool overflowed = true;
597    wi::overflow_type ovf1, ovf2;
598    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
599                            TYPE_SIGN (type), &ovf1);
600    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
601                            TYPE_SIGN (type), &ovf2);
602   if (TYPE_OVERFLOW_UNDEFINED (type))
603     {
604 #if GIMPLE
605       value_range vr0;
606       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
607           && get_global_range_query ()->range_of_expr (vr0, @4)
608           && !vr0.varying_p () && !vr0.undefined_p ())
609         {
610           wide_int wmin0 = vr0.lower_bound ();
611           wide_int wmax0 = vr0.upper_bound ();
612           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
613           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
614           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
615             {
616               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
617               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
618               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
619                 overflowed = false;
620             }
621         }
622 #endif
623     }
624   else
625    overflowed = false;
627   /* Skip folding on overflow.  */
628   (if (!overflowed)
629    (plus (mult @0 { wide_int_to_tree (type, mul); })
630          { wide_int_to_tree (type, add); }))))
632 /* Similar to above, but a multiplication between successive additions.  */
633 (simplify
634  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
635  (with {
636    bool overflowed = true;
637    wi::overflow_type ovf1;
638    wi::overflow_type ovf2;
639    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
640                            TYPE_SIGN (type), &ovf1);
641    wide_int add = wi::add (mul, wi::to_wide (@3),
642                            TYPE_SIGN (type), &ovf2);
643   if (TYPE_OVERFLOW_UNDEFINED (type))
644     {
645 #if GIMPLE
646       value_range vr0;
647       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
648           && get_global_range_query ()->range_of_expr (vr0, @0)
649           && !vr0.varying_p () && !vr0.undefined_p ())
650         {
651           wide_int wmin0 = vr0.lower_bound ();
652           wide_int wmax0 = vr0.upper_bound ();
653           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
654           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
655           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
656             {
657               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
658               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
659               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
660                 overflowed = false;
661             }
662         }
663 #endif
664     }
665   else
666    overflowed = false;
668   /* Skip folding on overflow.  */
669   (if (!overflowed)
670    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
672 /* Optimize A / A to 1.0 if we don't care about
673    NaNs or Infinities.  */
674 (simplify
675  (rdiv @0 @0)
676  (if (FLOAT_TYPE_P (type)
677       && ! HONOR_NANS (type)
678       && ! HONOR_INFINITIES (type))
679   { build_one_cst (type); }))
681 /* Optimize -A / A to -1.0 if we don't care about
682    NaNs or Infinities.  */
683 (simplify
684  (rdiv:C @0 (negate @0))
685  (if (FLOAT_TYPE_P (type)
686       && ! HONOR_NANS (type)
687       && ! HONOR_INFINITIES (type))
688   { build_minus_one_cst (type); }))
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
691 (simplify
692  (rdiv:C (convert? @0) (convert? (abs @0)))
693   (if (SCALAR_FLOAT_TYPE_P (type)
694        && ! HONOR_NANS (type)
695        && ! HONOR_INFINITIES (type))
696    (switch
697     (if (types_match (type, float_type_node))
698      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
699     (if (types_match (type, double_type_node))
700      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
701     (if (types_match (type, long_double_type_node))
702      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
704 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
705 (simplify
706  (rdiv @0 real_onep)
707  (if (!tree_expr_maybe_signaling_nan_p (@0))
708   (non_lvalue @0)))
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
711 (simplify
712  (rdiv @0 real_minus_onep)
713  (if (!tree_expr_maybe_signaling_nan_p (@0))
714   (negate @0)))
716 (if (flag_reciprocal_math)
717  /* Convert (A/B)/C to A/(B*C). */
718  (simplify
719   (rdiv (rdiv:s @0 @1) @2)
720   (rdiv @0 (mult @1 @2)))
722  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
723  (simplify
724   (rdiv @0 (mult:s @1 REAL_CST@2))
725   (with
726    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
727    (if (tem)
728     (rdiv (mult @0 { tem; } ) @1))))
730  /* Convert A/(B/C) to (A/B)*C  */
731  (simplify
732   (rdiv @0 (rdiv:s @1 @2))
733    (mult (rdiv @0 @1) @2)))
735 /* Simplify x / (- y) to -x / y.  */
736 (simplify
737  (rdiv @0 (negate @1))
738  (rdiv (negate @0) @1))
740 (if (flag_unsafe_math_optimizations)
741  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
742     Since C / x may underflow to zero, do this only for unsafe math.  */
743  (for op (lt le gt ge)
744       neg_op (gt ge lt le)
745   (simplify
746    (op (rdiv REAL_CST@0 @1) real_zerop@2)
747    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
748     (switch
749      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
750       (op @1 @2))
751      /* For C < 0, use the inverted operator.  */
752      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
753       (neg_op @1 @2)))))))
755 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
756 (for div (trunc_div ceil_div floor_div round_div exact_div)
757  (simplify
758   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
759   (if (integer_pow2p (@2)
760        && tree_int_cst_sgn (@2) > 0
761        && tree_nop_conversion_p (type, TREE_TYPE (@0))
762        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
763    (rshift (convert @0)
764            { build_int_cst (integer_type_node,
765                             wi::exact_log2 (wi::to_wide (@2))); }))))
767 /* If ARG1 is a constant, we can convert this to a multiply by the
768    reciprocal.  This does not have the same rounding properties,
769    so only do this if -freciprocal-math.  We can actually
770    always safely do it if ARG1 is a power of two, but it's hard to
771    tell if it is or not in a portable manner.  */
772 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
773  (simplify
774   (rdiv @0 cst@1)
775   (if (optimize)
776    (if (flag_reciprocal_math
777         && !real_zerop (@1))
778     (with
779      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
780      (if (tem)
781       (mult @0 { tem; } )))
782     (if (cst != COMPLEX_CST)
783      (with { tree inverse = exact_inverse (type, @1); }
784       (if (inverse)
785        (mult @0 { inverse; } ))))))))
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788  /* 0 % X is always zero.  */
789  (simplify
790   (mod integer_zerop@0 @1)
791   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
792   (if (!integer_zerop (@1))
793    @0))
794  /* X % 1 is always zero.  */
795  (simplify
796   (mod @0 integer_onep)
797   { build_zero_cst (type); })
798  /* X % -1 is zero.  */
799  (simplify
800   (mod @0 integer_minus_onep@1)
801   (if (!TYPE_UNSIGNED (type))
802    { build_zero_cst (type); }))
803  /* X % X is zero.  */
804  (simplify
805   (mod @0 @0)
806   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
807   (if (!integer_zerop (@0))
808    { build_zero_cst (type); }))
809  /* (X % Y) % Y is just X % Y.  */
810  (simplify
811   (mod (mod@2 @0 @1) @1)
812   @2)
813  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
814  (simplify
815   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
816   (if (ANY_INTEGRAL_TYPE_P (type)
817        && TYPE_OVERFLOW_UNDEFINED (type)
818        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
819                              TYPE_SIGN (type)))
820    { build_zero_cst (type); }))
821  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
822     modulo and comparison, since it is simpler and equivalent.  */
823  (for cmp (eq ne)
824   (simplify
825    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
826    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
827     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
828      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
830 /* X % -C is the same as X % C.  */
831 (simplify
832  (trunc_mod @0 INTEGER_CST@1)
833   (if (TYPE_SIGN (type) == SIGNED
834        && !TREE_OVERFLOW (@1)
835        && wi::neg_p (wi::to_wide (@1))
836        && !TYPE_OVERFLOW_TRAPS (type)
837        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
838        && !sign_bit_p (@1, @1))
839    (trunc_mod @0 (negate @1))))
841 /* X % -Y is the same as X % Y.  */
842 (simplify
843  (trunc_mod @0 (convert? (negate @1)))
844  (if (INTEGRAL_TYPE_P (type)
845       && !TYPE_UNSIGNED (type)
846       && !TYPE_OVERFLOW_TRAPS (type)
847       && tree_nop_conversion_p (type, TREE_TYPE (@1))
848       /* Avoid this transformation if X might be INT_MIN or
849          Y might be -1, because we would then change valid
850          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
851       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
852           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
853                                                         (TREE_TYPE (@1))))))
854   (trunc_mod @0 (convert @1))))
856 /* X - (X / Y) * Y is the same as X % Y.  */
857 (simplify
858  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
859  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
860   (convert (trunc_mod @0 @1))))
862 /* x * (1 + y / x) - y -> x - y % x */
863 (simplify
864  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
865  (if (INTEGRAL_TYPE_P (type))
866   (minus @0 (trunc_mod @1 @0))))
868 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
869    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
870    Also optimize A % (C << N)  where C is a power of 2,
871    to A & ((C << N) - 1).
872    Also optimize "A shift (B % C)", if C is a power of 2, to
873    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
874    and assume (B % C) is nonnegative as shifts negative values would
875    be UB.  */
876 (match (power_of_two_cand @1)
877  INTEGER_CST@1)
878 (match (power_of_two_cand @1)
879  (lshift INTEGER_CST@1 @2))
880 (for mod (trunc_mod floor_mod)
881  (for shift (lshift rshift)
882   (simplify
883    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
884    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
885     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
886                                                       1); }))))))
887  (simplify
888   (mod @0 (convert? (power_of_two_cand@1 @2)))
889   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
890        /* Allow any integral conversions of the divisor, except
891           conversion from narrower signed to wider unsigned type
892           where if @1 would be negative power of two, the divisor
893           would not be a power of two.  */
894        && INTEGRAL_TYPE_P (type)
895        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
896        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
897            || TYPE_UNSIGNED (TREE_TYPE (@1))
898            || !TYPE_UNSIGNED (type))
899        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
900    (with { tree utype = TREE_TYPE (@1);
901            if (!TYPE_OVERFLOW_WRAPS (utype))
902              utype = unsigned_type_for (utype); }
903     (bit_and @0 (convert (minus (convert:utype @1)
904                                 { build_one_cst (utype); })))))))
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
907 (simplify
908  (trunc_div (mult @0 integer_pow2p@1) @1)
909  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
910   (bit_and @0 { wide_int_to_tree
911                 (type, wi::mask (TYPE_PRECISION (type)
912                                  - wi::exact_log2 (wi::to_wide (@1)),
913                                  false, TYPE_PRECISION (type))); })))
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
916 (simplify
917  (mult (trunc_div @0 integer_pow2p@1) @1)
918  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
919   (bit_and @0 (negate @1))))
921 /* Simplify (t * 2) / 2) -> t.  */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
923  (simplify
924   (div (mult:c @0 @1) @1)
925   (if (ANY_INTEGRAL_TYPE_P (type))
926    (if (TYPE_OVERFLOW_UNDEFINED (type))
927     @0
928 #if GIMPLE
929     (with {value_range vr0, vr1;}
930      (if (INTEGRAL_TYPE_P (type)
931           && get_range_query (cfun)->range_of_expr (vr0, @0)
932           && get_range_query (cfun)->range_of_expr (vr1, @1)
933           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
934       @0))
935 #endif
936    ))))
938 #if GIMPLE
939 (for div (trunc_div exact_div)
940  /* Simplify (X + M*N) / N -> X / N + M.  */
941  (simplify
942   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
943   (with {value_range vr0, vr1, vr2, vr3, vr4;}
944   (if (INTEGRAL_TYPE_P (type)
945        && get_range_query (cfun)->range_of_expr (vr1, @1)
946        && get_range_query (cfun)->range_of_expr (vr2, @2)
947        /* "N*M" doesn't overflow.  */
948        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
949        && get_range_query (cfun)->range_of_expr (vr0, @0)
950        && get_range_query (cfun)->range_of_expr (vr3, @3)
951        /* "X+(N*M)" doesn't overflow.  */
952        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
953        && get_range_query (cfun)->range_of_expr (vr4, @4)
954        && !vr4.undefined_p ()
955        /* "X+N*M" is not with opposite sign as "X".  */
956        && (TYPE_UNSIGNED (type)
957            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
958            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
959   (plus (div @0 @2) @1))))
961  /* Simplify (X - M*N) / N -> X / N - M.  */
962  (simplify
963   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
964   (with {value_range vr0, vr1, vr2, vr3, vr4;}
965   (if (INTEGRAL_TYPE_P (type)
966        && get_range_query (cfun)->range_of_expr (vr1, @1)
967        && get_range_query (cfun)->range_of_expr (vr2, @2)
968        /* "N * M" doesn't overflow.  */
969        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
970        && get_range_query (cfun)->range_of_expr (vr0, @0)
971        && get_range_query (cfun)->range_of_expr (vr3, @3)
972        /* "X - (N*M)" doesn't overflow.  */
973        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
974        && get_range_query (cfun)->range_of_expr (vr4, @4)
975        && !vr4.undefined_p ()
976        /* "X-N*M" is not with opposite sign as "X".  */
977        && (TYPE_UNSIGNED (type)
978            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
979            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
980   (minus (div @0 @2) @1)))))
982 /* Simplify
983    (X + C) / N -> X / N + C / N where C is multiple of N.
984    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
985 (for op (trunc_div exact_div rshift)
986  (simplify
987   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
988    (with
989     {
990       wide_int c = wi::to_wide (@1);
991       wide_int n = wi::to_wide (@2);
992       bool shift = op == RSHIFT_EXPR;
993 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
994                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
995 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
996                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
997       value_range vr0, vr1, vr3;
998     }
999     (if (INTEGRAL_TYPE_P (type)
1000          && get_range_query (cfun)->range_of_expr (vr0, @0))
1001      (if (exact_mod (c)
1002           && get_range_query (cfun)->range_of_expr (vr1, @1)
1003           /* "X+C" doesn't overflow.  */
1004           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1005           && get_range_query (cfun)->range_of_expr (vr3, @3)
1006           && !vr3.undefined_p ()
1007           /* "X+C" and "X" are not of opposite sign.  */
1008           && (TYPE_UNSIGNED (type)
1009               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1010               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1011        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1012        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1013             && exact_mod (-c)
1014             /* unsigned "X-(-C)" doesn't underflow.  */
1015             && wi::geu_p (vr0.lower_bound (), -c))
1016          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1017 #undef plus_op1
1018 #undef exact_mod
1019 #endif
1021 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1022    if var is smaller in precision.
1023    This is always safe for both doing the negative in signed or unsigned
1024    as the value for undefined will not show up.  */
1025 (simplify
1026  (convert (negate:s@1 (convert:s @0)))
1027  (if (INTEGRAL_TYPE_P (type)
1028       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1029       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1030     (negate (convert @0))))
1032 (for op (negate abs)
1033  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1034  (for coss (COS COSH)
1035   (simplify
1036    (coss (op @0))
1037     (coss @0)))
1038  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1039  (for pows (POW)
1040   (simplify
1041    (pows (op @0) REAL_CST@1)
1042    (with { HOST_WIDE_INT n; }
1043     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1044      (pows @0 @1)))))
1045  /* Likewise for powi.  */
1046  (for pows (POWI)
1047   (simplify
1048    (pows (op @0) INTEGER_CST@1)
1049    (if ((wi::to_wide (@1) & 1) == 0)
1050     (pows @0 @1))))
1051  /* Strip negate and abs from both operands of hypot.  */
1052  (for hypots (HYPOT)
1053   (simplify
1054    (hypots (op @0) @1)
1055    (hypots @0 @1))
1056   (simplify
1057    (hypots @0 (op @1))
1058    (hypots @0 @1)))
1059  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1060  (for copysigns (COPYSIGN_ALL)
1061   (simplify
1062    (copysigns (op @0) @1)
1063    (copysigns @0 @1))))
1065 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1066 (simplify
1067  (mult (abs@1 @0) @1)
1068  (mult @0 @0))
1070 /* Convert absu(x)*absu(x) -> x*x.  */
1071 (simplify
1072  (mult (absu@1 @0) @1)
1073  (mult (convert@2 @0) @2))
1075 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1076 (for coss (COS COSH)
1077      copysigns (COPYSIGN)
1078  (simplify
1079   (coss (copysigns @0 @1))
1080    (coss @0)))
1082 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1083 (for pows (POW)
1084      copysigns (COPYSIGN)
1085  (simplify
1086   (pows (copysigns @0 @2) REAL_CST@1)
1087   (with { HOST_WIDE_INT n; }
1088    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1089     (pows @0 @1)))))
1090 /* Likewise for powi.  */
1091 (for pows (POWI)
1092      copysigns (COPYSIGN)
1093  (simplify
1094   (pows (copysigns @0 @2) INTEGER_CST@1)
1095   (if ((wi::to_wide (@1) & 1) == 0)
1096    (pows @0 @1))))
1098 (for hypots (HYPOT)
1099      copysigns (COPYSIGN)
1100  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1101  (simplify
1102   (hypots (copysigns @0 @1) @2)
1103   (hypots @0 @2))
1104  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1105  (simplify
1106   (hypots @0 (copysigns @1 @2))
1107   (hypots @0 @1)))
1109 /* copysign(x, CST) -> [-]abs (x).  */
1110 (for copysigns (COPYSIGN_ALL)
1111  (simplify
1112   (copysigns @0 REAL_CST@1)
1113   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1114    (negate (abs @0))
1115    (abs @0))))
1117 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1118 (for copysigns (COPYSIGN_ALL)
1119  (simplify
1120   (copysigns (copysigns @0 @1) @2)
1121   (copysigns @0 @2)))
1123 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1124 (for copysigns (COPYSIGN_ALL)
1125  (simplify
1126   (mult (copysigns@2 @0 @1) @2)
1127   (mult @0 @0)))
1129 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1130 (for ccoss (CCOS CCOSH)
1131  (simplify
1132   (ccoss (negate @0))
1133    (ccoss @0)))
1135 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1136 (for ops (conj negate)
1137  (for cabss (CABS)
1138   (simplify
1139    (cabss (ops @0))
1140    (cabss @0))))
1142 /* Fold (a * (1 << b)) into (a << b)  */
1143 (simplify
1144  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1145   (if (! FLOAT_TYPE_P (type)
1146        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1147    (lshift @0 @2)))
1149 /* Shifts by precision or greater result in zero.  */
1150 (for shift (lshift rshift)
1151  (simplify
1152   (shift @0 uniform_integer_cst_p@1)
1153   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1154        /* Leave arithmetic right shifts of possibly negative values alone.  */
1155        && (TYPE_UNSIGNED (type)
1156            || shift == LSHIFT_EXPR
1157            || tree_expr_nonnegative_p (@0))
1158        /* Use a signed compare to leave negative shift counts alone.  */
1159        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1160                      element_precision (type)))
1161    { build_zero_cst (type); })))
1163 /* Shifts by constants distribute over several binary operations,
1164    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1165 (for op (plus minus)
1166   (simplify
1167     (op (lshift:s @0 @1) (lshift:s @2 @1))
1168     (if (INTEGRAL_TYPE_P (type)
1169          && TYPE_OVERFLOW_WRAPS (type)
1170          && !TYPE_SATURATING (type))
1171       (lshift (op @0 @2) @1))))
1173 (for op (bit_and bit_ior bit_xor)
1174   (simplify
1175     (op (lshift:s @0 @1) (lshift:s @2 @1))
1176     (if (INTEGRAL_TYPE_P (type))
1177       (lshift (op @0 @2) @1)))
1178   (simplify
1179     (op (rshift:s @0 @1) (rshift:s @2 @1))
1180     (if (INTEGRAL_TYPE_P (type))
1181       (rshift (op @0 @2) @1))))
1183 /* Fold (1 << (C - x)) where C = precision(type) - 1
1184    into ((1 << C) >> x). */
1185 (simplify
1186  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1187   (if (INTEGRAL_TYPE_P (type)
1188        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1189        && single_use (@1))
1190    (if (TYPE_UNSIGNED (type))
1191      (rshift (lshift @0 @2) @3)
1192    (with
1193     { tree utype = unsigned_type_for (type); }
1194     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1196 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1197 (simplify
1198  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1199  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1200       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1201   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1202    (bit_and (convert @0)
1203             { wide_int_to_tree (type,
1204                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1206 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1207 (for cst (INTEGER_CST VECTOR_CST)
1208  (simplify
1209   (rshift (negate:s @0) cst@1)
1210    (if (!TYPE_UNSIGNED (type)
1211         && TYPE_OVERFLOW_UNDEFINED (type))
1212     (with { tree stype = TREE_TYPE (@1);
1213             tree bt = truth_type_for (type);
1214             tree zeros = build_zero_cst (type);
1215             tree cst = NULL_TREE; }
1216      (switch
1217       /* Handle scalar case.  */
1218       (if (INTEGRAL_TYPE_P (type)
1219            /* If we apply the rule to the scalar type before vectorization
1220               we will enforce the result of the comparison being a bool
1221               which will require an extra AND on the result that will be
1222               indistinguishable from when the user did actually want 0
1223               or 1 as the result so it can't be removed.  */
1224            && canonicalize_math_after_vectorization_p ()
1225            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1226        (negate (convert (gt @0 { zeros; }))))
1227       /* Handle vector case.  */
1228       (if (VECTOR_INTEGER_TYPE_P (type)
1229            /* First check whether the target has the same mode for vector
1230               comparison results as it's operands do.  */
1231            && TYPE_MODE (bt) == TYPE_MODE (type)
1232            /* Then check to see if the target is able to expand the comparison
1233               with the given type later on, otherwise we may ICE.  */
1234            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1235            && (cst = uniform_integer_cst_p (@1)) != NULL
1236            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1237        (view_convert (gt:bt @0 { zeros; }))))))))
1239 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1240 (simplify
1241  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1242   (if (flag_associative_math
1243        && single_use (@3))
1244    (with
1245     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1246     (if (tem)
1247      (rdiv { tem; } @1)))))
1249 /* Simplify ~X & X as zero.  */
1250 (simplify
1251  (bit_and (convert? @0) (convert? @1))
1252  (with { bool wascmp; }
1253   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1254        && bitwise_inverted_equal_p (@0, @1, wascmp))
1255    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1257 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1258 (simplify
1259   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1260   (if (TYPE_UNSIGNED (type))
1261     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1263 (for bitop (bit_and bit_ior)
1264      cmp (eq ne)
1265  /* PR35691: Transform
1266     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1267     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1268  (simplify
1269   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1270    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1271         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1272         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1273     (cmp (bit_ior @0 (convert @1)) @2)))
1274  /* Transform:
1275     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1276     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1277  (simplify
1278   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1279    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1280         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1281         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1282     (cmp (bit_and @0 (convert @1)) @2))))
1284 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1285 (simplify
1286  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1287   (minus (bit_xor @0 @1) @1))
1288 (simplify
1289  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1290  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1291   (minus (bit_xor @0 @1) @1)))
1293 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1294 (simplify
1295  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1296   (minus @1 (bit_xor @0 @1)))
1298 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1299 (for op (bit_ior bit_xor plus)
1300  (simplify
1301   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1302   (with { bool wascmp0, wascmp1; }
1303    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1304         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1305         && ((!wascmp0 && !wascmp1)
1306             || element_precision (type) == 1))
1307    (bit_xor @0 @1)))))
1309 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1310 (simplify
1311   (bit_ior:c (bit_xor:c @0 @1) @0)
1312   (bit_ior @0 @1))
1314 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1315 (simplify
1316  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1317  @2)
1319 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1320 (simplify
1321  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1322  (bit_not (bit_and @0 @1)))
1324 /* (~a & b) ^ a  -->   (a | b)   */
1325 (simplify
1326  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1327  (bit_ior @0 @1))
1329 /* (a | b) & ~(a ^ b)  -->  a & b  */
1330 (simplify
1331  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1332  (bit_and @0 @1))
1334 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1335 (simplify
1336  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1337  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1338       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1339   (bit_and @0 @1)))
1341 /* a | ~(a ^ b)  -->  a | ~b  */
1342 (simplify
1343  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1344  (bit_ior @0 (bit_not @1)))
1346 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1347 (simplify
1348  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1349  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1350       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1351   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1353 /* (a | b) | (a &^ b)  -->  a | b  */
1354 (for op (bit_and bit_xor)
1355  (simplify
1356   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1357   @2))
1359 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1360 (simplify
1361  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1362  @2)
1364 /* (a & b) | (a == b)  -->  a == b  */
1365 (simplify
1366  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1367  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1368       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1369   @2))
1371 /* ~(~a & b)  -->  a | ~b  */
1372 (simplify
1373  (bit_not (bit_and:cs (bit_not @0) @1))
1374  (bit_ior @0 (bit_not @1)))
1376 /* ~(~a | b) --> a & ~b */
1377 (simplify
1378  (bit_not (bit_ior:cs (bit_not @0) @1))
1379  (bit_and @0 (bit_not @1)))
1381 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1382 (simplify
1383  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1384  (bit_and @3 (bit_not @2)))
1386 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1387 (simplify
1388  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1389  (bit_ior @3 @2))
1391 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1392 (simplify
1393  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1394   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1396 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1397 (simplify
1398  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1399   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1401 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1402 (simplify
1403  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1404  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1405       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1406   (bit_xor @0 @1)))
1408 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1409    ((A & N) + B) & M -> (A + B) & M
1410    Similarly if (N & M) == 0,
1411    ((A | N) + B) & M -> (A + B) & M
1412    and for - instead of + (or unary - instead of +)
1413    and/or ^ instead of |.
1414    If B is constant and (B & M) == 0, fold into A & M.  */
1415 (for op (plus minus)
1416  (for bitop (bit_and bit_ior bit_xor)
1417   (simplify
1418    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1419     (with
1420      { tree pmop[2];
1421        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1422                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1423                                        NULL_TREE, pmop); }
1424      (if (utype)
1425       (convert (bit_and (op (convert:utype { pmop[0]; })
1426                             (convert:utype { pmop[1]; }))
1427                         (convert:utype @2))))))
1428   (simplify
1429    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1430     (with
1431      { tree pmop[2];
1432        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1433                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1434                                        @4, pmop); }
1435      (if (utype)
1436       (convert (bit_and (op (convert:utype { pmop[0]; })
1437                             (convert:utype { pmop[1]; }))
1438                         (convert:utype @2)))))))
1439  (simplify
1440   (bit_and (op:s @0 @1) INTEGER_CST@2)
1441    (with
1442     { tree pmop[2];
1443       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1444                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1445                                       NULL_TREE, NULL_TREE, pmop); }
1446     (if (utype)
1447      (convert (bit_and (op (convert:utype { pmop[0]; })
1448                            (convert:utype { pmop[1]; }))
1449                        (convert:utype @2)))))))
1450 (for bitop (bit_and bit_ior bit_xor)
1451  (simplify
1452   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1453    (with
1454     { tree pmop[2];
1455       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1456                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1457                                       NULL_TREE, NULL_TREE, pmop); }
1458     (if (utype)
1459      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1460                        (convert:utype @1)))))))
1462 /* X % Y is smaller than Y.  */
1463 (for cmp (lt ge)
1464  (simplify
1465   (cmp:c (trunc_mod @0 @1) @1)
1466   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1467    { constant_boolean_node (cmp == LT_EXPR, type); })))
1469 /* x | ~0 -> ~0  */
1470 (simplify
1471  (bit_ior @0 integer_all_onesp@1)
1472  @1)
1474 /* x | 0 -> x  */
1475 (simplify
1476  (bit_ior @0 integer_zerop)
1477  @0)
1479 /* x & 0 -> 0  */
1480 (simplify
1481  (bit_and @0 integer_zerop@1)
1482  @1)
1484 /* ~x | x -> -1 */
1485 /* ~x ^ x -> -1 */
1486 (for op (bit_ior bit_xor)
1487  (simplify
1488   (op (convert? @0) (convert? @1))
1489   (with { bool wascmp; }
1490    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1491         && bitwise_inverted_equal_p (@0, @1, wascmp))
1492     (convert
1493      { wascmp
1494         ? constant_boolean_node (true, type)
1495         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1497 /* x ^ x -> 0 */
1498 (simplify
1499   (bit_xor @0 @0)
1500   { build_zero_cst (type); })
1502 /* Canonicalize X ^ ~0 to ~X.  */
1503 (simplify
1504   (bit_xor @0 integer_all_onesp@1)
1505   (bit_not @0))
1507 /* x & ~0 -> x  */
1508 (simplify
1509  (bit_and @0 integer_all_onesp)
1510   (non_lvalue @0))
1512 /* x & x -> x,  x | x -> x  */
1513 (for bitop (bit_and bit_ior)
1514  (simplify
1515   (bitop @0 @0)
1516   (non_lvalue @0)))
1518 /* x & C -> x if we know that x & ~C == 0.  */
1519 #if GIMPLE
1520 (simplify
1521  (bit_and SSA_NAME@0 INTEGER_CST@1)
1522  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1523       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1524   @0))
1525 /* x | C -> C if we know that x & ~C == 0.  */
1526 (simplify
1527  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1528  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1529       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1530   @1))
1531 #endif
1533 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1534 (simplify
1535  (bit_not (minus (bit_not @0) @1))
1536  (plus @0 @1))
1537 (simplify
1538  (bit_not (plus:c (bit_not @0) @1))
1539  (minus @0 @1))
1540 /* (~X - ~Y) -> Y - X.  */
1541 (simplify
1542  (minus (bit_not @0) (bit_not @1))
1543   (if (!TYPE_OVERFLOW_SANITIZED (type))
1544    (with { tree utype = unsigned_type_for (type); }
1545     (convert (minus (convert:utype @1) (convert:utype @0))))))
1547 /* ~(X - Y) -> ~X + Y.  */
1548 (simplify
1549  (bit_not (minus:s @0 @1))
1550  (plus (bit_not @0) @1))
1551 (simplify
1552  (bit_not (plus:s @0 INTEGER_CST@1))
1553  (if ((INTEGRAL_TYPE_P (type)
1554        && TYPE_UNSIGNED (type))
1555       || (!TYPE_OVERFLOW_SANITIZED (type)
1556           && may_negate_without_overflow_p (@1)))
1557   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1559 #if GIMPLE
1560 /* ~X + Y -> (Y - X) - 1.  */
1561 (simplify
1562  (plus:c (bit_not @0) @1)
1563   (if (ANY_INTEGRAL_TYPE_P (type)
1564        && TYPE_OVERFLOW_WRAPS (type)
1565        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1566        && !integer_all_onesp (@1))
1567    (plus (minus @1 @0) { build_minus_one_cst (type); })
1568    (if (INTEGRAL_TYPE_P (type)
1569         && TREE_CODE (@1) == INTEGER_CST
1570         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1571                                               SIGNED))
1572     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1573 #endif
1575 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1576 (simplify
1577  (bit_not (rshift:s @0 @1))
1578   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1579    (rshift (bit_not! @0) @1)
1580    /* For logical right shifts, this is possible only if @0 doesn't
1581       have MSB set and the logical right shift is changed into
1582       arithmetic shift.  */
1583    (if (INTEGRAL_TYPE_P (type)
1584         && !wi::neg_p (tree_nonzero_bits (@0)))
1585     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1586      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1588 /* x + (x & 1) -> (x + 1) & ~1 */
1589 (simplify
1590  (plus:c @0 (bit_and:s @0 integer_onep@1))
1591  (bit_and (plus @0 @1) (bit_not @1)))
1593 /* x & ~(x & y) -> x & ~y */
1594 /* x | ~(x | y) -> x | ~y  */
1595 (for bitop (bit_and bit_ior)
1596  (simplify
1597   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1598   (bitop @0 (bit_not @1))))
1600 /* (~x & y) | ~(x | y) -> ~x */
1601 (simplify
1602  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1603  @2)
1605 /* (x | y) ^ (x | ~y) -> ~x */
1606 (simplify
1607  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1608  (bit_not @0))
1610 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1611 (simplify
1612  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1613  (bit_not (bit_xor @0 @1)))
1615 /* (~x | y) ^ (x ^ y) -> x | ~y */
1616 (simplify
1617  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1618  (bit_ior @0 (bit_not @1)))
1620 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1621 (simplify
1622  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1623  (bit_not (bit_and @0 @1)))
1625 /* (x & y) ^ (x | y) -> x ^ y */
1626 (simplify
1627  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1628  (bit_xor @0 @1))
1630 /* (x ^ y) ^ (x | y) -> x & y */
1631 (simplify
1632  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1633  (bit_and @0 @1))
1635 /* (x & y) + (x ^ y) -> x | y */
1636 /* (x & y) | (x ^ y) -> x | y */
1637 /* (x & y) ^ (x ^ y) -> x | y */
1638 (for op (plus bit_ior bit_xor)
1639  (simplify
1640   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1641   (bit_ior @0 @1)))
1643 /* (x & y) + (x | y) -> x + y */
1644 (simplify
1645  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1646  (plus @0 @1))
1648 /* (x + y) - (x | y) -> x & y */
1649 (simplify
1650  (minus (plus @0 @1) (bit_ior @0 @1))
1651  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1652       && !TYPE_SATURATING (type))
1653   (bit_and @0 @1)))
1655 /* (x + y) - (x & y) -> x | y */
1656 (simplify
1657  (minus (plus @0 @1) (bit_and @0 @1))
1658  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1659       && !TYPE_SATURATING (type))
1660   (bit_ior @0 @1)))
1662 /* (x | y) - y -> (x & ~y) */
1663 (simplify
1664  (minus (bit_ior:cs @0 @1) @1)
1665  (bit_and @0 (bit_not @1)))
1667 /* (x | y) - (x ^ y) -> x & y */
1668 (simplify
1669  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1670  (bit_and @0 @1))
1672 /* (x | y) - (x & y) -> x ^ y */
1673 (simplify
1674  (minus (bit_ior @0 @1) (bit_and @0 @1))
1675  (bit_xor @0 @1))
1677 /* (x | y) & ~(x & y) -> x ^ y */
1678 (simplify
1679  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1680  (bit_xor @0 @1))
1682 /* (x | y) & (~x ^ y) -> x & y */
1683 (simplify
1684  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1685  (with { bool wascmp; }
1686   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1687        && (!wascmp || element_precision (type) == 1))
1688    (bit_and @0 @1))))
1690 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1691 (simplify
1692  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1693  (bit_not (bit_xor @0 @1)))
1695 /* (~x | y) ^ (x | ~y) -> x ^ y */
1696 (simplify
1697  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1698  (bit_xor @0 @1))
1700 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1701 (simplify
1702  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1703                               (nop_convert2? (bit_ior @0 @1))))
1704        integer_all_onesp)
1705  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1706       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1707       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1708       && !TYPE_SATURATING (TREE_TYPE (@2)))
1709  (bit_not (convert (bit_xor @0 @1)))))
1710 (simplify
1711  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1712                                integer_all_onesp))
1713        (nop_convert3? (bit_ior @0 @1)))
1714  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1715       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1716       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1717       && !TYPE_SATURATING (TREE_TYPE (@2)))
1718  (bit_not (convert (bit_xor @0 @1)))))
1719 (simplify
1720  (minus (nop_convert1? (bit_and @0 @1))
1721        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1722                                integer_onep)))
1723  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1724       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1725       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1726       && !TYPE_SATURATING (TREE_TYPE (@2)))
1727  (bit_not (convert (bit_xor @0 @1)))))
1729 /* ~x & ~y -> ~(x | y)
1730    ~x | ~y -> ~(x & y) */
1731 (for op (bit_and bit_ior)
1732      rop (bit_ior bit_and)
1733  (simplify
1734   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1735   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1736        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1737    (bit_not (rop (convert @0) (convert @1))))))
1739 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1740    with a constant, and the two constants have no bits in common,
1741    we should treat this as a BIT_IOR_EXPR since this may produce more
1742    simplifications.  */
1743 (for op (bit_xor plus)
1744  (simplify
1745   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1746       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1747   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1748        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1749        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1750    (bit_ior (convert @4) (convert @5)))))
1752 /* (X | Y) ^ X -> Y & ~ X*/
1753 (simplify
1754  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1755  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1756   (convert (bit_and @1 (bit_not @0)))))
1758 /* (~X | Y) ^ X -> ~(X & Y).  */
1759 (simplify
1760  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1761  (if (bitwise_equal_p (@0, @2))
1762   (convert (bit_not (bit_and @0 (convert @1))))))
1764 /* Convert ~X ^ ~Y to X ^ Y.  */
1765 (simplify
1766  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1767  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1768       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1769   (bit_xor (convert @0) (convert @1))))
1771 /* Convert ~X ^ C to X ^ ~C.  */
1772 (simplify
1773  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1774  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1775   (bit_xor (convert @0) (bit_not @1))))
1777 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1778 (for opo (bit_and bit_xor)
1779      opi (bit_xor bit_and)
1780  (simplify
1781   (opo:c (opi:cs @0 @1) @1)
1782   (bit_and (bit_not @0) @1)))
1784 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1785    operands are another bit-wise operation with a common input.  If so,
1786    distribute the bit operations to save an operation and possibly two if
1787    constants are involved.  For example, convert
1788      (A | B) & (A | C) into A | (B & C)
1789    Further simplification will occur if B and C are constants.  */
1790 (for op (bit_and bit_ior bit_xor)
1791      rop (bit_ior bit_and bit_and)
1792  (simplify
1793   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1794   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1795        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1796    (rop (convert @0) (op (convert @1) (convert @2))))))
1798 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1799 /* (X & Y) & Y -> X & Y
1800    (X | Y) | Y -> X | Y  */
1801 (for op (bit_and bit_ior)
1802  (simplify
1803   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1804   @2))
1805 /* (X ^ Y) ^ Y -> X  */
1806 (simplify
1807  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1808  (convert @0))
1810 /* (X & ~Y) & Y -> 0 */
1811 (simplify
1812  (bit_and:c (bit_and @0 @1) @2)
1813  (with { bool wascmp; }
1814   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1815        || bitwise_inverted_equal_p (@1, @2, wascmp))
1816    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1817 /* (X | ~Y) | Y -> -1 */
1818 (simplify
1819  (bit_ior:c (bit_ior @0 @1) @2)
1820  (with { bool wascmp; }
1821   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1822         || bitwise_inverted_equal_p (@1, @2, wascmp))
1823        && (!wascmp || element_precision (type) == 1))
1824    { build_all_ones_cst (TREE_TYPE (@0)); })))
1826 /* (X & Y) & (X & Z) -> (X & Y) & Z
1827    (X | Y) | (X | Z) -> (X | Y) | Z  */
1828 (for op (bit_and bit_ior)
1829  (simplify
1830   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1831   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1832        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1833    (if (single_use (@5) && single_use (@6))
1834     (op @3 (convert @2))
1835     (if (single_use (@3) && single_use (@4))
1836      (op (convert @1) @5))))))
1837 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1838 (simplify
1839  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1840  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1841       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1842   (bit_xor (convert @1) (convert @2))))
1844 /* Convert abs (abs (X)) into abs (X).
1845    also absu (absu (X)) into absu (X).  */
1846 (simplify
1847  (abs (abs@1 @0))
1848  @1)
1850 (simplify
1851  (absu (convert@2 (absu@1 @0)))
1852  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1853   @1))
1855 /* Convert abs[u] (-X) -> abs[u] (X).  */
1856 (simplify
1857  (abs (negate @0))
1858  (abs @0))
1860 (simplify
1861  (absu (negate @0))
1862  (absu @0))
1864 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1865 (simplify
1866  (abs tree_expr_nonnegative_p@0)
1867  @0)
1869 (simplify
1870  (absu tree_expr_nonnegative_p@0)
1871  (convert @0))
1873 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1874 (simplify
1875  (mult:c (nop_convert1?
1876           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1877                     integer_onep))
1878          (nop_convert3? @0))
1879  (if (INTEGRAL_TYPE_P (type)
1880       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1881       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1882   (if (TYPE_UNSIGNED (type))
1883    (absu @0)
1884    (abs @0)
1885   )
1889 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1890 (match negate_expr_p
1891  INTEGER_CST
1892  (if ((INTEGRAL_TYPE_P (type)
1893        && TYPE_UNSIGNED (type))
1894       || (!TYPE_OVERFLOW_SANITIZED (type)
1895           && may_negate_without_overflow_p (t)))))
1896 (match negate_expr_p
1897  FIXED_CST)
1898 (match negate_expr_p
1899  (negate @0)
1900  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1901 (match negate_expr_p
1902  REAL_CST
1903  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1904 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1905    ways.  */
1906 (match negate_expr_p
1907  VECTOR_CST
1908  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1909 (match negate_expr_p
1910  (minus @0 @1)
1911  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1912       || (FLOAT_TYPE_P (type)
1913           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1914           && !HONOR_SIGNED_ZEROS (type)))))
1916 /* (-A) * (-B) -> A * B  */
1917 (simplify
1918  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1919   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1920        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1921    (mult (convert @0) (convert (negate @1)))))
1923 /* -(A + B) -> (-B) - A.  */
1924 (simplify
1925  (negate (plus:c @0 negate_expr_p@1))
1926  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1927       && !HONOR_SIGNED_ZEROS (type))
1928   (minus (negate @1) @0)))
1930 /* -(A - B) -> B - A.  */
1931 (simplify
1932  (negate (minus @0 @1))
1933  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1934       || (FLOAT_TYPE_P (type)
1935           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1936           && !HONOR_SIGNED_ZEROS (type)))
1937   (minus @1 @0)))
1938 (simplify
1939  (negate (pointer_diff @0 @1))
1940  (if (TYPE_OVERFLOW_UNDEFINED (type))
1941   (pointer_diff @1 @0)))
1943 /* A - B -> A + (-B) if B is easily negatable.  */
1944 (simplify
1945  (minus @0 negate_expr_p@1)
1946  (if (!FIXED_POINT_TYPE_P (type))
1947  (plus @0 (negate @1))))
1949 /* 1 - a is a ^ 1 if a had a bool range. */
1950 /* This is only enabled for gimple as sometimes
1951    cfun is not set for the function which contains
1952    the SSA_NAME (e.g. while IPA passes are happening,
1953    fold might be called).  */
1954 (simplify
1955  (minus integer_onep@0 SSA_NAME@1)
1956   (if (INTEGRAL_TYPE_P (type)
1957        && ssa_name_has_boolean_range (@1))
1958    (bit_xor @1 @0)))
1960 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1961 (simplify
1962  (negate (mult:c@0 @1 negate_expr_p@2))
1963  (if (! TYPE_UNSIGNED (type)
1964       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1965       && single_use (@0))
1966   (mult @1 (negate @2))))
1968 (simplify
1969  (negate (rdiv@0 @1 negate_expr_p@2))
1970  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1971       && single_use (@0))
1972   (rdiv @1 (negate @2))))
1974 (simplify
1975  (negate (rdiv@0 negate_expr_p@1 @2))
1976  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1977       && single_use (@0))
1978   (rdiv (negate @1) @2)))
1980 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1981 (simplify
1982  (negate (convert? (rshift @0 INTEGER_CST@1)))
1983  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1984       && wi::to_wide (@1) == element_precision (type) - 1)
1985   (with { tree stype = TREE_TYPE (@0);
1986           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1987                                              : unsigned_type_for (stype); }
1988    (if (VECTOR_TYPE_P (type))
1989     (view_convert (rshift (view_convert:ntype @0) @1))
1990     (convert (rshift (convert:ntype @0) @1))))))
1992 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1993    when profitable.
1994    For bitwise binary operations apply operand conversions to the
1995    binary operation result instead of to the operands.  This allows
1996    to combine successive conversions and bitwise binary operations.
1997    We combine the above two cases by using a conditional convert.  */
1998 (for bitop (bit_and bit_ior bit_xor)
1999  (simplify
2000   (bitop (convert@2 @0) (convert?@3 @1))
2001   (if (((TREE_CODE (@1) == INTEGER_CST
2002          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2003          && (int_fits_type_p (@1, TREE_TYPE (@0))
2004              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2005         || types_match (@0, @1))
2006        && !POINTER_TYPE_P (TREE_TYPE (@0))
2007        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2008        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2009        /* ???  This transform conflicts with fold-const.cc doing
2010           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2011           constants (if x has signed type, the sign bit cannot be set
2012           in c).  This folds extension into the BIT_AND_EXPR.
2013           Restrict it to GIMPLE to avoid endless recursions.  */
2014        && (bitop != BIT_AND_EXPR || GIMPLE)
2015        && (/* That's a good idea if the conversion widens the operand, thus
2016               after hoisting the conversion the operation will be narrower.
2017               It is also a good if the conversion is a nop as moves the
2018               conversion to one side; allowing for combining of the conversions.  */
2019            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2020            /* The conversion check for being a nop can only be done at the gimple
2021               level as fold_binary has some re-association code which can conflict
2022               with this if there is a "constant" which is not a full INTEGER_CST.  */
2023            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2024            /* It's also a good idea if the conversion is to a non-integer
2025               mode.  */
2026            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2027            /* Or if the precision of TO is not the same as the precision
2028               of its mode.  */
2029            || !type_has_mode_precision_p (type)
2030            /* In GIMPLE, getting rid of 2 conversions for one new results
2031               in smaller IL.  */
2032            || (GIMPLE
2033                && TREE_CODE (@1) != INTEGER_CST
2034                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2035                && single_use (@2)
2036                && single_use (@3))))
2037    (convert (bitop @0 (convert @1)))))
2038  /* In GIMPLE, getting rid of 2 conversions for one new results
2039     in smaller IL.  */
2040  (simplify
2041   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2042   (if (GIMPLE
2043        && TREE_CODE (@1) != INTEGER_CST
2044        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2045        && types_match (type, @0)
2046        && !POINTER_TYPE_P (TREE_TYPE (@0))
2047        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2048    (bitop @0 (convert @1)))))
2050 (for bitop (bit_and bit_ior)
2051      rbitop (bit_ior bit_and)
2052   /* (x | y) & x -> x */
2053   /* (x & y) | x -> x */
2054  (simplify
2055   (bitop:c (rbitop:c @0 @1) @0)
2056   @0)
2057  /* (~x | y) & x -> x & y */
2058  /* (~x & y) | x -> x | y */
2059  (simplify
2060   (bitop:c (rbitop:c @2 @1) @0)
2061   (with { bool wascmp; }
2062    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2063         && (!wascmp || element_precision (type) == 1))
2064     (bitop @0 @1))))
2065   /* (x | y) & (x & z) -> (x & z) */
2066   /* (x & y) | (x | z) -> (x | z) */
2067  (simplify
2068   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2069   @3)
2070  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2071  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2072  (simplify
2073   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2074   (bitop @0 @3))
2075  /* x & ~(y | x) -> 0 */
2076  /* x | ~(y & x) -> -1 */
2077  (simplify
2078   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2079   (if (bitop == BIT_AND_EXPR)
2080    { build_zero_cst (type); }
2081    { build_minus_one_cst (type); })))
2083 /* ((x | y) & z) | x -> (z & y) | x
2084    ((x ^ y) & z) | x -> (z & y) | x  */
2085 (for op (bit_ior bit_xor)
2086  (simplify
2087   (bit_ior:c (nop_convert1?:s
2088                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2089   (if (bitwise_equal_p (@0, @3))
2090    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2092 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2093 (simplify
2094   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2095   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2097 /* Combine successive equal operations with constants.  */
2098 (for bitop (bit_and bit_ior bit_xor)
2099  (simplify
2100   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2101   (if (!CONSTANT_CLASS_P (@0))
2102    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2103       folded to a constant.  */
2104    (bitop @0 (bitop! @1 @2))
2105    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2106       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2107       the values involved are such that the operation can't be decided at
2108       compile time.  Try folding one of @0 or @1 with @2 to see whether
2109       that combination can be decided at compile time.
2111       Keep the existing form if both folds fail, to avoid endless
2112       oscillation.  */
2113    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2114     (if (cst1)
2115      (bitop @1 { cst1; })
2116      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2117       (if (cst2)
2118        (bitop @0 { cst2; }))))))))
2120 /* Try simple folding for X op !X, and X op X with the help
2121    of the truth_valued_p and logical_inverted_value predicates.  */
2122 (match truth_valued_p
2123  @0
2124  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2125 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2126  (match truth_valued_p
2127   (op @0 @1)))
2128 (match truth_valued_p
2129   (truth_not @0))
2131 (match (logical_inverted_value @0)
2132  (truth_not @0))
2133 (match (logical_inverted_value @0)
2134  (bit_not truth_valued_p@0))
2135 (match (logical_inverted_value @0)
2136  (eq @0 integer_zerop))
2137 (match (logical_inverted_value @0)
2138  (ne truth_valued_p@0 integer_truep))
2139 (match (logical_inverted_value @0)
2140  (bit_xor truth_valued_p@0 integer_truep))
2142 /* X & !X -> 0.  */
2143 (simplify
2144  (bit_and:c @0 (logical_inverted_value @0))
2145  { build_zero_cst (type); })
2146 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2147 (for op (bit_ior bit_xor)
2148  (simplify
2149   (op:c truth_valued_p@0 (logical_inverted_value @0))
2150   { constant_boolean_node (true, type); }))
2151 /* X ==/!= !X is false/true.  */
2152 (for op (eq ne)
2153  (simplify
2154   (op:c truth_valued_p@0 (logical_inverted_value @0))
2155   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2157 /* ~~x -> x */
2158 (simplify
2159   (bit_not (bit_not @0))
2160   @0)
2162 /* zero_one_valued_p will match when a value is known to be either
2163    0 or 1 including constants 0 or 1.
2164    Signed 1-bits includes -1 so they cannot match here. */
2165 (match zero_one_valued_p
2166  @0
2167  (if (INTEGRAL_TYPE_P (type)
2168       && (TYPE_UNSIGNED (type)
2169           || TYPE_PRECISION (type) > 1)
2170       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2171 (match zero_one_valued_p
2172  truth_valued_p@0
2173  (if (INTEGRAL_TYPE_P (type)
2174       && (TYPE_UNSIGNED (type)
2175           || TYPE_PRECISION (type) > 1))))
2177 /* (a&1) is always [0,1] too. This is useful again when
2178    the range is not known. */
2179 /* Note this can't be recursive due to VN handling of equivalents,
2180    VN and would cause an infinite recursion. */
2181 (match zero_one_valued_p
2182  (bit_and:c@0 @1 integer_onep)
2183  (if (INTEGRAL_TYPE_P (type))))
2185 /* A conversion from an zero_one_valued_p is still a [0,1].
2186    This is useful when the range of a variable is not known */
2187 /* Note this matches can't be recursive because of the way VN handles
2188    nop conversions being equivalent and then recursive between them. */
2189 (match zero_one_valued_p
2190  (convert@0 @1)
2191  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2192       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2193           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2194       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2196 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2197 (simplify
2198  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2199  (if (INTEGRAL_TYPE_P (type))
2200   (bit_and @0 @1)))
2202 (for cmp (tcc_comparison)
2203      icmp (inverted_tcc_comparison)
2204  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2205  (simplify
2206   (bit_ior
2207    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2208    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2209     (if (INTEGRAL_TYPE_P (type)
2210          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2211          /* The scalar version has to be canonicalized after vectorization
2212             because it makes unconditional loads conditional ones, which
2213             means we lose vectorization because the loads may trap.  */
2214          && canonicalize_math_after_vectorization_p ())
2215      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2217  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2218     canonicalized further and we recognize the conditional form:
2219     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2220  (simplify
2221   (bit_ior
2222    (cond (cmp@0  @01 @02) @3 zerop)
2223    (cond (icmp@4 @01 @02) @5 zerop))
2224     (if (INTEGRAL_TYPE_P (type)
2225          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2226          /* The scalar version has to be canonicalized after vectorization
2227             because it makes unconditional loads conditional ones, which
2228             means we lose vectorization because the loads may trap.  */
2229          && canonicalize_math_after_vectorization_p ())
2230     (cond @0 @3 @5)))
2232  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2233     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2234  (simplify
2235   (bit_ior
2236    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2237    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2238     (if (integer_zerop (@5)
2239          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2240      (switch
2241       (if (integer_onep (@4))
2242        (bit_and (vec_cond @0 @2 @3) @4))
2243         (if (integer_minus_onep (@4))
2244          (vec_cond @0 @2 @3)))
2245     (if (integer_zerop (@4)
2246          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2247      (switch
2248       (if (integer_onep (@5))
2249        (bit_and (vec_cond @0 @3 @2) @5))
2250       (if (integer_minus_onep (@5))
2251        (vec_cond @0 @3 @2))))))
2253  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2254     into a < b ? d : c.  */
2255  (simplify
2256   (bit_ior
2257    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2258    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2259   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2260    (vec_cond @0 @2 @3))))
2262 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2263 (simplify
2264  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2265  (if (INTEGRAL_TYPE_P (type)
2266       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2267       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2268       /* Sign extending of the neg or a truncation of the neg
2269          is needed. */
2270       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2271           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2272   (mult (convert @0) @1)))
2274 /* Narrow integer multiplication by a zero_one_valued_p operand.
2275    Multiplication by [0,1] is guaranteed not to overflow.  */
2276 (simplify
2277  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2278  (if (INTEGRAL_TYPE_P (type)
2279       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2280       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2281   (mult (convert @1) (convert @2))))
2283 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2284    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2285    as some targets (such as x86's SSE) may return zero for larger C.  */
2286 (simplify
2287   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2288   (if (tree_fits_shwi_p (@1)
2289        && tree_to_shwi (@1) > 0
2290        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2291     (convert @0)))
2293 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2294    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2295    as some targets (such as x86's SSE) may return zero for larger C.  */
2296 (simplify
2297   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2298   (if (tree_fits_shwi_p (@1)
2299        && tree_to_shwi (@1) > 0
2300        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2301     (eq @0 @2)))
2303 /* Convert ~ (-A) to A - 1.  */
2304 (simplify
2305  (bit_not (convert? (negate @0)))
2306  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2307       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2308   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2310 /* Convert - (~A) to A + 1.  */
2311 (simplify
2312  (negate (nop_convert? (bit_not @0)))
2313  (plus (view_convert @0) { build_each_one_cst (type); }))
2315 /* (a & b) ^ (a == b) -> !(a | b) */
2316 /* (a & b) == (a ^ b) -> !(a | b) */
2317 (for first_op (bit_xor eq)
2318      second_op (eq bit_xor)
2319  (simplify
2320   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2321     (bit_not (bit_ior @0 @1))))
2323 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2324 (simplify
2325  (bit_not (convert? (minus @0 integer_each_onep)))
2326  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2327       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2328   (convert (negate @0))))
2329 (simplify
2330  (bit_not (convert? (plus @0 integer_all_onesp)))
2331  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2332       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2333   (convert (negate @0))))
2335 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2336 (simplify
2337  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2338  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2339   (convert (bit_xor @0 (bit_not @1)))))
2340 (simplify
2341  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2342  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2343   (convert (bit_xor @0 @1))))
2345 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2346 (simplify
2347  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2348  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2349   (bit_not (bit_xor (view_convert @0) @1))))
2351 /* ~(a ^ b) is a == b for truth valued a and b.  */
2352 (simplify
2353  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2354  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2355       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2356   (convert (eq @0 @1))))
2358 /* (~a) == b is a ^ b for truth valued a and b.  */
2359 (simplify
2360  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2361  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2362       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2363   (convert (bit_xor @0 @1))))
2365 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2366 (simplify
2367  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2368  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2370 /* Fold A - (A & B) into ~B & A.  */
2371 (simplify
2372  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2373  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2374       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2375   (convert (bit_and (bit_not @1) @0))))
2377 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2378 (if (!canonicalize_math_p ())
2379  (for cmp (tcc_comparison)
2380   (simplify
2381    (mult:c (convert (cmp@0 @1 @2)) @3)
2382    (if (INTEGRAL_TYPE_P (type)
2383         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2384      (cond @0 @3 { build_zero_cst (type); })))
2385 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2386   (simplify
2387    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2388    (if (INTEGRAL_TYPE_P (type)
2389         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2390      (cond @0 @3 { build_zero_cst (type); })))
2394 /* For integral types with undefined overflow and C != 0 fold
2395    x * C EQ/NE y * C into x EQ/NE y.  */
2396 (for cmp (eq ne)
2397  (simplify
2398   (cmp (mult:c @0 @1) (mult:c @2 @1))
2399   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2400        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2401        && tree_expr_nonzero_p (@1))
2402    (cmp @0 @2))))
2404 /* For integral types with wrapping overflow and C odd fold
2405    x * C EQ/NE y * C into x EQ/NE y.  */
2406 (for cmp (eq ne)
2407  (simplify
2408   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2409   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2410        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2411        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2412    (cmp @0 @2))))
2414 /* For integral types with undefined overflow and C != 0 fold
2415    x * C RELOP y * C into:
2417    x RELOP y for nonnegative C
2418    y RELOP x for negative C  */
2419 (for cmp (lt gt le ge)
2420  (simplify
2421   (cmp (mult:c @0 @1) (mult:c @2 @1))
2422   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2423        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2424    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2425     (cmp @0 @2)
2426    (if (TREE_CODE (@1) == INTEGER_CST
2427         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2428     (cmp @2 @0))))))
2430 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2431 (for cmp (le gt)
2432      icmp (gt le)
2433  (simplify
2434   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2435    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2436         && TYPE_UNSIGNED (TREE_TYPE (@0))
2437         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2438         && (wi::to_wide (@2)
2439             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2440     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2441      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2443 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2444 (for cmp (simple_comparison)
2445  (simplify
2446   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2447   (if (element_precision (@3) >= element_precision (@0)
2448        && types_match (@0, @1))
2449    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2450     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2451      (cmp @1 @0)
2452      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2453       (with
2454        {
2455         tree utype = unsigned_type_for (TREE_TYPE (@0));
2456        }
2457        (cmp (convert:utype @1) (convert:utype @0)))))
2458     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2459      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2460       (cmp @0 @1)
2461       (with
2462        {
2463         tree utype = unsigned_type_for (TREE_TYPE (@0));
2464        }
2465        (cmp (convert:utype @0) (convert:utype @1)))))))))
2467 /* X / C1 op C2 into a simple range test.  */
2468 (for cmp (simple_comparison)
2469  (simplify
2470   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2471   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2472        && integer_nonzerop (@1)
2473        && !TREE_OVERFLOW (@1)
2474        && !TREE_OVERFLOW (@2))
2475    (with { tree lo, hi; bool neg_overflow;
2476            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2477                                                    &neg_overflow); }
2478     (switch
2479      (if (code == LT_EXPR || code == GE_EXPR)
2480        (if (TREE_OVERFLOW (lo))
2481         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2482         (if (code == LT_EXPR)
2483          (lt @0 { lo; })
2484          (ge @0 { lo; }))))
2485      (if (code == LE_EXPR || code == GT_EXPR)
2486        (if (TREE_OVERFLOW (hi))
2487         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2488         (if (code == LE_EXPR)
2489          (le @0 { hi; })
2490          (gt @0 { hi; }))))
2491      (if (!lo && !hi)
2492       { build_int_cst (type, code == NE_EXPR); })
2493      (if (code == EQ_EXPR && !hi)
2494       (ge @0 { lo; }))
2495      (if (code == EQ_EXPR && !lo)
2496       (le @0 { hi; }))
2497      (if (code == NE_EXPR && !hi)
2498       (lt @0 { lo; }))
2499      (if (code == NE_EXPR && !lo)
2500       (gt @0 { hi; }))
2501      (if (GENERIC)
2502       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2503                            lo, hi); })
2504      (with
2505       {
2506         tree etype = range_check_type (TREE_TYPE (@0));
2507         if (etype)
2508           {
2509             hi = fold_convert (etype, hi);
2510             lo = fold_convert (etype, lo);
2511             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2512           }
2513       }
2514       (if (etype && hi && !TREE_OVERFLOW (hi))
2515        (if (code == EQ_EXPR)
2516         (le (minus (convert:etype @0) { lo; }) { hi; })
2517         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2519 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2520 (for op (lt le ge gt)
2521  (simplify
2522   (op (plus:c @0 @2) (plus:c @1 @2))
2523   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2525    (op @0 @1))))
2527 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2528    when C is an unsigned integer constant with only the MSB set, and X and
2529    Y have types of equal or lower integer conversion rank than C's.  */
2530 (for op (lt le ge gt)
2531  (simplify
2532   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2533   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2534        && TYPE_UNSIGNED (TREE_TYPE (@0))
2535        && wi::only_sign_bit_p (wi::to_wide (@0)))
2536    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2537     (op (convert:stype @1) (convert:stype @2))))))
2539 /* For equality and subtraction, this is also true with wrapping overflow.  */
2540 (for op (eq ne minus)
2541  (simplify
2542   (op (plus:c @0 @2) (plus:c @1 @2))
2543   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2544        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2545            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2546    (op @0 @1))))
2548 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2549 (for op (lt le ge gt)
2550  (simplify
2551   (op (minus @0 @2) (minus @1 @2))
2552   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2553        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2554    (op @0 @1))))
2555 /* For equality and subtraction, this is also true with wrapping overflow.  */
2556 (for op (eq ne minus)
2557  (simplify
2558   (op (minus @0 @2) (minus @1 @2))
2559   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2560        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2561            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2562    (op @0 @1))))
2563 /* And for pointers...  */
2564 (for op (simple_comparison)
2565  (simplify
2566   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2567   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2568    (op @0 @1))))
2569 (simplify
2570  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2571  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2572       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2573   (pointer_diff @0 @1)))
2575 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2576 (for op (lt le ge gt)
2577  (simplify
2578   (op (minus @2 @0) (minus @2 @1))
2579   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2580        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2581    (op @1 @0))))
2582 /* For equality and subtraction, this is also true with wrapping overflow.  */
2583 (for op (eq ne minus)
2584  (simplify
2585   (op (minus @2 @0) (minus @2 @1))
2586   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2587        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2588            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2589    (op @1 @0))))
2590 /* And for pointers...  */
2591 (for op (simple_comparison)
2592  (simplify
2593   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2594   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2595    (op @1 @0))))
2596 (simplify
2597  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2598  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2599       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2600   (pointer_diff @1 @0)))
2602 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2603 (for op (lt le gt ge)
2604  (simplify
2605   (op:c (plus:c@2 @0 @1) @1)
2606   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2607        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2608        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2609        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2610    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2611 /* For equality, this is also true with wrapping overflow.  */
2612 (for op (eq ne)
2613  (simplify
2614   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2615   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2616        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2617            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2618        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2619        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2620        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2621    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2622  (simplify
2623   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2624   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2625        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2626        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2627    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2629 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2630 (for neeq (ne eq)
2631  (simplify
2632   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2633    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2634     (if (ptr_difference_const (@0, @2, &diff))
2635      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2636  (simplify
2637   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2638    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2639     (if (ptr_difference_const (@0, @2, &diff))
2640      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2642 /* X - Y < X is the same as Y > 0 when there is no overflow.
2643    For equality, this is also true with wrapping overflow.  */
2644 (for op (simple_comparison)
2645  (simplify
2646   (op:c @0 (minus@2 @0 @1))
2647   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2648        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2649            || ((op == EQ_EXPR || op == NE_EXPR)
2650                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2651        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2652    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2654 /* Transform:
2655    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2656    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2657 (for cmp (eq ne)
2658      ocmp (lt ge)
2659  (simplify
2660   (cmp (trunc_div @0 @1) integer_zerop)
2661   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2662        /* Complex ==/!= is allowed, but not </>=.  */
2663        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2664        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2665    (ocmp @0 @1))))
2667 /* X == C - X can never be true if C is odd.  */
2668 (for cmp (eq ne)
2669  (simplify
2670   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2671   (if (TREE_INT_CST_LOW (@1) & 1)
2672    { constant_boolean_node (cmp == NE_EXPR, type); })))
2674 /* Arguments on which one can call get_nonzero_bits to get the bits
2675    possibly set.  */
2676 (match with_possible_nonzero_bits
2677  INTEGER_CST@0)
2678 (match with_possible_nonzero_bits
2679  SSA_NAME@0
2680  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2681 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2682 (match (with_possible_nonzero_bits2 @0)
2683  with_possible_nonzero_bits@0)
2684 (match (with_possible_nonzero_bits2 @0)
2685  (bit_and:c with_possible_nonzero_bits@0 @2))
2687 /* Same for bits that are known to be set, but we do not have
2688    an equivalent to get_nonzero_bits yet.  */
2689 (match (with_certain_nonzero_bits2 @0)
2690  INTEGER_CST@0)
2691 (match (with_certain_nonzero_bits2 @0)
2692  (bit_ior @1 INTEGER_CST@0))
2694 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2695 (for cmp (eq ne)
2696  (simplify
2697   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2698   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2699    { constant_boolean_node (cmp == NE_EXPR, type); })))
2701 /* ((X inner_op C0) outer_op C1)
2702    With X being a tree where value_range has reasoned certain bits to always be
2703    zero throughout its computed value range,
2704    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2705    where zero_mask has 1's for all bits that are sure to be 0 in
2706    and 0's otherwise.
2707    if (inner_op == '^') C0 &= ~C1;
2708    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2709    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2711 (for inner_op (bit_ior bit_xor)
2712      outer_op (bit_xor bit_ior)
2713 (simplify
2714  (outer_op
2715   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2716  (with
2717   {
2718     bool fail = false;
2719     wide_int zero_mask_not;
2720     wide_int C0;
2721     wide_int cst_emit;
2723     if (TREE_CODE (@2) == SSA_NAME)
2724       zero_mask_not = get_nonzero_bits (@2);
2725     else
2726       fail = true;
2728     if (inner_op == BIT_XOR_EXPR)
2729       {
2730         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2731         cst_emit = C0 | wi::to_wide (@1);
2732       }
2733     else
2734       {
2735         C0 = wi::to_wide (@0);
2736         cst_emit = C0 ^ wi::to_wide (@1);
2737       }
2738   }
2739   (if (!fail && (C0 & zero_mask_not) == 0)
2740    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2741    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2742     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2744 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2745 (simplify
2746   (pointer_plus (pointer_plus:s @0 @1) @3)
2747   (pointer_plus @0 (plus @1 @3)))
2748 #if GENERIC
2749 (simplify
2750   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2751   (convert:type (pointer_plus @0 (plus @1 @3))))
2752 #endif
2754 /* Pattern match
2755      tem1 = (long) ptr1;
2756      tem2 = (long) ptr2;
2757      tem3 = tem2 - tem1;
2758      tem4 = (unsigned long) tem3;
2759      tem5 = ptr1 + tem4;
2760    and produce
2761      tem5 = ptr2;  */
2762 (simplify
2763   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2764   /* Conditionally look through a sign-changing conversion.  */
2765   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2766        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2767             || (GENERIC && type == TREE_TYPE (@1))))
2768    @1))
2769 (simplify
2770   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2771   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2772    (convert @1)))
2774 /* Pattern match
2775      tem = (sizetype) ptr;
2776      tem = tem & algn;
2777      tem = -tem;
2778      ... = ptr p+ tem;
2779    and produce the simpler and easier to analyze with respect to alignment
2780      ... = ptr & ~algn;  */
2781 (simplify
2782   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2783   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2784    (bit_and @0 { algn; })))
2786 /* Try folding difference of addresses.  */
2787 (simplify
2788  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2789  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2790   (with { poly_int64 diff; }
2791    (if (ptr_difference_const (@0, @1, &diff))
2792     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2793 (simplify
2794  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2795  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2796   (with { poly_int64 diff; }
2797    (if (ptr_difference_const (@0, @1, &diff))
2798     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2799 (simplify
2800  (minus (convert ADDR_EXPR@0) (convert @1))
2801  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2802   (with { poly_int64 diff; }
2803    (if (ptr_difference_const (@0, @1, &diff))
2804     { build_int_cst_type (type, diff); }))))
2805 (simplify
2806  (minus (convert @0) (convert ADDR_EXPR@1))
2807  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2808   (with { poly_int64 diff; }
2809    (if (ptr_difference_const (@0, @1, &diff))
2810     { build_int_cst_type (type, diff); }))))
2811 (simplify
2812  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2813  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2814       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2815   (with { poly_int64 diff; }
2816    (if (ptr_difference_const (@0, @1, &diff))
2817     { build_int_cst_type (type, diff); }))))
2818 (simplify
2819  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2820  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2821       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2822   (with { poly_int64 diff; }
2823    (if (ptr_difference_const (@0, @1, &diff))
2824     { build_int_cst_type (type, diff); }))))
2826 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2827 (simplify
2828  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2829  (with { poly_int64 diff; }
2830    (if (ptr_difference_const (@0, @2, &diff))
2831     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2832 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2833 (simplify
2834  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2835  (with { poly_int64 diff; }
2836    (if (ptr_difference_const (@0, @2, &diff))
2837     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2838 (simplify
2839  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2840  (with { poly_int64 diff; }
2841    (if (ptr_difference_const (@0, @1, &diff))
2842     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2844 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2845 (simplify
2846  (convert (pointer_diff @0 INTEGER_CST@1))
2847  (if (POINTER_TYPE_P (type))
2848   { build_fold_addr_expr_with_type
2849       (build2 (MEM_REF, char_type_node, @0,
2850                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2851                type); }))
2853 /* If arg0 is derived from the address of an object or function, we may
2854    be able to fold this expression using the object or function's
2855    alignment.  */
2856 (simplify
2857  (bit_and (convert? @0) INTEGER_CST@1)
2858  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2859       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2860   (with
2861    {
2862      unsigned int align;
2863      unsigned HOST_WIDE_INT bitpos;
2864      get_pointer_alignment_1 (@0, &align, &bitpos);
2865    }
2866    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2867     { wide_int_to_tree (type, (wi::to_wide (@1)
2868                                & (bitpos / BITS_PER_UNIT))); }))))
2870 (match min_value
2871  uniform_integer_cst_p
2872  (with {
2873    tree int_cst = uniform_integer_cst_p (t);
2874    tree inner_type = TREE_TYPE (int_cst);
2875   }
2876   (if ((INTEGRAL_TYPE_P (inner_type)
2877         || POINTER_TYPE_P (inner_type))
2878        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2880 (match max_value
2881  uniform_integer_cst_p
2882  (with {
2883    tree int_cst = uniform_integer_cst_p (t);
2884    tree itype = TREE_TYPE (int_cst);
2885   }
2886  (if ((INTEGRAL_TYPE_P (itype)
2887        || POINTER_TYPE_P (itype))
2888       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2890 /* x >  y  &&  x != XXX_MIN  -->  x > y
2891    x >  y  &&  x == XXX_MIN  -->  false . */
2892 (for eqne (eq ne)
2893  (simplify
2894   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2895    (switch
2896     (if (eqne == EQ_EXPR)
2897      { constant_boolean_node (false, type); })
2898     (if (eqne == NE_EXPR)
2899      @2)
2900     )))
2902 /* x <  y  &&  x != XXX_MAX  -->  x < y
2903    x <  y  &&  x == XXX_MAX  -->  false.  */
2904 (for eqne (eq ne)
2905  (simplify
2906   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2907    (switch
2908     (if (eqne == EQ_EXPR)
2909      { constant_boolean_node (false, type); })
2910     (if (eqne == NE_EXPR)
2911      @2)
2912     )))
2914 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2915 (simplify
2916  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2917   @2)
2919 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2920 (simplify
2921  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2922   @2)
2924 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2925 (simplify
2926  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2927   @2)
2929 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2930 (simplify
2931  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2932   { constant_boolean_node (true, type); })
2934 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2935 (simplify
2936  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2937   @2)
2939 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2940 (simplify
2941  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2942   @2)
2944 /* x >=  y  ||  x != XXX_MAX   -->  true
2945    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2946 (for eqne (eq ne)
2947  (simplify
2948   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2949    (switch
2950     (if (eqne == EQ_EXPR)
2951      @2)
2952     (if (eqne == NE_EXPR)
2953      { constant_boolean_node (true, type); }))))
2955 /* y == XXX_MIN || x < y --> x <= y - 1 */
2956 (simplify
2957  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2958   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2959        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2960   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2962 /* y != XXX_MIN && x >= y --> x > y - 1 */
2963 (simplify
2964  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2965   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2966        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2967   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2969 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2970    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2971 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2972    Similarly for (X != Y).  */
2974 (for code1 (eq ne)
2975  (for code2 (eq ne lt gt le ge)
2976   (simplify
2977    (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
2978    (if ((TREE_CODE (@1) == INTEGER_CST
2979          && TREE_CODE (@2) == INTEGER_CST)
2980         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2981              || POINTER_TYPE_P (TREE_TYPE (@1)))
2982             && operand_equal_p (@1, @2)))
2983     (with
2984      {
2985       bool one_before = false;
2986       bool one_after = false;
2987       int cmp = 0;
2988       if (TREE_CODE (@1) == INTEGER_CST
2989           && TREE_CODE (@2) == INTEGER_CST)
2990         {
2991           cmp = tree_int_cst_compare (@1, @2);
2992           if (cmp < 0
2993               && wi::to_wide (@1) == wi::to_wide (@2) - 1)
2994             one_before = true;
2995           if (cmp > 0
2996               && wi::to_wide (@1) == wi::to_wide (@2) + 1)
2997             one_after = true;
2998         }
2999       bool val;
3000       switch (code2)
3001          {
3002         case EQ_EXPR: val = (cmp == 0); break;
3003         case NE_EXPR: val = (cmp != 0); break;
3004         case LT_EXPR: val = (cmp < 0); break;
3005         case GT_EXPR: val = (cmp > 0); break;
3006         case LE_EXPR: val = (cmp <= 0); break;
3007         case GE_EXPR: val = (cmp >= 0); break;
3008         default: gcc_unreachable ();
3009         }
3010      }
3011      (switch
3012       (if (code1 == EQ_EXPR && val) @3)
3013       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3014       (if (code1 == NE_EXPR && !val) @4)
3015       (if (code1 == NE_EXPR
3016            && code2 == GE_EXPR
3017            && cmp == 0)
3018        (gt @0 @1))
3019       (if (code1 == NE_EXPR
3020            && code2 == LE_EXPR
3021            && cmp == 0)
3022        (lt @0 @1))
3023       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3024       (if (code1 == NE_EXPR
3025            && code2 == GT_EXPR
3026            && one_after)
3027        (gt @0 @1))
3028       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3029       (if (code1 == NE_EXPR
3030            && code2 == LT_EXPR
3031            && one_before)
3032        (lt @0 @1))
3033      )
3034     )
3035    )
3036   )
3040 /* Convert (X OP1 CST1) && (X OP2 CST2).
3041    Convert (X OP1 Y) && (X OP2 Y).  */
3043 (for code1 (lt le gt ge)
3044  (for code2 (lt le gt ge)
3045   (simplify
3046   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3047   (if ((TREE_CODE (@1) == INTEGER_CST
3048         && TREE_CODE (@2) == INTEGER_CST)
3049        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3050             || POINTER_TYPE_P (TREE_TYPE (@1)))
3051            && operand_equal_p (@1, @2)))
3052    (with
3053     {
3054      int cmp = 0;
3055      if (TREE_CODE (@1) == INTEGER_CST
3056          && TREE_CODE (@2) == INTEGER_CST)
3057        cmp = tree_int_cst_compare (@1, @2);
3058     }
3059     (switch
3060      /* Choose the more restrictive of two < or <= comparisons.  */
3061      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3062           && (code2 == LT_EXPR || code2 == LE_EXPR))
3063       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3064        @3
3065        @4))
3066      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3067      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3068           && (code2 == GT_EXPR || code2 == GE_EXPR))
3069       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3070        @3
3071        @4))
3072      /* Check for singleton ranges.  */
3073      (if (cmp == 0
3074           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3075             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3076       (eq @0 @1))
3077      /* Check for disjoint ranges.  */
3078      (if (cmp <= 0
3079           && (code1 == LT_EXPR || code1 == LE_EXPR)
3080           && (code2 == GT_EXPR || code2 == GE_EXPR))
3081       { constant_boolean_node (false, type); })
3082      (if (cmp >= 0
3083           && (code1 == GT_EXPR || code1 == GE_EXPR)
3084           && (code2 == LT_EXPR || code2 == LE_EXPR))
3085       { constant_boolean_node (false, type); })
3086      ))))))
3088 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3089    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3090 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3091    Similarly for (X != Y).  */
3093 (for code1 (eq ne)
3094  (for code2 (eq ne lt gt le ge)
3095   (simplify
3096    (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
3097    (if ((TREE_CODE (@1) == INTEGER_CST
3098          && TREE_CODE (@2) == INTEGER_CST)
3099         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3100             || POINTER_TYPE_P (TREE_TYPE (@1)))
3101             && operand_equal_p (@1, @2)))
3102     (with
3103      {
3104       bool one_before = false;
3105       bool one_after = false;
3106       int cmp = 0;
3107       if (TREE_CODE (@1) == INTEGER_CST
3108           && TREE_CODE (@2) == INTEGER_CST)
3109         {
3110           cmp = tree_int_cst_compare (@1, @2);
3111           if (cmp < 0
3112               && wi::to_wide (@1) == wi::to_wide (@2) - 1)
3113             one_before = true;
3114           if (cmp > 0
3115               && wi::to_wide (@1) == wi::to_wide (@2) + 1)
3116             one_after = true;
3117         }
3118       bool val;
3119       switch (code2)
3120         {
3121         case EQ_EXPR: val = (cmp == 0); break;
3122         case NE_EXPR: val = (cmp != 0); break;
3123         case LT_EXPR: val = (cmp < 0); break;
3124         case GT_EXPR: val = (cmp > 0); break;
3125         case LE_EXPR: val = (cmp <= 0); break;
3126         case GE_EXPR: val = (cmp >= 0); break;
3127         default: gcc_unreachable ();
3128         }
3129      }
3130      (switch
3131       (if (code1 == EQ_EXPR && val) @4)
3132       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
3133       (if (code1 == NE_EXPR && !val) @3)
3134       (if (code1 == EQ_EXPR
3135            && code2 == GT_EXPR
3136            && cmp == 0)
3137        (ge @0 @1))
3138       (if (code1 == EQ_EXPR
3139            && code2 == LT_EXPR
3140            && cmp == 0)
3141        (le @0 @1))
3142       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3143       (if (code1 == EQ_EXPR
3144            && code2 == GE_EXPR
3145            && one_before)
3146        (ge @0 @1))
3147       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3148       (if (code1 == EQ_EXPR
3149            && code2 == LE_EXPR
3150            && one_after)
3151        (le @0 @1))
3152      )
3153     )
3154    )
3155   )
3159 /* Convert (X OP1 CST1) || (X OP2 CST2).
3160    Convert (X OP1 Y)    || (X OP2 Y).  */
3162 (for code1 (lt le gt ge)
3163  (for code2 (lt le gt ge)
3164   (simplify
3165   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3166   (if ((TREE_CODE (@1) == INTEGER_CST
3167         && TREE_CODE (@2) == INTEGER_CST)
3168        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3169             || POINTER_TYPE_P (TREE_TYPE (@1)))
3170            && operand_equal_p (@1, @2)))
3171    (with
3172     {
3173      int cmp = 0;
3174      if (TREE_CODE (@1) == INTEGER_CST
3175          && TREE_CODE (@2) == INTEGER_CST)
3176        cmp = tree_int_cst_compare (@1, @2);
3177     }
3178     (switch
3179      /* Choose the more restrictive of two < or <= comparisons.  */
3180      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3181           && (code2 == LT_EXPR || code2 == LE_EXPR))
3182       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3183        @4
3184        @3))
3185      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3186      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3187           && (code2 == GT_EXPR || code2 == GE_EXPR))
3188       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3189        @4
3190        @3))
3191      /* Check for singleton ranges.  */
3192      (if (cmp == 0
3193           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3194               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3195       (ne @0 @2))
3196      /* Check for disjoint ranges.  */
3197      (if (cmp >= 0
3198           && (code1 == LT_EXPR || code1 == LE_EXPR)
3199           && (code2 == GT_EXPR || code2 == GE_EXPR))
3200       { constant_boolean_node (true, type); })
3201      (if (cmp <= 0
3202           && (code1 == GT_EXPR || code1 == GE_EXPR)
3203           && (code2 == LT_EXPR || code2 == LE_EXPR))
3204       { constant_boolean_node (true, type); })
3205      ))))))
3207 /* Optimize (a CMP b) ^ (a CMP b)  */
3208 /* Optimize (a CMP b) != (a CMP b)  */
3209 (for op (bit_xor ne)
3210  (for cmp1 (lt lt lt le le le)
3211       cmp2 (gt eq ne ge eq ne)
3212       rcmp (ne le gt ne lt ge)
3213   (simplify
3214    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3215    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3216     (rcmp @0 @1)))))
3218 /* Optimize (a CMP b) == (a CMP b)  */
3219 (for cmp1 (lt lt lt le le le)
3220      cmp2 (gt eq ne ge eq ne)
3221      rcmp (eq gt le eq ge lt)
3222  (simplify
3223   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3224   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3225     (rcmp @0 @1))))
3227 /* We can't reassociate at all for saturating types.  */
3228 (if (!TYPE_SATURATING (type))
3230  /* Contract negates.  */
3231  /* A + (-B) -> A - B */
3232  (simplify
3233   (plus:c @0 (convert? (negate @1)))
3234   /* Apply STRIP_NOPS on the negate.  */
3235   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3236        && !TYPE_OVERFLOW_SANITIZED (type))
3237    (with
3238     {
3239      tree t1 = type;
3240      if (INTEGRAL_TYPE_P (type)
3241          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3242        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3243     }
3244     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3245  /* A - (-B) -> A + B */
3246  (simplify
3247   (minus @0 (convert? (negate @1)))
3248   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3249        && !TYPE_OVERFLOW_SANITIZED (type))
3250    (with
3251     {
3252      tree t1 = type;
3253      if (INTEGRAL_TYPE_P (type)
3254          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3255        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3256     }
3257     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3258  /* -(T)(-A) -> (T)A
3259     Sign-extension is ok except for INT_MIN, which thankfully cannot
3260     happen without overflow.  */
3261  (simplify
3262   (negate (convert (negate @1)))
3263   (if (INTEGRAL_TYPE_P (type)
3264        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3265            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3266                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3267        && !TYPE_OVERFLOW_SANITIZED (type)
3268        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3269    (convert @1)))
3270  (simplify
3271   (negate (convert negate_expr_p@1))
3272   (if (SCALAR_FLOAT_TYPE_P (type)
3273        && ((DECIMAL_FLOAT_TYPE_P (type)
3274             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3275             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3276            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3277    (convert (negate @1))))
3278  (simplify
3279   (negate (nop_convert? (negate @1)))
3280   (if (!TYPE_OVERFLOW_SANITIZED (type)
3281        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3282    (view_convert @1)))
3284  /* We can't reassociate floating-point unless -fassociative-math
3285     or fixed-point plus or minus because of saturation to +-Inf.  */
3286  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3287       && !FIXED_POINT_TYPE_P (type))
3289   /* Match patterns that allow contracting a plus-minus pair
3290      irrespective of overflow issues.  */
3291   /* (A +- B) - A       ->  +- B */
3292   /* (A +- B) -+ B      ->  A */
3293   /* A - (A +- B)       -> -+ B */
3294   /* A +- (B -+ A)      ->  +- B */
3295   (simplify
3296    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3297    (view_convert @1))
3298   (simplify
3299    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3300    (if (!ANY_INTEGRAL_TYPE_P (type)
3301         || TYPE_OVERFLOW_WRAPS (type))
3302    (negate (view_convert @1))
3303    (view_convert (negate @1))))
3304   (simplify
3305    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3306    (view_convert @0))
3307   (simplify
3308    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3309     (if (!ANY_INTEGRAL_TYPE_P (type)
3310          || TYPE_OVERFLOW_WRAPS (type))
3311      (negate (view_convert @1))
3312      (view_convert (negate @1))))
3313   (simplify
3314    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3315    (view_convert @1))
3316   /* (A +- B) + (C - A)   -> C +- B */
3317   /* (A +  B) - (A - C)   -> B + C */
3318   /* More cases are handled with comparisons.  */
3319   (simplify
3320    (plus:c (plus:c @0 @1) (minus @2 @0))
3321    (plus @2 @1))
3322   (simplify
3323    (plus:c (minus @0 @1) (minus @2 @0))
3324    (minus @2 @1))
3325   (simplify
3326    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3327    (if (TYPE_OVERFLOW_UNDEFINED (type)
3328         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3329     (pointer_diff @2 @1)))
3330   (simplify
3331    (minus (plus:c @0 @1) (minus @0 @2))
3332    (plus @1 @2))
3334   /* (A +- CST1) +- CST2 -> A + CST3
3335      Use view_convert because it is safe for vectors and equivalent for
3336      scalars.  */
3337   (for outer_op (plus minus)
3338    (for inner_op (plus minus)
3339         neg_inner_op (minus plus)
3340     (simplify
3341      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3342                CONSTANT_CLASS_P@2)
3343      /* If one of the types wraps, use that one.  */
3344      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3345       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3346          forever if something doesn't simplify into a constant.  */
3347       (if (!CONSTANT_CLASS_P (@0))
3348        (if (outer_op == PLUS_EXPR)
3349         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3350         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3351       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3352            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3353        (if (outer_op == PLUS_EXPR)
3354         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3355         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3356        /* If the constant operation overflows we cannot do the transform
3357           directly as we would introduce undefined overflow, for example
3358           with (a - 1) + INT_MIN.  */
3359        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3360         (with { tree cst = const_binop (outer_op == inner_op
3361                                         ? PLUS_EXPR : MINUS_EXPR,
3362                                         type, @1, @2); }
3363          (if (cst)
3364           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3365            (inner_op @0 { cst; } )
3366            /* X+INT_MAX+1 is X-INT_MIN.  */
3367            (if (INTEGRAL_TYPE_P (type)
3368                 && wi::to_wide (cst) == wi::min_value (type))
3369             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3370             /* Last resort, use some unsigned type.  */
3371             (with { tree utype = unsigned_type_for (type); }
3372              (if (utype)
3373               (view_convert (inner_op
3374                              (view_convert:utype @0)
3375                              (view_convert:utype
3376                               { TREE_OVERFLOW (cst)
3377                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3379   /* (CST1 - A) +- CST2 -> CST3 - A  */
3380   (for outer_op (plus minus)
3381    (simplify
3382     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3383     /* If one of the types wraps, use that one.  */
3384     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3385      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3386         forever if something doesn't simplify into a constant.  */
3387      (if (!CONSTANT_CLASS_P (@0))
3388       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3389      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3390           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3391       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3392       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3393        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3394         (if (cst && !TREE_OVERFLOW (cst))
3395          (minus { cst; } @0))))))))
3397   /* CST1 - (CST2 - A) -> CST3 + A
3398      Use view_convert because it is safe for vectors and equivalent for
3399      scalars.  */
3400   (simplify
3401    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3402    /* If one of the types wraps, use that one.  */
3403    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3404     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3405       forever if something doesn't simplify into a constant.  */
3406     (if (!CONSTANT_CLASS_P (@0))
3407      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3408     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3409          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3410      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3411      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3412       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3413        (if (cst && !TREE_OVERFLOW (cst))
3414         (plus { cst; } @0)))))))
3416 /* ((T)(A)) + CST -> (T)(A + CST)  */
3417 #if GIMPLE
3418   (simplify
3419    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3420     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3421          && TREE_CODE (type) == INTEGER_TYPE
3422          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3423          && int_fits_type_p (@1, TREE_TYPE (@0)))
3424      /* Perform binary operation inside the cast if the constant fits
3425         and (A + CST)'s range does not overflow.  */
3426      (with
3427       {
3428         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3429                           max_ovf = wi::OVF_OVERFLOW;
3430         tree inner_type = TREE_TYPE (@0);
3432         wide_int w1
3433           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3434                             TYPE_SIGN (inner_type));
3436         value_range vr;
3437         if (get_global_range_query ()->range_of_expr (vr, @0)
3438             && !vr.varying_p () && !vr.undefined_p ())
3439           {
3440             wide_int wmin0 = vr.lower_bound ();
3441             wide_int wmax0 = vr.upper_bound ();
3442             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3443             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3444           }
3445       }
3446      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3447       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3448      )))
3449 #endif
3451 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3452 #if GIMPLE
3453   (for op (plus minus)
3454    (simplify
3455     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3456      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3457           && TREE_CODE (type) == INTEGER_TYPE
3458           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3459           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3460           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3461           && TYPE_OVERFLOW_WRAPS (type))
3462        (plus (convert @0) (op @2 (convert @1))))))
3463 #endif
3465 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3466    to a simple value.  */
3467   (for op (plus minus)
3468    (simplify
3469     (op (convert @0) (convert @1))
3470      (if (INTEGRAL_TYPE_P (type)
3471           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3472           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3473           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3474           && !TYPE_OVERFLOW_TRAPS (type)
3475           && !TYPE_OVERFLOW_SANITIZED (type))
3476       (convert (op! @0 @1)))))
3478   /* ~A + A -> -1 */
3479   (simplify
3480    (plus:c (convert? (bit_not @0)) (convert? @0))
3481    (if (!TYPE_OVERFLOW_TRAPS (type))
3482     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3484   /* ~A + 1 -> -A */
3485   (simplify
3486    (plus (convert? (bit_not @0)) integer_each_onep)
3487    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3488     (negate (convert @0))))
3490   /* -A - 1 -> ~A */
3491   (simplify
3492    (minus (convert? (negate @0)) integer_each_onep)
3493    (if (!TYPE_OVERFLOW_TRAPS (type)
3494         && TREE_CODE (type) != COMPLEX_TYPE
3495         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3496     (bit_not (convert @0))))
3498   /* -1 - A -> ~A */
3499   (simplify
3500    (minus integer_all_onesp @0)
3501    (if (TREE_CODE (type) != COMPLEX_TYPE)
3502     (bit_not @0)))
3504   /* (T)(P + A) - (T)P -> (T) A */
3505   (simplify
3506    (minus (convert (plus:c @@0 @1))
3507     (convert? @0))
3508    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3509         /* For integer types, if A has a smaller type
3510            than T the result depends on the possible
3511            overflow in P + A.
3512            E.g. T=size_t, A=(unsigned)429497295, P>0.
3513            However, if an overflow in P + A would cause
3514            undefined behavior, we can assume that there
3515            is no overflow.  */
3516         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3517             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3518     (convert @1)))
3519   (simplify
3520    (minus (convert (pointer_plus @@0 @1))
3521     (convert @0))
3522    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3523         /* For pointer types, if the conversion of A to the
3524            final type requires a sign- or zero-extension,
3525            then we have to punt - it is not defined which
3526            one is correct.  */
3527         || (POINTER_TYPE_P (TREE_TYPE (@0))
3528             && TREE_CODE (@1) == INTEGER_CST
3529             && tree_int_cst_sign_bit (@1) == 0))
3530     (convert @1)))
3531    (simplify
3532     (pointer_diff (pointer_plus @@0 @1) @0)
3533     /* The second argument of pointer_plus must be interpreted as signed, and
3534        thus sign-extended if necessary.  */
3535     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3536      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3537         second arg is unsigned even when we need to consider it as signed,
3538         we don't want to diagnose overflow here.  */
3539      (convert (view_convert:stype @1))))
3541   /* (T)P - (T)(P + A) -> -(T) A */
3542   (simplify
3543    (minus (convert? @0)
3544     (convert (plus:c @@0 @1)))
3545    (if (INTEGRAL_TYPE_P (type)
3546         && TYPE_OVERFLOW_UNDEFINED (type)
3547         /* For integer literals, using an intermediate unsigned type to avoid
3548            an overflow at run time is counter-productive because it introduces
3549            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3550            the result, which may be problematic in GENERIC for some front-ends:
3551              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3552            so we use the direct path for them.  */
3553         && TREE_CODE (@1) != INTEGER_CST
3554         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3555     (with { tree utype = unsigned_type_for (type); }
3556      (convert (negate (convert:utype @1))))
3557     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3558          /* For integer types, if A has a smaller type
3559             than T the result depends on the possible
3560             overflow in P + A.
3561             E.g. T=size_t, A=(unsigned)429497295, P>0.
3562             However, if an overflow in P + A would cause
3563             undefined behavior, we can assume that there
3564             is no overflow.  */
3565          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3566              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3567      (negate (convert @1)))))
3568   (simplify
3569    (minus (convert @0)
3570     (convert (pointer_plus @@0 @1)))
3571    (if (INTEGRAL_TYPE_P (type)
3572         && TYPE_OVERFLOW_UNDEFINED (type)
3573         /* See above the rationale for this condition.  */
3574         && TREE_CODE (@1) != INTEGER_CST
3575         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3576     (with { tree utype = unsigned_type_for (type); }
3577      (convert (negate (convert:utype @1))))
3578     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3579          /* For pointer types, if the conversion of A to the
3580             final type requires a sign- or zero-extension,
3581             then we have to punt - it is not defined which
3582             one is correct.  */
3583          || (POINTER_TYPE_P (TREE_TYPE (@0))
3584              && TREE_CODE (@1) == INTEGER_CST
3585              && tree_int_cst_sign_bit (@1) == 0))
3586      (negate (convert @1)))))
3587    (simplify
3588     (pointer_diff @0 (pointer_plus @@0 @1))
3589     /* The second argument of pointer_plus must be interpreted as signed, and
3590        thus sign-extended if necessary.  */
3591     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3592      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3593         second arg is unsigned even when we need to consider it as signed,
3594         we don't want to diagnose overflow here.  */
3595      (negate (convert (view_convert:stype @1)))))
3597   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3598   (simplify
3599    (minus (convert (plus:c @@0 @1))
3600     (convert (plus:c @0 @2)))
3601    (if (INTEGRAL_TYPE_P (type)
3602         && TYPE_OVERFLOW_UNDEFINED (type)
3603         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3604         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3605     (with { tree utype = unsigned_type_for (type); }
3606      (convert (minus (convert:utype @1) (convert:utype @2))))
3607     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3608           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3609          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3610              /* For integer types, if A has a smaller type
3611                 than T the result depends on the possible
3612                 overflow in P + A.
3613                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3614                 However, if an overflow in P + A would cause
3615                 undefined behavior, we can assume that there
3616                 is no overflow.  */
3617              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3618                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3619                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3620                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3621      (minus (convert @1) (convert @2)))))
3622   (simplify
3623    (minus (convert (pointer_plus @@0 @1))
3624     (convert (pointer_plus @0 @2)))
3625    (if (INTEGRAL_TYPE_P (type)
3626         && TYPE_OVERFLOW_UNDEFINED (type)
3627         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3628     (with { tree utype = unsigned_type_for (type); }
3629      (convert (minus (convert:utype @1) (convert:utype @2))))
3630     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3631          /* For pointer types, if the conversion of A to the
3632             final type requires a sign- or zero-extension,
3633             then we have to punt - it is not defined which
3634             one is correct.  */
3635          || (POINTER_TYPE_P (TREE_TYPE (@0))
3636              && TREE_CODE (@1) == INTEGER_CST
3637              && tree_int_cst_sign_bit (@1) == 0
3638              && TREE_CODE (@2) == INTEGER_CST
3639              && tree_int_cst_sign_bit (@2) == 0))
3640      (minus (convert @1) (convert @2)))))
3641    (simplify
3642     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3643      (pointer_diff @0 @1))
3644    (simplify
3645     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3646     /* The second argument of pointer_plus must be interpreted as signed, and
3647        thus sign-extended if necessary.  */
3648     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3649      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3650         second arg is unsigned even when we need to consider it as signed,
3651         we don't want to diagnose overflow here.  */
3652      (minus (convert (view_convert:stype @1))
3653             (convert (view_convert:stype @2)))))))
3655 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3656     Modeled after fold_plusminus_mult_expr.  */
3657 (if (!TYPE_SATURATING (type)
3658      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3659  (for plusminus (plus minus)
3660   (simplify
3661    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3662    (if (!ANY_INTEGRAL_TYPE_P (type)
3663         || TYPE_OVERFLOW_WRAPS (type)
3664         || (INTEGRAL_TYPE_P (type)
3665             && tree_expr_nonzero_p (@0)
3666             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3667     (if (single_use (@3) || single_use (@4))
3668      /* If @1 +- @2 is constant require a hard single-use on either
3669         original operand (but not on both).  */
3670      (mult (plusminus @1 @2) @0)
3671      (mult! (plusminus @1 @2) @0)
3672   )))
3673   /* We cannot generate constant 1 for fract.  */
3674   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3675    (simplify
3676     (plusminus @0 (mult:c@3 @0 @2))
3677     (if ((!ANY_INTEGRAL_TYPE_P (type)
3678           || TYPE_OVERFLOW_WRAPS (type)
3679           /* For @0 + @0*@2 this transformation would introduce UB
3680              (where there was none before) for @0 in [-1,0] and @2 max.
3681              For @0 - @0*@2 this transformation would introduce UB
3682              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3683           || (INTEGRAL_TYPE_P (type)
3684               && ((tree_expr_nonzero_p (@0)
3685                    && expr_not_equal_to (@0,
3686                                 wi::minus_one (TYPE_PRECISION (type))))
3687                   || (plusminus == PLUS_EXPR
3688                       ? expr_not_equal_to (@2,
3689                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3690                       /* Let's ignore the @0 -1 and @2 min case.  */
3691                       : (expr_not_equal_to (@2,
3692                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3693                          && expr_not_equal_to (@2,
3694                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3695                                 + 1))))))
3696          && single_use (@3))
3697      (mult (plusminus { build_one_cst (type); } @2) @0)))
3698    (simplify
3699     (plusminus (mult:c@3 @0 @2) @0)
3700     (if ((!ANY_INTEGRAL_TYPE_P (type)
3701           || TYPE_OVERFLOW_WRAPS (type)
3702           /* For @0*@2 + @0 this transformation would introduce UB
3703              (where there was none before) for @0 in [-1,0] and @2 max.
3704              For @0*@2 - @0 this transformation would introduce UB
3705              for @0 0 and @2 min.  */
3706           || (INTEGRAL_TYPE_P (type)
3707               && ((tree_expr_nonzero_p (@0)
3708                    && (plusminus == MINUS_EXPR
3709                        || expr_not_equal_to (@0,
3710                                 wi::minus_one (TYPE_PRECISION (type)))))
3711                   || expr_not_equal_to (@2,
3712                         (plusminus == PLUS_EXPR
3713                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3714                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3715          && single_use (@3))
3716      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3718 #if GIMPLE
3719 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3720    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3721 (simplify
3722  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3723   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3724        && tree_fits_uhwi_p (@1)
3725        && tree_to_uhwi (@1) < element_precision (type)
3726        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3727            || optab_handler (smul_optab,
3728                              TYPE_MODE (type)) != CODE_FOR_nothing))
3729    (with { tree t = type;
3730            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3731            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3732                                              element_precision (type));
3733            w += 1;
3734            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3735                                         : t, w);
3736            cst = build_uniform_cst (t, cst); }
3737     (convert (mult (convert:t @0) { cst; })))))
3738 (simplify
3739  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3740   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3741        && tree_fits_uhwi_p (@1)
3742        && tree_to_uhwi (@1) < element_precision (type)
3743        && tree_fits_uhwi_p (@2)
3744        && tree_to_uhwi (@2) < element_precision (type)
3745        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3746            || optab_handler (smul_optab,
3747                              TYPE_MODE (type)) != CODE_FOR_nothing))
3748    (with { tree t = type;
3749            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3750            unsigned int prec = element_precision (type);
3751            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3752            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3753            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3754                                         : t, w);
3755            cst = build_uniform_cst (t, cst); }
3756     (convert (mult (convert:t @0) { cst; })))))
3757 #endif
3759 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3760    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3761    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3762 (for op (bit_ior bit_xor)
3763  (simplify
3764   (op (mult:s@0 @1 INTEGER_CST@2)
3765       (mult:s@3 @1 INTEGER_CST@4))
3766   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3767        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3768    (mult @1
3769          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3770  (simplify
3771   (op:c (mult:s@0 @1 INTEGER_CST@2)
3772         (lshift:s@3 @1 INTEGER_CST@4))
3773   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3774        && tree_int_cst_sgn (@4) > 0
3775        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3776    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3777            wide_int c = wi::add (wi::to_wide (@2),
3778                                  wi::lshift (wone, wi::to_wide (@4))); }
3779     (mult @1 { wide_int_to_tree (type, c); }))))
3780  (simplify
3781   (op:c (mult:s@0 @1 INTEGER_CST@2)
3782         @1)
3783   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3784        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3785    (mult @1
3786          { wide_int_to_tree (type,
3787                              wi::add (wi::to_wide (@2), 1)); })))
3788  (simplify
3789   (op (lshift:s@0 @1 INTEGER_CST@2)
3790       (lshift:s@3 @1 INTEGER_CST@4))
3791   (if (INTEGRAL_TYPE_P (type)
3792        && tree_int_cst_sgn (@2) > 0
3793        && tree_int_cst_sgn (@4) > 0
3794        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3795    (with { tree t = type;
3796            if (!TYPE_OVERFLOW_WRAPS (t))
3797              t = unsigned_type_for (t);
3798            wide_int wone = wi::one (TYPE_PRECISION (t));
3799            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3800                                  wi::lshift (wone, wi::to_wide (@4))); }
3801     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3802  (simplify
3803   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3804         @1)
3805   (if (INTEGRAL_TYPE_P (type)
3806        && tree_int_cst_sgn (@2) > 0
3807        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3808    (with { tree t = type;
3809            if (!TYPE_OVERFLOW_WRAPS (t))
3810              t = unsigned_type_for (t);
3811            wide_int wone = wi::one (TYPE_PRECISION (t));
3812            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3813     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3815 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3817 (for minmax (min max)
3818  (simplify
3819   (minmax @0 @0)
3820   @0)
3821 /* max(max(x,y),x) -> max(x,y)  */
3822  (simplify
3823   (minmax:c (minmax:c@2 @0 @1) @0)
3824   @2))
3825 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3826 (for minmax (FMIN_ALL FMAX_ALL)
3827  (simplify
3828   (minmax @0 @0)
3829   (if (!tree_expr_maybe_signaling_nan_p (@0))
3830     @0)))
3831 /* min(max(x,y),y) -> y.  */
3832 (simplify
3833  (min:c (max:c @0 @1) @1)
3834  @1)
3835 /* max(min(x,y),y) -> y.  */
3836 (simplify
3837  (max:c (min:c @0 @1) @1)
3838  @1)
3839 /* max(a,-a) -> abs(a).  */
3840 (simplify
3841  (max:c @0 (negate @0))
3842  (if (TREE_CODE (type) != COMPLEX_TYPE
3843       && (! ANY_INTEGRAL_TYPE_P (type)
3844           || TYPE_OVERFLOW_UNDEFINED (type)))
3845   (abs @0)))
3846 /* min(a,-a) -> -abs(a).  */
3847 (simplify
3848  (min:c @0 (negate @0))
3849  (if (TREE_CODE (type) != COMPLEX_TYPE
3850       && (! ANY_INTEGRAL_TYPE_P (type)
3851           || TYPE_OVERFLOW_UNDEFINED (type)))
3852   (negate (abs @0))))
3853 (simplify
3854  (min @0 @1)
3855  (switch
3856   (if (INTEGRAL_TYPE_P (type)
3857        && TYPE_MIN_VALUE (type)
3858        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3859    @1)
3860   (if (INTEGRAL_TYPE_P (type)
3861        && TYPE_MAX_VALUE (type)
3862        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3863    @0)))
3864 (simplify
3865  (max @0 @1)
3866  (switch
3867   (if (INTEGRAL_TYPE_P (type)
3868        && TYPE_MAX_VALUE (type)
3869        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3870    @1)
3871   (if (INTEGRAL_TYPE_P (type)
3872        && TYPE_MIN_VALUE (type)
3873        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3874    @0)))
3876 /* max (a, a + CST) -> a + CST where CST is positive.  */
3877 /* max (a, a + CST) -> a where CST is negative.  */
3878 (simplify
3879  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3880   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3881    (if (tree_int_cst_sgn (@1) > 0)
3882     @2
3883     @0)))
3885 /* min (a, a + CST) -> a where CST is positive.  */
3886 /* min (a, a + CST) -> a + CST where CST is negative. */
3887 (simplify
3888  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3889   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3890    (if (tree_int_cst_sgn (@1) > 0)
3891     @0
3892     @2)))
3894 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3895    the addresses are known to be less, equal or greater.  */
3896 (for minmax (min max)
3897      cmp (lt gt)
3898  (simplify
3899   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3900   (with
3901    {
3902      poly_int64 off0, off1;
3903      tree base0, base1;
3904      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3905                                   off0, off1, GENERIC);
3906    }
3907    (if (equal == 1)
3908     (if (minmax == MIN_EXPR)
3909      (if (known_le (off0, off1))
3910       @2
3911       (if (known_gt (off0, off1))
3912        @3))
3913      (if (known_ge (off0, off1))
3914       @2
3915       (if (known_lt (off0, off1))
3916        @3)))))))
3918 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3919    and the outer convert demotes the expression back to x's type.  */
3920 (for minmax (min max)
3921  (simplify
3922   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3923   (if (INTEGRAL_TYPE_P (type)
3924        && types_match (@1, type) && int_fits_type_p (@2, type)
3925        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3926        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3927    (minmax @1 (convert @2)))))
3929 (for minmax (FMIN_ALL FMAX_ALL)
3930  /* If either argument is NaN and other one is not sNaN, return the other
3931     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3932  (simplify
3933   (minmax:c @0 REAL_CST@1)
3934    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3935        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3936        && !tree_expr_maybe_signaling_nan_p (@0))
3937    @0)))
3938 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3939    functions to return the numeric arg if the other one is NaN.
3940    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3941    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3942    worry about it either.  */
3943 (if (flag_finite_math_only)
3944  (simplify
3945   (FMIN_ALL @0 @1)
3946   (min @0 @1))
3947  (simplify
3948   (FMAX_ALL @0 @1)
3949   (max @0 @1)))
3950 /* min (-A, -B) -> -max (A, B)  */
3951 (for minmax (min max FMIN_ALL FMAX_ALL)
3952      maxmin (max min FMAX_ALL FMIN_ALL)
3953  (simplify
3954   (minmax (negate:s@2 @0) (negate:s@3 @1))
3955   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3956        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3957            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3958    (negate (maxmin @0 @1)))))
3959 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3960    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3961 (for minmax (min max)
3962  maxmin (max min)
3963  (simplify
3964   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3965   (bit_not (maxmin @0 @1)))
3966 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
3967 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
3968  (simplify
3969   (bit_not (minmax:cs (bit_not @0) @1))
3970   (maxmin @0 (bit_not @1))))
3972 /* MIN (X, Y) == X -> X <= Y  */
3973 /* MIN (X, Y) < X -> X > Y  */
3974 /* MIN (X, Y) >= X -> X <= Y  */
3975 (for minmax (min min min min max max max max)
3976      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
3977      out    (le  gt  gt  le  ge  lt  lt  ge )
3978  (simplify
3979   (cmp:c (minmax:c @0 @1) @0)
3980   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3981    (out @0 @1))))
3982 /* MIN (X, 5) == 0 -> X == 0
3983    MIN (X, 5) == 7 -> false  */
3984 (for cmp (eq ne)
3985  (simplify
3986   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3987   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3988                  TYPE_SIGN (TREE_TYPE (@0))))
3989    { constant_boolean_node (cmp == NE_EXPR, type); }
3990    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3991                   TYPE_SIGN (TREE_TYPE (@0))))
3992     (cmp @0 @2)))))
3993 (for cmp (eq ne)
3994  (simplify
3995   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3996   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3997                  TYPE_SIGN (TREE_TYPE (@0))))
3998    { constant_boolean_node (cmp == NE_EXPR, type); }
3999    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4000                   TYPE_SIGN (TREE_TYPE (@0))))
4001     (cmp @0 @2)))))
4003 /* X <= MAX(X, Y) -> true
4004    X > MAX(X, Y) -> false 
4005    X >= MIN(X, Y) -> true
4006    X < MIN(X, Y) -> false */
4007 (for minmax (min     min     max     max     )
4008      cmp    (ge      lt      le      gt      )
4009  (simplify
4010   (cmp:c @0 (minmax:c @0 @1))
4011   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4013 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4014 (for minmax (min     min     max     max     min     min     max     max    )
4015      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4016      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4017  (simplify
4018   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4019   (comb (cmp @0 @2) (cmp @1 @2))))
4021 /* Undo fancy ways of writing max/min or other ?: expressions, like
4022    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4023    People normally use ?: and that is what we actually try to optimize.  */
4024 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4025 (simplify
4026  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4027  (if (INTEGRAL_TYPE_P (type)
4028       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4029   (cond (convert:boolean_type_node @2) @1 @0)))
4030 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4031 (simplify
4032  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4033  (if (INTEGRAL_TYPE_P (type)
4034       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4035   (cond (convert:boolean_type_node @2) @1 @0)))
4036 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4037 (simplify
4038  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4039  (if (INTEGRAL_TYPE_P (type)
4040       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4041   (cond (convert:boolean_type_node @2) @1 @0)))
4043 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4044 (simplify
4045   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4046   (max @2 @1))
4048 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4049 (for op (bit_xor bit_ior plus)
4050  (simplify
4051   (cond (eq zero_one_valued_p@0
4052             integer_zerop)
4053         @1
4054         (op:c @2 @1))
4055   (if (INTEGRAL_TYPE_P (type)
4056        && TYPE_PRECISION (type) > 1
4057        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4058        (op (mult (convert:type @0) @2) @1))))
4060 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4061 (for op (bit_xor bit_ior plus)
4062  (simplify
4063   (cond (ne zero_one_valued_p@0
4064             integer_zerop)
4065        (op:c @2 @1)
4066         @1)
4067   (if (INTEGRAL_TYPE_P (type)
4068        && TYPE_PRECISION (type) > 1
4069        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4070        (op (mult (convert:type @0) @2) @1))))
4072 /* Simplifications of shift and rotates.  */
4074 (for rotate (lrotate rrotate)
4075  (simplify
4076   (rotate integer_all_onesp@0 @1)
4077   @0))
4079 /* Optimize -1 >> x for arithmetic right shifts.  */
4080 (simplify
4081  (rshift integer_all_onesp@0 @1)
4082  (if (!TYPE_UNSIGNED (type))
4083   @0))
4085 /* Optimize (x >> c) << c into x & (-1<<c).  */
4086 (simplify
4087  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4088  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4089   /* It doesn't matter if the right shift is arithmetic or logical.  */
4090   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4092 (simplify
4093  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4094  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4095       /* Allow intermediate conversion to integral type with whatever sign, as
4096          long as the low TYPE_PRECISION (type)
4097          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4098       && INTEGRAL_TYPE_P (type)
4099       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4100       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4101       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4102       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4103           || wi::geu_p (wi::to_wide (@1),
4104                         TYPE_PRECISION (type)
4105                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4106   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4108 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4109    unsigned x OR truncate into the precision(type) - c lowest bits
4110    of signed x (if they have mode precision or a precision of 1).  */
4111 (simplify
4112  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4113  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4114   (if (TYPE_UNSIGNED (type))
4115    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4116    (if (INTEGRAL_TYPE_P (type))
4117     (with {
4118       int width = element_precision (type) - tree_to_uhwi (@1);
4119       tree stype = NULL_TREE;
4120       if (width <= MAX_FIXED_MODE_SIZE)
4121         stype = build_nonstandard_integer_type (width, 0);
4122      }
4123      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4124       (convert (convert:stype @0))))))))
4126 /* Optimize x >> x into 0 */
4127 (simplify
4128  (rshift @0 @0)
4129   { build_zero_cst (type); })
4131 (for shiftrotate (lrotate rrotate lshift rshift)
4132  (simplify
4133   (shiftrotate @0 integer_zerop)
4134   (non_lvalue @0))
4135  (simplify
4136   (shiftrotate integer_zerop@0 @1)
4137   @0)
4138  /* Prefer vector1 << scalar to vector1 << vector2
4139     if vector2 is uniform.  */
4140  (for vec (VECTOR_CST CONSTRUCTOR)
4141   (simplify
4142    (shiftrotate @0 vec@1)
4143    (with { tree tem = uniform_vector_p (@1); }
4144     (if (tem)
4145      (shiftrotate @0 { tem; }))))))
4147 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4148    Y is 0.  Similarly for X >> Y.  */
4149 #if GIMPLE
4150 (for shift (lshift rshift)
4151  (simplify
4152   (shift @0 SSA_NAME@1)
4153    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4154     (with {
4155       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4156       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4157      }
4158      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4159       @0)))))
4160 #endif
4162 /* Rewrite an LROTATE_EXPR by a constant into an
4163    RROTATE_EXPR by a new constant.  */
4164 (simplify
4165  (lrotate @0 INTEGER_CST@1)
4166  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4167                             build_int_cst (TREE_TYPE (@1),
4168                                            element_precision (type)), @1); }))
4170 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4171 (for op (lrotate rrotate rshift lshift)
4172  (simplify
4173   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4174   (with { unsigned int prec = element_precision (type); }
4175    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4176         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4177         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4178         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4179     (with { unsigned int low = (tree_to_uhwi (@1)
4180                                 + tree_to_uhwi (@2)); }
4181      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4182         being well defined.  */
4183      (if (low >= prec)
4184       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4185        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4186        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4187         { build_zero_cst (type); }
4188         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4189       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4192 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4193 (simplify
4194  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4195   (if ((wi::to_wide (@1) & 1) != 0)
4196    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4197    { build_zero_cst (type); }))
4199 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4200    either to false if D is smaller (unsigned comparison) than C, or to
4201    x == log2 (D) - log2 (C).  Similarly for right shifts.
4202    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4203 (for cmp (ne eq)
4204      icmp (eq ne)
4205  (simplify
4206   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4207    (with { int c1 = wi::clz (wi::to_wide (@1));
4208            int c2 = wi::clz (wi::to_wide (@2)); }
4209     (if (c1 < c2)
4210      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4211      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4212  (simplify
4213   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4214    (if (tree_int_cst_sgn (@1) > 0)
4215     (with { int c1 = wi::clz (wi::to_wide (@1));
4216             int c2 = wi::clz (wi::to_wide (@2)); }
4217      (if (c1 > c2)
4218       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4219       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4220  /* `(1 >> X) != 0` -> `X == 0` */
4221  /* `(1 >> X) == 0` -> `X != 0` */
4222  (simplify
4223   (cmp (rshift integer_onep@1 @0) integer_zerop)
4224    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4225     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4227 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4228    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4229    if CST2 != 0.  */
4230 (for cmp (ne eq)
4231  (simplify
4232   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4233   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4234    (if (cand < 0
4235         || (!integer_zerop (@2)
4236             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4237     { constant_boolean_node (cmp == NE_EXPR, type); }
4238     (if (!integer_zerop (@2)
4239          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4240      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4242 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4243         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4244 (for cmp (ne eq)
4245  (simplify
4246   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4247   (if (tree_fits_shwi_p (@1)
4248        && tree_to_shwi (@1) > 0
4249        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4250     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4251       { constant_boolean_node (cmp == NE_EXPR, type); }
4252       (with { wide_int c1 = wi::to_wide (@1);
4253               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4254               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4255         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4256              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4257  (simplify
4258   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4259   (if (tree_fits_shwi_p (@1)
4260        && tree_to_shwi (@1) > 0
4261        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4262     (with { tree t0 = TREE_TYPE (@0);
4263             unsigned int prec = TYPE_PRECISION (t0);
4264             wide_int c1 = wi::to_wide (@1);
4265             wide_int c2 = wi::to_wide (@2);
4266             wide_int c3 = wi::to_wide (@3);
4267             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4268       (if ((c2 & c3) != c3)
4269         { constant_boolean_node (cmp == NE_EXPR, type); }
4270         (if (TYPE_UNSIGNED (t0))
4271           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4272             { constant_boolean_node (cmp == NE_EXPR, type); }
4273             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4274                  { wide_int_to_tree (t0, c3 << c1); }))
4275           (with { wide_int smask = wi::arshift (sb, c1); }
4276             (switch
4277               (if ((c2 & smask) == 0)
4278                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4279                      { wide_int_to_tree (t0, c3 << c1); }))
4280               (if ((c3 & smask) == 0)
4281                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4282                      { wide_int_to_tree (t0, c3 << c1); }))
4283               (if ((c2 & smask) != (c3 & smask))
4284                 { constant_boolean_node (cmp == NE_EXPR, type); })
4285               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4286                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4288 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4289         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4290    if the new mask might be further optimized.  */
4291 (for shift (lshift rshift)
4292  (simplify
4293   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4294            INTEGER_CST@2)
4295    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4296         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4297         && tree_fits_uhwi_p (@1)
4298         && tree_to_uhwi (@1) > 0
4299         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4300     (with
4301      {
4302        unsigned int shiftc = tree_to_uhwi (@1);
4303        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4304        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4305        tree shift_type = TREE_TYPE (@3);
4306        unsigned int prec;
4308        if (shift == LSHIFT_EXPR)
4309          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4310        else if (shift == RSHIFT_EXPR
4311                 && type_has_mode_precision_p (shift_type))
4312          {
4313            prec = TYPE_PRECISION (TREE_TYPE (@3));
4314            tree arg00 = @0;
4315            /* See if more bits can be proven as zero because of
4316               zero extension.  */
4317            if (@3 != @0
4318                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4319              {
4320                tree inner_type = TREE_TYPE (@0);
4321                if (type_has_mode_precision_p (inner_type)
4322                    && TYPE_PRECISION (inner_type) < prec)
4323                  {
4324                    prec = TYPE_PRECISION (inner_type);
4325                    /* See if we can shorten the right shift.  */
4326                    if (shiftc < prec)
4327                      shift_type = inner_type;
4328                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4329                       it into (X, 0) later on by making sure zerobits
4330                       is all ones.  */
4331                  }
4332              }
4333            zerobits = HOST_WIDE_INT_M1U;
4334            if (shiftc < prec)
4335              {
4336                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4337                zerobits <<= prec - shiftc;
4338              }
4339            /* For arithmetic shift if sign bit could be set, zerobits
4340               can contain actually sign bits, so no transformation is
4341               possible, unless MASK masks them all away.  In that
4342               case the shift needs to be converted into logical shift.  */
4343            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4344                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4345              {
4346                if ((mask & zerobits) == 0)
4347                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4348                else
4349                  zerobits = 0;
4350              }
4351          }
4352      }
4353      /* ((X << 16) & 0xff00) is (X, 0).  */
4354      (if ((mask & zerobits) == mask)
4355       { build_int_cst (type, 0); }
4356       (with { newmask = mask | zerobits; }
4357        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4358         (with
4359          {
4360            /* Only do the transformation if NEWMASK is some integer
4361               mode's mask.  */
4362            for (prec = BITS_PER_UNIT;
4363                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4364              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4365                break;
4366          }
4367          (if (prec < HOST_BITS_PER_WIDE_INT
4368               || newmask == HOST_WIDE_INT_M1U)
4369           (with
4370            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4371            (if (!tree_int_cst_equal (newmaskt, @2))
4372             (if (shift_type != TREE_TYPE (@3))
4373              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4374              (bit_and @4 { newmaskt; })))))))))))))
4376 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4377 (for cmp (ne eq)
4378        icmp (eq ne)
4379  (simplify
4380   (cmp
4381    (bit_and
4382     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4383   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4384    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4385                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4387 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4388    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4389 (for shift (lshift rshift)
4390  (for bit_op (bit_and bit_xor bit_ior)
4391   (simplify
4392    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4393    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4394     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4395      (if (mask)
4396       (bit_op (shift (convert @0) @1) { mask; })))))))
4398 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4399 (simplify
4400  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4401   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4402        && (element_precision (TREE_TYPE (@0))
4403            <= element_precision (TREE_TYPE (@1))
4404            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4405    (with
4406     { tree shift_type = TREE_TYPE (@0); }
4407      (convert (rshift (convert:shift_type @1) @2)))))
4409 /* ~(~X >>r Y) -> X >>r Y
4410    ~(~X <<r Y) -> X <<r Y */
4411 (for rotate (lrotate rrotate)
4412  (simplify
4413   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4414    (if ((element_precision (TREE_TYPE (@0))
4415          <= element_precision (TREE_TYPE (@1))
4416          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4417         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4418             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4419     (with
4420      { tree rotate_type = TREE_TYPE (@0); }
4421       (convert (rotate (convert:rotate_type @1) @2))))))
4423 (for cmp (eq ne)
4424  (for rotate (lrotate rrotate)
4425       invrot (rrotate lrotate)
4426   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4427   (simplify
4428    (cmp (rotate @1 @0) (rotate @2 @0))
4429    (cmp @1 @2))
4430   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4431   (simplify
4432    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4433    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4434   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4435   (simplify
4436    (cmp (rotate @0 @1) INTEGER_CST@2)
4437     (if (integer_zerop (@2) || integer_all_onesp (@2))
4438      (cmp @0 @2)))))
4440 /* Narrow a lshift by constant.  */
4441 (simplify
4442  (convert (lshift:s@0 @1 INTEGER_CST@2))
4443  (if (INTEGRAL_TYPE_P (type)
4444       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4445       && !integer_zerop (@2)
4446       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4447   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4448        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4449    (lshift (convert @1) @2)
4450    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4451     { build_zero_cst (type); }))))
4453 /* Simplifications of conversions.  */
4455 /* Basic strip-useless-type-conversions / strip_nops.  */
4456 (for cvt (convert view_convert float fix_trunc)
4457  (simplify
4458   (cvt @0)
4459   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4460        || (GENERIC && type == TREE_TYPE (@0)))
4461    @0)))
4463 /* Contract view-conversions.  */
4464 (simplify
4465   (view_convert (view_convert @0))
4466   (view_convert @0))
4468 /* For integral conversions with the same precision or pointer
4469    conversions use a NOP_EXPR instead.  */
4470 (simplify
4471   (view_convert @0)
4472   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4473        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4474        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4475    (convert @0)))
4477 /* Strip inner integral conversions that do not change precision or size, or
4478    zero-extend while keeping the same size (for bool-to-char).  */
4479 (simplify
4480   (view_convert (convert@0 @1))
4481   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4482        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4483        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4484        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4485            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4486                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4487    (view_convert @1)))
4489 /* Simplify a view-converted empty or single-element constructor.  */
4490 (simplify
4491   (view_convert CONSTRUCTOR@0)
4492   (with
4493    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4494                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4495    (switch
4496     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4497      { build_zero_cst (type); })
4498     (if (CONSTRUCTOR_NELTS (ctor) == 1
4499          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4500          && operand_equal_p (TYPE_SIZE (type),
4501                              TYPE_SIZE (TREE_TYPE
4502                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4503      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4505 /* Re-association barriers around constants and other re-association
4506    barriers can be removed.  */
4507 (simplify
4508  (paren CONSTANT_CLASS_P@0)
4509  @0)
4510 (simplify
4511  (paren (paren@1 @0))
4512  @1)
4514 /* Handle cases of two conversions in a row.  */
4515 (for ocvt (convert float fix_trunc)
4516  (for icvt (convert float)
4517   (simplify
4518    (ocvt (icvt@1 @0))
4519    (with
4520     {
4521       tree inside_type = TREE_TYPE (@0);
4522       tree inter_type = TREE_TYPE (@1);
4523       int inside_int = INTEGRAL_TYPE_P (inside_type);
4524       int inside_ptr = POINTER_TYPE_P (inside_type);
4525       int inside_float = FLOAT_TYPE_P (inside_type);
4526       int inside_vec = VECTOR_TYPE_P (inside_type);
4527       unsigned int inside_prec = element_precision (inside_type);
4528       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4529       int inter_int = INTEGRAL_TYPE_P (inter_type);
4530       int inter_ptr = POINTER_TYPE_P (inter_type);
4531       int inter_float = FLOAT_TYPE_P (inter_type);
4532       int inter_vec = VECTOR_TYPE_P (inter_type);
4533       unsigned int inter_prec = element_precision (inter_type);
4534       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4535       int final_int = INTEGRAL_TYPE_P (type);
4536       int final_ptr = POINTER_TYPE_P (type);
4537       int final_float = FLOAT_TYPE_P (type);
4538       int final_vec = VECTOR_TYPE_P (type);
4539       unsigned int final_prec = element_precision (type);
4540       int final_unsignedp = TYPE_UNSIGNED (type);
4541     }
4542    (switch
4543     /* In addition to the cases of two conversions in a row
4544        handled below, if we are converting something to its own
4545        type via an object of identical or wider precision, neither
4546        conversion is needed.  */
4547     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4548           || (GENERIC
4549               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4550          && (((inter_int || inter_ptr) && final_int)
4551              || (inter_float && final_float))
4552          && inter_prec >= final_prec)
4553      (ocvt @0))
4555     /* Likewise, if the intermediate and initial types are either both
4556        float or both integer, we don't need the middle conversion if the
4557        former is wider than the latter and doesn't change the signedness
4558        (for integers).  Avoid this if the final type is a pointer since
4559        then we sometimes need the middle conversion.  */
4560     (if (((inter_int && inside_int) || (inter_float && inside_float))
4561          && (final_int || final_float)
4562          && inter_prec >= inside_prec
4563          && (inter_float || inter_unsignedp == inside_unsignedp))
4564      (ocvt @0))
4566     /* If we have a sign-extension of a zero-extended value, we can
4567        replace that by a single zero-extension.  Likewise if the
4568        final conversion does not change precision we can drop the
4569        intermediate conversion.  */
4570     (if (inside_int && inter_int && final_int
4571          && ((inside_prec < inter_prec && inter_prec < final_prec
4572               && inside_unsignedp && !inter_unsignedp)
4573              || final_prec == inter_prec))
4574      (ocvt @0))
4576     /* Two conversions in a row are not needed unless:
4577         - some conversion is floating-point (overstrict for now), or
4578         - some conversion is a vector (overstrict for now), or
4579         - the intermediate type is narrower than both initial and
4580           final, or
4581         - the intermediate type and innermost type differ in signedness,
4582           and the outermost type is wider than the intermediate, or
4583         - the initial type is a pointer type and the precisions of the
4584           intermediate and final types differ, or
4585         - the final type is a pointer type and the precisions of the
4586           initial and intermediate types differ.  */
4587     (if (! inside_float && ! inter_float && ! final_float
4588          && ! inside_vec && ! inter_vec && ! final_vec
4589          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4590          && ! (inside_int && inter_int
4591                && inter_unsignedp != inside_unsignedp
4592                && inter_prec < final_prec)
4593          && ((inter_unsignedp && inter_prec > inside_prec)
4594              == (final_unsignedp && final_prec > inter_prec))
4595          && ! (inside_ptr && inter_prec != final_prec)
4596          && ! (final_ptr && inside_prec != inter_prec))
4597      (ocvt @0))
4599    /* `(outer:M)(inter:N) a:O`
4600       can be converted to `(outer:M) a`
4601       if M <= O && N >= O. No matter what signedness of the casts,
4602       as the final is either a truncation from the original or just
4603       a sign change of the type. */
4604    (if (inside_int && inter_int && final_int
4605         && final_prec <= inside_prec
4606         && inter_prec >= inside_prec)
4607     (convert @0))
4609     /* A truncation to an unsigned type (a zero-extension) should be
4610        canonicalized as bitwise and of a mask.  */
4611     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4612          && final_int && inter_int && inside_int
4613          && final_prec == inside_prec
4614          && final_prec > inter_prec
4615          && inter_unsignedp)
4616      (convert (bit_and @0 { wide_int_to_tree
4617                               (inside_type,
4618                                wi::mask (inter_prec, false,
4619                                          TYPE_PRECISION (inside_type))); })))
4621     /* If we are converting an integer to a floating-point that can
4622        represent it exactly and back to an integer, we can skip the
4623        floating-point conversion.  */
4624     (if (GIMPLE /* PR66211 */
4625          && inside_int && inter_float && final_int &&
4626          (unsigned) significand_size (TYPE_MODE (inter_type))
4627          >= inside_prec - !inside_unsignedp)
4628      (convert @0)))))))
4630 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4631    float_type.  Only do the transformation if we do not need to preserve
4632    trapping behaviour, so require !flag_trapping_math. */
4633 #if GIMPLE
4634 (simplify
4635    (float (fix_trunc @0))
4636    (if (!flag_trapping_math
4637         && types_match (type, TREE_TYPE (@0))
4638         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4639                                           OPTIMIZE_FOR_BOTH))
4640       (IFN_TRUNC @0)))
4641 #endif
4643 /* If we have a narrowing conversion to an integral type that is fed by a
4644    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4645    masks off bits outside the final type (and nothing else).  */
4646 (simplify
4647   (convert (bit_and @0 INTEGER_CST@1))
4648   (if (INTEGRAL_TYPE_P (type)
4649        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4650        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4651        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4652                                                     TYPE_PRECISION (type)), 0))
4653    (convert @0)))
4656 /* (X /[ex] A) * A -> X.  */
4657 (simplify
4658   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4659   (convert @0))
4661 /* Simplify (A / B) * B + (A % B) -> A.  */
4662 (for div (trunc_div ceil_div floor_div round_div)
4663      mod (trunc_mod ceil_mod floor_mod round_mod)
4664   (simplify
4665    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4666    @0))
4668 /* x / y * y == x -> x % y == 0.  */
4669 (simplify
4670   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4671   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4672     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4674 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4675 (for op (plus minus)
4676  (simplify
4677   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4678   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4679        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4680    (with
4681      {
4682        wi::overflow_type overflow;
4683        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4684                                TYPE_SIGN (type), &overflow);
4685      }
4686      (if (types_match (type, TREE_TYPE (@2))
4687          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4688       (op @0 { wide_int_to_tree (type, mul); })
4689       (with { tree utype = unsigned_type_for (type); }
4690        (convert (op (convert:utype @0)
4691                     (mult (convert:utype @1) (convert:utype @2))))))))))
4693 /* Canonicalization of binary operations.  */
4695 /* Convert X + -C into X - C.  */
4696 (simplify
4697  (plus @0 REAL_CST@1)
4698  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4699   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4700    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4701     (minus @0 { tem; })))))
4703 /* Convert x+x into x*2.  */
4704 (simplify
4705  (plus @0 @0)
4706  (if (SCALAR_FLOAT_TYPE_P (type))
4707   (mult @0 { build_real (type, dconst2); })
4708   (if (INTEGRAL_TYPE_P (type))
4709    (mult @0 { build_int_cst (type, 2); }))))
4711 /* 0 - X  ->  -X.  */
4712 (simplify
4713  (minus integer_zerop @1)
4714  (negate @1))
4715 (simplify
4716  (pointer_diff integer_zerop @1)
4717  (negate (convert @1)))
4719 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4720    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4721    (-ARG1 + ARG0) reduces to -ARG1.  */
4722 (simplify
4723  (minus real_zerop@0 @1)
4724  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4725   (negate @1)))
4727 /* Transform x * -1 into -x.  */
4728 (simplify
4729  (mult @0 integer_minus_onep)
4730  (negate @0))
4732 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4733    signed overflow for CST != 0 && CST != -1.  */
4734 (simplify
4735  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4736  (if (TREE_CODE (@2) != INTEGER_CST
4737       && single_use (@3)
4738       && !integer_zerop (@1) && !integer_minus_onep (@1))
4739   (mult (mult @0 @2) @1)))
4741 /* True if we can easily extract the real and imaginary parts of a complex
4742    number.  */
4743 (match compositional_complex
4744  (convert? (complex @0 @1)))
4746 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4747 (simplify
4748  (complex (realpart @0) (imagpart @0))
4749  @0)
4750 (simplify
4751  (realpart (complex @0 @1))
4752  @0)
4753 (simplify
4754  (imagpart (complex @0 @1))
4755  @1)
4757 /* Sometimes we only care about half of a complex expression.  */
4758 (simplify
4759  (realpart (convert?:s (conj:s @0)))
4760  (convert (realpart @0)))
4761 (simplify
4762  (imagpart (convert?:s (conj:s @0)))
4763  (convert (negate (imagpart @0))))
4764 (for part (realpart imagpart)
4765  (for op (plus minus)
4766   (simplify
4767    (part (convert?:s@2 (op:s @0 @1)))
4768    (convert (op (part @0) (part @1))))))
4769 (simplify
4770  (realpart (convert?:s (CEXPI:s @0)))
4771  (convert (COS @0)))
4772 (simplify
4773  (imagpart (convert?:s (CEXPI:s @0)))
4774  (convert (SIN @0)))
4776 /* conj(conj(x)) -> x  */
4777 (simplify
4778  (conj (convert? (conj @0)))
4779  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4780   (convert @0)))
4782 /* conj({x,y}) -> {x,-y}  */
4783 (simplify
4784  (conj (convert?:s (complex:s @0 @1)))
4785  (with { tree itype = TREE_TYPE (type); }
4786   (complex (convert:itype @0) (negate (convert:itype @1)))))
4788 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4789 (for bswap (BSWAP)
4790  (simplify
4791   (bswap (bswap @0))
4792   @0)
4793  (simplify
4794   (bswap (bit_not (bswap @0)))
4795   (bit_not @0))
4796  (for bitop (bit_xor bit_ior bit_and)
4797   (simplify
4798    (bswap (bitop:c (bswap @0) @1))
4799    (bitop @0 (bswap @1))))
4800  (for cmp (eq ne)
4801   (simplify
4802    (cmp (bswap@2 @0) (bswap @1))
4803    (with { tree ctype = TREE_TYPE (@2); }
4804     (cmp (convert:ctype @0) (convert:ctype @1))))
4805   (simplify
4806    (cmp (bswap @0) INTEGER_CST@1)
4807    (with { tree ctype = TREE_TYPE (@1); }
4808     (cmp (convert:ctype @0) (bswap! @1)))))
4809  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4810  (simplify
4811   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4812            INTEGER_CST@3)
4813    (if (BITS_PER_UNIT == 8
4814         && tree_fits_uhwi_p (@2)
4815         && tree_fits_uhwi_p (@3))
4816     (with
4817      {
4818       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4819       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4820       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4821       unsigned HOST_WIDE_INT lo = bits & 7;
4822       unsigned HOST_WIDE_INT hi = bits - lo;
4823      }
4824      (if (bits < prec
4825           && mask < (256u>>lo)
4826           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4827       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4828        (if (ns == 0)
4829         (bit_and (convert @1) @3)
4830         (with
4831          {
4832           tree utype = unsigned_type_for (TREE_TYPE (@1));
4833           tree nst = build_int_cst (integer_type_node, ns);
4834          }
4835          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4836  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4837  (simplify
4838   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4839    (if (BITS_PER_UNIT == 8
4840         && CHAR_TYPE_SIZE == 8
4841         && tree_fits_uhwi_p (@1))
4842     (with
4843      {
4844       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4845       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4846       /* If the bswap was extended before the original shift, this
4847          byte (shift) has the sign of the extension, not the sign of
4848          the original shift.  */
4849       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4850      }
4851      /* Special case: logical right shift of sign-extended bswap.
4852         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4853      (if (TYPE_PRECISION (type) > prec
4854           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4855           && TYPE_UNSIGNED (type)
4856           && bits < prec && bits + 8 >= prec)
4857       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4858        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4859       (if (bits + 8 == prec)
4860        (if (TYPE_UNSIGNED (st))
4861         (convert (convert:unsigned_char_type_node @0))
4862         (convert (convert:signed_char_type_node @0)))
4863        (if (bits < prec && bits + 8 > prec)
4864         (with 
4865          {
4866           tree nst = build_int_cst (integer_type_node, bits & 7);
4867           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4868                                        : signed_char_type_node;
4869          }
4870          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4871  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4872  (simplify
4873   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4874    (if (BITS_PER_UNIT == 8
4875         && tree_fits_uhwi_p (@1)
4876         && tree_to_uhwi (@1) < 256)
4877     (with
4878      {
4879       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4880       tree utype = unsigned_type_for (TREE_TYPE (@0));
4881       tree nst = build_int_cst (integer_type_node, prec - 8);
4882      }
4883      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4886 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4888 /* Simplify constant conditions.
4889    Only optimize constant conditions when the selected branch
4890    has the same type as the COND_EXPR.  This avoids optimizing
4891    away "c ? x : throw", where the throw has a void type.
4892    Note that we cannot throw away the fold-const.cc variant nor
4893    this one as we depend on doing this transform before possibly
4894    A ? B : B -> B triggers and the fold-const.cc one can optimize
4895    0 ? A : B to B even if A has side-effects.  Something
4896    genmatch cannot handle.  */
4897 (simplify
4898  (cond INTEGER_CST@0 @1 @2)
4899  (if (integer_zerop (@0))
4900   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4901    @2)
4902   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4903    @1)))
4904 (simplify
4905  (vec_cond VECTOR_CST@0 @1 @2)
4906  (if (integer_all_onesp (@0))
4907   @1
4908   (if (integer_zerop (@0))
4909    @2)))
4911 /* Sink unary operations to branches, but only if we do fold both.  */
4912 (for op (negate bit_not abs absu)
4913  (simplify
4914   (op (vec_cond:s @0 @1 @2))
4915   (vec_cond @0 (op! @1) (op! @2))))
4917 /* Sink unary conversions to branches, but only if we do fold both
4918    and the target's truth type is the same as we already have.  */
4919 (simplify
4920  (convert (vec_cond:s @0 @1 @2))
4921  (if (VECTOR_TYPE_P (type)
4922       && types_match (TREE_TYPE (@0), truth_type_for (type)))
4923   (vec_cond @0 (convert! @1) (convert! @2))))
4925 /* Likewise for view_convert of nop_conversions. */
4926 (simplify
4927  (view_convert (vec_cond:s @0 @1 @2))
4928  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4929       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4930                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4931       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4932   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4934 /* Sink binary operation to branches, but only if we can fold it.  */
4935 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4936          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4937          trunc_mod ceil_mod floor_mod round_mod min max)
4938 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4939  (simplify
4940   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4941   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4943 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4944  (simplify
4945   (op (vec_cond:s @0 @1 @2) @3)
4946   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4947  (simplify
4948   (op @3 (vec_cond:s @0 @1 @2))
4949   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4951 #if GIMPLE
4952 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4953  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4954            INTEGER_CST@1)
4955  (with {
4956          int ibit = tree_log2 (@0);
4957          int ibit2 = tree_log2 (@1);
4958        }
4959   (if (ibit == ibit2
4960       && ibit >= 0
4961       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4963 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4964  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4965           INTEGER_CST@1)
4966  (with {
4967          int ibit = tree_log2 (@0);
4968          int ibit2 = tree_log2 (@1);
4969        }
4970   (if (ibit == ibit2
4971       && ibit >= 0
4972       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4974 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4975  (bit_and:c
4976   (convert1?@4
4977    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4978   (convert2? @0))
4979  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4981 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4982  (bit_and:c
4983   (convert1?@4
4984    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4985   (convert2? @0))
4986  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4988 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4989  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4990             INTEGER_CST@1)
4991  (with {
4992          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4993                                               TYPE_PRECISION(type)));
4994          int ibit2 = tree_log2 (@1);
4995        }
4996   (if (ibit == ibit2
4997       && ibit >= 0
4998       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5000 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5001  (bit_and@4
5002   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5003   INTEGER_CST@1)
5004  (with {
5005          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5006                                               TYPE_PRECISION(type)));
5007          int ibit2 = tree_log2 (@1);
5008        }
5009   (if (ibit == ibit2
5010       && ibit >= 0
5011       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5013 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5014  (bit_and:c
5015   (convert1?@3
5016    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5017   (convert2? @0))
5018  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5020 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5021  (bit_and:c
5022   (convert1?@3
5023    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5024   (convert2? @0))
5025   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5027 #endif
5029 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5030    Currently disabled after pass lvec because ARM understands
5031    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5032 (simplify
5033  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5034  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5035   (vec_cond (bit_and @0 @3) @1 @2)))
5036 (simplify
5037  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5038  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5039   (vec_cond (bit_ior @0 @3) @1 @2)))
5040 (simplify
5041  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5042  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5043   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5044 (simplify
5045  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5046  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5047   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5049 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5050 (simplify
5051  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5052  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5053   (vec_cond (bit_and @0 @1) @2 @3)))
5054 (simplify
5055  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5056  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5057   (vec_cond (bit_ior @0 @1) @2 @3)))
5058 (simplify
5059  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5060  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5061   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5062 (simplify
5063  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5064  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5065   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5067 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5068    types are compatible.  */
5069 (simplify
5070  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5071  (if (VECTOR_BOOLEAN_TYPE_P (type)
5072       && types_match (type, TREE_TYPE (@0)))
5073   (if (integer_zerop (@1) && integer_all_onesp (@2))
5074    (bit_not @0)
5075    (if (integer_all_onesp (@1) && integer_zerop (@2))
5076     @0))))
5078 /* A few simplifications of "a ? CST1 : CST2". */
5079 /* NOTE: Only do this on gimple as the if-chain-to-switch
5080    optimization depends on the gimple to have if statements in it. */
5081 #if GIMPLE
5082 (simplify
5083  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5084  (switch
5085   (if (integer_zerop (@2))
5086    (switch
5087     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5088     (if (integer_onep (@1))
5089      (convert (convert:boolean_type_node @0)))
5090     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5091     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5092      (with {
5093        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5094       }
5095       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
5096     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
5097        here as the powerof2cst case above will handle that case correctly.  */
5098     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5099      (negate (convert:type (convert:boolean_type_node @0))))))
5100   (if (integer_zerop (@1))
5101    (switch
5102     /* a ? 0 : 1 -> !a. */
5103     (if (integer_onep (@2))
5104      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5105     /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
5106     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5107      (with {
5108        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5109       }
5110       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5111                                 { boolean_true_node; })) { shift; })))
5112     /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
5113        here as the powerof2cst case above will handle that case correctly.  */
5114     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5115      (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5116                                     { boolean_true_node; }))))))))
5118 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5119    for unsigned types. */
5120 (simplify
5121  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5122  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5123       && bitwise_equal_p (@0, @2))
5124   (convert (eq @0 @1))
5128 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5129    for unsigned types. */
5130 (simplify
5131  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5132  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5133       && bitwise_equal_p (@0, @2))
5134   (convert (eq @0 @1))
5138 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5139    on the first bit of the CST.  */
5140 (simplify
5141  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5142  (if ((wi::to_wide (@1) & 1) != 0)
5143   @2
5144   { build_zero_cst (type); }))
5146 /* Optimize
5147    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5148    x_5 ? cstN ? cst4 : cst3
5149    # op is == or != and N is 1 or 2
5150    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5151    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5152    of cst3 and cst4 is smaller.
5153    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5154 (for eqne (ne eq)
5155  (simplify
5156   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5157   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5158        && INTEGRAL_TYPE_P (type)
5159        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5160            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5161    (with {
5162      value_range r;
5163      get_range_query (cfun)->range_of_expr (r, @0);
5164      if (r.undefined_p ())
5165        r.set_varying (TREE_TYPE (@0));
5167      wide_int min = r.lower_bound ();
5168      wide_int max = r.upper_bound ();
5169     }
5170     (if (min + 1 == max
5171          && (wi::to_wide (@1) == min
5172              || wi::to_wide (@1) == max))
5173      (with {
5174        tree arg0 = @2, arg1 = @3;
5175        tree type1;
5176        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5177          std::swap (arg0, arg1);
5178        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5179          type1 = TREE_TYPE (@0);
5180        else
5181          type1 = type;
5182        auto prec = TYPE_PRECISION (type1);
5183        auto unsign = TYPE_UNSIGNED (type1);
5184        type1 = build_nonstandard_integer_type (prec, unsign);
5185        min = wide_int::from (min, prec,
5186                              TYPE_SIGN (TREE_TYPE (@0)));
5187        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5188                                     TYPE_SIGN (type));
5189        enum tree_code code;
5190        wi::overflow_type ovf;
5191        if (tree_int_cst_lt (arg0, arg1))
5192          {
5193            code = PLUS_EXPR;
5194            a -= min;
5195            if (!unsign)
5196              {
5197                /* lhs is known to be in range [min, min+1] and we want to add a
5198                   to it.  Check if that operation can overflow for those 2 values
5199                   and if yes, force unsigned type.  */
5200                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5201                if (ovf)
5202                  type1 = unsigned_type_for (type1);
5203              }
5204          }
5205        else
5206          {
5207            code = MINUS_EXPR;
5208            a += min;
5209            if (!unsign)
5210              {
5211                /* lhs is known to be in range [min, min+1] and we want to subtract
5212                   it from a.  Check if that operation can overflow for those 2
5213                   values and if yes, force unsigned type.  */
5214                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5215                if (ovf)
5216                 type1 = unsigned_type_for (type1);
5217              }
5218          }
5219        tree arg = wide_int_to_tree (type1, a);
5220       }
5221       (if (code == PLUS_EXPR)
5222        (convert (plus (convert:type1 @0) { arg; }))
5223        (convert (minus { arg; } (convert:type1 @0)))
5224       )
5225      )
5226     )
5227    )
5228   )
5231 #endif
5233 (simplify
5234  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5235  (if (INTEGRAL_TYPE_P (type)
5236       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5237   (cond @1 (convert @2) (convert @3))))
5239 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5240    be extended.  */
5241 /* This pattern implements two kinds simplification:
5243    Case 1)
5244    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5245      1) Conversions are type widening from smaller type.
5246      2) Const c1 equals to c2 after canonicalizing comparison.
5247      3) Comparison has tree code LT, LE, GT or GE.
5248    This specific pattern is needed when (cmp (convert x) c) may not
5249    be simplified by comparison patterns because of multiple uses of
5250    x.  It also makes sense here because simplifying across multiple
5251    referred var is always benefitial for complicated cases.
5253    Case 2)
5254    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5255 (for cmp (lt le gt ge eq ne)
5256  (simplify
5257   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5258   (with
5259    {
5260      tree from_type = TREE_TYPE (@1);
5261      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5262      enum tree_code code = ERROR_MARK;
5264      if (INTEGRAL_TYPE_P (from_type)
5265          && int_fits_type_p (@2, from_type)
5266          && (types_match (c1_type, from_type)
5267              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5268                  && (TYPE_UNSIGNED (from_type)
5269                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5270          && (types_match (c2_type, from_type)
5271              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5272                  && (TYPE_UNSIGNED (from_type)
5273                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5274        {
5275          if (cmp != EQ_EXPR)
5276            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5277          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5278          else if (int_fits_type_p (@3, from_type))
5279            code = EQ_EXPR;
5280        }
5281    }
5282    (if (code == MAX_EXPR)
5283     (convert (max @1 (convert @2)))
5284     (if (code == MIN_EXPR)
5285      (convert (min @1 (convert @2)))
5286      (if (code == EQ_EXPR)
5287       (convert (cond (eq @1 (convert @3))
5288                      (convert:from_type @3) (convert:from_type @2)))))))))
5290 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5292      1) OP is PLUS or MINUS.
5293      2) CMP is LT, LE, GT or GE.
5294      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5296    This pattern also handles special cases like:
5298      A) Operand x is a unsigned to signed type conversion and c1 is
5299         integer zero.  In this case,
5300           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5301           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5302      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5303         check equality for (c1+1) and (c1-1) by adjusting comparison
5304         code.
5306    TODO: Though signed type is handled by this pattern, it cannot be
5307    simplified at the moment because C standard requires additional
5308    type promotion.  In order to match&simplify it here, the IR needs
5309    to be cleaned up by other optimizers, i.e, VRP.  */
5310 (for op (plus minus)
5311  (for cmp (lt le gt ge)
5312   (simplify
5313    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5314    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5315     (if (types_match (from_type, to_type)
5316          /* Check if it is special case A).  */
5317          || (TYPE_UNSIGNED (from_type)
5318              && !TYPE_UNSIGNED (to_type)
5319              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5320              && integer_zerop (@1)
5321              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5322      (with
5323       {
5324         wi::overflow_type overflow = wi::OVF_NONE;
5325         enum tree_code code, cmp_code = cmp;
5326         wide_int real_c1;
5327         wide_int c1 = wi::to_wide (@1);
5328         wide_int c2 = wi::to_wide (@2);
5329         wide_int c3 = wi::to_wide (@3);
5330         signop sgn = TYPE_SIGN (from_type);
5332         /* Handle special case A), given x of unsigned type:
5333             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5334             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5335         if (!types_match (from_type, to_type))
5336           {
5337             if (cmp_code == LT_EXPR)
5338               cmp_code = GT_EXPR;
5339             if (cmp_code == GE_EXPR)
5340               cmp_code = LE_EXPR;
5341             c1 = wi::max_value (to_type);
5342           }
5343         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5344            compute (c3 op' c2) and check if it equals to c1 with op' being
5345            the inverted operator of op.  Make sure overflow doesn't happen
5346            if it is undefined.  */
5347         if (op == PLUS_EXPR)
5348           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5349         else
5350           real_c1 = wi::add (c3, c2, sgn, &overflow);
5352         code = cmp_code;
5353         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5354           {
5355             /* Check if c1 equals to real_c1.  Boundary condition is handled
5356                by adjusting comparison operation if necessary.  */
5357             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5358                 && !overflow)
5359               {
5360                 /* X <= Y - 1 equals to X < Y.  */
5361                 if (cmp_code == LE_EXPR)
5362                   code = LT_EXPR;
5363                 /* X > Y - 1 equals to X >= Y.  */
5364                 if (cmp_code == GT_EXPR)
5365                   code = GE_EXPR;
5366               }
5367             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5368                 && !overflow)
5369               {
5370                 /* X < Y + 1 equals to X <= Y.  */
5371                 if (cmp_code == LT_EXPR)
5372                   code = LE_EXPR;
5373                 /* X >= Y + 1 equals to X > Y.  */
5374                 if (cmp_code == GE_EXPR)
5375                   code = GT_EXPR;
5376               }
5377             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5378               {
5379                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5380                   code = MIN_EXPR;
5381                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5382                   code = MAX_EXPR;
5383               }
5384           }
5385       }
5386       (if (code == MAX_EXPR)
5387        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5388            { wide_int_to_tree (from_type, c2); })
5389        (if (code == MIN_EXPR)
5390         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5391             { wide_int_to_tree (from_type, c2); })))))))))
5393 #if GIMPLE
5394 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5395    in fold_cond_expr_with_comparison for GENERIC folding with
5396    some extra constraints.  */
5397 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5398  (simplify
5399   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5400         (convert3? @0) (convert4? @1))
5401   (if (!HONOR_SIGNED_ZEROS (type)
5402        && (/* Allow widening conversions of the compare operands as data.  */
5403            (INTEGRAL_TYPE_P (type)
5404             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5405             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5406             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5407             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5408            /* Or sign conversions for the comparison.  */
5409            || (types_match (type, TREE_TYPE (@0))
5410                && types_match (type, TREE_TYPE (@1)))))
5411    (switch
5412     (if (cmp == EQ_EXPR)
5413      (if (VECTOR_TYPE_P (type))
5414       (view_convert @c1)
5415       (convert @c1)))
5416     (if (cmp == NE_EXPR)
5417      (if (VECTOR_TYPE_P (type))
5418       (view_convert @c0)
5419       (convert @c0)))
5420     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5421      (if (!HONOR_NANS (type))
5422       (if (VECTOR_TYPE_P (type))
5423        (view_convert (min @c0 @c1))
5424        (convert (min @c0 @c1)))))
5425     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5426      (if (!HONOR_NANS (type))
5427       (if (VECTOR_TYPE_P (type))
5428        (view_convert (max @c0 @c1))
5429        (convert (max @c0 @c1)))))
5430     (if (cmp == UNEQ_EXPR)
5431      (if (!HONOR_NANS (type))
5432       (if (VECTOR_TYPE_P (type))
5433        (view_convert @c1)
5434        (convert @c1))))
5435     (if (cmp == LTGT_EXPR)
5436      (if (!HONOR_NANS (type))
5437       (if (VECTOR_TYPE_P (type))
5438        (view_convert @c0)
5439        (convert @c0))))))))
5440 #endif
5442 (for cnd (cond vec_cond)
5443  /* (a != b) ? (a - b) : 0 -> (a - b) */
5444  (simplify
5445   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5446   @2)
5447  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5448  (simplify
5449   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5450   @2)
5451  /* (a != b) ? (a & b) : a -> (a & b) */
5452  /* (a != b) ? (a | b) : a -> (a | b) */
5453  /* (a != b) ? min(a,b) : a -> min(a,b) */
5454  /* (a != b) ? max(a,b) : a -> max(a,b) */
5455  (for op (bit_and bit_ior min max)
5456   (simplify
5457    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5458    @2))
5459  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5460  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5461  (for op (mult plus)
5462   (simplify
5463    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5464    (if (ANY_INTEGRAL_TYPE_P (type))
5465     @2)))
5466  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5467  (simplify
5468   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5469   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5470    @2))
5473 /* These was part of minmax phiopt.  */
5474 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5475    to minmax<min/max<a, b>, c> */
5476 (for minmax (min max)
5477  (for cmp (lt le gt ge ne)
5478   (simplify
5479    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5480    (with
5481     {
5482       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5483     }
5484     (if (code == MIN_EXPR)
5485      (minmax (min @1 @2) @4)
5486      (if (code == MAX_EXPR)
5487       (minmax (max @1 @2) @4)))))))
5489 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5490 (for cmp    (gt  ge  lt  le)
5491      minmax (min min max max)
5492  (simplify
5493   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5494    (with
5495     {
5496       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5497     }
5498     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5499          && code == MIN_EXPR
5500          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5501      (min @2 @4)
5502      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5503           && code == MAX_EXPR
5504           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5505       (max @2 @4))))))
5507 #if GIMPLE
5508 /* These patterns should be after min/max detection as simplifications
5509    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5510    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5511    Even without those, reaching min/max/and/ior faster is better.  */
5512 (simplify
5513  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5514  (switch
5515   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5516   (if (integer_zerop (@2))
5517    (bit_and (convert @0) @1))
5518   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5519   (if (integer_zerop (@1))
5520    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5521   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5522   (if (integer_onep (@1))
5523    (bit_ior (convert @0) @2))
5524   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5525   (if (integer_onep (@2))
5526    (bit_ior (bit_xor (convert @0) @2) @1))
5529 #endif
5531 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5532 (simplify
5533  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5534  (if (!TYPE_SATURATING (type)
5535       && (TYPE_OVERFLOW_WRAPS (type)
5536           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5537       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5538   @3))
5540 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5541 (simplify
5542  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5543  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5544   @3))
5546 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5547    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5548 (simplify
5549  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5550  (if (TYPE_UNSIGNED (type))
5551   (cond (ge @0 @1) (negate @0) @2)))
5553 (for cnd (cond vec_cond)
5554  /* A ? B : (A ? X : C) -> A ? B : C.  */
5555  (simplify
5556   (cnd @0 (cnd @0 @1 @2) @3)
5557   (cnd @0 @1 @3))
5558  (simplify
5559   (cnd @0 @1 (cnd @0 @2 @3))
5560   (cnd @0 @1 @3))
5561  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5562  /* ???  This matches embedded conditions open-coded because genmatch
5563     would generate matching code for conditions in separate stmts only.
5564     The following is still important to merge then and else arm cases
5565     from if-conversion.  */
5566  (simplify
5567   (cnd @0 @1 (cnd @2 @3 @4))
5568   (if (inverse_conditions_p (@0, @2))
5569    (cnd @0 @1 @3)))
5570  (simplify
5571   (cnd @0 (cnd @1 @2 @3) @4)
5572   (if (inverse_conditions_p (@0, @1))
5573    (cnd @0 @3 @4)))
5575  /* A ? B : B -> B.  */
5576  (simplify
5577   (cnd @0 @1 @1)
5578   @1)
5580  /* !A ? B : C -> A ? C : B.  */
5581  (simplify
5582   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5583   (cnd @0 @2 @1)))
5585 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5586    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5587    Need to handle UN* comparisons.
5589    None of these transformations work for modes with signed
5590    zeros.  If A is +/-0, the first two transformations will
5591    change the sign of the result (from +0 to -0, or vice
5592    versa).  The last four will fix the sign of the result,
5593    even though the original expressions could be positive or
5594    negative, depending on the sign of A.
5596    Note that all these transformations are correct if A is
5597    NaN, since the two alternatives (A and -A) are also NaNs.  */
5599 (for cnd (cond vec_cond)
5600  /* A == 0 ? A : -A    same as -A */
5601  (for cmp (eq uneq)
5602   (simplify
5603    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5604     (if (!HONOR_SIGNED_ZEROS (type))
5605      @1))
5606   (simplify
5607    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5608     (if (!HONOR_SIGNED_ZEROS (type))
5609      @1))
5611  /* A != 0 ? A : -A    same as A */
5612  (for cmp (ne ltgt)
5613   (simplify
5614    (cnd (cmp @0 zerop) @0 (negate @0))
5615     (if (!HONOR_SIGNED_ZEROS (type))
5616      @0))
5617   (simplify
5618    (cnd (cmp @0 zerop) @0 integer_zerop)
5619     (if (!HONOR_SIGNED_ZEROS (type))
5620      @0))
5622  /* A >=/> 0 ? A : -A    same as abs (A) */
5623  (for cmp (ge gt)
5624   (simplify
5625    (cnd (cmp @0 zerop) @0 (negate @0))
5626     (if (!HONOR_SIGNED_ZEROS (type)
5627          && !TYPE_UNSIGNED (type))
5628      (abs @0))))
5629  /* A <=/< 0 ? A : -A    same as -abs (A) */
5630  (for cmp (le lt)
5631   (simplify
5632    (cnd (cmp @0 zerop) @0 (negate @0))
5633     (if (!HONOR_SIGNED_ZEROS (type)
5634          && !TYPE_UNSIGNED (type))
5635      (if (ANY_INTEGRAL_TYPE_P (type)
5636           && !TYPE_OVERFLOW_WRAPS (type))
5637       (with {
5638         tree utype = unsigned_type_for (type);
5639        }
5640        (convert (negate (absu:utype @0))))
5641        (negate (abs @0)))))
5645 /* -(type)!A -> (type)A - 1.  */
5646 (simplify
5647  (negate (convert?:s (logical_inverted_value:s @0)))
5648  (if (INTEGRAL_TYPE_P (type)
5649       && TREE_CODE (type) != BOOLEAN_TYPE
5650       && TYPE_PRECISION (type) > 1
5651       && TREE_CODE (@0) == SSA_NAME
5652       && ssa_name_has_boolean_range (@0))
5653   (plus (convert:type @0) { build_all_ones_cst (type); })))
5655 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5656    return all -1 or all 0 results.  */
5657 /* ??? We could instead convert all instances of the vec_cond to negate,
5658    but that isn't necessarily a win on its own.  */
5659 (simplify
5660  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5661  (if (VECTOR_TYPE_P (type)
5662       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5663                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5664       && (TYPE_MODE (TREE_TYPE (type))
5665           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5666   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5668 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5669 (simplify
5670  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5671  (if (VECTOR_TYPE_P (type)
5672       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5673                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5674       && (TYPE_MODE (TREE_TYPE (type))
5675           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5676   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5679 /* Simplifications of comparisons.  */
5681 /* See if we can reduce the magnitude of a constant involved in a
5682    comparison by changing the comparison code.  This is a canonicalization
5683    formerly done by maybe_canonicalize_comparison_1.  */
5684 (for cmp  (le gt)
5685      acmp (lt ge)
5686  (simplify
5687   (cmp @0 uniform_integer_cst_p@1)
5688   (with { tree cst = uniform_integer_cst_p (@1); }
5689    (if (tree_int_cst_sgn (cst) == -1)
5690      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5691                                    wide_int_to_tree (TREE_TYPE (cst),
5692                                                      wi::to_wide (cst)
5693                                                      + 1)); })))))
5694 (for cmp  (ge lt)
5695      acmp (gt le)
5696  (simplify
5697   (cmp @0 uniform_integer_cst_p@1)
5698   (with { tree cst = uniform_integer_cst_p (@1); }
5699    (if (tree_int_cst_sgn (cst) == 1)
5700     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5701                                   wide_int_to_tree (TREE_TYPE (cst),
5702                                   wi::to_wide (cst) - 1)); })))))
5704 /* We can simplify a logical negation of a comparison to the
5705    inverted comparison.  As we cannot compute an expression
5706    operator using invert_tree_comparison we have to simulate
5707    that with expression code iteration.  */
5708 (for cmp (tcc_comparison)
5709      icmp (inverted_tcc_comparison)
5710      ncmp (inverted_tcc_comparison_with_nans)
5711  /* Ideally we'd like to combine the following two patterns
5712     and handle some more cases by using
5713       (logical_inverted_value (cmp @0 @1))
5714     here but for that genmatch would need to "inline" that.
5715     For now implement what forward_propagate_comparison did.  */
5716  (simplify
5717   (bit_not (cmp @0 @1))
5718   (if (VECTOR_TYPE_P (type)
5719        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5720    /* Comparison inversion may be impossible for trapping math,
5721       invert_tree_comparison will tell us.  But we can't use
5722       a computed operator in the replacement tree thus we have
5723       to play the trick below.  */
5724    (with { enum tree_code ic = invert_tree_comparison
5725              (cmp, HONOR_NANS (@0)); }
5726     (if (ic == icmp)
5727      (icmp @0 @1)
5728      (if (ic == ncmp)
5729       (ncmp @0 @1))))))
5730  (simplify
5731   (bit_xor (cmp @0 @1) integer_truep)
5732   (with { enum tree_code ic = invert_tree_comparison
5733             (cmp, HONOR_NANS (@0)); }
5734    (if (ic == icmp)
5735     (icmp @0 @1)
5736     (if (ic == ncmp)
5737      (ncmp @0 @1)))))
5738  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5739  (simplify
5740   (ne (cmp@2 @0 @1) integer_zerop)
5741   (if (types_match (type, TREE_TYPE (@2)))
5742    (cmp @0 @1)))
5743  (simplify
5744   (eq (cmp@2 @0 @1) integer_truep)
5745   (if (types_match (type, TREE_TYPE (@2)))
5746    (cmp @0 @1)))
5747  (simplify
5748   (ne (cmp@2 @0 @1) integer_truep)
5749   (if (types_match (type, TREE_TYPE (@2)))
5750    (with { enum tree_code ic = invert_tree_comparison
5751              (cmp, HONOR_NANS (@0)); }
5752     (if (ic == icmp)
5753      (icmp @0 @1)
5754      (if (ic == ncmp)
5755       (ncmp @0 @1))))))
5756  (simplify
5757   (eq (cmp@2 @0 @1) integer_zerop)
5758   (if (types_match (type, TREE_TYPE (@2)))
5759    (with { enum tree_code ic = invert_tree_comparison
5760              (cmp, HONOR_NANS (@0)); }
5761     (if (ic == icmp)
5762      (icmp @0 @1)
5763      (if (ic == ncmp)
5764       (ncmp @0 @1)))))))
5766 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5767    ??? The transformation is valid for the other operators if overflow
5768    is undefined for the type, but performing it here badly interacts
5769    with the transformation in fold_cond_expr_with_comparison which
5770    attempts to synthetize ABS_EXPR.  */
5771 (for cmp (eq ne)
5772  (for sub (minus pointer_diff)
5773   (simplify
5774    (cmp (sub@2 @0 @1) integer_zerop)
5775    (if (single_use (@2))
5776     (cmp @0 @1)))))
5778 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5779    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5780 (for cmp (lt ge)
5781  (simplify
5782   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5783    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5784         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5785         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5786     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5787 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5788    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5789 (simplify
5790  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5791   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5792        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5793        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5794    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5796 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5797    signed arithmetic case.  That form is created by the compiler
5798    often enough for folding it to be of value.  One example is in
5799    computing loop trip counts after Operator Strength Reduction.  */
5800 (for cmp (simple_comparison)
5801      scmp (swapped_simple_comparison)
5802  (simplify
5803   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5804   /* Handle unfolded multiplication by zero.  */
5805   (if (integer_zerop (@1))
5806    (cmp @1 @2)
5807    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5808         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5809         && single_use (@3))
5810     /* If @1 is negative we swap the sense of the comparison.  */
5811     (if (tree_int_cst_sgn (@1) < 0)
5812      (scmp @0 @2)
5813      (cmp @0 @2))))))
5815 /* For integral types with undefined overflow fold
5816    x * C1 == C2 into x == C2 / C1 or false.
5817    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5818    Z / 2^n Z.  */
5819 (for cmp (eq ne)
5820  (simplify
5821   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5822   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5823        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5824        && wi::to_wide (@1) != 0)
5825    (with { widest_int quot; }
5826     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5827                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5828      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5829      { constant_boolean_node (cmp == NE_EXPR, type); }))
5830    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5831         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5832         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5833     (cmp @0
5834      {
5835        tree itype = TREE_TYPE (@0);
5836        int p = TYPE_PRECISION (itype);
5837        wide_int m = wi::one (p + 1) << p;
5838        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5839        wide_int i = wide_int::from (wi::mod_inv (a, m),
5840                                     p, TYPE_SIGN (itype));
5841        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5842      })))))
5844 /* Simplify comparison of something with itself.  For IEEE
5845    floating-point, we can only do some of these simplifications.  */
5846 (for cmp (eq ge le)
5847  (simplify
5848   (cmp @0 @0)
5849   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5850        || ! tree_expr_maybe_nan_p (@0))
5851    { constant_boolean_node (true, type); }
5852    (if (cmp != EQ_EXPR
5853         /* With -ftrapping-math conversion to EQ loses an exception.  */
5854         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5855             || ! flag_trapping_math))
5856     (eq @0 @0)))))
5857 (for cmp (ne gt lt)
5858  (simplify
5859   (cmp @0 @0)
5860   (if (cmp != NE_EXPR
5861        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5862        || ! tree_expr_maybe_nan_p (@0))
5863    { constant_boolean_node (false, type); })))
5864 (for cmp (unle unge uneq)
5865  (simplify
5866   (cmp @0 @0)
5867   { constant_boolean_node (true, type); }))
5868 (for cmp (unlt ungt)
5869  (simplify
5870   (cmp @0 @0)
5871   (unordered @0 @0)))
5872 (simplify
5873  (ltgt @0 @0)
5874  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5875   { constant_boolean_node (false, type); }))
5877 /* x == ~x -> false */
5878 /* x != ~x -> true */
5879 (for cmp (eq ne)
5880  (simplify
5881   (cmp:c @0 (bit_not @0))
5882   { constant_boolean_node (cmp == NE_EXPR, type); }))
5884 /* Fold ~X op ~Y as Y op X.  */
5885 (for cmp (simple_comparison)
5886  (simplify
5887   (cmp (bit_not@2 @0) (bit_not@3 @1))
5888   (if (single_use (@2) && single_use (@3))
5889    (cmp @1 @0))))
5891 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5892 (for cmp (simple_comparison)
5893      scmp (swapped_simple_comparison)
5894  (simplify
5895   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5896   (if (single_use (@2)
5897        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5898    (scmp @0 (bit_not @1)))))
5900 (for cmp (simple_comparison)
5901  (simplify
5902   (cmp @0 REAL_CST@1)
5903   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5904   (switch
5905    /* a CMP (-0) -> a CMP 0  */
5906    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5907     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5908    /* (-0) CMP b -> 0 CMP b.  */
5909    (if (TREE_CODE (@0) == REAL_CST
5910         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5911     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5912    /* x != NaN is always true, other ops are always false.  */
5913    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5914         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5915         && !tree_expr_signaling_nan_p (@1)
5916         && !tree_expr_maybe_signaling_nan_p (@0))
5917     { constant_boolean_node (cmp == NE_EXPR, type); })
5918    /* NaN != y is always true, other ops are always false.  */
5919    (if (TREE_CODE (@0) == REAL_CST
5920         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5921         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5922         && !tree_expr_signaling_nan_p (@0)
5923         && !tree_expr_signaling_nan_p (@1))
5924     { constant_boolean_node (cmp == NE_EXPR, type); })
5925    /* Fold comparisons against infinity.  */
5926    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5927         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5928     (with
5929      {
5930        REAL_VALUE_TYPE max;
5931        enum tree_code code = cmp;
5932        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5933        if (neg)
5934          code = swap_tree_comparison (code);
5935      }
5936      (switch
5937       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5938       (if (code == GT_EXPR
5939            && !(HONOR_NANS (@0) && flag_trapping_math))
5940        { constant_boolean_node (false, type); })
5941       (if (code == LE_EXPR)
5942        /* x <= +Inf is always true, if we don't care about NaNs.  */
5943        (if (! HONOR_NANS (@0))
5944         { constant_boolean_node (true, type); }
5945         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5946            an "invalid" exception.  */
5947         (if (!flag_trapping_math)
5948          (eq @0 @0))))
5949       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5950          for == this introduces an exception for x a NaN.  */
5951       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5952            || code == GE_EXPR)
5953        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5954         (if (neg)
5955          (lt @0 { build_real (TREE_TYPE (@0), max); })
5956          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5957       /* x < +Inf is always equal to x <= DBL_MAX.  */
5958       (if (code == LT_EXPR)
5959        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5960         (if (neg)
5961          (ge @0 { build_real (TREE_TYPE (@0), max); })
5962          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5963       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5964          an exception for x a NaN so use an unordered comparison.  */
5965       (if (code == NE_EXPR)
5966        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5967         (if (! HONOR_NANS (@0))
5968          (if (neg)
5969           (ge @0 { build_real (TREE_TYPE (@0), max); })
5970           (le @0 { build_real (TREE_TYPE (@0), max); }))
5971          (if (neg)
5972           (unge @0 { build_real (TREE_TYPE (@0), max); })
5973           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5975  /* If this is a comparison of a real constant with a PLUS_EXPR
5976     or a MINUS_EXPR of a real constant, we can convert it into a
5977     comparison with a revised real constant as long as no overflow
5978     occurs when unsafe_math_optimizations are enabled.  */
5979  (if (flag_unsafe_math_optimizations)
5980   (for op (plus minus)
5981    (simplify
5982     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5983     (with
5984      {
5985        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5986                                TREE_TYPE (@1), @2, @1);
5987      }
5988      (if (tem && !TREE_OVERFLOW (tem))
5989       (cmp @0 { tem; }))))))
5991  /* Likewise, we can simplify a comparison of a real constant with
5992     a MINUS_EXPR whose first operand is also a real constant, i.e.
5993     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
5994     floating-point types only if -fassociative-math is set.  */
5995  (if (flag_associative_math)
5996   (simplify
5997    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5998    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5999     (if (tem && !TREE_OVERFLOW (tem))
6000      (cmp { tem; } @1)))))
6002  /* Fold comparisons against built-in math functions.  */
6003  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6004   (for sq (SQRT)
6005    (simplify
6006     (cmp (sq @0) REAL_CST@1)
6007     (switch
6008      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6009       (switch
6010        /* sqrt(x) < y is always false, if y is negative.  */
6011        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6012         { constant_boolean_node (false, type); })
6013        /* sqrt(x) > y is always true, if y is negative and we
6014           don't care about NaNs, i.e. negative values of x.  */
6015        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6016         { constant_boolean_node (true, type); })
6017        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6018        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6019      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6020       (switch
6021        /* sqrt(x) < 0 is always false.  */
6022        (if (cmp == LT_EXPR)
6023         { constant_boolean_node (false, type); })
6024        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6025        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6026         { constant_boolean_node (true, type); })
6027        /* sqrt(x) <= 0 -> x == 0.  */
6028        (if (cmp == LE_EXPR)
6029         (eq @0 @1))
6030        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6031           == or !=.  In the last case:
6033             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6035           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6036           the results for other x follow from natural arithmetic.  */
6037        (cmp @0 @1)))
6038      (if ((cmp == LT_EXPR
6039            || cmp == LE_EXPR
6040            || cmp == GT_EXPR
6041            || cmp == GE_EXPR)
6042           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6043           /* Give up for -frounding-math.  */
6044           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6045       (with
6046        {
6047          REAL_VALUE_TYPE c2;
6048          enum tree_code ncmp = cmp;
6049          const real_format *fmt
6050            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6051          real_arithmetic (&c2, MULT_EXPR,
6052                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6053          real_convert (&c2, fmt, &c2);
6054          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6055             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6056          if (!REAL_VALUE_ISINF (c2))
6057            {
6058              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6059                                         build_real (TREE_TYPE (@0), c2));
6060              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6061                ncmp = ERROR_MARK;
6062              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6063                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6064                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6065              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6066                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6067                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6068              else
6069                {
6070                  /* With rounding to even, sqrt of up to 3 different values
6071                     gives the same normal result, so in some cases c2 needs
6072                     to be adjusted.  */
6073                  REAL_VALUE_TYPE c2alt, tow;
6074                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6075                    tow = dconst0;
6076                  else
6077                    tow = dconstinf;
6078                  real_nextafter (&c2alt, fmt, &c2, &tow);
6079                  real_convert (&c2alt, fmt, &c2alt);
6080                  if (REAL_VALUE_ISINF (c2alt))
6081                    ncmp = ERROR_MARK;
6082                  else
6083                    {
6084                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6085                                            build_real (TREE_TYPE (@0), c2alt));
6086                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6087                        ncmp = ERROR_MARK;
6088                      else if (real_equal (&TREE_REAL_CST (c3),
6089                                           &TREE_REAL_CST (@1)))
6090                        c2 = c2alt;
6091                    }
6092                }
6093            }
6094        }
6095        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6096         (if (REAL_VALUE_ISINF (c2))
6097          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6098          (if (HONOR_INFINITIES (@0))
6099           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6100           { constant_boolean_node (false, type); })
6101          /* sqrt(x) > c is the same as x > c*c.  */
6102          (if (ncmp != ERROR_MARK)
6103           (if (ncmp == GE_EXPR)
6104            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6105            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6106         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6107         (if (REAL_VALUE_ISINF (c2))
6108          (switch
6109           /* sqrt(x) < y is always true, when y is a very large
6110              value and we don't care about NaNs or Infinities.  */
6111           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6112            { constant_boolean_node (true, type); })
6113           /* sqrt(x) < y is x != +Inf when y is very large and we
6114              don't care about NaNs.  */
6115           (if (! HONOR_NANS (@0))
6116            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6117           /* sqrt(x) < y is x >= 0 when y is very large and we
6118              don't care about Infinities.  */
6119           (if (! HONOR_INFINITIES (@0))
6120            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6121           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6122           (if (GENERIC)
6123            (truth_andif
6124             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6125             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6126          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6127          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6128           (if (ncmp == LT_EXPR)
6129            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6130            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6131           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6132           (if (ncmp != ERROR_MARK && GENERIC)
6133            (if (ncmp == LT_EXPR)
6134             (truth_andif
6135              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6136              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6137             (truth_andif
6138              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6139              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6140    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6141    (simplify
6142     (cmp (sq @0) (sq @1))
6143       (if (! HONOR_NANS (@0))
6144         (cmp @0 @1))))))
6146 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6147 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6148      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6149  (simplify
6150   (cmp (float@0 @1) (float @2))
6151    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6152         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6153     (with
6154      {
6155        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6156        tree type1 = TREE_TYPE (@1);
6157        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6158        tree type2 = TREE_TYPE (@2);
6159        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6160      }
6161      (if (fmt.can_represent_integral_type_p (type1)
6162           && fmt.can_represent_integral_type_p (type2))
6163       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6164        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6165        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6166             && type1_signed_p >= type2_signed_p)
6167         (icmp @1 (convert @2))
6168         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6169              && type1_signed_p <= type2_signed_p)
6170          (icmp (convert:type2 @1) @2)
6171          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6172               && type1_signed_p == type2_signed_p)
6173           (icmp @1 @2))))))))))
6175 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6176 (for cmp  (lt le eq ne ge gt)
6177      icmp (le le eq ne ge ge)
6178  (simplify
6179   (cmp (float @0) REAL_CST@1)
6180    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6181         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6182     (with
6183      {
6184        tree itype = TREE_TYPE (@0);
6185        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6186        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6187        /* Be careful to preserve any potential exceptions due to
6188           NaNs.  qNaNs are ok in == or != context.
6189           TODO: relax under -fno-trapping-math or
6190           -fno-signaling-nans.  */
6191        bool exception_p
6192          = real_isnan (cst) && (cst->signalling
6193                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6194      }
6195      /* TODO: allow non-fitting itype and SNaNs when
6196         -fno-trapping-math.  */
6197      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6198       (with
6199        {
6200          signop isign = TYPE_SIGN (itype);
6201          REAL_VALUE_TYPE imin, imax;
6202          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6203          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6205          REAL_VALUE_TYPE icst;
6206          if (cmp == GT_EXPR || cmp == GE_EXPR)
6207            real_ceil (&icst, fmt, cst);
6208          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6209            real_floor (&icst, fmt, cst);
6210          else
6211            real_trunc (&icst, fmt, cst);
6213          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6215          bool overflow_p = false;
6216          wide_int icst_val
6217            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6218        }
6219        (switch
6220         /* Optimize cases when CST is outside of ITYPE's range.  */
6221         (if (real_compare (LT_EXPR, cst, &imin))
6222          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6223                                   type); })
6224         (if (real_compare (GT_EXPR, cst, &imax))
6225          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6226                                   type); })
6227         /* Remove cast if CST is an integer representable by ITYPE.  */
6228         (if (cst_int_p)
6229          (cmp @0 { gcc_assert (!overflow_p);
6230                    wide_int_to_tree (itype, icst_val); })
6231         )
6232         /* When CST is fractional, optimize
6233             (FTYPE) N == CST -> 0
6234             (FTYPE) N != CST -> 1.  */
6235         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6236          { constant_boolean_node (cmp == NE_EXPR, type); })
6237         /* Otherwise replace with sensible integer constant.  */
6238         (with
6239          {
6240            gcc_checking_assert (!overflow_p);
6241          }
6242          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6244 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6245 (for cmp (eq ne)
6246  (simplify
6247   (cmp (exact_div @0 @1) INTEGER_CST@2)
6248   (if (!integer_zerop (@1))
6249    (if (wi::to_wide (@2) == 0)
6250     (cmp @0 @2)
6251     (if (TREE_CODE (@1) == INTEGER_CST)
6252      (with
6253       {
6254         wi::overflow_type ovf;
6255         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6256                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6257       }
6258       (if (ovf)
6259        { constant_boolean_node (cmp == NE_EXPR, type); }
6260        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6261 (for cmp (lt le gt ge)
6262  (simplify
6263   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6264   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6265    (with
6266     {
6267       wi::overflow_type ovf;
6268       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6269                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6270     }
6271     (if (ovf)
6272      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6273                                         TYPE_SIGN (TREE_TYPE (@2)))
6274                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6275      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6277 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6279    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6280    For large C (more than min/B+2^size), this is also true, with the
6281    multiplication computed modulo 2^size.
6282    For intermediate C, this just tests the sign of A.  */
6283 (for cmp  (lt le gt ge)
6284      cmp2 (ge ge lt lt)
6285  (simplify
6286   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6287   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6288        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6289        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6290    (with
6291     {
6292       tree utype = TREE_TYPE (@2);
6293       wide_int denom = wi::to_wide (@1);
6294       wide_int right = wi::to_wide (@2);
6295       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6296       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6297       bool small = wi::leu_p (right, smax);
6298       bool large = wi::geu_p (right, smin);
6299     }
6300     (if (small || large)
6301      (cmp (convert:utype @0) (mult @2 (convert @1)))
6302      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6304 /* Unordered tests if either argument is a NaN.  */
6305 (simplify
6306  (bit_ior (unordered @0 @0) (unordered @1 @1))
6307  (if (types_match (@0, @1))
6308   (unordered @0 @1)))
6309 (simplify
6310  (bit_and (ordered @0 @0) (ordered @1 @1))
6311  (if (types_match (@0, @1))
6312   (ordered @0 @1)))
6313 (simplify
6314  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6315  @2)
6316 (simplify
6317  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6318  @2)
6320 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6321    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6323    Note that comparisons
6324      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6325      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6326    will be canonicalized to above so there's no need to
6327    consider them here.
6328  */
6330 (for cmp (le gt)
6331      eqcmp (eq ne)
6332  (simplify
6333   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6334   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6335    (with
6336     {
6337      tree ty = TREE_TYPE (@0);
6338      unsigned prec = TYPE_PRECISION (ty);
6339      wide_int mask = wi::to_wide (@2, prec);
6340      wide_int rhs = wi::to_wide (@3, prec);
6341      signop sgn = TYPE_SIGN (ty);
6342     }
6343     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6344          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6345       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6346              { build_zero_cst (ty); }))))))
6348 /* -A CMP -B -> B CMP A.  */
6349 (for cmp (tcc_comparison)
6350      scmp (swapped_tcc_comparison)
6351  (simplify
6352   (cmp (negate @0) (negate @1))
6353   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6354        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6355            && (cmp == EQ_EXPR
6356                || cmp == NE_EXPR
6357                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6358    (scmp @0 @1)))
6359  (simplify
6360   (cmp (negate @0) CONSTANT_CLASS_P@1)
6361   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6362        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6363            && (cmp == EQ_EXPR
6364                || cmp == NE_EXPR
6365                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6366    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6367     (if (tem && !TREE_OVERFLOW (tem))
6368      (scmp @0 { tem; }))))))
6370 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6371 (for op (abs absu)
6372  (for eqne (eq ne)
6373   (simplify
6374    (eqne (op @0) zerop@1)
6375    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6377 /* From fold_sign_changed_comparison and fold_widened_comparison.
6378    FIXME: the lack of symmetry is disturbing.  */
6379 (for cmp (simple_comparison)
6380  (simplify
6381   (cmp (convert@0 @00) (convert?@1 @10))
6382   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6383        /* Disable this optimization if we're casting a function pointer
6384           type on targets that require function pointer canonicalization.  */
6385        && !(targetm.have_canonicalize_funcptr_for_compare ()
6386             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6387                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6388                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6389                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6390        && single_use (@0))
6391    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6392         && (TREE_CODE (@10) == INTEGER_CST
6393             || @1 != @10)
6394         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6395             || cmp == NE_EXPR
6396             || cmp == EQ_EXPR)
6397         && !POINTER_TYPE_P (TREE_TYPE (@00))
6398         /* (int)bool:32 != (int)uint is not the same as
6399            bool:32 != (bool:32)uint since boolean types only have two valid
6400            values independent of their precision.  */
6401         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6402             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6403     /* ???  The special-casing of INTEGER_CST conversion was in the original
6404        code and here to avoid a spurious overflow flag on the resulting
6405        constant which fold_convert produces.  */
6406     (if (TREE_CODE (@1) == INTEGER_CST)
6407      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6408                                 TREE_OVERFLOW (@1)); })
6409      (cmp @00 (convert @1)))
6411     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6412      /* If possible, express the comparison in the shorter mode.  */
6413      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6414            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6415            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6416                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6417           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6418               || ((TYPE_PRECISION (TREE_TYPE (@00))
6419                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6420                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6421                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6422               || (TREE_CODE (@10) == INTEGER_CST
6423                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6424                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6425       (cmp @00 (convert @10))
6426       (if (TREE_CODE (@10) == INTEGER_CST
6427            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6428            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6429        (with
6430         {
6431           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6432           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6433           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6434           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6435         }
6436         (if (above || below)
6437          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6438           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6439           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6440            { constant_boolean_node (above ? true : false, type); }
6441            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6442             { constant_boolean_node (above ? false : true, type); })))))))))
6443    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6444    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6445         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6446             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6447         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6448             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6449     (with
6450      {
6451        tree type1 = TREE_TYPE (@10);
6452        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6453          {
6454            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6455            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6456                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6457              type1 = float_type_node;
6458            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6459                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6460              type1 = double_type_node;
6461          }
6462       tree newtype
6463         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6464            ? TREE_TYPE (@00) : type1);
6465      }
6466      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6467       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6470 (for cmp (eq ne)
6471  (simplify
6472   /* SSA names are canonicalized to 2nd place.  */
6473   (cmp addr@0 SSA_NAME@1)
6474   (with
6475    {
6476      poly_int64 off; tree base;
6477      tree addr = (TREE_CODE (@0) == SSA_NAME
6478                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6479    }
6480    /* A local variable can never be pointed to by
6481       the default SSA name of an incoming parameter.  */
6482    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6483         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6484         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6485         && TREE_CODE (base) == VAR_DECL
6486         && auto_var_in_fn_p (base, current_function_decl))
6487     (if (cmp == NE_EXPR)
6488      { constant_boolean_node (true, type); }
6489      { constant_boolean_node (false, type); })
6490     /* If the address is based on @1 decide using the offset.  */
6491     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6492          && TREE_CODE (base) == MEM_REF
6493          && TREE_OPERAND (base, 0) == @1)
6494      (with { off += mem_ref_offset (base).force_shwi (); }
6495       (if (known_ne (off, 0))
6496        { constant_boolean_node (cmp == NE_EXPR, type); }
6497        (if (known_eq (off, 0))
6498         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6500 /* Equality compare simplifications from fold_binary  */
6501 (for cmp (eq ne)
6503  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6504     Similarly for NE_EXPR.  */
6505  (simplify
6506   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6507   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6508        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6509    { constant_boolean_node (cmp == NE_EXPR, type); }))
6511  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6512  (simplify
6513   (cmp (bit_xor @0 @1) integer_zerop)
6514   (cmp @0 @1))
6516  /* (X ^ Y) == Y becomes X == 0.
6517     Likewise (X ^ Y) == X becomes Y == 0.  */
6518  (simplify
6519   (cmp:c (bit_xor:c @0 @1) @0)
6520   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6522  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6523  (simplify
6524   (cmp:c (bit_and:c @0 @1) @0)
6525   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6526  (simplify
6527   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6528   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6529        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6530        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6531        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6532        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6533        && !wi::neg_p (wi::to_wide (@1)))
6534    (cmp (bit_and @0 (convert (bit_not @1)))
6535         { build_zero_cst (TREE_TYPE (@0)); })))
6537  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6538  (simplify
6539   (cmp:c (bit_ior:c @0 @1) @1)
6540   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6542  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6543  (simplify
6544   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6545   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6546    (cmp @0 (bit_xor @1 (convert @2)))))
6548  (simplify
6549   (cmp (nop_convert? @0) integer_zerop)
6550   (if (tree_expr_nonzero_p (@0))
6551    { constant_boolean_node (cmp == NE_EXPR, type); }))
6553  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6554  (simplify
6555   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6556   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6558 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6559    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6560    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6561    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6562 (for cmp (eq ne)
6563      ncmp (ge lt)
6564  (for sgncmp (ge lt)
6565   (simplify
6566    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6567    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6568         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6569         && types_match (@0, @1))
6570     (ncmp (bit_xor @0 @1) @2)))))
6571 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6572    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6573 (for cmp (eq ne)
6574      ncmp (lt ge)
6575  (simplify
6576   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6577    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6578         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6579         && types_match (@0, @1))
6580     (ncmp (bit_xor @0 @1) @2))))
6582 /* If we have (A & C) == C where C is a power of 2, convert this into
6583    (A & C) != 0.  Similarly for NE_EXPR.  */
6584 (for cmp (eq ne)
6585      icmp (ne eq)
6586  (simplify
6587   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6588   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6590 #if GIMPLE
6591 /* From fold_binary_op_with_conditional_arg handle the case of
6592    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6593    compares simplify.  */
6594 (for cmp (simple_comparison)
6595  (simplify
6596   (cmp:c (cond @0 @1 @2) @3)
6597   /* Do not move possibly trapping operations into the conditional as this
6598      pessimizes code and causes gimplification issues when applied late.  */
6599   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6600        || !operation_could_trap_p (cmp, true, false, @3))
6601    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6602 #endif
6604 (for cmp (ge lt)
6605 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6606 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6607  (simplify
6608   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6609    (if (INTEGRAL_TYPE_P (type)
6610         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6611         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6612         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6613     (with
6614      {
6615        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6616      }
6617     (if (cmp == LT_EXPR)
6618      (bit_xor (convert (rshift @0 {shifter;})) @1)
6619      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6620 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6621 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6622  (simplify
6623   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6624    (if (INTEGRAL_TYPE_P (type)
6625         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6626         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6627         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6628     (with
6629      {
6630        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6631      }
6632     (if (cmp == GE_EXPR)
6633      (bit_xor (convert (rshift @0 {shifter;})) @1)
6634      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6636 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6637    convert this into a shift followed by ANDing with D.  */
6638 (simplify
6639  (cond
6640   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6641   INTEGER_CST@2 integer_zerop)
6642  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6643   (with {
6644      int shift = (wi::exact_log2 (wi::to_wide (@2))
6645                   - wi::exact_log2 (wi::to_wide (@1)));
6646    }
6647    (if (shift > 0)
6648     (bit_and
6649      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6650     (bit_and
6651      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6652      @2)))))
6654 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6655    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6656 (for cmp (eq ne)
6657      ncmp (ge lt)
6658  (simplify
6659   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6660   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6661        && type_has_mode_precision_p (TREE_TYPE (@0))
6662        && element_precision (@2) >= element_precision (@0)
6663        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6664    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6665     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6667 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6668    this into a right shift or sign extension followed by ANDing with C.  */
6669 (simplify
6670  (cond
6671   (lt @0 integer_zerop)
6672   INTEGER_CST@1 integer_zerop)
6673  (if (integer_pow2p (@1)
6674       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6675   (with {
6676     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6677    }
6678    (if (shift >= 0)
6679     (bit_and
6680      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6681      @1)
6682     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6683        sign extension followed by AND with C will achieve the effect.  */
6684     (bit_and (convert @0) @1)))))
6686 /* When the addresses are not directly of decls compare base and offset.
6687    This implements some remaining parts of fold_comparison address
6688    comparisons but still no complete part of it.  Still it is good
6689    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6690 (for cmp (simple_comparison)
6691  (simplify
6692   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6693   (with
6694    {
6695      poly_int64 off0, off1;
6696      tree base0, base1;
6697      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6698                                   off0, off1, GENERIC);
6699    }
6700    (if (equal == 1)
6701     (switch
6702      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6703       { constant_boolean_node (known_eq (off0, off1), type); })
6704      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6705       { constant_boolean_node (known_ne (off0, off1), type); })
6706      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6707       { constant_boolean_node (known_lt (off0, off1), type); })
6708      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6709       { constant_boolean_node (known_le (off0, off1), type); })
6710      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6711       { constant_boolean_node (known_ge (off0, off1), type); })
6712      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6713       { constant_boolean_node (known_gt (off0, off1), type); }))
6714     (if (equal == 0)
6715      (switch
6716       (if (cmp == EQ_EXPR)
6717        { constant_boolean_node (false, type); })
6718       (if (cmp == NE_EXPR)
6719        { constant_boolean_node (true, type); })))))))
6721 #if GIMPLE
6722 /* a?~t:t -> (-(a))^t */
6723 (simplify
6724  (cond @0 @1 @2)
6725  (with { bool wascmp; }
6726   (if (INTEGRAL_TYPE_P (type)
6727        && bitwise_inverted_equal_p (@1, @2, wascmp)
6728        && (!wascmp || element_precision (type) == 1))
6729    (with {
6730      auto prec = TYPE_PRECISION (type);
6731      auto unsign = TYPE_UNSIGNED (type);
6732      tree inttype = build_nonstandard_integer_type (prec, unsign);
6733     }
6734     (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
6735 #endif
6737 /* Simplify pointer equality compares using PTA.  */
6738 (for neeq (ne eq)
6739  (simplify
6740   (neeq @0 @1)
6741   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6742        && ptrs_compare_unequal (@0, @1))
6743    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6745 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6746    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6747    Disable the transform if either operand is pointer to function.
6748    This broke pr22051-2.c for arm where function pointer
6749    canonicalizaion is not wanted.  */
6751 (for cmp (ne eq)
6752  (simplify
6753   (cmp (convert @0) INTEGER_CST@1)
6754   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6755          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6756          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6757          /* Don't perform this optimization in GENERIC if @0 has reference
6758             type when sanitizing.  See PR101210.  */
6759          && !(GENERIC
6760               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6761               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6762         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6763             && POINTER_TYPE_P (TREE_TYPE (@1))
6764             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6765        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6766    (cmp @0 (convert @1)))))
6768 /* Non-equality compare simplifications from fold_binary  */
6769 (for cmp (lt gt le ge)
6770  /* Comparisons with the highest or lowest possible integer of
6771     the specified precision will have known values.  */
6772  (simplify
6773   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6774   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6775         || POINTER_TYPE_P (TREE_TYPE (@1))
6776         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6777        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6778    (with
6779     {
6780       tree cst = uniform_integer_cst_p (@1);
6781       tree arg1_type = TREE_TYPE (cst);
6782       unsigned int prec = TYPE_PRECISION (arg1_type);
6783       wide_int max = wi::max_value (arg1_type);
6784       wide_int signed_max = wi::max_value (prec, SIGNED);
6785       wide_int min = wi::min_value (arg1_type);
6786     }
6787     (switch
6788      (if (wi::to_wide (cst) == max)
6789       (switch
6790        (if (cmp == GT_EXPR)
6791         { constant_boolean_node (false, type); })
6792        (if (cmp == GE_EXPR)
6793         (eq @2 @1))
6794        (if (cmp == LE_EXPR)
6795         { constant_boolean_node (true, type); })
6796        (if (cmp == LT_EXPR)
6797         (ne @2 @1))))
6798      (if (wi::to_wide (cst) == min)
6799       (switch
6800        (if (cmp == LT_EXPR)
6801         { constant_boolean_node (false, type); })
6802        (if (cmp == LE_EXPR)
6803         (eq @2 @1))
6804        (if (cmp == GE_EXPR)
6805         { constant_boolean_node (true, type); })
6806        (if (cmp == GT_EXPR)
6807         (ne @2 @1))))
6808      (if (wi::to_wide (cst) == max - 1)
6809       (switch
6810        (if (cmp == GT_EXPR)
6811         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6812                                     wide_int_to_tree (TREE_TYPE (cst),
6813                                                       wi::to_wide (cst)
6814                                                       + 1)); }))
6815        (if (cmp == LE_EXPR)
6816         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6817                                     wide_int_to_tree (TREE_TYPE (cst),
6818                                                       wi::to_wide (cst)
6819                                                       + 1)); }))))
6820      (if (wi::to_wide (cst) == min + 1)
6821       (switch
6822        (if (cmp == GE_EXPR)
6823         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6824                                     wide_int_to_tree (TREE_TYPE (cst),
6825                                                       wi::to_wide (cst)
6826                                                       - 1)); }))
6827        (if (cmp == LT_EXPR)
6828         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6829                                     wide_int_to_tree (TREE_TYPE (cst),
6830                                                       wi::to_wide (cst)
6831                                                       - 1)); }))))
6832      (if (wi::to_wide (cst) == signed_max
6833           && TYPE_UNSIGNED (arg1_type)
6834           && TYPE_MODE (arg1_type) != BLKmode
6835           /* We will flip the signedness of the comparison operator
6836              associated with the mode of @1, so the sign bit is
6837              specified by this mode.  Check that @1 is the signed
6838              max associated with this sign bit.  */
6839           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6840           /* signed_type does not work on pointer types.  */
6841           && INTEGRAL_TYPE_P (arg1_type))
6842       /* The following case also applies to X < signed_max+1
6843          and X >= signed_max+1 because previous transformations.  */
6844       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6845        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6846         (switch
6847          (if (cst == @1 && cmp == LE_EXPR)
6848           (ge (convert:st @0) { build_zero_cst (st); }))
6849          (if (cst == @1 && cmp == GT_EXPR)
6850           (lt (convert:st @0) { build_zero_cst (st); }))
6851          (if (cmp == LE_EXPR)
6852           (ge (view_convert:st @0) { build_zero_cst (st); }))
6853          (if (cmp == GT_EXPR)
6854           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6856 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
6857 (simplify
6858  (lt:c @0 (convert (ne @0 integer_zerop)))
6859  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6860   { constant_boolean_node (false, type); }))
6862 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6863 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6864 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6865 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6866 (for outer (ne eq)
6867  (for inner (ne eq)
6868   (simplify
6869    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6870    (with {
6871      bool cst1 = integer_onep (@1);
6872      bool cst0 = integer_zerop (@1);
6873      bool innereq = inner == EQ_EXPR;
6874      bool outereq = outer == EQ_EXPR;
6875     }
6876     (switch
6877      (if (innereq ? cst0 : cst1)
6878       { constant_boolean_node (!outereq, type); })
6879      (if (innereq ? cst1 : cst0)
6880       (with {
6881         tree utype = unsigned_type_for (TREE_TYPE (@0));
6882         tree ucst1 = build_one_cst (utype);
6883        }
6884        (if (!outereq)
6885         (gt (convert:utype @0) { ucst1; })
6886         (le (convert:utype @0) { ucst1; })
6887        )
6888       )
6889      )
6890      (with {
6891        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6892       }
6893       (if (outereq)
6894        (eq @0 { value; })
6895        (ne @0 { value; })
6896       )
6897      )
6898     )
6899    )
6900   )
6904 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6905  /* If the second operand is NaN, the result is constant.  */
6906  (simplify
6907   (cmp @0 REAL_CST@1)
6908   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6909        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6910    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6911                             ? false : true, type); })))
6913 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6914 (simplify
6915   (unordered @0 @1)
6916   (switch
6917     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6918         { constant_boolean_node (true, type); })
6919     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6920         { constant_boolean_node (false, type); })))
6922 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6923 (simplify
6924   (ordered @0 @1)
6925   (switch
6926     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6927         { constant_boolean_node (false, type); })
6928     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6929         { constant_boolean_node (true, type); })))
6931 /* bool_var != 0 becomes bool_var.  */
6932 (simplify
6933  (ne @0 integer_zerop)
6934  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6935       && types_match (type, TREE_TYPE (@0)))
6936   (non_lvalue @0)))
6937 /* bool_var == 1 becomes bool_var.  */
6938 (simplify
6939  (eq @0 integer_onep)
6940  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6941       && types_match (type, TREE_TYPE (@0)))
6942   (non_lvalue @0)))
6943 /* Do not handle
6944    bool_var == 0 becomes !bool_var or
6945    bool_var != 1 becomes !bool_var
6946    here because that only is good in assignment context as long
6947    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6948    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6949    clearly less optimal and which we'll transform again in forwprop.  */
6951 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6952    where ~Y + 1 == pow2 and Z = ~Y.  */
6953 (for cst (VECTOR_CST INTEGER_CST)
6954  (for cmp (eq ne)
6955       icmp (le gt)
6956   (simplify
6957    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6958     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6959      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6960       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6961                          ? optab_vector : optab_default;
6962               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6963        (if (target_supports_op_p (utype, icmp, optab)
6964             || (optimize_vectors_before_lowering_p ()
6965                 && (!target_supports_op_p (type, cmp, optab)
6966                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6967         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6968          (icmp @0 { csts; })
6969          (icmp (view_convert:utype @0) { csts; })))))))))
6971 /* When one argument is a constant, overflow detection can be simplified.
6972    Currently restricted to single use so as not to interfere too much with
6973    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6974    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6975 (for cmp (lt le ge gt)
6976      out (gt gt le le)
6977  (simplify
6978   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6979   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6980        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6981        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6982        && wi::to_wide (@1) != 0
6983        && single_use (@2))
6984    (with {
6985      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6986      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6987     }
6988     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6989                                 wi::max_value (prec, sign)
6990                                 - wi::to_wide (@1)); })))))
6992 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6993    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6994    expects the long form, so we restrict the transformation for now.  */
6995 (for cmp (gt le)
6996  (simplify
6997   (cmp:c (minus@2 @0 @1) @0)
6998   (if (single_use (@2)
6999        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7000        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7001    (cmp @1 @0))))
7003 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7004 (for cmp (ge lt)
7005  (simplify
7006   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7007    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7008         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7009     (cmp @1 @0))))
7011 /* Testing for overflow is unnecessary if we already know the result.  */
7012 /* A - B > A  */
7013 (for cmp (gt le)
7014      out (ne eq)
7015  (simplify
7016   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7017   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7018        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7019    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7020 /* A + B < A  */
7021 (for cmp (lt ge)
7022      out (ne eq)
7023  (simplify
7024   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7025   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7026        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7027    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7029 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7030    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7031 (for cmp (lt ge)
7032      out (ne eq)
7033  (simplify
7034   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7035   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7036    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7037     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7039 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7040    is at least twice as wide as type of A and B, simplify to
7041    __builtin_mul_overflow (A, B, <unused>).  */
7042 (for cmp (eq ne)
7043  (simplify
7044   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7045        integer_zerop)
7046   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7047        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7048        && TYPE_UNSIGNED (TREE_TYPE (@0))
7049        && (TYPE_PRECISION (TREE_TYPE (@3))
7050            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7051        && tree_fits_uhwi_p (@2)
7052        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7053        && types_match (@0, @1)
7054        && type_has_mode_precision_p (TREE_TYPE (@0))
7055        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7056            != CODE_FOR_nothing))
7057    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7058     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7060 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7061 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7062  (simplify
7063   (ovf (convert@2 @0) @1)
7064   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7065        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7066        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7067        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7068    (ovf @0 @1)))
7069  (simplify
7070   (ovf @1 (convert@2 @0))
7071   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7072        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7073        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7074        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7075    (ovf @1 @0))))
7077 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7078    are unsigned to x > (umax / cst).  Similarly for signed type, but
7079    in that case it needs to be outside of a range.  */
7080 (simplify
7081  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7082   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7083        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7084        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7085        && int_fits_type_p (@1, TREE_TYPE (@0)))
7086    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7087     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7088     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7089      (if (integer_minus_onep (@1))
7090       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7091       (with
7092        {
7093          tree div = fold_convert (TREE_TYPE (@0), @1);
7094          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7095                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7096          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7097                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7098          tree etype = range_check_type (TREE_TYPE (@0));
7099          if (etype)
7100            {
7101              if (wi::neg_p (wi::to_wide (div)))
7102                std::swap (lo, hi);
7103              lo = fold_convert (etype, lo);
7104              hi = fold_convert (etype, hi);
7105              hi = int_const_binop (MINUS_EXPR, hi, lo);
7106            }
7107        }
7108        (if (etype)
7109         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7111 /* Simplification of math builtins.  These rules must all be optimizations
7112    as well as IL simplifications.  If there is a possibility that the new
7113    form could be a pessimization, the rule should go in the canonicalization
7114    section that follows this one.
7116    Rules can generally go in this section if they satisfy one of
7117    the following:
7119    - the rule describes an identity
7121    - the rule replaces calls with something as simple as addition or
7122      multiplication
7124    - the rule contains unary calls only and simplifies the surrounding
7125      arithmetic.  (The idea here is to exclude non-unary calls in which
7126      one operand is constant and in which the call is known to be cheap
7127      when the operand has that value.)  */
7129 (if (flag_unsafe_math_optimizations)
7130  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7131  (simplify
7132   (mult (SQRT_ALL@1 @0) @1)
7133   (if (!tree_expr_maybe_signaling_nan_p (@0))
7134    @0))
7136  (for op (plus minus)
7137   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7138   (simplify
7139    (op (rdiv @0 @1)
7140        (rdiv @2 @1))
7141    (rdiv (op @0 @2) @1)))
7143  (for cmp (lt le gt ge)
7144       neg_cmp (gt ge lt le)
7145   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7146   (simplify
7147    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7148    (with
7149     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7150     (if (tem
7151          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7152               || (real_zerop (tem) && !real_zerop (@1))))
7153      (switch
7154       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7155        (cmp @0 { tem; }))
7156       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7157        (neg_cmp @0 { tem; })))))))
7159  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7160  (for root (SQRT CBRT)
7161   (simplify
7162    (mult (root:s @0) (root:s @1))
7163     (root (mult @0 @1))))
7165  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7166  (for exps (EXP EXP2 EXP10 POW10)
7167   (simplify
7168    (mult (exps:s @0) (exps:s @1))
7169     (exps (plus @0 @1))))
7171  /* Simplify a/root(b/c) into a*root(c/b).  */
7172  (for root (SQRT CBRT)
7173   (simplify
7174    (rdiv @0 (root:s (rdiv:s @1 @2)))
7175     (mult @0 (root (rdiv @2 @1)))))
7177  /* Simplify x/expN(y) into x*expN(-y).  */
7178  (for exps (EXP EXP2 EXP10 POW10)
7179   (simplify
7180    (rdiv @0 (exps:s @1))
7181     (mult @0 (exps (negate @1)))))
7183  (for logs (LOG LOG2 LOG10 LOG10)
7184       exps (EXP EXP2 EXP10 POW10)
7185   /* logN(expN(x)) -> x.  */
7186   (simplify
7187    (logs (exps @0))
7188    @0)
7189   /* expN(logN(x)) -> x.  */
7190   (simplify
7191    (exps (logs @0))
7192    @0))
7194  /* Optimize logN(func()) for various exponential functions.  We
7195     want to determine the value "x" and the power "exponent" in
7196     order to transform logN(x**exponent) into exponent*logN(x).  */
7197  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7198       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7199   (simplify
7200    (logs (exps @0))
7201    (if (SCALAR_FLOAT_TYPE_P (type))
7202     (with {
7203       tree x;
7204       switch (exps)
7205         {
7206         CASE_CFN_EXP:
7207           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7208           x = build_real_truncate (type, dconst_e ());
7209           break;
7210         CASE_CFN_EXP2:
7211           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7212           x = build_real (type, dconst2);
7213           break;
7214         CASE_CFN_EXP10:
7215         CASE_CFN_POW10:
7216           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7217           {
7218             REAL_VALUE_TYPE dconst10;
7219             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7220             x = build_real (type, dconst10);
7221           }
7222           break;
7223         default:
7224           gcc_unreachable ();
7225         }
7226       }
7227      (mult (logs { x; }) @0)))))
7229  (for logs (LOG LOG
7230             LOG2 LOG2
7231             LOG10 LOG10)
7232       exps (SQRT CBRT)
7233   (simplify
7234    (logs (exps @0))
7235    (if (SCALAR_FLOAT_TYPE_P (type))
7236     (with {
7237       tree x;
7238       switch (exps)
7239         {
7240         CASE_CFN_SQRT:
7241           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7242           x = build_real (type, dconsthalf);
7243           break;
7244         CASE_CFN_CBRT:
7245           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7246           x = build_real_truncate (type, dconst_third ());
7247           break;
7248         default:
7249           gcc_unreachable ();
7250         }
7251       }
7252      (mult { x; } (logs @0))))))
7254  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7255  (for logs (LOG LOG2 LOG10)
7256       pows (POW)
7257   (simplify
7258    (logs (pows @0 @1))
7259    (mult @1 (logs @0))))
7261  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7262     or if C is a positive power of 2,
7263     pow(C,x) -> exp2(log2(C)*x).  */
7264 #if GIMPLE
7265  (for pows (POW)
7266       exps (EXP)
7267       logs (LOG)
7268       exp2s (EXP2)
7269       log2s (LOG2)
7270   (simplify
7271    (pows REAL_CST@0 @1)
7272    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7273         && real_isfinite (TREE_REAL_CST_PTR (@0))
7274         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7275            the use_exp2 case until after vectorization.  It seems actually
7276            beneficial for all constants to postpone this until later,
7277            because exp(log(C)*x), while faster, will have worse precision
7278            and if x folds into a constant too, that is unnecessary
7279            pessimization.  */
7280         && canonicalize_math_after_vectorization_p ())
7281     (with {
7282        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7283        bool use_exp2 = false;
7284        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7285            && value->cl == rvc_normal)
7286          {
7287            REAL_VALUE_TYPE frac_rvt = *value;
7288            SET_REAL_EXP (&frac_rvt, 1);
7289            if (real_equal (&frac_rvt, &dconst1))
7290              use_exp2 = true;
7291          }
7292      }
7293      (if (!use_exp2)
7294       (if (optimize_pow_to_exp (@0, @1))
7295        (exps (mult (logs @0) @1)))
7296       (exp2s (mult (log2s @0) @1)))))))
7297 #endif
7299  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7300  (for pows (POW)
7301       exps (EXP EXP2 EXP10 POW10)
7302       logs (LOG LOG2 LOG10 LOG10)
7303   (simplify
7304    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7305    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7306         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7307     (exps (plus (mult (logs @0) @1) @2)))))
7309  (for sqrts (SQRT)
7310       cbrts (CBRT)
7311       pows (POW)
7312       exps (EXP EXP2 EXP10 POW10)
7313   /* sqrt(expN(x)) -> expN(x*0.5).  */
7314   (simplify
7315    (sqrts (exps @0))
7316    (exps (mult @0 { build_real (type, dconsthalf); })))
7317   /* cbrt(expN(x)) -> expN(x/3).  */
7318   (simplify
7319    (cbrts (exps @0))
7320    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7321   /* pow(expN(x), y) -> expN(x*y).  */
7322   (simplify
7323    (pows (exps @0) @1)
7324    (exps (mult @0 @1))))
7326  /* tan(atan(x)) -> x.  */
7327  (for tans (TAN)
7328       atans (ATAN)
7329   (simplify
7330    (tans (atans @0))
7331    @0)))
7333  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7334  (for sins (SIN)
7335       atans (ATAN)
7336       sqrts (SQRT)
7337       copysigns (COPYSIGN)
7338   (simplify
7339    (sins (atans:s @0))
7340    (with
7341      {
7342       REAL_VALUE_TYPE r_cst;
7343       build_sinatan_real (&r_cst, type);
7344       tree t_cst = build_real (type, r_cst);
7345       tree t_one = build_one_cst (type);
7346      }
7347     (if (SCALAR_FLOAT_TYPE_P (type))
7348      (cond (lt (abs @0) { t_cst; })
7349       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7350       (copysigns { t_one; } @0))))))
7352 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7353  (for coss (COS)
7354       atans (ATAN)
7355       sqrts (SQRT)
7356       copysigns (COPYSIGN)
7357   (simplify
7358    (coss (atans:s @0))
7359    (with
7360      {
7361       REAL_VALUE_TYPE r_cst;
7362       build_sinatan_real (&r_cst, type);
7363       tree t_cst = build_real (type, r_cst);
7364       tree t_one = build_one_cst (type);
7365       tree t_zero = build_zero_cst (type);
7366      }
7367     (if (SCALAR_FLOAT_TYPE_P (type))
7368      (cond (lt (abs @0) { t_cst; })
7369       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7370       (copysigns { t_zero; } @0))))))
7372  (if (!flag_errno_math)
7373   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7374   (for sinhs (SINH)
7375        atanhs (ATANH)
7376        sqrts (SQRT)
7377    (simplify
7378     (sinhs (atanhs:s @0))
7379     (with { tree t_one = build_one_cst (type); }
7380     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7382   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7383   (for coshs (COSH)
7384        atanhs (ATANH)
7385        sqrts (SQRT)
7386    (simplify
7387     (coshs (atanhs:s @0))
7388     (with { tree t_one = build_one_cst (type); }
7389     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7391 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7392 (simplify
7393  (CABS (complex:C @0 real_zerop@1))
7394  (abs @0))
7396 /* trunc(trunc(x)) -> trunc(x), etc.  */
7397 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7398  (simplify
7399   (fns (fns @0))
7400   (fns @0)))
7401 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7402 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7403  (simplify
7404   (fns integer_valued_real_p@0)
7405   @0))
7407 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7408 (simplify
7409  (HYPOT:c @0 real_zerop@1)
7410  (abs @0))
7412 /* pow(1,x) -> 1.  */
7413 (simplify
7414  (POW real_onep@0 @1)
7415  @0)
7417 (simplify
7418  /* copysign(x,x) -> x.  */
7419  (COPYSIGN_ALL @0 @0)
7420  @0)
7422 (simplify
7423  /* copysign(x,-x) -> -x.  */
7424  (COPYSIGN_ALL @0 (negate@1 @0))
7425  @1)
7427 (simplify
7428  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7429  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7430  (abs @0))
7432 (for scale (LDEXP SCALBN SCALBLN)
7433  /* ldexp(0, x) -> 0.  */
7434  (simplify
7435   (scale real_zerop@0 @1)
7436   @0)
7437  /* ldexp(x, 0) -> x.  */
7438  (simplify
7439   (scale @0 integer_zerop@1)
7440   @0)
7441  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7442  (simplify
7443   (scale REAL_CST@0 @1)
7444   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7445    @0)))
7447 /* Canonicalization of sequences of math builtins.  These rules represent
7448    IL simplifications but are not necessarily optimizations.
7450    The sincos pass is responsible for picking "optimal" implementations
7451    of math builtins, which may be more complicated and can sometimes go
7452    the other way, e.g. converting pow into a sequence of sqrts.
7453    We only want to do these canonicalizations before the pass has run.  */
7455 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7456  /* Simplify tan(x) * cos(x) -> sin(x). */
7457  (simplify
7458   (mult:c (TAN:s @0) (COS:s @0))
7459    (SIN @0))
7461  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7462  (simplify
7463   (mult:c @0 (POW:s @0 REAL_CST@1))
7464   (if (!TREE_OVERFLOW (@1))
7465    (POW @0 (plus @1 { build_one_cst (type); }))))
7467  /* Simplify sin(x) / cos(x) -> tan(x). */
7468  (simplify
7469   (rdiv (SIN:s @0) (COS:s @0))
7470    (TAN @0))
7472  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7473  (simplify
7474   (rdiv (SINH:s @0) (COSH:s @0))
7475    (TANH @0))
7477  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7478  (simplify
7479    (rdiv (TANH:s @0) (SINH:s @0))
7480    (rdiv {build_one_cst (type);} (COSH @0)))
7482  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7483  (simplify
7484   (rdiv (COS:s @0) (SIN:s @0))
7485    (rdiv { build_one_cst (type); } (TAN @0)))
7487  /* Simplify sin(x) / tan(x) -> cos(x). */
7488  (simplify
7489   (rdiv (SIN:s @0) (TAN:s @0))
7490   (if (! HONOR_NANS (@0)
7491        && ! HONOR_INFINITIES (@0))
7492    (COS @0)))
7494  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7495  (simplify
7496   (rdiv (TAN:s @0) (SIN:s @0))
7497   (if (! HONOR_NANS (@0)
7498        && ! HONOR_INFINITIES (@0))
7499    (rdiv { build_one_cst (type); } (COS @0))))
7501  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7502  (simplify
7503   (mult (POW:s @0 @1) (POW:s @0 @2))
7504    (POW @0 (plus @1 @2)))
7506  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7507  (simplify
7508   (mult (POW:s @0 @1) (POW:s @2 @1))
7509    (POW (mult @0 @2) @1))
7511  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7512  (simplify
7513   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7514    (POWI (mult @0 @2) @1))
7516  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7517  (simplify
7518   (rdiv (POW:s @0 REAL_CST@1) @0)
7519   (if (!TREE_OVERFLOW (@1))
7520    (POW @0 (minus @1 { build_one_cst (type); }))))
7522  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7523  (simplify
7524   (rdiv @0 (POW:s @1 @2))
7525    (mult @0 (POW @1 (negate @2))))
7527  (for sqrts (SQRT)
7528       cbrts (CBRT)
7529       pows (POW)
7530   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7531   (simplify
7532    (sqrts (sqrts @0))
7533    (pows @0 { build_real (type, dconst_quarter ()); }))
7534   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7535   (simplify
7536    (sqrts (cbrts @0))
7537    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7538   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7539   (simplify
7540    (cbrts (sqrts @0))
7541    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7542   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7543   (simplify
7544    (cbrts (cbrts tree_expr_nonnegative_p@0))
7545    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7546   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7547   (simplify
7548    (sqrts (pows @0 @1))
7549    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7550   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7551   (simplify
7552    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7553    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7554   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7555   (simplify
7556    (pows (sqrts @0) @1)
7557    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7558   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7559   (simplify
7560    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7561    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7562   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7563   (simplify
7564    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7565    (pows @0 (mult @1 @2))))
7567  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7568  (simplify
7569   (CABS (complex @0 @0))
7570   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7572  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7573  (simplify
7574   (HYPOT @0 @0)
7575   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7577  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7578  (for cexps (CEXP)
7579       exps (EXP)
7580       cexpis (CEXPI)
7581   (simplify
7582    (cexps compositional_complex@0)
7583    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7584     (complex
7585      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7586      (mult @1 (imagpart @2)))))))
7588 (if (canonicalize_math_p ())
7589  /* floor(x) -> trunc(x) if x is nonnegative.  */
7590  (for floors (FLOOR_ALL)
7591       truncs (TRUNC_ALL)
7592   (simplify
7593    (floors tree_expr_nonnegative_p@0)
7594    (truncs @0))))
7596 (match double_value_p
7597  @0
7598  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7599 (for froms (BUILT_IN_TRUNCL
7600             BUILT_IN_FLOORL
7601             BUILT_IN_CEILL
7602             BUILT_IN_ROUNDL
7603             BUILT_IN_NEARBYINTL
7604             BUILT_IN_RINTL)
7605      tos (BUILT_IN_TRUNC
7606           BUILT_IN_FLOOR
7607           BUILT_IN_CEIL
7608           BUILT_IN_ROUND
7609           BUILT_IN_NEARBYINT
7610           BUILT_IN_RINT)
7611  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7612  (if (optimize && canonicalize_math_p ())
7613   (simplify
7614    (froms (convert double_value_p@0))
7615    (convert (tos @0)))))
7617 (match float_value_p
7618  @0
7619  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7620 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7621             BUILT_IN_FLOORL BUILT_IN_FLOOR
7622             BUILT_IN_CEILL BUILT_IN_CEIL
7623             BUILT_IN_ROUNDL BUILT_IN_ROUND
7624             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7625             BUILT_IN_RINTL BUILT_IN_RINT)
7626      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7627           BUILT_IN_FLOORF BUILT_IN_FLOORF
7628           BUILT_IN_CEILF BUILT_IN_CEILF
7629           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7630           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7631           BUILT_IN_RINTF BUILT_IN_RINTF)
7632  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7633     if x is a float.  */
7634  (if (optimize && canonicalize_math_p ()
7635       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7636   (simplify
7637    (froms (convert float_value_p@0))
7638    (convert (tos @0)))))
7640 #if GIMPLE
7641 (match float16_value_p
7642  @0
7643  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7644 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7645             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7646             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7647             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7648             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7649             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7650             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7651             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7652      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7653           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7654           IFN_CEIL IFN_CEIL IFN_CEIL
7655           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7656           IFN_ROUND IFN_ROUND IFN_ROUND
7657           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7658           IFN_RINT IFN_RINT IFN_RINT
7659           IFN_SQRT IFN_SQRT IFN_SQRT)
7660  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7661     if x is a _Float16.  */
7662  (simplify
7663    (convert (froms (convert float16_value_p@0)))
7664      (if (optimize
7665           && types_match (type, TREE_TYPE (@0))
7666           && direct_internal_fn_supported_p (as_internal_fn (tos),
7667                                              type, OPTIMIZE_FOR_BOTH))
7668        (tos @0))))
7670 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7671    x,y is float value, similar for _Float16/double.  */
7672 (for copysigns (COPYSIGN_ALL)
7673  (simplify
7674   (convert (copysigns (convert@2 @0) (convert @1)))
7675    (if (optimize
7676        && !HONOR_SNANS (@2)
7677        && types_match (type, TREE_TYPE (@0))
7678        && types_match (type, TREE_TYPE (@1))
7679        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7680        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7681                                           type, OPTIMIZE_FOR_BOTH))
7682     (IFN_COPYSIGN @0 @1))))
7684 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7685      tos (IFN_FMA IFN_FMA IFN_FMA)
7686  (simplify
7687   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7688    (if (flag_unsafe_math_optimizations
7689        && optimize
7690        && FLOAT_TYPE_P (type)
7691        && FLOAT_TYPE_P (TREE_TYPE (@3))
7692        && types_match (type, TREE_TYPE (@0))
7693        && types_match (type, TREE_TYPE (@1))
7694        && types_match (type, TREE_TYPE (@2))
7695        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7696        && direct_internal_fn_supported_p (as_internal_fn (tos),
7697                                           type, OPTIMIZE_FOR_BOTH))
7698     (tos @0 @1 @2))))
7700 (for maxmin (max min)
7701  (simplify
7702   (convert (maxmin (convert@2 @0) (convert @1)))
7703    (if (optimize
7704        && FLOAT_TYPE_P (type)
7705        && FLOAT_TYPE_P (TREE_TYPE (@2))
7706        && types_match (type, TREE_TYPE (@0))
7707        && types_match (type, TREE_TYPE (@1))
7708        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7709     (maxmin @0 @1))))
7710 #endif
7712 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7713      tos (XFLOOR XCEIL XROUND XRINT)
7714  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7715  (if (optimize && canonicalize_math_p ())
7716   (simplify
7717    (froms (convert double_value_p@0))
7718    (tos @0))))
7720 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7721             XFLOOR XCEIL XROUND XRINT)
7722      tos (XFLOORF XCEILF XROUNDF XRINTF)
7723  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7724     if x is a float.  */
7725  (if (optimize && canonicalize_math_p ())
7726   (simplify
7727    (froms (convert float_value_p@0))
7728    (tos @0))))
7730 (if (canonicalize_math_p ())
7731  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7732  (for floors (IFLOOR LFLOOR LLFLOOR)
7733   (simplify
7734    (floors tree_expr_nonnegative_p@0)
7735    (fix_trunc @0))))
7737 (if (canonicalize_math_p ())
7738  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7739  (for fns (IFLOOR LFLOOR LLFLOOR
7740            ICEIL LCEIL LLCEIL
7741            IROUND LROUND LLROUND)
7742   (simplify
7743    (fns integer_valued_real_p@0)
7744    (fix_trunc @0)))
7745  (if (!flag_errno_math)
7746   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7747   (for rints (IRINT LRINT LLRINT)
7748    (simplify
7749     (rints integer_valued_real_p@0)
7750     (fix_trunc @0)))))
7752 (if (canonicalize_math_p ())
7753  (for ifn (IFLOOR ICEIL IROUND IRINT)
7754       lfn (LFLOOR LCEIL LROUND LRINT)
7755       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7756   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7757      sizeof (int) == sizeof (long).  */
7758   (if (TYPE_PRECISION (integer_type_node)
7759        == TYPE_PRECISION (long_integer_type_node))
7760    (simplify
7761     (ifn @0)
7762     (lfn:long_integer_type_node @0)))
7763   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7764      sizeof (long long) == sizeof (long).  */
7765   (if (TYPE_PRECISION (long_long_integer_type_node)
7766        == TYPE_PRECISION (long_integer_type_node))
7767    (simplify
7768     (llfn @0)
7769     (lfn:long_integer_type_node @0)))))
7771 /* cproj(x) -> x if we're ignoring infinities.  */
7772 (simplify
7773  (CPROJ @0)
7774  (if (!HONOR_INFINITIES (type))
7775    @0))
7777 /* If the real part is inf and the imag part is known to be
7778    nonnegative, return (inf + 0i).  */
7779 (simplify
7780  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7781  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7782   { build_complex_inf (type, false); }))
7784 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7785 (simplify
7786  (CPROJ (complex @0 REAL_CST@1))
7787  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7788   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7790 (for pows (POW)
7791      sqrts (SQRT)
7792      cbrts (CBRT)
7793  (simplify
7794   (pows @0 REAL_CST@1)
7795   (with {
7796     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7797     REAL_VALUE_TYPE tmp;
7798    }
7799    (switch
7800     /* pow(x,0) -> 1.  */
7801     (if (real_equal (value, &dconst0))
7802      { build_real (type, dconst1); })
7803     /* pow(x,1) -> x.  */
7804     (if (real_equal (value, &dconst1))
7805      @0)
7806     /* pow(x,-1) -> 1/x.  */
7807     (if (real_equal (value, &dconstm1))
7808      (rdiv { build_real (type, dconst1); } @0))
7809     /* pow(x,0.5) -> sqrt(x).  */
7810     (if (flag_unsafe_math_optimizations
7811          && canonicalize_math_p ()
7812          && real_equal (value, &dconsthalf))
7813      (sqrts @0))
7814     /* pow(x,1/3) -> cbrt(x).  */
7815     (if (flag_unsafe_math_optimizations
7816          && canonicalize_math_p ()
7817          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7818              real_equal (value, &tmp)))
7819      (cbrts @0))))))
7821 /* powi(1,x) -> 1.  */
7822 (simplify
7823  (POWI real_onep@0 @1)
7824  @0)
7826 (simplify
7827  (POWI @0 INTEGER_CST@1)
7828  (switch
7829   /* powi(x,0) -> 1.  */
7830   (if (wi::to_wide (@1) == 0)
7831    { build_real (type, dconst1); })
7832   /* powi(x,1) -> x.  */
7833   (if (wi::to_wide (@1) == 1)
7834    @0)
7835   /* powi(x,-1) -> 1/x.  */
7836   (if (wi::to_wide (@1) == -1)
7837    (rdiv { build_real (type, dconst1); } @0))))
7839 /* Narrowing of arithmetic and logical operations.
7841    These are conceptually similar to the transformations performed for
7842    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7843    term we want to move all that code out of the front-ends into here.  */
7845 /* Convert (outertype)((innertype0)a+(innertype1)b)
7846    into ((newtype)a+(newtype)b) where newtype
7847    is the widest mode from all of these.  */
7848 (for op (plus minus mult rdiv)
7849  (simplify
7850    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7851    /* If we have a narrowing conversion of an arithmetic operation where
7852       both operands are widening conversions from the same type as the outer
7853       narrowing conversion.  Then convert the innermost operands to a
7854       suitable unsigned type (to avoid introducing undefined behavior),
7855       perform the operation and convert the result to the desired type.  */
7856    (if (INTEGRAL_TYPE_P (type)
7857         && op != MULT_EXPR
7858         && op != RDIV_EXPR
7859         /* We check for type compatibility between @0 and @1 below,
7860            so there's no need to check that @2/@4 are integral types.  */
7861         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7862         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7863         /* The precision of the type of each operand must match the
7864            precision of the mode of each operand, similarly for the
7865            result.  */
7866         && type_has_mode_precision_p (TREE_TYPE (@1))
7867         && type_has_mode_precision_p (TREE_TYPE (@2))
7868         && type_has_mode_precision_p (type)
7869         /* The inner conversion must be a widening conversion.  */
7870         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7871         && types_match (@1, type)
7872         && (types_match (@1, @2)
7873             /* Or the second operand is const integer or converted const
7874                integer from valueize.  */
7875             || poly_int_tree_p (@4)))
7876      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7877        (op @1 (convert @2))
7878        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7879         (convert (op (convert:utype @1)
7880                      (convert:utype @2)))))
7881      (if (FLOAT_TYPE_P (type)
7882           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7883                == DECIMAL_FLOAT_TYPE_P (type))
7884       (with { tree arg0 = strip_float_extensions (@1);
7885               tree arg1 = strip_float_extensions (@2);
7886               tree itype = TREE_TYPE (@0);
7887               tree ty1 = TREE_TYPE (arg0);
7888               tree ty2 = TREE_TYPE (arg1);
7889               enum tree_code code = TREE_CODE (itype); }
7890         (if (FLOAT_TYPE_P (ty1)
7891              && FLOAT_TYPE_P (ty2))
7892          (with { tree newtype = type;
7893                  if (TYPE_MODE (ty1) == SDmode
7894                      || TYPE_MODE (ty2) == SDmode
7895                      || TYPE_MODE (type) == SDmode)
7896                    newtype = dfloat32_type_node;
7897                  if (TYPE_MODE (ty1) == DDmode
7898                      || TYPE_MODE (ty2) == DDmode
7899                      || TYPE_MODE (type) == DDmode)
7900                    newtype = dfloat64_type_node;
7901                  if (TYPE_MODE (ty1) == TDmode
7902                      || TYPE_MODE (ty2) == TDmode
7903                      || TYPE_MODE (type) == TDmode)
7904                    newtype = dfloat128_type_node; }
7905           (if ((newtype == dfloat32_type_node
7906                 || newtype == dfloat64_type_node
7907                 || newtype == dfloat128_type_node)
7908               && newtype == type
7909               && types_match (newtype, type))
7910             (op (convert:newtype @1) (convert:newtype @2))
7911             (with { if (element_precision (ty1) > element_precision (newtype))
7912                       newtype = ty1;
7913                     if (element_precision (ty2) > element_precision (newtype))
7914                       newtype = ty2; }
7915                /* Sometimes this transformation is safe (cannot
7916                   change results through affecting double rounding
7917                   cases) and sometimes it is not.  If NEWTYPE is
7918                   wider than TYPE, e.g. (float)((long double)double
7919                   + (long double)double) converted to
7920                   (float)(double + double), the transformation is
7921                   unsafe regardless of the details of the types
7922                   involved; double rounding can arise if the result
7923                   of NEWTYPE arithmetic is a NEWTYPE value half way
7924                   between two representable TYPE values but the
7925                   exact value is sufficiently different (in the
7926                   right direction) for this difference to be
7927                   visible in ITYPE arithmetic.  If NEWTYPE is the
7928                   same as TYPE, however, the transformation may be
7929                   safe depending on the types involved: it is safe
7930                   if the ITYPE has strictly more than twice as many
7931                   mantissa bits as TYPE, can represent infinities
7932                   and NaNs if the TYPE can, and has sufficient
7933                   exponent range for the product or ratio of two
7934                   values representable in the TYPE to be within the
7935                   range of normal values of ITYPE.  */
7936               (if (element_precision (newtype) < element_precision (itype)
7937                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7938                        || target_supports_op_p (newtype, op, optab_default))
7939                    && (flag_unsafe_math_optimizations
7940                        || (element_precision (newtype) == element_precision (type)
7941                            && real_can_shorten_arithmetic (element_mode (itype),
7942                                                            element_mode (type))
7943                            && !excess_precision_type (newtype)))
7944                    && !types_match (itype, newtype))
7945                  (convert:type (op (convert:newtype @1)
7946                                    (convert:newtype @2)))
7947          )))) )
7948    ))
7951 /* This is another case of narrowing, specifically when there's an outer
7952    BIT_AND_EXPR which masks off bits outside the type of the innermost
7953    operands.   Like the previous case we have to convert the operands
7954    to unsigned types to avoid introducing undefined behavior for the
7955    arithmetic operation.  */
7956 (for op (minus plus)
7957  (simplify
7958   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7959   (if (INTEGRAL_TYPE_P (type)
7960        /* We check for type compatibility between @0 and @1 below,
7961           so there's no need to check that @1/@3 are integral types.  */
7962        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7963        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7964        /* The precision of the type of each operand must match the
7965           precision of the mode of each operand, similarly for the
7966           result.  */
7967        && type_has_mode_precision_p (TREE_TYPE (@0))
7968        && type_has_mode_precision_p (TREE_TYPE (@1))
7969        && type_has_mode_precision_p (type)
7970        /* The inner conversion must be a widening conversion.  */
7971        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7972        && types_match (@0, @1)
7973        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7974            <= TYPE_PRECISION (TREE_TYPE (@0)))
7975        && (wi::to_wide (@4)
7976            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7977                        true, TYPE_PRECISION (type))) == 0)
7978    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7979     (with { tree ntype = TREE_TYPE (@0); }
7980      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7981     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7982      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7983                (convert:utype @4))))))))
7985 /* Transform (@0 < @1 and @0 < @2) to use min,
7986    (@0 > @1 and @0 > @2) to use max */
7987 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7988      op    (lt      le      gt      ge      lt      le      gt      ge     )
7989      ext   (min     min     max     max     max     max     min     min    )
7990  (simplify
7991   (logic (op:cs @0 @1) (op:cs @0 @2))
7992   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7993        && TREE_CODE (@0) != INTEGER_CST)
7994    (op @0 (ext @1 @2)))))
7996 /* Max<bool0, bool1> -> bool0 | bool1
7997    Min<bool0, bool1> -> bool0 & bool1 */
7998 (for op    (max     min)
7999      logic (bit_ior bit_and)
8000  (simplify
8001   (op zero_one_valued_p@0 zero_one_valued_p@1)
8002   (logic @0 @1)))
8004 /* signbit(x) != 0 ? -x : x -> abs(x)
8005    signbit(x) == 0 ? -x : x -> -abs(x) */
8006 (for sign (SIGNBIT)
8007  (for neeq (ne eq)
8008   (simplify
8009    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8010     (if (neeq == NE_EXPR)
8011      (abs @0)
8012      (negate (abs @0))))))
8014 (simplify
8015  /* signbit(x) -> 0 if x is nonnegative.  */
8016  (SIGNBIT tree_expr_nonnegative_p@0)
8017  { integer_zero_node; })
8019 (simplify
8020  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8021  (SIGNBIT @0)
8022  (if (!HONOR_SIGNED_ZEROS (@0))
8023   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8025 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8026 (for cmp (eq ne)
8027  (for op (plus minus)
8028       rop (minus plus)
8029   (simplify
8030    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8031    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8032         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8033         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8034         && !TYPE_SATURATING (TREE_TYPE (@0)))
8035     (with { tree res = int_const_binop (rop, @2, @1); }
8036      (if (TREE_OVERFLOW (res)
8037           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8038       { constant_boolean_node (cmp == NE_EXPR, type); }
8039       (if (single_use (@3))
8040        (cmp @0 { TREE_OVERFLOW (res)
8041                  ? drop_tree_overflow (res) : res; }))))))))
8042 (for cmp (lt le gt ge)
8043  (for op (plus minus)
8044       rop (minus plus)
8045   (simplify
8046    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8047    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8048         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8049     (with { tree res = int_const_binop (rop, @2, @1); }
8050      (if (TREE_OVERFLOW (res))
8051       {
8052         fold_overflow_warning (("assuming signed overflow does not occur "
8053                                 "when simplifying conditional to constant"),
8054                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8055         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8056         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8057         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8058                                   TYPE_SIGN (TREE_TYPE (@1)))
8059                         != (op == MINUS_EXPR);
8060         constant_boolean_node (less == ovf_high, type);
8061       }
8062       (if (single_use (@3))
8063        (with
8064         {
8065           fold_overflow_warning (("assuming signed overflow does not occur "
8066                                   "when changing X +- C1 cmp C2 to "
8067                                   "X cmp C2 -+ C1"),
8068                                  WARN_STRICT_OVERFLOW_COMPARISON);
8069         }
8070         (cmp @0 { res; })))))))))
8072 /* Canonicalizations of BIT_FIELD_REFs.  */
8074 (simplify
8075  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8076  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8078 (simplify
8079  (BIT_FIELD_REF (view_convert @0) @1 @2)
8080  (BIT_FIELD_REF @0 @1 @2))
8082 (simplify
8083  (BIT_FIELD_REF @0 @1 integer_zerop)
8084  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8085   (view_convert @0)))
8087 (simplify
8088  (BIT_FIELD_REF @0 @1 @2)
8089  (switch
8090   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8091        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8092    (switch
8093     (if (integer_zerop (@2))
8094      (view_convert (realpart @0)))
8095     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8096      (view_convert (imagpart @0)))))
8097   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8098        && INTEGRAL_TYPE_P (type)
8099        /* On GIMPLE this should only apply to register arguments.  */
8100        && (! GIMPLE || is_gimple_reg (@0))
8101        /* A bit-field-ref that referenced the full argument can be stripped.  */
8102        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8103             && integer_zerop (@2))
8104            /* Low-parts can be reduced to integral conversions.
8105               ???  The following doesn't work for PDP endian.  */
8106            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8107                /* But only do this after vectorization.  */
8108                && canonicalize_math_after_vectorization_p ()
8109                /* Don't even think about BITS_BIG_ENDIAN.  */
8110                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8111                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8112                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8113                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8114                                             - TYPE_PRECISION (type))
8115                                          : 0)) == 0)))
8116    (convert @0))))
8118 /* Simplify vector extracts.  */
8120 (simplify
8121  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8122  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8123       && tree_fits_uhwi_p (TYPE_SIZE (type))
8124       && ((tree_to_uhwi (TYPE_SIZE (type))
8125            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8126           || (VECTOR_TYPE_P (type)
8127               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8128                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8129   (with
8130    {
8131      tree ctor = (TREE_CODE (@0) == SSA_NAME
8132                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8133      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8134      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8135      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8136      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8137    }
8138    (if (n != 0
8139         && (idx % width) == 0
8140         && (n % width) == 0
8141         && known_le ((idx + n) / width,
8142                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8143     (with
8144      {
8145        idx = idx / width;
8146        n = n / width;
8147        /* Constructor elements can be subvectors.  */
8148        poly_uint64 k = 1;
8149        if (CONSTRUCTOR_NELTS (ctor) != 0)
8150          {
8151            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8152            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8153              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8154          }
8155        unsigned HOST_WIDE_INT elt, count, const_k;
8156      }
8157      (switch
8158       /* We keep an exact subset of the constructor elements.  */
8159       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8160        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8161         { build_zero_cst (type); }
8162         (if (count == 1)
8163          (if (elt < CONSTRUCTOR_NELTS (ctor))
8164           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8165           { build_zero_cst (type); })
8166          /* We don't want to emit new CTORs unless the old one goes away.
8167             ???  Eventually allow this if the CTOR ends up constant or
8168             uniform.  */
8169          (if (single_use (@0))
8170           (with
8171             {
8172               vec<constructor_elt, va_gc> *vals;
8173               vec_alloc (vals, count);
8174               bool constant_p = true;
8175               tree res;
8176               for (unsigned i = 0;
8177                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8178                 {
8179                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8180                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8181                   if (!CONSTANT_CLASS_P (e))
8182                     constant_p = false;
8183                 }
8184               tree evtype = (types_match (TREE_TYPE (type),
8185                                           TREE_TYPE (TREE_TYPE (ctor)))
8186                              ? type
8187                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8188                                                   count * k));
8189               /* We used to build a CTOR in the non-constant case here
8190                  but that's not a GIMPLE value.  We'd have to expose this
8191                  operation somehow so the code generation can properly
8192                  split it out to a separate stmt.  */
8193               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8194                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8195             }
8196             (if (res)
8197              (view_convert { res; })))))))
8198       /* The bitfield references a single constructor element.  */
8199       (if (k.is_constant (&const_k)
8200            && idx + n <= (idx / const_k + 1) * const_k)
8201        (switch
8202         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8203          { build_zero_cst (type); })
8204         (if (n == const_k)
8205          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8206         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8207                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8209 /* Simplify a bit extraction from a bit insertion for the cases with
8210    the inserted element fully covering the extraction or the insertion
8211    not touching the extraction.  */
8212 (simplify
8213  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8214  (with
8215   {
8216     unsigned HOST_WIDE_INT isize;
8217     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8218       isize = TYPE_PRECISION (TREE_TYPE (@1));
8219     else
8220       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8221   }
8222   (switch
8223    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8224          || type_has_mode_precision_p (TREE_TYPE (@1)))
8225         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8226         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8227                       wi::to_wide (@ipos) + isize))
8228     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8229                                                  wi::to_wide (@rpos)
8230                                                  - wi::to_wide (@ipos)); }))
8231    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8232         && compare_tree_int (@rsize, isize) == 0)
8233     (convert @1))
8234    (if (wi::geu_p (wi::to_wide (@ipos),
8235                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8236         || wi::geu_p (wi::to_wide (@rpos),
8237                       wi::to_wide (@ipos) + isize))
8238     (BIT_FIELD_REF @0 @rsize @rpos)))))
8240 /* Simplify vector inserts of other vector extracts to a permute.  */
8241 (simplify
8242  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8243  (if (VECTOR_TYPE_P (type)
8244       && types_match (@0, @1)
8245       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8246       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8247   (with
8248    {
8249      unsigned HOST_WIDE_INT elsz
8250        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8251      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8252      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8253      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8254      vec_perm_builder builder;
8255      builder.new_vector (nunits, nunits, 1);
8256      for (unsigned i = 0; i < nunits; ++i)
8257        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8258      vec_perm_indices sel (builder, 2, nunits);
8259    }
8260    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8261         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8262     (vec_perm @0 @1 { vec_perm_indices_to_tree
8263                         (build_vector_type (ssizetype, nunits), sel); })))))
8265 (if (canonicalize_math_after_vectorization_p ())
8266  (for fmas (FMA)
8267   (simplify
8268    (fmas:c (negate @0) @1 @2)
8269    (IFN_FNMA @0 @1 @2))
8270   (simplify
8271    (fmas @0 @1 (negate @2))
8272    (IFN_FMS @0 @1 @2))
8273   (simplify
8274    (fmas:c (negate @0) @1 (negate @2))
8275    (IFN_FNMS @0 @1 @2))
8276   (simplify
8277    (negate (fmas@3 @0 @1 @2))
8278    (if (single_use (@3))
8279     (IFN_FNMS @0 @1 @2))))
8281  (simplify
8282   (IFN_FMS:c (negate @0) @1 @2)
8283   (IFN_FNMS @0 @1 @2))
8284  (simplify
8285   (IFN_FMS @0 @1 (negate @2))
8286   (IFN_FMA @0 @1 @2))
8287  (simplify
8288   (IFN_FMS:c (negate @0) @1 (negate @2))
8289   (IFN_FNMA @0 @1 @2))
8290  (simplify
8291   (negate (IFN_FMS@3 @0 @1 @2))
8292    (if (single_use (@3))
8293     (IFN_FNMA @0 @1 @2)))
8295  (simplify
8296   (IFN_FNMA:c (negate @0) @1 @2)
8297   (IFN_FMA @0 @1 @2))
8298  (simplify
8299   (IFN_FNMA @0 @1 (negate @2))
8300   (IFN_FNMS @0 @1 @2))
8301  (simplify
8302   (IFN_FNMA:c (negate @0) @1 (negate @2))
8303   (IFN_FMS @0 @1 @2))
8304  (simplify
8305   (negate (IFN_FNMA@3 @0 @1 @2))
8306   (if (single_use (@3))
8307    (IFN_FMS @0 @1 @2)))
8309  (simplify
8310   (IFN_FNMS:c (negate @0) @1 @2)
8311   (IFN_FMS @0 @1 @2))
8312  (simplify
8313   (IFN_FNMS @0 @1 (negate @2))
8314   (IFN_FNMA @0 @1 @2))
8315  (simplify
8316   (IFN_FNMS:c (negate @0) @1 (negate @2))
8317   (IFN_FMA @0 @1 @2))
8318  (simplify
8319   (negate (IFN_FNMS@3 @0 @1 @2))
8320   (if (single_use (@3))
8321    (IFN_FMA @0 @1 @2))))
8323 /* CLZ simplifications.  */
8324 (for clz (CLZ)
8325  (for op (eq ne)
8326       cmp (lt ge)
8327   (simplify
8328    (op (clz:s@2 @0) INTEGER_CST@1)
8329    (if (integer_zerop (@1) && single_use (@2))
8330     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8331     (with { tree type0 = TREE_TYPE (@0);
8332             tree stype = signed_type_for (type0);
8333             HOST_WIDE_INT val = 0;
8334             /* Punt on hypothetical weird targets.  */
8335             if (clz == CFN_CLZ
8336                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8337                                               val) == 2
8338                 && val == 0)
8339               stype = NULL_TREE;
8340           }
8341      (if (stype)
8342       (cmp (convert:stype @0) { build_zero_cst (stype); })))
8343     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8344     (with { bool ok = true;
8345             HOST_WIDE_INT val = 0;
8346             tree type0 = TREE_TYPE (@0);
8347             /* Punt on hypothetical weird targets.  */
8348             if (clz == CFN_CLZ
8349                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8350                                               val) == 2
8351                 && val == TYPE_PRECISION (type0) - 1)
8352               ok = false;
8353           }
8354      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8355       (op @0 { build_one_cst (type0); })))))))
8357 /* CTZ simplifications.  */
8358 (for ctz (CTZ)
8359  (for op (ge gt le lt)
8360       cmp (eq eq ne ne)
8361   (simplify
8362    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8363    (op (ctz:s @0) INTEGER_CST@1)
8364     (with { bool ok = true;
8365             HOST_WIDE_INT val = 0;
8366             if (!tree_fits_shwi_p (@1))
8367               ok = false;
8368             else
8369               {
8370                 val = tree_to_shwi (@1);
8371                 /* Canonicalize to >= or <.  */
8372                 if (op == GT_EXPR || op == LE_EXPR)
8373                   {
8374                     if (val == HOST_WIDE_INT_MAX)
8375                       ok = false;
8376                     else
8377                       val++;
8378                   }
8379               }
8380             bool zero_res = false;
8381             HOST_WIDE_INT zero_val = 0;
8382             tree type0 = TREE_TYPE (@0);
8383             int prec = TYPE_PRECISION (type0);
8384             if (ctz == CFN_CTZ
8385                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8386                                               zero_val) == 2)
8387               zero_res = true;
8388           }
8389      (if (val <= 0)
8390       (if (ok && (!zero_res || zero_val >= val))
8391        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8392       (if (val >= prec)
8393        (if (ok && (!zero_res || zero_val < val))
8394         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8395        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8396         (cmp (bit_and @0 { wide_int_to_tree (type0,
8397                                              wi::mask (val, false, prec)); })
8398              { build_zero_cst (type0); })))))))
8399  (for op (eq ne)
8400   (simplify
8401    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8402    (op (ctz:s @0) INTEGER_CST@1)
8403     (with { bool zero_res = false;
8404             HOST_WIDE_INT zero_val = 0;
8405             tree type0 = TREE_TYPE (@0);
8406             int prec = TYPE_PRECISION (type0);
8407             if (ctz == CFN_CTZ
8408                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8409                                               zero_val) == 2)
8410               zero_res = true;
8411           }
8412      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8413       (if (!zero_res || zero_val != wi::to_widest (@1))
8414        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8415       (if (!zero_res || zero_val < 0 || zero_val >= prec)
8416        (op (bit_and @0 { wide_int_to_tree (type0,
8417                                            wi::mask (tree_to_uhwi (@1) + 1,
8418                                                      false, prec)); })
8419            { wide_int_to_tree (type0,
8420                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8421                                                  false, prec)); })))))))
8423 /* POPCOUNT simplifications.  */
8424 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8425 (simplify
8426   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8427   (if (INTEGRAL_TYPE_P (type)
8428        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8429     (POPCOUNT (bit_ior @0 @1))))
8431 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8432 (for popcount (POPCOUNT)
8433   (for cmp (le eq ne gt)
8434        rep (eq eq ne ne)
8435     (simplify
8436       (cmp (popcount @0) integer_zerop)
8437       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8439 /* popcount(bswap(x)) is popcount(x).  */
8440 (for popcount (POPCOUNT)
8441   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8442               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8443     (simplify
8444       (popcount (convert?@0 (bswap:s@1 @2)))
8445       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8446            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8447         (with { tree type0 = TREE_TYPE (@0);
8448                 tree type1 = TREE_TYPE (@1);
8449                 unsigned int prec0 = TYPE_PRECISION (type0);
8450                 unsigned int prec1 = TYPE_PRECISION (type1); }
8451           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8452             (popcount (convert:type0 (convert:type1 @2)))))))))
8454 /* popcount(rotate(X Y)) is popcount(X).  */
8455 (for popcount (POPCOUNT)
8456   (for rot (lrotate rrotate)
8457     (simplify
8458       (popcount (convert?@0 (rot:s@1 @2 @3)))
8459       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8460            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8461            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8462         (with { tree type0 = TREE_TYPE (@0);
8463                 tree type1 = TREE_TYPE (@1);
8464                 unsigned int prec0 = TYPE_PRECISION (type0);
8465                 unsigned int prec1 = TYPE_PRECISION (type1); }
8466           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8467             (popcount (convert:type0 @2))))))))
8469 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8470 (simplify
8471   (bit_and (POPCOUNT @0) integer_onep)
8472   (PARITY @0))
8474 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8475 (simplify
8476   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8477   (plus (POPCOUNT @0) (POPCOUNT @1)))
8479 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8480 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8481 (for popcount (POPCOUNT)
8482   (for log1 (bit_and bit_ior)
8483        log2 (bit_ior bit_and)
8484     (simplify
8485       (minus (plus:s (popcount:s @0) (popcount:s @1))
8486              (popcount:s (log1:cs @0 @1)))
8487       (popcount (log2 @0 @1)))
8488     (simplify
8489       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8490               (popcount:s @1))
8491       (popcount (log2 @0 @1)))))
8493 /* PARITY simplifications.  */
8494 /* parity(~X) is parity(X).  */
8495 (simplify
8496   (PARITY (bit_not @0))
8497   (PARITY @0))
8499 /* parity(bswap(x)) is parity(x).  */
8500 (for parity (PARITY)
8501   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8502               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8503     (simplify
8504       (parity (convert?@0 (bswap:s@1 @2)))
8505       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8506            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8507            && TYPE_PRECISION (TREE_TYPE (@0))
8508               >= TYPE_PRECISION (TREE_TYPE (@1)))
8509         (with { tree type0 = TREE_TYPE (@0);
8510                 tree type1 = TREE_TYPE (@1); }
8511           (parity (convert:type0 (convert:type1 @2))))))))
8513 /* parity(rotate(X Y)) is parity(X).  */
8514 (for parity (PARITY)
8515   (for rot (lrotate rrotate)
8516     (simplify
8517       (parity (convert?@0 (rot:s@1 @2 @3)))
8518       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8519            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8520            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8521            && TYPE_PRECISION (TREE_TYPE (@0))
8522               >= TYPE_PRECISION (TREE_TYPE (@1)))
8523         (with { tree type0 = TREE_TYPE (@0); }
8524           (parity (convert:type0 @2)))))))
8526 /* parity(X)^parity(Y) is parity(X^Y).  */
8527 (simplify
8528   (bit_xor (PARITY:s @0) (PARITY:s @1))
8529   (PARITY (bit_xor @0 @1)))
8531 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8532 (for func (POPCOUNT BSWAP FFS PARITY)
8533  (simplify
8534   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8535   @3))
8537 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8538    where CST is precision-1. */
8539 (for func (CLRSB)
8540  (simplify
8541   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8542   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8543    @4)))
8545 #if GIMPLE
8546 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8547 (for func (CLZ)
8548  (simplify
8549   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8550   (with { int val;
8551           internal_fn ifn = IFN_LAST;
8552           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8553               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8554                                             val) == 2)
8555             ifn = IFN_CLZ;
8556         }
8557    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8558     (IFN_CLZ @3)))))
8560 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8561 (for func (CTZ)
8562  (simplify
8563   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8564   (with { int val;
8565           internal_fn ifn = IFN_LAST;
8566           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8567               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8568                                             val) == 2)
8569             ifn = IFN_CTZ;
8570         }
8571    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8572     (IFN_CTZ @3)))))
8573 #endif
8575 /* Common POPCOUNT/PARITY simplifications.  */
8576 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8577 (for pfun (POPCOUNT PARITY)
8578   (simplify
8579     (pfun @0)
8580     (if (INTEGRAL_TYPE_P (type))
8581      (with { wide_int nz = tree_nonzero_bits (@0); }
8582        (switch
8583          (if (nz == 1)
8584            (convert @0))
8585          (if (wi::popcount (nz) == 1)
8586            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8587              (convert (rshift:utype (convert:utype @0)
8588                                     { build_int_cst (integer_type_node,
8589                                                      wi::ctz (nz)); })))))))))
8591 #if GIMPLE
8592 /* 64- and 32-bits branchless implementations of popcount are detected:
8594    int popcount64c (uint64_t x)
8595    {
8596      x -= (x >> 1) & 0x5555555555555555ULL;
8597      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8598      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8599      return (x * 0x0101010101010101ULL) >> 56;
8600    }
8602    int popcount32c (uint32_t x)
8603    {
8604      x -= (x >> 1) & 0x55555555;
8605      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8606      x = (x + (x >> 4)) & 0x0f0f0f0f;
8607      return (x * 0x01010101) >> 24;
8608    }  */
8609 (simplify
8610  (rshift
8611   (mult
8612    (bit_and
8613     (plus:c
8614      (rshift @8 INTEGER_CST@5)
8615       (plus:c@8
8616        (bit_and @6 INTEGER_CST@7)
8617         (bit_and
8618          (rshift
8619           (minus@6 @0
8620            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8621           INTEGER_CST@10)
8622          INTEGER_CST@9)))
8623     INTEGER_CST@3)
8624    INTEGER_CST@2)
8625   INTEGER_CST@1)
8626   /* Check constants and optab.  */
8627   (with { unsigned prec = TYPE_PRECISION (type);
8628           int shift = (64 - prec) & 63;
8629           unsigned HOST_WIDE_INT c1
8630             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8631           unsigned HOST_WIDE_INT c2
8632             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8633           unsigned HOST_WIDE_INT c3
8634             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8635           unsigned HOST_WIDE_INT c4
8636             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8637    }
8638    (if (prec >= 16
8639         && prec <= 64
8640         && pow2p_hwi (prec)
8641         && TYPE_UNSIGNED (type)
8642         && integer_onep (@4)
8643         && wi::to_widest (@10) == 2
8644         && wi::to_widest (@5) == 4
8645         && wi::to_widest (@1) == prec - 8
8646         && tree_to_uhwi (@2) == c1
8647         && tree_to_uhwi (@3) == c2
8648         && tree_to_uhwi (@9) == c3
8649         && tree_to_uhwi (@7) == c3
8650         && tree_to_uhwi (@11) == c4)
8651     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8652                                          OPTIMIZE_FOR_BOTH))
8653      (convert (IFN_POPCOUNT:type @0))
8654      /* Try to do popcount in two halves.  PREC must be at least
8655         five bits for this to work without extension before adding.  */
8656      (with {
8657        tree half_type = NULL_TREE;
8658        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8659        int half_prec = 8;
8660        if (m.exists ()
8661            && m.require () != TYPE_MODE (type))
8662          {
8663            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8664            half_type = build_nonstandard_integer_type (half_prec, 1);
8665          }
8666        gcc_assert (half_prec > 2);
8667       }
8668       (if (half_type != NULL_TREE
8669            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8670                                               OPTIMIZE_FOR_BOTH))
8671        (convert (plus
8672          (IFN_POPCOUNT:half_type (convert @0))
8673          (IFN_POPCOUNT:half_type (convert (rshift @0
8674             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8676 /* __builtin_ffs needs to deal on many targets with the possible zero
8677    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8678    should lead to better code.  */
8679 (simplify
8680  (FFS tree_expr_nonzero_p@0)
8681  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8682       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8683                                          OPTIMIZE_FOR_SPEED))
8684   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8685    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8686 #endif
8688 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8689           BUILT_IN_FFSIMAX)
8690  /* __builtin_ffs (X) == 0 -> X == 0.
8691     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8692  (for cmp (eq ne)
8693   (simplify
8694    (cmp (ffs@2 @0) INTEGER_CST@1)
8695     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8696      (switch
8697       (if (integer_zerop (@1))
8698        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8699       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8700        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8701       (if (single_use (@2))
8702        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8703                                             wi::mask (tree_to_uhwi (@1),
8704                                                       false, prec)); })
8705             { wide_int_to_tree (TREE_TYPE (@0),
8706                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8707                                                   false, prec)); }))))))
8709  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8710  (for cmp (gt le)
8711       cmp2 (ne eq)
8712       cmp3 (eq ne)
8713       bit_op (bit_and bit_ior)
8714   (simplify
8715    (cmp (ffs@2 @0) INTEGER_CST@1)
8716     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8717      (switch
8718       (if (integer_zerop (@1))
8719        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8720       (if (tree_int_cst_sgn (@1) < 0)
8721        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8722       (if (wi::to_widest (@1) >= prec)
8723        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8724       (if (wi::to_widest (@1) == prec - 1)
8725        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8726                                     wi::shifted_mask (prec - 1, 1,
8727                                                       false, prec)); }))
8728       (if (single_use (@2))
8729        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8730                (cmp3 (bit_and @0
8731                               { wide_int_to_tree (TREE_TYPE (@0),
8732                                                   wi::mask (tree_to_uhwi (@1),
8733                                                   false, prec)); })
8734                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8736 #if GIMPLE
8738 /* Simplify:
8739      a = op a1
8740      r = cond ? a : b
8741      --> r = .COND_FN (cond, a, b)
8742 and,
8743     a = op a1
8744     r = cond ? b : a
8745     --> r = .COND_FN (~cond, b, a).  */
8747 (for uncond_op (UNCOND_UNARY)
8748      cond_op (COND_UNARY)
8749  (simplify
8750   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8751    (with { tree op_type = TREE_TYPE (@3); }
8752     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8753         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8754      (cond_op @0 @1 @2))))
8755  (simplify
8756   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8757    (with { tree op_type = TREE_TYPE (@3); }
8758     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8759         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8760      (cond_op (bit_not @0) @2 @1)))))
8762 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
8763 (simplify
8764  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8765  (if (canonicalize_math_after_vectorization_p ()
8766       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8767       && is_truth_type_for (type, TREE_TYPE (@0)))
8768  (if (integer_all_onesp (@1) && integer_zerop (@2))
8769   (IFN_COND_NOT @0 @3 @3))
8770   (if (integer_all_onesp (@2) && integer_zerop (@1))
8771    (IFN_COND_NOT (bit_not @0) @3 @3))))
8773 /* Simplify:
8775      a = a1 op a2
8776      r = c ? a : b;
8778    to:
8780      r = c ? a1 op a2 : b;
8782    if the target can do it in one go.  This makes the operation conditional
8783    on c, so could drop potentially-trapping arithmetic, but that's a valid
8784    simplification if the result of the operation isn't needed.
8786    Avoid speculatively generating a stand-alone vector comparison
8787    on targets that might not support them.  Any target implementing
8788    conditional internal functions must support the same comparisons
8789    inside and outside a VEC_COND_EXPR.  */
8791 (for uncond_op (UNCOND_BINARY)
8792      cond_op (COND_BINARY)
8793  (simplify
8794   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8795   (with { tree op_type = TREE_TYPE (@4); }
8796    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8797         && is_truth_type_for (op_type, TREE_TYPE (@0))
8798         && single_use (@4))
8799     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8800  (simplify
8801   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8802   (with { tree op_type = TREE_TYPE (@4); }
8803    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8804         && is_truth_type_for (op_type, TREE_TYPE (@0))
8805         && single_use (@4))
8806     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8808 /* Same for ternary operations.  */
8809 (for uncond_op (UNCOND_TERNARY)
8810      cond_op (COND_TERNARY)
8811  (simplify
8812   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8813   (with { tree op_type = TREE_TYPE (@5); }
8814    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8815         && is_truth_type_for (op_type, TREE_TYPE (@0))
8816         && single_use (@5))
8817     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8818  (simplify
8819   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8820   (with { tree op_type = TREE_TYPE (@5); }
8821    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8822         && is_truth_type_for (op_type, TREE_TYPE (@0))
8823         && single_use (@5))
8824     (view_convert (cond_op (bit_not @0) @2 @3 @4
8825                   (view_convert:op_type @1)))))))
8826 #endif
8828 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8829    "else" value of an IFN_COND_*.  */
8830 (for cond_op (COND_BINARY)
8831  (simplify
8832   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8833   (with { tree op_type = TREE_TYPE (@3); }
8834    (if (element_precision (type) == element_precision (op_type))
8835     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8836  (simplify
8837   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8838   (with { tree op_type = TREE_TYPE (@5); }
8839    (if (inverse_conditions_p (@0, @2)
8840         && element_precision (type) == element_precision (op_type))
8841     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8843 /* Same for ternary operations.  */
8844 (for cond_op (COND_TERNARY)
8845  (simplify
8846   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8847   (with { tree op_type = TREE_TYPE (@4); }
8848    (if (element_precision (type) == element_precision (op_type))
8849     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8850  (simplify
8851   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8852   (with { tree op_type = TREE_TYPE (@6); }
8853    (if (inverse_conditions_p (@0, @2)
8854         && element_precision (type) == element_precision (op_type))
8855     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8857 /* Detect simplication for a conditional reduction where
8859    a = mask1 ? b : 0
8860    c = mask2 ? d + a : d
8862    is turned into
8864    c = mask1 && mask2 ? d + b : d.  */
8865 (simplify
8866   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8867    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8869 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8870    expressions like:
8872    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8873    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8875    If pointers are known not to wrap, B checks whether @1 bytes starting
8876    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8877    bytes.  A is more efficiently tested as:
8879    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8881    The equivalent expression for B is given by replacing @1 with @1 - 1:
8883    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8885    @0 and @2 can be swapped in both expressions without changing the result.
8887    The folds rely on sizetype's being unsigned (which is always true)
8888    and on its being the same width as the pointer (which we have to check).
8890    The fold replaces two pointer_plus expressions, two comparisons and
8891    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8892    the best case it's a saving of two operations.  The A fold retains one
8893    of the original pointer_pluses, so is a win even if both pointer_pluses
8894    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8895    used elsewhere, since all we end up doing is replacing a comparison with
8896    a pointer_plus.  We do still apply the fold under those circumstances
8897    though, in case applying it to other conditions eventually makes one of the
8898    pointer_pluses dead.  */
8899 (for ior (truth_orif truth_or bit_ior)
8900  (for cmp (le lt)
8901   (simplify
8902    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8903         (cmp:cs (pointer_plus@4 @2 @1) @0))
8904    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8905         && TYPE_OVERFLOW_WRAPS (sizetype)
8906         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8907     /* Calculate the rhs constant.  */
8908     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8909             offset_int rhs = off * 2; }
8910      /* Always fails for negative values.  */
8911      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8912       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8913          pick a canonical order.  This increases the chances of using the
8914          same pointer_plus in multiple checks.  */
8915       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8916               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8917        (if (cmp == LT_EXPR)
8918         (gt (convert:sizetype
8919              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8920                                      { swap_p ? @0 : @2; }))
8921             { rhs_tree; })
8922         (gt (convert:sizetype
8923              (pointer_diff:ssizetype
8924               (pointer_plus { swap_p ? @2 : @0; }
8925                             { wide_int_to_tree (sizetype, off); })
8926               { swap_p ? @0 : @2; }))
8927             { rhs_tree; })))))))))
8929 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8930    element of @1.  */
8931 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8932  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8933   (with { int i = single_nonzero_element (@1); }
8934    (if (i >= 0)
8935     (with { tree elt = vector_cst_elt (@1, i);
8936             tree elt_type = TREE_TYPE (elt);
8937             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8938             tree size = bitsize_int (elt_bits);
8939             tree pos = bitsize_int (elt_bits * i); }
8940      (view_convert
8941       (bit_and:elt_type
8942        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8943        { elt; })))))))
8945 /* Fold reduction of a single nonzero element constructor.  */
8946 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8947   (simplify (reduc (CONSTRUCTOR@0))
8948     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8949                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8950             tree elt = ctor_single_nonzero_element (ctor); }
8951       (if (elt
8952            && !HONOR_SNANS (type)
8953            && !HONOR_SIGNED_ZEROS (type))
8954         { elt; }))))
8956 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8957 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8958             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8959      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8960   (simplify (reduc (op @0 VECTOR_CST@1))
8961     (op (reduc:type @0) (reduc:type @1))))
8963 /* Simplify vector floating point operations of alternating sub/add pairs
8964    into using an fneg of a wider element type followed by a normal add.
8965    under IEEE 754 the fneg of the wider type will negate every even entry
8966    and when doing an add we get a sub of the even and add of every odd
8967    elements.  */
8968 (for plusminus (plus minus)
8969      minusplus (minus plus)
8970  (simplify
8971   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8972    (if (!VECTOR_INTEGER_TYPE_P (type)
8973         && !FLOAT_WORDS_BIG_ENDIAN
8974         /* plus is commutative, while minus is not, so :c can't be used.
8975            Do equality comparisons by hand and at the end pick the operands
8976            from the minus.  */
8977         && (operand_equal_p (@0, @2, 0)
8978             ? operand_equal_p (@1, @3, 0)
8979             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8980    (with
8981     {
8982       /* Build a vector of integers from the tree mask.  */
8983       vec_perm_builder builder;
8984     }
8985     (if (tree_to_vec_perm_builder (&builder, @4))
8986      (with
8987       {
8988         /* Create a vec_perm_indices for the integer vector.  */
8989         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8990         vec_perm_indices sel (builder, 2, nelts);
8991         machine_mode vec_mode = TYPE_MODE (type);
8992         machine_mode wide_mode;
8993         scalar_mode wide_elt_mode;
8994         poly_uint64 wide_nunits;
8995         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8996       }
8997       (if (VECTOR_MODE_P (vec_mode)
8998            && sel.series_p (0, 2, 0, 2)
8999            && sel.series_p (1, 2, nelts + 1, 2)
9000            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9001            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9002            && related_vector_mode (vec_mode, wide_elt_mode,
9003                                    wide_nunits).exists (&wide_mode))
9004        (with
9005         {
9006           tree stype
9007             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9008                                               TYPE_UNSIGNED (type));
9009           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9011           /* The format has to be a non-extended ieee format.  */
9012           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9013           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9014         }
9015         (if (TYPE_MODE (stype) != BLKmode
9016              && VECTOR_TYPE_P (ntype)
9017              && fmt_old != NULL
9018              && fmt_new != NULL)
9019          (with
9020           {
9021             /* If the target doesn't support v1xx vectors, try using
9022                scalar mode xx instead.  */
9023             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9024                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9025               ntype = stype;
9026           }
9027           (if (fmt_new->signbit_rw
9028                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9029                && fmt_new->signbit_rw == fmt_new->signbit_ro
9030                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9031                                                  TYPE_MODE (type), ALL_REGS)
9032                && ((optimize_vectors_before_lowering_p ()
9033                     && VECTOR_TYPE_P (ntype))
9034                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9035            (if (plusminus == PLUS_EXPR)
9036             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9037             (minus @0 (view_convert:type
9038                         (negate (view_convert:ntype @1))))))))))))))))
9040 (simplify
9041  (vec_perm @0 @1 VECTOR_CST@2)
9042  (with
9043   {
9044     tree op0 = @0, op1 = @1, op2 = @2;
9045     machine_mode result_mode = TYPE_MODE (type);
9046     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9048     /* Build a vector of integers from the tree mask.  */
9049     vec_perm_builder builder;
9050   }
9051   (if (tree_to_vec_perm_builder (&builder, op2))
9052    (with
9053     {
9054       /* Create a vec_perm_indices for the integer vector.  */
9055       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9056       bool single_arg = (op0 == op1);
9057       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9058     }
9059     (if (sel.series_p (0, 1, 0, 1))
9060      { op0; }
9061      (if (sel.series_p (0, 1, nelts, 1))
9062       { op1; }
9063       (with
9064        {
9065          if (!single_arg)
9066            {
9067              if (sel.all_from_input_p (0))
9068                op1 = op0;
9069              else if (sel.all_from_input_p (1))
9070                {
9071                  op0 = op1;
9072                  sel.rotate_inputs (1);
9073                }
9074              else if (known_ge (poly_uint64 (sel[0]), nelts))
9075                {
9076                  std::swap (op0, op1);
9077                  sel.rotate_inputs (1);
9078                }
9079            }
9080          gassign *def;
9081          tree cop0 = op0, cop1 = op1;
9082          if (TREE_CODE (op0) == SSA_NAME
9083              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9084              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9085            cop0 = gimple_assign_rhs1 (def);
9086          if (TREE_CODE (op1) == SSA_NAME
9087              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9088              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9089            cop1 = gimple_assign_rhs1 (def);
9090          tree t;
9091        }
9092        (if ((TREE_CODE (cop0) == VECTOR_CST
9093              || TREE_CODE (cop0) == CONSTRUCTOR)
9094             && (TREE_CODE (cop1) == VECTOR_CST
9095                 || TREE_CODE (cop1) == CONSTRUCTOR)
9096             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9097         { t; }
9098         (with
9099          {
9100            bool changed = (op0 == op1 && !single_arg);
9101            tree ins = NULL_TREE;
9102            unsigned at = 0;
9104            /* See if the permutation is performing a single element
9105               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9106               in that case.  But only if the vector mode is supported,
9107               otherwise this is invalid GIMPLE.  */
9108            if (op_mode != BLKmode
9109                && (TREE_CODE (cop0) == VECTOR_CST
9110                    || TREE_CODE (cop0) == CONSTRUCTOR
9111                    || TREE_CODE (cop1) == VECTOR_CST
9112                    || TREE_CODE (cop1) == CONSTRUCTOR))
9113              {
9114                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9115                if (insert_first_p)
9116                  {
9117                    /* After canonicalizing the first elt to come from the
9118                       first vector we only can insert the first elt from
9119                       the first vector.  */
9120                    at = 0;
9121                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9122                      op0 = op1;
9123                  }
9124                /* The above can fail for two-element vectors which always
9125                   appear to insert the first element, so try inserting
9126                   into the second lane as well.  For more than two
9127                   elements that's wasted time.  */
9128                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9129                  {
9130                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9131                    for (at = 0; at < encoded_nelts; ++at)
9132                      if (maybe_ne (sel[at], at))
9133                        break;
9134                    if (at < encoded_nelts
9135                        && (known_eq (at + 1, nelts)
9136                            || sel.series_p (at + 1, 1, at + 1, 1)))
9137                      {
9138                        if (known_lt (poly_uint64 (sel[at]), nelts))
9139                          ins = fold_read_from_vector (cop0, sel[at]);
9140                        else
9141                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9142                      }
9143                  }
9144              }
9146            /* Generate a canonical form of the selector.  */
9147            if (!ins && sel.encoding () != builder)
9148              {
9149                /* Some targets are deficient and fail to expand a single
9150                   argument permutation while still allowing an equivalent
9151                   2-argument version.  */
9152                tree oldop2 = op2;
9153                if (sel.ninputs () == 2
9154                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9155                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9156                else
9157                  {
9158                    vec_perm_indices sel2 (builder, 2, nelts);
9159                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9160                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9161                    else
9162                      /* Not directly supported with either encoding,
9163                         so use the preferred form.  */
9164                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9165                  }
9166                if (!operand_equal_p (op2, oldop2, 0))
9167                  changed = true;
9168              }
9169          }
9170          (if (ins)
9171           (bit_insert { op0; } { ins; }
9172            { bitsize_int (at * vector_element_bits (type)); })
9173           (if (changed)
9174            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9176 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9178 (match vec_same_elem_p
9179  (vec_duplicate @0))
9181 (match vec_same_elem_p
9182  CONSTRUCTOR@0
9183  (if (TREE_CODE (@0) == SSA_NAME
9184       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9186 (match vec_same_elem_p
9187  @0
9188  (if (uniform_vector_p (@0))))
9191 (simplify
9192  (vec_perm vec_same_elem_p@0 @0 @1)
9193  (if (types_match (type, TREE_TYPE (@0)))
9194   @0
9195   (with
9196    {
9197      tree elem = uniform_vector_p (@0);
9198    }
9199    (if (elem)
9200     { build_vector_from_val (type, elem); }))))
9202 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9203 (simplify
9204  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9205  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9206   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9207 (simplify
9208  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9209  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9210   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9213 /* Merge
9214      c = VEC_PERM_EXPR <a, b, VCST0>;
9215      d = VEC_PERM_EXPR <c, c, VCST1>;
9216    to
9217      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9219 (simplify
9220  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9221  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9222   (with
9223    {
9224      machine_mode result_mode = TYPE_MODE (type);
9225      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9226      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9227      vec_perm_builder builder0;
9228      vec_perm_builder builder1;
9229      vec_perm_builder builder2 (nelts, nelts, 1);
9230    }
9231    (if (tree_to_vec_perm_builder (&builder0, @3)
9232         && tree_to_vec_perm_builder (&builder1, @4))
9233     (with
9234      {
9235        vec_perm_indices sel0 (builder0, 2, nelts);
9236        vec_perm_indices sel1 (builder1, 1, nelts);
9238        for (int i = 0; i < nelts; i++)
9239          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9241        vec_perm_indices sel2 (builder2, 2, nelts);
9243        tree op0 = NULL_TREE;
9244        /* If the new VEC_PERM_EXPR can't be handled but both
9245           original VEC_PERM_EXPRs can, punt.
9246           If one or both of the original VEC_PERM_EXPRs can't be
9247           handled and the new one can't be either, don't increase
9248           number of VEC_PERM_EXPRs that can't be handled.  */
9249        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9250            || (single_use (@0)
9251                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9252                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9253                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9254          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9255      }
9256      (if (op0)
9257       (vec_perm @1 @2 { op0; })))))))
9259 /* Merge
9260      c = VEC_PERM_EXPR <a, b, VCST0>;
9261      d = VEC_PERM_EXPR <x, c, VCST1>;
9262    to
9263      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9264    when all elements from a or b are replaced by the later
9265    permutation.  */
9267 (simplify
9268  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9269  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9270   (with
9271    {
9272      machine_mode result_mode = TYPE_MODE (type);
9273      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9274      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9275      vec_perm_builder builder0;
9276      vec_perm_builder builder1;
9277      vec_perm_builder builder2 (nelts, nelts, 2);
9278    }
9279    (if (tree_to_vec_perm_builder (&builder0, @3)
9280         && tree_to_vec_perm_builder (&builder1, @4))
9281     (with
9282      {
9283        vec_perm_indices sel0 (builder0, 2, nelts);
9284        vec_perm_indices sel1 (builder1, 2, nelts);
9285        bool use_1 = false, use_2 = false;
9287        for (int i = 0; i < nelts; i++)
9288          {
9289            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9290              builder2.quick_push (sel1[i]);
9291            else
9292              {
9293                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9294                                     .to_constant ()];
9295                if (known_lt (j, sel0.nelts_per_input ()))
9296                  use_1 = true;
9297                else
9298                  {
9299                    use_2 = true;
9300                    j -= sel0.nelts_per_input ();
9301                  }
9302                builder2.quick_push (j + sel1.nelts_per_input ());
9303              }
9304          }
9305      }
9306      (if (use_1 ^ use_2)
9307       (with
9308        {
9309          vec_perm_indices sel2 (builder2, 2, nelts);
9310          tree op0 = NULL_TREE;
9311          /* If the new VEC_PERM_EXPR can't be handled but both
9312             original VEC_PERM_EXPRs can, punt.
9313             If one or both of the original VEC_PERM_EXPRs can't be
9314             handled and the new one can't be either, don't increase
9315             number of VEC_PERM_EXPRs that can't be handled.  */
9316          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9317              || (single_use (@0)
9318                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9319                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9320                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9321            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9322        }
9323        (if (op0)
9324         (switch
9325          (if (use_1)
9326           (vec_perm @5 @1 { op0; }))
9327          (if (use_2)
9328           (vec_perm @5 @2 { op0; })))))))))))
9330 /* And the case with swapped outer permute sources.  */
9332 (simplify
9333  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9334  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9335   (with
9336    {
9337      machine_mode result_mode = TYPE_MODE (type);
9338      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9339      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9340      vec_perm_builder builder0;
9341      vec_perm_builder builder1;
9342      vec_perm_builder builder2 (nelts, nelts, 2);
9343    }
9344    (if (tree_to_vec_perm_builder (&builder0, @3)
9345         && tree_to_vec_perm_builder (&builder1, @4))
9346     (with
9347      {
9348        vec_perm_indices sel0 (builder0, 2, nelts);
9349        vec_perm_indices sel1 (builder1, 2, nelts);
9350        bool use_1 = false, use_2 = false;
9352        for (int i = 0; i < nelts; i++)
9353          {
9354            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9355              builder2.quick_push (sel1[i]);
9356            else
9357              {
9358                poly_uint64 j = sel0[sel1[i].to_constant ()];
9359                if (known_lt (j, sel0.nelts_per_input ()))
9360                  use_1 = true;
9361                else
9362                  {
9363                    use_2 = true;
9364                    j -= sel0.nelts_per_input ();
9365                  }
9366                builder2.quick_push (j);
9367              }
9368          }
9369      }
9370      (if (use_1 ^ use_2)
9371       (with
9372        {
9373          vec_perm_indices sel2 (builder2, 2, nelts);
9374          tree op0 = NULL_TREE;
9375          /* If the new VEC_PERM_EXPR can't be handled but both
9376             original VEC_PERM_EXPRs can, punt.
9377             If one or both of the original VEC_PERM_EXPRs can't be
9378             handled and the new one can't be either, don't increase
9379             number of VEC_PERM_EXPRs that can't be handled.  */
9380          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9381              || (single_use (@0)
9382                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9383                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9384                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9385            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9386        }
9387        (if (op0)
9388         (switch
9389          (if (use_1)
9390           (vec_perm @1 @5 { op0; }))
9391          (if (use_2)
9392           (vec_perm @2 @5 { op0; })))))))))))
9395 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9396    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9397    constant which when multiplied by a power of 2 contains a unique value
9398    in the top 5 or 6 bits.  This is then indexed into a table which maps it
9399    to the number of trailing zeroes.  */
9400 (match (ctz_table_index @1 @2 @3)
9401   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9403 (match (cond_expr_convert_p @0 @2 @3 @6)
9404  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9405   (if (INTEGRAL_TYPE_P (type)
9406        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9407        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9408        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9409        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9410        && TYPE_PRECISION (TREE_TYPE (@0))
9411           == TYPE_PRECISION (TREE_TYPE (@2))
9412        && TYPE_PRECISION (TREE_TYPE (@0))
9413           == TYPE_PRECISION (TREE_TYPE (@3))
9414        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9415           signess when convert is truncation, but not ok for extension since
9416           it's sign_extend vs zero_extend.  */
9417        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9418            || (TYPE_UNSIGNED (TREE_TYPE (@2))
9419                == TYPE_UNSIGNED (TREE_TYPE (@3))))
9420        && single_use (@4)
9421        && single_use (@5))))
9423 (for bit_op (bit_and bit_ior bit_xor)
9424  (match (bitwise_induction_p @0 @2 @3)
9425   (bit_op:c
9426    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9427    @3)))
9429 (match (bitwise_induction_p @0 @2 @3)
9430  (bit_not
9431   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9433 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
9434    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
9435 (simplify
9436   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9437   (with { auto i = wi::neg (wi::to_wide (@2)); }
9438   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
9439     (if (wi::popcount (i) == 1
9440          && (wi::to_wide (@1)) == (i - 1))
9441       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9442         (bit_and @0 @1)
9443       (cond (le @0 @1) @0 (bit_and @0 @1))))))
9445 /* -x & 1 -> x & 1.  */
9446 (simplify 
9447  (bit_and (negate @0) integer_onep@1)
9448  (if (!TYPE_OVERFLOW_SANITIZED (type))
9449   (bit_and @0 @1)))
9451 /* `-a` is just `a` if the type is 1bit wide or when converting
9452    to a 1bit type; similar to the above transformation of `(-x)&1`.
9453    This is used mostly with the transformation of
9454    `a ? ~b : b` into `(-a)^b`.
9455    It also can show up with bitfields.  */
9456 (simplify
9457  (convert? (negate @0))
9458  (if (INTEGRAL_TYPE_P (type)
9459       && TYPE_PRECISION (type) == 1
9460       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9461   (convert @0)))
9463 /* Optimize
9464    c1 = VEC_PERM_EXPR (a, a, mask)
9465    c2 = VEC_PERM_EXPR (b, b, mask)
9466    c3 = c1 op c2
9467    -->
9468    c = a op b
9469    c3 = VEC_PERM_EXPR (c, c, mask)
9470    For all integer non-div operations.  */
9471 (for op (plus minus mult bit_and bit_ior bit_xor
9472          lshift rshift)
9473  (simplify
9474   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9475    (if (VECTOR_INTEGER_TYPE_P (type))
9476     (vec_perm (op@3 @0 @1) @3 @2))))
9478 /* Similar for float arithmetic when permutation constant covers
9479    all vector elements.  */
9480 (for op (plus minus mult)
9481  (simplify
9482   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9483    (if (VECTOR_FLOAT_TYPE_P (type)
9484         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9485     (with
9486      {
9487        tree perm_cst = @2;
9488        vec_perm_builder builder;
9489        bool full_perm_p = false;
9490        if (tree_to_vec_perm_builder (&builder, perm_cst))
9491          {
9492            unsigned HOST_WIDE_INT nelts;
9494            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9495            /* Create a vec_perm_indices for the VECTOR_CST.  */
9496            vec_perm_indices sel (builder, 1, nelts);
9498            /* Check if perm indices covers all vector elements.  */
9499            if (sel.encoding ().encoded_full_vector_p ())
9500              {
9501                auto_sbitmap seen (nelts);
9502                bitmap_clear (seen);
9504                unsigned HOST_WIDE_INT count = 0, i;
9506                for (i = 0; i < nelts; i++)
9507                  {
9508                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9509                     break;
9510                    count++;
9511                  }
9512                full_perm_p = count == nelts;
9513              }
9514          }
9515       }
9516       (if (full_perm_p)
9517         (vec_perm (op@3 @0 @1) @3 @2))))))