d: Merge upstream dmd, druntime 2bbf64907c, phobos b64bfbf91
[official-gcc.git] / gcc / match.pd
blob5deddd57d21ef94d45575f5e2c9d069a6467c03b
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    Note the outer cast cannot be a boolean type as the only valid values
1038    are 0,-1/1 (depending on the signedness of the boolean) and the negative
1039    is there to get the correct value.  */
1040 (simplify
1041  (convert (negate:s@1 (convert:s @0)))
1042  (if (INTEGRAL_TYPE_P (type)
1043       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1044       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1045       && TREE_CODE (type) != BOOLEAN_TYPE)
1046     (negate (convert @0))))
1048 (for op (negate abs)
1049  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1050  (for coss (COS COSH)
1051   (simplify
1052    (coss (op @0))
1053     (coss @0)))
1054  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1055  (for pows (POW)
1056   (simplify
1057    (pows (op @0) REAL_CST@1)
1058    (with { HOST_WIDE_INT n; }
1059     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1060      (pows @0 @1)))))
1061  /* Likewise for powi.  */
1062  (for pows (POWI)
1063   (simplify
1064    (pows (op @0) INTEGER_CST@1)
1065    (if ((wi::to_wide (@1) & 1) == 0)
1066     (pows @0 @1))))
1067  /* Strip negate and abs from both operands of hypot.  */
1068  (for hypots (HYPOT)
1069   (simplify
1070    (hypots (op @0) @1)
1071    (hypots @0 @1))
1072   (simplify
1073    (hypots @0 (op @1))
1074    (hypots @0 @1)))
1075  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1076  (for copysigns (COPYSIGN_ALL)
1077   (simplify
1078    (copysigns (op @0) @1)
1079    (copysigns @0 @1))))
1081 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1082 (simplify
1083  (mult (abs@1 @0) @1)
1084  (mult @0 @0))
1086 /* Convert absu(x)*absu(x) -> x*x.  */
1087 (simplify
1088  (mult (absu@1 @0) @1)
1089  (mult (convert@2 @0) @2))
1091 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1092 (for coss (COS COSH)
1093  (for copysigns (COPYSIGN)
1094   (simplify
1095    (coss (copysigns @0 @1))
1096     (coss @0))))
1098 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1099 (for pows (POW)
1100  (for copysigns (COPYSIGN)
1101   (simplify
1102    (pows (copysigns @0 @2) REAL_CST@1)
1103    (with { HOST_WIDE_INT n; }
1104     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1105      (pows @0 @1))))))
1106 /* Likewise for powi.  */
1107 (for pows (POWI)
1108  (for copysigns (COPYSIGN)
1109   (simplify
1110    (pows (copysigns @0 @2) INTEGER_CST@1)
1111    (if ((wi::to_wide (@1) & 1) == 0)
1112     (pows @0 @1)))))
1114 (for hypots (HYPOT)
1115  (for copysigns (COPYSIGN)
1116   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1117   (simplify
1118    (hypots (copysigns @0 @1) @2)
1119    (hypots @0 @2))
1120   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1121   (simplify
1122    (hypots @0 (copysigns @1 @2))
1123    (hypots @0 @1))))
1125 /* copysign(x, CST) -> abs (x).  */
1126 (for copysigns (COPYSIGN_ALL)
1127  (simplify
1128   (copysigns @0 REAL_CST@1)
1129   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1130    (abs @0))))
1132 /* Transform fneg (fabs (X)) -> copysign (X, -1).  */
1133 (simplify
1134  (negate (abs @0))
1135  (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1137 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1138 (for copysigns (COPYSIGN_ALL)
1139  (simplify
1140   (copysigns (copysigns @0 @1) @2)
1141   (copysigns @0 @2)))
1143 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1144 (for copysigns (COPYSIGN_ALL)
1145  (simplify
1146   (mult (copysigns@2 @0 @1) @2)
1147   (mult @0 @0)))
1149 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1150 (for ccoss (CCOS CCOSH)
1151  (simplify
1152   (ccoss (negate @0))
1153    (ccoss @0)))
1155 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1156 (for ops (conj negate)
1157  (for cabss (CABS)
1158   (simplify
1159    (cabss (ops @0))
1160    (cabss @0))))
1162 /* Fold (a * (1 << b)) into (a << b)  */
1163 (simplify
1164  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1165   (if (! FLOAT_TYPE_P (type)
1166        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1167    (lshift @0 @2)))
1169 /* Shifts by precision or greater result in zero.  */
1170 (for shift (lshift rshift)
1171  (simplify
1172   (shift @0 uniform_integer_cst_p@1)
1173   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1174        /* Leave arithmetic right shifts of possibly negative values alone.  */
1175        && (TYPE_UNSIGNED (type)
1176            || shift == LSHIFT_EXPR
1177            || tree_expr_nonnegative_p (@0))
1178        /* Use a signed compare to leave negative shift counts alone.  */
1179        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1180                      element_precision (type)))
1181    { build_zero_cst (type); })))
1183 /* Shifts by constants distribute over several binary operations,
1184    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1185 (for op (plus minus)
1186   (simplify
1187     (op (lshift:s @0 @1) (lshift:s @2 @1))
1188     (if (INTEGRAL_TYPE_P (type)
1189          && TYPE_OVERFLOW_WRAPS (type)
1190          && !TYPE_SATURATING (type))
1191       (lshift (op @0 @2) @1))))
1193 (for op (bit_and bit_ior bit_xor)
1194   (simplify
1195     (op (lshift:s @0 @1) (lshift:s @2 @1))
1196     (if (INTEGRAL_TYPE_P (type))
1197       (lshift (op @0 @2) @1)))
1198   (simplify
1199     (op (rshift:s @0 @1) (rshift:s @2 @1))
1200     (if (INTEGRAL_TYPE_P (type))
1201       (rshift (op @0 @2) @1))))
1203 /* Fold (1 << (C - x)) where C = precision(type) - 1
1204    into ((1 << C) >> x). */
1205 (simplify
1206  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1207   (if (INTEGRAL_TYPE_P (type)
1208        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1209        && single_use (@1))
1210    (if (TYPE_UNSIGNED (type))
1211      (rshift (lshift @0 @2) @3)
1212    (with
1213     { tree utype = unsigned_type_for (type); }
1214     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1216 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1217 (simplify
1218  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1219  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1220       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1221   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1222    (bit_and (convert @0)
1223             { wide_int_to_tree (type,
1224                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1226 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1227 (for cst (INTEGER_CST VECTOR_CST)
1228  (simplify
1229   (rshift (negate:s @0) cst@1)
1230    (if (!TYPE_UNSIGNED (type)
1231         && TYPE_OVERFLOW_UNDEFINED (type))
1232     (with { tree stype = TREE_TYPE (@1);
1233             tree bt = truth_type_for (type);
1234             tree zeros = build_zero_cst (type);
1235             tree cst = NULL_TREE; }
1236      (switch
1237       /* Handle scalar case.  */
1238       (if (INTEGRAL_TYPE_P (type)
1239            /* If we apply the rule to the scalar type before vectorization
1240               we will enforce the result of the comparison being a bool
1241               which will require an extra AND on the result that will be
1242               indistinguishable from when the user did actually want 0
1243               or 1 as the result so it can't be removed.  */
1244            && canonicalize_math_after_vectorization_p ()
1245            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1246        (negate (convert (gt @0 { zeros; }))))
1247       /* Handle vector case.  */
1248       (if (VECTOR_INTEGER_TYPE_P (type)
1249            /* First check whether the target has the same mode for vector
1250               comparison results as it's operands do.  */
1251            && TYPE_MODE (bt) == TYPE_MODE (type)
1252            /* Then check to see if the target is able to expand the comparison
1253               with the given type later on, otherwise we may ICE.  */
1254            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1255            && (cst = uniform_integer_cst_p (@1)) != NULL
1256            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1257        (view_convert (gt:bt @0 { zeros; }))))))))
1259 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1260 (simplify
1261  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1262   (if (flag_associative_math
1263        && single_use (@3))
1264    (with
1265     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1266     (if (tem)
1267      (rdiv { tem; } @1)))))
1269 /* Simplify ~X & X as zero.  */
1270 (simplify
1271  (bit_and (convert? @0) (convert? @1))
1272  (with { bool wascmp; }
1273   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1274        && bitwise_inverted_equal_p (@0, @1, wascmp))
1275    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1277 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1278 (simplify
1279   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1280   (if (TYPE_UNSIGNED (type))
1281     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1283 (for bitop (bit_and bit_ior)
1284      cmp (eq ne)
1285  /* PR35691: Transform
1286     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1287     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1288  (simplify
1289   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1290    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1291         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1292         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1293     (cmp (bit_ior @0 (convert @1)) @2)))
1294  /* Transform:
1295     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1296     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1297  (simplify
1298   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1299    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1300         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1301         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1302     (cmp (bit_and @0 (convert @1)) @2))))
1304 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1305 (simplify
1306  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1307   (minus (bit_xor @0 @1) @1))
1308 (simplify
1309  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1310  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1311   (minus (bit_xor @0 @1) @1)))
1313 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1314 (simplify
1315  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1316   (minus @1 (bit_xor @0 @1)))
1318 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1319 (for op (bit_ior bit_xor plus)
1320  (simplify
1321   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1322   (with { bool wascmp0, wascmp1; }
1323    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1324         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1325         && ((!wascmp0 && !wascmp1)
1326             || element_precision (type) == 1))
1327    (bit_xor @0 @1)))))
1329 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1330 (simplify
1331   (bit_ior:c (bit_xor:c @0 @1) @0)
1332   (bit_ior @0 @1))
1334 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1335 (simplify
1336  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1337  @2)
1339 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1340 (simplify
1341  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1342  (bit_not (bit_and @0 @1)))
1344 /* (~a & b) ^ a  -->   (a | b)   */
1345 (simplify
1346  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1347  (bit_ior @0 @1))
1349 /* (a | b) & ~(a ^ b)  -->  a & b  */
1350 (simplify
1351  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1352  (bit_and @0 @1))
1354 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1355 (simplify
1356  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1357  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1358       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1359   (bit_and @0 @1)))
1361 /* a | ~(a ^ b)  -->  a | ~b  */
1362 (simplify
1363  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1364  (bit_ior @0 (bit_not @1)))
1366 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1367 (simplify
1368  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1369  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1370       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1371   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1373 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1374 (simplify
1375  (bit_ior:c @0 (bit_xor:cs @1 @2))
1376  (with { bool wascmp; }
1377  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1378       && (!wascmp || element_precision (type) == 1))
1379   (bit_ior @0 (bit_not @2)))))
1381 /* a & ~(a ^ b)  -->  a & b  */
1382 (simplify
1383  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1384  (bit_and @0 @1))
1386 /* a & (a == b)  -->  a & b (boolean version of the above). */
1387 (simplify
1388  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1389  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1390       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1391   (bit_and @0 @1)))
1393 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1394 (simplify
1395  (bit_and:c @0 (bit_xor:c @1 @2))
1396  (with { bool wascmp; }
1397  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1398       && (!wascmp || element_precision (type) == 1))
1399   (bit_and @0 @2))))
1401 /* (a | b) | (a &^ b)  -->  a | b  */
1402 (for op (bit_and bit_xor)
1403  (simplify
1404   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1405   @2))
1407 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1408 (simplify
1409  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1410  @2)
1412 /* (a & b) | (a == b)  -->  a == b  */
1413 (simplify
1414  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1415  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1416       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1417   @2))
1419 /* ~(~a & b)  -->  a | ~b  */
1420 (simplify
1421  (bit_not (bit_and:cs (bit_not @0) @1))
1422  (bit_ior @0 (bit_not @1)))
1424 /* ~(~a | b) --> a & ~b */
1425 (simplify
1426  (bit_not (bit_ior:cs (bit_not @0) @1))
1427  (bit_and @0 (bit_not @1)))
1429 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1430 (simplify
1431  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1432  (bit_and @3 (bit_not @2)))
1434 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1435 (simplify
1436  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1437  (bit_ior @3 @2))
1439 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1440 (simplify
1441  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1442   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1444 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1445 (simplify
1446  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1447   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1449 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1450 (simplify
1451  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1452  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1453       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1454   (bit_xor @0 @1)))
1456 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1457    ((A & N) + B) & M -> (A + B) & M
1458    Similarly if (N & M) == 0,
1459    ((A | N) + B) & M -> (A + B) & M
1460    and for - instead of + (or unary - instead of +)
1461    and/or ^ instead of |.
1462    If B is constant and (B & M) == 0, fold into A & M.  */
1463 (for op (plus minus)
1464  (for bitop (bit_and bit_ior bit_xor)
1465   (simplify
1466    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1467     (with
1468      { tree pmop[2];
1469        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1470                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1471                                        NULL_TREE, pmop); }
1472      (if (utype)
1473       (convert (bit_and (op (convert:utype { pmop[0]; })
1474                             (convert:utype { pmop[1]; }))
1475                         (convert:utype @2))))))
1476   (simplify
1477    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1478     (with
1479      { tree pmop[2];
1480        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1481                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1482                                        @4, pmop); }
1483      (if (utype)
1484       (convert (bit_and (op (convert:utype { pmop[0]; })
1485                             (convert:utype { pmop[1]; }))
1486                         (convert:utype @2)))))))
1487  (simplify
1488   (bit_and (op:s @0 @1) INTEGER_CST@2)
1489    (with
1490     { tree pmop[2];
1491       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1492                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1493                                       NULL_TREE, NULL_TREE, pmop); }
1494     (if (utype)
1495      (convert (bit_and (op (convert:utype { pmop[0]; })
1496                            (convert:utype { pmop[1]; }))
1497                        (convert:utype @2)))))))
1498 (for bitop (bit_and bit_ior bit_xor)
1499  (simplify
1500   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1501    (with
1502     { tree pmop[2];
1503       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1504                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1505                                       NULL_TREE, NULL_TREE, pmop); }
1506     (if (utype)
1507      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1508                        (convert:utype @1)))))))
1510 /* X % Y is smaller than Y.  */
1511 (for cmp (lt ge)
1512  (simplify
1513   (cmp:c (trunc_mod @0 @1) @1)
1514   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1515    { constant_boolean_node (cmp == LT_EXPR, type); })))
1517 /* x | ~0 -> ~0  */
1518 (simplify
1519  (bit_ior @0 integer_all_onesp@1)
1520  @1)
1522 /* x | 0 -> x  */
1523 (simplify
1524  (bit_ior @0 integer_zerop)
1525  @0)
1527 /* x & 0 -> 0  */
1528 (simplify
1529  (bit_and @0 integer_zerop@1)
1530  @1)
1532 /* ~x | x -> -1 */
1533 /* ~x ^ x -> -1 */
1534 (for op (bit_ior bit_xor)
1535  (simplify
1536   (op (convert? @0) (convert? @1))
1537   (with { bool wascmp; }
1538    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1539         && bitwise_inverted_equal_p (@0, @1, wascmp))
1540     (convert
1541      { wascmp
1542         ? constant_boolean_node (true, type)
1543         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1545 /* x ^ x -> 0 */
1546 (simplify
1547   (bit_xor @0 @0)
1548   { build_zero_cst (type); })
1550 /* Canonicalize X ^ ~0 to ~X.  */
1551 (simplify
1552   (bit_xor @0 integer_all_onesp@1)
1553   (bit_not @0))
1555 /* x & ~0 -> x  */
1556 (simplify
1557  (bit_and @0 integer_all_onesp)
1558   (non_lvalue @0))
1560 /* x & x -> x,  x | x -> x  */
1561 (for bitop (bit_and bit_ior)
1562  (simplify
1563   (bitop @0 @0)
1564   (non_lvalue @0)))
1566 /* x & C -> x if we know that x & ~C == 0.  */
1567 #if GIMPLE
1568 (simplify
1569  (bit_and SSA_NAME@0 INTEGER_CST@1)
1570  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1571       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1572   @0))
1574 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1575 (simplify
1576  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1577  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1578       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1579   @0))
1581 /* x | C -> C if we know that x & ~C == 0.  */
1582 (simplify
1583  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1584  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1585       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1586   @1))
1587 #endif
1589 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1590 (simplify
1591  (bit_not (minus (bit_not @0) @1))
1592  (plus @0 @1))
1593 (simplify
1594  (bit_not (plus:c (bit_not @0) @1))
1595  (minus @0 @1))
1596 /* (~X - ~Y) -> Y - X.  */
1597 (simplify
1598  (minus (bit_not @0) (bit_not @1))
1599   (if (!TYPE_OVERFLOW_SANITIZED (type))
1600    (with { tree utype = unsigned_type_for (type); }
1601     (convert (minus (convert:utype @1) (convert:utype @0))))))
1603 /* ~(X - Y) -> ~X + Y.  */
1604 (simplify
1605  (bit_not (minus:s @0 @1))
1606  (plus (bit_not @0) @1))
1607 (simplify
1608  (bit_not (plus:s @0 INTEGER_CST@1))
1609  (if ((INTEGRAL_TYPE_P (type)
1610        && TYPE_UNSIGNED (type))
1611       || (!TYPE_OVERFLOW_SANITIZED (type)
1612           && may_negate_without_overflow_p (@1)))
1613   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1615 #if GIMPLE
1616 /* ~X + Y -> (Y - X) - 1.  */
1617 (simplify
1618  (plus:c (bit_not @0) @1)
1619   (if (ANY_INTEGRAL_TYPE_P (type)
1620        && TYPE_OVERFLOW_WRAPS (type)
1621        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1622        && !integer_all_onesp (@1))
1623    (plus (minus @1 @0) { build_minus_one_cst (type); })
1624    (if (INTEGRAL_TYPE_P (type)
1625         && TREE_CODE (@1) == INTEGER_CST
1626         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1627                                               SIGNED))
1628     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1629 #endif
1631 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1632 (simplify
1633  (bit_not (rshift:s @0 @1))
1634   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1635    (rshift (bit_not! @0) @1)
1636    /* For logical right shifts, this is possible only if @0 doesn't
1637       have MSB set and the logical right shift is changed into
1638       arithmetic shift.  */
1639    (if (INTEGRAL_TYPE_P (type)
1640         && !wi::neg_p (tree_nonzero_bits (@0)))
1641     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1642      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1644 /* x + (x & 1) -> (x + 1) & ~1 */
1645 (simplify
1646  (plus:c @0 (bit_and:s @0 integer_onep@1))
1647  (bit_and (plus @0 @1) (bit_not @1)))
1649 /* x & ~(x & y) -> x & ~y */
1650 /* x | ~(x | y) -> x | ~y  */
1651 (for bitop (bit_and bit_ior)
1652  (simplify
1653   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1654   (bitop @0 (bit_not @1))))
1656 /* (~x & y) | ~(x | y) -> ~x */
1657 (simplify
1658  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1659  @2)
1661 /* (x | y) ^ (x | ~y) -> ~x */
1662 (simplify
1663  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1664  (bit_not @0))
1666 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1667 (simplify
1668  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1669  (bit_not (bit_xor @0 @1)))
1671 /* (~x | y) ^ (x ^ y) -> x | ~y */
1672 (simplify
1673  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1674  (bit_ior @0 (bit_not @1)))
1676 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1677 (simplify
1678  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1679  (bit_not (bit_and @0 @1)))
1681 /* (x & y) ^ (x | y) -> x ^ y */
1682 (simplify
1683  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1684  (bit_xor @0 @1))
1686 /* (x ^ y) ^ (x | y) -> x & y */
1687 (simplify
1688  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1689  (bit_and @0 @1))
1691 /* (x & y) + (x ^ y) -> x | y */
1692 /* (x & y) | (x ^ y) -> x | y */
1693 /* (x & y) ^ (x ^ y) -> x | y */
1694 (for op (plus bit_ior bit_xor)
1695  (simplify
1696   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1697   (bit_ior @0 @1)))
1699 /* (x & y) + (x | y) -> x + y */
1700 (simplify
1701  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1702  (plus @0 @1))
1704 /* (x + y) - (x | y) -> x & y */
1705 (simplify
1706  (minus (plus @0 @1) (bit_ior @0 @1))
1707  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1708       && !TYPE_SATURATING (type))
1709   (bit_and @0 @1)))
1711 /* (x + y) - (x & y) -> x | y */
1712 (simplify
1713  (minus (plus @0 @1) (bit_and @0 @1))
1714  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1715       && !TYPE_SATURATING (type))
1716   (bit_ior @0 @1)))
1718 /* (x | y) - y -> (x & ~y) */
1719 (simplify
1720  (minus (bit_ior:cs @0 @1) @1)
1721  (bit_and @0 (bit_not @1)))
1723 /* (x | y) - (x ^ y) -> x & y */
1724 (simplify
1725  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1726  (bit_and @0 @1))
1728 /* (x | y) - (x & y) -> x ^ y */
1729 (simplify
1730  (minus (bit_ior @0 @1) (bit_and @0 @1))
1731  (bit_xor @0 @1))
1733 /* (x | y) & ~(x & y) -> x ^ y */
1734 (simplify
1735  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1736  (bit_xor @0 @1))
1738 /* (x | y) & (~x ^ y) -> x & y */
1739 (simplify
1740  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1741  (with { bool wascmp; }
1742   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1743        && (!wascmp || element_precision (type) == 1))
1744    (bit_and @0 @1))))
1746 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1747 (simplify
1748  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1749  (bit_not (bit_xor @0 @1)))
1751 /* (~x | y) ^ (x | ~y) -> x ^ y */
1752 (simplify
1753  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1754  (bit_xor @0 @1))
1756 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1757 (simplify
1758  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1759                               (nop_convert2? (bit_ior @0 @1))))
1760        integer_all_onesp)
1761  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1762       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1763       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1764       && !TYPE_SATURATING (TREE_TYPE (@2)))
1765  (bit_not (convert (bit_xor @0 @1)))))
1766 (simplify
1767  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1768                                integer_all_onesp))
1769        (nop_convert3? (bit_ior @0 @1)))
1770  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1771       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1772       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1773       && !TYPE_SATURATING (TREE_TYPE (@2)))
1774  (bit_not (convert (bit_xor @0 @1)))))
1775 (simplify
1776  (minus (nop_convert1? (bit_and @0 @1))
1777        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1778                                integer_onep)))
1779  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1780       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1781       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1782       && !TYPE_SATURATING (TREE_TYPE (@2)))
1783  (bit_not (convert (bit_xor @0 @1)))))
1785 /* ~x & ~y -> ~(x | y)
1786    ~x | ~y -> ~(x & y) */
1787 (for op (bit_and bit_ior)
1788      rop (bit_ior bit_and)
1789  (simplify
1790   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1791   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1792        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1793    (bit_not (rop (convert @0) (convert @1))))))
1795 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1796    with a constant, and the two constants have no bits in common,
1797    we should treat this as a BIT_IOR_EXPR since this may produce more
1798    simplifications.  */
1799 (for op (bit_xor plus)
1800  (simplify
1801   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1802       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1803   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1804        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1805        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1806    (bit_ior (convert @4) (convert @5)))))
1808 /* (X | Y) ^ X -> Y & ~ X*/
1809 (simplify
1810  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1811  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1812   (convert (bit_and @1 (bit_not @0)))))
1814 /* (~X | Y) ^ X -> ~(X & Y).  */
1815 (simplify
1816  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1817  (if (bitwise_equal_p (@0, @2))
1818   (convert (bit_not (bit_and @0 (convert @1))))))
1820 /* Convert ~X ^ ~Y to X ^ Y.  */
1821 (simplify
1822  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1823  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1824       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1825   (bit_xor (convert @0) (convert @1))))
1827 /* Convert ~X ^ C to X ^ ~C.  */
1828 (simplify
1829  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1830  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1831   (bit_xor (convert @0) (bit_not @1))))
1833 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1834 (for opo (bit_and bit_xor)
1835      opi (bit_xor bit_and)
1836  (simplify
1837   (opo:c (opi:cs @0 @1) @1)
1838   (bit_and (bit_not @0) @1)))
1840 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1841    operands are another bit-wise operation with a common input.  If so,
1842    distribute the bit operations to save an operation and possibly two if
1843    constants are involved.  For example, convert
1844      (A | B) & (A | C) into A | (B & C)
1845    Further simplification will occur if B and C are constants.  */
1846 (for op (bit_and bit_ior bit_xor)
1847      rop (bit_ior bit_and bit_and)
1848  (simplify
1849   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1850   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1851        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1852    (rop (convert @0) (op (convert @1) (convert @2))))))
1854 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1855 /* (X & Y) & Y -> X & Y
1856    (X | Y) | Y -> X | Y  */
1857 (for op (bit_and bit_ior)
1858  (simplify
1859   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1860   @2))
1861 /* (X ^ Y) ^ Y -> X  */
1862 (simplify
1863  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1864  (convert @0))
1866 /* (X & ~Y) & Y -> 0 */
1867 (simplify
1868  (bit_and:c (bit_and @0 @1) @2)
1869  (with { bool wascmp; }
1870   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1871        || bitwise_inverted_equal_p (@1, @2, wascmp))
1872    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1873 /* (X | ~Y) | Y -> -1 */
1874 (simplify
1875  (bit_ior:c (bit_ior @0 @1) @2)
1876  (with { bool wascmp; }
1877   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1878         || bitwise_inverted_equal_p (@1, @2, wascmp))
1879        && (!wascmp || element_precision (type) == 1))
1880    { build_all_ones_cst (TREE_TYPE (@0)); })))
1882 /* (X & Y) & (X & Z) -> (X & Y) & Z
1883    (X | Y) | (X | Z) -> (X | Y) | Z  */
1884 (for op (bit_and bit_ior)
1885  (simplify
1886   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1887   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1888        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1889    (if (single_use (@5) && single_use (@6))
1890     (op @3 (convert @2))
1891     (if (single_use (@3) && single_use (@4))
1892      (op (convert @1) @5))))))
1893 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1894 (simplify
1895  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1896  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1897       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1898   (bit_xor (convert @1) (convert @2))))
1900 /* Convert abs (abs (X)) into abs (X).
1901    also absu (absu (X)) into absu (X).  */
1902 (simplify
1903  (abs (abs@1 @0))
1904  @1)
1906 (simplify
1907  (absu (convert@2 (absu@1 @0)))
1908  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1909   @1))
1911 /* Convert abs[u] (-X) -> abs[u] (X).  */
1912 (simplify
1913  (abs (negate @0))
1914  (abs @0))
1916 (simplify
1917  (absu (negate @0))
1918  (absu @0))
1920 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1921 (simplify
1922  (abs tree_expr_nonnegative_p@0)
1923  @0)
1925 (simplify
1926  (absu tree_expr_nonnegative_p@0)
1927  (convert @0))
1929 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1930 (simplify
1931  (mult:c (nop_convert1?
1932           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1933                     integer_onep))
1934          (nop_convert3? @0))
1935  (if (INTEGRAL_TYPE_P (type)
1936       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1937       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1938   (if (TYPE_UNSIGNED (type))
1939    (absu @0)
1940    (abs @0)
1941   )
1945 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1946 (match negate_expr_p
1947  INTEGER_CST
1948  (if ((INTEGRAL_TYPE_P (type)
1949        && TYPE_UNSIGNED (type))
1950       || (!TYPE_OVERFLOW_SANITIZED (type)
1951           && may_negate_without_overflow_p (t)))))
1952 (match negate_expr_p
1953  FIXED_CST)
1954 (match negate_expr_p
1955  (negate @0)
1956  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1957 (match negate_expr_p
1958  REAL_CST
1959  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1960 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1961    ways.  */
1962 (match negate_expr_p
1963  VECTOR_CST
1964  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1965 (match negate_expr_p
1966  (minus @0 @1)
1967  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1968       || (FLOAT_TYPE_P (type)
1969           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1970           && !HONOR_SIGNED_ZEROS (type)))))
1972 /* (-A) * (-B) -> A * B  */
1973 (simplify
1974  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1975   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1976        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1977    (mult (convert @0) (convert (negate @1)))))
1979 /* -(A + B) -> (-B) - A.  */
1980 (simplify
1981  (negate (plus:c @0 negate_expr_p@1))
1982  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1983       && !HONOR_SIGNED_ZEROS (type))
1984   (minus (negate @1) @0)))
1986 /* -(A - B) -> B - A.  */
1987 (simplify
1988  (negate (minus @0 @1))
1989  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1990       || (FLOAT_TYPE_P (type)
1991           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1992           && !HONOR_SIGNED_ZEROS (type)))
1993   (minus @1 @0)))
1994 (simplify
1995  (negate (pointer_diff @0 @1))
1996  (if (TYPE_OVERFLOW_UNDEFINED (type))
1997   (pointer_diff @1 @0)))
1999 /* A - B -> A + (-B) if B is easily negatable.  */
2000 (simplify
2001  (minus @0 negate_expr_p@1)
2002  (if (!FIXED_POINT_TYPE_P (type))
2003  (plus @0 (negate @1))))
2005 /* 1 - a is a ^ 1 if a had a bool range. */
2006 /* This is only enabled for gimple as sometimes
2007    cfun is not set for the function which contains
2008    the SSA_NAME (e.g. while IPA passes are happening,
2009    fold might be called).  */
2010 (simplify
2011  (minus integer_onep@0 SSA_NAME@1)
2012   (if (INTEGRAL_TYPE_P (type)
2013        && ssa_name_has_boolean_range (@1))
2014    (bit_xor @1 @0)))
2016 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2017 (simplify
2018  (negate (mult:c@0 @1 negate_expr_p@2))
2019  (if (! TYPE_UNSIGNED (type)
2020       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2021       && single_use (@0))
2022   (mult @1 (negate @2))))
2024 (simplify
2025  (negate (rdiv@0 @1 negate_expr_p@2))
2026  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2027       && single_use (@0))
2028   (rdiv @1 (negate @2))))
2030 (simplify
2031  (negate (rdiv@0 negate_expr_p@1 @2))
2032  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2033       && single_use (@0))
2034   (rdiv (negate @1) @2)))
2036 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2037 (simplify
2038  (negate (convert? (rshift @0 INTEGER_CST@1)))
2039  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2040       && wi::to_wide (@1) == element_precision (type) - 1)
2041   (with { tree stype = TREE_TYPE (@0);
2042           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2043                                              : unsigned_type_for (stype); }
2044    (if (VECTOR_TYPE_P (type))
2045     (view_convert (rshift (view_convert:ntype @0) @1))
2046     (convert (rshift (convert:ntype @0) @1))))))
2048 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2049    when profitable.
2050    For bitwise binary operations apply operand conversions to the
2051    binary operation result instead of to the operands.  This allows
2052    to combine successive conversions and bitwise binary operations.
2053    We combine the above two cases by using a conditional convert.  */
2054 (for bitop (bit_and bit_ior bit_xor)
2055  (simplify
2056   (bitop (convert@2 @0) (convert?@3 @1))
2057   (if (((TREE_CODE (@1) == INTEGER_CST
2058          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2059          && (int_fits_type_p (@1, TREE_TYPE (@0))
2060              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2061         || types_match (@0, @1))
2062        && !POINTER_TYPE_P (TREE_TYPE (@0))
2063        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2064        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2065        /* ???  This transform conflicts with fold-const.cc doing
2066           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2067           constants (if x has signed type, the sign bit cannot be set
2068           in c).  This folds extension into the BIT_AND_EXPR.
2069           Restrict it to GIMPLE to avoid endless recursions.  */
2070        && (bitop != BIT_AND_EXPR || GIMPLE)
2071        && (/* That's a good idea if the conversion widens the operand, thus
2072               after hoisting the conversion the operation will be narrower.
2073               It is also a good if the conversion is a nop as moves the
2074               conversion to one side; allowing for combining of the conversions.  */
2075            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2076            /* The conversion check for being a nop can only be done at the gimple
2077               level as fold_binary has some re-association code which can conflict
2078               with this if there is a "constant" which is not a full INTEGER_CST.  */
2079            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2080            /* It's also a good idea if the conversion is to a non-integer
2081               mode.  */
2082            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2083            /* Or if the precision of TO is not the same as the precision
2084               of its mode.  */
2085            || !type_has_mode_precision_p (type)
2086            /* In GIMPLE, getting rid of 2 conversions for one new results
2087               in smaller IL.  */
2088            || (GIMPLE
2089                && TREE_CODE (@1) != INTEGER_CST
2090                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2091                && single_use (@2)
2092                && single_use (@3))))
2093    (convert (bitop @0 (convert @1)))))
2094  /* In GIMPLE, getting rid of 2 conversions for one new results
2095     in smaller IL.  */
2096  (simplify
2097   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2098   (if (GIMPLE
2099        && TREE_CODE (@1) != INTEGER_CST
2100        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2101        && types_match (type, @0)
2102        && !POINTER_TYPE_P (TREE_TYPE (@0))
2103        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2104    (bitop @0 (convert @1)))))
2106 (for bitop (bit_and bit_ior)
2107      rbitop (bit_ior bit_and)
2108   /* (x | y) & x -> x */
2109   /* (x & y) | x -> x */
2110  (simplify
2111   (bitop:c (rbitop:c @0 @1) @0)
2112   @0)
2113  /* (~x | y) & x -> x & y */
2114  /* (~x & y) | x -> x | y */
2115  (simplify
2116   (bitop:c (rbitop:c @2 @1) @0)
2117   (with { bool wascmp; }
2118    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2119         && (!wascmp || element_precision (type) == 1))
2120     (bitop @0 @1))))
2121   /* (x | y) & (x & z) -> (x & z) */
2122   /* (x & y) | (x | z) -> (x | z) */
2123  (simplify
2124   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2125   @3)
2126  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2127  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2128  (simplify
2129   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2130   (bitop @0 @3))
2131  /* x & ~(y | x) -> 0 */
2132  /* x | ~(y & x) -> -1 */
2133  (simplify
2134   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2135   (if (bitop == BIT_AND_EXPR)
2136    { build_zero_cst (type); }
2137    { build_minus_one_cst (type); })))
2139 /* ((x | y) & z) | x -> (z & y) | x
2140    ((x ^ y) & z) | x -> (z & y) | x  */
2141 (for op (bit_ior bit_xor)
2142  (simplify
2143   (bit_ior:c (nop_convert1?:s
2144                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2145   (if (bitwise_equal_p (@0, @3))
2146    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2148 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2149 (simplify
2150   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2151   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2153 /* Combine successive equal operations with constants.  */
2154 (for bitop (bit_and bit_ior bit_xor)
2155  (simplify
2156   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2157   (if (!CONSTANT_CLASS_P (@0))
2158    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2159       folded to a constant.  */
2160    (bitop @0 (bitop! @1 @2))
2161    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2162       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2163       the values involved are such that the operation can't be decided at
2164       compile time.  Try folding one of @0 or @1 with @2 to see whether
2165       that combination can be decided at compile time.
2167       Keep the existing form if both folds fail, to avoid endless
2168       oscillation.  */
2169    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2170     (if (cst1)
2171      (bitop @1 { cst1; })
2172      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2173       (if (cst2)
2174        (bitop @0 { cst2; }))))))))
2176 /* Try simple folding for X op !X, and X op X with the help
2177    of the truth_valued_p and logical_inverted_value predicates.  */
2178 (match truth_valued_p
2179  @0
2180  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2181 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2182  (match truth_valued_p
2183   (op @0 @1)))
2184 (match truth_valued_p
2185   (truth_not @0))
2187 (match (logical_inverted_value @0)
2188  (truth_not @0))
2189 (match (logical_inverted_value @0)
2190  (bit_not truth_valued_p@0))
2191 (match (logical_inverted_value @0)
2192  (eq @0 integer_zerop))
2193 (match (logical_inverted_value @0)
2194  (ne truth_valued_p@0 integer_truep))
2195 (match (logical_inverted_value @0)
2196  (bit_xor truth_valued_p@0 integer_truep))
2198 /* X & !X -> 0.  */
2199 (simplify
2200  (bit_and:c @0 (logical_inverted_value @0))
2201  { build_zero_cst (type); })
2202 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2203 (for op (bit_ior bit_xor)
2204  (simplify
2205   (op:c truth_valued_p@0 (logical_inverted_value @0))
2206   { constant_boolean_node (true, type); }))
2207 /* X ==/!= !X is false/true.  */
2208 (for op (eq ne)
2209  (simplify
2210   (op:c truth_valued_p@0 (logical_inverted_value @0))
2211   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2213 /* ~~x -> x */
2214 (simplify
2215   (bit_not (bit_not @0))
2216   @0)
2218 /* zero_one_valued_p will match when a value is known to be either
2219    0 or 1 including constants 0 or 1.
2220    Signed 1-bits includes -1 so they cannot match here. */
2221 (match zero_one_valued_p
2222  @0
2223  (if (INTEGRAL_TYPE_P (type)
2224       && (TYPE_UNSIGNED (type)
2225           || TYPE_PRECISION (type) > 1)
2226       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2227 (match zero_one_valued_p
2228  truth_valued_p@0
2229  (if (INTEGRAL_TYPE_P (type)
2230       && (TYPE_UNSIGNED (type)
2231           || TYPE_PRECISION (type) > 1))))
2233 /* (a&1) is always [0,1] too. This is useful again when
2234    the range is not known. */
2235 /* Note this can't be recursive due to VN handling of equivalents,
2236    VN and would cause an infinite recursion. */
2237 (match zero_one_valued_p
2238  (bit_and:c@0 @1 integer_onep)
2239  (if (INTEGRAL_TYPE_P (type))))
2241 /* A conversion from an zero_one_valued_p is still a [0,1].
2242    This is useful when the range of a variable is not known */
2243 /* Note this matches can't be recursive because of the way VN handles
2244    nop conversions being equivalent and then recursive between them. */
2245 (match zero_one_valued_p
2246  (convert@0 @1)
2247  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2248       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2249           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2250       && INTEGRAL_TYPE_P (type)
2251       && (TYPE_UNSIGNED (type)
2252           || TYPE_PRECISION (type) > 1)
2253       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2255 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2256 (simplify
2257  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2258  (if (INTEGRAL_TYPE_P (type))
2259   (bit_and @0 @1)))
2261 (for cmp (tcc_comparison)
2262      icmp (inverted_tcc_comparison)
2263  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2264  (simplify
2265   (bit_ior
2266    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2267    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2268     (if (INTEGRAL_TYPE_P (type)
2269          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2270          /* The scalar version has to be canonicalized after vectorization
2271             because it makes unconditional loads conditional ones, which
2272             means we lose vectorization because the loads may trap.  */
2273          && canonicalize_math_after_vectorization_p ())
2274      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2276  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2277     canonicalized further and we recognize the conditional form:
2278     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2279  (simplify
2280   (bit_ior
2281    (cond (cmp@0  @01 @02) @3 zerop)
2282    (cond (icmp@4 @01 @02) @5 zerop))
2283     (if (INTEGRAL_TYPE_P (type)
2284          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2285          /* The scalar version has to be canonicalized after vectorization
2286             because it makes unconditional loads conditional ones, which
2287             means we lose vectorization because the loads may trap.  */
2288          && canonicalize_math_after_vectorization_p ())
2289     (cond @0 @3 @5)))
2291  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2292     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2293  (simplify
2294   (bit_ior
2295    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2296    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2297     (if (integer_zerop (@5)
2298          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2299      (switch
2300       (if (integer_onep (@4))
2301        (bit_and (vec_cond @0 @2 @3) @4))
2302         (if (integer_minus_onep (@4))
2303          (vec_cond @0 @2 @3)))
2304     (if (integer_zerop (@4)
2305          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2306      (switch
2307       (if (integer_onep (@5))
2308        (bit_and (vec_cond @0 @3 @2) @5))
2309       (if (integer_minus_onep (@5))
2310        (vec_cond @0 @3 @2))))))
2312  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2313     into a < b ? d : c.  */
2314  (simplify
2315   (bit_ior
2316    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2317    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2318   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2319    (vec_cond @0 @2 @3))))
2321 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2322 (simplify
2323  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2324  (if (INTEGRAL_TYPE_P (type)
2325       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2326       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2327       /* Sign extending of the neg or a truncation of the neg
2328          is needed. */
2329       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2330           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2331   (mult (convert @0) @1)))
2333 /* Narrow integer multiplication by a zero_one_valued_p operand.
2334    Multiplication by [0,1] is guaranteed not to overflow.  */
2335 (simplify
2336  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2337  (if (INTEGRAL_TYPE_P (type)
2338       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2339       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2340   (mult (convert @1) (convert @2))))
2342 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2343    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2344    as some targets (such as x86's SSE) may return zero for larger C.  */
2345 (simplify
2346   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2347   (if (tree_fits_shwi_p (@1)
2348        && tree_to_shwi (@1) > 0
2349        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2350     (convert @0)))
2352 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2353    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2354    as some targets (such as x86's SSE) may return zero for larger C.  */
2355 (simplify
2356   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2357   (if (tree_fits_shwi_p (@1)
2358        && tree_to_shwi (@1) > 0
2359        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2360     (eq @0 @2)))
2362 /* Convert ~ (-A) to A - 1.  */
2363 (simplify
2364  (bit_not (convert? (negate @0)))
2365  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2366       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2367   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2369 /* Convert - (~A) to A + 1.  */
2370 (simplify
2371  (negate (nop_convert? (bit_not @0)))
2372  (plus (view_convert @0) { build_each_one_cst (type); }))
2374 /* (a & b) ^ (a == b) -> !(a | b) */
2375 /* (a & b) == (a ^ b) -> !(a | b) */
2376 (for first_op (bit_xor eq)
2377      second_op (eq bit_xor)
2378  (simplify
2379   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2380     (bit_not (bit_ior @0 @1))))
2382 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2383 (simplify
2384  (bit_not (convert? (minus @0 integer_each_onep)))
2385  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2386       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2387   (convert (negate @0))))
2388 (simplify
2389  (bit_not (convert? (plus @0 integer_all_onesp)))
2390  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2391       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2392   (convert (negate @0))))
2394 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2395 (simplify
2396  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2397  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2398   (convert (bit_xor @0 (bit_not @1)))))
2399 (simplify
2400  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2401  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2402   (convert (bit_xor @0 @1))))
2404 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2405 (simplify
2406  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2407  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2408   (bit_not (bit_xor (view_convert @0) @1))))
2410 /* ~(a ^ b) is a == b for truth valued a and b.  */
2411 (simplify
2412  (bit_not (bit_xor: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 (eq @0 @1))))
2417 /* (~a) == b is a ^ b for truth valued a and b.  */
2418 (simplify
2419  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2420  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2421       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2422   (convert (bit_xor @0 @1))))
2424 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2425 (simplify
2426  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2427  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2429 /* Fold A - (A & B) into ~B & A.  */
2430 (simplify
2431  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2432  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2433       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2434   (convert (bit_and (bit_not @1) @0))))
2436 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2437 (if (!canonicalize_math_p ())
2438  (for cmp (tcc_comparison)
2439   (simplify
2440    (mult:c (convert (cmp@0 @1 @2)) @3)
2441    (if (INTEGRAL_TYPE_P (type)
2442         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2443      (cond @0 @3 { build_zero_cst (type); })))
2444 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2445   (simplify
2446    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2447    (if (INTEGRAL_TYPE_P (type)
2448         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2449      (cond @0 @3 { build_zero_cst (type); })))
2453 /* For integral types with undefined overflow and C != 0 fold
2454    x * C EQ/NE y * C into x EQ/NE y.  */
2455 (for cmp (eq ne)
2456  (simplify
2457   (cmp (mult:c @0 @1) (mult:c @2 @1))
2458   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2459        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2460        && tree_expr_nonzero_p (@1))
2461    (cmp @0 @2))))
2463 /* For integral types with wrapping overflow and C odd fold
2464    x * C EQ/NE y * C into x EQ/NE y.  */
2465 (for cmp (eq ne)
2466  (simplify
2467   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2468   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2469        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2470        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2471    (cmp @0 @2))))
2473 /* For integral types with undefined overflow and C != 0 fold
2474    x * C RELOP y * C into:
2476    x RELOP y for nonnegative C
2477    y RELOP x for negative C  */
2478 (for cmp (lt gt le ge)
2479  (simplify
2480   (cmp (mult:c @0 @1) (mult:c @2 @1))
2481   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2482        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2483    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2484     (cmp @0 @2)
2485    (if (TREE_CODE (@1) == INTEGER_CST
2486         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2487     (cmp @2 @0))))))
2489 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2490 (for cmp (le gt)
2491      icmp (gt le)
2492  (simplify
2493   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2494    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2495         && TYPE_UNSIGNED (TREE_TYPE (@0))
2496         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2497         && (wi::to_wide (@2)
2498             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2499     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2500      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2502 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2503 (for cmp (simple_comparison)
2504  (simplify
2505   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2506   (if (element_precision (@3) >= element_precision (@0)
2507        && types_match (@0, @1))
2508    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2509     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2510      (cmp @1 @0)
2511      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2512       (with
2513        {
2514         tree utype = unsigned_type_for (TREE_TYPE (@0));
2515        }
2516        (cmp (convert:utype @1) (convert:utype @0)))))
2517     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2518      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2519       (cmp @0 @1)
2520       (with
2521        {
2522         tree utype = unsigned_type_for (TREE_TYPE (@0));
2523        }
2524        (cmp (convert:utype @0) (convert:utype @1)))))))))
2526 /* X / C1 op C2 into a simple range test.  */
2527 (for cmp (simple_comparison)
2528  (simplify
2529   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2530   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2531        && integer_nonzerop (@1)
2532        && !TREE_OVERFLOW (@1)
2533        && !TREE_OVERFLOW (@2))
2534    (with { tree lo, hi; bool neg_overflow;
2535            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2536                                                    &neg_overflow); }
2537     (switch
2538      (if (code == LT_EXPR || code == GE_EXPR)
2539        (if (TREE_OVERFLOW (lo))
2540         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2541         (if (code == LT_EXPR)
2542          (lt @0 { lo; })
2543          (ge @0 { lo; }))))
2544      (if (code == LE_EXPR || code == GT_EXPR)
2545        (if (TREE_OVERFLOW (hi))
2546         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2547         (if (code == LE_EXPR)
2548          (le @0 { hi; })
2549          (gt @0 { hi; }))))
2550      (if (!lo && !hi)
2551       { build_int_cst (type, code == NE_EXPR); })
2552      (if (code == EQ_EXPR && !hi)
2553       (ge @0 { lo; }))
2554      (if (code == EQ_EXPR && !lo)
2555       (le @0 { hi; }))
2556      (if (code == NE_EXPR && !hi)
2557       (lt @0 { lo; }))
2558      (if (code == NE_EXPR && !lo)
2559       (gt @0 { hi; }))
2560      (if (GENERIC)
2561       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2562                            lo, hi); })
2563      (with
2564       {
2565         tree etype = range_check_type (TREE_TYPE (@0));
2566         if (etype)
2567           {
2568             hi = fold_convert (etype, hi);
2569             lo = fold_convert (etype, lo);
2570             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2571           }
2572       }
2573       (if (etype && hi && !TREE_OVERFLOW (hi))
2574        (if (code == EQ_EXPR)
2575         (le (minus (convert:etype @0) { lo; }) { hi; })
2576         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2578 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2579 (for op (lt le ge gt)
2580  (simplify
2581   (op (plus:c @0 @2) (plus:c @1 @2))
2582   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2583        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2584    (op @0 @1))))
2586 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2587    when C is an unsigned integer constant with only the MSB set, and X and
2588    Y have types of equal or lower integer conversion rank than C's.  */
2589 (for op (lt le ge gt)
2590  (simplify
2591   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2592   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2593        && TYPE_UNSIGNED (TREE_TYPE (@0))
2594        && wi::only_sign_bit_p (wi::to_wide (@0)))
2595    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2596     (op (convert:stype @1) (convert:stype @2))))))
2598 /* For equality and subtraction, this is also true with wrapping overflow.  */
2599 (for op (eq ne minus)
2600  (simplify
2601   (op (plus:c @0 @2) (plus:c @1 @2))
2602   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2603        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2604            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2605    (op @0 @1))))
2606 /* And similar for pointers.  */
2607 (for op (eq ne)
2608  (simplify
2609   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2610   (op @1 @2)))
2611 (simplify
2612  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2613  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2614   (convert (minus @1 @2))))
2616 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2617 (for op (lt le ge gt)
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    (op @0 @1))))
2623 /* For equality and subtraction, this is also true with wrapping overflow.  */
2624 (for op (eq ne minus)
2625  (simplify
2626   (op (minus @0 @2) (minus @1 @2))
2627   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2628        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2629            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2630    (op @0 @1))))
2631 /* And for pointers...  */
2632 (for op (simple_comparison)
2633  (simplify
2634   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2635   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2636    (op @0 @1))))
2637 (simplify
2638  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2639  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2640       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2641   (pointer_diff @0 @1)))
2643 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2644 (for op (lt le ge gt)
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    (op @1 @0))))
2650 /* For equality and subtraction, this is also true with wrapping overflow.  */
2651 (for op (eq ne minus)
2652  (simplify
2653   (op (minus @2 @0) (minus @2 @1))
2654   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2655        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2656            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2657    (op @1 @0))))
2658 /* And for pointers...  */
2659 (for op (simple_comparison)
2660  (simplify
2661   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2662   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2663    (op @1 @0))))
2664 (simplify
2665  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2666  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2667       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2668   (pointer_diff @1 @0)))
2670 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2671 (for op (lt le gt ge)
2672  (simplify
2673   (op:c (plus:c@2 @0 @1) @1)
2674   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2675        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2676        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2677        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2678    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2679 /* For equality, this is also true with wrapping overflow.  */
2680 (for op (eq ne)
2681  (simplify
2682   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2683   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2684        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2685            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2686        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2687        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2688        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2689    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2690  (simplify
2691   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2692   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2693        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2694        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2695    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2697 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2698 (for neeq (ne eq)
2699  (simplify
2700   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2701    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2702     (if (ptr_difference_const (@0, @2, &diff))
2703      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2704  (simplify
2705   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2706    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2707     (if (ptr_difference_const (@0, @2, &diff))
2708      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2710 /* X - Y < X is the same as Y > 0 when there is no overflow.
2711    For equality, this is also true with wrapping overflow.  */
2712 (for op (simple_comparison)
2713  (simplify
2714   (op:c @0 (minus@2 @0 @1))
2715   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2716        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2717            || ((op == EQ_EXPR || op == NE_EXPR)
2718                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2719        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2720    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2722 /* Transform:
2723    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2724    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2725 (for cmp (eq ne)
2726      ocmp (lt ge)
2727  (simplify
2728   (cmp (trunc_div @0 @1) integer_zerop)
2729   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2730        /* Complex ==/!= is allowed, but not </>=.  */
2731        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2732        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2733    (ocmp @0 @1))))
2735 /* X == C - X can never be true if C is odd.  */
2736 (for cmp (eq ne)
2737  (simplify
2738   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2739   (if (TREE_INT_CST_LOW (@1) & 1)
2740    { constant_boolean_node (cmp == NE_EXPR, type); })))
2743    U & N <= U  -> true
2744    U & N >  U  -> false
2745    U needs to be non-negative.
2747    U | N <  U  -> false
2748    U | N >= U  -> true
2749    U and N needs to be non-negative
2751    U | N <  U  -> true
2752    U | N >= U  -> false
2753    U needs to be non-negative and N needs to be a negative constant.
2754    */
2755 (for cmp   (lt      ge      le      gt     )
2756      bitop (bit_ior bit_ior bit_and bit_and)
2757  (simplify
2758   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2759   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2760    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2761     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2762     /* The sign is opposite now so the comparison is swapped around. */
2763     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2764      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2766 /* Arguments on which one can call get_nonzero_bits to get the bits
2767    possibly set.  */
2768 (match with_possible_nonzero_bits
2769  INTEGER_CST@0)
2770 (match with_possible_nonzero_bits
2771  SSA_NAME@0
2772  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2773 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2774 (match (with_possible_nonzero_bits2 @0)
2775  with_possible_nonzero_bits@0)
2776 (match (with_possible_nonzero_bits2 @0)
2777  (bit_and:c with_possible_nonzero_bits@0 @2))
2779 /* Same for bits that are known to be set, but we do not have
2780    an equivalent to get_nonzero_bits yet.  */
2781 (match (with_certain_nonzero_bits2 @0)
2782  INTEGER_CST@0)
2783 (match (with_certain_nonzero_bits2 @0)
2784  (bit_ior @1 INTEGER_CST@0))
2786 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2787 (for cmp (eq ne)
2788  (simplify
2789   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2790   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2791    { constant_boolean_node (cmp == NE_EXPR, type); })))
2793 /* ((X inner_op C0) outer_op C1)
2794    With X being a tree where value_range has reasoned certain bits to always be
2795    zero throughout its computed value range,
2796    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2797    where zero_mask has 1's for all bits that are sure to be 0 in
2798    and 0's otherwise.
2799    if (inner_op == '^') C0 &= ~C1;
2800    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2801    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2803 (for inner_op (bit_ior bit_xor)
2804      outer_op (bit_xor bit_ior)
2805 (simplify
2806  (outer_op
2807   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2808  (with
2809   {
2810     bool fail = false;
2811     wide_int zero_mask_not;
2812     wide_int C0;
2813     wide_int cst_emit;
2815     if (TREE_CODE (@2) == SSA_NAME)
2816       zero_mask_not = get_nonzero_bits (@2);
2817     else
2818       fail = true;
2820     if (inner_op == BIT_XOR_EXPR)
2821       {
2822         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2823         cst_emit = C0 | wi::to_wide (@1);
2824       }
2825     else
2826       {
2827         C0 = wi::to_wide (@0);
2828         cst_emit = C0 ^ wi::to_wide (@1);
2829       }
2830   }
2831   (if (!fail && (C0 & zero_mask_not) == 0)
2832    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2833    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2834     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2836 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2837 (simplify
2838   (pointer_plus (pointer_plus:s @0 @1) @3)
2839   (pointer_plus @0 (plus @1 @3)))
2840 #if GENERIC
2841 (simplify
2842   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2843   (convert:type (pointer_plus @0 (plus @1 @3))))
2844 #endif
2846 /* Pattern match
2847      tem1 = (long) ptr1;
2848      tem2 = (long) ptr2;
2849      tem3 = tem2 - tem1;
2850      tem4 = (unsigned long) tem3;
2851      tem5 = ptr1 + tem4;
2852    and produce
2853      tem5 = ptr2;  */
2854 (simplify
2855   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2856   /* Conditionally look through a sign-changing conversion.  */
2857   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2858        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2859             || (GENERIC && type == TREE_TYPE (@1))))
2860    @1))
2861 (simplify
2862   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2863   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2864    (convert @1)))
2866 /* Pattern match
2867      tem = (sizetype) ptr;
2868      tem = tem & algn;
2869      tem = -tem;
2870      ... = ptr p+ tem;
2871    and produce the simpler and easier to analyze with respect to alignment
2872      ... = ptr & ~algn;  */
2873 (simplify
2874   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2875   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2876    (bit_and @0 { algn; })))
2878 /* Try folding difference of addresses.  */
2879 (simplify
2880  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2881  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2882   (with { poly_int64 diff; }
2883    (if (ptr_difference_const (@0, @1, &diff))
2884     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2885 (simplify
2886  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2887  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2888   (with { poly_int64 diff; }
2889    (if (ptr_difference_const (@0, @1, &diff))
2890     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2891 (simplify
2892  (minus (convert ADDR_EXPR@0) (convert @1))
2893  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2894   (with { poly_int64 diff; }
2895    (if (ptr_difference_const (@0, @1, &diff))
2896     { build_int_cst_type (type, diff); }))))
2897 (simplify
2898  (minus (convert @0) (convert ADDR_EXPR@1))
2899  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
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 ADDR_EXPR@0) (convert1?@3 @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); }))))
2910 (simplify
2911  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2912  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2913       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2914   (with { poly_int64 diff; }
2915    (if (ptr_difference_const (@0, @1, &diff))
2916     { build_int_cst_type (type, diff); }))))
2918 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2919 (simplify
2920  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2921  (with { poly_int64 diff; }
2922    (if (ptr_difference_const (@0, @2, &diff))
2923     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2924 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2925 (simplify
2926  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2927  (with { poly_int64 diff; }
2928    (if (ptr_difference_const (@0, @2, &diff))
2929     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2930 (simplify
2931  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2932  (with { poly_int64 diff; }
2933    (if (ptr_difference_const (@0, @1, &diff))
2934     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2936 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2937 (simplify
2938  (convert (pointer_diff @0 INTEGER_CST@1))
2939  (if (POINTER_TYPE_P (type))
2940   { build_fold_addr_expr_with_type
2941       (build2 (MEM_REF, char_type_node, @0,
2942                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2943                type); }))
2945 /* If arg0 is derived from the address of an object or function, we may
2946    be able to fold this expression using the object or function's
2947    alignment.  */
2948 (simplify
2949  (bit_and (convert? @0) INTEGER_CST@1)
2950  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2951       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2952   (with
2953    {
2954      unsigned int align;
2955      unsigned HOST_WIDE_INT bitpos;
2956      get_pointer_alignment_1 (@0, &align, &bitpos);
2957    }
2958    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2959     { wide_int_to_tree (type, (wi::to_wide (@1)
2960                                & (bitpos / BITS_PER_UNIT))); }))))
2962 (match min_value
2963  uniform_integer_cst_p
2964  (with {
2965    tree int_cst = uniform_integer_cst_p (t);
2966    tree inner_type = TREE_TYPE (int_cst);
2967   }
2968   (if ((INTEGRAL_TYPE_P (inner_type)
2969         || POINTER_TYPE_P (inner_type))
2970        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2972 (match max_value
2973  uniform_integer_cst_p
2974  (with {
2975    tree int_cst = uniform_integer_cst_p (t);
2976    tree itype = TREE_TYPE (int_cst);
2977   }
2978  (if ((INTEGRAL_TYPE_P (itype)
2979        || POINTER_TYPE_P (itype))
2980       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2982 /* x >  y  &&  x != XXX_MIN  -->  x > y
2983    x >  y  &&  x == XXX_MIN  -->  false . */
2984 (for eqne (eq ne)
2985  (simplify
2986   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2987    (switch
2988     (if (eqne == EQ_EXPR)
2989      { constant_boolean_node (false, type); })
2990     (if (eqne == NE_EXPR)
2991      @2)
2992     )))
2994 /* x <  y  &&  x != XXX_MAX  -->  x < y
2995    x <  y  &&  x == XXX_MAX  -->  false.  */
2996 (for eqne (eq ne)
2997  (simplify
2998   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2999    (switch
3000     (if (eqne == EQ_EXPR)
3001      { constant_boolean_node (false, type); })
3002     (if (eqne == NE_EXPR)
3003      @2)
3004     )))
3006 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3007 (simplify
3008  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3009   @2)
3011 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3012 (simplify
3013  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3014   @2)
3016 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3017 (simplify
3018  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3019   @2)
3021 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3022 (simplify
3023  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3024   { constant_boolean_node (true, type); })
3026 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3027 (simplify
3028  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3029   @2)
3031 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3032 (simplify
3033  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3034   @2)
3036 /* x >=  y  ||  x != XXX_MAX   -->  true
3037    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3038 (for eqne (eq ne)
3039  (simplify
3040   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3041    (switch
3042     (if (eqne == EQ_EXPR)
3043      @2)
3044     (if (eqne == NE_EXPR)
3045      { constant_boolean_node (true, type); }))))
3047 /* y == XXX_MIN || x < y --> x <= y - 1 */
3048 (simplify
3049  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3050   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3051        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3052   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3054 /* y != XXX_MIN && x >= y --> x > y - 1 */
3055 (simplify
3056  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3057   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3058        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3059   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3061 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3062    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3063 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3064    Similarly for (X != Y).  */
3066 (for code1 (eq ne)
3067  (for code2 (eq ne lt gt le ge)
3068   (simplify
3069    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3070    (if ((TREE_CODE (@1) == INTEGER_CST
3071          && TREE_CODE (@2) == INTEGER_CST)
3072         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3073              || POINTER_TYPE_P (TREE_TYPE (@1)))
3074             && bitwise_equal_p (@1, @2)))
3075     (with
3076      {
3077       bool one_before = false;
3078       bool one_after = false;
3079       int cmp = 0;
3080       bool allbits = true;
3081       if (TREE_CODE (@1) == INTEGER_CST
3082           && TREE_CODE (@2) == INTEGER_CST)
3083         {
3084           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3085           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3086           auto t2 = wi::to_wide (@2);
3087           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3088           if (cmp < 0
3089               && t1 == t2 - 1)
3090             one_before = true;
3091           if (cmp > 0
3092               && t1 == t2 + 1)
3093             one_after = true;
3094         }
3095       bool val;
3096       switch (code2)
3097          {
3098         case EQ_EXPR: val = (cmp == 0); break;
3099         case NE_EXPR: val = (cmp != 0); break;
3100         case LT_EXPR: val = (cmp < 0); break;
3101         case GT_EXPR: val = (cmp > 0); break;
3102         case LE_EXPR: val = (cmp <= 0); break;
3103         case GE_EXPR: val = (cmp >= 0); break;
3104         default: gcc_unreachable ();
3105         }
3106      }
3107      (switch
3108       (if (code1 == EQ_EXPR && val) @3)
3109       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3110       (if (code1 == NE_EXPR && !val && allbits) @4)
3111       (if (code1 == NE_EXPR
3112            && code2 == GE_EXPR
3113            && cmp == 0
3114            && allbits)
3115        (gt @c0 (convert @1)))
3116       (if (code1 == NE_EXPR
3117            && code2 == LE_EXPR
3118            && cmp == 0
3119            && allbits)
3120        (lt @c0 (convert @1)))
3121       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3122       (if (code1 == NE_EXPR
3123            && code2 == GT_EXPR
3124            && one_after
3125            && allbits)
3126        (gt @c0 (convert @1)))
3127       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3128       (if (code1 == NE_EXPR
3129            && code2 == LT_EXPR
3130            && one_before
3131            && allbits)
3132        (lt @c0 (convert @1)))
3133      )
3134     )
3135    )
3136   )
3140 /* Convert (X OP1 CST1) && (X OP2 CST2).
3141    Convert (X OP1 Y) && (X OP2 Y).  */
3143 (for code1 (lt le gt ge)
3144  (for code2 (lt le gt ge)
3145   (simplify
3146   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3147   (if ((TREE_CODE (@1) == INTEGER_CST
3148         && TREE_CODE (@2) == INTEGER_CST)
3149        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3150             || POINTER_TYPE_P (TREE_TYPE (@1)))
3151            && operand_equal_p (@1, @2)))
3152    (with
3153     {
3154      int cmp = 0;
3155      if (TREE_CODE (@1) == INTEGER_CST
3156          && TREE_CODE (@2) == INTEGER_CST)
3157        cmp = tree_int_cst_compare (@1, @2);
3158     }
3159     (switch
3160      /* Choose the more restrictive of two < or <= comparisons.  */
3161      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3162           && (code2 == LT_EXPR || code2 == LE_EXPR))
3163       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3164        @3
3165        @4))
3166      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3167      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3168           && (code2 == GT_EXPR || code2 == GE_EXPR))
3169       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3170        @3
3171        @4))
3172      /* Check for singleton ranges.  */
3173      (if (cmp == 0
3174           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3175             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3176       (eq @0 @1))
3177      /* Check for disjoint ranges.  */
3178      (if (cmp <= 0
3179           && (code1 == LT_EXPR || code1 == LE_EXPR)
3180           && (code2 == GT_EXPR || code2 == GE_EXPR))
3181       { constant_boolean_node (false, type); })
3182      (if (cmp >= 0
3183           && (code1 == GT_EXPR || code1 == GE_EXPR)
3184           && (code2 == LT_EXPR || code2 == LE_EXPR))
3185       { constant_boolean_node (false, type); })
3186      ))))))
3188 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3189    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3190 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3191    Similarly for (X != Y).  */
3193 (for code1 (eq ne)
3194  (for code2 (eq ne lt gt le ge)
3195   (simplify
3196    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3197    (if ((TREE_CODE (@1) == INTEGER_CST
3198          && TREE_CODE (@2) == INTEGER_CST)
3199         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3200             || POINTER_TYPE_P (TREE_TYPE (@1)))
3201             && bitwise_equal_p (@1, @2)))
3202     (with
3203      {
3204       bool one_before = false;
3205       bool one_after = false;
3206       int cmp = 0;
3207       bool allbits = true;
3208       if (TREE_CODE (@1) == INTEGER_CST
3209           && TREE_CODE (@2) == INTEGER_CST)
3210         {
3211           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3212           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3213           auto t2 = wi::to_wide (@2);
3214           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3215           if (cmp < 0
3216               && t1 == t2 - 1)
3217             one_before = true;
3218           if (cmp > 0
3219               && t1 == t2 + 1)
3220             one_after = true;
3221         }
3222       bool val;
3223       switch (code2)
3224         {
3225         case EQ_EXPR: val = (cmp == 0); break;
3226         case NE_EXPR: val = (cmp != 0); break;
3227         case LT_EXPR: val = (cmp < 0); break;
3228         case GT_EXPR: val = (cmp > 0); break;
3229         case LE_EXPR: val = (cmp <= 0); break;
3230         case GE_EXPR: val = (cmp >= 0); break;
3231         default: gcc_unreachable ();
3232         }
3233      }
3234      (switch
3235       (if (code1 == EQ_EXPR && val) @4)
3236       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3237       (if (code1 == NE_EXPR && !val && allbits) @3)
3238       (if (code1 == EQ_EXPR
3239            && code2 == GT_EXPR
3240            && cmp == 0
3241            && allbits)
3242        (ge @c0 @2))
3243       (if (code1 == EQ_EXPR
3244            && code2 == LT_EXPR
3245            && cmp == 0
3246            && allbits)
3247        (le @c0 @2))
3248       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3249       (if (code1 == EQ_EXPR
3250            && code2 == GE_EXPR
3251            && one_before
3252            && allbits)
3253        (ge @c0 (convert @1)))
3254       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3255       (if (code1 == EQ_EXPR
3256            && code2 == LE_EXPR
3257            && one_after
3258            && allbits)
3259        (le @c0 (convert @1)))
3260      )
3261     )
3262    )
3263   )
3267 /* Convert (X OP1 CST1) || (X OP2 CST2).
3268    Convert (X OP1 Y)    || (X OP2 Y).  */
3270 (for code1 (lt le gt ge)
3271  (for code2 (lt le gt ge)
3272   (simplify
3273   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3274   (if ((TREE_CODE (@1) == INTEGER_CST
3275         && TREE_CODE (@2) == INTEGER_CST)
3276        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3277             || POINTER_TYPE_P (TREE_TYPE (@1)))
3278            && operand_equal_p (@1, @2)))
3279    (with
3280     {
3281      int cmp = 0;
3282      if (TREE_CODE (@1) == INTEGER_CST
3283          && TREE_CODE (@2) == INTEGER_CST)
3284        cmp = tree_int_cst_compare (@1, @2);
3285     }
3286     (switch
3287      /* Choose the more restrictive of two < or <= comparisons.  */
3288      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3289           && (code2 == LT_EXPR || code2 == LE_EXPR))
3290       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3291        @4
3292        @3))
3293      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3294      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3295           && (code2 == GT_EXPR || code2 == GE_EXPR))
3296       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3297        @4
3298        @3))
3299      /* Check for singleton ranges.  */
3300      (if (cmp == 0
3301           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3302               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3303       (ne @0 @2))
3304      /* Check for disjoint ranges.  */
3305      (if (cmp >= 0
3306           && (code1 == LT_EXPR || code1 == LE_EXPR)
3307           && (code2 == GT_EXPR || code2 == GE_EXPR))
3308       { constant_boolean_node (true, type); })
3309      (if (cmp <= 0
3310           && (code1 == GT_EXPR || code1 == GE_EXPR)
3311           && (code2 == LT_EXPR || code2 == LE_EXPR))
3312       { constant_boolean_node (true, type); })
3313      ))))))
3315 /* Optimize (a CMP b) ^ (a CMP b)  */
3316 /* Optimize (a CMP b) != (a CMP b)  */
3317 (for op (bit_xor ne)
3318  (for cmp1 (lt lt lt le le le)
3319       cmp2 (gt eq ne ge eq ne)
3320       rcmp (ne le gt ne lt ge)
3321   (simplify
3322    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3323    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3324     (rcmp @0 @1)))))
3326 /* Optimize (a CMP b) == (a CMP b)  */
3327 (for cmp1 (lt lt lt le le le)
3328      cmp2 (gt eq ne ge eq ne)
3329      rcmp (eq gt le eq ge lt)
3330  (simplify
3331   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3332   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3333     (rcmp @0 @1))))
3335 /* We can't reassociate at all for saturating types.  */
3336 (if (!TYPE_SATURATING (type))
3338  /* Contract negates.  */
3339  /* A + (-B) -> A - B */
3340  (simplify
3341   (plus:c @0 (convert? (negate @1)))
3342   /* Apply STRIP_NOPS on the negate.  */
3343   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3344        && !TYPE_OVERFLOW_SANITIZED (type))
3345    (with
3346     {
3347      tree t1 = type;
3348      if (INTEGRAL_TYPE_P (type)
3349          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3350        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3351     }
3352     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3353  /* A - (-B) -> A + B */
3354  (simplify
3355   (minus @0 (convert? (negate @1)))
3356   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3357        && !TYPE_OVERFLOW_SANITIZED (type))
3358    (with
3359     {
3360      tree t1 = type;
3361      if (INTEGRAL_TYPE_P (type)
3362          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3363        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3364     }
3365     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3366  /* -(T)(-A) -> (T)A
3367     Sign-extension is ok except for INT_MIN, which thankfully cannot
3368     happen without overflow.  */
3369  (simplify
3370   (negate (convert (negate @1)))
3371   (if (INTEGRAL_TYPE_P (type)
3372        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3373            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3374                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3375        && !TYPE_OVERFLOW_SANITIZED (type)
3376        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3377    (convert @1)))
3378  (simplify
3379   (negate (convert negate_expr_p@1))
3380   (if (SCALAR_FLOAT_TYPE_P (type)
3381        && ((DECIMAL_FLOAT_TYPE_P (type)
3382             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3383             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3384            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3385    (convert (negate @1))))
3386  (simplify
3387   (negate (nop_convert? (negate @1)))
3388   (if (!TYPE_OVERFLOW_SANITIZED (type)
3389        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3390    (view_convert @1)))
3392  /* We can't reassociate floating-point unless -fassociative-math
3393     or fixed-point plus or minus because of saturation to +-Inf.  */
3394  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3395       && !FIXED_POINT_TYPE_P (type))
3397   /* Match patterns that allow contracting a plus-minus pair
3398      irrespective of overflow issues.  */
3399   /* (A +- B) - A       ->  +- B */
3400   /* (A +- B) -+ B      ->  A */
3401   /* A - (A +- B)       -> -+ B */
3402   /* A +- (B -+ A)      ->  +- B */
3403   (simplify
3404    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3405    (view_convert @1))
3406   (simplify
3407    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3408    (if (!ANY_INTEGRAL_TYPE_P (type)
3409         || TYPE_OVERFLOW_WRAPS (type))
3410    (negate (view_convert @1))
3411    (view_convert (negate @1))))
3412   (simplify
3413    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3414    (view_convert @0))
3415   (simplify
3416    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3417     (if (!ANY_INTEGRAL_TYPE_P (type)
3418          || TYPE_OVERFLOW_WRAPS (type))
3419      (negate (view_convert @1))
3420      (view_convert (negate @1))))
3421   (simplify
3422    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3423    (view_convert @1))
3424   /* (A +- B) + (C - A)   -> C +- B */
3425   /* (A +  B) - (A - C)   -> B + C */
3426   /* More cases are handled with comparisons.  */
3427   (simplify
3428    (plus:c (plus:c @0 @1) (minus @2 @0))
3429    (plus @2 @1))
3430   (simplify
3431    (plus:c (minus @0 @1) (minus @2 @0))
3432    (minus @2 @1))
3433   (simplify
3434    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3435    (if (TYPE_OVERFLOW_UNDEFINED (type)
3436         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3437     (pointer_diff @2 @1)))
3438   (simplify
3439    (minus (plus:c @0 @1) (minus @0 @2))
3440    (plus @1 @2))
3442   /* (A +- CST1) +- CST2 -> A + CST3
3443      Use view_convert because it is safe for vectors and equivalent for
3444      scalars.  */
3445   (for outer_op (plus minus)
3446    (for inner_op (plus minus)
3447         neg_inner_op (minus plus)
3448     (simplify
3449      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3450                CONSTANT_CLASS_P@2)
3451      /* If one of the types wraps, use that one.  */
3452      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3453       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3454          forever if something doesn't simplify into a constant.  */
3455       (if (!CONSTANT_CLASS_P (@0))
3456        (if (outer_op == PLUS_EXPR)
3457         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3458         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3459       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3460            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3461        (if (outer_op == PLUS_EXPR)
3462         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3463         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3464        /* If the constant operation overflows we cannot do the transform
3465           directly as we would introduce undefined overflow, for example
3466           with (a - 1) + INT_MIN.  */
3467        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3468         (with { tree cst = const_binop (outer_op == inner_op
3469                                         ? PLUS_EXPR : MINUS_EXPR,
3470                                         type, @1, @2); }
3471          (if (cst)
3472           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3473            (inner_op @0 { cst; } )
3474            /* X+INT_MAX+1 is X-INT_MIN.  */
3475            (if (INTEGRAL_TYPE_P (type)
3476                 && wi::to_wide (cst) == wi::min_value (type))
3477             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3478             /* Last resort, use some unsigned type.  */
3479             (with { tree utype = unsigned_type_for (type); }
3480              (if (utype)
3481               (view_convert (inner_op
3482                              (view_convert:utype @0)
3483                              (view_convert:utype
3484                               { TREE_OVERFLOW (cst)
3485                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3487   /* (CST1 - A) +- CST2 -> CST3 - A  */
3488   (for outer_op (plus minus)
3489    (simplify
3490     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3491     /* If one of the types wraps, use that one.  */
3492     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3493      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3494         forever if something doesn't simplify into a constant.  */
3495      (if (!CONSTANT_CLASS_P (@0))
3496       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3497      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3498           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3499       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3500       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3501        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3502         (if (cst && !TREE_OVERFLOW (cst))
3503          (minus { cst; } @0))))))))
3505   /* CST1 - (CST2 - A) -> CST3 + A
3506      Use view_convert because it is safe for vectors and equivalent for
3507      scalars.  */
3508   (simplify
3509    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3510    /* If one of the types wraps, use that one.  */
3511    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3512     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3513       forever if something doesn't simplify into a constant.  */
3514     (if (!CONSTANT_CLASS_P (@0))
3515      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3516     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3517          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3518      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3519      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3520       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3521        (if (cst && !TREE_OVERFLOW (cst))
3522         (plus { cst; } @0)))))))
3524 /* ((T)(A)) + CST -> (T)(A + CST)  */
3525 #if GIMPLE
3526   (simplify
3527    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3528     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3529          && TREE_CODE (type) == INTEGER_TYPE
3530          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3531          && int_fits_type_p (@1, TREE_TYPE (@0)))
3532      /* Perform binary operation inside the cast if the constant fits
3533         and (A + CST)'s range does not overflow.  */
3534      (with
3535       {
3536         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3537                           max_ovf = wi::OVF_OVERFLOW;
3538         tree inner_type = TREE_TYPE (@0);
3540         wide_int w1
3541           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3542                             TYPE_SIGN (inner_type));
3544         value_range vr;
3545         if (get_global_range_query ()->range_of_expr (vr, @0)
3546             && !vr.varying_p () && !vr.undefined_p ())
3547           {
3548             wide_int wmin0 = vr.lower_bound ();
3549             wide_int wmax0 = vr.upper_bound ();
3550             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3551             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3552           }
3553       }
3554      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3555       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3556      )))
3557 #endif
3559 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3560 #if GIMPLE
3561   (for op (plus minus)
3562    (simplify
3563     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3564      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3565           && TREE_CODE (type) == INTEGER_TYPE
3566           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3567           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3568           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3569           && TYPE_OVERFLOW_WRAPS (type))
3570        (plus (convert @0) (op @2 (convert @1))))))
3571 #endif
3573 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3574    to a simple value.  */
3575   (for op (plus minus)
3576    (simplify
3577     (op (convert @0) (convert @1))
3578      (if (INTEGRAL_TYPE_P (type)
3579           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3580           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3581           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3582           && !TYPE_OVERFLOW_TRAPS (type)
3583           && !TYPE_OVERFLOW_SANITIZED (type))
3584       (convert (op! @0 @1)))))
3586   /* ~A + A -> -1 */
3587   (simplify
3588    (plus:c (convert? (bit_not @0)) (convert? @0))
3589    (if (!TYPE_OVERFLOW_TRAPS (type))
3590     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3592   /* ~A + 1 -> -A */
3593   (simplify
3594    (plus (convert? (bit_not @0)) integer_each_onep)
3595    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3596     (negate (convert @0))))
3598   /* -A - 1 -> ~A */
3599   (simplify
3600    (minus (convert? (negate @0)) integer_each_onep)
3601    (if (!TYPE_OVERFLOW_TRAPS (type)
3602         && TREE_CODE (type) != COMPLEX_TYPE
3603         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3604     (bit_not (convert @0))))
3606   /* -1 - A -> ~A */
3607   (simplify
3608    (minus integer_all_onesp @0)
3609    (if (TREE_CODE (type) != COMPLEX_TYPE)
3610     (bit_not @0)))
3612   /* (T)(P + A) - (T)P -> (T) A */
3613   (simplify
3614    (minus (convert (plus:c @@0 @1))
3615     (convert? @0))
3616    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3617         /* For integer types, if A has a smaller type
3618            than T the result depends on the possible
3619            overflow in P + A.
3620            E.g. T=size_t, A=(unsigned)429497295, P>0.
3621            However, if an overflow in P + A would cause
3622            undefined behavior, we can assume that there
3623            is no overflow.  */
3624         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3625             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3626     (convert @1)))
3627   (simplify
3628    (minus (convert (pointer_plus @@0 @1))
3629     (convert @0))
3630    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3631         /* For pointer types, if the conversion of A to the
3632            final type requires a sign- or zero-extension,
3633            then we have to punt - it is not defined which
3634            one is correct.  */
3635         || (POINTER_TYPE_P (TREE_TYPE (@0))
3636             && TREE_CODE (@1) == INTEGER_CST
3637             && tree_int_cst_sign_bit (@1) == 0))
3638     (convert @1)))
3639    (simplify
3640     (pointer_diff (pointer_plus @@0 @1) @0)
3641     /* The second argument of pointer_plus must be interpreted as signed, and
3642        thus sign-extended if necessary.  */
3643     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3644      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3645         second arg is unsigned even when we need to consider it as signed,
3646         we don't want to diagnose overflow here.  */
3647      (convert (view_convert:stype @1))))
3649   /* (T)P - (T)(P + A) -> -(T) A */
3650   (simplify
3651    (minus (convert? @0)
3652     (convert (plus:c @@0 @1)))
3653    (if (INTEGRAL_TYPE_P (type)
3654         && TYPE_OVERFLOW_UNDEFINED (type)
3655         /* For integer literals, using an intermediate unsigned type to avoid
3656            an overflow at run time is counter-productive because it introduces
3657            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3658            the result, which may be problematic in GENERIC for some front-ends:
3659              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3660            so we use the direct path for them.  */
3661         && TREE_CODE (@1) != INTEGER_CST
3662         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3663     (with { tree utype = unsigned_type_for (type); }
3664      (convert (negate (convert:utype @1))))
3665     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3666          /* For integer types, if A has a smaller type
3667             than T the result depends on the possible
3668             overflow in P + A.
3669             E.g. T=size_t, A=(unsigned)429497295, P>0.
3670             However, if an overflow in P + A would cause
3671             undefined behavior, we can assume that there
3672             is no overflow.  */
3673          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3674              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3675      (negate (convert @1)))))
3676   (simplify
3677    (minus (convert @0)
3678     (convert (pointer_plus @@0 @1)))
3679    (if (INTEGRAL_TYPE_P (type)
3680         && TYPE_OVERFLOW_UNDEFINED (type)
3681         /* See above the rationale for this condition.  */
3682         && TREE_CODE (@1) != INTEGER_CST
3683         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3684     (with { tree utype = unsigned_type_for (type); }
3685      (convert (negate (convert:utype @1))))
3686     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3687          /* For pointer types, if the conversion of A to the
3688             final type requires a sign- or zero-extension,
3689             then we have to punt - it is not defined which
3690             one is correct.  */
3691          || (POINTER_TYPE_P (TREE_TYPE (@0))
3692              && TREE_CODE (@1) == INTEGER_CST
3693              && tree_int_cst_sign_bit (@1) == 0))
3694      (negate (convert @1)))))
3695    (simplify
3696     (pointer_diff @0 (pointer_plus @@0 @1))
3697     /* The second argument of pointer_plus must be interpreted as signed, and
3698        thus sign-extended if necessary.  */
3699     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3700      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3701         second arg is unsigned even when we need to consider it as signed,
3702         we don't want to diagnose overflow here.  */
3703      (negate (convert (view_convert:stype @1)))))
3705   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3706   (simplify
3707    (minus (convert (plus:c @@0 @1))
3708     (convert (plus:c @0 @2)))
3709    (if (INTEGRAL_TYPE_P (type)
3710         && TYPE_OVERFLOW_UNDEFINED (type)
3711         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3712         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3713     (with { tree utype = unsigned_type_for (type); }
3714      (convert (minus (convert:utype @1) (convert:utype @2))))
3715     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3716           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3717          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3718              /* For integer types, if A has a smaller type
3719                 than T the result depends on the possible
3720                 overflow in P + A.
3721                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3722                 However, if an overflow in P + A would cause
3723                 undefined behavior, we can assume that there
3724                 is no overflow.  */
3725              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3726                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3727                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3728                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3729      (minus (convert @1) (convert @2)))))
3730   (simplify
3731    (minus (convert (pointer_plus @@0 @1))
3732     (convert (pointer_plus @0 @2)))
3733    (if (INTEGRAL_TYPE_P (type)
3734         && TYPE_OVERFLOW_UNDEFINED (type)
3735         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3736     (with { tree utype = unsigned_type_for (type); }
3737      (convert (minus (convert:utype @1) (convert:utype @2))))
3738     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3739          /* For pointer types, if the conversion of A to the
3740             final type requires a sign- or zero-extension,
3741             then we have to punt - it is not defined which
3742             one is correct.  */
3743          || (POINTER_TYPE_P (TREE_TYPE (@0))
3744              && TREE_CODE (@1) == INTEGER_CST
3745              && tree_int_cst_sign_bit (@1) == 0
3746              && TREE_CODE (@2) == INTEGER_CST
3747              && tree_int_cst_sign_bit (@2) == 0))
3748      (minus (convert @1) (convert @2)))))
3749    (simplify
3750     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3751      (pointer_diff @0 @1))
3752    (simplify
3753     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3754     /* The second argument of pointer_plus must be interpreted as signed, and
3755        thus sign-extended if necessary.  */
3756     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3757      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3758         second arg is unsigned even when we need to consider it as signed,
3759         we don't want to diagnose overflow here.  */
3760      (minus (convert (view_convert:stype @1))
3761             (convert (view_convert:stype @2)))))))
3763 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3764     Modeled after fold_plusminus_mult_expr.  */
3765 (if (!TYPE_SATURATING (type)
3766      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3767  (for plusminus (plus minus)
3768   (simplify
3769    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3770    (if (!ANY_INTEGRAL_TYPE_P (type)
3771         || TYPE_OVERFLOW_WRAPS (type)
3772         || (INTEGRAL_TYPE_P (type)
3773             && tree_expr_nonzero_p (@0)
3774             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3775     (if (single_use (@3) || single_use (@4))
3776      /* If @1 +- @2 is constant require a hard single-use on either
3777         original operand (but not on both).  */
3778      (mult (plusminus @1 @2) @0)
3779      (mult! (plusminus @1 @2) @0)
3780   )))
3781   /* We cannot generate constant 1 for fract.  */
3782   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3783    (simplify
3784     (plusminus @0 (mult:c@3 @0 @2))
3785     (if ((!ANY_INTEGRAL_TYPE_P (type)
3786           || TYPE_OVERFLOW_WRAPS (type)
3787           /* For @0 + @0*@2 this transformation would introduce UB
3788              (where there was none before) for @0 in [-1,0] and @2 max.
3789              For @0 - @0*@2 this transformation would introduce UB
3790              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3791           || (INTEGRAL_TYPE_P (type)
3792               && ((tree_expr_nonzero_p (@0)
3793                    && expr_not_equal_to (@0,
3794                                 wi::minus_one (TYPE_PRECISION (type))))
3795                   || (plusminus == PLUS_EXPR
3796                       ? expr_not_equal_to (@2,
3797                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3798                       /* Let's ignore the @0 -1 and @2 min case.  */
3799                       : (expr_not_equal_to (@2,
3800                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3801                          && expr_not_equal_to (@2,
3802                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3803                                 + 1))))))
3804          && single_use (@3))
3805      (mult (plusminus { build_one_cst (type); } @2) @0)))
3806    (simplify
3807     (plusminus (mult:c@3 @0 @2) @0)
3808     (if ((!ANY_INTEGRAL_TYPE_P (type)
3809           || TYPE_OVERFLOW_WRAPS (type)
3810           /* For @0*@2 + @0 this transformation would introduce UB
3811              (where there was none before) for @0 in [-1,0] and @2 max.
3812              For @0*@2 - @0 this transformation would introduce UB
3813              for @0 0 and @2 min.  */
3814           || (INTEGRAL_TYPE_P (type)
3815               && ((tree_expr_nonzero_p (@0)
3816                    && (plusminus == MINUS_EXPR
3817                        || expr_not_equal_to (@0,
3818                                 wi::minus_one (TYPE_PRECISION (type)))))
3819                   || expr_not_equal_to (@2,
3820                         (plusminus == PLUS_EXPR
3821                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3822                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3823          && single_use (@3))
3824      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3826 #if GIMPLE
3827 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3828    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3829 (simplify
3830  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3831   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3832        && tree_fits_uhwi_p (@1)
3833        && tree_to_uhwi (@1) < element_precision (type)
3834        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3835            || optab_handler (smul_optab,
3836                              TYPE_MODE (type)) != CODE_FOR_nothing))
3837    (with { tree t = type;
3838            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3839            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3840                                              element_precision (type));
3841            w += 1;
3842            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3843                                         : t, w);
3844            cst = build_uniform_cst (t, cst); }
3845     (convert (mult (convert:t @0) { cst; })))))
3846 (simplify
3847  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3848   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3849        && tree_fits_uhwi_p (@1)
3850        && tree_to_uhwi (@1) < element_precision (type)
3851        && tree_fits_uhwi_p (@2)
3852        && tree_to_uhwi (@2) < element_precision (type)
3853        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3854            || optab_handler (smul_optab,
3855                              TYPE_MODE (type)) != CODE_FOR_nothing))
3856    (with { tree t = type;
3857            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3858            unsigned int prec = element_precision (type);
3859            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3860            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3861            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3862                                         : t, w);
3863            cst = build_uniform_cst (t, cst); }
3864     (convert (mult (convert:t @0) { cst; })))))
3865 #endif
3867 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3868    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3869    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3870 (for op (bit_ior bit_xor)
3871  (simplify
3872   (op (mult:s@0 @1 INTEGER_CST@2)
3873       (mult:s@3 @1 INTEGER_CST@4))
3874   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3875        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3876    (mult @1
3877          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3878  (simplify
3879   (op:c (mult:s@0 @1 INTEGER_CST@2)
3880         (lshift:s@3 @1 INTEGER_CST@4))
3881   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3882        && tree_int_cst_sgn (@4) > 0
3883        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3884    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3885            wide_int c = wi::add (wi::to_wide (@2),
3886                                  wi::lshift (wone, wi::to_wide (@4))); }
3887     (mult @1 { wide_int_to_tree (type, c); }))))
3888  (simplify
3889   (op:c (mult:s@0 @1 INTEGER_CST@2)
3890         @1)
3891   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3892        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3893    (mult @1
3894          { wide_int_to_tree (type,
3895                              wi::add (wi::to_wide (@2), 1)); })))
3896  (simplify
3897   (op (lshift:s@0 @1 INTEGER_CST@2)
3898       (lshift:s@3 @1 INTEGER_CST@4))
3899   (if (INTEGRAL_TYPE_P (type)
3900        && tree_int_cst_sgn (@2) > 0
3901        && tree_int_cst_sgn (@4) > 0
3902        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3903    (with { tree t = type;
3904            if (!TYPE_OVERFLOW_WRAPS (t))
3905              t = unsigned_type_for (t);
3906            wide_int wone = wi::one (TYPE_PRECISION (t));
3907            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3908                                  wi::lshift (wone, wi::to_wide (@4))); }
3909     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3910  (simplify
3911   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3912         @1)
3913   (if (INTEGRAL_TYPE_P (type)
3914        && tree_int_cst_sgn (@2) > 0
3915        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3916    (with { tree t = type;
3917            if (!TYPE_OVERFLOW_WRAPS (t))
3918              t = unsigned_type_for (t);
3919            wide_int wone = wi::one (TYPE_PRECISION (t));
3920            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3921     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3923 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3925 (for minmax (min max)
3926  (simplify
3927   (minmax @0 @0)
3928   @0)
3929 /* max(max(x,y),x) -> max(x,y)  */
3930  (simplify
3931   (minmax:c (minmax:c@2 @0 @1) @0)
3932   @2))
3933 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3934 (for minmax (FMIN_ALL FMAX_ALL)
3935  (simplify
3936   (minmax @0 @0)
3937   (if (!tree_expr_maybe_signaling_nan_p (@0))
3938     @0)))
3939 /* min(max(x,y),y) -> y.  */
3940 (simplify
3941  (min:c (max:c @0 @1) @1)
3942  @1)
3943 /* max(min(x,y),y) -> y.  */
3944 (simplify
3945  (max:c (min:c @0 @1) @1)
3946  @1)
3947 /* max(a,-a) -> abs(a).  */
3948 (simplify
3949  (max:c @0 (negate @0))
3950  (if (TREE_CODE (type) != COMPLEX_TYPE
3951       && (! ANY_INTEGRAL_TYPE_P (type)
3952           || TYPE_OVERFLOW_UNDEFINED (type)))
3953   (abs @0)))
3954 /* min(a,-a) -> -abs(a).  */
3955 (simplify
3956  (min:c @0 (negate @0))
3957  (if (TREE_CODE (type) != COMPLEX_TYPE
3958       && (! ANY_INTEGRAL_TYPE_P (type)
3959           || TYPE_OVERFLOW_UNDEFINED (type)))
3960   (negate (abs @0))))
3961 (simplify
3962  (min @0 @1)
3963  (switch
3964   (if (INTEGRAL_TYPE_P (type)
3965        && TYPE_MIN_VALUE (type)
3966        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3967    @1)
3968   (if (INTEGRAL_TYPE_P (type)
3969        && TYPE_MAX_VALUE (type)
3970        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3971    @0)))
3972 (simplify
3973  (max @0 @1)
3974  (switch
3975   (if (INTEGRAL_TYPE_P (type)
3976        && TYPE_MAX_VALUE (type)
3977        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3978    @1)
3979   (if (INTEGRAL_TYPE_P (type)
3980        && TYPE_MIN_VALUE (type)
3981        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3982    @0)))
3984 /* max (a, a + CST) -> a + CST where CST is positive.  */
3985 /* max (a, a + CST) -> a where CST is negative.  */
3986 (simplify
3987  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3988   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3989    (if (tree_int_cst_sgn (@1) > 0)
3990     @2
3991     @0)))
3993 /* min (a, a + CST) -> a where CST is positive.  */
3994 /* min (a, a + CST) -> a + CST where CST is negative. */
3995 (simplify
3996  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3997   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3998    (if (tree_int_cst_sgn (@1) > 0)
3999     @0
4000     @2)))
4002 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4003    the addresses are known to be less, equal or greater.  */
4004 (for minmax (min max)
4005      cmp (lt gt)
4006  (simplify
4007   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4008   (with
4009    {
4010      poly_int64 off0, off1;
4011      tree base0, base1;
4012      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4013                                   off0, off1, GENERIC);
4014    }
4015    (if (equal == 1)
4016     (if (minmax == MIN_EXPR)
4017      (if (known_le (off0, off1))
4018       @2
4019       (if (known_gt (off0, off1))
4020        @3))
4021      (if (known_ge (off0, off1))
4022       @2
4023       (if (known_lt (off0, off1))
4024        @3)))))))
4026 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4027    and the outer convert demotes the expression back to x's type.  */
4028 (for minmax (min max)
4029  (simplify
4030   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4031   (if (INTEGRAL_TYPE_P (type)
4032        && types_match (@1, type) && int_fits_type_p (@2, type)
4033        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4034        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4035    (minmax @1 (convert @2)))))
4037 (for minmax (FMIN_ALL FMAX_ALL)
4038  /* If either argument is NaN and other one is not sNaN, return the other
4039     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4040  (simplify
4041   (minmax:c @0 REAL_CST@1)
4042    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4043        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4044        && !tree_expr_maybe_signaling_nan_p (@0))
4045    @0)))
4046 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4047    functions to return the numeric arg if the other one is NaN.
4048    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4049    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4050    worry about it either.  */
4051 (if (flag_finite_math_only)
4052  (simplify
4053   (FMIN_ALL @0 @1)
4054   (min @0 @1))
4055  (simplify
4056   (FMAX_ALL @0 @1)
4057   (max @0 @1)))
4058 /* min (-A, -B) -> -max (A, B)  */
4059 (for minmax (min max FMIN_ALL FMAX_ALL)
4060      maxmin (max min FMAX_ALL FMIN_ALL)
4061  (simplify
4062   (minmax (negate:s@2 @0) (negate:s@3 @1))
4063   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4064        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4065            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4066    (negate (maxmin @0 @1)))))
4067 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4068    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4069 (for minmax (min max)
4070  maxmin (max min)
4071  (simplify
4072   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4073   (bit_not (maxmin @0 @1)))
4074 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4075 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4076  (simplify
4077   (bit_not (minmax:cs (bit_not @0) @1))
4078   (maxmin @0 (bit_not @1))))
4080 /* MIN (X, Y) == X -> X <= Y  */
4081 /* MIN (X, Y) < X -> X > Y  */
4082 /* MIN (X, Y) >= X -> X <= Y  */
4083 (for minmax (min min min min max max max max)
4084      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4085      out    (le  gt  gt  le  ge  lt  lt  ge )
4086  (simplify
4087   (cmp:c (minmax:c @0 @1) @0)
4088   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4089    (out @0 @1))))
4090 /* MIN (X, 5) == 0 -> X == 0
4091    MIN (X, 5) == 7 -> false  */
4092 (for cmp (eq ne)
4093  (simplify
4094   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4095   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4096                  TYPE_SIGN (TREE_TYPE (@0))))
4097    { constant_boolean_node (cmp == NE_EXPR, type); }
4098    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4099                   TYPE_SIGN (TREE_TYPE (@0))))
4100     (cmp @0 @2)))))
4101 (for cmp (eq ne)
4102  (simplify
4103   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4104   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4105                  TYPE_SIGN (TREE_TYPE (@0))))
4106    { constant_boolean_node (cmp == NE_EXPR, type); }
4107    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4108                   TYPE_SIGN (TREE_TYPE (@0))))
4109     (cmp @0 @2)))))
4111 /* X <= MAX(X, Y) -> true
4112    X > MAX(X, Y) -> false 
4113    X >= MIN(X, Y) -> true
4114    X < MIN(X, Y) -> false */
4115 (for minmax (min     min     max     max     )
4116      cmp    (ge      lt      le      gt      )
4117  (simplify
4118   (cmp:c @0 (minmax:c @0 @1))
4119   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4121 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4122 (for minmax (min     min     max     max     min     min     max     max    )
4123      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4124      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4125  (simplify
4126   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4127   (comb (cmp @0 @2) (cmp @1 @2))))
4129 /* Undo fancy ways of writing max/min or other ?: expressions, like
4130    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4131    People normally use ?: and that is what we actually try to optimize.  */
4132 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4133 (simplify
4134  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4135  (if (INTEGRAL_TYPE_P (type)
4136       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4137   (cond (convert:boolean_type_node @2) @1 @0)))
4138 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4139 (simplify
4140  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4141  (if (INTEGRAL_TYPE_P (type)
4142       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4143   (cond (convert:boolean_type_node @2) @1 @0)))
4144 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4145 (simplify
4146  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4147  (if (INTEGRAL_TYPE_P (type)
4148       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4149   (cond (convert:boolean_type_node @2) @1 @0)))
4151 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4152 (simplify
4153   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4154   (max @2 @1))
4156 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4157 (for op (bit_xor bit_ior plus)
4158  (simplify
4159   (cond (eq zero_one_valued_p@0
4160             integer_zerop)
4161         @1
4162         (op:c @2 @1))
4163   (if (INTEGRAL_TYPE_P (type)
4164        && TYPE_PRECISION (type) > 1
4165        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4166        (op (mult (convert:type @0) @2) @1))))
4168 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4169 (for op (bit_xor bit_ior plus)
4170  (simplify
4171   (cond (ne zero_one_valued_p@0
4172             integer_zerop)
4173        (op:c @2 @1)
4174         @1)
4175   (if (INTEGRAL_TYPE_P (type)
4176        && TYPE_PRECISION (type) > 1
4177        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4178        (op (mult (convert:type @0) @2) @1))))
4180 /* ?: Value replacement. */
4181 /* a == 0 ? b : b + a  -> b + a */
4182 (for op (plus bit_ior bit_xor)
4183  (simplify
4184   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4185    @2))
4186 /* a == 0 ? b : b - a  -> b - a */
4187 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4188 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4189 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4190  (simplify
4191   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4192    @2))
4194 /* a == 1 ? b : b / a  -> b / a */
4195 (for op (trunc_div ceil_div floor_div round_div exact_div)
4196  (simplify
4197   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4198    @2))
4200 /* a == 1 ? b : a * b -> a * b */
4201 (for op (mult)
4202  (simplify
4203   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4204    @2))
4206 /* a == -1 ? b : a & b -> a & b */
4207 (for op (bit_and)
4208  (simplify
4209   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4210    @2))
4212 /* Simplifications of shift and rotates.  */
4214 (for rotate (lrotate rrotate)
4215  (simplify
4216   (rotate integer_all_onesp@0 @1)
4217   @0))
4219 /* Optimize -1 >> x for arithmetic right shifts.  */
4220 (simplify
4221  (rshift integer_all_onesp@0 @1)
4222  (if (!TYPE_UNSIGNED (type))
4223   @0))
4225 /* Optimize (x >> c) << c into x & (-1<<c).  */
4226 (simplify
4227  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4228  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4229   /* It doesn't matter if the right shift is arithmetic or logical.  */
4230   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4232 (simplify
4233  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4234  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4235       /* Allow intermediate conversion to integral type with whatever sign, as
4236          long as the low TYPE_PRECISION (type)
4237          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4238       && INTEGRAL_TYPE_P (type)
4239       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4240       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4241       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4242       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4243           || wi::geu_p (wi::to_wide (@1),
4244                         TYPE_PRECISION (type)
4245                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4246   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4248 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4249    unsigned x OR truncate into the precision(type) - c lowest bits
4250    of signed x (if they have mode precision or a precision of 1).  */
4251 (simplify
4252  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4253  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4254   (if (TYPE_UNSIGNED (type))
4255    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4256    (if (INTEGRAL_TYPE_P (type))
4257     (with {
4258       int width = element_precision (type) - tree_to_uhwi (@1);
4259       tree stype = NULL_TREE;
4260       if (width <= MAX_FIXED_MODE_SIZE)
4261         stype = build_nonstandard_integer_type (width, 0);
4262      }
4263      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4264       (convert (convert:stype @0))))))))
4266 /* Optimize x >> x into 0 */
4267 (simplify
4268  (rshift @0 @0)
4269   { build_zero_cst (type); })
4271 (for shiftrotate (lrotate rrotate lshift rshift)
4272  (simplify
4273   (shiftrotate @0 integer_zerop)
4274   (non_lvalue @0))
4275  (simplify
4276   (shiftrotate integer_zerop@0 @1)
4277   @0)
4278  /* Prefer vector1 << scalar to vector1 << vector2
4279     if vector2 is uniform.  */
4280  (for vec (VECTOR_CST CONSTRUCTOR)
4281   (simplify
4282    (shiftrotate @0 vec@1)
4283    (with { tree tem = uniform_vector_p (@1); }
4284     (if (tem)
4285      (shiftrotate @0 { tem; }))))))
4287 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4288    Y is 0.  Similarly for X >> Y.  */
4289 #if GIMPLE
4290 (for shift (lshift rshift)
4291  (simplify
4292   (shift @0 SSA_NAME@1)
4293    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4294     (with {
4295       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4296       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4297      }
4298      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4299       @0)))))
4300 #endif
4302 /* Rewrite an LROTATE_EXPR by a constant into an
4303    RROTATE_EXPR by a new constant.  */
4304 (simplify
4305  (lrotate @0 INTEGER_CST@1)
4306  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4307                             build_int_cst (TREE_TYPE (@1),
4308                                            element_precision (type)), @1); }))
4310 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4311 (for op (lrotate rrotate rshift lshift)
4312  (simplify
4313   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4314   (with { unsigned int prec = element_precision (type); }
4315    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4316         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4317         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4318         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4319     (with { unsigned int low = (tree_to_uhwi (@1)
4320                                 + tree_to_uhwi (@2)); }
4321      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4322         being well defined.  */
4323      (if (low >= prec)
4324       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4325        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4326        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4327         { build_zero_cst (type); }
4328         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4329       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4332 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4333 (simplify
4334  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4335   (if ((wi::to_wide (@1) & 1) != 0)
4336    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4337    { build_zero_cst (type); }))
4339 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4340    either to false if D is smaller (unsigned comparison) than C, or to
4341    x == log2 (D) - log2 (C).  Similarly for right shifts.
4342    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4343 (for cmp (ne eq)
4344      icmp (eq ne)
4345  (simplify
4346   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4347    (with { int c1 = wi::clz (wi::to_wide (@1));
4348            int c2 = wi::clz (wi::to_wide (@2)); }
4349     (if (c1 < c2)
4350      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4351      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4352  (simplify
4353   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4354    (if (tree_int_cst_sgn (@1) > 0)
4355     (with { int c1 = wi::clz (wi::to_wide (@1));
4356             int c2 = wi::clz (wi::to_wide (@2)); }
4357      (if (c1 > c2)
4358       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4359       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4360  /* `(1 >> X) != 0` -> `X == 0` */
4361  /* `(1 >> X) == 0` -> `X != 0` */
4362  (simplify
4363   (cmp (rshift integer_onep@1 @0) integer_zerop)
4364    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4365     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4367 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4368    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4369    if CST2 != 0.  */
4370 (for cmp (ne eq)
4371  (simplify
4372   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4373   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4374    (if (cand < 0
4375         || (!integer_zerop (@2)
4376             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4377     { constant_boolean_node (cmp == NE_EXPR, type); }
4378     (if (!integer_zerop (@2)
4379          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4380      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4382 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4383         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4384 (for cmp (ne eq)
4385  (simplify
4386   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4387   (if (tree_fits_shwi_p (@1)
4388        && tree_to_shwi (@1) > 0
4389        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4390     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4391       { constant_boolean_node (cmp == NE_EXPR, type); }
4392       (with { wide_int c1 = wi::to_wide (@1);
4393               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4394               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4395         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4396              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4397  (simplify
4398   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4399   (if (tree_fits_shwi_p (@1)
4400        && tree_to_shwi (@1) > 0
4401        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4402     (with { tree t0 = TREE_TYPE (@0);
4403             unsigned int prec = TYPE_PRECISION (t0);
4404             wide_int c1 = wi::to_wide (@1);
4405             wide_int c2 = wi::to_wide (@2);
4406             wide_int c3 = wi::to_wide (@3);
4407             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4408       (if ((c2 & c3) != c3)
4409         { constant_boolean_node (cmp == NE_EXPR, type); }
4410         (if (TYPE_UNSIGNED (t0))
4411           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4412             { constant_boolean_node (cmp == NE_EXPR, type); }
4413             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4414                  { wide_int_to_tree (t0, c3 << c1); }))
4415           (with { wide_int smask = wi::arshift (sb, c1); }
4416             (switch
4417               (if ((c2 & smask) == 0)
4418                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4419                      { wide_int_to_tree (t0, c3 << c1); }))
4420               (if ((c3 & smask) == 0)
4421                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4422                      { wide_int_to_tree (t0, c3 << c1); }))
4423               (if ((c2 & smask) != (c3 & smask))
4424                 { constant_boolean_node (cmp == NE_EXPR, type); })
4425               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4426                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4428 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4429         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4430    if the new mask might be further optimized.  */
4431 (for shift (lshift rshift)
4432  (simplify
4433   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4434            INTEGER_CST@2)
4435    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4436         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4437         && tree_fits_uhwi_p (@1)
4438         && tree_to_uhwi (@1) > 0
4439         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4440     (with
4441      {
4442        unsigned int shiftc = tree_to_uhwi (@1);
4443        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4444        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4445        tree shift_type = TREE_TYPE (@3);
4446        unsigned int prec;
4448        if (shift == LSHIFT_EXPR)
4449          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4450        else if (shift == RSHIFT_EXPR
4451                 && type_has_mode_precision_p (shift_type))
4452          {
4453            prec = TYPE_PRECISION (TREE_TYPE (@3));
4454            tree arg00 = @0;
4455            /* See if more bits can be proven as zero because of
4456               zero extension.  */
4457            if (@3 != @0
4458                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4459              {
4460                tree inner_type = TREE_TYPE (@0);
4461                if (type_has_mode_precision_p (inner_type)
4462                    && TYPE_PRECISION (inner_type) < prec)
4463                  {
4464                    prec = TYPE_PRECISION (inner_type);
4465                    /* See if we can shorten the right shift.  */
4466                    if (shiftc < prec)
4467                      shift_type = inner_type;
4468                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4469                       it into (X, 0) later on by making sure zerobits
4470                       is all ones.  */
4471                  }
4472              }
4473            zerobits = HOST_WIDE_INT_M1U;
4474            if (shiftc < prec)
4475              {
4476                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4477                zerobits <<= prec - shiftc;
4478              }
4479            /* For arithmetic shift if sign bit could be set, zerobits
4480               can contain actually sign bits, so no transformation is
4481               possible, unless MASK masks them all away.  In that
4482               case the shift needs to be converted into logical shift.  */
4483            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4484                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4485              {
4486                if ((mask & zerobits) == 0)
4487                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4488                else
4489                  zerobits = 0;
4490              }
4491          }
4492      }
4493      /* ((X << 16) & 0xff00) is (X, 0).  */
4494      (if ((mask & zerobits) == mask)
4495       { build_int_cst (type, 0); }
4496       (with { newmask = mask | zerobits; }
4497        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4498         (with
4499          {
4500            /* Only do the transformation if NEWMASK is some integer
4501               mode's mask.  */
4502            for (prec = BITS_PER_UNIT;
4503                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4504              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4505                break;
4506          }
4507          (if (prec < HOST_BITS_PER_WIDE_INT
4508               || newmask == HOST_WIDE_INT_M1U)
4509           (with
4510            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4511            (if (!tree_int_cst_equal (newmaskt, @2))
4512             (if (shift_type != TREE_TYPE (@3))
4513              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4514              (bit_and @4 { newmaskt; })))))))))))))
4516 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4517 (for cmp (ne eq)
4518        icmp (eq ne)
4519  (simplify
4520   (cmp
4521    (bit_and
4522     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4523   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4524    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4525                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4527 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4528    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4529 (for shift (lshift rshift)
4530  (for bit_op (bit_and bit_xor bit_ior)
4531   (simplify
4532    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4533    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4534     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4535      (if (mask)
4536       (bit_op (shift (convert @0) @1) { mask; })))))))
4538 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4539 (simplify
4540  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4541   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4542        && (element_precision (TREE_TYPE (@0))
4543            <= element_precision (TREE_TYPE (@1))
4544            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4545    (with
4546     { tree shift_type = TREE_TYPE (@0); }
4547      (convert (rshift (convert:shift_type @1) @2)))))
4549 /* ~(~X >>r Y) -> X >>r Y
4550    ~(~X <<r Y) -> X <<r Y */
4551 (for rotate (lrotate rrotate)
4552  (simplify
4553   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4554    (if ((element_precision (TREE_TYPE (@0))
4555          <= element_precision (TREE_TYPE (@1))
4556          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4557         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4558             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4559     (with
4560      { tree rotate_type = TREE_TYPE (@0); }
4561       (convert (rotate (convert:rotate_type @1) @2))))))
4563 (for cmp (eq ne)
4564  (for rotate (lrotate rrotate)
4565       invrot (rrotate lrotate)
4566   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4567   (simplify
4568    (cmp (rotate @1 @0) (rotate @2 @0))
4569    (cmp @1 @2))
4570   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4571   (simplify
4572    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4573    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4574   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4575   (simplify
4576    (cmp (rotate @0 @1) INTEGER_CST@2)
4577     (if (integer_zerop (@2) || integer_all_onesp (@2))
4578      (cmp @0 @2)))))
4580 /* Narrow a lshift by constant.  */
4581 (simplify
4582  (convert (lshift:s@0 @1 INTEGER_CST@2))
4583  (if (INTEGRAL_TYPE_P (type)
4584       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4585       && !integer_zerop (@2)
4586       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4587   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4588        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4589    (lshift (convert @1) @2)
4590    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4591     { build_zero_cst (type); }))))
4593 /* Simplifications of conversions.  */
4595 /* Basic strip-useless-type-conversions / strip_nops.  */
4596 (for cvt (convert view_convert float fix_trunc)
4597  (simplify
4598   (cvt @0)
4599   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4600        || (GENERIC && type == TREE_TYPE (@0)))
4601    @0)))
4603 /* Contract view-conversions.  */
4604 (simplify
4605   (view_convert (view_convert @0))
4606   (view_convert @0))
4608 /* For integral conversions with the same precision or pointer
4609    conversions use a NOP_EXPR instead.  */
4610 (simplify
4611   (view_convert @0)
4612   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4613        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4614        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4615    (convert @0)))
4617 /* Strip inner integral conversions that do not change precision or size, or
4618    zero-extend while keeping the same size (for bool-to-char).  */
4619 (simplify
4620   (view_convert (convert@0 @1))
4621   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4622        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4623        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4624        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4625            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4626                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4627    (view_convert @1)))
4629 /* Simplify a view-converted empty or single-element constructor.  */
4630 (simplify
4631   (view_convert CONSTRUCTOR@0)
4632   (with
4633    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4634                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4635    (switch
4636     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4637      { build_zero_cst (type); })
4638     (if (CONSTRUCTOR_NELTS (ctor) == 1
4639          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4640          && operand_equal_p (TYPE_SIZE (type),
4641                              TYPE_SIZE (TREE_TYPE
4642                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4643      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4645 /* Re-association barriers around constants and other re-association
4646    barriers can be removed.  */
4647 (simplify
4648  (paren CONSTANT_CLASS_P@0)
4649  @0)
4650 (simplify
4651  (paren (paren@1 @0))
4652  @1)
4654 /* Handle cases of two conversions in a row.  */
4655 (for ocvt (convert float fix_trunc)
4656  (for icvt (convert float)
4657   (simplify
4658    (ocvt (icvt@1 @0))
4659    (with
4660     {
4661       tree inside_type = TREE_TYPE (@0);
4662       tree inter_type = TREE_TYPE (@1);
4663       int inside_int = INTEGRAL_TYPE_P (inside_type);
4664       int inside_ptr = POINTER_TYPE_P (inside_type);
4665       int inside_float = FLOAT_TYPE_P (inside_type);
4666       int inside_vec = VECTOR_TYPE_P (inside_type);
4667       unsigned int inside_prec = element_precision (inside_type);
4668       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4669       int inter_int = INTEGRAL_TYPE_P (inter_type);
4670       int inter_ptr = POINTER_TYPE_P (inter_type);
4671       int inter_float = FLOAT_TYPE_P (inter_type);
4672       int inter_vec = VECTOR_TYPE_P (inter_type);
4673       unsigned int inter_prec = element_precision (inter_type);
4674       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4675       int final_int = INTEGRAL_TYPE_P (type);
4676       int final_ptr = POINTER_TYPE_P (type);
4677       int final_float = FLOAT_TYPE_P (type);
4678       int final_vec = VECTOR_TYPE_P (type);
4679       unsigned int final_prec = element_precision (type);
4680       int final_unsignedp = TYPE_UNSIGNED (type);
4681     }
4682    (switch
4683     /* In addition to the cases of two conversions in a row
4684        handled below, if we are converting something to its own
4685        type via an object of identical or wider precision, neither
4686        conversion is needed.  */
4687     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4688           || (GENERIC
4689               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4690          && (((inter_int || inter_ptr) && final_int)
4691              || (inter_float && final_float))
4692          && inter_prec >= final_prec)
4693      (ocvt @0))
4695     /* Likewise, if the intermediate and initial types are either both
4696        float or both integer, we don't need the middle conversion if the
4697        former is wider than the latter and doesn't change the signedness
4698        (for integers).  Avoid this if the final type is a pointer since
4699        then we sometimes need the middle conversion.  */
4700     (if (((inter_int && inside_int) || (inter_float && inside_float))
4701          && (final_int || final_float)
4702          && inter_prec >= inside_prec
4703          && (inter_float || inter_unsignedp == inside_unsignedp))
4704      (ocvt @0))
4706     /* If we have a sign-extension of a zero-extended value, we can
4707        replace that by a single zero-extension.  Likewise if the
4708        final conversion does not change precision we can drop the
4709        intermediate conversion.  */
4710     (if (inside_int && inter_int && final_int
4711          && ((inside_prec < inter_prec && inter_prec < final_prec
4712               && inside_unsignedp && !inter_unsignedp)
4713              || final_prec == inter_prec))
4714      (ocvt @0))
4716     /* Two conversions in a row are not needed unless:
4717         - some conversion is floating-point (overstrict for now), or
4718         - some conversion is a vector (overstrict for now), or
4719         - the intermediate type is narrower than both initial and
4720           final, or
4721         - the intermediate type and innermost type differ in signedness,
4722           and the outermost type is wider than the intermediate, or
4723         - the initial type is a pointer type and the precisions of the
4724           intermediate and final types differ, or
4725         - the final type is a pointer type and the precisions of the
4726           initial and intermediate types differ.  */
4727     (if (! inside_float && ! inter_float && ! final_float
4728          && ! inside_vec && ! inter_vec && ! final_vec
4729          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4730          && ! (inside_int && inter_int
4731                && inter_unsignedp != inside_unsignedp
4732                && inter_prec < final_prec)
4733          && ((inter_unsignedp && inter_prec > inside_prec)
4734              == (final_unsignedp && final_prec > inter_prec))
4735          && ! (inside_ptr && inter_prec != final_prec)
4736          && ! (final_ptr && inside_prec != inter_prec))
4737      (ocvt @0))
4739    /* `(outer:M)(inter:N) a:O`
4740       can be converted to `(outer:M) a`
4741       if M <= O && N >= O. No matter what signedness of the casts,
4742       as the final is either a truncation from the original or just
4743       a sign change of the type. */
4744    (if (inside_int && inter_int && final_int
4745         && final_prec <= inside_prec
4746         && inter_prec >= inside_prec)
4747     (convert @0))
4749     /* A truncation to an unsigned type (a zero-extension) should be
4750        canonicalized as bitwise and of a mask.  */
4751     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4752          && final_int && inter_int && inside_int
4753          && final_prec == inside_prec
4754          && final_prec > inter_prec
4755          && inter_unsignedp)
4756      (convert (bit_and @0 { wide_int_to_tree
4757                               (inside_type,
4758                                wi::mask (inter_prec, false,
4759                                          TYPE_PRECISION (inside_type))); })))
4761     /* If we are converting an integer to a floating-point that can
4762        represent it exactly and back to an integer, we can skip the
4763        floating-point conversion.  */
4764     (if (GIMPLE /* PR66211 */
4765          && inside_int && inter_float && final_int &&
4766          (unsigned) significand_size (TYPE_MODE (inter_type))
4767          >= inside_prec - !inside_unsignedp)
4768      (convert @0)))))))
4770 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4771    float_type.  Only do the transformation if we do not need to preserve
4772    trapping behaviour, so require !flag_trapping_math. */
4773 #if GIMPLE
4774 (simplify
4775    (float (fix_trunc @0))
4776    (if (!flag_trapping_math
4777         && types_match (type, TREE_TYPE (@0))
4778         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4779                                           OPTIMIZE_FOR_BOTH))
4780       (IFN_TRUNC @0)))
4781 #endif
4783 /* If we have a narrowing conversion to an integral type that is fed by a
4784    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4785    masks off bits outside the final type (and nothing else).  */
4786 (simplify
4787   (convert (bit_and @0 INTEGER_CST@1))
4788   (if (INTEGRAL_TYPE_P (type)
4789        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4790        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4791        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4792                                                     TYPE_PRECISION (type)), 0))
4793    (convert @0)))
4796 /* (X /[ex] A) * A -> X.  */
4797 (simplify
4798   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4799   (convert @0))
4801 /* Simplify (A / B) * B + (A % B) -> A.  */
4802 (for div (trunc_div ceil_div floor_div round_div)
4803      mod (trunc_mod ceil_mod floor_mod round_mod)
4804   (simplify
4805    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4806    @0))
4808 /* x / y * y == x -> x % y == 0.  */
4809 (simplify
4810   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4811   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4812     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4814 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4815 (for op (plus minus)
4816  (simplify
4817   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4818   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4819        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4820    (with
4821      {
4822        wi::overflow_type overflow;
4823        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4824                                TYPE_SIGN (type), &overflow);
4825      }
4826      (if (types_match (type, TREE_TYPE (@2))
4827          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4828       (op @0 { wide_int_to_tree (type, mul); })
4829       (with { tree utype = unsigned_type_for (type); }
4830        (convert (op (convert:utype @0)
4831                     (mult (convert:utype @1) (convert:utype @2))))))))))
4833 /* Canonicalization of binary operations.  */
4835 /* Convert X + -C into X - C.  */
4836 (simplify
4837  (plus @0 REAL_CST@1)
4838  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4839   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4840    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4841     (minus @0 { tem; })))))
4843 /* Convert x+x into x*2.  */
4844 (simplify
4845  (plus @0 @0)
4846  (if (SCALAR_FLOAT_TYPE_P (type))
4847   (mult @0 { build_real (type, dconst2); })
4848   (if (INTEGRAL_TYPE_P (type))
4849    (mult @0 { build_int_cst (type, 2); }))))
4851 /* 0 - X  ->  -X.  */
4852 (simplify
4853  (minus integer_zerop @1)
4854  (negate @1))
4855 (simplify
4856  (pointer_diff integer_zerop @1)
4857  (negate (convert @1)))
4859 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4860    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4861    (-ARG1 + ARG0) reduces to -ARG1.  */
4862 (simplify
4863  (minus real_zerop@0 @1)
4864  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4865   (negate @1)))
4867 /* Transform x * -1 into -x.  */
4868 (simplify
4869  (mult @0 integer_minus_onep)
4870  (negate @0))
4872 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4873    signed overflow for CST != 0 && CST != -1.  */
4874 (simplify
4875  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4876  (if (TREE_CODE (@2) != INTEGER_CST
4877       && single_use (@3)
4878       && !integer_zerop (@1) && !integer_minus_onep (@1))
4879   (mult (mult @0 @2) @1)))
4881 /* True if we can easily extract the real and imaginary parts of a complex
4882    number.  */
4883 (match compositional_complex
4884  (convert? (complex @0 @1)))
4886 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4887 (simplify
4888  (complex (realpart @0) (imagpart @0))
4889  @0)
4890 (simplify
4891  (realpart (complex @0 @1))
4892  @0)
4893 (simplify
4894  (imagpart (complex @0 @1))
4895  @1)
4897 /* Sometimes we only care about half of a complex expression.  */
4898 (simplify
4899  (realpart (convert?:s (conj:s @0)))
4900  (convert (realpart @0)))
4901 (simplify
4902  (imagpart (convert?:s (conj:s @0)))
4903  (convert (negate (imagpart @0))))
4904 (for part (realpart imagpart)
4905  (for op (plus minus)
4906   (simplify
4907    (part (convert?:s@2 (op:s @0 @1)))
4908    (convert (op (part @0) (part @1))))))
4909 (simplify
4910  (realpart (convert?:s (CEXPI:s @0)))
4911  (convert (COS @0)))
4912 (simplify
4913  (imagpart (convert?:s (CEXPI:s @0)))
4914  (convert (SIN @0)))
4916 /* conj(conj(x)) -> x  */
4917 (simplify
4918  (conj (convert? (conj @0)))
4919  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4920   (convert @0)))
4922 /* conj({x,y}) -> {x,-y}  */
4923 (simplify
4924  (conj (convert?:s (complex:s @0 @1)))
4925  (with { tree itype = TREE_TYPE (type); }
4926   (complex (convert:itype @0) (negate (convert:itype @1)))))
4928 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4929 (for bswap (BSWAP)
4930  (simplify
4931   (bswap (bswap @0))
4932   @0)
4933  (simplify
4934   (bswap (bit_not (bswap @0)))
4935   (bit_not @0))
4936  (for bitop (bit_xor bit_ior bit_and)
4937   (simplify
4938    (bswap (bitop:c (bswap @0) @1))
4939    (bitop @0 (bswap @1))))
4940  (for cmp (eq ne)
4941   (simplify
4942    (cmp (bswap@2 @0) (bswap @1))
4943    (with { tree ctype = TREE_TYPE (@2); }
4944     (cmp (convert:ctype @0) (convert:ctype @1))))
4945   (simplify
4946    (cmp (bswap @0) INTEGER_CST@1)
4947    (with { tree ctype = TREE_TYPE (@1); }
4948     (cmp (convert:ctype @0) (bswap! @1)))))
4949  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4950  (simplify
4951   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4952            INTEGER_CST@3)
4953    (if (BITS_PER_UNIT == 8
4954         && tree_fits_uhwi_p (@2)
4955         && tree_fits_uhwi_p (@3))
4956     (with
4957      {
4958       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4959       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4960       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4961       unsigned HOST_WIDE_INT lo = bits & 7;
4962       unsigned HOST_WIDE_INT hi = bits - lo;
4963      }
4964      (if (bits < prec
4965           && mask < (256u>>lo)
4966           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4967       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4968        (if (ns == 0)
4969         (bit_and (convert @1) @3)
4970         (with
4971          {
4972           tree utype = unsigned_type_for (TREE_TYPE (@1));
4973           tree nst = build_int_cst (integer_type_node, ns);
4974          }
4975          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4976  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4977  (simplify
4978   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4979    (if (BITS_PER_UNIT == 8
4980         && CHAR_TYPE_SIZE == 8
4981         && tree_fits_uhwi_p (@1))
4982     (with
4983      {
4984       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4985       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4986       /* If the bswap was extended before the original shift, this
4987          byte (shift) has the sign of the extension, not the sign of
4988          the original shift.  */
4989       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4990      }
4991      /* Special case: logical right shift of sign-extended bswap.
4992         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4993      (if (TYPE_PRECISION (type) > prec
4994           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4995           && TYPE_UNSIGNED (type)
4996           && bits < prec && bits + 8 >= prec)
4997       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4998        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4999       (if (bits + 8 == prec)
5000        (if (TYPE_UNSIGNED (st))
5001         (convert (convert:unsigned_char_type_node @0))
5002         (convert (convert:signed_char_type_node @0)))
5003        (if (bits < prec && bits + 8 > prec)
5004         (with 
5005          {
5006           tree nst = build_int_cst (integer_type_node, bits & 7);
5007           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5008                                        : signed_char_type_node;
5009          }
5010          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5011  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5012  (simplify
5013   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5014    (if (BITS_PER_UNIT == 8
5015         && tree_fits_uhwi_p (@1)
5016         && tree_to_uhwi (@1) < 256)
5017     (with
5018      {
5019       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5020       tree utype = unsigned_type_for (TREE_TYPE (@0));
5021       tree nst = build_int_cst (integer_type_node, prec - 8);
5022      }
5023      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5026 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5028 /* Simplify constant conditions.
5029    Only optimize constant conditions when the selected branch
5030    has the same type as the COND_EXPR.  This avoids optimizing
5031    away "c ? x : throw", where the throw has a void type.
5032    Note that we cannot throw away the fold-const.cc variant nor
5033    this one as we depend on doing this transform before possibly
5034    A ? B : B -> B triggers and the fold-const.cc one can optimize
5035    0 ? A : B to B even if A has side-effects.  Something
5036    genmatch cannot handle.  */
5037 (simplify
5038  (cond INTEGER_CST@0 @1 @2)
5039  (if (integer_zerop (@0))
5040   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5041    @2)
5042   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5043    @1)))
5044 (simplify
5045  (vec_cond VECTOR_CST@0 @1 @2)
5046  (if (integer_all_onesp (@0))
5047   @1
5048   (if (integer_zerop (@0))
5049    @2)))
5051 /* Sink unary operations to branches, but only if we do fold both.  */
5052 (for op (negate bit_not abs absu)
5053  (simplify
5054   (op (vec_cond:s @0 @1 @2))
5055   (vec_cond @0 (op! @1) (op! @2))))
5057 /* Sink unary conversions to branches, but only if we do fold both
5058    and the target's truth type is the same as we already have.  */
5059 (simplify
5060  (convert (vec_cond:s @0 @1 @2))
5061  (if (VECTOR_TYPE_P (type)
5062       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5063   (vec_cond @0 (convert! @1) (convert! @2))))
5065 /* Likewise for view_convert of nop_conversions. */
5066 (simplify
5067  (view_convert (vec_cond:s @0 @1 @2))
5068  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5069       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5070                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5071       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5072   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5074 /* Sink binary operation to branches, but only if we can fold it.  */
5075 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5076          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5077          trunc_mod ceil_mod floor_mod round_mod min max)
5078 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5079  (simplify
5080   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5081   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5083 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5084  (simplify
5085   (op (vec_cond:s @0 @1 @2) @3)
5086   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5087  (simplify
5088   (op @3 (vec_cond:s @0 @1 @2))
5089   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5091 #if GIMPLE
5092 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5093  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5094            INTEGER_CST@1)
5095  (with {
5096          int ibit = tree_log2 (@0);
5097          int ibit2 = tree_log2 (@1);
5098        }
5099   (if (ibit == ibit2
5100       && ibit >= 0
5101       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5103 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5104  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5105           INTEGER_CST@1)
5106  (with {
5107          int ibit = tree_log2 (@0);
5108          int ibit2 = tree_log2 (@1);
5109        }
5110   (if (ibit == ibit2
5111       && ibit >= 0
5112       && 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    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5118   (convert2? @0))
5119  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5121 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5122  (bit_and:c
5123   (convert1?@4
5124    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5125   (convert2? @0))
5126  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5128 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5129  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5130             INTEGER_CST@1)
5131  (with {
5132          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5133                                               TYPE_PRECISION(type)));
5134          int ibit2 = tree_log2 (@1);
5135        }
5136   (if (ibit == ibit2
5137       && ibit >= 0
5138       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5140 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5141  (bit_and@4
5142   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5143   INTEGER_CST@1)
5144  (with {
5145          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5146                                               TYPE_PRECISION(type)));
5147          int ibit2 = tree_log2 (@1);
5148        }
5149   (if (ibit == ibit2
5150       && ibit >= 0
5151       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5153 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5154  (bit_and:c
5155   (convert1?@3
5156    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5157   (convert2? @0))
5158  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5160 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5161  (bit_and:c
5162   (convert1?@3
5163    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5164   (convert2? @0))
5165   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5167 #endif
5169 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5170    Currently disabled after pass lvec because ARM understands
5171    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5172 #if GIMPLE
5173 /* These can only be done in gimple as fold likes to convert:
5174    (CMP) & N into (CMP) ? N : 0
5175    and we try to match the same pattern again and again. */
5176 (simplify
5177  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5178  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5179   (vec_cond (bit_and @0 @3) @1 @2)))
5180 (simplify
5181  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5182  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5183   (vec_cond (bit_ior @0 @3) @1 @2)))
5184 (simplify
5185  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5186  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5187   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5188 (simplify
5189  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5190  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5191   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5193 /*  ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5194     (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5195     component type of the outer vec_cond is greater equal the inner one.  */
5196 (for cmp (simple_comparison)
5197  (simplify
5198   (vec_cond
5199     (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5200                                     integer_all_onesp
5201                                     integer_zerop))
5202           integer_zerop) @2 @3)
5203   (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5204        && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5205        && !TYPE_UNSIGNED (TREE_TYPE (@5))
5206        && VECTOR_TYPE_P (TREE_TYPE (@6))
5207        && VECTOR_TYPE_P (type)
5208        && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5209                            TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5210        && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5211    (with { tree vtype = TREE_TYPE (@6);}
5212      (view_convert:type
5213        (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5215 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5216 (simplify
5217  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5218  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5219   (vec_cond (bit_and @0 @1) @2 @3)))
5220 (simplify
5221  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5222  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5223   (vec_cond (bit_ior @0 @1) @2 @3)))
5224 (simplify
5225  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5226  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5227   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5228 (simplify
5229  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5230  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5231   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5232 #endif
5234 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5235    types are compatible.  */
5236 (simplify
5237  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5238  (if (VECTOR_BOOLEAN_TYPE_P (type)
5239       && types_match (type, TREE_TYPE (@0)))
5240   (if (integer_zerop (@1) && integer_all_onesp (@2))
5241    (bit_not @0)
5242    (if (integer_all_onesp (@1) && integer_zerop (@2))
5243     @0))))
5245 /* A few simplifications of "a ? CST1 : CST2". */
5246 /* NOTE: Only do this on gimple as the if-chain-to-switch
5247    optimization depends on the gimple to have if statements in it. */
5248 #if GIMPLE
5249 (simplify
5250  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5251  (switch
5252   (if (integer_zerop (@2))
5253    (switch
5254     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5255     (if (integer_onep (@1))
5256      (convert (convert:boolean_type_node @0)))
5257     /* a ? -1 : 0 -> -a.  */
5258     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5259      (if (TYPE_PRECISION (type) == 1)
5260       /* For signed 1-bit precision just cast bool to the type.  */
5261       (convert (convert:boolean_type_node @0))
5262       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5263        (with {
5264           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5265                                                       TYPE_UNSIGNED (type));
5266         }
5267         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5268        (negate (convert:type (convert:boolean_type_node @0))))))
5269     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5270     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5271      (with {
5272        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5273       }
5274       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5275   (if (integer_zerop (@1))
5276    (switch
5277     /* a ? 0 : 1 -> !a.  */
5278     (if (integer_onep (@2))
5279      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5280     /* a ? 0 : -1 -> -(!a).  */
5281     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5282      (if (TYPE_PRECISION (type) == 1)
5283       /* For signed 1-bit precision just cast bool to the type.  */
5284       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5285       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5286        (with {
5287           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5288                                                       TYPE_UNSIGNED (type));
5289         }
5290         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5291                                                 { boolean_true_node; })))))
5292        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5293                                       { boolean_true_node; }))))))
5294     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5295     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5296      (with {
5297        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5298       }
5299       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5300                                 { boolean_true_node; })) { shift; })))))))
5302 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5303    for unsigned types. */
5304 (simplify
5305  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5306  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5307       && bitwise_equal_p (@0, @2))
5308   (convert (eq @0 @1))
5312 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5313    for unsigned types. */
5314 (simplify
5315  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5316  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5317       && bitwise_equal_p (@0, @2))
5318   (convert (eq @0 @1))
5322 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5323    on the first bit of the CST.  */
5324 (simplify
5325  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5326  (if ((wi::to_wide (@1) & 1) != 0)
5327   @2
5328   { build_zero_cst (type); }))
5330 /* Optimize
5331    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5332    x_5 == cstN ? cst4 : cst3
5333    # op is == or != and N is 1 or 2
5334    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5335    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5336    of cst3 and cst4 is smaller.
5337    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5338 (for eqne (ne eq)
5339  (simplify
5340   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5341   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5342        && INTEGRAL_TYPE_P (type)
5343        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5344            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5345    (with {
5346      value_range r;
5347      get_range_query (cfun)->range_of_expr (r, @0);
5348      if (r.undefined_p ())
5349        r.set_varying (TREE_TYPE (@0));
5351      wide_int min = r.lower_bound ();
5352      wide_int max = r.upper_bound ();
5353     }
5354     (if (min + 1 == max
5355          && (wi::to_wide (@1) == min
5356              || wi::to_wide (@1) == max))
5357      (with {
5358        tree arg0 = @2, arg1 = @3;
5359        tree type1;
5360        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5361          std::swap (arg0, arg1);
5362        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5363          type1 = TREE_TYPE (@0);
5364        else
5365          type1 = type;
5366        auto prec = TYPE_PRECISION (type1);
5367        auto unsign = TYPE_UNSIGNED (type1);
5368        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5369         type1 = build_nonstandard_integer_type (prec, unsign);
5370        min = wide_int::from (min, prec,
5371                              TYPE_SIGN (TREE_TYPE (@0)));
5372        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5373                                     TYPE_SIGN (type));
5374        enum tree_code code;
5375        wi::overflow_type ovf;
5376        if (tree_int_cst_lt (arg0, arg1))
5377          {
5378            code = PLUS_EXPR;
5379            a -= min;
5380            if (!unsign)
5381              {
5382                /* lhs is known to be in range [min, min+1] and we want to add a
5383                   to it.  Check if that operation can overflow for those 2 values
5384                   and if yes, force unsigned type.  */
5385                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5386                if (ovf)
5387                  type1 = unsigned_type_for (type1);
5388              }
5389          }
5390        else
5391          {
5392            code = MINUS_EXPR;
5393            a += min;
5394            if (!unsign)
5395              {
5396                /* lhs is known to be in range [min, min+1] and we want to subtract
5397                   it from a.  Check if that operation can overflow for those 2
5398                   values and if yes, force unsigned type.  */
5399                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5400                if (ovf)
5401                 type1 = unsigned_type_for (type1);
5402              }
5403          }
5404        tree arg = wide_int_to_tree (type1, a);
5405       }
5406       (if (code == PLUS_EXPR)
5407        (convert (plus (convert:type1 @0) { arg; }))
5408        (convert (minus { arg; } (convert:type1 @0))))))))))
5409 #endif
5411 (simplify
5412  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5413  (if (INTEGRAL_TYPE_P (type)
5414       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5415   (cond @1 (convert @2) (convert @3))))
5417 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5418    be extended.  */
5419 /* This pattern implements two kinds simplification:
5421    Case 1)
5422    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5423      1) Conversions are type widening from smaller type.
5424      2) Const c1 equals to c2 after canonicalizing comparison.
5425      3) Comparison has tree code LT, LE, GT or GE.
5426    This specific pattern is needed when (cmp (convert x) c) may not
5427    be simplified by comparison patterns because of multiple uses of
5428    x.  It also makes sense here because simplifying across multiple
5429    referred var is always benefitial for complicated cases.
5431    Case 2)
5432    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5433 (for cmp (lt le gt ge eq ne)
5434  (simplify
5435   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5436   (with
5437    {
5438      tree from_type = TREE_TYPE (@1);
5439      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5440      enum tree_code code = ERROR_MARK;
5442      if (INTEGRAL_TYPE_P (from_type)
5443          && int_fits_type_p (@2, from_type)
5444          && (types_match (c1_type, from_type)
5445              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5446                  && (TYPE_UNSIGNED (from_type)
5447                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5448          && (types_match (c2_type, from_type)
5449              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5450                  && (TYPE_UNSIGNED (from_type)
5451                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5452        {
5453          if (cmp != EQ_EXPR)
5454            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5455          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5456          else if (int_fits_type_p (@3, from_type))
5457            code = EQ_EXPR;
5458        }
5459    }
5460    (if (code == MAX_EXPR)
5461     (convert (max @1 (convert @2)))
5462     (if (code == MIN_EXPR)
5463      (convert (min @1 (convert @2)))
5464      (if (code == EQ_EXPR)
5465       (convert (cond (eq @1 (convert @3))
5466                      (convert:from_type @3) (convert:from_type @2)))))))))
5468 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5470      1) OP is PLUS or MINUS.
5471      2) CMP is LT, LE, GT or GE.
5472      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5474    This pattern also handles special cases like:
5476      A) Operand x is a unsigned to signed type conversion and c1 is
5477         integer zero.  In this case,
5478           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5479           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5480      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5481         check equality for (c1+1) and (c1-1) by adjusting comparison
5482         code.
5484    TODO: Though signed type is handled by this pattern, it cannot be
5485    simplified at the moment because C standard requires additional
5486    type promotion.  In order to match&simplify it here, the IR needs
5487    to be cleaned up by other optimizers, i.e, VRP.  */
5488 (for op (plus minus)
5489  (for cmp (lt le gt ge)
5490   (simplify
5491    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5492    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5493     (if (types_match (from_type, to_type)
5494          /* Check if it is special case A).  */
5495          || (TYPE_UNSIGNED (from_type)
5496              && !TYPE_UNSIGNED (to_type)
5497              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5498              && integer_zerop (@1)
5499              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5500      (with
5501       {
5502         wi::overflow_type overflow = wi::OVF_NONE;
5503         enum tree_code code, cmp_code = cmp;
5504         wide_int real_c1;
5505         wide_int c1 = wi::to_wide (@1);
5506         wide_int c2 = wi::to_wide (@2);
5507         wide_int c3 = wi::to_wide (@3);
5508         signop sgn = TYPE_SIGN (from_type);
5510         /* Handle special case A), given x of unsigned type:
5511             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5512             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5513         if (!types_match (from_type, to_type))
5514           {
5515             if (cmp_code == LT_EXPR)
5516               cmp_code = GT_EXPR;
5517             if (cmp_code == GE_EXPR)
5518               cmp_code = LE_EXPR;
5519             c1 = wi::max_value (to_type);
5520           }
5521         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5522            compute (c3 op' c2) and check if it equals to c1 with op' being
5523            the inverted operator of op.  Make sure overflow doesn't happen
5524            if it is undefined.  */
5525         if (op == PLUS_EXPR)
5526           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5527         else
5528           real_c1 = wi::add (c3, c2, sgn, &overflow);
5530         code = cmp_code;
5531         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5532           {
5533             /* Check if c1 equals to real_c1.  Boundary condition is handled
5534                by adjusting comparison operation if necessary.  */
5535             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5536                 && !overflow)
5537               {
5538                 /* X <= Y - 1 equals to X < Y.  */
5539                 if (cmp_code == LE_EXPR)
5540                   code = LT_EXPR;
5541                 /* X > Y - 1 equals to X >= Y.  */
5542                 if (cmp_code == GT_EXPR)
5543                   code = GE_EXPR;
5544               }
5545             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5546                 && !overflow)
5547               {
5548                 /* X < Y + 1 equals to X <= Y.  */
5549                 if (cmp_code == LT_EXPR)
5550                   code = LE_EXPR;
5551                 /* X >= Y + 1 equals to X > Y.  */
5552                 if (cmp_code == GE_EXPR)
5553                   code = GT_EXPR;
5554               }
5555             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5556               {
5557                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5558                   code = MIN_EXPR;
5559                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5560                   code = MAX_EXPR;
5561               }
5562           }
5563       }
5564       (if (code == MAX_EXPR)
5565        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5566            { wide_int_to_tree (from_type, c2); })
5567        (if (code == MIN_EXPR)
5568         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5569             { wide_int_to_tree (from_type, c2); })))))))))
5571 #if GIMPLE
5572 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5573    in fold_cond_expr_with_comparison for GENERIC folding with
5574    some extra constraints.  */
5575 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5576  (simplify
5577   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5578         (convert3? @0) (convert4? @1))
5579   (if (!HONOR_SIGNED_ZEROS (type)
5580        && (/* Allow widening conversions of the compare operands as data.  */
5581            (INTEGRAL_TYPE_P (type)
5582             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5583             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5584             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5585             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5586            /* Or sign conversions for the comparison.  */
5587            || (types_match (type, TREE_TYPE (@0))
5588                && types_match (type, TREE_TYPE (@1)))))
5589    (switch
5590     (if (cmp == EQ_EXPR)
5591      (if (VECTOR_TYPE_P (type))
5592       (view_convert @c1)
5593       (convert @c1)))
5594     (if (cmp == NE_EXPR)
5595      (if (VECTOR_TYPE_P (type))
5596       (view_convert @c0)
5597       (convert @c0)))
5598     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5599      (if (!HONOR_NANS (type))
5600       (if (VECTOR_TYPE_P (type))
5601        (view_convert (min @c0 @c1))
5602        (convert (min @c0 @c1)))))
5603     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5604      (if (!HONOR_NANS (type))
5605       (if (VECTOR_TYPE_P (type))
5606        (view_convert (max @c0 @c1))
5607        (convert (max @c0 @c1)))))
5608     (if (cmp == UNEQ_EXPR)
5609      (if (!HONOR_NANS (type))
5610       (if (VECTOR_TYPE_P (type))
5611        (view_convert @c1)
5612        (convert @c1))))
5613     (if (cmp == LTGT_EXPR)
5614      (if (!HONOR_NANS (type))
5615       (if (VECTOR_TYPE_P (type))
5616        (view_convert @c0)
5617        (convert @c0))))))))
5618 #endif
5620 (for cnd (cond vec_cond)
5621  /* (a != b) ? (a - b) : 0 -> (a - b) */
5622  (simplify
5623   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5624   @2)
5625  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5626  (simplify
5627   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5628   @2)
5629  /* (a != b) ? (a & b) : a -> (a & b) */
5630  /* (a != b) ? (a | b) : a -> (a | b) */
5631  /* (a != b) ? min(a,b) : a -> min(a,b) */
5632  /* (a != b) ? max(a,b) : a -> max(a,b) */
5633  (for op (bit_and bit_ior min max)
5634   (simplify
5635    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5636    @2))
5637  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5638  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5639  (for op (mult plus)
5640   (simplify
5641    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5642    (if (ANY_INTEGRAL_TYPE_P (type))
5643     @2)))
5644  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5645  (simplify
5646   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5647   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5648    @2))
5651 /* These was part of minmax phiopt.  */
5652 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5653    to minmax<min/max<a, b>, c> */
5654 (for minmax (min max)
5655  (for cmp (lt le gt ge ne)
5656   (simplify
5657    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5658    (with
5659     {
5660       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5661     }
5662     (if (code == MIN_EXPR)
5663      (minmax (min @1 @2) @4)
5664      (if (code == MAX_EXPR)
5665       (minmax (max @1 @2) @4)))))))
5667 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5668 (for cmp    (gt  ge  lt  le)
5669      minmax (min min max max)
5670  (simplify
5671   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5672    (with
5673     {
5674       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5675     }
5676     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5677          && code == MIN_EXPR
5678          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5679      (min @2 @4)
5680      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5681           && code == MAX_EXPR
5682           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5683       (max @2 @4))))))
5685 #if GIMPLE
5686 /* These patterns should be after min/max detection as simplifications
5687    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5688    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5689    Even without those, reaching min/max/and/ior faster is better.  */
5690 (simplify
5691  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5692  (switch
5693   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5694   (if (integer_zerop (@2))
5695    (bit_and (convert @0) @1))
5696   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5697   (if (integer_zerop (@1))
5698    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5699   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5700   (if (integer_onep (@1))
5701    (bit_ior (convert @0) @2))
5702   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5703   (if (integer_onep (@2))
5704    (bit_ior (bit_xor (convert @0) @2) @1))
5707 #endif
5709 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5710 (simplify
5711  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5712  (if (!TYPE_SATURATING (type)
5713       && (TYPE_OVERFLOW_WRAPS (type)
5714           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5715       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5716   @3))
5718 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5719 (simplify
5720  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5721  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5722   @3))
5724 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5725 (for op (abs absu)
5726  (simplify
5727   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5728   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5729    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5730     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5731      (convert (absu:utype @0)))
5732     @3))))
5734 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5735    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5736 (simplify
5737  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5738  (if (TYPE_UNSIGNED (type))
5739   (cond (ge @0 @1) (negate @0) @2)))
5741 (for cnd (cond vec_cond)
5742  /* A ? B : (A ? X : C) -> A ? B : C.  */
5743  (simplify
5744   (cnd @0 (cnd @0 @1 @2) @3)
5745   (cnd @0 @1 @3))
5746  (simplify
5747   (cnd @0 @1 (cnd @0 @2 @3))
5748   (cnd @0 @1 @3))
5749  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5750  /* ???  This matches embedded conditions open-coded because genmatch
5751     would generate matching code for conditions in separate stmts only.
5752     The following is still important to merge then and else arm cases
5753     from if-conversion.  */
5754  (simplify
5755   (cnd @0 @1 (cnd @2 @3 @4))
5756   (if (inverse_conditions_p (@0, @2))
5757    (cnd @0 @1 @3)))
5758  (simplify
5759   (cnd @0 (cnd @1 @2 @3) @4)
5760   (if (inverse_conditions_p (@0, @1))
5761    (cnd @0 @3 @4)))
5763  /* A ? B : B -> B.  */
5764  (simplify
5765   (cnd @0 @1 @1)
5766   @1)
5768  /* !A ? B : C -> A ? C : B.  */
5769  (simplify
5770   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5771   (cnd @0 @2 @1)))
5773 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5775    None of these transformations work for modes with signed
5776    zeros.  If A is +/-0, the first two transformations will
5777    change the sign of the result (from +0 to -0, or vice
5778    versa).  The last four will fix the sign of the result,
5779    even though the original expressions could be positive or
5780    negative, depending on the sign of A.
5782    Note that all these transformations are correct if A is
5783    NaN, since the two alternatives (A and -A) are also NaNs.  */
5785 (for cnd (cond vec_cond)
5786  /* A == 0 ? A : -A    same as -A */
5787  (for cmp (eq uneq)
5788   (simplify
5789    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5790     (if (!HONOR_SIGNED_ZEROS (type)
5791          && bitwise_equal_p (@0, @2))
5792      @1))
5793   (simplify
5794    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5795     (if (!HONOR_SIGNED_ZEROS (type)
5796          && bitwise_equal_p (@0, @2))
5797      @1))
5799  /* A != 0 ? A : -A    same as A */
5800  (for cmp (ne ltgt)
5801   (simplify
5802    (cnd (cmp @0 zerop) @1 (negate @1))
5803     (if (!HONOR_SIGNED_ZEROS (type)
5804          && bitwise_equal_p (@0, @1))
5805      @1))
5806   (simplify
5807    (cnd (cmp @0 zerop) @1 integer_zerop)
5808     (if (!HONOR_SIGNED_ZEROS (type)
5809          && bitwise_equal_p (@0, @1))
5810      @1))
5812  /* A >=/> 0 ? A : -A    same as abs (A) */
5813  (for cmp (ge gt)
5814   (simplify
5815    (cnd (cmp @0 zerop) @1 (negate @1))
5816     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5817          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5818          && bitwise_equal_p (@0, @1))
5819      (if (TYPE_UNSIGNED (type))
5820       (absu:type @0)
5821       (abs @0)))))
5822  /* A <=/< 0 ? A : -A    same as -abs (A) */
5823  (for cmp (le lt)
5824   (simplify
5825    (cnd (cmp @0 zerop) @1 (negate @1))
5826     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5827          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5828          && bitwise_equal_p (@0, @1))
5829      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5830            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5831           || TYPE_UNSIGNED (type))
5832       (with {
5833         tree utype = unsigned_type_for (TREE_TYPE(@0));
5834        }
5835        (convert (negate (absu:utype @0))))
5836        (negate (abs @0)))))
5839  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5840  (for cmp (eq uneq)
5841   (simplify
5842    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5843    (if (!HONOR_SIGNED_ZEROS (type))
5844     @3))
5845   (simplify
5846    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5847    @3)
5849  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5850  (for cmp (ne ltgt)
5851   (simplify
5852    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5853    (if (!HONOR_SIGNED_ZEROS (type))
5854     @0))
5855   (simplify
5856    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5857    @0)
5859  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5860  (for cmp (ge gt)
5861   (simplify
5862    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5863    (if (!HONOR_SIGNED_ZEROS (type)
5864         && !TYPE_UNSIGNED (type))
5865     (abs @0))))
5866  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5867  (for cmp (le lt)
5868   (simplify
5869    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5870    (if (!HONOR_SIGNED_ZEROS (type)
5871         && !TYPE_UNSIGNED (type))
5872     (if (ANY_INTEGRAL_TYPE_P (type)
5873          && !TYPE_OVERFLOW_WRAPS (type))
5874      (with {
5875         tree utype = unsigned_type_for (type);
5876       }
5877       (convert (negate (absu:utype @0))))
5878       (negate (abs @0)))))
5882 /* -(type)!A -> (type)A - 1.  */
5883 (simplify
5884  (negate (convert?:s (logical_inverted_value:s @0)))
5885  (if (INTEGRAL_TYPE_P (type)
5886       && TREE_CODE (type) != BOOLEAN_TYPE
5887       && TYPE_PRECISION (type) > 1
5888       && TREE_CODE (@0) == SSA_NAME
5889       && ssa_name_has_boolean_range (@0))
5890   (plus (convert:type @0) { build_all_ones_cst (type); })))
5892 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5893    return all -1 or all 0 results.  */
5894 /* ??? We could instead convert all instances of the vec_cond to negate,
5895    but that isn't necessarily a win on its own.  */
5896 (simplify
5897  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5898  (if (VECTOR_TYPE_P (type)
5899       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5900                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5901       && (TYPE_MODE (TREE_TYPE (type))
5902           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5903   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5905 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5906 (simplify
5907  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5908  (if (VECTOR_TYPE_P (type)
5909       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5910                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5911       && (TYPE_MODE (TREE_TYPE (type))
5912           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5913   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5916 /* Simplifications of comparisons.  */
5918 /* See if we can reduce the magnitude of a constant involved in a
5919    comparison by changing the comparison code.  This is a canonicalization
5920    formerly done by maybe_canonicalize_comparison_1.  */
5921 (for cmp  (le gt)
5922      acmp (lt ge)
5923  (simplify
5924   (cmp @0 uniform_integer_cst_p@1)
5925   (with { tree cst = uniform_integer_cst_p (@1); }
5926    (if (tree_int_cst_sgn (cst) == -1)
5927      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5928                                    wide_int_to_tree (TREE_TYPE (cst),
5929                                                      wi::to_wide (cst)
5930                                                      + 1)); })))))
5931 (for cmp  (ge lt)
5932      acmp (gt le)
5933  (simplify
5934   (cmp @0 uniform_integer_cst_p@1)
5935   (with { tree cst = uniform_integer_cst_p (@1); }
5936    (if (tree_int_cst_sgn (cst) == 1)
5937     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5938                                   wide_int_to_tree (TREE_TYPE (cst),
5939                                   wi::to_wide (cst) - 1)); })))))
5941 /* We can simplify a logical negation of a comparison to the
5942    inverted comparison.  As we cannot compute an expression
5943    operator using invert_tree_comparison we have to simulate
5944    that with expression code iteration.  */
5945 (for cmp (tcc_comparison)
5946      icmp (inverted_tcc_comparison)
5947      ncmp (inverted_tcc_comparison_with_nans)
5948  /* Ideally we'd like to combine the following two patterns
5949     and handle some more cases by using
5950       (logical_inverted_value (cmp @0 @1))
5951     here but for that genmatch would need to "inline" that.
5952     For now implement what forward_propagate_comparison did.  */
5953  (simplify
5954   (bit_not (cmp @0 @1))
5955   (if (VECTOR_TYPE_P (type)
5956        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5957    /* Comparison inversion may be impossible for trapping math,
5958       invert_tree_comparison will tell us.  But we can't use
5959       a computed operator in the replacement tree thus we have
5960       to play the trick below.  */
5961    (with { enum tree_code ic = invert_tree_comparison
5962              (cmp, HONOR_NANS (@0)); }
5963     (if (ic == icmp)
5964      (icmp @0 @1)
5965      (if (ic == ncmp)
5966       (ncmp @0 @1))))))
5967  (simplify
5968   (bit_xor (cmp @0 @1) integer_truep)
5969   (with { enum tree_code ic = invert_tree_comparison
5970             (cmp, HONOR_NANS (@0)); }
5971    (if (ic == icmp)
5972     (icmp @0 @1)
5973     (if (ic == ncmp)
5974      (ncmp @0 @1)))))
5975  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5976  (simplify
5977   (ne (cmp@2 @0 @1) integer_zerop)
5978   (if (types_match (type, TREE_TYPE (@2)))
5979    (cmp @0 @1)))
5980  (simplify
5981   (eq (cmp@2 @0 @1) integer_truep)
5982   (if (types_match (type, TREE_TYPE (@2)))
5983    (cmp @0 @1)))
5984  (simplify
5985   (ne (cmp@2 @0 @1) integer_truep)
5986   (if (types_match (type, TREE_TYPE (@2)))
5987    (with { enum tree_code ic = invert_tree_comparison
5988              (cmp, HONOR_NANS (@0)); }
5989     (if (ic == icmp)
5990      (icmp @0 @1)
5991      (if (ic == ncmp)
5992       (ncmp @0 @1))))))
5993  (simplify
5994   (eq (cmp@2 @0 @1) integer_zerop)
5995   (if (types_match (type, TREE_TYPE (@2)))
5996    (with { enum tree_code ic = invert_tree_comparison
5997              (cmp, HONOR_NANS (@0)); }
5998     (if (ic == icmp)
5999      (icmp @0 @1)
6000      (if (ic == ncmp)
6001       (ncmp @0 @1)))))))
6003 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6004    ??? The transformation is valid for the other operators if overflow
6005    is undefined for the type, but performing it here badly interacts
6006    with the transformation in fold_cond_expr_with_comparison which
6007    attempts to synthetize ABS_EXPR.  */
6008 (for cmp (eq ne)
6009  (for sub (minus pointer_diff)
6010   (simplify
6011    (cmp (sub@2 @0 @1) integer_zerop)
6012    (if (single_use (@2))
6013     (cmp @0 @1)))))
6015 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6016    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
6017 (for cmp (lt ge)
6018  (simplify
6019   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6020    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6021         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6022         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6023     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6024 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6025    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
6026 (simplify
6027  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6028   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6029        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6030        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6031    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6033 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6034    signed arithmetic case.  That form is created by the compiler
6035    often enough for folding it to be of value.  One example is in
6036    computing loop trip counts after Operator Strength Reduction.  */
6037 (for cmp (simple_comparison)
6038      scmp (swapped_simple_comparison)
6039  (simplify
6040   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6041   /* Handle unfolded multiplication by zero.  */
6042   (if (integer_zerop (@1))
6043    (cmp @1 @2)
6044    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6045         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6046         && single_use (@3))
6047     /* If @1 is negative we swap the sense of the comparison.  */
6048     (if (tree_int_cst_sgn (@1) < 0)
6049      (scmp @0 @2)
6050      (cmp @0 @2))))))
6052 /* For integral types with undefined overflow fold
6053    x * C1 == C2 into x == C2 / C1 or false.
6054    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6055    Z / 2^n Z.  */
6056 (for cmp (eq ne)
6057  (simplify
6058   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6059   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6060        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6061        && wi::to_wide (@1) != 0)
6062    (with { widest_int quot; }
6063     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6064                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6065      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6066      { constant_boolean_node (cmp == NE_EXPR, type); }))
6067    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6068         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6069         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6070     (cmp @0
6071      {
6072        tree itype = TREE_TYPE (@0);
6073        int p = TYPE_PRECISION (itype);
6074        wide_int m = wi::one (p + 1) << p;
6075        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6076        wide_int i = wide_int::from (wi::mod_inv (a, m),
6077                                     p, TYPE_SIGN (itype));
6078        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6079      })))))
6081 /* Simplify comparison of something with itself.  For IEEE
6082    floating-point, we can only do some of these simplifications.  */
6083 (for cmp (eq ge le)
6084  (simplify
6085   (cmp @0 @0)
6086   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6087        || ! tree_expr_maybe_nan_p (@0))
6088    { constant_boolean_node (true, type); }
6089    (if (cmp != EQ_EXPR
6090         /* With -ftrapping-math conversion to EQ loses an exception.  */
6091         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6092             || ! flag_trapping_math))
6093     (eq @0 @0)))))
6094 (for cmp (ne gt lt)
6095  (simplify
6096   (cmp @0 @0)
6097   (if (cmp != NE_EXPR
6098        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6099        || ! tree_expr_maybe_nan_p (@0))
6100    { constant_boolean_node (false, type); })))
6101 (for cmp (unle unge uneq)
6102  (simplify
6103   (cmp @0 @0)
6104   { constant_boolean_node (true, type); }))
6105 (for cmp (unlt ungt)
6106  (simplify
6107   (cmp @0 @0)
6108   (unordered @0 @0)))
6109 (simplify
6110  (ltgt @0 @0)
6111  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6112   { constant_boolean_node (false, type); }))
6114 /* x == ~x -> false */
6115 /* x != ~x -> true */
6116 (for cmp (eq ne)
6117  (simplify
6118   (cmp:c @0 (bit_not @0))
6119   { constant_boolean_node (cmp == NE_EXPR, type); }))
6121 /* Fold ~X op ~Y as Y op X.  */
6122 (for cmp (simple_comparison)
6123  (simplify
6124   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6125   (if (single_use (@2) && single_use (@3))
6126    (with { tree otype = TREE_TYPE (@4); }
6127     (cmp (convert:otype @1) (convert:otype @0))))))
6129 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6130 (for cmp (simple_comparison)
6131      scmp (swapped_simple_comparison)
6132  (simplify
6133   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6134   (if (single_use (@2)
6135        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6136    (with { tree otype = TREE_TYPE (@1); }
6137     (scmp (convert:otype @0) (bit_not @1))))))
6139 (for cmp (simple_comparison)
6140  (simplify
6141   (cmp @0 REAL_CST@1)
6142   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6143   (switch
6144    /* a CMP (-0) -> a CMP 0  */
6145    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6146     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6147    /* (-0) CMP b -> 0 CMP b.  */
6148    (if (TREE_CODE (@0) == REAL_CST
6149         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6150     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6151    /* x != NaN is always true, other ops are always false.  */
6152    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6153         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6154         && !tree_expr_signaling_nan_p (@1)
6155         && !tree_expr_maybe_signaling_nan_p (@0))
6156     { constant_boolean_node (cmp == NE_EXPR, type); })
6157    /* NaN != y is always true, other ops are always false.  */
6158    (if (TREE_CODE (@0) == REAL_CST
6159         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6160         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6161         && !tree_expr_signaling_nan_p (@0)
6162         && !tree_expr_signaling_nan_p (@1))
6163     { constant_boolean_node (cmp == NE_EXPR, type); })
6164    /* Fold comparisons against infinity.  */
6165    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6166         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6167     (with
6168      {
6169        REAL_VALUE_TYPE max;
6170        enum tree_code code = cmp;
6171        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6172        if (neg)
6173          code = swap_tree_comparison (code);
6174      }
6175      (switch
6176       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6177       (if (code == GT_EXPR
6178            && !(HONOR_NANS (@0) && flag_trapping_math))
6179        { constant_boolean_node (false, type); })
6180       (if (code == LE_EXPR)
6181        /* x <= +Inf is always true, if we don't care about NaNs.  */
6182        (if (! HONOR_NANS (@0))
6183         { constant_boolean_node (true, type); }
6184         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6185            an "invalid" exception.  */
6186         (if (!flag_trapping_math)
6187          (eq @0 @0))))
6188       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6189          for == this introduces an exception for x a NaN.  */
6190       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6191            || code == GE_EXPR)
6192        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6193         (if (neg)
6194          (lt @0 { build_real (TREE_TYPE (@0), max); })
6195          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6196       /* x < +Inf is always equal to x <= DBL_MAX.  */
6197       (if (code == LT_EXPR)
6198        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6199         (if (neg)
6200          (ge @0 { build_real (TREE_TYPE (@0), max); })
6201          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6202       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6203          an exception for x a NaN so use an unordered comparison.  */
6204       (if (code == NE_EXPR)
6205        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6206         (if (! HONOR_NANS (@0))
6207          (if (neg)
6208           (ge @0 { build_real (TREE_TYPE (@0), max); })
6209           (le @0 { build_real (TREE_TYPE (@0), max); }))
6210          (if (neg)
6211           (unge @0 { build_real (TREE_TYPE (@0), max); })
6212           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6214  /* If this is a comparison of a real constant with a PLUS_EXPR
6215     or a MINUS_EXPR of a real constant, we can convert it into a
6216     comparison with a revised real constant as long as no overflow
6217     occurs when unsafe_math_optimizations are enabled.  */
6218  (if (flag_unsafe_math_optimizations)
6219   (for op (plus minus)
6220    (simplify
6221     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6222     (with
6223      {
6224        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6225                                TREE_TYPE (@1), @2, @1);
6226      }
6227      (if (tem && !TREE_OVERFLOW (tem))
6228       (cmp @0 { tem; }))))))
6230  /* Likewise, we can simplify a comparison of a real constant with
6231     a MINUS_EXPR whose first operand is also a real constant, i.e.
6232     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6233     floating-point types only if -fassociative-math is set.  */
6234  (if (flag_associative_math)
6235   (simplify
6236    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6237    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6238     (if (tem && !TREE_OVERFLOW (tem))
6239      (cmp { tem; } @1)))))
6241  /* Fold comparisons against built-in math functions.  */
6242  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6243   (for sq (SQRT)
6244    (simplify
6245     (cmp (sq @0) REAL_CST@1)
6246     (switch
6247      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6248       (switch
6249        /* sqrt(x) < y is always false, if y is negative.  */
6250        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6251         { constant_boolean_node (false, type); })
6252        /* sqrt(x) > y is always true, if y is negative and we
6253           don't care about NaNs, i.e. negative values of x.  */
6254        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6255         { constant_boolean_node (true, type); })
6256        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6257        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6258      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6259       (switch
6260        /* sqrt(x) < 0 is always false.  */
6261        (if (cmp == LT_EXPR)
6262         { constant_boolean_node (false, type); })
6263        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6264        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6265         { constant_boolean_node (true, type); })
6266        /* sqrt(x) <= 0 -> x == 0.  */
6267        (if (cmp == LE_EXPR)
6268         (eq @0 @1))
6269        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6270           == or !=.  In the last case:
6272             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6274           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6275           the results for other x follow from natural arithmetic.  */
6276        (cmp @0 @1)))
6277      (if ((cmp == LT_EXPR
6278            || cmp == LE_EXPR
6279            || cmp == GT_EXPR
6280            || cmp == GE_EXPR)
6281           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6282           /* Give up for -frounding-math.  */
6283           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6284       (with
6285        {
6286          REAL_VALUE_TYPE c2;
6287          enum tree_code ncmp = cmp;
6288          const real_format *fmt
6289            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6290          real_arithmetic (&c2, MULT_EXPR,
6291                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6292          real_convert (&c2, fmt, &c2);
6293          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6294             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6295          if (!REAL_VALUE_ISINF (c2))
6296            {
6297              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6298                                         build_real (TREE_TYPE (@0), c2));
6299              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6300                ncmp = ERROR_MARK;
6301              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6302                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6303                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6304              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6305                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6306                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6307              else
6308                {
6309                  /* With rounding to even, sqrt of up to 3 different values
6310                     gives the same normal result, so in some cases c2 needs
6311                     to be adjusted.  */
6312                  REAL_VALUE_TYPE c2alt, tow;
6313                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6314                    tow = dconst0;
6315                  else
6316                    tow = dconstinf;
6317                  real_nextafter (&c2alt, fmt, &c2, &tow);
6318                  real_convert (&c2alt, fmt, &c2alt);
6319                  if (REAL_VALUE_ISINF (c2alt))
6320                    ncmp = ERROR_MARK;
6321                  else
6322                    {
6323                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6324                                            build_real (TREE_TYPE (@0), c2alt));
6325                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6326                        ncmp = ERROR_MARK;
6327                      else if (real_equal (&TREE_REAL_CST (c3),
6328                                           &TREE_REAL_CST (@1)))
6329                        c2 = c2alt;
6330                    }
6331                }
6332            }
6333        }
6334        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6335         (if (REAL_VALUE_ISINF (c2))
6336          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6337          (if (HONOR_INFINITIES (@0))
6338           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6339           { constant_boolean_node (false, type); })
6340          /* sqrt(x) > c is the same as x > c*c.  */
6341          (if (ncmp != ERROR_MARK)
6342           (if (ncmp == GE_EXPR)
6343            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6344            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6345         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6346         (if (REAL_VALUE_ISINF (c2))
6347          (switch
6348           /* sqrt(x) < y is always true, when y is a very large
6349              value and we don't care about NaNs or Infinities.  */
6350           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6351            { constant_boolean_node (true, type); })
6352           /* sqrt(x) < y is x != +Inf when y is very large and we
6353              don't care about NaNs.  */
6354           (if (! HONOR_NANS (@0))
6355            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6356           /* sqrt(x) < y is x >= 0 when y is very large and we
6357              don't care about Infinities.  */
6358           (if (! HONOR_INFINITIES (@0))
6359            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6360           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6361           (if (GENERIC)
6362            (truth_andif
6363             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6364             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6365          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6366          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6367           (if (ncmp == LT_EXPR)
6368            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6369            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6370           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6371           (if (ncmp != ERROR_MARK && GENERIC)
6372            (if (ncmp == LT_EXPR)
6373             (truth_andif
6374              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6375              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6376             (truth_andif
6377              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6378              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6379    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6380    (simplify
6381     (cmp (sq @0) (sq @1))
6382       (if (! HONOR_NANS (@0))
6383         (cmp @0 @1))))))
6385 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6386 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6387      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6388  (simplify
6389   (cmp (float@0 @1) (float @2))
6390    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6391         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6392     (with
6393      {
6394        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6395        tree type1 = TREE_TYPE (@1);
6396        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6397        tree type2 = TREE_TYPE (@2);
6398        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6399      }
6400      (if (fmt.can_represent_integral_type_p (type1)
6401           && fmt.can_represent_integral_type_p (type2))
6402       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6403        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6404        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6405             && type1_signed_p >= type2_signed_p)
6406         (icmp @1 (convert @2))
6407         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6408              && type1_signed_p <= type2_signed_p)
6409          (icmp (convert:type2 @1) @2)
6410          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6411               && type1_signed_p == type2_signed_p)
6412           (icmp @1 @2))))))))))
6414 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6415 (for cmp  (lt le eq ne ge gt)
6416      icmp (le le eq ne ge ge)
6417  (simplify
6418   (cmp (float @0) REAL_CST@1)
6419    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6420         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6421     (with
6422      {
6423        tree itype = TREE_TYPE (@0);
6424        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6425        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6426        /* Be careful to preserve any potential exceptions due to
6427           NaNs.  qNaNs are ok in == or != context.
6428           TODO: relax under -fno-trapping-math or
6429           -fno-signaling-nans.  */
6430        bool exception_p
6431          = real_isnan (cst) && (cst->signalling
6432                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6433      }
6434      /* TODO: allow non-fitting itype and SNaNs when
6435         -fno-trapping-math.  */
6436      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6437       (with
6438        {
6439          signop isign = TYPE_SIGN (itype);
6440          REAL_VALUE_TYPE imin, imax;
6441          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6442          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6444          REAL_VALUE_TYPE icst;
6445          if (cmp == GT_EXPR || cmp == GE_EXPR)
6446            real_ceil (&icst, fmt, cst);
6447          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6448            real_floor (&icst, fmt, cst);
6449          else
6450            real_trunc (&icst, fmt, cst);
6452          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6454          bool overflow_p = false;
6455          wide_int icst_val
6456            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6457        }
6458        (switch
6459         /* Optimize cases when CST is outside of ITYPE's range.  */
6460         (if (real_compare (LT_EXPR, cst, &imin))
6461          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6462                                   type); })
6463         (if (real_compare (GT_EXPR, cst, &imax))
6464          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6465                                   type); })
6466         /* Remove cast if CST is an integer representable by ITYPE.  */
6467         (if (cst_int_p)
6468          (cmp @0 { gcc_assert (!overflow_p);
6469                    wide_int_to_tree (itype, icst_val); })
6470         )
6471         /* When CST is fractional, optimize
6472             (FTYPE) N == CST -> 0
6473             (FTYPE) N != CST -> 1.  */
6474         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6475          { constant_boolean_node (cmp == NE_EXPR, type); })
6476         /* Otherwise replace with sensible integer constant.  */
6477         (with
6478          {
6479            gcc_checking_assert (!overflow_p);
6480          }
6481          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6483 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6484 (for cmp (eq ne)
6485  (simplify
6486   (cmp (exact_div @0 @1) INTEGER_CST@2)
6487   (if (!integer_zerop (@1))
6488    (if (wi::to_wide (@2) == 0)
6489     (cmp @0 @2)
6490     (if (TREE_CODE (@1) == INTEGER_CST)
6491      (with
6492       {
6493         wi::overflow_type ovf;
6494         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6495                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6496       }
6497       (if (ovf)
6498        { constant_boolean_node (cmp == NE_EXPR, type); }
6499        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6500 (for cmp (lt le gt ge)
6501  (simplify
6502   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6503   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6504    (with
6505     {
6506       wi::overflow_type ovf;
6507       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6508                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6509     }
6510     (if (ovf)
6511      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6512                                         TYPE_SIGN (TREE_TYPE (@2)))
6513                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6514      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6516 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6518    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6519    For large C (more than min/B+2^size), this is also true, with the
6520    multiplication computed modulo 2^size.
6521    For intermediate C, this just tests the sign of A.  */
6522 (for cmp  (lt le gt ge)
6523      cmp2 (ge ge lt lt)
6524  (simplify
6525   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6526   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6527        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6528        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6529    (with
6530     {
6531       tree utype = TREE_TYPE (@2);
6532       wide_int denom = wi::to_wide (@1);
6533       wide_int right = wi::to_wide (@2);
6534       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6535       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6536       bool small = wi::leu_p (right, smax);
6537       bool large = wi::geu_p (right, smin);
6538     }
6539     (if (small || large)
6540      (cmp (convert:utype @0) (mult @2 (convert @1)))
6541      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6543 /* Unordered tests if either argument is a NaN.  */
6544 (simplify
6545  (bit_ior (unordered @0 @0) (unordered @1 @1))
6546  (if (types_match (@0, @1))
6547   (unordered @0 @1)))
6548 (simplify
6549  (bit_and (ordered @0 @0) (ordered @1 @1))
6550  (if (types_match (@0, @1))
6551   (ordered @0 @1)))
6552 (simplify
6553  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6554  @2)
6555 (simplify
6556  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6557  @2)
6559 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6560    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6562    Note that comparisons
6563      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6564      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6565    will be canonicalized to above so there's no need to
6566    consider them here.
6567  */
6569 (for cmp (le gt)
6570      eqcmp (eq ne)
6571  (simplify
6572   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6573   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6574    (with
6575     {
6576      tree ty = TREE_TYPE (@0);
6577      unsigned prec = TYPE_PRECISION (ty);
6578      wide_int mask = wi::to_wide (@2, prec);
6579      wide_int rhs = wi::to_wide (@3, prec);
6580      signop sgn = TYPE_SIGN (ty);
6581     }
6582     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6583          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6584       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6585              { build_zero_cst (ty); }))))))
6587 /* -A CMP -B -> B CMP A.  */
6588 (for cmp (tcc_comparison)
6589      scmp (swapped_tcc_comparison)
6590  (simplify
6591   (cmp (negate @0) (negate @1))
6592   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6593        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6594            && (cmp == EQ_EXPR
6595                || cmp == NE_EXPR
6596                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6597    (scmp @0 @1)))
6598  (simplify
6599   (cmp (negate @0) CONSTANT_CLASS_P@1)
6600   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6601        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6602            && (cmp == EQ_EXPR
6603                || cmp == NE_EXPR
6604                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6605    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6606     (if (tem && !TREE_OVERFLOW (tem))
6607      (scmp @0 { tem; }))))))
6609 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6610 (for op (abs absu)
6611  (for eqne (eq ne)
6612   (simplify
6613    (eqne (op @0) zerop@1)
6614    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6616 /* From fold_sign_changed_comparison and fold_widened_comparison.
6617    FIXME: the lack of symmetry is disturbing.  */
6618 (for cmp (simple_comparison)
6619  (simplify
6620   (cmp (convert@0 @00) (convert?@1 @10))
6621   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6622        /* Disable this optimization if we're casting a function pointer
6623           type on targets that require function pointer canonicalization.  */
6624        && !(targetm.have_canonicalize_funcptr_for_compare ()
6625             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6626                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6627                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6628                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6629        && single_use (@0))
6630    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6631         && (TREE_CODE (@10) == INTEGER_CST
6632             || @1 != @10)
6633         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6634             || cmp == NE_EXPR
6635             || cmp == EQ_EXPR)
6636         && !POINTER_TYPE_P (TREE_TYPE (@00))
6637         /* (int)bool:32 != (int)uint is not the same as
6638            bool:32 != (bool:32)uint since boolean types only have two valid
6639            values independent of their precision.  */
6640         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6641             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6642     /* ???  The special-casing of INTEGER_CST conversion was in the original
6643        code and here to avoid a spurious overflow flag on the resulting
6644        constant which fold_convert produces.  */
6645     (if (TREE_CODE (@1) == INTEGER_CST)
6646      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6647                                 wide_int::from (wi::to_wide (@1),
6648                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6649                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6650                                                 TYPE_SIGN (TREE_TYPE (@1))),
6651                                 0, TREE_OVERFLOW (@1)); })
6652      (cmp @00 (convert @1)))
6654     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6655      /* If possible, express the comparison in the shorter mode.  */
6656      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6657            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6658            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6659                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6660           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6661               || ((TYPE_PRECISION (TREE_TYPE (@00))
6662                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6663                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6664                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6665               || (TREE_CODE (@10) == INTEGER_CST
6666                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6667                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6668       (cmp @00 (convert @10))
6669       (if (TREE_CODE (@10) == INTEGER_CST
6670            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6671            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6672        (with
6673         {
6674           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6675           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6676           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6677           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6678         }
6679         (if (above || below)
6680          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6681           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6682           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6683            { constant_boolean_node (above ? true : false, type); }
6684            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6685             { constant_boolean_node (above ? false : true, type); })))))))))
6686    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6687    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6688         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6689             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6690         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6691             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6692     (with
6693      {
6694        tree type1 = TREE_TYPE (@10);
6695        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6696          {
6697            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6698            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6699                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6700              type1 = float_type_node;
6701            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6702                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6703              type1 = double_type_node;
6704          }
6705       tree newtype
6706         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6707            ? TREE_TYPE (@00) : type1);
6708      }
6709      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6710       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6713 (for cmp (eq ne)
6714  (simplify
6715   /* SSA names are canonicalized to 2nd place.  */
6716   (cmp addr@0 SSA_NAME@1)
6717   (with
6718    {
6719      poly_int64 off; tree base;
6720      tree addr = (TREE_CODE (@0) == SSA_NAME
6721                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6722    }
6723    /* A local variable can never be pointed to by
6724       the default SSA name of an incoming parameter.  */
6725    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6726         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6727         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6728         && TREE_CODE (base) == VAR_DECL
6729         && auto_var_in_fn_p (base, current_function_decl))
6730     (if (cmp == NE_EXPR)
6731      { constant_boolean_node (true, type); }
6732      { constant_boolean_node (false, type); })
6733     /* If the address is based on @1 decide using the offset.  */
6734     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6735          && TREE_CODE (base) == MEM_REF
6736          && TREE_OPERAND (base, 0) == @1)
6737      (with { off += mem_ref_offset (base).force_shwi (); }
6738       (if (known_ne (off, 0))
6739        { constant_boolean_node (cmp == NE_EXPR, type); }
6740        (if (known_eq (off, 0))
6741         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6743 /* Equality compare simplifications from fold_binary  */
6744 (for cmp (eq ne)
6746  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6747     Similarly for NE_EXPR.  */
6748  (simplify
6749   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6750   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6751        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6752    { constant_boolean_node (cmp == NE_EXPR, type); }))
6754  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6755  (simplify
6756   (cmp (bit_xor @0 @1) integer_zerop)
6757   (cmp @0 @1))
6759  /* (X ^ Y) == Y becomes X == 0.
6760     Likewise (X ^ Y) == X becomes Y == 0.  */
6761  (simplify
6762   (cmp:c (bit_xor:c @0 @1) @0)
6763   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6765  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6766  (simplify
6767   (cmp:c (bit_and:c @0 @1) @0)
6768   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6769  (simplify
6770   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6771   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6772        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6773        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6774        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6775        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6776        && !wi::neg_p (wi::to_wide (@1)))
6777    (cmp (bit_and @0 (convert (bit_not @1)))
6778         { build_zero_cst (TREE_TYPE (@0)); })))
6780  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6781  (simplify
6782   (cmp:c (bit_ior:c @0 @1) @1)
6783   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6785  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6786  (simplify
6787   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6788   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6789    (cmp @0 (bit_xor @1 (convert @2)))))
6791  (simplify
6792   (cmp (nop_convert? @0) integer_zerop)
6793   (if (tree_expr_nonzero_p (@0))
6794    { constant_boolean_node (cmp == NE_EXPR, type); }))
6796  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6797  (simplify
6798   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6799   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6801 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6802    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6803    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6804    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6805 (for cmp (eq ne)
6806      ncmp (ge lt)
6807  (for sgncmp (ge lt)
6808   (simplify
6809    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6810    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6811         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6812         && types_match (@0, @1))
6813     (ncmp (bit_xor @0 @1) @2)))))
6814 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6815    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6816 (for cmp (eq ne)
6817      ncmp (lt ge)
6818  (simplify
6819   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6820    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6821         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6822         && types_match (@0, @1))
6823     (ncmp (bit_xor @0 @1) @2))))
6825 /* If we have (A & C) == C where C is a power of 2, convert this into
6826    (A & C) != 0.  Similarly for NE_EXPR.  */
6827 (for cmp (eq ne)
6828      icmp (ne eq)
6829  (simplify
6830   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6831   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6833 #if GIMPLE
6834 /* From fold_binary_op_with_conditional_arg handle the case of
6835    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6836    compares simplify.  */
6837 (for cmp (simple_comparison)
6838  (simplify
6839   (cmp:c (cond @0 @1 @2) @3)
6840   /* Do not move possibly trapping operations into the conditional as this
6841      pessimizes code and causes gimplification issues when applied late.  */
6842   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6843        || !operation_could_trap_p (cmp, true, false, @3))
6844    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6845 #endif
6847 (for cmp (ge lt)
6848 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6849 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6850  (simplify
6851   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6852    (if (INTEGRAL_TYPE_P (type)
6853         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6854         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6855         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6856     (with
6857      {
6858        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6859      }
6860     (if (cmp == LT_EXPR)
6861      (bit_xor (convert (rshift @0 {shifter;})) @1)
6862      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6863 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6864 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6865  (simplify
6866   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6867    (if (INTEGRAL_TYPE_P (type)
6868         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6869         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6870         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6871     (with
6872      {
6873        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6874      }
6875     (if (cmp == GE_EXPR)
6876      (bit_xor (convert (rshift @0 {shifter;})) @1)
6877      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6879 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6880    convert this into a shift followed by ANDing with D.  */
6881 (simplify
6882  (cond
6883   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6884   INTEGER_CST@2 integer_zerop)
6885  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6886   (with {
6887      int shift = (wi::exact_log2 (wi::to_wide (@2))
6888                   - wi::exact_log2 (wi::to_wide (@1)));
6889    }
6890    (if (shift > 0)
6891     (bit_and
6892      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6893     (bit_and
6894      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6895      @2)))))
6897 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6898    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6899 (for cmp (eq ne)
6900      ncmp (ge lt)
6901  (simplify
6902   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6903   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6904        && type_has_mode_precision_p (TREE_TYPE (@0))
6905        && element_precision (@2) >= element_precision (@0)
6906        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6907    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6908     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6910 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6911    this into a right shift or sign extension followed by ANDing with C.  */
6912 (simplify
6913  (cond
6914   (lt @0 integer_zerop)
6915   INTEGER_CST@1 integer_zerop)
6916  (if (integer_pow2p (@1)
6917       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6918   (with {
6919     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6920    }
6921    (if (shift >= 0)
6922     (bit_and
6923      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6924      @1)
6925     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6926        sign extension followed by AND with C will achieve the effect.  */
6927     (bit_and (convert @0) @1)))))
6929 /* When the addresses are not directly of decls compare base and offset.
6930    This implements some remaining parts of fold_comparison address
6931    comparisons but still no complete part of it.  Still it is good
6932    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6933 (for cmp (simple_comparison)
6934  (simplify
6935   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6936   (with
6937    {
6938      poly_int64 off0, off1;
6939      tree base0, base1;
6940      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6941                                   off0, off1, GENERIC);
6942    }
6943    (if (equal == 1)
6944     (switch
6945      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6946       { constant_boolean_node (known_eq (off0, off1), type); })
6947      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6948       { constant_boolean_node (known_ne (off0, off1), type); })
6949      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6950       { constant_boolean_node (known_lt (off0, off1), type); })
6951      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6952       { constant_boolean_node (known_le (off0, off1), type); })
6953      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6954       { constant_boolean_node (known_ge (off0, off1), type); })
6955      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6956       { constant_boolean_node (known_gt (off0, off1), type); }))
6957     (if (equal == 0)
6958      (switch
6959       (if (cmp == EQ_EXPR)
6960        { constant_boolean_node (false, type); })
6961       (if (cmp == NE_EXPR)
6962        { constant_boolean_node (true, type); })))))))
6964 #if GIMPLE
6965 /* a?~t:t -> (-(a))^t */
6966 (simplify
6967  (cond @0 @1 @2)
6968  (with { bool wascmp; }
6969   (if (INTEGRAL_TYPE_P (type)
6970        && bitwise_inverted_equal_p (@1, @2, wascmp)
6971        && (!wascmp || TYPE_PRECISION (type) == 1))
6972    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6973         || TYPE_PRECISION (type) == 1)
6974     (bit_xor (convert:type @0) @2)
6975     (bit_xor (negate (convert:type @0)) @2)))))
6976 #endif
6978 /* Simplify pointer equality compares using PTA.  */
6979 (for neeq (ne eq)
6980  (simplify
6981   (neeq @0 @1)
6982   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6983        && ptrs_compare_unequal (@0, @1))
6984    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6986 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6987    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6988    Disable the transform if either operand is pointer to function.
6989    This broke pr22051-2.c for arm where function pointer
6990    canonicalizaion is not wanted.  */
6992 (for cmp (ne eq)
6993  (simplify
6994   (cmp (convert @0) INTEGER_CST@1)
6995   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6996          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6997          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6998          /* Don't perform this optimization in GENERIC if @0 has reference
6999             type when sanitizing.  See PR101210.  */
7000          && !(GENERIC
7001               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7002               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7003         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7004             && POINTER_TYPE_P (TREE_TYPE (@1))
7005             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7006        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7007    (cmp @0 (convert @1)))))
7009 /* Non-equality compare simplifications from fold_binary  */
7010 (for cmp (lt gt le ge)
7011  /* Comparisons with the highest or lowest possible integer of
7012     the specified precision will have known values.  */
7013  (simplify
7014   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7015   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7016         || POINTER_TYPE_P (TREE_TYPE (@1))
7017         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7018        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7019    (with
7020     {
7021       tree cst = uniform_integer_cst_p (@1);
7022       tree arg1_type = TREE_TYPE (cst);
7023       unsigned int prec = TYPE_PRECISION (arg1_type);
7024       wide_int max = wi::max_value (arg1_type);
7025       wide_int signed_max = wi::max_value (prec, SIGNED);
7026       wide_int min = wi::min_value (arg1_type);
7027     }
7028     (switch
7029      (if (wi::to_wide (cst) == max)
7030       (switch
7031        (if (cmp == GT_EXPR)
7032         { constant_boolean_node (false, type); })
7033        (if (cmp == GE_EXPR)
7034         (eq @2 @1))
7035        (if (cmp == LE_EXPR)
7036         { constant_boolean_node (true, type); })
7037        (if (cmp == LT_EXPR)
7038         (ne @2 @1))))
7039      (if (wi::to_wide (cst) == min)
7040       (switch
7041        (if (cmp == LT_EXPR)
7042         { constant_boolean_node (false, type); })
7043        (if (cmp == LE_EXPR)
7044         (eq @2 @1))
7045        (if (cmp == GE_EXPR)
7046         { constant_boolean_node (true, type); })
7047        (if (cmp == GT_EXPR)
7048         (ne @2 @1))))
7049      (if (wi::to_wide (cst) == max - 1)
7050       (switch
7051        (if (cmp == GT_EXPR)
7052         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7053                                     wide_int_to_tree (TREE_TYPE (cst),
7054                                                       wi::to_wide (cst)
7055                                                       + 1)); }))
7056        (if (cmp == LE_EXPR)
7057         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7058                                     wide_int_to_tree (TREE_TYPE (cst),
7059                                                       wi::to_wide (cst)
7060                                                       + 1)); }))))
7061      (if (wi::to_wide (cst) == min + 1)
7062       (switch
7063        (if (cmp == GE_EXPR)
7064         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7065                                     wide_int_to_tree (TREE_TYPE (cst),
7066                                                       wi::to_wide (cst)
7067                                                       - 1)); }))
7068        (if (cmp == LT_EXPR)
7069         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7070                                     wide_int_to_tree (TREE_TYPE (cst),
7071                                                       wi::to_wide (cst)
7072                                                       - 1)); }))))
7073      (if (wi::to_wide (cst) == signed_max
7074           && TYPE_UNSIGNED (arg1_type)
7075           && TYPE_MODE (arg1_type) != BLKmode
7076           /* We will flip the signedness of the comparison operator
7077              associated with the mode of @1, so the sign bit is
7078              specified by this mode.  Check that @1 is the signed
7079              max associated with this sign bit.  */
7080           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7081           /* signed_type does not work on pointer types.  */
7082           && INTEGRAL_TYPE_P (arg1_type))
7083       /* The following case also applies to X < signed_max+1
7084          and X >= signed_max+1 because previous transformations.  */
7085       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7086        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7087         (switch
7088          (if (cst == @1 && cmp == LE_EXPR)
7089           (ge (convert:st @0) { build_zero_cst (st); }))
7090          (if (cst == @1 && cmp == GT_EXPR)
7091           (lt (convert:st @0) { build_zero_cst (st); }))
7092          (if (cmp == LE_EXPR)
7093           (ge (view_convert:st @0) { build_zero_cst (st); }))
7094          (if (cmp == GT_EXPR)
7095           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7097 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7098 (simplify
7099  (lt:c @0 (convert (ne @0 integer_zerop)))
7100  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7101   { constant_boolean_node (false, type); }))
7103 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7104 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7105 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7106 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7107 (for outer (ne eq)
7108  (for inner (ne eq)
7109   (simplify
7110    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7111    (with {
7112      bool cst1 = integer_onep (@1);
7113      bool cst0 = integer_zerop (@1);
7114      bool innereq = inner == EQ_EXPR;
7115      bool outereq = outer == EQ_EXPR;
7116     }
7117     (switch
7118      (if (innereq ? cst0 : cst1)
7119       { constant_boolean_node (!outereq, type); })
7120      (if (innereq ? cst1 : cst0)
7121       (with {
7122         tree utype = unsigned_type_for (TREE_TYPE (@0));
7123         tree ucst1 = build_one_cst (utype);
7124        }
7125        (if (!outereq)
7126         (gt (convert:utype @0) { ucst1; })
7127         (le (convert:utype @0) { ucst1; })
7128        )
7129       )
7130      )
7131      (with {
7132        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7133       }
7134       (if (outereq)
7135        (eq @0 { value; })
7136        (ne @0 { value; })
7137       )
7138      )
7139     )
7140    )
7141   )
7145 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7146  /* If the second operand is NaN, the result is constant.  */
7147  (simplify
7148   (cmp @0 REAL_CST@1)
7149   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7150        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7151    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7152                             ? false : true, type); })))
7154 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7155 (simplify
7156   (unordered @0 @1)
7157   (switch
7158     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7159         { constant_boolean_node (true, type); })
7160     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7161         { constant_boolean_node (false, type); })))
7163 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7164 (simplify
7165   (ordered @0 @1)
7166   (switch
7167     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7168         { constant_boolean_node (false, type); })
7169     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7170         { constant_boolean_node (true, type); })))
7172 /* bool_var != 0 becomes bool_var.  */
7173 (simplify
7174  (ne @0 integer_zerop)
7175  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7176       && types_match (type, TREE_TYPE (@0)))
7177   (non_lvalue @0)))
7178 /* bool_var == 1 becomes bool_var.  */
7179 (simplify
7180  (eq @0 integer_onep)
7181  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7182       && types_match (type, TREE_TYPE (@0)))
7183   (non_lvalue @0)))
7184 /* Do not handle
7185    bool_var == 0 becomes !bool_var or
7186    bool_var != 1 becomes !bool_var
7187    here because that only is good in assignment context as long
7188    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7189    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7190    clearly less optimal and which we'll transform again in forwprop.  */
7192 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7193    where ~Y + 1 == pow2 and Z = ~Y.  */
7194 (for cst (VECTOR_CST INTEGER_CST)
7195  (for cmp (eq ne)
7196       icmp (le gt)
7197   (simplify
7198    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7199     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7200      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7201       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7202                          ? optab_vector : optab_default;
7203               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7204        (if (target_supports_op_p (utype, icmp, optab)
7205             || (optimize_vectors_before_lowering_p ()
7206                 && (!target_supports_op_p (type, cmp, optab)
7207                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7208         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7209          (icmp @0 { csts; })
7210          (icmp (view_convert:utype @0) { csts; })))))))))
7212 /* When one argument is a constant, overflow detection can be simplified.
7213    Currently restricted to single use so as not to interfere too much with
7214    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7215    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7216 (for cmp (lt le ge gt)
7217      out (gt gt le le)
7218  (simplify
7219   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7220   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7221        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7222        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7223        && wi::to_wide (@1) != 0
7224        && single_use (@2))
7225    (with {
7226      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7227      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7228     }
7229     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7230                                 wi::max_value (prec, sign)
7231                                 - wi::to_wide (@1)); })))))
7233 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7234    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7235    expects the long form, so we restrict the transformation for now.  */
7236 (for cmp (gt le)
7237  (simplify
7238   (cmp:c (minus@2 @0 @1) @0)
7239   (if (single_use (@2)
7240        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7241        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7242    (cmp @1 @0))))
7244 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7245 (for cmp (ge lt)
7246  (simplify
7247   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7248    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7249         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7250     (cmp @1 @0))))
7252 /* Testing for overflow is unnecessary if we already know the result.  */
7253 /* A - B > A  */
7254 (for cmp (gt le)
7255      out (ne eq)
7256  (simplify
7257   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7258   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7259        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7260    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7261 /* A + B < A  */
7262 (for cmp (lt ge)
7263      out (ne eq)
7264  (simplify
7265   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7266   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7267        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7268    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7270 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7271    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7272 (for cmp (lt ge)
7273      out (ne eq)
7274  (simplify
7275   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7276   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7277    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7278     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7280 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7281    is at least twice as wide as type of A and B, simplify to
7282    __builtin_mul_overflow (A, B, <unused>).  */
7283 (for cmp (eq ne)
7284  (simplify
7285   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7286        integer_zerop)
7287   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7288        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7289        && TYPE_UNSIGNED (TREE_TYPE (@0))
7290        && (TYPE_PRECISION (TREE_TYPE (@3))
7291            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7292        && tree_fits_uhwi_p (@2)
7293        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7294        && types_match (@0, @1)
7295        && type_has_mode_precision_p (TREE_TYPE (@0))
7296        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7297            != CODE_FOR_nothing))
7298    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7299     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7301 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7302 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7303  (simplify
7304   (ovf (convert@2 @0) @1)
7305   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7306        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7307        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7308        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7309    (ovf @0 @1)))
7310  (simplify
7311   (ovf @1 (convert@2 @0))
7312   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7313        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7314        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7315        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7316    (ovf @1 @0))))
7318 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7319    are unsigned to x > (umax / cst).  Similarly for signed type, but
7320    in that case it needs to be outside of a range.  */
7321 (simplify
7322  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7323   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7324        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7325        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7326        && int_fits_type_p (@1, TREE_TYPE (@0)))
7327    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7328     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7329     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7330      (if (integer_minus_onep (@1))
7331       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7332       (with
7333        {
7334          tree div = fold_convert (TREE_TYPE (@0), @1);
7335          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7336                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7337          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7338                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7339          tree etype = range_check_type (TREE_TYPE (@0));
7340          if (etype)
7341            {
7342              if (wi::neg_p (wi::to_wide (div)))
7343                std::swap (lo, hi);
7344              lo = fold_convert (etype, lo);
7345              hi = fold_convert (etype, hi);
7346              hi = int_const_binop (MINUS_EXPR, hi, lo);
7347            }
7348        }
7349        (if (etype)
7350         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7352 /* Simplification of math builtins.  These rules must all be optimizations
7353    as well as IL simplifications.  If there is a possibility that the new
7354    form could be a pessimization, the rule should go in the canonicalization
7355    section that follows this one.
7357    Rules can generally go in this section if they satisfy one of
7358    the following:
7360    - the rule describes an identity
7362    - the rule replaces calls with something as simple as addition or
7363      multiplication
7365    - the rule contains unary calls only and simplifies the surrounding
7366      arithmetic.  (The idea here is to exclude non-unary calls in which
7367      one operand is constant and in which the call is known to be cheap
7368      when the operand has that value.)  */
7370 (if (flag_unsafe_math_optimizations)
7371  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7372  (simplify
7373   (mult (SQRT_ALL@1 @0) @1)
7374   (if (!tree_expr_maybe_signaling_nan_p (@0))
7375    @0))
7377  (for op (plus minus)
7378   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7379   (simplify
7380    (op (rdiv @0 @1)
7381        (rdiv @2 @1))
7382    (rdiv (op @0 @2) @1)))
7384  (for cmp (lt le gt ge)
7385       neg_cmp (gt ge lt le)
7386   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7387   (simplify
7388    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7389    (with
7390     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7391     (if (tem
7392          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7393               || (real_zerop (tem) && !real_zerop (@1))))
7394      (switch
7395       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7396        (cmp @0 { tem; }))
7397       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7398        (neg_cmp @0 { tem; })))))))
7400  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7401  (for root (SQRT CBRT)
7402   (simplify
7403    (mult (root:s @0) (root:s @1))
7404     (root (mult @0 @1))))
7406  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7407  (for exps (EXP EXP2 EXP10 POW10)
7408   (simplify
7409    (mult (exps:s @0) (exps:s @1))
7410     (exps (plus @0 @1))))
7412  /* Simplify a/root(b/c) into a*root(c/b).  */
7413  (for root (SQRT CBRT)
7414   (simplify
7415    (rdiv @0 (root:s (rdiv:s @1 @2)))
7416     (mult @0 (root (rdiv @2 @1)))))
7418  /* Simplify x/expN(y) into x*expN(-y).  */
7419  (for exps (EXP EXP2 EXP10 POW10)
7420   (simplify
7421    (rdiv @0 (exps:s @1))
7422     (mult @0 (exps (negate @1)))))
7424  (for logs (LOG LOG2 LOG10 LOG10)
7425       exps (EXP EXP2 EXP10 POW10)
7426   /* logN(expN(x)) -> x.  */
7427   (simplify
7428    (logs (exps @0))
7429    @0)
7430   /* expN(logN(x)) -> x.  */
7431   (simplify
7432    (exps (logs @0))
7433    @0))
7435  /* Optimize logN(func()) for various exponential functions.  We
7436     want to determine the value "x" and the power "exponent" in
7437     order to transform logN(x**exponent) into exponent*logN(x).  */
7438  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7439       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7440   (simplify
7441    (logs (exps @0))
7442    (if (SCALAR_FLOAT_TYPE_P (type))
7443     (with {
7444       tree x;
7445       switch (exps)
7446         {
7447         CASE_CFN_EXP:
7448           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7449           x = build_real_truncate (type, dconst_e ());
7450           break;
7451         CASE_CFN_EXP2:
7452           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7453           x = build_real (type, dconst2);
7454           break;
7455         CASE_CFN_EXP10:
7456         CASE_CFN_POW10:
7457           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7458           {
7459             REAL_VALUE_TYPE dconst10;
7460             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7461             x = build_real (type, dconst10);
7462           }
7463           break;
7464         default:
7465           gcc_unreachable ();
7466         }
7467       }
7468      (mult (logs { x; }) @0)))))
7470  (for logs (LOG LOG
7471             LOG2 LOG2
7472             LOG10 LOG10)
7473       exps (SQRT CBRT)
7474   (simplify
7475    (logs (exps @0))
7476    (if (SCALAR_FLOAT_TYPE_P (type))
7477     (with {
7478       tree x;
7479       switch (exps)
7480         {
7481         CASE_CFN_SQRT:
7482           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7483           x = build_real (type, dconsthalf);
7484           break;
7485         CASE_CFN_CBRT:
7486           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7487           x = build_real_truncate (type, dconst_third ());
7488           break;
7489         default:
7490           gcc_unreachable ();
7491         }
7492       }
7493      (mult { x; } (logs @0))))))
7495  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7496  (for logs (LOG LOG2 LOG10)
7497       pows (POW)
7498   (simplify
7499    (logs (pows @0 @1))
7500    (mult @1 (logs @0))))
7502  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7503     or if C is a positive power of 2,
7504     pow(C,x) -> exp2(log2(C)*x).  */
7505 #if GIMPLE
7506  (for pows (POW)
7507       exps (EXP)
7508       logs (LOG)
7509       exp2s (EXP2)
7510       log2s (LOG2)
7511   (simplify
7512    (pows REAL_CST@0 @1)
7513    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7514         && real_isfinite (TREE_REAL_CST_PTR (@0))
7515         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7516            the use_exp2 case until after vectorization.  It seems actually
7517            beneficial for all constants to postpone this until later,
7518            because exp(log(C)*x), while faster, will have worse precision
7519            and if x folds into a constant too, that is unnecessary
7520            pessimization.  */
7521         && canonicalize_math_after_vectorization_p ())
7522     (with {
7523        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7524        bool use_exp2 = false;
7525        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7526            && value->cl == rvc_normal)
7527          {
7528            REAL_VALUE_TYPE frac_rvt = *value;
7529            SET_REAL_EXP (&frac_rvt, 1);
7530            if (real_equal (&frac_rvt, &dconst1))
7531              use_exp2 = true;
7532          }
7533      }
7534      (if (!use_exp2)
7535       (if (optimize_pow_to_exp (@0, @1))
7536        (exps (mult (logs @0) @1)))
7537       (exp2s (mult (log2s @0) @1)))))))
7538 #endif
7540  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7541  (for pows (POW)
7542       exps (EXP EXP2 EXP10 POW10)
7543       logs (LOG LOG2 LOG10 LOG10)
7544   (simplify
7545    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7546    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7547         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7548     (exps (plus (mult (logs @0) @1) @2)))))
7550  (for sqrts (SQRT)
7551       cbrts (CBRT)
7552       pows (POW)
7553       exps (EXP EXP2 EXP10 POW10)
7554   /* sqrt(expN(x)) -> expN(x*0.5).  */
7555   (simplify
7556    (sqrts (exps @0))
7557    (exps (mult @0 { build_real (type, dconsthalf); })))
7558   /* cbrt(expN(x)) -> expN(x/3).  */
7559   (simplify
7560    (cbrts (exps @0))
7561    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7562   /* pow(expN(x), y) -> expN(x*y).  */
7563   (simplify
7564    (pows (exps @0) @1)
7565    (exps (mult @0 @1))))
7567  /* tan(atan(x)) -> x.  */
7568  (for tans (TAN)
7569       atans (ATAN)
7570   (simplify
7571    (tans (atans @0))
7572    @0)))
7574  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7575  (for sins (SIN)
7576       atans (ATAN)
7577       sqrts (SQRT)
7578       copysigns (COPYSIGN)
7579   (simplify
7580    (sins (atans:s @0))
7581    (with
7582      {
7583       REAL_VALUE_TYPE r_cst;
7584       build_sinatan_real (&r_cst, type);
7585       tree t_cst = build_real (type, r_cst);
7586       tree t_one = build_one_cst (type);
7587      }
7588     (if (SCALAR_FLOAT_TYPE_P (type))
7589      (cond (lt (abs @0) { t_cst; })
7590       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7591       (copysigns { t_one; } @0))))))
7593 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7594  (for coss (COS)
7595       atans (ATAN)
7596       sqrts (SQRT)
7597       copysigns (COPYSIGN)
7598   (simplify
7599    (coss (atans:s @0))
7600    (with
7601      {
7602       REAL_VALUE_TYPE r_cst;
7603       build_sinatan_real (&r_cst, type);
7604       tree t_cst = build_real (type, r_cst);
7605       tree t_one = build_one_cst (type);
7606       tree t_zero = build_zero_cst (type);
7607      }
7608     (if (SCALAR_FLOAT_TYPE_P (type))
7609      (cond (lt (abs @0) { t_cst; })
7610       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7611       (copysigns { t_zero; } @0))))))
7613  (if (!flag_errno_math)
7614   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7615   (for sinhs (SINH)
7616        atanhs (ATANH)
7617        sqrts (SQRT)
7618    (simplify
7619     (sinhs (atanhs:s @0))
7620     (with { tree t_one = build_one_cst (type); }
7621     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7623   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7624   (for coshs (COSH)
7625        atanhs (ATANH)
7626        sqrts (SQRT)
7627    (simplify
7628     (coshs (atanhs:s @0))
7629     (with { tree t_one = build_one_cst (type); }
7630     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7632 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7633 (simplify
7634  (CABS (complex:C @0 real_zerop@1))
7635  (abs @0))
7637 /* trunc(trunc(x)) -> trunc(x), etc.  */
7638 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7639  (simplify
7640   (fns (fns @0))
7641   (fns @0)))
7642 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7643 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7644  (simplify
7645   (fns integer_valued_real_p@0)
7646   @0))
7648 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7649 (simplify
7650  (HYPOT:c @0 real_zerop@1)
7651  (abs @0))
7653 /* pow(1,x) -> 1.  */
7654 (simplify
7655  (POW real_onep@0 @1)
7656  @0)
7658 (simplify
7659  /* copysign(x,x) -> x.  */
7660  (COPYSIGN_ALL @0 @0)
7661  @0)
7663 (simplify
7664  /* copysign(x,-x) -> -x.  */
7665  (COPYSIGN_ALL @0 (negate@1 @0))
7666  @1)
7668 (simplify
7669  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7670  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7671  (abs @0))
7673 (simplify
7674  /* fabs (copysign(x, y)) -> fabs (x).  */
7675  (abs (COPYSIGN_ALL @0 @1))
7676  (abs @0))
7678 (for scale (LDEXP SCALBN SCALBLN)
7679  /* ldexp(0, x) -> 0.  */
7680  (simplify
7681   (scale real_zerop@0 @1)
7682   @0)
7683  /* ldexp(x, 0) -> x.  */
7684  (simplify
7685   (scale @0 integer_zerop@1)
7686   @0)
7687  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7688  (simplify
7689   (scale REAL_CST@0 @1)
7690   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7691    @0)))
7693 /* Canonicalization of sequences of math builtins.  These rules represent
7694    IL simplifications but are not necessarily optimizations.
7696    The sincos pass is responsible for picking "optimal" implementations
7697    of math builtins, which may be more complicated and can sometimes go
7698    the other way, e.g. converting pow into a sequence of sqrts.
7699    We only want to do these canonicalizations before the pass has run.  */
7701 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7702  /* Simplify tan(x) * cos(x) -> sin(x). */
7703  (simplify
7704   (mult:c (TAN:s @0) (COS:s @0))
7705    (SIN @0))
7707  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7708  (simplify
7709   (mult:c @0 (POW:s @0 REAL_CST@1))
7710   (if (!TREE_OVERFLOW (@1))
7711    (POW @0 (plus @1 { build_one_cst (type); }))))
7713  /* Simplify sin(x) / cos(x) -> tan(x). */
7714  (simplify
7715   (rdiv (SIN:s @0) (COS:s @0))
7716    (TAN @0))
7718  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7719  (simplify
7720   (rdiv (SINH:s @0) (COSH:s @0))
7721    (TANH @0))
7723  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7724  (simplify
7725    (rdiv (TANH:s @0) (SINH:s @0))
7726    (rdiv {build_one_cst (type);} (COSH @0)))
7728  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7729  (simplify
7730   (rdiv (COS:s @0) (SIN:s @0))
7731    (rdiv { build_one_cst (type); } (TAN @0)))
7733  /* Simplify sin(x) / tan(x) -> cos(x). */
7734  (simplify
7735   (rdiv (SIN:s @0) (TAN:s @0))
7736   (if (! HONOR_NANS (@0)
7737        && ! HONOR_INFINITIES (@0))
7738    (COS @0)))
7740  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7741  (simplify
7742   (rdiv (TAN:s @0) (SIN:s @0))
7743   (if (! HONOR_NANS (@0)
7744        && ! HONOR_INFINITIES (@0))
7745    (rdiv { build_one_cst (type); } (COS @0))))
7747  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7748  (simplify
7749   (mult (POW:s @0 @1) (POW:s @0 @2))
7750    (POW @0 (plus @1 @2)))
7752  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7753  (simplify
7754   (mult (POW:s @0 @1) (POW:s @2 @1))
7755    (POW (mult @0 @2) @1))
7757  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7758  (simplify
7759   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7760    (POWI (mult @0 @2) @1))
7762  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7763  (simplify
7764   (rdiv (POW:s @0 REAL_CST@1) @0)
7765   (if (!TREE_OVERFLOW (@1))
7766    (POW @0 (minus @1 { build_one_cst (type); }))))
7768  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7769  (simplify
7770   (rdiv @0 (POW:s @1 @2))
7771    (mult @0 (POW @1 (negate @2))))
7773  (for sqrts (SQRT)
7774       cbrts (CBRT)
7775       pows (POW)
7776   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7777   (simplify
7778    (sqrts (sqrts @0))
7779    (pows @0 { build_real (type, dconst_quarter ()); }))
7780   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7781   (simplify
7782    (sqrts (cbrts @0))
7783    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7784   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7785   (simplify
7786    (cbrts (sqrts @0))
7787    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7788   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7789   (simplify
7790    (cbrts (cbrts tree_expr_nonnegative_p@0))
7791    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7792   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7793   (simplify
7794    (sqrts (pows @0 @1))
7795    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7796   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7797   (simplify
7798    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7799    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7800   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7801   (simplify
7802    (pows (sqrts @0) @1)
7803    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7804   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7805   (simplify
7806    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7807    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7808   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7809   (simplify
7810    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7811    (pows @0 (mult @1 @2))))
7813  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7814  (simplify
7815   (CABS (complex @0 @0))
7816   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7818  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7819  (simplify
7820   (HYPOT @0 @0)
7821   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7823  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7824  (for cexps (CEXP)
7825       exps (EXP)
7826       cexpis (CEXPI)
7827   (simplify
7828    (cexps compositional_complex@0)
7829    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7830     (complex
7831      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7832      (mult @1 (imagpart @2)))))))
7834 (if (canonicalize_math_p ())
7835  /* floor(x) -> trunc(x) if x is nonnegative.  */
7836  (for floors (FLOOR_ALL)
7837       truncs (TRUNC_ALL)
7838   (simplify
7839    (floors tree_expr_nonnegative_p@0)
7840    (truncs @0))))
7842 (match double_value_p
7843  @0
7844  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7845 (for froms (BUILT_IN_TRUNCL
7846             BUILT_IN_FLOORL
7847             BUILT_IN_CEILL
7848             BUILT_IN_ROUNDL
7849             BUILT_IN_NEARBYINTL
7850             BUILT_IN_RINTL)
7851      tos (BUILT_IN_TRUNC
7852           BUILT_IN_FLOOR
7853           BUILT_IN_CEIL
7854           BUILT_IN_ROUND
7855           BUILT_IN_NEARBYINT
7856           BUILT_IN_RINT)
7857  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7858  (if (optimize && canonicalize_math_p ())
7859   (simplify
7860    (froms (convert double_value_p@0))
7861    (convert (tos @0)))))
7863 (match float_value_p
7864  @0
7865  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7866 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7867             BUILT_IN_FLOORL BUILT_IN_FLOOR
7868             BUILT_IN_CEILL BUILT_IN_CEIL
7869             BUILT_IN_ROUNDL BUILT_IN_ROUND
7870             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7871             BUILT_IN_RINTL BUILT_IN_RINT)
7872      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7873           BUILT_IN_FLOORF BUILT_IN_FLOORF
7874           BUILT_IN_CEILF BUILT_IN_CEILF
7875           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7876           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7877           BUILT_IN_RINTF BUILT_IN_RINTF)
7878  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7879     if x is a float.  */
7880  (if (optimize && canonicalize_math_p ()
7881       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7882   (simplify
7883    (froms (convert float_value_p@0))
7884    (convert (tos @0)))))
7886 #if GIMPLE
7887 (match float16_value_p
7888  @0
7889  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7890 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7891             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7892             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7893             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7894             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7895             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7896             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7897             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7898      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7899           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7900           IFN_CEIL IFN_CEIL IFN_CEIL
7901           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7902           IFN_ROUND IFN_ROUND IFN_ROUND
7903           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7904           IFN_RINT IFN_RINT IFN_RINT
7905           IFN_SQRT IFN_SQRT IFN_SQRT)
7906  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7907     if x is a _Float16.  */
7908  (simplify
7909    (convert (froms (convert float16_value_p@0)))
7910      (if (optimize
7911           && types_match (type, TREE_TYPE (@0))
7912           && direct_internal_fn_supported_p (as_internal_fn (tos),
7913                                              type, OPTIMIZE_FOR_BOTH))
7914        (tos @0))))
7916 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7917    x,y is float value, similar for _Float16/double.  */
7918 (for copysigns (COPYSIGN_ALL)
7919  (simplify
7920   (convert (copysigns (convert@2 @0) (convert @1)))
7921    (if (optimize
7922        && !HONOR_SNANS (@2)
7923        && types_match (type, TREE_TYPE (@0))
7924        && types_match (type, TREE_TYPE (@1))
7925        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7926        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7927                                           type, OPTIMIZE_FOR_BOTH))
7928     (IFN_COPYSIGN @0 @1))))
7930 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7931      tos (IFN_FMA IFN_FMA IFN_FMA)
7932  (simplify
7933   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7934    (if (flag_unsafe_math_optimizations
7935        && optimize
7936        && FLOAT_TYPE_P (type)
7937        && FLOAT_TYPE_P (TREE_TYPE (@3))
7938        && types_match (type, TREE_TYPE (@0))
7939        && types_match (type, TREE_TYPE (@1))
7940        && types_match (type, TREE_TYPE (@2))
7941        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7942        && direct_internal_fn_supported_p (as_internal_fn (tos),
7943                                           type, OPTIMIZE_FOR_BOTH))
7944     (tos @0 @1 @2))))
7946 (for maxmin (max min)
7947  (simplify
7948   (convert (maxmin (convert@2 @0) (convert @1)))
7949    (if (optimize
7950        && FLOAT_TYPE_P (type)
7951        && FLOAT_TYPE_P (TREE_TYPE (@2))
7952        && types_match (type, TREE_TYPE (@0))
7953        && types_match (type, TREE_TYPE (@1))
7954        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7955     (maxmin @0 @1))))
7956 #endif
7958 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7959      tos (XFLOOR XCEIL XROUND XRINT)
7960  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7961  (if (optimize && canonicalize_math_p ())
7962   (simplify
7963    (froms (convert double_value_p@0))
7964    (tos @0))))
7966 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7967             XFLOOR XCEIL XROUND XRINT)
7968      tos (XFLOORF XCEILF XROUNDF XRINTF)
7969  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7970     if x is a float.  */
7971  (if (optimize && canonicalize_math_p ())
7972   (simplify
7973    (froms (convert float_value_p@0))
7974    (tos @0))))
7976 (if (canonicalize_math_p ())
7977  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7978  (for floors (IFLOOR LFLOOR LLFLOOR)
7979   (simplify
7980    (floors tree_expr_nonnegative_p@0)
7981    (fix_trunc @0))))
7983 (if (canonicalize_math_p ())
7984  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7985  (for fns (IFLOOR LFLOOR LLFLOOR
7986            ICEIL LCEIL LLCEIL
7987            IROUND LROUND LLROUND)
7988   (simplify
7989    (fns integer_valued_real_p@0)
7990    (fix_trunc @0)))
7991  (if (!flag_errno_math)
7992   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7993   (for rints (IRINT LRINT LLRINT)
7994    (simplify
7995     (rints integer_valued_real_p@0)
7996     (fix_trunc @0)))))
7998 (if (canonicalize_math_p ())
7999  (for ifn (IFLOOR ICEIL IROUND IRINT)
8000       lfn (LFLOOR LCEIL LROUND LRINT)
8001       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8002   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8003      sizeof (int) == sizeof (long).  */
8004   (if (TYPE_PRECISION (integer_type_node)
8005        == TYPE_PRECISION (long_integer_type_node))
8006    (simplify
8007     (ifn @0)
8008     (lfn:long_integer_type_node @0)))
8009   /* Canonicalize llround (x) to lround (x) on LP64 targets where
8010      sizeof (long long) == sizeof (long).  */
8011   (if (TYPE_PRECISION (long_long_integer_type_node)
8012        == TYPE_PRECISION (long_integer_type_node))
8013    (simplify
8014     (llfn @0)
8015     (lfn:long_integer_type_node @0)))))
8017 /* cproj(x) -> x if we're ignoring infinities.  */
8018 (simplify
8019  (CPROJ @0)
8020  (if (!HONOR_INFINITIES (type))
8021    @0))
8023 /* If the real part is inf and the imag part is known to be
8024    nonnegative, return (inf + 0i).  */
8025 (simplify
8026  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8027  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8028   { build_complex_inf (type, false); }))
8030 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8031 (simplify
8032  (CPROJ (complex @0 REAL_CST@1))
8033  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8034   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8036 (for pows (POW)
8037      sqrts (SQRT)
8038      cbrts (CBRT)
8039  (simplify
8040   (pows @0 REAL_CST@1)
8041   (with {
8042     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8043     REAL_VALUE_TYPE tmp;
8044    }
8045    (switch
8046     /* pow(x,0) -> 1.  */
8047     (if (real_equal (value, &dconst0))
8048      { build_real (type, dconst1); })
8049     /* pow(x,1) -> x.  */
8050     (if (real_equal (value, &dconst1))
8051      @0)
8052     /* pow(x,-1) -> 1/x.  */
8053     (if (real_equal (value, &dconstm1))
8054      (rdiv { build_real (type, dconst1); } @0))
8055     /* pow(x,0.5) -> sqrt(x).  */
8056     (if (flag_unsafe_math_optimizations
8057          && canonicalize_math_p ()
8058          && real_equal (value, &dconsthalf))
8059      (sqrts @0))
8060     /* pow(x,1/3) -> cbrt(x).  */
8061     (if (flag_unsafe_math_optimizations
8062          && canonicalize_math_p ()
8063          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8064              real_equal (value, &tmp)))
8065      (cbrts @0))))))
8067 /* powi(1,x) -> 1.  */
8068 (simplify
8069  (POWI real_onep@0 @1)
8070  @0)
8072 (simplify
8073  (POWI @0 INTEGER_CST@1)
8074  (switch
8075   /* powi(x,0) -> 1.  */
8076   (if (wi::to_wide (@1) == 0)
8077    { build_real (type, dconst1); })
8078   /* powi(x,1) -> x.  */
8079   (if (wi::to_wide (@1) == 1)
8080    @0)
8081   /* powi(x,-1) -> 1/x.  */
8082   (if (wi::to_wide (@1) == -1)
8083    (rdiv { build_real (type, dconst1); } @0))))
8085 /* Narrowing of arithmetic and logical operations.
8087    These are conceptually similar to the transformations performed for
8088    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8089    term we want to move all that code out of the front-ends into here.  */
8091 /* Convert (outertype)((innertype0)a+(innertype1)b)
8092    into ((newtype)a+(newtype)b) where newtype
8093    is the widest mode from all of these.  */
8094 (for op (plus minus mult rdiv)
8095  (simplify
8096    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8097    /* If we have a narrowing conversion of an arithmetic operation where
8098       both operands are widening conversions from the same type as the outer
8099       narrowing conversion.  Then convert the innermost operands to a
8100       suitable unsigned type (to avoid introducing undefined behavior),
8101       perform the operation and convert the result to the desired type.  */
8102    (if (INTEGRAL_TYPE_P (type)
8103         && op != MULT_EXPR
8104         && op != RDIV_EXPR
8105         /* We check for type compatibility between @0 and @1 below,
8106            so there's no need to check that @2/@4 are integral types.  */
8107         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8108         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8109         /* The precision of the type of each operand must match the
8110            precision of the mode of each operand, similarly for the
8111            result.  */
8112         && type_has_mode_precision_p (TREE_TYPE (@1))
8113         && type_has_mode_precision_p (TREE_TYPE (@2))
8114         && type_has_mode_precision_p (type)
8115         /* The inner conversion must be a widening conversion.  */
8116         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8117         && types_match (@1, type)
8118         && (types_match (@1, @2)
8119             /* Or the second operand is const integer or converted const
8120                integer from valueize.  */
8121             || poly_int_tree_p (@4)))
8122      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8123        (op @1 (convert @2))
8124        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8125         (convert (op (convert:utype @1)
8126                      (convert:utype @2)))))
8127      (if (FLOAT_TYPE_P (type)
8128           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8129                == DECIMAL_FLOAT_TYPE_P (type))
8130       (with { tree arg0 = strip_float_extensions (@1);
8131               tree arg1 = strip_float_extensions (@2);
8132               tree itype = TREE_TYPE (@0);
8133               tree ty1 = TREE_TYPE (arg0);
8134               tree ty2 = TREE_TYPE (arg1);
8135               enum tree_code code = TREE_CODE (itype); }
8136         (if (FLOAT_TYPE_P (ty1)
8137              && FLOAT_TYPE_P (ty2))
8138          (with { tree newtype = type;
8139                  if (TYPE_MODE (ty1) == SDmode
8140                      || TYPE_MODE (ty2) == SDmode
8141                      || TYPE_MODE (type) == SDmode)
8142                    newtype = dfloat32_type_node;
8143                  if (TYPE_MODE (ty1) == DDmode
8144                      || TYPE_MODE (ty2) == DDmode
8145                      || TYPE_MODE (type) == DDmode)
8146                    newtype = dfloat64_type_node;
8147                  if (TYPE_MODE (ty1) == TDmode
8148                      || TYPE_MODE (ty2) == TDmode
8149                      || TYPE_MODE (type) == TDmode)
8150                    newtype = dfloat128_type_node; }
8151           (if ((newtype == dfloat32_type_node
8152                 || newtype == dfloat64_type_node
8153                 || newtype == dfloat128_type_node)
8154               && newtype == type
8155               && types_match (newtype, type))
8156             (op (convert:newtype @1) (convert:newtype @2))
8157             (with { if (element_precision (ty1) > element_precision (newtype))
8158                       newtype = ty1;
8159                     if (element_precision (ty2) > element_precision (newtype))
8160                       newtype = ty2; }
8161                /* Sometimes this transformation is safe (cannot
8162                   change results through affecting double rounding
8163                   cases) and sometimes it is not.  If NEWTYPE is
8164                   wider than TYPE, e.g. (float)((long double)double
8165                   + (long double)double) converted to
8166                   (float)(double + double), the transformation is
8167                   unsafe regardless of the details of the types
8168                   involved; double rounding can arise if the result
8169                   of NEWTYPE arithmetic is a NEWTYPE value half way
8170                   between two representable TYPE values but the
8171                   exact value is sufficiently different (in the
8172                   right direction) for this difference to be
8173                   visible in ITYPE arithmetic.  If NEWTYPE is the
8174                   same as TYPE, however, the transformation may be
8175                   safe depending on the types involved: it is safe
8176                   if the ITYPE has strictly more than twice as many
8177                   mantissa bits as TYPE, can represent infinities
8178                   and NaNs if the TYPE can, and has sufficient
8179                   exponent range for the product or ratio of two
8180                   values representable in the TYPE to be within the
8181                   range of normal values of ITYPE.  */
8182               (if (element_precision (newtype) < element_precision (itype)
8183                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8184                        || target_supports_op_p (newtype, op, optab_default))
8185                    && (flag_unsafe_math_optimizations
8186                        || (element_precision (newtype) == element_precision (type)
8187                            && real_can_shorten_arithmetic (element_mode (itype),
8188                                                            element_mode (type))
8189                            && !excess_precision_type (newtype)))
8190                    && !types_match (itype, newtype))
8191                  (convert:type (op (convert:newtype @1)
8192                                    (convert:newtype @2)))
8193          )))) )
8194    ))
8197 /* This is another case of narrowing, specifically when there's an outer
8198    BIT_AND_EXPR which masks off bits outside the type of the innermost
8199    operands.   Like the previous case we have to convert the operands
8200    to unsigned types to avoid introducing undefined behavior for the
8201    arithmetic operation.  */
8202 (for op (minus plus)
8203  (simplify
8204   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8205   (if (INTEGRAL_TYPE_P (type)
8206        /* We check for type compatibility between @0 and @1 below,
8207           so there's no need to check that @1/@3 are integral types.  */
8208        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8209        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8210        /* The precision of the type of each operand must match the
8211           precision of the mode of each operand, similarly for the
8212           result.  */
8213        && type_has_mode_precision_p (TREE_TYPE (@0))
8214        && type_has_mode_precision_p (TREE_TYPE (@1))
8215        && type_has_mode_precision_p (type)
8216        /* The inner conversion must be a widening conversion.  */
8217        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8218        && types_match (@0, @1)
8219        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8220            <= TYPE_PRECISION (TREE_TYPE (@0)))
8221        && (wi::to_wide (@4)
8222            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8223                        true, TYPE_PRECISION (type))) == 0)
8224    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8225     (with { tree ntype = TREE_TYPE (@0); }
8226      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8227     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8228      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8229                (convert:utype @4))))))))
8231 /* Transform (@0 < @1 and @0 < @2) to use min,
8232    (@0 > @1 and @0 > @2) to use max */
8233 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8234      op    (lt      le      gt      ge      lt      le      gt      ge     )
8235      ext   (min     min     max     max     max     max     min     min    )
8236  (simplify
8237   (logic (op:cs @0 @1) (op:cs @0 @2))
8238   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8239        && TREE_CODE (@0) != INTEGER_CST)
8240    (op @0 (ext @1 @2)))))
8242 /* Max<bool0, bool1> -> bool0 | bool1
8243    Min<bool0, bool1> -> bool0 & bool1 */
8244 (for op    (max     min)
8245      logic (bit_ior bit_and)
8246  (simplify
8247   (op zero_one_valued_p@0 zero_one_valued_p@1)
8248   (logic @0 @1)))
8250 /* signbit(x) != 0 ? -x : x -> abs(x)
8251    signbit(x) == 0 ? -x : x -> -abs(x) */
8252 (for sign (SIGNBIT)
8253  (for neeq (ne eq)
8254   (simplify
8255    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8256     (if (neeq == NE_EXPR)
8257      (abs @0)
8258      (negate (abs @0))))))
8260 (simplify
8261  /* signbit(x) -> 0 if x is nonnegative.  */
8262  (SIGNBIT tree_expr_nonnegative_p@0)
8263  { integer_zero_node; })
8265 (simplify
8266  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8267  (SIGNBIT @0)
8268  (if (!HONOR_SIGNED_ZEROS (@0))
8269   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8271 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8272 (for cmp (eq ne)
8273  (for op (plus minus)
8274       rop (minus plus)
8275   (simplify
8276    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8277    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8278         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8279         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8280         && !TYPE_SATURATING (TREE_TYPE (@0)))
8281     (with { tree res = int_const_binop (rop, @2, @1); }
8282      (if (TREE_OVERFLOW (res)
8283           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8284       { constant_boolean_node (cmp == NE_EXPR, type); }
8285       (if (single_use (@3))
8286        (cmp @0 { TREE_OVERFLOW (res)
8287                  ? drop_tree_overflow (res) : res; }))))))))
8288 (for cmp (lt le gt ge)
8289  (for op (plus minus)
8290       rop (minus plus)
8291   (simplify
8292    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8293    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8294         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8295     (with { tree res = int_const_binop (rop, @2, @1); }
8296      (if (TREE_OVERFLOW (res))
8297       {
8298         fold_overflow_warning (("assuming signed overflow does not occur "
8299                                 "when simplifying conditional to constant"),
8300                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8301         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8302         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8303         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8304                                   TYPE_SIGN (TREE_TYPE (@1)))
8305                         != (op == MINUS_EXPR);
8306         constant_boolean_node (less == ovf_high, type);
8307       }
8308       (if (single_use (@3))
8309        (with
8310         {
8311           fold_overflow_warning (("assuming signed overflow does not occur "
8312                                   "when changing X +- C1 cmp C2 to "
8313                                   "X cmp C2 -+ C1"),
8314                                  WARN_STRICT_OVERFLOW_COMPARISON);
8315         }
8316         (cmp @0 { res; })))))))))
8318 /* Canonicalizations of BIT_FIELD_REFs.  */
8320 (simplify
8321  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8322  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8324 (simplify
8325  (BIT_FIELD_REF (view_convert @0) @1 @2)
8326  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8327       || type_has_mode_precision_p (TREE_TYPE (@0)))
8328   (BIT_FIELD_REF @0 @1 @2)))
8330 (simplify
8331  (BIT_FIELD_REF @0 @1 integer_zerop)
8332  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8333   (view_convert @0)))
8335 (simplify
8336  (BIT_FIELD_REF @0 @1 @2)
8337  (switch
8338   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8339        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8340    (switch
8341     (if (integer_zerop (@2))
8342      (view_convert (realpart @0)))
8343     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8344      (view_convert (imagpart @0)))))
8345   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8346        && INTEGRAL_TYPE_P (type)
8347        /* On GIMPLE this should only apply to register arguments.  */
8348        && (! GIMPLE || is_gimple_reg (@0))
8349        /* A bit-field-ref that referenced the full argument can be stripped.  */
8350        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8351             && integer_zerop (@2))
8352            /* Low-parts can be reduced to integral conversions.
8353               ???  The following doesn't work for PDP endian.  */
8354            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8355                /* But only do this after vectorization.  */
8356                && canonicalize_math_after_vectorization_p ()
8357                /* Don't even think about BITS_BIG_ENDIAN.  */
8358                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8359                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8360                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8361                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8362                                             - TYPE_PRECISION (type))
8363                                          : 0)) == 0)))
8364    (convert @0))))
8366 /* Simplify vector extracts.  */
8368 (simplify
8369  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8370  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8371       && tree_fits_uhwi_p (TYPE_SIZE (type))
8372       && ((tree_to_uhwi (TYPE_SIZE (type))
8373            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8374           || (VECTOR_TYPE_P (type)
8375               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8376                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8377   (with
8378    {
8379      tree ctor = (TREE_CODE (@0) == SSA_NAME
8380                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8381      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8382      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8383      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8384      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8385    }
8386    (if (n != 0
8387         && (idx % width) == 0
8388         && (n % width) == 0
8389         && known_le ((idx + n) / width,
8390                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8391     (with
8392      {
8393        idx = idx / width;
8394        n = n / width;
8395        /* Constructor elements can be subvectors.  */
8396        poly_uint64 k = 1;
8397        if (CONSTRUCTOR_NELTS (ctor) != 0)
8398          {
8399            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8400            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8401              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8402          }
8403        unsigned HOST_WIDE_INT elt, count, const_k;
8404      }
8405      (switch
8406       /* We keep an exact subset of the constructor elements.  */
8407       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8408        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8409         { build_zero_cst (type); }
8410         (if (count == 1)
8411          (if (elt < CONSTRUCTOR_NELTS (ctor))
8412           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8413           { build_zero_cst (type); })
8414          /* We don't want to emit new CTORs unless the old one goes away.
8415             ???  Eventually allow this if the CTOR ends up constant or
8416             uniform.  */
8417          (if (single_use (@0))
8418           (with
8419             {
8420               vec<constructor_elt, va_gc> *vals;
8421               vec_alloc (vals, count);
8422               bool constant_p = true;
8423               tree res;
8424               for (unsigned i = 0;
8425                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8426                 {
8427                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8428                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8429                   if (!CONSTANT_CLASS_P (e))
8430                     constant_p = false;
8431                 }
8432               tree evtype = (types_match (TREE_TYPE (type),
8433                                           TREE_TYPE (TREE_TYPE (ctor)))
8434                              ? type
8435                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8436                                                   count * k));
8437               /* We used to build a CTOR in the non-constant case here
8438                  but that's not a GIMPLE value.  We'd have to expose this
8439                  operation somehow so the code generation can properly
8440                  split it out to a separate stmt.  */
8441               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8442                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8443             }
8444             (if (res)
8445              (view_convert { res; })))))))
8446       /* The bitfield references a single constructor element.  */
8447       (if (k.is_constant (&const_k)
8448            && idx + n <= (idx / const_k + 1) * const_k)
8449        (switch
8450         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8451          { build_zero_cst (type); })
8452         (if (n == const_k)
8453          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8454         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8455                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8457 /* Simplify a bit extraction from a bit insertion for the cases with
8458    the inserted element fully covering the extraction or the insertion
8459    not touching the extraction.  */
8460 (simplify
8461  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8462  (with
8463   {
8464     unsigned HOST_WIDE_INT isize;
8465     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8466       isize = TYPE_PRECISION (TREE_TYPE (@1));
8467     else
8468       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8469   }
8470   (switch
8471    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8472          || type_has_mode_precision_p (TREE_TYPE (@1)))
8473         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8474         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8475                       wi::to_wide (@ipos) + isize))
8476     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8477                                                  wi::to_wide (@rpos)
8478                                                  - wi::to_wide (@ipos)); }))
8479    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8480         && compare_tree_int (@rsize, isize) == 0)
8481     (convert @1))
8482    (if (wi::geu_p (wi::to_wide (@ipos),
8483                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8484         || wi::geu_p (wi::to_wide (@rpos),
8485                       wi::to_wide (@ipos) + isize))
8486     (BIT_FIELD_REF @0 @rsize @rpos)))))
8488 /* Simplify vector inserts of other vector extracts to a permute.  */
8489 (simplify
8490  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8491  (if (VECTOR_TYPE_P (type)
8492       && types_match (@0, @1)
8493       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8494       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8495   (with
8496    {
8497      unsigned HOST_WIDE_INT elsz
8498        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8499      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8500      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8501      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8502      vec_perm_builder builder;
8503      builder.new_vector (nunits, nunits, 1);
8504      for (unsigned i = 0; i < nunits; ++i)
8505        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8506      vec_perm_indices sel (builder, 2, nunits);
8507    }
8508    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8509         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8510     (vec_perm @0 @1 { vec_perm_indices_to_tree
8511                         (build_vector_type (ssizetype, nunits), sel); })))))
8513 (if (canonicalize_math_after_vectorization_p ())
8514  (for fmas (FMA)
8515   (simplify
8516    (fmas:c (negate @0) @1 @2)
8517    (IFN_FNMA @0 @1 @2))
8518   (simplify
8519    (fmas @0 @1 (negate @2))
8520    (IFN_FMS @0 @1 @2))
8521   (simplify
8522    (fmas:c (negate @0) @1 (negate @2))
8523    (IFN_FNMS @0 @1 @2))
8524   (simplify
8525    (negate (fmas@3 @0 @1 @2))
8526    (if (single_use (@3))
8527     (IFN_FNMS @0 @1 @2))))
8529  (simplify
8530   (IFN_FMS:c (negate @0) @1 @2)
8531   (IFN_FNMS @0 @1 @2))
8532  (simplify
8533   (IFN_FMS @0 @1 (negate @2))
8534   (IFN_FMA @0 @1 @2))
8535  (simplify
8536   (IFN_FMS:c (negate @0) @1 (negate @2))
8537   (IFN_FNMA @0 @1 @2))
8538  (simplify
8539   (negate (IFN_FMS@3 @0 @1 @2))
8540    (if (single_use (@3))
8541     (IFN_FNMA @0 @1 @2)))
8543  (simplify
8544   (IFN_FNMA:c (negate @0) @1 @2)
8545   (IFN_FMA @0 @1 @2))
8546  (simplify
8547   (IFN_FNMA @0 @1 (negate @2))
8548   (IFN_FNMS @0 @1 @2))
8549  (simplify
8550   (IFN_FNMA:c (negate @0) @1 (negate @2))
8551   (IFN_FMS @0 @1 @2))
8552  (simplify
8553   (negate (IFN_FNMA@3 @0 @1 @2))
8554   (if (single_use (@3))
8555    (IFN_FMS @0 @1 @2)))
8557  (simplify
8558   (IFN_FNMS:c (negate @0) @1 @2)
8559   (IFN_FMS @0 @1 @2))
8560  (simplify
8561   (IFN_FNMS @0 @1 (negate @2))
8562   (IFN_FNMA @0 @1 @2))
8563  (simplify
8564   (IFN_FNMS:c (negate @0) @1 (negate @2))
8565   (IFN_FMA @0 @1 @2))
8566  (simplify
8567   (negate (IFN_FNMS@3 @0 @1 @2))
8568   (if (single_use (@3))
8569    (IFN_FMA @0 @1 @2))))
8571 /* CLZ simplifications.  */
8572 (for clz (CLZ)
8573  (for op (eq ne)
8574       cmp (lt ge)
8575   (simplify
8576    (op (clz:s@2 @0) INTEGER_CST@1)
8577    (if (integer_zerop (@1) && single_use (@2))
8578     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8579     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8580      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8581     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8582     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8583      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8584 (for op (eq ne)
8585      cmp (lt ge)
8586  (simplify
8587   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8588   (if (integer_zerop (@1) && single_use (@2))
8589    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8590    (with { tree type0 = TREE_TYPE (@0);
8591            tree stype = signed_type_for (TREE_TYPE (@0));
8592            /* Punt if clz(0) == 0.  */
8593            if (integer_zerop (@3))
8594              stype = NULL_TREE;
8595          }
8596     (if (stype)
8597      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8598    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8599    (with { bool ok = true;
8600            tree type0 = TREE_TYPE (@0);
8601            /* Punt if clz(0) == prec - 1.  */
8602            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8603              ok = false;
8604          }
8605     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8606      (op @0 { build_one_cst (type0); }))))))
8608 /* CTZ simplifications.  */
8609 (for ctz (CTZ)
8610  (for op (ge gt le lt)
8611       cmp (eq eq ne ne)
8612   (simplify
8613    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8614    (op (ctz:s @0) INTEGER_CST@1)
8615     (with { bool ok = true;
8616             HOST_WIDE_INT val = 0;
8617             if (!tree_fits_shwi_p (@1))
8618               ok = false;
8619             else
8620               {
8621                 val = tree_to_shwi (@1);
8622                 /* Canonicalize to >= or <.  */
8623                 if (op == GT_EXPR || op == LE_EXPR)
8624                   {
8625                     if (val == HOST_WIDE_INT_MAX)
8626                       ok = false;
8627                     else
8628                       val++;
8629                   }
8630               }
8631             tree type0 = TREE_TYPE (@0);
8632             int prec = TYPE_PRECISION (type0);
8633           }
8634      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8635       (if (val <= 0)
8636        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8637        (if (val >= prec)
8638         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8639         (cmp (bit_and @0 { wide_int_to_tree (type0,
8640                                              wi::mask (val, false, prec)); })
8641              { build_zero_cst (type0); })))))))
8642  (for op (eq ne)
8643   (simplify
8644    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8645    (op (ctz:s @0) INTEGER_CST@1)
8646     (with { tree type0 = TREE_TYPE (@0);
8647             int prec = TYPE_PRECISION (type0);
8648           }
8649      (if (prec <= MAX_FIXED_MODE_SIZE)
8650       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8651        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8652        (op (bit_and @0 { wide_int_to_tree (type0,
8653                                            wi::mask (tree_to_uhwi (@1) + 1,
8654                                                      false, prec)); })
8655            { wide_int_to_tree (type0,
8656                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8657                                                  false, prec)); })))))))
8658 (for op (ge gt le lt)
8659      cmp (eq eq ne ne)
8660  (simplify
8661   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8662   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8663    (with { bool ok = true;
8664            HOST_WIDE_INT val = 0;
8665            if (!tree_fits_shwi_p (@1))
8666              ok = false;
8667            else
8668              {
8669                val = tree_to_shwi (@1);
8670                /* Canonicalize to >= or <.  */
8671                if (op == GT_EXPR || op == LE_EXPR)
8672                  {
8673                    if (val == HOST_WIDE_INT_MAX)
8674                      ok = false;
8675                    else
8676                      val++;
8677                  }
8678              }
8679            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8680            tree type0 = TREE_TYPE (@0);
8681            int prec = TYPE_PRECISION (type0);
8682            if (prec > MAX_FIXED_MODE_SIZE)
8683              ok = false;
8684           }
8685      (if (val <= 0)
8686       (if (ok && zero_val >= val)
8687        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8688       (if (val >= prec)
8689        (if (ok && zero_val < val)
8690         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8691        (if (ok && (zero_val < 0 || zero_val >= prec))
8692         (cmp (bit_and @0 { wide_int_to_tree (type0,
8693                                              wi::mask (val, false, prec)); })
8694              { build_zero_cst (type0); })))))))
8695 (for op (eq ne)
8696  (simplify
8697   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8698   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8699    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8700            tree type0 = TREE_TYPE (@0);
8701            int prec = TYPE_PRECISION (type0);
8702          }
8703     (if (prec <= MAX_FIXED_MODE_SIZE)
8704      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8705       (if (zero_val != wi::to_widest (@1))
8706        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8707       (if (zero_val < 0 || zero_val >= prec)
8708        (op (bit_and @0 { wide_int_to_tree (type0,
8709                                            wi::mask (tree_to_uhwi (@1) + 1,
8710                                                      false, prec)); })
8711            { wide_int_to_tree (type0,
8712                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8713                                                  false, prec)); })))))))
8715 #if GIMPLE
8716 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
8717 (simplify
8718   (CTZ (convert@1 @0))
8719   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8720        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8721        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8722    (with { combined_fn cfn = CFN_LAST;
8723            tree type0 = TREE_TYPE (@0);
8724            if (TREE_CODE (type0) == BITINT_TYPE)
8725              {
8726                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8727                  cfn = CFN_CTZ;
8728                else
8729                  type0
8730                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8731                                                      1);
8732              }
8733            type0 = unsigned_type_for (type0);
8734            if (cfn == CFN_LAST
8735                && direct_internal_fn_supported_p (IFN_CTZ, type0,
8736                                                   OPTIMIZE_FOR_BOTH))
8737              cfn = CFN_CTZ;
8738            if (cfn == CFN_LAST
8739                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8740                && !direct_internal_fn_supported_p (IFN_CTZ,
8741                                                    TREE_TYPE (@1),
8742                                                    OPTIMIZE_FOR_BOTH))
8743              {
8744                if (TYPE_PRECISION (type0)
8745                    == TYPE_PRECISION (unsigned_type_node))
8746                  cfn = CFN_BUILT_IN_CTZ;
8747                else if (TYPE_PRECISION (type0)
8748                         == TYPE_PRECISION (long_long_unsigned_type_node))
8749                  cfn = CFN_BUILT_IN_CTZLL;
8750              } }
8751     (if (cfn == CFN_CTZ)
8752      (IFN_CTZ (convert:type0 @0))
8753      (if (cfn == CFN_BUILT_IN_CTZ)
8754       (BUILT_IN_CTZ (convert:type0 @0))
8755       (if (cfn == CFN_BUILT_IN_CTZLL)
8756        (BUILT_IN_CTZLL (convert:type0 @0))))))))
8757 #endif
8759 /* POPCOUNT simplifications.  */
8760 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8761 (simplify
8762   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8763   (if (INTEGRAL_TYPE_P (type)
8764        && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8765                         widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8766            == 0))
8767    (with { tree utype = TREE_TYPE (@0);
8768            if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8769              utype = TREE_TYPE (@1); }
8770     (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8772 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8773 (for popcount (POPCOUNT)
8774   (for cmp (le eq ne gt)
8775        rep (eq eq ne ne)
8776     (simplify
8777       (cmp (popcount @0) integer_zerop)
8778       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8780 /* popcount(bswap(x)) is popcount(x).  */
8781 (for popcount (POPCOUNT)
8782   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8783               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8784     (simplify
8785       (popcount (convert?@0 (bswap:s@1 @2)))
8786       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8787            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8788         (with { tree type0 = TREE_TYPE (@0);
8789                 tree type1 = TREE_TYPE (@1);
8790                 unsigned int prec0 = TYPE_PRECISION (type0);
8791                 unsigned int prec1 = TYPE_PRECISION (type1); }
8792           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8793             (popcount (convert:type0 (convert:type1 @2)))))))))
8795 /* popcount(rotate(X Y)) is popcount(X).  */
8796 (for popcount (POPCOUNT)
8797   (for rot (lrotate rrotate)
8798     (simplify
8799       (popcount (convert?@0 (rot:s@1 @2 @3)))
8800       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8801            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8802            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8803         (with { tree type0 = TREE_TYPE (@0);
8804                 tree type1 = TREE_TYPE (@1);
8805                 unsigned int prec0 = TYPE_PRECISION (type0);
8806                 unsigned int prec1 = TYPE_PRECISION (type1); }
8807           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8808             (popcount (convert:type0 @2))))))))
8810 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8811 (simplify
8812   (bit_and (POPCOUNT @0) integer_onep)
8813   (PARITY @0))
8815 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8816 (simplify
8817   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8818   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8820 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8821 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8822 (for popcount (POPCOUNT)
8823   (for log1 (bit_and bit_ior)
8824        log2 (bit_ior bit_and)
8825     (simplify
8826       (minus (plus:s (popcount:s @0) (popcount:s @1))
8827              (popcount:s (log1:cs @0 @1)))
8828       (popcount (log2 @0 @1)))
8829     (simplify
8830       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8831               (popcount:s @1))
8832       (popcount (log2 @0 @1)))))
8834 #if GIMPLE
8835 /* popcount(zext(X)) == popcount(X).  */
8836 (simplify
8837   (POPCOUNT (convert@1 @0))
8838   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8839        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8840        && TYPE_UNSIGNED (TREE_TYPE (@0))
8841        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8842    (with { combined_fn cfn = CFN_LAST;
8843            tree type0 = TREE_TYPE (@0);
8844            if (TREE_CODE (type0) == BITINT_TYPE)
8845              {
8846                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8847                  cfn = CFN_POPCOUNT;
8848                else
8849                  type0
8850                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8851                                                      1);
8852              }
8853            if (cfn == CFN_LAST
8854                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8855                                                   OPTIMIZE_FOR_BOTH))
8856              cfn = CFN_POPCOUNT;
8857            if (cfn == CFN_LAST
8858                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8859                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8860                                                    TREE_TYPE (@1),
8861                                                    OPTIMIZE_FOR_BOTH))
8862              {
8863                if (TYPE_PRECISION (type0)
8864                    == TYPE_PRECISION (unsigned_type_node))
8865                  cfn = CFN_BUILT_IN_POPCOUNT;
8866                else if (TYPE_PRECISION (type0)
8867                         == TYPE_PRECISION (long_long_unsigned_type_node))
8868                  cfn = CFN_BUILT_IN_POPCOUNTLL;
8869              } }
8870     (if (cfn == CFN_POPCOUNT)
8871      (IFN_POPCOUNT (convert:type0 @0))
8872      (if (cfn == CFN_BUILT_IN_POPCOUNT)
8873       (BUILT_IN_POPCOUNT (convert:type0 @0))
8874       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8875        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8876 #endif
8878 /* PARITY simplifications.  */
8879 /* parity(~X) is parity(X).  */
8880 (simplify
8881   (PARITY (bit_not @0))
8882   (PARITY @0))
8884 /* parity(bswap(x)) is parity(x).  */
8885 (for parity (PARITY)
8886   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8887               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8888     (simplify
8889       (parity (convert?@0 (bswap:s@1 @2)))
8890       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8891            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8892            && TYPE_PRECISION (TREE_TYPE (@0))
8893               >= TYPE_PRECISION (TREE_TYPE (@1)))
8894         (with { tree type0 = TREE_TYPE (@0);
8895                 tree type1 = TREE_TYPE (@1); }
8896           (parity (convert:type0 (convert:type1 @2))))))))
8898 /* parity(rotate(X Y)) is parity(X).  */
8899 (for parity (PARITY)
8900   (for rot (lrotate rrotate)
8901     (simplify
8902       (parity (convert?@0 (rot:s@1 @2 @3)))
8903       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8904            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8905            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8906            && TYPE_PRECISION (TREE_TYPE (@0))
8907               >= TYPE_PRECISION (TREE_TYPE (@1)))
8908         (with { tree type0 = TREE_TYPE (@0); }
8909           (parity (convert:type0 @2)))))))
8911 /* parity(X)^parity(Y) is parity(X^Y).  */
8912 (simplify
8913   (bit_xor (PARITY:s @0) (PARITY:s @1))
8914   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8915    (PARITY (bit_xor @0 @1))
8916    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8917         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8918     (with { tree utype = TREE_TYPE (@0);
8919             if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8920               utype = TREE_TYPE (@1); }
8921      (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
8923 #if GIMPLE
8924 /* parity(zext(X)) == parity(X).  */
8925 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
8926 (simplify
8927   (PARITY (convert@1 @0))
8928   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8929        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8930        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8931        && (TYPE_UNSIGNED (TREE_TYPE (@0))
8932            || ((TYPE_PRECISION (TREE_TYPE (@1))
8933                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8934    (with { combined_fn cfn = CFN_LAST;
8935            tree type0 = TREE_TYPE (@0);
8936            if (TREE_CODE (type0) == BITINT_TYPE)
8937              {
8938                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8939                  cfn = CFN_PARITY;
8940                else
8941                  type0
8942                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8943                                                      1);
8944              }
8945            type0 = unsigned_type_for (type0);
8946            if (cfn == CFN_LAST
8947                && direct_internal_fn_supported_p (IFN_PARITY, type0,
8948                                                   OPTIMIZE_FOR_BOTH))
8949              cfn = CFN_PARITY;
8950            if (cfn == CFN_LAST
8951                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8952                && !direct_internal_fn_supported_p (IFN_PARITY,
8953                                                    TREE_TYPE (@1),
8954                                                    OPTIMIZE_FOR_BOTH))
8955              {
8956                if (TYPE_PRECISION (type0)
8957                    == TYPE_PRECISION (unsigned_type_node))
8958                  cfn = CFN_BUILT_IN_PARITY;
8959                else if (TYPE_PRECISION (type0)
8960                         == TYPE_PRECISION (long_long_unsigned_type_node))
8961                  cfn = CFN_BUILT_IN_PARITYLL;
8962              } }
8963     (if (cfn == CFN_PARITY)
8964      (IFN_PARITY (convert:type0 @0))
8965      (if (cfn == CFN_BUILT_IN_PARITY)
8966       (BUILT_IN_PARITY (convert:type0 @0))
8967       (if (cfn == CFN_BUILT_IN_PARITYLL)
8968        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
8969 #endif
8971 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8972 (for func (POPCOUNT BSWAP FFS PARITY)
8973  (simplify
8974   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8975   @3))
8977 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8978    where CST is precision-1. */
8979 (for func (CLRSB)
8980  (simplify
8981   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8982   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8983    @4)))
8985 #if GIMPLE
8986 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8987 (for func (CLZ)
8988  (simplify
8989   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8990   (with { int val;
8991           internal_fn ifn = IFN_LAST;
8992           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8993             {
8994               if (tree_fits_shwi_p (@2))
8995                 {
8996                   HOST_WIDE_INT valw = tree_to_shwi (@2);
8997                   if ((int) valw == valw)
8998                     {
8999                       val = valw;
9000                       ifn = IFN_CLZ;
9001                     }
9002                 }
9003             }
9004           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9005                                                    OPTIMIZE_FOR_BOTH)
9006                    && CLZ_DEFINED_VALUE_AT_ZERO
9007                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9008             ifn = IFN_CLZ;
9009         }
9010    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9011     (IFN_CLZ @3 @2)))))
9012 (simplify
9013  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9014   (with { int val;
9015           internal_fn ifn = IFN_LAST;
9016           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9017             ifn = IFN_CLZ;
9018           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9019                                                    OPTIMIZE_FOR_BOTH))
9020             ifn = IFN_CLZ;
9021         }
9022    (if (ifn == IFN_CLZ)
9023     (IFN_CLZ @3 @2))))
9025 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9026 (for func (CTZ)
9027  (simplify
9028   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9029   (with { int val;
9030           internal_fn ifn = IFN_LAST;
9031           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9032             {
9033               if (tree_fits_shwi_p (@2))
9034                 {
9035                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9036                   if ((int) valw == valw)
9037                     {
9038                       val = valw;
9039                       ifn = IFN_CTZ;
9040                     }
9041                 }
9042             }
9043           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9044                                                    OPTIMIZE_FOR_BOTH)
9045                    && CTZ_DEFINED_VALUE_AT_ZERO
9046                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9047             ifn = IFN_CTZ;
9048         }
9049    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9050     (IFN_CTZ @3 @2)))))
9051 (simplify
9052  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9053   (with { int val;
9054           internal_fn ifn = IFN_LAST;
9055           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9056             ifn = IFN_CTZ;
9057           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9058                                                    OPTIMIZE_FOR_BOTH))
9059             ifn = IFN_CTZ;
9060         }
9061    (if (ifn == IFN_CTZ)
9062     (IFN_CTZ @3 @2))))
9063 #endif
9065 /* Common POPCOUNT/PARITY simplifications.  */
9066 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9067 (for pfun (POPCOUNT PARITY)
9068   (simplify
9069     (pfun @0)
9070     (if (INTEGRAL_TYPE_P (type))
9071      (with { wide_int nz = tree_nonzero_bits (@0); }
9072        (switch
9073          (if (nz == 1)
9074            (convert @0))
9075          (if (wi::popcount (nz) == 1)
9076            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9077              (convert (rshift:utype (convert:utype @0)
9078                                     { build_int_cst (integer_type_node,
9079                                                      wi::ctz (nz)); })))))))))
9081 #if GIMPLE
9082 /* 64- and 32-bits branchless implementations of popcount are detected:
9084    int popcount64c (uint64_t x)
9085    {
9086      x -= (x >> 1) & 0x5555555555555555ULL;
9087      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9088      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9089      return (x * 0x0101010101010101ULL) >> 56;
9090    }
9092    int popcount32c (uint32_t x)
9093    {
9094      x -= (x >> 1) & 0x55555555;
9095      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9096      x = (x + (x >> 4)) & 0x0f0f0f0f;
9097      return (x * 0x01010101) >> 24;
9098    }  */
9099 (simplify
9100  (rshift
9101   (mult
9102    (bit_and
9103     (plus:c
9104      (rshift @8 INTEGER_CST@5)
9105       (plus:c@8
9106        (bit_and @6 INTEGER_CST@7)
9107         (bit_and
9108          (rshift
9109           (minus@6 @0
9110            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9111           INTEGER_CST@10)
9112          INTEGER_CST@9)))
9113     INTEGER_CST@3)
9114    INTEGER_CST@2)
9115   INTEGER_CST@1)
9116   /* Check constants and optab.  */
9117   (with { unsigned prec = TYPE_PRECISION (type);
9118           int shift = (64 - prec) & 63;
9119           unsigned HOST_WIDE_INT c1
9120             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9121           unsigned HOST_WIDE_INT c2
9122             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9123           unsigned HOST_WIDE_INT c3
9124             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9125           unsigned HOST_WIDE_INT c4
9126             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9127    }
9128    (if (prec >= 16
9129         && prec <= 64
9130         && pow2p_hwi (prec)
9131         && TYPE_UNSIGNED (type)
9132         && integer_onep (@4)
9133         && wi::to_widest (@10) == 2
9134         && wi::to_widest (@5) == 4
9135         && wi::to_widest (@1) == prec - 8
9136         && tree_to_uhwi (@2) == c1
9137         && tree_to_uhwi (@3) == c2
9138         && tree_to_uhwi (@9) == c3
9139         && tree_to_uhwi (@7) == c3
9140         && tree_to_uhwi (@11) == c4)
9141     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9142                                          OPTIMIZE_FOR_BOTH))
9143      (convert (IFN_POPCOUNT:type @0))
9144      /* Try to do popcount in two halves.  PREC must be at least
9145         five bits for this to work without extension before adding.  */
9146      (with {
9147        tree half_type = NULL_TREE;
9148        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9149        int half_prec = 8;
9150        if (m.exists ()
9151            && m.require () != TYPE_MODE (type))
9152          {
9153            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9154            half_type = build_nonstandard_integer_type (half_prec, 1);
9155          }
9156        gcc_assert (half_prec > 2);
9157       }
9158       (if (half_type != NULL_TREE
9159            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9160                                               OPTIMIZE_FOR_BOTH))
9161        (convert (plus
9162          (IFN_POPCOUNT:half_type (convert @0))
9163          (IFN_POPCOUNT:half_type (convert (rshift @0
9164             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9166 /* __builtin_ffs needs to deal on many targets with the possible zero
9167    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9168    should lead to better code.  */
9169 (simplify
9170  (FFS tree_expr_nonzero_p@0)
9171  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9172       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9173                                          OPTIMIZE_FOR_SPEED))
9174   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9175    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9176 #endif
9178 (for ffs (FFS)
9179  /* __builtin_ffs (X) == 0 -> X == 0.
9180     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9181  (for cmp (eq ne)
9182   (simplify
9183    (cmp (ffs@2 @0) INTEGER_CST@1)
9184     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9185      (switch
9186       (if (integer_zerop (@1))
9187        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9188       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9189        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9190       (if (single_use (@2))
9191        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9192                                             wi::mask (tree_to_uhwi (@1),
9193                                                       false, prec)); })
9194             { wide_int_to_tree (TREE_TYPE (@0),
9195                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9196                                                   false, prec)); }))))))
9198  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9199  (for cmp (gt le)
9200       cmp2 (ne eq)
9201       cmp3 (eq ne)
9202       bit_op (bit_and bit_ior)
9203   (simplify
9204    (cmp (ffs@2 @0) INTEGER_CST@1)
9205     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9206      (switch
9207       (if (integer_zerop (@1))
9208        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9209       (if (tree_int_cst_sgn (@1) < 0)
9210        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9211       (if (wi::to_widest (@1) >= prec)
9212        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9213       (if (wi::to_widest (@1) == prec - 1)
9214        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9215                                     wi::shifted_mask (prec - 1, 1,
9216                                                       false, prec)); }))
9217       (if (single_use (@2))
9218        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9219                (cmp3 (bit_and @0
9220                               { wide_int_to_tree (TREE_TYPE (@0),
9221                                                   wi::mask (tree_to_uhwi (@1),
9222                                                   false, prec)); })
9223                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9225 #if GIMPLE
9226 /* ffs(ext(X)) == ffs(X).  */
9227 (simplify
9228   (FFS (convert@1 @0))
9229   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9230        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9231        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9232    (with { combined_fn cfn = CFN_LAST;
9233            tree type0 = TREE_TYPE (@0);
9234            if (TREE_CODE (type0) == BITINT_TYPE)
9235              {
9236                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9237                  cfn = CFN_FFS;
9238                else
9239                  type0
9240                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9241                                                      0);
9242              }
9243            type0 = signed_type_for (type0);
9244            if (cfn == CFN_LAST
9245                && direct_internal_fn_supported_p (IFN_FFS, type0,
9246                                                   OPTIMIZE_FOR_BOTH))
9247              cfn = CFN_FFS;
9248            if (cfn == CFN_LAST
9249                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9250                && !direct_internal_fn_supported_p (IFN_FFS,
9251                                                    TREE_TYPE (@1),
9252                                                    OPTIMIZE_FOR_BOTH))
9253              {
9254                if (TYPE_PRECISION (type0)
9255                    == TYPE_PRECISION (integer_type_node))
9256                  cfn = CFN_BUILT_IN_FFS;
9257                else if (TYPE_PRECISION (type0)
9258                         == TYPE_PRECISION (long_long_integer_type_node))
9259                  cfn = CFN_BUILT_IN_FFSLL;
9260              } }
9261     (if (cfn == CFN_FFS)
9262      (IFN_FFS (convert:type0 @0))
9263      (if (cfn == CFN_BUILT_IN_FFS)
9264       (BUILT_IN_FFS (convert:type0 @0))
9265       (if (cfn == CFN_BUILT_IN_FFSLL)
9266        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9267 #endif
9269 #if GIMPLE
9271 /* Simplify:
9272      a = op a1
9273      r = cond ? a : b
9274      --> r = .COND_FN (cond, a, b)
9275 and,
9276     a = op a1
9277     r = cond ? b : a
9278     --> r = .COND_FN (~cond, b, a).  */
9280 (for uncond_op (UNCOND_UNARY)
9281      cond_op (COND_UNARY)
9282  (simplify
9283   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9284    (with { tree op_type = TREE_TYPE (@3); }
9285     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9286         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9287      (cond_op @0 (view_convert @1) @2))))
9288  (simplify
9289   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9290    (with { tree op_type = TREE_TYPE (@3); }
9291     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9292         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9293      (cond_op (bit_not @0) (view_convert @2) @1)))))
9295 (for uncond_op (UNCOND_UNARY)
9296      cond_op (COND_LEN_UNARY)
9297  (simplify
9298   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9299    (with { tree op_type = TREE_TYPE (@3); }
9300     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9301         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9302      (cond_op @0 (view_convert @1) @2 @4 @5))))
9303  (simplify
9304   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9305    (with { tree op_type = TREE_TYPE (@3); }
9306     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9307         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9308      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9310 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9311 (simplify
9312  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9313  (if (canonicalize_math_after_vectorization_p ()
9314       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9315       && is_truth_type_for (type, TREE_TYPE (@0)))
9316  (if (integer_all_onesp (@1) && integer_zerop (@2))
9317   (IFN_COND_NOT @0 @3 @3))
9318   (if (integer_all_onesp (@2) && integer_zerop (@1))
9319    (IFN_COND_NOT (bit_not @0) @3 @3))))
9321 /* Simplify:
9323      a = a1 op a2
9324      r = c ? a : b;
9326    to:
9328      r = c ? a1 op a2 : b;
9330    if the target can do it in one go.  This makes the operation conditional
9331    on c, so could drop potentially-trapping arithmetic, but that's a valid
9332    simplification if the result of the operation isn't needed.
9334    Avoid speculatively generating a stand-alone vector comparison
9335    on targets that might not support them.  Any target implementing
9336    conditional internal functions must support the same comparisons
9337    inside and outside a VEC_COND_EXPR.  */
9339 (for uncond_op (UNCOND_BINARY)
9340      cond_op (COND_BINARY)
9341  (simplify
9342   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9343   (with { tree op_type = TREE_TYPE (@4); }
9344    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9345         && is_truth_type_for (op_type, TREE_TYPE (@0))
9346         && single_use (@4))
9347     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9348  (simplify
9349   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9350   (with { tree op_type = TREE_TYPE (@4); }
9351    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9352         && is_truth_type_for (op_type, TREE_TYPE (@0))
9353         && single_use (@4))
9354     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9356 (for uncond_op (UNCOND_BINARY)
9357      cond_op (COND_LEN_BINARY)
9358  (simplify
9359   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9360   (with { tree op_type = TREE_TYPE (@4); }
9361    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9362         && is_truth_type_for (op_type, TREE_TYPE (@0))
9363         && single_use (@4))
9364     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9365  (simplify
9366   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9367   (with { tree op_type = TREE_TYPE (@4); }
9368    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9369         && is_truth_type_for (op_type, TREE_TYPE (@0))
9370         && single_use (@4))
9371     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9373 /* Same for ternary operations.  */
9374 (for uncond_op (UNCOND_TERNARY)
9375      cond_op (COND_TERNARY)
9376  (simplify
9377   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9378   (with { tree op_type = TREE_TYPE (@5); }
9379    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9380         && is_truth_type_for (op_type, TREE_TYPE (@0))
9381         && single_use (@5))
9382     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9383  (simplify
9384   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9385   (with { tree op_type = TREE_TYPE (@5); }
9386    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9387         && is_truth_type_for (op_type, TREE_TYPE (@0))
9388         && single_use (@5))
9389     (view_convert (cond_op (bit_not @0) @2 @3 @4
9390                   (view_convert:op_type @1)))))))
9392 (for uncond_op (UNCOND_TERNARY)
9393      cond_op (COND_LEN_TERNARY)
9394  (simplify
9395   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9396   (with { tree op_type = TREE_TYPE (@5); }
9397    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9398         && is_truth_type_for (op_type, TREE_TYPE (@0))
9399         && single_use (@5))
9400     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9401  (simplify
9402   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9403   (with { tree op_type = TREE_TYPE (@5); }
9404    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9405         && is_truth_type_for (op_type, TREE_TYPE (@0))
9406         && single_use (@5))
9407     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9408 #endif
9410 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9411    "else" value of an IFN_COND_*.  */
9412 (for cond_op (COND_BINARY)
9413  (simplify
9414   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9415   (with { tree op_type = TREE_TYPE (@3); }
9416    (if (element_precision (type) == element_precision (op_type))
9417     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9418  (simplify
9419   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9420   (with { tree op_type = TREE_TYPE (@5); }
9421    (if (inverse_conditions_p (@0, @2)
9422         && element_precision (type) == element_precision (op_type))
9423     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9425 /* Same for ternary operations.  */
9426 (for cond_op (COND_TERNARY)
9427  (simplify
9428   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9429   (with { tree op_type = TREE_TYPE (@4); }
9430    (if (element_precision (type) == element_precision (op_type))
9431     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9432  (simplify
9433   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9434   (with { tree op_type = TREE_TYPE (@6); }
9435    (if (inverse_conditions_p (@0, @2)
9436         && element_precision (type) == element_precision (op_type))
9437     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9439 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9440    "else" value of an IFN_COND_LEN_*.  */
9441 (for cond_len_op (COND_LEN_BINARY)
9442  (simplify
9443   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9444   (with { tree op_type = TREE_TYPE (@3); }
9445    (if (element_precision (type) == element_precision (op_type))
9446     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9447  (simplify
9448   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9449   (with { tree op_type = TREE_TYPE (@5); }
9450    (if (inverse_conditions_p (@0, @2)
9451         && element_precision (type) == element_precision (op_type))
9452     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9454 /* Same for ternary operations.  */
9455 (for cond_len_op (COND_LEN_TERNARY)
9456  (simplify
9457   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9458   (with { tree op_type = TREE_TYPE (@4); }
9459    (if (element_precision (type) == element_precision (op_type))
9460     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9461  (simplify
9462   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9463   (with { tree op_type = TREE_TYPE (@6); }
9464    (if (inverse_conditions_p (@0, @2)
9465         && element_precision (type) == element_precision (op_type))
9466     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9468 /* Detect simplication for a conditional reduction where
9470    a = mask1 ? b : 0
9471    c = mask2 ? d + a : d
9473    is turned into
9475    c = mask1 && mask2 ? d + b : d.  */
9476 (simplify
9477   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9478    (if (ANY_INTEGRAL_TYPE_P (type)
9479         || (FLOAT_TYPE_P (type)
9480             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9481    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9483 /* Detect simplication for a conditional length reduction where
9485    a = mask ? b : 0
9486    c = i < len + bias ? d + a : d
9488    is turned into
9490    c = mask && i < len + bias ? d + b : d.  */
9491 (simplify
9492   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9493    (if (ANY_INTEGRAL_TYPE_P (type)
9494         || (FLOAT_TYPE_P (type)
9495             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9496     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9498 /* Detect simplification for vector condition folding where
9500   c = mask1 ? (masked_op mask2 a b) : b
9502   into
9504   c = masked_op (mask1 & mask2) a b
9506   where the operation can be partially applied to one operand. */
9508 (for cond_op (COND_BINARY)
9509  (simplify
9510   (vec_cond @0
9511    (cond_op:s @1 @2 @3 @4) @3)
9512   (cond_op (bit_and @1 @0) @2 @3 @4)))
9514 /* And same for ternary expressions.  */
9516 (for cond_op (COND_TERNARY)
9517  (simplify
9518   (vec_cond @0
9519    (cond_op:s @1 @2 @3 @4 @5) @4)
9520   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9522 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9523    expressions like:
9525    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9526    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9528    If pointers are known not to wrap, B checks whether @1 bytes starting
9529    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9530    bytes.  A is more efficiently tested as:
9532    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9534    The equivalent expression for B is given by replacing @1 with @1 - 1:
9536    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9538    @0 and @2 can be swapped in both expressions without changing the result.
9540    The folds rely on sizetype's being unsigned (which is always true)
9541    and on its being the same width as the pointer (which we have to check).
9543    The fold replaces two pointer_plus expressions, two comparisons and
9544    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9545    the best case it's a saving of two operations.  The A fold retains one
9546    of the original pointer_pluses, so is a win even if both pointer_pluses
9547    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9548    used elsewhere, since all we end up doing is replacing a comparison with
9549    a pointer_plus.  We do still apply the fold under those circumstances
9550    though, in case applying it to other conditions eventually makes one of the
9551    pointer_pluses dead.  */
9552 (for ior (truth_orif truth_or bit_ior)
9553  (for cmp (le lt)
9554   (simplify
9555    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9556         (cmp:cs (pointer_plus@4 @2 @1) @0))
9557    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9558         && TYPE_OVERFLOW_WRAPS (sizetype)
9559         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9560     /* Calculate the rhs constant.  */
9561     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9562             offset_int rhs = off * 2; }
9563      /* Always fails for negative values.  */
9564      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9565       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9566          pick a canonical order.  This increases the chances of using the
9567          same pointer_plus in multiple checks.  */
9568       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9569               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9570        (if (cmp == LT_EXPR)
9571         (gt (convert:sizetype
9572              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9573                                      { swap_p ? @0 : @2; }))
9574             { rhs_tree; })
9575         (gt (convert:sizetype
9576              (pointer_diff:ssizetype
9577               (pointer_plus { swap_p ? @2 : @0; }
9578                             { wide_int_to_tree (sizetype, off); })
9579               { swap_p ? @0 : @2; }))
9580             { rhs_tree; })))))))))
9582 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9583    element of @1.  */
9584 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9585  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9586   (with { int i = single_nonzero_element (@1); }
9587    (if (i >= 0)
9588     (with { tree elt = vector_cst_elt (@1, i);
9589             tree elt_type = TREE_TYPE (elt);
9590             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9591             tree size = bitsize_int (elt_bits);
9592             tree pos = bitsize_int (elt_bits * i); }
9593      (view_convert
9594       (bit_and:elt_type
9595        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9596        { elt; })))))))
9598 /* Fold reduction of a single nonzero element constructor.  */
9599 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9600   (simplify (reduc (CONSTRUCTOR@0))
9601     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9602                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9603             tree elt = ctor_single_nonzero_element (ctor); }
9604       (if (elt
9605            && !HONOR_SNANS (type)
9606            && !HONOR_SIGNED_ZEROS (type))
9607         { elt; }))))
9609 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9610 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9611             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9612      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9613   (simplify (reduc (op @0 VECTOR_CST@1))
9614     (op (reduc:type @0) (reduc:type @1))))
9616 /* Simplify vector floating point operations of alternating sub/add pairs
9617    into using an fneg of a wider element type followed by a normal add.
9618    under IEEE 754 the fneg of the wider type will negate every even entry
9619    and when doing an add we get a sub of the even and add of every odd
9620    elements.  */
9621 (for plusminus (plus minus)
9622      minusplus (minus plus)
9623  (simplify
9624   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9625    (if (!VECTOR_INTEGER_TYPE_P (type)
9626         && !FLOAT_WORDS_BIG_ENDIAN
9627         /* plus is commutative, while minus is not, so :c can't be used.
9628            Do equality comparisons by hand and at the end pick the operands
9629            from the minus.  */
9630         && (operand_equal_p (@0, @2, 0)
9631             ? operand_equal_p (@1, @3, 0)
9632             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9633    (with
9634     {
9635       /* Build a vector of integers from the tree mask.  */
9636       vec_perm_builder builder;
9637     }
9638     (if (tree_to_vec_perm_builder (&builder, @4))
9639      (with
9640       {
9641         /* Create a vec_perm_indices for the integer vector.  */
9642         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9643         vec_perm_indices sel (builder, 2, nelts);
9644         machine_mode vec_mode = TYPE_MODE (type);
9645         machine_mode wide_mode;
9646         scalar_mode wide_elt_mode;
9647         poly_uint64 wide_nunits;
9648         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9649       }
9650       (if (VECTOR_MODE_P (vec_mode)
9651            && sel.series_p (0, 2, 0, 2)
9652            && sel.series_p (1, 2, nelts + 1, 2)
9653            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9654            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9655            && related_vector_mode (vec_mode, wide_elt_mode,
9656                                    wide_nunits).exists (&wide_mode))
9657        (with
9658         {
9659           tree stype
9660             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9661                                               TYPE_UNSIGNED (type));
9662           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9664           /* The format has to be a non-extended ieee format.  */
9665           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9666           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9667         }
9668         (if (TYPE_MODE (stype) != BLKmode
9669              && VECTOR_TYPE_P (ntype)
9670              && fmt_old != NULL
9671              && fmt_new != NULL)
9672          (with
9673           {
9674             /* If the target doesn't support v1xx vectors, try using
9675                scalar mode xx instead.  */
9676             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9677                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9678               ntype = stype;
9679           }
9680           (if (fmt_new->signbit_rw
9681                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9682                && fmt_new->signbit_rw == fmt_new->signbit_ro
9683                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9684                                                  TYPE_MODE (type), ALL_REGS)
9685                && ((optimize_vectors_before_lowering_p ()
9686                     && VECTOR_TYPE_P (ntype))
9687                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9688            (if (plusminus == PLUS_EXPR)
9689             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9690             (minus @0 (view_convert:type
9691                         (negate (view_convert:ntype @1))))))))))))))))
9693 (simplify
9694  (vec_perm @0 @1 VECTOR_CST@2)
9695  (with
9696   {
9697     tree op0 = @0, op1 = @1, op2 = @2;
9698     machine_mode result_mode = TYPE_MODE (type);
9699     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9701     /* Build a vector of integers from the tree mask.  */
9702     vec_perm_builder builder;
9703   }
9704   (if (tree_to_vec_perm_builder (&builder, op2))
9705    (with
9706     {
9707       /* Create a vec_perm_indices for the integer vector.  */
9708       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9709       bool single_arg = (op0 == op1);
9710       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9711     }
9712     (if (sel.series_p (0, 1, 0, 1))
9713      { op0; }
9714      (if (sel.series_p (0, 1, nelts, 1))
9715       { op1; }
9716       (with
9717        {
9718          if (!single_arg)
9719            {
9720              if (sel.all_from_input_p (0))
9721                op1 = op0;
9722              else if (sel.all_from_input_p (1))
9723                {
9724                  op0 = op1;
9725                  sel.rotate_inputs (1);
9726                }
9727              else if (known_ge (poly_uint64 (sel[0]), nelts))
9728                {
9729                  std::swap (op0, op1);
9730                  sel.rotate_inputs (1);
9731                }
9732            }
9733          gassign *def;
9734          tree cop0 = op0, cop1 = op1;
9735          if (TREE_CODE (op0) == SSA_NAME
9736              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9737              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9738            cop0 = gimple_assign_rhs1 (def);
9739          if (TREE_CODE (op1) == SSA_NAME
9740              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9741              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9742            cop1 = gimple_assign_rhs1 (def);
9743          tree t;
9744        }
9745        (if ((TREE_CODE (cop0) == VECTOR_CST
9746              || TREE_CODE (cop0) == CONSTRUCTOR)
9747             && (TREE_CODE (cop1) == VECTOR_CST
9748                 || TREE_CODE (cop1) == CONSTRUCTOR)
9749             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9750         { t; }
9751         (with
9752          {
9753            bool changed = (op0 == op1 && !single_arg);
9754            tree ins = NULL_TREE;
9755            unsigned at = 0;
9757            /* See if the permutation is performing a single element
9758               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9759               in that case.  But only if the vector mode is supported,
9760               otherwise this is invalid GIMPLE.  */
9761            if (op_mode != BLKmode
9762                && (TREE_CODE (cop0) == VECTOR_CST
9763                    || TREE_CODE (cop0) == CONSTRUCTOR
9764                    || TREE_CODE (cop1) == VECTOR_CST
9765                    || TREE_CODE (cop1) == CONSTRUCTOR))
9766              {
9767                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9768                if (insert_first_p)
9769                  {
9770                    /* After canonicalizing the first elt to come from the
9771                       first vector we only can insert the first elt from
9772                       the first vector.  */
9773                    at = 0;
9774                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9775                      op0 = op1;
9776                  }
9777                /* The above can fail for two-element vectors which always
9778                   appear to insert the first element, so try inserting
9779                   into the second lane as well.  For more than two
9780                   elements that's wasted time.  */
9781                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9782                  {
9783                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9784                    for (at = 0; at < encoded_nelts; ++at)
9785                      if (maybe_ne (sel[at], at))
9786                        break;
9787                    if (at < encoded_nelts
9788                        && (known_eq (at + 1, nelts)
9789                            || sel.series_p (at + 1, 1, at + 1, 1)))
9790                      {
9791                        if (known_lt (poly_uint64 (sel[at]), nelts))
9792                          ins = fold_read_from_vector (cop0, sel[at]);
9793                        else
9794                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9795                      }
9796                  }
9797              }
9799            /* Generate a canonical form of the selector.  */
9800            if (!ins && sel.encoding () != builder)
9801              {
9802                /* Some targets are deficient and fail to expand a single
9803                   argument permutation while still allowing an equivalent
9804                   2-argument version.  */
9805                tree oldop2 = op2;
9806                if (sel.ninputs () == 2
9807                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9808                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9809                else
9810                  {
9811                    vec_perm_indices sel2 (builder, 2, nelts);
9812                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9813                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9814                    else
9815                      /* Not directly supported with either encoding,
9816                         so use the preferred form.  */
9817                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9818                  }
9819                if (!operand_equal_p (op2, oldop2, 0))
9820                  changed = true;
9821              }
9822          }
9823          (if (ins)
9824           (bit_insert { op0; } { ins; }
9825            { bitsize_int (at * vector_element_bits (type)); })
9826           (if (changed)
9827            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9829 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9831 (match vec_same_elem_p
9832  (vec_duplicate @0))
9834 (match vec_same_elem_p
9835  CONSTRUCTOR@0
9836  (if (TREE_CODE (@0) == SSA_NAME
9837       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9839 (match vec_same_elem_p
9840  @0
9841  (if (uniform_vector_p (@0))))
9844 (simplify
9845  (vec_perm vec_same_elem_p@0 @0 @1)
9846  (if (types_match (type, TREE_TYPE (@0)))
9847   @0
9848   (with
9849    {
9850      tree elem = uniform_vector_p (@0);
9851    }
9852    (if (elem)
9853     { build_vector_from_val (type, elem); }))))
9855 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9856 (simplify
9857  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9858  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9859   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9860 (simplify
9861  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9862  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9863   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9866 /* Merge
9867      c = VEC_PERM_EXPR <a, b, VCST0>;
9868      d = VEC_PERM_EXPR <c, c, VCST1>;
9869    to
9870      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9872 (simplify
9873  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9874  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9875   (with
9876    {
9877      machine_mode result_mode = TYPE_MODE (type);
9878      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9879      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9880      vec_perm_builder builder0;
9881      vec_perm_builder builder1;
9882      vec_perm_builder builder2 (nelts, nelts, 1);
9883    }
9884    (if (tree_to_vec_perm_builder (&builder0, @3)
9885         && tree_to_vec_perm_builder (&builder1, @4))
9886     (with
9887      {
9888        vec_perm_indices sel0 (builder0, 2, nelts);
9889        vec_perm_indices sel1 (builder1, 1, nelts);
9891        for (int i = 0; i < nelts; i++)
9892          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9894        vec_perm_indices sel2 (builder2, 2, nelts);
9896        tree op0 = NULL_TREE;
9897        /* If the new VEC_PERM_EXPR can't be handled but both
9898           original VEC_PERM_EXPRs can, punt.
9899           If one or both of the original VEC_PERM_EXPRs can't be
9900           handled and the new one can't be either, don't increase
9901           number of VEC_PERM_EXPRs that can't be handled.  */
9902        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9903            || (single_use (@0)
9904                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9905                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9906                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9907          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9908      }
9909      (if (op0)
9910       (vec_perm @1 @2 { op0; })))))))
9912 /* Merge
9913      c = VEC_PERM_EXPR <a, b, VCST0>;
9914      d = VEC_PERM_EXPR <x, c, VCST1>;
9915    to
9916      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9917    when all elements from a or b are replaced by the later
9918    permutation.  */
9920 (simplify
9921  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9922  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9923   (with
9924    {
9925      machine_mode result_mode = TYPE_MODE (type);
9926      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9927      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9928      vec_perm_builder builder0;
9929      vec_perm_builder builder1;
9930      vec_perm_builder builder2 (nelts, nelts, 2);
9931    }
9932    (if (tree_to_vec_perm_builder (&builder0, @3)
9933         && tree_to_vec_perm_builder (&builder1, @4))
9934     (with
9935      {
9936        vec_perm_indices sel0 (builder0, 2, nelts);
9937        vec_perm_indices sel1 (builder1, 2, nelts);
9938        bool use_1 = false, use_2 = false;
9940        for (int i = 0; i < nelts; i++)
9941          {
9942            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9943              builder2.quick_push (sel1[i]);
9944            else
9945              {
9946                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9947                                     .to_constant ()];
9948                if (known_lt (j, sel0.nelts_per_input ()))
9949                  use_1 = true;
9950                else
9951                  {
9952                    use_2 = true;
9953                    j -= sel0.nelts_per_input ();
9954                  }
9955                builder2.quick_push (j + sel1.nelts_per_input ());
9956              }
9957          }
9958      }
9959      (if (use_1 ^ use_2)
9960       (with
9961        {
9962          vec_perm_indices sel2 (builder2, 2, nelts);
9963          tree op0 = NULL_TREE;
9964          /* If the new VEC_PERM_EXPR can't be handled but both
9965             original VEC_PERM_EXPRs can, punt.
9966             If one or both of the original VEC_PERM_EXPRs can't be
9967             handled and the new one can't be either, don't increase
9968             number of VEC_PERM_EXPRs that can't be handled.  */
9969          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9970              || (single_use (@0)
9971                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9972                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9973                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9974            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9975        }
9976        (if (op0)
9977         (switch
9978          (if (use_1)
9979           (vec_perm @5 @1 { op0; }))
9980          (if (use_2)
9981           (vec_perm @5 @2 { op0; })))))))))))
9983 /* And the case with swapped outer permute sources.  */
9985 (simplify
9986  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9987  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9988   (with
9989    {
9990      machine_mode result_mode = TYPE_MODE (type);
9991      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9992      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9993      vec_perm_builder builder0;
9994      vec_perm_builder builder1;
9995      vec_perm_builder builder2 (nelts, nelts, 2);
9996    }
9997    (if (tree_to_vec_perm_builder (&builder0, @3)
9998         && tree_to_vec_perm_builder (&builder1, @4))
9999     (with
10000      {
10001        vec_perm_indices sel0 (builder0, 2, nelts);
10002        vec_perm_indices sel1 (builder1, 2, nelts);
10003        bool use_1 = false, use_2 = false;
10005        for (int i = 0; i < nelts; i++)
10006          {
10007            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10008              builder2.quick_push (sel1[i]);
10009            else
10010              {
10011                poly_uint64 j = sel0[sel1[i].to_constant ()];
10012                if (known_lt (j, sel0.nelts_per_input ()))
10013                  use_1 = true;
10014                else
10015                  {
10016                    use_2 = true;
10017                    j -= sel0.nelts_per_input ();
10018                  }
10019                builder2.quick_push (j);
10020              }
10021          }
10022      }
10023      (if (use_1 ^ use_2)
10024       (with
10025        {
10026          vec_perm_indices sel2 (builder2, 2, nelts);
10027          tree op0 = NULL_TREE;
10028          /* If the new VEC_PERM_EXPR can't be handled but both
10029             original VEC_PERM_EXPRs can, punt.
10030             If one or both of the original VEC_PERM_EXPRs can't be
10031             handled and the new one can't be either, don't increase
10032             number of VEC_PERM_EXPRs that can't be handled.  */
10033          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10034              || (single_use (@0)
10035                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10036                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10037                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10038            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10039        }
10040        (if (op0)
10041         (switch
10042          (if (use_1)
10043           (vec_perm @1 @5 { op0; }))
10044          (if (use_2)
10045           (vec_perm @2 @5 { op0; })))))))))))
10048 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10049    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10050    constant which when multiplied by a power of 2 contains a unique value
10051    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10052    to the number of trailing zeroes.  */
10053 (match (ctz_table_index @1 @2 @3)
10054   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10056 (match (cond_expr_convert_p @0 @2 @3 @6)
10057  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10058   (if (INTEGRAL_TYPE_P (type)
10059        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10060        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10061        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10062        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10063        && TYPE_PRECISION (TREE_TYPE (@0))
10064           == TYPE_PRECISION (TREE_TYPE (@2))
10065        && TYPE_PRECISION (TREE_TYPE (@0))
10066           == TYPE_PRECISION (TREE_TYPE (@3))
10067        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10068           signess when convert is truncation, but not ok for extension since
10069           it's sign_extend vs zero_extend.  */
10070        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10071            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10072                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10073        && single_use (@4)
10074        && single_use (@5))))
10076 (for bit_op (bit_and bit_ior bit_xor)
10077  (match (bitwise_induction_p @0 @2 @3)
10078   (bit_op:c
10079    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10080    @3)))
10082 (match (bitwise_induction_p @0 @2 @3)
10083  (bit_not
10084   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10086 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10087    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10088 (simplify
10089   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10090   (with { auto i = wi::neg (wi::to_wide (@2)); }
10091   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10092     (if (wi::popcount (i) == 1
10093          && (wi::to_wide (@1)) == (i - 1))
10094       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10095         (bit_and @0 @1)
10096       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10098 /* -x & 1 -> x & 1.  */
10099 (simplify 
10100  (bit_and (negate @0) integer_onep@1)
10101  (if (!TYPE_OVERFLOW_SANITIZED (type))
10102   (bit_and @0 @1)))
10104 /* `-a` is just `a` if the type is 1bit wide or when converting
10105    to a 1bit type; similar to the above transformation of `(-x)&1`.
10106    This is used mostly with the transformation of
10107    `a ? ~b : b` into `(-a)^b`.
10108    It also can show up with bitfields.  */
10109 (simplify
10110  (convert? (negate @0))
10111  (if (INTEGRAL_TYPE_P (type)
10112       && TYPE_PRECISION (type) == 1
10113       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10114   (convert @0)))
10116 /* Optimize
10117    c1 = VEC_PERM_EXPR (a, a, mask)
10118    c2 = VEC_PERM_EXPR (b, b, mask)
10119    c3 = c1 op c2
10120    -->
10121    c = a op b
10122    c3 = VEC_PERM_EXPR (c, c, mask)
10123    For all integer non-div operations.  */
10124 (for op (plus minus mult bit_and bit_ior bit_xor
10125          lshift rshift)
10126  (simplify
10127   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10128    (if (VECTOR_INTEGER_TYPE_P (type))
10129     (vec_perm (op@3 @0 @1) @3 @2))))
10131 /* Similar for float arithmetic when permutation constant covers
10132    all vector elements.  */
10133 (for op (plus minus mult)
10134  (simplify
10135   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10136    (if (VECTOR_FLOAT_TYPE_P (type)
10137         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10138     (with
10139      {
10140        tree perm_cst = @2;
10141        vec_perm_builder builder;
10142        bool full_perm_p = false;
10143        if (tree_to_vec_perm_builder (&builder, perm_cst))
10144          {
10145            unsigned HOST_WIDE_INT nelts;
10147            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10148            /* Create a vec_perm_indices for the VECTOR_CST.  */
10149            vec_perm_indices sel (builder, 1, nelts);
10151            /* Check if perm indices covers all vector elements.  */
10152            if (sel.encoding ().encoded_full_vector_p ())
10153              {
10154                auto_sbitmap seen (nelts);
10155                bitmap_clear (seen);
10157                unsigned HOST_WIDE_INT count = 0, i;
10159                for (i = 0; i < nelts; i++)
10160                  {
10161                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10162                     break;
10163                    count++;
10164                  }
10165                full_perm_p = count == nelts;
10166              }
10167          }
10168       }
10169       (if (full_perm_p)
10170         (vec_perm (op@3 @0 @1) @3 @2))))))