MATCH: Remove redundant pattern for `(x | y) & ~x`
[official-gcc.git] / gcc / match.pd
blob0076392c522b9c2d42e982d3b448c2ba59754e4d
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2023 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
91 /* Binary operations and their associated IFN_COND_* function.  */
92 (define_operator_list UNCOND_BINARY
93   plus minus
94   mult trunc_div trunc_mod rdiv
95   min max
96   IFN_FMIN IFN_FMAX
97   bit_and bit_ior bit_xor
98   lshift rshift)
99 (define_operator_list COND_BINARY
100   IFN_COND_ADD IFN_COND_SUB
101   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102   IFN_COND_MIN IFN_COND_MAX
103   IFN_COND_FMIN IFN_COND_FMAX
104   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105   IFN_COND_SHL IFN_COND_SHR)
107 /* Same for ternary operations.  */
108 (define_operator_list UNCOND_TERNARY
109   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117   BUILT_IN_ATOMIC_FETCH_OR_16
118   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120   BUILT_IN_ATOMIC_FETCH_XOR_16
121   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123   BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128   BUILT_IN_SYNC_FETCH_AND_OR_16
129   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131   BUILT_IN_SYNC_FETCH_AND_XOR_16
132   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134   BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*.  */
136 (define_operator_list ATOMIC_FETCH_AND_N
137   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139   BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*.  */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144   BUILT_IN_SYNC_FETCH_AND_AND_16)
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147    plus conditionalize on tree_nop_conversion_p conversions.  */
148 (match (nop_convert @0)
149  (convert @0)
150  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152  (view_convert @0)
153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
158 #if GIMPLE
159 /* These are used by gimple_bitwise_inverted_equal_p to simplify
160    detection of BIT_NOT and comparisons. */
161 (match (bit_not_with_nop @0)
162  (bit_not @0))
163 (match (bit_not_with_nop @0)
164  (convert (bit_not @0))
165  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
166 (for cmp (tcc_comparison)
167  (match (maybe_cmp @0)
168   (cmp@0 @1 @2))
169  (match (maybe_cmp @0)
170   (convert (cmp@0 @1 @2))
171    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
173 #endif
175 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
176    ABSU_EXPR returns unsigned absolute value of the operand and the operand
177    of the ABSU_EXPR will have the corresponding signed type.  */
178 (simplify (abs (convert @0))
179  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
180       && !TYPE_UNSIGNED (TREE_TYPE (@0))
181       && element_precision (type) > element_precision (TREE_TYPE (@0)))
182   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
183    (convert (absu:utype @0)))))
185 #if GIMPLE
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
187 (simplify
188  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
189  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
190       && !TYPE_UNSIGNED (TREE_TYPE (@0))
191       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
192   (abs @0)))
193 #endif
195 /* Simplifications of operations with one constant operand and
196    simplifications to constants or single values.  */
198 (for op (plus pointer_plus minus bit_ior bit_xor)
199   (simplify
200     (op @0 integer_zerop)
201     (non_lvalue @0)))
203 /* 0 +p index -> (type)index */
204 (simplify
205  (pointer_plus integer_zerop @1)
206  (non_lvalue (convert @1)))
208 /* ptr - 0 -> (type)ptr */
209 (simplify
210  (pointer_diff @0 integer_zerop)
211  (convert @0))
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214    Likewise if the operands are reversed.  */
215 (simplify
216  (plus:c @0 real_zerop@1)
217  (if (fold_real_zero_addition_p (type, @0, @1, 0))
218   (non_lvalue @0)))
220 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
221 (simplify
222  (minus @0 real_zerop@1)
223  (if (fold_real_zero_addition_p (type, @0, @1, 1))
224   (non_lvalue @0)))
226 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
227    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
228    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
229    if not -frounding-math.  For sNaNs the first operation would raise
230    exceptions but turn the result into qNan, so the second operation
231    would not raise it.   */
232 (for inner_op (plus minus)
233  (for outer_op (plus minus)
234   (simplify
235    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
236     (if (real_zerop (@1)
237          && real_zerop (@2)
238          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
239      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
240                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
241              bool outer_plus
242                = ((outer_op == PLUS_EXPR)
243                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244       (if (outer_plus && !inner_plus)
245        (outer_op @0 @2)
246        @3))))))
248 /* Simplify x - x.
249    This is unsafe for certain floats even in non-IEEE formats.
250    In IEEE, it is unsafe because it does wrong for NaNs.
251    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
252    Also note that operand_equal_p is always false if an operand
253    is volatile.  */
254 (simplify
255  (minus @0 @0)
256  (if (!FLOAT_TYPE_P (type)
257       || (!tree_expr_maybe_nan_p (@0)
258           && !tree_expr_maybe_infinite_p (@0)
259           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
260               || !HONOR_SIGNED_ZEROS (type))))
261   { build_zero_cst (type); }))
262 (simplify
263  (pointer_diff @@0 @0)
264  { build_zero_cst (type); })
266 (simplify
267  (mult @0 integer_zerop@1)
268  @1)
270 /* -x == x -> x == 0 */
271 (for cmp (eq ne)
272  (simplify
273   (cmp:c @0 (negate @0))
274    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
275         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
276     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
278 /* Maybe fold x * 0 to 0.  The expressions aren't the same
279    when x is NaN, since x * 0 is also NaN.  Nor are they the
280    same in modes with signed zeros, since multiplying a
281    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
282    since x * 0 is NaN.  */
283 (simplify
284  (mult @0 real_zerop@1)
285  (if (!tree_expr_maybe_nan_p (@0)
286       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
287       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
288   @1))
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291    Likewise for complex arithmetic with signed zeros.  */
292 (simplify
293  (mult @0 real_onep)
294  (if (!tree_expr_maybe_signaling_nan_p (@0)
295       && (!HONOR_SIGNED_ZEROS (type)
296           || !COMPLEX_FLOAT_TYPE_P (type)))
297   (non_lvalue @0)))
299 /* Transform x * -1.0 into -x.  */
300 (simplify
301  (mult @0 real_minus_onep)
302   (if (!tree_expr_maybe_signaling_nan_p (@0)
303        && (!HONOR_SIGNED_ZEROS (type)
304            || !COMPLEX_FLOAT_TYPE_P (type)))
305    (negate @0)))
307 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
308    unless the target has native support for the former but not the latter.  */
309 (simplify
310  (mult @0 VECTOR_CST@1)
311  (if (initializer_each_zero_or_onep (@1)
312       && !HONOR_SNANS (type)
313       && !HONOR_SIGNED_ZEROS (type))
314   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
315    (if (itype
316         && (!VECTOR_MODE_P (TYPE_MODE (type))
317             || (VECTOR_MODE_P (TYPE_MODE (itype))
318                 && optab_handler (and_optab,
319                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
320     (view_convert (bit_and:itype (view_convert @0)
321                                  (ne @1 { build_zero_cst (type); })))))))
323 /* In SWAR (SIMD within a register) code a signed comparison of packed data
324    can be constructed with a particular combination of shift, bitwise and,
325    and multiplication by constants.  If that code is vectorized we can
326    convert this pattern into a more efficient vector comparison.  */
327 (simplify
328  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329             uniform_integer_cst_p@2)
330     uniform_integer_cst_p@3)
331  (with {
332    tree rshift_cst = uniform_integer_cst_p (@1);
333    tree bit_and_cst = uniform_integer_cst_p (@2);
334    tree mult_cst = uniform_integer_cst_p (@3);
335   }
336   /* Make sure we're working with vectors and uniform vector constants.  */
337   (if (VECTOR_TYPE_P (type)
338        && tree_fits_uhwi_p (rshift_cst)
339        && tree_fits_uhwi_p (mult_cst)
340        && tree_fits_uhwi_p (bit_and_cst))
341    /* Compute what constants would be needed for this to represent a packed
342       comparison based on the shift amount denoted by RSHIFT_CST.  */
343    (with {
344      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
345      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
346      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
347      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
348      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
349      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
350      mult_i = tree_to_uhwi (mult_cst);
351      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
352      bit_and_i = tree_to_uhwi (bit_and_cst);
353      target_bit_and_i = 0;
355      /* The bit pattern in BIT_AND_I should be a mask for the least
356         significant bit of each packed element that is CMP_BITS wide.  */
357      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
358        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
359     }
360     (if ((exact_log2 (cmp_bits_i)) >= 0
361          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
362          && multiple_p (vec_bits, cmp_bits_i)
363          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
364          && target_mult_i == mult_i
365          && target_bit_and_i == bit_and_i)
366      /* Compute the vector shape for the comparison and check if the target is
367         able to expand the comparison with that type.  */
368      (with {
369        /* We're doing a signed comparison.  */
370        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
371        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
372        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
373        tree vec_truth_type = truth_type_for (vec_cmp_type);
374        tree zeros = build_zero_cst (vec_cmp_type);
375        tree ones = build_all_ones_cst (vec_cmp_type);
376       }
377       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
378            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
379        (view_convert:type (vec_cond (lt:vec_truth_type
380                                      (view_convert:vec_cmp_type @0)
381                                      { zeros; })
382                            { ones; } { zeros; })))))))))
384 (for cmp (gt ge lt le)
385      outp (convert convert negate negate)
386      outn (negate negate convert convert)
387  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
388  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
389  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
390  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
391  (simplify
392   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
393   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
394    (outp (abs @0))))
395  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
396  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
397  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
398  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
399  (simplify
400   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
401   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
402    (outn (abs @0)))))
404 /* Transform X * copysign (1.0, X) into abs(X). */
405 (simplify
406  (mult:c @0 (COPYSIGN_ALL real_onep @0))
407  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
408   (abs @0)))
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
411 (simplify
412  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414   (negate (abs @0))))
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
417 (simplify
418  (COPYSIGN_ALL REAL_CST@0 @1)
419  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420   (COPYSIGN_ALL (negate @0) @1)))
422 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
423    tree-ssa-math-opts.cc does the corresponding optimization for
424    unconditional multiplications (via xorsign).  */
425 (simplify
426  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427  (with { tree signs = sign_mask_for (type); }
428   (if (signs)
429    (with { tree inttype = TREE_TYPE (signs); }
430     (view_convert:type
431      (IFN_COND_XOR:inttype @0
432       (view_convert:inttype @1)
433       (bit_and (view_convert:inttype @2) { signs; })
434       (view_convert:inttype @3)))))))
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
437 (simplify
438   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
439   (abs @0))
441 /* X * 1, X / 1 -> X.  */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
443   (simplify
444     (op @0 integer_onep)
445     (non_lvalue @0)))
447 /* (A / (1 << B)) -> (A >> B).
448    Only for unsigned A.  For signed A, this would not preserve rounding
449    toward zero.
450    For example: (-1 / ( 1 << B)) !=  -1 >> B.
451    Also handle widening conversions, like:
452    (A / (unsigned long long) (1U << B)) -> (A >> B)
453    or
454    (A / (unsigned long long) (1 << B)) -> (A >> B).
455    If the left shift is signed, it can be done only if the upper bits
456    of A starting from shift's type sign bit are zero, as
457    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
458    so it is valid only if A >> 31 is zero.  */
459 (simplify
460  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
461  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
462       && (!VECTOR_TYPE_P (type)
463           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
464           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
465       && (useless_type_conversion_p (type, TREE_TYPE (@1))
466           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
467               && (TYPE_UNSIGNED (TREE_TYPE (@1))
468                   || (element_precision (type)
469                       == element_precision (TREE_TYPE (@1)))
470                   || (INTEGRAL_TYPE_P (type)
471                       && (tree_nonzero_bits (@0)
472                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
473                                       true,
474                                       element_precision (type))) == 0)))))
475    (if (!VECTOR_TYPE_P (type)
476         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
477         && element_precision (TREE_TYPE (@3)) < element_precision (type))
478     (convert (rshift @3 @2))
479     (rshift @0 @2))))
481 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
482    undefined behavior in constexpr evaluation, and assuming that the division
483    traps enables better optimizations than these anyway.  */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
485  /* 0 / X is always zero.  */
486  (simplify
487   (div integer_zerop@0 @1)
488   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
489   (if (!integer_zerop (@1))
490    @0))
491  /* X / -1 is -X.  */
492  (simplify
493   (div @0 integer_minus_onep@1)
494   (if (!TYPE_UNSIGNED (type))
495    (negate @0)))
496  /* X / bool_range_Y is X.  */ 
497  (simplify
498   (div @0 SSA_NAME@1)
499   (if (INTEGRAL_TYPE_P (type)
500        && ssa_name_has_boolean_range (@1)
501        && !flag_non_call_exceptions)
502    @0))
503  /* X / X is one.  */
504  (simplify
505   (div @0 @0)
506   /* But not for 0 / 0 so that we can get the proper warnings and errors.
507      And not for _Fract types where we can't build 1.  */
508   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
509        && !integer_zerop (@0)
510        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511    { build_one_cst (type); }))
512  /* X / abs (X) is X < 0 ? -1 : 1.  */
513  (simplify
514    (div:C @0 (abs @0))
515    (if (INTEGRAL_TYPE_P (type)
516         && TYPE_OVERFLOW_UNDEFINED (type)
517         && !integer_zerop (@0)
518         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
519     (cond (lt @0 { build_zero_cst (type); })
520           { build_minus_one_cst (type); } { build_one_cst (type); })))
521  /* X / -X is -1.  */
522  (simplify
523    (div:C @0 (negate @0))
524    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
525         && TYPE_OVERFLOW_UNDEFINED (type)
526         && !integer_zerop (@0)
527         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
528     { build_minus_one_cst (type); })))
530 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
531    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
532    for MOD instead of DIV.  */
533 (for floor_divmod (floor_div floor_mod)
534      trunc_divmod (trunc_div trunc_mod)
535  (simplify
536   (floor_divmod @0 @1)
537   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538        && TYPE_UNSIGNED (type))
539    (trunc_divmod @0 @1))))
541 /* 1 / X -> X == 1 for unsigned integer X.
542    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
543    But not for 1 / 0 so that we can get proper warnings and errors,
544    and not for 1-bit integers as they are edge cases better handled
545    elsewhere.  */
546 (simplify
547  (trunc_div integer_onep@0 @1)
548  (if (INTEGRAL_TYPE_P (type)
549       && TYPE_PRECISION (type) > 1
550       && !integer_zerop (@1)
551       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
552   (if (TYPE_UNSIGNED (type))
553    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
554    (with { tree utype = unsigned_type_for (type); }
555     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
556               { build_int_cst (utype, 2); })
557      @1 { build_zero_cst (type); })))))
559 /* Combine two successive divisions.  Note that combining ceil_div
560    and floor_div is trickier and combining round_div even more so.  */
561 (for div (trunc_div exact_div)
562  (simplify
563   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
564   (with {
565     wi::overflow_type overflow;
566     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567                             TYPE_SIGN (type), &overflow);
568    }
569    (if (div == EXACT_DIV_EXPR
570         || optimize_successive_divisions_p (@2, @3))
571     (if (!overflow)
572      (div @0 { wide_int_to_tree (type, mul); })
573      (if (TYPE_UNSIGNED (type)
574           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
575       { build_zero_cst (type); }))))))
577 /* Combine successive multiplications.  Similar to above, but handling
578    overflow is different.  */
579 (simplify
580  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
581  (with {
582    wi::overflow_type overflow;
583    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584                            TYPE_SIGN (type), &overflow);
585   }
586   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
587      otherwise undefined overflow implies that @0 must be zero.  */
588   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
589    (mult @0 { wide_int_to_tree (type, mul); }))))
591 /* Similar to above, but there could be an extra add/sub between
592    successive multuiplications.  */
593 (simplify
594  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
595  (with {
596    bool overflowed = true;
597    wi::overflow_type ovf1, ovf2;
598    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
599                            TYPE_SIGN (type), &ovf1);
600    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
601                            TYPE_SIGN (type), &ovf2);
602   if (TYPE_OVERFLOW_UNDEFINED (type))
603     {
604 #if GIMPLE
605       value_range vr0;
606       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
607           && get_global_range_query ()->range_of_expr (vr0, @4)
608           && !vr0.varying_p () && !vr0.undefined_p ())
609         {
610           wide_int wmin0 = vr0.lower_bound ();
611           wide_int wmax0 = vr0.upper_bound ();
612           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
613           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
614           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
615             {
616               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
617               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
618               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
619                 overflowed = false;
620             }
621         }
622 #endif
623     }
624   else
625    overflowed = false;
627   /* Skip folding on overflow.  */
628   (if (!overflowed)
629    (plus (mult @0 { wide_int_to_tree (type, mul); })
630          { wide_int_to_tree (type, add); }))))
632 /* Similar to above, but a multiplication between successive additions.  */
633 (simplify
634  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
635  (with {
636    bool overflowed = true;
637    wi::overflow_type ovf1;
638    wi::overflow_type ovf2;
639    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
640                            TYPE_SIGN (type), &ovf1);
641    wide_int add = wi::add (mul, wi::to_wide (@3),
642                            TYPE_SIGN (type), &ovf2);
643   if (TYPE_OVERFLOW_UNDEFINED (type))
644     {
645 #if GIMPLE
646       value_range vr0;
647       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
648           && get_global_range_query ()->range_of_expr (vr0, @0)
649           && !vr0.varying_p () && !vr0.undefined_p ())
650         {
651           wide_int wmin0 = vr0.lower_bound ();
652           wide_int wmax0 = vr0.upper_bound ();
653           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
654           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
655           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
656             {
657               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
658               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
659               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
660                 overflowed = false;
661             }
662         }
663 #endif
664     }
665   else
666    overflowed = false;
668   /* Skip folding on overflow.  */
669   (if (!overflowed)
670    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
672 /* Optimize A / A to 1.0 if we don't care about
673    NaNs or Infinities.  */
674 (simplify
675  (rdiv @0 @0)
676  (if (FLOAT_TYPE_P (type)
677       && ! HONOR_NANS (type)
678       && ! HONOR_INFINITIES (type))
679   { build_one_cst (type); }))
681 /* Optimize -A / A to -1.0 if we don't care about
682    NaNs or Infinities.  */
683 (simplify
684  (rdiv:C @0 (negate @0))
685  (if (FLOAT_TYPE_P (type)
686       && ! HONOR_NANS (type)
687       && ! HONOR_INFINITIES (type))
688   { build_minus_one_cst (type); }))
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
691 (simplify
692  (rdiv:C (convert? @0) (convert? (abs @0)))
693   (if (SCALAR_FLOAT_TYPE_P (type)
694        && ! HONOR_NANS (type)
695        && ! HONOR_INFINITIES (type))
696    (switch
697     (if (types_match (type, float_type_node))
698      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
699     (if (types_match (type, double_type_node))
700      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
701     (if (types_match (type, long_double_type_node))
702      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
704 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
705 (simplify
706  (rdiv @0 real_onep)
707  (if (!tree_expr_maybe_signaling_nan_p (@0))
708   (non_lvalue @0)))
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
711 (simplify
712  (rdiv @0 real_minus_onep)
713  (if (!tree_expr_maybe_signaling_nan_p (@0))
714   (negate @0)))
716 (if (flag_reciprocal_math)
717  /* Convert (A/B)/C to A/(B*C). */
718  (simplify
719   (rdiv (rdiv:s @0 @1) @2)
720   (rdiv @0 (mult @1 @2)))
722  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
723  (simplify
724   (rdiv @0 (mult:s @1 REAL_CST@2))
725   (with
726    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
727    (if (tem)
728     (rdiv (mult @0 { tem; } ) @1))))
730  /* Convert A/(B/C) to (A/B)*C  */
731  (simplify
732   (rdiv @0 (rdiv:s @1 @2))
733    (mult (rdiv @0 @1) @2)))
735 /* Simplify x / (- y) to -x / y.  */
736 (simplify
737  (rdiv @0 (negate @1))
738  (rdiv (negate @0) @1))
740 (if (flag_unsafe_math_optimizations)
741  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
742     Since C / x may underflow to zero, do this only for unsafe math.  */
743  (for op (lt le gt ge)
744       neg_op (gt ge lt le)
745   (simplify
746    (op (rdiv REAL_CST@0 @1) real_zerop@2)
747    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
748     (switch
749      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
750       (op @1 @2))
751      /* For C < 0, use the inverted operator.  */
752      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
753       (neg_op @1 @2)))))))
755 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
756 (for div (trunc_div ceil_div floor_div round_div exact_div)
757  (simplify
758   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
759   (if (integer_pow2p (@2)
760        && tree_int_cst_sgn (@2) > 0
761        && tree_nop_conversion_p (type, TREE_TYPE (@0))
762        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
763    (rshift (convert @0)
764            { build_int_cst (integer_type_node,
765                             wi::exact_log2 (wi::to_wide (@2))); }))))
767 /* If ARG1 is a constant, we can convert this to a multiply by the
768    reciprocal.  This does not have the same rounding properties,
769    so only do this if -freciprocal-math.  We can actually
770    always safely do it if ARG1 is a power of two, but it's hard to
771    tell if it is or not in a portable manner.  */
772 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
773  (simplify
774   (rdiv @0 cst@1)
775   (if (optimize)
776    (if (flag_reciprocal_math
777         && !real_zerop (@1))
778     (with
779      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
780      (if (tem)
781       (mult @0 { tem; } )))
782     (if (cst != COMPLEX_CST)
783      (with { tree inverse = exact_inverse (type, @1); }
784       (if (inverse)
785        (mult @0 { inverse; } ))))))))
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788  /* 0 % X is always zero.  */
789  (simplify
790   (mod integer_zerop@0 @1)
791   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
792   (if (!integer_zerop (@1))
793    @0))
794  /* X % 1 is always zero.  */
795  (simplify
796   (mod @0 integer_onep)
797   { build_zero_cst (type); })
798  /* X % -1 is zero.  */
799  (simplify
800   (mod @0 integer_minus_onep@1)
801   (if (!TYPE_UNSIGNED (type))
802    { build_zero_cst (type); }))
803  /* X % X is zero.  */
804  (simplify
805   (mod @0 @0)
806   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
807   (if (!integer_zerop (@0))
808    { build_zero_cst (type); }))
809  /* (X % Y) % Y is just X % Y.  */
810  (simplify
811   (mod (mod@2 @0 @1) @1)
812   @2)
813  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
814  (simplify
815   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
816   (if (ANY_INTEGRAL_TYPE_P (type)
817        && TYPE_OVERFLOW_UNDEFINED (type)
818        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
819                              TYPE_SIGN (type)))
820    { build_zero_cst (type); }))
821  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
822     modulo and comparison, since it is simpler and equivalent.  */
823  (for cmp (eq ne)
824   (simplify
825    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
826    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
827     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
828      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
830 /* X % -C is the same as X % C.  */
831 (simplify
832  (trunc_mod @0 INTEGER_CST@1)
833   (if (TYPE_SIGN (type) == SIGNED
834        && !TREE_OVERFLOW (@1)
835        && wi::neg_p (wi::to_wide (@1))
836        && !TYPE_OVERFLOW_TRAPS (type)
837        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
838        && !sign_bit_p (@1, @1))
839    (trunc_mod @0 (negate @1))))
841 /* X % -Y is the same as X % Y.  */
842 (simplify
843  (trunc_mod @0 (convert? (negate @1)))
844  (if (INTEGRAL_TYPE_P (type)
845       && !TYPE_UNSIGNED (type)
846       && !TYPE_OVERFLOW_TRAPS (type)
847       && tree_nop_conversion_p (type, TREE_TYPE (@1))
848       /* Avoid this transformation if X might be INT_MIN or
849          Y might be -1, because we would then change valid
850          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
851       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
852           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
853                                                         (TREE_TYPE (@1))))))
854   (trunc_mod @0 (convert @1))))
856 /* X - (X / Y) * Y is the same as X % Y.  */
857 (simplify
858  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
859  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
860   (convert (trunc_mod @0 @1))))
862 /* x * (1 + y / x) - y -> x - y % x */
863 (simplify
864  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
865  (if (INTEGRAL_TYPE_P (type))
866   (minus @0 (trunc_mod @1 @0))))
868 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
869    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
870    Also optimize A % (C << N)  where C is a power of 2,
871    to A & ((C << N) - 1).
872    Also optimize "A shift (B % C)", if C is a power of 2, to
873    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
874    and assume (B % C) is nonnegative as shifts negative values would
875    be UB.  */
876 (match (power_of_two_cand @1)
877  INTEGER_CST@1)
878 (match (power_of_two_cand @1)
879  (lshift INTEGER_CST@1 @2))
880 (for mod (trunc_mod floor_mod)
881  (for shift (lshift rshift)
882   (simplify
883    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
884    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
885     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
886                                                       1); }))))))
887  (simplify
888   (mod @0 (convert? (power_of_two_cand@1 @2)))
889   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
890        /* Allow any integral conversions of the divisor, except
891           conversion from narrower signed to wider unsigned type
892           where if @1 would be negative power of two, the divisor
893           would not be a power of two.  */
894        && INTEGRAL_TYPE_P (type)
895        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
896        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
897            || TYPE_UNSIGNED (TREE_TYPE (@1))
898            || !TYPE_UNSIGNED (type))
899        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
900    (with { tree utype = TREE_TYPE (@1);
901            if (!TYPE_OVERFLOW_WRAPS (utype))
902              utype = unsigned_type_for (utype); }
903     (bit_and @0 (convert (minus (convert:utype @1)
904                                 { build_one_cst (utype); })))))))
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
907 (simplify
908  (trunc_div (mult @0 integer_pow2p@1) @1)
909  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
910   (bit_and @0 { wide_int_to_tree
911                 (type, wi::mask (TYPE_PRECISION (type)
912                                  - wi::exact_log2 (wi::to_wide (@1)),
913                                  false, TYPE_PRECISION (type))); })))
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
916 (simplify
917  (mult (trunc_div @0 integer_pow2p@1) @1)
918  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
919   (bit_and @0 (negate @1))))
921 /* Simplify (t * 2) / 2) -> t.  */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
923  (simplify
924   (div (mult:c @0 @1) @1)
925   (if (ANY_INTEGRAL_TYPE_P (type))
926    (if (TYPE_OVERFLOW_UNDEFINED (type))
927     @0
928 #if GIMPLE
929     (with
930      {
931        bool overflowed = true;
932        value_range vr0, vr1;
933        if (INTEGRAL_TYPE_P (type)
934            && get_global_range_query ()->range_of_expr (vr0, @0)
935            && get_global_range_query ()->range_of_expr (vr1, @1)
936            && !vr0.varying_p () && !vr0.undefined_p ()
937            && !vr1.varying_p () && !vr1.undefined_p ())
938          {
939            wide_int wmin0 = vr0.lower_bound ();
940            wide_int wmax0 = vr0.upper_bound ();
941            wide_int wmin1 = vr1.lower_bound ();
942            wide_int wmax1 = vr1.upper_bound ();
943            /* If the multiplication can't overflow/wrap around, then
944               it can be optimized too.  */
945            wi::overflow_type min_ovf, max_ovf;
946            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
947            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
948            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
949              {
950                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
951                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
952                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
953                  overflowed = false;
954              }
955          }
956      }
957     (if (!overflowed)
958      @0))
959 #endif
960    ))))
962 (for op (negate abs)
963  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
964  (for coss (COS COSH)
965   (simplify
966    (coss (op @0))
967     (coss @0)))
968  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
969  (for pows (POW)
970   (simplify
971    (pows (op @0) REAL_CST@1)
972    (with { HOST_WIDE_INT n; }
973     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
974      (pows @0 @1)))))
975  /* Likewise for powi.  */
976  (for pows (POWI)
977   (simplify
978    (pows (op @0) INTEGER_CST@1)
979    (if ((wi::to_wide (@1) & 1) == 0)
980     (pows @0 @1))))
981  /* Strip negate and abs from both operands of hypot.  */
982  (for hypots (HYPOT)
983   (simplify
984    (hypots (op @0) @1)
985    (hypots @0 @1))
986   (simplify
987    (hypots @0 (op @1))
988    (hypots @0 @1)))
989  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
990  (for copysigns (COPYSIGN_ALL)
991   (simplify
992    (copysigns (op @0) @1)
993    (copysigns @0 @1))))
995 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
996 (simplify
997  (mult (abs@1 @0) @1)
998  (mult @0 @0))
1000 /* Convert absu(x)*absu(x) -> x*x.  */
1001 (simplify
1002  (mult (absu@1 @0) @1)
1003  (mult (convert@2 @0) @2))
1005 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1006 (for coss (COS COSH)
1007      copysigns (COPYSIGN)
1008  (simplify
1009   (coss (copysigns @0 @1))
1010    (coss @0)))
1012 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1013 (for pows (POW)
1014      copysigns (COPYSIGN)
1015  (simplify
1016   (pows (copysigns @0 @2) REAL_CST@1)
1017   (with { HOST_WIDE_INT n; }
1018    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1019     (pows @0 @1)))))
1020 /* Likewise for powi.  */
1021 (for pows (POWI)
1022      copysigns (COPYSIGN)
1023  (simplify
1024   (pows (copysigns @0 @2) INTEGER_CST@1)
1025   (if ((wi::to_wide (@1) & 1) == 0)
1026    (pows @0 @1))))
1028 (for hypots (HYPOT)
1029      copysigns (COPYSIGN)
1030  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1031  (simplify
1032   (hypots (copysigns @0 @1) @2)
1033   (hypots @0 @2))
1034  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1035  (simplify
1036   (hypots @0 (copysigns @1 @2))
1037   (hypots @0 @1)))
1039 /* copysign(x, CST) -> [-]abs (x).  */
1040 (for copysigns (COPYSIGN_ALL)
1041  (simplify
1042   (copysigns @0 REAL_CST@1)
1043   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1044    (negate (abs @0))
1045    (abs @0))))
1047 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1048 (for copysigns (COPYSIGN_ALL)
1049  (simplify
1050   (copysigns (copysigns @0 @1) @2)
1051   (copysigns @0 @2)))
1053 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1054 (for copysigns (COPYSIGN_ALL)
1055  (simplify
1056   (mult (copysigns@2 @0 @1) @2)
1057   (mult @0 @0)))
1059 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1060 (for ccoss (CCOS CCOSH)
1061  (simplify
1062   (ccoss (negate @0))
1063    (ccoss @0)))
1065 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1066 (for ops (conj negate)
1067  (for cabss (CABS)
1068   (simplify
1069    (cabss (ops @0))
1070    (cabss @0))))
1072 /* Fold (a * (1 << b)) into (a << b)  */
1073 (simplify
1074  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1075   (if (! FLOAT_TYPE_P (type)
1076        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1077    (lshift @0 @2)))
1079 /* Shifts by precision or greater result in zero.  */
1080 (for shift (lshift rshift)
1081  (simplify
1082   (shift @0 uniform_integer_cst_p@1)
1083   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1084        /* Leave arithmetic right shifts of possibly negative values alone.  */
1085        && (TYPE_UNSIGNED (type)
1086            || shift == LSHIFT_EXPR
1087            || tree_expr_nonnegative_p (@0))
1088        /* Use a signed compare to leave negative shift counts alone.  */
1089        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1090                      element_precision (type)))
1091    { build_zero_cst (type); })))
1093 /* Shifts by constants distribute over several binary operations,
1094    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1095 (for op (plus minus)
1096   (simplify
1097     (op (lshift:s @0 @1) (lshift:s @2 @1))
1098     (if (INTEGRAL_TYPE_P (type)
1099          && TYPE_OVERFLOW_WRAPS (type)
1100          && !TYPE_SATURATING (type))
1101       (lshift (op @0 @2) @1))))
1103 (for op (bit_and bit_ior bit_xor)
1104   (simplify
1105     (op (lshift:s @0 @1) (lshift:s @2 @1))
1106     (if (INTEGRAL_TYPE_P (type))
1107       (lshift (op @0 @2) @1)))
1108   (simplify
1109     (op (rshift:s @0 @1) (rshift:s @2 @1))
1110     (if (INTEGRAL_TYPE_P (type))
1111       (rshift (op @0 @2) @1))))
1113 /* Fold (1 << (C - x)) where C = precision(type) - 1
1114    into ((1 << C) >> x). */
1115 (simplify
1116  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1117   (if (INTEGRAL_TYPE_P (type)
1118        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1119        && single_use (@1))
1120    (if (TYPE_UNSIGNED (type))
1121      (rshift (lshift @0 @2) @3)
1122    (with
1123     { tree utype = unsigned_type_for (type); }
1124     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1126 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1127 (simplify
1128  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1129  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1130       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1131   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1132    (bit_and (convert @0)
1133             { wide_int_to_tree (type,
1134                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1136 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1137 (for cst (INTEGER_CST VECTOR_CST)
1138  (simplify
1139   (rshift (negate:s @0) cst@1)
1140    (if (!TYPE_UNSIGNED (type)
1141         && TYPE_OVERFLOW_UNDEFINED (type))
1142     (with { tree stype = TREE_TYPE (@1);
1143             tree bt = truth_type_for (type);
1144             tree zeros = build_zero_cst (type);
1145             tree cst = NULL_TREE; }
1146      (switch
1147       /* Handle scalar case.  */
1148       (if (INTEGRAL_TYPE_P (type)
1149            /* If we apply the rule to the scalar type before vectorization
1150               we will enforce the result of the comparison being a bool
1151               which will require an extra AND on the result that will be
1152               indistinguishable from when the user did actually want 0
1153               or 1 as the result so it can't be removed.  */
1154            && canonicalize_math_after_vectorization_p ()
1155            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1156        (negate (convert (gt @0 { zeros; }))))
1157       /* Handle vector case.  */
1158       (if (VECTOR_INTEGER_TYPE_P (type)
1159            /* First check whether the target has the same mode for vector
1160               comparison results as it's operands do.  */
1161            && TYPE_MODE (bt) == TYPE_MODE (type)
1162            /* Then check to see if the target is able to expand the comparison
1163               with the given type later on, otherwise we may ICE.  */
1164            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1165            && (cst = uniform_integer_cst_p (@1)) != NULL
1166            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1167        (view_convert (gt:bt @0 { zeros; }))))))))
1169 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1170 (simplify
1171  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1172   (if (flag_associative_math
1173        && single_use (@3))
1174    (with
1175     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1176     (if (tem)
1177      (rdiv { tem; } @1)))))
1179 /* Simplify ~X & X as zero.  */
1180 (simplify
1181  (bit_and (convert? @0) (convert? @1))
1182  (with { bool wascmp; }
1183   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1184        && bitwise_inverted_equal_p (@0, @1, wascmp))
1185    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1187 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1188 (simplify
1189   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1190   (if (TYPE_UNSIGNED (type))
1191     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1193 (for bitop (bit_and bit_ior)
1194      cmp (eq ne)
1195  /* PR35691: Transform
1196     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1197     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1198  (simplify
1199   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1200    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1201         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1202         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1203     (cmp (bit_ior @0 (convert @1)) @2)))
1204  /* Transform:
1205     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1206     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1207  (simplify
1208   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1209    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1210         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1211         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1212     (cmp (bit_and @0 (convert @1)) @2))))
1214 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1215 (simplify
1216  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1217   (minus (bit_xor @0 @1) @1))
1218 (simplify
1219  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1220  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1221   (minus (bit_xor @0 @1) @1)))
1223 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1224 (simplify
1225  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1226   (minus @1 (bit_xor @0 @1)))
1228 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1229 (for op (bit_ior bit_xor plus)
1230  (simplify
1231   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1232    (bit_xor @0 @1))
1233  (simplify
1234   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1235   (if (~wi::to_wide (@2) == wi::to_wide (@1))
1236    (bit_xor @0 @1))))
1238 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1239 (simplify
1240   (bit_ior:c (bit_xor:c @0 @1) @0)
1241   (bit_ior @0 @1))
1243 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1244 (simplify
1245  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1246  @2)
1248 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1249 (simplify
1250  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1251  (bit_not (bit_and @0 @1)))
1253 /* (~a & b) ^ a  -->   (a | b)   */
1254 (simplify
1255  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1256  (bit_ior @0 @1))
1258 /* (a | b) & ~(a ^ b)  -->  a & b  */
1259 (simplify
1260  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1261  (bit_and @0 @1))
1263 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1264 (simplify
1265  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1266  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1267       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1268   (bit_and @0 @1)))
1270 /* a | ~(a ^ b)  -->  a | ~b  */
1271 (simplify
1272  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1273  (bit_ior @0 (bit_not @1)))
1275 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1276 (simplify
1277  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1278  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1279       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1280   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1282 /* (a | b) | (a &^ b)  -->  a | b  */
1283 (for op (bit_and bit_xor)
1284  (simplify
1285   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1286   @2))
1288 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1289 (simplify
1290  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1291  @2)
1293 /* (a & b) | (a == b)  -->  a == b  */
1294 (simplify
1295  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1296  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1297       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1298   @2))
1300 /* ~(~a & b)  -->  a | ~b  */
1301 (simplify
1302  (bit_not (bit_and:cs (bit_not @0) @1))
1303  (bit_ior @0 (bit_not @1)))
1305 /* ~(~a | b) --> a & ~b */
1306 (simplify
1307  (bit_not (bit_ior:cs (bit_not @0) @1))
1308  (bit_and @0 (bit_not @1)))
1310 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1311 (simplify
1312  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1313  (bit_and @3 (bit_not @2)))
1315 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1316 (simplify
1317  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1318  (bit_ior @3 @2))
1320 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1321 (simplify
1322  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1323   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1325 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1326 (simplify
1327  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1328   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1330 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1331 (simplify
1332  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1333  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1334       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1335   (bit_xor @0 @1)))
1337 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1338    ((A & N) + B) & M -> (A + B) & M
1339    Similarly if (N & M) == 0,
1340    ((A | N) + B) & M -> (A + B) & M
1341    and for - instead of + (or unary - instead of +)
1342    and/or ^ instead of |.
1343    If B is constant and (B & M) == 0, fold into A & M.  */
1344 (for op (plus minus)
1345  (for bitop (bit_and bit_ior bit_xor)
1346   (simplify
1347    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1348     (with
1349      { tree pmop[2];
1350        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1351                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1352                                        NULL_TREE, pmop); }
1353      (if (utype)
1354       (convert (bit_and (op (convert:utype { pmop[0]; })
1355                             (convert:utype { pmop[1]; }))
1356                         (convert:utype @2))))))
1357   (simplify
1358    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1359     (with
1360      { tree pmop[2];
1361        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1362                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1363                                        @4, pmop); }
1364      (if (utype)
1365       (convert (bit_and (op (convert:utype { pmop[0]; })
1366                             (convert:utype { pmop[1]; }))
1367                         (convert:utype @2)))))))
1368  (simplify
1369   (bit_and (op:s @0 @1) INTEGER_CST@2)
1370    (with
1371     { tree pmop[2];
1372       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1373                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1374                                       NULL_TREE, NULL_TREE, pmop); }
1375     (if (utype)
1376      (convert (bit_and (op (convert:utype { pmop[0]; })
1377                            (convert:utype { pmop[1]; }))
1378                        (convert:utype @2)))))))
1379 (for bitop (bit_and bit_ior bit_xor)
1380  (simplify
1381   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1382    (with
1383     { tree pmop[2];
1384       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1385                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1386                                       NULL_TREE, NULL_TREE, pmop); }
1387     (if (utype)
1388      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1389                        (convert:utype @1)))))))
1391 /* X % Y is smaller than Y.  */
1392 (for cmp (lt ge)
1393  (simplify
1394   (cmp (trunc_mod @0 @1) @1)
1395   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1396    { constant_boolean_node (cmp == LT_EXPR, type); })))
1397 (for cmp (gt le)
1398  (simplify
1399   (cmp @1 (trunc_mod @0 @1))
1400   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1401    { constant_boolean_node (cmp == GT_EXPR, type); })))
1403 /* x | ~0 -> ~0  */
1404 (simplify
1405  (bit_ior @0 integer_all_onesp@1)
1406  @1)
1408 /* x | 0 -> x  */
1409 (simplify
1410  (bit_ior @0 integer_zerop)
1411  @0)
1413 /* x & 0 -> 0  */
1414 (simplify
1415  (bit_and @0 integer_zerop@1)
1416  @1)
1418 /* ~x | x -> -1 */
1419 /* ~x ^ x -> -1 */
1420 (for op (bit_ior bit_xor)
1421  (simplify
1422   (op (convert? @0) (convert? @1))
1423   (with { bool wascmp; }
1424    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1425         && bitwise_inverted_equal_p (@0, @1, wascmp))
1426     (convert
1427      { wascmp
1428         ? constant_boolean_node (true, type)
1429         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1431 /* x ^ x -> 0 */
1432 (simplify
1433   (bit_xor @0 @0)
1434   { build_zero_cst (type); })
1436 /* Canonicalize X ^ ~0 to ~X.  */
1437 (simplify
1438   (bit_xor @0 integer_all_onesp@1)
1439   (bit_not @0))
1441 /* x & ~0 -> x  */
1442 (simplify
1443  (bit_and @0 integer_all_onesp)
1444   (non_lvalue @0))
1446 /* x & x -> x,  x | x -> x  */
1447 (for bitop (bit_and bit_ior)
1448  (simplify
1449   (bitop @0 @0)
1450   (non_lvalue @0)))
1452 /* x & C -> x if we know that x & ~C == 0.  */
1453 #if GIMPLE
1454 (simplify
1455  (bit_and SSA_NAME@0 INTEGER_CST@1)
1456  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1457       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1458   @0))
1459 /* x | C -> C if we know that x & ~C == 0.  */
1460 (simplify
1461  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1462  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1463       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1464   @1))
1465 #endif
1467 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1468 (simplify
1469  (bit_not (minus (bit_not @0) @1))
1470  (plus @0 @1))
1471 (simplify
1472  (bit_not (plus:c (bit_not @0) @1))
1473  (minus @0 @1))
1474 /* (~X - ~Y) -> Y - X.  */
1475 (simplify
1476  (minus (bit_not @0) (bit_not @1))
1477   (if (!TYPE_OVERFLOW_SANITIZED (type))
1478    (with { tree utype = unsigned_type_for (type); }
1479     (convert (minus (convert:utype @1) (convert:utype @0))))))
1481 /* ~(X - Y) -> ~X + Y.  */
1482 (simplify
1483  (bit_not (minus:s @0 @1))
1484  (plus (bit_not @0) @1))
1485 (simplify
1486  (bit_not (plus:s @0 INTEGER_CST@1))
1487  (if ((INTEGRAL_TYPE_P (type)
1488        && TYPE_UNSIGNED (type))
1489       || (!TYPE_OVERFLOW_SANITIZED (type)
1490           && may_negate_without_overflow_p (@1)))
1491   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1493 #if GIMPLE
1494 /* ~X + Y -> (Y - X) - 1.  */
1495 (simplify
1496  (plus:c (bit_not @0) @1)
1497   (if (ANY_INTEGRAL_TYPE_P (type)
1498        && TYPE_OVERFLOW_WRAPS (type)
1499        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1500        && !integer_all_onesp (@1))
1501    (plus (minus @1 @0) { build_minus_one_cst (type); })
1502    (if (INTEGRAL_TYPE_P (type)
1503         && TREE_CODE (@1) == INTEGER_CST
1504         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1505                                               SIGNED))
1506     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1507 #endif
1509 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1510 (simplify
1511  (bit_not (rshift:s @0 @1))
1512   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1513    (rshift (bit_not! @0) @1)
1514    /* For logical right shifts, this is possible only if @0 doesn't
1515       have MSB set and the logical right shift is changed into
1516       arithmetic shift.  */
1517    (if (INTEGRAL_TYPE_P (type)
1518         && !wi::neg_p (tree_nonzero_bits (@0)))
1519     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1520      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1522 /* x + (x & 1) -> (x + 1) & ~1 */
1523 (simplify
1524  (plus:c @0 (bit_and:s @0 integer_onep@1))
1525  (bit_and (plus @0 @1) (bit_not @1)))
1527 /* x & ~(x & y) -> x & ~y */
1528 /* x | ~(x | y) -> x | ~y  */
1529 (for bitop (bit_and bit_ior)
1530  (simplify
1531   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1532   (bitop @0 (bit_not @1))))
1534 /* (~x & y) | ~(x | y) -> ~x */
1535 (simplify
1536  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1537  @2)
1539 /* (x | y) ^ (x | ~y) -> ~x */
1540 (simplify
1541  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1542  (bit_not @0))
1544 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1545 (simplify
1546  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1547  (bit_not (bit_xor @0 @1)))
1549 /* (~x | y) ^ (x ^ y) -> x | ~y */
1550 (simplify
1551  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1552  (bit_ior @0 (bit_not @1)))
1554 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1555 (simplify
1556  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1557  (bit_not (bit_and @0 @1)))
1559 /* (x & y) ^ (x | y) -> x ^ y */
1560 (simplify
1561  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1562  (bit_xor @0 @1))
1564 /* (x ^ y) ^ (x | y) -> x & y */
1565 (simplify
1566  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1567  (bit_and @0 @1))
1569 /* (x & y) + (x ^ y) -> x | y */
1570 /* (x & y) | (x ^ y) -> x | y */
1571 /* (x & y) ^ (x ^ y) -> x | y */
1572 (for op (plus bit_ior bit_xor)
1573  (simplify
1574   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1575   (bit_ior @0 @1)))
1577 /* (x & y) + (x | y) -> x + y */
1578 (simplify
1579  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1580  (plus @0 @1))
1582 /* (x + y) - (x | y) -> x & y */
1583 (simplify
1584  (minus (plus @0 @1) (bit_ior @0 @1))
1585  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1586       && !TYPE_SATURATING (type))
1587   (bit_and @0 @1)))
1589 /* (x + y) - (x & y) -> x | y */
1590 (simplify
1591  (minus (plus @0 @1) (bit_and @0 @1))
1592  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1593       && !TYPE_SATURATING (type))
1594   (bit_ior @0 @1)))
1596 /* (x | y) - y -> (x & ~y) */
1597 (simplify
1598  (minus (bit_ior:cs @0 @1) @1)
1599  (bit_and @0 (bit_not @1)))
1601 /* (x | y) - (x ^ y) -> x & y */
1602 (simplify
1603  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1604  (bit_and @0 @1))
1606 /* (x | y) - (x & y) -> x ^ y */
1607 (simplify
1608  (minus (bit_ior @0 @1) (bit_and @0 @1))
1609  (bit_xor @0 @1))
1611 /* (x | y) & ~(x & y) -> x ^ y */
1612 (simplify
1613  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1614  (bit_xor @0 @1))
1616 /* (x | y) & (~x ^ y) -> x & y */
1617 (simplify
1618  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1619  (bit_and @0 @1))
1621 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1622 (simplify
1623  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1624  (bit_not (bit_xor @0 @1)))
1626 /* (~x | y) ^ (x | ~y) -> x ^ y */
1627 (simplify
1628  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1629  (bit_xor @0 @1))
1631 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1632 (simplify
1633  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1634                               (nop_convert2? (bit_ior @0 @1))))
1635        integer_all_onesp)
1636  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1637       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1638       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1639       && !TYPE_SATURATING (TREE_TYPE (@2)))
1640  (bit_not (convert (bit_xor @0 @1)))))
1641 (simplify
1642  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1643                                integer_all_onesp))
1644        (nop_convert3? (bit_ior @0 @1)))
1645  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1646       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1647       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1648       && !TYPE_SATURATING (TREE_TYPE (@2)))
1649  (bit_not (convert (bit_xor @0 @1)))))
1650 (simplify
1651  (minus (nop_convert1? (bit_and @0 @1))
1652        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1653                                integer_onep)))
1654  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1655       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1656       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1657       && !TYPE_SATURATING (TREE_TYPE (@2)))
1658  (bit_not (convert (bit_xor @0 @1)))))
1660 /* ~x & ~y -> ~(x | y)
1661    ~x | ~y -> ~(x & y) */
1662 (for op (bit_and bit_ior)
1663      rop (bit_ior bit_and)
1664  (simplify
1665   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1666   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1667        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1668    (bit_not (rop (convert @0) (convert @1))))))
1670 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1671    with a constant, and the two constants have no bits in common,
1672    we should treat this as a BIT_IOR_EXPR since this may produce more
1673    simplifications.  */
1674 (for op (bit_xor plus)
1675  (simplify
1676   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1677       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1678   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1679        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1680        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1681    (bit_ior (convert @4) (convert @5)))))
1683 /* (X | Y) ^ X -> Y & ~ X*/
1684 (simplify
1685  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1686  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1687   (convert (bit_and @1 (bit_not @0)))))
1689 /* (~X | Y) ^ X -> ~(X & Y).  */
1690 (simplify
1691  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1692  (if (bitwise_equal_p (@0, @2))
1693   (convert (bit_not (bit_and @0 (convert @1))))))
1695 /* Convert ~X ^ ~Y to X ^ Y.  */
1696 (simplify
1697  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1698  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1699       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1700   (bit_xor (convert @0) (convert @1))))
1702 /* Convert ~X ^ C to X ^ ~C.  */
1703 (simplify
1704  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1705  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1706   (bit_xor (convert @0) (bit_not @1))))
1708 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1709 (for opo (bit_and bit_xor)
1710      opi (bit_xor bit_and)
1711  (simplify
1712   (opo:c (opi:cs @0 @1) @1)
1713   (bit_and (bit_not @0) @1)))
1715 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1716    operands are another bit-wise operation with a common input.  If so,
1717    distribute the bit operations to save an operation and possibly two if
1718    constants are involved.  For example, convert
1719      (A | B) & (A | C) into A | (B & C)
1720    Further simplification will occur if B and C are constants.  */
1721 (for op (bit_and bit_ior bit_xor)
1722      rop (bit_ior bit_and bit_and)
1723  (simplify
1724   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1725   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1726        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1727    (rop (convert @0) (op (convert @1) (convert @2))))))
1729 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1730 /* (X & Y) & Y -> X & Y
1731    (X | Y) | Y -> X | Y  */
1732 (for op (bit_and bit_ior)
1733  (simplify
1734   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1735   @2))
1736 /* (X ^ Y) ^ Y -> X  */
1737 (simplify
1738  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1739  (convert @0))
1740 /* (X & Y) & (X & Z) -> (X & Y) & Z
1741    (X | Y) | (X | Z) -> (X | Y) | Z  */
1742 (for op (bit_and bit_ior)
1743  (simplify
1744   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1745   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1746        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1747    (if (single_use (@5) && single_use (@6))
1748     (op @3 (convert @2))
1749     (if (single_use (@3) && single_use (@4))
1750      (op (convert @1) @5))))))
1751 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1752 (simplify
1753  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1754  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1755       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1756   (bit_xor (convert @1) (convert @2))))
1758 /* Convert abs (abs (X)) into abs (X).
1759    also absu (absu (X)) into absu (X).  */
1760 (simplify
1761  (abs (abs@1 @0))
1762  @1)
1764 (simplify
1765  (absu (convert@2 (absu@1 @0)))
1766  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1767   @1))
1769 /* Convert abs[u] (-X) -> abs[u] (X).  */
1770 (simplify
1771  (abs (negate @0))
1772  (abs @0))
1774 (simplify
1775  (absu (negate @0))
1776  (absu @0))
1778 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1779 (simplify
1780  (abs tree_expr_nonnegative_p@0)
1781  @0)
1783 (simplify
1784  (absu tree_expr_nonnegative_p@0)
1785  (convert @0))
1787 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1788 (simplify
1789  (mult:c (nop_convert1?
1790           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1791                     integer_onep))
1792          (nop_convert3? @0))
1793  (if (INTEGRAL_TYPE_P (type)
1794       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1795       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1796   (if (TYPE_UNSIGNED (type))
1797    (absu @0)
1798    (abs @0)
1799   )
1803 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1804 (match negate_expr_p
1805  INTEGER_CST
1806  (if ((INTEGRAL_TYPE_P (type)
1807        && TYPE_UNSIGNED (type))
1808       || (!TYPE_OVERFLOW_SANITIZED (type)
1809           && may_negate_without_overflow_p (t)))))
1810 (match negate_expr_p
1811  FIXED_CST)
1812 (match negate_expr_p
1813  (negate @0)
1814  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1815 (match negate_expr_p
1816  REAL_CST
1817  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1818 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1819    ways.  */
1820 (match negate_expr_p
1821  VECTOR_CST
1822  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1823 (match negate_expr_p
1824  (minus @0 @1)
1825  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1826       || (FLOAT_TYPE_P (type)
1827           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1828           && !HONOR_SIGNED_ZEROS (type)))))
1830 /* (-A) * (-B) -> A * B  */
1831 (simplify
1832  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1833   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1834        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1835    (mult (convert @0) (convert (negate @1)))))
1837 /* -(A + B) -> (-B) - A.  */
1838 (simplify
1839  (negate (plus:c @0 negate_expr_p@1))
1840  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1841       && !HONOR_SIGNED_ZEROS (type))
1842   (minus (negate @1) @0)))
1844 /* -(A - B) -> B - A.  */
1845 (simplify
1846  (negate (minus @0 @1))
1847  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1848       || (FLOAT_TYPE_P (type)
1849           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1850           && !HONOR_SIGNED_ZEROS (type)))
1851   (minus @1 @0)))
1852 (simplify
1853  (negate (pointer_diff @0 @1))
1854  (if (TYPE_OVERFLOW_UNDEFINED (type))
1855   (pointer_diff @1 @0)))
1857 /* A - B -> A + (-B) if B is easily negatable.  */
1858 (simplify
1859  (minus @0 negate_expr_p@1)
1860  (if (!FIXED_POINT_TYPE_P (type))
1861  (plus @0 (negate @1))))
1863 /* 1 - a is a ^ 1 if a had a bool range. */
1864 /* This is only enabled for gimple as sometimes
1865    cfun is not set for the function which contains
1866    the SSA_NAME (e.g. while IPA passes are happening,
1867    fold might be called).  */
1868 (simplify
1869  (minus integer_onep@0 SSA_NAME@1)
1870   (if (INTEGRAL_TYPE_P (type)
1871        && ssa_name_has_boolean_range (@1))
1872    (bit_xor @1 @0)))
1874 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1875 (simplify
1876  (negate (mult:c@0 @1 negate_expr_p@2))
1877  (if (! TYPE_UNSIGNED (type)
1878       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1879       && single_use (@0))
1880   (mult @1 (negate @2))))
1882 (simplify
1883  (negate (rdiv@0 @1 negate_expr_p@2))
1884  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1885       && single_use (@0))
1886   (rdiv @1 (negate @2))))
1888 (simplify
1889  (negate (rdiv@0 negate_expr_p@1 @2))
1890  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1891       && single_use (@0))
1892   (rdiv (negate @1) @2)))
1894 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1895 (simplify
1896  (negate (convert? (rshift @0 INTEGER_CST@1)))
1897  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1898       && wi::to_wide (@1) == element_precision (type) - 1)
1899   (with { tree stype = TREE_TYPE (@0);
1900           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1901                                              : unsigned_type_for (stype); }
1902    (if (VECTOR_TYPE_P (type))
1903     (view_convert (rshift (view_convert:ntype @0) @1))
1904     (convert (rshift (convert:ntype @0) @1))))))
1906 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1907    when profitable.
1908    For bitwise binary operations apply operand conversions to the
1909    binary operation result instead of to the operands.  This allows
1910    to combine successive conversions and bitwise binary operations.
1911    We combine the above two cases by using a conditional convert.  */
1912 (for bitop (bit_and bit_ior bit_xor)
1913  (simplify
1914   (bitop (convert@2 @0) (convert?@3 @1))
1915   (if (((TREE_CODE (@1) == INTEGER_CST
1916          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1917          && (int_fits_type_p (@1, TREE_TYPE (@0))
1918              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1919         || types_match (@0, @1))
1920        && !POINTER_TYPE_P (TREE_TYPE (@0))
1921        && !VECTOR_TYPE_P (TREE_TYPE (@0))
1922        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
1923        /* ???  This transform conflicts with fold-const.cc doing
1924           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1925           constants (if x has signed type, the sign bit cannot be set
1926           in c).  This folds extension into the BIT_AND_EXPR.
1927           Restrict it to GIMPLE to avoid endless recursions.  */
1928        && (bitop != BIT_AND_EXPR || GIMPLE)
1929        && (/* That's a good idea if the conversion widens the operand, thus
1930               after hoisting the conversion the operation will be narrower.
1931               It is also a good if the conversion is a nop as moves the
1932               conversion to one side; allowing for combining of the conversions.  */
1933            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1934            /* The conversion check for being a nop can only be done at the gimple
1935               level as fold_binary has some re-association code which can conflict
1936               with this if there is a "constant" which is not a full INTEGER_CST.  */
1937            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1938            /* It's also a good idea if the conversion is to a non-integer
1939               mode.  */
1940            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1941            /* Or if the precision of TO is not the same as the precision
1942               of its mode.  */
1943            || !type_has_mode_precision_p (type)
1944            /* In GIMPLE, getting rid of 2 conversions for one new results
1945               in smaller IL.  */
1946            || (GIMPLE
1947                && TREE_CODE (@1) != INTEGER_CST
1948                && tree_nop_conversion_p (type, TREE_TYPE (@0))
1949                && single_use (@2)
1950                && single_use (@3))))
1951    (convert (bitop @0 (convert @1)))))
1952  /* In GIMPLE, getting rid of 2 conversions for one new results
1953     in smaller IL.  */
1954  (simplify
1955   (convert (bitop:cs@2 (nop_convert:s @0) @1))
1956   (if (GIMPLE
1957        && TREE_CODE (@1) != INTEGER_CST
1958        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1959        && types_match (type, @0)
1960        && !POINTER_TYPE_P (TREE_TYPE (@0))
1961        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
1962    (bitop @0 (convert @1)))))
1964 (for bitop (bit_and bit_ior)
1965      rbitop (bit_ior bit_and)
1966   /* (x | y) & x -> x */
1967   /* (x & y) | x -> x */
1968  (simplify
1969   (bitop:c (rbitop:c @0 @1) @0)
1970   @0)
1971  /* (~x | y) & x -> x & y */
1972  /* (~x & y) | x -> x | y */
1973  (simplify
1974   (bitop:c (rbitop:c @2 @1) @0)
1975   (with { bool wascmp; }
1976    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1977         && (!wascmp || element_precision (type) == 1))
1978     (bitop @0 @1)))))
1980 /* ((x | y) & z) | x -> (z & y) | x
1981    ((x ^ y) & z) | x -> (z & y) | x  */
1982 (for op (bit_ior bit_xor)
1983  (simplify
1984   (bit_ior:c (nop_convert1?:s
1985                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
1986   (if (bitwise_equal_p (@0, @3))
1987    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
1989 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1990 (simplify
1991   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1992   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
1994 /* Combine successive equal operations with constants.  */
1995 (for bitop (bit_and bit_ior bit_xor)
1996  (simplify
1997   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1998   (if (!CONSTANT_CLASS_P (@0))
1999    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2000       folded to a constant.  */
2001    (bitop @0 (bitop! @1 @2))
2002    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2003       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2004       the values involved are such that the operation can't be decided at
2005       compile time.  Try folding one of @0 or @1 with @2 to see whether
2006       that combination can be decided at compile time.
2008       Keep the existing form if both folds fail, to avoid endless
2009       oscillation.  */
2010    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2011     (if (cst1)
2012      (bitop @1 { cst1; })
2013      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2014       (if (cst2)
2015        (bitop @0 { cst2; }))))))))
2017 /* Try simple folding for X op !X, and X op X with the help
2018    of the truth_valued_p and logical_inverted_value predicates.  */
2019 (match truth_valued_p
2020  @0
2021  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2022 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2023  (match truth_valued_p
2024   (op @0 @1)))
2025 (match truth_valued_p
2026   (truth_not @0))
2028 (match (logical_inverted_value @0)
2029  (truth_not @0))
2030 (match (logical_inverted_value @0)
2031  (bit_not truth_valued_p@0))
2032 (match (logical_inverted_value @0)
2033  (eq @0 integer_zerop))
2034 (match (logical_inverted_value @0)
2035  (ne truth_valued_p@0 integer_truep))
2036 (match (logical_inverted_value @0)
2037  (bit_xor truth_valued_p@0 integer_truep))
2039 /* X & !X -> 0.  */
2040 (simplify
2041  (bit_and:c @0 (logical_inverted_value @0))
2042  { build_zero_cst (type); })
2043 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2044 (for op (bit_ior bit_xor)
2045  (simplify
2046   (op:c truth_valued_p@0 (logical_inverted_value @0))
2047   { constant_boolean_node (true, type); }))
2048 /* X ==/!= !X is false/true.  */
2049 (for op (eq ne)
2050  (simplify
2051   (op:c truth_valued_p@0 (logical_inverted_value @0))
2052   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2054 /* ~~x -> x */
2055 (simplify
2056   (bit_not (bit_not @0))
2057   @0)
2059 /* zero_one_valued_p will match when a value is known to be either
2060    0 or 1 including constants 0 or 1.
2061    Signed 1-bits includes -1 so they cannot match here. */
2062 (match zero_one_valued_p
2063  @0
2064  (if (INTEGRAL_TYPE_P (type)
2065       && (TYPE_UNSIGNED (type)
2066           || TYPE_PRECISION (type) > 1)
2067       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2068 (match zero_one_valued_p
2069  truth_valued_p@0
2070  (if (INTEGRAL_TYPE_P (type)
2071       && (TYPE_UNSIGNED (type)
2072           || TYPE_PRECISION (type) > 1))))
2074 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2075 (simplify
2076  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2077  (if (INTEGRAL_TYPE_P (type))
2078   (bit_and @0 @1)))
2080 (for cmp (tcc_comparison)
2081      icmp (inverted_tcc_comparison)
2082  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2083  (simplify
2084   (bit_ior
2085    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2086    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2087     (if (INTEGRAL_TYPE_P (type)
2088          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2089          /* The scalar version has to be canonicalized after vectorization
2090             because it makes unconditional loads conditional ones, which
2091             means we lose vectorization because the loads may trap.  */
2092          && canonicalize_math_after_vectorization_p ())
2093      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2095  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2096     canonicalized further and we recognize the conditional form:
2097     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2098  (simplify
2099   (bit_ior
2100    (cond (cmp@0  @01 @02) @3 zerop)
2101    (cond (icmp@4 @01 @02) @5 zerop))
2102     (if (INTEGRAL_TYPE_P (type)
2103          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2104          /* The scalar version has to be canonicalized after vectorization
2105             because it makes unconditional loads conditional ones, which
2106             means we lose vectorization because the loads may trap.  */
2107          && canonicalize_math_after_vectorization_p ())
2108     (cond @0 @3 @5)))
2110  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2111     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2112  (simplify
2113   (bit_ior
2114    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2115    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2116     (if (integer_zerop (@5)
2117          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2118      (switch
2119       (if (integer_onep (@4))
2120        (bit_and (vec_cond @0 @2 @3) @4))
2121         (if (integer_minus_onep (@4))
2122          (vec_cond @0 @2 @3)))
2123     (if (integer_zerop (@4)
2124          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2125      (switch
2126       (if (integer_onep (@5))
2127        (bit_and (vec_cond @0 @3 @2) @5))
2128       (if (integer_minus_onep (@5))
2129        (vec_cond @0 @3 @2))))))
2131  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2132     into a < b ? d : c.  */
2133  (simplify
2134   (bit_ior
2135    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2136    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2137   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2138    (vec_cond @0 @2 @3))))
2140 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2141 (simplify
2142  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2143  (if (INTEGRAL_TYPE_P (type)
2144       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2145       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2146       /* Sign extending of the neg or a truncation of the neg
2147          is needed. */
2148       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2149           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2150   (mult (convert @0) @1)))
2152 /* Narrow integer multiplication by a zero_one_valued_p operand.
2153    Multiplication by [0,1] is guaranteed not to overflow.  */
2154 (simplify
2155  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2156  (if (INTEGRAL_TYPE_P (type)
2157       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2158       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2159   (mult (convert @1) (convert @2))))
2161 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2162    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2163    as some targets (such as x86's SSE) may return zero for larger C.  */
2164 (simplify
2165   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2166   (if (tree_fits_shwi_p (@1)
2167        && tree_to_shwi (@1) > 0
2168        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2169     (convert @0)))
2171 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2172    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2173    as some targets (such as x86's SSE) may return zero for larger C.  */
2174 (simplify
2175   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2176   (if (tree_fits_shwi_p (@1)
2177        && tree_to_shwi (@1) > 0
2178        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2179     (eq @0 @2)))
2181 /* Convert ~ (-A) to A - 1.  */
2182 (simplify
2183  (bit_not (convert? (negate @0)))
2184  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2185       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2186   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2188 /* Convert - (~A) to A + 1.  */
2189 (simplify
2190  (negate (nop_convert? (bit_not @0)))
2191  (plus (view_convert @0) { build_each_one_cst (type); }))
2193 /* (a & b) ^ (a == b) -> !(a | b) */
2194 /* (a & b) == (a ^ b) -> !(a | b) */
2195 (for first_op (bit_xor eq)
2196      second_op (eq bit_xor)
2197  (simplify
2198   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2199     (bit_not (bit_ior @0 @1))))
2201 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2202 (simplify
2203  (bit_not (convert? (minus @0 integer_each_onep)))
2204  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2205       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2206   (convert (negate @0))))
2207 (simplify
2208  (bit_not (convert? (plus @0 integer_all_onesp)))
2209  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2210       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2211   (convert (negate @0))))
2213 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2214 (simplify
2215  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2216  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2217   (convert (bit_xor @0 (bit_not @1)))))
2218 (simplify
2219  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2220  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2221   (convert (bit_xor @0 @1))))
2223 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2224 (simplify
2225  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2226  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2227   (bit_not (bit_xor (view_convert @0) @1))))
2229 /* ~(a ^ b) is a == b for truth valued a and b.  */
2230 (simplify
2231  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2232  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2233       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2234   (convert (eq @0 @1))))
2236 /* (~a) == b is a ^ b for truth valued a and b.  */
2237 (simplify
2238  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2239  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2240       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2241   (convert (bit_xor @0 @1))))
2243 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2244 (simplify
2245  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2246  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2248 /* Fold A - (A & B) into ~B & A.  */
2249 (simplify
2250  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2251  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2252       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2253   (convert (bit_and (bit_not @1) @0))))
2255 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2256 (if (!canonicalize_math_p ())
2257  (for cmp (tcc_comparison)
2258   (simplify
2259    (mult:c (convert (cmp@0 @1 @2)) @3)
2260    (if (INTEGRAL_TYPE_P (type)
2261         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2262      (cond @0 @3 { build_zero_cst (type); })))
2263 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2264   (simplify
2265    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2266    (if (INTEGRAL_TYPE_P (type)
2267         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2268      (cond @0 @3 { build_zero_cst (type); })))
2272 /* For integral types with undefined overflow and C != 0 fold
2273    x * C EQ/NE y * C into x EQ/NE y.  */
2274 (for cmp (eq ne)
2275  (simplify
2276   (cmp (mult:c @0 @1) (mult:c @2 @1))
2277   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2278        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2279        && tree_expr_nonzero_p (@1))
2280    (cmp @0 @2))))
2282 /* For integral types with wrapping overflow and C odd fold
2283    x * C EQ/NE y * C into x EQ/NE y.  */
2284 (for cmp (eq ne)
2285  (simplify
2286   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2287   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2288        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2289        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2290    (cmp @0 @2))))
2292 /* For integral types with undefined overflow and C != 0 fold
2293    x * C RELOP y * C into:
2295    x RELOP y for nonnegative C
2296    y RELOP x for negative C  */
2297 (for cmp (lt gt le ge)
2298  (simplify
2299   (cmp (mult:c @0 @1) (mult:c @2 @1))
2300   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2301        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2302    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2303     (cmp @0 @2)
2304    (if (TREE_CODE (@1) == INTEGER_CST
2305         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2306     (cmp @2 @0))))))
2308 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2309 (for cmp (le gt)
2310      icmp (gt le)
2311  (simplify
2312   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2313    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2314         && TYPE_UNSIGNED (TREE_TYPE (@0))
2315         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2316         && (wi::to_wide (@2)
2317             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2318     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2319      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2321 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2322 (for cmp (simple_comparison)
2323  (simplify
2324   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2325   (if (element_precision (@3) >= element_precision (@0)
2326        && types_match (@0, @1))
2327    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2328     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2329      (cmp @1 @0)
2330      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2331       (with
2332        {
2333         tree utype = unsigned_type_for (TREE_TYPE (@0));
2334        }
2335        (cmp (convert:utype @1) (convert:utype @0)))))
2336     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2337      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2338       (cmp @0 @1)
2339       (with
2340        {
2341         tree utype = unsigned_type_for (TREE_TYPE (@0));
2342        }
2343        (cmp (convert:utype @0) (convert:utype @1)))))))))
2345 /* X / C1 op C2 into a simple range test.  */
2346 (for cmp (simple_comparison)
2347  (simplify
2348   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2349   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2350        && integer_nonzerop (@1)
2351        && !TREE_OVERFLOW (@1)
2352        && !TREE_OVERFLOW (@2))
2353    (with { tree lo, hi; bool neg_overflow;
2354            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2355                                                    &neg_overflow); }
2356     (switch
2357      (if (code == LT_EXPR || code == GE_EXPR)
2358        (if (TREE_OVERFLOW (lo))
2359         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2360         (if (code == LT_EXPR)
2361          (lt @0 { lo; })
2362          (ge @0 { lo; }))))
2363      (if (code == LE_EXPR || code == GT_EXPR)
2364        (if (TREE_OVERFLOW (hi))
2365         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2366         (if (code == LE_EXPR)
2367          (le @0 { hi; })
2368          (gt @0 { hi; }))))
2369      (if (!lo && !hi)
2370       { build_int_cst (type, code == NE_EXPR); })
2371      (if (code == EQ_EXPR && !hi)
2372       (ge @0 { lo; }))
2373      (if (code == EQ_EXPR && !lo)
2374       (le @0 { hi; }))
2375      (if (code == NE_EXPR && !hi)
2376       (lt @0 { lo; }))
2377      (if (code == NE_EXPR && !lo)
2378       (gt @0 { hi; }))
2379      (if (GENERIC)
2380       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2381                            lo, hi); })
2382      (with
2383       {
2384         tree etype = range_check_type (TREE_TYPE (@0));
2385         if (etype)
2386           {
2387             hi = fold_convert (etype, hi);
2388             lo = fold_convert (etype, lo);
2389             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2390           }
2391       }
2392       (if (etype && hi && !TREE_OVERFLOW (hi))
2393        (if (code == EQ_EXPR)
2394         (le (minus (convert:etype @0) { lo; }) { hi; })
2395         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2397 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2398 (for op (lt le ge gt)
2399  (simplify
2400   (op (plus:c @0 @2) (plus:c @1 @2))
2401   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2402        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2403    (op @0 @1))))
2405 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2406    when C is an unsigned integer constant with only the MSB set, and X and
2407    Y have types of equal or lower integer conversion rank than C's.  */
2408 (for op (lt le ge gt)
2409  (simplify
2410   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2411   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2412        && TYPE_UNSIGNED (TREE_TYPE (@0))
2413        && wi::only_sign_bit_p (wi::to_wide (@0)))
2414    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2415     (op (convert:stype @1) (convert:stype @2))))))
2417 /* For equality and subtraction, this is also true with wrapping overflow.  */
2418 (for op (eq ne minus)
2419  (simplify
2420   (op (plus:c @0 @2) (plus:c @1 @2))
2421   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2422        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2423            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2424    (op @0 @1))))
2426 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2427 (for op (lt le ge gt)
2428  (simplify
2429   (op (minus @0 @2) (minus @1 @2))
2430   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2431        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2432    (op @0 @1))))
2433 /* For equality and subtraction, this is also true with wrapping overflow.  */
2434 (for op (eq ne minus)
2435  (simplify
2436   (op (minus @0 @2) (minus @1 @2))
2437   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2438        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2439            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2440    (op @0 @1))))
2441 /* And for pointers...  */
2442 (for op (simple_comparison)
2443  (simplify
2444   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2445   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2446    (op @0 @1))))
2447 (simplify
2448  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2449  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2450       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2451   (pointer_diff @0 @1)))
2453 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2454 (for op (lt le ge gt)
2455  (simplify
2456   (op (minus @2 @0) (minus @2 @1))
2457   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2458        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2459    (op @1 @0))))
2460 /* For equality and subtraction, this is also true with wrapping overflow.  */
2461 (for op (eq ne minus)
2462  (simplify
2463   (op (minus @2 @0) (minus @2 @1))
2464   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2465        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2466            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2467    (op @1 @0))))
2468 /* And for pointers...  */
2469 (for op (simple_comparison)
2470  (simplify
2471   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2472   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2473    (op @1 @0))))
2474 (simplify
2475  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2476  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2477       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2478   (pointer_diff @1 @0)))
2480 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2481 (for op (lt le gt ge)
2482  (simplify
2483   (op:c (plus:c@2 @0 @1) @1)
2484   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2485        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2486        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2487        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2488    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2489 /* For equality, this is also true with wrapping overflow.  */
2490 (for op (eq ne)
2491  (simplify
2492   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2493   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2494        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2495            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2496        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2497        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2498        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2499    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2500  (simplify
2501   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2502   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2503        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2504        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2505    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2507 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2508 (for neeq (ne eq)
2509  (simplify
2510   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2511    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2512     (if (ptr_difference_const (@0, @2, &diff))
2513      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2514  (simplify
2515   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2516    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2517     (if (ptr_difference_const (@0, @2, &diff))
2518      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2520 /* X - Y < X is the same as Y > 0 when there is no overflow.
2521    For equality, this is also true with wrapping overflow.  */
2522 (for op (simple_comparison)
2523  (simplify
2524   (op:c @0 (minus@2 @0 @1))
2525   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2526        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2527            || ((op == EQ_EXPR || op == NE_EXPR)
2528                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2529        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2530    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2532 /* Transform:
2533    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2534    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2535 (for cmp (eq ne)
2536      ocmp (lt ge)
2537  (simplify
2538   (cmp (trunc_div @0 @1) integer_zerop)
2539   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2540        /* Complex ==/!= is allowed, but not </>=.  */
2541        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2542        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2543    (ocmp @0 @1))))
2545 /* X == C - X can never be true if C is odd.  */
2546 (for cmp (eq ne)
2547  (simplify
2548   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2549   (if (TREE_INT_CST_LOW (@1) & 1)
2550    { constant_boolean_node (cmp == NE_EXPR, type); })))
2552 /* Arguments on which one can call get_nonzero_bits to get the bits
2553    possibly set.  */
2554 (match with_possible_nonzero_bits
2555  INTEGER_CST@0)
2556 (match with_possible_nonzero_bits
2557  SSA_NAME@0
2558  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2559 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2560 (match (with_possible_nonzero_bits2 @0)
2561  with_possible_nonzero_bits@0)
2562 (match (with_possible_nonzero_bits2 @0)
2563  (bit_and:c with_possible_nonzero_bits@0 @2))
2565 /* Same for bits that are known to be set, but we do not have
2566    an equivalent to get_nonzero_bits yet.  */
2567 (match (with_certain_nonzero_bits2 @0)
2568  INTEGER_CST@0)
2569 (match (with_certain_nonzero_bits2 @0)
2570  (bit_ior @1 INTEGER_CST@0))
2572 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2573 (for cmp (eq ne)
2574  (simplify
2575   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2576   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2577    { constant_boolean_node (cmp == NE_EXPR, type); })))
2579 /* ((X inner_op C0) outer_op C1)
2580    With X being a tree where value_range has reasoned certain bits to always be
2581    zero throughout its computed value range,
2582    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2583    where zero_mask has 1's for all bits that are sure to be 0 in
2584    and 0's otherwise.
2585    if (inner_op == '^') C0 &= ~C1;
2586    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2587    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2589 (for inner_op (bit_ior bit_xor)
2590      outer_op (bit_xor bit_ior)
2591 (simplify
2592  (outer_op
2593   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2594  (with
2595   {
2596     bool fail = false;
2597     wide_int zero_mask_not;
2598     wide_int C0;
2599     wide_int cst_emit;
2601     if (TREE_CODE (@2) == SSA_NAME)
2602       zero_mask_not = get_nonzero_bits (@2);
2603     else
2604       fail = true;
2606     if (inner_op == BIT_XOR_EXPR)
2607       {
2608         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2609         cst_emit = C0 | wi::to_wide (@1);
2610       }
2611     else
2612       {
2613         C0 = wi::to_wide (@0);
2614         cst_emit = C0 ^ wi::to_wide (@1);
2615       }
2616   }
2617   (if (!fail && (C0 & zero_mask_not) == 0)
2618    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2619    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2620     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2622 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2623 (simplify
2624   (pointer_plus (pointer_plus:s @0 @1) @3)
2625   (pointer_plus @0 (plus @1 @3)))
2626 #if GENERIC
2627 (simplify
2628   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2629   (convert:type (pointer_plus @0 (plus @1 @3))))
2630 #endif
2632 /* Pattern match
2633      tem1 = (long) ptr1;
2634      tem2 = (long) ptr2;
2635      tem3 = tem2 - tem1;
2636      tem4 = (unsigned long) tem3;
2637      tem5 = ptr1 + tem4;
2638    and produce
2639      tem5 = ptr2;  */
2640 (simplify
2641   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2642   /* Conditionally look through a sign-changing conversion.  */
2643   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2644        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2645             || (GENERIC && type == TREE_TYPE (@1))))
2646    @1))
2647 (simplify
2648   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2649   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2650    (convert @1)))
2652 /* Pattern match
2653      tem = (sizetype) ptr;
2654      tem = tem & algn;
2655      tem = -tem;
2656      ... = ptr p+ tem;
2657    and produce the simpler and easier to analyze with respect to alignment
2658      ... = ptr & ~algn;  */
2659 (simplify
2660   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2661   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2662    (bit_and @0 { algn; })))
2664 /* Try folding difference of addresses.  */
2665 (simplify
2666  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2667  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2668   (with { poly_int64 diff; }
2669    (if (ptr_difference_const (@0, @1, &diff))
2670     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2671 (simplify
2672  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2673  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2674   (with { poly_int64 diff; }
2675    (if (ptr_difference_const (@0, @1, &diff))
2676     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2677 (simplify
2678  (minus (convert ADDR_EXPR@0) (convert @1))
2679  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2680   (with { poly_int64 diff; }
2681    (if (ptr_difference_const (@0, @1, &diff))
2682     { build_int_cst_type (type, diff); }))))
2683 (simplify
2684  (minus (convert @0) (convert ADDR_EXPR@1))
2685  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2686   (with { poly_int64 diff; }
2687    (if (ptr_difference_const (@0, @1, &diff))
2688     { build_int_cst_type (type, diff); }))))
2689 (simplify
2690  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2691  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2692       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2693   (with { poly_int64 diff; }
2694    (if (ptr_difference_const (@0, @1, &diff))
2695     { build_int_cst_type (type, diff); }))))
2696 (simplify
2697  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2698  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2699       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2700   (with { poly_int64 diff; }
2701    (if (ptr_difference_const (@0, @1, &diff))
2702     { build_int_cst_type (type, diff); }))))
2704 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2705 (simplify
2706  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2707  (with { poly_int64 diff; }
2708    (if (ptr_difference_const (@0, @2, &diff))
2709     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2710 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2711 (simplify
2712  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2713  (with { poly_int64 diff; }
2714    (if (ptr_difference_const (@0, @2, &diff))
2715     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2716 (simplify
2717  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2718  (with { poly_int64 diff; }
2719    (if (ptr_difference_const (@0, @1, &diff))
2720     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2722 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2723 (simplify
2724  (convert (pointer_diff @0 INTEGER_CST@1))
2725  (if (POINTER_TYPE_P (type))
2726   { build_fold_addr_expr_with_type
2727       (build2 (MEM_REF, char_type_node, @0,
2728                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2729                type); }))
2731 /* If arg0 is derived from the address of an object or function, we may
2732    be able to fold this expression using the object or function's
2733    alignment.  */
2734 (simplify
2735  (bit_and (convert? @0) INTEGER_CST@1)
2736  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2737       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2738   (with
2739    {
2740      unsigned int align;
2741      unsigned HOST_WIDE_INT bitpos;
2742      get_pointer_alignment_1 (@0, &align, &bitpos);
2743    }
2744    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2745     { wide_int_to_tree (type, (wi::to_wide (@1)
2746                                & (bitpos / BITS_PER_UNIT))); }))))
2748 (match min_value
2749  INTEGER_CST
2750  (if ((INTEGRAL_TYPE_P (type)
2751        || POINTER_TYPE_P(type))
2752       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2754 (match max_value
2755  INTEGER_CST
2756  (if ((INTEGRAL_TYPE_P (type)
2757        || POINTER_TYPE_P(type))
2758       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2760 /* x >  y  &&  x != XXX_MIN  -->  x > y
2761    x >  y  &&  x == XXX_MIN  -->  false . */
2762 (for eqne (eq ne)
2763  (simplify
2764   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2765    (switch
2766     (if (eqne == EQ_EXPR)
2767      { constant_boolean_node (false, type); })
2768     (if (eqne == NE_EXPR)
2769      @2)
2770     )))
2772 /* x <  y  &&  x != XXX_MAX  -->  x < y
2773    x <  y  &&  x == XXX_MAX  -->  false.  */
2774 (for eqne (eq ne)
2775  (simplify
2776   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2777    (switch
2778     (if (eqne == EQ_EXPR)
2779      { constant_boolean_node (false, type); })
2780     (if (eqne == NE_EXPR)
2781      @2)
2782     )))
2784 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2785 (simplify
2786  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2787   @2)
2789 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2790 (simplify
2791  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2792   @2)
2794 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2795 (simplify
2796  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2797   @2)
2799 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2800 (simplify
2801  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2802   { constant_boolean_node (true, type); })
2804 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2805 (simplify
2806  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2807   @2)
2809 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2810 (simplify
2811  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2812   @2)
2814 /* x >=  y  ||  x != XXX_MAX   -->  true
2815    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2816 (for eqne (eq ne)
2817  (simplify
2818   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2819    (switch
2820     (if (eqne == EQ_EXPR)
2821      @2)
2822     (if (eqne == NE_EXPR)
2823      { constant_boolean_node (true, type); }))))
2825 /* y == XXX_MIN || x < y --> x <= y - 1 */
2826 (simplify
2827  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2828   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2829        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2830   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2832 /* y != XXX_MIN && x >= y --> x > y - 1 */
2833 (simplify
2834  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2835   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2836        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2837   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2839 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2840    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2841 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2842    Similarly for (X != Y).  */
2844 (for code1 (eq ne)
2845  (for code2 (eq ne lt gt le ge)
2846   (simplify
2847    (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
2848    (if ((TREE_CODE (@1) == INTEGER_CST
2849          && TREE_CODE (@2) == INTEGER_CST)
2850         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2851              || POINTER_TYPE_P (TREE_TYPE (@1)))
2852             && operand_equal_p (@1, @2)))
2853     (with
2854      {
2855       int cmp = 0;
2856       if (TREE_CODE (@1) == INTEGER_CST
2857           && TREE_CODE (@2) == INTEGER_CST)
2858         cmp = tree_int_cst_compare (@1, @2);
2859       bool val;
2860       switch (code2)
2861          {
2862         case EQ_EXPR: val = (cmp == 0); break;
2863         case NE_EXPR: val = (cmp != 0); break;
2864         case LT_EXPR: val = (cmp < 0); break;
2865         case GT_EXPR: val = (cmp > 0); break;
2866         case LE_EXPR: val = (cmp <= 0); break;
2867         case GE_EXPR: val = (cmp >= 0); break;
2868         default: gcc_unreachable ();
2869         }
2870      }
2871      (switch
2872       (if (code1 == EQ_EXPR && val) @3)
2873       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2874       (if (code1 == NE_EXPR && !val) @4)
2875       (if (code1 == NE_EXPR
2876            && code2 == GE_EXPR
2877            && cmp == 0)
2878        (gt @0 @1))
2879       (if (code1 == NE_EXPR
2880            && code2 == LE_EXPR
2881            && cmp == 0)
2882        (lt @0 @1))
2883      )
2884     )
2885    )
2886   )
2890 /* Convert (X OP1 CST1) && (X OP2 CST2).
2891    Convert (X OP1 Y) && (X OP2 Y).  */
2893 (for code1 (lt le gt ge)
2894  (for code2 (lt le gt ge)
2895   (simplify
2896   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
2897   (if ((TREE_CODE (@1) == INTEGER_CST
2898         && TREE_CODE (@2) == INTEGER_CST)
2899        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2900             || POINTER_TYPE_P (TREE_TYPE (@1)))
2901            && operand_equal_p (@1, @2)))
2902    (with
2903     {
2904      int cmp = 0;
2905      if (TREE_CODE (@1) == INTEGER_CST
2906          && TREE_CODE (@2) == INTEGER_CST)
2907        cmp = tree_int_cst_compare (@1, @2);
2908     }
2909     (switch
2910      /* Choose the more restrictive of two < or <= comparisons.  */
2911      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2912           && (code2 == LT_EXPR || code2 == LE_EXPR))
2913       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2914        @3
2915        @4))
2916      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2917      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2918           && (code2 == GT_EXPR || code2 == GE_EXPR))
2919       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2920        @3
2921        @4))
2922      /* Check for singleton ranges.  */
2923      (if (cmp == 0
2924           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2925             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2926       (eq @0 @1))
2927      /* Check for disjoint ranges.  */
2928      (if (cmp <= 0
2929           && (code1 == LT_EXPR || code1 == LE_EXPR)
2930           && (code2 == GT_EXPR || code2 == GE_EXPR))
2931       { constant_boolean_node (false, type); })
2932      (if (cmp >= 0
2933           && (code1 == GT_EXPR || code1 == GE_EXPR)
2934           && (code2 == LT_EXPR || code2 == LE_EXPR))
2935       { constant_boolean_node (false, type); })
2936      ))))))
2938 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2939    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2940 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
2941    Similarly for (X != Y).  */
2943 (for code1 (eq ne)
2944  (for code2 (eq ne lt gt le ge)
2945   (simplify
2946    (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
2947    (if ((TREE_CODE (@1) == INTEGER_CST
2948          && TREE_CODE (@2) == INTEGER_CST)
2949         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2950             || POINTER_TYPE_P (TREE_TYPE (@1)))
2951             && operand_equal_p (@1, @2)))
2952     (with
2953      {
2954       int cmp = 0;
2955       if (TREE_CODE (@1) == INTEGER_CST
2956           && TREE_CODE (@2) == INTEGER_CST)
2957         cmp = tree_int_cst_compare (@1, @2);
2958       bool val;
2959       switch (code2)
2960         {
2961         case EQ_EXPR: val = (cmp == 0); break;
2962         case NE_EXPR: val = (cmp != 0); break;
2963         case LT_EXPR: val = (cmp < 0); break;
2964         case GT_EXPR: val = (cmp > 0); break;
2965         case LE_EXPR: val = (cmp <= 0); break;
2966         case GE_EXPR: val = (cmp >= 0); break;
2967         default: gcc_unreachable ();
2968         }
2969      }
2970      (switch
2971       (if (code1 == EQ_EXPR && val) @4)
2972       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2973       (if (code1 == NE_EXPR && !val) @3)
2974       (if (code1 == EQ_EXPR
2975            && code2 == GT_EXPR
2976            && cmp == 0)
2977        (ge @0 @1))
2978       (if (code1 == EQ_EXPR
2979            && code2 == LT_EXPR
2980            && cmp == 0)
2981        (le @0 @1))
2982      )
2983     )
2984    )
2985   )
2989 /* Convert (X OP1 CST1) || (X OP2 CST2).
2990    Convert (X OP1 Y)    || (X OP2 Y).  */
2992 (for code1 (lt le gt ge)
2993  (for code2 (lt le gt ge)
2994   (simplify
2995   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
2996   (if ((TREE_CODE (@1) == INTEGER_CST
2997         && TREE_CODE (@2) == INTEGER_CST)
2998        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2999             || POINTER_TYPE_P (TREE_TYPE (@1)))
3000            && operand_equal_p (@1, @2)))
3001    (with
3002     {
3003      int cmp = 0;
3004      if (TREE_CODE (@1) == INTEGER_CST
3005          && TREE_CODE (@2) == INTEGER_CST)
3006        cmp = tree_int_cst_compare (@1, @2);
3007     }
3008     (switch
3009      /* Choose the more restrictive of two < or <= comparisons.  */
3010      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3011           && (code2 == LT_EXPR || code2 == LE_EXPR))
3012       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3013        @4
3014        @3))
3015      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3016      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3017           && (code2 == GT_EXPR || code2 == GE_EXPR))
3018       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3019        @4
3020        @3))
3021      /* Check for singleton ranges.  */
3022      (if (cmp == 0
3023           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3024               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3025       (ne @0 @2))
3026      /* Check for disjoint ranges.  */
3027      (if (cmp >= 0
3028           && (code1 == LT_EXPR || code1 == LE_EXPR)
3029           && (code2 == GT_EXPR || code2 == GE_EXPR))
3030       { constant_boolean_node (true, type); })
3031      (if (cmp <= 0
3032           && (code1 == GT_EXPR || code1 == GE_EXPR)
3033           && (code2 == LT_EXPR || code2 == LE_EXPR))
3034       { constant_boolean_node (true, type); })
3035      ))))))
3037 /* We can't reassociate at all for saturating types.  */
3038 (if (!TYPE_SATURATING (type))
3040  /* Contract negates.  */
3041  /* A + (-B) -> A - B */
3042  (simplify
3043   (plus:c @0 (convert? (negate @1)))
3044   /* Apply STRIP_NOPS on the negate.  */
3045   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3046        && !TYPE_OVERFLOW_SANITIZED (type))
3047    (with
3048     {
3049      tree t1 = type;
3050      if (INTEGRAL_TYPE_P (type)
3051          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3052        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3053     }
3054     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3055  /* A - (-B) -> A + B */
3056  (simplify
3057   (minus @0 (convert? (negate @1)))
3058   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3059        && !TYPE_OVERFLOW_SANITIZED (type))
3060    (with
3061     {
3062      tree t1 = type;
3063      if (INTEGRAL_TYPE_P (type)
3064          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3065        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3066     }
3067     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3068  /* -(T)(-A) -> (T)A
3069     Sign-extension is ok except for INT_MIN, which thankfully cannot
3070     happen without overflow.  */
3071  (simplify
3072   (negate (convert (negate @1)))
3073   (if (INTEGRAL_TYPE_P (type)
3074        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3075            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3076                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3077        && !TYPE_OVERFLOW_SANITIZED (type)
3078        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3079    (convert @1)))
3080  (simplify
3081   (negate (convert negate_expr_p@1))
3082   (if (SCALAR_FLOAT_TYPE_P (type)
3083        && ((DECIMAL_FLOAT_TYPE_P (type)
3084             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3085             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3086            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3087    (convert (negate @1))))
3088  (simplify
3089   (negate (nop_convert? (negate @1)))
3090   (if (!TYPE_OVERFLOW_SANITIZED (type)
3091        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3092    (view_convert @1)))
3094  /* We can't reassociate floating-point unless -fassociative-math
3095     or fixed-point plus or minus because of saturation to +-Inf.  */
3096  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3097       && !FIXED_POINT_TYPE_P (type))
3099   /* Match patterns that allow contracting a plus-minus pair
3100      irrespective of overflow issues.  */
3101   /* (A +- B) - A       ->  +- B */
3102   /* (A +- B) -+ B      ->  A */
3103   /* A - (A +- B)       -> -+ B */
3104   /* A +- (B -+ A)      ->  +- B */
3105   (simplify
3106    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3107    (view_convert @1))
3108   (simplify
3109    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3110    (if (!ANY_INTEGRAL_TYPE_P (type)
3111         || TYPE_OVERFLOW_WRAPS (type))
3112    (negate (view_convert @1))
3113    (view_convert (negate @1))))
3114   (simplify
3115    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3116    (view_convert @0))
3117   (simplify
3118    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3119     (if (!ANY_INTEGRAL_TYPE_P (type)
3120          || TYPE_OVERFLOW_WRAPS (type))
3121      (negate (view_convert @1))
3122      (view_convert (negate @1))))
3123   (simplify
3124    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3125    (view_convert @1))
3126   /* (A +- B) + (C - A)   -> C +- B */
3127   /* (A +  B) - (A - C)   -> B + C */
3128   /* More cases are handled with comparisons.  */
3129   (simplify
3130    (plus:c (plus:c @0 @1) (minus @2 @0))
3131    (plus @2 @1))
3132   (simplify
3133    (plus:c (minus @0 @1) (minus @2 @0))
3134    (minus @2 @1))
3135   (simplify
3136    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3137    (if (TYPE_OVERFLOW_UNDEFINED (type)
3138         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3139     (pointer_diff @2 @1)))
3140   (simplify
3141    (minus (plus:c @0 @1) (minus @0 @2))
3142    (plus @1 @2))
3144   /* (A +- CST1) +- CST2 -> A + CST3
3145      Use view_convert because it is safe for vectors and equivalent for
3146      scalars.  */
3147   (for outer_op (plus minus)
3148    (for inner_op (plus minus)
3149         neg_inner_op (minus plus)
3150     (simplify
3151      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3152                CONSTANT_CLASS_P@2)
3153      /* If one of the types wraps, use that one.  */
3154      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3155       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3156          forever if something doesn't simplify into a constant.  */
3157       (if (!CONSTANT_CLASS_P (@0))
3158        (if (outer_op == PLUS_EXPR)
3159         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3160         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3161       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3162            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3163        (if (outer_op == PLUS_EXPR)
3164         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3165         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3166        /* If the constant operation overflows we cannot do the transform
3167           directly as we would introduce undefined overflow, for example
3168           with (a - 1) + INT_MIN.  */
3169        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3170         (with { tree cst = const_binop (outer_op == inner_op
3171                                         ? PLUS_EXPR : MINUS_EXPR,
3172                                         type, @1, @2); }
3173          (if (cst)
3174           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3175            (inner_op @0 { cst; } )
3176            /* X+INT_MAX+1 is X-INT_MIN.  */
3177            (if (INTEGRAL_TYPE_P (type)
3178                 && wi::to_wide (cst) == wi::min_value (type))
3179             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3180             /* Last resort, use some unsigned type.  */
3181             (with { tree utype = unsigned_type_for (type); }
3182              (if (utype)
3183               (view_convert (inner_op
3184                              (view_convert:utype @0)
3185                              (view_convert:utype
3186                               { TREE_OVERFLOW (cst)
3187                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3189   /* (CST1 - A) +- CST2 -> CST3 - A  */
3190   (for outer_op (plus minus)
3191    (simplify
3192     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3193     /* If one of the types wraps, use that one.  */
3194     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3195      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3196         forever if something doesn't simplify into a constant.  */
3197      (if (!CONSTANT_CLASS_P (@0))
3198       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3199      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3200           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3201       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3202       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3203        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3204         (if (cst && !TREE_OVERFLOW (cst))
3205          (minus { cst; } @0))))))))
3207   /* CST1 - (CST2 - A) -> CST3 + A
3208      Use view_convert because it is safe for vectors and equivalent for
3209      scalars.  */
3210   (simplify
3211    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3212    /* If one of the types wraps, use that one.  */
3213    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3214     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3215       forever if something doesn't simplify into a constant.  */
3216     (if (!CONSTANT_CLASS_P (@0))
3217      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3218     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3219          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3220      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3221      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3222       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3223        (if (cst && !TREE_OVERFLOW (cst))
3224         (plus { cst; } @0)))))))
3226 /* ((T)(A)) + CST -> (T)(A + CST)  */
3227 #if GIMPLE
3228   (simplify
3229    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3230     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3231          && TREE_CODE (type) == INTEGER_TYPE
3232          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3233          && int_fits_type_p (@1, TREE_TYPE (@0)))
3234      /* Perform binary operation inside the cast if the constant fits
3235         and (A + CST)'s range does not overflow.  */
3236      (with
3237       {
3238         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3239                           max_ovf = wi::OVF_OVERFLOW;
3240         tree inner_type = TREE_TYPE (@0);
3242         wide_int w1
3243           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3244                             TYPE_SIGN (inner_type));
3246         value_range vr;
3247         if (get_global_range_query ()->range_of_expr (vr, @0)
3248             && !vr.varying_p () && !vr.undefined_p ())
3249           {
3250             wide_int wmin0 = vr.lower_bound ();
3251             wide_int wmax0 = vr.upper_bound ();
3252             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3253             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3254           }
3255       }
3256      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3257       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3258      )))
3259 #endif
3261 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3262 #if GIMPLE
3263   (for op (plus minus)
3264    (simplify
3265     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3266      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3267           && TREE_CODE (type) == INTEGER_TYPE
3268           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3269           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3270           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3271           && TYPE_OVERFLOW_WRAPS (type))
3272        (plus (convert @0) (op @2 (convert @1))))))
3273 #endif
3275 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3276    to a simple value.  */
3277   (for op (plus minus)
3278    (simplify
3279     (op (convert @0) (convert @1))
3280      (if (INTEGRAL_TYPE_P (type)
3281           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3282           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3283           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3284           && !TYPE_OVERFLOW_TRAPS (type)
3285           && !TYPE_OVERFLOW_SANITIZED (type))
3286       (convert (op! @0 @1)))))
3288   /* ~A + A -> -1 */
3289   (simplify
3290    (plus:c (convert? (bit_not @0)) (convert? @0))
3291    (if (!TYPE_OVERFLOW_TRAPS (type))
3292     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3294   /* ~A + 1 -> -A */
3295   (simplify
3296    (plus (convert? (bit_not @0)) integer_each_onep)
3297    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3298     (negate (convert @0))))
3300   /* -A - 1 -> ~A */
3301   (simplify
3302    (minus (convert? (negate @0)) integer_each_onep)
3303    (if (!TYPE_OVERFLOW_TRAPS (type)
3304         && TREE_CODE (type) != COMPLEX_TYPE
3305         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3306     (bit_not (convert @0))))
3308   /* -1 - A -> ~A */
3309   (simplify
3310    (minus integer_all_onesp @0)
3311    (if (TREE_CODE (type) != COMPLEX_TYPE)
3312     (bit_not @0)))
3314   /* (T)(P + A) - (T)P -> (T) A */
3315   (simplify
3316    (minus (convert (plus:c @@0 @1))
3317     (convert? @0))
3318    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3319         /* For integer types, if A has a smaller type
3320            than T the result depends on the possible
3321            overflow in P + A.
3322            E.g. T=size_t, A=(unsigned)429497295, P>0.
3323            However, if an overflow in P + A would cause
3324            undefined behavior, we can assume that there
3325            is no overflow.  */
3326         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3327             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3328     (convert @1)))
3329   (simplify
3330    (minus (convert (pointer_plus @@0 @1))
3331     (convert @0))
3332    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3333         /* For pointer types, if the conversion of A to the
3334            final type requires a sign- or zero-extension,
3335            then we have to punt - it is not defined which
3336            one is correct.  */
3337         || (POINTER_TYPE_P (TREE_TYPE (@0))
3338             && TREE_CODE (@1) == INTEGER_CST
3339             && tree_int_cst_sign_bit (@1) == 0))
3340     (convert @1)))
3341    (simplify
3342     (pointer_diff (pointer_plus @@0 @1) @0)
3343     /* The second argument of pointer_plus must be interpreted as signed, and
3344        thus sign-extended if necessary.  */
3345     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3346      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3347         second arg is unsigned even when we need to consider it as signed,
3348         we don't want to diagnose overflow here.  */
3349      (convert (view_convert:stype @1))))
3351   /* (T)P - (T)(P + A) -> -(T) A */
3352   (simplify
3353    (minus (convert? @0)
3354     (convert (plus:c @@0 @1)))
3355    (if (INTEGRAL_TYPE_P (type)
3356         && TYPE_OVERFLOW_UNDEFINED (type)
3357         /* For integer literals, using an intermediate unsigned type to avoid
3358            an overflow at run time is counter-productive because it introduces
3359            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3360            the result, which may be problematic in GENERIC for some front-ends:
3361              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3362            so we use the direct path for them.  */
3363         && TREE_CODE (@1) != INTEGER_CST
3364         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3365     (with { tree utype = unsigned_type_for (type); }
3366      (convert (negate (convert:utype @1))))
3367     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3368          /* For integer types, if A has a smaller type
3369             than T the result depends on the possible
3370             overflow in P + A.
3371             E.g. T=size_t, A=(unsigned)429497295, P>0.
3372             However, if an overflow in P + A would cause
3373             undefined behavior, we can assume that there
3374             is no overflow.  */
3375          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3376              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3377      (negate (convert @1)))))
3378   (simplify
3379    (minus (convert @0)
3380     (convert (pointer_plus @@0 @1)))
3381    (if (INTEGRAL_TYPE_P (type)
3382         && TYPE_OVERFLOW_UNDEFINED (type)
3383         /* See above the rationale for this condition.  */
3384         && TREE_CODE (@1) != INTEGER_CST
3385         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3386     (with { tree utype = unsigned_type_for (type); }
3387      (convert (negate (convert:utype @1))))
3388     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3389          /* For pointer types, if the conversion of A to the
3390             final type requires a sign- or zero-extension,
3391             then we have to punt - it is not defined which
3392             one is correct.  */
3393          || (POINTER_TYPE_P (TREE_TYPE (@0))
3394              && TREE_CODE (@1) == INTEGER_CST
3395              && tree_int_cst_sign_bit (@1) == 0))
3396      (negate (convert @1)))))
3397    (simplify
3398     (pointer_diff @0 (pointer_plus @@0 @1))
3399     /* The second argument of pointer_plus must be interpreted as signed, and
3400        thus sign-extended if necessary.  */
3401     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3402      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3403         second arg is unsigned even when we need to consider it as signed,
3404         we don't want to diagnose overflow here.  */
3405      (negate (convert (view_convert:stype @1)))))
3407   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3408   (simplify
3409    (minus (convert (plus:c @@0 @1))
3410     (convert (plus:c @0 @2)))
3411    (if (INTEGRAL_TYPE_P (type)
3412         && TYPE_OVERFLOW_UNDEFINED (type)
3413         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3414         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3415     (with { tree utype = unsigned_type_for (type); }
3416      (convert (minus (convert:utype @1) (convert:utype @2))))
3417     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3418           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3419          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3420              /* For integer types, if A has a smaller type
3421                 than T the result depends on the possible
3422                 overflow in P + A.
3423                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3424                 However, if an overflow in P + A would cause
3425                 undefined behavior, we can assume that there
3426                 is no overflow.  */
3427              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3428                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3429                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3430                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3431      (minus (convert @1) (convert @2)))))
3432   (simplify
3433    (minus (convert (pointer_plus @@0 @1))
3434     (convert (pointer_plus @0 @2)))
3435    (if (INTEGRAL_TYPE_P (type)
3436         && TYPE_OVERFLOW_UNDEFINED (type)
3437         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3438     (with { tree utype = unsigned_type_for (type); }
3439      (convert (minus (convert:utype @1) (convert:utype @2))))
3440     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3441          /* For pointer types, if the conversion of A to the
3442             final type requires a sign- or zero-extension,
3443             then we have to punt - it is not defined which
3444             one is correct.  */
3445          || (POINTER_TYPE_P (TREE_TYPE (@0))
3446              && TREE_CODE (@1) == INTEGER_CST
3447              && tree_int_cst_sign_bit (@1) == 0
3448              && TREE_CODE (@2) == INTEGER_CST
3449              && tree_int_cst_sign_bit (@2) == 0))
3450      (minus (convert @1) (convert @2)))))
3451    (simplify
3452     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3453      (pointer_diff @0 @1))
3454    (simplify
3455     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3456     /* The second argument of pointer_plus must be interpreted as signed, and
3457        thus sign-extended if necessary.  */
3458     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3459      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3460         second arg is unsigned even when we need to consider it as signed,
3461         we don't want to diagnose overflow here.  */
3462      (minus (convert (view_convert:stype @1))
3463             (convert (view_convert:stype @2)))))))
3465 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3466     Modeled after fold_plusminus_mult_expr.  */
3467 (if (!TYPE_SATURATING (type)
3468      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3469  (for plusminus (plus minus)
3470   (simplify
3471    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3472    (if (!ANY_INTEGRAL_TYPE_P (type)
3473         || TYPE_OVERFLOW_WRAPS (type)
3474         || (INTEGRAL_TYPE_P (type)
3475             && tree_expr_nonzero_p (@0)
3476             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3477     (if (single_use (@3) || single_use (@4))
3478      /* If @1 +- @2 is constant require a hard single-use on either
3479         original operand (but not on both).  */
3480      (mult (plusminus @1 @2) @0)
3481      (mult! (plusminus @1 @2) @0)
3482   )))
3483   /* We cannot generate constant 1 for fract.  */
3484   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3485    (simplify
3486     (plusminus @0 (mult:c@3 @0 @2))
3487     (if ((!ANY_INTEGRAL_TYPE_P (type)
3488           || TYPE_OVERFLOW_WRAPS (type)
3489           /* For @0 + @0*@2 this transformation would introduce UB
3490              (where there was none before) for @0 in [-1,0] and @2 max.
3491              For @0 - @0*@2 this transformation would introduce UB
3492              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3493           || (INTEGRAL_TYPE_P (type)
3494               && ((tree_expr_nonzero_p (@0)
3495                    && expr_not_equal_to (@0,
3496                                 wi::minus_one (TYPE_PRECISION (type))))
3497                   || (plusminus == PLUS_EXPR
3498                       ? expr_not_equal_to (@2,
3499                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3500                       /* Let's ignore the @0 -1 and @2 min case.  */
3501                       : (expr_not_equal_to (@2,
3502                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3503                          && expr_not_equal_to (@2,
3504                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3505                                 + 1))))))
3506          && single_use (@3))
3507      (mult (plusminus { build_one_cst (type); } @2) @0)))
3508    (simplify
3509     (plusminus (mult:c@3 @0 @2) @0)
3510     (if ((!ANY_INTEGRAL_TYPE_P (type)
3511           || TYPE_OVERFLOW_WRAPS (type)
3512           /* For @0*@2 + @0 this transformation would introduce UB
3513              (where there was none before) for @0 in [-1,0] and @2 max.
3514              For @0*@2 - @0 this transformation would introduce UB
3515              for @0 0 and @2 min.  */
3516           || (INTEGRAL_TYPE_P (type)
3517               && ((tree_expr_nonzero_p (@0)
3518                    && (plusminus == MINUS_EXPR
3519                        || expr_not_equal_to (@0,
3520                                 wi::minus_one (TYPE_PRECISION (type)))))
3521                   || expr_not_equal_to (@2,
3522                         (plusminus == PLUS_EXPR
3523                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3524                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3525          && single_use (@3))
3526      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3528 #if GIMPLE
3529 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3530    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3531 (simplify
3532  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3533   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3534        && tree_fits_uhwi_p (@1)
3535        && tree_to_uhwi (@1) < element_precision (type)
3536        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3537            || optab_handler (smul_optab,
3538                              TYPE_MODE (type)) != CODE_FOR_nothing))
3539    (with { tree t = type;
3540            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3541            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3542                                              element_precision (type));
3543            w += 1;
3544            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3545                                         : t, w);
3546            cst = build_uniform_cst (t, cst); }
3547     (convert (mult (convert:t @0) { cst; })))))
3548 (simplify
3549  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3550   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3551        && tree_fits_uhwi_p (@1)
3552        && tree_to_uhwi (@1) < element_precision (type)
3553        && tree_fits_uhwi_p (@2)
3554        && tree_to_uhwi (@2) < element_precision (type)
3555        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3556            || optab_handler (smul_optab,
3557                              TYPE_MODE (type)) != CODE_FOR_nothing))
3558    (with { tree t = type;
3559            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3560            unsigned int prec = element_precision (type);
3561            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3562            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3563            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3564                                         : t, w);
3565            cst = build_uniform_cst (t, cst); }
3566     (convert (mult (convert:t @0) { cst; })))))
3567 #endif
3569 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3570    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3571    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3572 (for op (bit_ior bit_xor)
3573  (simplify
3574   (op (mult:s@0 @1 INTEGER_CST@2)
3575       (mult:s@3 @1 INTEGER_CST@4))
3576   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3577        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3578    (mult @1
3579          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3580  (simplify
3581   (op:c (mult:s@0 @1 INTEGER_CST@2)
3582         (lshift:s@3 @1 INTEGER_CST@4))
3583   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3584        && tree_int_cst_sgn (@4) > 0
3585        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3586    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3587            wide_int c = wi::add (wi::to_wide (@2),
3588                                  wi::lshift (wone, wi::to_wide (@4))); }
3589     (mult @1 { wide_int_to_tree (type, c); }))))
3590  (simplify
3591   (op:c (mult:s@0 @1 INTEGER_CST@2)
3592         @1)
3593   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3594        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3595    (mult @1
3596          { wide_int_to_tree (type,
3597                              wi::add (wi::to_wide (@2), 1)); })))
3598  (simplify
3599   (op (lshift:s@0 @1 INTEGER_CST@2)
3600       (lshift:s@3 @1 INTEGER_CST@4))
3601   (if (INTEGRAL_TYPE_P (type)
3602        && tree_int_cst_sgn (@2) > 0
3603        && tree_int_cst_sgn (@4) > 0
3604        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3605    (with { tree t = type;
3606            if (!TYPE_OVERFLOW_WRAPS (t))
3607              t = unsigned_type_for (t);
3608            wide_int wone = wi::one (TYPE_PRECISION (t));
3609            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3610                                  wi::lshift (wone, wi::to_wide (@4))); }
3611     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3612  (simplify
3613   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3614         @1)
3615   (if (INTEGRAL_TYPE_P (type)
3616        && tree_int_cst_sgn (@2) > 0
3617        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3618    (with { tree t = type;
3619            if (!TYPE_OVERFLOW_WRAPS (t))
3620              t = unsigned_type_for (t);
3621            wide_int wone = wi::one (TYPE_PRECISION (t));
3622            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3623     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3625 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3627 (for minmax (min max)
3628  (simplify
3629   (minmax @0 @0)
3630   @0)
3631 /* max(max(x,y),x) -> max(x,y)  */
3632  (simplify
3633   (minmax:c (minmax:c@2 @0 @1) @0)
3634   @2))
3635 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3636 (for minmax (FMIN_ALL FMAX_ALL)
3637  (simplify
3638   (minmax @0 @0)
3639   (if (!tree_expr_maybe_signaling_nan_p (@0))
3640     @0)))
3641 /* min(max(x,y),y) -> y.  */
3642 (simplify
3643  (min:c (max:c @0 @1) @1)
3644  @1)
3645 /* max(min(x,y),y) -> y.  */
3646 (simplify
3647  (max:c (min:c @0 @1) @1)
3648  @1)
3649 /* max(a,-a) -> abs(a).  */
3650 (simplify
3651  (max:c @0 (negate @0))
3652  (if (TREE_CODE (type) != COMPLEX_TYPE
3653       && (! ANY_INTEGRAL_TYPE_P (type)
3654           || TYPE_OVERFLOW_UNDEFINED (type)))
3655   (abs @0)))
3656 /* min(a,-a) -> -abs(a).  */
3657 (simplify
3658  (min:c @0 (negate @0))
3659  (if (TREE_CODE (type) != COMPLEX_TYPE
3660       && (! ANY_INTEGRAL_TYPE_P (type)
3661           || TYPE_OVERFLOW_UNDEFINED (type)))
3662   (negate (abs @0))))
3663 (simplify
3664  (min @0 @1)
3665  (switch
3666   (if (INTEGRAL_TYPE_P (type)
3667        && TYPE_MIN_VALUE (type)
3668        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3669    @1)
3670   (if (INTEGRAL_TYPE_P (type)
3671        && TYPE_MAX_VALUE (type)
3672        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3673    @0)))
3674 (simplify
3675  (max @0 @1)
3676  (switch
3677   (if (INTEGRAL_TYPE_P (type)
3678        && TYPE_MAX_VALUE (type)
3679        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3680    @1)
3681   (if (INTEGRAL_TYPE_P (type)
3682        && TYPE_MIN_VALUE (type)
3683        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3684    @0)))
3686 /* max (a, a + CST) -> a + CST where CST is positive.  */
3687 /* max (a, a + CST) -> a where CST is negative.  */
3688 (simplify
3689  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3690   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3691    (if (tree_int_cst_sgn (@1) > 0)
3692     @2
3693     @0)))
3695 /* min (a, a + CST) -> a where CST is positive.  */
3696 /* min (a, a + CST) -> a + CST where CST is negative. */
3697 (simplify
3698  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3699   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3700    (if (tree_int_cst_sgn (@1) > 0)
3701     @0
3702     @2)))
3704 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3705    the addresses are known to be less, equal or greater.  */
3706 (for minmax (min max)
3707      cmp (lt gt)
3708  (simplify
3709   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3710   (with
3711    {
3712      poly_int64 off0, off1;
3713      tree base0, base1;
3714      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3715                                   off0, off1, GENERIC);
3716    }
3717    (if (equal == 1)
3718     (if (minmax == MIN_EXPR)
3719      (if (known_le (off0, off1))
3720       @2
3721       (if (known_gt (off0, off1))
3722        @3))
3723      (if (known_ge (off0, off1))
3724       @2
3725       (if (known_lt (off0, off1))
3726        @3)))))))
3728 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3729    and the outer convert demotes the expression back to x's type.  */
3730 (for minmax (min max)
3731  (simplify
3732   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3733   (if (INTEGRAL_TYPE_P (type)
3734        && types_match (@1, type) && int_fits_type_p (@2, type)
3735        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3736        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3737    (minmax @1 (convert @2)))))
3739 (for minmax (FMIN_ALL FMAX_ALL)
3740  /* If either argument is NaN and other one is not sNaN, return the other
3741     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3742  (simplify
3743   (minmax:c @0 REAL_CST@1)
3744    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3745        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3746        && !tree_expr_maybe_signaling_nan_p (@0))
3747    @0)))
3748 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3749    functions to return the numeric arg if the other one is NaN.
3750    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3751    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3752    worry about it either.  */
3753 (if (flag_finite_math_only)
3754  (simplify
3755   (FMIN_ALL @0 @1)
3756   (min @0 @1))
3757  (simplify
3758   (FMAX_ALL @0 @1)
3759   (max @0 @1)))
3760 /* min (-A, -B) -> -max (A, B)  */
3761 (for minmax (min max FMIN_ALL FMAX_ALL)
3762      maxmin (max min FMAX_ALL FMIN_ALL)
3763  (simplify
3764   (minmax (negate:s@2 @0) (negate:s@3 @1))
3765   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3766        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3767            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3768    (negate (maxmin @0 @1)))))
3769 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3770    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3771 (for minmax (min max)
3772  maxmin (max min)
3773  (simplify
3774   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3775   (bit_not (maxmin @0 @1))))
3777 /* MIN (X, Y) == X -> X <= Y  */
3778 (for minmax (min min max max)
3779      cmp    (eq  ne  eq  ne )
3780      out    (le  gt  ge  lt )
3781  (simplify
3782   (cmp:c (minmax:c @0 @1) @0)
3783   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3784    (out @0 @1))))
3785 /* MIN (X, 5) == 0 -> X == 0
3786    MIN (X, 5) == 7 -> false  */
3787 (for cmp (eq ne)
3788  (simplify
3789   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3790   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3791                  TYPE_SIGN (TREE_TYPE (@0))))
3792    { constant_boolean_node (cmp == NE_EXPR, type); }
3793    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3794                   TYPE_SIGN (TREE_TYPE (@0))))
3795     (cmp @0 @2)))))
3796 (for cmp (eq ne)
3797  (simplify
3798   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3799   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3800                  TYPE_SIGN (TREE_TYPE (@0))))
3801    { constant_boolean_node (cmp == NE_EXPR, type); }
3802    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3803                   TYPE_SIGN (TREE_TYPE (@0))))
3804     (cmp @0 @2)))))
3805 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
3806 (for minmax (min     min     max     max     min     min     max     max    )
3807      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
3808      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3809  (simplify
3810   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3811   (comb (cmp @0 @2) (cmp @1 @2))))
3813 /* X <= MAX(X, Y) -> true
3814    X > MAX(X, Y) -> false 
3815    X >= MIN(X, Y) -> true
3816    X < MIN(X, Y) -> false */
3817 (for minmax (min     min     max     max     )
3818      cmp    (ge      lt      le      gt      )
3819  (simplify
3820   (cmp @0 (minmax:c @0 @1))
3821   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3823 /* Undo fancy ways of writing max/min or other ?: expressions, like
3824    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
3825    People normally use ?: and that is what we actually try to optimize.  */
3826 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
3827 (simplify
3828  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3829  (if (INTEGRAL_TYPE_P (type)
3830       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3831   (cond (convert:boolean_type_node @2) @1 @0)))
3832 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
3833 (simplify
3834  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3835  (if (INTEGRAL_TYPE_P (type)
3836       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3837   (cond (convert:boolean_type_node @2) @1 @0)))
3838 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
3839 (simplify
3840  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3841  (if (INTEGRAL_TYPE_P (type)
3842       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3843   (cond (convert:boolean_type_node @2) @1 @0)))
3845 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
3846 (simplify
3847   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3848   (max @2 @1))
3850 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
3851 (for op (bit_xor bit_ior plus)
3852  (simplify
3853   (cond (eq zero_one_valued_p@0
3854             integer_zerop)
3855         @1
3856         (op:c @2 @1))
3857   (if (INTEGRAL_TYPE_P (type)
3858        && TYPE_PRECISION (type) > 1
3859        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3860        (op (mult (convert:type @0) @2) @1))))
3862 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
3863 (for op (bit_xor bit_ior plus)
3864  (simplify
3865   (cond (ne zero_one_valued_p@0
3866             integer_zerop)
3867        (op:c @2 @1)
3868         @1)
3869   (if (INTEGRAL_TYPE_P (type)
3870        && TYPE_PRECISION (type) > 1
3871        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3872        (op (mult (convert:type @0) @2) @1))))
3874 /* Simplifications of shift and rotates.  */
3876 (for rotate (lrotate rrotate)
3877  (simplify
3878   (rotate integer_all_onesp@0 @1)
3879   @0))
3881 /* Optimize -1 >> x for arithmetic right shifts.  */
3882 (simplify
3883  (rshift integer_all_onesp@0 @1)
3884  (if (!TYPE_UNSIGNED (type))
3885   @0))
3887 /* Optimize (x >> c) << c into x & (-1<<c).  */
3888 (simplify
3889  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3890  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3891   /* It doesn't matter if the right shift is arithmetic or logical.  */
3892   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3894 (simplify
3895  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3896  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3897       /* Allow intermediate conversion to integral type with whatever sign, as
3898          long as the low TYPE_PRECISION (type)
3899          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3900       && INTEGRAL_TYPE_P (type)
3901       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3902       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3903       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3904       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3905           || wi::geu_p (wi::to_wide (@1),
3906                         TYPE_PRECISION (type)
3907                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3908   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3910 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
3911    unsigned x OR truncate into the precision(type) - c lowest bits
3912    of signed x (if they have mode precision or a precision of 1).  */
3913 (simplify
3914  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
3915  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3916   (if (TYPE_UNSIGNED (type))
3917    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
3918    (if (INTEGRAL_TYPE_P (type))
3919     (with {
3920       int width = element_precision (type) - tree_to_uhwi (@1);
3921       tree stype = build_nonstandard_integer_type (width, 0);
3922      }
3923      (if (width == 1 || type_has_mode_precision_p (stype))
3924       (convert (convert:stype @0))))))))
3926 /* Optimize x >> x into 0 */
3927 (simplify
3928  (rshift @0 @0)
3929   { build_zero_cst (type); })
3931 (for shiftrotate (lrotate rrotate lshift rshift)
3932  (simplify
3933   (shiftrotate @0 integer_zerop)
3934   (non_lvalue @0))
3935  (simplify
3936   (shiftrotate integer_zerop@0 @1)
3937   @0)
3938  /* Prefer vector1 << scalar to vector1 << vector2
3939     if vector2 is uniform.  */
3940  (for vec (VECTOR_CST CONSTRUCTOR)
3941   (simplify
3942    (shiftrotate @0 vec@1)
3943    (with { tree tem = uniform_vector_p (@1); }
3944     (if (tem)
3945      (shiftrotate @0 { tem; }))))))
3947 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3948    Y is 0.  Similarly for X >> Y.  */
3949 #if GIMPLE
3950 (for shift (lshift rshift)
3951  (simplify
3952   (shift @0 SSA_NAME@1)
3953    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3954     (with {
3955       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3956       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3957      }
3958      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3959       @0)))))
3960 #endif
3962 /* Rewrite an LROTATE_EXPR by a constant into an
3963    RROTATE_EXPR by a new constant.  */
3964 (simplify
3965  (lrotate @0 INTEGER_CST@1)
3966  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3967                             build_int_cst (TREE_TYPE (@1),
3968                                            element_precision (type)), @1); }))
3970 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3971 (for op (lrotate rrotate rshift lshift)
3972  (simplify
3973   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3974   (with { unsigned int prec = element_precision (type); }
3975    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3976         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3977         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3978         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3979     (with { unsigned int low = (tree_to_uhwi (@1)
3980                                 + tree_to_uhwi (@2)); }
3981      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3982         being well defined.  */
3983      (if (low >= prec)
3984       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3985        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3986        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3987         { build_zero_cst (type); }
3988         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3989       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3992 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3993 (simplify
3994  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3995   (if ((wi::to_wide (@1) & 1) != 0)
3996    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3997    { build_zero_cst (type); }))
3999 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4000    either to false if D is smaller (unsigned comparison) than C, or to
4001    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
4002 (for cmp (ne eq)
4003      icmp (eq ne)
4004  (simplify
4005   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4006    (with { int c1 = wi::clz (wi::to_wide (@1));
4007            int c2 = wi::clz (wi::to_wide (@2)); }
4008     (if (c1 < c2)
4009      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4010      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4011  (simplify
4012   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4013    (if (tree_int_cst_sgn (@1) > 0)
4014     (with { int c1 = wi::clz (wi::to_wide (@1));
4015             int c2 = wi::clz (wi::to_wide (@2)); }
4016      (if (c1 > c2)
4017       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4018       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
4020 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4021    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4022    if CST2 != 0.  */
4023 (for cmp (ne eq)
4024  (simplify
4025   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4026   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4027    (if (cand < 0
4028         || (!integer_zerop (@2)
4029             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4030     { constant_boolean_node (cmp == NE_EXPR, type); }
4031     (if (!integer_zerop (@2)
4032          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4033      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4035 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4036         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4037 (for cmp (ne eq)
4038  (simplify
4039   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4040   (if (tree_fits_shwi_p (@1)
4041        && tree_to_shwi (@1) > 0
4042        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4043     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4044       { constant_boolean_node (cmp == NE_EXPR, type); }
4045       (with { wide_int c1 = wi::to_wide (@1);
4046               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4047               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4048         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4049              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4050  (simplify
4051   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4052   (if (tree_fits_shwi_p (@1)
4053        && tree_to_shwi (@1) > 0
4054        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4055     (with { tree t0 = TREE_TYPE (@0);
4056             unsigned int prec = TYPE_PRECISION (t0);
4057             wide_int c1 = wi::to_wide (@1);
4058             wide_int c2 = wi::to_wide (@2);
4059             wide_int c3 = wi::to_wide (@3);
4060             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4061       (if ((c2 & c3) != c3)
4062         { constant_boolean_node (cmp == NE_EXPR, type); }
4063         (if (TYPE_UNSIGNED (t0))
4064           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4065             { constant_boolean_node (cmp == NE_EXPR, type); }
4066             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4067                  { wide_int_to_tree (t0, c3 << c1); }))
4068           (with { wide_int smask = wi::arshift (sb, c1); }
4069             (switch
4070               (if ((c2 & smask) == 0)
4071                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4072                      { wide_int_to_tree (t0, c3 << c1); }))
4073               (if ((c3 & smask) == 0)
4074                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4075                      { wide_int_to_tree (t0, c3 << c1); }))
4076               (if ((c2 & smask) != (c3 & smask))
4077                 { constant_boolean_node (cmp == NE_EXPR, type); })
4078               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4079                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4081 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4082         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4083    if the new mask might be further optimized.  */
4084 (for shift (lshift rshift)
4085  (simplify
4086   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4087            INTEGER_CST@2)
4088    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4089         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4090         && tree_fits_uhwi_p (@1)
4091         && tree_to_uhwi (@1) > 0
4092         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4093     (with
4094      {
4095        unsigned int shiftc = tree_to_uhwi (@1);
4096        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4097        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4098        tree shift_type = TREE_TYPE (@3);
4099        unsigned int prec;
4101        if (shift == LSHIFT_EXPR)
4102          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4103        else if (shift == RSHIFT_EXPR
4104                 && type_has_mode_precision_p (shift_type))
4105          {
4106            prec = TYPE_PRECISION (TREE_TYPE (@3));
4107            tree arg00 = @0;
4108            /* See if more bits can be proven as zero because of
4109               zero extension.  */
4110            if (@3 != @0
4111                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4112              {
4113                tree inner_type = TREE_TYPE (@0);
4114                if (type_has_mode_precision_p (inner_type)
4115                    && TYPE_PRECISION (inner_type) < prec)
4116                  {
4117                    prec = TYPE_PRECISION (inner_type);
4118                    /* See if we can shorten the right shift.  */
4119                    if (shiftc < prec)
4120                      shift_type = inner_type;
4121                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4122                       it into (X, 0) later on by making sure zerobits
4123                       is all ones.  */
4124                  }
4125              }
4126            zerobits = HOST_WIDE_INT_M1U;
4127            if (shiftc < prec)
4128              {
4129                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4130                zerobits <<= prec - shiftc;
4131              }
4132            /* For arithmetic shift if sign bit could be set, zerobits
4133               can contain actually sign bits, so no transformation is
4134               possible, unless MASK masks them all away.  In that
4135               case the shift needs to be converted into logical shift.  */
4136            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4137                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4138              {
4139                if ((mask & zerobits) == 0)
4140                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4141                else
4142                  zerobits = 0;
4143              }
4144          }
4145      }
4146      /* ((X << 16) & 0xff00) is (X, 0).  */
4147      (if ((mask & zerobits) == mask)
4148       { build_int_cst (type, 0); }
4149       (with { newmask = mask | zerobits; }
4150        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4151         (with
4152          {
4153            /* Only do the transformation if NEWMASK is some integer
4154               mode's mask.  */
4155            for (prec = BITS_PER_UNIT;
4156                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4157              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4158                break;
4159          }
4160          (if (prec < HOST_BITS_PER_WIDE_INT
4161               || newmask == HOST_WIDE_INT_M1U)
4162           (with
4163            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4164            (if (!tree_int_cst_equal (newmaskt, @2))
4165             (if (shift_type != TREE_TYPE (@3))
4166              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4167              (bit_and @4 { newmaskt; })))))))))))))
4169 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4170 (for cmp (ne eq)
4171        icmp (eq ne)
4172  (simplify
4173   (cmp
4174    (bit_and
4175     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4176   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4177    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4178                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4180 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4181    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4182 (for shift (lshift rshift)
4183  (for bit_op (bit_and bit_xor bit_ior)
4184   (simplify
4185    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4186    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4187     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4188      (if (mask)
4189       (bit_op (shift (convert @0) @1) { mask; })))))))
4191 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4192 (simplify
4193  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4194   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4195        && (element_precision (TREE_TYPE (@0))
4196            <= element_precision (TREE_TYPE (@1))
4197            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4198    (with
4199     { tree shift_type = TREE_TYPE (@0); }
4200      (convert (rshift (convert:shift_type @1) @2)))))
4202 /* ~(~X >>r Y) -> X >>r Y
4203    ~(~X <<r Y) -> X <<r Y */
4204 (for rotate (lrotate rrotate)
4205  (simplify
4206   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4207    (if ((element_precision (TREE_TYPE (@0))
4208          <= element_precision (TREE_TYPE (@1))
4209          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4210         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4211             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4212     (with
4213      { tree rotate_type = TREE_TYPE (@0); }
4214       (convert (rotate (convert:rotate_type @1) @2))))))
4216 (for cmp (eq ne)
4217  (for rotate (lrotate rrotate)
4218       invrot (rrotate lrotate)
4219   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4220   (simplify
4221    (cmp (rotate @1 @0) (rotate @2 @0))
4222    (cmp @1 @2))
4223   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4224   (simplify
4225    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4226    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4227   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4228   (simplify
4229    (cmp (rotate @0 @1) INTEGER_CST@2)
4230     (if (integer_zerop (@2) || integer_all_onesp (@2))
4231      (cmp @0 @2)))))
4233 /* Narrow a lshift by constant.  */
4234 (simplify
4235  (convert (lshift:s@0 @1 INTEGER_CST@2))
4236  (if (INTEGRAL_TYPE_P (type)
4237       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4238       && !integer_zerop (@2)
4239       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4240   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4241        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4242    (lshift (convert @1) @2)
4243    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4244     { build_zero_cst (type); }))))
4246 /* Simplifications of conversions.  */
4248 /* Basic strip-useless-type-conversions / strip_nops.  */
4249 (for cvt (convert view_convert float fix_trunc)
4250  (simplify
4251   (cvt @0)
4252   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4253        || (GENERIC && type == TREE_TYPE (@0)))
4254    @0)))
4256 /* Contract view-conversions.  */
4257 (simplify
4258   (view_convert (view_convert @0))
4259   (view_convert @0))
4261 /* For integral conversions with the same precision or pointer
4262    conversions use a NOP_EXPR instead.  */
4263 (simplify
4264   (view_convert @0)
4265   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4266        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4267        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4268    (convert @0)))
4270 /* Strip inner integral conversions that do not change precision or size, or
4271    zero-extend while keeping the same size (for bool-to-char).  */
4272 (simplify
4273   (view_convert (convert@0 @1))
4274   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4275        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4276        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4277        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4278            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4279                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4280    (view_convert @1)))
4282 /* Simplify a view-converted empty or single-element constructor.  */
4283 (simplify
4284   (view_convert CONSTRUCTOR@0)
4285   (with
4286    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4287                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4288    (switch
4289     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4290      { build_zero_cst (type); })
4291     (if (CONSTRUCTOR_NELTS (ctor) == 1
4292          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4293          && operand_equal_p (TYPE_SIZE (type),
4294                              TYPE_SIZE (TREE_TYPE
4295                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4296      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4298 /* Re-association barriers around constants and other re-association
4299    barriers can be removed.  */
4300 (simplify
4301  (paren CONSTANT_CLASS_P@0)
4302  @0)
4303 (simplify
4304  (paren (paren@1 @0))
4305  @1)
4307 /* Handle cases of two conversions in a row.  */
4308 (for ocvt (convert float fix_trunc)
4309  (for icvt (convert float)
4310   (simplify
4311    (ocvt (icvt@1 @0))
4312    (with
4313     {
4314       tree inside_type = TREE_TYPE (@0);
4315       tree inter_type = TREE_TYPE (@1);
4316       int inside_int = INTEGRAL_TYPE_P (inside_type);
4317       int inside_ptr = POINTER_TYPE_P (inside_type);
4318       int inside_float = FLOAT_TYPE_P (inside_type);
4319       int inside_vec = VECTOR_TYPE_P (inside_type);
4320       unsigned int inside_prec = element_precision (inside_type);
4321       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4322       int inter_int = INTEGRAL_TYPE_P (inter_type);
4323       int inter_ptr = POINTER_TYPE_P (inter_type);
4324       int inter_float = FLOAT_TYPE_P (inter_type);
4325       int inter_vec = VECTOR_TYPE_P (inter_type);
4326       unsigned int inter_prec = element_precision (inter_type);
4327       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4328       int final_int = INTEGRAL_TYPE_P (type);
4329       int final_ptr = POINTER_TYPE_P (type);
4330       int final_float = FLOAT_TYPE_P (type);
4331       int final_vec = VECTOR_TYPE_P (type);
4332       unsigned int final_prec = element_precision (type);
4333       int final_unsignedp = TYPE_UNSIGNED (type);
4334     }
4335    (switch
4336     /* In addition to the cases of two conversions in a row
4337        handled below, if we are converting something to its own
4338        type via an object of identical or wider precision, neither
4339        conversion is needed.  */
4340     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4341           || (GENERIC
4342               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4343          && (((inter_int || inter_ptr) && final_int)
4344              || (inter_float && final_float))
4345          && inter_prec >= final_prec)
4346      (ocvt @0))
4348     /* Likewise, if the intermediate and initial types are either both
4349        float or both integer, we don't need the middle conversion if the
4350        former is wider than the latter and doesn't change the signedness
4351        (for integers).  Avoid this if the final type is a pointer since
4352        then we sometimes need the middle conversion.  */
4353     (if (((inter_int && inside_int) || (inter_float && inside_float))
4354          && (final_int || final_float)
4355          && inter_prec >= inside_prec
4356          && (inter_float || inter_unsignedp == inside_unsignedp))
4357      (ocvt @0))
4359     /* If we have a sign-extension of a zero-extended value, we can
4360        replace that by a single zero-extension.  Likewise if the
4361        final conversion does not change precision we can drop the
4362        intermediate conversion.  */
4363     (if (inside_int && inter_int && final_int
4364          && ((inside_prec < inter_prec && inter_prec < final_prec
4365               && inside_unsignedp && !inter_unsignedp)
4366              || final_prec == inter_prec))
4367      (ocvt @0))
4369     /* Two conversions in a row are not needed unless:
4370         - some conversion is floating-point (overstrict for now), or
4371         - some conversion is a vector (overstrict for now), or
4372         - the intermediate type is narrower than both initial and
4373           final, or
4374         - the intermediate type and innermost type differ in signedness,
4375           and the outermost type is wider than the intermediate, or
4376         - the initial type is a pointer type and the precisions of the
4377           intermediate and final types differ, or
4378         - the final type is a pointer type and the precisions of the
4379           initial and intermediate types differ.  */
4380     (if (! inside_float && ! inter_float && ! final_float
4381          && ! inside_vec && ! inter_vec && ! final_vec
4382          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4383          && ! (inside_int && inter_int
4384                && inter_unsignedp != inside_unsignedp
4385                && inter_prec < final_prec)
4386          && ((inter_unsignedp && inter_prec > inside_prec)
4387              == (final_unsignedp && final_prec > inter_prec))
4388          && ! (inside_ptr && inter_prec != final_prec)
4389          && ! (final_ptr && inside_prec != inter_prec))
4390      (ocvt @0))
4392    /* `(outer:M)(inter:N) a:O`
4393       can be converted to `(outer:M) a`
4394       if M <= O && N >= O. No matter what signedness of the casts,
4395       as the final is either a truncation from the original or just
4396       a sign change of the type. */
4397    (if (inside_int && inter_int && final_int
4398         && final_prec <= inside_prec
4399         && inter_prec >= inside_prec)
4400     (convert @0))
4402     /* A truncation to an unsigned type (a zero-extension) should be
4403        canonicalized as bitwise and of a mask.  */
4404     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4405          && final_int && inter_int && inside_int
4406          && final_prec == inside_prec
4407          && final_prec > inter_prec
4408          && inter_unsignedp)
4409      (convert (bit_and @0 { wide_int_to_tree
4410                               (inside_type,
4411                                wi::mask (inter_prec, false,
4412                                          TYPE_PRECISION (inside_type))); })))
4414     /* If we are converting an integer to a floating-point that can
4415        represent it exactly and back to an integer, we can skip the
4416        floating-point conversion.  */
4417     (if (GIMPLE /* PR66211 */
4418          && inside_int && inter_float && final_int &&
4419          (unsigned) significand_size (TYPE_MODE (inter_type))
4420          >= inside_prec - !inside_unsignedp)
4421      (convert @0)))))))
4423 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4424    float_type.  Only do the transformation if we do not need to preserve
4425    trapping behaviour, so require !flag_trapping_math. */
4426 #if GIMPLE
4427 (simplify
4428    (float (fix_trunc @0))
4429    (if (!flag_trapping_math
4430         && types_match (type, TREE_TYPE (@0))
4431         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4432                                           OPTIMIZE_FOR_BOTH))
4433       (IFN_TRUNC @0)))
4434 #endif
4436 /* If we have a narrowing conversion to an integral type that is fed by a
4437    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4438    masks off bits outside the final type (and nothing else).  */
4439 (simplify
4440   (convert (bit_and @0 INTEGER_CST@1))
4441   (if (INTEGRAL_TYPE_P (type)
4442        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4443        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4444        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4445                                                     TYPE_PRECISION (type)), 0))
4446    (convert @0)))
4449 /* (X /[ex] A) * A -> X.  */
4450 (simplify
4451   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4452   (convert @0))
4454 /* Simplify (A / B) * B + (A % B) -> A.  */
4455 (for div (trunc_div ceil_div floor_div round_div)
4456      mod (trunc_mod ceil_mod floor_mod round_mod)
4457   (simplify
4458    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4459    @0))
4461 /* x / y * y == x -> x % y == 0.  */
4462 (simplify
4463   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4464   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4465     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4467 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4468 (for op (plus minus)
4469  (simplify
4470   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4471   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4472        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4473    (with
4474      {
4475        wi::overflow_type overflow;
4476        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4477                                TYPE_SIGN (type), &overflow);
4478      }
4479      (if (types_match (type, TREE_TYPE (@2))
4480          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4481       (op @0 { wide_int_to_tree (type, mul); })
4482       (with { tree utype = unsigned_type_for (type); }
4483        (convert (op (convert:utype @0)
4484                     (mult (convert:utype @1) (convert:utype @2))))))))))
4486 /* Canonicalization of binary operations.  */
4488 /* Convert X + -C into X - C.  */
4489 (simplify
4490  (plus @0 REAL_CST@1)
4491  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4492   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4493    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4494     (minus @0 { tem; })))))
4496 /* Convert x+x into x*2.  */
4497 (simplify
4498  (plus @0 @0)
4499  (if (SCALAR_FLOAT_TYPE_P (type))
4500   (mult @0 { build_real (type, dconst2); })
4501   (if (INTEGRAL_TYPE_P (type))
4502    (mult @0 { build_int_cst (type, 2); }))))
4504 /* 0 - X  ->  -X.  */
4505 (simplify
4506  (minus integer_zerop @1)
4507  (negate @1))
4508 (simplify
4509  (pointer_diff integer_zerop @1)
4510  (negate (convert @1)))
4512 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4513    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4514    (-ARG1 + ARG0) reduces to -ARG1.  */
4515 (simplify
4516  (minus real_zerop@0 @1)
4517  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4518   (negate @1)))
4520 /* Transform x * -1 into -x.  */
4521 (simplify
4522  (mult @0 integer_minus_onep)
4523  (negate @0))
4525 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4526    signed overflow for CST != 0 && CST != -1.  */
4527 (simplify
4528  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4529  (if (TREE_CODE (@2) != INTEGER_CST
4530       && single_use (@3)
4531       && !integer_zerop (@1) && !integer_minus_onep (@1))
4532   (mult (mult @0 @2) @1)))
4534 /* True if we can easily extract the real and imaginary parts of a complex
4535    number.  */
4536 (match compositional_complex
4537  (convert? (complex @0 @1)))
4539 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4540 (simplify
4541  (complex (realpart @0) (imagpart @0))
4542  @0)
4543 (simplify
4544  (realpart (complex @0 @1))
4545  @0)
4546 (simplify
4547  (imagpart (complex @0 @1))
4548  @1)
4550 /* Sometimes we only care about half of a complex expression.  */
4551 (simplify
4552  (realpart (convert?:s (conj:s @0)))
4553  (convert (realpart @0)))
4554 (simplify
4555  (imagpart (convert?:s (conj:s @0)))
4556  (convert (negate (imagpart @0))))
4557 (for part (realpart imagpart)
4558  (for op (plus minus)
4559   (simplify
4560    (part (convert?:s@2 (op:s @0 @1)))
4561    (convert (op (part @0) (part @1))))))
4562 (simplify
4563  (realpart (convert?:s (CEXPI:s @0)))
4564  (convert (COS @0)))
4565 (simplify
4566  (imagpart (convert?:s (CEXPI:s @0)))
4567  (convert (SIN @0)))
4569 /* conj(conj(x)) -> x  */
4570 (simplify
4571  (conj (convert? (conj @0)))
4572  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4573   (convert @0)))
4575 /* conj({x,y}) -> {x,-y}  */
4576 (simplify
4577  (conj (convert?:s (complex:s @0 @1)))
4578  (with { tree itype = TREE_TYPE (type); }
4579   (complex (convert:itype @0) (negate (convert:itype @1)))))
4581 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4582 (for bswap (BSWAP)
4583  (simplify
4584   (bswap (bswap @0))
4585   @0)
4586  (simplify
4587   (bswap (bit_not (bswap @0)))
4588   (bit_not @0))
4589  (for bitop (bit_xor bit_ior bit_and)
4590   (simplify
4591    (bswap (bitop:c (bswap @0) @1))
4592    (bitop @0 (bswap @1))))
4593  (for cmp (eq ne)
4594   (simplify
4595    (cmp (bswap@2 @0) (bswap @1))
4596    (with { tree ctype = TREE_TYPE (@2); }
4597     (cmp (convert:ctype @0) (convert:ctype @1))))
4598   (simplify
4599    (cmp (bswap @0) INTEGER_CST@1)
4600    (with { tree ctype = TREE_TYPE (@1); }
4601     (cmp (convert:ctype @0) (bswap! @1)))))
4602  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4603  (simplify
4604   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4605            INTEGER_CST@3)
4606    (if (BITS_PER_UNIT == 8
4607         && tree_fits_uhwi_p (@2)
4608         && tree_fits_uhwi_p (@3))
4609     (with
4610      {
4611       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4612       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4613       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4614       unsigned HOST_WIDE_INT lo = bits & 7;
4615       unsigned HOST_WIDE_INT hi = bits - lo;
4616      }
4617      (if (bits < prec
4618           && mask < (256u>>lo)
4619           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4620       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4621        (if (ns == 0)
4622         (bit_and (convert @1) @3)
4623         (with
4624          {
4625           tree utype = unsigned_type_for (TREE_TYPE (@1));
4626           tree nst = build_int_cst (integer_type_node, ns);
4627          }
4628          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4629  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4630  (simplify
4631   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4632    (if (BITS_PER_UNIT == 8
4633         && CHAR_TYPE_SIZE == 8
4634         && tree_fits_uhwi_p (@1))
4635     (with
4636      {
4637       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4638       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4639       /* If the bswap was extended before the original shift, this
4640          byte (shift) has the sign of the extension, not the sign of
4641          the original shift.  */
4642       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4643      }
4644      /* Special case: logical right shift of sign-extended bswap.
4645         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4646      (if (TYPE_PRECISION (type) > prec
4647           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4648           && TYPE_UNSIGNED (type)
4649           && bits < prec && bits + 8 >= prec)
4650       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4651        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4652       (if (bits + 8 == prec)
4653        (if (TYPE_UNSIGNED (st))
4654         (convert (convert:unsigned_char_type_node @0))
4655         (convert (convert:signed_char_type_node @0)))
4656        (if (bits < prec && bits + 8 > prec)
4657         (with 
4658          {
4659           tree nst = build_int_cst (integer_type_node, bits & 7);
4660           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4661                                        : signed_char_type_node;
4662          }
4663          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4664  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4665  (simplify
4666   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4667    (if (BITS_PER_UNIT == 8
4668         && tree_fits_uhwi_p (@1)
4669         && tree_to_uhwi (@1) < 256)
4670     (with
4671      {
4672       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4673       tree utype = unsigned_type_for (TREE_TYPE (@0));
4674       tree nst = build_int_cst (integer_type_node, prec - 8);
4675      }
4676      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4679 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4681 /* Simplify constant conditions.
4682    Only optimize constant conditions when the selected branch
4683    has the same type as the COND_EXPR.  This avoids optimizing
4684    away "c ? x : throw", where the throw has a void type.
4685    Note that we cannot throw away the fold-const.cc variant nor
4686    this one as we depend on doing this transform before possibly
4687    A ? B : B -> B triggers and the fold-const.cc one can optimize
4688    0 ? A : B to B even if A has side-effects.  Something
4689    genmatch cannot handle.  */
4690 (simplify
4691  (cond INTEGER_CST@0 @1 @2)
4692  (if (integer_zerop (@0))
4693   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4694    @2)
4695   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4696    @1)))
4697 (simplify
4698  (vec_cond VECTOR_CST@0 @1 @2)
4699  (if (integer_all_onesp (@0))
4700   @1
4701   (if (integer_zerop (@0))
4702    @2)))
4704 /* Sink unary operations to branches, but only if we do fold both.  */
4705 (for op (negate bit_not abs absu)
4706  (simplify
4707   (op (vec_cond:s @0 @1 @2))
4708   (vec_cond @0 (op! @1) (op! @2))))
4710 /* Sink unary conversions to branches, but only if we do fold both
4711    and the target's truth type is the same as we already have.  */
4712 (simplify
4713  (convert (vec_cond:s @0 @1 @2))
4714  (if (VECTOR_TYPE_P (type)
4715       && types_match (TREE_TYPE (@0), truth_type_for (type)))
4716   (vec_cond @0 (convert! @1) (convert! @2))))
4718 /* Likewise for view_convert of nop_conversions. */
4719 (simplify
4720  (view_convert (vec_cond:s @0 @1 @2))
4721  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4722       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4723                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4724       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4725   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4727 /* Sink binary operation to branches, but only if we can fold it.  */
4728 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4729          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4730          trunc_mod ceil_mod floor_mod round_mod min max)
4731 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4732  (simplify
4733   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4734   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4736 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4737  (simplify
4738   (op (vec_cond:s @0 @1 @2) @3)
4739   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4740  (simplify
4741   (op @3 (vec_cond:s @0 @1 @2))
4742   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4744 #if GIMPLE
4745 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4746  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4747            INTEGER_CST@1)
4748  (with {
4749          int ibit = tree_log2 (@0);
4750          int ibit2 = tree_log2 (@1);
4751        }
4752   (if (ibit == ibit2
4753       && ibit >= 0
4754       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4756 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4757  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4758           INTEGER_CST@1)
4759  (with {
4760          int ibit = tree_log2 (@0);
4761          int ibit2 = tree_log2 (@1);
4762        }
4763   (if (ibit == ibit2
4764       && ibit >= 0
4765       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4767 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4768  (bit_and:c
4769   (convert1?@4
4770    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4771   (convert2? @0))
4772  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4774 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4775  (bit_and:c
4776   (convert1?@4
4777    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4778   (convert2? @0))
4779  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4781 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4782  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4783             INTEGER_CST@1)
4784  (with {
4785          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4786                                               TYPE_PRECISION(type)));
4787          int ibit2 = tree_log2 (@1);
4788        }
4789   (if (ibit == ibit2
4790       && ibit >= 0
4791       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4793 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4794  (bit_and@4
4795   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4796   INTEGER_CST@1)
4797  (with {
4798          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4799                                               TYPE_PRECISION(type)));
4800          int ibit2 = tree_log2 (@1);
4801        }
4802   (if (ibit == ibit2
4803       && ibit >= 0
4804       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4806 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4807  (bit_and:c
4808   (convert1?@3
4809    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4810   (convert2? @0))
4811  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4813 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4814  (bit_and:c
4815   (convert1?@3
4816    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4817   (convert2? @0))
4818   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4820 #endif
4822 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4823    Currently disabled after pass lvec because ARM understands
4824    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
4825 (simplify
4826  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4827  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4828   (vec_cond (bit_and @0 @3) @1 @2)))
4829 (simplify
4830  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4831  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4832   (vec_cond (bit_ior @0 @3) @1 @2)))
4833 (simplify
4834  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4835  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4836   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4837 (simplify
4838  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4839  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4840   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4842 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
4843 (simplify
4844  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4845  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4846   (vec_cond (bit_and @0 @1) @2 @3)))
4847 (simplify
4848  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4849  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4850   (vec_cond (bit_ior @0 @1) @2 @3)))
4851 (simplify
4852  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4853  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4854   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4855 (simplify
4856  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4857  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4858   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4860 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4861    types are compatible.  */
4862 (simplify
4863  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4864  (if (VECTOR_BOOLEAN_TYPE_P (type)
4865       && types_match (type, TREE_TYPE (@0)))
4866   (if (integer_zerop (@1) && integer_all_onesp (@2))
4867    (bit_not @0)
4868    (if (integer_all_onesp (@1) && integer_zerop (@2))
4869     @0))))
4871 /* A few simplifications of "a ? CST1 : CST2". */
4872 /* NOTE: Only do this on gimple as the if-chain-to-switch
4873    optimization depends on the gimple to have if statements in it. */
4874 #if GIMPLE
4875 (simplify
4876  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4877  (switch
4878   (if (integer_zerop (@2))
4879    (switch
4880     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4881     (if (integer_onep (@1))
4882      (convert (convert:boolean_type_node @0)))
4883     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4884     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4885      (with {
4886        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4887       }
4888       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4889     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
4890        here as the powerof2cst case above will handle that case correctly.  */
4891     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4892      (with {
4893        auto prec = TYPE_PRECISION (type);
4894        auto unsign = TYPE_UNSIGNED (type);
4895        tree inttype = build_nonstandard_integer_type (prec, unsign);
4896       }
4897       (convert (negate (convert:inttype (convert:boolean_type_node @0))))))))
4898   (if (integer_zerop (@1))
4899    (with {
4900       tree booltrue = constant_boolean_node (true, boolean_type_node);
4901     }
4902     (switch
4903      /* a ? 0 : 1 -> !a. */
4904      (if (integer_onep (@2))
4905       (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4906      /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4907      (if (INTEGRAL_TYPE_P (type) &&  integer_pow2p (@2))
4908       (with {
4909         tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4910        }
4911        (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4912         { shift; })))
4913      /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
4914        here as the powerof2cst case above will handle that case correctly.  */
4915      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4916       (with {
4917         auto prec = TYPE_PRECISION (type);
4918         auto unsign = TYPE_UNSIGNED (type);
4919         tree inttype = build_nonstandard_integer_type (prec, unsign);
4920        }
4921        (convert
4922         (negate
4923          (convert:inttype
4924           (bit_xor (convert:boolean_type_node @0) { booltrue; } )
4925          )
4926         )
4927        )
4928       )
4929      )
4930     )
4931    )
4932   )
4936 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
4937    for unsigned types. */
4938 (simplify
4939  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
4940  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4941       && bitwise_equal_p (@0, @2))
4942   (convert (eq @0 @1))
4946 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
4947    for unsigned types. */
4948 (simplify
4949  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
4950  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4951       && bitwise_equal_p (@0, @2))
4952   (convert (eq @0 @1))
4956 /* Optimize
4957    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
4958    x_5 ? cstN ? cst4 : cst3
4959    # op is == or != and N is 1 or 2
4960    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
4961    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
4962    of cst3 and cst4 is smaller.
4963    This was originally done by two_value_replacement in phiopt (PR 88676).  */
4964 (for eqne (ne eq)
4965  (simplify
4966   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
4967   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4968        && INTEGRAL_TYPE_P (type)
4969        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
4970            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
4971    (with {
4972      value_range r;
4973      get_range_query (cfun)->range_of_expr (r, @0);
4974      if (r.undefined_p ())
4975        r.set_varying (TREE_TYPE (@0));
4977      wide_int min = r.lower_bound ();
4978      wide_int max = r.upper_bound ();
4979     }
4980     (if (min + 1 == max
4981          && (wi::to_wide (@1) == min
4982              || wi::to_wide (@1) == max))
4983      (with {
4984        tree arg0 = @2, arg1 = @3;
4985        tree type1;
4986        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
4987          std::swap (arg0, arg1);
4988        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4989          type1 = TREE_TYPE (@0);
4990        else
4991          type1 = type;
4992        auto prec = TYPE_PRECISION (type1);
4993        auto unsign = TYPE_UNSIGNED (type1);
4994        type1 = build_nonstandard_integer_type (prec, unsign);
4995        min = wide_int::from (min, prec,
4996                              TYPE_SIGN (TREE_TYPE (@0)));
4997        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
4998                                     TYPE_SIGN (type));
4999        enum tree_code code;
5000        wi::overflow_type ovf;
5001        if (tree_int_cst_lt (arg0, arg1))
5002          {
5003            code = PLUS_EXPR;
5004            a -= min;
5005            if (!unsign)
5006              {
5007                /* lhs is known to be in range [min, min+1] and we want to add a
5008                   to it.  Check if that operation can overflow for those 2 values
5009                   and if yes, force unsigned type.  */
5010                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5011                if (ovf)
5012                  type1 = unsigned_type_for (type1);
5013              }
5014          }
5015        else
5016          {
5017            code = MINUS_EXPR;
5018            a += min;
5019            if (!unsign)
5020              {
5021                /* lhs is known to be in range [min, min+1] and we want to subtract
5022                   it from a.  Check if that operation can overflow for those 2
5023                   values and if yes, force unsigned type.  */
5024                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5025                if (ovf)
5026                 type1 = unsigned_type_for (type1);
5027              }
5028          }
5029        tree arg = wide_int_to_tree (type1, a);
5030       }
5031       (if (code == PLUS_EXPR)
5032        (convert (plus (convert:type1 @0) { arg; }))
5033        (convert (minus { arg; } (convert:type1 @0)))
5034       )
5035      )
5036     )
5037    )
5038   )
5041 #endif
5043 (simplify
5044  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5045  (if (INTEGRAL_TYPE_P (type)
5046       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5047   (cond @1 (convert @2) (convert @3))))
5049 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5050    be extended.  */
5051 /* This pattern implements two kinds simplification:
5053    Case 1)
5054    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5055      1) Conversions are type widening from smaller type.
5056      2) Const c1 equals to c2 after canonicalizing comparison.
5057      3) Comparison has tree code LT, LE, GT or GE.
5058    This specific pattern is needed when (cmp (convert x) c) may not
5059    be simplified by comparison patterns because of multiple uses of
5060    x.  It also makes sense here because simplifying across multiple
5061    referred var is always benefitial for complicated cases.
5063    Case 2)
5064    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5065 (for cmp (lt le gt ge eq ne)
5066  (simplify
5067   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5068   (with
5069    {
5070      tree from_type = TREE_TYPE (@1);
5071      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5072      enum tree_code code = ERROR_MARK;
5074      if (INTEGRAL_TYPE_P (from_type)
5075          && int_fits_type_p (@2, from_type)
5076          && (types_match (c1_type, from_type)
5077              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5078                  && (TYPE_UNSIGNED (from_type)
5079                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5080          && (types_match (c2_type, from_type)
5081              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5082                  && (TYPE_UNSIGNED (from_type)
5083                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5084        {
5085          if (cmp != EQ_EXPR)
5086            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5087          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5088          else if (int_fits_type_p (@3, from_type))
5089            code = EQ_EXPR;
5090        }
5091    }
5092    (if (code == MAX_EXPR)
5093     (convert (max @1 (convert @2)))
5094     (if (code == MIN_EXPR)
5095      (convert (min @1 (convert @2)))
5096      (if (code == EQ_EXPR)
5097       (convert (cond (eq @1 (convert @3))
5098                      (convert:from_type @3) (convert:from_type @2)))))))))
5100 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5102      1) OP is PLUS or MINUS.
5103      2) CMP is LT, LE, GT or GE.
5104      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5106    This pattern also handles special cases like:
5108      A) Operand x is a unsigned to signed type conversion and c1 is
5109         integer zero.  In this case,
5110           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5111           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5112      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5113         check equality for (c1+1) and (c1-1) by adjusting comparison
5114         code.
5116    TODO: Though signed type is handled by this pattern, it cannot be
5117    simplified at the moment because C standard requires additional
5118    type promotion.  In order to match&simplify it here, the IR needs
5119    to be cleaned up by other optimizers, i.e, VRP.  */
5120 (for op (plus minus)
5121  (for cmp (lt le gt ge)
5122   (simplify
5123    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5124    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5125     (if (types_match (from_type, to_type)
5126          /* Check if it is special case A).  */
5127          || (TYPE_UNSIGNED (from_type)
5128              && !TYPE_UNSIGNED (to_type)
5129              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5130              && integer_zerop (@1)
5131              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5132      (with
5133       {
5134         wi::overflow_type overflow = wi::OVF_NONE;
5135         enum tree_code code, cmp_code = cmp;
5136         wide_int real_c1;
5137         wide_int c1 = wi::to_wide (@1);
5138         wide_int c2 = wi::to_wide (@2);
5139         wide_int c3 = wi::to_wide (@3);
5140         signop sgn = TYPE_SIGN (from_type);
5142         /* Handle special case A), given x of unsigned type:
5143             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5144             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5145         if (!types_match (from_type, to_type))
5146           {
5147             if (cmp_code == LT_EXPR)
5148               cmp_code = GT_EXPR;
5149             if (cmp_code == GE_EXPR)
5150               cmp_code = LE_EXPR;
5151             c1 = wi::max_value (to_type);
5152           }
5153         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5154            compute (c3 op' c2) and check if it equals to c1 with op' being
5155            the inverted operator of op.  Make sure overflow doesn't happen
5156            if it is undefined.  */
5157         if (op == PLUS_EXPR)
5158           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5159         else
5160           real_c1 = wi::add (c3, c2, sgn, &overflow);
5162         code = cmp_code;
5163         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5164           {
5165             /* Check if c1 equals to real_c1.  Boundary condition is handled
5166                by adjusting comparison operation if necessary.  */
5167             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5168                 && !overflow)
5169               {
5170                 /* X <= Y - 1 equals to X < Y.  */
5171                 if (cmp_code == LE_EXPR)
5172                   code = LT_EXPR;
5173                 /* X > Y - 1 equals to X >= Y.  */
5174                 if (cmp_code == GT_EXPR)
5175                   code = GE_EXPR;
5176               }
5177             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5178                 && !overflow)
5179               {
5180                 /* X < Y + 1 equals to X <= Y.  */
5181                 if (cmp_code == LT_EXPR)
5182                   code = LE_EXPR;
5183                 /* X >= Y + 1 equals to X > Y.  */
5184                 if (cmp_code == GE_EXPR)
5185                   code = GT_EXPR;
5186               }
5187             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5188               {
5189                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5190                   code = MIN_EXPR;
5191                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5192                   code = MAX_EXPR;
5193               }
5194           }
5195       }
5196       (if (code == MAX_EXPR)
5197        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5198            { wide_int_to_tree (from_type, c2); })
5199        (if (code == MIN_EXPR)
5200         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5201             { wide_int_to_tree (from_type, c2); })))))))))
5203 #if GIMPLE
5204 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5205    in fold_cond_expr_with_comparison for GENERIC folding with
5206    some extra constraints.  */
5207 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5208  (simplify
5209   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5210         (convert3? @0) (convert4? @1))
5211   (if (!HONOR_SIGNED_ZEROS (type)
5212        && (/* Allow widening conversions of the compare operands as data.  */
5213            (INTEGRAL_TYPE_P (type)
5214             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5215             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5216             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5217             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5218            /* Or sign conversions for the comparison.  */
5219            || (types_match (type, TREE_TYPE (@0))
5220                && types_match (type, TREE_TYPE (@1)))))
5221    (switch
5222     (if (cmp == EQ_EXPR)
5223      (if (VECTOR_TYPE_P (type))
5224       (view_convert @c1)
5225       (convert @c1)))
5226     (if (cmp == NE_EXPR)
5227      (if (VECTOR_TYPE_P (type))
5228       (view_convert @c0)
5229       (convert @c0)))
5230     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5231      (if (!HONOR_NANS (type))
5232       (if (VECTOR_TYPE_P (type))
5233        (view_convert (min @c0 @c1))
5234        (convert (min @c0 @c1)))))
5235     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5236      (if (!HONOR_NANS (type))
5237       (if (VECTOR_TYPE_P (type))
5238        (view_convert (max @c0 @c1))
5239        (convert (max @c0 @c1)))))
5240     (if (cmp == UNEQ_EXPR)
5241      (if (!HONOR_NANS (type))
5242       (if (VECTOR_TYPE_P (type))
5243        (view_convert @c1)
5244        (convert @c1))))
5245     (if (cmp == LTGT_EXPR)
5246      (if (!HONOR_NANS (type))
5247       (if (VECTOR_TYPE_P (type))
5248        (view_convert @c0)
5249        (convert @c0))))))))
5250 #endif
5252 /* These was part of minmax phiopt.  */
5253 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5254    to minmax<min/max<a, b>, c> */
5255 (for minmax (min max)
5256  (for cmp (lt le gt ge ne)
5257   (simplify
5258    (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5259    (with
5260     {
5261       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5262     }
5263     (if (code == MIN_EXPR)
5264      (minmax (min @1 @2) @4)
5265      (if (code == MAX_EXPR)
5266       (minmax (max @1 @2) @4)))))))
5268 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5269 (for cmp    (gt  ge  lt  le)
5270      minmax (min min max max)
5271  (simplify
5272   (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4)
5273    (with
5274     {
5275       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5276     }
5277     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5278          && code == MIN_EXPR
5279          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @1)))
5280      (min @2 @4)
5281      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5282           && code == MAX_EXPR
5283           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1)))
5284       (max @2 @4))))))
5286 #if GIMPLE
5287 /* These patterns should be after min/max detection as simplifications
5288    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5289    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5290    Even without those, reaching min/max/and/ior faster is better.  */
5291 (simplify
5292  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5293  (switch
5294   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5295   (if (integer_zerop (@2))
5296    (bit_and (convert @0) @1))
5297   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5298   (if (integer_zerop (@1))
5299    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5300   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5301   (if (integer_onep (@1))
5302    (bit_ior (convert @0) @2))
5303   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5304   (if (integer_onep (@2))
5305    (bit_ior (bit_xor (convert @0) @2) @1))
5308 #endif
5310 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5311 (simplify
5312  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5313  (if (!TYPE_SATURATING (type)
5314       && (TYPE_OVERFLOW_WRAPS (type)
5315           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5316       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5317   @3))
5319 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5320 (simplify
5321  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5322  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5323   @3))
5325 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5326    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5327 (simplify
5328  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5329  (if (TYPE_UNSIGNED (type))
5330   (cond (ge @0 @1) (negate @0) @2)))
5332 (for cnd (cond vec_cond)
5333  /* A ? B : (A ? X : C) -> A ? B : C.  */
5334  (simplify
5335   (cnd @0 (cnd @0 @1 @2) @3)
5336   (cnd @0 @1 @3))
5337  (simplify
5338   (cnd @0 @1 (cnd @0 @2 @3))
5339   (cnd @0 @1 @3))
5340  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5341  /* ???  This matches embedded conditions open-coded because genmatch
5342     would generate matching code for conditions in separate stmts only.
5343     The following is still important to merge then and else arm cases
5344     from if-conversion.  */
5345  (simplify
5346   (cnd @0 @1 (cnd @2 @3 @4))
5347   (if (inverse_conditions_p (@0, @2))
5348    (cnd @0 @1 @3)))
5349  (simplify
5350   (cnd @0 (cnd @1 @2 @3) @4)
5351   (if (inverse_conditions_p (@0, @1))
5352    (cnd @0 @3 @4)))
5354  /* A ? B : B -> B.  */
5355  (simplify
5356   (cnd @0 @1 @1)
5357   @1)
5359  /* !A ? B : C -> A ? C : B.  */
5360  (simplify
5361   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5362   (cnd @0 @2 @1)))
5364 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5365    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5366    Need to handle UN* comparisons.
5368    None of these transformations work for modes with signed
5369    zeros.  If A is +/-0, the first two transformations will
5370    change the sign of the result (from +0 to -0, or vice
5371    versa).  The last four will fix the sign of the result,
5372    even though the original expressions could be positive or
5373    negative, depending on the sign of A.
5375    Note that all these transformations are correct if A is
5376    NaN, since the two alternatives (A and -A) are also NaNs.  */
5378 (for cnd (cond vec_cond)
5379  /* A == 0 ? A : -A    same as -A */
5380  (for cmp (eq uneq)
5381   (simplify
5382    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5383     (if (!HONOR_SIGNED_ZEROS (type))
5384      @1))
5385   (simplify
5386    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5387     (if (!HONOR_SIGNED_ZEROS (type))
5388      @1))
5390  /* A != 0 ? A : -A    same as A */
5391  (for cmp (ne ltgt)
5392   (simplify
5393    (cnd (cmp @0 zerop) @0 (negate @0))
5394     (if (!HONOR_SIGNED_ZEROS (type))
5395      @0))
5396   (simplify
5397    (cnd (cmp @0 zerop) @0 integer_zerop)
5398     (if (!HONOR_SIGNED_ZEROS (type))
5399      @0))
5401  /* A >=/> 0 ? A : -A    same as abs (A) */
5402  (for cmp (ge gt)
5403   (simplify
5404    (cnd (cmp @0 zerop) @0 (negate @0))
5405     (if (!HONOR_SIGNED_ZEROS (type)
5406          && !TYPE_UNSIGNED (type))
5407      (abs @0))))
5408  /* A <=/< 0 ? A : -A    same as -abs (A) */
5409  (for cmp (le lt)
5410   (simplify
5411    (cnd (cmp @0 zerop) @0 (negate @0))
5412     (if (!HONOR_SIGNED_ZEROS (type)
5413          && !TYPE_UNSIGNED (type))
5414      (if (ANY_INTEGRAL_TYPE_P (type)
5415           && !TYPE_OVERFLOW_WRAPS (type))
5416       (with {
5417         tree utype = unsigned_type_for (type);
5418        }
5419        (convert (negate (absu:utype @0))))
5420        (negate (abs @0)))))
5424 /* -(type)!A -> (type)A - 1.  */
5425 (simplify
5426  (negate (convert?:s (logical_inverted_value:s @0)))
5427  (if (INTEGRAL_TYPE_P (type)
5428       && TREE_CODE (type) != BOOLEAN_TYPE
5429       && TYPE_PRECISION (type) > 1
5430       && TREE_CODE (@0) == SSA_NAME
5431       && ssa_name_has_boolean_range (@0))
5432   (plus (convert:type @0) { build_all_ones_cst (type); })))
5434 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5435    return all -1 or all 0 results.  */
5436 /* ??? We could instead convert all instances of the vec_cond to negate,
5437    but that isn't necessarily a win on its own.  */
5438 (simplify
5439  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5440  (if (VECTOR_TYPE_P (type)
5441       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5442                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5443       && (TYPE_MODE (TREE_TYPE (type))
5444           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5445   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5447 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5448 (simplify
5449  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5450  (if (VECTOR_TYPE_P (type)
5451       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5452                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5453       && (TYPE_MODE (TREE_TYPE (type))
5454           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5455   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5458 /* Simplifications of comparisons.  */
5460 /* See if we can reduce the magnitude of a constant involved in a
5461    comparison by changing the comparison code.  This is a canonicalization
5462    formerly done by maybe_canonicalize_comparison_1.  */
5463 (for cmp  (le gt)
5464      acmp (lt ge)
5465  (simplify
5466   (cmp @0 uniform_integer_cst_p@1)
5467   (with { tree cst = uniform_integer_cst_p (@1); }
5468    (if (tree_int_cst_sgn (cst) == -1)
5469      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5470                                    wide_int_to_tree (TREE_TYPE (cst),
5471                                                      wi::to_wide (cst)
5472                                                      + 1)); })))))
5473 (for cmp  (ge lt)
5474      acmp (gt le)
5475  (simplify
5476   (cmp @0 uniform_integer_cst_p@1)
5477   (with { tree cst = uniform_integer_cst_p (@1); }
5478    (if (tree_int_cst_sgn (cst) == 1)
5479     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5480                                   wide_int_to_tree (TREE_TYPE (cst),
5481                                   wi::to_wide (cst) - 1)); })))))
5483 /* We can simplify a logical negation of a comparison to the
5484    inverted comparison.  As we cannot compute an expression
5485    operator using invert_tree_comparison we have to simulate
5486    that with expression code iteration.  */
5487 (for cmp (tcc_comparison)
5488      icmp (inverted_tcc_comparison)
5489      ncmp (inverted_tcc_comparison_with_nans)
5490  /* Ideally we'd like to combine the following two patterns
5491     and handle some more cases by using
5492       (logical_inverted_value (cmp @0 @1))
5493     here but for that genmatch would need to "inline" that.
5494     For now implement what forward_propagate_comparison did.  */
5495  (simplify
5496   (bit_not (cmp @0 @1))
5497   (if (VECTOR_TYPE_P (type)
5498        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5499    /* Comparison inversion may be impossible for trapping math,
5500       invert_tree_comparison will tell us.  But we can't use
5501       a computed operator in the replacement tree thus we have
5502       to play the trick below.  */
5503    (with { enum tree_code ic = invert_tree_comparison
5504              (cmp, HONOR_NANS (@0)); }
5505     (if (ic == icmp)
5506      (icmp @0 @1)
5507      (if (ic == ncmp)
5508       (ncmp @0 @1))))))
5509  (simplify
5510   (bit_xor (cmp @0 @1) integer_truep)
5511   (with { enum tree_code ic = invert_tree_comparison
5512             (cmp, HONOR_NANS (@0)); }
5513    (if (ic == icmp)
5514     (icmp @0 @1)
5515     (if (ic == ncmp)
5516      (ncmp @0 @1)))))
5517  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5518  (simplify
5519   (ne (cmp@2 @0 @1) integer_zerop)
5520   (if (types_match (type, TREE_TYPE (@2)))
5521    (cmp @0 @1)))
5522  (simplify
5523   (eq (cmp@2 @0 @1) integer_truep)
5524   (if (types_match (type, TREE_TYPE (@2)))
5525    (cmp @0 @1)))
5526  (simplify
5527   (ne (cmp@2 @0 @1) integer_truep)
5528   (if (types_match (type, TREE_TYPE (@2)))
5529    (with { enum tree_code ic = invert_tree_comparison
5530              (cmp, HONOR_NANS (@0)); }
5531     (if (ic == icmp)
5532      (icmp @0 @1)
5533      (if (ic == ncmp)
5534       (ncmp @0 @1))))))
5535  (simplify
5536   (eq (cmp@2 @0 @1) integer_zerop)
5537   (if (types_match (type, TREE_TYPE (@2)))
5538    (with { enum tree_code ic = invert_tree_comparison
5539              (cmp, HONOR_NANS (@0)); }
5540     (if (ic == icmp)
5541      (icmp @0 @1)
5542      (if (ic == ncmp)
5543       (ncmp @0 @1)))))))
5545 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5546    ??? The transformation is valid for the other operators if overflow
5547    is undefined for the type, but performing it here badly interacts
5548    with the transformation in fold_cond_expr_with_comparison which
5549    attempts to synthetize ABS_EXPR.  */
5550 (for cmp (eq ne)
5551  (for sub (minus pointer_diff)
5552   (simplify
5553    (cmp (sub@2 @0 @1) integer_zerop)
5554    (if (single_use (@2))
5555     (cmp @0 @1)))))
5557 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5558    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5559 (for cmp (lt ge)
5560  (simplify
5561   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5562    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5563         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5564         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5565     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5566 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5567    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5568 (simplify
5569  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5570   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5571        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5572        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5573    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5575 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5576    signed arithmetic case.  That form is created by the compiler
5577    often enough for folding it to be of value.  One example is in
5578    computing loop trip counts after Operator Strength Reduction.  */
5579 (for cmp (simple_comparison)
5580      scmp (swapped_simple_comparison)
5581  (simplify
5582   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5583   /* Handle unfolded multiplication by zero.  */
5584   (if (integer_zerop (@1))
5585    (cmp @1 @2)
5586    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5587         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5588         && single_use (@3))
5589     /* If @1 is negative we swap the sense of the comparison.  */
5590     (if (tree_int_cst_sgn (@1) < 0)
5591      (scmp @0 @2)
5592      (cmp @0 @2))))))
5594 /* For integral types with undefined overflow fold
5595    x * C1 == C2 into x == C2 / C1 or false.
5596    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5597    Z / 2^n Z.  */
5598 (for cmp (eq ne)
5599  (simplify
5600   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5601   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5602        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5603        && wi::to_wide (@1) != 0)
5604    (with { widest_int quot; }
5605     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5606                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5607      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5608      { constant_boolean_node (cmp == NE_EXPR, type); }))
5609    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5610         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5611         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5612     (cmp @0
5613      {
5614        tree itype = TREE_TYPE (@0);
5615        int p = TYPE_PRECISION (itype);
5616        wide_int m = wi::one (p + 1) << p;
5617        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5618        wide_int i = wide_int::from (wi::mod_inv (a, m),
5619                                     p, TYPE_SIGN (itype));
5620        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5621      })))))
5623 /* Simplify comparison of something with itself.  For IEEE
5624    floating-point, we can only do some of these simplifications.  */
5625 (for cmp (eq ge le)
5626  (simplify
5627   (cmp @0 @0)
5628   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5629        || ! tree_expr_maybe_nan_p (@0))
5630    { constant_boolean_node (true, type); }
5631    (if (cmp != EQ_EXPR
5632         /* With -ftrapping-math conversion to EQ loses an exception.  */
5633         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5634             || ! flag_trapping_math))
5635     (eq @0 @0)))))
5636 (for cmp (ne gt lt)
5637  (simplify
5638   (cmp @0 @0)
5639   (if (cmp != NE_EXPR
5640        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5641        || ! tree_expr_maybe_nan_p (@0))
5642    { constant_boolean_node (false, type); })))
5643 (for cmp (unle unge uneq)
5644  (simplify
5645   (cmp @0 @0)
5646   { constant_boolean_node (true, type); }))
5647 (for cmp (unlt ungt)
5648  (simplify
5649   (cmp @0 @0)
5650   (unordered @0 @0)))
5651 (simplify
5652  (ltgt @0 @0)
5653  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5654   { constant_boolean_node (false, type); }))
5656 /* x == ~x -> false */
5657 /* x != ~x -> true */
5658 (for cmp (eq ne)
5659  (simplify
5660   (cmp:c @0 (bit_not @0))
5661   { constant_boolean_node (cmp == NE_EXPR, type); }))
5663 /* Fold ~X op ~Y as Y op X.  */
5664 (for cmp (simple_comparison)
5665  (simplify
5666   (cmp (bit_not@2 @0) (bit_not@3 @1))
5667   (if (single_use (@2) && single_use (@3))
5668    (cmp @1 @0))))
5670 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5671 (for cmp (simple_comparison)
5672      scmp (swapped_simple_comparison)
5673  (simplify
5674   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5675   (if (single_use (@2)
5676        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5677    (scmp @0 (bit_not @1)))))
5679 (for cmp (simple_comparison)
5680  (simplify
5681   (cmp @0 REAL_CST@1)
5682   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5683   (switch
5684    /* a CMP (-0) -> a CMP 0  */
5685    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5686     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5687    /* (-0) CMP b -> 0 CMP b.  */
5688    (if (TREE_CODE (@0) == REAL_CST
5689         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5690     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5691    /* x != NaN is always true, other ops are always false.  */
5692    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5693         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5694         && !tree_expr_signaling_nan_p (@1)
5695         && !tree_expr_maybe_signaling_nan_p (@0))
5696     { constant_boolean_node (cmp == NE_EXPR, type); })
5697    /* NaN != y is always true, other ops are always false.  */
5698    (if (TREE_CODE (@0) == REAL_CST
5699         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5700         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5701         && !tree_expr_signaling_nan_p (@0)
5702         && !tree_expr_signaling_nan_p (@1))
5703     { constant_boolean_node (cmp == NE_EXPR, type); })
5704    /* Fold comparisons against infinity.  */
5705    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5706         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5707     (with
5708      {
5709        REAL_VALUE_TYPE max;
5710        enum tree_code code = cmp;
5711        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5712        if (neg)
5713          code = swap_tree_comparison (code);
5714      }
5715      (switch
5716       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5717       (if (code == GT_EXPR
5718            && !(HONOR_NANS (@0) && flag_trapping_math))
5719        { constant_boolean_node (false, type); })
5720       (if (code == LE_EXPR)
5721        /* x <= +Inf is always true, if we don't care about NaNs.  */
5722        (if (! HONOR_NANS (@0))
5723         { constant_boolean_node (true, type); }
5724         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5725            an "invalid" exception.  */
5726         (if (!flag_trapping_math)
5727          (eq @0 @0))))
5728       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5729          for == this introduces an exception for x a NaN.  */
5730       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5731            || code == GE_EXPR)
5732        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5733         (if (neg)
5734          (lt @0 { build_real (TREE_TYPE (@0), max); })
5735          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5736       /* x < +Inf is always equal to x <= DBL_MAX.  */
5737       (if (code == LT_EXPR)
5738        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5739         (if (neg)
5740          (ge @0 { build_real (TREE_TYPE (@0), max); })
5741          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5742       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5743          an exception for x a NaN so use an unordered comparison.  */
5744       (if (code == NE_EXPR)
5745        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5746         (if (! HONOR_NANS (@0))
5747          (if (neg)
5748           (ge @0 { build_real (TREE_TYPE (@0), max); })
5749           (le @0 { build_real (TREE_TYPE (@0), max); }))
5750          (if (neg)
5751           (unge @0 { build_real (TREE_TYPE (@0), max); })
5752           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5754  /* If this is a comparison of a real constant with a PLUS_EXPR
5755     or a MINUS_EXPR of a real constant, we can convert it into a
5756     comparison with a revised real constant as long as no overflow
5757     occurs when unsafe_math_optimizations are enabled.  */
5758  (if (flag_unsafe_math_optimizations)
5759   (for op (plus minus)
5760    (simplify
5761     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5762     (with
5763      {
5764        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5765                                TREE_TYPE (@1), @2, @1);
5766      }
5767      (if (tem && !TREE_OVERFLOW (tem))
5768       (cmp @0 { tem; }))))))
5770  /* Likewise, we can simplify a comparison of a real constant with
5771     a MINUS_EXPR whose first operand is also a real constant, i.e.
5772     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
5773     floating-point types only if -fassociative-math is set.  */
5774  (if (flag_associative_math)
5775   (simplify
5776    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5777    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5778     (if (tem && !TREE_OVERFLOW (tem))
5779      (cmp { tem; } @1)))))
5781  /* Fold comparisons against built-in math functions.  */
5782  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5783   (for sq (SQRT)
5784    (simplify
5785     (cmp (sq @0) REAL_CST@1)
5786     (switch
5787      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5788       (switch
5789        /* sqrt(x) < y is always false, if y is negative.  */
5790        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5791         { constant_boolean_node (false, type); })
5792        /* sqrt(x) > y is always true, if y is negative and we
5793           don't care about NaNs, i.e. negative values of x.  */
5794        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5795         { constant_boolean_node (true, type); })
5796        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5797        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5798      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5799       (switch
5800        /* sqrt(x) < 0 is always false.  */
5801        (if (cmp == LT_EXPR)
5802         { constant_boolean_node (false, type); })
5803        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
5804        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5805         { constant_boolean_node (true, type); })
5806        /* sqrt(x) <= 0 -> x == 0.  */
5807        (if (cmp == LE_EXPR)
5808         (eq @0 @1))
5809        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
5810           == or !=.  In the last case:
5812             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5814           if x is negative or NaN.  Due to -funsafe-math-optimizations,
5815           the results for other x follow from natural arithmetic.  */
5816        (cmp @0 @1)))
5817      (if ((cmp == LT_EXPR
5818            || cmp == LE_EXPR
5819            || cmp == GT_EXPR
5820            || cmp == GE_EXPR)
5821           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5822           /* Give up for -frounding-math.  */
5823           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5824       (with
5825        {
5826          REAL_VALUE_TYPE c2;
5827          enum tree_code ncmp = cmp;
5828          const real_format *fmt
5829            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5830          real_arithmetic (&c2, MULT_EXPR,
5831                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5832          real_convert (&c2, fmt, &c2);
5833          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5834             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
5835          if (!REAL_VALUE_ISINF (c2))
5836            {
5837              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5838                                         build_real (TREE_TYPE (@0), c2));
5839              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5840                ncmp = ERROR_MARK;
5841              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5842                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5843                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5844              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5845                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5846                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5847              else
5848                {
5849                  /* With rounding to even, sqrt of up to 3 different values
5850                     gives the same normal result, so in some cases c2 needs
5851                     to be adjusted.  */
5852                  REAL_VALUE_TYPE c2alt, tow;
5853                  if (cmp == LT_EXPR || cmp == GE_EXPR)
5854                    tow = dconst0;
5855                  else
5856                    tow = dconstinf;
5857                  real_nextafter (&c2alt, fmt, &c2, &tow);
5858                  real_convert (&c2alt, fmt, &c2alt);
5859                  if (REAL_VALUE_ISINF (c2alt))
5860                    ncmp = ERROR_MARK;
5861                  else
5862                    {
5863                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5864                                            build_real (TREE_TYPE (@0), c2alt));
5865                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5866                        ncmp = ERROR_MARK;
5867                      else if (real_equal (&TREE_REAL_CST (c3),
5868                                           &TREE_REAL_CST (@1)))
5869                        c2 = c2alt;
5870                    }
5871                }
5872            }
5873        }
5874        (if (cmp == GT_EXPR || cmp == GE_EXPR)
5875         (if (REAL_VALUE_ISINF (c2))
5876          /* sqrt(x) > y is x == +Inf, when y is very large.  */
5877          (if (HONOR_INFINITIES (@0))
5878           (eq @0 { build_real (TREE_TYPE (@0), c2); })
5879           { constant_boolean_node (false, type); })
5880          /* sqrt(x) > c is the same as x > c*c.  */
5881          (if (ncmp != ERROR_MARK)
5882           (if (ncmp == GE_EXPR)
5883            (ge @0 { build_real (TREE_TYPE (@0), c2); })
5884            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5885         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
5886         (if (REAL_VALUE_ISINF (c2))
5887          (switch
5888           /* sqrt(x) < y is always true, when y is a very large
5889              value and we don't care about NaNs or Infinities.  */
5890           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5891            { constant_boolean_node (true, type); })
5892           /* sqrt(x) < y is x != +Inf when y is very large and we
5893              don't care about NaNs.  */
5894           (if (! HONOR_NANS (@0))
5895            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5896           /* sqrt(x) < y is x >= 0 when y is very large and we
5897              don't care about Infinities.  */
5898           (if (! HONOR_INFINITIES (@0))
5899            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5900           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5901           (if (GENERIC)
5902            (truth_andif
5903             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5904             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5905          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5906          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5907           (if (ncmp == LT_EXPR)
5908            (lt @0 { build_real (TREE_TYPE (@0), c2); })
5909            (le @0 { build_real (TREE_TYPE (@0), c2); }))
5910           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5911           (if (ncmp != ERROR_MARK && GENERIC)
5912            (if (ncmp == LT_EXPR)
5913             (truth_andif
5914              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5915              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5916             (truth_andif
5917              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5918              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5919    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
5920    (simplify
5921     (cmp (sq @0) (sq @1))
5922       (if (! HONOR_NANS (@0))
5923         (cmp @0 @1))))))
5925 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
5926 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5927      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
5928  (simplify
5929   (cmp (float@0 @1) (float @2))
5930    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5931         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5932     (with
5933      {
5934        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5935        tree type1 = TREE_TYPE (@1);
5936        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5937        tree type2 = TREE_TYPE (@2);
5938        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5939      }
5940      (if (fmt.can_represent_integral_type_p (type1)
5941           && fmt.can_represent_integral_type_p (type2))
5942       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5943        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5944        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5945             && type1_signed_p >= type2_signed_p)
5946         (icmp @1 (convert @2))
5947         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5948              && type1_signed_p <= type2_signed_p)
5949          (icmp (convert:type2 @1) @2)
5950          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5951               && type1_signed_p == type2_signed_p)
5952           (icmp @1 @2))))))))))
5954 /* Optimize various special cases of (FTYPE) N CMP CST.  */
5955 (for cmp  (lt le eq ne ge gt)
5956      icmp (le le eq ne ge ge)
5957  (simplify
5958   (cmp (float @0) REAL_CST@1)
5959    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5960         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5961     (with
5962      {
5963        tree itype = TREE_TYPE (@0);
5964        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5965        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5966        /* Be careful to preserve any potential exceptions due to
5967           NaNs.  qNaNs are ok in == or != context.
5968           TODO: relax under -fno-trapping-math or
5969           -fno-signaling-nans.  */
5970        bool exception_p
5971          = real_isnan (cst) && (cst->signalling
5972                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5973      }
5974      /* TODO: allow non-fitting itype and SNaNs when
5975         -fno-trapping-math.  */
5976      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5977       (with
5978        {
5979          signop isign = TYPE_SIGN (itype);
5980          REAL_VALUE_TYPE imin, imax;
5981          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5982          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5984          REAL_VALUE_TYPE icst;
5985          if (cmp == GT_EXPR || cmp == GE_EXPR)
5986            real_ceil (&icst, fmt, cst);
5987          else if (cmp == LT_EXPR || cmp == LE_EXPR)
5988            real_floor (&icst, fmt, cst);
5989          else
5990            real_trunc (&icst, fmt, cst);
5992          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5994          bool overflow_p = false;
5995          wide_int icst_val
5996            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5997        }
5998        (switch
5999         /* Optimize cases when CST is outside of ITYPE's range.  */
6000         (if (real_compare (LT_EXPR, cst, &imin))
6001          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6002                                   type); })
6003         (if (real_compare (GT_EXPR, cst, &imax))
6004          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6005                                   type); })
6006         /* Remove cast if CST is an integer representable by ITYPE.  */
6007         (if (cst_int_p)
6008          (cmp @0 { gcc_assert (!overflow_p);
6009                    wide_int_to_tree (itype, icst_val); })
6010         )
6011         /* When CST is fractional, optimize
6012             (FTYPE) N == CST -> 0
6013             (FTYPE) N != CST -> 1.  */
6014         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6015          { constant_boolean_node (cmp == NE_EXPR, type); })
6016         /* Otherwise replace with sensible integer constant.  */
6017         (with
6018          {
6019            gcc_checking_assert (!overflow_p);
6020          }
6021          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6023 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6024 (for cmp (eq ne)
6025  (simplify
6026   (cmp (exact_div @0 @1) INTEGER_CST@2)
6027   (if (!integer_zerop (@1))
6028    (if (wi::to_wide (@2) == 0)
6029     (cmp @0 @2)
6030     (if (TREE_CODE (@1) == INTEGER_CST)
6031      (with
6032       {
6033         wi::overflow_type ovf;
6034         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6035                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6036       }
6037       (if (ovf)
6038        { constant_boolean_node (cmp == NE_EXPR, type); }
6039        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6040 (for cmp (lt le gt ge)
6041  (simplify
6042   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6043   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6044    (with
6045     {
6046       wi::overflow_type ovf;
6047       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6048                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6049     }
6050     (if (ovf)
6051      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6052                                         TYPE_SIGN (TREE_TYPE (@2)))
6053                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6054      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6056 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6058    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6059    For large C (more than min/B+2^size), this is also true, with the
6060    multiplication computed modulo 2^size.
6061    For intermediate C, this just tests the sign of A.  */
6062 (for cmp  (lt le gt ge)
6063      cmp2 (ge ge lt lt)
6064  (simplify
6065   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6066   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6067        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6068        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6069    (with
6070     {
6071       tree utype = TREE_TYPE (@2);
6072       wide_int denom = wi::to_wide (@1);
6073       wide_int right = wi::to_wide (@2);
6074       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6075       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6076       bool small = wi::leu_p (right, smax);
6077       bool large = wi::geu_p (right, smin);
6078     }
6079     (if (small || large)
6080      (cmp (convert:utype @0) (mult @2 (convert @1)))
6081      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6083 /* Unordered tests if either argument is a NaN.  */
6084 (simplify
6085  (bit_ior (unordered @0 @0) (unordered @1 @1))
6086  (if (types_match (@0, @1))
6087   (unordered @0 @1)))
6088 (simplify
6089  (bit_and (ordered @0 @0) (ordered @1 @1))
6090  (if (types_match (@0, @1))
6091   (ordered @0 @1)))
6092 (simplify
6093  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6094  @2)
6095 (simplify
6096  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6097  @2)
6099 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6100    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6102    Note that comparisons
6103      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6104      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6105    will be canonicalized to above so there's no need to
6106    consider them here.
6107  */
6109 (for cmp (le gt)
6110      eqcmp (eq ne)
6111  (simplify
6112   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6113   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6114    (with
6115     {
6116      tree ty = TREE_TYPE (@0);
6117      unsigned prec = TYPE_PRECISION (ty);
6118      wide_int mask = wi::to_wide (@2, prec);
6119      wide_int rhs = wi::to_wide (@3, prec);
6120      signop sgn = TYPE_SIGN (ty);
6121     }
6122     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6123          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6124       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6125              { build_zero_cst (ty); }))))))
6127 /* -A CMP -B -> B CMP A.  */
6128 (for cmp (tcc_comparison)
6129      scmp (swapped_tcc_comparison)
6130  (simplify
6131   (cmp (negate @0) (negate @1))
6132   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6133        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6134            && (cmp == EQ_EXPR
6135                || cmp == NE_EXPR
6136                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6137    (scmp @0 @1)))
6138  (simplify
6139   (cmp (negate @0) CONSTANT_CLASS_P@1)
6140   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6141        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6142            && (cmp == EQ_EXPR
6143                || cmp == NE_EXPR
6144                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6145    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6146     (if (tem && !TREE_OVERFLOW (tem))
6147      (scmp @0 { tem; }))))))
6149 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6150 (for op (abs absu)
6151  (for eqne (eq ne)
6152   (simplify
6153    (eqne (op @0) zerop@1)
6154    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6156 /* From fold_sign_changed_comparison and fold_widened_comparison.
6157    FIXME: the lack of symmetry is disturbing.  */
6158 (for cmp (simple_comparison)
6159  (simplify
6160   (cmp (convert@0 @00) (convert?@1 @10))
6161   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6162        /* Disable this optimization if we're casting a function pointer
6163           type on targets that require function pointer canonicalization.  */
6164        && !(targetm.have_canonicalize_funcptr_for_compare ()
6165             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6166                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6167                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6168                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6169        && single_use (@0))
6170    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6171         && (TREE_CODE (@10) == INTEGER_CST
6172             || @1 != @10)
6173         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6174             || cmp == NE_EXPR
6175             || cmp == EQ_EXPR)
6176         && !POINTER_TYPE_P (TREE_TYPE (@00))
6177         /* (int)bool:32 != (int)uint is not the same as
6178            bool:32 != (bool:32)uint since boolean types only have two valid
6179            values independent of their precision.  */
6180         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6181             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6182     /* ???  The special-casing of INTEGER_CST conversion was in the original
6183        code and here to avoid a spurious overflow flag on the resulting
6184        constant which fold_convert produces.  */
6185     (if (TREE_CODE (@1) == INTEGER_CST)
6186      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6187                                 TREE_OVERFLOW (@1)); })
6188      (cmp @00 (convert @1)))
6190     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6191      /* If possible, express the comparison in the shorter mode.  */
6192      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6193            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6194            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6195                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6196           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6197               || ((TYPE_PRECISION (TREE_TYPE (@00))
6198                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6199                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6200                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6201               || (TREE_CODE (@10) == INTEGER_CST
6202                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6203                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6204       (cmp @00 (convert @10))
6205       (if (TREE_CODE (@10) == INTEGER_CST
6206            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6207            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6208        (with
6209         {
6210           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6211           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6212           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6213           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6214         }
6215         (if (above || below)
6216          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6217           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6218           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6219            { constant_boolean_node (above ? true : false, type); }
6220            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6221             { constant_boolean_node (above ? false : true, type); })))))))))
6222    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6223    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6224         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6225             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6226         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6227             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6228     (with
6229      {
6230        tree type1 = TREE_TYPE (@10);
6231        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6232          {
6233            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6234            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6235                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6236              type1 = float_type_node;
6237            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6238                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6239              type1 = double_type_node;
6240          }
6241       tree newtype
6242         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6243            ? TREE_TYPE (@00) : type1);
6244      }
6245      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6246       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6249 (for cmp (eq ne)
6250  (simplify
6251   /* SSA names are canonicalized to 2nd place.  */
6252   (cmp addr@0 SSA_NAME@1)
6253   (with
6254    {
6255      poly_int64 off; tree base;
6256      tree addr = (TREE_CODE (@0) == SSA_NAME
6257                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6258    }
6259    /* A local variable can never be pointed to by
6260       the default SSA name of an incoming parameter.  */
6261    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6262         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6263         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6264         && TREE_CODE (base) == VAR_DECL
6265         && auto_var_in_fn_p (base, current_function_decl))
6266     (if (cmp == NE_EXPR)
6267      { constant_boolean_node (true, type); }
6268      { constant_boolean_node (false, type); })
6269     /* If the address is based on @1 decide using the offset.  */
6270     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6271          && TREE_CODE (base) == MEM_REF
6272          && TREE_OPERAND (base, 0) == @1)
6273      (with { off += mem_ref_offset (base).force_shwi (); }
6274       (if (known_ne (off, 0))
6275        { constant_boolean_node (cmp == NE_EXPR, type); }
6276        (if (known_eq (off, 0))
6277         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6279 /* Equality compare simplifications from fold_binary  */
6280 (for cmp (eq ne)
6282  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6283     Similarly for NE_EXPR.  */
6284  (simplify
6285   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6286   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6287        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6288    { constant_boolean_node (cmp == NE_EXPR, type); }))
6290  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6291  (simplify
6292   (cmp (bit_xor @0 @1) integer_zerop)
6293   (cmp @0 @1))
6295  /* (X ^ Y) == Y becomes X == 0.
6296     Likewise (X ^ Y) == X becomes Y == 0.  */
6297  (simplify
6298   (cmp:c (bit_xor:c @0 @1) @0)
6299   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6301  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6302  (simplify
6303   (cmp:c (bit_and:c @0 @1) @0)
6304   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6305  (simplify
6306   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6307   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6308        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6309        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6310        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6311        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6312        && !wi::neg_p (wi::to_wide (@1)))
6313    (cmp (bit_and @0 (convert (bit_not @1)))
6314         { build_zero_cst (TREE_TYPE (@0)); })))
6316  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6317  (simplify
6318   (cmp:c (bit_ior:c @0 @1) @1)
6319   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6321  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6322  (simplify
6323   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6324   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6325    (cmp @0 (bit_xor @1 (convert @2)))))
6327  (simplify
6328   (cmp (nop_convert? @0) integer_zerop)
6329   (if (tree_expr_nonzero_p (@0))
6330    { constant_boolean_node (cmp == NE_EXPR, type); }))
6332  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6333  (simplify
6334   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6335   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6337 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6338    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6339    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6340    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6341 (for cmp (eq ne)
6342      ncmp (ge lt)
6343  (for sgncmp (ge lt)
6344   (simplify
6345    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6346    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6347         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6348         && types_match (@0, @1))
6349     (ncmp (bit_xor @0 @1) @2)))))
6350 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6351    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6352 (for cmp (eq ne)
6353      ncmp (lt ge)
6354  (simplify
6355   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6356    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6357         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6358         && types_match (@0, @1))
6359     (ncmp (bit_xor @0 @1) @2))))
6361 /* If we have (A & C) == C where C is a power of 2, convert this into
6362    (A & C) != 0.  Similarly for NE_EXPR.  */
6363 (for cmp (eq ne)
6364      icmp (ne eq)
6365  (simplify
6366   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6367   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6369 #if GIMPLE
6370 /* From fold_binary_op_with_conditional_arg handle the case of
6371    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6372    compares simplify.  */
6373 (for cmp (simple_comparison)
6374  (simplify
6375   (cmp:c (cond @0 @1 @2) @3)
6376   /* Do not move possibly trapping operations into the conditional as this
6377      pessimizes code and causes gimplification issues when applied late.  */
6378   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6379        || !operation_could_trap_p (cmp, true, false, @3))
6380    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6381 #endif
6383 (for cmp (ge lt)
6384 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6385 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6386  (simplify
6387   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6388    (if (INTEGRAL_TYPE_P (type)
6389         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6390         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6391         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6392     (with
6393      {
6394        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6395      }
6396     (if (cmp == LT_EXPR)
6397      (bit_xor (convert (rshift @0 {shifter;})) @1)
6398      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6399 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6400 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6401  (simplify
6402   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6403    (if (INTEGRAL_TYPE_P (type)
6404         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6405         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6406         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6407     (with
6408      {
6409        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6410      }
6411     (if (cmp == GE_EXPR)
6412      (bit_xor (convert (rshift @0 {shifter;})) @1)
6413      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6415 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6416    convert this into a shift followed by ANDing with D.  */
6417 (simplify
6418  (cond
6419   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6420   INTEGER_CST@2 integer_zerop)
6421  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6422   (with {
6423      int shift = (wi::exact_log2 (wi::to_wide (@2))
6424                   - wi::exact_log2 (wi::to_wide (@1)));
6425    }
6426    (if (shift > 0)
6427     (bit_and
6428      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6429     (bit_and
6430      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6431      @2)))))
6433 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6434    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6435 (for cmp (eq ne)
6436      ncmp (ge lt)
6437  (simplify
6438   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6439   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6440        && type_has_mode_precision_p (TREE_TYPE (@0))
6441        && element_precision (@2) >= element_precision (@0)
6442        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6443    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6444     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6446 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6447    this into a right shift or sign extension followed by ANDing with C.  */
6448 (simplify
6449  (cond
6450   (lt @0 integer_zerop)
6451   INTEGER_CST@1 integer_zerop)
6452  (if (integer_pow2p (@1)
6453       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6454   (with {
6455     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6456    }
6457    (if (shift >= 0)
6458     (bit_and
6459      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6460      @1)
6461     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6462        sign extension followed by AND with C will achieve the effect.  */
6463     (bit_and (convert @0) @1)))))
6465 /* When the addresses are not directly of decls compare base and offset.
6466    This implements some remaining parts of fold_comparison address
6467    comparisons but still no complete part of it.  Still it is good
6468    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6469 (for cmp (simple_comparison)
6470  (simplify
6471   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6472   (with
6473    {
6474      poly_int64 off0, off1;
6475      tree base0, base1;
6476      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6477                                   off0, off1, GENERIC);
6478    }
6479    (if (equal == 1)
6480     (switch
6481      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6482       { constant_boolean_node (known_eq (off0, off1), type); })
6483      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6484       { constant_boolean_node (known_ne (off0, off1), type); })
6485      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6486       { constant_boolean_node (known_lt (off0, off1), type); })
6487      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6488       { constant_boolean_node (known_le (off0, off1), type); })
6489      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6490       { constant_boolean_node (known_ge (off0, off1), type); })
6491      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6492       { constant_boolean_node (known_gt (off0, off1), type); }))
6493     (if (equal == 0)
6494      (switch
6495       (if (cmp == EQ_EXPR)
6496        { constant_boolean_node (false, type); })
6497       (if (cmp == NE_EXPR)
6498        { constant_boolean_node (true, type); })))))))
6500 #if GIMPLE
6501 /* a?~t:t -> (-(a))^t */
6502 (simplify
6503  (cond @0 @1 @2)
6504  (with { bool wascmp; }
6505   (if (INTEGRAL_TYPE_P (type)
6506        && bitwise_inverted_equal_p (@1, @2, wascmp)
6507        && (!wascmp || element_precision (type) == 1))
6508    (with {
6509      auto prec = TYPE_PRECISION (type);
6510      auto unsign = TYPE_UNSIGNED (type);
6511      tree inttype = build_nonstandard_integer_type (prec, unsign);
6512     }
6513     (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
6514 #endif
6516 /* Simplify pointer equality compares using PTA.  */
6517 (for neeq (ne eq)
6518  (simplify
6519   (neeq @0 @1)
6520   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6521        && ptrs_compare_unequal (@0, @1))
6522    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6524 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6525    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6526    Disable the transform if either operand is pointer to function.
6527    This broke pr22051-2.c for arm where function pointer
6528    canonicalizaion is not wanted.  */
6530 (for cmp (ne eq)
6531  (simplify
6532   (cmp (convert @0) INTEGER_CST@1)
6533   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6534          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6535          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6536          /* Don't perform this optimization in GENERIC if @0 has reference
6537             type when sanitizing.  See PR101210.  */
6538          && !(GENERIC
6539               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6540               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6541         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6542             && POINTER_TYPE_P (TREE_TYPE (@1))
6543             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6544        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6545    (cmp @0 (convert @1)))))
6547 /* Non-equality compare simplifications from fold_binary  */
6548 (for cmp (lt gt le ge)
6549  /* Comparisons with the highest or lowest possible integer of
6550     the specified precision will have known values.  */
6551  (simplify
6552   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6553   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6554         || POINTER_TYPE_P (TREE_TYPE (@1))
6555         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6556        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6557    (with
6558     {
6559       tree cst = uniform_integer_cst_p (@1);
6560       tree arg1_type = TREE_TYPE (cst);
6561       unsigned int prec = TYPE_PRECISION (arg1_type);
6562       wide_int max = wi::max_value (arg1_type);
6563       wide_int signed_max = wi::max_value (prec, SIGNED);
6564       wide_int min = wi::min_value (arg1_type);
6565     }
6566     (switch
6567      (if (wi::to_wide (cst) == max)
6568       (switch
6569        (if (cmp == GT_EXPR)
6570         { constant_boolean_node (false, type); })
6571        (if (cmp == GE_EXPR)
6572         (eq @2 @1))
6573        (if (cmp == LE_EXPR)
6574         { constant_boolean_node (true, type); })
6575        (if (cmp == LT_EXPR)
6576         (ne @2 @1))))
6577      (if (wi::to_wide (cst) == min)
6578       (switch
6579        (if (cmp == LT_EXPR)
6580         { constant_boolean_node (false, type); })
6581        (if (cmp == LE_EXPR)
6582         (eq @2 @1))
6583        (if (cmp == GE_EXPR)
6584         { constant_boolean_node (true, type); })
6585        (if (cmp == GT_EXPR)
6586         (ne @2 @1))))
6587      (if (wi::to_wide (cst) == max - 1)
6588       (switch
6589        (if (cmp == GT_EXPR)
6590         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6591                                     wide_int_to_tree (TREE_TYPE (cst),
6592                                                       wi::to_wide (cst)
6593                                                       + 1)); }))
6594        (if (cmp == LE_EXPR)
6595         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6596                                     wide_int_to_tree (TREE_TYPE (cst),
6597                                                       wi::to_wide (cst)
6598                                                       + 1)); }))))
6599      (if (wi::to_wide (cst) == min + 1)
6600       (switch
6601        (if (cmp == GE_EXPR)
6602         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6603                                     wide_int_to_tree (TREE_TYPE (cst),
6604                                                       wi::to_wide (cst)
6605                                                       - 1)); }))
6606        (if (cmp == LT_EXPR)
6607         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6608                                     wide_int_to_tree (TREE_TYPE (cst),
6609                                                       wi::to_wide (cst)
6610                                                       - 1)); }))))
6611      (if (wi::to_wide (cst) == signed_max
6612           && TYPE_UNSIGNED (arg1_type)
6613           /* We will flip the signedness of the comparison operator
6614              associated with the mode of @1, so the sign bit is
6615              specified by this mode.  Check that @1 is the signed
6616              max associated with this sign bit.  */
6617           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6618           /* signed_type does not work on pointer types.  */
6619           && INTEGRAL_TYPE_P (arg1_type))
6620       /* The following case also applies to X < signed_max+1
6621          and X >= signed_max+1 because previous transformations.  */
6622       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6623        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6624         (switch
6625          (if (cst == @1 && cmp == LE_EXPR)
6626           (ge (convert:st @0) { build_zero_cst (st); }))
6627          (if (cst == @1 && cmp == GT_EXPR)
6628           (lt (convert:st @0) { build_zero_cst (st); }))
6629          (if (cmp == LE_EXPR)
6630           (ge (view_convert:st @0) { build_zero_cst (st); }))
6631          (if (cmp == GT_EXPR)
6632           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6634 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
6635 (simplify
6636  (lt:c @0 (convert (ne @0 integer_zerop)))
6637  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6638   { constant_boolean_node (false, type); }))
6640 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6641 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6642 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6643 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6644 (for outer (ne eq)
6645  (for inner (ne eq)
6646   (simplify
6647    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6648    (with {
6649      bool cst1 = integer_onep (@1);
6650      bool cst0 = integer_zerop (@1);
6651      bool innereq = inner == EQ_EXPR;
6652      bool outereq = outer == EQ_EXPR;
6653     }
6654     (switch
6655      (if (innereq ? cst0 : cst1)
6656       { constant_boolean_node (!outereq, type); })
6657      (if (innereq ? cst1 : cst0)
6658       (with {
6659         tree utype = unsigned_type_for (TREE_TYPE (@0));
6660         tree ucst1 = build_one_cst (utype);
6661        }
6662        (if (!outereq)
6663         (gt (convert:utype @0) { ucst1; })
6664         (le (convert:utype @0) { ucst1; })
6665        )
6666       )
6667      )
6668      (with {
6669        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6670       }
6671       (if (outereq)
6672        (eq @0 { value; })
6673        (ne @0 { value; })
6674       )
6675      )
6676     )
6677    )
6678   )
6682 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6683  /* If the second operand is NaN, the result is constant.  */
6684  (simplify
6685   (cmp @0 REAL_CST@1)
6686   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6687        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6688    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6689                             ? false : true, type); })))
6691 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6692 (simplify
6693   (unordered @0 @1)
6694   (switch
6695     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6696         { constant_boolean_node (true, type); })
6697     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6698         { constant_boolean_node (false, type); })))
6700 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6701 (simplify
6702   (ordered @0 @1)
6703   (switch
6704     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6705         { constant_boolean_node (false, type); })
6706     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6707         { constant_boolean_node (true, type); })))
6709 /* bool_var != 0 becomes bool_var.  */
6710 (simplify
6711  (ne @0 integer_zerop)
6712  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6713       && types_match (type, TREE_TYPE (@0)))
6714   (non_lvalue @0)))
6715 /* bool_var == 1 becomes bool_var.  */
6716 (simplify
6717  (eq @0 integer_onep)
6718  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6719       && types_match (type, TREE_TYPE (@0)))
6720   (non_lvalue @0)))
6721 /* Do not handle
6722    bool_var == 0 becomes !bool_var or
6723    bool_var != 1 becomes !bool_var
6724    here because that only is good in assignment context as long
6725    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6726    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6727    clearly less optimal and which we'll transform again in forwprop.  */
6729 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6730    where ~Y + 1 == pow2 and Z = ~Y.  */
6731 (for cst (VECTOR_CST INTEGER_CST)
6732  (for cmp (eq ne)
6733       icmp (le gt)
6734   (simplify
6735    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6736     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6737      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6738       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6739                          ? optab_vector : optab_default;
6740               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6741        (if (target_supports_op_p (utype, icmp, optab)
6742             || (optimize_vectors_before_lowering_p ()
6743                 && (!target_supports_op_p (type, cmp, optab)
6744                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6745         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6746          (icmp @0 { csts; })
6747          (icmp (view_convert:utype @0) { csts; })))))))))
6749 /* When one argument is a constant, overflow detection can be simplified.
6750    Currently restricted to single use so as not to interfere too much with
6751    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6752    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6753 (for cmp (lt le ge gt)
6754      out (gt gt le le)
6755  (simplify
6756   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6757   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6758        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6759        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6760        && wi::to_wide (@1) != 0
6761        && single_use (@2))
6762    (with {
6763      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6764      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6765     }
6766     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6767                                 wi::max_value (prec, sign)
6768                                 - wi::to_wide (@1)); })))))
6770 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6771    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6772    expects the long form, so we restrict the transformation for now.  */
6773 (for cmp (gt le)
6774  (simplify
6775   (cmp:c (minus@2 @0 @1) @0)
6776   (if (single_use (@2)
6777        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6778        && TYPE_UNSIGNED (TREE_TYPE (@0)))
6779    (cmp @1 @0))))
6781 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
6782 (for cmp (ge lt)
6783  (simplify
6784   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6785    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6786         && TYPE_UNSIGNED (TREE_TYPE (@0)))
6787     (cmp @1 @0))))
6789 /* Testing for overflow is unnecessary if we already know the result.  */
6790 /* A - B > A  */
6791 (for cmp (gt le)
6792      out (ne eq)
6793  (simplify
6794   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6795   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6796        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6797    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6798 /* A + B < A  */
6799 (for cmp (lt ge)
6800      out (ne eq)
6801  (simplify
6802   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6803   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6804        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6805    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6807 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6808    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
6809 (for cmp (lt ge)
6810      out (ne eq)
6811  (simplify
6812   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6813   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6814    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6815     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6817 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6818    is at least twice as wide as type of A and B, simplify to
6819    __builtin_mul_overflow (A, B, <unused>).  */
6820 (for cmp (eq ne)
6821  (simplify
6822   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6823        integer_zerop)
6824   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6825        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6826        && TYPE_UNSIGNED (TREE_TYPE (@0))
6827        && (TYPE_PRECISION (TREE_TYPE (@3))
6828            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6829        && tree_fits_uhwi_p (@2)
6830        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6831        && types_match (@0, @1)
6832        && type_has_mode_precision_p (TREE_TYPE (@0))
6833        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6834            != CODE_FOR_nothing))
6835    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6836     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6838 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
6839 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6840  (simplify
6841   (ovf (convert@2 @0) @1)
6842   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6843        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6844        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6845        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6846    (ovf @0 @1)))
6847  (simplify
6848   (ovf @1 (convert@2 @0))
6849   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6850        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6851        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6852        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6853    (ovf @1 @0))))
6855 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6856    are unsigned to x > (umax / cst).  Similarly for signed type, but
6857    in that case it needs to be outside of a range.  */
6858 (simplify
6859  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6860   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6861        && TYPE_MAX_VALUE (TREE_TYPE (@0))
6862        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6863        && int_fits_type_p (@1, TREE_TYPE (@0)))
6864    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6865     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6866     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6867      (if (integer_minus_onep (@1))
6868       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6869       (with
6870        {
6871          tree div = fold_convert (TREE_TYPE (@0), @1);
6872          tree lo = int_const_binop (TRUNC_DIV_EXPR,
6873                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6874          tree hi = int_const_binop (TRUNC_DIV_EXPR,
6875                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6876          tree etype = range_check_type (TREE_TYPE (@0));
6877          if (etype)
6878            {
6879              if (wi::neg_p (wi::to_wide (div)))
6880                std::swap (lo, hi);
6881              lo = fold_convert (etype, lo);
6882              hi = fold_convert (etype, hi);
6883              hi = int_const_binop (MINUS_EXPR, hi, lo);
6884            }
6885        }
6886        (if (etype)
6887         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6889 /* Simplification of math builtins.  These rules must all be optimizations
6890    as well as IL simplifications.  If there is a possibility that the new
6891    form could be a pessimization, the rule should go in the canonicalization
6892    section that follows this one.
6894    Rules can generally go in this section if they satisfy one of
6895    the following:
6897    - the rule describes an identity
6899    - the rule replaces calls with something as simple as addition or
6900      multiplication
6902    - the rule contains unary calls only and simplifies the surrounding
6903      arithmetic.  (The idea here is to exclude non-unary calls in which
6904      one operand is constant and in which the call is known to be cheap
6905      when the operand has that value.)  */
6907 (if (flag_unsafe_math_optimizations)
6908  /* Simplify sqrt(x) * sqrt(x) -> x.  */
6909  (simplify
6910   (mult (SQRT_ALL@1 @0) @1)
6911   (if (!tree_expr_maybe_signaling_nan_p (@0))
6912    @0))
6914  (for op (plus minus)
6915   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
6916   (simplify
6917    (op (rdiv @0 @1)
6918        (rdiv @2 @1))
6919    (rdiv (op @0 @2) @1)))
6921  (for cmp (lt le gt ge)
6922       neg_cmp (gt ge lt le)
6923   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
6924   (simplify
6925    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6926    (with
6927     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6928     (if (tem
6929          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6930               || (real_zerop (tem) && !real_zerop (@1))))
6931      (switch
6932       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6933        (cmp @0 { tem; }))
6934       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6935        (neg_cmp @0 { tem; })))))))
6937  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
6938  (for root (SQRT CBRT)
6939   (simplify
6940    (mult (root:s @0) (root:s @1))
6941     (root (mult @0 @1))))
6943  /* Simplify expN(x) * expN(y) -> expN(x+y). */
6944  (for exps (EXP EXP2 EXP10 POW10)
6945   (simplify
6946    (mult (exps:s @0) (exps:s @1))
6947     (exps (plus @0 @1))))
6949  /* Simplify a/root(b/c) into a*root(c/b).  */
6950  (for root (SQRT CBRT)
6951   (simplify
6952    (rdiv @0 (root:s (rdiv:s @1 @2)))
6953     (mult @0 (root (rdiv @2 @1)))))
6955  /* Simplify x/expN(y) into x*expN(-y).  */
6956  (for exps (EXP EXP2 EXP10 POW10)
6957   (simplify
6958    (rdiv @0 (exps:s @1))
6959     (mult @0 (exps (negate @1)))))
6961  (for logs (LOG LOG2 LOG10 LOG10)
6962       exps (EXP EXP2 EXP10 POW10)
6963   /* logN(expN(x)) -> x.  */
6964   (simplify
6965    (logs (exps @0))
6966    @0)
6967   /* expN(logN(x)) -> x.  */
6968   (simplify
6969    (exps (logs @0))
6970    @0))
6972  /* Optimize logN(func()) for various exponential functions.  We
6973     want to determine the value "x" and the power "exponent" in
6974     order to transform logN(x**exponent) into exponent*logN(x).  */
6975  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
6976       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
6977   (simplify
6978    (logs (exps @0))
6979    (if (SCALAR_FLOAT_TYPE_P (type))
6980     (with {
6981       tree x;
6982       switch (exps)
6983         {
6984         CASE_CFN_EXP:
6985           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
6986           x = build_real_truncate (type, dconst_e ());
6987           break;
6988         CASE_CFN_EXP2:
6989           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
6990           x = build_real (type, dconst2);
6991           break;
6992         CASE_CFN_EXP10:
6993         CASE_CFN_POW10:
6994           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
6995           {
6996             REAL_VALUE_TYPE dconst10;
6997             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6998             x = build_real (type, dconst10);
6999           }
7000           break;
7001         default:
7002           gcc_unreachable ();
7003         }
7004       }
7005      (mult (logs { x; }) @0)))))
7007  (for logs (LOG LOG
7008             LOG2 LOG2
7009             LOG10 LOG10)
7010       exps (SQRT CBRT)
7011   (simplify
7012    (logs (exps @0))
7013    (if (SCALAR_FLOAT_TYPE_P (type))
7014     (with {
7015       tree x;
7016       switch (exps)
7017         {
7018         CASE_CFN_SQRT:
7019           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7020           x = build_real (type, dconsthalf);
7021           break;
7022         CASE_CFN_CBRT:
7023           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7024           x = build_real_truncate (type, dconst_third ());
7025           break;
7026         default:
7027           gcc_unreachable ();
7028         }
7029       }
7030      (mult { x; } (logs @0))))))
7032  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7033  (for logs (LOG LOG2 LOG10)
7034       pows (POW)
7035   (simplify
7036    (logs (pows @0 @1))
7037    (mult @1 (logs @0))))
7039  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7040     or if C is a positive power of 2,
7041     pow(C,x) -> exp2(log2(C)*x).  */
7042 #if GIMPLE
7043  (for pows (POW)
7044       exps (EXP)
7045       logs (LOG)
7046       exp2s (EXP2)
7047       log2s (LOG2)
7048   (simplify
7049    (pows REAL_CST@0 @1)
7050    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7051         && real_isfinite (TREE_REAL_CST_PTR (@0))
7052         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7053            the use_exp2 case until after vectorization.  It seems actually
7054            beneficial for all constants to postpone this until later,
7055            because exp(log(C)*x), while faster, will have worse precision
7056            and if x folds into a constant too, that is unnecessary
7057            pessimization.  */
7058         && canonicalize_math_after_vectorization_p ())
7059     (with {
7060        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7061        bool use_exp2 = false;
7062        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7063            && value->cl == rvc_normal)
7064          {
7065            REAL_VALUE_TYPE frac_rvt = *value;
7066            SET_REAL_EXP (&frac_rvt, 1);
7067            if (real_equal (&frac_rvt, &dconst1))
7068              use_exp2 = true;
7069          }
7070      }
7071      (if (!use_exp2)
7072       (if (optimize_pow_to_exp (@0, @1))
7073        (exps (mult (logs @0) @1)))
7074       (exp2s (mult (log2s @0) @1)))))))
7075 #endif
7077  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7078  (for pows (POW)
7079       exps (EXP EXP2 EXP10 POW10)
7080       logs (LOG LOG2 LOG10 LOG10)
7081   (simplify
7082    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7083    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7084         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7085     (exps (plus (mult (logs @0) @1) @2)))))
7087  (for sqrts (SQRT)
7088       cbrts (CBRT)
7089       pows (POW)
7090       exps (EXP EXP2 EXP10 POW10)
7091   /* sqrt(expN(x)) -> expN(x*0.5).  */
7092   (simplify
7093    (sqrts (exps @0))
7094    (exps (mult @0 { build_real (type, dconsthalf); })))
7095   /* cbrt(expN(x)) -> expN(x/3).  */
7096   (simplify
7097    (cbrts (exps @0))
7098    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7099   /* pow(expN(x), y) -> expN(x*y).  */
7100   (simplify
7101    (pows (exps @0) @1)
7102    (exps (mult @0 @1))))
7104  /* tan(atan(x)) -> x.  */
7105  (for tans (TAN)
7106       atans (ATAN)
7107   (simplify
7108    (tans (atans @0))
7109    @0)))
7111  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7112  (for sins (SIN)
7113       atans (ATAN)
7114       sqrts (SQRT)
7115       copysigns (COPYSIGN)
7116   (simplify
7117    (sins (atans:s @0))
7118    (with
7119      {
7120       REAL_VALUE_TYPE r_cst;
7121       build_sinatan_real (&r_cst, type);
7122       tree t_cst = build_real (type, r_cst);
7123       tree t_one = build_one_cst (type);
7124      }
7125     (if (SCALAR_FLOAT_TYPE_P (type))
7126      (cond (lt (abs @0) { t_cst; })
7127       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7128       (copysigns { t_one; } @0))))))
7130 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7131  (for coss (COS)
7132       atans (ATAN)
7133       sqrts (SQRT)
7134       copysigns (COPYSIGN)
7135   (simplify
7136    (coss (atans:s @0))
7137    (with
7138      {
7139       REAL_VALUE_TYPE r_cst;
7140       build_sinatan_real (&r_cst, type);
7141       tree t_cst = build_real (type, r_cst);
7142       tree t_one = build_one_cst (type);
7143       tree t_zero = build_zero_cst (type);
7144      }
7145     (if (SCALAR_FLOAT_TYPE_P (type))
7146      (cond (lt (abs @0) { t_cst; })
7147       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7148       (copysigns { t_zero; } @0))))))
7150  (if (!flag_errno_math)
7151   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7152   (for sinhs (SINH)
7153        atanhs (ATANH)
7154        sqrts (SQRT)
7155    (simplify
7156     (sinhs (atanhs:s @0))
7157     (with { tree t_one = build_one_cst (type); }
7158     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7160   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7161   (for coshs (COSH)
7162        atanhs (ATANH)
7163        sqrts (SQRT)
7164    (simplify
7165     (coshs (atanhs:s @0))
7166     (with { tree t_one = build_one_cst (type); }
7167     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7169 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7170 (simplify
7171  (CABS (complex:C @0 real_zerop@1))
7172  (abs @0))
7174 /* trunc(trunc(x)) -> trunc(x), etc.  */
7175 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7176  (simplify
7177   (fns (fns @0))
7178   (fns @0)))
7179 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7180 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7181  (simplify
7182   (fns integer_valued_real_p@0)
7183   @0))
7185 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7186 (simplify
7187  (HYPOT:c @0 real_zerop@1)
7188  (abs @0))
7190 /* pow(1,x) -> 1.  */
7191 (simplify
7192  (POW real_onep@0 @1)
7193  @0)
7195 (simplify
7196  /* copysign(x,x) -> x.  */
7197  (COPYSIGN_ALL @0 @0)
7198  @0)
7200 (simplify
7201  /* copysign(x,-x) -> -x.  */
7202  (COPYSIGN_ALL @0 (negate@1 @0))
7203  @1)
7205 (simplify
7206  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7207  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7208  (abs @0))
7210 (for scale (LDEXP SCALBN SCALBLN)
7211  /* ldexp(0, x) -> 0.  */
7212  (simplify
7213   (scale real_zerop@0 @1)
7214   @0)
7215  /* ldexp(x, 0) -> x.  */
7216  (simplify
7217   (scale @0 integer_zerop@1)
7218   @0)
7219  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7220  (simplify
7221   (scale REAL_CST@0 @1)
7222   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7223    @0)))
7225 /* Canonicalization of sequences of math builtins.  These rules represent
7226    IL simplifications but are not necessarily optimizations.
7228    The sincos pass is responsible for picking "optimal" implementations
7229    of math builtins, which may be more complicated and can sometimes go
7230    the other way, e.g. converting pow into a sequence of sqrts.
7231    We only want to do these canonicalizations before the pass has run.  */
7233 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7234  /* Simplify tan(x) * cos(x) -> sin(x). */
7235  (simplify
7236   (mult:c (TAN:s @0) (COS:s @0))
7237    (SIN @0))
7239  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7240  (simplify
7241   (mult:c @0 (POW:s @0 REAL_CST@1))
7242   (if (!TREE_OVERFLOW (@1))
7243    (POW @0 (plus @1 { build_one_cst (type); }))))
7245  /* Simplify sin(x) / cos(x) -> tan(x). */
7246  (simplify
7247   (rdiv (SIN:s @0) (COS:s @0))
7248    (TAN @0))
7250  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7251  (simplify
7252   (rdiv (SINH:s @0) (COSH:s @0))
7253    (TANH @0))
7255  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7256  (simplify
7257    (rdiv (TANH:s @0) (SINH:s @0))
7258    (rdiv {build_one_cst (type);} (COSH @0)))
7260  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7261  (simplify
7262   (rdiv (COS:s @0) (SIN:s @0))
7263    (rdiv { build_one_cst (type); } (TAN @0)))
7265  /* Simplify sin(x) / tan(x) -> cos(x). */
7266  (simplify
7267   (rdiv (SIN:s @0) (TAN:s @0))
7268   (if (! HONOR_NANS (@0)
7269        && ! HONOR_INFINITIES (@0))
7270    (COS @0)))
7272  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7273  (simplify
7274   (rdiv (TAN:s @0) (SIN:s @0))
7275   (if (! HONOR_NANS (@0)
7276        && ! HONOR_INFINITIES (@0))
7277    (rdiv { build_one_cst (type); } (COS @0))))
7279  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7280  (simplify
7281   (mult (POW:s @0 @1) (POW:s @0 @2))
7282    (POW @0 (plus @1 @2)))
7284  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7285  (simplify
7286   (mult (POW:s @0 @1) (POW:s @2 @1))
7287    (POW (mult @0 @2) @1))
7289  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7290  (simplify
7291   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7292    (POWI (mult @0 @2) @1))
7294  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7295  (simplify
7296   (rdiv (POW:s @0 REAL_CST@1) @0)
7297   (if (!TREE_OVERFLOW (@1))
7298    (POW @0 (minus @1 { build_one_cst (type); }))))
7300  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7301  (simplify
7302   (rdiv @0 (POW:s @1 @2))
7303    (mult @0 (POW @1 (negate @2))))
7305  (for sqrts (SQRT)
7306       cbrts (CBRT)
7307       pows (POW)
7308   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7309   (simplify
7310    (sqrts (sqrts @0))
7311    (pows @0 { build_real (type, dconst_quarter ()); }))
7312   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7313   (simplify
7314    (sqrts (cbrts @0))
7315    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7316   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7317   (simplify
7318    (cbrts (sqrts @0))
7319    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7320   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7321   (simplify
7322    (cbrts (cbrts tree_expr_nonnegative_p@0))
7323    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7324   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7325   (simplify
7326    (sqrts (pows @0 @1))
7327    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7328   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7329   (simplify
7330    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7331    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7332   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7333   (simplify
7334    (pows (sqrts @0) @1)
7335    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7336   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7337   (simplify
7338    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7339    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7340   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7341   (simplify
7342    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7343    (pows @0 (mult @1 @2))))
7345  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7346  (simplify
7347   (CABS (complex @0 @0))
7348   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7350  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7351  (simplify
7352   (HYPOT @0 @0)
7353   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7355  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7356  (for cexps (CEXP)
7357       exps (EXP)
7358       cexpis (CEXPI)
7359   (simplify
7360    (cexps compositional_complex@0)
7361    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7362     (complex
7363      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7364      (mult @1 (imagpart @2)))))))
7366 (if (canonicalize_math_p ())
7367  /* floor(x) -> trunc(x) if x is nonnegative.  */
7368  (for floors (FLOOR_ALL)
7369       truncs (TRUNC_ALL)
7370   (simplify
7371    (floors tree_expr_nonnegative_p@0)
7372    (truncs @0))))
7374 (match double_value_p
7375  @0
7376  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7377 (for froms (BUILT_IN_TRUNCL
7378             BUILT_IN_FLOORL
7379             BUILT_IN_CEILL
7380             BUILT_IN_ROUNDL
7381             BUILT_IN_NEARBYINTL
7382             BUILT_IN_RINTL)
7383      tos (BUILT_IN_TRUNC
7384           BUILT_IN_FLOOR
7385           BUILT_IN_CEIL
7386           BUILT_IN_ROUND
7387           BUILT_IN_NEARBYINT
7388           BUILT_IN_RINT)
7389  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7390  (if (optimize && canonicalize_math_p ())
7391   (simplify
7392    (froms (convert double_value_p@0))
7393    (convert (tos @0)))))
7395 (match float_value_p
7396  @0
7397  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7398 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7399             BUILT_IN_FLOORL BUILT_IN_FLOOR
7400             BUILT_IN_CEILL BUILT_IN_CEIL
7401             BUILT_IN_ROUNDL BUILT_IN_ROUND
7402             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7403             BUILT_IN_RINTL BUILT_IN_RINT)
7404      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7405           BUILT_IN_FLOORF BUILT_IN_FLOORF
7406           BUILT_IN_CEILF BUILT_IN_CEILF
7407           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7408           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7409           BUILT_IN_RINTF BUILT_IN_RINTF)
7410  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7411     if x is a float.  */
7412  (if (optimize && canonicalize_math_p ()
7413       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7414   (simplify
7415    (froms (convert float_value_p@0))
7416    (convert (tos @0)))))
7418 #if GIMPLE
7419 (match float16_value_p
7420  @0
7421  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7422 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7423             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7424             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7425             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7426             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7427             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7428             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7429             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7430      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7431           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7432           IFN_CEIL IFN_CEIL IFN_CEIL
7433           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7434           IFN_ROUND IFN_ROUND IFN_ROUND
7435           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7436           IFN_RINT IFN_RINT IFN_RINT
7437           IFN_SQRT IFN_SQRT IFN_SQRT)
7438  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7439     if x is a _Float16.  */
7440  (simplify
7441    (convert (froms (convert float16_value_p@0)))
7442      (if (optimize
7443           && types_match (type, TREE_TYPE (@0))
7444           && direct_internal_fn_supported_p (as_internal_fn (tos),
7445                                              type, OPTIMIZE_FOR_BOTH))
7446        (tos @0))))
7448 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7449    x,y is float value, similar for _Float16/double.  */
7450 (for copysigns (COPYSIGN_ALL)
7451  (simplify
7452   (convert (copysigns (convert@2 @0) (convert @1)))
7453    (if (optimize
7454        && !HONOR_SNANS (@2)
7455        && types_match (type, TREE_TYPE (@0))
7456        && types_match (type, TREE_TYPE (@1))
7457        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7458        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7459                                           type, OPTIMIZE_FOR_BOTH))
7460     (IFN_COPYSIGN @0 @1))))
7462 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7463      tos (IFN_FMA IFN_FMA IFN_FMA)
7464  (simplify
7465   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7466    (if (flag_unsafe_math_optimizations
7467        && optimize
7468        && FLOAT_TYPE_P (type)
7469        && FLOAT_TYPE_P (TREE_TYPE (@3))
7470        && types_match (type, TREE_TYPE (@0))
7471        && types_match (type, TREE_TYPE (@1))
7472        && types_match (type, TREE_TYPE (@2))
7473        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7474        && direct_internal_fn_supported_p (as_internal_fn (tos),
7475                                           type, OPTIMIZE_FOR_BOTH))
7476     (tos @0 @1 @2))))
7478 (for maxmin (max min)
7479  (simplify
7480   (convert (maxmin (convert@2 @0) (convert @1)))
7481    (if (optimize
7482        && FLOAT_TYPE_P (type)
7483        && FLOAT_TYPE_P (TREE_TYPE (@2))
7484        && types_match (type, TREE_TYPE (@0))
7485        && types_match (type, TREE_TYPE (@1))
7486        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7487     (maxmin @0 @1))))
7488 #endif
7490 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7491      tos (XFLOOR XCEIL XROUND XRINT)
7492  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7493  (if (optimize && canonicalize_math_p ())
7494   (simplify
7495    (froms (convert double_value_p@0))
7496    (tos @0))))
7498 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7499             XFLOOR XCEIL XROUND XRINT)
7500      tos (XFLOORF XCEILF XROUNDF XRINTF)
7501  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7502     if x is a float.  */
7503  (if (optimize && canonicalize_math_p ())
7504   (simplify
7505    (froms (convert float_value_p@0))
7506    (tos @0))))
7508 (if (canonicalize_math_p ())
7509  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7510  (for floors (IFLOOR LFLOOR LLFLOOR)
7511   (simplify
7512    (floors tree_expr_nonnegative_p@0)
7513    (fix_trunc @0))))
7515 (if (canonicalize_math_p ())
7516  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7517  (for fns (IFLOOR LFLOOR LLFLOOR
7518            ICEIL LCEIL LLCEIL
7519            IROUND LROUND LLROUND)
7520   (simplify
7521    (fns integer_valued_real_p@0)
7522    (fix_trunc @0)))
7523  (if (!flag_errno_math)
7524   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7525   (for rints (IRINT LRINT LLRINT)
7526    (simplify
7527     (rints integer_valued_real_p@0)
7528     (fix_trunc @0)))))
7530 (if (canonicalize_math_p ())
7531  (for ifn (IFLOOR ICEIL IROUND IRINT)
7532       lfn (LFLOOR LCEIL LROUND LRINT)
7533       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7534   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7535      sizeof (int) == sizeof (long).  */
7536   (if (TYPE_PRECISION (integer_type_node)
7537        == TYPE_PRECISION (long_integer_type_node))
7538    (simplify
7539     (ifn @0)
7540     (lfn:long_integer_type_node @0)))
7541   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7542      sizeof (long long) == sizeof (long).  */
7543   (if (TYPE_PRECISION (long_long_integer_type_node)
7544        == TYPE_PRECISION (long_integer_type_node))
7545    (simplify
7546     (llfn @0)
7547     (lfn:long_integer_type_node @0)))))
7549 /* cproj(x) -> x if we're ignoring infinities.  */
7550 (simplify
7551  (CPROJ @0)
7552  (if (!HONOR_INFINITIES (type))
7553    @0))
7555 /* If the real part is inf and the imag part is known to be
7556    nonnegative, return (inf + 0i).  */
7557 (simplify
7558  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7559  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7560   { build_complex_inf (type, false); }))
7562 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7563 (simplify
7564  (CPROJ (complex @0 REAL_CST@1))
7565  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7566   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7568 (for pows (POW)
7569      sqrts (SQRT)
7570      cbrts (CBRT)
7571  (simplify
7572   (pows @0 REAL_CST@1)
7573   (with {
7574     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7575     REAL_VALUE_TYPE tmp;
7576    }
7577    (switch
7578     /* pow(x,0) -> 1.  */
7579     (if (real_equal (value, &dconst0))
7580      { build_real (type, dconst1); })
7581     /* pow(x,1) -> x.  */
7582     (if (real_equal (value, &dconst1))
7583      @0)
7584     /* pow(x,-1) -> 1/x.  */
7585     (if (real_equal (value, &dconstm1))
7586      (rdiv { build_real (type, dconst1); } @0))
7587     /* pow(x,0.5) -> sqrt(x).  */
7588     (if (flag_unsafe_math_optimizations
7589          && canonicalize_math_p ()
7590          && real_equal (value, &dconsthalf))
7591      (sqrts @0))
7592     /* pow(x,1/3) -> cbrt(x).  */
7593     (if (flag_unsafe_math_optimizations
7594          && canonicalize_math_p ()
7595          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7596              real_equal (value, &tmp)))
7597      (cbrts @0))))))
7599 /* powi(1,x) -> 1.  */
7600 (simplify
7601  (POWI real_onep@0 @1)
7602  @0)
7604 (simplify
7605  (POWI @0 INTEGER_CST@1)
7606  (switch
7607   /* powi(x,0) -> 1.  */
7608   (if (wi::to_wide (@1) == 0)
7609    { build_real (type, dconst1); })
7610   /* powi(x,1) -> x.  */
7611   (if (wi::to_wide (@1) == 1)
7612    @0)
7613   /* powi(x,-1) -> 1/x.  */
7614   (if (wi::to_wide (@1) == -1)
7615    (rdiv { build_real (type, dconst1); } @0))))
7617 /* Narrowing of arithmetic and logical operations.
7619    These are conceptually similar to the transformations performed for
7620    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7621    term we want to move all that code out of the front-ends into here.  */
7623 /* Convert (outertype)((innertype0)a+(innertype1)b)
7624    into ((newtype)a+(newtype)b) where newtype
7625    is the widest mode from all of these.  */
7626 (for op (plus minus mult rdiv)
7627  (simplify
7628    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7629    /* If we have a narrowing conversion of an arithmetic operation where
7630       both operands are widening conversions from the same type as the outer
7631       narrowing conversion.  Then convert the innermost operands to a
7632       suitable unsigned type (to avoid introducing undefined behavior),
7633       perform the operation and convert the result to the desired type.  */
7634    (if (INTEGRAL_TYPE_P (type)
7635         && op != MULT_EXPR
7636         && op != RDIV_EXPR
7637         /* We check for type compatibility between @0 and @1 below,
7638            so there's no need to check that @2/@4 are integral types.  */
7639         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7640         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7641         /* The precision of the type of each operand must match the
7642            precision of the mode of each operand, similarly for the
7643            result.  */
7644         && type_has_mode_precision_p (TREE_TYPE (@1))
7645         && type_has_mode_precision_p (TREE_TYPE (@2))
7646         && type_has_mode_precision_p (type)
7647         /* The inner conversion must be a widening conversion.  */
7648         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7649         && types_match (@1, type)
7650         && (types_match (@1, @2)
7651             /* Or the second operand is const integer or converted const
7652                integer from valueize.  */
7653             || poly_int_tree_p (@4)))
7654      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7655        (op @1 (convert @2))
7656        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7657         (convert (op (convert:utype @1)
7658                      (convert:utype @2)))))
7659      (if (FLOAT_TYPE_P (type)
7660           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7661                == DECIMAL_FLOAT_TYPE_P (type))
7662       (with { tree arg0 = strip_float_extensions (@1);
7663               tree arg1 = strip_float_extensions (@2);
7664               tree itype = TREE_TYPE (@0);
7665               tree ty1 = TREE_TYPE (arg0);
7666               tree ty2 = TREE_TYPE (arg1);
7667               enum tree_code code = TREE_CODE (itype); }
7668         (if (FLOAT_TYPE_P (ty1)
7669              && FLOAT_TYPE_P (ty2))
7670          (with { tree newtype = type;
7671                  if (TYPE_MODE (ty1) == SDmode
7672                      || TYPE_MODE (ty2) == SDmode
7673                      || TYPE_MODE (type) == SDmode)
7674                    newtype = dfloat32_type_node;
7675                  if (TYPE_MODE (ty1) == DDmode
7676                      || TYPE_MODE (ty2) == DDmode
7677                      || TYPE_MODE (type) == DDmode)
7678                    newtype = dfloat64_type_node;
7679                  if (TYPE_MODE (ty1) == TDmode
7680                      || TYPE_MODE (ty2) == TDmode
7681                      || TYPE_MODE (type) == TDmode)
7682                    newtype = dfloat128_type_node; }
7683           (if ((newtype == dfloat32_type_node
7684                 || newtype == dfloat64_type_node
7685                 || newtype == dfloat128_type_node)
7686               && newtype == type
7687               && types_match (newtype, type))
7688             (op (convert:newtype @1) (convert:newtype @2))
7689             (with { if (element_precision (ty1) > element_precision (newtype))
7690                       newtype = ty1;
7691                     if (element_precision (ty2) > element_precision (newtype))
7692                       newtype = ty2; }
7693                /* Sometimes this transformation is safe (cannot
7694                   change results through affecting double rounding
7695                   cases) and sometimes it is not.  If NEWTYPE is
7696                   wider than TYPE, e.g. (float)((long double)double
7697                   + (long double)double) converted to
7698                   (float)(double + double), the transformation is
7699                   unsafe regardless of the details of the types
7700                   involved; double rounding can arise if the result
7701                   of NEWTYPE arithmetic is a NEWTYPE value half way
7702                   between two representable TYPE values but the
7703                   exact value is sufficiently different (in the
7704                   right direction) for this difference to be
7705                   visible in ITYPE arithmetic.  If NEWTYPE is the
7706                   same as TYPE, however, the transformation may be
7707                   safe depending on the types involved: it is safe
7708                   if the ITYPE has strictly more than twice as many
7709                   mantissa bits as TYPE, can represent infinities
7710                   and NaNs if the TYPE can, and has sufficient
7711                   exponent range for the product or ratio of two
7712                   values representable in the TYPE to be within the
7713                   range of normal values of ITYPE.  */
7714               (if (element_precision (newtype) < element_precision (itype)
7715                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7716                        || target_supports_op_p (newtype, op, optab_default))
7717                    && (flag_unsafe_math_optimizations
7718                        || (element_precision (newtype) == element_precision (type)
7719                            && real_can_shorten_arithmetic (element_mode (itype),
7720                                                            element_mode (type))
7721                            && !excess_precision_type (newtype)))
7722                    && !types_match (itype, newtype))
7723                  (convert:type (op (convert:newtype @1)
7724                                    (convert:newtype @2)))
7725          )))) )
7726    ))
7729 /* This is another case of narrowing, specifically when there's an outer
7730    BIT_AND_EXPR which masks off bits outside the type of the innermost
7731    operands.   Like the previous case we have to convert the operands
7732    to unsigned types to avoid introducing undefined behavior for the
7733    arithmetic operation.  */
7734 (for op (minus plus)
7735  (simplify
7736   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7737   (if (INTEGRAL_TYPE_P (type)
7738        /* We check for type compatibility between @0 and @1 below,
7739           so there's no need to check that @1/@3 are integral types.  */
7740        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7741        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7742        /* The precision of the type of each operand must match the
7743           precision of the mode of each operand, similarly for the
7744           result.  */
7745        && type_has_mode_precision_p (TREE_TYPE (@0))
7746        && type_has_mode_precision_p (TREE_TYPE (@1))
7747        && type_has_mode_precision_p (type)
7748        /* The inner conversion must be a widening conversion.  */
7749        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7750        && types_match (@0, @1)
7751        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7752            <= TYPE_PRECISION (TREE_TYPE (@0)))
7753        && (wi::to_wide (@4)
7754            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7755                        true, TYPE_PRECISION (type))) == 0)
7756    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7757     (with { tree ntype = TREE_TYPE (@0); }
7758      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7759     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7760      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7761                (convert:utype @4))))))))
7763 /* Transform (@0 < @1 and @0 < @2) to use min,
7764    (@0 > @1 and @0 > @2) to use max */
7765 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7766      op    (lt      le      gt      ge      lt      le      gt      ge     )
7767      ext   (min     min     max     max     max     max     min     min    )
7768  (simplify
7769   (logic (op:cs @0 @1) (op:cs @0 @2))
7770   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7771        && TREE_CODE (@0) != INTEGER_CST)
7772    (op @0 (ext @1 @2)))))
7774 /* Max<bool0, bool1> -> bool0 | bool1
7775    Min<bool0, bool1> -> bool0 & bool1 */
7776 (for op    (max     min)
7777      logic (bit_ior bit_and)
7778  (simplify
7779   (op zero_one_valued_p@0 zero_one_valued_p@1)
7780   (logic @0 @1)))
7782 /* signbit(x) != 0 ? -x : x -> abs(x)
7783    signbit(x) == 0 ? -x : x -> -abs(x) */
7784 (for sign (SIGNBIT)
7785  (for neeq (ne eq)
7786   (simplify
7787    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
7788     (if (neeq == NE_EXPR)
7789      (abs @0)
7790      (negate (abs @0))))))
7792 (simplify
7793  /* signbit(x) -> 0 if x is nonnegative.  */
7794  (SIGNBIT tree_expr_nonnegative_p@0)
7795  { integer_zero_node; })
7797 (simplify
7798  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
7799  (SIGNBIT @0)
7800  (if (!HONOR_SIGNED_ZEROS (@0))
7801   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7803 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
7804 (for cmp (eq ne)
7805  (for op (plus minus)
7806       rop (minus plus)
7807   (simplify
7808    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7809    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7810         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7811         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7812         && !TYPE_SATURATING (TREE_TYPE (@0)))
7813     (with { tree res = int_const_binop (rop, @2, @1); }
7814      (if (TREE_OVERFLOW (res)
7815           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7816       { constant_boolean_node (cmp == NE_EXPR, type); }
7817       (if (single_use (@3))
7818        (cmp @0 { TREE_OVERFLOW (res)
7819                  ? drop_tree_overflow (res) : res; }))))))))
7820 (for cmp (lt le gt ge)
7821  (for op (plus minus)
7822       rop (minus plus)
7823   (simplify
7824    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7825    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7826         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7827     (with { tree res = int_const_binop (rop, @2, @1); }
7828      (if (TREE_OVERFLOW (res))
7829       {
7830         fold_overflow_warning (("assuming signed overflow does not occur "
7831                                 "when simplifying conditional to constant"),
7832                                WARN_STRICT_OVERFLOW_CONDITIONAL);
7833         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7834         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
7835         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7836                                   TYPE_SIGN (TREE_TYPE (@1)))
7837                         != (op == MINUS_EXPR);
7838         constant_boolean_node (less == ovf_high, type);
7839       }
7840       (if (single_use (@3))
7841        (with
7842         {
7843           fold_overflow_warning (("assuming signed overflow does not occur "
7844                                   "when changing X +- C1 cmp C2 to "
7845                                   "X cmp C2 -+ C1"),
7846                                  WARN_STRICT_OVERFLOW_COMPARISON);
7847         }
7848         (cmp @0 { res; })))))))))
7850 /* Canonicalizations of BIT_FIELD_REFs.  */
7852 (simplify
7853  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7854  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7856 (simplify
7857  (BIT_FIELD_REF (view_convert @0) @1 @2)
7858  (BIT_FIELD_REF @0 @1 @2))
7860 (simplify
7861  (BIT_FIELD_REF @0 @1 integer_zerop)
7862  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7863   (view_convert @0)))
7865 (simplify
7866  (BIT_FIELD_REF @0 @1 @2)
7867  (switch
7868   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7869        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7870    (switch
7871     (if (integer_zerop (@2))
7872      (view_convert (realpart @0)))
7873     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7874      (view_convert (imagpart @0)))))
7875   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7876        && INTEGRAL_TYPE_P (type)
7877        /* On GIMPLE this should only apply to register arguments.  */
7878        && (! GIMPLE || is_gimple_reg (@0))
7879        /* A bit-field-ref that referenced the full argument can be stripped.  */
7880        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7881             && integer_zerop (@2))
7882            /* Low-parts can be reduced to integral conversions.
7883               ???  The following doesn't work for PDP endian.  */
7884            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7885                /* But only do this after vectorization.  */
7886                && canonicalize_math_after_vectorization_p ()
7887                /* Don't even think about BITS_BIG_ENDIAN.  */
7888                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7889                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7890                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7891                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
7892                                             - TYPE_PRECISION (type))
7893                                          : 0)) == 0)))
7894    (convert @0))))
7896 /* Simplify vector extracts.  */
7898 (simplify
7899  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7900  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7901       && tree_fits_uhwi_p (TYPE_SIZE (type))
7902       && ((tree_to_uhwi (TYPE_SIZE (type))
7903            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7904           || (VECTOR_TYPE_P (type)
7905               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7906                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7907   (with
7908    {
7909      tree ctor = (TREE_CODE (@0) == SSA_NAME
7910                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7911      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7912      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7913      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7914      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7915    }
7916    (if (n != 0
7917         && (idx % width) == 0
7918         && (n % width) == 0
7919         && known_le ((idx + n) / width,
7920                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7921     (with
7922      {
7923        idx = idx / width;
7924        n = n / width;
7925        /* Constructor elements can be subvectors.  */
7926        poly_uint64 k = 1;
7927        if (CONSTRUCTOR_NELTS (ctor) != 0)
7928          {
7929            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7930            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7931              k = TYPE_VECTOR_SUBPARTS (cons_elem);
7932          }
7933        unsigned HOST_WIDE_INT elt, count, const_k;
7934      }
7935      (switch
7936       /* We keep an exact subset of the constructor elements.  */
7937       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7938        (if (CONSTRUCTOR_NELTS (ctor) == 0)
7939         { build_zero_cst (type); }
7940         (if (count == 1)
7941          (if (elt < CONSTRUCTOR_NELTS (ctor))
7942           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7943           { build_zero_cst (type); })
7944          /* We don't want to emit new CTORs unless the old one goes away.
7945             ???  Eventually allow this if the CTOR ends up constant or
7946             uniform.  */
7947          (if (single_use (@0))
7948           (with
7949             {
7950               vec<constructor_elt, va_gc> *vals;
7951               vec_alloc (vals, count);
7952               bool constant_p = true;
7953               tree res;
7954               for (unsigned i = 0;
7955                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7956                 {
7957                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7958                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7959                   if (!CONSTANT_CLASS_P (e))
7960                     constant_p = false;
7961                 }
7962               tree evtype = (types_match (TREE_TYPE (type),
7963                                           TREE_TYPE (TREE_TYPE (ctor)))
7964                              ? type
7965                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7966                                                   count * k));
7967               /* We used to build a CTOR in the non-constant case here
7968                  but that's not a GIMPLE value.  We'd have to expose this
7969                  operation somehow so the code generation can properly
7970                  split it out to a separate stmt.  */
7971               res = (constant_p ? build_vector_from_ctor (evtype, vals)
7972                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
7973             }
7974             (if (res)
7975              (view_convert { res; })))))))
7976       /* The bitfield references a single constructor element.  */
7977       (if (k.is_constant (&const_k)
7978            && idx + n <= (idx / const_k + 1) * const_k)
7979        (switch
7980         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7981          { build_zero_cst (type); })
7982         (if (n == const_k)
7983          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7984         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7985                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
7987 /* Simplify a bit extraction from a bit insertion for the cases with
7988    the inserted element fully covering the extraction or the insertion
7989    not touching the extraction.  */
7990 (simplify
7991  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7992  (with
7993   {
7994     unsigned HOST_WIDE_INT isize;
7995     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7996       isize = TYPE_PRECISION (TREE_TYPE (@1));
7997     else
7998       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7999   }
8000   (switch
8001    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8002          || type_has_mode_precision_p (TREE_TYPE (@1)))
8003         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8004         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8005                       wi::to_wide (@ipos) + isize))
8006     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8007                                                  wi::to_wide (@rpos)
8008                                                  - wi::to_wide (@ipos)); }))
8009    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8010         && compare_tree_int (@rsize, isize) == 0)
8011     (convert @1))
8012    (if (wi::geu_p (wi::to_wide (@ipos),
8013                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8014         || wi::geu_p (wi::to_wide (@rpos),
8015                       wi::to_wide (@ipos) + isize))
8016     (BIT_FIELD_REF @0 @rsize @rpos)))))
8018 /* Simplify vector inserts of other vector extracts to a permute.  */
8019 (simplify
8020  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8021  (if (VECTOR_TYPE_P (type)
8022       && types_match (@0, @1)
8023       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8024       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8025   (with
8026    {
8027      unsigned HOST_WIDE_INT elsz
8028        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8029      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8030      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8031      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8032      vec_perm_builder builder;
8033      builder.new_vector (nunits, nunits, 1);
8034      for (unsigned i = 0; i < nunits; ++i)
8035        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8036      vec_perm_indices sel (builder, 2, nunits);
8037    }
8038    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8039         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8040     (vec_perm @0 @1 { vec_perm_indices_to_tree
8041                         (build_vector_type (ssizetype, nunits), sel); })))))
8043 (if (canonicalize_math_after_vectorization_p ())
8044  (for fmas (FMA)
8045   (simplify
8046    (fmas:c (negate @0) @1 @2)
8047    (IFN_FNMA @0 @1 @2))
8048   (simplify
8049    (fmas @0 @1 (negate @2))
8050    (IFN_FMS @0 @1 @2))
8051   (simplify
8052    (fmas:c (negate @0) @1 (negate @2))
8053    (IFN_FNMS @0 @1 @2))
8054   (simplify
8055    (negate (fmas@3 @0 @1 @2))
8056    (if (single_use (@3))
8057     (IFN_FNMS @0 @1 @2))))
8059  (simplify
8060   (IFN_FMS:c (negate @0) @1 @2)
8061   (IFN_FNMS @0 @1 @2))
8062  (simplify
8063   (IFN_FMS @0 @1 (negate @2))
8064   (IFN_FMA @0 @1 @2))
8065  (simplify
8066   (IFN_FMS:c (negate @0) @1 (negate @2))
8067   (IFN_FNMA @0 @1 @2))
8068  (simplify
8069   (negate (IFN_FMS@3 @0 @1 @2))
8070    (if (single_use (@3))
8071     (IFN_FNMA @0 @1 @2)))
8073  (simplify
8074   (IFN_FNMA:c (negate @0) @1 @2)
8075   (IFN_FMA @0 @1 @2))
8076  (simplify
8077   (IFN_FNMA @0 @1 (negate @2))
8078   (IFN_FNMS @0 @1 @2))
8079  (simplify
8080   (IFN_FNMA:c (negate @0) @1 (negate @2))
8081   (IFN_FMS @0 @1 @2))
8082  (simplify
8083   (negate (IFN_FNMA@3 @0 @1 @2))
8084   (if (single_use (@3))
8085    (IFN_FMS @0 @1 @2)))
8087  (simplify
8088   (IFN_FNMS:c (negate @0) @1 @2)
8089   (IFN_FMS @0 @1 @2))
8090  (simplify
8091   (IFN_FNMS @0 @1 (negate @2))
8092   (IFN_FNMA @0 @1 @2))
8093  (simplify
8094   (IFN_FNMS:c (negate @0) @1 (negate @2))
8095   (IFN_FMA @0 @1 @2))
8096  (simplify
8097   (negate (IFN_FNMS@3 @0 @1 @2))
8098   (if (single_use (@3))
8099    (IFN_FMA @0 @1 @2))))
8101 /* CLZ simplifications.  */
8102 (for clz (CLZ)
8103  (for op (eq ne)
8104       cmp (lt ge)
8105   (simplify
8106    (op (clz:s@2 @0) INTEGER_CST@1)
8107    (if (integer_zerop (@1) && single_use (@2))
8108     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8109     (with { tree type0 = TREE_TYPE (@0);
8110             tree stype = signed_type_for (type0);
8111             HOST_WIDE_INT val = 0;
8112             /* Punt on hypothetical weird targets.  */
8113             if (clz == CFN_CLZ
8114                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8115                                               val) == 2
8116                 && val == 0)
8117               stype = NULL_TREE;
8118           }
8119      (if (stype)
8120       (cmp (convert:stype @0) { build_zero_cst (stype); })))
8121     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8122     (with { bool ok = true;
8123             HOST_WIDE_INT val = 0;
8124             tree type0 = TREE_TYPE (@0);
8125             /* Punt on hypothetical weird targets.  */
8126             if (clz == CFN_CLZ
8127                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8128                                               val) == 2
8129                 && val == TYPE_PRECISION (type0) - 1)
8130               ok = false;
8131           }
8132      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8133       (op @0 { build_one_cst (type0); })))))))
8135 /* CTZ simplifications.  */
8136 (for ctz (CTZ)
8137  (for op (ge gt le lt)
8138       cmp (eq eq ne ne)
8139   (simplify
8140    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8141    (op (ctz:s @0) INTEGER_CST@1)
8142     (with { bool ok = true;
8143             HOST_WIDE_INT val = 0;
8144             if (!tree_fits_shwi_p (@1))
8145               ok = false;
8146             else
8147               {
8148                 val = tree_to_shwi (@1);
8149                 /* Canonicalize to >= or <.  */
8150                 if (op == GT_EXPR || op == LE_EXPR)
8151                   {
8152                     if (val == HOST_WIDE_INT_MAX)
8153                       ok = false;
8154                     else
8155                       val++;
8156                   }
8157               }
8158             bool zero_res = false;
8159             HOST_WIDE_INT zero_val = 0;
8160             tree type0 = TREE_TYPE (@0);
8161             int prec = TYPE_PRECISION (type0);
8162             if (ctz == CFN_CTZ
8163                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8164                                               zero_val) == 2)
8165               zero_res = true;
8166           }
8167      (if (val <= 0)
8168       (if (ok && (!zero_res || zero_val >= val))
8169        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8170       (if (val >= prec)
8171        (if (ok && (!zero_res || zero_val < val))
8172         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8173        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8174         (cmp (bit_and @0 { wide_int_to_tree (type0,
8175                                              wi::mask (val, false, prec)); })
8176              { build_zero_cst (type0); })))))))
8177  (for op (eq ne)
8178   (simplify
8179    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8180    (op (ctz:s @0) INTEGER_CST@1)
8181     (with { bool zero_res = false;
8182             HOST_WIDE_INT zero_val = 0;
8183             tree type0 = TREE_TYPE (@0);
8184             int prec = TYPE_PRECISION (type0);
8185             if (ctz == CFN_CTZ
8186                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8187                                               zero_val) == 2)
8188               zero_res = true;
8189           }
8190      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8191       (if (!zero_res || zero_val != wi::to_widest (@1))
8192        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8193       (if (!zero_res || zero_val < 0 || zero_val >= prec)
8194        (op (bit_and @0 { wide_int_to_tree (type0,
8195                                            wi::mask (tree_to_uhwi (@1) + 1,
8196                                                      false, prec)); })
8197            { wide_int_to_tree (type0,
8198                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8199                                                  false, prec)); })))))))
8201 /* POPCOUNT simplifications.  */
8202 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8203 (simplify
8204   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8205   (if (INTEGRAL_TYPE_P (type)
8206        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8207     (POPCOUNT (bit_ior @0 @1))))
8209 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8210 (for popcount (POPCOUNT)
8211   (for cmp (le eq ne gt)
8212        rep (eq eq ne ne)
8213     (simplify
8214       (cmp (popcount @0) integer_zerop)
8215       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8217 /* popcount(bswap(x)) is popcount(x).  */
8218 (for popcount (POPCOUNT)
8219   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8220               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8221     (simplify
8222       (popcount (convert?@0 (bswap:s@1 @2)))
8223       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8224            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8225         (with { tree type0 = TREE_TYPE (@0);
8226                 tree type1 = TREE_TYPE (@1);
8227                 unsigned int prec0 = TYPE_PRECISION (type0);
8228                 unsigned int prec1 = TYPE_PRECISION (type1); }
8229           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8230             (popcount (convert:type0 (convert:type1 @2)))))))))
8232 /* popcount(rotate(X Y)) is popcount(X).  */
8233 (for popcount (POPCOUNT)
8234   (for rot (lrotate rrotate)
8235     (simplify
8236       (popcount (convert?@0 (rot:s@1 @2 @3)))
8237       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8238            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8239            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8240         (with { tree type0 = TREE_TYPE (@0);
8241                 tree type1 = TREE_TYPE (@1);
8242                 unsigned int prec0 = TYPE_PRECISION (type0);
8243                 unsigned int prec1 = TYPE_PRECISION (type1); }
8244           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8245             (popcount (convert:type0 @2))))))))
8247 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8248 (simplify
8249   (bit_and (POPCOUNT @0) integer_onep)
8250   (PARITY @0))
8252 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8253 (simplify
8254   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8255   (plus (POPCOUNT @0) (POPCOUNT @1)))
8257 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8258 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8259 (for popcount (POPCOUNT)
8260   (for log1 (bit_and bit_ior)
8261        log2 (bit_ior bit_and)
8262     (simplify
8263       (minus (plus:s (popcount:s @0) (popcount:s @1))
8264              (popcount:s (log1:cs @0 @1)))
8265       (popcount (log2 @0 @1)))
8266     (simplify
8267       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8268               (popcount:s @1))
8269       (popcount (log2 @0 @1)))))
8271 /* PARITY simplifications.  */
8272 /* parity(~X) is parity(X).  */
8273 (simplify
8274   (PARITY (bit_not @0))
8275   (PARITY @0))
8277 /* parity(bswap(x)) is parity(x).  */
8278 (for parity (PARITY)
8279   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8280               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8281     (simplify
8282       (parity (convert?@0 (bswap:s@1 @2)))
8283       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8284            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8285            && TYPE_PRECISION (TREE_TYPE (@0))
8286               >= TYPE_PRECISION (TREE_TYPE (@1)))
8287         (with { tree type0 = TREE_TYPE (@0);
8288                 tree type1 = TREE_TYPE (@1); }
8289           (parity (convert:type0 (convert:type1 @2))))))))
8291 /* parity(rotate(X Y)) is parity(X).  */
8292 (for parity (PARITY)
8293   (for rot (lrotate rrotate)
8294     (simplify
8295       (parity (convert?@0 (rot:s@1 @2 @3)))
8296       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8297            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8298            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8299            && TYPE_PRECISION (TREE_TYPE (@0))
8300               >= TYPE_PRECISION (TREE_TYPE (@1)))
8301         (with { tree type0 = TREE_TYPE (@0); }
8302           (parity (convert:type0 @2)))))))
8304 /* parity(X)^parity(Y) is parity(X^Y).  */
8305 (simplify
8306   (bit_xor (PARITY:s @0) (PARITY:s @1))
8307   (PARITY (bit_xor @0 @1)))
8309 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8310 (for func (POPCOUNT BSWAP FFS PARITY)
8311  (simplify
8312   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8313   @3))
8315 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8316    where CST is precision-1. */
8317 (for func (CLRSB)
8318  (simplify
8319   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8320   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8321    @4)))
8323 #if GIMPLE
8324 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8325 (for func (CLZ)
8326  (simplify
8327   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8328   (with { int val;
8329           internal_fn ifn = IFN_LAST;
8330           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8331               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8332                                             val) == 2)
8333             ifn = IFN_CLZ;
8334         }
8335    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8336     (IFN_CLZ @3)))))
8338 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8339 (for func (CTZ)
8340  (simplify
8341   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8342   (with { int val;
8343           internal_fn ifn = IFN_LAST;
8344           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8345               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8346                                             val) == 2)
8347             ifn = IFN_CTZ;
8348         }
8349    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8350     (IFN_CTZ @3)))))
8351 #endif
8353 /* Common POPCOUNT/PARITY simplifications.  */
8354 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8355 (for pfun (POPCOUNT PARITY)
8356   (simplify
8357     (pfun @0)
8358     (if (INTEGRAL_TYPE_P (type))
8359      (with { wide_int nz = tree_nonzero_bits (@0); }
8360        (switch
8361          (if (nz == 1)
8362            (convert @0))
8363          (if (wi::popcount (nz) == 1)
8364            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8365              (convert (rshift:utype (convert:utype @0)
8366                                     { build_int_cst (integer_type_node,
8367                                                      wi::ctz (nz)); })))))))))
8369 #if GIMPLE
8370 /* 64- and 32-bits branchless implementations of popcount are detected:
8372    int popcount64c (uint64_t x)
8373    {
8374      x -= (x >> 1) & 0x5555555555555555ULL;
8375      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8376      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8377      return (x * 0x0101010101010101ULL) >> 56;
8378    }
8380    int popcount32c (uint32_t x)
8381    {
8382      x -= (x >> 1) & 0x55555555;
8383      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8384      x = (x + (x >> 4)) & 0x0f0f0f0f;
8385      return (x * 0x01010101) >> 24;
8386    }  */
8387 (simplify
8388  (rshift
8389   (mult
8390    (bit_and
8391     (plus:c
8392      (rshift @8 INTEGER_CST@5)
8393       (plus:c@8
8394        (bit_and @6 INTEGER_CST@7)
8395         (bit_and
8396          (rshift
8397           (minus@6 @0
8398            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8399           INTEGER_CST@10)
8400          INTEGER_CST@9)))
8401     INTEGER_CST@3)
8402    INTEGER_CST@2)
8403   INTEGER_CST@1)
8404   /* Check constants and optab.  */
8405   (with { unsigned prec = TYPE_PRECISION (type);
8406           int shift = (64 - prec) & 63;
8407           unsigned HOST_WIDE_INT c1
8408             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8409           unsigned HOST_WIDE_INT c2
8410             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8411           unsigned HOST_WIDE_INT c3
8412             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8413           unsigned HOST_WIDE_INT c4
8414             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8415    }
8416    (if (prec >= 16
8417         && prec <= 64
8418         && pow2p_hwi (prec)
8419         && TYPE_UNSIGNED (type)
8420         && integer_onep (@4)
8421         && wi::to_widest (@10) == 2
8422         && wi::to_widest (@5) == 4
8423         && wi::to_widest (@1) == prec - 8
8424         && tree_to_uhwi (@2) == c1
8425         && tree_to_uhwi (@3) == c2
8426         && tree_to_uhwi (@9) == c3
8427         && tree_to_uhwi (@7) == c3
8428         && tree_to_uhwi (@11) == c4)
8429     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8430                                          OPTIMIZE_FOR_BOTH))
8431      (convert (IFN_POPCOUNT:type @0))
8432      /* Try to do popcount in two halves.  PREC must be at least
8433         five bits for this to work without extension before adding.  */
8434      (with {
8435        tree half_type = NULL_TREE;
8436        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8437        int half_prec = 8;
8438        if (m.exists ()
8439            && m.require () != TYPE_MODE (type))
8440          {
8441            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8442            half_type = build_nonstandard_integer_type (half_prec, 1);
8443          }
8444        gcc_assert (half_prec > 2);
8445       }
8446       (if (half_type != NULL_TREE
8447            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8448                                               OPTIMIZE_FOR_BOTH))
8449        (convert (plus
8450          (IFN_POPCOUNT:half_type (convert @0))
8451          (IFN_POPCOUNT:half_type (convert (rshift @0
8452             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8454 /* __builtin_ffs needs to deal on many targets with the possible zero
8455    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8456    should lead to better code.  */
8457 (simplify
8458  (FFS tree_expr_nonzero_p@0)
8459  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8460       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8461                                          OPTIMIZE_FOR_SPEED))
8462   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8463    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8464 #endif
8466 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8467           BUILT_IN_FFSIMAX)
8468  /* __builtin_ffs (X) == 0 -> X == 0.
8469     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8470  (for cmp (eq ne)
8471   (simplify
8472    (cmp (ffs@2 @0) INTEGER_CST@1)
8473     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8474      (switch
8475       (if (integer_zerop (@1))
8476        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8477       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8478        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8479       (if (single_use (@2))
8480        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8481                                             wi::mask (tree_to_uhwi (@1),
8482                                                       false, prec)); })
8483             { wide_int_to_tree (TREE_TYPE (@0),
8484                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8485                                                   false, prec)); }))))))
8487  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8488  (for cmp (gt le)
8489       cmp2 (ne eq)
8490       cmp3 (eq ne)
8491       bit_op (bit_and bit_ior)
8492   (simplify
8493    (cmp (ffs@2 @0) INTEGER_CST@1)
8494     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8495      (switch
8496       (if (integer_zerop (@1))
8497        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8498       (if (tree_int_cst_sgn (@1) < 0)
8499        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8500       (if (wi::to_widest (@1) >= prec)
8501        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8502       (if (wi::to_widest (@1) == prec - 1)
8503        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8504                                     wi::shifted_mask (prec - 1, 1,
8505                                                       false, prec)); }))
8506       (if (single_use (@2))
8507        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8508                (cmp3 (bit_and @0
8509                               { wide_int_to_tree (TREE_TYPE (@0),
8510                                                   wi::mask (tree_to_uhwi (@1),
8511                                                   false, prec)); })
8512                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8514 #if GIMPLE
8516 /* Simplify:
8517      a = op a1
8518      r = cond ? a : b
8519      --> r = .COND_FN (cond, a, b)
8520 and,
8521     a = op a1
8522     r = cond ? b : a
8523     --> r = .COND_FN (~cond, b, a).  */
8525 (for uncond_op (UNCOND_UNARY)
8526      cond_op (COND_UNARY)
8527  (simplify
8528   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8529    (with { tree op_type = TREE_TYPE (@3); }
8530     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8531         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8532      (cond_op @0 @1 @2))))
8533  (simplify
8534   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8535    (with { tree op_type = TREE_TYPE (@3); }
8536     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8537         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8538      (cond_op (bit_not @0) @2 @1)))))
8540 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
8541 (simplify
8542  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8543  (if (canonicalize_math_after_vectorization_p ()
8544       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8545       && is_truth_type_for (type, TREE_TYPE (@0)))
8546  (if (integer_all_onesp (@1) && integer_zerop (@2))
8547   (IFN_COND_NOT @0 @3 @3))
8548   (if (integer_all_onesp (@2) && integer_zerop (@1))
8549    (IFN_COND_NOT (bit_not @0) @3 @3))))
8551 /* Simplify:
8553      a = a1 op a2
8554      r = c ? a : b;
8556    to:
8558      r = c ? a1 op a2 : b;
8560    if the target can do it in one go.  This makes the operation conditional
8561    on c, so could drop potentially-trapping arithmetic, but that's a valid
8562    simplification if the result of the operation isn't needed.
8564    Avoid speculatively generating a stand-alone vector comparison
8565    on targets that might not support them.  Any target implementing
8566    conditional internal functions must support the same comparisons
8567    inside and outside a VEC_COND_EXPR.  */
8569 (for uncond_op (UNCOND_BINARY)
8570      cond_op (COND_BINARY)
8571  (simplify
8572   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8573   (with { tree op_type = TREE_TYPE (@4); }
8574    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8575         && is_truth_type_for (op_type, TREE_TYPE (@0))
8576         && single_use (@4))
8577     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8578  (simplify
8579   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8580   (with { tree op_type = TREE_TYPE (@4); }
8581    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8582         && is_truth_type_for (op_type, TREE_TYPE (@0))
8583         && single_use (@4))
8584     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8586 /* Same for ternary operations.  */
8587 (for uncond_op (UNCOND_TERNARY)
8588      cond_op (COND_TERNARY)
8589  (simplify
8590   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8591   (with { tree op_type = TREE_TYPE (@5); }
8592    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8593         && is_truth_type_for (op_type, TREE_TYPE (@0))
8594         && single_use (@5))
8595     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8596  (simplify
8597   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8598   (with { tree op_type = TREE_TYPE (@5); }
8599    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8600         && is_truth_type_for (op_type, TREE_TYPE (@0))
8601         && single_use (@5))
8602     (view_convert (cond_op (bit_not @0) @2 @3 @4
8603                   (view_convert:op_type @1)))))))
8604 #endif
8606 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8607    "else" value of an IFN_COND_*.  */
8608 (for cond_op (COND_BINARY)
8609  (simplify
8610   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8611   (with { tree op_type = TREE_TYPE (@3); }
8612    (if (element_precision (type) == element_precision (op_type))
8613     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8614  (simplify
8615   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8616   (with { tree op_type = TREE_TYPE (@5); }
8617    (if (inverse_conditions_p (@0, @2)
8618         && element_precision (type) == element_precision (op_type))
8619     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8621 /* Same for ternary operations.  */
8622 (for cond_op (COND_TERNARY)
8623  (simplify
8624   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8625   (with { tree op_type = TREE_TYPE (@4); }
8626    (if (element_precision (type) == element_precision (op_type))
8627     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8628  (simplify
8629   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8630   (with { tree op_type = TREE_TYPE (@6); }
8631    (if (inverse_conditions_p (@0, @2)
8632         && element_precision (type) == element_precision (op_type))
8633     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8635 /* Detect simplication for a conditional reduction where
8637    a = mask1 ? b : 0
8638    c = mask2 ? d + a : d
8640    is turned into
8642    c = mask1 && mask2 ? d + b : d.  */
8643 (simplify
8644   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8645    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8647 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8648    expressions like:
8650    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8651    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8653    If pointers are known not to wrap, B checks whether @1 bytes starting
8654    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8655    bytes.  A is more efficiently tested as:
8657    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8659    The equivalent expression for B is given by replacing @1 with @1 - 1:
8661    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8663    @0 and @2 can be swapped in both expressions without changing the result.
8665    The folds rely on sizetype's being unsigned (which is always true)
8666    and on its being the same width as the pointer (which we have to check).
8668    The fold replaces two pointer_plus expressions, two comparisons and
8669    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8670    the best case it's a saving of two operations.  The A fold retains one
8671    of the original pointer_pluses, so is a win even if both pointer_pluses
8672    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8673    used elsewhere, since all we end up doing is replacing a comparison with
8674    a pointer_plus.  We do still apply the fold under those circumstances
8675    though, in case applying it to other conditions eventually makes one of the
8676    pointer_pluses dead.  */
8677 (for ior (truth_orif truth_or bit_ior)
8678  (for cmp (le lt)
8679   (simplify
8680    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8681         (cmp:cs (pointer_plus@4 @2 @1) @0))
8682    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8683         && TYPE_OVERFLOW_WRAPS (sizetype)
8684         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8685     /* Calculate the rhs constant.  */
8686     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8687             offset_int rhs = off * 2; }
8688      /* Always fails for negative values.  */
8689      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8690       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8691          pick a canonical order.  This increases the chances of using the
8692          same pointer_plus in multiple checks.  */
8693       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8694               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8695        (if (cmp == LT_EXPR)
8696         (gt (convert:sizetype
8697              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8698                                      { swap_p ? @0 : @2; }))
8699             { rhs_tree; })
8700         (gt (convert:sizetype
8701              (pointer_diff:ssizetype
8702               (pointer_plus { swap_p ? @2 : @0; }
8703                             { wide_int_to_tree (sizetype, off); })
8704               { swap_p ? @0 : @2; }))
8705             { rhs_tree; })))))))))
8707 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8708    element of @1.  */
8709 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8710  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8711   (with { int i = single_nonzero_element (@1); }
8712    (if (i >= 0)
8713     (with { tree elt = vector_cst_elt (@1, i);
8714             tree elt_type = TREE_TYPE (elt);
8715             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8716             tree size = bitsize_int (elt_bits);
8717             tree pos = bitsize_int (elt_bits * i); }
8718      (view_convert
8719       (bit_and:elt_type
8720        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8721        { elt; })))))))
8723 /* Fold reduction of a single nonzero element constructor.  */
8724 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8725   (simplify (reduc (CONSTRUCTOR@0))
8726     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8727                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8728             tree elt = ctor_single_nonzero_element (ctor); }
8729       (if (elt
8730            && !HONOR_SNANS (type)
8731            && !HONOR_SIGNED_ZEROS (type))
8732         { elt; }))))
8734 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8735 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8736             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8737      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8738   (simplify (reduc (op @0 VECTOR_CST@1))
8739     (op (reduc:type @0) (reduc:type @1))))
8741 /* Simplify vector floating point operations of alternating sub/add pairs
8742    into using an fneg of a wider element type followed by a normal add.
8743    under IEEE 754 the fneg of the wider type will negate every even entry
8744    and when doing an add we get a sub of the even and add of every odd
8745    elements.  */
8746 (for plusminus (plus minus)
8747      minusplus (minus plus)
8748  (simplify
8749   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8750    (if (!VECTOR_INTEGER_TYPE_P (type)
8751         && !FLOAT_WORDS_BIG_ENDIAN
8752         /* plus is commutative, while minus is not, so :c can't be used.
8753            Do equality comparisons by hand and at the end pick the operands
8754            from the minus.  */
8755         && (operand_equal_p (@0, @2, 0)
8756             ? operand_equal_p (@1, @3, 0)
8757             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8758    (with
8759     {
8760       /* Build a vector of integers from the tree mask.  */
8761       vec_perm_builder builder;
8762     }
8763     (if (tree_to_vec_perm_builder (&builder, @4))
8764      (with
8765       {
8766         /* Create a vec_perm_indices for the integer vector.  */
8767         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8768         vec_perm_indices sel (builder, 2, nelts);
8769         machine_mode vec_mode = TYPE_MODE (type);
8770         machine_mode wide_mode;
8771         scalar_mode wide_elt_mode;
8772         poly_uint64 wide_nunits;
8773         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8774       }
8775       (if (VECTOR_MODE_P (vec_mode)
8776            && sel.series_p (0, 2, 0, 2)
8777            && sel.series_p (1, 2, nelts + 1, 2)
8778            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
8779            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
8780            && related_vector_mode (vec_mode, wide_elt_mode,
8781                                    wide_nunits).exists (&wide_mode))
8782        (with
8783         {
8784           tree stype
8785             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
8786                                               TYPE_UNSIGNED (type));
8787           tree ntype = build_vector_type_for_mode (stype, wide_mode);
8789           /* The format has to be a non-extended ieee format.  */
8790           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
8791           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
8792         }
8793         (if (TYPE_MODE (stype) != BLKmode
8794              && VECTOR_TYPE_P (ntype)
8795              && fmt_old != NULL
8796              && fmt_new != NULL)
8797          (with
8798           {
8799             /* If the target doesn't support v1xx vectors, try using
8800                scalar mode xx instead.  */
8801             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
8802                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
8803               ntype = stype;
8804           }
8805           (if (fmt_new->signbit_rw
8806                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
8807                && fmt_new->signbit_rw == fmt_new->signbit_ro
8808                && targetm.can_change_mode_class (TYPE_MODE (ntype),
8809                                                  TYPE_MODE (type), ALL_REGS)
8810                && ((optimize_vectors_before_lowering_p ()
8811                     && VECTOR_TYPE_P (ntype))
8812                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
8813            (if (plusminus == PLUS_EXPR)
8814             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
8815             (minus @0 (view_convert:type
8816                         (negate (view_convert:ntype @1))))))))))))))))
8818 (simplify
8819  (vec_perm @0 @1 VECTOR_CST@2)
8820  (with
8821   {
8822     tree op0 = @0, op1 = @1, op2 = @2;
8823     machine_mode result_mode = TYPE_MODE (type);
8824     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
8826     /* Build a vector of integers from the tree mask.  */
8827     vec_perm_builder builder;
8828   }
8829   (if (tree_to_vec_perm_builder (&builder, op2))
8830    (with
8831     {
8832       /* Create a vec_perm_indices for the integer vector.  */
8833       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8834       bool single_arg = (op0 == op1);
8835       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8836     }
8837     (if (sel.series_p (0, 1, 0, 1))
8838      { op0; }
8839      (if (sel.series_p (0, 1, nelts, 1))
8840       { op1; }
8841       (with
8842        {
8843          if (!single_arg)
8844            {
8845              if (sel.all_from_input_p (0))
8846                op1 = op0;
8847              else if (sel.all_from_input_p (1))
8848                {
8849                  op0 = op1;
8850                  sel.rotate_inputs (1);
8851                }
8852              else if (known_ge (poly_uint64 (sel[0]), nelts))
8853                {
8854                  std::swap (op0, op1);
8855                  sel.rotate_inputs (1);
8856                }
8857            }
8858          gassign *def;
8859          tree cop0 = op0, cop1 = op1;
8860          if (TREE_CODE (op0) == SSA_NAME
8861              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
8862              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8863            cop0 = gimple_assign_rhs1 (def);
8864          if (TREE_CODE (op1) == SSA_NAME
8865              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
8866              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8867            cop1 = gimple_assign_rhs1 (def);
8868          tree t;
8869        }
8870        (if ((TREE_CODE (cop0) == VECTOR_CST
8871              || TREE_CODE (cop0) == CONSTRUCTOR)
8872             && (TREE_CODE (cop1) == VECTOR_CST
8873                 || TREE_CODE (cop1) == CONSTRUCTOR)
8874             && (t = fold_vec_perm (type, cop0, cop1, sel)))
8875         { t; }
8876         (with
8877          {
8878            bool changed = (op0 == op1 && !single_arg);
8879            tree ins = NULL_TREE;
8880            unsigned at = 0;
8882            /* See if the permutation is performing a single element
8883               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
8884               in that case.  But only if the vector mode is supported,
8885               otherwise this is invalid GIMPLE.  */
8886            if (op_mode != BLKmode
8887                && (TREE_CODE (cop0) == VECTOR_CST
8888                    || TREE_CODE (cop0) == CONSTRUCTOR
8889                    || TREE_CODE (cop1) == VECTOR_CST
8890                    || TREE_CODE (cop1) == CONSTRUCTOR))
8891              {
8892                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
8893                if (insert_first_p)
8894                  {
8895                    /* After canonicalizing the first elt to come from the
8896                       first vector we only can insert the first elt from
8897                       the first vector.  */
8898                    at = 0;
8899                    if ((ins = fold_read_from_vector (cop0, sel[0])))
8900                      op0 = op1;
8901                  }
8902                /* The above can fail for two-element vectors which always
8903                   appear to insert the first element, so try inserting
8904                   into the second lane as well.  For more than two
8905                   elements that's wasted time.  */
8906                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
8907                  {
8908                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
8909                    for (at = 0; at < encoded_nelts; ++at)
8910                      if (maybe_ne (sel[at], at))
8911                        break;
8912                    if (at < encoded_nelts
8913                        && (known_eq (at + 1, nelts)
8914                            || sel.series_p (at + 1, 1, at + 1, 1)))
8915                      {
8916                        if (known_lt (poly_uint64 (sel[at]), nelts))
8917                          ins = fold_read_from_vector (cop0, sel[at]);
8918                        else
8919                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
8920                      }
8921                  }
8922              }
8924            /* Generate a canonical form of the selector.  */
8925            if (!ins && sel.encoding () != builder)
8926              {
8927                /* Some targets are deficient and fail to expand a single
8928                   argument permutation while still allowing an equivalent
8929                   2-argument version.  */
8930                tree oldop2 = op2;
8931                if (sel.ninputs () == 2
8932                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
8933                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8934                else
8935                  {
8936                    vec_perm_indices sel2 (builder, 2, nelts);
8937                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
8938                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
8939                    else
8940                      /* Not directly supported with either encoding,
8941                         so use the preferred form.  */
8942                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8943                  }
8944                if (!operand_equal_p (op2, oldop2, 0))
8945                  changed = true;
8946              }
8947          }
8948          (if (ins)
8949           (bit_insert { op0; } { ins; }
8950            { bitsize_int (at * vector_element_bits (type)); })
8951           (if (changed)
8952            (vec_perm { op0; } { op1; } { op2; }))))))))))))
8954 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
8956 (match vec_same_elem_p
8957  (vec_duplicate @0))
8959 (match vec_same_elem_p
8960  CONSTRUCTOR@0
8961  (if (TREE_CODE (@0) == SSA_NAME
8962       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
8964 (match vec_same_elem_p
8965  @0
8966  (if (uniform_vector_p (@0))))
8969 (simplify
8970  (vec_perm vec_same_elem_p@0 @0 @1)
8971  (if (types_match (type, TREE_TYPE (@0)))
8972   @0
8973   (with
8974    {
8975      tree elem = uniform_vector_p (@0);
8976    }
8977    (if (elem)
8978     { build_vector_from_val (type, elem); }))))
8980 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
8981 (simplify
8982  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8983  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8984   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8985 (simplify
8986  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8987  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8988   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8991 /* Merge
8992    c = VEC_PERM_EXPR <a, b, VCST0>;
8993    d = VEC_PERM_EXPR <c, c, VCST1>;
8994    to
8995    d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
8997 (simplify
8998  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
8999  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9000   (with
9001    {
9002      machine_mode result_mode = TYPE_MODE (type);
9003      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9004      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9005      vec_perm_builder builder0;
9006      vec_perm_builder builder1;
9007      vec_perm_builder builder2 (nelts, nelts, 1);
9008    }
9009    (if (tree_to_vec_perm_builder (&builder0, @3)
9010         && tree_to_vec_perm_builder (&builder1, @4))
9011     (with
9012      {
9013        vec_perm_indices sel0 (builder0, 2, nelts);
9014        vec_perm_indices sel1 (builder1, 1, nelts);
9016        for (int i = 0; i < nelts; i++)
9017          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9019        vec_perm_indices sel2 (builder2, 2, nelts);
9021        tree op0 = NULL_TREE;
9022        /* If the new VEC_PERM_EXPR can't be handled but both
9023           original VEC_PERM_EXPRs can, punt.
9024           If one or both of the original VEC_PERM_EXPRs can't be
9025           handled and the new one can't be either, don't increase
9026           number of VEC_PERM_EXPRs that can't be handled.  */
9027        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9028            || (single_use (@0)
9029                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9030                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9031                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9032          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9033      }
9034      (if (op0)
9035       (vec_perm @1 @2 { op0; })))))))
9038 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9039    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9040    constant which when multiplied by a power of 2 contains a unique value
9041    in the top 5 or 6 bits.  This is then indexed into a table which maps it
9042    to the number of trailing zeroes.  */
9043 (match (ctz_table_index @1 @2 @3)
9044   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9046 (match (cond_expr_convert_p @0 @2 @3 @6)
9047  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9048   (if (INTEGRAL_TYPE_P (type)
9049        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9050        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9051        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9052        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9053        && TYPE_PRECISION (TREE_TYPE (@0))
9054           == TYPE_PRECISION (TREE_TYPE (@2))
9055        && TYPE_PRECISION (TREE_TYPE (@0))
9056           == TYPE_PRECISION (TREE_TYPE (@3))
9057        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9058           signess when convert is truncation, but not ok for extension since
9059           it's sign_extend vs zero_extend.  */
9060        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9061            || (TYPE_UNSIGNED (TREE_TYPE (@2))
9062                == TYPE_UNSIGNED (TREE_TYPE (@3))))
9063        && single_use (@4)
9064        && single_use (@5))))
9066 (for bit_op (bit_and bit_ior bit_xor)
9067  (match (bitwise_induction_p @0 @2 @3)
9068   (bit_op:c
9069    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9070    @3)))
9072 (match (bitwise_induction_p @0 @2 @3)
9073  (bit_not
9074   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9076 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
9077    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
9078 (simplify
9079   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9080   (with { auto i = wi::neg (wi::to_wide (@2)); }
9081   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
9082     (if (wi::popcount (i) == 1
9083          && (wi::to_wide (@1)) == (i - 1))
9084       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9085         (bit_and @0 @1)
9086       (cond (le @0 @1) @0 (bit_and @0 @1))))))
9088 /* -x & 1 -> x & 1.  */
9089 (simplify 
9090  (bit_and (negate @0) integer_onep@1)
9091  (if (!TYPE_OVERFLOW_SANITIZED (type))
9092   (bit_and @0 @1)))
9094 /* `-a` is just `a` if the type is 1bit wide or when converting
9095    to a 1bit type; similar to the above transformation of `(-x)&1`.
9096    This is used mostly with the transformation of
9097    `a ? ~b : b` into `(-a)^b`.
9098    It also can show up with bitfields.  */
9099 (simplify
9100  (convert? (negate @0))
9101  (if (INTEGRAL_TYPE_P (type)
9102       && TYPE_PRECISION (type) == 1
9103       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9104   (convert @0)))
9106 /* Optimize
9107    c1 = VEC_PERM_EXPR (a, a, mask)
9108    c2 = VEC_PERM_EXPR (b, b, mask)
9109    c3 = c1 op c2
9110    -->
9111    c = a op b
9112    c3 = VEC_PERM_EXPR (c, c, mask)
9113    For all integer non-div operations.  */
9114 (for op (plus minus mult bit_and bit_ior bit_xor
9115          lshift rshift)
9116  (simplify
9117   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9118    (if (VECTOR_INTEGER_TYPE_P (type))
9119     (vec_perm (op@3 @0 @1) @3 @2))))
9121 /* Similar for float arithmetic when permutation constant covers
9122    all vector elements.  */
9123 (for op (plus minus mult)
9124  (simplify
9125   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9126    (if (VECTOR_FLOAT_TYPE_P (type)
9127         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9128     (with
9129      {
9130        tree perm_cst = @2;
9131        vec_perm_builder builder;
9132        bool full_perm_p = false;
9133        if (tree_to_vec_perm_builder (&builder, perm_cst))
9134          {
9135            unsigned HOST_WIDE_INT nelts;
9137            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9138            /* Create a vec_perm_indices for the VECTOR_CST.  */
9139            vec_perm_indices sel (builder, 1, nelts);
9141            /* Check if perm indices covers all vector elements.  */
9142            if (sel.encoding ().encoded_full_vector_p ())
9143              {
9144                auto_sbitmap seen (nelts);
9145                bitmap_clear (seen);
9147                unsigned HOST_WIDE_INT count = 0, i;
9149                for (i = 0; i < nelts; i++)
9150                  {
9151                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9152                     break;
9153                    count++;
9154                  }
9155                full_perm_p = count == nelts;
9156              }
9157          }
9158       }
9159       (if (full_perm_p)
9160         (vec_perm (op@3 @0 @1) @3 @2))))))