Daily bump.
[official-gcc.git] / gcc / match.pd
blob4edba7c84fb9ac5ea5b02777e2ebbd3c0d201cd7
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-2024 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)
90 (define_operator_list COND_LEN_UNARY
91   IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function.  */
94 (define_operator_list UNCOND_BINARY
95   plus minus
96   mult trunc_div trunc_mod rdiv
97   min max
98   IFN_FMIN IFN_FMAX IFN_COPYSIGN
99   bit_and bit_ior bit_xor
100   lshift rshift)
101 (define_operator_list COND_BINARY
102   IFN_COND_ADD IFN_COND_SUB
103   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104   IFN_COND_MIN IFN_COND_MAX
105   IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107   IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109   IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110   IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111   IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112   IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113   IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114   IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115   IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations.  */
118 (define_operator_list UNCOND_TERNARY
119   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123   IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129   BUILT_IN_ATOMIC_FETCH_OR_16
130   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132   BUILT_IN_ATOMIC_FETCH_XOR_16
133   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135   BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140   BUILT_IN_SYNC_FETCH_AND_OR_16
141   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143   BUILT_IN_SYNC_FETCH_AND_XOR_16
144   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146   BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*.  */
148 (define_operator_list ATOMIC_FETCH_AND_N
149   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151   BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*.  */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156   BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159    plus conditionalize on tree_nop_conversion_p conversions.  */
160 (match (nop_convert @0)
161  (convert @0)
162  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
164  (view_convert @0)
165  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166       && known_eq (TYPE_VECTOR_SUBPARTS (type),
167                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
170 #if GIMPLE
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172    detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
174  (bit_not @0))
175 (match (bit_not_with_nop @0)
176  (convert (bit_not @0))
177  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179  (match (maybe_cmp @0)
180   (cmp@0 @1 @2))
181  (match (maybe_cmp @0)
182   (convert (cmp@0 @1 @2))
183    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
185 /* `a ^ b` is another form of `a != b` when the type
186     is a 1bit precission integer.  */
187 (match (maybe_cmp @0)
188  (bit_xor@0 @1 @2)
189  (if (INTEGRAL_TYPE_P (type)
190       && TYPE_PRECISION (type) == 1)))
191 #endif
193 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
194    ABSU_EXPR returns unsigned absolute value of the operand and the operand
195    of the ABSU_EXPR will have the corresponding signed type.  */
196 (simplify (abs (convert @0))
197  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
198       && !TYPE_UNSIGNED (TREE_TYPE (@0))
199       && element_precision (type) > element_precision (TREE_TYPE (@0)))
200   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
201    (convert (absu:utype @0)))))
203 #if GIMPLE
204 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
205 (simplify
206  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
207  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
208       && !TYPE_UNSIGNED (TREE_TYPE (@0))
209       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
210   (abs @0)))
211 #endif
213 /* Simplifications of operations with one constant operand and
214    simplifications to constants or single values.  */
216 (for op (plus pointer_plus minus bit_ior bit_xor)
217   (simplify
218     (op @0 integer_zerop)
219     (non_lvalue @0)))
221 /* 0 +p index -> (type)index */
222 (simplify
223  (pointer_plus integer_zerop @1)
224  (non_lvalue (convert @1)))
226 /* ptr - 0 -> (type)ptr */
227 (simplify
228  (pointer_diff @0 integer_zerop)
229  (convert @0))
231 /* See if ARG1 is zero and X + ARG1 reduces to X.
232    Likewise if the operands are reversed.  */
233 (simplify
234  (plus:c @0 real_zerop@1)
235  (if (fold_real_zero_addition_p (type, @0, @1, 0))
236   (non_lvalue @0)))
238 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
239 (simplify
240  (minus @0 real_zerop@1)
241  (if (fold_real_zero_addition_p (type, @0, @1, 1))
242   (non_lvalue @0)))
244 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
245    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
246    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
247    if not -frounding-math.  For sNaNs the first operation would raise
248    exceptions but turn the result into qNan, so the second operation
249    would not raise it.   */
250 (for inner_op (plus minus)
251  (for outer_op (plus minus)
252   (simplify
253    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
254     (if (real_zerop (@1)
255          && real_zerop (@2)
256          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
257      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
258                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
259              bool outer_plus
260                = ((outer_op == PLUS_EXPR)
261                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
262       (if (outer_plus && !inner_plus)
263        (outer_op @0 @2)
264        @3))))))
266 /* Simplify x - x.
267    This is unsafe for certain floats even in non-IEEE formats.
268    In IEEE, it is unsafe because it does wrong for NaNs.
269    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
270    Also note that operand_equal_p is always false if an operand
271    is volatile.  */
272 (simplify
273  (minus @0 @0)
274  (if (!FLOAT_TYPE_P (type)
275       || (!tree_expr_maybe_nan_p (@0)
276           && !tree_expr_maybe_infinite_p (@0)
277           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
278               || !HONOR_SIGNED_ZEROS (type))))
279   { build_zero_cst (type); }))
280 (simplify
281  (pointer_diff @@0 @0)
282  { build_zero_cst (type); })
284 (simplify
285  (mult @0 integer_zerop@1)
286  @1)
288 /* -x == x -> x == 0 */
289 (for cmp (eq ne)
290  (simplify
291   (cmp:c @0 (negate @0))
292    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
293         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
294     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
296 /* Maybe fold x * 0 to 0.  The expressions aren't the same
297    when x is NaN, since x * 0 is also NaN.  Nor are they the
298    same in modes with signed zeros, since multiplying a
299    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
300    since x * 0 is NaN.  */
301 (simplify
302  (mult @0 real_zerop@1)
303  (if (!tree_expr_maybe_nan_p (@0)
304       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
305       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
306   @1))
308 /* In IEEE floating point, x*1 is not equivalent to x for snans.
309    Likewise for complex arithmetic with signed zeros.  */
310 (simplify
311  (mult @0 real_onep)
312  (if (!tree_expr_maybe_signaling_nan_p (@0)
313       && (!HONOR_SIGNED_ZEROS (type)
314           || !COMPLEX_FLOAT_TYPE_P (type)))
315   (non_lvalue @0)))
317 /* Transform x * -1.0 into -x.  */
318 (simplify
319  (mult @0 real_minus_onep)
320   (if (!tree_expr_maybe_signaling_nan_p (@0)
321        && (!HONOR_SIGNED_ZEROS (type)
322            || !COMPLEX_FLOAT_TYPE_P (type)))
323    (negate @0)))
325 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
326    unless the target has native support for the former but not the latter.  */
327 (simplify
328  (mult @0 VECTOR_CST@1)
329  (if (initializer_each_zero_or_onep (@1)
330       && !HONOR_SNANS (type)
331       && !HONOR_SIGNED_ZEROS (type))
332   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
333    (if (itype
334         && (!VECTOR_MODE_P (TYPE_MODE (type))
335             || (VECTOR_MODE_P (TYPE_MODE (itype))
336                 && optab_handler (and_optab,
337                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
338     (view_convert (bit_and:itype (view_convert @0)
339                                  (ne @1 { build_zero_cst (type); })))))))
341 /* In SWAR (SIMD within a register) code a signed comparison of packed data
342    can be constructed with a particular combination of shift, bitwise and,
343    and multiplication by constants.  If that code is vectorized we can
344    convert this pattern into a more efficient vector comparison.  */
345 (simplify
346  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
347             uniform_integer_cst_p@2)
348     uniform_integer_cst_p@3)
349  (with {
350    tree rshift_cst = uniform_integer_cst_p (@1);
351    tree bit_and_cst = uniform_integer_cst_p (@2);
352    tree mult_cst = uniform_integer_cst_p (@3);
353   }
354   /* Make sure we're working with vectors and uniform vector constants.  */
355   (if (VECTOR_TYPE_P (type)
356        && tree_fits_uhwi_p (rshift_cst)
357        && tree_fits_uhwi_p (mult_cst)
358        && tree_fits_uhwi_p (bit_and_cst))
359    /* Compute what constants would be needed for this to represent a packed
360       comparison based on the shift amount denoted by RSHIFT_CST.  */
361    (with {
362      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
363      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
364      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
365      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
366      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
367      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
368      mult_i = tree_to_uhwi (mult_cst);
369      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
370      bit_and_i = tree_to_uhwi (bit_and_cst);
371      target_bit_and_i = 0;
373      /* The bit pattern in BIT_AND_I should be a mask for the least
374         significant bit of each packed element that is CMP_BITS wide.  */
375      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
376        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
377     }
378     (if ((exact_log2 (cmp_bits_i)) >= 0
379          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
380          && multiple_p (vec_bits, cmp_bits_i)
381          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
382          && target_mult_i == mult_i
383          && target_bit_and_i == bit_and_i)
384      /* Compute the vector shape for the comparison and check if the target is
385         able to expand the comparison with that type.  */
386      (with {
387        /* We're doing a signed comparison.  */
388        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
389        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
390        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
391        tree vec_truth_type = truth_type_for (vec_cmp_type);
392        tree zeros = build_zero_cst (vec_cmp_type);
393        tree ones = build_all_ones_cst (vec_cmp_type);
394       }
395       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
396            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
397        (view_convert:type (vec_cond (lt:vec_truth_type
398                                      (view_convert:vec_cmp_type @0)
399                                      { zeros; })
400                            { ones; } { zeros; })))))))))
402 (for cmp (gt ge lt le)
403      outp (convert convert negate negate)
404      outn (negate negate convert convert)
405  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
406  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
407  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
408  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
409  (simplify
410   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
411   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
412    (outp (abs @0))))
413  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
414  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
415  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
416  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
417  (simplify
418   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
419   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
420    (outn (abs @0)))))
422 /* Transform X * copysign (1.0, X) into abs(X). */
423 (simplify
424  (mult:c @0 (COPYSIGN_ALL real_onep @0))
425  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
426   (abs @0)))
428 /* Transform X * copysign (1.0, -X) into -abs(X). */
429 (simplify
430  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
431  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
432   (negate (abs @0))))
434 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
435 (simplify
436  (COPYSIGN_ALL REAL_CST@0 @1)
437  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
438   (COPYSIGN_ALL (negate @0) @1)))
440 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
441    tree-ssa-math-opts.cc does the corresponding optimization for
442    unconditional multiplications (via xorsign).  */
443 (simplify
444  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
445  (with { tree signs = sign_mask_for (type); }
446   (if (signs)
447    (with { tree inttype = TREE_TYPE (signs); }
448     (view_convert:type
449      (IFN_COND_XOR:inttype @0
450       (view_convert:inttype @1)
451       (bit_and (view_convert:inttype @2) { signs; })
452       (view_convert:inttype @3)))))))
454 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
455 (simplify
456  (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
457  (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
458   (abs @0)))
460 /* X * 1, X / 1 -> X.  */
461 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
462   (simplify
463     (op @0 integer_onep)
464     (non_lvalue @0)))
466 /* (A / (1 << B)) -> (A >> B).
467    Only for unsigned A.  For signed A, this would not preserve rounding
468    toward zero.
469    For example: (-1 / ( 1 << B)) !=  -1 >> B.
470    Also handle widening conversions, like:
471    (A / (unsigned long long) (1U << B)) -> (A >> B)
472    or
473    (A / (unsigned long long) (1 << B)) -> (A >> B).
474    If the left shift is signed, it can be done only if the upper bits
475    of A starting from shift's type sign bit are zero, as
476    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
477    so it is valid only if A >> 31 is zero.  */
478 (simplify
479  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
480  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
481       && (!VECTOR_TYPE_P (type)
482           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
483           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
484       && (useless_type_conversion_p (type, TREE_TYPE (@1))
485           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
486               && (TYPE_UNSIGNED (TREE_TYPE (@1))
487                   || (element_precision (type)
488                       == element_precision (TREE_TYPE (@1)))
489                   || (INTEGRAL_TYPE_P (type)
490                       && (tree_nonzero_bits (@0)
491                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
492                                       true,
493                                       element_precision (type))) == 0)))))
494    (if (!VECTOR_TYPE_P (type)
495         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
496         && element_precision (TREE_TYPE (@3)) < element_precision (type))
497     (convert (rshift @3 @2))
498     (rshift @0 @2))))
500 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
501    undefined behavior in constexpr evaluation, and assuming that the division
502    traps enables better optimizations than these anyway.  */
503 (for div (trunc_div ceil_div floor_div round_div exact_div)
504  /* 0 / X is always zero.  */
505  (simplify
506   (div integer_zerop@0 @1)
507   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
508   (if (!integer_zerop (@1))
509    @0))
510  /* X / -1 is -X.  */
511  (simplify
512   (div @0 integer_minus_onep@1)
513   (if (!TYPE_UNSIGNED (type))
514    (negate @0)))
515  /* X / bool_range_Y is X.  */ 
516  (simplify
517   (div @0 SSA_NAME@1)
518   (if (INTEGRAL_TYPE_P (type)
519        && ssa_name_has_boolean_range (@1)
520        && !flag_non_call_exceptions)
521    @0))
522  /* X / X is one.  */
523  (simplify
524   (div @0 @0)
525   /* But not for 0 / 0 so that we can get the proper warnings and errors.
526      And not for _Fract types where we can't build 1.  */
527   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
528        && !integer_zerop (@0)
529        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
530    { build_one_cst (type); }))
531  /* X / abs (X) is X < 0 ? -1 : 1.  */
532  (simplify
533    (div:C @0 (abs @0))
534    (if (INTEGRAL_TYPE_P (type)
535         && TYPE_OVERFLOW_UNDEFINED (type)
536         && !integer_zerop (@0)
537         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
538     (cond (lt @0 { build_zero_cst (type); })
539           { build_minus_one_cst (type); } { build_one_cst (type); })))
540  /* X / -X is -1.  */
541  (simplify
542    (div:C @0 (negate @0))
543    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
544         && TYPE_OVERFLOW_UNDEFINED (type)
545         && !integer_zerop (@0)
546         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
547     { build_minus_one_cst (type); })))
549 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
550    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
551    for MOD instead of DIV.  */
552 (for floor_divmod (floor_div floor_mod)
553      trunc_divmod (trunc_div trunc_mod)
554  (simplify
555   (floor_divmod @0 @1)
556   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
557        && TYPE_UNSIGNED (type))
558    (trunc_divmod @0 @1))))
560 /* 1 / X -> X == 1 for unsigned integer X.
561    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
562    But not for 1 / 0 so that we can get proper warnings and errors,
563    and not for 1-bit integers as they are edge cases better handled
564    elsewhere.  Delay the conversion of the signed division until late
565    because `1 / X` is simplier to handle than the resulting COND_EXPR.  */
566 (simplify
567  (trunc_div integer_onep@0 @1)
568  (if (INTEGRAL_TYPE_P (type)
569       && TYPE_PRECISION (type) > 1
570       && !integer_zerop (@1)
571       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
572   (if (TYPE_UNSIGNED (type))
573    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
574    (if (!canonicalize_math_p ())
575     (with { tree utype = unsigned_type_for (type); }
576      (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
577                 { build_int_cst (utype, 2); })
578       @1 { build_zero_cst (type); }))))))
580 /* Combine two successive divisions.  Note that combining ceil_div
581    and floor_div is trickier and combining round_div even more so.  */
582 (for div (trunc_div exact_div)
583  (simplify
584   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
585   (with {
586     wi::overflow_type overflow;
587     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
588                             TYPE_SIGN (type), &overflow);
589    }
590    (if (div == EXACT_DIV_EXPR
591         || optimize_successive_divisions_p (@2, @3))
592     (if (!overflow)
593      (div @0 { wide_int_to_tree (type, mul); })
594      (if (TYPE_UNSIGNED (type)
595           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
596       { build_zero_cst (type); }))))))
598 /* Combine successive multiplications.  Similar to above, but handling
599    overflow is different.  */
600 (simplify
601  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
602  (with {
603    wi::overflow_type overflow;
604    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
605                            TYPE_SIGN (type), &overflow);
606   }
607   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
608      otherwise undefined overflow implies that @0 must be zero.  */
609   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
610    (mult @0 { wide_int_to_tree (type, mul); }))))
612 /* Similar to above, but there could be an extra add/sub between
613    successive multuiplications.  */
614 (simplify
615  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
616  (with {
617    bool overflowed = true;
618    wi::overflow_type ovf1, ovf2;
619    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
620                            TYPE_SIGN (type), &ovf1);
621    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
622                            TYPE_SIGN (type), &ovf2);
623   if (TYPE_OVERFLOW_UNDEFINED (type))
624     {
625 #if GIMPLE
626       value_range vr0;
627       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
628           && get_global_range_query ()->range_of_expr (vr0, @4)
629           && !vr0.varying_p () && !vr0.undefined_p ())
630         {
631           wide_int wmin0 = vr0.lower_bound ();
632           wide_int wmax0 = vr0.upper_bound ();
633           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
634           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
635           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
636             {
637               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
638               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
639               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
640                 overflowed = false;
641             }
642         }
643 #endif
644     }
645   else
646    overflowed = false;
648   /* Skip folding on overflow.  */
649   (if (!overflowed)
650    (plus (mult @0 { wide_int_to_tree (type, mul); })
651          { wide_int_to_tree (type, add); }))))
653 /* Similar to above, but a multiplication between successive additions.  */
654 (simplify
655  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
656  (with {
657    bool overflowed = true;
658    wi::overflow_type ovf1;
659    wi::overflow_type ovf2;
660    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
661                            TYPE_SIGN (type), &ovf1);
662    wide_int add = wi::add (mul, wi::to_wide (@3),
663                            TYPE_SIGN (type), &ovf2);
664   if (TYPE_OVERFLOW_UNDEFINED (type))
665     {
666 #if GIMPLE
667       value_range vr0;
668       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
669           && get_global_range_query ()->range_of_expr (vr0, @0)
670           && !vr0.varying_p () && !vr0.undefined_p ())
671         {
672           wide_int wmin0 = vr0.lower_bound ();
673           wide_int wmax0 = vr0.upper_bound ();
674           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
675           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
676           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
677             {
678               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
679               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
680               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
681                 overflowed = false;
682             }
683         }
684 #endif
685     }
686   else
687    overflowed = false;
689   /* Skip folding on overflow.  */
690   (if (!overflowed)
691    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
693 /* Optimize A / A to 1.0 if we don't care about
694    NaNs or Infinities.  */
695 (simplify
696  (rdiv @0 @0)
697  (if (FLOAT_TYPE_P (type)
698       && ! HONOR_NANS (type)
699       && ! HONOR_INFINITIES (type))
700   { build_one_cst (type); }))
702 /* Optimize -A / A to -1.0 if we don't care about
703    NaNs or Infinities.  */
704 (simplify
705  (rdiv:C @0 (negate @0))
706  (if (FLOAT_TYPE_P (type)
707       && ! HONOR_NANS (type)
708       && ! HONOR_INFINITIES (type))
709   { build_minus_one_cst (type); }))
711 /* PR71078: x / abs(x) -> copysign (1.0, x) */
712 (simplify
713  (rdiv:C (convert? @0) (convert? (abs @0)))
714   (if (SCALAR_FLOAT_TYPE_P (type)
715        && ! HONOR_NANS (type)
716        && ! HONOR_INFINITIES (type))
717    (switch
718     (if (types_match (type, float_type_node))
719      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
720     (if (types_match (type, double_type_node))
721      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
722     (if (types_match (type, long_double_type_node))
723      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
725 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
726 (simplify
727  (rdiv @0 real_onep)
728  (if (!tree_expr_maybe_signaling_nan_p (@0))
729   (non_lvalue @0)))
731 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
732 (simplify
733  (rdiv @0 real_minus_onep)
734  (if (!tree_expr_maybe_signaling_nan_p (@0))
735   (negate @0)))
737 (if (flag_reciprocal_math)
738  /* Convert (A/B)/C to A/(B*C). */
739  (simplify
740   (rdiv (rdiv:s @0 @1) @2)
741   (rdiv @0 (mult @1 @2)))
743  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
744  (simplify
745   (rdiv @0 (mult:s @1 REAL_CST@2))
746   (with
747    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
748    (if (tem)
749     (rdiv (mult @0 { tem; } ) @1))))
751  /* Convert A/(B/C) to (A/B)*C  */
752  (simplify
753   (rdiv @0 (rdiv:s @1 @2))
754    (mult (rdiv @0 @1) @2)))
756 /* Simplify x / (- y) to -x / y.  */
757 (simplify
758  (rdiv @0 (negate @1))
759  (rdiv (negate @0) @1))
761 (if (flag_unsafe_math_optimizations)
762  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
763     Since C / x may underflow to zero, do this only for unsafe math.  */
764  (for op (lt le gt ge)
765       neg_op (gt ge lt le)
766   (simplify
767    (op (rdiv REAL_CST@0 @1) real_zerop@2)
768    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
769     (switch
770      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
771       (op @1 @2))
772      /* For C < 0, use the inverted operator.  */
773      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
774       (neg_op @1 @2)))))))
776 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
777 (for div (trunc_div ceil_div floor_div round_div exact_div)
778  (simplify
779   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
780   (if (integer_pow2p (@2)
781        && tree_int_cst_sgn (@2) > 0
782        && tree_nop_conversion_p (type, TREE_TYPE (@0))
783        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
784    (rshift (convert @0)
785            { build_int_cst (integer_type_node,
786                             wi::exact_log2 (wi::to_wide (@2))); }))))
788 /* If ARG1 is a constant, we can convert this to a multiply by the
789    reciprocal.  This does not have the same rounding properties,
790    so only do this if -freciprocal-math.  We can actually
791    always safely do it if ARG1 is a power of two, but it's hard to
792    tell if it is or not in a portable manner.  */
793 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
794  (simplify
795   (rdiv @0 cst@1)
796   (if (optimize)
797    (if (flag_reciprocal_math
798         && !real_zerop (@1))
799     (with
800      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
801      (if (tem)
802       (mult @0 { tem; } )))
803     (if (cst != COMPLEX_CST)
804      (with { tree inverse = exact_inverse (type, @1); }
805       (if (inverse)
806        (mult @0 { inverse; } ))))))))
808 (for mod (ceil_mod floor_mod round_mod trunc_mod)
809  /* 0 % X is always zero.  */
810  (simplify
811   (mod integer_zerop@0 @1)
812   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
813   (if (!integer_zerop (@1))
814    @0))
815  /* X % 1 is always zero.  */
816  (simplify
817   (mod @0 integer_onep)
818   { build_zero_cst (type); })
819  /* X % -1 is zero.  */
820  (simplify
821   (mod @0 integer_minus_onep@1)
822   (if (!TYPE_UNSIGNED (type))
823    { build_zero_cst (type); }))
824  /* X % X is zero.  */
825  (simplify
826   (mod @0 @0)
827   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
828   (if (!integer_zerop (@0))
829    { build_zero_cst (type); }))
830  /* (X % Y) % Y is just X % Y.  */
831  (simplify
832   (mod (mod@2 @0 @1) @1)
833   @2)
834  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
835  (simplify
836   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
837   (if (ANY_INTEGRAL_TYPE_P (type)
838        && TYPE_OVERFLOW_UNDEFINED (type)
839        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
840                              TYPE_SIGN (type)))
841    { build_zero_cst (type); }))
842  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
843     modulo and comparison, since it is simpler and equivalent.  */
844  (for cmp (eq ne)
845   (simplify
846    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
847    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
848     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
849      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
851 /* X % -C is the same as X % C.  */
852 (simplify
853  (trunc_mod @0 INTEGER_CST@1)
854   (if (TYPE_SIGN (type) == SIGNED
855        && !TREE_OVERFLOW (@1)
856        && wi::neg_p (wi::to_wide (@1))
857        && !TYPE_OVERFLOW_TRAPS (type)
858        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
859        && !sign_bit_p (@1, @1))
860    (trunc_mod @0 (negate @1))))
862 /* X % -Y is the same as X % Y.  */
863 (simplify
864  (trunc_mod @0 (convert? (negate @1)))
865  (if (INTEGRAL_TYPE_P (type)
866       && !TYPE_UNSIGNED (type)
867       && !TYPE_OVERFLOW_TRAPS (type)
868       && tree_nop_conversion_p (type, TREE_TYPE (@1))
869       /* Avoid this transformation if X might be INT_MIN or
870          Y might be -1, because we would then change valid
871          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
872       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
873           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
874                                                         (TREE_TYPE (@1))))))
875   (trunc_mod @0 (convert @1))))
877 /* X - (X / Y) * Y is the same as X % Y.  */
878 (simplify
879  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
880  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
881   (convert (trunc_mod @0 @1))))
883 /* x * (1 + y / x) - y -> x - y % x */
884 (simplify
885  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
886  (if (INTEGRAL_TYPE_P (type))
887   (minus @0 (trunc_mod @1 @0))))
889 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
890    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
891    Also optimize A % (C << N)  where C is a power of 2,
892    to A & ((C << N) - 1).
893    Also optimize "A shift (B % C)", if C is a power of 2, to
894    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
895    and assume (B % C) is nonnegative as shifts negative values would
896    be UB.  */
897 (match (power_of_two_cand @1)
898  INTEGER_CST@1)
899 (match (power_of_two_cand @1)
900  (lshift INTEGER_CST@1 @2))
901 (for mod (trunc_mod floor_mod)
902  (for shift (lshift rshift)
903   (simplify
904    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
905    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
906     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
907                                                       1); }))))))
908  (simplify
909   (mod @0 (convert? (power_of_two_cand@1 @2)))
910   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
911        /* Allow any integral conversions of the divisor, except
912           conversion from narrower signed to wider unsigned type
913           where if @1 would be negative power of two, the divisor
914           would not be a power of two.  */
915        && INTEGRAL_TYPE_P (type)
916        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
917        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
918            || TYPE_UNSIGNED (TREE_TYPE (@1))
919            || !TYPE_UNSIGNED (type))
920        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
921    (with { tree utype = TREE_TYPE (@1);
922            if (!TYPE_OVERFLOW_WRAPS (utype))
923              utype = unsigned_type_for (utype); }
924     (bit_and @0 (convert (minus (convert:utype @1)
925                                 { build_one_cst (utype); })))))))
927 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
928 (simplify
929  (trunc_div (mult @0 integer_pow2p@1) @1)
930  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931   (bit_and @0 { wide_int_to_tree
932                 (type, wi::mask (TYPE_PRECISION (type)
933                                  - wi::exact_log2 (wi::to_wide (@1)),
934                                  false, TYPE_PRECISION (type))); })))
936 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
937 (simplify
938  (mult (trunc_div @0 integer_pow2p@1) @1)
939  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
940   (bit_and @0 (negate @1))))
942 (for div (trunc_div ceil_div floor_div round_div exact_div)
943  /* Simplify (t * u) / u -> t.  */
944  (simplify
945   (div (mult:c @0 @1) @1)
946   (if (ANY_INTEGRAL_TYPE_P (type))
947    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
948     @0
949 #if GIMPLE
950     (with {value_range vr0, vr1;}
951      (if (INTEGRAL_TYPE_P (type)
952           && get_range_query (cfun)->range_of_expr (vr0, @0)
953           && get_range_query (cfun)->range_of_expr (vr1, @1)
954           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
955       @0))
956 #endif
957    )))
958 #if GIMPLE
959  /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v.  */
960  (simplify
961   (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
962   (if (INTEGRAL_TYPE_P (type)
963        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
964    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
965     (mult @0 (div! @1 @2))
966     (with {value_range vr0, vr1;}
967      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
968           && get_range_query (cfun)->range_of_expr (vr1, @1)
969           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
970       (mult @0 (div! @1 @2))))
971    )))
972 #endif
973  /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v.  */
974  (simplify
975   (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
976   (if (INTEGRAL_TYPE_P (type)
977        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
978    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
979     (div @1 @2)
980 #if GIMPLE
981     (with {value_range vr0, vr1, vr2;}
982      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
983           && get_range_query (cfun)->range_of_expr (vr1, @1)
984           && get_range_query (cfun)->range_of_expr (vr2, @2)
985           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
986           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
987       (div @1 @2)))
988 #endif
989    ))))
991 #if GIMPLE
992 (for div (trunc_div exact_div)
993  /* Simplify (X + M*N) / N -> X / N + M.  */
994  (simplify
995   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
996   (with {value_range vr0, vr1, vr2, vr3, vr4;}
997   (if (INTEGRAL_TYPE_P (type)
998        && get_range_query (cfun)->range_of_expr (vr1, @1)
999        && get_range_query (cfun)->range_of_expr (vr2, @2)
1000        /* "N*M" doesn't overflow.  */
1001        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1002        && get_range_query (cfun)->range_of_expr (vr0, @0)
1003        && get_range_query (cfun)->range_of_expr (vr3, @3)
1004        /* "X+(N*M)" doesn't overflow.  */
1005        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1006        && get_range_query (cfun)->range_of_expr (vr4, @4)
1007        && !vr4.undefined_p ()
1008        /* "X+N*M" is not with opposite sign as "X".  */
1009        && (TYPE_UNSIGNED (type)
1010            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1011            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1012   (plus (div @0 @2) @1))))
1014  /* Simplify (X - M*N) / N -> X / N - M.  */
1015  (simplify
1016   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1017   (with {value_range vr0, vr1, vr2, vr3, vr4;}
1018   (if (INTEGRAL_TYPE_P (type)
1019        && get_range_query (cfun)->range_of_expr (vr1, @1)
1020        && get_range_query (cfun)->range_of_expr (vr2, @2)
1021        /* "N * M" doesn't overflow.  */
1022        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1023        && get_range_query (cfun)->range_of_expr (vr0, @0)
1024        && get_range_query (cfun)->range_of_expr (vr3, @3)
1025        /* "X - (N*M)" doesn't overflow.  */
1026        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1027        && get_range_query (cfun)->range_of_expr (vr4, @4)
1028        && !vr4.undefined_p ()
1029        /* "X-N*M" is not with opposite sign as "X".  */
1030        && (TYPE_UNSIGNED (type)
1031            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1032            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1033   (minus (div @0 @2) @1)))))
1035 /* Simplify
1036    (X + C) / N -> X / N + C / N where C is multiple of N.
1037    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
1038 (for op (trunc_div exact_div rshift)
1039  (simplify
1040   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1041    (with
1042     {
1043       wide_int c = wi::to_wide (@1);
1044       wide_int n = wi::to_wide (@2);
1045       bool shift = op == RSHIFT_EXPR;
1046 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1047                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1048 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1049                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1050       value_range vr0, vr1, vr3;
1051     }
1052     (if (INTEGRAL_TYPE_P (type)
1053          && get_range_query (cfun)->range_of_expr (vr0, @0))
1054      (if (exact_mod (c)
1055           && get_range_query (cfun)->range_of_expr (vr1, @1)
1056           /* "X+C" doesn't overflow.  */
1057           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1058           && get_range_query (cfun)->range_of_expr (vr3, @3)
1059           && !vr3.undefined_p ()
1060           /* "X+C" and "X" are not of opposite sign.  */
1061           && (TYPE_UNSIGNED (type)
1062               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1063               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1064        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1065        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1066             && exact_mod (-c)
1067             /* unsigned "X-(-C)" doesn't underflow.  */
1068             && wi::geu_p (vr0.lower_bound (), -c))
1069          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1070 #undef plus_op1
1071 #undef exact_mod
1072 #endif
1074 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1075    if var is smaller in precision.
1076    This is always safe for both doing the negative in signed or unsigned
1077    as the value for undefined will not show up.
1078    Note the outer cast cannot be a boolean type as the only valid values
1079    are 0,-1/1 (depending on the signedness of the boolean) and the negative
1080    is there to get the correct value.  */
1081 (simplify
1082  (convert (negate:s@1 (convert:s @0)))
1083  (if (INTEGRAL_TYPE_P (type)
1084       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1085       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1086       && TREE_CODE (type) != BOOLEAN_TYPE)
1087     (negate (convert @0))))
1089 (for op (negate abs)
1090  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1091  (for coss (COS COSH)
1092   (simplify
1093    (coss (op @0))
1094     (coss @0)))
1095  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1096  (for pows (POW)
1097   (simplify
1098    (pows (op @0) REAL_CST@1)
1099    (with { HOST_WIDE_INT n; }
1100     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1101      (pows @0 @1)))))
1102  /* Likewise for powi.  */
1103  (for pows (POWI)
1104   (simplify
1105    (pows (op @0) INTEGER_CST@1)
1106    (if ((wi::to_wide (@1) & 1) == 0)
1107     (pows @0 @1))))
1108  /* Strip negate and abs from both operands of hypot.  */
1109  (for hypots (HYPOT)
1110   (simplify
1111    (hypots (op @0) @1)
1112    (hypots @0 @1))
1113   (simplify
1114    (hypots @0 (op @1))
1115    (hypots @0 @1)))
1116  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1117  (for copysigns (COPYSIGN_ALL)
1118   (simplify
1119    (copysigns (op @0) @1)
1120    (copysigns @0 @1))))
1122 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1123 (simplify
1124  (mult (abs@1 @0) @1)
1125  (mult @0 @0))
1127 /* Convert absu(x)*absu(x) -> x*x.  */
1128 (simplify
1129  (mult (absu@1 @0) @1)
1130  (mult (convert@2 @0) @2))
1132 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1133 (for coss (COS COSH)
1134  (for copysigns (COPYSIGN)
1135   (simplify
1136    (coss (copysigns @0 @1))
1137     (coss @0))))
1139 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1140 (for pows (POW)
1141  (for copysigns (COPYSIGN)
1142   (simplify
1143    (pows (copysigns @0 @2) REAL_CST@1)
1144    (with { HOST_WIDE_INT n; }
1145     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1146      (pows @0 @1))))))
1147 /* Likewise for powi.  */
1148 (for pows (POWI)
1149  (for copysigns (COPYSIGN)
1150   (simplify
1151    (pows (copysigns @0 @2) INTEGER_CST@1)
1152    (if ((wi::to_wide (@1) & 1) == 0)
1153     (pows @0 @1)))))
1155 (for hypots (HYPOT)
1156  (for copysigns (COPYSIGN)
1157   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1158   (simplify
1159    (hypots (copysigns @0 @1) @2)
1160    (hypots @0 @2))
1161   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1162   (simplify
1163    (hypots @0 (copysigns @1 @2))
1164    (hypots @0 @1))))
1166 /* copysign(x, CST) -> abs (x).  If the target does not
1167    support the copysign optab then canonicalize
1168    copysign(x, -CST) -> fneg (abs (x)).   */
1169 (for copysigns (COPYSIGN_ALL)
1170  (simplify
1171   (copysigns @0 REAL_CST@1)
1172   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1173    (abs @0)
1174 #if GIMPLE
1175    (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1176                                          OPTIMIZE_FOR_BOTH))
1177     (negate (abs @0)))
1178 #endif
1179    )))
1181 #if GIMPLE
1182 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1183    representation if the target supports the copysign optab.  */
1184 (simplify
1185  (negate (abs @0))
1186  (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1187                                       OPTIMIZE_FOR_BOTH))
1188    (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1189 #endif
1190 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1191 (for copysigns (COPYSIGN_ALL)
1192  (simplify
1193   (copysigns (copysigns @0 @1) @2)
1194   (copysigns @0 @2)))
1196 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1197 (for copysigns (COPYSIGN_ALL)
1198  (simplify
1199   (mult (copysigns@2 @0 @1) @2)
1200   (mult @0 @0)))
1202 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1203 (for ccoss (CCOS CCOSH)
1204  (simplify
1205   (ccoss (negate @0))
1206    (ccoss @0)))
1208 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1209 (for ops (conj negate)
1210  (for cabss (CABS)
1211   (simplify
1212    (cabss (ops @0))
1213    (cabss @0))))
1215 /* Fold (a * (1 << b)) into (a << b)  */
1216 (simplify
1217  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1218   (if (! FLOAT_TYPE_P (type)
1219        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1220    (lshift @0 @2)))
1222 /* Shifts by precision or greater result in zero.  */
1223 (for shift (lshift rshift)
1224  (simplify
1225   (shift @0 uniform_integer_cst_p@1)
1226   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1227        /* Leave arithmetic right shifts of possibly negative values alone.  */
1228        && (TYPE_UNSIGNED (type)
1229            || shift == LSHIFT_EXPR
1230            || tree_expr_nonnegative_p (@0))
1231        /* Use a signed compare to leave negative shift counts alone.  */
1232        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1233                      element_precision (type)))
1234    { build_zero_cst (type); })))
1236 /* Shifts by constants distribute over several binary operations,
1237    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1238 (for op (plus minus)
1239   (simplify
1240     (op (lshift:s @0 @1) (lshift:s @2 @1))
1241     (if (INTEGRAL_TYPE_P (type)
1242          && TYPE_OVERFLOW_WRAPS (type)
1243          && !TYPE_SATURATING (type))
1244       (lshift (op @0 @2) @1))))
1246 (for op (bit_and bit_ior bit_xor)
1247   (simplify
1248     (op (lshift:s @0 @1) (lshift:s @2 @1))
1249     (if (INTEGRAL_TYPE_P (type))
1250       (lshift (op @0 @2) @1)))
1251   (simplify
1252     (op (rshift:s @0 @1) (rshift:s @2 @1))
1253     (if (INTEGRAL_TYPE_P (type))
1254       (rshift (op @0 @2) @1))))
1256 /* Fold (1 << (C - x)) where C = precision(type) - 1
1257    into ((1 << C) >> x). */
1258 (simplify
1259  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1260   (if (INTEGRAL_TYPE_P (type)
1261        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1262        && single_use (@1))
1263    (if (TYPE_UNSIGNED (type))
1264      (rshift (lshift @0 @2) @3)
1265    (with
1266     { tree utype = unsigned_type_for (type); }
1267     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1269 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1270 (simplify
1271  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1272  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1273       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1274   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1275    (bit_and (convert @0)
1276             { wide_int_to_tree (type,
1277                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1279 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1280 (for cst (INTEGER_CST VECTOR_CST)
1281  (simplify
1282   (rshift (negate:s @0) cst@1)
1283    (if (!TYPE_UNSIGNED (type)
1284         && TYPE_OVERFLOW_UNDEFINED (type))
1285     (with { tree stype = TREE_TYPE (@1);
1286             tree bt = truth_type_for (type);
1287             tree zeros = build_zero_cst (type);
1288             tree cst = NULL_TREE; }
1289      (switch
1290       /* Handle scalar case.  */
1291       (if (INTEGRAL_TYPE_P (type)
1292            /* If we apply the rule to the scalar type before vectorization
1293               we will enforce the result of the comparison being a bool
1294               which will require an extra AND on the result that will be
1295               indistinguishable from when the user did actually want 0
1296               or 1 as the result so it can't be removed.  */
1297            && canonicalize_math_after_vectorization_p ()
1298            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1299        (negate (convert (gt @0 { zeros; }))))
1300       /* Handle vector case.  */
1301       (if (VECTOR_INTEGER_TYPE_P (type)
1302            /* First check whether the target has the same mode for vector
1303               comparison results as it's operands do.  */
1304            && TYPE_MODE (bt) == TYPE_MODE (type)
1305            /* Then check to see if the target is able to expand the comparison
1306               with the given type later on, otherwise we may ICE.  */
1307            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1308            && (cst = uniform_integer_cst_p (@1)) != NULL
1309            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1310        (view_convert (gt:bt @0 { zeros; }))))))))
1312 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1313 (simplify
1314  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1315   (if (flag_associative_math
1316        && single_use (@3))
1317    (with
1318     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1319     (if (tem)
1320      (rdiv { tem; } @1)))))
1322 /* Simplify ~X & X as zero.  */
1323 (simplify
1324  (bit_and (convert? @0) (convert? @1))
1325  (with { bool wascmp; }
1326   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1327        && bitwise_inverted_equal_p (@0, @1, wascmp))
1328    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1330 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1331 (simplify
1332   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1333   (if (TYPE_UNSIGNED (type))
1334     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1336 (for bitop (bit_and bit_ior)
1337      cmp (eq ne)
1338  /* PR35691: Transform
1339     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1340     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1341  (simplify
1342   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1343    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1344         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1345         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1346     (cmp (bit_ior @0 (convert @1)) @2)))
1347  /* Transform:
1348     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1349     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1350  (simplify
1351   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1352    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1353         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1354         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1355     (cmp (bit_and @0 (convert @1)) @2))))
1357 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1358 (simplify
1359  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1360   (minus (bit_xor @0 @1) @1))
1361 (simplify
1362  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1363  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1364   (minus (bit_xor @0 @1) @1)))
1366 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1367 (simplify
1368  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1369   (minus @1 (bit_xor @0 @1)))
1371 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1372 (for op (bit_ior bit_xor plus)
1373  (simplify
1374   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1375   (with { bool wascmp0, wascmp1; }
1376    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1377         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1378         && ((!wascmp0 && !wascmp1)
1379             || element_precision (type) == 1))
1380    (bit_xor @0 @1)))))
1382 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1383 (simplify
1384   (bit_ior:c (bit_xor:c @0 @1) @0)
1385   (bit_ior @0 @1))
1387 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1388 (simplify
1389  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1390  @2)
1392 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1393 (simplify
1394  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1395  (bit_not (bit_and @0 @1)))
1397 /* (~a & b) ^ a  -->   (a | b)   */
1398 (simplify
1399  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1400  (bit_ior @0 @1))
1402 /* (a | b) & ~(a ^ b)  -->  a & b  */
1403 (simplify
1404  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1405  (bit_and @0 @1))
1407 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1408 (simplify
1409  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1410  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1411       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1412   (bit_and @0 @1)))
1414 /* a | ~(a ^ b)  -->  a | ~b  */
1415 (simplify
1416  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1417  (bit_ior @0 (bit_not @1)))
1419 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1420 (simplify
1421  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1422  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1423       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1424   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1426 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1427 (simplify
1428  (bit_ior:c @0 (bit_xor:cs @1 @2))
1429  (with { bool wascmp; }
1430  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1431       && (!wascmp || element_precision (type) == 1))
1432   (bit_ior @0 (bit_not @2)))))
1434 /* a & ~(a ^ b)  -->  a & b  */
1435 (simplify
1436  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1437  (bit_and @0 @1))
1439 /* a & (a == b)  -->  a & b (boolean version of the above). */
1440 (simplify
1441  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1442  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1443       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1444   (bit_and @0 @1)))
1446 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1447 (simplify
1448  (bit_and:c @0 (bit_xor:c @1 @2))
1449  (with { bool wascmp; }
1450  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1451       && (!wascmp || element_precision (type) == 1))
1452   (bit_and @0 @2))))
1454 /* (a | b) | (a &^ b)  -->  a | b  */
1455 (for op (bit_and bit_xor)
1456  (simplify
1457   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1458   @2))
1460 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1461 (simplify
1462  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1463  @2)
1465 /* (a & b) | (a == b)  -->  a == b  */
1466 (simplify
1467  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1468  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1469       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1470   @2))
1472 /* ~(~a & b)  -->  a | ~b  */
1473 (simplify
1474  (bit_not (bit_and:cs (bit_not @0) @1))
1475  (bit_ior @0 (bit_not @1)))
1477 /* ~(~a | b) --> a & ~b */
1478 (simplify
1479  (bit_not (bit_ior:cs (bit_not @0) @1))
1480  (bit_and @0 (bit_not @1)))
1482 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1483 (simplify
1484  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1485  (bit_and @3 (bit_not @2)))
1487 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1488 (simplify
1489  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1490  (bit_ior @3 @2))
1492 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1493 (simplify
1494  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1495   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1497 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1498 (simplify
1499  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1500   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1502 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1503 (simplify
1504  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1505  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1506       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1507   (bit_xor @0 @1)))
1509 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1510    ((A & N) + B) & M -> (A + B) & M
1511    Similarly if (N & M) == 0,
1512    ((A | N) + B) & M -> (A + B) & M
1513    and for - instead of + (or unary - instead of +)
1514    and/or ^ instead of |.
1515    If B is constant and (B & M) == 0, fold into A & M.  */
1516 (for op (plus minus)
1517  (for bitop (bit_and bit_ior bit_xor)
1518   (simplify
1519    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1520     (with
1521      { tree pmop[2];
1522        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1523                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1524                                        NULL_TREE, pmop); }
1525      (if (utype)
1526       (convert (bit_and (op (convert:utype { pmop[0]; })
1527                             (convert:utype { pmop[1]; }))
1528                         (convert:utype @2))))))
1529   (simplify
1530    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1531     (with
1532      { tree pmop[2];
1533        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1534                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1535                                        @4, pmop); }
1536      (if (utype)
1537       (convert (bit_and (op (convert:utype { pmop[0]; })
1538                             (convert:utype { pmop[1]; }))
1539                         (convert:utype @2)))))))
1540  (simplify
1541   (bit_and (op:s @0 @1) INTEGER_CST@2)
1542    (with
1543     { tree pmop[2];
1544       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1545                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1546                                       NULL_TREE, NULL_TREE, pmop); }
1547     (if (utype)
1548      (convert (bit_and (op (convert:utype { pmop[0]; })
1549                            (convert:utype { pmop[1]; }))
1550                        (convert:utype @2)))))))
1551 (for bitop (bit_and bit_ior bit_xor)
1552  (simplify
1553   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1554    (with
1555     { tree pmop[2];
1556       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1557                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1558                                       NULL_TREE, NULL_TREE, pmop); }
1559     (if (utype)
1560      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1561                        (convert:utype @1)))))))
1563 /* X % Y is smaller than Y.  */
1564 (for cmp (lt ge)
1565  (simplify
1566   (cmp:c (trunc_mod @0 @1) @1)
1567   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1568    { constant_boolean_node (cmp == LT_EXPR, type); })))
1570 /* x | ~0 -> ~0  */
1571 (simplify
1572  (bit_ior @0 integer_all_onesp@1)
1573  @1)
1575 /* x | 0 -> x  */
1576 (simplify
1577  (bit_ior @0 integer_zerop)
1578  @0)
1580 /* x & 0 -> 0  */
1581 (simplify
1582  (bit_and @0 integer_zerop@1)
1583  @1)
1585 /* ~x | x -> -1 */
1586 /* ~x ^ x -> -1 */
1587 (for op (bit_ior bit_xor)
1588  (simplify
1589   (op (convert? @0) (convert? @1))
1590   (with { bool wascmp; }
1591    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1592         && bitwise_inverted_equal_p (@0, @1, wascmp))
1593     (convert
1594      { wascmp
1595         ? constant_boolean_node (true, type)
1596         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1598 /* x ^ x -> 0 */
1599 (simplify
1600   (bit_xor @0 @0)
1601   { build_zero_cst (type); })
1603 /* Canonicalize X ^ ~0 to ~X.  */
1604 (simplify
1605   (bit_xor @0 integer_all_onesp@1)
1606   (bit_not @0))
1608 /* x & ~0 -> x  */
1609 (simplify
1610  (bit_and @0 integer_all_onesp)
1611   (non_lvalue @0))
1613 /* x & x -> x,  x | x -> x  */
1614 (for bitop (bit_and bit_ior)
1615  (simplify
1616   (bitop @0 @0)
1617   (non_lvalue @0)))
1619 /* x & C -> x if we know that x & ~C == 0.  */
1620 #if GIMPLE
1621 (simplify
1622  (bit_and SSA_NAME@0 INTEGER_CST@1)
1623  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1624       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1625   @0))
1627 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1628 (simplify
1629  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1630  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1631       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1632   @0))
1634 /* x | C -> C if we know that x & ~C == 0.  */
1635 (simplify
1636  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1637  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1638       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1639   @1))
1640 #endif
1642 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1643 (simplify
1644  (bit_not (minus (bit_not @0) @1))
1645  (plus @0 @1))
1646 (simplify
1647  (bit_not (plus:c (bit_not @0) @1))
1648  (minus @0 @1))
1649 /* (~X - ~Y) -> Y - X.  */
1650 (simplify
1651  (minus (bit_not @0) (bit_not @1))
1652   (if (!TYPE_OVERFLOW_SANITIZED (type))
1653    (with { tree utype = unsigned_type_for (type); }
1654     (convert (minus (convert:utype @1) (convert:utype @0))))))
1656 /* ~(X - Y) -> ~X + Y.  */
1657 (simplify
1658  (bit_not (minus:s @0 @1))
1659  (plus (bit_not @0) @1))
1660 (simplify
1661  (bit_not (plus:s @0 INTEGER_CST@1))
1662  (if ((INTEGRAL_TYPE_P (type)
1663        && TYPE_UNSIGNED (type))
1664       || (!TYPE_OVERFLOW_SANITIZED (type)
1665           && may_negate_without_overflow_p (@1)))
1666   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1668 #if GIMPLE
1669 /* ~X + Y -> (Y - X) - 1.  */
1670 (simplify
1671  (plus:c (bit_not @0) @1)
1672   (if (ANY_INTEGRAL_TYPE_P (type)
1673        && TYPE_OVERFLOW_WRAPS (type)
1674        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1675        && !integer_all_onesp (@1))
1676    (plus (minus @1 @0) { build_minus_one_cst (type); })
1677    (if (INTEGRAL_TYPE_P (type)
1678         && TREE_CODE (@1) == INTEGER_CST
1679         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1680                                               SIGNED))
1681     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1682 #endif
1684 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1685 (simplify
1686  (bit_not (rshift:s @0 @1))
1687   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1688    (rshift (bit_not! @0) @1)
1689    /* For logical right shifts, this is possible only if @0 doesn't
1690       have MSB set and the logical right shift is changed into
1691       arithmetic shift.  */
1692    (if (INTEGRAL_TYPE_P (type)
1693         && !wi::neg_p (tree_nonzero_bits (@0)))
1694     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1695      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1697 /* x + (x & 1) -> (x + 1) & ~1 */
1698 (simplify
1699  (plus:c @0 (bit_and:s @0 integer_onep@1))
1700  (bit_and (plus @0 @1) (bit_not @1)))
1702 /* x & ~(x & y) -> x & ~y */
1703 /* x | ~(x | y) -> x | ~y  */
1704 (for bitop (bit_and bit_ior)
1705  (simplify
1706   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1707   (bitop @0 (bit_not @1))))
1709 /* (~x & y) | ~(x | y) -> ~x */
1710 (simplify
1711  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1712  @2)
1714 /* (x | y) ^ (x | ~y) -> ~x */
1715 (simplify
1716  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1717  (bit_not @0))
1719 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1720 (simplify
1721  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1722  (bit_not (bit_xor @0 @1)))
1724 /* (~x | y) ^ (x ^ y) -> x | ~y */
1725 (simplify
1726  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1727  (bit_ior @0 (bit_not @1)))
1729 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1730 (simplify
1731  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1732  (bit_not (bit_and @0 @1)))
1734 /* (x & y) ^ (x | y) -> x ^ y */
1735 (simplify
1736  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1737  (bit_xor @0 @1))
1739 /* (x ^ y) ^ (x | y) -> x & y */
1740 (simplify
1741  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1742  (bit_and @0 @1))
1744 /* (x & y) + (x ^ y) -> x | y */
1745 /* (x & y) | (x ^ y) -> x | y */
1746 /* (x & y) ^ (x ^ y) -> x | y */
1747 (for op (plus bit_ior bit_xor)
1748  (simplify
1749   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1750   (bit_ior @0 @1)))
1752 /* (x & y) + (x | y) -> x + y */
1753 (simplify
1754  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1755  (plus @0 @1))
1757 /* (x + y) - (x | y) -> x & y */
1758 (simplify
1759  (minus (plus @0 @1) (bit_ior @0 @1))
1760  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1761       && !TYPE_SATURATING (type))
1762   (bit_and @0 @1)))
1764 /* (x + y) - (x & y) -> x | y */
1765 (simplify
1766  (minus (plus @0 @1) (bit_and @0 @1))
1767  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1768       && !TYPE_SATURATING (type))
1769   (bit_ior @0 @1)))
1771 /* (x | y) - y -> (x & ~y) */
1772 (simplify
1773  (minus (bit_ior:cs @0 @1) @1)
1774  (bit_and @0 (bit_not @1)))
1776 /* (x | y) - (x ^ y) -> x & y */
1777 (simplify
1778  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1779  (bit_and @0 @1))
1781 /* (x | y) - (x & y) -> x ^ y */
1782 (simplify
1783  (minus (bit_ior @0 @1) (bit_and @0 @1))
1784  (bit_xor @0 @1))
1786 /* (x | y) & ~(x & y) -> x ^ y */
1787 (simplify
1788  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1789  (bit_xor @0 @1))
1791 /* (x | y) & (~x ^ y) -> x & y */
1792 (simplify
1793  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1794  (with { bool wascmp; }
1795   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1796        && (!wascmp || element_precision (type) == 1))
1797    (bit_and @0 @1))))
1799 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1800 (simplify
1801  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1802  (bit_not (bit_xor @0 @1)))
1804 /* (~x | y) ^ (x | ~y) -> x ^ y */
1805 (simplify
1806  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1807  (bit_xor @0 @1))
1809 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1810 (simplify
1811  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1812                               (nop_convert2? (bit_ior @0 @1))))
1813        integer_all_onesp)
1814  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1815       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1816       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1817       && !TYPE_SATURATING (TREE_TYPE (@2)))
1818  (bit_not (convert (bit_xor @0 @1)))))
1819 (simplify
1820  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1821                                integer_all_onesp))
1822        (nop_convert3? (bit_ior @0 @1)))
1823  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1824       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1825       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1826       && !TYPE_SATURATING (TREE_TYPE (@2)))
1827  (bit_not (convert (bit_xor @0 @1)))))
1828 (simplify
1829  (minus (nop_convert1? (bit_and @0 @1))
1830        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1831                                integer_onep)))
1832  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1833       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1834       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1835       && !TYPE_SATURATING (TREE_TYPE (@2)))
1836  (bit_not (convert (bit_xor @0 @1)))))
1838 /* ~x & ~y -> ~(x | y)
1839    ~x | ~y -> ~(x & y) */
1840 (for op (bit_and bit_ior)
1841      rop (bit_ior bit_and)
1842  (simplify
1843   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1844   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1845        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1846    (bit_not (rop (convert @0) (convert @1))))))
1848 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1849    with a constant, and the two constants have no bits in common,
1850    we should treat this as a BIT_IOR_EXPR since this may produce more
1851    simplifications.  */
1852 (for op (bit_xor plus)
1853  (simplify
1854   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1855       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1856   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1857        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1858        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1859    (bit_ior (convert @4) (convert @5)))))
1861 /* (X | Y) ^ X -> Y & ~ X*/
1862 (simplify
1863  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1864  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1865   (convert (bit_and @1 (bit_not @0)))))
1867 /* (~X | Y) ^ X -> ~(X & Y).  */
1868 (simplify
1869  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1870  (if (bitwise_equal_p (@0, @2))
1871   (convert (bit_not (bit_and @0 (convert @1))))))
1873 /* Convert ~X ^ ~Y to X ^ Y.  */
1874 (simplify
1875  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1876  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1877       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1878   (bit_xor (convert @0) (convert @1))))
1880 /* Convert ~X ^ C to X ^ ~C.  */
1881 (simplify
1882  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1883  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1884   (bit_xor (convert @0) (bit_not @1))))
1886 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1887 (for opo (bit_and bit_xor)
1888      opi (bit_xor bit_and)
1889  (simplify
1890   (opo:c (opi:cs @0 @1) @1)
1891   (bit_and (bit_not @0) @1)))
1893 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1894    operands are another bit-wise operation with a common input.  If so,
1895    distribute the bit operations to save an operation and possibly two if
1896    constants are involved.  For example, convert
1897      (A | B) & (A | C) into A | (B & C)
1898    Further simplification will occur if B and C are constants.  */
1899 (for op (bit_and bit_ior bit_xor)
1900      rop (bit_ior bit_and bit_and)
1901  (simplify
1902   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1903   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1904        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1905    (rop (convert @0) (op (convert @1) (convert @2))))))
1907 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1908 /* (X & Y) & Y -> X & Y
1909    (X | Y) | Y -> X | Y  */
1910 (for op (bit_and bit_ior)
1911  (simplify
1912   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1913   @2))
1914 /* (X ^ Y) ^ Y -> X  */
1915 (simplify
1916  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1917  (convert @0))
1919 /* (X & ~Y) & Y -> 0 */
1920 (simplify
1921  (bit_and:c (bit_and @0 @1) @2)
1922  (with { bool wascmp; }
1923   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1924        || bitwise_inverted_equal_p (@1, @2, wascmp))
1925    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1926 /* (X | ~Y) | Y -> -1 */
1927 (simplify
1928  (bit_ior:c (bit_ior @0 @1) @2)
1929  (with { bool wascmp; }
1930   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1931         || bitwise_inverted_equal_p (@1, @2, wascmp))
1932        && (!wascmp || element_precision (type) == 1))
1933    { build_all_ones_cst (TREE_TYPE (@0)); })))
1935 /* (X & Y) & (X & Z) -> (X & Y) & Z
1936    (X | Y) | (X | Z) -> (X | Y) | Z  */
1937 (for op (bit_and bit_ior)
1938  (simplify
1939   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1940   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1941        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1942    (if (single_use (@5) && single_use (@6))
1943     (op @3 (convert @2))
1944     (if (single_use (@3) && single_use (@4))
1945      (op (convert @1) @5))))))
1946 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1947 (simplify
1948  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1949  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1950       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1951   (bit_xor (convert @1) (convert @2))))
1953 /* Convert abs (abs (X)) into abs (X).
1954    also absu (absu (X)) into absu (X).  */
1955 (simplify
1956  (abs (abs@1 @0))
1957  @1)
1959 (simplify
1960  (absu (convert@2 (absu@1 @0)))
1961  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1962   @1))
1964 /* Convert abs[u] (-X) -> abs[u] (X).  */
1965 (simplify
1966  (abs (negate @0))
1967  (abs @0))
1969 (simplify
1970  (absu (negate @0))
1971  (absu @0))
1973 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1974 (simplify
1975  (abs tree_expr_nonnegative_p@0)
1976  @0)
1978 (simplify
1979  (absu tree_expr_nonnegative_p@0)
1980  (convert @0))
1982 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1983 (simplify
1984  (mult:c (nop_convert1?
1985           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1986                     integer_onep))
1987          (nop_convert3? @0))
1988  (if (INTEGRAL_TYPE_P (type)
1989       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1990       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1991   (if (TYPE_UNSIGNED (type))
1992    (absu @0)
1993    (abs @0)
1994   )
1998 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1999 (match negate_expr_p
2000  INTEGER_CST
2001  (if ((INTEGRAL_TYPE_P (type)
2002        && TYPE_UNSIGNED (type))
2003       || (!TYPE_OVERFLOW_SANITIZED (type)
2004           && may_negate_without_overflow_p (t)))))
2005 (match negate_expr_p
2006  FIXED_CST)
2007 (match negate_expr_p
2008  (negate @0)
2009  (if (!TYPE_OVERFLOW_SANITIZED (type))))
2010 (match negate_expr_p
2011  REAL_CST
2012  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2013 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2014    ways.  */
2015 (match negate_expr_p
2016  VECTOR_CST
2017  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2018 (match negate_expr_p
2019  (minus @0 @1)
2020  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2021       || (FLOAT_TYPE_P (type)
2022           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2023           && !HONOR_SIGNED_ZEROS (type)))))
2025 /* (-A) * (-B) -> A * B  */
2026 (simplify
2027  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2028   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2029        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2030    (mult (convert @0) (convert (negate @1)))))
2032 /* -(A + B) -> (-B) - A.  */
2033 (simplify
2034  (negate (plus:c @0 negate_expr_p@1))
2035  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2036       && !HONOR_SIGNED_ZEROS (type))
2037   (minus (negate @1) @0)))
2039 /* -(A - B) -> B - A.  */
2040 (simplify
2041  (negate (minus @0 @1))
2042  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2043       || (FLOAT_TYPE_P (type)
2044           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2045           && !HONOR_SIGNED_ZEROS (type)))
2046   (minus @1 @0)))
2047 (simplify
2048  (negate (pointer_diff @0 @1))
2049  (if (TYPE_OVERFLOW_UNDEFINED (type))
2050   (pointer_diff @1 @0)))
2052 /* A - B -> A + (-B) if B is easily negatable.  */
2053 (simplify
2054  (minus @0 negate_expr_p@1)
2055  (if (!FIXED_POINT_TYPE_P (type))
2056  (plus @0 (negate @1))))
2058 /* 1 - a is a ^ 1 if a had a bool range. */
2059 /* This is only enabled for gimple as sometimes
2060    cfun is not set for the function which contains
2061    the SSA_NAME (e.g. while IPA passes are happening,
2062    fold might be called).  */
2063 (simplify
2064  (minus integer_onep@0 SSA_NAME@1)
2065   (if (INTEGRAL_TYPE_P (type)
2066        && ssa_name_has_boolean_range (@1))
2067    (bit_xor @1 @0)))
2069 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2070 (simplify
2071  (negate (mult:c@0 @1 negate_expr_p@2))
2072  (if (! TYPE_UNSIGNED (type)
2073       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2074       && single_use (@0))
2075   (mult @1 (negate @2))))
2077 (simplify
2078  (negate (rdiv@0 @1 negate_expr_p@2))
2079  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2080       && single_use (@0))
2081   (rdiv @1 (negate @2))))
2083 (simplify
2084  (negate (rdiv@0 negate_expr_p@1 @2))
2085  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2086       && single_use (@0))
2087   (rdiv (negate @1) @2)))
2089 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2090 (simplify
2091  (negate (convert? (rshift @0 INTEGER_CST@1)))
2092  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2093       && wi::to_wide (@1) == element_precision (type) - 1)
2094   (with { tree stype = TREE_TYPE (@0);
2095           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2096                                              : unsigned_type_for (stype); }
2097    (if (VECTOR_TYPE_P (type))
2098     (view_convert (rshift (view_convert:ntype @0) @1))
2099     (convert (rshift (convert:ntype @0) @1))))))
2101 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2102    when profitable.
2103    For bitwise binary operations apply operand conversions to the
2104    binary operation result instead of to the operands.  This allows
2105    to combine successive conversions and bitwise binary operations.
2106    We combine the above two cases by using a conditional convert.  */
2107 (for bitop (bit_and bit_ior bit_xor)
2108  (simplify
2109   (bitop (convert@2 @0) (convert?@3 @1))
2110   (if (((TREE_CODE (@1) == INTEGER_CST
2111          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2112          && (int_fits_type_p (@1, TREE_TYPE (@0))
2113              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2114         || types_match (@0, @1))
2115        && !POINTER_TYPE_P (TREE_TYPE (@0))
2116        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2117        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2118        /* ???  This transform conflicts with fold-const.cc doing
2119           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2120           constants (if x has signed type, the sign bit cannot be set
2121           in c).  This folds extension into the BIT_AND_EXPR.
2122           Restrict it to GIMPLE to avoid endless recursions.  */
2123        && (bitop != BIT_AND_EXPR || GIMPLE)
2124        && (/* That's a good idea if the conversion widens the operand, thus
2125               after hoisting the conversion the operation will be narrower.
2126               It is also a good if the conversion is a nop as moves the
2127               conversion to one side; allowing for combining of the conversions.  */
2128            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2129            /* The conversion check for being a nop can only be done at the gimple
2130               level as fold_binary has some re-association code which can conflict
2131               with this if there is a "constant" which is not a full INTEGER_CST.  */
2132            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2133            /* It's also a good idea if the conversion is to a non-integer
2134               mode.  */
2135            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2136            /* Or if the precision of TO is not the same as the precision
2137               of its mode.  */
2138            || !type_has_mode_precision_p (type)
2139            /* In GIMPLE, getting rid of 2 conversions for one new results
2140               in smaller IL.  */
2141            || (GIMPLE
2142                && TREE_CODE (@1) != INTEGER_CST
2143                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2144                && single_use (@2)
2145                && single_use (@3))))
2146    (convert (bitop @0 (convert @1)))))
2147  /* In GIMPLE, getting rid of 2 conversions for one new results
2148     in smaller IL.  */
2149  (simplify
2150   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2151   (if (GIMPLE
2152        && TREE_CODE (@1) != INTEGER_CST
2153        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2154        && types_match (type, @0)
2155        && !POINTER_TYPE_P (TREE_TYPE (@0))
2156        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2157    (bitop @0 (convert @1)))))
2159 (for bitop (bit_and bit_ior)
2160      rbitop (bit_ior bit_and)
2161   /* (x | y) & x -> x */
2162   /* (x & y) | x -> x */
2163  (simplify
2164   (bitop:c (rbitop:c @0 @1) @0)
2165   @0)
2166  /* (~x | y) & x -> x & y */
2167  /* (~x & y) | x -> x | y */
2168  (simplify
2169   (bitop:c (rbitop:c @2 @1) @0)
2170   (with { bool wascmp; }
2171    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2172         && (!wascmp || element_precision (type) == 1))
2173     (bitop @0 @1))))
2174   /* (x | y) & (x & z) -> (x & z) */
2175   /* (x & y) | (x | z) -> (x | z) */
2176  (simplify
2177   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2178   @3)
2179  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2180  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2181  (simplify
2182   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2183   (bitop @0 @3))
2184  /* x & ~(y | x) -> 0 */
2185  /* x | ~(y & x) -> -1 */
2186  (simplify
2187   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2188   (if (bitop == BIT_AND_EXPR)
2189    { build_zero_cst (type); }
2190    { build_minus_one_cst (type); })))
2192 /* ((x | y) & z) | x -> (z & y) | x
2193    ((x ^ y) & z) | x -> (z & y) | x  */
2194 (for op (bit_ior bit_xor)
2195  (simplify
2196   (bit_ior:c (nop_convert1?:s
2197                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2198   (if (bitwise_equal_p (@0, @3))
2199    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2201 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2202 (simplify
2203   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2204   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2206 /* Combine successive equal operations with constants.  */
2207 (for bitop (bit_and bit_ior bit_xor)
2208  (simplify
2209   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2210   (if (!CONSTANT_CLASS_P (@0))
2211    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2212       folded to a constant.  */
2213    (bitop @0 (bitop! @1 @2))
2214    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2215       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2216       the values involved are such that the operation can't be decided at
2217       compile time.  Try folding one of @0 or @1 with @2 to see whether
2218       that combination can be decided at compile time.
2220       Keep the existing form if both folds fail, to avoid endless
2221       oscillation.  */
2222    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2223     (if (cst1)
2224      (bitop @1 { cst1; })
2225      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2226       (if (cst2)
2227        (bitop @0 { cst2; }))))))))
2229 /* Try simple folding for X op !X, and X op X with the help
2230    of the truth_valued_p and logical_inverted_value predicates.  */
2231 (match truth_valued_p
2232  @0
2233  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2234 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2235  (match truth_valued_p
2236   (op @0 @1)))
2237 (match truth_valued_p
2238   (truth_not @0))
2240 (match (logical_inverted_value @0)
2241  (truth_not @0))
2242 (match (logical_inverted_value @0)
2243  (bit_not truth_valued_p@0))
2244 (match (logical_inverted_value @0)
2245  (eq @0 integer_zerop))
2246 (match (logical_inverted_value @0)
2247  (ne truth_valued_p@0 integer_truep))
2248 (match (logical_inverted_value @0)
2249  (bit_xor truth_valued_p@0 integer_truep))
2251 /* X & !X -> 0.  */
2252 (simplify
2253  (bit_and:c @0 (logical_inverted_value @0))
2254  { build_zero_cst (type); })
2255 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2256 (for op (bit_ior bit_xor)
2257  (simplify
2258   (op:c truth_valued_p@0 (logical_inverted_value @0))
2259   { constant_boolean_node (true, type); }))
2260 /* X ==/!= !X is false/true.  */
2261 (for op (eq ne)
2262  (simplify
2263   (op:c truth_valued_p@0 (logical_inverted_value @0))
2264   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2266 /* ~~x -> x */
2267 (simplify
2268   (bit_not (bit_not @0))
2269   @0)
2271 /* zero_one_valued_p will match when a value is known to be either
2272    0 or 1 including constants 0 or 1.
2273    Signed 1-bits includes -1 so they cannot match here. */
2274 (match zero_one_valued_p
2275  @0
2276  (if (INTEGRAL_TYPE_P (type)
2277       && (TYPE_UNSIGNED (type)
2278           || TYPE_PRECISION (type) > 1)
2279       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2280 (match zero_one_valued_p
2281  truth_valued_p@0
2282  (if (INTEGRAL_TYPE_P (type)
2283       && (TYPE_UNSIGNED (type)
2284           || TYPE_PRECISION (type) > 1))))
2286 /* (a&1) is always [0,1] too. This is useful again when
2287    the range is not known. */
2288 /* Note this can't be recursive due to VN handling of equivalents,
2289    VN and would cause an infinite recursion. */
2290 (match zero_one_valued_p
2291  (bit_and:c@0 @1 integer_onep)
2292  (if (INTEGRAL_TYPE_P (type))))
2294 /* A conversion from an zero_one_valued_p is still a [0,1].
2295    This is useful when the range of a variable is not known */
2296 /* Note this matches can't be recursive because of the way VN handles
2297    nop conversions being equivalent and then recursive between them. */
2298 (match zero_one_valued_p
2299  (convert@0 @1)
2300  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2301       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2302           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2303       && INTEGRAL_TYPE_P (type)
2304       && (TYPE_UNSIGNED (type)
2305           || TYPE_PRECISION (type) > 1)
2306       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2308 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2309 (simplify
2310  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2311  (if (INTEGRAL_TYPE_P (type))
2312   (bit_and @0 @1)))
2314 (for cmp (tcc_comparison)
2315      icmp (inverted_tcc_comparison)
2316  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2317  (simplify
2318   (bit_ior
2319    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2320    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2321     (if (INTEGRAL_TYPE_P (type)
2322          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2323          /* The scalar version has to be canonicalized after vectorization
2324             because it makes unconditional loads conditional ones, which
2325             means we lose vectorization because the loads may trap.  */
2326          && canonicalize_math_after_vectorization_p ())
2327      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2329  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2330     canonicalized further and we recognize the conditional form:
2331     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2332  (simplify
2333   (bit_ior
2334    (cond (cmp@0  @01 @02) @3 zerop)
2335    (cond (icmp@4 @01 @02) @5 zerop))
2336     (if (INTEGRAL_TYPE_P (type)
2337          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2338          /* The scalar version has to be canonicalized after vectorization
2339             because it makes unconditional loads conditional ones, which
2340             means we lose vectorization because the loads may trap.  */
2341          && canonicalize_math_after_vectorization_p ())
2342     (cond @0 @3 @5)))
2344  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2345     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2346  (simplify
2347   (bit_ior
2348    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2349    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2350     (if (integer_zerop (@5)
2351          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2352      (switch
2353       (if (integer_onep (@4))
2354        (bit_and (vec_cond @0 @2 @3) @4))
2355         (if (integer_minus_onep (@4))
2356          (vec_cond @0 @2 @3)))
2357     (if (integer_zerop (@4)
2358          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2359      (switch
2360       (if (integer_onep (@5))
2361        (bit_and (vec_cond @0 @3 @2) @5))
2362       (if (integer_minus_onep (@5))
2363        (vec_cond @0 @3 @2))))))
2365  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2366     into a < b ? d : c.  */
2367  (simplify
2368   (bit_ior
2369    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2370    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2371   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2372    (vec_cond @0 @2 @3))))
2374 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2375 (simplify
2376  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2377  (if (INTEGRAL_TYPE_P (type)
2378       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2379       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2380       /* Sign extending of the neg or a truncation of the neg
2381          is needed. */
2382       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2383           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2384   (mult (convert @0) @1)))
2386 /* Narrow integer multiplication by a zero_one_valued_p operand.
2387    Multiplication by [0,1] is guaranteed not to overflow.  */
2388 (simplify
2389  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2390  (if (INTEGRAL_TYPE_P (type)
2391       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2392       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2393   (mult (convert @1) (convert @2))))
2395 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2396    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2397    as some targets (such as x86's SSE) may return zero for larger C.  */
2398 (simplify
2399   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2400   (if (tree_fits_shwi_p (@1)
2401        && tree_to_shwi (@1) > 0
2402        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2403     (convert @0)))
2405 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2406    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2407    as some targets (such as x86's SSE) may return zero for larger C.  */
2408 (simplify
2409   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2410   (if (tree_fits_shwi_p (@1)
2411        && tree_to_shwi (@1) > 0
2412        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2413     (eq @0 @2)))
2415 /* Convert ~ (-A) to A - 1.  */
2416 (simplify
2417  (bit_not (convert? (negate @0)))
2418  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2419       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2420   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2422 /* Convert - (~A) to A + 1.  */
2423 (simplify
2424  (negate (nop_convert? (bit_not @0)))
2425  (plus (view_convert @0) { build_each_one_cst (type); }))
2427 /* (a & b) ^ (a == b) -> !(a | b) */
2428 /* (a & b) == (a ^ b) -> !(a | b) */
2429 (for first_op (bit_xor eq)
2430      second_op (eq bit_xor)
2431  (simplify
2432   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2433     (bit_not (bit_ior @0 @1))))
2435 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2436 (simplify
2437  (bit_not (convert? (minus @0 integer_each_onep)))
2438  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2439       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2440   (convert (negate @0))))
2441 (simplify
2442  (bit_not (convert? (plus @0 integer_all_onesp)))
2443  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2444       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2445   (convert (negate @0))))
2447 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2448 (simplify
2449  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2450  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2451   (convert (bit_xor @0 (bit_not @1)))))
2452 (simplify
2453  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2454  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2455   (convert (bit_xor @0 @1))))
2457 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2458 (simplify
2459  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2460  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2461   (bit_not (bit_xor (view_convert @0) @1))))
2463 /* ~(a ^ b) is a == b for truth valued a and b.  */
2464 (simplify
2465  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2466  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2467       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2468   (convert (eq @0 @1))))
2470 /* (~a) == b is a ^ b for truth valued a and b.  */
2471 (simplify
2472  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2473  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2474       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2475   (convert (bit_xor @0 @1))))
2477 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2478 (simplify
2479  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2480  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2482 /* Fold A - (A & B) into ~B & A.  */
2483 (simplify
2484  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2485  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2486       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2487   (convert (bit_and (bit_not @1) @0))))
2489 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2490 (if (!canonicalize_math_p ())
2491  (for cmp (tcc_comparison)
2492   (simplify
2493    (mult:c (convert (cmp@0 @1 @2)) @3)
2494    (if (INTEGRAL_TYPE_P (type)
2495         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2496      (cond @0 @3 { build_zero_cst (type); })))
2497 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2498   (simplify
2499    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2500    (if (INTEGRAL_TYPE_P (type)
2501         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2502      (cond @0 @3 { build_zero_cst (type); })))
2506 /* For integral types with undefined overflow and C != 0 fold
2507    x * C EQ/NE y * C into x EQ/NE y.  */
2508 (for cmp (eq ne)
2509  (simplify
2510   (cmp (mult:c @0 @1) (mult:c @2 @1))
2511   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2512        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2513        && tree_expr_nonzero_p (@1))
2514    (cmp @0 @2))))
2516 /* For integral types with wrapping overflow and C odd fold
2517    x * C EQ/NE y * C into x EQ/NE y.  */
2518 (for cmp (eq ne)
2519  (simplify
2520   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2521   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2522        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2523        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2524    (cmp @0 @2))))
2526 /* For integral types with undefined overflow and C != 0 fold
2527    x * C RELOP y * C into:
2529    x RELOP y for nonnegative C
2530    y RELOP x for negative C  */
2531 (for cmp (lt gt le ge)
2532  (simplify
2533   (cmp (mult:c @0 @1) (mult:c @2 @1))
2534   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2535        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2536    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2537     (cmp @0 @2)
2538    (if (TREE_CODE (@1) == INTEGER_CST
2539         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2540     (cmp @2 @0))))))
2542 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2543 (for cmp (le gt)
2544      icmp (gt le)
2545  (simplify
2546   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2547    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2548         && TYPE_UNSIGNED (TREE_TYPE (@0))
2549         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2550         && (wi::to_wide (@2)
2551             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2552     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2553      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2555 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2556 (for cmp (simple_comparison)
2557  (simplify
2558   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2559   (if (element_precision (@3) >= element_precision (@0)
2560        && types_match (@0, @1))
2561    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2562     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2563      (cmp @1 @0)
2564      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2565       (with
2566        {
2567         tree utype = unsigned_type_for (TREE_TYPE (@0));
2568        }
2569        (cmp (convert:utype @1) (convert:utype @0)))))
2570     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2571      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2572       (cmp @0 @1)
2573       (with
2574        {
2575         tree utype = unsigned_type_for (TREE_TYPE (@0));
2576        }
2577        (cmp (convert:utype @0) (convert:utype @1)))))))))
2579 /* X / C1 op C2 into a simple range test.  */
2580 (for cmp (simple_comparison)
2581  (simplify
2582   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2583   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2584        && integer_nonzerop (@1)
2585        && !TREE_OVERFLOW (@1)
2586        && !TREE_OVERFLOW (@2))
2587    (with { tree lo, hi; bool neg_overflow;
2588            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2589                                                    &neg_overflow); }
2590     (switch
2591      (if (code == LT_EXPR || code == GE_EXPR)
2592        (if (TREE_OVERFLOW (lo))
2593         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2594         (if (code == LT_EXPR)
2595          (lt @0 { lo; })
2596          (ge @0 { lo; }))))
2597      (if (code == LE_EXPR || code == GT_EXPR)
2598        (if (TREE_OVERFLOW (hi))
2599         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2600         (if (code == LE_EXPR)
2601          (le @0 { hi; })
2602          (gt @0 { hi; }))))
2603      (if (!lo && !hi)
2604       { build_int_cst (type, code == NE_EXPR); })
2605      (if (code == EQ_EXPR && !hi)
2606       (ge @0 { lo; }))
2607      (if (code == EQ_EXPR && !lo)
2608       (le @0 { hi; }))
2609      (if (code == NE_EXPR && !hi)
2610       (lt @0 { lo; }))
2611      (if (code == NE_EXPR && !lo)
2612       (gt @0 { hi; }))
2613      (if (GENERIC)
2614       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2615                            lo, hi); })
2616      (with
2617       {
2618         tree etype = range_check_type (TREE_TYPE (@0));
2619         if (etype)
2620           {
2621             hi = fold_convert (etype, hi);
2622             lo = fold_convert (etype, lo);
2623             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2624           }
2625       }
2626       (if (etype && hi && !TREE_OVERFLOW (hi))
2627        (if (code == EQ_EXPR)
2628         (le (minus (convert:etype @0) { lo; }) { hi; })
2629         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2631 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2632 (for op (lt le ge gt)
2633  (simplify
2634   (op (plus:c @0 @2) (plus:c @1 @2))
2635   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2636        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2637    (op @0 @1))))
2639 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2640    when C is an unsigned integer constant with only the MSB set, and X and
2641    Y have types of equal or lower integer conversion rank than C's.  */
2642 (for op (lt le ge gt)
2643  (simplify
2644   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2645   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2646        && TYPE_UNSIGNED (TREE_TYPE (@0))
2647        && wi::only_sign_bit_p (wi::to_wide (@0)))
2648    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2649     (op (convert:stype @1) (convert:stype @2))))))
2651 /* For equality and subtraction, this is also true with wrapping overflow.  */
2652 (for op (eq ne minus)
2653  (simplify
2654   (op (plus:c @0 @2) (plus:c @1 @2))
2655   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2656        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2657            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2658    (op @0 @1))))
2659 /* And similar for pointers.  */
2660 (for op (eq ne)
2661  (simplify
2662   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2663   (op @1 @2)))
2664 (simplify
2665  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2666  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2667   (convert (minus @1 @2))))
2669 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2670 (for op (lt le ge gt)
2671  (simplify
2672   (op (minus @0 @2) (minus @1 @2))
2673   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2674        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2675    (op @0 @1))))
2676 /* For equality and subtraction, this is also true with wrapping overflow.  */
2677 (for op (eq ne minus)
2678  (simplify
2679   (op (minus @0 @2) (minus @1 @2))
2680   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2681        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2682            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2683    (op @0 @1))))
2684 /* And for pointers...  */
2685 (for op (simple_comparison)
2686  (simplify
2687   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2688   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2689    (op @0 @1))))
2690 (simplify
2691  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2692  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2693       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2694   (pointer_diff @0 @1)))
2696 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2697 (for op (lt le ge gt)
2698  (simplify
2699   (op (minus @2 @0) (minus @2 @1))
2700   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2701        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2702    (op @1 @0))))
2703 /* For equality and subtraction, this is also true with wrapping overflow.  */
2704 (for op (eq ne minus)
2705  (simplify
2706   (op (minus @2 @0) (minus @2 @1))
2707   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2708        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2709            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2710    (op @1 @0))))
2711 /* And for pointers...  */
2712 (for op (simple_comparison)
2713  (simplify
2714   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2715   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2716    (op @1 @0))))
2717 (simplify
2718  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2719  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2720       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2721   (pointer_diff @1 @0)))
2723 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2724 (for op (lt le gt ge)
2725  (simplify
2726   (op:c (plus:c@2 @0 @1) @1)
2727   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2728        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2729        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2730        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2731    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2732 /* For equality, this is also true with wrapping overflow.  */
2733 (for op (eq ne)
2734  (simplify
2735   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2736   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2737        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2738            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2739        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2740        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2741        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2742    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2743  (simplify
2744   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2745   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2746        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2747        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2748    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2750 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2751 (for neeq (ne eq)
2752  (simplify
2753   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2754    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2755     (if (ptr_difference_const (@0, @2, &diff))
2756      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2757  (simplify
2758   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2759    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2760     (if (ptr_difference_const (@0, @2, &diff))
2761      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2763 /* X - Y < X is the same as Y > 0 when there is no overflow.
2764    For equality, this is also true with wrapping overflow.  */
2765 (for op (simple_comparison)
2766  (simplify
2767   (op:c @0 (minus@2 @0 @1))
2768   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2769        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2770            || ((op == EQ_EXPR || op == NE_EXPR)
2771                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2772        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2773    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2775 /* Transform:
2776    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2777    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2778 (for cmp (eq ne)
2779      ocmp (lt ge)
2780  (simplify
2781   (cmp (trunc_div @0 @1) integer_zerop)
2782   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2783        /* Complex ==/!= is allowed, but not </>=.  */
2784        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2785        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2786    (ocmp @0 @1))))
2788 /* X == C - X can never be true if C is odd.  */
2789 (for cmp (eq ne)
2790  (simplify
2791   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2792   (if (TREE_INT_CST_LOW (@1) & 1)
2793    { constant_boolean_node (cmp == NE_EXPR, type); })))
2796    U & N <= U  -> true
2797    U & N >  U  -> false
2798    U needs to be non-negative.
2800    U | N <  U  -> false
2801    U | N >= U  -> true
2802    U and N needs to be non-negative
2804    U | N <  U  -> true
2805    U | N >= U  -> false
2806    U needs to be non-negative and N needs to be a negative constant.
2807    */
2808 (for cmp   (lt      ge      le      gt     )
2809      bitop (bit_ior bit_ior bit_and bit_and)
2810  (simplify
2811   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2812   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2813    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2814     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2815     /* The sign is opposite now so the comparison is swapped around. */
2816     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2817      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2819 /* Arguments on which one can call get_nonzero_bits to get the bits
2820    possibly set.  */
2821 (match with_possible_nonzero_bits
2822  INTEGER_CST@0)
2823 (match with_possible_nonzero_bits
2824  SSA_NAME@0
2825  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2826 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2827 (match (with_possible_nonzero_bits2 @0)
2828  with_possible_nonzero_bits@0)
2829 (match (with_possible_nonzero_bits2 @0)
2830  (bit_and:c with_possible_nonzero_bits@0 @2))
2832 /* Same for bits that are known to be set, but we do not have
2833    an equivalent to get_nonzero_bits yet.  */
2834 (match (with_certain_nonzero_bits2 @0)
2835  INTEGER_CST@0)
2836 (match (with_certain_nonzero_bits2 @0)
2837  (bit_ior @1 INTEGER_CST@0))
2839 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2840 (for cmp (eq ne)
2841  (simplify
2842   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2843   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2844    { constant_boolean_node (cmp == NE_EXPR, type); })))
2846 /* ((X inner_op C0) outer_op C1)
2847    With X being a tree where value_range has reasoned certain bits to always be
2848    zero throughout its computed value range,
2849    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2850    where zero_mask has 1's for all bits that are sure to be 0 in
2851    and 0's otherwise.
2852    if (inner_op == '^') C0 &= ~C1;
2853    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2854    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2856 (for inner_op (bit_ior bit_xor)
2857      outer_op (bit_xor bit_ior)
2858 (simplify
2859  (outer_op
2860   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2861  (with
2862   {
2863     bool fail = false;
2864     wide_int zero_mask_not;
2865     wide_int C0;
2866     wide_int cst_emit;
2868     if (TREE_CODE (@2) == SSA_NAME)
2869       zero_mask_not = get_nonzero_bits (@2);
2870     else
2871       fail = true;
2873     if (inner_op == BIT_XOR_EXPR)
2874       {
2875         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2876         cst_emit = C0 | wi::to_wide (@1);
2877       }
2878     else
2879       {
2880         C0 = wi::to_wide (@0);
2881         cst_emit = C0 ^ wi::to_wide (@1);
2882       }
2883   }
2884   (if (!fail && (C0 & zero_mask_not) == 0)
2885    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2886    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2887     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2889 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2890 (simplify
2891   (pointer_plus (pointer_plus:s @0 @1) @3)
2892   (pointer_plus @0 (plus @1 @3)))
2893 #if GENERIC
2894 (simplify
2895   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2896   (convert:type (pointer_plus @0 (plus @1 @3))))
2897 #endif
2899 /* Pattern match
2900      tem1 = (long) ptr1;
2901      tem2 = (long) ptr2;
2902      tem3 = tem2 - tem1;
2903      tem4 = (unsigned long) tem3;
2904      tem5 = ptr1 + tem4;
2905    and produce
2906      tem5 = ptr2;  */
2907 (simplify
2908   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2909   /* Conditionally look through a sign-changing conversion.  */
2910   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2911        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2912             || (GENERIC && type == TREE_TYPE (@1))))
2913    @1))
2914 (simplify
2915   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2916   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2917    (convert @1)))
2919 /* Pattern match
2920      tem = (sizetype) ptr;
2921      tem = tem & algn;
2922      tem = -tem;
2923      ... = ptr p+ tem;
2924    and produce the simpler and easier to analyze with respect to alignment
2925      ... = ptr & ~algn;  */
2926 (simplify
2927   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2928   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2929    (bit_and @0 { algn; })))
2931 /* Try folding difference of addresses.  */
2932 (simplify
2933  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2934  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2935   (with { poly_int64 diff; }
2936    (if (ptr_difference_const (@0, @1, &diff))
2937     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2938 (simplify
2939  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2940  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2941   (with { poly_int64 diff; }
2942    (if (ptr_difference_const (@0, @1, &diff))
2943     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2944 (simplify
2945  (minus (convert ADDR_EXPR@0) (convert @1))
2946  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2947   (with { poly_int64 diff; }
2948    (if (ptr_difference_const (@0, @1, &diff))
2949     { build_int_cst_type (type, diff); }))))
2950 (simplify
2951  (minus (convert @0) (convert ADDR_EXPR@1))
2952  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2953   (with { poly_int64 diff; }
2954    (if (ptr_difference_const (@0, @1, &diff))
2955     { build_int_cst_type (type, diff); }))))
2956 (simplify
2957  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2958  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2959       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2960   (with { poly_int64 diff; }
2961    (if (ptr_difference_const (@0, @1, &diff))
2962     { build_int_cst_type (type, diff); }))))
2963 (simplify
2964  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2965  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2966       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2967   (with { poly_int64 diff; }
2968    (if (ptr_difference_const (@0, @1, &diff))
2969     { build_int_cst_type (type, diff); }))))
2971 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2972 (simplify
2973  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2974  (with { poly_int64 diff; }
2975    (if (ptr_difference_const (@0, @2, &diff))
2976     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2977 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2978 (simplify
2979  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2980  (with { poly_int64 diff; }
2981    (if (ptr_difference_const (@0, @2, &diff))
2982     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2983 (simplify
2984  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2985  (with { poly_int64 diff; }
2986    (if (ptr_difference_const (@0, @1, &diff))
2987     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2989 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2990 (simplify
2991  (convert (pointer_diff @0 INTEGER_CST@1))
2992  (if (POINTER_TYPE_P (type))
2993   { build_fold_addr_expr_with_type
2994       (build2 (MEM_REF, char_type_node, @0,
2995                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2996                type); }))
2998 /* If arg0 is derived from the address of an object or function, we may
2999    be able to fold this expression using the object or function's
3000    alignment.  */
3001 (simplify
3002  (bit_and (convert? @0) INTEGER_CST@1)
3003  (if (POINTER_TYPE_P (TREE_TYPE (@0))
3004       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3005   (with
3006    {
3007      unsigned int align;
3008      unsigned HOST_WIDE_INT bitpos;
3009      get_pointer_alignment_1 (@0, &align, &bitpos);
3010    }
3011    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3012     { wide_int_to_tree (type, (wi::to_wide (@1)
3013                                & (bitpos / BITS_PER_UNIT))); }))))
3015 (match min_value
3016  uniform_integer_cst_p
3017  (with {
3018    tree int_cst = uniform_integer_cst_p (t);
3019    tree inner_type = TREE_TYPE (int_cst);
3020   }
3021   (if ((INTEGRAL_TYPE_P (inner_type)
3022         || POINTER_TYPE_P (inner_type))
3023        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3025 (match max_value
3026  uniform_integer_cst_p
3027  (with {
3028    tree int_cst = uniform_integer_cst_p (t);
3029    tree itype = TREE_TYPE (int_cst);
3030   }
3031  (if ((INTEGRAL_TYPE_P (itype)
3032        || POINTER_TYPE_P (itype))
3033       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3035 /* x >  y  &&  x != XXX_MIN  -->  x > y
3036    x >  y  &&  x == XXX_MIN  -->  false . */
3037 (for eqne (eq ne)
3038  (simplify
3039   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3040    (switch
3041     (if (eqne == EQ_EXPR)
3042      { constant_boolean_node (false, type); })
3043     (if (eqne == NE_EXPR)
3044      @2)
3045     )))
3047 /* x <  y  &&  x != XXX_MAX  -->  x < y
3048    x <  y  &&  x == XXX_MAX  -->  false.  */
3049 (for eqne (eq ne)
3050  (simplify
3051   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3052    (switch
3053     (if (eqne == EQ_EXPR)
3054      { constant_boolean_node (false, type); })
3055     (if (eqne == NE_EXPR)
3056      @2)
3057     )))
3059 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3060 (simplify
3061  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3062   @2)
3064 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3065 (simplify
3066  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3067   @2)
3069 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3070 (simplify
3071  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3072   @2)
3074 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3075 (simplify
3076  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3077   { constant_boolean_node (true, type); })
3079 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3080 (simplify
3081  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3082   @2)
3084 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3085 (simplify
3086  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3087   @2)
3089 /* x >=  y  ||  x != XXX_MAX   -->  true
3090    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3091 (for eqne (eq ne)
3092  (simplify
3093   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3094    (switch
3095     (if (eqne == EQ_EXPR)
3096      @2)
3097     (if (eqne == NE_EXPR)
3098      { constant_boolean_node (true, type); }))))
3100 /* y == XXX_MIN || x < y --> x <= y - 1 */
3101 (simplify
3102  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3103   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3104        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3105   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3107 /* y != XXX_MIN && x >= y --> x > y - 1 */
3108 (simplify
3109  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3110   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3111        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3112   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3114 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3115    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3116 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3117    Similarly for (X != Y).  */
3119 (for code1 (eq ne)
3120  (for code2 (eq ne lt gt le ge)
3121   (simplify
3122    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3123    (if ((TREE_CODE (@1) == INTEGER_CST
3124          && TREE_CODE (@2) == INTEGER_CST)
3125         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3126              || POINTER_TYPE_P (TREE_TYPE (@1)))
3127             && bitwise_equal_p (@1, @2)))
3128     (with
3129      {
3130       bool one_before = false;
3131       bool one_after = false;
3132       int cmp = 0;
3133       bool allbits = true;
3134       if (TREE_CODE (@1) == INTEGER_CST
3135           && TREE_CODE (@2) == INTEGER_CST)
3136         {
3137           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3138           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3139           auto t2 = wi::to_wide (@2);
3140           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3141           if (cmp < 0
3142               && t1 == t2 - 1)
3143             one_before = true;
3144           if (cmp > 0
3145               && t1 == t2 + 1)
3146             one_after = true;
3147         }
3148       bool val;
3149       switch (code2)
3150          {
3151         case EQ_EXPR: val = (cmp == 0); break;
3152         case NE_EXPR: val = (cmp != 0); break;
3153         case LT_EXPR: val = (cmp < 0); break;
3154         case GT_EXPR: val = (cmp > 0); break;
3155         case LE_EXPR: val = (cmp <= 0); break;
3156         case GE_EXPR: val = (cmp >= 0); break;
3157         default: gcc_unreachable ();
3158         }
3159      }
3160      (switch
3161       (if (code1 == EQ_EXPR && val) @3)
3162       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3163       (if (code1 == NE_EXPR && !val && allbits) @4)
3164       (if (code1 == NE_EXPR
3165            && code2 == GE_EXPR
3166            && cmp == 0
3167            && allbits)
3168        (gt @c0 (convert @1)))
3169       (if (code1 == NE_EXPR
3170            && code2 == LE_EXPR
3171            && cmp == 0
3172            && allbits)
3173        (lt @c0 (convert @1)))
3174       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3175       (if (code1 == NE_EXPR
3176            && code2 == GT_EXPR
3177            && one_after
3178            && allbits)
3179        (gt @c0 (convert @1)))
3180       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3181       (if (code1 == NE_EXPR
3182            && code2 == LT_EXPR
3183            && one_before
3184            && allbits)
3185        (lt @c0 (convert @1)))
3186      )
3187     )
3188    )
3189   )
3193 /* Convert (X OP1 CST1) && (X OP2 CST2).
3194    Convert (X OP1 Y) && (X OP2 Y).  */
3196 (for code1 (lt le gt ge)
3197  (for code2 (lt le gt ge)
3198   (simplify
3199   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3200   (if ((TREE_CODE (@1) == INTEGER_CST
3201         && TREE_CODE (@2) == INTEGER_CST)
3202        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3203             || POINTER_TYPE_P (TREE_TYPE (@1)))
3204            && operand_equal_p (@1, @2)))
3205    (with
3206     {
3207      int cmp = 0;
3208      if (TREE_CODE (@1) == INTEGER_CST
3209          && TREE_CODE (@2) == INTEGER_CST)
3210        cmp = tree_int_cst_compare (@1, @2);
3211     }
3212     (switch
3213      /* Choose the more restrictive of two < or <= comparisons.  */
3214      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3215           && (code2 == LT_EXPR || code2 == LE_EXPR))
3216       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3217        @3
3218        @4))
3219      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3220      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3221           && (code2 == GT_EXPR || code2 == GE_EXPR))
3222       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3223        @3
3224        @4))
3225      /* Check for singleton ranges.  */
3226      (if (cmp == 0
3227           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3228             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3229       (eq @0 @1))
3230      /* Check for disjoint ranges.  */
3231      (if (cmp <= 0
3232           && (code1 == LT_EXPR || code1 == LE_EXPR)
3233           && (code2 == GT_EXPR || code2 == GE_EXPR))
3234       { constant_boolean_node (false, type); })
3235      (if (cmp >= 0
3236           && (code1 == GT_EXPR || code1 == GE_EXPR)
3237           && (code2 == LT_EXPR || code2 == LE_EXPR))
3238       { constant_boolean_node (false, type); })
3239      ))))))
3241 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3242    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3243 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3244    Similarly for (X != Y).  */
3246 (for code1 (eq ne)
3247  (for code2 (eq ne lt gt le ge)
3248   (simplify
3249    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3250    (if ((TREE_CODE (@1) == INTEGER_CST
3251          && TREE_CODE (@2) == INTEGER_CST)
3252         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3253             || POINTER_TYPE_P (TREE_TYPE (@1)))
3254             && bitwise_equal_p (@1, @2)))
3255     (with
3256      {
3257       bool one_before = false;
3258       bool one_after = false;
3259       int cmp = 0;
3260       bool allbits = true;
3261       if (TREE_CODE (@1) == INTEGER_CST
3262           && TREE_CODE (@2) == INTEGER_CST)
3263         {
3264           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3265           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3266           auto t2 = wi::to_wide (@2);
3267           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3268           if (cmp < 0
3269               && t1 == t2 - 1)
3270             one_before = true;
3271           if (cmp > 0
3272               && t1 == t2 + 1)
3273             one_after = true;
3274         }
3275       bool val;
3276       switch (code2)
3277         {
3278         case EQ_EXPR: val = (cmp == 0); break;
3279         case NE_EXPR: val = (cmp != 0); break;
3280         case LT_EXPR: val = (cmp < 0); break;
3281         case GT_EXPR: val = (cmp > 0); break;
3282         case LE_EXPR: val = (cmp <= 0); break;
3283         case GE_EXPR: val = (cmp >= 0); break;
3284         default: gcc_unreachable ();
3285         }
3286      }
3287      (switch
3288       (if (code1 == EQ_EXPR && val) @4)
3289       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3290       (if (code1 == NE_EXPR && !val && allbits) @3)
3291       (if (code1 == EQ_EXPR
3292            && code2 == GT_EXPR
3293            && cmp == 0
3294            && allbits)
3295        (ge @c0 @2))
3296       (if (code1 == EQ_EXPR
3297            && code2 == LT_EXPR
3298            && cmp == 0
3299            && allbits)
3300        (le @c0 @2))
3301       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3302       (if (code1 == EQ_EXPR
3303            && code2 == GE_EXPR
3304            && one_before
3305            && allbits)
3306        (ge @c0 (convert @1)))
3307       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3308       (if (code1 == EQ_EXPR
3309            && code2 == LE_EXPR
3310            && one_after
3311            && allbits)
3312        (le @c0 (convert @1)))
3313      )
3314     )
3315    )
3316   )
3320 /* Convert (X OP1 CST1) || (X OP2 CST2).
3321    Convert (X OP1 Y)    || (X OP2 Y).  */
3323 (for code1 (lt le gt ge)
3324  (for code2 (lt le gt ge)
3325   (simplify
3326   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3327   (if ((TREE_CODE (@1) == INTEGER_CST
3328         && TREE_CODE (@2) == INTEGER_CST)
3329        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3330             || POINTER_TYPE_P (TREE_TYPE (@1)))
3331            && operand_equal_p (@1, @2)))
3332    (with
3333     {
3334      int cmp = 0;
3335      if (TREE_CODE (@1) == INTEGER_CST
3336          && TREE_CODE (@2) == INTEGER_CST)
3337        cmp = tree_int_cst_compare (@1, @2);
3338     }
3339     (switch
3340      /* Choose the more restrictive of two < or <= comparisons.  */
3341      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3342           && (code2 == LT_EXPR || code2 == LE_EXPR))
3343       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3344        @4
3345        @3))
3346      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3347      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3348           && (code2 == GT_EXPR || code2 == GE_EXPR))
3349       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3350        @4
3351        @3))
3352      /* Check for singleton ranges.  */
3353      (if (cmp == 0
3354           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3355               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3356       (ne @0 @2))
3357      /* Check for disjoint ranges.  */
3358      (if (cmp >= 0
3359           && (code1 == LT_EXPR || code1 == LE_EXPR)
3360           && (code2 == GT_EXPR || code2 == GE_EXPR))
3361       { constant_boolean_node (true, type); })
3362      (if (cmp <= 0
3363           && (code1 == GT_EXPR || code1 == GE_EXPR)
3364           && (code2 == LT_EXPR || code2 == LE_EXPR))
3365       { constant_boolean_node (true, type); })
3366      ))))))
3368 /* Optimize (a CMP b) ^ (a CMP b)  */
3369 /* Optimize (a CMP b) != (a CMP b)  */
3370 (for op (bit_xor ne)
3371  (for cmp1 (lt lt lt le le le)
3372       cmp2 (gt eq ne ge eq ne)
3373       rcmp (ne le gt ne lt ge)
3374   (simplify
3375    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3376    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3377     (rcmp @0 @1)))))
3379 /* Optimize (a CMP b) == (a CMP b)  */
3380 (for cmp1 (lt lt lt le le le)
3381      cmp2 (gt eq ne ge eq ne)
3382      rcmp (eq gt le eq ge lt)
3383  (simplify
3384   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3385   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3386     (rcmp @0 @1))))
3388 /* (type)([0,1]@a != 0) -> (type)a
3389    (type)([0,1]@a == 1) -> (type)a
3390    (type)([0,1]@a == 0) -> a ^ 1
3391    (type)([0,1]@a != 1) -> a ^ 1.  */
3392 (for eqne (eq ne)
3393  (simplify
3394   (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3395   (if ((integer_zerop (@1) || integer_onep (@1)))
3396    (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3397     (convert @0)
3398     /* Only do this if the types match as (type)(a == 0) is
3399        canonical form normally, while `a ^ 1` is canonical when
3400        there is no type change. */
3401     (if (types_match (type, TREE_TYPE (@0)))
3402      (bit_xor @0 { build_one_cst (type); } ))))))
3404 /* We can't reassociate at all for saturating types.  */
3405 (if (!TYPE_SATURATING (type))
3407  /* Contract negates.  */
3408  /* A + (-B) -> A - B */
3409  (simplify
3410   (plus:c @0 (convert? (negate @1)))
3411   /* Apply STRIP_NOPS on the negate.  */
3412   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3413        && !TYPE_OVERFLOW_SANITIZED (type))
3414    (with
3415     {
3416      tree t1 = type;
3417      if (INTEGRAL_TYPE_P (type)
3418          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3419        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3420     }
3421     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3422  /* A - (-B) -> A + B */
3423  (simplify
3424   (minus @0 (convert? (negate @1)))
3425   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3426        && !TYPE_OVERFLOW_SANITIZED (type))
3427    (with
3428     {
3429      tree t1 = type;
3430      if (INTEGRAL_TYPE_P (type)
3431          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3432        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3433     }
3434     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3435  /* -(T)(-A) -> (T)A
3436     Sign-extension is ok except for INT_MIN, which thankfully cannot
3437     happen without overflow.  */
3438  (simplify
3439   (negate (convert (negate @1)))
3440   (if (INTEGRAL_TYPE_P (type)
3441        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3442            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3443                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3444        && !TYPE_OVERFLOW_SANITIZED (type)
3445        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3446    (convert @1)))
3447  (simplify
3448   (negate (convert negate_expr_p@1))
3449   (if (SCALAR_FLOAT_TYPE_P (type)
3450        && ((DECIMAL_FLOAT_TYPE_P (type)
3451             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3452             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3453            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3454    (convert (negate @1))))
3455  (simplify
3456   (negate (nop_convert? (negate @1)))
3457   (if (!TYPE_OVERFLOW_SANITIZED (type)
3458        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3459    (view_convert @1)))
3461  /* We can't reassociate floating-point unless -fassociative-math
3462     or fixed-point plus or minus because of saturation to +-Inf.  */
3463  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3464       && !FIXED_POINT_TYPE_P (type))
3466   /* Match patterns that allow contracting a plus-minus pair
3467      irrespective of overflow issues.  */
3468   /* (A +- B) - A       ->  +- B */
3469   /* (A +- B) -+ B      ->  A */
3470   /* A - (A +- B)       -> -+ B */
3471   /* A +- (B -+ A)      ->  +- B */
3472   (simplify
3473    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3474    (view_convert @1))
3475   (simplify
3476    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3477    (if (!ANY_INTEGRAL_TYPE_P (type)
3478         || TYPE_OVERFLOW_WRAPS (type))
3479    (negate (view_convert @1))
3480    (view_convert (negate @1))))
3481   (simplify
3482    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3483    (view_convert @0))
3484   (simplify
3485    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3486     (if (!ANY_INTEGRAL_TYPE_P (type)
3487          || TYPE_OVERFLOW_WRAPS (type))
3488      (negate (view_convert @1))
3489      (view_convert (negate @1))))
3490   (simplify
3491    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3492    (view_convert @1))
3493   /* (A +- B) + (C - A)   -> C +- B */
3494   /* (A +  B) - (A - C)   -> B + C */
3495   /* More cases are handled with comparisons.  */
3496   (simplify
3497    (plus:c (plus:c @0 @1) (minus @2 @0))
3498    (plus @2 @1))
3499   (simplify
3500    (plus:c (minus @0 @1) (minus @2 @0))
3501    (minus @2 @1))
3502   (simplify
3503    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3504    (if (TYPE_OVERFLOW_UNDEFINED (type)
3505         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3506     (pointer_diff @2 @1)))
3507   (simplify
3508    (minus (plus:c @0 @1) (minus @0 @2))
3509    (plus @1 @2))
3511   /* (A +- CST1) +- CST2 -> A + CST3
3512      Use view_convert because it is safe for vectors and equivalent for
3513      scalars.  */
3514   (for outer_op (plus minus)
3515    (for inner_op (plus minus)
3516         neg_inner_op (minus plus)
3517     (simplify
3518      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3519                CONSTANT_CLASS_P@2)
3520      /* If one of the types wraps, use that one.  */
3521      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3522       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3523          forever if something doesn't simplify into a constant.  */
3524       (if (!CONSTANT_CLASS_P (@0))
3525        (if (outer_op == PLUS_EXPR)
3526         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3527         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3528       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3529            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3530        (if (outer_op == PLUS_EXPR)
3531         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3532         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3533        /* If the constant operation overflows we cannot do the transform
3534           directly as we would introduce undefined overflow, for example
3535           with (a - 1) + INT_MIN.  */
3536        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3537         (with { tree cst = const_binop (outer_op == inner_op
3538                                         ? PLUS_EXPR : MINUS_EXPR,
3539                                         type, @1, @2); }
3540          (if (cst)
3541           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3542            (inner_op @0 { cst; } )
3543            /* X+INT_MAX+1 is X-INT_MIN.  */
3544            (if (INTEGRAL_TYPE_P (type)
3545                 && wi::to_wide (cst) == wi::min_value (type))
3546             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3547             /* Last resort, use some unsigned type.  */
3548             (with { tree utype = unsigned_type_for (type); }
3549              (if (utype)
3550               (view_convert (inner_op
3551                              (view_convert:utype @0)
3552                              (view_convert:utype
3553                               { TREE_OVERFLOW (cst)
3554                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3556   /* (CST1 - A) +- CST2 -> CST3 - A  */
3557   (for outer_op (plus minus)
3558    (simplify
3559     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3560     /* If one of the types wraps, use that one.  */
3561     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3562      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3563         forever if something doesn't simplify into a constant.  */
3564      (if (!CONSTANT_CLASS_P (@0))
3565       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3566      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3567           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3568       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3569       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3570        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3571         (if (cst && !TREE_OVERFLOW (cst))
3572          (minus { cst; } @0))))))))
3574   /* CST1 - (CST2 - A) -> CST3 + A
3575      Use view_convert because it is safe for vectors and equivalent for
3576      scalars.  */
3577   (simplify
3578    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3579    /* If one of the types wraps, use that one.  */
3580    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3581     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3582       forever if something doesn't simplify into a constant.  */
3583     (if (!CONSTANT_CLASS_P (@0))
3584      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3585     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3586          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3587      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3588      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3589       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3590        (if (cst && !TREE_OVERFLOW (cst))
3591         (plus { cst; } @0)))))))
3593 /* ((T)(A)) + CST -> (T)(A + CST)  */
3594 #if GIMPLE
3595   (simplify
3596    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3597     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3598          && TREE_CODE (type) == INTEGER_TYPE
3599          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3600          && int_fits_type_p (@1, TREE_TYPE (@0)))
3601      /* Perform binary operation inside the cast if the constant fits
3602         and (A + CST)'s range does not overflow.  */
3603      (with
3604       {
3605         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3606                           max_ovf = wi::OVF_OVERFLOW;
3607         tree inner_type = TREE_TYPE (@0);
3609         wide_int w1
3610           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3611                             TYPE_SIGN (inner_type));
3613         value_range vr;
3614         if (get_global_range_query ()->range_of_expr (vr, @0)
3615             && !vr.varying_p () && !vr.undefined_p ())
3616           {
3617             wide_int wmin0 = vr.lower_bound ();
3618             wide_int wmax0 = vr.upper_bound ();
3619             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3620             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3621           }
3622       }
3623      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3624       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3625      )))
3626 #endif
3628 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3629 #if GIMPLE
3630   (for op (plus minus)
3631    (simplify
3632     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3633      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3634           && TREE_CODE (type) == INTEGER_TYPE
3635           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3636           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3637           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3638           && TYPE_OVERFLOW_WRAPS (type))
3639        (plus (convert @0) (op @2 (convert @1))))))
3640 #endif
3642 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3643    to a simple value.  */
3644   (for op (plus minus)
3645    (simplify
3646     (op (convert @0) (convert @1))
3647      (if (INTEGRAL_TYPE_P (type)
3648           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3649           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3650           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3651           && !TYPE_OVERFLOW_TRAPS (type)
3652           && !TYPE_OVERFLOW_SANITIZED (type))
3653       (convert (op! @0 @1)))))
3655   /* ~A + A -> -1 */
3656   (simplify
3657    (plus:c (convert? (bit_not @0)) (convert? @0))
3658    (if (!TYPE_OVERFLOW_TRAPS (type))
3659     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3661   /* ~A + 1 -> -A */
3662   (simplify
3663    (plus (convert? (bit_not @0)) integer_each_onep)
3664    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3665     (negate (convert @0))))
3667   /* -A - 1 -> ~A */
3668   (simplify
3669    (minus (convert? (negate @0)) integer_each_onep)
3670    (if (!TYPE_OVERFLOW_TRAPS (type)
3671         && TREE_CODE (type) != COMPLEX_TYPE
3672         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3673     (bit_not (convert @0))))
3675   /* -1 - A -> ~A */
3676   (simplify
3677    (minus integer_all_onesp @0)
3678    (if (TREE_CODE (type) != COMPLEX_TYPE)
3679     (bit_not @0)))
3681   /* (T)(P + A) - (T)P -> (T) A */
3682   (simplify
3683    (minus (convert (plus:c @@0 @1))
3684     (convert? @0))
3685    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3686         /* For integer types, if A has a smaller type
3687            than T the result depends on the possible
3688            overflow in P + A.
3689            E.g. T=size_t, A=(unsigned)429497295, P>0.
3690            However, if an overflow in P + A would cause
3691            undefined behavior, we can assume that there
3692            is no overflow.  */
3693         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3694             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3695     (convert @1)))
3696   (simplify
3697    (minus (convert (pointer_plus @@0 @1))
3698     (convert @0))
3699    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3700         /* For pointer types, if the conversion of A to the
3701            final type requires a sign- or zero-extension,
3702            then we have to punt - it is not defined which
3703            one is correct.  */
3704         || (POINTER_TYPE_P (TREE_TYPE (@0))
3705             && TREE_CODE (@1) == INTEGER_CST
3706             && tree_int_cst_sign_bit (@1) == 0))
3707     (convert @1)))
3708    (simplify
3709     (pointer_diff (pointer_plus @@0 @1) @0)
3710     /* The second argument of pointer_plus must be interpreted as signed, and
3711        thus sign-extended if necessary.  */
3712     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3713      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3714         second arg is unsigned even when we need to consider it as signed,
3715         we don't want to diagnose overflow here.  */
3716      (convert (view_convert:stype @1))))
3718   /* (T)P - (T)(P + A) -> -(T) A */
3719   (simplify
3720    (minus (convert? @0)
3721     (convert (plus:c @@0 @1)))
3722    (if (INTEGRAL_TYPE_P (type)
3723         && TYPE_OVERFLOW_UNDEFINED (type)
3724         /* For integer literals, using an intermediate unsigned type to avoid
3725            an overflow at run time is counter-productive because it introduces
3726            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3727            the result, which may be problematic in GENERIC for some front-ends:
3728              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3729            so we use the direct path for them.  */
3730         && TREE_CODE (@1) != INTEGER_CST
3731         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3732     (with { tree utype = unsigned_type_for (type); }
3733      (convert (negate (convert:utype @1))))
3734     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3735          /* For integer types, if A has a smaller type
3736             than T the result depends on the possible
3737             overflow in P + A.
3738             E.g. T=size_t, A=(unsigned)429497295, P>0.
3739             However, if an overflow in P + A would cause
3740             undefined behavior, we can assume that there
3741             is no overflow.  */
3742          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3743              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3744      (negate (convert @1)))))
3745   (simplify
3746    (minus (convert @0)
3747     (convert (pointer_plus @@0 @1)))
3748    (if (INTEGRAL_TYPE_P (type)
3749         && TYPE_OVERFLOW_UNDEFINED (type)
3750         /* See above the rationale for this condition.  */
3751         && TREE_CODE (@1) != INTEGER_CST
3752         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3753     (with { tree utype = unsigned_type_for (type); }
3754      (convert (negate (convert:utype @1))))
3755     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3756          /* For pointer types, if the conversion of A to the
3757             final type requires a sign- or zero-extension,
3758             then we have to punt - it is not defined which
3759             one is correct.  */
3760          || (POINTER_TYPE_P (TREE_TYPE (@0))
3761              && TREE_CODE (@1) == INTEGER_CST
3762              && tree_int_cst_sign_bit (@1) == 0))
3763      (negate (convert @1)))))
3764    (simplify
3765     (pointer_diff @0 (pointer_plus @@0 @1))
3766     /* The second argument of pointer_plus must be interpreted as signed, and
3767        thus sign-extended if necessary.  */
3768     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3769      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3770         second arg is unsigned even when we need to consider it as signed,
3771         we don't want to diagnose overflow here.  */
3772      (negate (convert (view_convert:stype @1)))))
3774   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3775   (simplify
3776    (minus (convert (plus:c @@0 @1))
3777     (convert (plus:c @0 @2)))
3778    (if (INTEGRAL_TYPE_P (type)
3779         && TYPE_OVERFLOW_UNDEFINED (type)
3780         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3781         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3782     (with { tree utype = unsigned_type_for (type); }
3783      (convert (minus (convert:utype @1) (convert:utype @2))))
3784     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3785           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3786          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3787              /* For integer types, if A has a smaller type
3788                 than T the result depends on the possible
3789                 overflow in P + A.
3790                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3791                 However, if an overflow in P + A would cause
3792                 undefined behavior, we can assume that there
3793                 is no overflow.  */
3794              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3795                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3796                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3797                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3798      (minus (convert @1) (convert @2)))))
3799   (simplify
3800    (minus (convert (pointer_plus @@0 @1))
3801     (convert (pointer_plus @0 @2)))
3802    (if (INTEGRAL_TYPE_P (type)
3803         && TYPE_OVERFLOW_UNDEFINED (type)
3804         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3805     (with { tree utype = unsigned_type_for (type); }
3806      (convert (minus (convert:utype @1) (convert:utype @2))))
3807     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3808          /* For pointer types, if the conversion of A to the
3809             final type requires a sign- or zero-extension,
3810             then we have to punt - it is not defined which
3811             one is correct.  */
3812          || (POINTER_TYPE_P (TREE_TYPE (@0))
3813              && TREE_CODE (@1) == INTEGER_CST
3814              && tree_int_cst_sign_bit (@1) == 0
3815              && TREE_CODE (@2) == INTEGER_CST
3816              && tree_int_cst_sign_bit (@2) == 0))
3817      (minus (convert @1) (convert @2)))))
3818    (simplify
3819     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3820      (pointer_diff @0 @1))
3821    (simplify
3822     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3823     /* The second argument of pointer_plus must be interpreted as signed, and
3824        thus sign-extended if necessary.  */
3825     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3826      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3827         second arg is unsigned even when we need to consider it as signed,
3828         we don't want to diagnose overflow here.  */
3829      (minus (convert (view_convert:stype @1))
3830             (convert (view_convert:stype @2)))))))
3832 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3833     Modeled after fold_plusminus_mult_expr.  */
3834 (if (!TYPE_SATURATING (type)
3835      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3836  (for plusminus (plus minus)
3837   (simplify
3838    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3839    (if (!ANY_INTEGRAL_TYPE_P (type)
3840         || TYPE_OVERFLOW_WRAPS (type)
3841         || (INTEGRAL_TYPE_P (type)
3842             && tree_expr_nonzero_p (@0)
3843             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3844     (if (single_use (@3) || single_use (@4))
3845      /* If @1 +- @2 is constant require a hard single-use on either
3846         original operand (but not on both).  */
3847      (mult (plusminus @1 @2) @0)
3848      (mult! (plusminus @1 @2) @0)
3849   )))
3850   /* We cannot generate constant 1 for fract.  */
3851   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3852    (simplify
3853     (plusminus @0 (mult:c@3 @0 @2))
3854     (if ((!ANY_INTEGRAL_TYPE_P (type)
3855           || TYPE_OVERFLOW_WRAPS (type)
3856           /* For @0 + @0*@2 this transformation would introduce UB
3857              (where there was none before) for @0 in [-1,0] and @2 max.
3858              For @0 - @0*@2 this transformation would introduce UB
3859              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3860           || (INTEGRAL_TYPE_P (type)
3861               && ((tree_expr_nonzero_p (@0)
3862                    && expr_not_equal_to (@0,
3863                                 wi::minus_one (TYPE_PRECISION (type))))
3864                   || (plusminus == PLUS_EXPR
3865                       ? expr_not_equal_to (@2,
3866                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3867                       /* Let's ignore the @0 -1 and @2 min case.  */
3868                       : (expr_not_equal_to (@2,
3869                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3870                          && expr_not_equal_to (@2,
3871                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3872                                 + 1))))))
3873          && single_use (@3))
3874      (mult (plusminus { build_one_cst (type); } @2) @0)))
3875    (simplify
3876     (plusminus (mult:c@3 @0 @2) @0)
3877     (if ((!ANY_INTEGRAL_TYPE_P (type)
3878           || TYPE_OVERFLOW_WRAPS (type)
3879           /* For @0*@2 + @0 this transformation would introduce UB
3880              (where there was none before) for @0 in [-1,0] and @2 max.
3881              For @0*@2 - @0 this transformation would introduce UB
3882              for @0 0 and @2 min.  */
3883           || (INTEGRAL_TYPE_P (type)
3884               && ((tree_expr_nonzero_p (@0)
3885                    && (plusminus == MINUS_EXPR
3886                        || expr_not_equal_to (@0,
3887                                 wi::minus_one (TYPE_PRECISION (type)))))
3888                   || expr_not_equal_to (@2,
3889                         (plusminus == PLUS_EXPR
3890                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3891                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3892          && single_use (@3))
3893      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3895 #if GIMPLE
3896 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3897    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3898 (simplify
3899  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3900   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3901        && tree_fits_uhwi_p (@1)
3902        && tree_to_uhwi (@1) < element_precision (type)
3903        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3904            || optab_handler (smul_optab,
3905                              TYPE_MODE (type)) != CODE_FOR_nothing))
3906    (with { tree t = type;
3907            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3908            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3909                                              element_precision (type));
3910            w += 1;
3911            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3912                                         : t, w);
3913            cst = build_uniform_cst (t, cst); }
3914     (convert (mult (convert:t @0) { cst; })))))
3915 (simplify
3916  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3917   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3918        && tree_fits_uhwi_p (@1)
3919        && tree_to_uhwi (@1) < element_precision (type)
3920        && tree_fits_uhwi_p (@2)
3921        && tree_to_uhwi (@2) < element_precision (type)
3922        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3923            || optab_handler (smul_optab,
3924                              TYPE_MODE (type)) != CODE_FOR_nothing))
3925    (with { tree t = type;
3926            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3927            unsigned int prec = element_precision (type);
3928            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3929            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3930            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3931                                         : t, w);
3932            cst = build_uniform_cst (t, cst); }
3933     (convert (mult (convert:t @0) { cst; })))))
3934 #endif
3936 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3937    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3938    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3939 (for op (bit_ior bit_xor)
3940  (simplify
3941   (op (mult:s@0 @1 INTEGER_CST@2)
3942       (mult:s@3 @1 INTEGER_CST@4))
3943   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3944        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3945    (mult @1
3946          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3947  (simplify
3948   (op:c (mult:s@0 @1 INTEGER_CST@2)
3949         (lshift:s@3 @1 INTEGER_CST@4))
3950   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3951        && tree_int_cst_sgn (@4) > 0
3952        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3953    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3954            wide_int c = wi::add (wi::to_wide (@2),
3955                                  wi::lshift (wone, wi::to_wide (@4))); }
3956     (mult @1 { wide_int_to_tree (type, c); }))))
3957  (simplify
3958   (op:c (mult:s@0 @1 INTEGER_CST@2)
3959         @1)
3960   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3961        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3962    (mult @1
3963          { wide_int_to_tree (type,
3964                              wi::add (wi::to_wide (@2), 1)); })))
3965  (simplify
3966   (op (lshift:s@0 @1 INTEGER_CST@2)
3967       (lshift:s@3 @1 INTEGER_CST@4))
3968   (if (INTEGRAL_TYPE_P (type)
3969        && tree_int_cst_sgn (@2) > 0
3970        && tree_int_cst_sgn (@4) > 0
3971        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3972    (with { tree t = type;
3973            if (!TYPE_OVERFLOW_WRAPS (t))
3974              t = unsigned_type_for (t);
3975            wide_int wone = wi::one (TYPE_PRECISION (t));
3976            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3977                                  wi::lshift (wone, wi::to_wide (@4))); }
3978     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3979  (simplify
3980   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3981         @1)
3982   (if (INTEGRAL_TYPE_P (type)
3983        && tree_int_cst_sgn (@2) > 0
3984        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3985    (with { tree t = type;
3986            if (!TYPE_OVERFLOW_WRAPS (t))
3987              t = unsigned_type_for (t);
3988            wide_int wone = wi::one (TYPE_PRECISION (t));
3989            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3990     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3992 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3994 (for minmax (min max)
3995  (simplify
3996   (minmax @0 @0)
3997   @0)
3998 /* max(max(x,y),x) -> max(x,y)  */
3999  (simplify
4000   (minmax:c (minmax:c@2 @0 @1) @0)
4001   @2))
4002 /* For fmin() and fmax(), skip folding when both are sNaN.  */
4003 (for minmax (FMIN_ALL FMAX_ALL)
4004  (simplify
4005   (minmax @0 @0)
4006   (if (!tree_expr_maybe_signaling_nan_p (@0))
4007     @0)))
4008 /* min(max(x,y),y) -> y.  */
4009 (simplify
4010  (min:c (max:c @0 @1) @1)
4011  @1)
4012 /* max(min(x,y),y) -> y.  */
4013 (simplify
4014  (max:c (min:c @0 @1) @1)
4015  @1)
4016 /* max(a,-a) -> abs(a).  */
4017 (simplify
4018  (max:c @0 (negate @0))
4019  (if (TREE_CODE (type) != COMPLEX_TYPE
4020       && (! ANY_INTEGRAL_TYPE_P (type)
4021           || TYPE_OVERFLOW_UNDEFINED (type)))
4022   (abs @0)))
4023 /* min(a,-a) -> -abs(a).  */
4024 (simplify
4025  (min:c @0 (negate @0))
4026  (if (TREE_CODE (type) != COMPLEX_TYPE
4027       && (! ANY_INTEGRAL_TYPE_P (type)
4028           || TYPE_OVERFLOW_UNDEFINED (type)))
4029   (negate (abs @0))))
4030 (simplify
4031  (min @0 @1)
4032  (switch
4033   (if (INTEGRAL_TYPE_P (type)
4034        && TYPE_MIN_VALUE (type)
4035        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4036    @1)
4037   (if (INTEGRAL_TYPE_P (type)
4038        && TYPE_MAX_VALUE (type)
4039        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4040    @0)))
4041 (simplify
4042  (max @0 @1)
4043  (switch
4044   (if (INTEGRAL_TYPE_P (type)
4045        && TYPE_MAX_VALUE (type)
4046        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4047    @1)
4048   (if (INTEGRAL_TYPE_P (type)
4049        && TYPE_MIN_VALUE (type)
4050        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4051    @0)))
4053 /* max (a, a + CST) -> a + CST where CST is positive.  */
4054 /* max (a, a + CST) -> a where CST is negative.  */
4055 (simplify
4056  (max:c @0 (plus@2 @0 INTEGER_CST@1))
4057   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4058    (if (tree_int_cst_sgn (@1) > 0)
4059     @2
4060     @0)))
4062 /* min (a, a + CST) -> a where CST is positive.  */
4063 /* min (a, a + CST) -> a + CST where CST is negative. */
4064 (simplify
4065  (min:c @0 (plus@2 @0 INTEGER_CST@1))
4066   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4067    (if (tree_int_cst_sgn (@1) > 0)
4068     @0
4069     @2)))
4071 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4072    the addresses are known to be less, equal or greater.  */
4073 (for minmax (min max)
4074      cmp (lt gt)
4075  (simplify
4076   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4077   (with
4078    {
4079      poly_int64 off0, off1;
4080      tree base0, base1;
4081      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4082                                   off0, off1, GENERIC);
4083    }
4084    (if (equal == 1)
4085     (if (minmax == MIN_EXPR)
4086      (if (known_le (off0, off1))
4087       @2
4088       (if (known_gt (off0, off1))
4089        @3))
4090      (if (known_ge (off0, off1))
4091       @2
4092       (if (known_lt (off0, off1))
4093        @3)))))))
4095 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4096    and the outer convert demotes the expression back to x's type.  */
4097 (for minmax (min max)
4098  (simplify
4099   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4100   (if (INTEGRAL_TYPE_P (type)
4101        && types_match (@1, type) && int_fits_type_p (@2, type)
4102        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4103        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4104    (minmax @1 (convert @2)))))
4106 (for minmax (FMIN_ALL FMAX_ALL)
4107  /* If either argument is NaN and other one is not sNaN, return the other
4108     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4109  (simplify
4110   (minmax:c @0 REAL_CST@1)
4111    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4112        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4113        && !tree_expr_maybe_signaling_nan_p (@0))
4114    @0)))
4115 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4116    functions to return the numeric arg if the other one is NaN.
4117    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4118    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4119    worry about it either.  */
4120 (if (flag_finite_math_only)
4121  (simplify
4122   (FMIN_ALL @0 @1)
4123   (min @0 @1))
4124  (simplify
4125   (FMAX_ALL @0 @1)
4126   (max @0 @1)))
4127 /* min (-A, -B) -> -max (A, B)  */
4128 (for minmax (min max FMIN_ALL FMAX_ALL)
4129      maxmin (max min FMAX_ALL FMIN_ALL)
4130  (simplify
4131   (minmax (negate:s@2 @0) (negate:s@3 @1))
4132   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4133        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4134            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4135    (negate (maxmin @0 @1)))))
4136 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4137    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4138 (for minmax (min max)
4139  maxmin (max min)
4140  (simplify
4141   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4142   (bit_not (maxmin @0 @1)))
4143 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4144 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4145  (simplify
4146   (bit_not (minmax:cs (bit_not @0) @1))
4147   (maxmin @0 (bit_not @1))))
4149 /* MIN (X, Y) == X -> X <= Y  */
4150 /* MIN (X, Y) < X -> X > Y  */
4151 /* MIN (X, Y) >= X -> X <= Y  */
4152 (for minmax (min min min min max max max max)
4153      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4154      out    (le  gt  gt  le  ge  lt  lt  ge )
4155  (simplify
4156   (cmp:c (minmax:c @0 @1) @0)
4157   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4158    (out @0 @1))))
4159 /* MIN (X, 5) == 0 -> X == 0
4160    MIN (X, 5) == 7 -> false  */
4161 (for cmp (eq ne)
4162  (simplify
4163   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4164   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4165                  TYPE_SIGN (TREE_TYPE (@0))))
4166    { constant_boolean_node (cmp == NE_EXPR, type); }
4167    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4168                   TYPE_SIGN (TREE_TYPE (@0))))
4169     (cmp @0 @2)))))
4170 (for cmp (eq ne)
4171  (simplify
4172   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4173   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4174                  TYPE_SIGN (TREE_TYPE (@0))))
4175    { constant_boolean_node (cmp == NE_EXPR, type); }
4176    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4177                   TYPE_SIGN (TREE_TYPE (@0))))
4178     (cmp @0 @2)))))
4180 /* X <= MAX(X, Y) -> true
4181    X > MAX(X, Y) -> false 
4182    X >= MIN(X, Y) -> true
4183    X < MIN(X, Y) -> false */
4184 (for minmax (min     min     max     max     )
4185      cmp    (ge      lt      le      gt      )
4186  (simplify
4187   (cmp:c @0 (minmax:c @0 @1))
4188   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4190 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4191 (for minmax (min     min     max     max     min     min     max     max    )
4192      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4193      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4194  (simplify
4195   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4196   (comb (cmp @0 @2) (cmp @1 @2))))
4198 /* Undo fancy ways of writing max/min or other ?: expressions, like
4199    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4200    People normally use ?: and that is what we actually try to optimize.  */
4201 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4202 (simplify
4203  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4204  (if (INTEGRAL_TYPE_P (type)
4205       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4206   (cond (convert:boolean_type_node @2) @1 @0)))
4207 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4208 (simplify
4209  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4210  (if (INTEGRAL_TYPE_P (type)
4211       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4212   (cond (convert:boolean_type_node @2) @1 @0)))
4213 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4214 (simplify
4215  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4216  (if (INTEGRAL_TYPE_P (type)
4217       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4218   (cond (convert:boolean_type_node @2) @1 @0)))
4220 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4221 (simplify
4222  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4223  (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
4224   (max @2 @1)))
4226 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4227 (for op (bit_xor bit_ior plus)
4228  (simplify
4229   (cond (eq zero_one_valued_p@0
4230             integer_zerop)
4231         @1
4232         (op:c @2 @1))
4233   (if (INTEGRAL_TYPE_P (type)
4234        && TYPE_PRECISION (type) > 1
4235        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4236        (op (mult (convert:type @0) @2) @1))))
4238 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4239 (for op (bit_xor bit_ior plus)
4240  (simplify
4241   (cond (ne zero_one_valued_p@0
4242             integer_zerop)
4243        (op:c @2 @1)
4244         @1)
4245   (if (INTEGRAL_TYPE_P (type)
4246        && TYPE_PRECISION (type) > 1
4247        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4248        (op (mult (convert:type @0) @2) @1))))
4250 /* ?: Value replacement. */
4251 /* a == 0 ? b : b + a  -> b + a */
4252 (for op (plus bit_ior bit_xor)
4253  (simplify
4254   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4255    @2))
4256 /* a == 0 ? b : b - a  -> b - a */
4257 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4258 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4259 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4260  (simplify
4261   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4262    @2))
4264 /* a == 1 ? b : b / a  -> b / a */
4265 (for op (trunc_div ceil_div floor_div round_div exact_div)
4266  (simplify
4267   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4268    @2))
4270 /* a == 1 ? b : a * b -> a * b */
4271 (for op (mult)
4272  (simplify
4273   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4274    @2))
4276 /* a == -1 ? b : a & b -> a & b */
4277 (for op (bit_and)
4278  (simplify
4279   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4280    @2))
4282 /* Simplifications of shift and rotates.  */
4284 (for rotate (lrotate rrotate)
4285  (simplify
4286   (rotate integer_all_onesp@0 @1)
4287   @0))
4289 /* Optimize -1 >> x for arithmetic right shifts.  */
4290 (simplify
4291  (rshift integer_all_onesp@0 @1)
4292  (if (!TYPE_UNSIGNED (type))
4293   @0))
4295 /* Optimize (x >> c) << c into x & (-1<<c).  */
4296 (simplify
4297  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4298  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4299   /* It doesn't matter if the right shift is arithmetic or logical.  */
4300   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4302 (simplify
4303  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4304  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4305       /* Allow intermediate conversion to integral type with whatever sign, as
4306          long as the low TYPE_PRECISION (type)
4307          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4308       && INTEGRAL_TYPE_P (type)
4309       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4310       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4311       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4312       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4313           || wi::geu_p (wi::to_wide (@1),
4314                         TYPE_PRECISION (type)
4315                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4316   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4318 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4319    unsigned x OR truncate into the precision(type) - c lowest bits
4320    of signed x (if they have mode precision or a precision of 1).  */
4321 (simplify
4322  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4323  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4324   (if (TYPE_UNSIGNED (type))
4325    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4326    (if (INTEGRAL_TYPE_P (type))
4327     (with {
4328       int width = element_precision (type) - tree_to_uhwi (@1);
4329       tree stype = NULL_TREE;
4330       if (width <= MAX_FIXED_MODE_SIZE)
4331         stype = build_nonstandard_integer_type (width, 0);
4332      }
4333      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4334       (convert (convert:stype @0))))))))
4336 /* Optimize x >> x into 0 */
4337 (simplify
4338  (rshift @0 @0)
4339   { build_zero_cst (type); })
4341 (for shiftrotate (lrotate rrotate lshift rshift)
4342  (simplify
4343   (shiftrotate @0 integer_zerop)
4344   (non_lvalue @0))
4345  (simplify
4346   (shiftrotate integer_zerop@0 @1)
4347   @0)
4348  /* Prefer vector1 << scalar to vector1 << vector2
4349     if vector2 is uniform.  */
4350  (for vec (VECTOR_CST CONSTRUCTOR)
4351   (simplify
4352    (shiftrotate @0 vec@1)
4353    (with { tree tem = uniform_vector_p (@1); }
4354     (if (tem)
4355      (shiftrotate @0 { tem; }))))))
4357 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4358    Y is 0.  Similarly for X >> Y.  */
4359 #if GIMPLE
4360 (for shift (lshift rshift)
4361  (simplify
4362   (shift @0 SSA_NAME@1)
4363    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4364     (with {
4365       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4366       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4367      }
4368      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4369       @0)))))
4370 #endif
4372 /* Rewrite an LROTATE_EXPR by a constant into an
4373    RROTATE_EXPR by a new constant.  */
4374 (simplify
4375  (lrotate @0 INTEGER_CST@1)
4376  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4377                             build_int_cst (TREE_TYPE (@1),
4378                                            element_precision (type)), @1); }))
4380 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4381 (for op (lrotate rrotate rshift lshift)
4382  (simplify
4383   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4384   (with { unsigned int prec = element_precision (type); }
4385    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4386         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4387         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4388         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4389     (with { unsigned int low = (tree_to_uhwi (@1)
4390                                 + tree_to_uhwi (@2)); }
4391      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4392         being well defined.  */
4393      (if (low >= prec)
4394       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4395        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4396        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4397         { build_zero_cst (type); }
4398         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4399       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4402 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4403 (simplify
4404  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4405   (if ((wi::to_wide (@1) & 1) != 0)
4406    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4407    { build_zero_cst (type); }))
4409 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4410    either to false if D is smaller (unsigned comparison) than C, or to
4411    x == log2 (D) - log2 (C).  Similarly for right shifts.
4412    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4413 (for cmp (ne eq)
4414      icmp (eq ne)
4415  (simplify
4416   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4417    (with { int c1 = wi::clz (wi::to_wide (@1));
4418            int c2 = wi::clz (wi::to_wide (@2)); }
4419     (if (c1 < c2)
4420      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4421      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4422  (simplify
4423   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4424    (if (tree_int_cst_sgn (@1) > 0)
4425     (with { int c1 = wi::clz (wi::to_wide (@1));
4426             int c2 = wi::clz (wi::to_wide (@2)); }
4427      (if (c1 > c2)
4428       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4429       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4430  /* `(1 >> X) != 0` -> `X == 0` */
4431  /* `(1 >> X) == 0` -> `X != 0` */
4432  (simplify
4433   (cmp (rshift integer_onep@1 @0) integer_zerop)
4434    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4435     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4437 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4438    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4439    if CST2 != 0.  */
4440 (for cmp (ne eq)
4441  (simplify
4442   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4443   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4444    (if (cand < 0
4445         || (!integer_zerop (@2)
4446             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4447     { constant_boolean_node (cmp == NE_EXPR, type); }
4448     (if (!integer_zerop (@2)
4449          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4450      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4452 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4453         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4454 (for cmp (ne eq)
4455  (simplify
4456   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4457   (if (tree_fits_shwi_p (@1)
4458        && tree_to_shwi (@1) > 0
4459        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4460     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4461       { constant_boolean_node (cmp == NE_EXPR, type); }
4462       (with { wide_int c1 = wi::to_wide (@1);
4463               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4464               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4465         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4466              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4467  (simplify
4468   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4469   (if (tree_fits_shwi_p (@1)
4470        && tree_to_shwi (@1) > 0
4471        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4472     (with { tree t0 = TREE_TYPE (@0);
4473             unsigned int prec = TYPE_PRECISION (t0);
4474             wide_int c1 = wi::to_wide (@1);
4475             wide_int c2 = wi::to_wide (@2);
4476             wide_int c3 = wi::to_wide (@3);
4477             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4478       (if ((c2 & c3) != c3)
4479         { constant_boolean_node (cmp == NE_EXPR, type); }
4480         (if (TYPE_UNSIGNED (t0))
4481           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4482             { constant_boolean_node (cmp == NE_EXPR, type); }
4483             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4484                  { wide_int_to_tree (t0, c3 << c1); }))
4485           (with { wide_int smask = wi::arshift (sb, c1); }
4486             (switch
4487               (if ((c2 & smask) == 0)
4488                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4489                      { wide_int_to_tree (t0, c3 << c1); }))
4490               (if ((c3 & smask) == 0)
4491                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4492                      { wide_int_to_tree (t0, c3 << c1); }))
4493               (if ((c2 & smask) != (c3 & smask))
4494                 { constant_boolean_node (cmp == NE_EXPR, type); })
4495               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4496                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4498 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4499         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4500    if the new mask might be further optimized.  */
4501 (for shift (lshift rshift)
4502  (simplify
4503   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4504            INTEGER_CST@2)
4505    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4506         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4507         && tree_fits_uhwi_p (@1)
4508         && tree_to_uhwi (@1) > 0
4509         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4510     (with
4511      {
4512        unsigned int shiftc = tree_to_uhwi (@1);
4513        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4514        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4515        tree shift_type = TREE_TYPE (@3);
4516        unsigned int prec;
4518        if (shift == LSHIFT_EXPR)
4519          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4520        else if (shift == RSHIFT_EXPR
4521                 && type_has_mode_precision_p (shift_type))
4522          {
4523            prec = TYPE_PRECISION (TREE_TYPE (@3));
4524            tree arg00 = @0;
4525            /* See if more bits can be proven as zero because of
4526               zero extension.  */
4527            if (@3 != @0
4528                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4529              {
4530                tree inner_type = TREE_TYPE (@0);
4531                if (type_has_mode_precision_p (inner_type)
4532                    && TYPE_PRECISION (inner_type) < prec)
4533                  {
4534                    prec = TYPE_PRECISION (inner_type);
4535                    /* See if we can shorten the right shift.  */
4536                    if (shiftc < prec)
4537                      shift_type = inner_type;
4538                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4539                       it into (X, 0) later on by making sure zerobits
4540                       is all ones.  */
4541                  }
4542              }
4543            zerobits = HOST_WIDE_INT_M1U;
4544            if (shiftc < prec)
4545              {
4546                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4547                zerobits <<= prec - shiftc;
4548              }
4549            /* For arithmetic shift if sign bit could be set, zerobits
4550               can contain actually sign bits, so no transformation is
4551               possible, unless MASK masks them all away.  In that
4552               case the shift needs to be converted into logical shift.  */
4553            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4554                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4555              {
4556                if ((mask & zerobits) == 0)
4557                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4558                else
4559                  zerobits = 0;
4560              }
4561          }
4562      }
4563      /* ((X << 16) & 0xff00) is (X, 0).  */
4564      (if ((mask & zerobits) == mask)
4565       { build_int_cst (type, 0); }
4566       (with { newmask = mask | zerobits; }
4567        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4568         (with
4569          {
4570            /* Only do the transformation if NEWMASK is some integer
4571               mode's mask.  */
4572            for (prec = BITS_PER_UNIT;
4573                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4574              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4575                break;
4576          }
4577          (if (prec < HOST_BITS_PER_WIDE_INT
4578               || newmask == HOST_WIDE_INT_M1U)
4579           (with
4580            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4581            (if (!tree_int_cst_equal (newmaskt, @2))
4582             (if (shift_type != TREE_TYPE (@3))
4583              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4584              (bit_and @4 { newmaskt; })))))))))))))
4586 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4587 (for cmp (ne eq)
4588        icmp (eq ne)
4589  (simplify
4590   (cmp
4591    (bit_and
4592     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4593   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4594    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4595                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4597 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4598    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4599 (for shift (lshift rshift)
4600  (for bit_op (bit_and bit_xor bit_ior)
4601   (simplify
4602    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4603    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4604     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4605      (if (mask)
4606       (bit_op (shift (convert @0) @1) { mask; })))))))
4608 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4609 (simplify
4610  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4611   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4612        && (element_precision (TREE_TYPE (@0))
4613            <= element_precision (TREE_TYPE (@1))
4614            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4615    (with
4616     { tree shift_type = TREE_TYPE (@0); }
4617      (convert (rshift (convert:shift_type @1) @2)))))
4619 /* ~(~X >>r Y) -> X >>r Y
4620    ~(~X <<r Y) -> X <<r Y */
4621 (for rotate (lrotate rrotate)
4622  (simplify
4623   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4624    (if ((element_precision (TREE_TYPE (@0))
4625          <= element_precision (TREE_TYPE (@1))
4626          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4627         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4628             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4629     (with
4630      { tree rotate_type = TREE_TYPE (@0); }
4631       (convert (rotate (convert:rotate_type @1) @2))))))
4633 (for cmp (eq ne)
4634  (for rotate (lrotate rrotate)
4635       invrot (rrotate lrotate)
4636   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4637   (simplify
4638    (cmp (rotate @1 @0) (rotate @2 @0))
4639    (cmp @1 @2))
4640   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4641   (simplify
4642    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4643    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4644   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4645   (simplify
4646    (cmp (rotate @0 @1) INTEGER_CST@2)
4647     (if (integer_zerop (@2) || integer_all_onesp (@2))
4648      (cmp @0 @2)))))
4650 /* Narrow a lshift by constant.  */
4651 (simplify
4652  (convert (lshift:s@0 @1 INTEGER_CST@2))
4653  (if (INTEGRAL_TYPE_P (type)
4654       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4655       && !integer_zerop (@2)
4656       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4657   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4658        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4659    (lshift (convert @1) @2)
4660    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4661     { build_zero_cst (type); }))))
4663 /* Simplifications of conversions.  */
4665 /* Basic strip-useless-type-conversions / strip_nops.  */
4666 (for cvt (convert view_convert float fix_trunc)
4667  (simplify
4668   (cvt @0)
4669   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4670        || (GENERIC && type == TREE_TYPE (@0)))
4671    @0)))
4673 /* Contract view-conversions.  */
4674 (simplify
4675   (view_convert (view_convert @0))
4676   (view_convert @0))
4678 /* For integral conversions with the same precision or pointer
4679    conversions use a NOP_EXPR instead.  */
4680 (simplify
4681   (view_convert @0)
4682   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4683        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4684        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4685    (convert @0)))
4687 /* Strip inner integral conversions that do not change precision or size, or
4688    zero-extend while keeping the same size (for bool-to-char).  */
4689 (simplify
4690   (view_convert (convert@0 @1))
4691   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4692        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4693        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4694        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4695            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4696                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4697    (view_convert @1)))
4699 /* Simplify a view-converted empty or single-element constructor.  */
4700 (simplify
4701   (view_convert CONSTRUCTOR@0)
4702   (with
4703    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4704                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4705    (switch
4706     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4707      { build_zero_cst (type); })
4708     (if (CONSTRUCTOR_NELTS (ctor) == 1
4709          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4710          && operand_equal_p (TYPE_SIZE (type),
4711                              TYPE_SIZE (TREE_TYPE
4712                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4713      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4715 /* Re-association barriers around constants and other re-association
4716    barriers can be removed.  */
4717 (simplify
4718  (paren CONSTANT_CLASS_P@0)
4719  @0)
4720 (simplify
4721  (paren (paren@1 @0))
4722  @1)
4724 /* Handle cases of two conversions in a row.  */
4725 (for ocvt (convert float fix_trunc)
4726  (for icvt (convert float)
4727   (simplify
4728    (ocvt (icvt@1 @0))
4729    (with
4730     {
4731       tree inside_type = TREE_TYPE (@0);
4732       tree inter_type = TREE_TYPE (@1);
4733       int inside_int = INTEGRAL_TYPE_P (inside_type);
4734       int inside_ptr = POINTER_TYPE_P (inside_type);
4735       int inside_float = FLOAT_TYPE_P (inside_type);
4736       int inside_vec = VECTOR_TYPE_P (inside_type);
4737       unsigned int inside_prec = element_precision (inside_type);
4738       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4739       int inter_int = INTEGRAL_TYPE_P (inter_type);
4740       int inter_ptr = POINTER_TYPE_P (inter_type);
4741       int inter_float = FLOAT_TYPE_P (inter_type);
4742       int inter_vec = VECTOR_TYPE_P (inter_type);
4743       unsigned int inter_prec = element_precision (inter_type);
4744       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4745       int final_int = INTEGRAL_TYPE_P (type);
4746       int final_ptr = POINTER_TYPE_P (type);
4747       int final_float = FLOAT_TYPE_P (type);
4748       int final_vec = VECTOR_TYPE_P (type);
4749       unsigned int final_prec = element_precision (type);
4750       int final_unsignedp = TYPE_UNSIGNED (type);
4751     }
4752    (switch
4753     /* In addition to the cases of two conversions in a row
4754        handled below, if we are converting something to its own
4755        type via an object of identical or wider precision, neither
4756        conversion is needed.  */
4757     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4758           || (GENERIC
4759               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4760          && (((inter_int || inter_ptr) && final_int)
4761              || (inter_float && final_float))
4762          && inter_prec >= final_prec)
4763      (ocvt @0))
4765     /* Likewise, if the intermediate and initial types are either both
4766        float or both integer, we don't need the middle conversion if the
4767        former is wider than the latter and doesn't change the signedness
4768        (for integers).  Avoid this if the final type is a pointer since
4769        then we sometimes need the middle conversion.  */
4770     (if (((inter_int && inside_int) || (inter_float && inside_float))
4771          && (final_int || final_float)
4772          && inter_prec >= inside_prec
4773          && (inter_float || inter_unsignedp == inside_unsignedp))
4774      (ocvt @0))
4776     /* If we have a sign-extension of a zero-extended value, we can
4777        replace that by a single zero-extension.  Likewise if the
4778        final conversion does not change precision we can drop the
4779        intermediate conversion.  Similarly truncation of a sign-extension
4780        can be replaced by a single sign-extension.  */
4781     (if (inside_int && inter_int && final_int
4782          && ((inside_prec < inter_prec && inter_prec < final_prec
4783               && inside_unsignedp && !inter_unsignedp)
4784              || final_prec == inter_prec
4785              || (inside_prec < inter_prec && inter_prec > final_prec
4786                  && !inside_unsignedp && inter_unsignedp)))
4787      (ocvt @0))
4789     /* Two conversions in a row are not needed unless:
4790         - some conversion is floating-point (overstrict for now), or
4791         - some conversion is a vector (overstrict for now), or
4792         - the intermediate type is narrower than both initial and
4793           final, or
4794         - the intermediate type and innermost type differ in signedness,
4795           and the outermost type is wider than the intermediate, or
4796         - the initial type is a pointer type and the precisions of the
4797           intermediate and final types differ, or
4798         - the final type is a pointer type and the precisions of the
4799           initial and intermediate types differ.  */
4800     (if (! inside_float && ! inter_float && ! final_float
4801          && ! inside_vec && ! inter_vec && ! final_vec
4802          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4803          && ! (inside_int && inter_int
4804                && inter_unsignedp != inside_unsignedp
4805                && inter_prec < final_prec)
4806          && ((inter_unsignedp && inter_prec > inside_prec)
4807              == (final_unsignedp && final_prec > inter_prec))
4808          && ! (inside_ptr && inter_prec != final_prec)
4809          && ! (final_ptr && inside_prec != inter_prec))
4810      (ocvt @0))
4812    /* `(outer:M)(inter:N) a:O`
4813       can be converted to `(outer:M) a`
4814       if M <= O && N >= O. No matter what signedness of the casts,
4815       as the final is either a truncation from the original or just
4816       a sign change of the type. */
4817    (if (inside_int && inter_int && final_int
4818         && final_prec <= inside_prec
4819         && inter_prec >= inside_prec)
4820     (convert @0))
4822     /* A truncation to an unsigned type (a zero-extension) should be
4823        canonicalized as bitwise and of a mask.  */
4824     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4825          && final_int && inter_int && inside_int
4826          && final_prec == inside_prec
4827          && final_prec > inter_prec
4828          && inter_unsignedp)
4829      (convert (bit_and @0 { wide_int_to_tree
4830                               (inside_type,
4831                                wi::mask (inter_prec, false,
4832                                          TYPE_PRECISION (inside_type))); })))
4834     /* If we are converting an integer to a floating-point that can
4835        represent it exactly and back to an integer, we can skip the
4836        floating-point conversion.  */
4837     (if (GIMPLE /* PR66211 */
4838          && inside_int && inter_float && final_int &&
4839          (unsigned) significand_size (TYPE_MODE (inter_type))
4840          >= inside_prec - !inside_unsignedp)
4841      (convert @0)))))))
4843 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4844    float_type.  Only do the transformation if we do not need to preserve
4845    trapping behaviour, so require !flag_trapping_math. */
4846 #if GIMPLE
4847 (simplify
4848    (float (fix_trunc @0))
4849    (if (!flag_trapping_math
4850         && types_match (type, TREE_TYPE (@0))
4851         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4852                                           OPTIMIZE_FOR_BOTH))
4853       (IFN_TRUNC @0)))
4854 #endif
4856 /* If we have a narrowing conversion to an integral type that is fed by a
4857    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4858    masks off bits outside the final type (and nothing else).  */
4859 (simplify
4860   (convert (bit_and @0 INTEGER_CST@1))
4861   (if (INTEGRAL_TYPE_P (type)
4862        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4863        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4864        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4865                                                     TYPE_PRECISION (type)), 0))
4866    (convert @0)))
4869 /* (X /[ex] A) * A -> X.  */
4870 (simplify
4871   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4872   (convert @0))
4874 /* Simplify (A / B) * B + (A % B) -> A.  */
4875 (for div (trunc_div ceil_div floor_div round_div)
4876      mod (trunc_mod ceil_mod floor_mod round_mod)
4877   (simplify
4878    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4879    @0))
4881 /* x / y * y == x -> x % y == 0.  */
4882 (simplify
4883   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4884   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4885     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4887 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4888 (for op (plus minus)
4889  (simplify
4890   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4891   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4892        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4893    (with
4894      {
4895        wi::overflow_type overflow;
4896        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4897                                TYPE_SIGN (type), &overflow);
4898      }
4899      (if (types_match (type, TREE_TYPE (@2))
4900          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4901       (op @0 { wide_int_to_tree (type, mul); })
4902       (with { tree utype = unsigned_type_for (type); }
4903        (convert (op (convert:utype @0)
4904                     (mult (convert:utype @1) (convert:utype @2))))))))))
4906 /* Canonicalization of binary operations.  */
4908 /* Convert X + -C into X - C.  */
4909 (simplify
4910  (plus @0 REAL_CST@1)
4911  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4912   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4913    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4914     (minus @0 { tem; })))))
4916 /* Convert x+x into x*2.  */
4917 (simplify
4918  (plus @0 @0)
4919  (if (SCALAR_FLOAT_TYPE_P (type))
4920   (mult @0 { build_real (type, dconst2); })
4921   (if (INTEGRAL_TYPE_P (type))
4922    (mult @0 { build_int_cst (type, 2); }))))
4924 /* 0 - X  ->  -X.  */
4925 (simplify
4926  (minus integer_zerop @1)
4927  (negate @1))
4928 (simplify
4929  (pointer_diff integer_zerop @1)
4930  (negate (convert @1)))
4932 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4933    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4934    (-ARG1 + ARG0) reduces to -ARG1.  */
4935 (simplify
4936  (minus real_zerop@0 @1)
4937  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4938   (negate @1)))
4940 /* Transform x * -1 into -x.  */
4941 (simplify
4942  (mult @0 integer_minus_onep)
4943  (negate @0))
4945 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4946    signed overflow for CST != 0 && CST != -1.  */
4947 (simplify
4948  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4949  (if (TREE_CODE (@2) != INTEGER_CST
4950       && single_use (@3)
4951       && !integer_zerop (@1) && !integer_minus_onep (@1))
4952   (mult (mult @0 @2) @1)))
4954 /* True if we can easily extract the real and imaginary parts of a complex
4955    number.  */
4956 (match compositional_complex
4957  (convert? (complex @0 @1)))
4959 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4960 (simplify
4961  (complex (realpart @0) (imagpart @0))
4962  @0)
4963 (simplify
4964  (realpart (complex @0 @1))
4965  @0)
4966 (simplify
4967  (imagpart (complex @0 @1))
4968  @1)
4970 /* Sometimes we only care about half of a complex expression.  */
4971 (simplify
4972  (realpart (convert?:s (conj:s @0)))
4973  (convert (realpart @0)))
4974 (simplify
4975  (imagpart (convert?:s (conj:s @0)))
4976  (convert (negate (imagpart @0))))
4977 (for part (realpart imagpart)
4978  (for op (plus minus)
4979   (simplify
4980    (part (convert?:s@2 (op:s @0 @1)))
4981    (convert (op (part @0) (part @1))))))
4982 (simplify
4983  (realpart (convert?:s (CEXPI:s @0)))
4984  (convert (COS @0)))
4985 (simplify
4986  (imagpart (convert?:s (CEXPI:s @0)))
4987  (convert (SIN @0)))
4989 /* conj(conj(x)) -> x  */
4990 (simplify
4991  (conj (convert? (conj @0)))
4992  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4993   (convert @0)))
4995 /* conj({x,y}) -> {x,-y}  */
4996 (simplify
4997  (conj (convert?:s (complex:s @0 @1)))
4998  (with { tree itype = TREE_TYPE (type); }
4999   (complex (convert:itype @0) (negate (convert:itype @1)))))
5001 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
5002 (for bswap (BSWAP)
5003  (simplify
5004   (bswap (bswap @0))
5005   @0)
5006  (simplify
5007   (bswap (bit_not (bswap @0)))
5008   (bit_not @0))
5009  (for bitop (bit_xor bit_ior bit_and)
5010   (simplify
5011    (bswap (bitop:c (bswap @0) @1))
5012    (bitop @0 (bswap @1))))
5013  (for cmp (eq ne)
5014   (simplify
5015    (cmp (bswap@2 @0) (bswap @1))
5016    (with { tree ctype = TREE_TYPE (@2); }
5017     (cmp (convert:ctype @0) (convert:ctype @1))))
5018   (simplify
5019    (cmp (bswap @0) INTEGER_CST@1)
5020    (with { tree ctype = TREE_TYPE (@1); }
5021     (cmp (convert:ctype @0) (bswap! @1)))))
5022  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
5023  (simplify
5024   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5025            INTEGER_CST@3)
5026    (if (BITS_PER_UNIT == 8
5027         && tree_fits_uhwi_p (@2)
5028         && tree_fits_uhwi_p (@3))
5029     (with
5030      {
5031       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5032       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5033       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5034       unsigned HOST_WIDE_INT lo = bits & 7;
5035       unsigned HOST_WIDE_INT hi = bits - lo;
5036      }
5037      (if (bits < prec
5038           && mask < (256u>>lo)
5039           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5040       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5041        (if (ns == 0)
5042         (bit_and (convert @1) @3)
5043         (with
5044          {
5045           tree utype = unsigned_type_for (TREE_TYPE (@1));
5046           tree nst = build_int_cst (integer_type_node, ns);
5047          }
5048          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5049  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
5050  (simplify
5051   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5052    (if (BITS_PER_UNIT == 8
5053         && CHAR_TYPE_SIZE == 8
5054         && tree_fits_uhwi_p (@1))
5055     (with
5056      {
5057       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5058       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5059       /* If the bswap was extended before the original shift, this
5060          byte (shift) has the sign of the extension, not the sign of
5061          the original shift.  */
5062       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5063      }
5064      /* Special case: logical right shift of sign-extended bswap.
5065         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5066      (if (TYPE_PRECISION (type) > prec
5067           && !TYPE_UNSIGNED (TREE_TYPE (@2))
5068           && TYPE_UNSIGNED (type)
5069           && bits < prec && bits + 8 >= prec)
5070       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5071        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5072       (if (bits + 8 == prec)
5073        (if (TYPE_UNSIGNED (st))
5074         (convert (convert:unsigned_char_type_node @0))
5075         (convert (convert:signed_char_type_node @0)))
5076        (if (bits < prec && bits + 8 > prec)
5077         (with 
5078          {
5079           tree nst = build_int_cst (integer_type_node, bits & 7);
5080           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5081                                        : signed_char_type_node;
5082          }
5083          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5084  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5085  (simplify
5086   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5087    (if (BITS_PER_UNIT == 8
5088         && tree_fits_uhwi_p (@1)
5089         && tree_to_uhwi (@1) < 256)
5090     (with
5091      {
5092       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5093       tree utype = unsigned_type_for (TREE_TYPE (@0));
5094       tree nst = build_int_cst (integer_type_node, prec - 8);
5095      }
5096      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5099 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5101 /* Simplify constant conditions.
5102    Only optimize constant conditions when the selected branch
5103    has the same type as the COND_EXPR.  This avoids optimizing
5104    away "c ? x : throw", where the throw has a void type.
5105    Note that we cannot throw away the fold-const.cc variant nor
5106    this one as we depend on doing this transform before possibly
5107    A ? B : B -> B triggers and the fold-const.cc one can optimize
5108    0 ? A : B to B even if A has side-effects.  Something
5109    genmatch cannot handle.  */
5110 (simplify
5111  (cond INTEGER_CST@0 @1 @2)
5112  (if (integer_zerop (@0))
5113   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5114    @2)
5115   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5116    @1)))
5117 (simplify
5118  (vec_cond VECTOR_CST@0 @1 @2)
5119  (if (integer_all_onesp (@0))
5120   @1
5121   (if (integer_zerop (@0))
5122    @2)))
5124 /* Sink unary operations to branches, but only if we do fold both.  */
5125 (for op (negate bit_not abs absu)
5126  (simplify
5127   (op (vec_cond:s @0 @1 @2))
5128   (vec_cond @0 (op! @1) (op! @2))))
5130 /* Sink unary conversions to branches, but only if we do fold both
5131    and the target's truth type is the same as we already have.  */
5132 (simplify
5133  (convert (vec_cond:s @0 @1 @2))
5134  (if (VECTOR_TYPE_P (type)
5135       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5136   (vec_cond @0 (convert! @1) (convert! @2))))
5138 /* Likewise for view_convert of nop_conversions. */
5139 (simplify
5140  (view_convert (vec_cond:s @0 @1 @2))
5141  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5142       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5143                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5144       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5145   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5147 /* Sink binary operation to branches, but only if we can fold it.  */
5148 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5149          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5150          trunc_mod ceil_mod floor_mod round_mod min max)
5151 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5152  (simplify
5153   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5154   (if (TREE_CODE_CLASS (op) != tcc_comparison
5155        || types_match (type, TREE_TYPE (@1))
5156        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5157        || (optimize_vectors_before_lowering_p ()
5158            /* The following is optimistic on the side of non-support, we are
5159               missing the legacy vcond{,u,eq} cases.  Do this only when
5160               lowering will be able to fixup..  */
5161            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5162                                        TREE_TYPE (@0), ERROR_MARK)))
5163    (vec_cond @0 (op! @1 @3) (op! @2 @4))))
5165 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5166  (simplify
5167   (op (vec_cond:s @0 @1 @2) @3)
5168   (if (TREE_CODE_CLASS (op) != tcc_comparison
5169        || types_match (type, TREE_TYPE (@1))
5170        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5171        || (optimize_vectors_before_lowering_p ()
5172            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5173                                        TREE_TYPE (@0), ERROR_MARK)))
5174    (vec_cond @0 (op! @1 @3) (op! @2 @3))))
5175  (simplify
5176   (op @3 (vec_cond:s @0 @1 @2))
5177   (if (TREE_CODE_CLASS (op) != tcc_comparison
5178        || types_match (type, TREE_TYPE (@1))
5179        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5180        || (optimize_vectors_before_lowering_p ()
5181            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5182                                        TREE_TYPE (@0), ERROR_MARK)))
5183    (vec_cond @0 (op! @3 @1) (op! @3 @2)))))
5185 #if GIMPLE
5186 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5187  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5188            INTEGER_CST@1)
5189  (with {
5190          int ibit = tree_log2 (@0);
5191          int ibit2 = tree_log2 (@1);
5192        }
5193   (if (ibit == ibit2
5194       && ibit >= 0
5195       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5197 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5198  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5199           INTEGER_CST@1)
5200  (with {
5201          int ibit = tree_log2 (@0);
5202          int ibit2 = tree_log2 (@1);
5203        }
5204   (if (ibit == ibit2
5205       && ibit >= 0
5206       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5208 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5209  (bit_and:c
5210   (convert1?@4
5211    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5212   (convert2? @0))
5213  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5215 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5216  (bit_and:c
5217   (convert1?@4
5218    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5219   (convert2? @0))
5220  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5222 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5223  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5224             INTEGER_CST@1)
5225  (with {
5226          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5227                                               TYPE_PRECISION(type)));
5228          int ibit2 = tree_log2 (@1);
5229        }
5230   (if (ibit == ibit2
5231       && ibit >= 0
5232       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5234 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5235  (bit_and@4
5236   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5237   INTEGER_CST@1)
5238  (with {
5239          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5240                                               TYPE_PRECISION(type)));
5241          int ibit2 = tree_log2 (@1);
5242        }
5243   (if (ibit == ibit2
5244       && ibit >= 0
5245       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5247 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5248  (bit_and:c
5249   (convert1?@3
5250    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5251   (convert2? @0))
5252  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5254 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5255  (bit_and:c
5256   (convert1?@3
5257    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5258   (convert2? @0))
5259   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5261 #endif
5263 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5264    Currently disabled after pass lvec because ARM understands
5265    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5266 #if GIMPLE
5267 /* These can only be done in gimple as fold likes to convert:
5268    (CMP) & N into (CMP) ? N : 0
5269    and we try to match the same pattern again and again. */
5270 (simplify
5271  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5272  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5273   (vec_cond (bit_and @0 @3) @1 @2)))
5274 (simplify
5275  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5276  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5277   (vec_cond (bit_ior @0 @3) @1 @2)))
5278 (simplify
5279  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5280  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5281   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5282 (simplify
5283  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5284  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5285   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5287 /*  ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5288     (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5289     component type of the outer vec_cond is greater equal the inner one.  */
5290 (for cmp (simple_comparison)
5291  (simplify
5292   (vec_cond
5293     (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5294                                     integer_all_onesp
5295                                     integer_zerop))
5296           integer_zerop) @2 @3)
5297   (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5298        && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5299        && !TYPE_UNSIGNED (TREE_TYPE (@5))
5300        && VECTOR_TYPE_P (TREE_TYPE (@6))
5301        && VECTOR_TYPE_P (type)
5302        && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5303                            TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5304        && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5305    (with { tree vtype = TREE_TYPE (@6);}
5306      (view_convert:type
5307        (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5309 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5310 (simplify
5311  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5312  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5313   (vec_cond (bit_and @0 @1) @2 @3)))
5314 (simplify
5315  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5316  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5317   (vec_cond (bit_ior @0 @1) @2 @3)))
5318 (simplify
5319  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5320  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5321   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5322 (simplify
5323  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5324  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5325   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5326 #endif
5328 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5329    types are compatible.  */
5330 (simplify
5331  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5332  (if (VECTOR_BOOLEAN_TYPE_P (type)
5333       && types_match (type, TREE_TYPE (@0)))
5334   (if (integer_zerop (@1) && integer_all_onesp (@2))
5335    (bit_not @0)
5336    (if (integer_all_onesp (@1) && integer_zerop (@2))
5337     @0))))
5339 /* A few simplifications of "a ? CST1 : CST2". */
5340 /* NOTE: Only do this on gimple as the if-chain-to-switch
5341    optimization depends on the gimple to have if statements in it. */
5342 #if GIMPLE
5343 (simplify
5344  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5345  (switch
5346   (if (integer_zerop (@2))
5347    (switch
5348     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5349     (if (integer_onep (@1))
5350      (convert (convert:boolean_type_node @0)))
5351     /* a ? -1 : 0 -> -a.  */
5352     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5353      (if (TYPE_PRECISION (type) == 1)
5354       /* For signed 1-bit precision just cast bool to the type.  */
5355       (convert (convert:boolean_type_node @0))
5356       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5357        (with {
5358           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5359                                                       TYPE_UNSIGNED (type));
5360         }
5361         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5362        (negate (convert:type (convert:boolean_type_node @0))))))
5363     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5364     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5365      (with {
5366        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5367       }
5368       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5369   (if (integer_zerop (@1))
5370    (switch
5371     /* a ? 0 : 1 -> !a.  */
5372     (if (integer_onep (@2))
5373      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5374     /* a ? 0 : -1 -> -(!a).  */
5375     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5376      (if (TYPE_PRECISION (type) == 1)
5377       /* For signed 1-bit precision just cast bool to the type.  */
5378       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5379       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5380        (with {
5381           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5382                                                       TYPE_UNSIGNED (type));
5383         }
5384         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5385                                                 { boolean_true_node; })))))
5386        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5387                                       { boolean_true_node; }))))))
5388     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5389     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5390      (with {
5391        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5392       }
5393       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5394                                 { boolean_true_node; })) { shift; })))))))
5396 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5397    for unsigned types. */
5398 (simplify
5399  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5400  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5401       && bitwise_equal_p (@0, @2))
5402   (convert (eq @0 @1))
5406 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5407    for unsigned types. */
5408 (simplify
5409  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5410  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5411       && bitwise_equal_p (@0, @2))
5412   (convert (eq @0 @1))
5416 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5417    on the first bit of the CST.  */
5418 (simplify
5419  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5420  (if ((wi::to_wide (@1) & 1) != 0)
5421   @2
5422   { build_zero_cst (type); }))
5424 /* Optimize
5425    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5426    x_5 == cstN ? cst4 : cst3
5427    # op is == or != and N is 1 or 2
5428    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5429    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5430    of cst3 and cst4 is smaller.
5431    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5432 (for eqne (ne eq)
5433  (simplify
5434   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5435   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5436        && INTEGRAL_TYPE_P (type)
5437        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5438            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5439    (with {
5440      value_range r;
5441      get_range_query (cfun)->range_of_expr (r, @0);
5442      if (r.undefined_p ())
5443        r.set_varying (TREE_TYPE (@0));
5445      wide_int min = r.lower_bound ();
5446      wide_int max = r.upper_bound ();
5447     }
5448     (if (min + 1 == max
5449          && (wi::to_wide (@1) == min
5450              || wi::to_wide (@1) == max))
5451      (with {
5452        tree arg0 = @2, arg1 = @3;
5453        tree type1;
5454        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5455          std::swap (arg0, arg1);
5456        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5457          type1 = TREE_TYPE (@0);
5458        else
5459          type1 = type;
5460        auto prec = TYPE_PRECISION (type1);
5461        auto unsign = TYPE_UNSIGNED (type1);
5462        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5463         type1 = build_nonstandard_integer_type (prec, unsign);
5464        min = wide_int::from (min, prec,
5465                              TYPE_SIGN (TREE_TYPE (@0)));
5466        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5467                                     TYPE_SIGN (type));
5468        enum tree_code code;
5469        wi::overflow_type ovf;
5470        if (tree_int_cst_lt (arg0, arg1))
5471          {
5472            code = PLUS_EXPR;
5473            a -= min;
5474            if (!unsign)
5475              {
5476                /* lhs is known to be in range [min, min+1] and we want to add a
5477                   to it.  Check if that operation can overflow for those 2 values
5478                   and if yes, force unsigned type.  */
5479                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5480                if (ovf)
5481                  type1 = unsigned_type_for (type1);
5482              }
5483          }
5484        else
5485          {
5486            code = MINUS_EXPR;
5487            a += min;
5488            if (!unsign)
5489              {
5490                /* lhs is known to be in range [min, min+1] and we want to subtract
5491                   it from a.  Check if that operation can overflow for those 2
5492                   values and if yes, force unsigned type.  */
5493                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5494                if (ovf)
5495                 type1 = unsigned_type_for (type1);
5496              }
5497          }
5498        tree arg = wide_int_to_tree (type1, a);
5499       }
5500       (if (code == PLUS_EXPR)
5501        (convert (plus (convert:type1 @0) { arg; }))
5502        (convert (minus { arg; } (convert:type1 @0))))))))))
5503 #endif
5505 (simplify
5506  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5507  (if (INTEGRAL_TYPE_P (type)
5508       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5509   (cond @1 (convert @2) (convert @3))))
5511 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5512    be extended.  */
5513 /* This pattern implements two kinds simplification:
5515    Case 1)
5516    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5517      1) Conversions are type widening from smaller type.
5518      2) Const c1 equals to c2 after canonicalizing comparison.
5519      3) Comparison has tree code LT, LE, GT or GE.
5520    This specific pattern is needed when (cmp (convert x) c) may not
5521    be simplified by comparison patterns because of multiple uses of
5522    x.  It also makes sense here because simplifying across multiple
5523    referred var is always benefitial for complicated cases.
5525    Case 2)
5526    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5527 (for cmp (lt le gt ge eq ne)
5528  (simplify
5529   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5530   (with
5531    {
5532      tree from_type = TREE_TYPE (@1);
5533      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5534      enum tree_code code = ERROR_MARK;
5536      if (INTEGRAL_TYPE_P (from_type)
5537          && int_fits_type_p (@2, from_type)
5538          && (types_match (c1_type, from_type)
5539              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5540                  && (TYPE_UNSIGNED (from_type)
5541                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5542          && (types_match (c2_type, from_type)
5543              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5544                  && (TYPE_UNSIGNED (from_type)
5545                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5546        {
5547          if (cmp != EQ_EXPR)
5548            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5549          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5550          else if (int_fits_type_p (@3, from_type))
5551            code = EQ_EXPR;
5552        }
5553    }
5554    (if (code == MAX_EXPR)
5555     (convert (max @1 (convert @2)))
5556     (if (code == MIN_EXPR)
5557      (convert (min @1 (convert @2)))
5558      (if (code == EQ_EXPR)
5559       (convert (cond (eq @1 (convert @3))
5560                      (convert:from_type @3) (convert:from_type @2)))))))))
5562 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5564      1) OP is PLUS or MINUS.
5565      2) CMP is LT, LE, GT or GE.
5566      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5568    This pattern also handles special cases like:
5570      A) Operand x is a unsigned to signed type conversion and c1 is
5571         integer zero.  In this case,
5572           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5573           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5574      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5575         check equality for (c1+1) and (c1-1) by adjusting comparison
5576         code.
5578    TODO: Though signed type is handled by this pattern, it cannot be
5579    simplified at the moment because C standard requires additional
5580    type promotion.  In order to match&simplify it here, the IR needs
5581    to be cleaned up by other optimizers, i.e, VRP.  */
5582 (for op (plus minus)
5583  (for cmp (lt le gt ge)
5584   (simplify
5585    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5586    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5587     (if (types_match (from_type, to_type)
5588          /* Check if it is special case A).  */
5589          || (TYPE_UNSIGNED (from_type)
5590              && !TYPE_UNSIGNED (to_type)
5591              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5592              && integer_zerop (@1)
5593              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5594      (with
5595       {
5596         wi::overflow_type overflow = wi::OVF_NONE;
5597         enum tree_code code, cmp_code = cmp;
5598         wide_int real_c1;
5599         wide_int c1 = wi::to_wide (@1);
5600         wide_int c2 = wi::to_wide (@2);
5601         wide_int c3 = wi::to_wide (@3);
5602         signop sgn = TYPE_SIGN (from_type);
5604         /* Handle special case A), given x of unsigned type:
5605             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5606             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5607         if (!types_match (from_type, to_type))
5608           {
5609             if (cmp_code == LT_EXPR)
5610               cmp_code = GT_EXPR;
5611             if (cmp_code == GE_EXPR)
5612               cmp_code = LE_EXPR;
5613             c1 = wi::max_value (to_type);
5614           }
5615         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5616            compute (c3 op' c2) and check if it equals to c1 with op' being
5617            the inverted operator of op.  Make sure overflow doesn't happen
5618            if it is undefined.  */
5619         if (op == PLUS_EXPR)
5620           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5621         else
5622           real_c1 = wi::add (c3, c2, sgn, &overflow);
5624         code = cmp_code;
5625         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5626           {
5627             /* Check if c1 equals to real_c1.  Boundary condition is handled
5628                by adjusting comparison operation if necessary.  */
5629             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5630                 && !overflow)
5631               {
5632                 /* X <= Y - 1 equals to X < Y.  */
5633                 if (cmp_code == LE_EXPR)
5634                   code = LT_EXPR;
5635                 /* X > Y - 1 equals to X >= Y.  */
5636                 if (cmp_code == GT_EXPR)
5637                   code = GE_EXPR;
5638               }
5639             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5640                 && !overflow)
5641               {
5642                 /* X < Y + 1 equals to X <= Y.  */
5643                 if (cmp_code == LT_EXPR)
5644                   code = LE_EXPR;
5645                 /* X >= Y + 1 equals to X > Y.  */
5646                 if (cmp_code == GE_EXPR)
5647                   code = GT_EXPR;
5648               }
5649             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5650               {
5651                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5652                   code = MIN_EXPR;
5653                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5654                   code = MAX_EXPR;
5655               }
5656           }
5657       }
5658       (if (code == MAX_EXPR)
5659        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5660            { wide_int_to_tree (from_type, c2); })
5661        (if (code == MIN_EXPR)
5662         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5663             { wide_int_to_tree (from_type, c2); })))))))))
5665 #if GIMPLE
5666 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5667    in fold_cond_expr_with_comparison for GENERIC folding with
5668    some extra constraints.  */
5669 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5670  (simplify
5671   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5672         (convert3? @0) (convert4? @1))
5673   (if (!HONOR_SIGNED_ZEROS (type)
5674        && (/* Allow widening conversions of the compare operands as data.  */
5675            (INTEGRAL_TYPE_P (type)
5676             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5677             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5678             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5679             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5680            /* Or sign conversions for the comparison.  */
5681            || (types_match (type, TREE_TYPE (@0))
5682                && types_match (type, TREE_TYPE (@1)))))
5683    (switch
5684     (if (cmp == EQ_EXPR)
5685      (if (VECTOR_TYPE_P (type))
5686       (view_convert @c1)
5687       (convert @c1)))
5688     (if (cmp == NE_EXPR)
5689      (if (VECTOR_TYPE_P (type))
5690       (view_convert @c0)
5691       (convert @c0)))
5692     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5693      (if (!HONOR_NANS (type))
5694       (if (VECTOR_TYPE_P (type))
5695        (view_convert (min @c0 @c1))
5696        (convert (min @c0 @c1)))))
5697     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5698      (if (!HONOR_NANS (type))
5699       (if (VECTOR_TYPE_P (type))
5700        (view_convert (max @c0 @c1))
5701        (convert (max @c0 @c1)))))
5702     (if (cmp == UNEQ_EXPR)
5703      (if (!HONOR_NANS (type))
5704       (if (VECTOR_TYPE_P (type))
5705        (view_convert @c1)
5706        (convert @c1))))
5707     (if (cmp == LTGT_EXPR)
5708      (if (!HONOR_NANS (type))
5709       (if (VECTOR_TYPE_P (type))
5710        (view_convert @c0)
5711        (convert @c0))))))))
5713 /* This is for VEC_COND_EXPR
5714    Optimize A < B ? A : B to MIN (A, B)
5715             A > B ? A : B to MAX (A, B).  */
5716 (for cmp (lt le ungt unge gt ge unlt unle)
5717      minmax (min min min min max max max max)
5718      MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
5719  (simplify
5720   (vec_cond (cmp @0 @1) @0 @1)
5721    (if (VECTOR_INTEGER_TYPE_P (type)
5722        && target_supports_op_p (type, MINMAX, optab_vector))
5723     (minmax @0 @1))))
5725 (for cmp (lt le ungt unge gt ge unlt unle)
5726      minmax (max max max max min min min min)
5727      MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
5728  (simplify
5729   (vec_cond (cmp @0 @1) @1 @0)
5730    (if (VECTOR_INTEGER_TYPE_P (type)
5731        && target_supports_op_p (type, MINMAX, optab_vector))
5732     (minmax @0 @1))))
5733 #endif
5735 (for cnd (cond vec_cond)
5736  /* (a != b) ? (a - b) : 0 -> (a - b) */
5737  (simplify
5738   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5739   @2)
5740  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5741  (simplify
5742   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5743   @2)
5744  /* (a != b) ? (a & b) : a -> (a & b) */
5745  /* (a != b) ? (a | b) : a -> (a | b) */
5746  /* (a != b) ? min(a,b) : a -> min(a,b) */
5747  /* (a != b) ? max(a,b) : a -> max(a,b) */
5748  (for op (bit_and bit_ior min max)
5749   (simplify
5750    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5751    @2))
5752  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5753  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5754  (for op (mult plus)
5755   (simplify
5756    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5757    (if (ANY_INTEGRAL_TYPE_P (type))
5758     @2)))
5759  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5760  (simplify
5761   (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5762   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5763    @2))
5766 /* These was part of minmax phiopt.  */
5767 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5768    to minmax<min/max<a, b>, c> */
5769 (for minmax (min max)
5770  (for cmp (lt le gt ge ne)
5771   (simplify
5772    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5773    (with
5774     {
5775       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5776     }
5777     (if (code == MIN_EXPR)
5778      (minmax (min @1 @2) @4)
5779      (if (code == MAX_EXPR)
5780       (minmax (max @1 @2) @4)))))))
5782 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5783 (for cmp    (gt  ge  lt  le)
5784      minmax (min min max max)
5785  (simplify
5786   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5787    (with
5788     {
5789       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5790     }
5791     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5792          && code == MIN_EXPR
5793          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5794      (min @2 @4)
5795      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5796           && code == MAX_EXPR
5797           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5798       (max @2 @4))))))
5800 #if GIMPLE
5801 /* These patterns should be after min/max detection as simplifications
5802    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5803    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5804    Even without those, reaching min/max/and/ior faster is better.  */
5805 (simplify
5806  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5807  (switch
5808   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5809   (if (integer_zerop (@2))
5810    (bit_and (convert @0) @1))
5811   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5812   (if (integer_zerop (@1))
5813    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5814   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5815   (if (integer_onep (@1))
5816    (bit_ior (convert @0) @2))
5817   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5818   (if (integer_onep (@2))
5819    (bit_ior (bit_xor (convert @0) @2) @1))
5822 #endif
5824 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5825 (simplify
5826  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5827  (if (!TYPE_SATURATING (type)
5828       && (TYPE_OVERFLOW_WRAPS (type)
5829           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5830       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5831   @3))
5833 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5834 (simplify
5835  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5836  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5837   @3))
5839 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5840 (for op (abs absu)
5841  (simplify
5842   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5843   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5844    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5845     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5846      (convert (absu:utype @0)))
5847     @3))))
5849 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5850    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5851 (simplify
5852  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5853  (if (TYPE_UNSIGNED (type))
5854   (cond (ge @0 @1) (negate @0) @2)))
5856 (for cnd (cond vec_cond)
5857  /* A ? B : (A ? X : C) -> A ? B : C.  */
5858  (simplify
5859   (cnd @0 (cnd @0 @1 @2) @3)
5860   (cnd @0 @1 @3))
5861  (simplify
5862   (cnd @0 @1 (cnd @0 @2 @3))
5863   (cnd @0 @1 @3))
5864  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5865  /* ???  This matches embedded conditions open-coded because genmatch
5866     would generate matching code for conditions in separate stmts only.
5867     The following is still important to merge then and else arm cases
5868     from if-conversion.  */
5869  (simplify
5870   (cnd @0 @1 (cnd @2 @3 @4))
5871   (if (inverse_conditions_p (@0, @2))
5872    (cnd @0 @1 @3)))
5873  (simplify
5874   (cnd @0 (cnd @1 @2 @3) @4)
5875   (if (inverse_conditions_p (@0, @1))
5876    (cnd @0 @3 @4)))
5878  /* A ? B : B -> B.  */
5879  (simplify
5880   (cnd @0 @1 @1)
5881   @1)
5883  /* !A ? B : C -> A ? C : B.  */
5884  (simplify
5885   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5886   (cnd @0 @2 @1)))
5888 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5890    None of these transformations work for modes with signed
5891    zeros.  If A is +/-0, the first two transformations will
5892    change the sign of the result (from +0 to -0, or vice
5893    versa).  The last four will fix the sign of the result,
5894    even though the original expressions could be positive or
5895    negative, depending on the sign of A.
5897    Note that all these transformations are correct if A is
5898    NaN, since the two alternatives (A and -A) are also NaNs.  */
5900 (for cnd (cond vec_cond)
5901  /* A == 0 ? A : -A    same as -A */
5902  (for cmp (eq uneq)
5903   (simplify
5904    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5905     (if (!HONOR_SIGNED_ZEROS (type)
5906          && bitwise_equal_p (@0, @2))
5907      @1))
5908   (simplify
5909    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5910     (if (!HONOR_SIGNED_ZEROS (type)
5911          && bitwise_equal_p (@0, @2))
5912      @1))
5914  /* A != 0 ? A : -A    same as A */
5915  (for cmp (ne ltgt)
5916   (simplify
5917    (cnd (cmp @0 zerop) @1 (negate @1))
5918     (if (!HONOR_SIGNED_ZEROS (type)
5919          && bitwise_equal_p (@0, @1))
5920      @1))
5921   (simplify
5922    (cnd (cmp @0 zerop) @1 integer_zerop)
5923     (if (!HONOR_SIGNED_ZEROS (type)
5924          && bitwise_equal_p (@0, @1))
5925      @1))
5927  /* A >=/> 0 ? A : -A    same as abs (A) */
5928  (for cmp (ge gt)
5929   (simplify
5930    (cnd (cmp @0 zerop) @1 (negate @1))
5931     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5932          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5933          && bitwise_equal_p (@0, @1))
5934      (if (TYPE_UNSIGNED (type))
5935       (absu:type @0)
5936       (abs @0)))))
5937  /* A <=/< 0 ? A : -A    same as -abs (A) */
5938  (for cmp (le lt)
5939   (simplify
5940    (cnd (cmp @0 zerop) @1 (negate @1))
5941     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5942          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5943          && bitwise_equal_p (@0, @1))
5944      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5945            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5946           || TYPE_UNSIGNED (type))
5947       (with {
5948         tree utype = unsigned_type_for (TREE_TYPE(@0));
5949        }
5950        (convert (negate (absu:utype @0))))
5951        (negate (abs @0)))))
5954  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5955  (for cmp (eq uneq)
5956   (simplify
5957    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5958    (if (!HONOR_SIGNED_ZEROS (type))
5959     @3))
5960   (simplify
5961    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5962    @3)
5964  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5965  (for cmp (ne ltgt)
5966   (simplify
5967    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5968    (if (!HONOR_SIGNED_ZEROS (type))
5969     @0))
5970   (simplify
5971    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5972    @0)
5974  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5975  (for cmp (ge gt)
5976   (simplify
5977    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5978    (if (!HONOR_SIGNED_ZEROS (type)
5979         && !TYPE_UNSIGNED (type))
5980     (abs @0))))
5981  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5982  (for cmp (le lt)
5983   (simplify
5984    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5985    (if (!HONOR_SIGNED_ZEROS (type)
5986         && !TYPE_UNSIGNED (type))
5987     (if (ANY_INTEGRAL_TYPE_P (type)
5988          && !TYPE_OVERFLOW_WRAPS (type))
5989      (with {
5990         tree utype = unsigned_type_for (type);
5991       }
5992       (convert (negate (absu:utype @0))))
5993       (negate (abs @0)))))
5997 /* -(type)!A -> (type)A - 1.  */
5998 (simplify
5999  (negate (convert?:s (logical_inverted_value:s @0)))
6000  (if (INTEGRAL_TYPE_P (type)
6001       && TREE_CODE (type) != BOOLEAN_TYPE
6002       && TYPE_PRECISION (type) > 1
6003       && TREE_CODE (@0) == SSA_NAME
6004       && ssa_name_has_boolean_range (@0))
6005   (plus (convert:type @0) { build_all_ones_cst (type); })))
6007 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
6008    return all -1 or all 0 results.  */
6009 /* ??? We could instead convert all instances of the vec_cond to negate,
6010    but that isn't necessarily a win on its own.  */
6011 (simplify
6012  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6013  (if (VECTOR_TYPE_P (type)
6014       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6015                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6016       && (TYPE_MODE (TREE_TYPE (type))
6017           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6018   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6020 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
6021 (simplify
6022  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6023  (if (VECTOR_TYPE_P (type)
6024       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6025                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6026       && (TYPE_MODE (TREE_TYPE (type))
6027           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6028   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6031 /* Simplifications of comparisons.  */
6033 /* See if we can reduce the magnitude of a constant involved in a
6034    comparison by changing the comparison code.  This is a canonicalization
6035    formerly done by maybe_canonicalize_comparison_1.  */
6036 (for cmp  (le gt)
6037      acmp (lt ge)
6038  (simplify
6039   (cmp @0 uniform_integer_cst_p@1)
6040   (with { tree cst = uniform_integer_cst_p (@1); }
6041    (if (tree_int_cst_sgn (cst) == -1)
6042      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6043                                    wide_int_to_tree (TREE_TYPE (cst),
6044                                                      wi::to_wide (cst)
6045                                                      + 1)); })))))
6046 (for cmp  (ge lt)
6047      acmp (gt le)
6048  (simplify
6049   (cmp @0 uniform_integer_cst_p@1)
6050   (with { tree cst = uniform_integer_cst_p (@1); }
6051    (if (tree_int_cst_sgn (cst) == 1)
6052     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6053                                   wide_int_to_tree (TREE_TYPE (cst),
6054                                   wi::to_wide (cst) - 1)); })))))
6056 /* We can simplify a logical negation of a comparison to the
6057    inverted comparison.  As we cannot compute an expression
6058    operator using invert_tree_comparison we have to simulate
6059    that with expression code iteration.  */
6060 (for cmp (tcc_comparison)
6061      icmp (inverted_tcc_comparison)
6062      ncmp (inverted_tcc_comparison_with_nans)
6063  /* Ideally we'd like to combine the following two patterns
6064     and handle some more cases by using
6065       (logical_inverted_value (cmp @0 @1))
6066     here but for that genmatch would need to "inline" that.
6067     For now implement what forward_propagate_comparison did.  */
6068  (simplify
6069   (bit_not (cmp @0 @1))
6070   (if (VECTOR_TYPE_P (type)
6071        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6072    /* Comparison inversion may be impossible for trapping math,
6073       invert_tree_comparison will tell us.  But we can't use
6074       a computed operator in the replacement tree thus we have
6075       to play the trick below.  */
6076    (with { enum tree_code ic = invert_tree_comparison
6077              (cmp, HONOR_NANS (@0)); }
6078     (if (ic == icmp)
6079      (icmp @0 @1)
6080      (if (ic == ncmp)
6081       (ncmp @0 @1))))))
6082  (simplify
6083   (bit_xor (cmp @0 @1) integer_truep)
6084   (with { enum tree_code ic = invert_tree_comparison
6085             (cmp, HONOR_NANS (@0)); }
6086    (if (ic == icmp)
6087     (icmp @0 @1)
6088     (if (ic == ncmp)
6089      (ncmp @0 @1)))))
6090  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
6091  (simplify
6092   (ne (cmp@2 @0 @1) integer_zerop)
6093   (if (types_match (type, TREE_TYPE (@2)))
6094    (cmp @0 @1)))
6095  (simplify
6096   (eq (cmp@2 @0 @1) integer_truep)
6097   (if (types_match (type, TREE_TYPE (@2)))
6098    (cmp @0 @1)))
6099  (simplify
6100   (ne (cmp@2 @0 @1) integer_truep)
6101   (if (types_match (type, TREE_TYPE (@2)))
6102    (with { enum tree_code ic = invert_tree_comparison
6103              (cmp, HONOR_NANS (@0)); }
6104     (if (ic == icmp)
6105      (icmp @0 @1)
6106      (if (ic == ncmp)
6107       (ncmp @0 @1))))))
6108  (simplify
6109   (eq (cmp@2 @0 @1) integer_zerop)
6110   (if (types_match (type, TREE_TYPE (@2)))
6111    (with { enum tree_code ic = invert_tree_comparison
6112              (cmp, HONOR_NANS (@0)); }
6113     (if (ic == icmp)
6114      (icmp @0 @1)
6115      (if (ic == ncmp)
6116       (ncmp @0 @1)))))))
6118 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6119    ??? The transformation is valid for the other operators if overflow
6120    is undefined for the type, but performing it here badly interacts
6121    with the transformation in fold_cond_expr_with_comparison which
6122    attempts to synthetize ABS_EXPR.  */
6123 (for cmp (eq ne)
6124  (for sub (minus pointer_diff)
6125   (simplify
6126    (cmp (sub@2 @0 @1) integer_zerop)
6127    (if (single_use (@2))
6128     (cmp @0 @1)))))
6130 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6131    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
6132 (for cmp (lt ge)
6133  (simplify
6134   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6135    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6136         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6137         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6138     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6139 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6140    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
6141 (simplify
6142  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6143   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6144        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6145        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6146    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6148 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6149    signed arithmetic case.  That form is created by the compiler
6150    often enough for folding it to be of value.  One example is in
6151    computing loop trip counts after Operator Strength Reduction.  */
6152 (for cmp (simple_comparison)
6153      scmp (swapped_simple_comparison)
6154  (simplify
6155   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6156   /* Handle unfolded multiplication by zero.  */
6157   (if (integer_zerop (@1))
6158    (cmp @1 @2)
6159    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6160         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6161         && single_use (@3))
6162     /* If @1 is negative we swap the sense of the comparison.  */
6163     (if (tree_int_cst_sgn (@1) < 0)
6164      (scmp @0 @2)
6165      (cmp @0 @2))))))
6167 /* For integral types with undefined overflow fold
6168    x * C1 == C2 into x == C2 / C1 or false.
6169    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6170    Z / 2^n Z.  */
6171 (for cmp (eq ne)
6172  (simplify
6173   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6174   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6175        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6176        && wi::to_wide (@1) != 0)
6177    (with { widest_int quot; }
6178     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6179                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6180      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6181      { constant_boolean_node (cmp == NE_EXPR, type); }))
6182    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6183         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6184         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6185     (cmp @0
6186      {
6187        tree itype = TREE_TYPE (@0);
6188        int p = TYPE_PRECISION (itype);
6189        wide_int m = wi::one (p + 1) << p;
6190        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6191        wide_int i = wide_int::from (wi::mod_inv (a, m),
6192                                     p, TYPE_SIGN (itype));
6193        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6194      })))))
6196 /* Simplify comparison of something with itself.  For IEEE
6197    floating-point, we can only do some of these simplifications.  */
6198 (for cmp (eq ge le)
6199  (simplify
6200   (cmp @0 @0)
6201   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6202        || ! tree_expr_maybe_nan_p (@0))
6203    { constant_boolean_node (true, type); }
6204    (if (cmp != EQ_EXPR
6205         /* With -ftrapping-math conversion to EQ loses an exception.  */
6206         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6207             || ! flag_trapping_math))
6208     (eq @0 @0)))))
6209 (for cmp (ne gt lt)
6210  (simplify
6211   (cmp @0 @0)
6212   (if (cmp != NE_EXPR
6213        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6214        || ! tree_expr_maybe_nan_p (@0))
6215    { constant_boolean_node (false, type); })))
6216 (for cmp (unle unge uneq)
6217  (simplify
6218   (cmp @0 @0)
6219   { constant_boolean_node (true, type); }))
6220 (for cmp (unlt ungt)
6221  (simplify
6222   (cmp @0 @0)
6223   (unordered @0 @0)))
6224 (simplify
6225  (ltgt @0 @0)
6226  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6227   { constant_boolean_node (false, type); }))
6229 /* x == ~x -> false */
6230 /* x != ~x -> true */
6231 (for cmp (eq ne)
6232  (simplify
6233   (cmp:c @0 (bit_not @0))
6234   { constant_boolean_node (cmp == NE_EXPR, type); }))
6236 /* Fold ~X op ~Y as Y op X.  */
6237 (for cmp (simple_comparison)
6238  (simplify
6239   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6240   (if (single_use (@2) && single_use (@3))
6241    (with { tree otype = TREE_TYPE (@4); }
6242     (cmp (convert:otype @1) (convert:otype @0))))))
6244 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6245 (for cmp (simple_comparison)
6246      scmp (swapped_simple_comparison)
6247  (simplify
6248   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6249   (if (single_use (@2)
6250        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6251    (with { tree otype = TREE_TYPE (@1); }
6252     (scmp (convert:otype @0) (bit_not @1))))))
6254 (for cmp (simple_comparison)
6255  (simplify
6256   (cmp @0 REAL_CST@1)
6257   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6258   (switch
6259    /* a CMP (-0) -> a CMP 0  */
6260    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6261     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6262    /* (-0) CMP b -> 0 CMP b.  */
6263    (if (TREE_CODE (@0) == REAL_CST
6264         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6265     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6266    /* x != NaN is always true, other ops are always false.  */
6267    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6268         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6269         && !tree_expr_signaling_nan_p (@1)
6270         && !tree_expr_maybe_signaling_nan_p (@0))
6271     { constant_boolean_node (cmp == NE_EXPR, type); })
6272    /* NaN != y is always true, other ops are always false.  */
6273    (if (TREE_CODE (@0) == REAL_CST
6274         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6275         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6276         && !tree_expr_signaling_nan_p (@0)
6277         && !tree_expr_signaling_nan_p (@1))
6278     { constant_boolean_node (cmp == NE_EXPR, type); })
6279    /* Fold comparisons against infinity.  */
6280    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6281         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6282     (with
6283      {
6284        REAL_VALUE_TYPE max;
6285        enum tree_code code = cmp;
6286        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6287        if (neg)
6288          code = swap_tree_comparison (code);
6289      }
6290      (switch
6291       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6292       (if (code == GT_EXPR
6293            && !(HONOR_NANS (@0) && flag_trapping_math))
6294        { constant_boolean_node (false, type); })
6295       (if (code == LE_EXPR)
6296        /* x <= +Inf is always true, if we don't care about NaNs.  */
6297        (if (! HONOR_NANS (@0))
6298         { constant_boolean_node (true, type); }
6299         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6300            an "invalid" exception.  */
6301         (if (!flag_trapping_math)
6302          (eq @0 @0))))
6303       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6304          for == this introduces an exception for x a NaN.  */
6305       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6306            || code == GE_EXPR)
6307        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6308         (if (neg)
6309          (lt @0 { build_real (TREE_TYPE (@0), max); })
6310          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6311       /* x < +Inf is always equal to x <= DBL_MAX.  */
6312       (if (code == LT_EXPR)
6313        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6314         (if (neg)
6315          (ge @0 { build_real (TREE_TYPE (@0), max); })
6316          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6317       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6318          an exception for x a NaN so use an unordered comparison.  */
6319       (if (code == NE_EXPR)
6320        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6321         (if (! HONOR_NANS (@0))
6322          (if (neg)
6323           (ge @0 { build_real (TREE_TYPE (@0), max); })
6324           (le @0 { build_real (TREE_TYPE (@0), max); }))
6325          (if (neg)
6326           (unge @0 { build_real (TREE_TYPE (@0), max); })
6327           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6329  /* If this is a comparison of a real constant with a PLUS_EXPR
6330     or a MINUS_EXPR of a real constant, we can convert it into a
6331     comparison with a revised real constant as long as no overflow
6332     occurs when unsafe_math_optimizations are enabled.  */
6333  (if (flag_unsafe_math_optimizations)
6334   (for op (plus minus)
6335    (simplify
6336     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6337     (with
6338      {
6339        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6340                                TREE_TYPE (@1), @2, @1);
6341      }
6342      (if (tem && !TREE_OVERFLOW (tem))
6343       (cmp @0 { tem; }))))))
6345  /* Likewise, we can simplify a comparison of a real constant with
6346     a MINUS_EXPR whose first operand is also a real constant, i.e.
6347     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6348     floating-point types only if -fassociative-math is set.  */
6349  (if (flag_associative_math)
6350   (simplify
6351    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6352    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6353     (if (tem && !TREE_OVERFLOW (tem))
6354      (cmp { tem; } @1)))))
6356  /* Fold comparisons against built-in math functions.  */
6357  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6358   (for sq (SQRT)
6359    (simplify
6360     (cmp (sq @0) REAL_CST@1)
6361     (switch
6362      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6363       (switch
6364        /* sqrt(x) < y is always false, if y is negative.  */
6365        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6366         { constant_boolean_node (false, type); })
6367        /* sqrt(x) > y is always true, if y is negative and we
6368           don't care about NaNs, i.e. negative values of x.  */
6369        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6370         { constant_boolean_node (true, type); })
6371        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6372        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6373      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6374       (switch
6375        /* sqrt(x) < 0 is always false.  */
6376        (if (cmp == LT_EXPR)
6377         { constant_boolean_node (false, type); })
6378        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6379        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6380         { constant_boolean_node (true, type); })
6381        /* sqrt(x) <= 0 -> x == 0.  */
6382        (if (cmp == LE_EXPR)
6383         (eq @0 @1))
6384        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6385           == or !=.  In the last case:
6387             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6389           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6390           the results for other x follow from natural arithmetic.  */
6391        (cmp @0 @1)))
6392      (if ((cmp == LT_EXPR
6393            || cmp == LE_EXPR
6394            || cmp == GT_EXPR
6395            || cmp == GE_EXPR)
6396           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6397           /* Give up for -frounding-math.  */
6398           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6399       (with
6400        {
6401          REAL_VALUE_TYPE c2;
6402          enum tree_code ncmp = cmp;
6403          const real_format *fmt
6404            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6405          real_arithmetic (&c2, MULT_EXPR,
6406                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6407          real_convert (&c2, fmt, &c2);
6408          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6409             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6410          if (!REAL_VALUE_ISINF (c2))
6411            {
6412              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6413                                         build_real (TREE_TYPE (@0), c2));
6414              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6415                ncmp = ERROR_MARK;
6416              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6417                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6418                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6419              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6420                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6421                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6422              else
6423                {
6424                  /* With rounding to even, sqrt of up to 3 different values
6425                     gives the same normal result, so in some cases c2 needs
6426                     to be adjusted.  */
6427                  REAL_VALUE_TYPE c2alt, tow;
6428                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6429                    tow = dconst0;
6430                  else
6431                    tow = dconstinf;
6432                  real_nextafter (&c2alt, fmt, &c2, &tow);
6433                  real_convert (&c2alt, fmt, &c2alt);
6434                  if (REAL_VALUE_ISINF (c2alt))
6435                    ncmp = ERROR_MARK;
6436                  else
6437                    {
6438                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6439                                            build_real (TREE_TYPE (@0), c2alt));
6440                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6441                        ncmp = ERROR_MARK;
6442                      else if (real_equal (&TREE_REAL_CST (c3),
6443                                           &TREE_REAL_CST (@1)))
6444                        c2 = c2alt;
6445                    }
6446                }
6447            }
6448        }
6449        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6450         (if (REAL_VALUE_ISINF (c2))
6451          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6452          (if (HONOR_INFINITIES (@0))
6453           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6454           { constant_boolean_node (false, type); })
6455          /* sqrt(x) > c is the same as x > c*c.  */
6456          (if (ncmp != ERROR_MARK)
6457           (if (ncmp == GE_EXPR)
6458            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6459            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6460         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6461         (if (REAL_VALUE_ISINF (c2))
6462          (switch
6463           /* sqrt(x) < y is always true, when y is a very large
6464              value and we don't care about NaNs or Infinities.  */
6465           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6466            { constant_boolean_node (true, type); })
6467           /* sqrt(x) < y is x != +Inf when y is very large and we
6468              don't care about NaNs.  */
6469           (if (! HONOR_NANS (@0))
6470            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6471           /* sqrt(x) < y is x >= 0 when y is very large and we
6472              don't care about Infinities.  */
6473           (if (! HONOR_INFINITIES (@0))
6474            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6475           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6476           (if (GENERIC)
6477            (truth_andif
6478             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6479             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6480          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6481          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6482           (if (ncmp == LT_EXPR)
6483            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6484            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6485           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6486           (if (ncmp != ERROR_MARK && GENERIC)
6487            (if (ncmp == LT_EXPR)
6488             (truth_andif
6489              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6490              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6491             (truth_andif
6492              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6493              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6494    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6495    (simplify
6496     (cmp (sq @0) (sq @1))
6497       (if (! HONOR_NANS (@0))
6498         (cmp @0 @1))))))
6500 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6501 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6502      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6503  (simplify
6504   (cmp (float@0 @1) (float @2))
6505    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6506         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6507     (with
6508      {
6509        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6510        tree type1 = TREE_TYPE (@1);
6511        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6512        tree type2 = TREE_TYPE (@2);
6513        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6514      }
6515      (if (fmt.can_represent_integral_type_p (type1)
6516           && fmt.can_represent_integral_type_p (type2))
6517       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6518        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6519        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6520             && type1_signed_p >= type2_signed_p)
6521         (icmp @1 (convert @2))
6522         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6523              && type1_signed_p <= type2_signed_p)
6524          (icmp (convert:type2 @1) @2)
6525          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6526               && type1_signed_p == type2_signed_p)
6527           (icmp @1 @2))))))))))
6529 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6530 (for cmp  (lt le eq ne ge gt)
6531      icmp (le le eq ne ge ge)
6532  (simplify
6533   (cmp (float @0) REAL_CST@1)
6534    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6535         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6536     (with
6537      {
6538        tree itype = TREE_TYPE (@0);
6539        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6540        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6541        /* Be careful to preserve any potential exceptions due to
6542           NaNs.  qNaNs are ok in == or != context.
6543           TODO: relax under -fno-trapping-math or
6544           -fno-signaling-nans.  */
6545        bool exception_p
6546          = real_isnan (cst) && (cst->signalling
6547                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6548      }
6549      /* TODO: allow non-fitting itype and SNaNs when
6550         -fno-trapping-math.  */
6551      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6552       (with
6553        {
6554          signop isign = TYPE_SIGN (itype);
6555          REAL_VALUE_TYPE imin, imax;
6556          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6557          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6559          REAL_VALUE_TYPE icst;
6560          if (cmp == GT_EXPR || cmp == GE_EXPR)
6561            real_ceil (&icst, fmt, cst);
6562          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6563            real_floor (&icst, fmt, cst);
6564          else
6565            real_trunc (&icst, fmt, cst);
6567          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6569          bool overflow_p = false;
6570          wide_int icst_val
6571            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6572        }
6573        (switch
6574         /* Optimize cases when CST is outside of ITYPE's range.  */
6575         (if (real_compare (LT_EXPR, cst, &imin))
6576          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6577                                   type); })
6578         (if (real_compare (GT_EXPR, cst, &imax))
6579          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6580                                   type); })
6581         /* Remove cast if CST is an integer representable by ITYPE.  */
6582         (if (cst_int_p)
6583          (cmp @0 { gcc_assert (!overflow_p);
6584                    wide_int_to_tree (itype, icst_val); })
6585         )
6586         /* When CST is fractional, optimize
6587             (FTYPE) N == CST -> 0
6588             (FTYPE) N != CST -> 1.  */
6589         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6590          { constant_boolean_node (cmp == NE_EXPR, type); })
6591         /* Otherwise replace with sensible integer constant.  */
6592         (with
6593          {
6594            gcc_checking_assert (!overflow_p);
6595          }
6596          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6598 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6599 (for cmp (eq ne)
6600  (simplify
6601   (cmp (exact_div @0 @1) INTEGER_CST@2)
6602   (if (!integer_zerop (@1))
6603    (if (wi::to_wide (@2) == 0)
6604     (cmp @0 @2)
6605     (if (TREE_CODE (@1) == INTEGER_CST)
6606      (with
6607       {
6608         wi::overflow_type ovf;
6609         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6610                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6611       }
6612       (if (ovf)
6613        { constant_boolean_node (cmp == NE_EXPR, type); }
6614        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6615 (for cmp (lt le gt ge)
6616  (simplify
6617   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6618   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6619    (with
6620     {
6621       wi::overflow_type ovf;
6622       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6623                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6624     }
6625     (if (ovf)
6626      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6627                                         TYPE_SIGN (TREE_TYPE (@2)))
6628                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6629      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6631 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6633    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6634    For large C (more than min/B+2^size), this is also true, with the
6635    multiplication computed modulo 2^size.
6636    For intermediate C, this just tests the sign of A.  */
6637 (for cmp  (lt le gt ge)
6638      cmp2 (ge ge lt lt)
6639  (simplify
6640   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6641   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6642        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6643        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6644    (with
6645     {
6646       tree utype = TREE_TYPE (@2);
6647       wide_int denom = wi::to_wide (@1);
6648       wide_int right = wi::to_wide (@2);
6649       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6650       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6651       bool small = wi::leu_p (right, smax);
6652       bool large = wi::geu_p (right, smin);
6653     }
6654     (if (small || large)
6655      (cmp (convert:utype @0) (mult @2 (convert @1)))
6656      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6658 /* Unordered tests if either argument is a NaN.  */
6659 (simplify
6660  (bit_ior (unordered @0 @0) (unordered @1 @1))
6661  (if (types_match (@0, @1))
6662   (unordered @0 @1)))
6663 (simplify
6664  (bit_and (ordered @0 @0) (ordered @1 @1))
6665  (if (types_match (@0, @1))
6666   (ordered @0 @1)))
6667 (simplify
6668  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6669  @2)
6670 (simplify
6671  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6672  @2)
6674 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6675    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6677    Note that comparisons
6678      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6679      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6680    will be canonicalized to above so there's no need to
6681    consider them here.
6682  */
6684 (for cmp (le gt)
6685      eqcmp (eq ne)
6686  (simplify
6687   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6688   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6689    (with
6690     {
6691      tree ty = TREE_TYPE (@0);
6692      unsigned prec = TYPE_PRECISION (ty);
6693      wide_int mask = wi::to_wide (@2, prec);
6694      wide_int rhs = wi::to_wide (@3, prec);
6695      signop sgn = TYPE_SIGN (ty);
6696     }
6697     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6698          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6699       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6700              { build_zero_cst (ty); }))))))
6702 /* -A CMP -B -> B CMP A.  */
6703 (for cmp (tcc_comparison)
6704      scmp (swapped_tcc_comparison)
6705  (simplify
6706   (cmp (negate @0) (negate @1))
6707   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6708        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6709            && (cmp == EQ_EXPR
6710                || cmp == NE_EXPR
6711                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6712    (scmp @0 @1)))
6713  (simplify
6714   (cmp (negate @0) CONSTANT_CLASS_P@1)
6715   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6716        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6717            && (cmp == EQ_EXPR
6718                || cmp == NE_EXPR
6719                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6720    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6721     (if (tem && !TREE_OVERFLOW (tem))
6722      (scmp @0 { tem; }))))))
6724 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6725 (for op (abs absu)
6726  (for eqne (eq ne)
6727   (simplify
6728    (eqne (op @0) zerop@1)
6729    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6731 /* From fold_sign_changed_comparison and fold_widened_comparison.
6732    FIXME: the lack of symmetry is disturbing.  */
6733 (for cmp (simple_comparison)
6734  (simplify
6735   (cmp (convert@0 @00) (convert?@1 @10))
6736   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6737        /* Disable this optimization if we're casting a function pointer
6738           type on targets that require function pointer canonicalization.  */
6739        && !(targetm.have_canonicalize_funcptr_for_compare ()
6740             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6741                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6742                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6743                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6744        && single_use (@0))
6745    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6746         && (TREE_CODE (@10) == INTEGER_CST
6747             || @1 != @10)
6748         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6749             || cmp == NE_EXPR
6750             || cmp == EQ_EXPR)
6751         && !POINTER_TYPE_P (TREE_TYPE (@00))
6752         /* (int)bool:32 != (int)uint is not the same as
6753            bool:32 != (bool:32)uint since boolean types only have two valid
6754            values independent of their precision.  */
6755         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6756             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6757     /* ???  The special-casing of INTEGER_CST conversion was in the original
6758        code and here to avoid a spurious overflow flag on the resulting
6759        constant which fold_convert produces.  */
6760     (if (TREE_CODE (@1) == INTEGER_CST)
6761      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6762                                 wide_int::from (wi::to_wide (@1),
6763                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6764                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6765                                                 TYPE_SIGN (TREE_TYPE (@1))),
6766                                 0, TREE_OVERFLOW (@1)); })
6767      (cmp @00 (convert @1)))
6769     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6770      /* If possible, express the comparison in the shorter mode.  */
6771      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6772            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6773            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6774                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6775           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6776               || ((TYPE_PRECISION (TREE_TYPE (@00))
6777                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6778                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6779                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6780               || (TREE_CODE (@1) == INTEGER_CST
6781                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6782                   && int_fits_type_p (@1, TREE_TYPE (@00)))))
6783       (cmp @00 (convert @10))
6784       (if (TREE_CODE (@1) == INTEGER_CST
6785            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6786            && !int_fits_type_p (@1, TREE_TYPE (@00)))
6787        (with
6788         {
6789           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6790           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6791           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @1));
6792           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @1, min));
6793         }
6794         (if (above || below)
6795          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6796           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6797           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6798            { constant_boolean_node (above ? true : false, type); }
6799            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6800             { constant_boolean_node (above ? false : true, type); })))))))))
6801    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6802    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6803         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6804             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6805         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6806             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6807     (with
6808      {
6809        tree type1 = TREE_TYPE (@10);
6810        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6811          {
6812            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6813            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6814                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6815              type1 = float_type_node;
6816            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6817                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6818              type1 = double_type_node;
6819          }
6820        tree newtype
6821          = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6822             ? TREE_TYPE (@00) : type1);
6823      }
6824      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
6825           && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
6826       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6829 (for cmp (eq ne)
6830  (simplify
6831   /* SSA names are canonicalized to 2nd place.  */
6832   (cmp addr@0 SSA_NAME@1)
6833   (with
6834    {
6835      poly_int64 off; tree base;
6836      tree addr = (TREE_CODE (@0) == SSA_NAME
6837                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6838    }
6839    /* A local variable can never be pointed to by
6840       the default SSA name of an incoming parameter.  */
6841    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6842         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6843         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6844         && TREE_CODE (base) == VAR_DECL
6845         && auto_var_in_fn_p (base, current_function_decl))
6846     (if (cmp == NE_EXPR)
6847      { constant_boolean_node (true, type); }
6848      { constant_boolean_node (false, type); })
6849     /* If the address is based on @1 decide using the offset.  */
6850     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6851          && TREE_CODE (base) == MEM_REF
6852          && TREE_OPERAND (base, 0) == @1)
6853      (with { off += mem_ref_offset (base).force_shwi (); }
6854       (if (known_ne (off, 0))
6855        { constant_boolean_node (cmp == NE_EXPR, type); }
6856        (if (known_eq (off, 0))
6857         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6859 /* Equality compare simplifications from fold_binary  */
6860 (for cmp (eq ne)
6862  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6863     Similarly for NE_EXPR.  */
6864  (simplify
6865   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6866   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6867        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6868    { constant_boolean_node (cmp == NE_EXPR, type); }))
6870  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6871  (simplify
6872   (cmp (bit_xor @0 @1) integer_zerop)
6873   (cmp @0 @1))
6875  /* (X ^ Y) == Y becomes X == 0.
6876     Likewise (X ^ Y) == X becomes Y == 0.  */
6877  (simplify
6878   (cmp:c (bit_xor:c @0 @1) @0)
6879   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6881  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6882  (simplify
6883   (cmp:c (bit_and:c @0 @1) @0)
6884   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6885  (simplify
6886   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6887   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6888        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6889        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6890        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6891        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6892        && !wi::neg_p (wi::to_wide (@1)))
6893    (cmp (bit_and @0 (convert (bit_not @1)))
6894         { build_zero_cst (TREE_TYPE (@0)); })))
6896  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6897  (simplify
6898   (cmp:c (bit_ior:c @0 @1) @1)
6899   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6901  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6902  (simplify
6903   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6904   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6905    (cmp @0 (bit_xor @1 (convert @2)))))
6907  (simplify
6908   (cmp (nop_convert? @0) integer_zerop)
6909   (if (tree_expr_nonzero_p (@0))
6910    { constant_boolean_node (cmp == NE_EXPR, type); }))
6912  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6913  (simplify
6914   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6915   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6917 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6918    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6919    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6920    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6921 (for cmp (eq ne)
6922      ncmp (ge lt)
6923  (for sgncmp (ge lt)
6924   (simplify
6925    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6926    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6927         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6928         && types_match (@0, @1))
6929     (ncmp (bit_xor @0 @1) @2)))))
6930 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6931    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6932 (for cmp (eq ne)
6933      ncmp (lt ge)
6934  (simplify
6935   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6936    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6937         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6938         && types_match (@0, @1))
6939     (ncmp (bit_xor @0 @1) @2))))
6941 /* If we have (A & C) == C where C is a power of 2, convert this into
6942    (A & C) != 0.  Similarly for NE_EXPR.  */
6943 (for cmp (eq ne)
6944      icmp (ne eq)
6945  (simplify
6946   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6947   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6949 #if GIMPLE
6950 /* From fold_binary_op_with_conditional_arg handle the case of
6951    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6952    compares simplify.  */
6953 (for cmp (simple_comparison)
6954  (simplify
6955   (cmp:c (cond @0 @1 @2) @3)
6956   /* Do not move possibly trapping operations into the conditional as this
6957      pessimizes code and causes gimplification issues when applied late.  */
6958   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6959        || !operation_could_trap_p (cmp, true, false, @3))
6960    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6961 #endif
6963 (for cmp (ge lt)
6964 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6965 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6966  (simplify
6967   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6968    (if (INTEGRAL_TYPE_P (type)
6969         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6970         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6971         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6972     (with
6973      {
6974        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6975      }
6976     (if (cmp == LT_EXPR)
6977      (bit_xor (convert (rshift @0 {shifter;})) @1)
6978      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6979 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6980 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6981  (simplify
6982   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6983    (if (INTEGRAL_TYPE_P (type)
6984         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6985         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6986         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6987     (with
6988      {
6989        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6990      }
6991     (if (cmp == GE_EXPR)
6992      (bit_xor (convert (rshift @0 {shifter;})) @1)
6993      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6995 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6996    convert this into a shift followed by ANDing with D.  */
6997 (simplify
6998  (cond
6999   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
7000   INTEGER_CST@2 integer_zerop)
7001  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
7002   (with {
7003      int shift = (wi::exact_log2 (wi::to_wide (@2))
7004                   - wi::exact_log2 (wi::to_wide (@1)));
7005    }
7006    (if (shift > 0)
7007     (bit_and
7008      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
7009     (bit_and
7010      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
7011      @2)))))
7013 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7014    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7015 (for cmp (eq ne)
7016      ncmp (ge lt)
7017  (simplify
7018   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
7019   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7020        && type_has_mode_precision_p (TREE_TYPE (@0))
7021        && element_precision (@2) >= element_precision (@0)
7022        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
7023    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
7024     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7026 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7027    this into a right shift or sign extension followed by ANDing with C.  */
7028 (simplify
7029  (cond
7030   (lt @0 integer_zerop)
7031   INTEGER_CST@1 integer_zerop)
7032  (if (integer_pow2p (@1)
7033       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7034   (with {
7035     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7036    }
7037    (if (shift >= 0)
7038     (bit_and
7039      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7040      @1)
7041     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7042        sign extension followed by AND with C will achieve the effect.  */
7043     (bit_and (convert @0) @1)))))
7045 /* When the addresses are not directly of decls compare base and offset.
7046    This implements some remaining parts of fold_comparison address
7047    comparisons but still no complete part of it.  Still it is good
7048    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
7049 (for cmp (simple_comparison)
7050  (simplify
7051   (cmp (convert1?@2 addr@0) (convert2? addr@1))
7052   (with
7053    {
7054      poly_int64 off0, off1;
7055      tree base0, base1;
7056      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7057                                   off0, off1, GENERIC);
7058    }
7059    (if (equal == 1)
7060     (switch
7061      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7062       { constant_boolean_node (known_eq (off0, off1), type); })
7063      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7064       { constant_boolean_node (known_ne (off0, off1), type); })
7065      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7066       { constant_boolean_node (known_lt (off0, off1), type); })
7067      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7068       { constant_boolean_node (known_le (off0, off1), type); })
7069      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7070       { constant_boolean_node (known_ge (off0, off1), type); })
7071      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7072       { constant_boolean_node (known_gt (off0, off1), type); }))
7073     (if (equal == 0)
7074      (switch
7075       (if (cmp == EQ_EXPR)
7076        { constant_boolean_node (false, type); })
7077       (if (cmp == NE_EXPR)
7078        { constant_boolean_node (true, type); })))))))
7080 #if GIMPLE
7081 /* a?~t:t -> (-(a))^t */
7082 (simplify
7083  (cond @0 @1 @2)
7084  (with { bool wascmp; }
7085   (if (INTEGRAL_TYPE_P (type)
7086        && bitwise_inverted_equal_p (@1, @2, wascmp)
7087        && (!wascmp || TYPE_PRECISION (type) == 1))
7088    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7089         || TYPE_PRECISION (type) == 1)
7090     (bit_xor (convert:type @0) @2)
7091     (bit_xor (negate (convert:type @0)) @2)))))
7092 #endif
7094 /* Simplify pointer equality compares using PTA.  */
7095 (for neeq (ne eq)
7096  (simplify
7097   (neeq @0 @1)
7098   (if (POINTER_TYPE_P (TREE_TYPE (@0))
7099        && ptrs_compare_unequal (@0, @1))
7100    { constant_boolean_node (neeq != EQ_EXPR, type); })))
7102 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7103    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7104    Disable the transform if either operand is pointer to function.
7105    This broke pr22051-2.c for arm where function pointer
7106    canonicalizaion is not wanted.  */
7108 (for cmp (ne eq)
7109  (simplify
7110   (cmp (convert @0) INTEGER_CST@1)
7111   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7112          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7113          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7114          /* Don't perform this optimization in GENERIC if @0 has reference
7115             type when sanitizing.  See PR101210.  */
7116          && !(GENERIC
7117               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7118               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7119         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7120             && POINTER_TYPE_P (TREE_TYPE (@1))
7121             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7122        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7123    (cmp @0 (convert @1)))))
7125 /* Non-equality compare simplifications from fold_binary  */
7126 (for cmp (lt gt le ge)
7127  /* Comparisons with the highest or lowest possible integer of
7128     the specified precision will have known values.  */
7129  (simplify
7130   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7131   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7132         || POINTER_TYPE_P (TREE_TYPE (@1))
7133         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7134        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7135    (with
7136     {
7137       tree cst = uniform_integer_cst_p (@1);
7138       tree arg1_type = TREE_TYPE (cst);
7139       unsigned int prec = TYPE_PRECISION (arg1_type);
7140       wide_int max = wi::max_value (arg1_type);
7141       wide_int signed_max = wi::max_value (prec, SIGNED);
7142       wide_int min = wi::min_value (arg1_type);
7143     }
7144     (switch
7145      (if (wi::to_wide (cst) == max)
7146       (switch
7147        (if (cmp == GT_EXPR)
7148         { constant_boolean_node (false, type); })
7149        (if (cmp == GE_EXPR)
7150         (eq @2 @1))
7151        (if (cmp == LE_EXPR)
7152         { constant_boolean_node (true, type); })
7153        (if (cmp == LT_EXPR)
7154         (ne @2 @1))))
7155      (if (wi::to_wide (cst) == min)
7156       (switch
7157        (if (cmp == LT_EXPR)
7158         { constant_boolean_node (false, type); })
7159        (if (cmp == LE_EXPR)
7160         (eq @2 @1))
7161        (if (cmp == GE_EXPR)
7162         { constant_boolean_node (true, type); })
7163        (if (cmp == GT_EXPR)
7164         (ne @2 @1))))
7165      (if (wi::to_wide (cst) == max - 1)
7166       (switch
7167        (if (cmp == GT_EXPR)
7168         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7169                                     wide_int_to_tree (TREE_TYPE (cst),
7170                                                       wi::to_wide (cst)
7171                                                       + 1)); }))
7172        (if (cmp == LE_EXPR)
7173         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7174                                     wide_int_to_tree (TREE_TYPE (cst),
7175                                                       wi::to_wide (cst)
7176                                                       + 1)); }))))
7177      (if (wi::to_wide (cst) == min + 1)
7178       (switch
7179        (if (cmp == GE_EXPR)
7180         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7181                                     wide_int_to_tree (TREE_TYPE (cst),
7182                                                       wi::to_wide (cst)
7183                                                       - 1)); }))
7184        (if (cmp == LT_EXPR)
7185         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7186                                     wide_int_to_tree (TREE_TYPE (cst),
7187                                                       wi::to_wide (cst)
7188                                                       - 1)); }))))
7189      (if (wi::to_wide (cst) == signed_max
7190           && TYPE_UNSIGNED (arg1_type)
7191           && TYPE_MODE (arg1_type) != BLKmode
7192           /* We will flip the signedness of the comparison operator
7193              associated with the mode of @1, so the sign bit is
7194              specified by this mode.  Check that @1 is the signed
7195              max associated with this sign bit.  */
7196           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7197           /* signed_type does not work on pointer types.  */
7198           && INTEGRAL_TYPE_P (arg1_type))
7199       /* The following case also applies to X < signed_max+1
7200          and X >= signed_max+1 because previous transformations.  */
7201       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7202        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7203         (switch
7204          (if (cst == @1 && cmp == LE_EXPR)
7205           (ge (convert:st @0) { build_zero_cst (st); }))
7206          (if (cst == @1 && cmp == GT_EXPR)
7207           (lt (convert:st @0) { build_zero_cst (st); }))
7208          (if (cmp == LE_EXPR)
7209           (ge (view_convert:st @0) { build_zero_cst (st); }))
7210          (if (cmp == GT_EXPR)
7211           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7213 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7214 (simplify
7215  (lt:c @0 (convert (ne @0 integer_zerop)))
7216  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7217   { constant_boolean_node (false, type); }))
7219 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7220 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7221 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7222 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7223 (for outer (ne eq)
7224  (for inner (ne eq)
7225   (simplify
7226    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7227    (with {
7228      bool cst1 = integer_onep (@1);
7229      bool cst0 = integer_zerop (@1);
7230      bool innereq = inner == EQ_EXPR;
7231      bool outereq = outer == EQ_EXPR;
7232     }
7233     (switch
7234      (if (innereq ? cst0 : cst1)
7235       { constant_boolean_node (!outereq, type); })
7236      (if (innereq ? cst1 : cst0)
7237       (with {
7238         tree utype = unsigned_type_for (TREE_TYPE (@0));
7239         tree ucst1 = build_one_cst (utype);
7240        }
7241        (if (!outereq)
7242         (gt (convert:utype @0) { ucst1; })
7243         (le (convert:utype @0) { ucst1; })
7244        )
7245       )
7246      )
7247      (with {
7248        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7249       }
7250       (if (outereq)
7251        (eq @0 { value; })
7252        (ne @0 { value; })
7253       )
7254      )
7255     )
7256    )
7257   )
7261 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7262  /* If the second operand is NaN, the result is constant.  */
7263  (simplify
7264   (cmp @0 REAL_CST@1)
7265   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7266        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7267    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7268                             ? false : true, type); })))
7270 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7271 (simplify
7272   (unordered @0 @1)
7273   (switch
7274     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7275         { constant_boolean_node (true, type); })
7276     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7277         { constant_boolean_node (false, type); })))
7279 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7280 (simplify
7281   (ordered @0 @1)
7282   (switch
7283     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7284         { constant_boolean_node (false, type); })
7285     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7286         { constant_boolean_node (true, type); })))
7288 /* bool_var != 0 becomes bool_var.  */
7289 (simplify
7290  (ne @0 integer_zerop)
7291  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7292       && types_match (type, TREE_TYPE (@0)))
7293   (non_lvalue @0)))
7294 /* bool_var == 1 becomes bool_var.  */
7295 (simplify
7296  (eq @0 integer_onep)
7297  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7298       && types_match (type, TREE_TYPE (@0)))
7299   (non_lvalue @0)))
7300 /* Do not handle
7301    bool_var == 0 becomes !bool_var or
7302    bool_var != 1 becomes !bool_var
7303    here because that only is good in assignment context as long
7304    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7305    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7306    clearly less optimal and which we'll transform again in forwprop.  */
7308 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7309    where ~Y + 1 == pow2 and Z = ~Y.  */
7310 (for cst (VECTOR_CST INTEGER_CST)
7311  (for cmp (eq ne)
7312       icmp (le gt)
7313   (simplify
7314    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7315     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7316      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7317       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7318                          ? optab_vector : optab_default;
7319               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7320        (if (target_supports_op_p (utype, icmp, optab)
7321             || (optimize_vectors_before_lowering_p ()
7322                 && (!target_supports_op_p (type, cmp, optab)
7323                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7324         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7325          (icmp @0 { csts; })
7326          (icmp (view_convert:utype @0) { csts; })))))))))
7328 /* When one argument is a constant, overflow detection can be simplified.
7329    Currently restricted to single use so as not to interfere too much with
7330    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7331    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7332 (for cmp (lt le ge gt)
7333      out (gt gt le le)
7334  (simplify
7335   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7336   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7337        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7338        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7339        && wi::to_wide (@1) != 0
7340        && single_use (@2))
7341    (with {
7342      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7343      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7344     }
7345     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7346                                 wi::max_value (prec, sign)
7347                                 - wi::to_wide (@1)); })))))
7349 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7350    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7351    expects the long form, so we restrict the transformation for now.  */
7352 (for cmp (gt le)
7353  (simplify
7354   (cmp:c (minus@2 @0 @1) @0)
7355   (if (single_use (@2)
7356        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7357        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7358    (cmp @1 @0))))
7360 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7361 (for cmp (ge lt)
7362  (simplify
7363   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7364    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7365         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7366     (cmp @1 @0))))
7368 /* Testing for overflow is unnecessary if we already know the result.  */
7369 /* A - B > A  */
7370 (for cmp (gt le)
7371      out (ne eq)
7372  (simplify
7373   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7374   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7375        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7376    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7377 /* A + B < A  */
7378 (for cmp (lt ge)
7379      out (ne eq)
7380  (simplify
7381   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7382   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7383        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7384    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7386 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7387    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7388 (for cmp (lt ge)
7389      out (ne eq)
7390  (simplify
7391   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7392   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7393    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7394     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7396 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7397    is at least twice as wide as type of A and B, simplify to
7398    __builtin_mul_overflow (A, B, <unused>).  */
7399 (for cmp (eq ne)
7400  (simplify
7401   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7402        integer_zerop)
7403   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7404        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7405        && TYPE_UNSIGNED (TREE_TYPE (@0))
7406        && (TYPE_PRECISION (TREE_TYPE (@3))
7407            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7408        && tree_fits_uhwi_p (@2)
7409        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7410        && types_match (@0, @1)
7411        && type_has_mode_precision_p (TREE_TYPE (@0))
7412        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7413            != CODE_FOR_nothing))
7414    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7415     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7417 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7418 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7419  (simplify
7420   (ovf (convert@2 @0) @1)
7421   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7422        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7423        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7424        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7425    (ovf @0 @1)))
7426  (simplify
7427   (ovf @1 (convert@2 @0))
7428   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7429        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7430        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7431        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7432    (ovf @1 @0))))
7434 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7435    are unsigned to x > (umax / cst).  Similarly for signed type, but
7436    in that case it needs to be outside of a range.  */
7437 (simplify
7438  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7439   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7440        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7441        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7442        && int_fits_type_p (@1, TREE_TYPE (@0)))
7443    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7444     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7445     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7446      (if (integer_minus_onep (@1))
7447       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7448       (with
7449        {
7450          tree div = fold_convert (TREE_TYPE (@0), @1);
7451          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7452                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7453          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7454                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7455          tree etype = range_check_type (TREE_TYPE (@0));
7456          if (etype)
7457            {
7458              if (wi::neg_p (wi::to_wide (div)))
7459                std::swap (lo, hi);
7460              lo = fold_convert (etype, lo);
7461              hi = fold_convert (etype, hi);
7462              hi = int_const_binop (MINUS_EXPR, hi, lo);
7463            }
7464        }
7465        (if (etype)
7466         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7468 /* Simplification of math builtins.  These rules must all be optimizations
7469    as well as IL simplifications.  If there is a possibility that the new
7470    form could be a pessimization, the rule should go in the canonicalization
7471    section that follows this one.
7473    Rules can generally go in this section if they satisfy one of
7474    the following:
7476    - the rule describes an identity
7478    - the rule replaces calls with something as simple as addition or
7479      multiplication
7481    - the rule contains unary calls only and simplifies the surrounding
7482      arithmetic.  (The idea here is to exclude non-unary calls in which
7483      one operand is constant and in which the call is known to be cheap
7484      when the operand has that value.)  */
7486 (if (flag_unsafe_math_optimizations)
7487  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7488  (simplify
7489   (mult (SQRT_ALL@1 @0) @1)
7490   (if (!tree_expr_maybe_signaling_nan_p (@0))
7491    @0))
7493  (for op (plus minus)
7494   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7495   (simplify
7496    (op (rdiv @0 @1)
7497        (rdiv @2 @1))
7498    (rdiv (op @0 @2) @1)))
7500  (for cmp (lt le gt ge)
7501       neg_cmp (gt ge lt le)
7502   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7503   (simplify
7504    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7505    (with
7506     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7507     (if (tem
7508          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7509               || (real_zerop (tem) && !real_zerop (@1))))
7510      (switch
7511       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7512        (cmp @0 { tem; }))
7513       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7514        (neg_cmp @0 { tem; })))))))
7516  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7517  (for root (SQRT CBRT)
7518   (simplify
7519    (mult (root:s @0) (root:s @1))
7520     (root (mult @0 @1))))
7522  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7523  (for exps (EXP EXP2 EXP10 POW10)
7524   (simplify
7525    (mult (exps:s @0) (exps:s @1))
7526     (exps (plus @0 @1))))
7528  /* Simplify a/root(b/c) into a*root(c/b).  */
7529  (for root (SQRT CBRT)
7530   (simplify
7531    (rdiv @0 (root:s (rdiv:s @1 @2)))
7532     (mult @0 (root (rdiv @2 @1)))))
7534  /* Simplify x/expN(y) into x*expN(-y).  */
7535  (for exps (EXP EXP2 EXP10 POW10)
7536   (simplify
7537    (rdiv @0 (exps:s @1))
7538     (mult @0 (exps (negate @1)))))
7540  (for logs (LOG LOG2 LOG10 LOG10)
7541       exps (EXP EXP2 EXP10 POW10)
7542   /* logN(expN(x)) -> x.  */
7543   (simplify
7544    (logs (exps @0))
7545    @0)
7546   /* expN(logN(x)) -> x.  */
7547   (simplify
7548    (exps (logs @0))
7549    @0))
7551  /* Optimize logN(func()) for various exponential functions.  We
7552     want to determine the value "x" and the power "exponent" in
7553     order to transform logN(x**exponent) into exponent*logN(x).  */
7554  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7555       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7556   (simplify
7557    (logs (exps @0))
7558    (if (SCALAR_FLOAT_TYPE_P (type))
7559     (with {
7560       tree x;
7561       switch (exps)
7562         {
7563         CASE_CFN_EXP:
7564           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7565           x = build_real_truncate (type, dconst_e ());
7566           break;
7567         CASE_CFN_EXP2:
7568           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7569           x = build_real (type, dconst2);
7570           break;
7571         CASE_CFN_EXP10:
7572         CASE_CFN_POW10:
7573           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7574           {
7575             REAL_VALUE_TYPE dconst10;
7576             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7577             x = build_real (type, dconst10);
7578           }
7579           break;
7580         default:
7581           gcc_unreachable ();
7582         }
7583       }
7584      (mult (logs { x; }) @0)))))
7586  (for logs (LOG LOG
7587             LOG2 LOG2
7588             LOG10 LOG10)
7589       exps (SQRT CBRT)
7590   (simplify
7591    (logs (exps @0))
7592    (if (SCALAR_FLOAT_TYPE_P (type))
7593     (with {
7594       tree x;
7595       switch (exps)
7596         {
7597         CASE_CFN_SQRT:
7598           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7599           x = build_real (type, dconsthalf);
7600           break;
7601         CASE_CFN_CBRT:
7602           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7603           x = build_real_truncate (type, dconst_third ());
7604           break;
7605         default:
7606           gcc_unreachable ();
7607         }
7608       }
7609      (mult { x; } (logs @0))))))
7611  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7612  (for logs (LOG LOG2 LOG10)
7613       pows (POW)
7614   (simplify
7615    (logs (pows @0 @1))
7616    (mult @1 (logs @0))))
7618  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7619     or if C is a positive power of 2,
7620     pow(C,x) -> exp2(log2(C)*x).  */
7621 #if GIMPLE
7622  (for pows (POW)
7623       exps (EXP)
7624       logs (LOG)
7625       exp2s (EXP2)
7626       log2s (LOG2)
7627   (simplify
7628    (pows REAL_CST@0 @1)
7629    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7630         && real_isfinite (TREE_REAL_CST_PTR (@0))
7631         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7632            the use_exp2 case until after vectorization.  It seems actually
7633            beneficial for all constants to postpone this until later,
7634            because exp(log(C)*x), while faster, will have worse precision
7635            and if x folds into a constant too, that is unnecessary
7636            pessimization.  */
7637         && canonicalize_math_after_vectorization_p ())
7638     (with {
7639        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7640        bool use_exp2 = false;
7641        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7642            && value->cl == rvc_normal)
7643          {
7644            REAL_VALUE_TYPE frac_rvt = *value;
7645            SET_REAL_EXP (&frac_rvt, 1);
7646            if (real_equal (&frac_rvt, &dconst1))
7647              use_exp2 = true;
7648          }
7649      }
7650      (if (!use_exp2)
7651       (if (optimize_pow_to_exp (@0, @1))
7652        (exps (mult (logs @0) @1)))
7653       (exp2s (mult (log2s @0) @1)))))))
7654 #endif
7656  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7657  (for pows (POW)
7658       exps (EXP EXP2 EXP10 POW10)
7659       logs (LOG LOG2 LOG10 LOG10)
7660   (simplify
7661    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7662    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7663         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7664     (exps (plus (mult (logs @0) @1) @2)))))
7666  (for sqrts (SQRT)
7667       cbrts (CBRT)
7668       pows (POW)
7669       exps (EXP EXP2 EXP10 POW10)
7670   /* sqrt(expN(x)) -> expN(x*0.5).  */
7671   (simplify
7672    (sqrts (exps @0))
7673    (exps (mult @0 { build_real (type, dconsthalf); })))
7674   /* cbrt(expN(x)) -> expN(x/3).  */
7675   (simplify
7676    (cbrts (exps @0))
7677    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7678   /* pow(expN(x), y) -> expN(x*y).  */
7679   (simplify
7680    (pows (exps @0) @1)
7681    (exps (mult @0 @1))))
7683  /* tan(atan(x)) -> x.  */
7684  (for tans (TAN)
7685       atans (ATAN)
7686   (simplify
7687    (tans (atans @0))
7688    @0)))
7690  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7691  (for sins (SIN)
7692       atans (ATAN)
7693       sqrts (SQRT)
7694       copysigns (COPYSIGN)
7695   (simplify
7696    (sins (atans:s @0))
7697    (with
7698      {
7699       REAL_VALUE_TYPE r_cst;
7700       build_sinatan_real (&r_cst, type);
7701       tree t_cst = build_real (type, r_cst);
7702       tree t_one = build_one_cst (type);
7703      }
7704     (if (SCALAR_FLOAT_TYPE_P (type))
7705      (cond (lt (abs @0) { t_cst; })
7706       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7707       (copysigns { t_one; } @0))))))
7709 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7710  (for coss (COS)
7711       atans (ATAN)
7712       sqrts (SQRT)
7713       copysigns (COPYSIGN)
7714   (simplify
7715    (coss (atans:s @0))
7716    (with
7717      {
7718       REAL_VALUE_TYPE r_cst;
7719       build_sinatan_real (&r_cst, type);
7720       tree t_cst = build_real (type, r_cst);
7721       tree t_one = build_one_cst (type);
7722       tree t_zero = build_zero_cst (type);
7723      }
7724     (if (SCALAR_FLOAT_TYPE_P (type))
7725      (cond (lt (abs @0) { t_cst; })
7726       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7727       (copysigns { t_zero; } @0))))))
7729  (if (!flag_errno_math)
7730   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7731   (for sinhs (SINH)
7732        atanhs (ATANH)
7733        sqrts (SQRT)
7734    (simplify
7735     (sinhs (atanhs:s @0))
7736     (with { tree t_one = build_one_cst (type); }
7737     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7739   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7740   (for coshs (COSH)
7741        atanhs (ATANH)
7742        sqrts (SQRT)
7743    (simplify
7744     (coshs (atanhs:s @0))
7745     (with { tree t_one = build_one_cst (type); }
7746     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7748 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7749 (simplify
7750  (CABS (complex:C @0 real_zerop@1))
7751  (abs @0))
7753 /* trunc(trunc(x)) -> trunc(x), etc.  */
7754 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7755  (simplify
7756   (fns (fns @0))
7757   (fns @0)))
7758 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7759 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7760  (simplify
7761   (fns integer_valued_real_p@0)
7762   @0))
7764 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7765 (simplify
7766  (HYPOT:c @0 real_zerop@1)
7767  (abs @0))
7769 /* pow(1,x) -> 1.  */
7770 (simplify
7771  (POW real_onep@0 @1)
7772  @0)
7774 (simplify
7775  /* copysign(x,x) -> x.  */
7776  (COPYSIGN_ALL @0 @0)
7777  @0)
7779 (simplify
7780  /* copysign(x,-x) -> -x.  */
7781  (COPYSIGN_ALL @0 (negate@1 @0))
7782  @1)
7784 (simplify
7785  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7786  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7787  (abs @0))
7789 (simplify
7790  /* fabs (copysign(x, y)) -> fabs (x).  */
7791  (abs (COPYSIGN_ALL @0 @1))
7792  (abs @0))
7794 (for scale (LDEXP SCALBN SCALBLN)
7795  /* ldexp(0, x) -> 0.  */
7796  (simplify
7797   (scale real_zerop@0 @1)
7798   @0)
7799  /* ldexp(x, 0) -> x.  */
7800  (simplify
7801   (scale @0 integer_zerop@1)
7802   @0)
7803  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7804  (simplify
7805   (scale REAL_CST@0 @1)
7806   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7807    @0)))
7809 /* Canonicalization of sequences of math builtins.  These rules represent
7810    IL simplifications but are not necessarily optimizations.
7812    The sincos pass is responsible for picking "optimal" implementations
7813    of math builtins, which may be more complicated and can sometimes go
7814    the other way, e.g. converting pow into a sequence of sqrts.
7815    We only want to do these canonicalizations before the pass has run.  */
7817 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7818  /* Simplify tan(x) * cos(x) -> sin(x). */
7819  (simplify
7820   (mult:c (TAN:s @0) (COS:s @0))
7821    (SIN @0))
7823  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7824  (simplify
7825   (mult:c @0 (POW:s @0 REAL_CST@1))
7826   (if (!TREE_OVERFLOW (@1))
7827    (POW @0 (plus @1 { build_one_cst (type); }))))
7829  /* Simplify sin(x) / cos(x) -> tan(x). */
7830  (simplify
7831   (rdiv (SIN:s @0) (COS:s @0))
7832    (TAN @0))
7834  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7835  (simplify
7836   (rdiv (SINH:s @0) (COSH:s @0))
7837    (TANH @0))
7839  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7840  (simplify
7841    (rdiv (TANH:s @0) (SINH:s @0))
7842    (rdiv {build_one_cst (type);} (COSH @0)))
7844  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7845  (simplify
7846   (rdiv (COS:s @0) (SIN:s @0))
7847    (rdiv { build_one_cst (type); } (TAN @0)))
7849  /* Simplify sin(x) / tan(x) -> cos(x). */
7850  (simplify
7851   (rdiv (SIN:s @0) (TAN:s @0))
7852   (if (! HONOR_NANS (@0)
7853        && ! HONOR_INFINITIES (@0))
7854    (COS @0)))
7856  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7857  (simplify
7858   (rdiv (TAN:s @0) (SIN:s @0))
7859   (if (! HONOR_NANS (@0)
7860        && ! HONOR_INFINITIES (@0))
7861    (rdiv { build_one_cst (type); } (COS @0))))
7863  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7864  (simplify
7865   (mult (POW:s @0 @1) (POW:s @0 @2))
7866    (POW @0 (plus @1 @2)))
7868  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7869  (simplify
7870   (mult (POW:s @0 @1) (POW:s @2 @1))
7871    (POW (mult @0 @2) @1))
7873  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7874  (simplify
7875   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7876    (POWI (mult @0 @2) @1))
7878  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7879  (simplify
7880   (rdiv (POW:s @0 REAL_CST@1) @0)
7881   (if (!TREE_OVERFLOW (@1))
7882    (POW @0 (minus @1 { build_one_cst (type); }))))
7884  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7885  (simplify
7886   (rdiv @0 (POW:s @1 @2))
7887    (mult @0 (POW @1 (negate @2))))
7889  (for sqrts (SQRT)
7890       cbrts (CBRT)
7891       pows (POW)
7892   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7893   (simplify
7894    (sqrts (sqrts @0))
7895    (pows @0 { build_real (type, dconst_quarter ()); }))
7896   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7897   (simplify
7898    (sqrts (cbrts @0))
7899    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7900   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7901   (simplify
7902    (cbrts (sqrts @0))
7903    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7904   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7905   (simplify
7906    (cbrts (cbrts tree_expr_nonnegative_p@0))
7907    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7908   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7909   (simplify
7910    (sqrts (pows @0 @1))
7911    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7912   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7913   (simplify
7914    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7915    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7916   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7917   (simplify
7918    (pows (sqrts @0) @1)
7919    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7920   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7921   (simplify
7922    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7923    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7924   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7925   (simplify
7926    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7927    (pows @0 (mult @1 @2))))
7929  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7930  (simplify
7931   (CABS (complex @0 @0))
7932   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7934  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7935  (simplify
7936   (HYPOT @0 @0)
7937   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7939  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7940  (for cexps (CEXP)
7941       exps (EXP)
7942       cexpis (CEXPI)
7943   (simplify
7944    (cexps compositional_complex@0)
7945    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7946     (complex
7947      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7948      (mult @1 (imagpart @2)))))))
7950 (if (canonicalize_math_p ())
7951  /* floor(x) -> trunc(x) if x is nonnegative.  */
7952  (for floors (FLOOR_ALL)
7953       truncs (TRUNC_ALL)
7954   (simplify
7955    (floors tree_expr_nonnegative_p@0)
7956    (truncs @0))))
7958 (match double_value_p
7959  @0
7960  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7961 (for froms (BUILT_IN_TRUNCL
7962             BUILT_IN_FLOORL
7963             BUILT_IN_CEILL
7964             BUILT_IN_ROUNDL
7965             BUILT_IN_NEARBYINTL
7966             BUILT_IN_RINTL)
7967      tos (BUILT_IN_TRUNC
7968           BUILT_IN_FLOOR
7969           BUILT_IN_CEIL
7970           BUILT_IN_ROUND
7971           BUILT_IN_NEARBYINT
7972           BUILT_IN_RINT)
7973  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7974  (if (optimize && canonicalize_math_p ())
7975   (simplify
7976    (froms (convert double_value_p@0))
7977    (convert (tos @0)))))
7979 (match float_value_p
7980  @0
7981  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7982 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7983             BUILT_IN_FLOORL BUILT_IN_FLOOR
7984             BUILT_IN_CEILL BUILT_IN_CEIL
7985             BUILT_IN_ROUNDL BUILT_IN_ROUND
7986             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7987             BUILT_IN_RINTL BUILT_IN_RINT)
7988      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7989           BUILT_IN_FLOORF BUILT_IN_FLOORF
7990           BUILT_IN_CEILF BUILT_IN_CEILF
7991           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7992           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7993           BUILT_IN_RINTF BUILT_IN_RINTF)
7994  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7995     if x is a float.  */
7996  (if (optimize && canonicalize_math_p ()
7997       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7998   (simplify
7999    (froms (convert float_value_p@0))
8000    (convert (tos @0)))))
8002 #if GIMPLE
8003 (match float16_value_p
8004  @0
8005  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
8006 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
8007             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
8008             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
8009             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
8010             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
8011             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
8012             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
8013             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
8014      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
8015           IFN_FLOOR IFN_FLOOR IFN_FLOOR
8016           IFN_CEIL IFN_CEIL IFN_CEIL
8017           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
8018           IFN_ROUND IFN_ROUND IFN_ROUND
8019           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
8020           IFN_RINT IFN_RINT IFN_RINT
8021           IFN_SQRT IFN_SQRT IFN_SQRT)
8022  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
8023     if x is a _Float16.  */
8024  (simplify
8025    (convert (froms (convert float16_value_p@0)))
8026      (if (optimize
8027           && types_match (type, TREE_TYPE (@0))
8028           && direct_internal_fn_supported_p (as_internal_fn (tos),
8029                                              type, OPTIMIZE_FOR_BOTH))
8030        (tos @0))))
8032 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8033    x,y is float value, similar for _Float16/double.  */
8034 (for copysigns (COPYSIGN_ALL)
8035  (simplify
8036   (convert (copysigns (convert@2 @0) (convert @1)))
8037    (if (optimize
8038        && !HONOR_SNANS (@2)
8039        && types_match (type, TREE_TYPE (@0))
8040        && types_match (type, TREE_TYPE (@1))
8041        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8042        && direct_internal_fn_supported_p (IFN_COPYSIGN,
8043                                           type, OPTIMIZE_FOR_BOTH))
8044     (IFN_COPYSIGN @0 @1))))
8046 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8047      tos (IFN_FMA IFN_FMA IFN_FMA)
8048  (simplify
8049   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8050    (if (flag_unsafe_math_optimizations
8051        && optimize
8052        && FLOAT_TYPE_P (type)
8053        && FLOAT_TYPE_P (TREE_TYPE (@3))
8054        && types_match (type, TREE_TYPE (@0))
8055        && types_match (type, TREE_TYPE (@1))
8056        && types_match (type, TREE_TYPE (@2))
8057        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8058        && direct_internal_fn_supported_p (as_internal_fn (tos),
8059                                           type, OPTIMIZE_FOR_BOTH))
8060     (tos @0 @1 @2))))
8062 (for maxmin (max min)
8063  (simplify
8064   (convert (maxmin (convert@2 @0) (convert @1)))
8065    (if (optimize
8066        && FLOAT_TYPE_P (type)
8067        && FLOAT_TYPE_P (TREE_TYPE (@2))
8068        && types_match (type, TREE_TYPE (@0))
8069        && types_match (type, TREE_TYPE (@1))
8070        && element_precision (type) < element_precision (TREE_TYPE (@2)))
8071     (maxmin @0 @1))))
8072 #endif
8074 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8075      tos (XFLOOR XCEIL XROUND XRINT)
8076  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
8077  (if (optimize && canonicalize_math_p ())
8078   (simplify
8079    (froms (convert double_value_p@0))
8080    (tos @0))))
8082 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8083             XFLOOR XCEIL XROUND XRINT)
8084      tos (XFLOORF XCEILF XROUNDF XRINTF)
8085  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8086     if x is a float.  */
8087  (if (optimize && canonicalize_math_p ())
8088   (simplify
8089    (froms (convert float_value_p@0))
8090    (tos @0))))
8092 (if (canonicalize_math_p ())
8093  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
8094  (for floors (IFLOOR LFLOOR LLFLOOR)
8095   (simplify
8096    (floors tree_expr_nonnegative_p@0)
8097    (fix_trunc @0))))
8099 (if (canonicalize_math_p ())
8100  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
8101  (for fns (IFLOOR LFLOOR LLFLOOR
8102            ICEIL LCEIL LLCEIL
8103            IROUND LROUND LLROUND)
8104   (simplify
8105    (fns integer_valued_real_p@0)
8106    (fix_trunc @0)))
8107  (if (!flag_errno_math)
8108   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
8109   (for rints (IRINT LRINT LLRINT)
8110    (simplify
8111     (rints integer_valued_real_p@0)
8112     (fix_trunc @0)))))
8114 (if (canonicalize_math_p ())
8115  (for ifn (IFLOOR ICEIL IROUND IRINT)
8116       lfn (LFLOOR LCEIL LROUND LRINT)
8117       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8118   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8119      sizeof (int) == sizeof (long).  */
8120   (if (TYPE_PRECISION (integer_type_node)
8121        == TYPE_PRECISION (long_integer_type_node))
8122    (simplify
8123     (ifn @0)
8124     (lfn:long_integer_type_node @0)))
8125   /* Canonicalize llround (x) to lround (x) on LP64 targets where
8126      sizeof (long long) == sizeof (long).  */
8127   (if (TYPE_PRECISION (long_long_integer_type_node)
8128        == TYPE_PRECISION (long_integer_type_node))
8129    (simplify
8130     (llfn @0)
8131     (lfn:long_integer_type_node @0)))))
8133 /* cproj(x) -> x if we're ignoring infinities.  */
8134 (simplify
8135  (CPROJ @0)
8136  (if (!HONOR_INFINITIES (type))
8137    @0))
8139 /* If the real part is inf and the imag part is known to be
8140    nonnegative, return (inf + 0i).  */
8141 (simplify
8142  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8143  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8144   { build_complex_inf (type, false); }))
8146 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8147 (simplify
8148  (CPROJ (complex @0 REAL_CST@1))
8149  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8150   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8152 (for pows (POW)
8153      sqrts (SQRT)
8154      cbrts (CBRT)
8155  (simplify
8156   (pows @0 REAL_CST@1)
8157   (with {
8158     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8159     REAL_VALUE_TYPE tmp;
8160    }
8161    (switch
8162     /* pow(x,0) -> 1.  */
8163     (if (real_equal (value, &dconst0))
8164      { build_real (type, dconst1); })
8165     /* pow(x,1) -> x.  */
8166     (if (real_equal (value, &dconst1))
8167      @0)
8168     /* pow(x,-1) -> 1/x.  */
8169     (if (real_equal (value, &dconstm1))
8170      (rdiv { build_real (type, dconst1); } @0))
8171     /* pow(x,0.5) -> sqrt(x).  */
8172     (if (flag_unsafe_math_optimizations
8173          && canonicalize_math_p ()
8174          && real_equal (value, &dconsthalf))
8175      (sqrts @0))
8176     /* pow(x,1/3) -> cbrt(x).  */
8177     (if (flag_unsafe_math_optimizations
8178          && canonicalize_math_p ()
8179          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8180              real_equal (value, &tmp)))
8181      (cbrts @0))))))
8183 /* powi(1,x) -> 1.  */
8184 (simplify
8185  (POWI real_onep@0 @1)
8186  @0)
8188 (simplify
8189  (POWI @0 INTEGER_CST@1)
8190  (switch
8191   /* powi(x,0) -> 1.  */
8192   (if (wi::to_wide (@1) == 0)
8193    { build_real (type, dconst1); })
8194   /* powi(x,1) -> x.  */
8195   (if (wi::to_wide (@1) == 1)
8196    @0)
8197   /* powi(x,-1) -> 1/x.  */
8198   (if (wi::to_wide (@1) == -1)
8199    (rdiv { build_real (type, dconst1); } @0))))
8201 /* Narrowing of arithmetic and logical operations.
8203    These are conceptually similar to the transformations performed for
8204    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8205    term we want to move all that code out of the front-ends into here.  */
8207 /* Convert (outertype)((innertype0)a+(innertype1)b)
8208    into ((newtype)a+(newtype)b) where newtype
8209    is the widest mode from all of these.  */
8210 (for op (plus minus mult rdiv)
8211  (simplify
8212    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8213    /* If we have a narrowing conversion of an arithmetic operation where
8214       both operands are widening conversions from the same type as the outer
8215       narrowing conversion.  Then convert the innermost operands to a
8216       suitable unsigned type (to avoid introducing undefined behavior),
8217       perform the operation and convert the result to the desired type.  */
8218    (if (INTEGRAL_TYPE_P (type)
8219         && op != MULT_EXPR
8220         && op != RDIV_EXPR
8221         /* We check for type compatibility between @0 and @1 below,
8222            so there's no need to check that @2/@4 are integral types.  */
8223         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8224         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8225         /* The precision of the type of each operand must match the
8226            precision of the mode of each operand, similarly for the
8227            result.  */
8228         && type_has_mode_precision_p (TREE_TYPE (@1))
8229         && type_has_mode_precision_p (TREE_TYPE (@2))
8230         && type_has_mode_precision_p (type)
8231         /* The inner conversion must be a widening conversion.  */
8232         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8233         && types_match (@1, type)
8234         && (types_match (@1, @2)
8235             /* Or the second operand is const integer or converted const
8236                integer from valueize.  */
8237             || poly_int_tree_p (@4)))
8238      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8239        (op @1 (convert @2))
8240        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8241         (convert (op (convert:utype @1)
8242                      (convert:utype @2)))))
8243      (if (FLOAT_TYPE_P (type)
8244           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8245                == DECIMAL_FLOAT_TYPE_P (type))
8246       (with { tree arg0 = strip_float_extensions (@1);
8247               tree arg1 = strip_float_extensions (@2);
8248               tree itype = TREE_TYPE (@0);
8249               tree ty1 = TREE_TYPE (arg0);
8250               tree ty2 = TREE_TYPE (arg1);
8251               enum tree_code code = TREE_CODE (itype); }
8252         (if (FLOAT_TYPE_P (ty1)
8253              && FLOAT_TYPE_P (ty2))
8254          (with { tree newtype = type;
8255                  if (TYPE_MODE (ty1) == SDmode
8256                      || TYPE_MODE (ty2) == SDmode
8257                      || TYPE_MODE (type) == SDmode)
8258                    newtype = dfloat32_type_node;
8259                  if (TYPE_MODE (ty1) == DDmode
8260                      || TYPE_MODE (ty2) == DDmode
8261                      || TYPE_MODE (type) == DDmode)
8262                    newtype = dfloat64_type_node;
8263                  if (TYPE_MODE (ty1) == TDmode
8264                      || TYPE_MODE (ty2) == TDmode
8265                      || TYPE_MODE (type) == TDmode)
8266                    newtype = dfloat128_type_node; }
8267           (if ((newtype == dfloat32_type_node
8268                 || newtype == dfloat64_type_node
8269                 || newtype == dfloat128_type_node)
8270               && newtype == type
8271               && types_match (newtype, type))
8272             (op (convert:newtype @1) (convert:newtype @2))
8273             (with { if (element_precision (ty1) > element_precision (newtype))
8274                       newtype = ty1;
8275                     if (element_precision (ty2) > element_precision (newtype))
8276                       newtype = ty2; }
8277                /* Sometimes this transformation is safe (cannot
8278                   change results through affecting double rounding
8279                   cases) and sometimes it is not.  If NEWTYPE is
8280                   wider than TYPE, e.g. (float)((long double)double
8281                   + (long double)double) converted to
8282                   (float)(double + double), the transformation is
8283                   unsafe regardless of the details of the types
8284                   involved; double rounding can arise if the result
8285                   of NEWTYPE arithmetic is a NEWTYPE value half way
8286                   between two representable TYPE values but the
8287                   exact value is sufficiently different (in the
8288                   right direction) for this difference to be
8289                   visible in ITYPE arithmetic.  If NEWTYPE is the
8290                   same as TYPE, however, the transformation may be
8291                   safe depending on the types involved: it is safe
8292                   if the ITYPE has strictly more than twice as many
8293                   mantissa bits as TYPE, can represent infinities
8294                   and NaNs if the TYPE can, and has sufficient
8295                   exponent range for the product or ratio of two
8296                   values representable in the TYPE to be within the
8297                   range of normal values of ITYPE.  */
8298               (if (element_precision (newtype) < element_precision (itype)
8299                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8300                        || target_supports_op_p (newtype, op, optab_default))
8301                    && (flag_unsafe_math_optimizations
8302                        || (element_precision (newtype) == element_precision (type)
8303                            && real_can_shorten_arithmetic (element_mode (itype),
8304                                                            element_mode (type))
8305                            && !excess_precision_type (newtype)))
8306                    && !types_match (itype, newtype))
8307                  (convert:type (op (convert:newtype @1)
8308                                    (convert:newtype @2)))
8309          )))) )
8310    ))
8313 /* This is another case of narrowing, specifically when there's an outer
8314    BIT_AND_EXPR which masks off bits outside the type of the innermost
8315    operands.   Like the previous case we have to convert the operands
8316    to unsigned types to avoid introducing undefined behavior for the
8317    arithmetic operation.  */
8318 (for op (minus plus)
8319  (simplify
8320   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8321   (if (INTEGRAL_TYPE_P (type)
8322        /* We check for type compatibility between @0 and @1 below,
8323           so there's no need to check that @1/@3 are integral types.  */
8324        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8325        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8326        /* The precision of the type of each operand must match the
8327           precision of the mode of each operand, similarly for the
8328           result.  */
8329        && type_has_mode_precision_p (TREE_TYPE (@0))
8330        && type_has_mode_precision_p (TREE_TYPE (@1))
8331        && type_has_mode_precision_p (type)
8332        /* The inner conversion must be a widening conversion.  */
8333        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8334        && types_match (@0, @1)
8335        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8336            <= TYPE_PRECISION (TREE_TYPE (@0)))
8337        && (wi::to_wide (@4)
8338            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8339                        true, TYPE_PRECISION (type))) == 0)
8340    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8341     (with { tree ntype = TREE_TYPE (@0); }
8342      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8343     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8344      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8345                (convert:utype @4))))))))
8347 /* Transform (@0 < @1 and @0 < @2) to use min,
8348    (@0 > @1 and @0 > @2) to use max */
8349 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8350      op    (lt      le      gt      ge      lt      le      gt      ge     )
8351      ext   (min     min     max     max     max     max     min     min    )
8352  (simplify
8353   (logic (op:cs @0 @1) (op:cs @0 @2))
8354   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8355        && TREE_CODE (@0) != INTEGER_CST)
8356    (op @0 (ext @1 @2)))))
8358 /* Max<bool0, bool1> -> bool0 | bool1
8359    Min<bool0, bool1> -> bool0 & bool1 */
8360 (for op    (max     min)
8361      logic (bit_ior bit_and)
8362  (simplify
8363   (op zero_one_valued_p@0 zero_one_valued_p@1)
8364   (logic @0 @1)))
8366 /* signbit(x) != 0 ? -x : x -> abs(x)
8367    signbit(x) == 0 ? -x : x -> -abs(x) */
8368 (for sign (SIGNBIT)
8369  (for neeq (ne eq)
8370   (simplify
8371    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8372     (if (neeq == NE_EXPR)
8373      (abs @0)
8374      (negate (abs @0))))))
8376 (simplify
8377  /* signbit(x) -> 0 if x is nonnegative.  */
8378  (SIGNBIT tree_expr_nonnegative_p@0)
8379  { integer_zero_node; })
8381 (simplify
8382  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8383  (SIGNBIT @0)
8384  (if (!HONOR_SIGNED_ZEROS (@0))
8385   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8387 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8388 (for cmp (eq ne)
8389  (for op (plus minus)
8390       rop (minus plus)
8391   (simplify
8392    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8393    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8394         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8395         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8396         && !TYPE_SATURATING (TREE_TYPE (@0)))
8397     (with { tree res = int_const_binop (rop, @2, @1); }
8398      (if (TREE_OVERFLOW (res)
8399           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8400       { constant_boolean_node (cmp == NE_EXPR, type); }
8401       (if (single_use (@3))
8402        (cmp @0 { TREE_OVERFLOW (res)
8403                  ? drop_tree_overflow (res) : res; }))))))))
8404 (for cmp (lt le gt ge)
8405  (for op (plus minus)
8406       rop (minus plus)
8407   (simplify
8408    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8409    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8410         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8411     (with { tree res = int_const_binop (rop, @2, @1); }
8412      (if (TREE_OVERFLOW (res))
8413       {
8414         fold_overflow_warning (("assuming signed overflow does not occur "
8415                                 "when simplifying conditional to constant"),
8416                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8417         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8418         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8419         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8420                                   TYPE_SIGN (TREE_TYPE (@1)))
8421                         != (op == MINUS_EXPR);
8422         constant_boolean_node (less == ovf_high, type);
8423       }
8424       (if (single_use (@3))
8425        (with
8426         {
8427           fold_overflow_warning (("assuming signed overflow does not occur "
8428                                   "when changing X +- C1 cmp C2 to "
8429                                   "X cmp C2 -+ C1"),
8430                                  WARN_STRICT_OVERFLOW_COMPARISON);
8431         }
8432         (cmp @0 { res; })))))))))
8434 /* Canonicalizations of BIT_FIELD_REFs.  */
8436 (simplify
8437  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8438  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8440 (simplify
8441  (BIT_FIELD_REF (view_convert @0) @1 @2)
8442  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8443       || type_has_mode_precision_p (TREE_TYPE (@0)))
8444   (BIT_FIELD_REF @0 @1 @2)))
8446 (simplify
8447  (BIT_FIELD_REF @0 @1 integer_zerop)
8448  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8449   (view_convert @0)))
8451 (simplify
8452  (BIT_FIELD_REF @0 @1 @2)
8453  (switch
8454   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8455        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8456    (switch
8457     (if (integer_zerop (@2))
8458      (view_convert (realpart @0)))
8459     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8460      (view_convert (imagpart @0)))))
8461   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8462        && INTEGRAL_TYPE_P (type)
8463        /* On GIMPLE this should only apply to register arguments.  */
8464        && (! GIMPLE || is_gimple_reg (@0))
8465        /* A bit-field-ref that referenced the full argument can be stripped.  */
8466        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8467             && integer_zerop (@2))
8468            /* Low-parts can be reduced to integral conversions.
8469               ???  The following doesn't work for PDP endian.  */
8470            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8471                /* But only do this after vectorization.  */
8472                && canonicalize_math_after_vectorization_p ()
8473                /* Don't even think about BITS_BIG_ENDIAN.  */
8474                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8475                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8476                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8477                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8478                                             - TYPE_PRECISION (type))
8479                                          : 0)) == 0)))
8480    (convert @0))))
8482 /* Simplify vector extracts.  */
8484 (simplify
8485  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8486  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8487       && tree_fits_uhwi_p (TYPE_SIZE (type))
8488       && ((tree_to_uhwi (TYPE_SIZE (type))
8489            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8490           || (VECTOR_TYPE_P (type)
8491               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8492                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8493   (with
8494    {
8495      tree ctor = (TREE_CODE (@0) == SSA_NAME
8496                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8497      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8498      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8499      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8500      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8501    }
8502    (if (n != 0
8503         && (idx % width) == 0
8504         && (n % width) == 0
8505         && known_le ((idx + n) / width,
8506                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8507     (with
8508      {
8509        idx = idx / width;
8510        n = n / width;
8511        /* Constructor elements can be subvectors.  */
8512        poly_uint64 k = 1;
8513        if (CONSTRUCTOR_NELTS (ctor) != 0)
8514          {
8515            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8516            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8517              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8518          }
8519        unsigned HOST_WIDE_INT elt, count, const_k;
8520      }
8521      (switch
8522       /* We keep an exact subset of the constructor elements.  */
8523       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8524        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8525         { build_zero_cst (type); }
8526         (if (count == 1)
8527          (if (elt < CONSTRUCTOR_NELTS (ctor))
8528           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8529           { build_zero_cst (type); })
8530          /* We don't want to emit new CTORs unless the old one goes away.
8531             ???  Eventually allow this if the CTOR ends up constant or
8532             uniform.  */
8533          (if (single_use (@0))
8534           (with
8535             {
8536               vec<constructor_elt, va_gc> *vals;
8537               vec_alloc (vals, count);
8538               bool constant_p = true;
8539               tree res;
8540               for (unsigned i = 0;
8541                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8542                 {
8543                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8544                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8545                   if (!CONSTANT_CLASS_P (e))
8546                     constant_p = false;
8547                 }
8548               tree evtype = (types_match (TREE_TYPE (type),
8549                                           TREE_TYPE (TREE_TYPE (ctor)))
8550                              ? type
8551                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8552                                                   count * k));
8553               /* We used to build a CTOR in the non-constant case here
8554                  but that's not a GIMPLE value.  We'd have to expose this
8555                  operation somehow so the code generation can properly
8556                  split it out to a separate stmt.  */
8557               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8558                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8559             }
8560             (if (res)
8561              (view_convert { res; })))))))
8562       /* The bitfield references a single constructor element.  */
8563       (if (k.is_constant (&const_k)
8564            && idx + n <= (idx / const_k + 1) * const_k)
8565        (switch
8566         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8567          { build_zero_cst (type); })
8568         (if (n == const_k)
8569          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8570         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8571                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8573 /* Simplify a bit extraction from a bit insertion for the cases with
8574    the inserted element fully covering the extraction or the insertion
8575    not touching the extraction.  */
8576 (simplify
8577  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8578  (with
8579   {
8580     unsigned HOST_WIDE_INT isize;
8581     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8582       isize = TYPE_PRECISION (TREE_TYPE (@1));
8583     else
8584       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8585   }
8586   (switch
8587    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8588          || type_has_mode_precision_p (TREE_TYPE (@1)))
8589         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8590         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8591                       wi::to_wide (@ipos) + isize))
8592     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8593                                                  wi::to_wide (@rpos)
8594                                                  - wi::to_wide (@ipos)); }))
8595    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8596         && compare_tree_int (@rsize, isize) == 0)
8597     (convert @1))
8598    (if (wi::geu_p (wi::to_wide (@ipos),
8599                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8600         || wi::geu_p (wi::to_wide (@rpos),
8601                       wi::to_wide (@ipos) + isize))
8602     (BIT_FIELD_REF @0 @rsize @rpos)))))
8604 /* Simplify vector inserts of other vector extracts to a permute.  */
8605 (simplify
8606  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8607  (if (VECTOR_TYPE_P (type)
8608       && (VECTOR_MODE_P (TYPE_MODE (type))
8609           || optimize_vectors_before_lowering_p ())
8610       && types_match (@0, @1)
8611       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8612       && TYPE_VECTOR_SUBPARTS (type).is_constant ()
8613       && multiple_p (wi::to_poly_offset (@rpos),
8614                      wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
8615   (with
8616    {
8617      unsigned HOST_WIDE_INT elsz
8618        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8619      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8620      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8621      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8622      vec_perm_builder builder;
8623      builder.new_vector (nunits, nunits, 1);
8624      for (unsigned i = 0; i < nunits; ++i)
8625        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8626      vec_perm_indices sel (builder, 2, nunits);
8627    }
8628    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8629         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8630     (vec_perm @0 @1 { vec_perm_indices_to_tree
8631                         (build_vector_type (ssizetype, nunits), sel); })))))
8633 (if (canonicalize_math_after_vectorization_p ())
8634  (for fmas (FMA)
8635   (simplify
8636    (fmas:c (negate @0) @1 @2)
8637    (IFN_FNMA @0 @1 @2))
8638   (simplify
8639    (fmas @0 @1 (negate @2))
8640    (IFN_FMS @0 @1 @2))
8641   (simplify
8642    (fmas:c (negate @0) @1 (negate @2))
8643    (IFN_FNMS @0 @1 @2))
8644   (simplify
8645    (negate (fmas@3 @0 @1 @2))
8646    (if (single_use (@3))
8647     (IFN_FNMS @0 @1 @2))))
8649  (simplify
8650   (IFN_FMS:c (negate @0) @1 @2)
8651   (IFN_FNMS @0 @1 @2))
8652  (simplify
8653   (IFN_FMS @0 @1 (negate @2))
8654   (IFN_FMA @0 @1 @2))
8655  (simplify
8656   (IFN_FMS:c (negate @0) @1 (negate @2))
8657   (IFN_FNMA @0 @1 @2))
8658  (simplify
8659   (negate (IFN_FMS@3 @0 @1 @2))
8660    (if (single_use (@3))
8661     (IFN_FNMA @0 @1 @2)))
8663  (simplify
8664   (IFN_FNMA:c (negate @0) @1 @2)
8665   (IFN_FMA @0 @1 @2))
8666  (simplify
8667   (IFN_FNMA @0 @1 (negate @2))
8668   (IFN_FNMS @0 @1 @2))
8669  (simplify
8670   (IFN_FNMA:c (negate @0) @1 (negate @2))
8671   (IFN_FMS @0 @1 @2))
8672  (simplify
8673   (negate (IFN_FNMA@3 @0 @1 @2))
8674   (if (single_use (@3))
8675    (IFN_FMS @0 @1 @2)))
8677  (simplify
8678   (IFN_FNMS:c (negate @0) @1 @2)
8679   (IFN_FMS @0 @1 @2))
8680  (simplify
8681   (IFN_FNMS @0 @1 (negate @2))
8682   (IFN_FNMA @0 @1 @2))
8683  (simplify
8684   (IFN_FNMS:c (negate @0) @1 (negate @2))
8685   (IFN_FMA @0 @1 @2))
8686  (simplify
8687   (negate (IFN_FNMS@3 @0 @1 @2))
8688   (if (single_use (@3))
8689    (IFN_FMA @0 @1 @2))))
8691 /* CLZ simplifications.  */
8692 (for clz (CLZ)
8693  (for op (eq ne)
8694       cmp (lt ge)
8695   (simplify
8696    (op (clz:s@2 @0) INTEGER_CST@1)
8697    (if (integer_zerop (@1) && single_use (@2))
8698     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8699     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8700      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8701     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8702     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8703      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8704 (for op (eq ne)
8705      cmp (lt ge)
8706  (simplify
8707   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8708   (if (integer_zerop (@1) && single_use (@2))
8709    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8710    (with { tree type0 = TREE_TYPE (@0);
8711            tree stype = signed_type_for (TREE_TYPE (@0));
8712            /* Punt if clz(0) == 0.  */
8713            if (integer_zerop (@3))
8714              stype = NULL_TREE;
8715          }
8716     (if (stype)
8717      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8718    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8719    (with { bool ok = true;
8720            tree type0 = TREE_TYPE (@0);
8721            /* Punt if clz(0) == prec - 1.  */
8722            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8723              ok = false;
8724          }
8725     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8726      (op @0 { build_one_cst (type0); }))))))
8728 /* CTZ simplifications.  */
8729 (for ctz (CTZ)
8730  (for op (ge gt le lt)
8731       cmp (eq eq ne ne)
8732   (simplify
8733    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8734    (op (ctz:s @0) INTEGER_CST@1)
8735     (with { bool ok = true;
8736             HOST_WIDE_INT val = 0;
8737             if (!tree_fits_shwi_p (@1))
8738               ok = false;
8739             else
8740               {
8741                 val = tree_to_shwi (@1);
8742                 /* Canonicalize to >= or <.  */
8743                 if (op == GT_EXPR || op == LE_EXPR)
8744                   {
8745                     if (val == HOST_WIDE_INT_MAX)
8746                       ok = false;
8747                     else
8748                       val++;
8749                   }
8750               }
8751             tree type0 = TREE_TYPE (@0);
8752             int prec = TYPE_PRECISION (type0);
8753           }
8754      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8755       (if (val <= 0)
8756        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8757        (if (val >= prec)
8758         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8759         (cmp (bit_and @0 { wide_int_to_tree (type0,
8760                                              wi::mask (val, false, prec)); })
8761              { build_zero_cst (type0); })))))))
8762  (for op (eq ne)
8763   (simplify
8764    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8765    (op (ctz:s @0) INTEGER_CST@1)
8766     (with { tree type0 = TREE_TYPE (@0);
8767             int prec = TYPE_PRECISION (type0);
8768           }
8769      (if (prec <= MAX_FIXED_MODE_SIZE)
8770       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8771        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8772        (op (bit_and @0 { wide_int_to_tree (type0,
8773                                            wi::mask (tree_to_uhwi (@1) + 1,
8774                                                      false, prec)); })
8775            { wide_int_to_tree (type0,
8776                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8777                                                  false, prec)); })))))))
8778 (for op (ge gt le lt)
8779      cmp (eq eq ne ne)
8780  (simplify
8781   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8782   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8783    (with { bool ok = true;
8784            HOST_WIDE_INT val = 0;
8785            if (!tree_fits_shwi_p (@1))
8786              ok = false;
8787            else
8788              {
8789                val = tree_to_shwi (@1);
8790                /* Canonicalize to >= or <.  */
8791                if (op == GT_EXPR || op == LE_EXPR)
8792                  {
8793                    if (val == HOST_WIDE_INT_MAX)
8794                      ok = false;
8795                    else
8796                      val++;
8797                  }
8798              }
8799            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8800            tree type0 = TREE_TYPE (@0);
8801            int prec = TYPE_PRECISION (type0);
8802            if (prec > MAX_FIXED_MODE_SIZE)
8803              ok = false;
8804           }
8805      (if (val <= 0)
8806       (if (ok && zero_val >= val)
8807        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8808       (if (val >= prec)
8809        (if (ok && zero_val < val)
8810         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8811        (if (ok && (zero_val < 0 || zero_val >= prec))
8812         (cmp (bit_and @0 { wide_int_to_tree (type0,
8813                                              wi::mask (val, false, prec)); })
8814              { build_zero_cst (type0); })))))))
8815 (for op (eq ne)
8816  (simplify
8817   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8818   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8819    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8820            tree type0 = TREE_TYPE (@0);
8821            int prec = TYPE_PRECISION (type0);
8822          }
8823     (if (prec <= MAX_FIXED_MODE_SIZE)
8824      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8825       (if (zero_val != wi::to_widest (@1))
8826        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8827       (if (zero_val < 0 || zero_val >= prec)
8828        (op (bit_and @0 { wide_int_to_tree (type0,
8829                                            wi::mask (tree_to_uhwi (@1) + 1,
8830                                                      false, prec)); })
8831            { wide_int_to_tree (type0,
8832                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8833                                                  false, prec)); })))))))
8835 #if GIMPLE
8836 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
8837 (simplify
8838   (CTZ (convert@1 @0))
8839   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8840        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8841        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8842    (with { combined_fn cfn = CFN_LAST;
8843            tree type0 = TREE_TYPE (@0);
8844            if (TREE_CODE (type0) == BITINT_TYPE)
8845              {
8846                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8847                  cfn = CFN_CTZ;
8848                else
8849                  type0
8850                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8851                                                      1);
8852              }
8853            type0 = unsigned_type_for (type0);
8854            if (cfn == CFN_LAST
8855                && direct_internal_fn_supported_p (IFN_CTZ, type0,
8856                                                   OPTIMIZE_FOR_BOTH))
8857              cfn = CFN_CTZ;
8858            if (cfn == CFN_LAST
8859                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8860                && !direct_internal_fn_supported_p (IFN_CTZ,
8861                                                    TREE_TYPE (@1),
8862                                                    OPTIMIZE_FOR_BOTH))
8863              {
8864                if (TYPE_PRECISION (type0)
8865                    == TYPE_PRECISION (unsigned_type_node))
8866                  cfn = CFN_BUILT_IN_CTZ;
8867                else if (TYPE_PRECISION (type0)
8868                         == TYPE_PRECISION (long_long_unsigned_type_node))
8869                  cfn = CFN_BUILT_IN_CTZLL;
8870              } }
8871     (if (cfn == CFN_CTZ)
8872      (IFN_CTZ (convert:type0 @0))
8873      (if (cfn == CFN_BUILT_IN_CTZ)
8874       (BUILT_IN_CTZ (convert:type0 @0))
8875       (if (cfn == CFN_BUILT_IN_CTZLL)
8876        (BUILT_IN_CTZLL (convert:type0 @0))))))))
8877 #endif
8879 /* POPCOUNT simplifications.  */
8880 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8881 (simplify
8882   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8883   (if (INTEGRAL_TYPE_P (type)
8884        && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8885                         widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8886            == 0))
8887    (with { tree utype = TREE_TYPE (@0);
8888            if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8889              utype = TREE_TYPE (@1); }
8890     (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8892 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8893 (for popcount (POPCOUNT)
8894   (for cmp (le eq ne gt)
8895        rep (eq eq ne ne)
8896     (simplify
8897       (cmp (popcount @0) integer_zerop)
8898       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8900 /* popcount(bswap(x)) is popcount(x).  */
8901 (for popcount (POPCOUNT)
8902   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8903               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8904     (simplify
8905       (popcount (convert?@0 (bswap:s@1 @2)))
8906       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8907            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8908         (with { tree type0 = TREE_TYPE (@0);
8909                 tree type1 = TREE_TYPE (@1);
8910                 unsigned int prec0 = TYPE_PRECISION (type0);
8911                 unsigned int prec1 = TYPE_PRECISION (type1); }
8912           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8913             (popcount (convert:type0 (convert:type1 @2)))))))))
8915 /* popcount(rotate(X Y)) is popcount(X).  */
8916 (for popcount (POPCOUNT)
8917   (for rot (lrotate rrotate)
8918     (simplify
8919       (popcount (convert?@0 (rot:s@1 @2 @3)))
8920       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8921            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8922            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8923         (with { tree type0 = TREE_TYPE (@0);
8924                 tree type1 = TREE_TYPE (@1);
8925                 unsigned int prec0 = TYPE_PRECISION (type0);
8926                 unsigned int prec1 = TYPE_PRECISION (type1); }
8927           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8928             (popcount (convert:type0 @2))))))))
8930 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8931 (simplify
8932   (bit_and (POPCOUNT @0) integer_onep)
8933   (PARITY @0))
8935 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8936 (simplify
8937   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8938   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8940 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8941 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8942 (for popcount (POPCOUNT)
8943   (for log1 (bit_and bit_ior)
8944        log2 (bit_ior bit_and)
8945     (simplify
8946       (minus (plus:s (popcount:s @0) (popcount:s @1))
8947              (popcount:s (log1:cs @0 @1)))
8948       (popcount (log2 @0 @1)))
8949     (simplify
8950       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8951               (popcount:s @1))
8952       (popcount (log2 @0 @1)))))
8954 #if GIMPLE
8955 /* popcount(zext(X)) == popcount(X).  */
8956 (simplify
8957   (POPCOUNT (convert@1 @0))
8958   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8959        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8960        && TYPE_UNSIGNED (TREE_TYPE (@0))
8961        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8962    (with { combined_fn cfn = CFN_LAST;
8963            tree type0 = TREE_TYPE (@0);
8964            if (TREE_CODE (type0) == BITINT_TYPE)
8965              {
8966                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8967                  cfn = CFN_POPCOUNT;
8968                else
8969                  type0
8970                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8971                                                      1);
8972              }
8973            if (cfn == CFN_LAST
8974                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8975                                                   OPTIMIZE_FOR_BOTH))
8976              cfn = CFN_POPCOUNT;
8977            if (cfn == CFN_LAST
8978                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8979                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8980                                                    TREE_TYPE (@1),
8981                                                    OPTIMIZE_FOR_BOTH))
8982              {
8983                if (TYPE_PRECISION (type0)
8984                    == TYPE_PRECISION (unsigned_type_node))
8985                  cfn = CFN_BUILT_IN_POPCOUNT;
8986                else if (TYPE_PRECISION (type0)
8987                         == TYPE_PRECISION (long_long_unsigned_type_node))
8988                  cfn = CFN_BUILT_IN_POPCOUNTLL;
8989              } }
8990     (if (cfn == CFN_POPCOUNT)
8991      (IFN_POPCOUNT (convert:type0 @0))
8992      (if (cfn == CFN_BUILT_IN_POPCOUNT)
8993       (BUILT_IN_POPCOUNT (convert:type0 @0))
8994       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8995        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8996 #endif
8998 /* PARITY simplifications.  */
8999 /* parity(~X) is parity(X).  */
9000 (simplify
9001   (PARITY (bit_not @0))
9002   (PARITY @0))
9004 /* parity(bswap(x)) is parity(x).  */
9005 (for parity (PARITY)
9006   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
9007               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
9008     (simplify
9009       (parity (convert?@0 (bswap:s@1 @2)))
9010       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9011            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9012            && TYPE_PRECISION (TREE_TYPE (@0))
9013               >= TYPE_PRECISION (TREE_TYPE (@1)))
9014         (with { tree type0 = TREE_TYPE (@0);
9015                 tree type1 = TREE_TYPE (@1); }
9016           (parity (convert:type0 (convert:type1 @2))))))))
9018 /* parity(rotate(X Y)) is parity(X).  */
9019 (for parity (PARITY)
9020   (for rot (lrotate rrotate)
9021     (simplify
9022       (parity (convert?@0 (rot:s@1 @2 @3)))
9023       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9024            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9025            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
9026            && TYPE_PRECISION (TREE_TYPE (@0))
9027               >= TYPE_PRECISION (TREE_TYPE (@1)))
9028         (with { tree type0 = TREE_TYPE (@0); }
9029           (parity (convert:type0 @2)))))))
9031 /* parity(X)^parity(Y) is parity(X^Y).  */
9032 (simplify
9033   (bit_xor (PARITY:s @0) (PARITY:s @1))
9034   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
9035    (PARITY (bit_xor @0 @1))
9036    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9037         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9038     (with { tree utype = TREE_TYPE (@0);
9039             if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9040               utype = TREE_TYPE (@1); }
9041      (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
9043 #if GIMPLE
9044 /* parity(zext(X)) == parity(X).  */
9045 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
9046 (simplify
9047   (PARITY (convert@1 @0))
9048   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9049        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9050        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
9051        && (TYPE_UNSIGNED (TREE_TYPE (@0))
9052            || ((TYPE_PRECISION (TREE_TYPE (@1))
9053                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
9054    (with { combined_fn cfn = CFN_LAST;
9055            tree type0 = TREE_TYPE (@0);
9056            if (TREE_CODE (type0) == BITINT_TYPE)
9057              {
9058                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9059                  cfn = CFN_PARITY;
9060                else
9061                  type0
9062                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9063                                                      1);
9064              }
9065            type0 = unsigned_type_for (type0);
9066            if (cfn == CFN_LAST
9067                && direct_internal_fn_supported_p (IFN_PARITY, type0,
9068                                                   OPTIMIZE_FOR_BOTH))
9069              cfn = CFN_PARITY;
9070            if (cfn == CFN_LAST
9071                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9072                && !direct_internal_fn_supported_p (IFN_PARITY,
9073                                                    TREE_TYPE (@1),
9074                                                    OPTIMIZE_FOR_BOTH))
9075              {
9076                if (TYPE_PRECISION (type0)
9077                    == TYPE_PRECISION (unsigned_type_node))
9078                  cfn = CFN_BUILT_IN_PARITY;
9079                else if (TYPE_PRECISION (type0)
9080                         == TYPE_PRECISION (long_long_unsigned_type_node))
9081                  cfn = CFN_BUILT_IN_PARITYLL;
9082              } }
9083     (if (cfn == CFN_PARITY)
9084      (IFN_PARITY (convert:type0 @0))
9085      (if (cfn == CFN_BUILT_IN_PARITY)
9086       (BUILT_IN_PARITY (convert:type0 @0))
9087       (if (cfn == CFN_BUILT_IN_PARITYLL)
9088        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9089 #endif
9091 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9092 (for func (POPCOUNT BSWAP FFS PARITY)
9093  (simplify
9094   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9095   @3))
9097 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9098    where CST is precision-1. */
9099 (for func (CLRSB)
9100  (simplify
9101   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9102   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9103    @4)))
9105 #if GIMPLE
9106 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9107 (for func (CLZ)
9108  (simplify
9109   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9110   (with { int val;
9111           internal_fn ifn = IFN_LAST;
9112           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9113             {
9114               if (tree_fits_shwi_p (@2))
9115                 {
9116                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9117                   if ((int) valw == valw)
9118                     {
9119                       val = valw;
9120                       ifn = IFN_CLZ;
9121                     }
9122                 }
9123             }
9124           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9125                                                    OPTIMIZE_FOR_BOTH)
9126                    && CLZ_DEFINED_VALUE_AT_ZERO
9127                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9128             ifn = IFN_CLZ;
9129         }
9130    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9131     (IFN_CLZ @3 @2)))))
9132 (simplify
9133  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9134   (with { int val;
9135           internal_fn ifn = IFN_LAST;
9136           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9137             ifn = IFN_CLZ;
9138           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9139                                                    OPTIMIZE_FOR_BOTH))
9140             ifn = IFN_CLZ;
9141         }
9142    (if (ifn == IFN_CLZ)
9143     (IFN_CLZ @3 @2))))
9145 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9146 (for func (CTZ)
9147  (simplify
9148   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9149   (with { int val;
9150           internal_fn ifn = IFN_LAST;
9151           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9152             {
9153               if (tree_fits_shwi_p (@2))
9154                 {
9155                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9156                   if ((int) valw == valw)
9157                     {
9158                       val = valw;
9159                       ifn = IFN_CTZ;
9160                     }
9161                 }
9162             }
9163           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9164                                                    OPTIMIZE_FOR_BOTH)
9165                    && CTZ_DEFINED_VALUE_AT_ZERO
9166                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9167             ifn = IFN_CTZ;
9168         }
9169    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9170     (IFN_CTZ @3 @2)))))
9171 (simplify
9172  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9173   (with { int val;
9174           internal_fn ifn = IFN_LAST;
9175           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9176             ifn = IFN_CTZ;
9177           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9178                                                    OPTIMIZE_FOR_BOTH))
9179             ifn = IFN_CTZ;
9180         }
9181    (if (ifn == IFN_CTZ)
9182     (IFN_CTZ @3 @2))))
9183 #endif
9185 /* Common POPCOUNT/PARITY simplifications.  */
9186 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9187 (for pfun (POPCOUNT PARITY)
9188   (simplify
9189     (pfun @0)
9190     (if (INTEGRAL_TYPE_P (type))
9191      (with { wide_int nz = tree_nonzero_bits (@0); }
9192        (switch
9193          (if (nz == 1)
9194            (convert @0))
9195          (if (wi::popcount (nz) == 1)
9196            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9197              (convert (rshift:utype (convert:utype @0)
9198                                     { build_int_cst (integer_type_node,
9199                                                      wi::ctz (nz)); })))))))))
9201 #if GIMPLE
9202 /* 64- and 32-bits branchless implementations of popcount are detected:
9204    int popcount64c (uint64_t x)
9205    {
9206      x -= (x >> 1) & 0x5555555555555555ULL;
9207      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9208      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9209      return (x * 0x0101010101010101ULL) >> 56;
9210    }
9212    int popcount32c (uint32_t x)
9213    {
9214      x -= (x >> 1) & 0x55555555;
9215      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9216      x = (x + (x >> 4)) & 0x0f0f0f0f;
9217      return (x * 0x01010101) >> 24;
9218    }  */
9219 (simplify
9220  (rshift
9221   (mult
9222    (bit_and
9223     (plus:c
9224      (rshift @8 INTEGER_CST@5)
9225       (plus:c@8
9226        (bit_and @6 INTEGER_CST@7)
9227         (bit_and
9228          (rshift
9229           (minus@6 @0
9230            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9231           INTEGER_CST@10)
9232          INTEGER_CST@9)))
9233     INTEGER_CST@3)
9234    INTEGER_CST@2)
9235   INTEGER_CST@1)
9236   /* Check constants and optab.  */
9237   (with { unsigned prec = TYPE_PRECISION (type);
9238           int shift = (64 - prec) & 63;
9239           unsigned HOST_WIDE_INT c1
9240             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9241           unsigned HOST_WIDE_INT c2
9242             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9243           unsigned HOST_WIDE_INT c3
9244             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9245           unsigned HOST_WIDE_INT c4
9246             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9247    }
9248    (if (prec >= 16
9249         && prec <= 64
9250         && pow2p_hwi (prec)
9251         && TYPE_UNSIGNED (type)
9252         && integer_onep (@4)
9253         && wi::to_widest (@10) == 2
9254         && wi::to_widest (@5) == 4
9255         && wi::to_widest (@1) == prec - 8
9256         && tree_to_uhwi (@2) == c1
9257         && tree_to_uhwi (@3) == c2
9258         && tree_to_uhwi (@9) == c3
9259         && tree_to_uhwi (@7) == c3
9260         && tree_to_uhwi (@11) == c4)
9261     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9262                                          OPTIMIZE_FOR_BOTH))
9263      (convert (IFN_POPCOUNT:type @0))
9264      /* Try to do popcount in two halves.  PREC must be at least
9265         five bits for this to work without extension before adding.  */
9266      (with {
9267        tree half_type = NULL_TREE;
9268        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9269        int half_prec = 8;
9270        if (m.exists ()
9271            && m.require () != TYPE_MODE (type))
9272          {
9273            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9274            half_type = build_nonstandard_integer_type (half_prec, 1);
9275          }
9276        gcc_assert (half_prec > 2);
9277       }
9278       (if (half_type != NULL_TREE
9279            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9280                                               OPTIMIZE_FOR_BOTH))
9281        (convert (plus
9282          (IFN_POPCOUNT:half_type (convert @0))
9283          (IFN_POPCOUNT:half_type (convert (rshift @0
9284             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9286 /* __builtin_ffs needs to deal on many targets with the possible zero
9287    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9288    should lead to better code.  */
9289 (simplify
9290  (FFS tree_expr_nonzero_p@0)
9291  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9292       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9293                                          OPTIMIZE_FOR_SPEED))
9294   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9295    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9296 #endif
9298 (for ffs (FFS)
9299  /* __builtin_ffs (X) == 0 -> X == 0.
9300     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9301  (for cmp (eq ne)
9302   (simplify
9303    (cmp (ffs@2 @0) INTEGER_CST@1)
9304     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9305      (switch
9306       (if (integer_zerop (@1))
9307        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9308       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9309        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9310       (if (single_use (@2))
9311        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9312                                             wi::mask (tree_to_uhwi (@1),
9313                                                       false, prec)); })
9314             { wide_int_to_tree (TREE_TYPE (@0),
9315                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9316                                                   false, prec)); }))))))
9318  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9319  (for cmp (gt le)
9320       cmp2 (ne eq)
9321       cmp3 (eq ne)
9322       bit_op (bit_and bit_ior)
9323   (simplify
9324    (cmp (ffs@2 @0) INTEGER_CST@1)
9325     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9326      (switch
9327       (if (integer_zerop (@1))
9328        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9329       (if (tree_int_cst_sgn (@1) < 0)
9330        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9331       (if (wi::to_widest (@1) >= prec)
9332        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9333       (if (wi::to_widest (@1) == prec - 1)
9334        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9335                                     wi::shifted_mask (prec - 1, 1,
9336                                                       false, prec)); }))
9337       (if (single_use (@2))
9338        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9339                (cmp3 (bit_and @0
9340                               { wide_int_to_tree (TREE_TYPE (@0),
9341                                                   wi::mask (tree_to_uhwi (@1),
9342                                                   false, prec)); })
9343                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9345 #if GIMPLE
9346 /* ffs(ext(X)) == ffs(X).  */
9347 (simplify
9348   (FFS (convert@1 @0))
9349   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9350        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9351        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9352    (with { combined_fn cfn = CFN_LAST;
9353            tree type0 = TREE_TYPE (@0);
9354            if (TREE_CODE (type0) == BITINT_TYPE)
9355              {
9356                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9357                  cfn = CFN_FFS;
9358                else
9359                  type0
9360                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9361                                                      0);
9362              }
9363            type0 = signed_type_for (type0);
9364            if (cfn == CFN_LAST
9365                && direct_internal_fn_supported_p (IFN_FFS, type0,
9366                                                   OPTIMIZE_FOR_BOTH))
9367              cfn = CFN_FFS;
9368            if (cfn == CFN_LAST
9369                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9370                && !direct_internal_fn_supported_p (IFN_FFS,
9371                                                    TREE_TYPE (@1),
9372                                                    OPTIMIZE_FOR_BOTH))
9373              {
9374                if (TYPE_PRECISION (type0)
9375                    == TYPE_PRECISION (integer_type_node))
9376                  cfn = CFN_BUILT_IN_FFS;
9377                else if (TYPE_PRECISION (type0)
9378                         == TYPE_PRECISION (long_long_integer_type_node))
9379                  cfn = CFN_BUILT_IN_FFSLL;
9380              } }
9381     (if (cfn == CFN_FFS)
9382      (IFN_FFS (convert:type0 @0))
9383      (if (cfn == CFN_BUILT_IN_FFS)
9384       (BUILT_IN_FFS (convert:type0 @0))
9385       (if (cfn == CFN_BUILT_IN_FFSLL)
9386        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9387 #endif
9389 #if GIMPLE
9391 /* Simplify:
9392      a = op a1
9393      r = cond ? a : b
9394      --> r = .COND_FN (cond, a, b)
9395 and,
9396     a = op a1
9397     r = cond ? b : a
9398     --> r = .COND_FN (~cond, b, a).  */
9400 (for uncond_op (UNCOND_UNARY)
9401      cond_op (COND_UNARY)
9402  (simplify
9403   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9404    (with { tree op_type = TREE_TYPE (@3); }
9405     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9406         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9407      (cond_op @0 (view_convert @1) @2))))
9408  (simplify
9409   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9410    (with { tree op_type = TREE_TYPE (@3); }
9411     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9412         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9413      (cond_op (bit_not @0) (view_convert @2) @1)))))
9415 (for uncond_op (UNCOND_UNARY)
9416      cond_op (COND_LEN_UNARY)
9417  (simplify
9418   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9419    (with { tree op_type = TREE_TYPE (@3); }
9420     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9421         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9422      (cond_op @0 (view_convert @1) @2 @4 @5))))
9423  (simplify
9424   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9425    (with { tree op_type = TREE_TYPE (@3); }
9426     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9427         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9428      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9430 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9431 (simplify
9432  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9433  (if (canonicalize_math_after_vectorization_p ()
9434       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9435       && is_truth_type_for (type, TREE_TYPE (@0)))
9436  (if (integer_all_onesp (@1) && integer_zerop (@2))
9437   (IFN_COND_NOT @0 @3 @3))
9438   (if (integer_all_onesp (@2) && integer_zerop (@1))
9439    (IFN_COND_NOT (bit_not @0) @3 @3))))
9441 /* Simplify:
9443      a = a1 op a2
9444      r = c ? a : b;
9446    to:
9448      r = c ? a1 op a2 : b;
9450    if the target can do it in one go.  This makes the operation conditional
9451    on c, so could drop potentially-trapping arithmetic, but that's a valid
9452    simplification if the result of the operation isn't needed.
9454    Avoid speculatively generating a stand-alone vector comparison
9455    on targets that might not support them.  Any target implementing
9456    conditional internal functions must support the same comparisons
9457    inside and outside a VEC_COND_EXPR.  */
9459 (for uncond_op (UNCOND_BINARY)
9460      cond_op (COND_BINARY)
9461  (simplify
9462   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9463   (with { tree op_type = TREE_TYPE (@4); }
9464    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9465         && is_truth_type_for (op_type, TREE_TYPE (@0))
9466         && single_use (@4))
9467     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9468  (simplify
9469   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9470   (with { tree op_type = TREE_TYPE (@4); }
9471    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9472         && is_truth_type_for (op_type, TREE_TYPE (@0))
9473         && single_use (@4))
9474     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9476 (for uncond_op (UNCOND_BINARY)
9477      cond_op (COND_LEN_BINARY)
9478  (simplify
9479   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9480   (with { tree op_type = TREE_TYPE (@4); }
9481    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9482         && is_truth_type_for (op_type, TREE_TYPE (@0))
9483         && single_use (@4))
9484     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9485  (simplify
9486   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9487   (with { tree op_type = TREE_TYPE (@4); }
9488    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9489         && is_truth_type_for (op_type, TREE_TYPE (@0))
9490         && single_use (@4))
9491     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9493 /* Same for ternary operations.  */
9494 (for uncond_op (UNCOND_TERNARY)
9495      cond_op (COND_TERNARY)
9496  (simplify
9497   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9498   (with { tree op_type = TREE_TYPE (@5); }
9499    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9500         && is_truth_type_for (op_type, TREE_TYPE (@0))
9501         && single_use (@5))
9502     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9503  (simplify
9504   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9505   (with { tree op_type = TREE_TYPE (@5); }
9506    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9507         && is_truth_type_for (op_type, TREE_TYPE (@0))
9508         && single_use (@5))
9509     (view_convert (cond_op (bit_not @0) @2 @3 @4
9510                   (view_convert:op_type @1)))))))
9512 (for uncond_op (UNCOND_TERNARY)
9513      cond_op (COND_LEN_TERNARY)
9514  (simplify
9515   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9516   (with { tree op_type = TREE_TYPE (@5); }
9517    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9518         && is_truth_type_for (op_type, TREE_TYPE (@0))
9519         && single_use (@5))
9520     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9521  (simplify
9522   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9523   (with { tree op_type = TREE_TYPE (@5); }
9524    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9525         && is_truth_type_for (op_type, TREE_TYPE (@0))
9526         && single_use (@5))
9527     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9528 #endif
9530 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9531    "else" value of an IFN_COND_*.  */
9532 (for cond_op (COND_BINARY)
9533  (simplify
9534   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9535   (with { tree op_type = TREE_TYPE (@3); }
9536    (if (element_precision (type) == element_precision (op_type))
9537     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9538  (simplify
9539   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9540   (with { tree op_type = TREE_TYPE (@5); }
9541    (if (inverse_conditions_p (@0, @2)
9542         && element_precision (type) == element_precision (op_type))
9543     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9545 /* Same for ternary operations.  */
9546 (for cond_op (COND_TERNARY)
9547  (simplify
9548   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9549   (with { tree op_type = TREE_TYPE (@4); }
9550    (if (element_precision (type) == element_precision (op_type))
9551     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9552  (simplify
9553   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9554   (with { tree op_type = TREE_TYPE (@6); }
9555    (if (inverse_conditions_p (@0, @2)
9556         && element_precision (type) == element_precision (op_type))
9557     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9559 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9560    "else" value of an IFN_COND_LEN_*.  */
9561 (for cond_len_op (COND_LEN_BINARY)
9562  (simplify
9563   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9564   (with { tree op_type = TREE_TYPE (@3); }
9565    (if (element_precision (type) == element_precision (op_type))
9566     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9567  (simplify
9568   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9569   (with { tree op_type = TREE_TYPE (@5); }
9570    (if (inverse_conditions_p (@0, @2)
9571         && element_precision (type) == element_precision (op_type))
9572     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9574 /* Same for ternary operations.  */
9575 (for cond_len_op (COND_LEN_TERNARY)
9576  (simplify
9577   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9578   (with { tree op_type = TREE_TYPE (@4); }
9579    (if (element_precision (type) == element_precision (op_type))
9580     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9581  (simplify
9582   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9583   (with { tree op_type = TREE_TYPE (@6); }
9584    (if (inverse_conditions_p (@0, @2)
9585         && element_precision (type) == element_precision (op_type))
9586     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9588 /* Detect simplication for a conditional reduction where
9590    a = mask1 ? b : 0
9591    c = mask2 ? d + a : d
9593    is turned into
9595    c = mask1 && mask2 ? d + b : d.  */
9596 (simplify
9597   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9598    (if (ANY_INTEGRAL_TYPE_P (type)
9599         || (FLOAT_TYPE_P (type)
9600             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9601    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9603 /* Detect simplication for a conditional length reduction where
9605    a = mask ? b : 0
9606    c = i < len + bias ? d + a : d
9608    is turned into
9610    c = mask && i < len + bias ? d + b : d.  */
9611 (simplify
9612   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9613    (if (ANY_INTEGRAL_TYPE_P (type)
9614         || (FLOAT_TYPE_P (type)
9615             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9616     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9618 /* Detect simplification for vector condition folding where
9620   c = mask1 ? (masked_op mask2 a b els) : els
9622   into
9624   c = masked_op (mask1 & mask2) a b els
9626   where the operation can be partially applied to one operand. */
9628 (for cond_op (COND_BINARY)
9629  (simplify
9630   (vec_cond @0
9631    (cond_op:s @1 @2 @3 @4) @4)
9632   (cond_op (bit_and @1 @0) @2 @3 @4)))
9634 /* And same for ternary expressions.  */
9636 (for cond_op (COND_TERNARY)
9637  (simplify
9638   (vec_cond @0
9639    (cond_op:s @1 @2 @3 @4 @5) @5)
9640   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9642 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9643    expressions like:
9645    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9646    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9648    If pointers are known not to wrap, B checks whether @1 bytes starting
9649    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9650    bytes.  A is more efficiently tested as:
9652    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9654    The equivalent expression for B is given by replacing @1 with @1 - 1:
9656    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9658    @0 and @2 can be swapped in both expressions without changing the result.
9660    The folds rely on sizetype's being unsigned (which is always true)
9661    and on its being the same width as the pointer (which we have to check).
9663    The fold replaces two pointer_plus expressions, two comparisons and
9664    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9665    the best case it's a saving of two operations.  The A fold retains one
9666    of the original pointer_pluses, so is a win even if both pointer_pluses
9667    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9668    used elsewhere, since all we end up doing is replacing a comparison with
9669    a pointer_plus.  We do still apply the fold under those circumstances
9670    though, in case applying it to other conditions eventually makes one of the
9671    pointer_pluses dead.  */
9672 (for ior (truth_orif truth_or bit_ior)
9673  (for cmp (le lt)
9674   (simplify
9675    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9676         (cmp:cs (pointer_plus@4 @2 @1) @0))
9677    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9678         && TYPE_OVERFLOW_WRAPS (sizetype)
9679         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9680     /* Calculate the rhs constant.  */
9681     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9682             offset_int rhs = off * 2; }
9683      /* Always fails for negative values.  */
9684      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9685       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9686          pick a canonical order.  This increases the chances of using the
9687          same pointer_plus in multiple checks.  */
9688       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9689               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9690        (if (cmp == LT_EXPR)
9691         (gt (convert:sizetype
9692              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9693                                      { swap_p ? @0 : @2; }))
9694             { rhs_tree; })
9695         (gt (convert:sizetype
9696              (pointer_diff:ssizetype
9697               (pointer_plus { swap_p ? @2 : @0; }
9698                             { wide_int_to_tree (sizetype, off); })
9699               { swap_p ? @0 : @2; }))
9700             { rhs_tree; })))))))))
9702 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9703    element of @1.  */
9704 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9705  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9706   (with { int i = single_nonzero_element (@1); }
9707    (if (i >= 0)
9708     (with { tree elt = vector_cst_elt (@1, i);
9709             tree elt_type = TREE_TYPE (elt);
9710             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9711             tree size = bitsize_int (elt_bits);
9712             tree pos = bitsize_int (elt_bits * i); }
9713      (view_convert
9714       (bit_and:elt_type
9715        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9716        { elt; })))))))
9718 /* Fold reduction of a single nonzero element constructor.  */
9719 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9720   (simplify (reduc (CONSTRUCTOR@0))
9721     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9722                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9723             tree elt = ctor_single_nonzero_element (ctor); }
9724       (if (elt
9725            && !HONOR_SNANS (type)
9726            && !HONOR_SIGNED_ZEROS (type))
9727         { elt; }))))
9729 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9730 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9731             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9732      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9733   (simplify (reduc (op @0 VECTOR_CST@1))
9734     (op (reduc:type @0) (reduc:type @1))))
9736 /* Simplify vector floating point operations of alternating sub/add pairs
9737    into using an fneg of a wider element type followed by a normal add.
9738    under IEEE 754 the fneg of the wider type will negate every even entry
9739    and when doing an add we get a sub of the even and add of every odd
9740    elements.  */
9741 (for plusminus (plus minus)
9742      minusplus (minus plus)
9743  (simplify
9744   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9745    (if (!VECTOR_INTEGER_TYPE_P (type)
9746         && !FLOAT_WORDS_BIG_ENDIAN
9747         /* plus is commutative, while minus is not, so :c can't be used.
9748            Do equality comparisons by hand and at the end pick the operands
9749            from the minus.  */
9750         && (operand_equal_p (@0, @2, 0)
9751             ? operand_equal_p (@1, @3, 0)
9752             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9753    (with
9754     {
9755       /* Build a vector of integers from the tree mask.  */
9756       vec_perm_builder builder;
9757     }
9758     (if (tree_to_vec_perm_builder (&builder, @4))
9759      (with
9760       {
9761         /* Create a vec_perm_indices for the integer vector.  */
9762         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9763         vec_perm_indices sel (builder, 2, nelts);
9764         machine_mode vec_mode = TYPE_MODE (type);
9765         machine_mode wide_mode;
9766         scalar_mode wide_elt_mode;
9767         poly_uint64 wide_nunits;
9768         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9769       }
9770       (if (VECTOR_MODE_P (vec_mode)
9771            && sel.series_p (0, 2, 0, 2)
9772            && sel.series_p (1, 2, nelts + 1, 2)
9773            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9774            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9775            && related_vector_mode (vec_mode, wide_elt_mode,
9776                                    wide_nunits).exists (&wide_mode))
9777        (with
9778         {
9779           tree stype
9780             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9781                                               TYPE_UNSIGNED (type));
9782           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9784           /* The format has to be a non-extended ieee format.  */
9785           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9786           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9787         }
9788         (if (TYPE_MODE (stype) != BLKmode
9789              && VECTOR_TYPE_P (ntype)
9790              && fmt_old != NULL
9791              && fmt_new != NULL)
9792          (with
9793           {
9794             /* If the target doesn't support v1xx vectors, try using
9795                scalar mode xx instead.  */
9796             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9797                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9798               ntype = stype;
9799           }
9800           (if (fmt_new->signbit_rw
9801                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9802                && fmt_new->signbit_rw == fmt_new->signbit_ro
9803                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9804                                                  TYPE_MODE (type), ALL_REGS)
9805                && ((optimize_vectors_before_lowering_p ()
9806                     && VECTOR_TYPE_P (ntype))
9807                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9808            (if (plusminus == PLUS_EXPR)
9809             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9810             (minus @0 (view_convert:type
9811                         (negate (view_convert:ntype @1))))))))))))))))
9813 (simplify
9814  (vec_perm @0 @1 VECTOR_CST@2)
9815  (with
9816   {
9817     tree op0 = @0, op1 = @1, op2 = @2;
9818     machine_mode result_mode = TYPE_MODE (type);
9819     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9821     /* Build a vector of integers from the tree mask.  */
9822     vec_perm_builder builder;
9823   }
9824   (if (tree_to_vec_perm_builder (&builder, op2))
9825    (with
9826     {
9827       /* Create a vec_perm_indices for the integer vector.  */
9828       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9829       bool single_arg = (op0 == op1);
9830       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9831     }
9832     (if (sel.series_p (0, 1, 0, 1))
9833      { op0; }
9834      (if (sel.series_p (0, 1, nelts, 1))
9835       { op1; }
9836       (with
9837        {
9838          if (!single_arg)
9839            {
9840              if (sel.all_from_input_p (0))
9841                op1 = op0;
9842              else if (sel.all_from_input_p (1))
9843                {
9844                  op0 = op1;
9845                  sel.rotate_inputs (1);
9846                }
9847              else if (known_ge (poly_uint64 (sel[0]), nelts))
9848                {
9849                  std::swap (op0, op1);
9850                  sel.rotate_inputs (1);
9851                }
9852            }
9853          gassign *def;
9854          tree cop0 = op0, cop1 = op1;
9855          if (TREE_CODE (op0) == SSA_NAME
9856              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9857              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9858            cop0 = gimple_assign_rhs1 (def);
9859          if (TREE_CODE (op1) == SSA_NAME
9860              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9861              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9862            cop1 = gimple_assign_rhs1 (def);
9863          tree t;
9864        }
9865        (if ((TREE_CODE (cop0) == VECTOR_CST
9866              || TREE_CODE (cop0) == CONSTRUCTOR)
9867             && (TREE_CODE (cop1) == VECTOR_CST
9868                 || TREE_CODE (cop1) == CONSTRUCTOR)
9869             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9870         { t; }
9871         (with
9872          {
9873            bool changed = (op0 == op1 && !single_arg);
9874            tree ins = NULL_TREE;
9875            unsigned at = 0;
9877            /* See if the permutation is performing a single element
9878               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9879               in that case.  But only if the vector mode is supported,
9880               otherwise this is invalid GIMPLE.  */
9881            if (op_mode != BLKmode
9882                && (TREE_CODE (cop0) == VECTOR_CST
9883                    || TREE_CODE (cop0) == CONSTRUCTOR
9884                    || TREE_CODE (cop1) == VECTOR_CST
9885                    || TREE_CODE (cop1) == CONSTRUCTOR))
9886              {
9887                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9888                if (insert_first_p)
9889                  {
9890                    /* After canonicalizing the first elt to come from the
9891                       first vector we only can insert the first elt from
9892                       the first vector.  */
9893                    at = 0;
9894                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9895                      op0 = op1;
9896                  }
9897                /* The above can fail for two-element vectors which always
9898                   appear to insert the first element, so try inserting
9899                   into the second lane as well.  For more than two
9900                   elements that's wasted time.  */
9901                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9902                  {
9903                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9904                    for (at = 0; at < encoded_nelts; ++at)
9905                      if (maybe_ne (sel[at], at))
9906                        break;
9907                    if (at < encoded_nelts
9908                        && (known_eq (at + 1, nelts)
9909                            || sel.series_p (at + 1, 1, at + 1, 1)))
9910                      {
9911                        if (known_lt (poly_uint64 (sel[at]), nelts))
9912                          ins = fold_read_from_vector (cop0, sel[at]);
9913                        else
9914                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9915                      }
9916                  }
9917              }
9919            /* Generate a canonical form of the selector.  */
9920            if (!ins && sel.encoding () != builder)
9921              {
9922                /* Some targets are deficient and fail to expand a single
9923                   argument permutation while still allowing an equivalent
9924                   2-argument version.  */
9925                tree oldop2 = op2;
9926                if (sel.ninputs () == 2
9927                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9928                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9929                else
9930                  {
9931                    vec_perm_indices sel2 (builder, 2, nelts);
9932                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9933                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9934                    else
9935                      /* Not directly supported with either encoding,
9936                         so use the preferred form.  */
9937                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9938                  }
9939                if (!operand_equal_p (op2, oldop2, 0))
9940                  changed = true;
9941              }
9942          }
9943          (if (ins)
9944           (bit_insert { op0; } { ins; }
9945            { bitsize_int (at * vector_element_bits (type)); })
9946           (if (changed)
9947            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9949 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9951 (match vec_same_elem_p
9952  (vec_duplicate @0))
9954 (match vec_same_elem_p
9955  CONSTRUCTOR@0
9956  (if (TREE_CODE (@0) == SSA_NAME
9957       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9959 (match vec_same_elem_p
9960  @0
9961  (if (uniform_vector_p (@0))))
9964 (simplify
9965  (vec_perm vec_same_elem_p@0 @0 @1)
9966  (if (types_match (type, TREE_TYPE (@0)))
9967   @0
9968   (with
9969    {
9970      tree elem = uniform_vector_p (@0);
9971    }
9972    (if (elem)
9973     { build_vector_from_val (type, elem); }))))
9975 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9976 (simplify
9977  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9978  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9979   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9980 (simplify
9981  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9982  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9983   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9986 /* Merge
9987      c = VEC_PERM_EXPR <a, b, VCST0>;
9988      d = VEC_PERM_EXPR <c, c, VCST1>;
9989    to
9990      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9992 (simplify
9993  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9994  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9995   (with
9996    {
9997      machine_mode result_mode = TYPE_MODE (type);
9998      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9999      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10000      vec_perm_builder builder0;
10001      vec_perm_builder builder1;
10002      vec_perm_builder builder2 (nelts, nelts, 1);
10003    }
10004    (if (tree_to_vec_perm_builder (&builder0, @3)
10005         && tree_to_vec_perm_builder (&builder1, @4))
10006     (with
10007      {
10008        vec_perm_indices sel0 (builder0, 2, nelts);
10009        vec_perm_indices sel1 (builder1, 1, nelts);
10011        for (int i = 0; i < nelts; i++)
10012          builder2.quick_push (sel0[sel1[i].to_constant ()]);
10014        vec_perm_indices sel2 (builder2, 2, nelts);
10016        tree op0 = NULL_TREE;
10017        /* If the new VEC_PERM_EXPR can't be handled but both
10018           original VEC_PERM_EXPRs can, punt.
10019           If one or both of the original VEC_PERM_EXPRs can't be
10020           handled and the new one can't be either, don't increase
10021           number of VEC_PERM_EXPRs that can't be handled.  */
10022        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10023            || (single_use (@0)
10024                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10025                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10026                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10027          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10028      }
10029      (if (op0)
10030       (vec_perm @1 @2 { op0; })))))))
10032 /* Merge
10033      c = VEC_PERM_EXPR <a, b, VCST0>;
10034      d = VEC_PERM_EXPR <x, c, VCST1>;
10035    to
10036      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
10037    when all elements from a or b are replaced by the later
10038    permutation.  */
10040 (simplify
10041  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
10042  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10043   (with
10044    {
10045      machine_mode result_mode = TYPE_MODE (type);
10046      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10047      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10048      vec_perm_builder builder0;
10049      vec_perm_builder builder1;
10050      vec_perm_builder builder2 (nelts, nelts, 2);
10051    }
10052    (if (tree_to_vec_perm_builder (&builder0, @3)
10053         && tree_to_vec_perm_builder (&builder1, @4))
10054     (with
10055      {
10056        vec_perm_indices sel0 (builder0, 2, nelts);
10057        vec_perm_indices sel1 (builder1, 2, nelts);
10058        bool use_1 = false, use_2 = false;
10060        for (int i = 0; i < nelts; i++)
10061          {
10062            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10063              builder2.quick_push (sel1[i]);
10064            else
10065              {
10066                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10067                                     .to_constant ()];
10068                if (known_lt (j, sel0.nelts_per_input ()))
10069                  use_1 = true;
10070                else
10071                  {
10072                    use_2 = true;
10073                    j -= sel0.nelts_per_input ();
10074                  }
10075                builder2.quick_push (j + sel1.nelts_per_input ());
10076              }
10077          }
10078      }
10079      (if (use_1 ^ use_2)
10080       (with
10081        {
10082          vec_perm_indices sel2 (builder2, 2, nelts);
10083          tree op0 = NULL_TREE;
10084          /* If the new VEC_PERM_EXPR can't be handled but both
10085             original VEC_PERM_EXPRs can, punt.
10086             If one or both of the original VEC_PERM_EXPRs can't be
10087             handled and the new one can't be either, don't increase
10088             number of VEC_PERM_EXPRs that can't be handled.  */
10089          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10090              || (single_use (@0)
10091                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10092                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10093                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10094            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10095        }
10096        (if (op0)
10097         (switch
10098          (if (use_1)
10099           (vec_perm @5 @1 { op0; }))
10100          (if (use_2)
10101           (vec_perm @5 @2 { op0; })))))))))))
10103 /* And the case with swapped outer permute sources.  */
10105 (simplify
10106  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10107  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10108   (with
10109    {
10110      machine_mode result_mode = TYPE_MODE (type);
10111      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10112      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10113      vec_perm_builder builder0;
10114      vec_perm_builder builder1;
10115      vec_perm_builder builder2 (nelts, nelts, 2);
10116    }
10117    (if (tree_to_vec_perm_builder (&builder0, @3)
10118         && tree_to_vec_perm_builder (&builder1, @4))
10119     (with
10120      {
10121        vec_perm_indices sel0 (builder0, 2, nelts);
10122        vec_perm_indices sel1 (builder1, 2, nelts);
10123        bool use_1 = false, use_2 = false;
10125        for (int i = 0; i < nelts; i++)
10126          {
10127            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10128              builder2.quick_push (sel1[i]);
10129            else
10130              {
10131                poly_uint64 j = sel0[sel1[i].to_constant ()];
10132                if (known_lt (j, sel0.nelts_per_input ()))
10133                  use_1 = true;
10134                else
10135                  {
10136                    use_2 = true;
10137                    j -= sel0.nelts_per_input ();
10138                  }
10139                builder2.quick_push (j);
10140              }
10141          }
10142      }
10143      (if (use_1 ^ use_2)
10144       (with
10145        {
10146          vec_perm_indices sel2 (builder2, 2, nelts);
10147          tree op0 = NULL_TREE;
10148          /* If the new VEC_PERM_EXPR can't be handled but both
10149             original VEC_PERM_EXPRs can, punt.
10150             If one or both of the original VEC_PERM_EXPRs can't be
10151             handled and the new one can't be either, don't increase
10152             number of VEC_PERM_EXPRs that can't be handled.  */
10153          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10154              || (single_use (@0)
10155                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10156                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10157                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10158            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10159        }
10160        (if (op0)
10161         (switch
10162          (if (use_1)
10163           (vec_perm @1 @5 { op0; }))
10164          (if (use_2)
10165           (vec_perm @2 @5 { op0; })))))))))))
10168 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10169    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10170    constant which when multiplied by a power of 2 contains a unique value
10171    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10172    to the number of trailing zeroes.  */
10173 (match (ctz_table_index @1 @2 @3)
10174   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10176 (match (cond_expr_convert_p @0 @2 @3 @6)
10177  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10178   (if (INTEGRAL_TYPE_P (type)
10179        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10180        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10181        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10182        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10183        && TYPE_PRECISION (TREE_TYPE (@0))
10184           == TYPE_PRECISION (TREE_TYPE (@2))
10185        && TYPE_PRECISION (TREE_TYPE (@0))
10186           == TYPE_PRECISION (TREE_TYPE (@3))
10187        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10188           signess when convert is truncation, but not ok for extension since
10189           it's sign_extend vs zero_extend.  */
10190        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10191            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10192                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10193        && single_use (@4)
10194        && single_use (@5))))
10196 (for bit_op (bit_and bit_ior bit_xor)
10197  (match (bitwise_induction_p @0 @2 @3)
10198   (bit_op:c
10199    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10200    @3)))
10202 (match (bitwise_induction_p @0 @2 @3)
10203  (bit_not
10204   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10206 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10207    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10208 (simplify
10209   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10210   (with { auto i = wi::neg (wi::to_wide (@2)); }
10211   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10212     (if (wi::popcount (i) == 1
10213          && (wi::to_wide (@1)) == (i - 1))
10214       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10215         (bit_and @0 @1)
10216       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10218 /* -x & 1 -> x & 1.  */
10219 (simplify 
10220  (bit_and (negate @0) integer_onep@1)
10221  (if (!TYPE_OVERFLOW_SANITIZED (type))
10222   (bit_and @0 @1)))
10224 /* `-a` is just `a` if the type is 1bit wide or when converting
10225    to a 1bit type; similar to the above transformation of `(-x)&1`.
10226    This is used mostly with the transformation of
10227    `a ? ~b : b` into `(-a)^b`.
10228    It also can show up with bitfields.  */
10229 (simplify
10230  (convert? (negate @0))
10231  (if (INTEGRAL_TYPE_P (type)
10232       && TYPE_PRECISION (type) == 1
10233       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10234   (convert @0)))
10236 /* Optimize
10237    c1 = VEC_PERM_EXPR (a, a, mask)
10238    c2 = VEC_PERM_EXPR (b, b, mask)
10239    c3 = c1 op c2
10240    -->
10241    c = a op b
10242    c3 = VEC_PERM_EXPR (c, c, mask)
10243    For all integer non-div operations.  */
10244 (for op (plus minus mult bit_and bit_ior bit_xor
10245          lshift rshift)
10246  (simplify
10247   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10248    (if (VECTOR_INTEGER_TYPE_P (type))
10249     (vec_perm (op@3 @0 @1) @3 @2))))
10251 /* Similar for float arithmetic when permutation constant covers
10252    all vector elements.  */
10253 (for op (plus minus mult)
10254  (simplify
10255   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10256    (if (VECTOR_FLOAT_TYPE_P (type)
10257         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10258     (with
10259      {
10260        tree perm_cst = @2;
10261        vec_perm_builder builder;
10262        bool full_perm_p = false;
10263        if (tree_to_vec_perm_builder (&builder, perm_cst))
10264          {
10265            unsigned HOST_WIDE_INT nelts;
10267            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10268            /* Create a vec_perm_indices for the VECTOR_CST.  */
10269            vec_perm_indices sel (builder, 1, nelts);
10271            /* Check if perm indices covers all vector elements.  */
10272            if (sel.encoding ().encoded_full_vector_p ())
10273              {
10274                auto_sbitmap seen (nelts);
10275                bitmap_clear (seen);
10277                unsigned HOST_WIDE_INT count = 0, i;
10279                for (i = 0; i < nelts; i++)
10280                  {
10281                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10282                     break;
10283                    count++;
10284                  }
10285                full_perm_p = count == nelts;
10286              }
10287          }
10288       }
10289       (if (full_perm_p)
10290         (vec_perm (op@3 @0 @1) @3 @2))))))