tree-optimization/112706 - missed simplification of condition
[official-gcc.git] / gcc / match.pd
blob95225e4ca5f6ad651226d8d88fc112092516ec7f
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2023 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
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 #endif
187 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
188    ABSU_EXPR returns unsigned absolute value of the operand and the operand
189    of the ABSU_EXPR will have the corresponding signed type.  */
190 (simplify (abs (convert @0))
191  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
192       && !TYPE_UNSIGNED (TREE_TYPE (@0))
193       && element_precision (type) > element_precision (TREE_TYPE (@0)))
194   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
195    (convert (absu:utype @0)))))
197 #if GIMPLE
198 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
199 (simplify
200  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
201  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
202       && !TYPE_UNSIGNED (TREE_TYPE (@0))
203       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
204   (abs @0)))
205 #endif
207 /* Simplifications of operations with one constant operand and
208    simplifications to constants or single values.  */
210 (for op (plus pointer_plus minus bit_ior bit_xor)
211   (simplify
212     (op @0 integer_zerop)
213     (non_lvalue @0)))
215 /* 0 +p index -> (type)index */
216 (simplify
217  (pointer_plus integer_zerop @1)
218  (non_lvalue (convert @1)))
220 /* ptr - 0 -> (type)ptr */
221 (simplify
222  (pointer_diff @0 integer_zerop)
223  (convert @0))
225 /* See if ARG1 is zero and X + ARG1 reduces to X.
226    Likewise if the operands are reversed.  */
227 (simplify
228  (plus:c @0 real_zerop@1)
229  (if (fold_real_zero_addition_p (type, @0, @1, 0))
230   (non_lvalue @0)))
232 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
233 (simplify
234  (minus @0 real_zerop@1)
235  (if (fold_real_zero_addition_p (type, @0, @1, 1))
236   (non_lvalue @0)))
238 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
239    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
240    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
241    if not -frounding-math.  For sNaNs the first operation would raise
242    exceptions but turn the result into qNan, so the second operation
243    would not raise it.   */
244 (for inner_op (plus minus)
245  (for outer_op (plus minus)
246   (simplify
247    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
248     (if (real_zerop (@1)
249          && real_zerop (@2)
250          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
251      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
252                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
253              bool outer_plus
254                = ((outer_op == PLUS_EXPR)
255                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
256       (if (outer_plus && !inner_plus)
257        (outer_op @0 @2)
258        @3))))))
260 /* Simplify x - x.
261    This is unsafe for certain floats even in non-IEEE formats.
262    In IEEE, it is unsafe because it does wrong for NaNs.
263    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
264    Also note that operand_equal_p is always false if an operand
265    is volatile.  */
266 (simplify
267  (minus @0 @0)
268  (if (!FLOAT_TYPE_P (type)
269       || (!tree_expr_maybe_nan_p (@0)
270           && !tree_expr_maybe_infinite_p (@0)
271           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
272               || !HONOR_SIGNED_ZEROS (type))))
273   { build_zero_cst (type); }))
274 (simplify
275  (pointer_diff @@0 @0)
276  { build_zero_cst (type); })
278 (simplify
279  (mult @0 integer_zerop@1)
280  @1)
282 /* -x == x -> x == 0 */
283 (for cmp (eq ne)
284  (simplify
285   (cmp:c @0 (negate @0))
286    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
287         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
288     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
290 /* Maybe fold x * 0 to 0.  The expressions aren't the same
291    when x is NaN, since x * 0 is also NaN.  Nor are they the
292    same in modes with signed zeros, since multiplying a
293    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
294    since x * 0 is NaN.  */
295 (simplify
296  (mult @0 real_zerop@1)
297  (if (!tree_expr_maybe_nan_p (@0)
298       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
299       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
300   @1))
302 /* In IEEE floating point, x*1 is not equivalent to x for snans.
303    Likewise for complex arithmetic with signed zeros.  */
304 (simplify
305  (mult @0 real_onep)
306  (if (!tree_expr_maybe_signaling_nan_p (@0)
307       && (!HONOR_SIGNED_ZEROS (type)
308           || !COMPLEX_FLOAT_TYPE_P (type)))
309   (non_lvalue @0)))
311 /* Transform x * -1.0 into -x.  */
312 (simplify
313  (mult @0 real_minus_onep)
314   (if (!tree_expr_maybe_signaling_nan_p (@0)
315        && (!HONOR_SIGNED_ZEROS (type)
316            || !COMPLEX_FLOAT_TYPE_P (type)))
317    (negate @0)))
319 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
320    unless the target has native support for the former but not the latter.  */
321 (simplify
322  (mult @0 VECTOR_CST@1)
323  (if (initializer_each_zero_or_onep (@1)
324       && !HONOR_SNANS (type)
325       && !HONOR_SIGNED_ZEROS (type))
326   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
327    (if (itype
328         && (!VECTOR_MODE_P (TYPE_MODE (type))
329             || (VECTOR_MODE_P (TYPE_MODE (itype))
330                 && optab_handler (and_optab,
331                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
332     (view_convert (bit_and:itype (view_convert @0)
333                                  (ne @1 { build_zero_cst (type); })))))))
335 /* In SWAR (SIMD within a register) code a signed comparison of packed data
336    can be constructed with a particular combination of shift, bitwise and,
337    and multiplication by constants.  If that code is vectorized we can
338    convert this pattern into a more efficient vector comparison.  */
339 (simplify
340  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
341             uniform_integer_cst_p@2)
342     uniform_integer_cst_p@3)
343  (with {
344    tree rshift_cst = uniform_integer_cst_p (@1);
345    tree bit_and_cst = uniform_integer_cst_p (@2);
346    tree mult_cst = uniform_integer_cst_p (@3);
347   }
348   /* Make sure we're working with vectors and uniform vector constants.  */
349   (if (VECTOR_TYPE_P (type)
350        && tree_fits_uhwi_p (rshift_cst)
351        && tree_fits_uhwi_p (mult_cst)
352        && tree_fits_uhwi_p (bit_and_cst))
353    /* Compute what constants would be needed for this to represent a packed
354       comparison based on the shift amount denoted by RSHIFT_CST.  */
355    (with {
356      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
357      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
358      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
359      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
360      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
361      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
362      mult_i = tree_to_uhwi (mult_cst);
363      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
364      bit_and_i = tree_to_uhwi (bit_and_cst);
365      target_bit_and_i = 0;
367      /* The bit pattern in BIT_AND_I should be a mask for the least
368         significant bit of each packed element that is CMP_BITS wide.  */
369      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
370        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
371     }
372     (if ((exact_log2 (cmp_bits_i)) >= 0
373          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
374          && multiple_p (vec_bits, cmp_bits_i)
375          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
376          && target_mult_i == mult_i
377          && target_bit_and_i == bit_and_i)
378      /* Compute the vector shape for the comparison and check if the target is
379         able to expand the comparison with that type.  */
380      (with {
381        /* We're doing a signed comparison.  */
382        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
383        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
384        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
385        tree vec_truth_type = truth_type_for (vec_cmp_type);
386        tree zeros = build_zero_cst (vec_cmp_type);
387        tree ones = build_all_ones_cst (vec_cmp_type);
388       }
389       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
390            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
391        (view_convert:type (vec_cond (lt:vec_truth_type
392                                      (view_convert:vec_cmp_type @0)
393                                      { zeros; })
394                            { ones; } { zeros; })))))))))
396 (for cmp (gt ge lt le)
397      outp (convert convert negate negate)
398      outn (negate negate convert convert)
399  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
400  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
401  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
402  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
403  (simplify
404   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
405   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
406    (outp (abs @0))))
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  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
410  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
411  (simplify
412   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
413   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414    (outn (abs @0)))))
416 /* Transform X * copysign (1.0, X) into abs(X). */
417 (simplify
418  (mult:c @0 (COPYSIGN_ALL real_onep @0))
419  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
420   (abs @0)))
422 /* Transform X * copysign (1.0, -X) into -abs(X). */
423 (simplify
424  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
425  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
426   (negate (abs @0))))
428 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
429 (simplify
430  (COPYSIGN_ALL REAL_CST@0 @1)
431  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
432   (COPYSIGN_ALL (negate @0) @1)))
434 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
435    tree-ssa-math-opts.cc does the corresponding optimization for
436    unconditional multiplications (via xorsign).  */
437 (simplify
438  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
439  (with { tree signs = sign_mask_for (type); }
440   (if (signs)
441    (with { tree inttype = TREE_TYPE (signs); }
442     (view_convert:type
443      (IFN_COND_XOR:inttype @0
444       (view_convert:inttype @1)
445       (bit_and (view_convert:inttype @2) { signs; })
446       (view_convert:inttype @3)))))))
448 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
449 (simplify
450   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
451   (abs @0))
453 /* X * 1, X / 1 -> X.  */
454 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
455   (simplify
456     (op @0 integer_onep)
457     (non_lvalue @0)))
459 /* (A / (1 << B)) -> (A >> B).
460    Only for unsigned A.  For signed A, this would not preserve rounding
461    toward zero.
462    For example: (-1 / ( 1 << B)) !=  -1 >> B.
463    Also handle widening conversions, like:
464    (A / (unsigned long long) (1U << B)) -> (A >> B)
465    or
466    (A / (unsigned long long) (1 << B)) -> (A >> B).
467    If the left shift is signed, it can be done only if the upper bits
468    of A starting from shift's type sign bit are zero, as
469    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
470    so it is valid only if A >> 31 is zero.  */
471 (simplify
472  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
473  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
474       && (!VECTOR_TYPE_P (type)
475           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
476           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
477       && (useless_type_conversion_p (type, TREE_TYPE (@1))
478           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
479               && (TYPE_UNSIGNED (TREE_TYPE (@1))
480                   || (element_precision (type)
481                       == element_precision (TREE_TYPE (@1)))
482                   || (INTEGRAL_TYPE_P (type)
483                       && (tree_nonzero_bits (@0)
484                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
485                                       true,
486                                       element_precision (type))) == 0)))))
487    (if (!VECTOR_TYPE_P (type)
488         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
489         && element_precision (TREE_TYPE (@3)) < element_precision (type))
490     (convert (rshift @3 @2))
491     (rshift @0 @2))))
493 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
494    undefined behavior in constexpr evaluation, and assuming that the division
495    traps enables better optimizations than these anyway.  */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
497  /* 0 / X is always zero.  */
498  (simplify
499   (div integer_zerop@0 @1)
500   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
501   (if (!integer_zerop (@1))
502    @0))
503  /* X / -1 is -X.  */
504  (simplify
505   (div @0 integer_minus_onep@1)
506   (if (!TYPE_UNSIGNED (type))
507    (negate @0)))
508  /* X / bool_range_Y is X.  */ 
509  (simplify
510   (div @0 SSA_NAME@1)
511   (if (INTEGRAL_TYPE_P (type)
512        && ssa_name_has_boolean_range (@1)
513        && !flag_non_call_exceptions)
514    @0))
515  /* X / X is one.  */
516  (simplify
517   (div @0 @0)
518   /* But not for 0 / 0 so that we can get the proper warnings and errors.
519      And not for _Fract types where we can't build 1.  */
520   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
521        && !integer_zerop (@0)
522        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
523    { build_one_cst (type); }))
524  /* X / abs (X) is X < 0 ? -1 : 1.  */
525  (simplify
526    (div:C @0 (abs @0))
527    (if (INTEGRAL_TYPE_P (type)
528         && TYPE_OVERFLOW_UNDEFINED (type)
529         && !integer_zerop (@0)
530         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
531     (cond (lt @0 { build_zero_cst (type); })
532           { build_minus_one_cst (type); } { build_one_cst (type); })))
533  /* X / -X is -1.  */
534  (simplify
535    (div:C @0 (negate @0))
536    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
537         && TYPE_OVERFLOW_UNDEFINED (type)
538         && !integer_zerop (@0)
539         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
540     { build_minus_one_cst (type); })))
542 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
543    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
544    for MOD instead of DIV.  */
545 (for floor_divmod (floor_div floor_mod)
546      trunc_divmod (trunc_div trunc_mod)
547  (simplify
548   (floor_divmod @0 @1)
549   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
550        && TYPE_UNSIGNED (type))
551    (trunc_divmod @0 @1))))
553 /* 1 / X -> X == 1 for unsigned integer X.
554    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
555    But not for 1 / 0 so that we can get proper warnings and errors,
556    and not for 1-bit integers as they are edge cases better handled
557    elsewhere.  */
558 (simplify
559  (trunc_div integer_onep@0 @1)
560  (if (INTEGRAL_TYPE_P (type)
561       && TYPE_PRECISION (type) > 1
562       && !integer_zerop (@1)
563       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
564   (if (TYPE_UNSIGNED (type))
565    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
566    (with { tree utype = unsigned_type_for (type); }
567     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
568               { build_int_cst (utype, 2); })
569      @1 { build_zero_cst (type); })))))
571 /* Combine two successive divisions.  Note that combining ceil_div
572    and floor_div is trickier and combining round_div even more so.  */
573 (for div (trunc_div exact_div)
574  (simplify
575   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
576   (with {
577     wi::overflow_type overflow;
578     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
579                             TYPE_SIGN (type), &overflow);
580    }
581    (if (div == EXACT_DIV_EXPR
582         || optimize_successive_divisions_p (@2, @3))
583     (if (!overflow)
584      (div @0 { wide_int_to_tree (type, mul); })
585      (if (TYPE_UNSIGNED (type)
586           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
587       { build_zero_cst (type); }))))))
589 /* Combine successive multiplications.  Similar to above, but handling
590    overflow is different.  */
591 (simplify
592  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
593  (with {
594    wi::overflow_type overflow;
595    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
596                            TYPE_SIGN (type), &overflow);
597   }
598   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
599      otherwise undefined overflow implies that @0 must be zero.  */
600   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
601    (mult @0 { wide_int_to_tree (type, mul); }))))
603 /* Similar to above, but there could be an extra add/sub between
604    successive multuiplications.  */
605 (simplify
606  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
607  (with {
608    bool overflowed = true;
609    wi::overflow_type ovf1, ovf2;
610    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
611                            TYPE_SIGN (type), &ovf1);
612    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
613                            TYPE_SIGN (type), &ovf2);
614   if (TYPE_OVERFLOW_UNDEFINED (type))
615     {
616 #if GIMPLE
617       value_range vr0;
618       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
619           && get_global_range_query ()->range_of_expr (vr0, @4)
620           && !vr0.varying_p () && !vr0.undefined_p ())
621         {
622           wide_int wmin0 = vr0.lower_bound ();
623           wide_int wmax0 = vr0.upper_bound ();
624           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
625           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
626           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
627             {
628               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
629               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
630               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
631                 overflowed = false;
632             }
633         }
634 #endif
635     }
636   else
637    overflowed = false;
639   /* Skip folding on overflow.  */
640   (if (!overflowed)
641    (plus (mult @0 { wide_int_to_tree (type, mul); })
642          { wide_int_to_tree (type, add); }))))
644 /* Similar to above, but a multiplication between successive additions.  */
645 (simplify
646  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
647  (with {
648    bool overflowed = true;
649    wi::overflow_type ovf1;
650    wi::overflow_type ovf2;
651    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
652                            TYPE_SIGN (type), &ovf1);
653    wide_int add = wi::add (mul, wi::to_wide (@3),
654                            TYPE_SIGN (type), &ovf2);
655   if (TYPE_OVERFLOW_UNDEFINED (type))
656     {
657 #if GIMPLE
658       value_range vr0;
659       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
660           && get_global_range_query ()->range_of_expr (vr0, @0)
661           && !vr0.varying_p () && !vr0.undefined_p ())
662         {
663           wide_int wmin0 = vr0.lower_bound ();
664           wide_int wmax0 = vr0.upper_bound ();
665           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
666           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
667           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
668             {
669               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
670               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
671               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
672                 overflowed = false;
673             }
674         }
675 #endif
676     }
677   else
678    overflowed = false;
680   /* Skip folding on overflow.  */
681   (if (!overflowed)
682    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
684 /* Optimize A / A to 1.0 if we don't care about
685    NaNs or Infinities.  */
686 (simplify
687  (rdiv @0 @0)
688  (if (FLOAT_TYPE_P (type)
689       && ! HONOR_NANS (type)
690       && ! HONOR_INFINITIES (type))
691   { build_one_cst (type); }))
693 /* Optimize -A / A to -1.0 if we don't care about
694    NaNs or Infinities.  */
695 (simplify
696  (rdiv:C @0 (negate @0))
697  (if (FLOAT_TYPE_P (type)
698       && ! HONOR_NANS (type)
699       && ! HONOR_INFINITIES (type))
700   { build_minus_one_cst (type); }))
702 /* PR71078: x / abs(x) -> copysign (1.0, x) */
703 (simplify
704  (rdiv:C (convert? @0) (convert? (abs @0)))
705   (if (SCALAR_FLOAT_TYPE_P (type)
706        && ! HONOR_NANS (type)
707        && ! HONOR_INFINITIES (type))
708    (switch
709     (if (types_match (type, float_type_node))
710      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
711     (if (types_match (type, double_type_node))
712      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
713     (if (types_match (type, long_double_type_node))
714      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
716 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
717 (simplify
718  (rdiv @0 real_onep)
719  (if (!tree_expr_maybe_signaling_nan_p (@0))
720   (non_lvalue @0)))
722 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
723 (simplify
724  (rdiv @0 real_minus_onep)
725  (if (!tree_expr_maybe_signaling_nan_p (@0))
726   (negate @0)))
728 (if (flag_reciprocal_math)
729  /* Convert (A/B)/C to A/(B*C). */
730  (simplify
731   (rdiv (rdiv:s @0 @1) @2)
732   (rdiv @0 (mult @1 @2)))
734  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
735  (simplify
736   (rdiv @0 (mult:s @1 REAL_CST@2))
737   (with
738    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
739    (if (tem)
740     (rdiv (mult @0 { tem; } ) @1))))
742  /* Convert A/(B/C) to (A/B)*C  */
743  (simplify
744   (rdiv @0 (rdiv:s @1 @2))
745    (mult (rdiv @0 @1) @2)))
747 /* Simplify x / (- y) to -x / y.  */
748 (simplify
749  (rdiv @0 (negate @1))
750  (rdiv (negate @0) @1))
752 (if (flag_unsafe_math_optimizations)
753  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
754     Since C / x may underflow to zero, do this only for unsafe math.  */
755  (for op (lt le gt ge)
756       neg_op (gt ge lt le)
757   (simplify
758    (op (rdiv REAL_CST@0 @1) real_zerop@2)
759    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
760     (switch
761      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
762       (op @1 @2))
763      /* For C < 0, use the inverted operator.  */
764      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
765       (neg_op @1 @2)))))))
767 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
768 (for div (trunc_div ceil_div floor_div round_div exact_div)
769  (simplify
770   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
771   (if (integer_pow2p (@2)
772        && tree_int_cst_sgn (@2) > 0
773        && tree_nop_conversion_p (type, TREE_TYPE (@0))
774        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
775    (rshift (convert @0)
776            { build_int_cst (integer_type_node,
777                             wi::exact_log2 (wi::to_wide (@2))); }))))
779 /* If ARG1 is a constant, we can convert this to a multiply by the
780    reciprocal.  This does not have the same rounding properties,
781    so only do this if -freciprocal-math.  We can actually
782    always safely do it if ARG1 is a power of two, but it's hard to
783    tell if it is or not in a portable manner.  */
784 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
785  (simplify
786   (rdiv @0 cst@1)
787   (if (optimize)
788    (if (flag_reciprocal_math
789         && !real_zerop (@1))
790     (with
791      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
792      (if (tem)
793       (mult @0 { tem; } )))
794     (if (cst != COMPLEX_CST)
795      (with { tree inverse = exact_inverse (type, @1); }
796       (if (inverse)
797        (mult @0 { inverse; } ))))))))
799 (for mod (ceil_mod floor_mod round_mod trunc_mod)
800  /* 0 % X is always zero.  */
801  (simplify
802   (mod integer_zerop@0 @1)
803   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
804   (if (!integer_zerop (@1))
805    @0))
806  /* X % 1 is always zero.  */
807  (simplify
808   (mod @0 integer_onep)
809   { build_zero_cst (type); })
810  /* X % -1 is zero.  */
811  (simplify
812   (mod @0 integer_minus_onep@1)
813   (if (!TYPE_UNSIGNED (type))
814    { build_zero_cst (type); }))
815  /* X % X is zero.  */
816  (simplify
817   (mod @0 @0)
818   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
819   (if (!integer_zerop (@0))
820    { build_zero_cst (type); }))
821  /* (X % Y) % Y is just X % Y.  */
822  (simplify
823   (mod (mod@2 @0 @1) @1)
824   @2)
825  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
826  (simplify
827   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
828   (if (ANY_INTEGRAL_TYPE_P (type)
829        && TYPE_OVERFLOW_UNDEFINED (type)
830        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
831                              TYPE_SIGN (type)))
832    { build_zero_cst (type); }))
833  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
834     modulo and comparison, since it is simpler and equivalent.  */
835  (for cmp (eq ne)
836   (simplify
837    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
838    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
839     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
840      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
842 /* X % -C is the same as X % C.  */
843 (simplify
844  (trunc_mod @0 INTEGER_CST@1)
845   (if (TYPE_SIGN (type) == SIGNED
846        && !TREE_OVERFLOW (@1)
847        && wi::neg_p (wi::to_wide (@1))
848        && !TYPE_OVERFLOW_TRAPS (type)
849        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
850        && !sign_bit_p (@1, @1))
851    (trunc_mod @0 (negate @1))))
853 /* X % -Y is the same as X % Y.  */
854 (simplify
855  (trunc_mod @0 (convert? (negate @1)))
856  (if (INTEGRAL_TYPE_P (type)
857       && !TYPE_UNSIGNED (type)
858       && !TYPE_OVERFLOW_TRAPS (type)
859       && tree_nop_conversion_p (type, TREE_TYPE (@1))
860       /* Avoid this transformation if X might be INT_MIN or
861          Y might be -1, because we would then change valid
862          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
863       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
864           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
865                                                         (TREE_TYPE (@1))))))
866   (trunc_mod @0 (convert @1))))
868 /* X - (X / Y) * Y is the same as X % Y.  */
869 (simplify
870  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
871  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
872   (convert (trunc_mod @0 @1))))
874 /* x * (1 + y / x) - y -> x - y % x */
875 (simplify
876  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
877  (if (INTEGRAL_TYPE_P (type))
878   (minus @0 (trunc_mod @1 @0))))
880 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
881    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
882    Also optimize A % (C << N)  where C is a power of 2,
883    to A & ((C << N) - 1).
884    Also optimize "A shift (B % C)", if C is a power of 2, to
885    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
886    and assume (B % C) is nonnegative as shifts negative values would
887    be UB.  */
888 (match (power_of_two_cand @1)
889  INTEGER_CST@1)
890 (match (power_of_two_cand @1)
891  (lshift INTEGER_CST@1 @2))
892 (for mod (trunc_mod floor_mod)
893  (for shift (lshift rshift)
894   (simplify
895    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
896    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
897     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
898                                                       1); }))))))
899  (simplify
900   (mod @0 (convert? (power_of_two_cand@1 @2)))
901   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
902        /* Allow any integral conversions of the divisor, except
903           conversion from narrower signed to wider unsigned type
904           where if @1 would be negative power of two, the divisor
905           would not be a power of two.  */
906        && INTEGRAL_TYPE_P (type)
907        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
908        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
909            || TYPE_UNSIGNED (TREE_TYPE (@1))
910            || !TYPE_UNSIGNED (type))
911        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
912    (with { tree utype = TREE_TYPE (@1);
913            if (!TYPE_OVERFLOW_WRAPS (utype))
914              utype = unsigned_type_for (utype); }
915     (bit_and @0 (convert (minus (convert:utype @1)
916                                 { build_one_cst (utype); })))))))
918 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
919 (simplify
920  (trunc_div (mult @0 integer_pow2p@1) @1)
921  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
922   (bit_and @0 { wide_int_to_tree
923                 (type, wi::mask (TYPE_PRECISION (type)
924                                  - wi::exact_log2 (wi::to_wide (@1)),
925                                  false, TYPE_PRECISION (type))); })))
927 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
928 (simplify
929  (mult (trunc_div @0 integer_pow2p@1) @1)
930  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931   (bit_and @0 (negate @1))))
933 /* Simplify (t * 2) / 2) -> t.  */
934 (for div (trunc_div ceil_div floor_div round_div exact_div)
935  (simplify
936   (div (mult:c @0 @1) @1)
937   (if (ANY_INTEGRAL_TYPE_P (type))
938    (if (TYPE_OVERFLOW_UNDEFINED (type))
939     @0
940 #if GIMPLE
941     (with {value_range vr0, vr1;}
942      (if (INTEGRAL_TYPE_P (type)
943           && get_range_query (cfun)->range_of_expr (vr0, @0)
944           && get_range_query (cfun)->range_of_expr (vr1, @1)
945           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
946       @0))
947 #endif
948    ))))
950 #if GIMPLE
951 (for div (trunc_div exact_div)
952  /* Simplify (X + M*N) / N -> X / N + M.  */
953  (simplify
954   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
955   (with {value_range vr0, vr1, vr2, vr3, vr4;}
956   (if (INTEGRAL_TYPE_P (type)
957        && get_range_query (cfun)->range_of_expr (vr1, @1)
958        && get_range_query (cfun)->range_of_expr (vr2, @2)
959        /* "N*M" doesn't overflow.  */
960        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
961        && get_range_query (cfun)->range_of_expr (vr0, @0)
962        && get_range_query (cfun)->range_of_expr (vr3, @3)
963        /* "X+(N*M)" doesn't overflow.  */
964        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
965        && get_range_query (cfun)->range_of_expr (vr4, @4)
966        && !vr4.undefined_p ()
967        /* "X+N*M" is not with opposite sign as "X".  */
968        && (TYPE_UNSIGNED (type)
969            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
970            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
971   (plus (div @0 @2) @1))))
973  /* Simplify (X - M*N) / N -> X / N - M.  */
974  (simplify
975   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
976   (with {value_range vr0, vr1, vr2, vr3, vr4;}
977   (if (INTEGRAL_TYPE_P (type)
978        && get_range_query (cfun)->range_of_expr (vr1, @1)
979        && get_range_query (cfun)->range_of_expr (vr2, @2)
980        /* "N * M" doesn't overflow.  */
981        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
982        && get_range_query (cfun)->range_of_expr (vr0, @0)
983        && get_range_query (cfun)->range_of_expr (vr3, @3)
984        /* "X - (N*M)" doesn't overflow.  */
985        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
986        && get_range_query (cfun)->range_of_expr (vr4, @4)
987        && !vr4.undefined_p ()
988        /* "X-N*M" is not with opposite sign as "X".  */
989        && (TYPE_UNSIGNED (type)
990            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
991            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
992   (minus (div @0 @2) @1)))))
994 /* Simplify
995    (X + C) / N -> X / N + C / N where C is multiple of N.
996    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
997 (for op (trunc_div exact_div rshift)
998  (simplify
999   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1000    (with
1001     {
1002       wide_int c = wi::to_wide (@1);
1003       wide_int n = wi::to_wide (@2);
1004       bool shift = op == RSHIFT_EXPR;
1005 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1006                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1007 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1008                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1009       value_range vr0, vr1, vr3;
1010     }
1011     (if (INTEGRAL_TYPE_P (type)
1012          && get_range_query (cfun)->range_of_expr (vr0, @0))
1013      (if (exact_mod (c)
1014           && get_range_query (cfun)->range_of_expr (vr1, @1)
1015           /* "X+C" doesn't overflow.  */
1016           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1017           && get_range_query (cfun)->range_of_expr (vr3, @3)
1018           && !vr3.undefined_p ()
1019           /* "X+C" and "X" are not of opposite sign.  */
1020           && (TYPE_UNSIGNED (type)
1021               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1022               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1023        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1024        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1025             && exact_mod (-c)
1026             /* unsigned "X-(-C)" doesn't underflow.  */
1027             && wi::geu_p (vr0.lower_bound (), -c))
1028          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1029 #undef plus_op1
1030 #undef exact_mod
1031 #endif
1033 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1034    if var is smaller in precision.
1035    This is always safe for both doing the negative in signed or unsigned
1036    as the value for undefined will not show up.  */
1037 (simplify
1038  (convert (negate:s@1 (convert:s @0)))
1039  (if (INTEGRAL_TYPE_P (type)
1040       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1041       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1042     (negate (convert @0))))
1044 (for op (negate abs)
1045  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1046  (for coss (COS COSH)
1047   (simplify
1048    (coss (op @0))
1049     (coss @0)))
1050  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1051  (for pows (POW)
1052   (simplify
1053    (pows (op @0) REAL_CST@1)
1054    (with { HOST_WIDE_INT n; }
1055     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1056      (pows @0 @1)))))
1057  /* Likewise for powi.  */
1058  (for pows (POWI)
1059   (simplify
1060    (pows (op @0) INTEGER_CST@1)
1061    (if ((wi::to_wide (@1) & 1) == 0)
1062     (pows @0 @1))))
1063  /* Strip negate and abs from both operands of hypot.  */
1064  (for hypots (HYPOT)
1065   (simplify
1066    (hypots (op @0) @1)
1067    (hypots @0 @1))
1068   (simplify
1069    (hypots @0 (op @1))
1070    (hypots @0 @1)))
1071  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1072  (for copysigns (COPYSIGN_ALL)
1073   (simplify
1074    (copysigns (op @0) @1)
1075    (copysigns @0 @1))))
1077 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1078 (simplify
1079  (mult (abs@1 @0) @1)
1080  (mult @0 @0))
1082 /* Convert absu(x)*absu(x) -> x*x.  */
1083 (simplify
1084  (mult (absu@1 @0) @1)
1085  (mult (convert@2 @0) @2))
1087 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1088 (for coss (COS COSH)
1089  (for copysigns (COPYSIGN)
1090   (simplify
1091    (coss (copysigns @0 @1))
1092     (coss @0))))
1094 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1095 (for pows (POW)
1096  (for copysigns (COPYSIGN)
1097   (simplify
1098    (pows (copysigns @0 @2) 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  (for copysigns (COPYSIGN)
1105   (simplify
1106    (pows (copysigns @0 @2) INTEGER_CST@1)
1107    (if ((wi::to_wide (@1) & 1) == 0)
1108     (pows @0 @1)))))
1110 (for hypots (HYPOT)
1111  (for copysigns (COPYSIGN)
1112   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1113   (simplify
1114    (hypots (copysigns @0 @1) @2)
1115    (hypots @0 @2))
1116   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1117   (simplify
1118    (hypots @0 (copysigns @1 @2))
1119    (hypots @0 @1))))
1121 /* copysign(x, CST) -> abs (x).  */
1122 (for copysigns (COPYSIGN_ALL)
1123  (simplify
1124   (copysigns @0 REAL_CST@1)
1125   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1126    (abs @0))))
1128 /* Transform fneg (fabs (X)) -> copysign (X, -1).  */
1129 (simplify
1130  (negate (abs @0))
1131  (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1133 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1134 (for copysigns (COPYSIGN_ALL)
1135  (simplify
1136   (copysigns (copysigns @0 @1) @2)
1137   (copysigns @0 @2)))
1139 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1140 (for copysigns (COPYSIGN_ALL)
1141  (simplify
1142   (mult (copysigns@2 @0 @1) @2)
1143   (mult @0 @0)))
1145 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1146 (for ccoss (CCOS CCOSH)
1147  (simplify
1148   (ccoss (negate @0))
1149    (ccoss @0)))
1151 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1152 (for ops (conj negate)
1153  (for cabss (CABS)
1154   (simplify
1155    (cabss (ops @0))
1156    (cabss @0))))
1158 /* Fold (a * (1 << b)) into (a << b)  */
1159 (simplify
1160  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1161   (if (! FLOAT_TYPE_P (type)
1162        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1163    (lshift @0 @2)))
1165 /* Shifts by precision or greater result in zero.  */
1166 (for shift (lshift rshift)
1167  (simplify
1168   (shift @0 uniform_integer_cst_p@1)
1169   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1170        /* Leave arithmetic right shifts of possibly negative values alone.  */
1171        && (TYPE_UNSIGNED (type)
1172            || shift == LSHIFT_EXPR
1173            || tree_expr_nonnegative_p (@0))
1174        /* Use a signed compare to leave negative shift counts alone.  */
1175        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1176                      element_precision (type)))
1177    { build_zero_cst (type); })))
1179 /* Shifts by constants distribute over several binary operations,
1180    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1181 (for op (plus minus)
1182   (simplify
1183     (op (lshift:s @0 @1) (lshift:s @2 @1))
1184     (if (INTEGRAL_TYPE_P (type)
1185          && TYPE_OVERFLOW_WRAPS (type)
1186          && !TYPE_SATURATING (type))
1187       (lshift (op @0 @2) @1))))
1189 (for op (bit_and bit_ior bit_xor)
1190   (simplify
1191     (op (lshift:s @0 @1) (lshift:s @2 @1))
1192     (if (INTEGRAL_TYPE_P (type))
1193       (lshift (op @0 @2) @1)))
1194   (simplify
1195     (op (rshift:s @0 @1) (rshift:s @2 @1))
1196     (if (INTEGRAL_TYPE_P (type))
1197       (rshift (op @0 @2) @1))))
1199 /* Fold (1 << (C - x)) where C = precision(type) - 1
1200    into ((1 << C) >> x). */
1201 (simplify
1202  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1203   (if (INTEGRAL_TYPE_P (type)
1204        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1205        && single_use (@1))
1206    (if (TYPE_UNSIGNED (type))
1207      (rshift (lshift @0 @2) @3)
1208    (with
1209     { tree utype = unsigned_type_for (type); }
1210     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1212 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1213 (simplify
1214  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1215  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1216       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1217   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1218    (bit_and (convert @0)
1219             { wide_int_to_tree (type,
1220                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1222 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1223 (for cst (INTEGER_CST VECTOR_CST)
1224  (simplify
1225   (rshift (negate:s @0) cst@1)
1226    (if (!TYPE_UNSIGNED (type)
1227         && TYPE_OVERFLOW_UNDEFINED (type))
1228     (with { tree stype = TREE_TYPE (@1);
1229             tree bt = truth_type_for (type);
1230             tree zeros = build_zero_cst (type);
1231             tree cst = NULL_TREE; }
1232      (switch
1233       /* Handle scalar case.  */
1234       (if (INTEGRAL_TYPE_P (type)
1235            /* If we apply the rule to the scalar type before vectorization
1236               we will enforce the result of the comparison being a bool
1237               which will require an extra AND on the result that will be
1238               indistinguishable from when the user did actually want 0
1239               or 1 as the result so it can't be removed.  */
1240            && canonicalize_math_after_vectorization_p ()
1241            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1242        (negate (convert (gt @0 { zeros; }))))
1243       /* Handle vector case.  */
1244       (if (VECTOR_INTEGER_TYPE_P (type)
1245            /* First check whether the target has the same mode for vector
1246               comparison results as it's operands do.  */
1247            && TYPE_MODE (bt) == TYPE_MODE (type)
1248            /* Then check to see if the target is able to expand the comparison
1249               with the given type later on, otherwise we may ICE.  */
1250            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1251            && (cst = uniform_integer_cst_p (@1)) != NULL
1252            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1253        (view_convert (gt:bt @0 { zeros; }))))))))
1255 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1256 (simplify
1257  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1258   (if (flag_associative_math
1259        && single_use (@3))
1260    (with
1261     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1262     (if (tem)
1263      (rdiv { tem; } @1)))))
1265 /* Simplify ~X & X as zero.  */
1266 (simplify
1267  (bit_and (convert? @0) (convert? @1))
1268  (with { bool wascmp; }
1269   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1270        && bitwise_inverted_equal_p (@0, @1, wascmp))
1271    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1273 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1274 (simplify
1275   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1276   (if (TYPE_UNSIGNED (type))
1277     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1279 (for bitop (bit_and bit_ior)
1280      cmp (eq ne)
1281  /* PR35691: Transform
1282     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1283     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1284  (simplify
1285   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1286    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1287         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1288         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1289     (cmp (bit_ior @0 (convert @1)) @2)))
1290  /* Transform:
1291     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1292     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1293  (simplify
1294   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1295    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1296         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1297         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1298     (cmp (bit_and @0 (convert @1)) @2))))
1300 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1301 (simplify
1302  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1303   (minus (bit_xor @0 @1) @1))
1304 (simplify
1305  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1306  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1307   (minus (bit_xor @0 @1) @1)))
1309 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1310 (simplify
1311  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1312   (minus @1 (bit_xor @0 @1)))
1314 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1315 (for op (bit_ior bit_xor plus)
1316  (simplify
1317   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1318   (with { bool wascmp0, wascmp1; }
1319    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1320         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1321         && ((!wascmp0 && !wascmp1)
1322             || element_precision (type) == 1))
1323    (bit_xor @0 @1)))))
1325 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1326 (simplify
1327   (bit_ior:c (bit_xor:c @0 @1) @0)
1328   (bit_ior @0 @1))
1330 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1331 (simplify
1332  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1333  @2)
1335 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1336 (simplify
1337  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1338  (bit_not (bit_and @0 @1)))
1340 /* (~a & b) ^ a  -->   (a | b)   */
1341 (simplify
1342  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1343  (bit_ior @0 @1))
1345 /* (a | b) & ~(a ^ b)  -->  a & b  */
1346 (simplify
1347  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1348  (bit_and @0 @1))
1350 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1351 (simplify
1352  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1353  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1354       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1355   (bit_and @0 @1)))
1357 /* a | ~(a ^ b)  -->  a | ~b  */
1358 (simplify
1359  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1360  (bit_ior @0 (bit_not @1)))
1362 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1363 (simplify
1364  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1365  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1366       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1367   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1369 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1370 (simplify
1371  (bit_ior:c @0 (bit_xor:cs @1 @2))
1372  (with { bool wascmp; }
1373  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1374       && (!wascmp || element_precision (type) == 1))
1375   (bit_ior @0 (bit_not @2)))))
1377 /* a & ~(a ^ b)  -->  a & b  */
1378 (simplify
1379  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1380  (bit_and @0 @1))
1382 /* a & (a == b)  -->  a & b (boolean version of the above). */
1383 (simplify
1384  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1385  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1386       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1387   (bit_and @0 @1)))
1389 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1390 (simplify
1391  (bit_and:c @0 (bit_xor:c @1 @2))
1392  (with { bool wascmp; }
1393  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1394       && (!wascmp || element_precision (type) == 1))
1395   (bit_and @0 @2))))
1397 /* (a | b) | (a &^ b)  -->  a | b  */
1398 (for op (bit_and bit_xor)
1399  (simplify
1400   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1401   @2))
1403 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1404 (simplify
1405  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1406  @2)
1408 /* (a & b) | (a == b)  -->  a == b  */
1409 (simplify
1410  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1411  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1412       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1413   @2))
1415 /* ~(~a & b)  -->  a | ~b  */
1416 (simplify
1417  (bit_not (bit_and:cs (bit_not @0) @1))
1418  (bit_ior @0 (bit_not @1)))
1420 /* ~(~a | b) --> a & ~b */
1421 (simplify
1422  (bit_not (bit_ior:cs (bit_not @0) @1))
1423  (bit_and @0 (bit_not @1)))
1425 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1426 (simplify
1427  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1428  (bit_and @3 (bit_not @2)))
1430 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1431 (simplify
1432  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1433  (bit_ior @3 @2))
1435 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1436 (simplify
1437  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1438   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1440 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1441 (simplify
1442  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1443   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1445 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1446 (simplify
1447  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1448  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1449       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1450   (bit_xor @0 @1)))
1452 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1453    ((A & N) + B) & M -> (A + B) & M
1454    Similarly if (N & M) == 0,
1455    ((A | N) + B) & M -> (A + B) & M
1456    and for - instead of + (or unary - instead of +)
1457    and/or ^ instead of |.
1458    If B is constant and (B & M) == 0, fold into A & M.  */
1459 (for op (plus minus)
1460  (for bitop (bit_and bit_ior bit_xor)
1461   (simplify
1462    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1463     (with
1464      { tree pmop[2];
1465        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1466                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1467                                        NULL_TREE, pmop); }
1468      (if (utype)
1469       (convert (bit_and (op (convert:utype { pmop[0]; })
1470                             (convert:utype { pmop[1]; }))
1471                         (convert:utype @2))))))
1472   (simplify
1473    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1474     (with
1475      { tree pmop[2];
1476        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1477                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1478                                        @4, pmop); }
1479      (if (utype)
1480       (convert (bit_and (op (convert:utype { pmop[0]; })
1481                             (convert:utype { pmop[1]; }))
1482                         (convert:utype @2)))))))
1483  (simplify
1484   (bit_and (op:s @0 @1) INTEGER_CST@2)
1485    (with
1486     { tree pmop[2];
1487       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1488                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1489                                       NULL_TREE, NULL_TREE, pmop); }
1490     (if (utype)
1491      (convert (bit_and (op (convert:utype { pmop[0]; })
1492                            (convert:utype { pmop[1]; }))
1493                        (convert:utype @2)))))))
1494 (for bitop (bit_and bit_ior bit_xor)
1495  (simplify
1496   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1497    (with
1498     { tree pmop[2];
1499       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1500                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1501                                       NULL_TREE, NULL_TREE, pmop); }
1502     (if (utype)
1503      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1504                        (convert:utype @1)))))))
1506 /* X % Y is smaller than Y.  */
1507 (for cmp (lt ge)
1508  (simplify
1509   (cmp:c (trunc_mod @0 @1) @1)
1510   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1511    { constant_boolean_node (cmp == LT_EXPR, type); })))
1513 /* x | ~0 -> ~0  */
1514 (simplify
1515  (bit_ior @0 integer_all_onesp@1)
1516  @1)
1518 /* x | 0 -> x  */
1519 (simplify
1520  (bit_ior @0 integer_zerop)
1521  @0)
1523 /* x & 0 -> 0  */
1524 (simplify
1525  (bit_and @0 integer_zerop@1)
1526  @1)
1528 /* ~x | x -> -1 */
1529 /* ~x ^ x -> -1 */
1530 (for op (bit_ior bit_xor)
1531  (simplify
1532   (op (convert? @0) (convert? @1))
1533   (with { bool wascmp; }
1534    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1535         && bitwise_inverted_equal_p (@0, @1, wascmp))
1536     (convert
1537      { wascmp
1538         ? constant_boolean_node (true, type)
1539         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1541 /* x ^ x -> 0 */
1542 (simplify
1543   (bit_xor @0 @0)
1544   { build_zero_cst (type); })
1546 /* Canonicalize X ^ ~0 to ~X.  */
1547 (simplify
1548   (bit_xor @0 integer_all_onesp@1)
1549   (bit_not @0))
1551 /* x & ~0 -> x  */
1552 (simplify
1553  (bit_and @0 integer_all_onesp)
1554   (non_lvalue @0))
1556 /* x & x -> x,  x | x -> x  */
1557 (for bitop (bit_and bit_ior)
1558  (simplify
1559   (bitop @0 @0)
1560   (non_lvalue @0)))
1562 /* x & C -> x if we know that x & ~C == 0.  */
1563 #if GIMPLE
1564 (simplify
1565  (bit_and SSA_NAME@0 INTEGER_CST@1)
1566  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1567       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1568   @0))
1570 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1571 (simplify
1572  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1573  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1574       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1575   @0))
1577 /* x | C -> C if we know that x & ~C == 0.  */
1578 (simplify
1579  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1580  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1581       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1582   @1))
1583 #endif
1585 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1586 (simplify
1587  (bit_not (minus (bit_not @0) @1))
1588  (plus @0 @1))
1589 (simplify
1590  (bit_not (plus:c (bit_not @0) @1))
1591  (minus @0 @1))
1592 /* (~X - ~Y) -> Y - X.  */
1593 (simplify
1594  (minus (bit_not @0) (bit_not @1))
1595   (if (!TYPE_OVERFLOW_SANITIZED (type))
1596    (with { tree utype = unsigned_type_for (type); }
1597     (convert (minus (convert:utype @1) (convert:utype @0))))))
1599 /* ~(X - Y) -> ~X + Y.  */
1600 (simplify
1601  (bit_not (minus:s @0 @1))
1602  (plus (bit_not @0) @1))
1603 (simplify
1604  (bit_not (plus:s @0 INTEGER_CST@1))
1605  (if ((INTEGRAL_TYPE_P (type)
1606        && TYPE_UNSIGNED (type))
1607       || (!TYPE_OVERFLOW_SANITIZED (type)
1608           && may_negate_without_overflow_p (@1)))
1609   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1611 #if GIMPLE
1612 /* ~X + Y -> (Y - X) - 1.  */
1613 (simplify
1614  (plus:c (bit_not @0) @1)
1615   (if (ANY_INTEGRAL_TYPE_P (type)
1616        && TYPE_OVERFLOW_WRAPS (type)
1617        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1618        && !integer_all_onesp (@1))
1619    (plus (minus @1 @0) { build_minus_one_cst (type); })
1620    (if (INTEGRAL_TYPE_P (type)
1621         && TREE_CODE (@1) == INTEGER_CST
1622         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1623                                               SIGNED))
1624     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1625 #endif
1627 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1628 (simplify
1629  (bit_not (rshift:s @0 @1))
1630   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1631    (rshift (bit_not! @0) @1)
1632    /* For logical right shifts, this is possible only if @0 doesn't
1633       have MSB set and the logical right shift is changed into
1634       arithmetic shift.  */
1635    (if (INTEGRAL_TYPE_P (type)
1636         && !wi::neg_p (tree_nonzero_bits (@0)))
1637     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1638      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1640 /* x + (x & 1) -> (x + 1) & ~1 */
1641 (simplify
1642  (plus:c @0 (bit_and:s @0 integer_onep@1))
1643  (bit_and (plus @0 @1) (bit_not @1)))
1645 /* x & ~(x & y) -> x & ~y */
1646 /* x | ~(x | y) -> x | ~y  */
1647 (for bitop (bit_and bit_ior)
1648  (simplify
1649   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1650   (bitop @0 (bit_not @1))))
1652 /* (~x & y) | ~(x | y) -> ~x */
1653 (simplify
1654  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1655  @2)
1657 /* (x | y) ^ (x | ~y) -> ~x */
1658 (simplify
1659  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1660  (bit_not @0))
1662 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1663 (simplify
1664  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1665  (bit_not (bit_xor @0 @1)))
1667 /* (~x | y) ^ (x ^ y) -> x | ~y */
1668 (simplify
1669  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1670  (bit_ior @0 (bit_not @1)))
1672 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1673 (simplify
1674  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1675  (bit_not (bit_and @0 @1)))
1677 /* (x & y) ^ (x | y) -> x ^ y */
1678 (simplify
1679  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1680  (bit_xor @0 @1))
1682 /* (x ^ y) ^ (x | y) -> x & y */
1683 (simplify
1684  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1685  (bit_and @0 @1))
1687 /* (x & y) + (x ^ y) -> x | y */
1688 /* (x & y) | (x ^ y) -> x | y */
1689 /* (x & y) ^ (x ^ y) -> x | y */
1690 (for op (plus bit_ior bit_xor)
1691  (simplify
1692   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1693   (bit_ior @0 @1)))
1695 /* (x & y) + (x | y) -> x + y */
1696 (simplify
1697  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1698  (plus @0 @1))
1700 /* (x + y) - (x | y) -> x & y */
1701 (simplify
1702  (minus (plus @0 @1) (bit_ior @0 @1))
1703  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1704       && !TYPE_SATURATING (type))
1705   (bit_and @0 @1)))
1707 /* (x + y) - (x & y) -> x | y */
1708 (simplify
1709  (minus (plus @0 @1) (bit_and @0 @1))
1710  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1711       && !TYPE_SATURATING (type))
1712   (bit_ior @0 @1)))
1714 /* (x | y) - y -> (x & ~y) */
1715 (simplify
1716  (minus (bit_ior:cs @0 @1) @1)
1717  (bit_and @0 (bit_not @1)))
1719 /* (x | y) - (x ^ y) -> x & y */
1720 (simplify
1721  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1722  (bit_and @0 @1))
1724 /* (x | y) - (x & y) -> x ^ y */
1725 (simplify
1726  (minus (bit_ior @0 @1) (bit_and @0 @1))
1727  (bit_xor @0 @1))
1729 /* (x | y) & ~(x & y) -> x ^ y */
1730 (simplify
1731  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1732  (bit_xor @0 @1))
1734 /* (x | y) & (~x ^ y) -> x & y */
1735 (simplify
1736  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1737  (with { bool wascmp; }
1738   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1739        && (!wascmp || element_precision (type) == 1))
1740    (bit_and @0 @1))))
1742 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1743 (simplify
1744  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1745  (bit_not (bit_xor @0 @1)))
1747 /* (~x | y) ^ (x | ~y) -> x ^ y */
1748 (simplify
1749  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1750  (bit_xor @0 @1))
1752 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1753 (simplify
1754  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1755                               (nop_convert2? (bit_ior @0 @1))))
1756        integer_all_onesp)
1757  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1758       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1759       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1760       && !TYPE_SATURATING (TREE_TYPE (@2)))
1761  (bit_not (convert (bit_xor @0 @1)))))
1762 (simplify
1763  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1764                                integer_all_onesp))
1765        (nop_convert3? (bit_ior @0 @1)))
1766  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1767       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1768       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1769       && !TYPE_SATURATING (TREE_TYPE (@2)))
1770  (bit_not (convert (bit_xor @0 @1)))))
1771 (simplify
1772  (minus (nop_convert1? (bit_and @0 @1))
1773        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1774                                integer_onep)))
1775  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1776       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1777       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1778       && !TYPE_SATURATING (TREE_TYPE (@2)))
1779  (bit_not (convert (bit_xor @0 @1)))))
1781 /* ~x & ~y -> ~(x | y)
1782    ~x | ~y -> ~(x & y) */
1783 (for op (bit_and bit_ior)
1784      rop (bit_ior bit_and)
1785  (simplify
1786   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1787   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1788        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1789    (bit_not (rop (convert @0) (convert @1))))))
1791 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1792    with a constant, and the two constants have no bits in common,
1793    we should treat this as a BIT_IOR_EXPR since this may produce more
1794    simplifications.  */
1795 (for op (bit_xor plus)
1796  (simplify
1797   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1798       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1799   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1800        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1801        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1802    (bit_ior (convert @4) (convert @5)))))
1804 /* (X | Y) ^ X -> Y & ~ X*/
1805 (simplify
1806  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1807  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1808   (convert (bit_and @1 (bit_not @0)))))
1810 /* (~X | Y) ^ X -> ~(X & Y).  */
1811 (simplify
1812  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1813  (if (bitwise_equal_p (@0, @2))
1814   (convert (bit_not (bit_and @0 (convert @1))))))
1816 /* Convert ~X ^ ~Y to X ^ Y.  */
1817 (simplify
1818  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1819  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1820       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1821   (bit_xor (convert @0) (convert @1))))
1823 /* Convert ~X ^ C to X ^ ~C.  */
1824 (simplify
1825  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1826  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1827   (bit_xor (convert @0) (bit_not @1))))
1829 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1830 (for opo (bit_and bit_xor)
1831      opi (bit_xor bit_and)
1832  (simplify
1833   (opo:c (opi:cs @0 @1) @1)
1834   (bit_and (bit_not @0) @1)))
1836 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1837    operands are another bit-wise operation with a common input.  If so,
1838    distribute the bit operations to save an operation and possibly two if
1839    constants are involved.  For example, convert
1840      (A | B) & (A | C) into A | (B & C)
1841    Further simplification will occur if B and C are constants.  */
1842 (for op (bit_and bit_ior bit_xor)
1843      rop (bit_ior bit_and bit_and)
1844  (simplify
1845   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1846   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1847        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1848    (rop (convert @0) (op (convert @1) (convert @2))))))
1850 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1851 /* (X & Y) & Y -> X & Y
1852    (X | Y) | Y -> X | Y  */
1853 (for op (bit_and bit_ior)
1854  (simplify
1855   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1856   @2))
1857 /* (X ^ Y) ^ Y -> X  */
1858 (simplify
1859  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1860  (convert @0))
1862 /* (X & ~Y) & Y -> 0 */
1863 (simplify
1864  (bit_and:c (bit_and @0 @1) @2)
1865  (with { bool wascmp; }
1866   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1867        || bitwise_inverted_equal_p (@1, @2, wascmp))
1868    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1869 /* (X | ~Y) | Y -> -1 */
1870 (simplify
1871  (bit_ior:c (bit_ior @0 @1) @2)
1872  (with { bool wascmp; }
1873   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1874         || bitwise_inverted_equal_p (@1, @2, wascmp))
1875        && (!wascmp || element_precision (type) == 1))
1876    { build_all_ones_cst (TREE_TYPE (@0)); })))
1878 /* (X & Y) & (X & Z) -> (X & Y) & Z
1879    (X | Y) | (X | Z) -> (X | Y) | Z  */
1880 (for op (bit_and bit_ior)
1881  (simplify
1882   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1883   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1884        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1885    (if (single_use (@5) && single_use (@6))
1886     (op @3 (convert @2))
1887     (if (single_use (@3) && single_use (@4))
1888      (op (convert @1) @5))))))
1889 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1890 (simplify
1891  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1892  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1893       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1894   (bit_xor (convert @1) (convert @2))))
1896 /* Convert abs (abs (X)) into abs (X).
1897    also absu (absu (X)) into absu (X).  */
1898 (simplify
1899  (abs (abs@1 @0))
1900  @1)
1902 (simplify
1903  (absu (convert@2 (absu@1 @0)))
1904  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1905   @1))
1907 /* Convert abs[u] (-X) -> abs[u] (X).  */
1908 (simplify
1909  (abs (negate @0))
1910  (abs @0))
1912 (simplify
1913  (absu (negate @0))
1914  (absu @0))
1916 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1917 (simplify
1918  (abs tree_expr_nonnegative_p@0)
1919  @0)
1921 (simplify
1922  (absu tree_expr_nonnegative_p@0)
1923  (convert @0))
1925 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1926 (simplify
1927  (mult:c (nop_convert1?
1928           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1929                     integer_onep))
1930          (nop_convert3? @0))
1931  (if (INTEGRAL_TYPE_P (type)
1932       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1933       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1934   (if (TYPE_UNSIGNED (type))
1935    (absu @0)
1936    (abs @0)
1937   )
1941 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1942 (match negate_expr_p
1943  INTEGER_CST
1944  (if ((INTEGRAL_TYPE_P (type)
1945        && TYPE_UNSIGNED (type))
1946       || (!TYPE_OVERFLOW_SANITIZED (type)
1947           && may_negate_without_overflow_p (t)))))
1948 (match negate_expr_p
1949  FIXED_CST)
1950 (match negate_expr_p
1951  (negate @0)
1952  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1953 (match negate_expr_p
1954  REAL_CST
1955  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1956 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1957    ways.  */
1958 (match negate_expr_p
1959  VECTOR_CST
1960  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1961 (match negate_expr_p
1962  (minus @0 @1)
1963  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1964       || (FLOAT_TYPE_P (type)
1965           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1966           && !HONOR_SIGNED_ZEROS (type)))))
1968 /* (-A) * (-B) -> A * B  */
1969 (simplify
1970  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1971   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1972        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1973    (mult (convert @0) (convert (negate @1)))))
1975 /* -(A + B) -> (-B) - A.  */
1976 (simplify
1977  (negate (plus:c @0 negate_expr_p@1))
1978  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1979       && !HONOR_SIGNED_ZEROS (type))
1980   (minus (negate @1) @0)))
1982 /* -(A - B) -> B - A.  */
1983 (simplify
1984  (negate (minus @0 @1))
1985  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1986       || (FLOAT_TYPE_P (type)
1987           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1988           && !HONOR_SIGNED_ZEROS (type)))
1989   (minus @1 @0)))
1990 (simplify
1991  (negate (pointer_diff @0 @1))
1992  (if (TYPE_OVERFLOW_UNDEFINED (type))
1993   (pointer_diff @1 @0)))
1995 /* A - B -> A + (-B) if B is easily negatable.  */
1996 (simplify
1997  (minus @0 negate_expr_p@1)
1998  (if (!FIXED_POINT_TYPE_P (type))
1999  (plus @0 (negate @1))))
2001 /* 1 - a is a ^ 1 if a had a bool range. */
2002 /* This is only enabled for gimple as sometimes
2003    cfun is not set for the function which contains
2004    the SSA_NAME (e.g. while IPA passes are happening,
2005    fold might be called).  */
2006 (simplify
2007  (minus integer_onep@0 SSA_NAME@1)
2008   (if (INTEGRAL_TYPE_P (type)
2009        && ssa_name_has_boolean_range (@1))
2010    (bit_xor @1 @0)))
2012 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2013 (simplify
2014  (negate (mult:c@0 @1 negate_expr_p@2))
2015  (if (! TYPE_UNSIGNED (type)
2016       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2017       && single_use (@0))
2018   (mult @1 (negate @2))))
2020 (simplify
2021  (negate (rdiv@0 @1 negate_expr_p@2))
2022  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2023       && single_use (@0))
2024   (rdiv @1 (negate @2))))
2026 (simplify
2027  (negate (rdiv@0 negate_expr_p@1 @2))
2028  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2029       && single_use (@0))
2030   (rdiv (negate @1) @2)))
2032 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2033 (simplify
2034  (negate (convert? (rshift @0 INTEGER_CST@1)))
2035  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2036       && wi::to_wide (@1) == element_precision (type) - 1)
2037   (with { tree stype = TREE_TYPE (@0);
2038           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2039                                              : unsigned_type_for (stype); }
2040    (if (VECTOR_TYPE_P (type))
2041     (view_convert (rshift (view_convert:ntype @0) @1))
2042     (convert (rshift (convert:ntype @0) @1))))))
2044 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2045    when profitable.
2046    For bitwise binary operations apply operand conversions to the
2047    binary operation result instead of to the operands.  This allows
2048    to combine successive conversions and bitwise binary operations.
2049    We combine the above two cases by using a conditional convert.  */
2050 (for bitop (bit_and bit_ior bit_xor)
2051  (simplify
2052   (bitop (convert@2 @0) (convert?@3 @1))
2053   (if (((TREE_CODE (@1) == INTEGER_CST
2054          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2055          && (int_fits_type_p (@1, TREE_TYPE (@0))
2056              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2057         || types_match (@0, @1))
2058        && !POINTER_TYPE_P (TREE_TYPE (@0))
2059        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2060        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2061        /* ???  This transform conflicts with fold-const.cc doing
2062           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2063           constants (if x has signed type, the sign bit cannot be set
2064           in c).  This folds extension into the BIT_AND_EXPR.
2065           Restrict it to GIMPLE to avoid endless recursions.  */
2066        && (bitop != BIT_AND_EXPR || GIMPLE)
2067        && (/* That's a good idea if the conversion widens the operand, thus
2068               after hoisting the conversion the operation will be narrower.
2069               It is also a good if the conversion is a nop as moves the
2070               conversion to one side; allowing for combining of the conversions.  */
2071            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2072            /* The conversion check for being a nop can only be done at the gimple
2073               level as fold_binary has some re-association code which can conflict
2074               with this if there is a "constant" which is not a full INTEGER_CST.  */
2075            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2076            /* It's also a good idea if the conversion is to a non-integer
2077               mode.  */
2078            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2079            /* Or if the precision of TO is not the same as the precision
2080               of its mode.  */
2081            || !type_has_mode_precision_p (type)
2082            /* In GIMPLE, getting rid of 2 conversions for one new results
2083               in smaller IL.  */
2084            || (GIMPLE
2085                && TREE_CODE (@1) != INTEGER_CST
2086                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2087                && single_use (@2)
2088                && single_use (@3))))
2089    (convert (bitop @0 (convert @1)))))
2090  /* In GIMPLE, getting rid of 2 conversions for one new results
2091     in smaller IL.  */
2092  (simplify
2093   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2094   (if (GIMPLE
2095        && TREE_CODE (@1) != INTEGER_CST
2096        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2097        && types_match (type, @0)
2098        && !POINTER_TYPE_P (TREE_TYPE (@0))
2099        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2100    (bitop @0 (convert @1)))))
2102 (for bitop (bit_and bit_ior)
2103      rbitop (bit_ior bit_and)
2104   /* (x | y) & x -> x */
2105   /* (x & y) | x -> x */
2106  (simplify
2107   (bitop:c (rbitop:c @0 @1) @0)
2108   @0)
2109  /* (~x | y) & x -> x & y */
2110  /* (~x & y) | x -> x | y */
2111  (simplify
2112   (bitop:c (rbitop:c @2 @1) @0)
2113   (with { bool wascmp; }
2114    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2115         && (!wascmp || element_precision (type) == 1))
2116     (bitop @0 @1))))
2117   /* (x | y) & (x & z) -> (x & z) */
2118   /* (x & y) | (x | z) -> (x | z) */
2119  (simplify
2120   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2121   @3)
2122  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2123  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2124  (simplify
2125   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2126   (bitop @0 @3))
2127  /* x & ~(y | x) -> 0 */
2128  /* x | ~(y & x) -> -1 */
2129  (simplify
2130   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2131   (if (bitop == BIT_AND_EXPR)
2132    { build_zero_cst (type); }
2133    { build_minus_one_cst (type); })))
2135 /* ((x | y) & z) | x -> (z & y) | x
2136    ((x ^ y) & z) | x -> (z & y) | x  */
2137 (for op (bit_ior bit_xor)
2138  (simplify
2139   (bit_ior:c (nop_convert1?:s
2140                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2141   (if (bitwise_equal_p (@0, @3))
2142    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2144 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2145 (simplify
2146   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2147   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2149 /* Combine successive equal operations with constants.  */
2150 (for bitop (bit_and bit_ior bit_xor)
2151  (simplify
2152   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2153   (if (!CONSTANT_CLASS_P (@0))
2154    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2155       folded to a constant.  */
2156    (bitop @0 (bitop! @1 @2))
2157    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2158       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2159       the values involved are such that the operation can't be decided at
2160       compile time.  Try folding one of @0 or @1 with @2 to see whether
2161       that combination can be decided at compile time.
2163       Keep the existing form if both folds fail, to avoid endless
2164       oscillation.  */
2165    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2166     (if (cst1)
2167      (bitop @1 { cst1; })
2168      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2169       (if (cst2)
2170        (bitop @0 { cst2; }))))))))
2172 /* Try simple folding for X op !X, and X op X with the help
2173    of the truth_valued_p and logical_inverted_value predicates.  */
2174 (match truth_valued_p
2175  @0
2176  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2177 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2178  (match truth_valued_p
2179   (op @0 @1)))
2180 (match truth_valued_p
2181   (truth_not @0))
2183 (match (logical_inverted_value @0)
2184  (truth_not @0))
2185 (match (logical_inverted_value @0)
2186  (bit_not truth_valued_p@0))
2187 (match (logical_inverted_value @0)
2188  (eq @0 integer_zerop))
2189 (match (logical_inverted_value @0)
2190  (ne truth_valued_p@0 integer_truep))
2191 (match (logical_inverted_value @0)
2192  (bit_xor truth_valued_p@0 integer_truep))
2194 /* X & !X -> 0.  */
2195 (simplify
2196  (bit_and:c @0 (logical_inverted_value @0))
2197  { build_zero_cst (type); })
2198 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2199 (for op (bit_ior bit_xor)
2200  (simplify
2201   (op:c truth_valued_p@0 (logical_inverted_value @0))
2202   { constant_boolean_node (true, type); }))
2203 /* X ==/!= !X is false/true.  */
2204 (for op (eq ne)
2205  (simplify
2206   (op:c truth_valued_p@0 (logical_inverted_value @0))
2207   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2209 /* ~~x -> x */
2210 (simplify
2211   (bit_not (bit_not @0))
2212   @0)
2214 /* zero_one_valued_p will match when a value is known to be either
2215    0 or 1 including constants 0 or 1.
2216    Signed 1-bits includes -1 so they cannot match here. */
2217 (match zero_one_valued_p
2218  @0
2219  (if (INTEGRAL_TYPE_P (type)
2220       && (TYPE_UNSIGNED (type)
2221           || TYPE_PRECISION (type) > 1)
2222       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2223 (match zero_one_valued_p
2224  truth_valued_p@0
2225  (if (INTEGRAL_TYPE_P (type)
2226       && (TYPE_UNSIGNED (type)
2227           || TYPE_PRECISION (type) > 1))))
2229 /* (a&1) is always [0,1] too. This is useful again when
2230    the range is not known. */
2231 /* Note this can't be recursive due to VN handling of equivalents,
2232    VN and would cause an infinite recursion. */
2233 (match zero_one_valued_p
2234  (bit_and:c@0 @1 integer_onep)
2235  (if (INTEGRAL_TYPE_P (type))))
2237 /* A conversion from an zero_one_valued_p is still a [0,1].
2238    This is useful when the range of a variable is not known */
2239 /* Note this matches can't be recursive because of the way VN handles
2240    nop conversions being equivalent and then recursive between them. */
2241 (match zero_one_valued_p
2242  (convert@0 @1)
2243  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2244       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2245           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2246       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2248 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2249 (simplify
2250  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2251  (if (INTEGRAL_TYPE_P (type))
2252   (bit_and @0 @1)))
2254 (for cmp (tcc_comparison)
2255      icmp (inverted_tcc_comparison)
2256  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2257  (simplify
2258   (bit_ior
2259    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2260    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2261     (if (INTEGRAL_TYPE_P (type)
2262          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2263          /* The scalar version has to be canonicalized after vectorization
2264             because it makes unconditional loads conditional ones, which
2265             means we lose vectorization because the loads may trap.  */
2266          && canonicalize_math_after_vectorization_p ())
2267      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2269  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2270     canonicalized further and we recognize the conditional form:
2271     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2272  (simplify
2273   (bit_ior
2274    (cond (cmp@0  @01 @02) @3 zerop)
2275    (cond (icmp@4 @01 @02) @5 zerop))
2276     (if (INTEGRAL_TYPE_P (type)
2277          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2278          /* The scalar version has to be canonicalized after vectorization
2279             because it makes unconditional loads conditional ones, which
2280             means we lose vectorization because the loads may trap.  */
2281          && canonicalize_math_after_vectorization_p ())
2282     (cond @0 @3 @5)))
2284  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2285     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2286  (simplify
2287   (bit_ior
2288    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2289    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2290     (if (integer_zerop (@5)
2291          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2292      (switch
2293       (if (integer_onep (@4))
2294        (bit_and (vec_cond @0 @2 @3) @4))
2295         (if (integer_minus_onep (@4))
2296          (vec_cond @0 @2 @3)))
2297     (if (integer_zerop (@4)
2298          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2299      (switch
2300       (if (integer_onep (@5))
2301        (bit_and (vec_cond @0 @3 @2) @5))
2302       (if (integer_minus_onep (@5))
2303        (vec_cond @0 @3 @2))))))
2305  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2306     into a < b ? d : c.  */
2307  (simplify
2308   (bit_ior
2309    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2310    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2311   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2312    (vec_cond @0 @2 @3))))
2314 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2315 (simplify
2316  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2317  (if (INTEGRAL_TYPE_P (type)
2318       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2319       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2320       /* Sign extending of the neg or a truncation of the neg
2321          is needed. */
2322       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2323           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2324   (mult (convert @0) @1)))
2326 /* Narrow integer multiplication by a zero_one_valued_p operand.
2327    Multiplication by [0,1] is guaranteed not to overflow.  */
2328 (simplify
2329  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2330  (if (INTEGRAL_TYPE_P (type)
2331       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2333   (mult (convert @1) (convert @2))))
2335 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2336    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2337    as some targets (such as x86's SSE) may return zero for larger C.  */
2338 (simplify
2339   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2340   (if (tree_fits_shwi_p (@1)
2341        && tree_to_shwi (@1) > 0
2342        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2343     (convert @0)))
2345 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2346    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2347    as some targets (such as x86's SSE) may return zero for larger C.  */
2348 (simplify
2349   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2350   (if (tree_fits_shwi_p (@1)
2351        && tree_to_shwi (@1) > 0
2352        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2353     (eq @0 @2)))
2355 /* Convert ~ (-A) to A - 1.  */
2356 (simplify
2357  (bit_not (convert? (negate @0)))
2358  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2359       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2360   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2362 /* Convert - (~A) to A + 1.  */
2363 (simplify
2364  (negate (nop_convert? (bit_not @0)))
2365  (plus (view_convert @0) { build_each_one_cst (type); }))
2367 /* (a & b) ^ (a == b) -> !(a | b) */
2368 /* (a & b) == (a ^ b) -> !(a | b) */
2369 (for first_op (bit_xor eq)
2370      second_op (eq bit_xor)
2371  (simplify
2372   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2373     (bit_not (bit_ior @0 @1))))
2375 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2376 (simplify
2377  (bit_not (convert? (minus @0 integer_each_onep)))
2378  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2379       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2380   (convert (negate @0))))
2381 (simplify
2382  (bit_not (convert? (plus @0 integer_all_onesp)))
2383  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2384       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2385   (convert (negate @0))))
2387 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2388 (simplify
2389  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2390  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2391   (convert (bit_xor @0 (bit_not @1)))))
2392 (simplify
2393  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2394  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2395   (convert (bit_xor @0 @1))))
2397 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2398 (simplify
2399  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2400  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2401   (bit_not (bit_xor (view_convert @0) @1))))
2403 /* ~(a ^ b) is a == b for truth valued a and b.  */
2404 (simplify
2405  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2406  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2408   (convert (eq @0 @1))))
2410 /* (~a) == b is a ^ b for truth valued a and b.  */
2411 (simplify
2412  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2413  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2414       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2415   (convert (bit_xor @0 @1))))
2417 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2418 (simplify
2419  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2420  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2422 /* Fold A - (A & B) into ~B & A.  */
2423 (simplify
2424  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2425  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2426       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2427   (convert (bit_and (bit_not @1) @0))))
2429 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2430 (if (!canonicalize_math_p ())
2431  (for cmp (tcc_comparison)
2432   (simplify
2433    (mult:c (convert (cmp@0 @1 @2)) @3)
2434    (if (INTEGRAL_TYPE_P (type)
2435         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2436      (cond @0 @3 { build_zero_cst (type); })))
2437 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2438   (simplify
2439    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2440    (if (INTEGRAL_TYPE_P (type)
2441         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2442      (cond @0 @3 { build_zero_cst (type); })))
2446 /* For integral types with undefined overflow and C != 0 fold
2447    x * C EQ/NE y * C into x EQ/NE y.  */
2448 (for cmp (eq ne)
2449  (simplify
2450   (cmp (mult:c @0 @1) (mult:c @2 @1))
2451   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2452        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2453        && tree_expr_nonzero_p (@1))
2454    (cmp @0 @2))))
2456 /* For integral types with wrapping overflow and C odd fold
2457    x * C EQ/NE y * C into x EQ/NE y.  */
2458 (for cmp (eq ne)
2459  (simplify
2460   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2461   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2462        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2463        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2464    (cmp @0 @2))))
2466 /* For integral types with undefined overflow and C != 0 fold
2467    x * C RELOP y * C into:
2469    x RELOP y for nonnegative C
2470    y RELOP x for negative C  */
2471 (for cmp (lt gt le ge)
2472  (simplify
2473   (cmp (mult:c @0 @1) (mult:c @2 @1))
2474   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2475        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2476    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2477     (cmp @0 @2)
2478    (if (TREE_CODE (@1) == INTEGER_CST
2479         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2480     (cmp @2 @0))))))
2482 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2483 (for cmp (le gt)
2484      icmp (gt le)
2485  (simplify
2486   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2487    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2488         && TYPE_UNSIGNED (TREE_TYPE (@0))
2489         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2490         && (wi::to_wide (@2)
2491             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2492     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2493      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2495 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2496 (for cmp (simple_comparison)
2497  (simplify
2498   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2499   (if (element_precision (@3) >= element_precision (@0)
2500        && types_match (@0, @1))
2501    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2502     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2503      (cmp @1 @0)
2504      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2505       (with
2506        {
2507         tree utype = unsigned_type_for (TREE_TYPE (@0));
2508        }
2509        (cmp (convert:utype @1) (convert:utype @0)))))
2510     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2511      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2512       (cmp @0 @1)
2513       (with
2514        {
2515         tree utype = unsigned_type_for (TREE_TYPE (@0));
2516        }
2517        (cmp (convert:utype @0) (convert:utype @1)))))))))
2519 /* X / C1 op C2 into a simple range test.  */
2520 (for cmp (simple_comparison)
2521  (simplify
2522   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2523   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524        && integer_nonzerop (@1)
2525        && !TREE_OVERFLOW (@1)
2526        && !TREE_OVERFLOW (@2))
2527    (with { tree lo, hi; bool neg_overflow;
2528            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2529                                                    &neg_overflow); }
2530     (switch
2531      (if (code == LT_EXPR || code == GE_EXPR)
2532        (if (TREE_OVERFLOW (lo))
2533         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2534         (if (code == LT_EXPR)
2535          (lt @0 { lo; })
2536          (ge @0 { lo; }))))
2537      (if (code == LE_EXPR || code == GT_EXPR)
2538        (if (TREE_OVERFLOW (hi))
2539         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2540         (if (code == LE_EXPR)
2541          (le @0 { hi; })
2542          (gt @0 { hi; }))))
2543      (if (!lo && !hi)
2544       { build_int_cst (type, code == NE_EXPR); })
2545      (if (code == EQ_EXPR && !hi)
2546       (ge @0 { lo; }))
2547      (if (code == EQ_EXPR && !lo)
2548       (le @0 { hi; }))
2549      (if (code == NE_EXPR && !hi)
2550       (lt @0 { lo; }))
2551      (if (code == NE_EXPR && !lo)
2552       (gt @0 { hi; }))
2553      (if (GENERIC)
2554       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2555                            lo, hi); })
2556      (with
2557       {
2558         tree etype = range_check_type (TREE_TYPE (@0));
2559         if (etype)
2560           {
2561             hi = fold_convert (etype, hi);
2562             lo = fold_convert (etype, lo);
2563             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2564           }
2565       }
2566       (if (etype && hi && !TREE_OVERFLOW (hi))
2567        (if (code == EQ_EXPR)
2568         (le (minus (convert:etype @0) { lo; }) { hi; })
2569         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2571 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2572 (for op (lt le ge gt)
2573  (simplify
2574   (op (plus:c @0 @2) (plus:c @1 @2))
2575   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2576        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2577    (op @0 @1))))
2579 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2580    when C is an unsigned integer constant with only the MSB set, and X and
2581    Y have types of equal or lower integer conversion rank than C's.  */
2582 (for op (lt le ge gt)
2583  (simplify
2584   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2585   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2586        && TYPE_UNSIGNED (TREE_TYPE (@0))
2587        && wi::only_sign_bit_p (wi::to_wide (@0)))
2588    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2589     (op (convert:stype @1) (convert:stype @2))))))
2591 /* For equality and subtraction, this is also true with wrapping overflow.  */
2592 (for op (eq ne minus)
2593  (simplify
2594   (op (plus:c @0 @2) (plus:c @1 @2))
2595   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2596        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2597            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2598    (op @0 @1))))
2599 /* And similar for pointers.  */
2600 (for op (eq ne)
2601  (simplify
2602   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2603   (op @1 @2)))
2604 (simplify
2605  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2606  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2607   (convert (minus @1 @2))))
2609 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2610 (for op (lt le ge gt)
2611  (simplify
2612   (op (minus @0 @2) (minus @1 @2))
2613   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2614        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2615    (op @0 @1))))
2616 /* For equality and subtraction, this is also true with wrapping overflow.  */
2617 (for op (eq ne minus)
2618  (simplify
2619   (op (minus @0 @2) (minus @1 @2))
2620   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2621        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2622            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2623    (op @0 @1))))
2624 /* And for pointers...  */
2625 (for op (simple_comparison)
2626  (simplify
2627   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2628   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2629    (op @0 @1))))
2630 (simplify
2631  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2632  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2633       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2634   (pointer_diff @0 @1)))
2636 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2637 (for op (lt le ge gt)
2638  (simplify
2639   (op (minus @2 @0) (minus @2 @1))
2640   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2641        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2642    (op @1 @0))))
2643 /* For equality and subtraction, this is also true with wrapping overflow.  */
2644 (for op (eq ne minus)
2645  (simplify
2646   (op (minus @2 @0) (minus @2 @1))
2647   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2648        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2649            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2650    (op @1 @0))))
2651 /* And for pointers...  */
2652 (for op (simple_comparison)
2653  (simplify
2654   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2655   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2656    (op @1 @0))))
2657 (simplify
2658  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2659  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2660       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2661   (pointer_diff @1 @0)))
2663 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2664 (for op (lt le gt ge)
2665  (simplify
2666   (op:c (plus:c@2 @0 @1) @1)
2667   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2668        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2669        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2670        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2671    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2672 /* For equality, this is also true with wrapping overflow.  */
2673 (for op (eq ne)
2674  (simplify
2675   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2676   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2677        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2678            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2679        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2680        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2681        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2682    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2683  (simplify
2684   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2685   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2686        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2687        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2688    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2690 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2691 (for neeq (ne eq)
2692  (simplify
2693   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2694    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2695     (if (ptr_difference_const (@0, @2, &diff))
2696      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2697  (simplify
2698   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2699    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2700     (if (ptr_difference_const (@0, @2, &diff))
2701      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2703 /* X - Y < X is the same as Y > 0 when there is no overflow.
2704    For equality, this is also true with wrapping overflow.  */
2705 (for op (simple_comparison)
2706  (simplify
2707   (op:c @0 (minus@2 @0 @1))
2708   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2709        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2710            || ((op == EQ_EXPR || op == NE_EXPR)
2711                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2712        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2713    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2715 /* Transform:
2716    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2717    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2718 (for cmp (eq ne)
2719      ocmp (lt ge)
2720  (simplify
2721   (cmp (trunc_div @0 @1) integer_zerop)
2722   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2723        /* Complex ==/!= is allowed, but not </>=.  */
2724        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2725        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2726    (ocmp @0 @1))))
2728 /* X == C - X can never be true if C is odd.  */
2729 (for cmp (eq ne)
2730  (simplify
2731   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2732   (if (TREE_INT_CST_LOW (@1) & 1)
2733    { constant_boolean_node (cmp == NE_EXPR, type); })))
2736    U & N <= U  -> true
2737    U & N >  U  -> false
2738    U needs to be non-negative.
2740    U | N <  U  -> false
2741    U | N >= U  -> true
2742    U and N needs to be non-negative
2744    U | N <  U  -> true
2745    U | N >= U  -> false
2746    U needs to be non-negative and N needs to be a negative constant.
2747    */
2748 (for cmp   (lt      ge      le      gt     )
2749      bitop (bit_ior bit_ior bit_and bit_and)
2750  (simplify
2751   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2752   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2753    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2754     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2755     /* The sign is opposite now so the comparison is swapped around. */
2756     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2757      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2759 /* Arguments on which one can call get_nonzero_bits to get the bits
2760    possibly set.  */
2761 (match with_possible_nonzero_bits
2762  INTEGER_CST@0)
2763 (match with_possible_nonzero_bits
2764  SSA_NAME@0
2765  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2766 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2767 (match (with_possible_nonzero_bits2 @0)
2768  with_possible_nonzero_bits@0)
2769 (match (with_possible_nonzero_bits2 @0)
2770  (bit_and:c with_possible_nonzero_bits@0 @2))
2772 /* Same for bits that are known to be set, but we do not have
2773    an equivalent to get_nonzero_bits yet.  */
2774 (match (with_certain_nonzero_bits2 @0)
2775  INTEGER_CST@0)
2776 (match (with_certain_nonzero_bits2 @0)
2777  (bit_ior @1 INTEGER_CST@0))
2779 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2780 (for cmp (eq ne)
2781  (simplify
2782   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2783   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2784    { constant_boolean_node (cmp == NE_EXPR, type); })))
2786 /* ((X inner_op C0) outer_op C1)
2787    With X being a tree where value_range has reasoned certain bits to always be
2788    zero throughout its computed value range,
2789    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2790    where zero_mask has 1's for all bits that are sure to be 0 in
2791    and 0's otherwise.
2792    if (inner_op == '^') C0 &= ~C1;
2793    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2794    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2796 (for inner_op (bit_ior bit_xor)
2797      outer_op (bit_xor bit_ior)
2798 (simplify
2799  (outer_op
2800   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2801  (with
2802   {
2803     bool fail = false;
2804     wide_int zero_mask_not;
2805     wide_int C0;
2806     wide_int cst_emit;
2808     if (TREE_CODE (@2) == SSA_NAME)
2809       zero_mask_not = get_nonzero_bits (@2);
2810     else
2811       fail = true;
2813     if (inner_op == BIT_XOR_EXPR)
2814       {
2815         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2816         cst_emit = C0 | wi::to_wide (@1);
2817       }
2818     else
2819       {
2820         C0 = wi::to_wide (@0);
2821         cst_emit = C0 ^ wi::to_wide (@1);
2822       }
2823   }
2824   (if (!fail && (C0 & zero_mask_not) == 0)
2825    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2826    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2827     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2829 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2830 (simplify
2831   (pointer_plus (pointer_plus:s @0 @1) @3)
2832   (pointer_plus @0 (plus @1 @3)))
2833 #if GENERIC
2834 (simplify
2835   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2836   (convert:type (pointer_plus @0 (plus @1 @3))))
2837 #endif
2839 /* Pattern match
2840      tem1 = (long) ptr1;
2841      tem2 = (long) ptr2;
2842      tem3 = tem2 - tem1;
2843      tem4 = (unsigned long) tem3;
2844      tem5 = ptr1 + tem4;
2845    and produce
2846      tem5 = ptr2;  */
2847 (simplify
2848   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2849   /* Conditionally look through a sign-changing conversion.  */
2850   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2851        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2852             || (GENERIC && type == TREE_TYPE (@1))))
2853    @1))
2854 (simplify
2855   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2856   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2857    (convert @1)))
2859 /* Pattern match
2860      tem = (sizetype) ptr;
2861      tem = tem & algn;
2862      tem = -tem;
2863      ... = ptr p+ tem;
2864    and produce the simpler and easier to analyze with respect to alignment
2865      ... = ptr & ~algn;  */
2866 (simplify
2867   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2868   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2869    (bit_and @0 { algn; })))
2871 /* Try folding difference of addresses.  */
2872 (simplify
2873  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2874  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2875   (with { poly_int64 diff; }
2876    (if (ptr_difference_const (@0, @1, &diff))
2877     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2878 (simplify
2879  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2880  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2881   (with { poly_int64 diff; }
2882    (if (ptr_difference_const (@0, @1, &diff))
2883     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2884 (simplify
2885  (minus (convert ADDR_EXPR@0) (convert @1))
2886  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2887   (with { poly_int64 diff; }
2888    (if (ptr_difference_const (@0, @1, &diff))
2889     { build_int_cst_type (type, diff); }))))
2890 (simplify
2891  (minus (convert @0) (convert ADDR_EXPR@1))
2892  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2893   (with { poly_int64 diff; }
2894    (if (ptr_difference_const (@0, @1, &diff))
2895     { build_int_cst_type (type, diff); }))))
2896 (simplify
2897  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2898  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2899       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2900   (with { poly_int64 diff; }
2901    (if (ptr_difference_const (@0, @1, &diff))
2902     { build_int_cst_type (type, diff); }))))
2903 (simplify
2904  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2905  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2906       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2907   (with { poly_int64 diff; }
2908    (if (ptr_difference_const (@0, @1, &diff))
2909     { build_int_cst_type (type, diff); }))))
2911 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2912 (simplify
2913  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2914  (with { poly_int64 diff; }
2915    (if (ptr_difference_const (@0, @2, &diff))
2916     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2917 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2918 (simplify
2919  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2920  (with { poly_int64 diff; }
2921    (if (ptr_difference_const (@0, @2, &diff))
2922     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2923 (simplify
2924  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2925  (with { poly_int64 diff; }
2926    (if (ptr_difference_const (@0, @1, &diff))
2927     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2929 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2930 (simplify
2931  (convert (pointer_diff @0 INTEGER_CST@1))
2932  (if (POINTER_TYPE_P (type))
2933   { build_fold_addr_expr_with_type
2934       (build2 (MEM_REF, char_type_node, @0,
2935                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2936                type); }))
2938 /* If arg0 is derived from the address of an object or function, we may
2939    be able to fold this expression using the object or function's
2940    alignment.  */
2941 (simplify
2942  (bit_and (convert? @0) INTEGER_CST@1)
2943  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2944       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2945   (with
2946    {
2947      unsigned int align;
2948      unsigned HOST_WIDE_INT bitpos;
2949      get_pointer_alignment_1 (@0, &align, &bitpos);
2950    }
2951    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2952     { wide_int_to_tree (type, (wi::to_wide (@1)
2953                                & (bitpos / BITS_PER_UNIT))); }))))
2955 (match min_value
2956  uniform_integer_cst_p
2957  (with {
2958    tree int_cst = uniform_integer_cst_p (t);
2959    tree inner_type = TREE_TYPE (int_cst);
2960   }
2961   (if ((INTEGRAL_TYPE_P (inner_type)
2962         || POINTER_TYPE_P (inner_type))
2963        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2965 (match max_value
2966  uniform_integer_cst_p
2967  (with {
2968    tree int_cst = uniform_integer_cst_p (t);
2969    tree itype = TREE_TYPE (int_cst);
2970   }
2971  (if ((INTEGRAL_TYPE_P (itype)
2972        || POINTER_TYPE_P (itype))
2973       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2975 /* x >  y  &&  x != XXX_MIN  -->  x > y
2976    x >  y  &&  x == XXX_MIN  -->  false . */
2977 (for eqne (eq ne)
2978  (simplify
2979   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2980    (switch
2981     (if (eqne == EQ_EXPR)
2982      { constant_boolean_node (false, type); })
2983     (if (eqne == NE_EXPR)
2984      @2)
2985     )))
2987 /* x <  y  &&  x != XXX_MAX  -->  x < y
2988    x <  y  &&  x == XXX_MAX  -->  false.  */
2989 (for eqne (eq ne)
2990  (simplify
2991   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2992    (switch
2993     (if (eqne == EQ_EXPR)
2994      { constant_boolean_node (false, type); })
2995     (if (eqne == NE_EXPR)
2996      @2)
2997     )))
2999 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3000 (simplify
3001  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3002   @2)
3004 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3005 (simplify
3006  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3007   @2)
3009 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3010 (simplify
3011  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3012   @2)
3014 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3015 (simplify
3016  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3017   { constant_boolean_node (true, type); })
3019 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3020 (simplify
3021  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3022   @2)
3024 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3025 (simplify
3026  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3027   @2)
3029 /* x >=  y  ||  x != XXX_MAX   -->  true
3030    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3031 (for eqne (eq ne)
3032  (simplify
3033   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3034    (switch
3035     (if (eqne == EQ_EXPR)
3036      @2)
3037     (if (eqne == NE_EXPR)
3038      { constant_boolean_node (true, type); }))))
3040 /* y == XXX_MIN || x < y --> x <= y - 1 */
3041 (simplify
3042  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3043   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3044        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3045   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3047 /* y != XXX_MIN && x >= y --> x > y - 1 */
3048 (simplify
3049  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3050   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3051        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3052   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3054 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3055    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3056 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3057    Similarly for (X != Y).  */
3059 (for code1 (eq ne)
3060  (for code2 (eq ne lt gt le ge)
3061   (simplify
3062    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3063    (if ((TREE_CODE (@1) == INTEGER_CST
3064          && TREE_CODE (@2) == INTEGER_CST)
3065         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3066              || POINTER_TYPE_P (TREE_TYPE (@1)))
3067             && bitwise_equal_p (@1, @2)))
3068     (with
3069      {
3070       bool one_before = false;
3071       bool one_after = false;
3072       int cmp = 0;
3073       bool allbits = true;
3074       if (TREE_CODE (@1) == INTEGER_CST
3075           && TREE_CODE (@2) == INTEGER_CST)
3076         {
3077           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3078           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3079           auto t2 = wi::to_wide (@2);
3080           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3081           if (cmp < 0
3082               && t1 == t2 - 1)
3083             one_before = true;
3084           if (cmp > 0
3085               && t1 == t2 + 1)
3086             one_after = true;
3087         }
3088       bool val;
3089       switch (code2)
3090          {
3091         case EQ_EXPR: val = (cmp == 0); break;
3092         case NE_EXPR: val = (cmp != 0); break;
3093         case LT_EXPR: val = (cmp < 0); break;
3094         case GT_EXPR: val = (cmp > 0); break;
3095         case LE_EXPR: val = (cmp <= 0); break;
3096         case GE_EXPR: val = (cmp >= 0); break;
3097         default: gcc_unreachable ();
3098         }
3099      }
3100      (switch
3101       (if (code1 == EQ_EXPR && val) @3)
3102       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3103       (if (code1 == NE_EXPR && !val && allbits) @4)
3104       (if (code1 == NE_EXPR
3105            && code2 == GE_EXPR
3106            && cmp == 0
3107            && allbits)
3108        (gt @c0 (convert @1)))
3109       (if (code1 == NE_EXPR
3110            && code2 == LE_EXPR
3111            && cmp == 0
3112            && allbits)
3113        (lt @c0 (convert @1)))
3114       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3115       (if (code1 == NE_EXPR
3116            && code2 == GT_EXPR
3117            && one_after
3118            && allbits)
3119        (gt @c0 (convert @1)))
3120       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3121       (if (code1 == NE_EXPR
3122            && code2 == LT_EXPR
3123            && one_before
3124            && allbits)
3125        (lt @c0 (convert @1)))
3126      )
3127     )
3128    )
3129   )
3133 /* Convert (X OP1 CST1) && (X OP2 CST2).
3134    Convert (X OP1 Y) && (X OP2 Y).  */
3136 (for code1 (lt le gt ge)
3137  (for code2 (lt le gt ge)
3138   (simplify
3139   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3140   (if ((TREE_CODE (@1) == INTEGER_CST
3141         && TREE_CODE (@2) == INTEGER_CST)
3142        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3143             || POINTER_TYPE_P (TREE_TYPE (@1)))
3144            && operand_equal_p (@1, @2)))
3145    (with
3146     {
3147      int cmp = 0;
3148      if (TREE_CODE (@1) == INTEGER_CST
3149          && TREE_CODE (@2) == INTEGER_CST)
3150        cmp = tree_int_cst_compare (@1, @2);
3151     }
3152     (switch
3153      /* Choose the more restrictive of two < or <= comparisons.  */
3154      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3155           && (code2 == LT_EXPR || code2 == LE_EXPR))
3156       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3157        @3
3158        @4))
3159      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3160      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3161           && (code2 == GT_EXPR || code2 == GE_EXPR))
3162       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3163        @3
3164        @4))
3165      /* Check for singleton ranges.  */
3166      (if (cmp == 0
3167           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3168             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3169       (eq @0 @1))
3170      /* Check for disjoint ranges.  */
3171      (if (cmp <= 0
3172           && (code1 == LT_EXPR || code1 == LE_EXPR)
3173           && (code2 == GT_EXPR || code2 == GE_EXPR))
3174       { constant_boolean_node (false, type); })
3175      (if (cmp >= 0
3176           && (code1 == GT_EXPR || code1 == GE_EXPR)
3177           && (code2 == LT_EXPR || code2 == LE_EXPR))
3178       { constant_boolean_node (false, type); })
3179      ))))))
3181 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3182    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3183 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3184    Similarly for (X != Y).  */
3186 (for code1 (eq ne)
3187  (for code2 (eq ne lt gt le ge)
3188   (simplify
3189    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3190    (if ((TREE_CODE (@1) == INTEGER_CST
3191          && TREE_CODE (@2) == INTEGER_CST)
3192         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3193             || POINTER_TYPE_P (TREE_TYPE (@1)))
3194             && bitwise_equal_p (@1, @2)))
3195     (with
3196      {
3197       bool one_before = false;
3198       bool one_after = false;
3199       int cmp = 0;
3200       bool allbits = true;
3201       if (TREE_CODE (@1) == INTEGER_CST
3202           && TREE_CODE (@2) == INTEGER_CST)
3203         {
3204           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3205           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3206           auto t2 = wi::to_wide (@2);
3207           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3208           if (cmp < 0
3209               && t1 == t2 - 1)
3210             one_before = true;
3211           if (cmp > 0
3212               && t1 == t2 + 1)
3213             one_after = true;
3214         }
3215       bool val;
3216       switch (code2)
3217         {
3218         case EQ_EXPR: val = (cmp == 0); break;
3219         case NE_EXPR: val = (cmp != 0); break;
3220         case LT_EXPR: val = (cmp < 0); break;
3221         case GT_EXPR: val = (cmp > 0); break;
3222         case LE_EXPR: val = (cmp <= 0); break;
3223         case GE_EXPR: val = (cmp >= 0); break;
3224         default: gcc_unreachable ();
3225         }
3226      }
3227      (switch
3228       (if (code1 == EQ_EXPR && val) @4)
3229       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3230       (if (code1 == NE_EXPR && !val && allbits) @3)
3231       (if (code1 == EQ_EXPR
3232            && code2 == GT_EXPR
3233            && cmp == 0
3234            && allbits)
3235        (ge @c0 @2))
3236       (if (code1 == EQ_EXPR
3237            && code2 == LT_EXPR
3238            && cmp == 0
3239            && allbits)
3240        (le @c0 @2))
3241       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3242       (if (code1 == EQ_EXPR
3243            && code2 == GE_EXPR
3244            && one_before
3245            && allbits)
3246        (ge @c0 (convert @1)))
3247       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3248       (if (code1 == EQ_EXPR
3249            && code2 == LE_EXPR
3250            && one_after
3251            && allbits)
3252        (le @c0 (convert @1)))
3253      )
3254     )
3255    )
3256   )
3260 /* Convert (X OP1 CST1) || (X OP2 CST2).
3261    Convert (X OP1 Y)    || (X OP2 Y).  */
3263 (for code1 (lt le gt ge)
3264  (for code2 (lt le gt ge)
3265   (simplify
3266   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3267   (if ((TREE_CODE (@1) == INTEGER_CST
3268         && TREE_CODE (@2) == INTEGER_CST)
3269        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3270             || POINTER_TYPE_P (TREE_TYPE (@1)))
3271            && operand_equal_p (@1, @2)))
3272    (with
3273     {
3274      int cmp = 0;
3275      if (TREE_CODE (@1) == INTEGER_CST
3276          && TREE_CODE (@2) == INTEGER_CST)
3277        cmp = tree_int_cst_compare (@1, @2);
3278     }
3279     (switch
3280      /* Choose the more restrictive of two < or <= comparisons.  */
3281      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3282           && (code2 == LT_EXPR || code2 == LE_EXPR))
3283       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3284        @4
3285        @3))
3286      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3287      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3288           && (code2 == GT_EXPR || code2 == GE_EXPR))
3289       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3290        @4
3291        @3))
3292      /* Check for singleton ranges.  */
3293      (if (cmp == 0
3294           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3295               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3296       (ne @0 @2))
3297      /* Check for disjoint ranges.  */
3298      (if (cmp >= 0
3299           && (code1 == LT_EXPR || code1 == LE_EXPR)
3300           && (code2 == GT_EXPR || code2 == GE_EXPR))
3301       { constant_boolean_node (true, type); })
3302      (if (cmp <= 0
3303           && (code1 == GT_EXPR || code1 == GE_EXPR)
3304           && (code2 == LT_EXPR || code2 == LE_EXPR))
3305       { constant_boolean_node (true, type); })
3306      ))))))
3308 /* Optimize (a CMP b) ^ (a CMP b)  */
3309 /* Optimize (a CMP b) != (a CMP b)  */
3310 (for op (bit_xor ne)
3311  (for cmp1 (lt lt lt le le le)
3312       cmp2 (gt eq ne ge eq ne)
3313       rcmp (ne le gt ne lt ge)
3314   (simplify
3315    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3316    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3317     (rcmp @0 @1)))))
3319 /* Optimize (a CMP b) == (a CMP b)  */
3320 (for cmp1 (lt lt lt le le le)
3321      cmp2 (gt eq ne ge eq ne)
3322      rcmp (eq gt le eq ge lt)
3323  (simplify
3324   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3325   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3326     (rcmp @0 @1))))
3328 /* We can't reassociate at all for saturating types.  */
3329 (if (!TYPE_SATURATING (type))
3331  /* Contract negates.  */
3332  /* A + (-B) -> A - B */
3333  (simplify
3334   (plus:c @0 (convert? (negate @1)))
3335   /* Apply STRIP_NOPS on the negate.  */
3336   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3337        && !TYPE_OVERFLOW_SANITIZED (type))
3338    (with
3339     {
3340      tree t1 = type;
3341      if (INTEGRAL_TYPE_P (type)
3342          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3343        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3344     }
3345     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3346  /* A - (-B) -> A + B */
3347  (simplify
3348   (minus @0 (convert? (negate @1)))
3349   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3350        && !TYPE_OVERFLOW_SANITIZED (type))
3351    (with
3352     {
3353      tree t1 = type;
3354      if (INTEGRAL_TYPE_P (type)
3355          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3356        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3357     }
3358     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3359  /* -(T)(-A) -> (T)A
3360     Sign-extension is ok except for INT_MIN, which thankfully cannot
3361     happen without overflow.  */
3362  (simplify
3363   (negate (convert (negate @1)))
3364   (if (INTEGRAL_TYPE_P (type)
3365        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3366            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3367                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3368        && !TYPE_OVERFLOW_SANITIZED (type)
3369        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3370    (convert @1)))
3371  (simplify
3372   (negate (convert negate_expr_p@1))
3373   (if (SCALAR_FLOAT_TYPE_P (type)
3374        && ((DECIMAL_FLOAT_TYPE_P (type)
3375             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3376             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3377            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3378    (convert (negate @1))))
3379  (simplify
3380   (negate (nop_convert? (negate @1)))
3381   (if (!TYPE_OVERFLOW_SANITIZED (type)
3382        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3383    (view_convert @1)))
3385  /* We can't reassociate floating-point unless -fassociative-math
3386     or fixed-point plus or minus because of saturation to +-Inf.  */
3387  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3388       && !FIXED_POINT_TYPE_P (type))
3390   /* Match patterns that allow contracting a plus-minus pair
3391      irrespective of overflow issues.  */
3392   /* (A +- B) - A       ->  +- B */
3393   /* (A +- B) -+ B      ->  A */
3394   /* A - (A +- B)       -> -+ B */
3395   /* A +- (B -+ A)      ->  +- B */
3396   (simplify
3397    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3398    (view_convert @1))
3399   (simplify
3400    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3401    (if (!ANY_INTEGRAL_TYPE_P (type)
3402         || TYPE_OVERFLOW_WRAPS (type))
3403    (negate (view_convert @1))
3404    (view_convert (negate @1))))
3405   (simplify
3406    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3407    (view_convert @0))
3408   (simplify
3409    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3410     (if (!ANY_INTEGRAL_TYPE_P (type)
3411          || TYPE_OVERFLOW_WRAPS (type))
3412      (negate (view_convert @1))
3413      (view_convert (negate @1))))
3414   (simplify
3415    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3416    (view_convert @1))
3417   /* (A +- B) + (C - A)   -> C +- B */
3418   /* (A +  B) - (A - C)   -> B + C */
3419   /* More cases are handled with comparisons.  */
3420   (simplify
3421    (plus:c (plus:c @0 @1) (minus @2 @0))
3422    (plus @2 @1))
3423   (simplify
3424    (plus:c (minus @0 @1) (minus @2 @0))
3425    (minus @2 @1))
3426   (simplify
3427    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3428    (if (TYPE_OVERFLOW_UNDEFINED (type)
3429         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3430     (pointer_diff @2 @1)))
3431   (simplify
3432    (minus (plus:c @0 @1) (minus @0 @2))
3433    (plus @1 @2))
3435   /* (A +- CST1) +- CST2 -> A + CST3
3436      Use view_convert because it is safe for vectors and equivalent for
3437      scalars.  */
3438   (for outer_op (plus minus)
3439    (for inner_op (plus minus)
3440         neg_inner_op (minus plus)
3441     (simplify
3442      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3443                CONSTANT_CLASS_P@2)
3444      /* If one of the types wraps, use that one.  */
3445      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3446       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3447          forever if something doesn't simplify into a constant.  */
3448       (if (!CONSTANT_CLASS_P (@0))
3449        (if (outer_op == PLUS_EXPR)
3450         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3451         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3452       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3453            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3454        (if (outer_op == PLUS_EXPR)
3455         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3456         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3457        /* If the constant operation overflows we cannot do the transform
3458           directly as we would introduce undefined overflow, for example
3459           with (a - 1) + INT_MIN.  */
3460        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3461         (with { tree cst = const_binop (outer_op == inner_op
3462                                         ? PLUS_EXPR : MINUS_EXPR,
3463                                         type, @1, @2); }
3464          (if (cst)
3465           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3466            (inner_op @0 { cst; } )
3467            /* X+INT_MAX+1 is X-INT_MIN.  */
3468            (if (INTEGRAL_TYPE_P (type)
3469                 && wi::to_wide (cst) == wi::min_value (type))
3470             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3471             /* Last resort, use some unsigned type.  */
3472             (with { tree utype = unsigned_type_for (type); }
3473              (if (utype)
3474               (view_convert (inner_op
3475                              (view_convert:utype @0)
3476                              (view_convert:utype
3477                               { TREE_OVERFLOW (cst)
3478                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3480   /* (CST1 - A) +- CST2 -> CST3 - A  */
3481   (for outer_op (plus minus)
3482    (simplify
3483     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3484     /* If one of the types wraps, use that one.  */
3485     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3486      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3487         forever if something doesn't simplify into a constant.  */
3488      (if (!CONSTANT_CLASS_P (@0))
3489       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3490      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3491           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3492       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3493       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3494        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3495         (if (cst && !TREE_OVERFLOW (cst))
3496          (minus { cst; } @0))))))))
3498   /* CST1 - (CST2 - A) -> CST3 + A
3499      Use view_convert because it is safe for vectors and equivalent for
3500      scalars.  */
3501   (simplify
3502    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3503    /* If one of the types wraps, use that one.  */
3504    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3505     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3506       forever if something doesn't simplify into a constant.  */
3507     (if (!CONSTANT_CLASS_P (@0))
3508      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3509     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3510          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3511      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3512      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3513       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3514        (if (cst && !TREE_OVERFLOW (cst))
3515         (plus { cst; } @0)))))))
3517 /* ((T)(A)) + CST -> (T)(A + CST)  */
3518 #if GIMPLE
3519   (simplify
3520    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3521     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3522          && TREE_CODE (type) == INTEGER_TYPE
3523          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3524          && int_fits_type_p (@1, TREE_TYPE (@0)))
3525      /* Perform binary operation inside the cast if the constant fits
3526         and (A + CST)'s range does not overflow.  */
3527      (with
3528       {
3529         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3530                           max_ovf = wi::OVF_OVERFLOW;
3531         tree inner_type = TREE_TYPE (@0);
3533         wide_int w1
3534           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3535                             TYPE_SIGN (inner_type));
3537         value_range vr;
3538         if (get_global_range_query ()->range_of_expr (vr, @0)
3539             && !vr.varying_p () && !vr.undefined_p ())
3540           {
3541             wide_int wmin0 = vr.lower_bound ();
3542             wide_int wmax0 = vr.upper_bound ();
3543             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3544             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3545           }
3546       }
3547      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3548       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3549      )))
3550 #endif
3552 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3553 #if GIMPLE
3554   (for op (plus minus)
3555    (simplify
3556     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3557      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3558           && TREE_CODE (type) == INTEGER_TYPE
3559           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3560           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3561           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3562           && TYPE_OVERFLOW_WRAPS (type))
3563        (plus (convert @0) (op @2 (convert @1))))))
3564 #endif
3566 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3567    to a simple value.  */
3568   (for op (plus minus)
3569    (simplify
3570     (op (convert @0) (convert @1))
3571      (if (INTEGRAL_TYPE_P (type)
3572           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3573           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3574           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3575           && !TYPE_OVERFLOW_TRAPS (type)
3576           && !TYPE_OVERFLOW_SANITIZED (type))
3577       (convert (op! @0 @1)))))
3579   /* ~A + A -> -1 */
3580   (simplify
3581    (plus:c (convert? (bit_not @0)) (convert? @0))
3582    (if (!TYPE_OVERFLOW_TRAPS (type))
3583     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3585   /* ~A + 1 -> -A */
3586   (simplify
3587    (plus (convert? (bit_not @0)) integer_each_onep)
3588    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3589     (negate (convert @0))))
3591   /* -A - 1 -> ~A */
3592   (simplify
3593    (minus (convert? (negate @0)) integer_each_onep)
3594    (if (!TYPE_OVERFLOW_TRAPS (type)
3595         && TREE_CODE (type) != COMPLEX_TYPE
3596         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3597     (bit_not (convert @0))))
3599   /* -1 - A -> ~A */
3600   (simplify
3601    (minus integer_all_onesp @0)
3602    (if (TREE_CODE (type) != COMPLEX_TYPE)
3603     (bit_not @0)))
3605   /* (T)(P + A) - (T)P -> (T) A */
3606   (simplify
3607    (minus (convert (plus:c @@0 @1))
3608     (convert? @0))
3609    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3610         /* For integer types, if A has a smaller type
3611            than T the result depends on the possible
3612            overflow in P + A.
3613            E.g. T=size_t, A=(unsigned)429497295, P>0.
3614            However, if an overflow in P + A would cause
3615            undefined behavior, we can assume that there
3616            is no overflow.  */
3617         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3618             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3619     (convert @1)))
3620   (simplify
3621    (minus (convert (pointer_plus @@0 @1))
3622     (convert @0))
3623    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3624         /* For pointer types, if the conversion of A to the
3625            final type requires a sign- or zero-extension,
3626            then we have to punt - it is not defined which
3627            one is correct.  */
3628         || (POINTER_TYPE_P (TREE_TYPE (@0))
3629             && TREE_CODE (@1) == INTEGER_CST
3630             && tree_int_cst_sign_bit (@1) == 0))
3631     (convert @1)))
3632    (simplify
3633     (pointer_diff (pointer_plus @@0 @1) @0)
3634     /* The second argument of pointer_plus must be interpreted as signed, and
3635        thus sign-extended if necessary.  */
3636     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3637      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3638         second arg is unsigned even when we need to consider it as signed,
3639         we don't want to diagnose overflow here.  */
3640      (convert (view_convert:stype @1))))
3642   /* (T)P - (T)(P + A) -> -(T) A */
3643   (simplify
3644    (minus (convert? @0)
3645     (convert (plus:c @@0 @1)))
3646    (if (INTEGRAL_TYPE_P (type)
3647         && TYPE_OVERFLOW_UNDEFINED (type)
3648         /* For integer literals, using an intermediate unsigned type to avoid
3649            an overflow at run time is counter-productive because it introduces
3650            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3651            the result, which may be problematic in GENERIC for some front-ends:
3652              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3653            so we use the direct path for them.  */
3654         && TREE_CODE (@1) != INTEGER_CST
3655         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3656     (with { tree utype = unsigned_type_for (type); }
3657      (convert (negate (convert:utype @1))))
3658     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3659          /* For integer types, if A has a smaller type
3660             than T the result depends on the possible
3661             overflow in P + A.
3662             E.g. T=size_t, A=(unsigned)429497295, P>0.
3663             However, if an overflow in P + A would cause
3664             undefined behavior, we can assume that there
3665             is no overflow.  */
3666          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3667              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3668      (negate (convert @1)))))
3669   (simplify
3670    (minus (convert @0)
3671     (convert (pointer_plus @@0 @1)))
3672    (if (INTEGRAL_TYPE_P (type)
3673         && TYPE_OVERFLOW_UNDEFINED (type)
3674         /* See above the rationale for this condition.  */
3675         && TREE_CODE (@1) != INTEGER_CST
3676         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3677     (with { tree utype = unsigned_type_for (type); }
3678      (convert (negate (convert:utype @1))))
3679     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3680          /* For pointer types, if the conversion of A to the
3681             final type requires a sign- or zero-extension,
3682             then we have to punt - it is not defined which
3683             one is correct.  */
3684          || (POINTER_TYPE_P (TREE_TYPE (@0))
3685              && TREE_CODE (@1) == INTEGER_CST
3686              && tree_int_cst_sign_bit (@1) == 0))
3687      (negate (convert @1)))))
3688    (simplify
3689     (pointer_diff @0 (pointer_plus @@0 @1))
3690     /* The second argument of pointer_plus must be interpreted as signed, and
3691        thus sign-extended if necessary.  */
3692     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3693      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3694         second arg is unsigned even when we need to consider it as signed,
3695         we don't want to diagnose overflow here.  */
3696      (negate (convert (view_convert:stype @1)))))
3698   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3699   (simplify
3700    (minus (convert (plus:c @@0 @1))
3701     (convert (plus:c @0 @2)))
3702    (if (INTEGRAL_TYPE_P (type)
3703         && TYPE_OVERFLOW_UNDEFINED (type)
3704         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3705         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3706     (with { tree utype = unsigned_type_for (type); }
3707      (convert (minus (convert:utype @1) (convert:utype @2))))
3708     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3709           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3710          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3711              /* For integer types, if A has a smaller type
3712                 than T the result depends on the possible
3713                 overflow in P + A.
3714                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3715                 However, if an overflow in P + A would cause
3716                 undefined behavior, we can assume that there
3717                 is no overflow.  */
3718              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3719                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3720                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3721                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3722      (minus (convert @1) (convert @2)))))
3723   (simplify
3724    (minus (convert (pointer_plus @@0 @1))
3725     (convert (pointer_plus @0 @2)))
3726    (if (INTEGRAL_TYPE_P (type)
3727         && TYPE_OVERFLOW_UNDEFINED (type)
3728         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3729     (with { tree utype = unsigned_type_for (type); }
3730      (convert (minus (convert:utype @1) (convert:utype @2))))
3731     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3732          /* For pointer types, if the conversion of A to the
3733             final type requires a sign- or zero-extension,
3734             then we have to punt - it is not defined which
3735             one is correct.  */
3736          || (POINTER_TYPE_P (TREE_TYPE (@0))
3737              && TREE_CODE (@1) == INTEGER_CST
3738              && tree_int_cst_sign_bit (@1) == 0
3739              && TREE_CODE (@2) == INTEGER_CST
3740              && tree_int_cst_sign_bit (@2) == 0))
3741      (minus (convert @1) (convert @2)))))
3742    (simplify
3743     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3744      (pointer_diff @0 @1))
3745    (simplify
3746     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3747     /* The second argument of pointer_plus must be interpreted as signed, and
3748        thus sign-extended if necessary.  */
3749     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3750      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3751         second arg is unsigned even when we need to consider it as signed,
3752         we don't want to diagnose overflow here.  */
3753      (minus (convert (view_convert:stype @1))
3754             (convert (view_convert:stype @2)))))))
3756 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3757     Modeled after fold_plusminus_mult_expr.  */
3758 (if (!TYPE_SATURATING (type)
3759      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3760  (for plusminus (plus minus)
3761   (simplify
3762    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3763    (if (!ANY_INTEGRAL_TYPE_P (type)
3764         || TYPE_OVERFLOW_WRAPS (type)
3765         || (INTEGRAL_TYPE_P (type)
3766             && tree_expr_nonzero_p (@0)
3767             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3768     (if (single_use (@3) || single_use (@4))
3769      /* If @1 +- @2 is constant require a hard single-use on either
3770         original operand (but not on both).  */
3771      (mult (plusminus @1 @2) @0)
3772      (mult! (plusminus @1 @2) @0)
3773   )))
3774   /* We cannot generate constant 1 for fract.  */
3775   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3776    (simplify
3777     (plusminus @0 (mult:c@3 @0 @2))
3778     (if ((!ANY_INTEGRAL_TYPE_P (type)
3779           || TYPE_OVERFLOW_WRAPS (type)
3780           /* For @0 + @0*@2 this transformation would introduce UB
3781              (where there was none before) for @0 in [-1,0] and @2 max.
3782              For @0 - @0*@2 this transformation would introduce UB
3783              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3784           || (INTEGRAL_TYPE_P (type)
3785               && ((tree_expr_nonzero_p (@0)
3786                    && expr_not_equal_to (@0,
3787                                 wi::minus_one (TYPE_PRECISION (type))))
3788                   || (plusminus == PLUS_EXPR
3789                       ? expr_not_equal_to (@2,
3790                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3791                       /* Let's ignore the @0 -1 and @2 min case.  */
3792                       : (expr_not_equal_to (@2,
3793                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3794                          && expr_not_equal_to (@2,
3795                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3796                                 + 1))))))
3797          && single_use (@3))
3798      (mult (plusminus { build_one_cst (type); } @2) @0)))
3799    (simplify
3800     (plusminus (mult:c@3 @0 @2) @0)
3801     (if ((!ANY_INTEGRAL_TYPE_P (type)
3802           || TYPE_OVERFLOW_WRAPS (type)
3803           /* For @0*@2 + @0 this transformation would introduce UB
3804              (where there was none before) for @0 in [-1,0] and @2 max.
3805              For @0*@2 - @0 this transformation would introduce UB
3806              for @0 0 and @2 min.  */
3807           || (INTEGRAL_TYPE_P (type)
3808               && ((tree_expr_nonzero_p (@0)
3809                    && (plusminus == MINUS_EXPR
3810                        || expr_not_equal_to (@0,
3811                                 wi::minus_one (TYPE_PRECISION (type)))))
3812                   || expr_not_equal_to (@2,
3813                         (plusminus == PLUS_EXPR
3814                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3815                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3816          && single_use (@3))
3817      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3819 #if GIMPLE
3820 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3821    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3822 (simplify
3823  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3824   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3825        && tree_fits_uhwi_p (@1)
3826        && tree_to_uhwi (@1) < element_precision (type)
3827        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3828            || optab_handler (smul_optab,
3829                              TYPE_MODE (type)) != CODE_FOR_nothing))
3830    (with { tree t = type;
3831            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3832            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3833                                              element_precision (type));
3834            w += 1;
3835            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3836                                         : t, w);
3837            cst = build_uniform_cst (t, cst); }
3838     (convert (mult (convert:t @0) { cst; })))))
3839 (simplify
3840  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3841   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3842        && tree_fits_uhwi_p (@1)
3843        && tree_to_uhwi (@1) < element_precision (type)
3844        && tree_fits_uhwi_p (@2)
3845        && tree_to_uhwi (@2) < element_precision (type)
3846        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3847            || optab_handler (smul_optab,
3848                              TYPE_MODE (type)) != CODE_FOR_nothing))
3849    (with { tree t = type;
3850            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3851            unsigned int prec = element_precision (type);
3852            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3853            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3854            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3855                                         : t, w);
3856            cst = build_uniform_cst (t, cst); }
3857     (convert (mult (convert:t @0) { cst; })))))
3858 #endif
3860 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3861    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3862    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3863 (for op (bit_ior bit_xor)
3864  (simplify
3865   (op (mult:s@0 @1 INTEGER_CST@2)
3866       (mult:s@3 @1 INTEGER_CST@4))
3867   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3868        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3869    (mult @1
3870          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3871  (simplify
3872   (op:c (mult:s@0 @1 INTEGER_CST@2)
3873         (lshift:s@3 @1 INTEGER_CST@4))
3874   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3875        && tree_int_cst_sgn (@4) > 0
3876        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3877    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3878            wide_int c = wi::add (wi::to_wide (@2),
3879                                  wi::lshift (wone, wi::to_wide (@4))); }
3880     (mult @1 { wide_int_to_tree (type, c); }))))
3881  (simplify
3882   (op:c (mult:s@0 @1 INTEGER_CST@2)
3883         @1)
3884   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3885        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3886    (mult @1
3887          { wide_int_to_tree (type,
3888                              wi::add (wi::to_wide (@2), 1)); })))
3889  (simplify
3890   (op (lshift:s@0 @1 INTEGER_CST@2)
3891       (lshift:s@3 @1 INTEGER_CST@4))
3892   (if (INTEGRAL_TYPE_P (type)
3893        && tree_int_cst_sgn (@2) > 0
3894        && tree_int_cst_sgn (@4) > 0
3895        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3896    (with { tree t = type;
3897            if (!TYPE_OVERFLOW_WRAPS (t))
3898              t = unsigned_type_for (t);
3899            wide_int wone = wi::one (TYPE_PRECISION (t));
3900            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3901                                  wi::lshift (wone, wi::to_wide (@4))); }
3902     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3903  (simplify
3904   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3905         @1)
3906   (if (INTEGRAL_TYPE_P (type)
3907        && tree_int_cst_sgn (@2) > 0
3908        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3909    (with { tree t = type;
3910            if (!TYPE_OVERFLOW_WRAPS (t))
3911              t = unsigned_type_for (t);
3912            wide_int wone = wi::one (TYPE_PRECISION (t));
3913            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3914     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3916 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3918 (for minmax (min max)
3919  (simplify
3920   (minmax @0 @0)
3921   @0)
3922 /* max(max(x,y),x) -> max(x,y)  */
3923  (simplify
3924   (minmax:c (minmax:c@2 @0 @1) @0)
3925   @2))
3926 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3927 (for minmax (FMIN_ALL FMAX_ALL)
3928  (simplify
3929   (minmax @0 @0)
3930   (if (!tree_expr_maybe_signaling_nan_p (@0))
3931     @0)))
3932 /* min(max(x,y),y) -> y.  */
3933 (simplify
3934  (min:c (max:c @0 @1) @1)
3935  @1)
3936 /* max(min(x,y),y) -> y.  */
3937 (simplify
3938  (max:c (min:c @0 @1) @1)
3939  @1)
3940 /* max(a,-a) -> abs(a).  */
3941 (simplify
3942  (max:c @0 (negate @0))
3943  (if (TREE_CODE (type) != COMPLEX_TYPE
3944       && (! ANY_INTEGRAL_TYPE_P (type)
3945           || TYPE_OVERFLOW_UNDEFINED (type)))
3946   (abs @0)))
3947 /* min(a,-a) -> -abs(a).  */
3948 (simplify
3949  (min:c @0 (negate @0))
3950  (if (TREE_CODE (type) != COMPLEX_TYPE
3951       && (! ANY_INTEGRAL_TYPE_P (type)
3952           || TYPE_OVERFLOW_UNDEFINED (type)))
3953   (negate (abs @0))))
3954 (simplify
3955  (min @0 @1)
3956  (switch
3957   (if (INTEGRAL_TYPE_P (type)
3958        && TYPE_MIN_VALUE (type)
3959        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3960    @1)
3961   (if (INTEGRAL_TYPE_P (type)
3962        && TYPE_MAX_VALUE (type)
3963        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3964    @0)))
3965 (simplify
3966  (max @0 @1)
3967  (switch
3968   (if (INTEGRAL_TYPE_P (type)
3969        && TYPE_MAX_VALUE (type)
3970        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3971    @1)
3972   (if (INTEGRAL_TYPE_P (type)
3973        && TYPE_MIN_VALUE (type)
3974        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3975    @0)))
3977 /* max (a, a + CST) -> a + CST where CST is positive.  */
3978 /* max (a, a + CST) -> a where CST is negative.  */
3979 (simplify
3980  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3981   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3982    (if (tree_int_cst_sgn (@1) > 0)
3983     @2
3984     @0)))
3986 /* min (a, a + CST) -> a where CST is positive.  */
3987 /* min (a, a + CST) -> a + CST where CST is negative. */
3988 (simplify
3989  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3990   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3991    (if (tree_int_cst_sgn (@1) > 0)
3992     @0
3993     @2)))
3995 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3996    the addresses are known to be less, equal or greater.  */
3997 (for minmax (min max)
3998      cmp (lt gt)
3999  (simplify
4000   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4001   (with
4002    {
4003      poly_int64 off0, off1;
4004      tree base0, base1;
4005      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4006                                   off0, off1, GENERIC);
4007    }
4008    (if (equal == 1)
4009     (if (minmax == MIN_EXPR)
4010      (if (known_le (off0, off1))
4011       @2
4012       (if (known_gt (off0, off1))
4013        @3))
4014      (if (known_ge (off0, off1))
4015       @2
4016       (if (known_lt (off0, off1))
4017        @3)))))))
4019 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4020    and the outer convert demotes the expression back to x's type.  */
4021 (for minmax (min max)
4022  (simplify
4023   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4024   (if (INTEGRAL_TYPE_P (type)
4025        && types_match (@1, type) && int_fits_type_p (@2, type)
4026        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4027        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4028    (minmax @1 (convert @2)))))
4030 (for minmax (FMIN_ALL FMAX_ALL)
4031  /* If either argument is NaN and other one is not sNaN, return the other
4032     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4033  (simplify
4034   (minmax:c @0 REAL_CST@1)
4035    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4036        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4037        && !tree_expr_maybe_signaling_nan_p (@0))
4038    @0)))
4039 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4040    functions to return the numeric arg if the other one is NaN.
4041    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4042    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4043    worry about it either.  */
4044 (if (flag_finite_math_only)
4045  (simplify
4046   (FMIN_ALL @0 @1)
4047   (min @0 @1))
4048  (simplify
4049   (FMAX_ALL @0 @1)
4050   (max @0 @1)))
4051 /* min (-A, -B) -> -max (A, B)  */
4052 (for minmax (min max FMIN_ALL FMAX_ALL)
4053      maxmin (max min FMAX_ALL FMIN_ALL)
4054  (simplify
4055   (minmax (negate:s@2 @0) (negate:s@3 @1))
4056   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4057        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4058            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4059    (negate (maxmin @0 @1)))))
4060 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4061    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4062 (for minmax (min max)
4063  maxmin (max min)
4064  (simplify
4065   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4066   (bit_not (maxmin @0 @1)))
4067 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4068 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4069  (simplify
4070   (bit_not (minmax:cs (bit_not @0) @1))
4071   (maxmin @0 (bit_not @1))))
4073 /* MIN (X, Y) == X -> X <= Y  */
4074 /* MIN (X, Y) < X -> X > Y  */
4075 /* MIN (X, Y) >= X -> X <= Y  */
4076 (for minmax (min min min min max max max max)
4077      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4078      out    (le  gt  gt  le  ge  lt  lt  ge )
4079  (simplify
4080   (cmp:c (minmax:c @0 @1) @0)
4081   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4082    (out @0 @1))))
4083 /* MIN (X, 5) == 0 -> X == 0
4084    MIN (X, 5) == 7 -> false  */
4085 (for cmp (eq ne)
4086  (simplify
4087   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4088   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4089                  TYPE_SIGN (TREE_TYPE (@0))))
4090    { constant_boolean_node (cmp == NE_EXPR, type); }
4091    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4092                   TYPE_SIGN (TREE_TYPE (@0))))
4093     (cmp @0 @2)))))
4094 (for cmp (eq ne)
4095  (simplify
4096   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4097   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4098                  TYPE_SIGN (TREE_TYPE (@0))))
4099    { constant_boolean_node (cmp == NE_EXPR, type); }
4100    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4101                   TYPE_SIGN (TREE_TYPE (@0))))
4102     (cmp @0 @2)))))
4104 /* X <= MAX(X, Y) -> true
4105    X > MAX(X, Y) -> false 
4106    X >= MIN(X, Y) -> true
4107    X < MIN(X, Y) -> false */
4108 (for minmax (min     min     max     max     )
4109      cmp    (ge      lt      le      gt      )
4110  (simplify
4111   (cmp:c @0 (minmax:c @0 @1))
4112   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4114 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4115 (for minmax (min     min     max     max     min     min     max     max    )
4116      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4117      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4118  (simplify
4119   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4120   (comb (cmp @0 @2) (cmp @1 @2))))
4122 /* Undo fancy ways of writing max/min or other ?: expressions, like
4123    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4124    People normally use ?: and that is what we actually try to optimize.  */
4125 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4126 (simplify
4127  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4128  (if (INTEGRAL_TYPE_P (type)
4129       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4130   (cond (convert:boolean_type_node @2) @1 @0)))
4131 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4132 (simplify
4133  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4134  (if (INTEGRAL_TYPE_P (type)
4135       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4136   (cond (convert:boolean_type_node @2) @1 @0)))
4137 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4138 (simplify
4139  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4140  (if (INTEGRAL_TYPE_P (type)
4141       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4142   (cond (convert:boolean_type_node @2) @1 @0)))
4144 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4145 (simplify
4146   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4147   (max @2 @1))
4149 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4150 (for op (bit_xor bit_ior plus)
4151  (simplify
4152   (cond (eq zero_one_valued_p@0
4153             integer_zerop)
4154         @1
4155         (op:c @2 @1))
4156   (if (INTEGRAL_TYPE_P (type)
4157        && TYPE_PRECISION (type) > 1
4158        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4159        (op (mult (convert:type @0) @2) @1))))
4161 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4162 (for op (bit_xor bit_ior plus)
4163  (simplify
4164   (cond (ne zero_one_valued_p@0
4165             integer_zerop)
4166        (op:c @2 @1)
4167         @1)
4168   (if (INTEGRAL_TYPE_P (type)
4169        && TYPE_PRECISION (type) > 1
4170        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4171        (op (mult (convert:type @0) @2) @1))))
4173 /* ?: Value replacement. */
4174 /* a == 0 ? b : b + a  -> b + a */
4175 (for op (plus bit_ior bit_xor)
4176  (simplify
4177   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4178    @2))
4179 /* a == 0 ? b : b - a  -> b - a */
4180 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4181 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4182 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4183  (simplify
4184   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4185    @2))
4187 /* a == 1 ? b : b / a  -> b / a */
4188 (for op (trunc_div ceil_div floor_div round_div exact_div)
4189  (simplify
4190   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4191    @2))
4193 /* a == 1 ? b : a * b -> a * b */
4194 (for op (mult)
4195  (simplify
4196   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4197    @2))
4199 /* a == -1 ? b : a & b -> a & b */
4200 (for op (bit_and)
4201  (simplify
4202   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4203    @2))
4205 /* Simplifications of shift and rotates.  */
4207 (for rotate (lrotate rrotate)
4208  (simplify
4209   (rotate integer_all_onesp@0 @1)
4210   @0))
4212 /* Optimize -1 >> x for arithmetic right shifts.  */
4213 (simplify
4214  (rshift integer_all_onesp@0 @1)
4215  (if (!TYPE_UNSIGNED (type))
4216   @0))
4218 /* Optimize (x >> c) << c into x & (-1<<c).  */
4219 (simplify
4220  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4221  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4222   /* It doesn't matter if the right shift is arithmetic or logical.  */
4223   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4225 (simplify
4226  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4227  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4228       /* Allow intermediate conversion to integral type with whatever sign, as
4229          long as the low TYPE_PRECISION (type)
4230          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4231       && INTEGRAL_TYPE_P (type)
4232       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4233       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4234       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4235       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4236           || wi::geu_p (wi::to_wide (@1),
4237                         TYPE_PRECISION (type)
4238                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4239   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4241 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4242    unsigned x OR truncate into the precision(type) - c lowest bits
4243    of signed x (if they have mode precision or a precision of 1).  */
4244 (simplify
4245  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4246  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4247   (if (TYPE_UNSIGNED (type))
4248    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4249    (if (INTEGRAL_TYPE_P (type))
4250     (with {
4251       int width = element_precision (type) - tree_to_uhwi (@1);
4252       tree stype = NULL_TREE;
4253       if (width <= MAX_FIXED_MODE_SIZE)
4254         stype = build_nonstandard_integer_type (width, 0);
4255      }
4256      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4257       (convert (convert:stype @0))))))))
4259 /* Optimize x >> x into 0 */
4260 (simplify
4261  (rshift @0 @0)
4262   { build_zero_cst (type); })
4264 (for shiftrotate (lrotate rrotate lshift rshift)
4265  (simplify
4266   (shiftrotate @0 integer_zerop)
4267   (non_lvalue @0))
4268  (simplify
4269   (shiftrotate integer_zerop@0 @1)
4270   @0)
4271  /* Prefer vector1 << scalar to vector1 << vector2
4272     if vector2 is uniform.  */
4273  (for vec (VECTOR_CST CONSTRUCTOR)
4274   (simplify
4275    (shiftrotate @0 vec@1)
4276    (with { tree tem = uniform_vector_p (@1); }
4277     (if (tem)
4278      (shiftrotate @0 { tem; }))))))
4280 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4281    Y is 0.  Similarly for X >> Y.  */
4282 #if GIMPLE
4283 (for shift (lshift rshift)
4284  (simplify
4285   (shift @0 SSA_NAME@1)
4286    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4287     (with {
4288       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4289       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4290      }
4291      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4292       @0)))))
4293 #endif
4295 /* Rewrite an LROTATE_EXPR by a constant into an
4296    RROTATE_EXPR by a new constant.  */
4297 (simplify
4298  (lrotate @0 INTEGER_CST@1)
4299  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4300                             build_int_cst (TREE_TYPE (@1),
4301                                            element_precision (type)), @1); }))
4303 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4304 (for op (lrotate rrotate rshift lshift)
4305  (simplify
4306   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4307   (with { unsigned int prec = element_precision (type); }
4308    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4309         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4310         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4311         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4312     (with { unsigned int low = (tree_to_uhwi (@1)
4313                                 + tree_to_uhwi (@2)); }
4314      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4315         being well defined.  */
4316      (if (low >= prec)
4317       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4318        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4319        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4320         { build_zero_cst (type); }
4321         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4322       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4325 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4326 (simplify
4327  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4328   (if ((wi::to_wide (@1) & 1) != 0)
4329    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4330    { build_zero_cst (type); }))
4332 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4333    either to false if D is smaller (unsigned comparison) than C, or to
4334    x == log2 (D) - log2 (C).  Similarly for right shifts.
4335    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4336 (for cmp (ne eq)
4337      icmp (eq ne)
4338  (simplify
4339   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4340    (with { int c1 = wi::clz (wi::to_wide (@1));
4341            int c2 = wi::clz (wi::to_wide (@2)); }
4342     (if (c1 < c2)
4343      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4344      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4345  (simplify
4346   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4347    (if (tree_int_cst_sgn (@1) > 0)
4348     (with { int c1 = wi::clz (wi::to_wide (@1));
4349             int c2 = wi::clz (wi::to_wide (@2)); }
4350      (if (c1 > c2)
4351       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4352       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4353  /* `(1 >> X) != 0` -> `X == 0` */
4354  /* `(1 >> X) == 0` -> `X != 0` */
4355  (simplify
4356   (cmp (rshift integer_onep@1 @0) integer_zerop)
4357    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4358     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4360 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4361    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4362    if CST2 != 0.  */
4363 (for cmp (ne eq)
4364  (simplify
4365   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4366   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4367    (if (cand < 0
4368         || (!integer_zerop (@2)
4369             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4370     { constant_boolean_node (cmp == NE_EXPR, type); }
4371     (if (!integer_zerop (@2)
4372          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4373      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4375 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4376         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4377 (for cmp (ne eq)
4378  (simplify
4379   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4380   (if (tree_fits_shwi_p (@1)
4381        && tree_to_shwi (@1) > 0
4382        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4383     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4384       { constant_boolean_node (cmp == NE_EXPR, type); }
4385       (with { wide_int c1 = wi::to_wide (@1);
4386               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4387               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4388         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4389              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4390  (simplify
4391   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4392   (if (tree_fits_shwi_p (@1)
4393        && tree_to_shwi (@1) > 0
4394        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4395     (with { tree t0 = TREE_TYPE (@0);
4396             unsigned int prec = TYPE_PRECISION (t0);
4397             wide_int c1 = wi::to_wide (@1);
4398             wide_int c2 = wi::to_wide (@2);
4399             wide_int c3 = wi::to_wide (@3);
4400             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4401       (if ((c2 & c3) != c3)
4402         { constant_boolean_node (cmp == NE_EXPR, type); }
4403         (if (TYPE_UNSIGNED (t0))
4404           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4405             { constant_boolean_node (cmp == NE_EXPR, type); }
4406             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4407                  { wide_int_to_tree (t0, c3 << c1); }))
4408           (with { wide_int smask = wi::arshift (sb, c1); }
4409             (switch
4410               (if ((c2 & smask) == 0)
4411                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4412                      { wide_int_to_tree (t0, c3 << c1); }))
4413               (if ((c3 & smask) == 0)
4414                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4415                      { wide_int_to_tree (t0, c3 << c1); }))
4416               (if ((c2 & smask) != (c3 & smask))
4417                 { constant_boolean_node (cmp == NE_EXPR, type); })
4418               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4419                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4421 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4422         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4423    if the new mask might be further optimized.  */
4424 (for shift (lshift rshift)
4425  (simplify
4426   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4427            INTEGER_CST@2)
4428    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4429         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4430         && tree_fits_uhwi_p (@1)
4431         && tree_to_uhwi (@1) > 0
4432         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4433     (with
4434      {
4435        unsigned int shiftc = tree_to_uhwi (@1);
4436        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4437        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4438        tree shift_type = TREE_TYPE (@3);
4439        unsigned int prec;
4441        if (shift == LSHIFT_EXPR)
4442          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4443        else if (shift == RSHIFT_EXPR
4444                 && type_has_mode_precision_p (shift_type))
4445          {
4446            prec = TYPE_PRECISION (TREE_TYPE (@3));
4447            tree arg00 = @0;
4448            /* See if more bits can be proven as zero because of
4449               zero extension.  */
4450            if (@3 != @0
4451                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4452              {
4453                tree inner_type = TREE_TYPE (@0);
4454                if (type_has_mode_precision_p (inner_type)
4455                    && TYPE_PRECISION (inner_type) < prec)
4456                  {
4457                    prec = TYPE_PRECISION (inner_type);
4458                    /* See if we can shorten the right shift.  */
4459                    if (shiftc < prec)
4460                      shift_type = inner_type;
4461                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4462                       it into (X, 0) later on by making sure zerobits
4463                       is all ones.  */
4464                  }
4465              }
4466            zerobits = HOST_WIDE_INT_M1U;
4467            if (shiftc < prec)
4468              {
4469                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4470                zerobits <<= prec - shiftc;
4471              }
4472            /* For arithmetic shift if sign bit could be set, zerobits
4473               can contain actually sign bits, so no transformation is
4474               possible, unless MASK masks them all away.  In that
4475               case the shift needs to be converted into logical shift.  */
4476            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4477                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4478              {
4479                if ((mask & zerobits) == 0)
4480                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4481                else
4482                  zerobits = 0;
4483              }
4484          }
4485      }
4486      /* ((X << 16) & 0xff00) is (X, 0).  */
4487      (if ((mask & zerobits) == mask)
4488       { build_int_cst (type, 0); }
4489       (with { newmask = mask | zerobits; }
4490        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4491         (with
4492          {
4493            /* Only do the transformation if NEWMASK is some integer
4494               mode's mask.  */
4495            for (prec = BITS_PER_UNIT;
4496                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4497              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4498                break;
4499          }
4500          (if (prec < HOST_BITS_PER_WIDE_INT
4501               || newmask == HOST_WIDE_INT_M1U)
4502           (with
4503            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4504            (if (!tree_int_cst_equal (newmaskt, @2))
4505             (if (shift_type != TREE_TYPE (@3))
4506              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4507              (bit_and @4 { newmaskt; })))))))))))))
4509 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4510 (for cmp (ne eq)
4511        icmp (eq ne)
4512  (simplify
4513   (cmp
4514    (bit_and
4515     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4516   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4517    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4518                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4520 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4521    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4522 (for shift (lshift rshift)
4523  (for bit_op (bit_and bit_xor bit_ior)
4524   (simplify
4525    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4526    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4527     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4528      (if (mask)
4529       (bit_op (shift (convert @0) @1) { mask; })))))))
4531 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4532 (simplify
4533  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4534   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4535        && (element_precision (TREE_TYPE (@0))
4536            <= element_precision (TREE_TYPE (@1))
4537            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4538    (with
4539     { tree shift_type = TREE_TYPE (@0); }
4540      (convert (rshift (convert:shift_type @1) @2)))))
4542 /* ~(~X >>r Y) -> X >>r Y
4543    ~(~X <<r Y) -> X <<r Y */
4544 (for rotate (lrotate rrotate)
4545  (simplify
4546   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4547    (if ((element_precision (TREE_TYPE (@0))
4548          <= element_precision (TREE_TYPE (@1))
4549          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4550         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4551             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4552     (with
4553      { tree rotate_type = TREE_TYPE (@0); }
4554       (convert (rotate (convert:rotate_type @1) @2))))))
4556 (for cmp (eq ne)
4557  (for rotate (lrotate rrotate)
4558       invrot (rrotate lrotate)
4559   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4560   (simplify
4561    (cmp (rotate @1 @0) (rotate @2 @0))
4562    (cmp @1 @2))
4563   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4564   (simplify
4565    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4566    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4567   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4568   (simplify
4569    (cmp (rotate @0 @1) INTEGER_CST@2)
4570     (if (integer_zerop (@2) || integer_all_onesp (@2))
4571      (cmp @0 @2)))))
4573 /* Narrow a lshift by constant.  */
4574 (simplify
4575  (convert (lshift:s@0 @1 INTEGER_CST@2))
4576  (if (INTEGRAL_TYPE_P (type)
4577       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4578       && !integer_zerop (@2)
4579       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4580   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4581        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4582    (lshift (convert @1) @2)
4583    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4584     { build_zero_cst (type); }))))
4586 /* Simplifications of conversions.  */
4588 /* Basic strip-useless-type-conversions / strip_nops.  */
4589 (for cvt (convert view_convert float fix_trunc)
4590  (simplify
4591   (cvt @0)
4592   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4593        || (GENERIC && type == TREE_TYPE (@0)))
4594    @0)))
4596 /* Contract view-conversions.  */
4597 (simplify
4598   (view_convert (view_convert @0))
4599   (view_convert @0))
4601 /* For integral conversions with the same precision or pointer
4602    conversions use a NOP_EXPR instead.  */
4603 (simplify
4604   (view_convert @0)
4605   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4606        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4607        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4608    (convert @0)))
4610 /* Strip inner integral conversions that do not change precision or size, or
4611    zero-extend while keeping the same size (for bool-to-char).  */
4612 (simplify
4613   (view_convert (convert@0 @1))
4614   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4615        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4616        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4617        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4618            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4619                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4620    (view_convert @1)))
4622 /* Simplify a view-converted empty or single-element constructor.  */
4623 (simplify
4624   (view_convert CONSTRUCTOR@0)
4625   (with
4626    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4627                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4628    (switch
4629     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4630      { build_zero_cst (type); })
4631     (if (CONSTRUCTOR_NELTS (ctor) == 1
4632          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4633          && operand_equal_p (TYPE_SIZE (type),
4634                              TYPE_SIZE (TREE_TYPE
4635                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4636      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4638 /* Re-association barriers around constants and other re-association
4639    barriers can be removed.  */
4640 (simplify
4641  (paren CONSTANT_CLASS_P@0)
4642  @0)
4643 (simplify
4644  (paren (paren@1 @0))
4645  @1)
4647 /* Handle cases of two conversions in a row.  */
4648 (for ocvt (convert float fix_trunc)
4649  (for icvt (convert float)
4650   (simplify
4651    (ocvt (icvt@1 @0))
4652    (with
4653     {
4654       tree inside_type = TREE_TYPE (@0);
4655       tree inter_type = TREE_TYPE (@1);
4656       int inside_int = INTEGRAL_TYPE_P (inside_type);
4657       int inside_ptr = POINTER_TYPE_P (inside_type);
4658       int inside_float = FLOAT_TYPE_P (inside_type);
4659       int inside_vec = VECTOR_TYPE_P (inside_type);
4660       unsigned int inside_prec = element_precision (inside_type);
4661       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4662       int inter_int = INTEGRAL_TYPE_P (inter_type);
4663       int inter_ptr = POINTER_TYPE_P (inter_type);
4664       int inter_float = FLOAT_TYPE_P (inter_type);
4665       int inter_vec = VECTOR_TYPE_P (inter_type);
4666       unsigned int inter_prec = element_precision (inter_type);
4667       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4668       int final_int = INTEGRAL_TYPE_P (type);
4669       int final_ptr = POINTER_TYPE_P (type);
4670       int final_float = FLOAT_TYPE_P (type);
4671       int final_vec = VECTOR_TYPE_P (type);
4672       unsigned int final_prec = element_precision (type);
4673       int final_unsignedp = TYPE_UNSIGNED (type);
4674     }
4675    (switch
4676     /* In addition to the cases of two conversions in a row
4677        handled below, if we are converting something to its own
4678        type via an object of identical or wider precision, neither
4679        conversion is needed.  */
4680     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4681           || (GENERIC
4682               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4683          && (((inter_int || inter_ptr) && final_int)
4684              || (inter_float && final_float))
4685          && inter_prec >= final_prec)
4686      (ocvt @0))
4688     /* Likewise, if the intermediate and initial types are either both
4689        float or both integer, we don't need the middle conversion if the
4690        former is wider than the latter and doesn't change the signedness
4691        (for integers).  Avoid this if the final type is a pointer since
4692        then we sometimes need the middle conversion.  */
4693     (if (((inter_int && inside_int) || (inter_float && inside_float))
4694          && (final_int || final_float)
4695          && inter_prec >= inside_prec
4696          && (inter_float || inter_unsignedp == inside_unsignedp))
4697      (ocvt @0))
4699     /* If we have a sign-extension of a zero-extended value, we can
4700        replace that by a single zero-extension.  Likewise if the
4701        final conversion does not change precision we can drop the
4702        intermediate conversion.  */
4703     (if (inside_int && inter_int && final_int
4704          && ((inside_prec < inter_prec && inter_prec < final_prec
4705               && inside_unsignedp && !inter_unsignedp)
4706              || final_prec == inter_prec))
4707      (ocvt @0))
4709     /* Two conversions in a row are not needed unless:
4710         - some conversion is floating-point (overstrict for now), or
4711         - some conversion is a vector (overstrict for now), or
4712         - the intermediate type is narrower than both initial and
4713           final, or
4714         - the intermediate type and innermost type differ in signedness,
4715           and the outermost type is wider than the intermediate, or
4716         - the initial type is a pointer type and the precisions of the
4717           intermediate and final types differ, or
4718         - the final type is a pointer type and the precisions of the
4719           initial and intermediate types differ.  */
4720     (if (! inside_float && ! inter_float && ! final_float
4721          && ! inside_vec && ! inter_vec && ! final_vec
4722          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4723          && ! (inside_int && inter_int
4724                && inter_unsignedp != inside_unsignedp
4725                && inter_prec < final_prec)
4726          && ((inter_unsignedp && inter_prec > inside_prec)
4727              == (final_unsignedp && final_prec > inter_prec))
4728          && ! (inside_ptr && inter_prec != final_prec)
4729          && ! (final_ptr && inside_prec != inter_prec))
4730      (ocvt @0))
4732    /* `(outer:M)(inter:N) a:O`
4733       can be converted to `(outer:M) a`
4734       if M <= O && N >= O. No matter what signedness of the casts,
4735       as the final is either a truncation from the original or just
4736       a sign change of the type. */
4737    (if (inside_int && inter_int && final_int
4738         && final_prec <= inside_prec
4739         && inter_prec >= inside_prec)
4740     (convert @0))
4742     /* A truncation to an unsigned type (a zero-extension) should be
4743        canonicalized as bitwise and of a mask.  */
4744     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4745          && final_int && inter_int && inside_int
4746          && final_prec == inside_prec
4747          && final_prec > inter_prec
4748          && inter_unsignedp)
4749      (convert (bit_and @0 { wide_int_to_tree
4750                               (inside_type,
4751                                wi::mask (inter_prec, false,
4752                                          TYPE_PRECISION (inside_type))); })))
4754     /* If we are converting an integer to a floating-point that can
4755        represent it exactly and back to an integer, we can skip the
4756        floating-point conversion.  */
4757     (if (GIMPLE /* PR66211 */
4758          && inside_int && inter_float && final_int &&
4759          (unsigned) significand_size (TYPE_MODE (inter_type))
4760          >= inside_prec - !inside_unsignedp)
4761      (convert @0)))))))
4763 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4764    float_type.  Only do the transformation if we do not need to preserve
4765    trapping behaviour, so require !flag_trapping_math. */
4766 #if GIMPLE
4767 (simplify
4768    (float (fix_trunc @0))
4769    (if (!flag_trapping_math
4770         && types_match (type, TREE_TYPE (@0))
4771         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4772                                           OPTIMIZE_FOR_BOTH))
4773       (IFN_TRUNC @0)))
4774 #endif
4776 /* If we have a narrowing conversion to an integral type that is fed by a
4777    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4778    masks off bits outside the final type (and nothing else).  */
4779 (simplify
4780   (convert (bit_and @0 INTEGER_CST@1))
4781   (if (INTEGRAL_TYPE_P (type)
4782        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4783        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4784        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4785                                                     TYPE_PRECISION (type)), 0))
4786    (convert @0)))
4789 /* (X /[ex] A) * A -> X.  */
4790 (simplify
4791   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4792   (convert @0))
4794 /* Simplify (A / B) * B + (A % B) -> A.  */
4795 (for div (trunc_div ceil_div floor_div round_div)
4796      mod (trunc_mod ceil_mod floor_mod round_mod)
4797   (simplify
4798    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4799    @0))
4801 /* x / y * y == x -> x % y == 0.  */
4802 (simplify
4803   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4804   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4805     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4807 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4808 (for op (plus minus)
4809  (simplify
4810   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4811   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4812        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4813    (with
4814      {
4815        wi::overflow_type overflow;
4816        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4817                                TYPE_SIGN (type), &overflow);
4818      }
4819      (if (types_match (type, TREE_TYPE (@2))
4820          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4821       (op @0 { wide_int_to_tree (type, mul); })
4822       (with { tree utype = unsigned_type_for (type); }
4823        (convert (op (convert:utype @0)
4824                     (mult (convert:utype @1) (convert:utype @2))))))))))
4826 /* Canonicalization of binary operations.  */
4828 /* Convert X + -C into X - C.  */
4829 (simplify
4830  (plus @0 REAL_CST@1)
4831  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4832   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4833    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4834     (minus @0 { tem; })))))
4836 /* Convert x+x into x*2.  */
4837 (simplify
4838  (plus @0 @0)
4839  (if (SCALAR_FLOAT_TYPE_P (type))
4840   (mult @0 { build_real (type, dconst2); })
4841   (if (INTEGRAL_TYPE_P (type))
4842    (mult @0 { build_int_cst (type, 2); }))))
4844 /* 0 - X  ->  -X.  */
4845 (simplify
4846  (minus integer_zerop @1)
4847  (negate @1))
4848 (simplify
4849  (pointer_diff integer_zerop @1)
4850  (negate (convert @1)))
4852 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4853    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4854    (-ARG1 + ARG0) reduces to -ARG1.  */
4855 (simplify
4856  (minus real_zerop@0 @1)
4857  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4858   (negate @1)))
4860 /* Transform x * -1 into -x.  */
4861 (simplify
4862  (mult @0 integer_minus_onep)
4863  (negate @0))
4865 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4866    signed overflow for CST != 0 && CST != -1.  */
4867 (simplify
4868  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4869  (if (TREE_CODE (@2) != INTEGER_CST
4870       && single_use (@3)
4871       && !integer_zerop (@1) && !integer_minus_onep (@1))
4872   (mult (mult @0 @2) @1)))
4874 /* True if we can easily extract the real and imaginary parts of a complex
4875    number.  */
4876 (match compositional_complex
4877  (convert? (complex @0 @1)))
4879 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4880 (simplify
4881  (complex (realpart @0) (imagpart @0))
4882  @0)
4883 (simplify
4884  (realpart (complex @0 @1))
4885  @0)
4886 (simplify
4887  (imagpart (complex @0 @1))
4888  @1)
4890 /* Sometimes we only care about half of a complex expression.  */
4891 (simplify
4892  (realpart (convert?:s (conj:s @0)))
4893  (convert (realpart @0)))
4894 (simplify
4895  (imagpart (convert?:s (conj:s @0)))
4896  (convert (negate (imagpart @0))))
4897 (for part (realpart imagpart)
4898  (for op (plus minus)
4899   (simplify
4900    (part (convert?:s@2 (op:s @0 @1)))
4901    (convert (op (part @0) (part @1))))))
4902 (simplify
4903  (realpart (convert?:s (CEXPI:s @0)))
4904  (convert (COS @0)))
4905 (simplify
4906  (imagpart (convert?:s (CEXPI:s @0)))
4907  (convert (SIN @0)))
4909 /* conj(conj(x)) -> x  */
4910 (simplify
4911  (conj (convert? (conj @0)))
4912  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4913   (convert @0)))
4915 /* conj({x,y}) -> {x,-y}  */
4916 (simplify
4917  (conj (convert?:s (complex:s @0 @1)))
4918  (with { tree itype = TREE_TYPE (type); }
4919   (complex (convert:itype @0) (negate (convert:itype @1)))))
4921 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4922 (for bswap (BSWAP)
4923  (simplify
4924   (bswap (bswap @0))
4925   @0)
4926  (simplify
4927   (bswap (bit_not (bswap @0)))
4928   (bit_not @0))
4929  (for bitop (bit_xor bit_ior bit_and)
4930   (simplify
4931    (bswap (bitop:c (bswap @0) @1))
4932    (bitop @0 (bswap @1))))
4933  (for cmp (eq ne)
4934   (simplify
4935    (cmp (bswap@2 @0) (bswap @1))
4936    (with { tree ctype = TREE_TYPE (@2); }
4937     (cmp (convert:ctype @0) (convert:ctype @1))))
4938   (simplify
4939    (cmp (bswap @0) INTEGER_CST@1)
4940    (with { tree ctype = TREE_TYPE (@1); }
4941     (cmp (convert:ctype @0) (bswap! @1)))))
4942  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4943  (simplify
4944   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4945            INTEGER_CST@3)
4946    (if (BITS_PER_UNIT == 8
4947         && tree_fits_uhwi_p (@2)
4948         && tree_fits_uhwi_p (@3))
4949     (with
4950      {
4951       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4952       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4953       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4954       unsigned HOST_WIDE_INT lo = bits & 7;
4955       unsigned HOST_WIDE_INT hi = bits - lo;
4956      }
4957      (if (bits < prec
4958           && mask < (256u>>lo)
4959           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4960       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4961        (if (ns == 0)
4962         (bit_and (convert @1) @3)
4963         (with
4964          {
4965           tree utype = unsigned_type_for (TREE_TYPE (@1));
4966           tree nst = build_int_cst (integer_type_node, ns);
4967          }
4968          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4969  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4970  (simplify
4971   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4972    (if (BITS_PER_UNIT == 8
4973         && CHAR_TYPE_SIZE == 8
4974         && tree_fits_uhwi_p (@1))
4975     (with
4976      {
4977       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4978       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4979       /* If the bswap was extended before the original shift, this
4980          byte (shift) has the sign of the extension, not the sign of
4981          the original shift.  */
4982       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4983      }
4984      /* Special case: logical right shift of sign-extended bswap.
4985         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4986      (if (TYPE_PRECISION (type) > prec
4987           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4988           && TYPE_UNSIGNED (type)
4989           && bits < prec && bits + 8 >= prec)
4990       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4991        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4992       (if (bits + 8 == prec)
4993        (if (TYPE_UNSIGNED (st))
4994         (convert (convert:unsigned_char_type_node @0))
4995         (convert (convert:signed_char_type_node @0)))
4996        (if (bits < prec && bits + 8 > prec)
4997         (with 
4998          {
4999           tree nst = build_int_cst (integer_type_node, bits & 7);
5000           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5001                                        : signed_char_type_node;
5002          }
5003          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5004  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5005  (simplify
5006   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5007    (if (BITS_PER_UNIT == 8
5008         && tree_fits_uhwi_p (@1)
5009         && tree_to_uhwi (@1) < 256)
5010     (with
5011      {
5012       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5013       tree utype = unsigned_type_for (TREE_TYPE (@0));
5014       tree nst = build_int_cst (integer_type_node, prec - 8);
5015      }
5016      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5019 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5021 /* Simplify constant conditions.
5022    Only optimize constant conditions when the selected branch
5023    has the same type as the COND_EXPR.  This avoids optimizing
5024    away "c ? x : throw", where the throw has a void type.
5025    Note that we cannot throw away the fold-const.cc variant nor
5026    this one as we depend on doing this transform before possibly
5027    A ? B : B -> B triggers and the fold-const.cc one can optimize
5028    0 ? A : B to B even if A has side-effects.  Something
5029    genmatch cannot handle.  */
5030 (simplify
5031  (cond INTEGER_CST@0 @1 @2)
5032  (if (integer_zerop (@0))
5033   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5034    @2)
5035   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5036    @1)))
5037 (simplify
5038  (vec_cond VECTOR_CST@0 @1 @2)
5039  (if (integer_all_onesp (@0))
5040   @1
5041   (if (integer_zerop (@0))
5042    @2)))
5044 /* Sink unary operations to branches, but only if we do fold both.  */
5045 (for op (negate bit_not abs absu)
5046  (simplify
5047   (op (vec_cond:s @0 @1 @2))
5048   (vec_cond @0 (op! @1) (op! @2))))
5050 /* Sink unary conversions to branches, but only if we do fold both
5051    and the target's truth type is the same as we already have.  */
5052 (simplify
5053  (convert (vec_cond:s @0 @1 @2))
5054  (if (VECTOR_TYPE_P (type)
5055       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5056   (vec_cond @0 (convert! @1) (convert! @2))))
5058 /* Likewise for view_convert of nop_conversions. */
5059 (simplify
5060  (view_convert (vec_cond:s @0 @1 @2))
5061  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5062       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5063                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5064       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5065   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5067 /* Sink binary operation to branches, but only if we can fold it.  */
5068 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5069          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5070          trunc_mod ceil_mod floor_mod round_mod min max)
5071 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5072  (simplify
5073   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5074   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5076 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5077  (simplify
5078   (op (vec_cond:s @0 @1 @2) @3)
5079   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5080  (simplify
5081   (op @3 (vec_cond:s @0 @1 @2))
5082   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5084 #if GIMPLE
5085 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5086  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5087            INTEGER_CST@1)
5088  (with {
5089          int ibit = tree_log2 (@0);
5090          int ibit2 = tree_log2 (@1);
5091        }
5092   (if (ibit == ibit2
5093       && ibit >= 0
5094       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5096 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5097  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5098           INTEGER_CST@1)
5099  (with {
5100          int ibit = tree_log2 (@0);
5101          int ibit2 = tree_log2 (@1);
5102        }
5103   (if (ibit == ibit2
5104       && ibit >= 0
5105       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5107 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5108  (bit_and:c
5109   (convert1?@4
5110    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5111   (convert2? @0))
5112  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5114 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5115  (bit_and:c
5116   (convert1?@4
5117    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5118   (convert2? @0))
5119  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5121 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5122  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5123             INTEGER_CST@1)
5124  (with {
5125          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5126                                               TYPE_PRECISION(type)));
5127          int ibit2 = tree_log2 (@1);
5128        }
5129   (if (ibit == ibit2
5130       && ibit >= 0
5131       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5133 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5134  (bit_and@4
5135   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5136   INTEGER_CST@1)
5137  (with {
5138          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5139                                               TYPE_PRECISION(type)));
5140          int ibit2 = tree_log2 (@1);
5141        }
5142   (if (ibit == ibit2
5143       && ibit >= 0
5144       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5146 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5147  (bit_and:c
5148   (convert1?@3
5149    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5150   (convert2? @0))
5151  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5153 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5154  (bit_and:c
5155   (convert1?@3
5156    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5157   (convert2? @0))
5158   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5160 #endif
5162 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5163    Currently disabled after pass lvec because ARM understands
5164    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5165 #if GIMPLE
5166 /* These can only be done in gimple as fold likes to convert:
5167    (CMP) & N into (CMP) ? N : 0
5168    and we try to match the same pattern again and again. */
5169 (simplify
5170  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5171  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5172   (vec_cond (bit_and @0 @3) @1 @2)))
5173 (simplify
5174  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5175  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5176   (vec_cond (bit_ior @0 @3) @1 @2)))
5177 (simplify
5178  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5179  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5180   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5181 (simplify
5182  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5183  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5184   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5186 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5187 (simplify
5188  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5189  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5190   (vec_cond (bit_and @0 @1) @2 @3)))
5191 (simplify
5192  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5193  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5194   (vec_cond (bit_ior @0 @1) @2 @3)))
5195 (simplify
5196  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5197  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5198   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5199 (simplify
5200  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5201  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5202   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5203 #endif
5205 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5206    types are compatible.  */
5207 (simplify
5208  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5209  (if (VECTOR_BOOLEAN_TYPE_P (type)
5210       && types_match (type, TREE_TYPE (@0)))
5211   (if (integer_zerop (@1) && integer_all_onesp (@2))
5212    (bit_not @0)
5213    (if (integer_all_onesp (@1) && integer_zerop (@2))
5214     @0))))
5216 /* A few simplifications of "a ? CST1 : CST2". */
5217 /* NOTE: Only do this on gimple as the if-chain-to-switch
5218    optimization depends on the gimple to have if statements in it. */
5219 #if GIMPLE
5220 (simplify
5221  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5222  (switch
5223   (if (integer_zerop (@2))
5224    (switch
5225     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5226     (if (integer_onep (@1))
5227      (convert (convert:boolean_type_node @0)))
5228     /* a ? -1 : 0 -> -a.  */
5229     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5230      (if (TYPE_PRECISION (type) == 1)
5231       /* For signed 1-bit precision just cast bool to the type.  */
5232       (convert (convert:boolean_type_node @0))
5233       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5234        (with {
5235           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5236                                                       TYPE_UNSIGNED (type));
5237         }
5238         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5239        (negate (convert:type (convert:boolean_type_node @0))))))
5240     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5241     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5242      (with {
5243        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5244       }
5245       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5246   (if (integer_zerop (@1))
5247    (switch
5248     /* a ? 0 : 1 -> !a.  */
5249     (if (integer_onep (@2))
5250      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5251     /* a ? 0 : -1 -> -(!a).  */
5252     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5253      (if (TYPE_PRECISION (type) == 1)
5254       /* For signed 1-bit precision just cast bool to the type.  */
5255       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5256       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5257        (with {
5258           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5259                                                       TYPE_UNSIGNED (type));
5260         }
5261         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5262                                                 { boolean_true_node; })))))
5263        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5264                                       { boolean_true_node; }))))))
5265     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5266     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5267      (with {
5268        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5269       }
5270       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5271                                 { boolean_true_node; })) { shift; })))))))
5273 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5274    for unsigned types. */
5275 (simplify
5276  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5277  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5278       && bitwise_equal_p (@0, @2))
5279   (convert (eq @0 @1))
5283 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5284    for unsigned types. */
5285 (simplify
5286  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5287  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5288       && bitwise_equal_p (@0, @2))
5289   (convert (eq @0 @1))
5293 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5294    on the first bit of the CST.  */
5295 (simplify
5296  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5297  (if ((wi::to_wide (@1) & 1) != 0)
5298   @2
5299   { build_zero_cst (type); }))
5301 /* Optimize
5302    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5303    x_5 == cstN ? cst4 : cst3
5304    # op is == or != and N is 1 or 2
5305    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5306    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5307    of cst3 and cst4 is smaller.
5308    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5309 (for eqne (ne eq)
5310  (simplify
5311   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5312   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5313        && INTEGRAL_TYPE_P (type)
5314        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5315            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5316    (with {
5317      value_range r;
5318      get_range_query (cfun)->range_of_expr (r, @0);
5319      if (r.undefined_p ())
5320        r.set_varying (TREE_TYPE (@0));
5322      wide_int min = r.lower_bound ();
5323      wide_int max = r.upper_bound ();
5324     }
5325     (if (min + 1 == max
5326          && (wi::to_wide (@1) == min
5327              || wi::to_wide (@1) == max))
5328      (with {
5329        tree arg0 = @2, arg1 = @3;
5330        tree type1;
5331        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5332          std::swap (arg0, arg1);
5333        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5334          type1 = TREE_TYPE (@0);
5335        else
5336          type1 = type;
5337        auto prec = TYPE_PRECISION (type1);
5338        auto unsign = TYPE_UNSIGNED (type1);
5339        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5340         type1 = build_nonstandard_integer_type (prec, unsign);
5341        min = wide_int::from (min, prec,
5342                              TYPE_SIGN (TREE_TYPE (@0)));
5343        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5344                                     TYPE_SIGN (type));
5345        enum tree_code code;
5346        wi::overflow_type ovf;
5347        if (tree_int_cst_lt (arg0, arg1))
5348          {
5349            code = PLUS_EXPR;
5350            a -= min;
5351            if (!unsign)
5352              {
5353                /* lhs is known to be in range [min, min+1] and we want to add a
5354                   to it.  Check if that operation can overflow for those 2 values
5355                   and if yes, force unsigned type.  */
5356                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5357                if (ovf)
5358                  type1 = unsigned_type_for (type1);
5359              }
5360          }
5361        else
5362          {
5363            code = MINUS_EXPR;
5364            a += min;
5365            if (!unsign)
5366              {
5367                /* lhs is known to be in range [min, min+1] and we want to subtract
5368                   it from a.  Check if that operation can overflow for those 2
5369                   values and if yes, force unsigned type.  */
5370                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5371                if (ovf)
5372                 type1 = unsigned_type_for (type1);
5373              }
5374          }
5375        tree arg = wide_int_to_tree (type1, a);
5376       }
5377       (if (code == PLUS_EXPR)
5378        (convert (plus (convert:type1 @0) { arg; }))
5379        (convert (minus { arg; } (convert:type1 @0))))))))))
5380 #endif
5382 (simplify
5383  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5384  (if (INTEGRAL_TYPE_P (type)
5385       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5386   (cond @1 (convert @2) (convert @3))))
5388 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5389    be extended.  */
5390 /* This pattern implements two kinds simplification:
5392    Case 1)
5393    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5394      1) Conversions are type widening from smaller type.
5395      2) Const c1 equals to c2 after canonicalizing comparison.
5396      3) Comparison has tree code LT, LE, GT or GE.
5397    This specific pattern is needed when (cmp (convert x) c) may not
5398    be simplified by comparison patterns because of multiple uses of
5399    x.  It also makes sense here because simplifying across multiple
5400    referred var is always benefitial for complicated cases.
5402    Case 2)
5403    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5404 (for cmp (lt le gt ge eq ne)
5405  (simplify
5406   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5407   (with
5408    {
5409      tree from_type = TREE_TYPE (@1);
5410      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5411      enum tree_code code = ERROR_MARK;
5413      if (INTEGRAL_TYPE_P (from_type)
5414          && int_fits_type_p (@2, from_type)
5415          && (types_match (c1_type, from_type)
5416              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5417                  && (TYPE_UNSIGNED (from_type)
5418                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5419          && (types_match (c2_type, from_type)
5420              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5421                  && (TYPE_UNSIGNED (from_type)
5422                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5423        {
5424          if (cmp != EQ_EXPR)
5425            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5426          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5427          else if (int_fits_type_p (@3, from_type))
5428            code = EQ_EXPR;
5429        }
5430    }
5431    (if (code == MAX_EXPR)
5432     (convert (max @1 (convert @2)))
5433     (if (code == MIN_EXPR)
5434      (convert (min @1 (convert @2)))
5435      (if (code == EQ_EXPR)
5436       (convert (cond (eq @1 (convert @3))
5437                      (convert:from_type @3) (convert:from_type @2)))))))))
5439 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5441      1) OP is PLUS or MINUS.
5442      2) CMP is LT, LE, GT or GE.
5443      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5445    This pattern also handles special cases like:
5447      A) Operand x is a unsigned to signed type conversion and c1 is
5448         integer zero.  In this case,
5449           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5450           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5451      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5452         check equality for (c1+1) and (c1-1) by adjusting comparison
5453         code.
5455    TODO: Though signed type is handled by this pattern, it cannot be
5456    simplified at the moment because C standard requires additional
5457    type promotion.  In order to match&simplify it here, the IR needs
5458    to be cleaned up by other optimizers, i.e, VRP.  */
5459 (for op (plus minus)
5460  (for cmp (lt le gt ge)
5461   (simplify
5462    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5463    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5464     (if (types_match (from_type, to_type)
5465          /* Check if it is special case A).  */
5466          || (TYPE_UNSIGNED (from_type)
5467              && !TYPE_UNSIGNED (to_type)
5468              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5469              && integer_zerop (@1)
5470              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5471      (with
5472       {
5473         wi::overflow_type overflow = wi::OVF_NONE;
5474         enum tree_code code, cmp_code = cmp;
5475         wide_int real_c1;
5476         wide_int c1 = wi::to_wide (@1);
5477         wide_int c2 = wi::to_wide (@2);
5478         wide_int c3 = wi::to_wide (@3);
5479         signop sgn = TYPE_SIGN (from_type);
5481         /* Handle special case A), given x of unsigned type:
5482             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5483             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5484         if (!types_match (from_type, to_type))
5485           {
5486             if (cmp_code == LT_EXPR)
5487               cmp_code = GT_EXPR;
5488             if (cmp_code == GE_EXPR)
5489               cmp_code = LE_EXPR;
5490             c1 = wi::max_value (to_type);
5491           }
5492         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5493            compute (c3 op' c2) and check if it equals to c1 with op' being
5494            the inverted operator of op.  Make sure overflow doesn't happen
5495            if it is undefined.  */
5496         if (op == PLUS_EXPR)
5497           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5498         else
5499           real_c1 = wi::add (c3, c2, sgn, &overflow);
5501         code = cmp_code;
5502         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5503           {
5504             /* Check if c1 equals to real_c1.  Boundary condition is handled
5505                by adjusting comparison operation if necessary.  */
5506             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5507                 && !overflow)
5508               {
5509                 /* X <= Y - 1 equals to X < Y.  */
5510                 if (cmp_code == LE_EXPR)
5511                   code = LT_EXPR;
5512                 /* X > Y - 1 equals to X >= Y.  */
5513                 if (cmp_code == GT_EXPR)
5514                   code = GE_EXPR;
5515               }
5516             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5517                 && !overflow)
5518               {
5519                 /* X < Y + 1 equals to X <= Y.  */
5520                 if (cmp_code == LT_EXPR)
5521                   code = LE_EXPR;
5522                 /* X >= Y + 1 equals to X > Y.  */
5523                 if (cmp_code == GE_EXPR)
5524                   code = GT_EXPR;
5525               }
5526             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5527               {
5528                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5529                   code = MIN_EXPR;
5530                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5531                   code = MAX_EXPR;
5532               }
5533           }
5534       }
5535       (if (code == MAX_EXPR)
5536        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5537            { wide_int_to_tree (from_type, c2); })
5538        (if (code == MIN_EXPR)
5539         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5540             { wide_int_to_tree (from_type, c2); })))))))))
5542 #if GIMPLE
5543 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5544    in fold_cond_expr_with_comparison for GENERIC folding with
5545    some extra constraints.  */
5546 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5547  (simplify
5548   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5549         (convert3? @0) (convert4? @1))
5550   (if (!HONOR_SIGNED_ZEROS (type)
5551        && (/* Allow widening conversions of the compare operands as data.  */
5552            (INTEGRAL_TYPE_P (type)
5553             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5554             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5555             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5556             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5557            /* Or sign conversions for the comparison.  */
5558            || (types_match (type, TREE_TYPE (@0))
5559                && types_match (type, TREE_TYPE (@1)))))
5560    (switch
5561     (if (cmp == EQ_EXPR)
5562      (if (VECTOR_TYPE_P (type))
5563       (view_convert @c1)
5564       (convert @c1)))
5565     (if (cmp == NE_EXPR)
5566      (if (VECTOR_TYPE_P (type))
5567       (view_convert @c0)
5568       (convert @c0)))
5569     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5570      (if (!HONOR_NANS (type))
5571       (if (VECTOR_TYPE_P (type))
5572        (view_convert (min @c0 @c1))
5573        (convert (min @c0 @c1)))))
5574     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5575      (if (!HONOR_NANS (type))
5576       (if (VECTOR_TYPE_P (type))
5577        (view_convert (max @c0 @c1))
5578        (convert (max @c0 @c1)))))
5579     (if (cmp == UNEQ_EXPR)
5580      (if (!HONOR_NANS (type))
5581       (if (VECTOR_TYPE_P (type))
5582        (view_convert @c1)
5583        (convert @c1))))
5584     (if (cmp == LTGT_EXPR)
5585      (if (!HONOR_NANS (type))
5586       (if (VECTOR_TYPE_P (type))
5587        (view_convert @c0)
5588        (convert @c0))))))))
5589 #endif
5591 (for cnd (cond vec_cond)
5592  /* (a != b) ? (a - b) : 0 -> (a - b) */
5593  (simplify
5594   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5595   @2)
5596  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5597  (simplify
5598   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5599   @2)
5600  /* (a != b) ? (a & b) : a -> (a & b) */
5601  /* (a != b) ? (a | b) : a -> (a | b) */
5602  /* (a != b) ? min(a,b) : a -> min(a,b) */
5603  /* (a != b) ? max(a,b) : a -> max(a,b) */
5604  (for op (bit_and bit_ior min max)
5605   (simplify
5606    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5607    @2))
5608  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5609  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5610  (for op (mult plus)
5611   (simplify
5612    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5613    (if (ANY_INTEGRAL_TYPE_P (type))
5614     @2)))
5615  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5616  (simplify
5617   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5618   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5619    @2))
5622 /* These was part of minmax phiopt.  */
5623 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5624    to minmax<min/max<a, b>, c> */
5625 (for minmax (min max)
5626  (for cmp (lt le gt ge ne)
5627   (simplify
5628    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5629    (with
5630     {
5631       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5632     }
5633     (if (code == MIN_EXPR)
5634      (minmax (min @1 @2) @4)
5635      (if (code == MAX_EXPR)
5636       (minmax (max @1 @2) @4)))))))
5638 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5639 (for cmp    (gt  ge  lt  le)
5640      minmax (min min max max)
5641  (simplify
5642   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5643    (with
5644     {
5645       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5646     }
5647     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5648          && code == MIN_EXPR
5649          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5650      (min @2 @4)
5651      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5652           && code == MAX_EXPR
5653           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5654       (max @2 @4))))))
5656 #if GIMPLE
5657 /* These patterns should be after min/max detection as simplifications
5658    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5659    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5660    Even without those, reaching min/max/and/ior faster is better.  */
5661 (simplify
5662  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5663  (switch
5664   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5665   (if (integer_zerop (@2))
5666    (bit_and (convert @0) @1))
5667   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5668   (if (integer_zerop (@1))
5669    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5670   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5671   (if (integer_onep (@1))
5672    (bit_ior (convert @0) @2))
5673   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5674   (if (integer_onep (@2))
5675    (bit_ior (bit_xor (convert @0) @2) @1))
5678 #endif
5680 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5681 (simplify
5682  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5683  (if (!TYPE_SATURATING (type)
5684       && (TYPE_OVERFLOW_WRAPS (type)
5685           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5686       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5687   @3))
5689 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5690 (simplify
5691  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5692  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5693   @3))
5695 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5696 (for op (abs absu)
5697  (simplify
5698   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5699   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5700    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5701     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5702      (convert (absu:utype @0)))
5703     @3))))
5705 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5706    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5707 (simplify
5708  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5709  (if (TYPE_UNSIGNED (type))
5710   (cond (ge @0 @1) (negate @0) @2)))
5712 (for cnd (cond vec_cond)
5713  /* A ? B : (A ? X : C) -> A ? B : C.  */
5714  (simplify
5715   (cnd @0 (cnd @0 @1 @2) @3)
5716   (cnd @0 @1 @3))
5717  (simplify
5718   (cnd @0 @1 (cnd @0 @2 @3))
5719   (cnd @0 @1 @3))
5720  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5721  /* ???  This matches embedded conditions open-coded because genmatch
5722     would generate matching code for conditions in separate stmts only.
5723     The following is still important to merge then and else arm cases
5724     from if-conversion.  */
5725  (simplify
5726   (cnd @0 @1 (cnd @2 @3 @4))
5727   (if (inverse_conditions_p (@0, @2))
5728    (cnd @0 @1 @3)))
5729  (simplify
5730   (cnd @0 (cnd @1 @2 @3) @4)
5731   (if (inverse_conditions_p (@0, @1))
5732    (cnd @0 @3 @4)))
5734  /* A ? B : B -> B.  */
5735  (simplify
5736   (cnd @0 @1 @1)
5737   @1)
5739  /* !A ? B : C -> A ? C : B.  */
5740  (simplify
5741   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5742   (cnd @0 @2 @1)))
5744 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5746    None of these transformations work for modes with signed
5747    zeros.  If A is +/-0, the first two transformations will
5748    change the sign of the result (from +0 to -0, or vice
5749    versa).  The last four will fix the sign of the result,
5750    even though the original expressions could be positive or
5751    negative, depending on the sign of A.
5753    Note that all these transformations are correct if A is
5754    NaN, since the two alternatives (A and -A) are also NaNs.  */
5756 (for cnd (cond vec_cond)
5757  /* A == 0 ? A : -A    same as -A */
5758  (for cmp (eq uneq)
5759   (simplify
5760    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5761     (if (!HONOR_SIGNED_ZEROS (type)
5762          && bitwise_equal_p (@0, @2))
5763      @1))
5764   (simplify
5765    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5766     (if (!HONOR_SIGNED_ZEROS (type)
5767          && bitwise_equal_p (@0, @2))
5768      @1))
5770  /* A != 0 ? A : -A    same as A */
5771  (for cmp (ne ltgt)
5772   (simplify
5773    (cnd (cmp @0 zerop) @1 (negate @1))
5774     (if (!HONOR_SIGNED_ZEROS (type)
5775          && bitwise_equal_p (@0, @1))
5776      @1))
5777   (simplify
5778    (cnd (cmp @0 zerop) @1 integer_zerop)
5779     (if (!HONOR_SIGNED_ZEROS (type)
5780          && bitwise_equal_p (@0, @1))
5781      @1))
5783  /* A >=/> 0 ? A : -A    same as abs (A) */
5784  (for cmp (ge gt)
5785   (simplify
5786    (cnd (cmp @0 zerop) @1 (negate @1))
5787     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5788          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5789          && bitwise_equal_p (@0, @1))
5790      (if (TYPE_UNSIGNED (type))
5791       (absu:type @0)
5792       (abs @0)))))
5793  /* A <=/< 0 ? A : -A    same as -abs (A) */
5794  (for cmp (le lt)
5795   (simplify
5796    (cnd (cmp @0 zerop) @1 (negate @1))
5797     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5798          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5799          && bitwise_equal_p (@0, @1))
5800      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5801            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5802           || TYPE_UNSIGNED (type))
5803       (with {
5804         tree utype = unsigned_type_for (TREE_TYPE(@0));
5805        }
5806        (convert (negate (absu:utype @0))))
5807        (negate (abs @0)))))
5810  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5811  (for cmp (eq uneq)
5812   (simplify
5813    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5814    (if (!HONOR_SIGNED_ZEROS (type))
5815     @3))
5816   (simplify
5817    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5818    @3)
5820  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5821  (for cmp (ne ltgt)
5822   (simplify
5823    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5824    (if (!HONOR_SIGNED_ZEROS (type))
5825     @0))
5826   (simplify
5827    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5828    @0)
5830  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5831  (for cmp (ge gt)
5832   (simplify
5833    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5834    (if (!HONOR_SIGNED_ZEROS (type)
5835         && !TYPE_UNSIGNED (type))
5836     (abs @0))))
5837  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5838  (for cmp (le lt)
5839   (simplify
5840    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5841    (if (!HONOR_SIGNED_ZEROS (type)
5842         && !TYPE_UNSIGNED (type))
5843     (if (ANY_INTEGRAL_TYPE_P (type)
5844          && !TYPE_OVERFLOW_WRAPS (type))
5845      (with {
5846         tree utype = unsigned_type_for (type);
5847       }
5848       (convert (negate (absu:utype @0))))
5849       (negate (abs @0)))))
5853 /* -(type)!A -> (type)A - 1.  */
5854 (simplify
5855  (negate (convert?:s (logical_inverted_value:s @0)))
5856  (if (INTEGRAL_TYPE_P (type)
5857       && TREE_CODE (type) != BOOLEAN_TYPE
5858       && TYPE_PRECISION (type) > 1
5859       && TREE_CODE (@0) == SSA_NAME
5860       && ssa_name_has_boolean_range (@0))
5861   (plus (convert:type @0) { build_all_ones_cst (type); })))
5863 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5864    return all -1 or all 0 results.  */
5865 /* ??? We could instead convert all instances of the vec_cond to negate,
5866    but that isn't necessarily a win on its own.  */
5867 (simplify
5868  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5869  (if (VECTOR_TYPE_P (type)
5870       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5871                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5872       && (TYPE_MODE (TREE_TYPE (type))
5873           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5874   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5876 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5877 (simplify
5878  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5879  (if (VECTOR_TYPE_P (type)
5880       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5881                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5882       && (TYPE_MODE (TREE_TYPE (type))
5883           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5884   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5887 /* Simplifications of comparisons.  */
5889 /* See if we can reduce the magnitude of a constant involved in a
5890    comparison by changing the comparison code.  This is a canonicalization
5891    formerly done by maybe_canonicalize_comparison_1.  */
5892 (for cmp  (le gt)
5893      acmp (lt ge)
5894  (simplify
5895   (cmp @0 uniform_integer_cst_p@1)
5896   (with { tree cst = uniform_integer_cst_p (@1); }
5897    (if (tree_int_cst_sgn (cst) == -1)
5898      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5899                                    wide_int_to_tree (TREE_TYPE (cst),
5900                                                      wi::to_wide (cst)
5901                                                      + 1)); })))))
5902 (for cmp  (ge lt)
5903      acmp (gt le)
5904  (simplify
5905   (cmp @0 uniform_integer_cst_p@1)
5906   (with { tree cst = uniform_integer_cst_p (@1); }
5907    (if (tree_int_cst_sgn (cst) == 1)
5908     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5909                                   wide_int_to_tree (TREE_TYPE (cst),
5910                                   wi::to_wide (cst) - 1)); })))))
5912 /* We can simplify a logical negation of a comparison to the
5913    inverted comparison.  As we cannot compute an expression
5914    operator using invert_tree_comparison we have to simulate
5915    that with expression code iteration.  */
5916 (for cmp (tcc_comparison)
5917      icmp (inverted_tcc_comparison)
5918      ncmp (inverted_tcc_comparison_with_nans)
5919  /* Ideally we'd like to combine the following two patterns
5920     and handle some more cases by using
5921       (logical_inverted_value (cmp @0 @1))
5922     here but for that genmatch would need to "inline" that.
5923     For now implement what forward_propagate_comparison did.  */
5924  (simplify
5925   (bit_not (cmp @0 @1))
5926   (if (VECTOR_TYPE_P (type)
5927        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5928    /* Comparison inversion may be impossible for trapping math,
5929       invert_tree_comparison will tell us.  But we can't use
5930       a computed operator in the replacement tree thus we have
5931       to play the trick below.  */
5932    (with { enum tree_code ic = invert_tree_comparison
5933              (cmp, HONOR_NANS (@0)); }
5934     (if (ic == icmp)
5935      (icmp @0 @1)
5936      (if (ic == ncmp)
5937       (ncmp @0 @1))))))
5938  (simplify
5939   (bit_xor (cmp @0 @1) integer_truep)
5940   (with { enum tree_code ic = invert_tree_comparison
5941             (cmp, HONOR_NANS (@0)); }
5942    (if (ic == icmp)
5943     (icmp @0 @1)
5944     (if (ic == ncmp)
5945      (ncmp @0 @1)))))
5946  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5947  (simplify
5948   (ne (cmp@2 @0 @1) integer_zerop)
5949   (if (types_match (type, TREE_TYPE (@2)))
5950    (cmp @0 @1)))
5951  (simplify
5952   (eq (cmp@2 @0 @1) integer_truep)
5953   (if (types_match (type, TREE_TYPE (@2)))
5954    (cmp @0 @1)))
5955  (simplify
5956   (ne (cmp@2 @0 @1) integer_truep)
5957   (if (types_match (type, TREE_TYPE (@2)))
5958    (with { enum tree_code ic = invert_tree_comparison
5959              (cmp, HONOR_NANS (@0)); }
5960     (if (ic == icmp)
5961      (icmp @0 @1)
5962      (if (ic == ncmp)
5963       (ncmp @0 @1))))))
5964  (simplify
5965   (eq (cmp@2 @0 @1) integer_zerop)
5966   (if (types_match (type, TREE_TYPE (@2)))
5967    (with { enum tree_code ic = invert_tree_comparison
5968              (cmp, HONOR_NANS (@0)); }
5969     (if (ic == icmp)
5970      (icmp @0 @1)
5971      (if (ic == ncmp)
5972       (ncmp @0 @1)))))))
5974 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5975    ??? The transformation is valid for the other operators if overflow
5976    is undefined for the type, but performing it here badly interacts
5977    with the transformation in fold_cond_expr_with_comparison which
5978    attempts to synthetize ABS_EXPR.  */
5979 (for cmp (eq ne)
5980  (for sub (minus pointer_diff)
5981   (simplify
5982    (cmp (sub@2 @0 @1) integer_zerop)
5983    (if (single_use (@2))
5984     (cmp @0 @1)))))
5986 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5987    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5988 (for cmp (lt ge)
5989  (simplify
5990   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5991    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5992         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5993         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5994     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5995 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5996    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5997 (simplify
5998  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5999   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6000        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6001        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6002    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6004 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6005    signed arithmetic case.  That form is created by the compiler
6006    often enough for folding it to be of value.  One example is in
6007    computing loop trip counts after Operator Strength Reduction.  */
6008 (for cmp (simple_comparison)
6009      scmp (swapped_simple_comparison)
6010  (simplify
6011   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6012   /* Handle unfolded multiplication by zero.  */
6013   (if (integer_zerop (@1))
6014    (cmp @1 @2)
6015    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6016         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6017         && single_use (@3))
6018     /* If @1 is negative we swap the sense of the comparison.  */
6019     (if (tree_int_cst_sgn (@1) < 0)
6020      (scmp @0 @2)
6021      (cmp @0 @2))))))
6023 /* For integral types with undefined overflow fold
6024    x * C1 == C2 into x == C2 / C1 or false.
6025    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6026    Z / 2^n Z.  */
6027 (for cmp (eq ne)
6028  (simplify
6029   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6030   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6031        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6032        && wi::to_wide (@1) != 0)
6033    (with { widest_int quot; }
6034     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6035                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6036      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6037      { constant_boolean_node (cmp == NE_EXPR, type); }))
6038    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6039         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6040         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6041     (cmp @0
6042      {
6043        tree itype = TREE_TYPE (@0);
6044        int p = TYPE_PRECISION (itype);
6045        wide_int m = wi::one (p + 1) << p;
6046        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6047        wide_int i = wide_int::from (wi::mod_inv (a, m),
6048                                     p, TYPE_SIGN (itype));
6049        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6050      })))))
6052 /* Simplify comparison of something with itself.  For IEEE
6053    floating-point, we can only do some of these simplifications.  */
6054 (for cmp (eq ge le)
6055  (simplify
6056   (cmp @0 @0)
6057   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6058        || ! tree_expr_maybe_nan_p (@0))
6059    { constant_boolean_node (true, type); }
6060    (if (cmp != EQ_EXPR
6061         /* With -ftrapping-math conversion to EQ loses an exception.  */
6062         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6063             || ! flag_trapping_math))
6064     (eq @0 @0)))))
6065 (for cmp (ne gt lt)
6066  (simplify
6067   (cmp @0 @0)
6068   (if (cmp != NE_EXPR
6069        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6070        || ! tree_expr_maybe_nan_p (@0))
6071    { constant_boolean_node (false, type); })))
6072 (for cmp (unle unge uneq)
6073  (simplify
6074   (cmp @0 @0)
6075   { constant_boolean_node (true, type); }))
6076 (for cmp (unlt ungt)
6077  (simplify
6078   (cmp @0 @0)
6079   (unordered @0 @0)))
6080 (simplify
6081  (ltgt @0 @0)
6082  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6083   { constant_boolean_node (false, type); }))
6085 /* x == ~x -> false */
6086 /* x != ~x -> true */
6087 (for cmp (eq ne)
6088  (simplify
6089   (cmp:c @0 (bit_not @0))
6090   { constant_boolean_node (cmp == NE_EXPR, type); }))
6092 /* Fold ~X op ~Y as Y op X.  */
6093 (for cmp (simple_comparison)
6094  (simplify
6095   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6096   (if (single_use (@2) && single_use (@3))
6097    (with { tree otype = TREE_TYPE (@4); }
6098     (cmp (convert:otype @1) (convert:otype @0))))))
6100 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6101 (for cmp (simple_comparison)
6102      scmp (swapped_simple_comparison)
6103  (simplify
6104   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6105   (if (single_use (@2)
6106        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6107    (with { tree otype = TREE_TYPE (@1); }
6108     (scmp (convert:otype @0) (bit_not @1))))))
6110 (for cmp (simple_comparison)
6111  (simplify
6112   (cmp @0 REAL_CST@1)
6113   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6114   (switch
6115    /* a CMP (-0) -> a CMP 0  */
6116    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6117     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6118    /* (-0) CMP b -> 0 CMP b.  */
6119    (if (TREE_CODE (@0) == REAL_CST
6120         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6121     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6122    /* x != NaN is always true, other ops are always false.  */
6123    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6124         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6125         && !tree_expr_signaling_nan_p (@1)
6126         && !tree_expr_maybe_signaling_nan_p (@0))
6127     { constant_boolean_node (cmp == NE_EXPR, type); })
6128    /* NaN != y is always true, other ops are always false.  */
6129    (if (TREE_CODE (@0) == REAL_CST
6130         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6131         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6132         && !tree_expr_signaling_nan_p (@0)
6133         && !tree_expr_signaling_nan_p (@1))
6134     { constant_boolean_node (cmp == NE_EXPR, type); })
6135    /* Fold comparisons against infinity.  */
6136    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6137         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6138     (with
6139      {
6140        REAL_VALUE_TYPE max;
6141        enum tree_code code = cmp;
6142        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6143        if (neg)
6144          code = swap_tree_comparison (code);
6145      }
6146      (switch
6147       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6148       (if (code == GT_EXPR
6149            && !(HONOR_NANS (@0) && flag_trapping_math))
6150        { constant_boolean_node (false, type); })
6151       (if (code == LE_EXPR)
6152        /* x <= +Inf is always true, if we don't care about NaNs.  */
6153        (if (! HONOR_NANS (@0))
6154         { constant_boolean_node (true, type); }
6155         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6156            an "invalid" exception.  */
6157         (if (!flag_trapping_math)
6158          (eq @0 @0))))
6159       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6160          for == this introduces an exception for x a NaN.  */
6161       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6162            || code == GE_EXPR)
6163        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6164         (if (neg)
6165          (lt @0 { build_real (TREE_TYPE (@0), max); })
6166          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6167       /* x < +Inf is always equal to x <= DBL_MAX.  */
6168       (if (code == LT_EXPR)
6169        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6170         (if (neg)
6171          (ge @0 { build_real (TREE_TYPE (@0), max); })
6172          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6173       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6174          an exception for x a NaN so use an unordered comparison.  */
6175       (if (code == NE_EXPR)
6176        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6177         (if (! HONOR_NANS (@0))
6178          (if (neg)
6179           (ge @0 { build_real (TREE_TYPE (@0), max); })
6180           (le @0 { build_real (TREE_TYPE (@0), max); }))
6181          (if (neg)
6182           (unge @0 { build_real (TREE_TYPE (@0), max); })
6183           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6185  /* If this is a comparison of a real constant with a PLUS_EXPR
6186     or a MINUS_EXPR of a real constant, we can convert it into a
6187     comparison with a revised real constant as long as no overflow
6188     occurs when unsafe_math_optimizations are enabled.  */
6189  (if (flag_unsafe_math_optimizations)
6190   (for op (plus minus)
6191    (simplify
6192     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6193     (with
6194      {
6195        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6196                                TREE_TYPE (@1), @2, @1);
6197      }
6198      (if (tem && !TREE_OVERFLOW (tem))
6199       (cmp @0 { tem; }))))))
6201  /* Likewise, we can simplify a comparison of a real constant with
6202     a MINUS_EXPR whose first operand is also a real constant, i.e.
6203     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6204     floating-point types only if -fassociative-math is set.  */
6205  (if (flag_associative_math)
6206   (simplify
6207    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6208    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6209     (if (tem && !TREE_OVERFLOW (tem))
6210      (cmp { tem; } @1)))))
6212  /* Fold comparisons against built-in math functions.  */
6213  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6214   (for sq (SQRT)
6215    (simplify
6216     (cmp (sq @0) REAL_CST@1)
6217     (switch
6218      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6219       (switch
6220        /* sqrt(x) < y is always false, if y is negative.  */
6221        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6222         { constant_boolean_node (false, type); })
6223        /* sqrt(x) > y is always true, if y is negative and we
6224           don't care about NaNs, i.e. negative values of x.  */
6225        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6226         { constant_boolean_node (true, type); })
6227        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6228        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6229      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6230       (switch
6231        /* sqrt(x) < 0 is always false.  */
6232        (if (cmp == LT_EXPR)
6233         { constant_boolean_node (false, type); })
6234        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6235        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6236         { constant_boolean_node (true, type); })
6237        /* sqrt(x) <= 0 -> x == 0.  */
6238        (if (cmp == LE_EXPR)
6239         (eq @0 @1))
6240        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6241           == or !=.  In the last case:
6243             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6245           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6246           the results for other x follow from natural arithmetic.  */
6247        (cmp @0 @1)))
6248      (if ((cmp == LT_EXPR
6249            || cmp == LE_EXPR
6250            || cmp == GT_EXPR
6251            || cmp == GE_EXPR)
6252           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6253           /* Give up for -frounding-math.  */
6254           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6255       (with
6256        {
6257          REAL_VALUE_TYPE c2;
6258          enum tree_code ncmp = cmp;
6259          const real_format *fmt
6260            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6261          real_arithmetic (&c2, MULT_EXPR,
6262                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6263          real_convert (&c2, fmt, &c2);
6264          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6265             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6266          if (!REAL_VALUE_ISINF (c2))
6267            {
6268              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6269                                         build_real (TREE_TYPE (@0), c2));
6270              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6271                ncmp = ERROR_MARK;
6272              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6273                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6274                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6275              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6276                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6277                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6278              else
6279                {
6280                  /* With rounding to even, sqrt of up to 3 different values
6281                     gives the same normal result, so in some cases c2 needs
6282                     to be adjusted.  */
6283                  REAL_VALUE_TYPE c2alt, tow;
6284                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6285                    tow = dconst0;
6286                  else
6287                    tow = dconstinf;
6288                  real_nextafter (&c2alt, fmt, &c2, &tow);
6289                  real_convert (&c2alt, fmt, &c2alt);
6290                  if (REAL_VALUE_ISINF (c2alt))
6291                    ncmp = ERROR_MARK;
6292                  else
6293                    {
6294                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6295                                            build_real (TREE_TYPE (@0), c2alt));
6296                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6297                        ncmp = ERROR_MARK;
6298                      else if (real_equal (&TREE_REAL_CST (c3),
6299                                           &TREE_REAL_CST (@1)))
6300                        c2 = c2alt;
6301                    }
6302                }
6303            }
6304        }
6305        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6306         (if (REAL_VALUE_ISINF (c2))
6307          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6308          (if (HONOR_INFINITIES (@0))
6309           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6310           { constant_boolean_node (false, type); })
6311          /* sqrt(x) > c is the same as x > c*c.  */
6312          (if (ncmp != ERROR_MARK)
6313           (if (ncmp == GE_EXPR)
6314            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6315            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6316         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6317         (if (REAL_VALUE_ISINF (c2))
6318          (switch
6319           /* sqrt(x) < y is always true, when y is a very large
6320              value and we don't care about NaNs or Infinities.  */
6321           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6322            { constant_boolean_node (true, type); })
6323           /* sqrt(x) < y is x != +Inf when y is very large and we
6324              don't care about NaNs.  */
6325           (if (! HONOR_NANS (@0))
6326            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6327           /* sqrt(x) < y is x >= 0 when y is very large and we
6328              don't care about Infinities.  */
6329           (if (! HONOR_INFINITIES (@0))
6330            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6331           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6332           (if (GENERIC)
6333            (truth_andif
6334             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6335             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6336          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6337          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6338           (if (ncmp == LT_EXPR)
6339            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6340            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6341           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6342           (if (ncmp != ERROR_MARK && GENERIC)
6343            (if (ncmp == LT_EXPR)
6344             (truth_andif
6345              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6346              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6347             (truth_andif
6348              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6349              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6350    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6351    (simplify
6352     (cmp (sq @0) (sq @1))
6353       (if (! HONOR_NANS (@0))
6354         (cmp @0 @1))))))
6356 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6357 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6358      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6359  (simplify
6360   (cmp (float@0 @1) (float @2))
6361    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6362         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6363     (with
6364      {
6365        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6366        tree type1 = TREE_TYPE (@1);
6367        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6368        tree type2 = TREE_TYPE (@2);
6369        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6370      }
6371      (if (fmt.can_represent_integral_type_p (type1)
6372           && fmt.can_represent_integral_type_p (type2))
6373       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6374        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6375        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6376             && type1_signed_p >= type2_signed_p)
6377         (icmp @1 (convert @2))
6378         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6379              && type1_signed_p <= type2_signed_p)
6380          (icmp (convert:type2 @1) @2)
6381          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6382               && type1_signed_p == type2_signed_p)
6383           (icmp @1 @2))))))))))
6385 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6386 (for cmp  (lt le eq ne ge gt)
6387      icmp (le le eq ne ge ge)
6388  (simplify
6389   (cmp (float @0) REAL_CST@1)
6390    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6391         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6392     (with
6393      {
6394        tree itype = TREE_TYPE (@0);
6395        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6396        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6397        /* Be careful to preserve any potential exceptions due to
6398           NaNs.  qNaNs are ok in == or != context.
6399           TODO: relax under -fno-trapping-math or
6400           -fno-signaling-nans.  */
6401        bool exception_p
6402          = real_isnan (cst) && (cst->signalling
6403                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6404      }
6405      /* TODO: allow non-fitting itype and SNaNs when
6406         -fno-trapping-math.  */
6407      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6408       (with
6409        {
6410          signop isign = TYPE_SIGN (itype);
6411          REAL_VALUE_TYPE imin, imax;
6412          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6413          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6415          REAL_VALUE_TYPE icst;
6416          if (cmp == GT_EXPR || cmp == GE_EXPR)
6417            real_ceil (&icst, fmt, cst);
6418          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6419            real_floor (&icst, fmt, cst);
6420          else
6421            real_trunc (&icst, fmt, cst);
6423          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6425          bool overflow_p = false;
6426          wide_int icst_val
6427            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6428        }
6429        (switch
6430         /* Optimize cases when CST is outside of ITYPE's range.  */
6431         (if (real_compare (LT_EXPR, cst, &imin))
6432          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6433                                   type); })
6434         (if (real_compare (GT_EXPR, cst, &imax))
6435          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6436                                   type); })
6437         /* Remove cast if CST is an integer representable by ITYPE.  */
6438         (if (cst_int_p)
6439          (cmp @0 { gcc_assert (!overflow_p);
6440                    wide_int_to_tree (itype, icst_val); })
6441         )
6442         /* When CST is fractional, optimize
6443             (FTYPE) N == CST -> 0
6444             (FTYPE) N != CST -> 1.  */
6445         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6446          { constant_boolean_node (cmp == NE_EXPR, type); })
6447         /* Otherwise replace with sensible integer constant.  */
6448         (with
6449          {
6450            gcc_checking_assert (!overflow_p);
6451          }
6452          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6454 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6455 (for cmp (eq ne)
6456  (simplify
6457   (cmp (exact_div @0 @1) INTEGER_CST@2)
6458   (if (!integer_zerop (@1))
6459    (if (wi::to_wide (@2) == 0)
6460     (cmp @0 @2)
6461     (if (TREE_CODE (@1) == INTEGER_CST)
6462      (with
6463       {
6464         wi::overflow_type ovf;
6465         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6466                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6467       }
6468       (if (ovf)
6469        { constant_boolean_node (cmp == NE_EXPR, type); }
6470        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6471 (for cmp (lt le gt ge)
6472  (simplify
6473   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6474   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6475    (with
6476     {
6477       wi::overflow_type ovf;
6478       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6479                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6480     }
6481     (if (ovf)
6482      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6483                                         TYPE_SIGN (TREE_TYPE (@2)))
6484                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6485      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6487 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6489    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6490    For large C (more than min/B+2^size), this is also true, with the
6491    multiplication computed modulo 2^size.
6492    For intermediate C, this just tests the sign of A.  */
6493 (for cmp  (lt le gt ge)
6494      cmp2 (ge ge lt lt)
6495  (simplify
6496   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6497   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6498        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6499        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6500    (with
6501     {
6502       tree utype = TREE_TYPE (@2);
6503       wide_int denom = wi::to_wide (@1);
6504       wide_int right = wi::to_wide (@2);
6505       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6506       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6507       bool small = wi::leu_p (right, smax);
6508       bool large = wi::geu_p (right, smin);
6509     }
6510     (if (small || large)
6511      (cmp (convert:utype @0) (mult @2 (convert @1)))
6512      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6514 /* Unordered tests if either argument is a NaN.  */
6515 (simplify
6516  (bit_ior (unordered @0 @0) (unordered @1 @1))
6517  (if (types_match (@0, @1))
6518   (unordered @0 @1)))
6519 (simplify
6520  (bit_and (ordered @0 @0) (ordered @1 @1))
6521  (if (types_match (@0, @1))
6522   (ordered @0 @1)))
6523 (simplify
6524  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6525  @2)
6526 (simplify
6527  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6528  @2)
6530 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6531    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6533    Note that comparisons
6534      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6535      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6536    will be canonicalized to above so there's no need to
6537    consider them here.
6538  */
6540 (for cmp (le gt)
6541      eqcmp (eq ne)
6542  (simplify
6543   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6544   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6545    (with
6546     {
6547      tree ty = TREE_TYPE (@0);
6548      unsigned prec = TYPE_PRECISION (ty);
6549      wide_int mask = wi::to_wide (@2, prec);
6550      wide_int rhs = wi::to_wide (@3, prec);
6551      signop sgn = TYPE_SIGN (ty);
6552     }
6553     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6554          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6555       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6556              { build_zero_cst (ty); }))))))
6558 /* -A CMP -B -> B CMP A.  */
6559 (for cmp (tcc_comparison)
6560      scmp (swapped_tcc_comparison)
6561  (simplify
6562   (cmp (negate @0) (negate @1))
6563   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6564        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6565            && (cmp == EQ_EXPR
6566                || cmp == NE_EXPR
6567                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6568    (scmp @0 @1)))
6569  (simplify
6570   (cmp (negate @0) CONSTANT_CLASS_P@1)
6571   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6572        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6573            && (cmp == EQ_EXPR
6574                || cmp == NE_EXPR
6575                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6576    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6577     (if (tem && !TREE_OVERFLOW (tem))
6578      (scmp @0 { tem; }))))))
6580 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6581 (for op (abs absu)
6582  (for eqne (eq ne)
6583   (simplify
6584    (eqne (op @0) zerop@1)
6585    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6587 /* From fold_sign_changed_comparison and fold_widened_comparison.
6588    FIXME: the lack of symmetry is disturbing.  */
6589 (for cmp (simple_comparison)
6590  (simplify
6591   (cmp (convert@0 @00) (convert?@1 @10))
6592   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6593        /* Disable this optimization if we're casting a function pointer
6594           type on targets that require function pointer canonicalization.  */
6595        && !(targetm.have_canonicalize_funcptr_for_compare ()
6596             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6597                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6598                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6599                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6600        && single_use (@0))
6601    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6602         && (TREE_CODE (@10) == INTEGER_CST
6603             || @1 != @10)
6604         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6605             || cmp == NE_EXPR
6606             || cmp == EQ_EXPR)
6607         && !POINTER_TYPE_P (TREE_TYPE (@00))
6608         /* (int)bool:32 != (int)uint is not the same as
6609            bool:32 != (bool:32)uint since boolean types only have two valid
6610            values independent of their precision.  */
6611         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6612             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6613     /* ???  The special-casing of INTEGER_CST conversion was in the original
6614        code and here to avoid a spurious overflow flag on the resulting
6615        constant which fold_convert produces.  */
6616     (if (TREE_CODE (@1) == INTEGER_CST)
6617      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6618                                 wide_int::from (wi::to_wide (@1),
6619                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6620                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6621                                                 TYPE_SIGN (TREE_TYPE (@1))),
6622                                 0, TREE_OVERFLOW (@1)); })
6623      (cmp @00 (convert @1)))
6625     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6626      /* If possible, express the comparison in the shorter mode.  */
6627      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6628            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6629            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6630                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6631           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6632               || ((TYPE_PRECISION (TREE_TYPE (@00))
6633                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6634                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6635                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6636               || (TREE_CODE (@10) == INTEGER_CST
6637                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6638                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6639       (cmp @00 (convert @10))
6640       (if (TREE_CODE (@10) == INTEGER_CST
6641            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6642            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6643        (with
6644         {
6645           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6646           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6647           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6648           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6649         }
6650         (if (above || below)
6651          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6652           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6653           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6654            { constant_boolean_node (above ? true : false, type); }
6655            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6656             { constant_boolean_node (above ? false : true, type); })))))))))
6657    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6658    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6659         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6660             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6661         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6662             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6663     (with
6664      {
6665        tree type1 = TREE_TYPE (@10);
6666        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6667          {
6668            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6669            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6670                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6671              type1 = float_type_node;
6672            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6673                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6674              type1 = double_type_node;
6675          }
6676       tree newtype
6677         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6678            ? TREE_TYPE (@00) : type1);
6679      }
6680      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6681       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6684 (for cmp (eq ne)
6685  (simplify
6686   /* SSA names are canonicalized to 2nd place.  */
6687   (cmp addr@0 SSA_NAME@1)
6688   (with
6689    {
6690      poly_int64 off; tree base;
6691      tree addr = (TREE_CODE (@0) == SSA_NAME
6692                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6693    }
6694    /* A local variable can never be pointed to by
6695       the default SSA name of an incoming parameter.  */
6696    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6697         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6698         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6699         && TREE_CODE (base) == VAR_DECL
6700         && auto_var_in_fn_p (base, current_function_decl))
6701     (if (cmp == NE_EXPR)
6702      { constant_boolean_node (true, type); }
6703      { constant_boolean_node (false, type); })
6704     /* If the address is based on @1 decide using the offset.  */
6705     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6706          && TREE_CODE (base) == MEM_REF
6707          && TREE_OPERAND (base, 0) == @1)
6708      (with { off += mem_ref_offset (base).force_shwi (); }
6709       (if (known_ne (off, 0))
6710        { constant_boolean_node (cmp == NE_EXPR, type); }
6711        (if (known_eq (off, 0))
6712         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6714 /* Equality compare simplifications from fold_binary  */
6715 (for cmp (eq ne)
6717  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6718     Similarly for NE_EXPR.  */
6719  (simplify
6720   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6721   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6722        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6723    { constant_boolean_node (cmp == NE_EXPR, type); }))
6725  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6726  (simplify
6727   (cmp (bit_xor @0 @1) integer_zerop)
6728   (cmp @0 @1))
6730  /* (X ^ Y) == Y becomes X == 0.
6731     Likewise (X ^ Y) == X becomes Y == 0.  */
6732  (simplify
6733   (cmp:c (bit_xor:c @0 @1) @0)
6734   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6736  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6737  (simplify
6738   (cmp:c (bit_and:c @0 @1) @0)
6739   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6740  (simplify
6741   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6742   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6743        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6744        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6745        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6746        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6747        && !wi::neg_p (wi::to_wide (@1)))
6748    (cmp (bit_and @0 (convert (bit_not @1)))
6749         { build_zero_cst (TREE_TYPE (@0)); })))
6751  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6752  (simplify
6753   (cmp:c (bit_ior:c @0 @1) @1)
6754   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6756  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6757  (simplify
6758   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6759   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6760    (cmp @0 (bit_xor @1 (convert @2)))))
6762  (simplify
6763   (cmp (nop_convert? @0) integer_zerop)
6764   (if (tree_expr_nonzero_p (@0))
6765    { constant_boolean_node (cmp == NE_EXPR, type); }))
6767  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6768  (simplify
6769   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6770   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6772 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6773    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6774    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6775    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6776 (for cmp (eq ne)
6777      ncmp (ge lt)
6778  (for sgncmp (ge lt)
6779   (simplify
6780    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6781    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6782         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6783         && types_match (@0, @1))
6784     (ncmp (bit_xor @0 @1) @2)))))
6785 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6786    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6787 (for cmp (eq ne)
6788      ncmp (lt ge)
6789  (simplify
6790   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6791    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6792         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6793         && types_match (@0, @1))
6794     (ncmp (bit_xor @0 @1) @2))))
6796 /* If we have (A & C) == C where C is a power of 2, convert this into
6797    (A & C) != 0.  Similarly for NE_EXPR.  */
6798 (for cmp (eq ne)
6799      icmp (ne eq)
6800  (simplify
6801   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6802   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6804 #if GIMPLE
6805 /* From fold_binary_op_with_conditional_arg handle the case of
6806    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6807    compares simplify.  */
6808 (for cmp (simple_comparison)
6809  (simplify
6810   (cmp:c (cond @0 @1 @2) @3)
6811   /* Do not move possibly trapping operations into the conditional as this
6812      pessimizes code and causes gimplification issues when applied late.  */
6813   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6814        || !operation_could_trap_p (cmp, true, false, @3))
6815    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6816 #endif
6818 (for cmp (ge lt)
6819 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6820 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6821  (simplify
6822   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6823    (if (INTEGRAL_TYPE_P (type)
6824         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6825         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6826         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6827     (with
6828      {
6829        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6830      }
6831     (if (cmp == LT_EXPR)
6832      (bit_xor (convert (rshift @0 {shifter;})) @1)
6833      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6834 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6835 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6836  (simplify
6837   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6838    (if (INTEGRAL_TYPE_P (type)
6839         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6840         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6841         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6842     (with
6843      {
6844        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6845      }
6846     (if (cmp == GE_EXPR)
6847      (bit_xor (convert (rshift @0 {shifter;})) @1)
6848      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6850 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6851    convert this into a shift followed by ANDing with D.  */
6852 (simplify
6853  (cond
6854   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6855   INTEGER_CST@2 integer_zerop)
6856  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6857   (with {
6858      int shift = (wi::exact_log2 (wi::to_wide (@2))
6859                   - wi::exact_log2 (wi::to_wide (@1)));
6860    }
6861    (if (shift > 0)
6862     (bit_and
6863      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6864     (bit_and
6865      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6866      @2)))))
6868 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6869    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6870 (for cmp (eq ne)
6871      ncmp (ge lt)
6872  (simplify
6873   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6874   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6875        && type_has_mode_precision_p (TREE_TYPE (@0))
6876        && element_precision (@2) >= element_precision (@0)
6877        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6878    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6879     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6881 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6882    this into a right shift or sign extension followed by ANDing with C.  */
6883 (simplify
6884  (cond
6885   (lt @0 integer_zerop)
6886   INTEGER_CST@1 integer_zerop)
6887  (if (integer_pow2p (@1)
6888       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6889   (with {
6890     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6891    }
6892    (if (shift >= 0)
6893     (bit_and
6894      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6895      @1)
6896     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6897        sign extension followed by AND with C will achieve the effect.  */
6898     (bit_and (convert @0) @1)))))
6900 /* When the addresses are not directly of decls compare base and offset.
6901    This implements some remaining parts of fold_comparison address
6902    comparisons but still no complete part of it.  Still it is good
6903    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6904 (for cmp (simple_comparison)
6905  (simplify
6906   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6907   (with
6908    {
6909      poly_int64 off0, off1;
6910      tree base0, base1;
6911      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6912                                   off0, off1, GENERIC);
6913    }
6914    (if (equal == 1)
6915     (switch
6916      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6917       { constant_boolean_node (known_eq (off0, off1), type); })
6918      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6919       { constant_boolean_node (known_ne (off0, off1), type); })
6920      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6921       { constant_boolean_node (known_lt (off0, off1), type); })
6922      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6923       { constant_boolean_node (known_le (off0, off1), type); })
6924      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6925       { constant_boolean_node (known_ge (off0, off1), type); })
6926      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6927       { constant_boolean_node (known_gt (off0, off1), type); }))
6928     (if (equal == 0)
6929      (switch
6930       (if (cmp == EQ_EXPR)
6931        { constant_boolean_node (false, type); })
6932       (if (cmp == NE_EXPR)
6933        { constant_boolean_node (true, type); })))))))
6935 #if GIMPLE
6936 /* a?~t:t -> (-(a))^t */
6937 (simplify
6938  (cond @0 @1 @2)
6939  (with { bool wascmp; }
6940   (if (INTEGRAL_TYPE_P (type)
6941        && bitwise_inverted_equal_p (@1, @2, wascmp)
6942        && (!wascmp || TYPE_PRECISION (type) == 1))
6943    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6944         || TYPE_PRECISION (type) == 1)
6945     (bit_xor (convert:type @0) @2)
6946     (bit_xor (negate (convert:type @0)) @2)))))
6947 #endif
6949 /* Simplify pointer equality compares using PTA.  */
6950 (for neeq (ne eq)
6951  (simplify
6952   (neeq @0 @1)
6953   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6954        && ptrs_compare_unequal (@0, @1))
6955    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6957 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6958    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6959    Disable the transform if either operand is pointer to function.
6960    This broke pr22051-2.c for arm where function pointer
6961    canonicalizaion is not wanted.  */
6963 (for cmp (ne eq)
6964  (simplify
6965   (cmp (convert @0) INTEGER_CST@1)
6966   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6967          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6968          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6969          /* Don't perform this optimization in GENERIC if @0 has reference
6970             type when sanitizing.  See PR101210.  */
6971          && !(GENERIC
6972               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6973               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6974         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6975             && POINTER_TYPE_P (TREE_TYPE (@1))
6976             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6977        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6978    (cmp @0 (convert @1)))))
6980 /* Non-equality compare simplifications from fold_binary  */
6981 (for cmp (lt gt le ge)
6982  /* Comparisons with the highest or lowest possible integer of
6983     the specified precision will have known values.  */
6984  (simplify
6985   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6986   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6987         || POINTER_TYPE_P (TREE_TYPE (@1))
6988         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6989        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6990    (with
6991     {
6992       tree cst = uniform_integer_cst_p (@1);
6993       tree arg1_type = TREE_TYPE (cst);
6994       unsigned int prec = TYPE_PRECISION (arg1_type);
6995       wide_int max = wi::max_value (arg1_type);
6996       wide_int signed_max = wi::max_value (prec, SIGNED);
6997       wide_int min = wi::min_value (arg1_type);
6998     }
6999     (switch
7000      (if (wi::to_wide (cst) == max)
7001       (switch
7002        (if (cmp == GT_EXPR)
7003         { constant_boolean_node (false, type); })
7004        (if (cmp == GE_EXPR)
7005         (eq @2 @1))
7006        (if (cmp == LE_EXPR)
7007         { constant_boolean_node (true, type); })
7008        (if (cmp == LT_EXPR)
7009         (ne @2 @1))))
7010      (if (wi::to_wide (cst) == min)
7011       (switch
7012        (if (cmp == LT_EXPR)
7013         { constant_boolean_node (false, type); })
7014        (if (cmp == LE_EXPR)
7015         (eq @2 @1))
7016        (if (cmp == GE_EXPR)
7017         { constant_boolean_node (true, type); })
7018        (if (cmp == GT_EXPR)
7019         (ne @2 @1))))
7020      (if (wi::to_wide (cst) == max - 1)
7021       (switch
7022        (if (cmp == GT_EXPR)
7023         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7024                                     wide_int_to_tree (TREE_TYPE (cst),
7025                                                       wi::to_wide (cst)
7026                                                       + 1)); }))
7027        (if (cmp == LE_EXPR)
7028         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7029                                     wide_int_to_tree (TREE_TYPE (cst),
7030                                                       wi::to_wide (cst)
7031                                                       + 1)); }))))
7032      (if (wi::to_wide (cst) == min + 1)
7033       (switch
7034        (if (cmp == GE_EXPR)
7035         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7036                                     wide_int_to_tree (TREE_TYPE (cst),
7037                                                       wi::to_wide (cst)
7038                                                       - 1)); }))
7039        (if (cmp == LT_EXPR)
7040         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7041                                     wide_int_to_tree (TREE_TYPE (cst),
7042                                                       wi::to_wide (cst)
7043                                                       - 1)); }))))
7044      (if (wi::to_wide (cst) == signed_max
7045           && TYPE_UNSIGNED (arg1_type)
7046           && TYPE_MODE (arg1_type) != BLKmode
7047           /* We will flip the signedness of the comparison operator
7048              associated with the mode of @1, so the sign bit is
7049              specified by this mode.  Check that @1 is the signed
7050              max associated with this sign bit.  */
7051           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7052           /* signed_type does not work on pointer types.  */
7053           && INTEGRAL_TYPE_P (arg1_type))
7054       /* The following case also applies to X < signed_max+1
7055          and X >= signed_max+1 because previous transformations.  */
7056       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7057        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7058         (switch
7059          (if (cst == @1 && cmp == LE_EXPR)
7060           (ge (convert:st @0) { build_zero_cst (st); }))
7061          (if (cst == @1 && cmp == GT_EXPR)
7062           (lt (convert:st @0) { build_zero_cst (st); }))
7063          (if (cmp == LE_EXPR)
7064           (ge (view_convert:st @0) { build_zero_cst (st); }))
7065          (if (cmp == GT_EXPR)
7066           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7068 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7069 (simplify
7070  (lt:c @0 (convert (ne @0 integer_zerop)))
7071  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7072   { constant_boolean_node (false, type); }))
7074 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7075 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7076 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7077 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7078 (for outer (ne eq)
7079  (for inner (ne eq)
7080   (simplify
7081    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7082    (with {
7083      bool cst1 = integer_onep (@1);
7084      bool cst0 = integer_zerop (@1);
7085      bool innereq = inner == EQ_EXPR;
7086      bool outereq = outer == EQ_EXPR;
7087     }
7088     (switch
7089      (if (innereq ? cst0 : cst1)
7090       { constant_boolean_node (!outereq, type); })
7091      (if (innereq ? cst1 : cst0)
7092       (with {
7093         tree utype = unsigned_type_for (TREE_TYPE (@0));
7094         tree ucst1 = build_one_cst (utype);
7095        }
7096        (if (!outereq)
7097         (gt (convert:utype @0) { ucst1; })
7098         (le (convert:utype @0) { ucst1; })
7099        )
7100       )
7101      )
7102      (with {
7103        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7104       }
7105       (if (outereq)
7106        (eq @0 { value; })
7107        (ne @0 { value; })
7108       )
7109      )
7110     )
7111    )
7112   )
7116 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7117  /* If the second operand is NaN, the result is constant.  */
7118  (simplify
7119   (cmp @0 REAL_CST@1)
7120   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7121        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7122    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7123                             ? false : true, type); })))
7125 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7126 (simplify
7127   (unordered @0 @1)
7128   (switch
7129     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7130         { constant_boolean_node (true, type); })
7131     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7132         { constant_boolean_node (false, type); })))
7134 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7135 (simplify
7136   (ordered @0 @1)
7137   (switch
7138     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7139         { constant_boolean_node (false, type); })
7140     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7141         { constant_boolean_node (true, type); })))
7143 /* bool_var != 0 becomes bool_var.  */
7144 (simplify
7145  (ne @0 integer_zerop)
7146  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7147       && types_match (type, TREE_TYPE (@0)))
7148   (non_lvalue @0)))
7149 /* bool_var == 1 becomes bool_var.  */
7150 (simplify
7151  (eq @0 integer_onep)
7152  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7153       && types_match (type, TREE_TYPE (@0)))
7154   (non_lvalue @0)))
7155 /* Do not handle
7156    bool_var == 0 becomes !bool_var or
7157    bool_var != 1 becomes !bool_var
7158    here because that only is good in assignment context as long
7159    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7160    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7161    clearly less optimal and which we'll transform again in forwprop.  */
7163 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7164    where ~Y + 1 == pow2 and Z = ~Y.  */
7165 (for cst (VECTOR_CST INTEGER_CST)
7166  (for cmp (eq ne)
7167       icmp (le gt)
7168   (simplify
7169    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7170     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7171      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7172       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7173                          ? optab_vector : optab_default;
7174               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7175        (if (target_supports_op_p (utype, icmp, optab)
7176             || (optimize_vectors_before_lowering_p ()
7177                 && (!target_supports_op_p (type, cmp, optab)
7178                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7179         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7180          (icmp @0 { csts; })
7181          (icmp (view_convert:utype @0) { csts; })))))))))
7183 /* When one argument is a constant, overflow detection can be simplified.
7184    Currently restricted to single use so as not to interfere too much with
7185    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7186    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7187 (for cmp (lt le ge gt)
7188      out (gt gt le le)
7189  (simplify
7190   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7191   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7192        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7193        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7194        && wi::to_wide (@1) != 0
7195        && single_use (@2))
7196    (with {
7197      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7198      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7199     }
7200     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7201                                 wi::max_value (prec, sign)
7202                                 - wi::to_wide (@1)); })))))
7204 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7205    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7206    expects the long form, so we restrict the transformation for now.  */
7207 (for cmp (gt le)
7208  (simplify
7209   (cmp:c (minus@2 @0 @1) @0)
7210   (if (single_use (@2)
7211        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7212        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7213    (cmp @1 @0))))
7215 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7216 (for cmp (ge lt)
7217  (simplify
7218   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7219    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7220         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7221     (cmp @1 @0))))
7223 /* Testing for overflow is unnecessary if we already know the result.  */
7224 /* A - B > A  */
7225 (for cmp (gt le)
7226      out (ne eq)
7227  (simplify
7228   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7229   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7230        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7231    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7232 /* A + B < A  */
7233 (for cmp (lt ge)
7234      out (ne eq)
7235  (simplify
7236   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7237   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7238        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7239    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7241 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7242    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7243 (for cmp (lt ge)
7244      out (ne eq)
7245  (simplify
7246   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7247   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7248    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7249     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7251 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7252    is at least twice as wide as type of A and B, simplify to
7253    __builtin_mul_overflow (A, B, <unused>).  */
7254 (for cmp (eq ne)
7255  (simplify
7256   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7257        integer_zerop)
7258   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7259        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7260        && TYPE_UNSIGNED (TREE_TYPE (@0))
7261        && (TYPE_PRECISION (TREE_TYPE (@3))
7262            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7263        && tree_fits_uhwi_p (@2)
7264        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7265        && types_match (@0, @1)
7266        && type_has_mode_precision_p (TREE_TYPE (@0))
7267        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7268            != CODE_FOR_nothing))
7269    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7270     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7272 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7273 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7274  (simplify
7275   (ovf (convert@2 @0) @1)
7276   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7277        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7278        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7279        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7280    (ovf @0 @1)))
7281  (simplify
7282   (ovf @1 (convert@2 @0))
7283   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7284        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7285        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7286        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7287    (ovf @1 @0))))
7289 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7290    are unsigned to x > (umax / cst).  Similarly for signed type, but
7291    in that case it needs to be outside of a range.  */
7292 (simplify
7293  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7294   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7295        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7296        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7297        && int_fits_type_p (@1, TREE_TYPE (@0)))
7298    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7299     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7300     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7301      (if (integer_minus_onep (@1))
7302       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7303       (with
7304        {
7305          tree div = fold_convert (TREE_TYPE (@0), @1);
7306          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7307                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7308          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7309                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7310          tree etype = range_check_type (TREE_TYPE (@0));
7311          if (etype)
7312            {
7313              if (wi::neg_p (wi::to_wide (div)))
7314                std::swap (lo, hi);
7315              lo = fold_convert (etype, lo);
7316              hi = fold_convert (etype, hi);
7317              hi = int_const_binop (MINUS_EXPR, hi, lo);
7318            }
7319        }
7320        (if (etype)
7321         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7323 /* Simplification of math builtins.  These rules must all be optimizations
7324    as well as IL simplifications.  If there is a possibility that the new
7325    form could be a pessimization, the rule should go in the canonicalization
7326    section that follows this one.
7328    Rules can generally go in this section if they satisfy one of
7329    the following:
7331    - the rule describes an identity
7333    - the rule replaces calls with something as simple as addition or
7334      multiplication
7336    - the rule contains unary calls only and simplifies the surrounding
7337      arithmetic.  (The idea here is to exclude non-unary calls in which
7338      one operand is constant and in which the call is known to be cheap
7339      when the operand has that value.)  */
7341 (if (flag_unsafe_math_optimizations)
7342  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7343  (simplify
7344   (mult (SQRT_ALL@1 @0) @1)
7345   (if (!tree_expr_maybe_signaling_nan_p (@0))
7346    @0))
7348  (for op (plus minus)
7349   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7350   (simplify
7351    (op (rdiv @0 @1)
7352        (rdiv @2 @1))
7353    (rdiv (op @0 @2) @1)))
7355  (for cmp (lt le gt ge)
7356       neg_cmp (gt ge lt le)
7357   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7358   (simplify
7359    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7360    (with
7361     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7362     (if (tem
7363          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7364               || (real_zerop (tem) && !real_zerop (@1))))
7365      (switch
7366       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7367        (cmp @0 { tem; }))
7368       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7369        (neg_cmp @0 { tem; })))))))
7371  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7372  (for root (SQRT CBRT)
7373   (simplify
7374    (mult (root:s @0) (root:s @1))
7375     (root (mult @0 @1))))
7377  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7378  (for exps (EXP EXP2 EXP10 POW10)
7379   (simplify
7380    (mult (exps:s @0) (exps:s @1))
7381     (exps (plus @0 @1))))
7383  /* Simplify a/root(b/c) into a*root(c/b).  */
7384  (for root (SQRT CBRT)
7385   (simplify
7386    (rdiv @0 (root:s (rdiv:s @1 @2)))
7387     (mult @0 (root (rdiv @2 @1)))))
7389  /* Simplify x/expN(y) into x*expN(-y).  */
7390  (for exps (EXP EXP2 EXP10 POW10)
7391   (simplify
7392    (rdiv @0 (exps:s @1))
7393     (mult @0 (exps (negate @1)))))
7395  (for logs (LOG LOG2 LOG10 LOG10)
7396       exps (EXP EXP2 EXP10 POW10)
7397   /* logN(expN(x)) -> x.  */
7398   (simplify
7399    (logs (exps @0))
7400    @0)
7401   /* expN(logN(x)) -> x.  */
7402   (simplify
7403    (exps (logs @0))
7404    @0))
7406  /* Optimize logN(func()) for various exponential functions.  We
7407     want to determine the value "x" and the power "exponent" in
7408     order to transform logN(x**exponent) into exponent*logN(x).  */
7409  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7410       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7411   (simplify
7412    (logs (exps @0))
7413    (if (SCALAR_FLOAT_TYPE_P (type))
7414     (with {
7415       tree x;
7416       switch (exps)
7417         {
7418         CASE_CFN_EXP:
7419           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7420           x = build_real_truncate (type, dconst_e ());
7421           break;
7422         CASE_CFN_EXP2:
7423           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7424           x = build_real (type, dconst2);
7425           break;
7426         CASE_CFN_EXP10:
7427         CASE_CFN_POW10:
7428           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7429           {
7430             REAL_VALUE_TYPE dconst10;
7431             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7432             x = build_real (type, dconst10);
7433           }
7434           break;
7435         default:
7436           gcc_unreachable ();
7437         }
7438       }
7439      (mult (logs { x; }) @0)))))
7441  (for logs (LOG LOG
7442             LOG2 LOG2
7443             LOG10 LOG10)
7444       exps (SQRT CBRT)
7445   (simplify
7446    (logs (exps @0))
7447    (if (SCALAR_FLOAT_TYPE_P (type))
7448     (with {
7449       tree x;
7450       switch (exps)
7451         {
7452         CASE_CFN_SQRT:
7453           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7454           x = build_real (type, dconsthalf);
7455           break;
7456         CASE_CFN_CBRT:
7457           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7458           x = build_real_truncate (type, dconst_third ());
7459           break;
7460         default:
7461           gcc_unreachable ();
7462         }
7463       }
7464      (mult { x; } (logs @0))))))
7466  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7467  (for logs (LOG LOG2 LOG10)
7468       pows (POW)
7469   (simplify
7470    (logs (pows @0 @1))
7471    (mult @1 (logs @0))))
7473  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7474     or if C is a positive power of 2,
7475     pow(C,x) -> exp2(log2(C)*x).  */
7476 #if GIMPLE
7477  (for pows (POW)
7478       exps (EXP)
7479       logs (LOG)
7480       exp2s (EXP2)
7481       log2s (LOG2)
7482   (simplify
7483    (pows REAL_CST@0 @1)
7484    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7485         && real_isfinite (TREE_REAL_CST_PTR (@0))
7486         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7487            the use_exp2 case until after vectorization.  It seems actually
7488            beneficial for all constants to postpone this until later,
7489            because exp(log(C)*x), while faster, will have worse precision
7490            and if x folds into a constant too, that is unnecessary
7491            pessimization.  */
7492         && canonicalize_math_after_vectorization_p ())
7493     (with {
7494        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7495        bool use_exp2 = false;
7496        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7497            && value->cl == rvc_normal)
7498          {
7499            REAL_VALUE_TYPE frac_rvt = *value;
7500            SET_REAL_EXP (&frac_rvt, 1);
7501            if (real_equal (&frac_rvt, &dconst1))
7502              use_exp2 = true;
7503          }
7504      }
7505      (if (!use_exp2)
7506       (if (optimize_pow_to_exp (@0, @1))
7507        (exps (mult (logs @0) @1)))
7508       (exp2s (mult (log2s @0) @1)))))))
7509 #endif
7511  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7512  (for pows (POW)
7513       exps (EXP EXP2 EXP10 POW10)
7514       logs (LOG LOG2 LOG10 LOG10)
7515   (simplify
7516    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7517    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7518         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7519     (exps (plus (mult (logs @0) @1) @2)))))
7521  (for sqrts (SQRT)
7522       cbrts (CBRT)
7523       pows (POW)
7524       exps (EXP EXP2 EXP10 POW10)
7525   /* sqrt(expN(x)) -> expN(x*0.5).  */
7526   (simplify
7527    (sqrts (exps @0))
7528    (exps (mult @0 { build_real (type, dconsthalf); })))
7529   /* cbrt(expN(x)) -> expN(x/3).  */
7530   (simplify
7531    (cbrts (exps @0))
7532    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7533   /* pow(expN(x), y) -> expN(x*y).  */
7534   (simplify
7535    (pows (exps @0) @1)
7536    (exps (mult @0 @1))))
7538  /* tan(atan(x)) -> x.  */
7539  (for tans (TAN)
7540       atans (ATAN)
7541   (simplify
7542    (tans (atans @0))
7543    @0)))
7545  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7546  (for sins (SIN)
7547       atans (ATAN)
7548       sqrts (SQRT)
7549       copysigns (COPYSIGN)
7550   (simplify
7551    (sins (atans:s @0))
7552    (with
7553      {
7554       REAL_VALUE_TYPE r_cst;
7555       build_sinatan_real (&r_cst, type);
7556       tree t_cst = build_real (type, r_cst);
7557       tree t_one = build_one_cst (type);
7558      }
7559     (if (SCALAR_FLOAT_TYPE_P (type))
7560      (cond (lt (abs @0) { t_cst; })
7561       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7562       (copysigns { t_one; } @0))))))
7564 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7565  (for coss (COS)
7566       atans (ATAN)
7567       sqrts (SQRT)
7568       copysigns (COPYSIGN)
7569   (simplify
7570    (coss (atans:s @0))
7571    (with
7572      {
7573       REAL_VALUE_TYPE r_cst;
7574       build_sinatan_real (&r_cst, type);
7575       tree t_cst = build_real (type, r_cst);
7576       tree t_one = build_one_cst (type);
7577       tree t_zero = build_zero_cst (type);
7578      }
7579     (if (SCALAR_FLOAT_TYPE_P (type))
7580      (cond (lt (abs @0) { t_cst; })
7581       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7582       (copysigns { t_zero; } @0))))))
7584  (if (!flag_errno_math)
7585   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7586   (for sinhs (SINH)
7587        atanhs (ATANH)
7588        sqrts (SQRT)
7589    (simplify
7590     (sinhs (atanhs:s @0))
7591     (with { tree t_one = build_one_cst (type); }
7592     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7594   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7595   (for coshs (COSH)
7596        atanhs (ATANH)
7597        sqrts (SQRT)
7598    (simplify
7599     (coshs (atanhs:s @0))
7600     (with { tree t_one = build_one_cst (type); }
7601     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7603 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7604 (simplify
7605  (CABS (complex:C @0 real_zerop@1))
7606  (abs @0))
7608 /* trunc(trunc(x)) -> trunc(x), etc.  */
7609 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7610  (simplify
7611   (fns (fns @0))
7612   (fns @0)))
7613 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7614 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7615  (simplify
7616   (fns integer_valued_real_p@0)
7617   @0))
7619 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7620 (simplify
7621  (HYPOT:c @0 real_zerop@1)
7622  (abs @0))
7624 /* pow(1,x) -> 1.  */
7625 (simplify
7626  (POW real_onep@0 @1)
7627  @0)
7629 (simplify
7630  /* copysign(x,x) -> x.  */
7631  (COPYSIGN_ALL @0 @0)
7632  @0)
7634 (simplify
7635  /* copysign(x,-x) -> -x.  */
7636  (COPYSIGN_ALL @0 (negate@1 @0))
7637  @1)
7639 (simplify
7640  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7641  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7642  (abs @0))
7644 (simplify
7645  /* fabs (copysign(x, y)) -> fabs (x).  */
7646  (abs (COPYSIGN_ALL @0 @1))
7647  (abs @0))
7649 (for scale (LDEXP SCALBN SCALBLN)
7650  /* ldexp(0, x) -> 0.  */
7651  (simplify
7652   (scale real_zerop@0 @1)
7653   @0)
7654  /* ldexp(x, 0) -> x.  */
7655  (simplify
7656   (scale @0 integer_zerop@1)
7657   @0)
7658  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7659  (simplify
7660   (scale REAL_CST@0 @1)
7661   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7662    @0)))
7664 /* Canonicalization of sequences of math builtins.  These rules represent
7665    IL simplifications but are not necessarily optimizations.
7667    The sincos pass is responsible for picking "optimal" implementations
7668    of math builtins, which may be more complicated and can sometimes go
7669    the other way, e.g. converting pow into a sequence of sqrts.
7670    We only want to do these canonicalizations before the pass has run.  */
7672 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7673  /* Simplify tan(x) * cos(x) -> sin(x). */
7674  (simplify
7675   (mult:c (TAN:s @0) (COS:s @0))
7676    (SIN @0))
7678  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7679  (simplify
7680   (mult:c @0 (POW:s @0 REAL_CST@1))
7681   (if (!TREE_OVERFLOW (@1))
7682    (POW @0 (plus @1 { build_one_cst (type); }))))
7684  /* Simplify sin(x) / cos(x) -> tan(x). */
7685  (simplify
7686   (rdiv (SIN:s @0) (COS:s @0))
7687    (TAN @0))
7689  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7690  (simplify
7691   (rdiv (SINH:s @0) (COSH:s @0))
7692    (TANH @0))
7694  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7695  (simplify
7696    (rdiv (TANH:s @0) (SINH:s @0))
7697    (rdiv {build_one_cst (type);} (COSH @0)))
7699  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7700  (simplify
7701   (rdiv (COS:s @0) (SIN:s @0))
7702    (rdiv { build_one_cst (type); } (TAN @0)))
7704  /* Simplify sin(x) / tan(x) -> cos(x). */
7705  (simplify
7706   (rdiv (SIN:s @0) (TAN:s @0))
7707   (if (! HONOR_NANS (@0)
7708        && ! HONOR_INFINITIES (@0))
7709    (COS @0)))
7711  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7712  (simplify
7713   (rdiv (TAN:s @0) (SIN:s @0))
7714   (if (! HONOR_NANS (@0)
7715        && ! HONOR_INFINITIES (@0))
7716    (rdiv { build_one_cst (type); } (COS @0))))
7718  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7719  (simplify
7720   (mult (POW:s @0 @1) (POW:s @0 @2))
7721    (POW @0 (plus @1 @2)))
7723  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7724  (simplify
7725   (mult (POW:s @0 @1) (POW:s @2 @1))
7726    (POW (mult @0 @2) @1))
7728  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7729  (simplify
7730   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7731    (POWI (mult @0 @2) @1))
7733  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7734  (simplify
7735   (rdiv (POW:s @0 REAL_CST@1) @0)
7736   (if (!TREE_OVERFLOW (@1))
7737    (POW @0 (minus @1 { build_one_cst (type); }))))
7739  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7740  (simplify
7741   (rdiv @0 (POW:s @1 @2))
7742    (mult @0 (POW @1 (negate @2))))
7744  (for sqrts (SQRT)
7745       cbrts (CBRT)
7746       pows (POW)
7747   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7748   (simplify
7749    (sqrts (sqrts @0))
7750    (pows @0 { build_real (type, dconst_quarter ()); }))
7751   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7752   (simplify
7753    (sqrts (cbrts @0))
7754    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7755   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7756   (simplify
7757    (cbrts (sqrts @0))
7758    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7759   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7760   (simplify
7761    (cbrts (cbrts tree_expr_nonnegative_p@0))
7762    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7763   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7764   (simplify
7765    (sqrts (pows @0 @1))
7766    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7767   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7768   (simplify
7769    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7770    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7771   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7772   (simplify
7773    (pows (sqrts @0) @1)
7774    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7775   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7776   (simplify
7777    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7778    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7779   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7780   (simplify
7781    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7782    (pows @0 (mult @1 @2))))
7784  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7785  (simplify
7786   (CABS (complex @0 @0))
7787   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7789  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7790  (simplify
7791   (HYPOT @0 @0)
7792   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7794  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7795  (for cexps (CEXP)
7796       exps (EXP)
7797       cexpis (CEXPI)
7798   (simplify
7799    (cexps compositional_complex@0)
7800    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7801     (complex
7802      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7803      (mult @1 (imagpart @2)))))))
7805 (if (canonicalize_math_p ())
7806  /* floor(x) -> trunc(x) if x is nonnegative.  */
7807  (for floors (FLOOR_ALL)
7808       truncs (TRUNC_ALL)
7809   (simplify
7810    (floors tree_expr_nonnegative_p@0)
7811    (truncs @0))))
7813 (match double_value_p
7814  @0
7815  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7816 (for froms (BUILT_IN_TRUNCL
7817             BUILT_IN_FLOORL
7818             BUILT_IN_CEILL
7819             BUILT_IN_ROUNDL
7820             BUILT_IN_NEARBYINTL
7821             BUILT_IN_RINTL)
7822      tos (BUILT_IN_TRUNC
7823           BUILT_IN_FLOOR
7824           BUILT_IN_CEIL
7825           BUILT_IN_ROUND
7826           BUILT_IN_NEARBYINT
7827           BUILT_IN_RINT)
7828  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7829  (if (optimize && canonicalize_math_p ())
7830   (simplify
7831    (froms (convert double_value_p@0))
7832    (convert (tos @0)))))
7834 (match float_value_p
7835  @0
7836  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7837 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7838             BUILT_IN_FLOORL BUILT_IN_FLOOR
7839             BUILT_IN_CEILL BUILT_IN_CEIL
7840             BUILT_IN_ROUNDL BUILT_IN_ROUND
7841             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7842             BUILT_IN_RINTL BUILT_IN_RINT)
7843      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7844           BUILT_IN_FLOORF BUILT_IN_FLOORF
7845           BUILT_IN_CEILF BUILT_IN_CEILF
7846           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7847           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7848           BUILT_IN_RINTF BUILT_IN_RINTF)
7849  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7850     if x is a float.  */
7851  (if (optimize && canonicalize_math_p ()
7852       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7853   (simplify
7854    (froms (convert float_value_p@0))
7855    (convert (tos @0)))))
7857 #if GIMPLE
7858 (match float16_value_p
7859  @0
7860  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7861 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7862             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7863             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7864             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7865             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7866             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7867             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7868             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7869      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7870           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7871           IFN_CEIL IFN_CEIL IFN_CEIL
7872           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7873           IFN_ROUND IFN_ROUND IFN_ROUND
7874           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7875           IFN_RINT IFN_RINT IFN_RINT
7876           IFN_SQRT IFN_SQRT IFN_SQRT)
7877  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7878     if x is a _Float16.  */
7879  (simplify
7880    (convert (froms (convert float16_value_p@0)))
7881      (if (optimize
7882           && types_match (type, TREE_TYPE (@0))
7883           && direct_internal_fn_supported_p (as_internal_fn (tos),
7884                                              type, OPTIMIZE_FOR_BOTH))
7885        (tos @0))))
7887 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7888    x,y is float value, similar for _Float16/double.  */
7889 (for copysigns (COPYSIGN_ALL)
7890  (simplify
7891   (convert (copysigns (convert@2 @0) (convert @1)))
7892    (if (optimize
7893        && !HONOR_SNANS (@2)
7894        && types_match (type, TREE_TYPE (@0))
7895        && types_match (type, TREE_TYPE (@1))
7896        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7897        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7898                                           type, OPTIMIZE_FOR_BOTH))
7899     (IFN_COPYSIGN @0 @1))))
7901 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7902      tos (IFN_FMA IFN_FMA IFN_FMA)
7903  (simplify
7904   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7905    (if (flag_unsafe_math_optimizations
7906        && optimize
7907        && FLOAT_TYPE_P (type)
7908        && FLOAT_TYPE_P (TREE_TYPE (@3))
7909        && types_match (type, TREE_TYPE (@0))
7910        && types_match (type, TREE_TYPE (@1))
7911        && types_match (type, TREE_TYPE (@2))
7912        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7913        && direct_internal_fn_supported_p (as_internal_fn (tos),
7914                                           type, OPTIMIZE_FOR_BOTH))
7915     (tos @0 @1 @2))))
7917 (for maxmin (max min)
7918  (simplify
7919   (convert (maxmin (convert@2 @0) (convert @1)))
7920    (if (optimize
7921        && FLOAT_TYPE_P (type)
7922        && FLOAT_TYPE_P (TREE_TYPE (@2))
7923        && types_match (type, TREE_TYPE (@0))
7924        && types_match (type, TREE_TYPE (@1))
7925        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7926     (maxmin @0 @1))))
7927 #endif
7929 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7930      tos (XFLOOR XCEIL XROUND XRINT)
7931  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7932  (if (optimize && canonicalize_math_p ())
7933   (simplify
7934    (froms (convert double_value_p@0))
7935    (tos @0))))
7937 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7938             XFLOOR XCEIL XROUND XRINT)
7939      tos (XFLOORF XCEILF XROUNDF XRINTF)
7940  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7941     if x is a float.  */
7942  (if (optimize && canonicalize_math_p ())
7943   (simplify
7944    (froms (convert float_value_p@0))
7945    (tos @0))))
7947 (if (canonicalize_math_p ())
7948  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7949  (for floors (IFLOOR LFLOOR LLFLOOR)
7950   (simplify
7951    (floors tree_expr_nonnegative_p@0)
7952    (fix_trunc @0))))
7954 (if (canonicalize_math_p ())
7955  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7956  (for fns (IFLOOR LFLOOR LLFLOOR
7957            ICEIL LCEIL LLCEIL
7958            IROUND LROUND LLROUND)
7959   (simplify
7960    (fns integer_valued_real_p@0)
7961    (fix_trunc @0)))
7962  (if (!flag_errno_math)
7963   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7964   (for rints (IRINT LRINT LLRINT)
7965    (simplify
7966     (rints integer_valued_real_p@0)
7967     (fix_trunc @0)))))
7969 (if (canonicalize_math_p ())
7970  (for ifn (IFLOOR ICEIL IROUND IRINT)
7971       lfn (LFLOOR LCEIL LROUND LRINT)
7972       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7973   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7974      sizeof (int) == sizeof (long).  */
7975   (if (TYPE_PRECISION (integer_type_node)
7976        == TYPE_PRECISION (long_integer_type_node))
7977    (simplify
7978     (ifn @0)
7979     (lfn:long_integer_type_node @0)))
7980   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7981      sizeof (long long) == sizeof (long).  */
7982   (if (TYPE_PRECISION (long_long_integer_type_node)
7983        == TYPE_PRECISION (long_integer_type_node))
7984    (simplify
7985     (llfn @0)
7986     (lfn:long_integer_type_node @0)))))
7988 /* cproj(x) -> x if we're ignoring infinities.  */
7989 (simplify
7990  (CPROJ @0)
7991  (if (!HONOR_INFINITIES (type))
7992    @0))
7994 /* If the real part is inf and the imag part is known to be
7995    nonnegative, return (inf + 0i).  */
7996 (simplify
7997  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7998  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7999   { build_complex_inf (type, false); }))
8001 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8002 (simplify
8003  (CPROJ (complex @0 REAL_CST@1))
8004  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8005   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8007 (for pows (POW)
8008      sqrts (SQRT)
8009      cbrts (CBRT)
8010  (simplify
8011   (pows @0 REAL_CST@1)
8012   (with {
8013     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8014     REAL_VALUE_TYPE tmp;
8015    }
8016    (switch
8017     /* pow(x,0) -> 1.  */
8018     (if (real_equal (value, &dconst0))
8019      { build_real (type, dconst1); })
8020     /* pow(x,1) -> x.  */
8021     (if (real_equal (value, &dconst1))
8022      @0)
8023     /* pow(x,-1) -> 1/x.  */
8024     (if (real_equal (value, &dconstm1))
8025      (rdiv { build_real (type, dconst1); } @0))
8026     /* pow(x,0.5) -> sqrt(x).  */
8027     (if (flag_unsafe_math_optimizations
8028          && canonicalize_math_p ()
8029          && real_equal (value, &dconsthalf))
8030      (sqrts @0))
8031     /* pow(x,1/3) -> cbrt(x).  */
8032     (if (flag_unsafe_math_optimizations
8033          && canonicalize_math_p ()
8034          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8035              real_equal (value, &tmp)))
8036      (cbrts @0))))))
8038 /* powi(1,x) -> 1.  */
8039 (simplify
8040  (POWI real_onep@0 @1)
8041  @0)
8043 (simplify
8044  (POWI @0 INTEGER_CST@1)
8045  (switch
8046   /* powi(x,0) -> 1.  */
8047   (if (wi::to_wide (@1) == 0)
8048    { build_real (type, dconst1); })
8049   /* powi(x,1) -> x.  */
8050   (if (wi::to_wide (@1) == 1)
8051    @0)
8052   /* powi(x,-1) -> 1/x.  */
8053   (if (wi::to_wide (@1) == -1)
8054    (rdiv { build_real (type, dconst1); } @0))))
8056 /* Narrowing of arithmetic and logical operations.
8058    These are conceptually similar to the transformations performed for
8059    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8060    term we want to move all that code out of the front-ends into here.  */
8062 /* Convert (outertype)((innertype0)a+(innertype1)b)
8063    into ((newtype)a+(newtype)b) where newtype
8064    is the widest mode from all of these.  */
8065 (for op (plus minus mult rdiv)
8066  (simplify
8067    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8068    /* If we have a narrowing conversion of an arithmetic operation where
8069       both operands are widening conversions from the same type as the outer
8070       narrowing conversion.  Then convert the innermost operands to a
8071       suitable unsigned type (to avoid introducing undefined behavior),
8072       perform the operation and convert the result to the desired type.  */
8073    (if (INTEGRAL_TYPE_P (type)
8074         && op != MULT_EXPR
8075         && op != RDIV_EXPR
8076         /* We check for type compatibility between @0 and @1 below,
8077            so there's no need to check that @2/@4 are integral types.  */
8078         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8079         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8080         /* The precision of the type of each operand must match the
8081            precision of the mode of each operand, similarly for the
8082            result.  */
8083         && type_has_mode_precision_p (TREE_TYPE (@1))
8084         && type_has_mode_precision_p (TREE_TYPE (@2))
8085         && type_has_mode_precision_p (type)
8086         /* The inner conversion must be a widening conversion.  */
8087         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8088         && types_match (@1, type)
8089         && (types_match (@1, @2)
8090             /* Or the second operand is const integer or converted const
8091                integer from valueize.  */
8092             || poly_int_tree_p (@4)))
8093      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8094        (op @1 (convert @2))
8095        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8096         (convert (op (convert:utype @1)
8097                      (convert:utype @2)))))
8098      (if (FLOAT_TYPE_P (type)
8099           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8100                == DECIMAL_FLOAT_TYPE_P (type))
8101       (with { tree arg0 = strip_float_extensions (@1);
8102               tree arg1 = strip_float_extensions (@2);
8103               tree itype = TREE_TYPE (@0);
8104               tree ty1 = TREE_TYPE (arg0);
8105               tree ty2 = TREE_TYPE (arg1);
8106               enum tree_code code = TREE_CODE (itype); }
8107         (if (FLOAT_TYPE_P (ty1)
8108              && FLOAT_TYPE_P (ty2))
8109          (with { tree newtype = type;
8110                  if (TYPE_MODE (ty1) == SDmode
8111                      || TYPE_MODE (ty2) == SDmode
8112                      || TYPE_MODE (type) == SDmode)
8113                    newtype = dfloat32_type_node;
8114                  if (TYPE_MODE (ty1) == DDmode
8115                      || TYPE_MODE (ty2) == DDmode
8116                      || TYPE_MODE (type) == DDmode)
8117                    newtype = dfloat64_type_node;
8118                  if (TYPE_MODE (ty1) == TDmode
8119                      || TYPE_MODE (ty2) == TDmode
8120                      || TYPE_MODE (type) == TDmode)
8121                    newtype = dfloat128_type_node; }
8122           (if ((newtype == dfloat32_type_node
8123                 || newtype == dfloat64_type_node
8124                 || newtype == dfloat128_type_node)
8125               && newtype == type
8126               && types_match (newtype, type))
8127             (op (convert:newtype @1) (convert:newtype @2))
8128             (with { if (element_precision (ty1) > element_precision (newtype))
8129                       newtype = ty1;
8130                     if (element_precision (ty2) > element_precision (newtype))
8131                       newtype = ty2; }
8132                /* Sometimes this transformation is safe (cannot
8133                   change results through affecting double rounding
8134                   cases) and sometimes it is not.  If NEWTYPE is
8135                   wider than TYPE, e.g. (float)((long double)double
8136                   + (long double)double) converted to
8137                   (float)(double + double), the transformation is
8138                   unsafe regardless of the details of the types
8139                   involved; double rounding can arise if the result
8140                   of NEWTYPE arithmetic is a NEWTYPE value half way
8141                   between two representable TYPE values but the
8142                   exact value is sufficiently different (in the
8143                   right direction) for this difference to be
8144                   visible in ITYPE arithmetic.  If NEWTYPE is the
8145                   same as TYPE, however, the transformation may be
8146                   safe depending on the types involved: it is safe
8147                   if the ITYPE has strictly more than twice as many
8148                   mantissa bits as TYPE, can represent infinities
8149                   and NaNs if the TYPE can, and has sufficient
8150                   exponent range for the product or ratio of two
8151                   values representable in the TYPE to be within the
8152                   range of normal values of ITYPE.  */
8153               (if (element_precision (newtype) < element_precision (itype)
8154                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8155                        || target_supports_op_p (newtype, op, optab_default))
8156                    && (flag_unsafe_math_optimizations
8157                        || (element_precision (newtype) == element_precision (type)
8158                            && real_can_shorten_arithmetic (element_mode (itype),
8159                                                            element_mode (type))
8160                            && !excess_precision_type (newtype)))
8161                    && !types_match (itype, newtype))
8162                  (convert:type (op (convert:newtype @1)
8163                                    (convert:newtype @2)))
8164          )))) )
8165    ))
8168 /* This is another case of narrowing, specifically when there's an outer
8169    BIT_AND_EXPR which masks off bits outside the type of the innermost
8170    operands.   Like the previous case we have to convert the operands
8171    to unsigned types to avoid introducing undefined behavior for the
8172    arithmetic operation.  */
8173 (for op (minus plus)
8174  (simplify
8175   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8176   (if (INTEGRAL_TYPE_P (type)
8177        /* We check for type compatibility between @0 and @1 below,
8178           so there's no need to check that @1/@3 are integral types.  */
8179        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8180        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8181        /* The precision of the type of each operand must match the
8182           precision of the mode of each operand, similarly for the
8183           result.  */
8184        && type_has_mode_precision_p (TREE_TYPE (@0))
8185        && type_has_mode_precision_p (TREE_TYPE (@1))
8186        && type_has_mode_precision_p (type)
8187        /* The inner conversion must be a widening conversion.  */
8188        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8189        && types_match (@0, @1)
8190        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8191            <= TYPE_PRECISION (TREE_TYPE (@0)))
8192        && (wi::to_wide (@4)
8193            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8194                        true, TYPE_PRECISION (type))) == 0)
8195    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8196     (with { tree ntype = TREE_TYPE (@0); }
8197      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8198     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8199      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8200                (convert:utype @4))))))))
8202 /* Transform (@0 < @1 and @0 < @2) to use min,
8203    (@0 > @1 and @0 > @2) to use max */
8204 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8205      op    (lt      le      gt      ge      lt      le      gt      ge     )
8206      ext   (min     min     max     max     max     max     min     min    )
8207  (simplify
8208   (logic (op:cs @0 @1) (op:cs @0 @2))
8209   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8210        && TREE_CODE (@0) != INTEGER_CST)
8211    (op @0 (ext @1 @2)))))
8213 /* Max<bool0, bool1> -> bool0 | bool1
8214    Min<bool0, bool1> -> bool0 & bool1 */
8215 (for op    (max     min)
8216      logic (bit_ior bit_and)
8217  (simplify
8218   (op zero_one_valued_p@0 zero_one_valued_p@1)
8219   (logic @0 @1)))
8221 /* signbit(x) != 0 ? -x : x -> abs(x)
8222    signbit(x) == 0 ? -x : x -> -abs(x) */
8223 (for sign (SIGNBIT)
8224  (for neeq (ne eq)
8225   (simplify
8226    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8227     (if (neeq == NE_EXPR)
8228      (abs @0)
8229      (negate (abs @0))))))
8231 (simplify
8232  /* signbit(x) -> 0 if x is nonnegative.  */
8233  (SIGNBIT tree_expr_nonnegative_p@0)
8234  { integer_zero_node; })
8236 (simplify
8237  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8238  (SIGNBIT @0)
8239  (if (!HONOR_SIGNED_ZEROS (@0))
8240   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8242 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8243 (for cmp (eq ne)
8244  (for op (plus minus)
8245       rop (minus plus)
8246   (simplify
8247    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8248    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8249         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8250         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8251         && !TYPE_SATURATING (TREE_TYPE (@0)))
8252     (with { tree res = int_const_binop (rop, @2, @1); }
8253      (if (TREE_OVERFLOW (res)
8254           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8255       { constant_boolean_node (cmp == NE_EXPR, type); }
8256       (if (single_use (@3))
8257        (cmp @0 { TREE_OVERFLOW (res)
8258                  ? drop_tree_overflow (res) : res; }))))))))
8259 (for cmp (lt le gt ge)
8260  (for op (plus minus)
8261       rop (minus plus)
8262   (simplify
8263    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8264    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8265         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8266     (with { tree res = int_const_binop (rop, @2, @1); }
8267      (if (TREE_OVERFLOW (res))
8268       {
8269         fold_overflow_warning (("assuming signed overflow does not occur "
8270                                 "when simplifying conditional to constant"),
8271                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8272         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8273         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8274         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8275                                   TYPE_SIGN (TREE_TYPE (@1)))
8276                         != (op == MINUS_EXPR);
8277         constant_boolean_node (less == ovf_high, type);
8278       }
8279       (if (single_use (@3))
8280        (with
8281         {
8282           fold_overflow_warning (("assuming signed overflow does not occur "
8283                                   "when changing X +- C1 cmp C2 to "
8284                                   "X cmp C2 -+ C1"),
8285                                  WARN_STRICT_OVERFLOW_COMPARISON);
8286         }
8287         (cmp @0 { res; })))))))))
8289 /* Canonicalizations of BIT_FIELD_REFs.  */
8291 (simplify
8292  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8293  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8295 (simplify
8296  (BIT_FIELD_REF (view_convert @0) @1 @2)
8297  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8298       || type_has_mode_precision_p (TREE_TYPE (@0)))
8299   (BIT_FIELD_REF @0 @1 @2)))
8301 (simplify
8302  (BIT_FIELD_REF @0 @1 integer_zerop)
8303  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8304   (view_convert @0)))
8306 (simplify
8307  (BIT_FIELD_REF @0 @1 @2)
8308  (switch
8309   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8310        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8311    (switch
8312     (if (integer_zerop (@2))
8313      (view_convert (realpart @0)))
8314     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8315      (view_convert (imagpart @0)))))
8316   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8317        && INTEGRAL_TYPE_P (type)
8318        /* On GIMPLE this should only apply to register arguments.  */
8319        && (! GIMPLE || is_gimple_reg (@0))
8320        /* A bit-field-ref that referenced the full argument can be stripped.  */
8321        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8322             && integer_zerop (@2))
8323            /* Low-parts can be reduced to integral conversions.
8324               ???  The following doesn't work for PDP endian.  */
8325            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8326                /* But only do this after vectorization.  */
8327                && canonicalize_math_after_vectorization_p ()
8328                /* Don't even think about BITS_BIG_ENDIAN.  */
8329                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8330                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8331                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8332                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8333                                             - TYPE_PRECISION (type))
8334                                          : 0)) == 0)))
8335    (convert @0))))
8337 /* Simplify vector extracts.  */
8339 (simplify
8340  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8341  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8342       && tree_fits_uhwi_p (TYPE_SIZE (type))
8343       && ((tree_to_uhwi (TYPE_SIZE (type))
8344            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8345           || (VECTOR_TYPE_P (type)
8346               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8347                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8348   (with
8349    {
8350      tree ctor = (TREE_CODE (@0) == SSA_NAME
8351                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8352      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8353      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8354      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8355      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8356    }
8357    (if (n != 0
8358         && (idx % width) == 0
8359         && (n % width) == 0
8360         && known_le ((idx + n) / width,
8361                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8362     (with
8363      {
8364        idx = idx / width;
8365        n = n / width;
8366        /* Constructor elements can be subvectors.  */
8367        poly_uint64 k = 1;
8368        if (CONSTRUCTOR_NELTS (ctor) != 0)
8369          {
8370            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8371            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8372              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8373          }
8374        unsigned HOST_WIDE_INT elt, count, const_k;
8375      }
8376      (switch
8377       /* We keep an exact subset of the constructor elements.  */
8378       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8379        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8380         { build_zero_cst (type); }
8381         (if (count == 1)
8382          (if (elt < CONSTRUCTOR_NELTS (ctor))
8383           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8384           { build_zero_cst (type); })
8385          /* We don't want to emit new CTORs unless the old one goes away.
8386             ???  Eventually allow this if the CTOR ends up constant or
8387             uniform.  */
8388          (if (single_use (@0))
8389           (with
8390             {
8391               vec<constructor_elt, va_gc> *vals;
8392               vec_alloc (vals, count);
8393               bool constant_p = true;
8394               tree res;
8395               for (unsigned i = 0;
8396                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8397                 {
8398                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8399                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8400                   if (!CONSTANT_CLASS_P (e))
8401                     constant_p = false;
8402                 }
8403               tree evtype = (types_match (TREE_TYPE (type),
8404                                           TREE_TYPE (TREE_TYPE (ctor)))
8405                              ? type
8406                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8407                                                   count * k));
8408               /* We used to build a CTOR in the non-constant case here
8409                  but that's not a GIMPLE value.  We'd have to expose this
8410                  operation somehow so the code generation can properly
8411                  split it out to a separate stmt.  */
8412               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8413                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8414             }
8415             (if (res)
8416              (view_convert { res; })))))))
8417       /* The bitfield references a single constructor element.  */
8418       (if (k.is_constant (&const_k)
8419            && idx + n <= (idx / const_k + 1) * const_k)
8420        (switch
8421         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8422          { build_zero_cst (type); })
8423         (if (n == const_k)
8424          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8425         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8426                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8428 /* Simplify a bit extraction from a bit insertion for the cases with
8429    the inserted element fully covering the extraction or the insertion
8430    not touching the extraction.  */
8431 (simplify
8432  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8433  (with
8434   {
8435     unsigned HOST_WIDE_INT isize;
8436     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8437       isize = TYPE_PRECISION (TREE_TYPE (@1));
8438     else
8439       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8440   }
8441   (switch
8442    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8443          || type_has_mode_precision_p (TREE_TYPE (@1)))
8444         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8445         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8446                       wi::to_wide (@ipos) + isize))
8447     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8448                                                  wi::to_wide (@rpos)
8449                                                  - wi::to_wide (@ipos)); }))
8450    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8451         && compare_tree_int (@rsize, isize) == 0)
8452     (convert @1))
8453    (if (wi::geu_p (wi::to_wide (@ipos),
8454                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8455         || wi::geu_p (wi::to_wide (@rpos),
8456                       wi::to_wide (@ipos) + isize))
8457     (BIT_FIELD_REF @0 @rsize @rpos)))))
8459 /* Simplify vector inserts of other vector extracts to a permute.  */
8460 (simplify
8461  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8462  (if (VECTOR_TYPE_P (type)
8463       && types_match (@0, @1)
8464       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8465       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8466   (with
8467    {
8468      unsigned HOST_WIDE_INT elsz
8469        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8470      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8471      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8472      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8473      vec_perm_builder builder;
8474      builder.new_vector (nunits, nunits, 1);
8475      for (unsigned i = 0; i < nunits; ++i)
8476        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8477      vec_perm_indices sel (builder, 2, nunits);
8478    }
8479    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8480         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8481     (vec_perm @0 @1 { vec_perm_indices_to_tree
8482                         (build_vector_type (ssizetype, nunits), sel); })))))
8484 (if (canonicalize_math_after_vectorization_p ())
8485  (for fmas (FMA)
8486   (simplify
8487    (fmas:c (negate @0) @1 @2)
8488    (IFN_FNMA @0 @1 @2))
8489   (simplify
8490    (fmas @0 @1 (negate @2))
8491    (IFN_FMS @0 @1 @2))
8492   (simplify
8493    (fmas:c (negate @0) @1 (negate @2))
8494    (IFN_FNMS @0 @1 @2))
8495   (simplify
8496    (negate (fmas@3 @0 @1 @2))
8497    (if (single_use (@3))
8498     (IFN_FNMS @0 @1 @2))))
8500  (simplify
8501   (IFN_FMS:c (negate @0) @1 @2)
8502   (IFN_FNMS @0 @1 @2))
8503  (simplify
8504   (IFN_FMS @0 @1 (negate @2))
8505   (IFN_FMA @0 @1 @2))
8506  (simplify
8507   (IFN_FMS:c (negate @0) @1 (negate @2))
8508   (IFN_FNMA @0 @1 @2))
8509  (simplify
8510   (negate (IFN_FMS@3 @0 @1 @2))
8511    (if (single_use (@3))
8512     (IFN_FNMA @0 @1 @2)))
8514  (simplify
8515   (IFN_FNMA:c (negate @0) @1 @2)
8516   (IFN_FMA @0 @1 @2))
8517  (simplify
8518   (IFN_FNMA @0 @1 (negate @2))
8519   (IFN_FNMS @0 @1 @2))
8520  (simplify
8521   (IFN_FNMA:c (negate @0) @1 (negate @2))
8522   (IFN_FMS @0 @1 @2))
8523  (simplify
8524   (negate (IFN_FNMA@3 @0 @1 @2))
8525   (if (single_use (@3))
8526    (IFN_FMS @0 @1 @2)))
8528  (simplify
8529   (IFN_FNMS:c (negate @0) @1 @2)
8530   (IFN_FMS @0 @1 @2))
8531  (simplify
8532   (IFN_FNMS @0 @1 (negate @2))
8533   (IFN_FNMA @0 @1 @2))
8534  (simplify
8535   (IFN_FNMS:c (negate @0) @1 (negate @2))
8536   (IFN_FMA @0 @1 @2))
8537  (simplify
8538   (negate (IFN_FNMS@3 @0 @1 @2))
8539   (if (single_use (@3))
8540    (IFN_FMA @0 @1 @2))))
8542 /* CLZ simplifications.  */
8543 (for clz (CLZ)
8544  (for op (eq ne)
8545       cmp (lt ge)
8546   (simplify
8547    (op (clz:s@2 @0) INTEGER_CST@1)
8548    (if (integer_zerop (@1) && single_use (@2))
8549     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8550     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8551      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8552     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8553     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8554      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8555 (for op (eq ne)
8556      cmp (lt ge)
8557  (simplify
8558   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8559   (if (integer_zerop (@1) && single_use (@2))
8560    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8561    (with { tree type0 = TREE_TYPE (@0);
8562            tree stype = signed_type_for (TREE_TYPE (@0));
8563            /* Punt if clz(0) == 0.  */
8564            if (integer_zerop (@3))
8565              stype = NULL_TREE;
8566          }
8567     (if (stype)
8568      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8569    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8570    (with { bool ok = true;
8571            tree type0 = TREE_TYPE (@0);
8572            /* Punt if clz(0) == prec - 1.  */
8573            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8574              ok = false;
8575          }
8576     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8577      (op @0 { build_one_cst (type0); }))))))
8579 /* CTZ simplifications.  */
8580 (for ctz (CTZ)
8581  (for op (ge gt le lt)
8582       cmp (eq eq ne ne)
8583   (simplify
8584    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8585    (op (ctz:s @0) INTEGER_CST@1)
8586     (with { bool ok = true;
8587             HOST_WIDE_INT val = 0;
8588             if (!tree_fits_shwi_p (@1))
8589               ok = false;
8590             else
8591               {
8592                 val = tree_to_shwi (@1);
8593                 /* Canonicalize to >= or <.  */
8594                 if (op == GT_EXPR || op == LE_EXPR)
8595                   {
8596                     if (val == HOST_WIDE_INT_MAX)
8597                       ok = false;
8598                     else
8599                       val++;
8600                   }
8601               }
8602             tree type0 = TREE_TYPE (@0);
8603             int prec = TYPE_PRECISION (type0);
8604           }
8605      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8606       (if (val <= 0)
8607        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8608        (if (val >= prec)
8609         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8610         (cmp (bit_and @0 { wide_int_to_tree (type0,
8611                                              wi::mask (val, false, prec)); })
8612              { build_zero_cst (type0); })))))))
8613  (for op (eq ne)
8614   (simplify
8615    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8616    (op (ctz:s @0) INTEGER_CST@1)
8617     (with { tree type0 = TREE_TYPE (@0);
8618             int prec = TYPE_PRECISION (type0);
8619           }
8620      (if (prec <= MAX_FIXED_MODE_SIZE)
8621       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8622        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8623        (op (bit_and @0 { wide_int_to_tree (type0,
8624                                            wi::mask (tree_to_uhwi (@1) + 1,
8625                                                      false, prec)); })
8626            { wide_int_to_tree (type0,
8627                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8628                                                  false, prec)); })))))))
8629 (for op (ge gt le lt)
8630      cmp (eq eq ne ne)
8631  (simplify
8632   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8633   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8634    (with { bool ok = true;
8635            HOST_WIDE_INT val = 0;
8636            if (!tree_fits_shwi_p (@1))
8637              ok = false;
8638            else
8639              {
8640                val = tree_to_shwi (@1);
8641                /* Canonicalize to >= or <.  */
8642                if (op == GT_EXPR || op == LE_EXPR)
8643                  {
8644                    if (val == HOST_WIDE_INT_MAX)
8645                      ok = false;
8646                    else
8647                      val++;
8648                  }
8649              }
8650            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8651            tree type0 = TREE_TYPE (@0);
8652            int prec = TYPE_PRECISION (type0);
8653            if (prec > MAX_FIXED_MODE_SIZE)
8654              ok = false;
8655           }
8656      (if (val <= 0)
8657       (if (ok && zero_val >= val)
8658        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8659       (if (val >= prec)
8660        (if (ok && zero_val < val)
8661         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8662        (if (ok && (zero_val < 0 || zero_val >= prec))
8663         (cmp (bit_and @0 { wide_int_to_tree (type0,
8664                                              wi::mask (val, false, prec)); })
8665              { build_zero_cst (type0); })))))))
8666 (for op (eq ne)
8667  (simplify
8668   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8669   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8670    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8671            tree type0 = TREE_TYPE (@0);
8672            int prec = TYPE_PRECISION (type0);
8673          }
8674     (if (prec <= MAX_FIXED_MODE_SIZE)
8675      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8676       (if (zero_val != wi::to_widest (@1))
8677        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8678       (if (zero_val < 0 || zero_val >= prec)
8679        (op (bit_and @0 { wide_int_to_tree (type0,
8680                                            wi::mask (tree_to_uhwi (@1) + 1,
8681                                                      false, prec)); })
8682            { wide_int_to_tree (type0,
8683                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8684                                                  false, prec)); })))))))
8686 #if GIMPLE
8687 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
8688 (simplify
8689   (CTZ (convert@1 @0))
8690   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8691        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8692        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8693    (with { combined_fn cfn = CFN_LAST;
8694            tree type0 = TREE_TYPE (@0);
8695            if (TREE_CODE (type0) == BITINT_TYPE)
8696              {
8697                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8698                  cfn = CFN_CTZ;
8699                else
8700                  type0
8701                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8702                                                      1);
8703              }
8704            type0 = unsigned_type_for (type0);
8705            if (cfn == CFN_LAST
8706                && direct_internal_fn_supported_p (IFN_CTZ, type0,
8707                                                   OPTIMIZE_FOR_BOTH))
8708              cfn = CFN_CTZ;
8709            if (cfn == CFN_LAST
8710                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8711                && !direct_internal_fn_supported_p (IFN_CTZ,
8712                                                    TREE_TYPE (@1),
8713                                                    OPTIMIZE_FOR_BOTH))
8714              {
8715                if (TYPE_PRECISION (type0)
8716                    == TYPE_PRECISION (unsigned_type_node))
8717                  cfn = CFN_BUILT_IN_CTZ;
8718                else if (TYPE_PRECISION (type0)
8719                         == TYPE_PRECISION (long_long_unsigned_type_node))
8720                  cfn = CFN_BUILT_IN_CTZLL;
8721              } }
8722     (if (cfn == CFN_CTZ)
8723      (IFN_CTZ (convert:type0 @0))
8724      (if (cfn == CFN_BUILT_IN_CTZ)
8725       (BUILT_IN_CTZ (convert:type0 @0))
8726       (if (cfn == CFN_BUILT_IN_CTZLL)
8727        (BUILT_IN_CTZLL (convert:type0 @0))))))))
8728 #endif
8730 /* POPCOUNT simplifications.  */
8731 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8732 (simplify
8733   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8734   (if (INTEGRAL_TYPE_P (type)
8735        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8736     (POPCOUNT (bit_ior @0 @1))))
8738 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8739 (for popcount (POPCOUNT)
8740   (for cmp (le eq ne gt)
8741        rep (eq eq ne ne)
8742     (simplify
8743       (cmp (popcount @0) integer_zerop)
8744       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8746 /* popcount(bswap(x)) is popcount(x).  */
8747 (for popcount (POPCOUNT)
8748   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8749               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8750     (simplify
8751       (popcount (convert?@0 (bswap:s@1 @2)))
8752       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8753            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8754         (with { tree type0 = TREE_TYPE (@0);
8755                 tree type1 = TREE_TYPE (@1);
8756                 unsigned int prec0 = TYPE_PRECISION (type0);
8757                 unsigned int prec1 = TYPE_PRECISION (type1); }
8758           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8759             (popcount (convert:type0 (convert:type1 @2)))))))))
8761 /* popcount(rotate(X Y)) is popcount(X).  */
8762 (for popcount (POPCOUNT)
8763   (for rot (lrotate rrotate)
8764     (simplify
8765       (popcount (convert?@0 (rot:s@1 @2 @3)))
8766       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8767            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8768            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8769         (with { tree type0 = TREE_TYPE (@0);
8770                 tree type1 = TREE_TYPE (@1);
8771                 unsigned int prec0 = TYPE_PRECISION (type0);
8772                 unsigned int prec1 = TYPE_PRECISION (type1); }
8773           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8774             (popcount (convert:type0 @2))))))))
8776 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8777 (simplify
8778   (bit_and (POPCOUNT @0) integer_onep)
8779   (PARITY @0))
8781 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8782 (simplify
8783   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8784   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8786 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8787 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8788 (for popcount (POPCOUNT)
8789   (for log1 (bit_and bit_ior)
8790        log2 (bit_ior bit_and)
8791     (simplify
8792       (minus (plus:s (popcount:s @0) (popcount:s @1))
8793              (popcount:s (log1:cs @0 @1)))
8794       (popcount (log2 @0 @1)))
8795     (simplify
8796       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8797               (popcount:s @1))
8798       (popcount (log2 @0 @1)))))
8800 #if GIMPLE
8801 /* popcount(zext(X)) == popcount(X).  */
8802 (simplify
8803   (POPCOUNT (convert@1 @0))
8804   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8805        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8806        && TYPE_UNSIGNED (TREE_TYPE (@0))
8807        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8808    (with { combined_fn cfn = CFN_LAST;
8809            tree type0 = TREE_TYPE (@0);
8810            if (TREE_CODE (type0) == BITINT_TYPE)
8811              {
8812                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8813                  cfn = CFN_POPCOUNT;
8814                else
8815                  type0
8816                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8817                                                      1);
8818              }
8819            if (cfn == CFN_LAST
8820                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8821                                                   OPTIMIZE_FOR_BOTH))
8822              cfn = CFN_POPCOUNT;
8823            if (cfn == CFN_LAST
8824                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8825                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8826                                                    TREE_TYPE (@1),
8827                                                    OPTIMIZE_FOR_BOTH))
8828              {
8829                if (TYPE_PRECISION (type0)
8830                    == TYPE_PRECISION (unsigned_type_node))
8831                  cfn = CFN_BUILT_IN_POPCOUNT;
8832                else if (TYPE_PRECISION (type0)
8833                         == TYPE_PRECISION (long_long_unsigned_type_node))
8834                  cfn = CFN_BUILT_IN_POPCOUNTLL;
8835              } }
8836     (if (cfn == CFN_POPCOUNT)
8837      (IFN_POPCOUNT (convert:type0 @0))
8838      (if (cfn == CFN_BUILT_IN_POPCOUNT)
8839       (BUILT_IN_POPCOUNT (convert:type0 @0))
8840       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8841        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8842 #endif
8844 /* PARITY simplifications.  */
8845 /* parity(~X) is parity(X).  */
8846 (simplify
8847   (PARITY (bit_not @0))
8848   (PARITY @0))
8850 /* parity(bswap(x)) is parity(x).  */
8851 (for parity (PARITY)
8852   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8853               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8854     (simplify
8855       (parity (convert?@0 (bswap:s@1 @2)))
8856       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8857            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8858            && TYPE_PRECISION (TREE_TYPE (@0))
8859               >= TYPE_PRECISION (TREE_TYPE (@1)))
8860         (with { tree type0 = TREE_TYPE (@0);
8861                 tree type1 = TREE_TYPE (@1); }
8862           (parity (convert:type0 (convert:type1 @2))))))))
8864 /* parity(rotate(X Y)) is parity(X).  */
8865 (for parity (PARITY)
8866   (for rot (lrotate rrotate)
8867     (simplify
8868       (parity (convert?@0 (rot:s@1 @2 @3)))
8869       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8870            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8871            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8872            && TYPE_PRECISION (TREE_TYPE (@0))
8873               >= TYPE_PRECISION (TREE_TYPE (@1)))
8874         (with { tree type0 = TREE_TYPE (@0); }
8875           (parity (convert:type0 @2)))))))
8877 /* parity(X)^parity(Y) is parity(X^Y).  */
8878 (simplify
8879   (bit_xor (PARITY:s @0) (PARITY:s @1))
8880   (PARITY (bit_xor @0 @1)))
8882 #if GIMPLE
8883 /* parity(zext(X)) == parity(X).  */
8884 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
8885 (simplify
8886   (PARITY (convert@1 @0))
8887   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8888        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8889        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8890        && (TYPE_UNSIGNED (TREE_TYPE (@0))
8891            || ((TYPE_PRECISION (TREE_TYPE (@1))
8892                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8893    (with { combined_fn cfn = CFN_LAST;
8894            tree type0 = TREE_TYPE (@0);
8895            if (TREE_CODE (type0) == BITINT_TYPE)
8896              {
8897                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8898                  cfn = CFN_PARITY;
8899                else
8900                  type0
8901                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8902                                                      1);
8903              }
8904            type0 = unsigned_type_for (type0);
8905            if (cfn == CFN_LAST
8906                && direct_internal_fn_supported_p (IFN_PARITY, type0,
8907                                                   OPTIMIZE_FOR_BOTH))
8908              cfn = CFN_PARITY;
8909            if (cfn == CFN_LAST
8910                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8911                && !direct_internal_fn_supported_p (IFN_PARITY,
8912                                                    TREE_TYPE (@1),
8913                                                    OPTIMIZE_FOR_BOTH))
8914              {
8915                if (TYPE_PRECISION (type0)
8916                    == TYPE_PRECISION (unsigned_type_node))
8917                  cfn = CFN_BUILT_IN_PARITY;
8918                else if (TYPE_PRECISION (type0)
8919                         == TYPE_PRECISION (long_long_unsigned_type_node))
8920                  cfn = CFN_BUILT_IN_PARITYLL;
8921              } }
8922     (if (cfn == CFN_PARITY)
8923      (IFN_PARITY (convert:type0 @0))
8924      (if (cfn == CFN_BUILT_IN_PARITY)
8925       (BUILT_IN_PARITY (convert:type0 @0))
8926       (if (cfn == CFN_BUILT_IN_PARITYLL)
8927        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
8928 #endif
8930 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8931 (for func (POPCOUNT BSWAP FFS PARITY)
8932  (simplify
8933   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8934   @3))
8936 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8937    where CST is precision-1. */
8938 (for func (CLRSB)
8939  (simplify
8940   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8941   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8942    @4)))
8944 #if GIMPLE
8945 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8946 (for func (CLZ)
8947  (simplify
8948   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8949   (with { int val;
8950           internal_fn ifn = IFN_LAST;
8951           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8952             {
8953               if (tree_fits_shwi_p (@2))
8954                 {
8955                   HOST_WIDE_INT valw = tree_to_shwi (@2);
8956                   if ((int) valw == valw)
8957                     {
8958                       val = valw;
8959                       ifn = IFN_CLZ;
8960                     }
8961                 }
8962             }
8963           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8964                                                    OPTIMIZE_FOR_BOTH)
8965                    && CLZ_DEFINED_VALUE_AT_ZERO
8966                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8967             ifn = IFN_CLZ;
8968         }
8969    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8970     (IFN_CLZ @3 @2)))))
8971 (simplify
8972  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
8973   (with { int val;
8974           internal_fn ifn = IFN_LAST;
8975           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8976             ifn = IFN_CLZ;
8977           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8978                                                    OPTIMIZE_FOR_BOTH))
8979             ifn = IFN_CLZ;
8980         }
8981    (if (ifn == IFN_CLZ)
8982     (IFN_CLZ @3 @2))))
8984 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8985 (for func (CTZ)
8986  (simplify
8987   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8988   (with { int val;
8989           internal_fn ifn = IFN_LAST;
8990           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8991             {
8992               if (tree_fits_shwi_p (@2))
8993                 {
8994                   HOST_WIDE_INT valw = tree_to_shwi (@2);
8995                   if ((int) valw == valw)
8996                     {
8997                       val = valw;
8998                       ifn = IFN_CTZ;
8999                     }
9000                 }
9001             }
9002           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9003                                                    OPTIMIZE_FOR_BOTH)
9004                    && CTZ_DEFINED_VALUE_AT_ZERO
9005                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9006             ifn = IFN_CTZ;
9007         }
9008    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9009     (IFN_CTZ @3 @2)))))
9010 (simplify
9011  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9012   (with { int val;
9013           internal_fn ifn = IFN_LAST;
9014           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9015             ifn = IFN_CTZ;
9016           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9017                                                    OPTIMIZE_FOR_BOTH))
9018             ifn = IFN_CTZ;
9019         }
9020    (if (ifn == IFN_CTZ)
9021     (IFN_CTZ @3 @2))))
9022 #endif
9024 /* Common POPCOUNT/PARITY simplifications.  */
9025 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9026 (for pfun (POPCOUNT PARITY)
9027   (simplify
9028     (pfun @0)
9029     (if (INTEGRAL_TYPE_P (type))
9030      (with { wide_int nz = tree_nonzero_bits (@0); }
9031        (switch
9032          (if (nz == 1)
9033            (convert @0))
9034          (if (wi::popcount (nz) == 1)
9035            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9036              (convert (rshift:utype (convert:utype @0)
9037                                     { build_int_cst (integer_type_node,
9038                                                      wi::ctz (nz)); })))))))))
9040 #if GIMPLE
9041 /* 64- and 32-bits branchless implementations of popcount are detected:
9043    int popcount64c (uint64_t x)
9044    {
9045      x -= (x >> 1) & 0x5555555555555555ULL;
9046      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9047      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9048      return (x * 0x0101010101010101ULL) >> 56;
9049    }
9051    int popcount32c (uint32_t x)
9052    {
9053      x -= (x >> 1) & 0x55555555;
9054      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9055      x = (x + (x >> 4)) & 0x0f0f0f0f;
9056      return (x * 0x01010101) >> 24;
9057    }  */
9058 (simplify
9059  (rshift
9060   (mult
9061    (bit_and
9062     (plus:c
9063      (rshift @8 INTEGER_CST@5)
9064       (plus:c@8
9065        (bit_and @6 INTEGER_CST@7)
9066         (bit_and
9067          (rshift
9068           (minus@6 @0
9069            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9070           INTEGER_CST@10)
9071          INTEGER_CST@9)))
9072     INTEGER_CST@3)
9073    INTEGER_CST@2)
9074   INTEGER_CST@1)
9075   /* Check constants and optab.  */
9076   (with { unsigned prec = TYPE_PRECISION (type);
9077           int shift = (64 - prec) & 63;
9078           unsigned HOST_WIDE_INT c1
9079             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9080           unsigned HOST_WIDE_INT c2
9081             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9082           unsigned HOST_WIDE_INT c3
9083             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9084           unsigned HOST_WIDE_INT c4
9085             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9086    }
9087    (if (prec >= 16
9088         && prec <= 64
9089         && pow2p_hwi (prec)
9090         && TYPE_UNSIGNED (type)
9091         && integer_onep (@4)
9092         && wi::to_widest (@10) == 2
9093         && wi::to_widest (@5) == 4
9094         && wi::to_widest (@1) == prec - 8
9095         && tree_to_uhwi (@2) == c1
9096         && tree_to_uhwi (@3) == c2
9097         && tree_to_uhwi (@9) == c3
9098         && tree_to_uhwi (@7) == c3
9099         && tree_to_uhwi (@11) == c4)
9100     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9101                                          OPTIMIZE_FOR_BOTH))
9102      (convert (IFN_POPCOUNT:type @0))
9103      /* Try to do popcount in two halves.  PREC must be at least
9104         five bits for this to work without extension before adding.  */
9105      (with {
9106        tree half_type = NULL_TREE;
9107        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9108        int half_prec = 8;
9109        if (m.exists ()
9110            && m.require () != TYPE_MODE (type))
9111          {
9112            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9113            half_type = build_nonstandard_integer_type (half_prec, 1);
9114          }
9115        gcc_assert (half_prec > 2);
9116       }
9117       (if (half_type != NULL_TREE
9118            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9119                                               OPTIMIZE_FOR_BOTH))
9120        (convert (plus
9121          (IFN_POPCOUNT:half_type (convert @0))
9122          (IFN_POPCOUNT:half_type (convert (rshift @0
9123             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9125 /* __builtin_ffs needs to deal on many targets with the possible zero
9126    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9127    should lead to better code.  */
9128 (simplify
9129  (FFS tree_expr_nonzero_p@0)
9130  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9131       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9132                                          OPTIMIZE_FOR_SPEED))
9133   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9134    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9135 #endif
9137 (for ffs (FFS)
9138  /* __builtin_ffs (X) == 0 -> X == 0.
9139     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9140  (for cmp (eq ne)
9141   (simplify
9142    (cmp (ffs@2 @0) INTEGER_CST@1)
9143     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9144      (switch
9145       (if (integer_zerop (@1))
9146        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9147       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9148        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9149       (if (single_use (@2))
9150        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9151                                             wi::mask (tree_to_uhwi (@1),
9152                                                       false, prec)); })
9153             { wide_int_to_tree (TREE_TYPE (@0),
9154                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9155                                                   false, prec)); }))))))
9157  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9158  (for cmp (gt le)
9159       cmp2 (ne eq)
9160       cmp3 (eq ne)
9161       bit_op (bit_and bit_ior)
9162   (simplify
9163    (cmp (ffs@2 @0) INTEGER_CST@1)
9164     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9165      (switch
9166       (if (integer_zerop (@1))
9167        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9168       (if (tree_int_cst_sgn (@1) < 0)
9169        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9170       (if (wi::to_widest (@1) >= prec)
9171        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9172       (if (wi::to_widest (@1) == prec - 1)
9173        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9174                                     wi::shifted_mask (prec - 1, 1,
9175                                                       false, prec)); }))
9176       (if (single_use (@2))
9177        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9178                (cmp3 (bit_and @0
9179                               { wide_int_to_tree (TREE_TYPE (@0),
9180                                                   wi::mask (tree_to_uhwi (@1),
9181                                                   false, prec)); })
9182                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9184 #if GIMPLE
9185 /* ffs(ext(X)) == ffs(X).  */
9186 (simplify
9187   (FFS (convert@1 @0))
9188   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9189        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9190        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9191    (with { combined_fn cfn = CFN_LAST;
9192            tree type0 = TREE_TYPE (@0);
9193            if (TREE_CODE (type0) == BITINT_TYPE)
9194              {
9195                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9196                  cfn = CFN_FFS;
9197                else
9198                  type0
9199                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9200                                                      0);
9201              }
9202            type0 = signed_type_for (type0);
9203            if (cfn == CFN_LAST
9204                && direct_internal_fn_supported_p (IFN_FFS, type0,
9205                                                   OPTIMIZE_FOR_BOTH))
9206              cfn = CFN_FFS;
9207            if (cfn == CFN_LAST
9208                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9209                && !direct_internal_fn_supported_p (IFN_FFS,
9210                                                    TREE_TYPE (@1),
9211                                                    OPTIMIZE_FOR_BOTH))
9212              {
9213                if (TYPE_PRECISION (type0)
9214                    == TYPE_PRECISION (integer_type_node))
9215                  cfn = CFN_BUILT_IN_FFS;
9216                else if (TYPE_PRECISION (type0)
9217                         == TYPE_PRECISION (long_long_integer_type_node))
9218                  cfn = CFN_BUILT_IN_FFSLL;
9219              } }
9220     (if (cfn == CFN_FFS)
9221      (IFN_FFS (convert:type0 @0))
9222      (if (cfn == CFN_BUILT_IN_FFS)
9223       (BUILT_IN_FFS (convert:type0 @0))
9224       (if (cfn == CFN_BUILT_IN_FFSLL)
9225        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9226 #endif
9228 #if GIMPLE
9230 /* Simplify:
9231      a = op a1
9232      r = cond ? a : b
9233      --> r = .COND_FN (cond, a, b)
9234 and,
9235     a = op a1
9236     r = cond ? b : a
9237     --> r = .COND_FN (~cond, b, a).  */
9239 (for uncond_op (UNCOND_UNARY)
9240      cond_op (COND_UNARY)
9241  (simplify
9242   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9243    (with { tree op_type = TREE_TYPE (@3); }
9244     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9245         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9246      (cond_op @0 (view_convert @1) @2))))
9247  (simplify
9248   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9249    (with { tree op_type = TREE_TYPE (@3); }
9250     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9251         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9252      (cond_op (bit_not @0) (view_convert @2) @1)))))
9254 (for uncond_op (UNCOND_UNARY)
9255      cond_op (COND_LEN_UNARY)
9256  (simplify
9257   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9258    (with { tree op_type = TREE_TYPE (@3); }
9259     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9260         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9261      (cond_op @0 (view_convert @1) @2 @4 @5))))
9262  (simplify
9263   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9264    (with { tree op_type = TREE_TYPE (@3); }
9265     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9266         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9267      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9269 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9270 (simplify
9271  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9272  (if (canonicalize_math_after_vectorization_p ()
9273       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9274       && is_truth_type_for (type, TREE_TYPE (@0)))
9275  (if (integer_all_onesp (@1) && integer_zerop (@2))
9276   (IFN_COND_NOT @0 @3 @3))
9277   (if (integer_all_onesp (@2) && integer_zerop (@1))
9278    (IFN_COND_NOT (bit_not @0) @3 @3))))
9280 /* Simplify:
9282      a = a1 op a2
9283      r = c ? a : b;
9285    to:
9287      r = c ? a1 op a2 : b;
9289    if the target can do it in one go.  This makes the operation conditional
9290    on c, so could drop potentially-trapping arithmetic, but that's a valid
9291    simplification if the result of the operation isn't needed.
9293    Avoid speculatively generating a stand-alone vector comparison
9294    on targets that might not support them.  Any target implementing
9295    conditional internal functions must support the same comparisons
9296    inside and outside a VEC_COND_EXPR.  */
9298 (for uncond_op (UNCOND_BINARY)
9299      cond_op (COND_BINARY)
9300  (simplify
9301   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9302   (with { tree op_type = TREE_TYPE (@4); }
9303    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9304         && is_truth_type_for (op_type, TREE_TYPE (@0))
9305         && single_use (@4))
9306     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9307  (simplify
9308   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9309   (with { tree op_type = TREE_TYPE (@4); }
9310    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9311         && is_truth_type_for (op_type, TREE_TYPE (@0))
9312         && single_use (@4))
9313     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9315 (for uncond_op (UNCOND_BINARY)
9316      cond_op (COND_LEN_BINARY)
9317  (simplify
9318   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9319   (with { tree op_type = TREE_TYPE (@4); }
9320    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9321         && is_truth_type_for (op_type, TREE_TYPE (@0))
9322         && single_use (@4))
9323     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9324  (simplify
9325   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9326   (with { tree op_type = TREE_TYPE (@4); }
9327    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9328         && is_truth_type_for (op_type, TREE_TYPE (@0))
9329         && single_use (@4))
9330     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9332 /* Same for ternary operations.  */
9333 (for uncond_op (UNCOND_TERNARY)
9334      cond_op (COND_TERNARY)
9335  (simplify
9336   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9337   (with { tree op_type = TREE_TYPE (@5); }
9338    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9339         && is_truth_type_for (op_type, TREE_TYPE (@0))
9340         && single_use (@5))
9341     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9342  (simplify
9343   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9344   (with { tree op_type = TREE_TYPE (@5); }
9345    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9346         && is_truth_type_for (op_type, TREE_TYPE (@0))
9347         && single_use (@5))
9348     (view_convert (cond_op (bit_not @0) @2 @3 @4
9349                   (view_convert:op_type @1)))))))
9351 (for uncond_op (UNCOND_TERNARY)
9352      cond_op (COND_LEN_TERNARY)
9353  (simplify
9354   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9355   (with { tree op_type = TREE_TYPE (@5); }
9356    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9357         && is_truth_type_for (op_type, TREE_TYPE (@0))
9358         && single_use (@5))
9359     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9360  (simplify
9361   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9362   (with { tree op_type = TREE_TYPE (@5); }
9363    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9364         && is_truth_type_for (op_type, TREE_TYPE (@0))
9365         && single_use (@5))
9366     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9367 #endif
9369 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9370    "else" value of an IFN_COND_*.  */
9371 (for cond_op (COND_BINARY)
9372  (simplify
9373   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9374   (with { tree op_type = TREE_TYPE (@3); }
9375    (if (element_precision (type) == element_precision (op_type))
9376     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9377  (simplify
9378   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9379   (with { tree op_type = TREE_TYPE (@5); }
9380    (if (inverse_conditions_p (@0, @2)
9381         && element_precision (type) == element_precision (op_type))
9382     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9384 /* Same for ternary operations.  */
9385 (for cond_op (COND_TERNARY)
9386  (simplify
9387   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9388   (with { tree op_type = TREE_TYPE (@4); }
9389    (if (element_precision (type) == element_precision (op_type))
9390     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9391  (simplify
9392   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9393   (with { tree op_type = TREE_TYPE (@6); }
9394    (if (inverse_conditions_p (@0, @2)
9395         && element_precision (type) == element_precision (op_type))
9396     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9398 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9399    "else" value of an IFN_COND_LEN_*.  */
9400 (for cond_len_op (COND_LEN_BINARY)
9401  (simplify
9402   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9403   (with { tree op_type = TREE_TYPE (@3); }
9404    (if (element_precision (type) == element_precision (op_type))
9405     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9406  (simplify
9407   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9408   (with { tree op_type = TREE_TYPE (@5); }
9409    (if (inverse_conditions_p (@0, @2)
9410         && element_precision (type) == element_precision (op_type))
9411     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9413 /* Same for ternary operations.  */
9414 (for cond_len_op (COND_LEN_TERNARY)
9415  (simplify
9416   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9417   (with { tree op_type = TREE_TYPE (@4); }
9418    (if (element_precision (type) == element_precision (op_type))
9419     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9420  (simplify
9421   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9422   (with { tree op_type = TREE_TYPE (@6); }
9423    (if (inverse_conditions_p (@0, @2)
9424         && element_precision (type) == element_precision (op_type))
9425     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9427 /* Detect simplication for a conditional reduction where
9429    a = mask1 ? b : 0
9430    c = mask2 ? d + a : d
9432    is turned into
9434    c = mask1 && mask2 ? d + b : d.  */
9435 (simplify
9436   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9437    (if (ANY_INTEGRAL_TYPE_P (type)
9438         || (FLOAT_TYPE_P (type)
9439             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9440    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9442 /* Detect simplication for a conditional length reduction where
9444    a = mask ? b : 0
9445    c = i < len + bias ? d + a : d
9447    is turned into
9449    c = mask && i < len + bias ? d + b : d.  */
9450 (simplify
9451   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9452    (if (ANY_INTEGRAL_TYPE_P (type)
9453         || (FLOAT_TYPE_P (type)
9454             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9455     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9457 /* Detect simplification for vector condition folding where
9459   c = mask1 ? (masked_op mask2 a b) : b
9461   into
9463   c = masked_op (mask1 & mask2) a b
9465   where the operation can be partially applied to one operand. */
9467 (for cond_op (COND_BINARY)
9468  (simplify
9469   (vec_cond @0
9470    (cond_op:s @1 @2 @3 @4) @3)
9471   (cond_op (bit_and @1 @0) @2 @3 @4)))
9473 /* And same for ternary expressions.  */
9475 (for cond_op (COND_TERNARY)
9476  (simplify
9477   (vec_cond @0
9478    (cond_op:s @1 @2 @3 @4 @5) @4)
9479   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9481 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9482    expressions like:
9484    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9485    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9487    If pointers are known not to wrap, B checks whether @1 bytes starting
9488    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9489    bytes.  A is more efficiently tested as:
9491    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9493    The equivalent expression for B is given by replacing @1 with @1 - 1:
9495    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9497    @0 and @2 can be swapped in both expressions without changing the result.
9499    The folds rely on sizetype's being unsigned (which is always true)
9500    and on its being the same width as the pointer (which we have to check).
9502    The fold replaces two pointer_plus expressions, two comparisons and
9503    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9504    the best case it's a saving of two operations.  The A fold retains one
9505    of the original pointer_pluses, so is a win even if both pointer_pluses
9506    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9507    used elsewhere, since all we end up doing is replacing a comparison with
9508    a pointer_plus.  We do still apply the fold under those circumstances
9509    though, in case applying it to other conditions eventually makes one of the
9510    pointer_pluses dead.  */
9511 (for ior (truth_orif truth_or bit_ior)
9512  (for cmp (le lt)
9513   (simplify
9514    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9515         (cmp:cs (pointer_plus@4 @2 @1) @0))
9516    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9517         && TYPE_OVERFLOW_WRAPS (sizetype)
9518         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9519     /* Calculate the rhs constant.  */
9520     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9521             offset_int rhs = off * 2; }
9522      /* Always fails for negative values.  */
9523      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9524       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9525          pick a canonical order.  This increases the chances of using the
9526          same pointer_plus in multiple checks.  */
9527       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9528               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9529        (if (cmp == LT_EXPR)
9530         (gt (convert:sizetype
9531              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9532                                      { swap_p ? @0 : @2; }))
9533             { rhs_tree; })
9534         (gt (convert:sizetype
9535              (pointer_diff:ssizetype
9536               (pointer_plus { swap_p ? @2 : @0; }
9537                             { wide_int_to_tree (sizetype, off); })
9538               { swap_p ? @0 : @2; }))
9539             { rhs_tree; })))))))))
9541 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9542    element of @1.  */
9543 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9544  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9545   (with { int i = single_nonzero_element (@1); }
9546    (if (i >= 0)
9547     (with { tree elt = vector_cst_elt (@1, i);
9548             tree elt_type = TREE_TYPE (elt);
9549             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9550             tree size = bitsize_int (elt_bits);
9551             tree pos = bitsize_int (elt_bits * i); }
9552      (view_convert
9553       (bit_and:elt_type
9554        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9555        { elt; })))))))
9557 /* Fold reduction of a single nonzero element constructor.  */
9558 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9559   (simplify (reduc (CONSTRUCTOR@0))
9560     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9561                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9562             tree elt = ctor_single_nonzero_element (ctor); }
9563       (if (elt
9564            && !HONOR_SNANS (type)
9565            && !HONOR_SIGNED_ZEROS (type))
9566         { elt; }))))
9568 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9569 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9570             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9571      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9572   (simplify (reduc (op @0 VECTOR_CST@1))
9573     (op (reduc:type @0) (reduc:type @1))))
9575 /* Simplify vector floating point operations of alternating sub/add pairs
9576    into using an fneg of a wider element type followed by a normal add.
9577    under IEEE 754 the fneg of the wider type will negate every even entry
9578    and when doing an add we get a sub of the even and add of every odd
9579    elements.  */
9580 (for plusminus (plus minus)
9581      minusplus (minus plus)
9582  (simplify
9583   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9584    (if (!VECTOR_INTEGER_TYPE_P (type)
9585         && !FLOAT_WORDS_BIG_ENDIAN
9586         /* plus is commutative, while minus is not, so :c can't be used.
9587            Do equality comparisons by hand and at the end pick the operands
9588            from the minus.  */
9589         && (operand_equal_p (@0, @2, 0)
9590             ? operand_equal_p (@1, @3, 0)
9591             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9592    (with
9593     {
9594       /* Build a vector of integers from the tree mask.  */
9595       vec_perm_builder builder;
9596     }
9597     (if (tree_to_vec_perm_builder (&builder, @4))
9598      (with
9599       {
9600         /* Create a vec_perm_indices for the integer vector.  */
9601         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9602         vec_perm_indices sel (builder, 2, nelts);
9603         machine_mode vec_mode = TYPE_MODE (type);
9604         machine_mode wide_mode;
9605         scalar_mode wide_elt_mode;
9606         poly_uint64 wide_nunits;
9607         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9608       }
9609       (if (VECTOR_MODE_P (vec_mode)
9610            && sel.series_p (0, 2, 0, 2)
9611            && sel.series_p (1, 2, nelts + 1, 2)
9612            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9613            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9614            && related_vector_mode (vec_mode, wide_elt_mode,
9615                                    wide_nunits).exists (&wide_mode))
9616        (with
9617         {
9618           tree stype
9619             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9620                                               TYPE_UNSIGNED (type));
9621           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9623           /* The format has to be a non-extended ieee format.  */
9624           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9625           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9626         }
9627         (if (TYPE_MODE (stype) != BLKmode
9628              && VECTOR_TYPE_P (ntype)
9629              && fmt_old != NULL
9630              && fmt_new != NULL)
9631          (with
9632           {
9633             /* If the target doesn't support v1xx vectors, try using
9634                scalar mode xx instead.  */
9635             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9636                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9637               ntype = stype;
9638           }
9639           (if (fmt_new->signbit_rw
9640                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9641                && fmt_new->signbit_rw == fmt_new->signbit_ro
9642                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9643                                                  TYPE_MODE (type), ALL_REGS)
9644                && ((optimize_vectors_before_lowering_p ()
9645                     && VECTOR_TYPE_P (ntype))
9646                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9647            (if (plusminus == PLUS_EXPR)
9648             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9649             (minus @0 (view_convert:type
9650                         (negate (view_convert:ntype @1))))))))))))))))
9652 (simplify
9653  (vec_perm @0 @1 VECTOR_CST@2)
9654  (with
9655   {
9656     tree op0 = @0, op1 = @1, op2 = @2;
9657     machine_mode result_mode = TYPE_MODE (type);
9658     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9660     /* Build a vector of integers from the tree mask.  */
9661     vec_perm_builder builder;
9662   }
9663   (if (tree_to_vec_perm_builder (&builder, op2))
9664    (with
9665     {
9666       /* Create a vec_perm_indices for the integer vector.  */
9667       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9668       bool single_arg = (op0 == op1);
9669       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9670     }
9671     (if (sel.series_p (0, 1, 0, 1))
9672      { op0; }
9673      (if (sel.series_p (0, 1, nelts, 1))
9674       { op1; }
9675       (with
9676        {
9677          if (!single_arg)
9678            {
9679              if (sel.all_from_input_p (0))
9680                op1 = op0;
9681              else if (sel.all_from_input_p (1))
9682                {
9683                  op0 = op1;
9684                  sel.rotate_inputs (1);
9685                }
9686              else if (known_ge (poly_uint64 (sel[0]), nelts))
9687                {
9688                  std::swap (op0, op1);
9689                  sel.rotate_inputs (1);
9690                }
9691            }
9692          gassign *def;
9693          tree cop0 = op0, cop1 = op1;
9694          if (TREE_CODE (op0) == SSA_NAME
9695              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9696              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9697            cop0 = gimple_assign_rhs1 (def);
9698          if (TREE_CODE (op1) == SSA_NAME
9699              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9700              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9701            cop1 = gimple_assign_rhs1 (def);
9702          tree t;
9703        }
9704        (if ((TREE_CODE (cop0) == VECTOR_CST
9705              || TREE_CODE (cop0) == CONSTRUCTOR)
9706             && (TREE_CODE (cop1) == VECTOR_CST
9707                 || TREE_CODE (cop1) == CONSTRUCTOR)
9708             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9709         { t; }
9710         (with
9711          {
9712            bool changed = (op0 == op1 && !single_arg);
9713            tree ins = NULL_TREE;
9714            unsigned at = 0;
9716            /* See if the permutation is performing a single element
9717               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9718               in that case.  But only if the vector mode is supported,
9719               otherwise this is invalid GIMPLE.  */
9720            if (op_mode != BLKmode
9721                && (TREE_CODE (cop0) == VECTOR_CST
9722                    || TREE_CODE (cop0) == CONSTRUCTOR
9723                    || TREE_CODE (cop1) == VECTOR_CST
9724                    || TREE_CODE (cop1) == CONSTRUCTOR))
9725              {
9726                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9727                if (insert_first_p)
9728                  {
9729                    /* After canonicalizing the first elt to come from the
9730                       first vector we only can insert the first elt from
9731                       the first vector.  */
9732                    at = 0;
9733                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9734                      op0 = op1;
9735                  }
9736                /* The above can fail for two-element vectors which always
9737                   appear to insert the first element, so try inserting
9738                   into the second lane as well.  For more than two
9739                   elements that's wasted time.  */
9740                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9741                  {
9742                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9743                    for (at = 0; at < encoded_nelts; ++at)
9744                      if (maybe_ne (sel[at], at))
9745                        break;
9746                    if (at < encoded_nelts
9747                        && (known_eq (at + 1, nelts)
9748                            || sel.series_p (at + 1, 1, at + 1, 1)))
9749                      {
9750                        if (known_lt (poly_uint64 (sel[at]), nelts))
9751                          ins = fold_read_from_vector (cop0, sel[at]);
9752                        else
9753                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9754                      }
9755                  }
9756              }
9758            /* Generate a canonical form of the selector.  */
9759            if (!ins && sel.encoding () != builder)
9760              {
9761                /* Some targets are deficient and fail to expand a single
9762                   argument permutation while still allowing an equivalent
9763                   2-argument version.  */
9764                tree oldop2 = op2;
9765                if (sel.ninputs () == 2
9766                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9767                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9768                else
9769                  {
9770                    vec_perm_indices sel2 (builder, 2, nelts);
9771                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9772                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9773                    else
9774                      /* Not directly supported with either encoding,
9775                         so use the preferred form.  */
9776                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9777                  }
9778                if (!operand_equal_p (op2, oldop2, 0))
9779                  changed = true;
9780              }
9781          }
9782          (if (ins)
9783           (bit_insert { op0; } { ins; }
9784            { bitsize_int (at * vector_element_bits (type)); })
9785           (if (changed)
9786            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9788 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9790 (match vec_same_elem_p
9791  (vec_duplicate @0))
9793 (match vec_same_elem_p
9794  CONSTRUCTOR@0
9795  (if (TREE_CODE (@0) == SSA_NAME
9796       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9798 (match vec_same_elem_p
9799  @0
9800  (if (uniform_vector_p (@0))))
9803 (simplify
9804  (vec_perm vec_same_elem_p@0 @0 @1)
9805  (if (types_match (type, TREE_TYPE (@0)))
9806   @0
9807   (with
9808    {
9809      tree elem = uniform_vector_p (@0);
9810    }
9811    (if (elem)
9812     { build_vector_from_val (type, elem); }))))
9814 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9815 (simplify
9816  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9817  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9818   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9819 (simplify
9820  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9821  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9822   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9825 /* Merge
9826      c = VEC_PERM_EXPR <a, b, VCST0>;
9827      d = VEC_PERM_EXPR <c, c, VCST1>;
9828    to
9829      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9831 (simplify
9832  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9833  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9834   (with
9835    {
9836      machine_mode result_mode = TYPE_MODE (type);
9837      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9838      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9839      vec_perm_builder builder0;
9840      vec_perm_builder builder1;
9841      vec_perm_builder builder2 (nelts, nelts, 1);
9842    }
9843    (if (tree_to_vec_perm_builder (&builder0, @3)
9844         && tree_to_vec_perm_builder (&builder1, @4))
9845     (with
9846      {
9847        vec_perm_indices sel0 (builder0, 2, nelts);
9848        vec_perm_indices sel1 (builder1, 1, nelts);
9850        for (int i = 0; i < nelts; i++)
9851          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9853        vec_perm_indices sel2 (builder2, 2, nelts);
9855        tree op0 = NULL_TREE;
9856        /* If the new VEC_PERM_EXPR can't be handled but both
9857           original VEC_PERM_EXPRs can, punt.
9858           If one or both of the original VEC_PERM_EXPRs can't be
9859           handled and the new one can't be either, don't increase
9860           number of VEC_PERM_EXPRs that can't be handled.  */
9861        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9862            || (single_use (@0)
9863                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9864                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9865                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9866          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9867      }
9868      (if (op0)
9869       (vec_perm @1 @2 { op0; })))))))
9871 /* Merge
9872      c = VEC_PERM_EXPR <a, b, VCST0>;
9873      d = VEC_PERM_EXPR <x, c, VCST1>;
9874    to
9875      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9876    when all elements from a or b are replaced by the later
9877    permutation.  */
9879 (simplify
9880  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9881  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9882   (with
9883    {
9884      machine_mode result_mode = TYPE_MODE (type);
9885      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9886      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9887      vec_perm_builder builder0;
9888      vec_perm_builder builder1;
9889      vec_perm_builder builder2 (nelts, nelts, 2);
9890    }
9891    (if (tree_to_vec_perm_builder (&builder0, @3)
9892         && tree_to_vec_perm_builder (&builder1, @4))
9893     (with
9894      {
9895        vec_perm_indices sel0 (builder0, 2, nelts);
9896        vec_perm_indices sel1 (builder1, 2, nelts);
9897        bool use_1 = false, use_2 = false;
9899        for (int i = 0; i < nelts; i++)
9900          {
9901            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9902              builder2.quick_push (sel1[i]);
9903            else
9904              {
9905                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9906                                     .to_constant ()];
9907                if (known_lt (j, sel0.nelts_per_input ()))
9908                  use_1 = true;
9909                else
9910                  {
9911                    use_2 = true;
9912                    j -= sel0.nelts_per_input ();
9913                  }
9914                builder2.quick_push (j + sel1.nelts_per_input ());
9915              }
9916          }
9917      }
9918      (if (use_1 ^ use_2)
9919       (with
9920        {
9921          vec_perm_indices sel2 (builder2, 2, nelts);
9922          tree op0 = NULL_TREE;
9923          /* If the new VEC_PERM_EXPR can't be handled but both
9924             original VEC_PERM_EXPRs can, punt.
9925             If one or both of the original VEC_PERM_EXPRs can't be
9926             handled and the new one can't be either, don't increase
9927             number of VEC_PERM_EXPRs that can't be handled.  */
9928          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9929              || (single_use (@0)
9930                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9931                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9932                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9933            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9934        }
9935        (if (op0)
9936         (switch
9937          (if (use_1)
9938           (vec_perm @5 @1 { op0; }))
9939          (if (use_2)
9940           (vec_perm @5 @2 { op0; })))))))))))
9942 /* And the case with swapped outer permute sources.  */
9944 (simplify
9945  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9946  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9947   (with
9948    {
9949      machine_mode result_mode = TYPE_MODE (type);
9950      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9951      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9952      vec_perm_builder builder0;
9953      vec_perm_builder builder1;
9954      vec_perm_builder builder2 (nelts, nelts, 2);
9955    }
9956    (if (tree_to_vec_perm_builder (&builder0, @3)
9957         && tree_to_vec_perm_builder (&builder1, @4))
9958     (with
9959      {
9960        vec_perm_indices sel0 (builder0, 2, nelts);
9961        vec_perm_indices sel1 (builder1, 2, nelts);
9962        bool use_1 = false, use_2 = false;
9964        for (int i = 0; i < nelts; i++)
9965          {
9966            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9967              builder2.quick_push (sel1[i]);
9968            else
9969              {
9970                poly_uint64 j = sel0[sel1[i].to_constant ()];
9971                if (known_lt (j, sel0.nelts_per_input ()))
9972                  use_1 = true;
9973                else
9974                  {
9975                    use_2 = true;
9976                    j -= sel0.nelts_per_input ();
9977                  }
9978                builder2.quick_push (j);
9979              }
9980          }
9981      }
9982      (if (use_1 ^ use_2)
9983       (with
9984        {
9985          vec_perm_indices sel2 (builder2, 2, nelts);
9986          tree op0 = NULL_TREE;
9987          /* If the new VEC_PERM_EXPR can't be handled but both
9988             original VEC_PERM_EXPRs can, punt.
9989             If one or both of the original VEC_PERM_EXPRs can't be
9990             handled and the new one can't be either, don't increase
9991             number of VEC_PERM_EXPRs that can't be handled.  */
9992          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9993              || (single_use (@0)
9994                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9995                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9996                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9997            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9998        }
9999        (if (op0)
10000         (switch
10001          (if (use_1)
10002           (vec_perm @1 @5 { op0; }))
10003          (if (use_2)
10004           (vec_perm @2 @5 { op0; })))))))))))
10007 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10008    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10009    constant which when multiplied by a power of 2 contains a unique value
10010    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10011    to the number of trailing zeroes.  */
10012 (match (ctz_table_index @1 @2 @3)
10013   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10015 (match (cond_expr_convert_p @0 @2 @3 @6)
10016  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10017   (if (INTEGRAL_TYPE_P (type)
10018        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10019        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10020        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10021        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10022        && TYPE_PRECISION (TREE_TYPE (@0))
10023           == TYPE_PRECISION (TREE_TYPE (@2))
10024        && TYPE_PRECISION (TREE_TYPE (@0))
10025           == TYPE_PRECISION (TREE_TYPE (@3))
10026        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10027           signess when convert is truncation, but not ok for extension since
10028           it's sign_extend vs zero_extend.  */
10029        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10030            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10031                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10032        && single_use (@4)
10033        && single_use (@5))))
10035 (for bit_op (bit_and bit_ior bit_xor)
10036  (match (bitwise_induction_p @0 @2 @3)
10037   (bit_op:c
10038    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10039    @3)))
10041 (match (bitwise_induction_p @0 @2 @3)
10042  (bit_not
10043   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10045 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10046    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10047 (simplify
10048   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10049   (with { auto i = wi::neg (wi::to_wide (@2)); }
10050   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10051     (if (wi::popcount (i) == 1
10052          && (wi::to_wide (@1)) == (i - 1))
10053       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10054         (bit_and @0 @1)
10055       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10057 /* -x & 1 -> x & 1.  */
10058 (simplify 
10059  (bit_and (negate @0) integer_onep@1)
10060  (if (!TYPE_OVERFLOW_SANITIZED (type))
10061   (bit_and @0 @1)))
10063 /* `-a` is just `a` if the type is 1bit wide or when converting
10064    to a 1bit type; similar to the above transformation of `(-x)&1`.
10065    This is used mostly with the transformation of
10066    `a ? ~b : b` into `(-a)^b`.
10067    It also can show up with bitfields.  */
10068 (simplify
10069  (convert? (negate @0))
10070  (if (INTEGRAL_TYPE_P (type)
10071       && TYPE_PRECISION (type) == 1
10072       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10073   (convert @0)))
10075 /* Optimize
10076    c1 = VEC_PERM_EXPR (a, a, mask)
10077    c2 = VEC_PERM_EXPR (b, b, mask)
10078    c3 = c1 op c2
10079    -->
10080    c = a op b
10081    c3 = VEC_PERM_EXPR (c, c, mask)
10082    For all integer non-div operations.  */
10083 (for op (plus minus mult bit_and bit_ior bit_xor
10084          lshift rshift)
10085  (simplify
10086   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10087    (if (VECTOR_INTEGER_TYPE_P (type))
10088     (vec_perm (op@3 @0 @1) @3 @2))))
10090 /* Similar for float arithmetic when permutation constant covers
10091    all vector elements.  */
10092 (for op (plus minus mult)
10093  (simplify
10094   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10095    (if (VECTOR_FLOAT_TYPE_P (type)
10096         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10097     (with
10098      {
10099        tree perm_cst = @2;
10100        vec_perm_builder builder;
10101        bool full_perm_p = false;
10102        if (tree_to_vec_perm_builder (&builder, perm_cst))
10103          {
10104            unsigned HOST_WIDE_INT nelts;
10106            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10107            /* Create a vec_perm_indices for the VECTOR_CST.  */
10108            vec_perm_indices sel (builder, 1, nelts);
10110            /* Check if perm indices covers all vector elements.  */
10111            if (sel.encoding ().encoded_full_vector_p ())
10112              {
10113                auto_sbitmap seen (nelts);
10114                bitmap_clear (seen);
10116                unsigned HOST_WIDE_INT count = 0, i;
10118                for (i = 0; i < nelts; i++)
10119                  {
10120                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10121                     break;
10122                    count++;
10123                  }
10124                full_perm_p = count == nelts;
10125              }
10126          }
10127       }
10128       (if (full_perm_p)
10129         (vec_perm (op@3 @0 @1) @3 @2))))))