hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / match.pd
blob59791f44851230409cad09a0bc9e08e8612cae7e
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2023 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91   IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function.  */
94 (define_operator_list UNCOND_BINARY
95   plus minus
96   mult trunc_div trunc_mod rdiv
97   min max
98   IFN_FMIN IFN_FMAX IFN_COPYSIGN
99   bit_and bit_ior bit_xor
100   lshift rshift)
101 (define_operator_list COND_BINARY
102   IFN_COND_ADD IFN_COND_SUB
103   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104   IFN_COND_MIN IFN_COND_MAX
105   IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107   IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109   IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110   IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111   IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112   IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113   IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114   IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115   IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations.  */
118 (define_operator_list UNCOND_TERNARY
119   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123   IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129   BUILT_IN_ATOMIC_FETCH_OR_16
130   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132   BUILT_IN_ATOMIC_FETCH_XOR_16
133   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135   BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140   BUILT_IN_SYNC_FETCH_AND_OR_16
141   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143   BUILT_IN_SYNC_FETCH_AND_XOR_16
144   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146   BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*.  */
148 (define_operator_list ATOMIC_FETCH_AND_N
149   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151   BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*.  */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156   BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159    plus conditionalize on tree_nop_conversion_p conversions.  */
160 (match (nop_convert @0)
161  (convert @0)
162  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
164  (view_convert @0)
165  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166       && known_eq (TYPE_VECTOR_SUBPARTS (type),
167                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
170 #if GIMPLE
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172    detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
174  (bit_not @0))
175 (match (bit_not_with_nop @0)
176  (convert (bit_not @0))
177  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179  (match (maybe_cmp @0)
180   (cmp@0 @1 @2))
181  (match (maybe_cmp @0)
182   (convert (cmp@0 @1 @2))
183    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
185 #endif
187 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
188    ABSU_EXPR returns unsigned absolute value of the operand and the operand
189    of the ABSU_EXPR will have the corresponding signed type.  */
190 (simplify (abs (convert @0))
191  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
192       && !TYPE_UNSIGNED (TREE_TYPE (@0))
193       && element_precision (type) > element_precision (TREE_TYPE (@0)))
194   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
195    (convert (absu:utype @0)))))
197 #if GIMPLE
198 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
199 (simplify
200  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
201  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
202       && !TYPE_UNSIGNED (TREE_TYPE (@0))
203       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
204   (abs @0)))
205 #endif
207 /* Simplifications of operations with one constant operand and
208    simplifications to constants or single values.  */
210 (for op (plus pointer_plus minus bit_ior bit_xor)
211   (simplify
212     (op @0 integer_zerop)
213     (non_lvalue @0)))
215 /* 0 +p index -> (type)index */
216 (simplify
217  (pointer_plus integer_zerop @1)
218  (non_lvalue (convert @1)))
220 /* ptr - 0 -> (type)ptr */
221 (simplify
222  (pointer_diff @0 integer_zerop)
223  (convert @0))
225 /* See if ARG1 is zero and X + ARG1 reduces to X.
226    Likewise if the operands are reversed.  */
227 (simplify
228  (plus:c @0 real_zerop@1)
229  (if (fold_real_zero_addition_p (type, @0, @1, 0))
230   (non_lvalue @0)))
232 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
233 (simplify
234  (minus @0 real_zerop@1)
235  (if (fold_real_zero_addition_p (type, @0, @1, 1))
236   (non_lvalue @0)))
238 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
239    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
240    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
241    if not -frounding-math.  For sNaNs the first operation would raise
242    exceptions but turn the result into qNan, so the second operation
243    would not raise it.   */
244 (for inner_op (plus minus)
245  (for outer_op (plus minus)
246   (simplify
247    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
248     (if (real_zerop (@1)
249          && real_zerop (@2)
250          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
251      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
252                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
253              bool outer_plus
254                = ((outer_op == PLUS_EXPR)
255                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
256       (if (outer_plus && !inner_plus)
257        (outer_op @0 @2)
258        @3))))))
260 /* Simplify x - x.
261    This is unsafe for certain floats even in non-IEEE formats.
262    In IEEE, it is unsafe because it does wrong for NaNs.
263    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
264    Also note that operand_equal_p is always false if an operand
265    is volatile.  */
266 (simplify
267  (minus @0 @0)
268  (if (!FLOAT_TYPE_P (type)
269       || (!tree_expr_maybe_nan_p (@0)
270           && !tree_expr_maybe_infinite_p (@0)
271           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
272               || !HONOR_SIGNED_ZEROS (type))))
273   { build_zero_cst (type); }))
274 (simplify
275  (pointer_diff @@0 @0)
276  { build_zero_cst (type); })
278 (simplify
279  (mult @0 integer_zerop@1)
280  @1)
282 /* -x == x -> x == 0 */
283 (for cmp (eq ne)
284  (simplify
285   (cmp:c @0 (negate @0))
286    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
287         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
288     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
290 /* Maybe fold x * 0 to 0.  The expressions aren't the same
291    when x is NaN, since x * 0 is also NaN.  Nor are they the
292    same in modes with signed zeros, since multiplying a
293    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
294    since x * 0 is NaN.  */
295 (simplify
296  (mult @0 real_zerop@1)
297  (if (!tree_expr_maybe_nan_p (@0)
298       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
299       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
300   @1))
302 /* In IEEE floating point, x*1 is not equivalent to x for snans.
303    Likewise for complex arithmetic with signed zeros.  */
304 (simplify
305  (mult @0 real_onep)
306  (if (!tree_expr_maybe_signaling_nan_p (@0)
307       && (!HONOR_SIGNED_ZEROS (type)
308           || !COMPLEX_FLOAT_TYPE_P (type)))
309   (non_lvalue @0)))
311 /* Transform x * -1.0 into -x.  */
312 (simplify
313  (mult @0 real_minus_onep)
314   (if (!tree_expr_maybe_signaling_nan_p (@0)
315        && (!HONOR_SIGNED_ZEROS (type)
316            || !COMPLEX_FLOAT_TYPE_P (type)))
317    (negate @0)))
319 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
320    unless the target has native support for the former but not the latter.  */
321 (simplify
322  (mult @0 VECTOR_CST@1)
323  (if (initializer_each_zero_or_onep (@1)
324       && !HONOR_SNANS (type)
325       && !HONOR_SIGNED_ZEROS (type))
326   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
327    (if (itype
328         && (!VECTOR_MODE_P (TYPE_MODE (type))
329             || (VECTOR_MODE_P (TYPE_MODE (itype))
330                 && optab_handler (and_optab,
331                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
332     (view_convert (bit_and:itype (view_convert @0)
333                                  (ne @1 { build_zero_cst (type); })))))))
335 /* In SWAR (SIMD within a register) code a signed comparison of packed data
336    can be constructed with a particular combination of shift, bitwise and,
337    and multiplication by constants.  If that code is vectorized we can
338    convert this pattern into a more efficient vector comparison.  */
339 (simplify
340  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
341             uniform_integer_cst_p@2)
342     uniform_integer_cst_p@3)
343  (with {
344    tree rshift_cst = uniform_integer_cst_p (@1);
345    tree bit_and_cst = uniform_integer_cst_p (@2);
346    tree mult_cst = uniform_integer_cst_p (@3);
347   }
348   /* Make sure we're working with vectors and uniform vector constants.  */
349   (if (VECTOR_TYPE_P (type)
350        && tree_fits_uhwi_p (rshift_cst)
351        && tree_fits_uhwi_p (mult_cst)
352        && tree_fits_uhwi_p (bit_and_cst))
353    /* Compute what constants would be needed for this to represent a packed
354       comparison based on the shift amount denoted by RSHIFT_CST.  */
355    (with {
356      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
357      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
358      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
359      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
360      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
361      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
362      mult_i = tree_to_uhwi (mult_cst);
363      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
364      bit_and_i = tree_to_uhwi (bit_and_cst);
365      target_bit_and_i = 0;
367      /* The bit pattern in BIT_AND_I should be a mask for the least
368         significant bit of each packed element that is CMP_BITS wide.  */
369      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
370        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
371     }
372     (if ((exact_log2 (cmp_bits_i)) >= 0
373          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
374          && multiple_p (vec_bits, cmp_bits_i)
375          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
376          && target_mult_i == mult_i
377          && target_bit_and_i == bit_and_i)
378      /* Compute the vector shape for the comparison and check if the target is
379         able to expand the comparison with that type.  */
380      (with {
381        /* We're doing a signed comparison.  */
382        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
383        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
384        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
385        tree vec_truth_type = truth_type_for (vec_cmp_type);
386        tree zeros = build_zero_cst (vec_cmp_type);
387        tree ones = build_all_ones_cst (vec_cmp_type);
388       }
389       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
390            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
391        (view_convert:type (vec_cond (lt:vec_truth_type
392                                      (view_convert:vec_cmp_type @0)
393                                      { zeros; })
394                            { ones; } { zeros; })))))))))
396 (for cmp (gt ge lt le)
397      outp (convert convert negate negate)
398      outn (negate negate convert convert)
399  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
400  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
401  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
402  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
403  (simplify
404   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
405   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
406    (outp (abs @0))))
407  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
408  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
409  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
410  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
411  (simplify
412   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
413   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414    (outn (abs @0)))))
416 /* Transform X * copysign (1.0, X) into abs(X). */
417 (simplify
418  (mult:c @0 (COPYSIGN_ALL real_onep @0))
419  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
420   (abs @0)))
422 /* Transform X * copysign (1.0, -X) into -abs(X). */
423 (simplify
424  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
425  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
426   (negate (abs @0))))
428 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
429 (simplify
430  (COPYSIGN_ALL REAL_CST@0 @1)
431  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
432   (COPYSIGN_ALL (negate @0) @1)))
434 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
435    tree-ssa-math-opts.cc does the corresponding optimization for
436    unconditional multiplications (via xorsign).  */
437 (simplify
438  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
439  (with { tree signs = sign_mask_for (type); }
440   (if (signs)
441    (with { tree inttype = TREE_TYPE (signs); }
442     (view_convert:type
443      (IFN_COND_XOR:inttype @0
444       (view_convert:inttype @1)
445       (bit_and (view_convert:inttype @2) { signs; })
446       (view_convert:inttype @3)))))))
448 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
449 (simplify
450   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
451   (abs @0))
453 /* X * 1, X / 1 -> X.  */
454 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
455   (simplify
456     (op @0 integer_onep)
457     (non_lvalue @0)))
459 /* (A / (1 << B)) -> (A >> B).
460    Only for unsigned A.  For signed A, this would not preserve rounding
461    toward zero.
462    For example: (-1 / ( 1 << B)) !=  -1 >> B.
463    Also handle widening conversions, like:
464    (A / (unsigned long long) (1U << B)) -> (A >> B)
465    or
466    (A / (unsigned long long) (1 << B)) -> (A >> B).
467    If the left shift is signed, it can be done only if the upper bits
468    of A starting from shift's type sign bit are zero, as
469    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
470    so it is valid only if A >> 31 is zero.  */
471 (simplify
472  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
473  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
474       && (!VECTOR_TYPE_P (type)
475           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
476           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
477       && (useless_type_conversion_p (type, TREE_TYPE (@1))
478           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
479               && (TYPE_UNSIGNED (TREE_TYPE (@1))
480                   || (element_precision (type)
481                       == element_precision (TREE_TYPE (@1)))
482                   || (INTEGRAL_TYPE_P (type)
483                       && (tree_nonzero_bits (@0)
484                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
485                                       true,
486                                       element_precision (type))) == 0)))))
487    (if (!VECTOR_TYPE_P (type)
488         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
489         && element_precision (TREE_TYPE (@3)) < element_precision (type))
490     (convert (rshift @3 @2))
491     (rshift @0 @2))))
493 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
494    undefined behavior in constexpr evaluation, and assuming that the division
495    traps enables better optimizations than these anyway.  */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
497  /* 0 / X is always zero.  */
498  (simplify
499   (div integer_zerop@0 @1)
500   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
501   (if (!integer_zerop (@1))
502    @0))
503  /* X / -1 is -X.  */
504  (simplify
505   (div @0 integer_minus_onep@1)
506   (if (!TYPE_UNSIGNED (type))
507    (negate @0)))
508  /* X / bool_range_Y is X.  */ 
509  (simplify
510   (div @0 SSA_NAME@1)
511   (if (INTEGRAL_TYPE_P (type)
512        && ssa_name_has_boolean_range (@1)
513        && !flag_non_call_exceptions)
514    @0))
515  /* X / X is one.  */
516  (simplify
517   (div @0 @0)
518   /* But not for 0 / 0 so that we can get the proper warnings and errors.
519      And not for _Fract types where we can't build 1.  */
520   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
521        && !integer_zerop (@0)
522        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
523    { build_one_cst (type); }))
524  /* X / abs (X) is X < 0 ? -1 : 1.  */
525  (simplify
526    (div:C @0 (abs @0))
527    (if (INTEGRAL_TYPE_P (type)
528         && TYPE_OVERFLOW_UNDEFINED (type)
529         && !integer_zerop (@0)
530         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
531     (cond (lt @0 { build_zero_cst (type); })
532           { build_minus_one_cst (type); } { build_one_cst (type); })))
533  /* X / -X is -1.  */
534  (simplify
535    (div:C @0 (negate @0))
536    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
537         && TYPE_OVERFLOW_UNDEFINED (type)
538         && !integer_zerop (@0)
539         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
540     { build_minus_one_cst (type); })))
542 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
543    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
544    for MOD instead of DIV.  */
545 (for floor_divmod (floor_div floor_mod)
546      trunc_divmod (trunc_div trunc_mod)
547  (simplify
548   (floor_divmod @0 @1)
549   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
550        && TYPE_UNSIGNED (type))
551    (trunc_divmod @0 @1))))
553 /* 1 / X -> X == 1 for unsigned integer X.
554    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
555    But not for 1 / 0 so that we can get proper warnings and errors,
556    and not for 1-bit integers as they are edge cases better handled
557    elsewhere.  */
558 (simplify
559  (trunc_div integer_onep@0 @1)
560  (if (INTEGRAL_TYPE_P (type)
561       && TYPE_PRECISION (type) > 1
562       && !integer_zerop (@1)
563       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
564   (if (TYPE_UNSIGNED (type))
565    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
566    (with { tree utype = unsigned_type_for (type); }
567     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
568               { build_int_cst (utype, 2); })
569      @1 { build_zero_cst (type); })))))
571 /* Combine two successive divisions.  Note that combining ceil_div
572    and floor_div is trickier and combining round_div even more so.  */
573 (for div (trunc_div exact_div)
574  (simplify
575   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
576   (with {
577     wi::overflow_type overflow;
578     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
579                             TYPE_SIGN (type), &overflow);
580    }
581    (if (div == EXACT_DIV_EXPR
582         || optimize_successive_divisions_p (@2, @3))
583     (if (!overflow)
584      (div @0 { wide_int_to_tree (type, mul); })
585      (if (TYPE_UNSIGNED (type)
586           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
587       { build_zero_cst (type); }))))))
589 /* Combine successive multiplications.  Similar to above, but handling
590    overflow is different.  */
591 (simplify
592  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
593  (with {
594    wi::overflow_type overflow;
595    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
596                            TYPE_SIGN (type), &overflow);
597   }
598   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
599      otherwise undefined overflow implies that @0 must be zero.  */
600   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
601    (mult @0 { wide_int_to_tree (type, mul); }))))
603 /* Similar to above, but there could be an extra add/sub between
604    successive multuiplications.  */
605 (simplify
606  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
607  (with {
608    bool overflowed = true;
609    wi::overflow_type ovf1, ovf2;
610    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
611                            TYPE_SIGN (type), &ovf1);
612    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
613                            TYPE_SIGN (type), &ovf2);
614   if (TYPE_OVERFLOW_UNDEFINED (type))
615     {
616 #if GIMPLE
617       value_range vr0;
618       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
619           && get_global_range_query ()->range_of_expr (vr0, @4)
620           && !vr0.varying_p () && !vr0.undefined_p ())
621         {
622           wide_int wmin0 = vr0.lower_bound ();
623           wide_int wmax0 = vr0.upper_bound ();
624           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
625           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
626           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
627             {
628               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
629               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
630               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
631                 overflowed = false;
632             }
633         }
634 #endif
635     }
636   else
637    overflowed = false;
639   /* Skip folding on overflow.  */
640   (if (!overflowed)
641    (plus (mult @0 { wide_int_to_tree (type, mul); })
642          { wide_int_to_tree (type, add); }))))
644 /* Similar to above, but a multiplication between successive additions.  */
645 (simplify
646  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
647  (with {
648    bool overflowed = true;
649    wi::overflow_type ovf1;
650    wi::overflow_type ovf2;
651    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
652                            TYPE_SIGN (type), &ovf1);
653    wide_int add = wi::add (mul, wi::to_wide (@3),
654                            TYPE_SIGN (type), &ovf2);
655   if (TYPE_OVERFLOW_UNDEFINED (type))
656     {
657 #if GIMPLE
658       value_range vr0;
659       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
660           && get_global_range_query ()->range_of_expr (vr0, @0)
661           && !vr0.varying_p () && !vr0.undefined_p ())
662         {
663           wide_int wmin0 = vr0.lower_bound ();
664           wide_int wmax0 = vr0.upper_bound ();
665           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
666           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
667           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
668             {
669               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
670               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
671               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
672                 overflowed = false;
673             }
674         }
675 #endif
676     }
677   else
678    overflowed = false;
680   /* Skip folding on overflow.  */
681   (if (!overflowed)
682    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
684 /* Optimize A / A to 1.0 if we don't care about
685    NaNs or Infinities.  */
686 (simplify
687  (rdiv @0 @0)
688  (if (FLOAT_TYPE_P (type)
689       && ! HONOR_NANS (type)
690       && ! HONOR_INFINITIES (type))
691   { build_one_cst (type); }))
693 /* Optimize -A / A to -1.0 if we don't care about
694    NaNs or Infinities.  */
695 (simplify
696  (rdiv:C @0 (negate @0))
697  (if (FLOAT_TYPE_P (type)
698       && ! HONOR_NANS (type)
699       && ! HONOR_INFINITIES (type))
700   { build_minus_one_cst (type); }))
702 /* PR71078: x / abs(x) -> copysign (1.0, x) */
703 (simplify
704  (rdiv:C (convert? @0) (convert? (abs @0)))
705   (if (SCALAR_FLOAT_TYPE_P (type)
706        && ! HONOR_NANS (type)
707        && ! HONOR_INFINITIES (type))
708    (switch
709     (if (types_match (type, float_type_node))
710      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
711     (if (types_match (type, double_type_node))
712      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
713     (if (types_match (type, long_double_type_node))
714      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
716 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
717 (simplify
718  (rdiv @0 real_onep)
719  (if (!tree_expr_maybe_signaling_nan_p (@0))
720   (non_lvalue @0)))
722 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
723 (simplify
724  (rdiv @0 real_minus_onep)
725  (if (!tree_expr_maybe_signaling_nan_p (@0))
726   (negate @0)))
728 (if (flag_reciprocal_math)
729  /* Convert (A/B)/C to A/(B*C). */
730  (simplify
731   (rdiv (rdiv:s @0 @1) @2)
732   (rdiv @0 (mult @1 @2)))
734  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
735  (simplify
736   (rdiv @0 (mult:s @1 REAL_CST@2))
737   (with
738    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
739    (if (tem)
740     (rdiv (mult @0 { tem; } ) @1))))
742  /* Convert A/(B/C) to (A/B)*C  */
743  (simplify
744   (rdiv @0 (rdiv:s @1 @2))
745    (mult (rdiv @0 @1) @2)))
747 /* Simplify x / (- y) to -x / y.  */
748 (simplify
749  (rdiv @0 (negate @1))
750  (rdiv (negate @0) @1))
752 (if (flag_unsafe_math_optimizations)
753  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
754     Since C / x may underflow to zero, do this only for unsafe math.  */
755  (for op (lt le gt ge)
756       neg_op (gt ge lt le)
757   (simplify
758    (op (rdiv REAL_CST@0 @1) real_zerop@2)
759    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
760     (switch
761      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
762       (op @1 @2))
763      /* For C < 0, use the inverted operator.  */
764      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
765       (neg_op @1 @2)))))))
767 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
768 (for div (trunc_div ceil_div floor_div round_div exact_div)
769  (simplify
770   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
771   (if (integer_pow2p (@2)
772        && tree_int_cst_sgn (@2) > 0
773        && tree_nop_conversion_p (type, TREE_TYPE (@0))
774        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
775    (rshift (convert @0)
776            { build_int_cst (integer_type_node,
777                             wi::exact_log2 (wi::to_wide (@2))); }))))
779 /* If ARG1 is a constant, we can convert this to a multiply by the
780    reciprocal.  This does not have the same rounding properties,
781    so only do this if -freciprocal-math.  We can actually
782    always safely do it if ARG1 is a power of two, but it's hard to
783    tell if it is or not in a portable manner.  */
784 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
785  (simplify
786   (rdiv @0 cst@1)
787   (if (optimize)
788    (if (flag_reciprocal_math
789         && !real_zerop (@1))
790     (with
791      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
792      (if (tem)
793       (mult @0 { tem; } )))
794     (if (cst != COMPLEX_CST)
795      (with { tree inverse = exact_inverse (type, @1); }
796       (if (inverse)
797        (mult @0 { inverse; } ))))))))
799 (for mod (ceil_mod floor_mod round_mod trunc_mod)
800  /* 0 % X is always zero.  */
801  (simplify
802   (mod integer_zerop@0 @1)
803   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
804   (if (!integer_zerop (@1))
805    @0))
806  /* X % 1 is always zero.  */
807  (simplify
808   (mod @0 integer_onep)
809   { build_zero_cst (type); })
810  /* X % -1 is zero.  */
811  (simplify
812   (mod @0 integer_minus_onep@1)
813   (if (!TYPE_UNSIGNED (type))
814    { build_zero_cst (type); }))
815  /* X % X is zero.  */
816  (simplify
817   (mod @0 @0)
818   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
819   (if (!integer_zerop (@0))
820    { build_zero_cst (type); }))
821  /* (X % Y) % Y is just X % Y.  */
822  (simplify
823   (mod (mod@2 @0 @1) @1)
824   @2)
825  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
826  (simplify
827   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
828   (if (ANY_INTEGRAL_TYPE_P (type)
829        && TYPE_OVERFLOW_UNDEFINED (type)
830        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
831                              TYPE_SIGN (type)))
832    { build_zero_cst (type); }))
833  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
834     modulo and comparison, since it is simpler and equivalent.  */
835  (for cmp (eq ne)
836   (simplify
837    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
838    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
839     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
840      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
842 /* X % -C is the same as X % C.  */
843 (simplify
844  (trunc_mod @0 INTEGER_CST@1)
845   (if (TYPE_SIGN (type) == SIGNED
846        && !TREE_OVERFLOW (@1)
847        && wi::neg_p (wi::to_wide (@1))
848        && !TYPE_OVERFLOW_TRAPS (type)
849        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
850        && !sign_bit_p (@1, @1))
851    (trunc_mod @0 (negate @1))))
853 /* X % -Y is the same as X % Y.  */
854 (simplify
855  (trunc_mod @0 (convert? (negate @1)))
856  (if (INTEGRAL_TYPE_P (type)
857       && !TYPE_UNSIGNED (type)
858       && !TYPE_OVERFLOW_TRAPS (type)
859       && tree_nop_conversion_p (type, TREE_TYPE (@1))
860       /* Avoid this transformation if X might be INT_MIN or
861          Y might be -1, because we would then change valid
862          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
863       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
864           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
865                                                         (TREE_TYPE (@1))))))
866   (trunc_mod @0 (convert @1))))
868 /* X - (X / Y) * Y is the same as X % Y.  */
869 (simplify
870  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
871  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
872   (convert (trunc_mod @0 @1))))
874 /* x * (1 + y / x) - y -> x - y % x */
875 (simplify
876  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
877  (if (INTEGRAL_TYPE_P (type))
878   (minus @0 (trunc_mod @1 @0))))
880 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
881    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
882    Also optimize A % (C << N)  where C is a power of 2,
883    to A & ((C << N) - 1).
884    Also optimize "A shift (B % C)", if C is a power of 2, to
885    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
886    and assume (B % C) is nonnegative as shifts negative values would
887    be UB.  */
888 (match (power_of_two_cand @1)
889  INTEGER_CST@1)
890 (match (power_of_two_cand @1)
891  (lshift INTEGER_CST@1 @2))
892 (for mod (trunc_mod floor_mod)
893  (for shift (lshift rshift)
894   (simplify
895    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
896    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
897     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
898                                                       1); }))))))
899  (simplify
900   (mod @0 (convert? (power_of_two_cand@1 @2)))
901   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
902        /* Allow any integral conversions of the divisor, except
903           conversion from narrower signed to wider unsigned type
904           where if @1 would be negative power of two, the divisor
905           would not be a power of two.  */
906        && INTEGRAL_TYPE_P (type)
907        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
908        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
909            || TYPE_UNSIGNED (TREE_TYPE (@1))
910            || !TYPE_UNSIGNED (type))
911        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
912    (with { tree utype = TREE_TYPE (@1);
913            if (!TYPE_OVERFLOW_WRAPS (utype))
914              utype = unsigned_type_for (utype); }
915     (bit_and @0 (convert (minus (convert:utype @1)
916                                 { build_one_cst (utype); })))))))
918 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
919 (simplify
920  (trunc_div (mult @0 integer_pow2p@1) @1)
921  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
922   (bit_and @0 { wide_int_to_tree
923                 (type, wi::mask (TYPE_PRECISION (type)
924                                  - wi::exact_log2 (wi::to_wide (@1)),
925                                  false, TYPE_PRECISION (type))); })))
927 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
928 (simplify
929  (mult (trunc_div @0 integer_pow2p@1) @1)
930  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931   (bit_and @0 (negate @1))))
933 /* Simplify (t * 2) / 2) -> t.  */
934 (for div (trunc_div ceil_div floor_div round_div exact_div)
935  (simplify
936   (div (mult:c @0 @1) @1)
937   (if (ANY_INTEGRAL_TYPE_P (type))
938    (if (TYPE_OVERFLOW_UNDEFINED (type))
939     @0
940 #if GIMPLE
941     (with {value_range vr0, vr1;}
942      (if (INTEGRAL_TYPE_P (type)
943           && get_range_query (cfun)->range_of_expr (vr0, @0)
944           && get_range_query (cfun)->range_of_expr (vr1, @1)
945           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
946       @0))
947 #endif
948    ))))
950 #if GIMPLE
951 (for div (trunc_div exact_div)
952  /* Simplify (X + M*N) / N -> X / N + M.  */
953  (simplify
954   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
955   (with {value_range vr0, vr1, vr2, vr3, vr4;}
956   (if (INTEGRAL_TYPE_P (type)
957        && get_range_query (cfun)->range_of_expr (vr1, @1)
958        && get_range_query (cfun)->range_of_expr (vr2, @2)
959        /* "N*M" doesn't overflow.  */
960        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
961        && get_range_query (cfun)->range_of_expr (vr0, @0)
962        && get_range_query (cfun)->range_of_expr (vr3, @3)
963        /* "X+(N*M)" doesn't overflow.  */
964        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
965        && get_range_query (cfun)->range_of_expr (vr4, @4)
966        && !vr4.undefined_p ()
967        /* "X+N*M" is not with opposite sign as "X".  */
968        && (TYPE_UNSIGNED (type)
969            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
970            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
971   (plus (div @0 @2) @1))))
973  /* Simplify (X - M*N) / N -> X / N - M.  */
974  (simplify
975   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
976   (with {value_range vr0, vr1, vr2, vr3, vr4;}
977   (if (INTEGRAL_TYPE_P (type)
978        && get_range_query (cfun)->range_of_expr (vr1, @1)
979        && get_range_query (cfun)->range_of_expr (vr2, @2)
980        /* "N * M" doesn't overflow.  */
981        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
982        && get_range_query (cfun)->range_of_expr (vr0, @0)
983        && get_range_query (cfun)->range_of_expr (vr3, @3)
984        /* "X - (N*M)" doesn't overflow.  */
985        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
986        && get_range_query (cfun)->range_of_expr (vr4, @4)
987        && !vr4.undefined_p ()
988        /* "X-N*M" is not with opposite sign as "X".  */
989        && (TYPE_UNSIGNED (type)
990            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
991            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
992   (minus (div @0 @2) @1)))))
994 /* Simplify
995    (X + C) / N -> X / N + C / N where C is multiple of N.
996    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
997 (for op (trunc_div exact_div rshift)
998  (simplify
999   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1000    (with
1001     {
1002       wide_int c = wi::to_wide (@1);
1003       wide_int n = wi::to_wide (@2);
1004       bool shift = op == RSHIFT_EXPR;
1005 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1006                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1007 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1008                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1009       value_range vr0, vr1, vr3;
1010     }
1011     (if (INTEGRAL_TYPE_P (type)
1012          && get_range_query (cfun)->range_of_expr (vr0, @0))
1013      (if (exact_mod (c)
1014           && get_range_query (cfun)->range_of_expr (vr1, @1)
1015           /* "X+C" doesn't overflow.  */
1016           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1017           && get_range_query (cfun)->range_of_expr (vr3, @3)
1018           && !vr3.undefined_p ()
1019           /* "X+C" and "X" are not of opposite sign.  */
1020           && (TYPE_UNSIGNED (type)
1021               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1022               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1023        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1024        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1025             && exact_mod (-c)
1026             /* unsigned "X-(-C)" doesn't underflow.  */
1027             && wi::geu_p (vr0.lower_bound (), -c))
1028          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1029 #undef plus_op1
1030 #undef exact_mod
1031 #endif
1033 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1034    if var is smaller in precision.
1035    This is always safe for both doing the negative in signed or unsigned
1036    as the value for undefined will not show up.  */
1037 (simplify
1038  (convert (negate:s@1 (convert:s @0)))
1039  (if (INTEGRAL_TYPE_P (type)
1040       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1041       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1042     (negate (convert @0))))
1044 (for op (negate abs)
1045  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1046  (for coss (COS COSH)
1047   (simplify
1048    (coss (op @0))
1049     (coss @0)))
1050  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1051  (for pows (POW)
1052   (simplify
1053    (pows (op @0) REAL_CST@1)
1054    (with { HOST_WIDE_INT n; }
1055     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1056      (pows @0 @1)))))
1057  /* Likewise for powi.  */
1058  (for pows (POWI)
1059   (simplify
1060    (pows (op @0) INTEGER_CST@1)
1061    (if ((wi::to_wide (@1) & 1) == 0)
1062     (pows @0 @1))))
1063  /* Strip negate and abs from both operands of hypot.  */
1064  (for hypots (HYPOT)
1065   (simplify
1066    (hypots (op @0) @1)
1067    (hypots @0 @1))
1068   (simplify
1069    (hypots @0 (op @1))
1070    (hypots @0 @1)))
1071  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1072  (for copysigns (COPYSIGN_ALL)
1073   (simplify
1074    (copysigns (op @0) @1)
1075    (copysigns @0 @1))))
1077 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1078 (simplify
1079  (mult (abs@1 @0) @1)
1080  (mult @0 @0))
1082 /* Convert absu(x)*absu(x) -> x*x.  */
1083 (simplify
1084  (mult (absu@1 @0) @1)
1085  (mult (convert@2 @0) @2))
1087 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1088 (for coss (COS COSH)
1089  (for copysigns (COPYSIGN)
1090   (simplify
1091    (coss (copysigns @0 @1))
1092     (coss @0))))
1094 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1095 (for pows (POW)
1096  (for copysigns (COPYSIGN)
1097   (simplify
1098    (pows (copysigns @0 @2) REAL_CST@1)
1099    (with { HOST_WIDE_INT n; }
1100     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1101      (pows @0 @1))))))
1102 /* Likewise for powi.  */
1103 (for pows (POWI)
1104  (for copysigns (COPYSIGN)
1105   (simplify
1106    (pows (copysigns @0 @2) INTEGER_CST@1)
1107    (if ((wi::to_wide (@1) & 1) == 0)
1108     (pows @0 @1)))))
1110 (for hypots (HYPOT)
1111  (for copysigns (COPYSIGN)
1112   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1113   (simplify
1114    (hypots (copysigns @0 @1) @2)
1115    (hypots @0 @2))
1116   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1117   (simplify
1118    (hypots @0 (copysigns @1 @2))
1119    (hypots @0 @1))))
1121 /* copysign(x, CST) -> abs (x).  */
1122 (for copysigns (COPYSIGN_ALL)
1123  (simplify
1124   (copysigns @0 REAL_CST@1)
1125   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1126    (abs @0))))
1128 /* Transform fneg (fabs (X)) -> copysign (X, -1).  */
1129 (simplify
1130  (negate (abs @0))
1131  (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1133 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1134 (for copysigns (COPYSIGN_ALL)
1135  (simplify
1136   (copysigns (copysigns @0 @1) @2)
1137   (copysigns @0 @2)))
1139 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1140 (for copysigns (COPYSIGN_ALL)
1141  (simplify
1142   (mult (copysigns@2 @0 @1) @2)
1143   (mult @0 @0)))
1145 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1146 (for ccoss (CCOS CCOSH)
1147  (simplify
1148   (ccoss (negate @0))
1149    (ccoss @0)))
1151 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1152 (for ops (conj negate)
1153  (for cabss (CABS)
1154   (simplify
1155    (cabss (ops @0))
1156    (cabss @0))))
1158 /* Fold (a * (1 << b)) into (a << b)  */
1159 (simplify
1160  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1161   (if (! FLOAT_TYPE_P (type)
1162        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1163    (lshift @0 @2)))
1165 /* Shifts by precision or greater result in zero.  */
1166 (for shift (lshift rshift)
1167  (simplify
1168   (shift @0 uniform_integer_cst_p@1)
1169   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1170        /* Leave arithmetic right shifts of possibly negative values alone.  */
1171        && (TYPE_UNSIGNED (type)
1172            || shift == LSHIFT_EXPR
1173            || tree_expr_nonnegative_p (@0))
1174        /* Use a signed compare to leave negative shift counts alone.  */
1175        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1176                      element_precision (type)))
1177    { build_zero_cst (type); })))
1179 /* Shifts by constants distribute over several binary operations,
1180    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1181 (for op (plus minus)
1182   (simplify
1183     (op (lshift:s @0 @1) (lshift:s @2 @1))
1184     (if (INTEGRAL_TYPE_P (type)
1185          && TYPE_OVERFLOW_WRAPS (type)
1186          && !TYPE_SATURATING (type))
1187       (lshift (op @0 @2) @1))))
1189 (for op (bit_and bit_ior bit_xor)
1190   (simplify
1191     (op (lshift:s @0 @1) (lshift:s @2 @1))
1192     (if (INTEGRAL_TYPE_P (type))
1193       (lshift (op @0 @2) @1)))
1194   (simplify
1195     (op (rshift:s @0 @1) (rshift:s @2 @1))
1196     (if (INTEGRAL_TYPE_P (type))
1197       (rshift (op @0 @2) @1))))
1199 /* Fold (1 << (C - x)) where C = precision(type) - 1
1200    into ((1 << C) >> x). */
1201 (simplify
1202  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1203   (if (INTEGRAL_TYPE_P (type)
1204        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1205        && single_use (@1))
1206    (if (TYPE_UNSIGNED (type))
1207      (rshift (lshift @0 @2) @3)
1208    (with
1209     { tree utype = unsigned_type_for (type); }
1210     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1212 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1213 (simplify
1214  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1215  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1216       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1217   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1218    (bit_and (convert @0)
1219             { wide_int_to_tree (type,
1220                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1222 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1223 (for cst (INTEGER_CST VECTOR_CST)
1224  (simplify
1225   (rshift (negate:s @0) cst@1)
1226    (if (!TYPE_UNSIGNED (type)
1227         && TYPE_OVERFLOW_UNDEFINED (type))
1228     (with { tree stype = TREE_TYPE (@1);
1229             tree bt = truth_type_for (type);
1230             tree zeros = build_zero_cst (type);
1231             tree cst = NULL_TREE; }
1232      (switch
1233       /* Handle scalar case.  */
1234       (if (INTEGRAL_TYPE_P (type)
1235            /* If we apply the rule to the scalar type before vectorization
1236               we will enforce the result of the comparison being a bool
1237               which will require an extra AND on the result that will be
1238               indistinguishable from when the user did actually want 0
1239               or 1 as the result so it can't be removed.  */
1240            && canonicalize_math_after_vectorization_p ()
1241            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1242        (negate (convert (gt @0 { zeros; }))))
1243       /* Handle vector case.  */
1244       (if (VECTOR_INTEGER_TYPE_P (type)
1245            /* First check whether the target has the same mode for vector
1246               comparison results as it's operands do.  */
1247            && TYPE_MODE (bt) == TYPE_MODE (type)
1248            /* Then check to see if the target is able to expand the comparison
1249               with the given type later on, otherwise we may ICE.  */
1250            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1251            && (cst = uniform_integer_cst_p (@1)) != NULL
1252            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1253        (view_convert (gt:bt @0 { zeros; }))))))))
1255 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1256 (simplify
1257  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1258   (if (flag_associative_math
1259        && single_use (@3))
1260    (with
1261     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1262     (if (tem)
1263      (rdiv { tem; } @1)))))
1265 /* Simplify ~X & X as zero.  */
1266 (simplify
1267  (bit_and (convert? @0) (convert? @1))
1268  (with { bool wascmp; }
1269   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1270        && bitwise_inverted_equal_p (@0, @1, wascmp))
1271    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1273 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1274 (simplify
1275   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1276   (if (TYPE_UNSIGNED (type))
1277     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1279 (for bitop (bit_and bit_ior)
1280      cmp (eq ne)
1281  /* PR35691: Transform
1282     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1283     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1284  (simplify
1285   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1286    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1287         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1288         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1289     (cmp (bit_ior @0 (convert @1)) @2)))
1290  /* Transform:
1291     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1292     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1293  (simplify
1294   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1295    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1296         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1297         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1298     (cmp (bit_and @0 (convert @1)) @2))))
1300 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1301 (simplify
1302  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1303   (minus (bit_xor @0 @1) @1))
1304 (simplify
1305  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1306  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1307   (minus (bit_xor @0 @1) @1)))
1309 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1310 (simplify
1311  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1312   (minus @1 (bit_xor @0 @1)))
1314 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1315 (for op (bit_ior bit_xor plus)
1316  (simplify
1317   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1318   (with { bool wascmp0, wascmp1; }
1319    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1320         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1321         && ((!wascmp0 && !wascmp1)
1322             || element_precision (type) == 1))
1323    (bit_xor @0 @1)))))
1325 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1326 (simplify
1327   (bit_ior:c (bit_xor:c @0 @1) @0)
1328   (bit_ior @0 @1))
1330 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1331 (simplify
1332  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1333  @2)
1335 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1336 (simplify
1337  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1338  (bit_not (bit_and @0 @1)))
1340 /* (~a & b) ^ a  -->   (a | b)   */
1341 (simplify
1342  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1343  (bit_ior @0 @1))
1345 /* (a | b) & ~(a ^ b)  -->  a & b  */
1346 (simplify
1347  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1348  (bit_and @0 @1))
1350 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1351 (simplify
1352  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1353  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1354       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1355   (bit_and @0 @1)))
1357 /* a | ~(a ^ b)  -->  a | ~b  */
1358 (simplify
1359  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1360  (bit_ior @0 (bit_not @1)))
1362 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1363 (simplify
1364  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1365  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1366       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1367   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1369 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1370 (simplify
1371  (bit_ior:c @0 (bit_xor:cs @1 @2))
1372  (with { bool wascmp; }
1373  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1374       && (!wascmp || element_precision (type) == 1))
1375   (bit_ior @0 (bit_not @2)))))
1377 /* a & ~(a ^ b)  -->  a & b  */
1378 (simplify
1379  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1380  (bit_and @0 @1))
1382 /* a & (a == b)  -->  a & b (boolean version of the above). */
1383 (simplify
1384  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1385  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1386       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1387   (bit_and @0 @1)))
1389 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1390 (simplify
1391  (bit_and:c @0 (bit_xor:c @1 @2))
1392  (with { bool wascmp; }
1393  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1394       && (!wascmp || element_precision (type) == 1))
1395   (bit_and @0 @2))))
1397 /* (a | b) | (a &^ b)  -->  a | b  */
1398 (for op (bit_and bit_xor)
1399  (simplify
1400   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1401   @2))
1403 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1404 (simplify
1405  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1406  @2)
1408 /* (a & b) | (a == b)  -->  a == b  */
1409 (simplify
1410  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1411  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1412       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1413   @2))
1415 /* ~(~a & b)  -->  a | ~b  */
1416 (simplify
1417  (bit_not (bit_and:cs (bit_not @0) @1))
1418  (bit_ior @0 (bit_not @1)))
1420 /* ~(~a | b) --> a & ~b */
1421 (simplify
1422  (bit_not (bit_ior:cs (bit_not @0) @1))
1423  (bit_and @0 (bit_not @1)))
1425 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1426 (simplify
1427  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1428  (bit_and @3 (bit_not @2)))
1430 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1431 (simplify
1432  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1433  (bit_ior @3 @2))
1435 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1436 (simplify
1437  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1438   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1440 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1441 (simplify
1442  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1443   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1445 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1446 (simplify
1447  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1448  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1449       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1450   (bit_xor @0 @1)))
1452 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1453    ((A & N) + B) & M -> (A + B) & M
1454    Similarly if (N & M) == 0,
1455    ((A | N) + B) & M -> (A + B) & M
1456    and for - instead of + (or unary - instead of +)
1457    and/or ^ instead of |.
1458    If B is constant and (B & M) == 0, fold into A & M.  */
1459 (for op (plus minus)
1460  (for bitop (bit_and bit_ior bit_xor)
1461   (simplify
1462    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1463     (with
1464      { tree pmop[2];
1465        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1466                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1467                                        NULL_TREE, pmop); }
1468      (if (utype)
1469       (convert (bit_and (op (convert:utype { pmop[0]; })
1470                             (convert:utype { pmop[1]; }))
1471                         (convert:utype @2))))))
1472   (simplify
1473    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1474     (with
1475      { tree pmop[2];
1476        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1477                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1478                                        @4, pmop); }
1479      (if (utype)
1480       (convert (bit_and (op (convert:utype { pmop[0]; })
1481                             (convert:utype { pmop[1]; }))
1482                         (convert:utype @2)))))))
1483  (simplify
1484   (bit_and (op:s @0 @1) INTEGER_CST@2)
1485    (with
1486     { tree pmop[2];
1487       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1488                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1489                                       NULL_TREE, NULL_TREE, pmop); }
1490     (if (utype)
1491      (convert (bit_and (op (convert:utype { pmop[0]; })
1492                            (convert:utype { pmop[1]; }))
1493                        (convert:utype @2)))))))
1494 (for bitop (bit_and bit_ior bit_xor)
1495  (simplify
1496   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1497    (with
1498     { tree pmop[2];
1499       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1500                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1501                                       NULL_TREE, NULL_TREE, pmop); }
1502     (if (utype)
1503      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1504                        (convert:utype @1)))))))
1506 /* X % Y is smaller than Y.  */
1507 (for cmp (lt ge)
1508  (simplify
1509   (cmp:c (trunc_mod @0 @1) @1)
1510   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1511    { constant_boolean_node (cmp == LT_EXPR, type); })))
1513 /* x | ~0 -> ~0  */
1514 (simplify
1515  (bit_ior @0 integer_all_onesp@1)
1516  @1)
1518 /* x | 0 -> x  */
1519 (simplify
1520  (bit_ior @0 integer_zerop)
1521  @0)
1523 /* x & 0 -> 0  */
1524 (simplify
1525  (bit_and @0 integer_zerop@1)
1526  @1)
1528 /* ~x | x -> -1 */
1529 /* ~x ^ x -> -1 */
1530 (for op (bit_ior bit_xor)
1531  (simplify
1532   (op (convert? @0) (convert? @1))
1533   (with { bool wascmp; }
1534    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1535         && bitwise_inverted_equal_p (@0, @1, wascmp))
1536     (convert
1537      { wascmp
1538         ? constant_boolean_node (true, type)
1539         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1541 /* x ^ x -> 0 */
1542 (simplify
1543   (bit_xor @0 @0)
1544   { build_zero_cst (type); })
1546 /* Canonicalize X ^ ~0 to ~X.  */
1547 (simplify
1548   (bit_xor @0 integer_all_onesp@1)
1549   (bit_not @0))
1551 /* x & ~0 -> x  */
1552 (simplify
1553  (bit_and @0 integer_all_onesp)
1554   (non_lvalue @0))
1556 /* x & x -> x,  x | x -> x  */
1557 (for bitop (bit_and bit_ior)
1558  (simplify
1559   (bitop @0 @0)
1560   (non_lvalue @0)))
1562 /* x & C -> x if we know that x & ~C == 0.  */
1563 #if GIMPLE
1564 (simplify
1565  (bit_and SSA_NAME@0 INTEGER_CST@1)
1566  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1567       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1568   @0))
1570 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1571 (simplify
1572  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1573  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1574       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1575   @0))
1577 /* x | C -> C if we know that x & ~C == 0.  */
1578 (simplify
1579  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1580  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1581       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1582   @1))
1583 #endif
1585 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1586 (simplify
1587  (bit_not (minus (bit_not @0) @1))
1588  (plus @0 @1))
1589 (simplify
1590  (bit_not (plus:c (bit_not @0) @1))
1591  (minus @0 @1))
1592 /* (~X - ~Y) -> Y - X.  */
1593 (simplify
1594  (minus (bit_not @0) (bit_not @1))
1595   (if (!TYPE_OVERFLOW_SANITIZED (type))
1596    (with { tree utype = unsigned_type_for (type); }
1597     (convert (minus (convert:utype @1) (convert:utype @0))))))
1599 /* ~(X - Y) -> ~X + Y.  */
1600 (simplify
1601  (bit_not (minus:s @0 @1))
1602  (plus (bit_not @0) @1))
1603 (simplify
1604  (bit_not (plus:s @0 INTEGER_CST@1))
1605  (if ((INTEGRAL_TYPE_P (type)
1606        && TYPE_UNSIGNED (type))
1607       || (!TYPE_OVERFLOW_SANITIZED (type)
1608           && may_negate_without_overflow_p (@1)))
1609   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1611 #if GIMPLE
1612 /* ~X + Y -> (Y - X) - 1.  */
1613 (simplify
1614  (plus:c (bit_not @0) @1)
1615   (if (ANY_INTEGRAL_TYPE_P (type)
1616        && TYPE_OVERFLOW_WRAPS (type)
1617        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1618        && !integer_all_onesp (@1))
1619    (plus (minus @1 @0) { build_minus_one_cst (type); })
1620    (if (INTEGRAL_TYPE_P (type)
1621         && TREE_CODE (@1) == INTEGER_CST
1622         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1623                                               SIGNED))
1624     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1625 #endif
1627 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1628 (simplify
1629  (bit_not (rshift:s @0 @1))
1630   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1631    (rshift (bit_not! @0) @1)
1632    /* For logical right shifts, this is possible only if @0 doesn't
1633       have MSB set and the logical right shift is changed into
1634       arithmetic shift.  */
1635    (if (INTEGRAL_TYPE_P (type)
1636         && !wi::neg_p (tree_nonzero_bits (@0)))
1637     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1638      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1640 /* x + (x & 1) -> (x + 1) & ~1 */
1641 (simplify
1642  (plus:c @0 (bit_and:s @0 integer_onep@1))
1643  (bit_and (plus @0 @1) (bit_not @1)))
1645 /* x & ~(x & y) -> x & ~y */
1646 /* x | ~(x | y) -> x | ~y  */
1647 (for bitop (bit_and bit_ior)
1648  (simplify
1649   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1650   (bitop @0 (bit_not @1))))
1652 /* (~x & y) | ~(x | y) -> ~x */
1653 (simplify
1654  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1655  @2)
1657 /* (x | y) ^ (x | ~y) -> ~x */
1658 (simplify
1659  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1660  (bit_not @0))
1662 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1663 (simplify
1664  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1665  (bit_not (bit_xor @0 @1)))
1667 /* (~x | y) ^ (x ^ y) -> x | ~y */
1668 (simplify
1669  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1670  (bit_ior @0 (bit_not @1)))
1672 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1673 (simplify
1674  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1675  (bit_not (bit_and @0 @1)))
1677 /* (x & y) ^ (x | y) -> x ^ y */
1678 (simplify
1679  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1680  (bit_xor @0 @1))
1682 /* (x ^ y) ^ (x | y) -> x & y */
1683 (simplify
1684  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1685  (bit_and @0 @1))
1687 /* (x & y) + (x ^ y) -> x | y */
1688 /* (x & y) | (x ^ y) -> x | y */
1689 /* (x & y) ^ (x ^ y) -> x | y */
1690 (for op (plus bit_ior bit_xor)
1691  (simplify
1692   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1693   (bit_ior @0 @1)))
1695 /* (x & y) + (x | y) -> x + y */
1696 (simplify
1697  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1698  (plus @0 @1))
1700 /* (x + y) - (x | y) -> x & y */
1701 (simplify
1702  (minus (plus @0 @1) (bit_ior @0 @1))
1703  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1704       && !TYPE_SATURATING (type))
1705   (bit_and @0 @1)))
1707 /* (x + y) - (x & y) -> x | y */
1708 (simplify
1709  (minus (plus @0 @1) (bit_and @0 @1))
1710  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1711       && !TYPE_SATURATING (type))
1712   (bit_ior @0 @1)))
1714 /* (x | y) - y -> (x & ~y) */
1715 (simplify
1716  (minus (bit_ior:cs @0 @1) @1)
1717  (bit_and @0 (bit_not @1)))
1719 /* (x | y) - (x ^ y) -> x & y */
1720 (simplify
1721  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1722  (bit_and @0 @1))
1724 /* (x | y) - (x & y) -> x ^ y */
1725 (simplify
1726  (minus (bit_ior @0 @1) (bit_and @0 @1))
1727  (bit_xor @0 @1))
1729 /* (x | y) & ~(x & y) -> x ^ y */
1730 (simplify
1731  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1732  (bit_xor @0 @1))
1734 /* (x | y) & (~x ^ y) -> x & y */
1735 (simplify
1736  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1737  (with { bool wascmp; }
1738   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1739        && (!wascmp || element_precision (type) == 1))
1740    (bit_and @0 @1))))
1742 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1743 (simplify
1744  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1745  (bit_not (bit_xor @0 @1)))
1747 /* (~x | y) ^ (x | ~y) -> x ^ y */
1748 (simplify
1749  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1750  (bit_xor @0 @1))
1752 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1753 (simplify
1754  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1755                               (nop_convert2? (bit_ior @0 @1))))
1756        integer_all_onesp)
1757  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1758       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1759       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1760       && !TYPE_SATURATING (TREE_TYPE (@2)))
1761  (bit_not (convert (bit_xor @0 @1)))))
1762 (simplify
1763  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1764                                integer_all_onesp))
1765        (nop_convert3? (bit_ior @0 @1)))
1766  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1767       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1768       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1769       && !TYPE_SATURATING (TREE_TYPE (@2)))
1770  (bit_not (convert (bit_xor @0 @1)))))
1771 (simplify
1772  (minus (nop_convert1? (bit_and @0 @1))
1773        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1774                                integer_onep)))
1775  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1776       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1777       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1778       && !TYPE_SATURATING (TREE_TYPE (@2)))
1779  (bit_not (convert (bit_xor @0 @1)))))
1781 /* ~x & ~y -> ~(x | y)
1782    ~x | ~y -> ~(x & y) */
1783 (for op (bit_and bit_ior)
1784      rop (bit_ior bit_and)
1785  (simplify
1786   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1787   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1788        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1789    (bit_not (rop (convert @0) (convert @1))))))
1791 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1792    with a constant, and the two constants have no bits in common,
1793    we should treat this as a BIT_IOR_EXPR since this may produce more
1794    simplifications.  */
1795 (for op (bit_xor plus)
1796  (simplify
1797   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1798       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1799   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1800        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1801        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1802    (bit_ior (convert @4) (convert @5)))))
1804 /* (X | Y) ^ X -> Y & ~ X*/
1805 (simplify
1806  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1807  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1808   (convert (bit_and @1 (bit_not @0)))))
1810 /* (~X | Y) ^ X -> ~(X & Y).  */
1811 (simplify
1812  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1813  (if (bitwise_equal_p (@0, @2))
1814   (convert (bit_not (bit_and @0 (convert @1))))))
1816 /* Convert ~X ^ ~Y to X ^ Y.  */
1817 (simplify
1818  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1819  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1820       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1821   (bit_xor (convert @0) (convert @1))))
1823 /* Convert ~X ^ C to X ^ ~C.  */
1824 (simplify
1825  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1826  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1827   (bit_xor (convert @0) (bit_not @1))))
1829 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1830 (for opo (bit_and bit_xor)
1831      opi (bit_xor bit_and)
1832  (simplify
1833   (opo:c (opi:cs @0 @1) @1)
1834   (bit_and (bit_not @0) @1)))
1836 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1837    operands are another bit-wise operation with a common input.  If so,
1838    distribute the bit operations to save an operation and possibly two if
1839    constants are involved.  For example, convert
1840      (A | B) & (A | C) into A | (B & C)
1841    Further simplification will occur if B and C are constants.  */
1842 (for op (bit_and bit_ior bit_xor)
1843      rop (bit_ior bit_and bit_and)
1844  (simplify
1845   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1846   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1847        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1848    (rop (convert @0) (op (convert @1) (convert @2))))))
1850 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1851 /* (X & Y) & Y -> X & Y
1852    (X | Y) | Y -> X | Y  */
1853 (for op (bit_and bit_ior)
1854  (simplify
1855   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1856   @2))
1857 /* (X ^ Y) ^ Y -> X  */
1858 (simplify
1859  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1860  (convert @0))
1862 /* (X & ~Y) & Y -> 0 */
1863 (simplify
1864  (bit_and:c (bit_and @0 @1) @2)
1865  (with { bool wascmp; }
1866   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1867        || bitwise_inverted_equal_p (@1, @2, wascmp))
1868    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1869 /* (X | ~Y) | Y -> -1 */
1870 (simplify
1871  (bit_ior:c (bit_ior @0 @1) @2)
1872  (with { bool wascmp; }
1873   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1874         || bitwise_inverted_equal_p (@1, @2, wascmp))
1875        && (!wascmp || element_precision (type) == 1))
1876    { build_all_ones_cst (TREE_TYPE (@0)); })))
1878 /* (X & Y) & (X & Z) -> (X & Y) & Z
1879    (X | Y) | (X | Z) -> (X | Y) | Z  */
1880 (for op (bit_and bit_ior)
1881  (simplify
1882   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1883   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1884        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1885    (if (single_use (@5) && single_use (@6))
1886     (op @3 (convert @2))
1887     (if (single_use (@3) && single_use (@4))
1888      (op (convert @1) @5))))))
1889 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1890 (simplify
1891  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1892  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1893       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1894   (bit_xor (convert @1) (convert @2))))
1896 /* Convert abs (abs (X)) into abs (X).
1897    also absu (absu (X)) into absu (X).  */
1898 (simplify
1899  (abs (abs@1 @0))
1900  @1)
1902 (simplify
1903  (absu (convert@2 (absu@1 @0)))
1904  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1905   @1))
1907 /* Convert abs[u] (-X) -> abs[u] (X).  */
1908 (simplify
1909  (abs (negate @0))
1910  (abs @0))
1912 (simplify
1913  (absu (negate @0))
1914  (absu @0))
1916 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1917 (simplify
1918  (abs tree_expr_nonnegative_p@0)
1919  @0)
1921 (simplify
1922  (absu tree_expr_nonnegative_p@0)
1923  (convert @0))
1925 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1926 (simplify
1927  (mult:c (nop_convert1?
1928           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1929                     integer_onep))
1930          (nop_convert3? @0))
1931  (if (INTEGRAL_TYPE_P (type)
1932       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1933       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1934   (if (TYPE_UNSIGNED (type))
1935    (absu @0)
1936    (abs @0)
1937   )
1941 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1942 (match negate_expr_p
1943  INTEGER_CST
1944  (if ((INTEGRAL_TYPE_P (type)
1945        && TYPE_UNSIGNED (type))
1946       || (!TYPE_OVERFLOW_SANITIZED (type)
1947           && may_negate_without_overflow_p (t)))))
1948 (match negate_expr_p
1949  FIXED_CST)
1950 (match negate_expr_p
1951  (negate @0)
1952  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1953 (match negate_expr_p
1954  REAL_CST
1955  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1956 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1957    ways.  */
1958 (match negate_expr_p
1959  VECTOR_CST
1960  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1961 (match negate_expr_p
1962  (minus @0 @1)
1963  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1964       || (FLOAT_TYPE_P (type)
1965           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1966           && !HONOR_SIGNED_ZEROS (type)))))
1968 /* (-A) * (-B) -> A * B  */
1969 (simplify
1970  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1971   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1972        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1973    (mult (convert @0) (convert (negate @1)))))
1975 /* -(A + B) -> (-B) - A.  */
1976 (simplify
1977  (negate (plus:c @0 negate_expr_p@1))
1978  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1979       && !HONOR_SIGNED_ZEROS (type))
1980   (minus (negate @1) @0)))
1982 /* -(A - B) -> B - A.  */
1983 (simplify
1984  (negate (minus @0 @1))
1985  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1986       || (FLOAT_TYPE_P (type)
1987           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1988           && !HONOR_SIGNED_ZEROS (type)))
1989   (minus @1 @0)))
1990 (simplify
1991  (negate (pointer_diff @0 @1))
1992  (if (TYPE_OVERFLOW_UNDEFINED (type))
1993   (pointer_diff @1 @0)))
1995 /* A - B -> A + (-B) if B is easily negatable.  */
1996 (simplify
1997  (minus @0 negate_expr_p@1)
1998  (if (!FIXED_POINT_TYPE_P (type))
1999  (plus @0 (negate @1))))
2001 /* 1 - a is a ^ 1 if a had a bool range. */
2002 /* This is only enabled for gimple as sometimes
2003    cfun is not set for the function which contains
2004    the SSA_NAME (e.g. while IPA passes are happening,
2005    fold might be called).  */
2006 (simplify
2007  (minus integer_onep@0 SSA_NAME@1)
2008   (if (INTEGRAL_TYPE_P (type)
2009        && ssa_name_has_boolean_range (@1))
2010    (bit_xor @1 @0)))
2012 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2013 (simplify
2014  (negate (mult:c@0 @1 negate_expr_p@2))
2015  (if (! TYPE_UNSIGNED (type)
2016       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2017       && single_use (@0))
2018   (mult @1 (negate @2))))
2020 (simplify
2021  (negate (rdiv@0 @1 negate_expr_p@2))
2022  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2023       && single_use (@0))
2024   (rdiv @1 (negate @2))))
2026 (simplify
2027  (negate (rdiv@0 negate_expr_p@1 @2))
2028  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2029       && single_use (@0))
2030   (rdiv (negate @1) @2)))
2032 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2033 (simplify
2034  (negate (convert? (rshift @0 INTEGER_CST@1)))
2035  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2036       && wi::to_wide (@1) == element_precision (type) - 1)
2037   (with { tree stype = TREE_TYPE (@0);
2038           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2039                                              : unsigned_type_for (stype); }
2040    (if (VECTOR_TYPE_P (type))
2041     (view_convert (rshift (view_convert:ntype @0) @1))
2042     (convert (rshift (convert:ntype @0) @1))))))
2044 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2045    when profitable.
2046    For bitwise binary operations apply operand conversions to the
2047    binary operation result instead of to the operands.  This allows
2048    to combine successive conversions and bitwise binary operations.
2049    We combine the above two cases by using a conditional convert.  */
2050 (for bitop (bit_and bit_ior bit_xor)
2051  (simplify
2052   (bitop (convert@2 @0) (convert?@3 @1))
2053   (if (((TREE_CODE (@1) == INTEGER_CST
2054          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2055          && (int_fits_type_p (@1, TREE_TYPE (@0))
2056              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2057         || types_match (@0, @1))
2058        && !POINTER_TYPE_P (TREE_TYPE (@0))
2059        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2060        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2061        /* ???  This transform conflicts with fold-const.cc doing
2062           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2063           constants (if x has signed type, the sign bit cannot be set
2064           in c).  This folds extension into the BIT_AND_EXPR.
2065           Restrict it to GIMPLE to avoid endless recursions.  */
2066        && (bitop != BIT_AND_EXPR || GIMPLE)
2067        && (/* That's a good idea if the conversion widens the operand, thus
2068               after hoisting the conversion the operation will be narrower.
2069               It is also a good if the conversion is a nop as moves the
2070               conversion to one side; allowing for combining of the conversions.  */
2071            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2072            /* The conversion check for being a nop can only be done at the gimple
2073               level as fold_binary has some re-association code which can conflict
2074               with this if there is a "constant" which is not a full INTEGER_CST.  */
2075            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2076            /* It's also a good idea if the conversion is to a non-integer
2077               mode.  */
2078            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2079            /* Or if the precision of TO is not the same as the precision
2080               of its mode.  */
2081            || !type_has_mode_precision_p (type)
2082            /* In GIMPLE, getting rid of 2 conversions for one new results
2083               in smaller IL.  */
2084            || (GIMPLE
2085                && TREE_CODE (@1) != INTEGER_CST
2086                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2087                && single_use (@2)
2088                && single_use (@3))))
2089    (convert (bitop @0 (convert @1)))))
2090  /* In GIMPLE, getting rid of 2 conversions for one new results
2091     in smaller IL.  */
2092  (simplify
2093   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2094   (if (GIMPLE
2095        && TREE_CODE (@1) != INTEGER_CST
2096        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2097        && types_match (type, @0)
2098        && !POINTER_TYPE_P (TREE_TYPE (@0))
2099        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2100    (bitop @0 (convert @1)))))
2102 (for bitop (bit_and bit_ior)
2103      rbitop (bit_ior bit_and)
2104   /* (x | y) & x -> x */
2105   /* (x & y) | x -> x */
2106  (simplify
2107   (bitop:c (rbitop:c @0 @1) @0)
2108   @0)
2109  /* (~x | y) & x -> x & y */
2110  /* (~x & y) | x -> x | y */
2111  (simplify
2112   (bitop:c (rbitop:c @2 @1) @0)
2113   (with { bool wascmp; }
2114    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2115         && (!wascmp || element_precision (type) == 1))
2116     (bitop @0 @1))))
2117   /* (x | y) & (x & z) -> (x & z) */
2118   /* (x & y) | (x | z) -> (x | z) */
2119  (simplify
2120   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2121   @3)
2122  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2123  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2124  (simplify
2125   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2126   (bitop @0 @3))
2127  /* x & ~(y | x) -> 0 */
2128  /* x | ~(y & x) -> -1 */
2129  (simplify
2130   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2131   (if (bitop == BIT_AND_EXPR)
2132    { build_zero_cst (type); }
2133    { build_minus_one_cst (type); })))
2135 /* ((x | y) & z) | x -> (z & y) | x
2136    ((x ^ y) & z) | x -> (z & y) | x  */
2137 (for op (bit_ior bit_xor)
2138  (simplify
2139   (bit_ior:c (nop_convert1?:s
2140                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2141   (if (bitwise_equal_p (@0, @3))
2142    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2144 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2145 (simplify
2146   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2147   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2149 /* Combine successive equal operations with constants.  */
2150 (for bitop (bit_and bit_ior bit_xor)
2151  (simplify
2152   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2153   (if (!CONSTANT_CLASS_P (@0))
2154    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2155       folded to a constant.  */
2156    (bitop @0 (bitop! @1 @2))
2157    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2158       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2159       the values involved are such that the operation can't be decided at
2160       compile time.  Try folding one of @0 or @1 with @2 to see whether
2161       that combination can be decided at compile time.
2163       Keep the existing form if both folds fail, to avoid endless
2164       oscillation.  */
2165    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2166     (if (cst1)
2167      (bitop @1 { cst1; })
2168      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2169       (if (cst2)
2170        (bitop @0 { cst2; }))))))))
2172 /* Try simple folding for X op !X, and X op X with the help
2173    of the truth_valued_p and logical_inverted_value predicates.  */
2174 (match truth_valued_p
2175  @0
2176  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2177 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2178  (match truth_valued_p
2179   (op @0 @1)))
2180 (match truth_valued_p
2181   (truth_not @0))
2183 (match (logical_inverted_value @0)
2184  (truth_not @0))
2185 (match (logical_inverted_value @0)
2186  (bit_not truth_valued_p@0))
2187 (match (logical_inverted_value @0)
2188  (eq @0 integer_zerop))
2189 (match (logical_inverted_value @0)
2190  (ne truth_valued_p@0 integer_truep))
2191 (match (logical_inverted_value @0)
2192  (bit_xor truth_valued_p@0 integer_truep))
2194 /* X & !X -> 0.  */
2195 (simplify
2196  (bit_and:c @0 (logical_inverted_value @0))
2197  { build_zero_cst (type); })
2198 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2199 (for op (bit_ior bit_xor)
2200  (simplify
2201   (op:c truth_valued_p@0 (logical_inverted_value @0))
2202   { constant_boolean_node (true, type); }))
2203 /* X ==/!= !X is false/true.  */
2204 (for op (eq ne)
2205  (simplify
2206   (op:c truth_valued_p@0 (logical_inverted_value @0))
2207   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2209 /* ~~x -> x */
2210 (simplify
2211   (bit_not (bit_not @0))
2212   @0)
2214 /* zero_one_valued_p will match when a value is known to be either
2215    0 or 1 including constants 0 or 1.
2216    Signed 1-bits includes -1 so they cannot match here. */
2217 (match zero_one_valued_p
2218  @0
2219  (if (INTEGRAL_TYPE_P (type)
2220       && (TYPE_UNSIGNED (type)
2221           || TYPE_PRECISION (type) > 1)
2222       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2223 (match zero_one_valued_p
2224  truth_valued_p@0
2225  (if (INTEGRAL_TYPE_P (type)
2226       && (TYPE_UNSIGNED (type)
2227           || TYPE_PRECISION (type) > 1))))
2229 /* (a&1) is always [0,1] too. This is useful again when
2230    the range is not known. */
2231 /* Note this can't be recursive due to VN handling of equivalents,
2232    VN and would cause an infinite recursion. */
2233 (match zero_one_valued_p
2234  (bit_and:c@0 @1 integer_onep)
2235  (if (INTEGRAL_TYPE_P (type))))
2237 /* A conversion from an zero_one_valued_p is still a [0,1].
2238    This is useful when the range of a variable is not known */
2239 /* Note this matches can't be recursive because of the way VN handles
2240    nop conversions being equivalent and then recursive between them. */
2241 (match zero_one_valued_p
2242  (convert@0 @1)
2243  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2244       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2245           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2246       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2248 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2249 (simplify
2250  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2251  (if (INTEGRAL_TYPE_P (type))
2252   (bit_and @0 @1)))
2254 (for cmp (tcc_comparison)
2255      icmp (inverted_tcc_comparison)
2256  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2257  (simplify
2258   (bit_ior
2259    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2260    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2261     (if (INTEGRAL_TYPE_P (type)
2262          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2263          /* The scalar version has to be canonicalized after vectorization
2264             because it makes unconditional loads conditional ones, which
2265             means we lose vectorization because the loads may trap.  */
2266          && canonicalize_math_after_vectorization_p ())
2267      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2269  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2270     canonicalized further and we recognize the conditional form:
2271     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2272  (simplify
2273   (bit_ior
2274    (cond (cmp@0  @01 @02) @3 zerop)
2275    (cond (icmp@4 @01 @02) @5 zerop))
2276     (if (INTEGRAL_TYPE_P (type)
2277          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2278          /* The scalar version has to be canonicalized after vectorization
2279             because it makes unconditional loads conditional ones, which
2280             means we lose vectorization because the loads may trap.  */
2281          && canonicalize_math_after_vectorization_p ())
2282     (cond @0 @3 @5)))
2284  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2285     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2286  (simplify
2287   (bit_ior
2288    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2289    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2290     (if (integer_zerop (@5)
2291          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2292      (switch
2293       (if (integer_onep (@4))
2294        (bit_and (vec_cond @0 @2 @3) @4))
2295         (if (integer_minus_onep (@4))
2296          (vec_cond @0 @2 @3)))
2297     (if (integer_zerop (@4)
2298          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2299      (switch
2300       (if (integer_onep (@5))
2301        (bit_and (vec_cond @0 @3 @2) @5))
2302       (if (integer_minus_onep (@5))
2303        (vec_cond @0 @3 @2))))))
2305  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2306     into a < b ? d : c.  */
2307  (simplify
2308   (bit_ior
2309    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2310    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2311   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2312    (vec_cond @0 @2 @3))))
2314 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2315 (simplify
2316  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2317  (if (INTEGRAL_TYPE_P (type)
2318       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2319       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2320       /* Sign extending of the neg or a truncation of the neg
2321          is needed. */
2322       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2323           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2324   (mult (convert @0) @1)))
2326 /* Narrow integer multiplication by a zero_one_valued_p operand.
2327    Multiplication by [0,1] is guaranteed not to overflow.  */
2328 (simplify
2329  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2330  (if (INTEGRAL_TYPE_P (type)
2331       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2333   (mult (convert @1) (convert @2))))
2335 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2336    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2337    as some targets (such as x86's SSE) may return zero for larger C.  */
2338 (simplify
2339   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2340   (if (tree_fits_shwi_p (@1)
2341        && tree_to_shwi (@1) > 0
2342        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2343     (convert @0)))
2345 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2346    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2347    as some targets (such as x86's SSE) may return zero for larger C.  */
2348 (simplify
2349   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2350   (if (tree_fits_shwi_p (@1)
2351        && tree_to_shwi (@1) > 0
2352        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2353     (eq @0 @2)))
2355 /* Convert ~ (-A) to A - 1.  */
2356 (simplify
2357  (bit_not (convert? (negate @0)))
2358  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2359       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2360   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2362 /* Convert - (~A) to A + 1.  */
2363 (simplify
2364  (negate (nop_convert? (bit_not @0)))
2365  (plus (view_convert @0) { build_each_one_cst (type); }))
2367 /* (a & b) ^ (a == b) -> !(a | b) */
2368 /* (a & b) == (a ^ b) -> !(a | b) */
2369 (for first_op (bit_xor eq)
2370      second_op (eq bit_xor)
2371  (simplify
2372   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2373     (bit_not (bit_ior @0 @1))))
2375 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2376 (simplify
2377  (bit_not (convert? (minus @0 integer_each_onep)))
2378  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2379       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2380   (convert (negate @0))))
2381 (simplify
2382  (bit_not (convert? (plus @0 integer_all_onesp)))
2383  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2384       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2385   (convert (negate @0))))
2387 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2388 (simplify
2389  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2390  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2391   (convert (bit_xor @0 (bit_not @1)))))
2392 (simplify
2393  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2394  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2395   (convert (bit_xor @0 @1))))
2397 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2398 (simplify
2399  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2400  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2401   (bit_not (bit_xor (view_convert @0) @1))))
2403 /* ~(a ^ b) is a == b for truth valued a and b.  */
2404 (simplify
2405  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2406  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2408   (convert (eq @0 @1))))
2410 /* (~a) == b is a ^ b for truth valued a and b.  */
2411 (simplify
2412  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2413  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2414       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2415   (convert (bit_xor @0 @1))))
2417 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2418 (simplify
2419  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2420  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2422 /* Fold A - (A & B) into ~B & A.  */
2423 (simplify
2424  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2425  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2426       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2427   (convert (bit_and (bit_not @1) @0))))
2429 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2430 (if (!canonicalize_math_p ())
2431  (for cmp (tcc_comparison)
2432   (simplify
2433    (mult:c (convert (cmp@0 @1 @2)) @3)
2434    (if (INTEGRAL_TYPE_P (type)
2435         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2436      (cond @0 @3 { build_zero_cst (type); })))
2437 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2438   (simplify
2439    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2440    (if (INTEGRAL_TYPE_P (type)
2441         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2442      (cond @0 @3 { build_zero_cst (type); })))
2446 /* For integral types with undefined overflow and C != 0 fold
2447    x * C EQ/NE y * C into x EQ/NE y.  */
2448 (for cmp (eq ne)
2449  (simplify
2450   (cmp (mult:c @0 @1) (mult:c @2 @1))
2451   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2452        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2453        && tree_expr_nonzero_p (@1))
2454    (cmp @0 @2))))
2456 /* For integral types with wrapping overflow and C odd fold
2457    x * C EQ/NE y * C into x EQ/NE y.  */
2458 (for cmp (eq ne)
2459  (simplify
2460   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2461   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2462        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2463        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2464    (cmp @0 @2))))
2466 /* For integral types with undefined overflow and C != 0 fold
2467    x * C RELOP y * C into:
2469    x RELOP y for nonnegative C
2470    y RELOP x for negative C  */
2471 (for cmp (lt gt le ge)
2472  (simplify
2473   (cmp (mult:c @0 @1) (mult:c @2 @1))
2474   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2475        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2476    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2477     (cmp @0 @2)
2478    (if (TREE_CODE (@1) == INTEGER_CST
2479         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2480     (cmp @2 @0))))))
2482 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2483 (for cmp (le gt)
2484      icmp (gt le)
2485  (simplify
2486   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2487    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2488         && TYPE_UNSIGNED (TREE_TYPE (@0))
2489         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2490         && (wi::to_wide (@2)
2491             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2492     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2493      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2495 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2496 (for cmp (simple_comparison)
2497  (simplify
2498   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2499   (if (element_precision (@3) >= element_precision (@0)
2500        && types_match (@0, @1))
2501    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2502     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2503      (cmp @1 @0)
2504      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2505       (with
2506        {
2507         tree utype = unsigned_type_for (TREE_TYPE (@0));
2508        }
2509        (cmp (convert:utype @1) (convert:utype @0)))))
2510     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2511      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2512       (cmp @0 @1)
2513       (with
2514        {
2515         tree utype = unsigned_type_for (TREE_TYPE (@0));
2516        }
2517        (cmp (convert:utype @0) (convert:utype @1)))))))))
2519 /* X / C1 op C2 into a simple range test.  */
2520 (for cmp (simple_comparison)
2521  (simplify
2522   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2523   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524        && integer_nonzerop (@1)
2525        && !TREE_OVERFLOW (@1)
2526        && !TREE_OVERFLOW (@2))
2527    (with { tree lo, hi; bool neg_overflow;
2528            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2529                                                    &neg_overflow); }
2530     (switch
2531      (if (code == LT_EXPR || code == GE_EXPR)
2532        (if (TREE_OVERFLOW (lo))
2533         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2534         (if (code == LT_EXPR)
2535          (lt @0 { lo; })
2536          (ge @0 { lo; }))))
2537      (if (code == LE_EXPR || code == GT_EXPR)
2538        (if (TREE_OVERFLOW (hi))
2539         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2540         (if (code == LE_EXPR)
2541          (le @0 { hi; })
2542          (gt @0 { hi; }))))
2543      (if (!lo && !hi)
2544       { build_int_cst (type, code == NE_EXPR); })
2545      (if (code == EQ_EXPR && !hi)
2546       (ge @0 { lo; }))
2547      (if (code == EQ_EXPR && !lo)
2548       (le @0 { hi; }))
2549      (if (code == NE_EXPR && !hi)
2550       (lt @0 { lo; }))
2551      (if (code == NE_EXPR && !lo)
2552       (gt @0 { hi; }))
2553      (if (GENERIC)
2554       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2555                            lo, hi); })
2556      (with
2557       {
2558         tree etype = range_check_type (TREE_TYPE (@0));
2559         if (etype)
2560           {
2561             hi = fold_convert (etype, hi);
2562             lo = fold_convert (etype, lo);
2563             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2564           }
2565       }
2566       (if (etype && hi && !TREE_OVERFLOW (hi))
2567        (if (code == EQ_EXPR)
2568         (le (minus (convert:etype @0) { lo; }) { hi; })
2569         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2571 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2572 (for op (lt le ge gt)
2573  (simplify
2574   (op (plus:c @0 @2) (plus:c @1 @2))
2575   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2576        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2577    (op @0 @1))))
2579 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2580    when C is an unsigned integer constant with only the MSB set, and X and
2581    Y have types of equal or lower integer conversion rank than C's.  */
2582 (for op (lt le ge gt)
2583  (simplify
2584   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2585   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2586        && TYPE_UNSIGNED (TREE_TYPE (@0))
2587        && wi::only_sign_bit_p (wi::to_wide (@0)))
2588    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2589     (op (convert:stype @1) (convert:stype @2))))))
2591 /* For equality and subtraction, this is also true with wrapping overflow.  */
2592 (for op (eq ne minus)
2593  (simplify
2594   (op (plus:c @0 @2) (plus:c @1 @2))
2595   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2596        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2597            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2598    (op @0 @1))))
2600 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2601 (for op (lt le ge gt)
2602  (simplify
2603   (op (minus @0 @2) (minus @1 @2))
2604   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2605        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2606    (op @0 @1))))
2607 /* For equality and subtraction, this is also true with wrapping overflow.  */
2608 (for op (eq ne minus)
2609  (simplify
2610   (op (minus @0 @2) (minus @1 @2))
2611   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2612        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2613            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2614    (op @0 @1))))
2615 /* And for pointers...  */
2616 (for op (simple_comparison)
2617  (simplify
2618   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2619   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2620    (op @0 @1))))
2621 (simplify
2622  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2623  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2624       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2625   (pointer_diff @0 @1)))
2627 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2628 (for op (lt le ge gt)
2629  (simplify
2630   (op (minus @2 @0) (minus @2 @1))
2631   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2632        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2633    (op @1 @0))))
2634 /* For equality and subtraction, this is also true with wrapping overflow.  */
2635 (for op (eq ne minus)
2636  (simplify
2637   (op (minus @2 @0) (minus @2 @1))
2638   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2639        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2640            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2641    (op @1 @0))))
2642 /* And for pointers...  */
2643 (for op (simple_comparison)
2644  (simplify
2645   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2646   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2647    (op @1 @0))))
2648 (simplify
2649  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2650  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2651       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2652   (pointer_diff @1 @0)))
2654 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2655 (for op (lt le gt ge)
2656  (simplify
2657   (op:c (plus:c@2 @0 @1) @1)
2658   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2659        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2660        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2661        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2662    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2663 /* For equality, this is also true with wrapping overflow.  */
2664 (for op (eq ne)
2665  (simplify
2666   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2667   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2668        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2669            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2670        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2671        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2672        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2673    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2674  (simplify
2675   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2676   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2677        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2678        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2679    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2681 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2682 (for neeq (ne eq)
2683  (simplify
2684   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2685    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2686     (if (ptr_difference_const (@0, @2, &diff))
2687      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2688  (simplify
2689   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2690    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2691     (if (ptr_difference_const (@0, @2, &diff))
2692      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2694 /* X - Y < X is the same as Y > 0 when there is no overflow.
2695    For equality, this is also true with wrapping overflow.  */
2696 (for op (simple_comparison)
2697  (simplify
2698   (op:c @0 (minus@2 @0 @1))
2699   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2700        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2701            || ((op == EQ_EXPR || op == NE_EXPR)
2702                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2703        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2704    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2706 /* Transform:
2707    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2708    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2709 (for cmp (eq ne)
2710      ocmp (lt ge)
2711  (simplify
2712   (cmp (trunc_div @0 @1) integer_zerop)
2713   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2714        /* Complex ==/!= is allowed, but not </>=.  */
2715        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2716        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2717    (ocmp @0 @1))))
2719 /* X == C - X can never be true if C is odd.  */
2720 (for cmp (eq ne)
2721  (simplify
2722   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2723   (if (TREE_INT_CST_LOW (@1) & 1)
2724    { constant_boolean_node (cmp == NE_EXPR, type); })))
2727    U & N <= U  -> true
2728    U & N >  U  -> false
2729    U needs to be non-negative.
2731    U | N <  U  -> false
2732    U | N >= U  -> true
2733    U and N needs to be non-negative
2735    U | N <  U  -> true
2736    U | N >= U  -> false
2737    U needs to be non-negative and N needs to be a negative constant.
2738    */
2739 (for cmp   (lt      ge      le      gt     )
2740      bitop (bit_ior bit_ior bit_and bit_and)
2741  (simplify
2742   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2743   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2744    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2745     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2746     /* The sign is opposite now so the comparison is swapped around. */
2747     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2748      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2750 /* Arguments on which one can call get_nonzero_bits to get the bits
2751    possibly set.  */
2752 (match with_possible_nonzero_bits
2753  INTEGER_CST@0)
2754 (match with_possible_nonzero_bits
2755  SSA_NAME@0
2756  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2757 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2758 (match (with_possible_nonzero_bits2 @0)
2759  with_possible_nonzero_bits@0)
2760 (match (with_possible_nonzero_bits2 @0)
2761  (bit_and:c with_possible_nonzero_bits@0 @2))
2763 /* Same for bits that are known to be set, but we do not have
2764    an equivalent to get_nonzero_bits yet.  */
2765 (match (with_certain_nonzero_bits2 @0)
2766  INTEGER_CST@0)
2767 (match (with_certain_nonzero_bits2 @0)
2768  (bit_ior @1 INTEGER_CST@0))
2770 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2771 (for cmp (eq ne)
2772  (simplify
2773   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2774   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2775    { constant_boolean_node (cmp == NE_EXPR, type); })))
2777 /* ((X inner_op C0) outer_op C1)
2778    With X being a tree where value_range has reasoned certain bits to always be
2779    zero throughout its computed value range,
2780    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2781    where zero_mask has 1's for all bits that are sure to be 0 in
2782    and 0's otherwise.
2783    if (inner_op == '^') C0 &= ~C1;
2784    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2785    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2787 (for inner_op (bit_ior bit_xor)
2788      outer_op (bit_xor bit_ior)
2789 (simplify
2790  (outer_op
2791   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2792  (with
2793   {
2794     bool fail = false;
2795     wide_int zero_mask_not;
2796     wide_int C0;
2797     wide_int cst_emit;
2799     if (TREE_CODE (@2) == SSA_NAME)
2800       zero_mask_not = get_nonzero_bits (@2);
2801     else
2802       fail = true;
2804     if (inner_op == BIT_XOR_EXPR)
2805       {
2806         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2807         cst_emit = C0 | wi::to_wide (@1);
2808       }
2809     else
2810       {
2811         C0 = wi::to_wide (@0);
2812         cst_emit = C0 ^ wi::to_wide (@1);
2813       }
2814   }
2815   (if (!fail && (C0 & zero_mask_not) == 0)
2816    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2817    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2818     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2820 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2821 (simplify
2822   (pointer_plus (pointer_plus:s @0 @1) @3)
2823   (pointer_plus @0 (plus @1 @3)))
2824 #if GENERIC
2825 (simplify
2826   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2827   (convert:type (pointer_plus @0 (plus @1 @3))))
2828 #endif
2830 /* Pattern match
2831      tem1 = (long) ptr1;
2832      tem2 = (long) ptr2;
2833      tem3 = tem2 - tem1;
2834      tem4 = (unsigned long) tem3;
2835      tem5 = ptr1 + tem4;
2836    and produce
2837      tem5 = ptr2;  */
2838 (simplify
2839   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2840   /* Conditionally look through a sign-changing conversion.  */
2841   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2842        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2843             || (GENERIC && type == TREE_TYPE (@1))))
2844    @1))
2845 (simplify
2846   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2847   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2848    (convert @1)))
2850 /* Pattern match
2851      tem = (sizetype) ptr;
2852      tem = tem & algn;
2853      tem = -tem;
2854      ... = ptr p+ tem;
2855    and produce the simpler and easier to analyze with respect to alignment
2856      ... = ptr & ~algn;  */
2857 (simplify
2858   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2859   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2860    (bit_and @0 { algn; })))
2862 /* Try folding difference of addresses.  */
2863 (simplify
2864  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2865  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2866   (with { poly_int64 diff; }
2867    (if (ptr_difference_const (@0, @1, &diff))
2868     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2869 (simplify
2870  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2871  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2872   (with { poly_int64 diff; }
2873    (if (ptr_difference_const (@0, @1, &diff))
2874     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2875 (simplify
2876  (minus (convert ADDR_EXPR@0) (convert @1))
2877  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2878   (with { poly_int64 diff; }
2879    (if (ptr_difference_const (@0, @1, &diff))
2880     { build_int_cst_type (type, diff); }))))
2881 (simplify
2882  (minus (convert @0) (convert ADDR_EXPR@1))
2883  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2884   (with { poly_int64 diff; }
2885    (if (ptr_difference_const (@0, @1, &diff))
2886     { build_int_cst_type (type, diff); }))))
2887 (simplify
2888  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2889  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2890       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2891   (with { poly_int64 diff; }
2892    (if (ptr_difference_const (@0, @1, &diff))
2893     { build_int_cst_type (type, diff); }))))
2894 (simplify
2895  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2896  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2897       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2898   (with { poly_int64 diff; }
2899    (if (ptr_difference_const (@0, @1, &diff))
2900     { build_int_cst_type (type, diff); }))))
2902 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2903 (simplify
2904  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2905  (with { poly_int64 diff; }
2906    (if (ptr_difference_const (@0, @2, &diff))
2907     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2908 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2909 (simplify
2910  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2911  (with { poly_int64 diff; }
2912    (if (ptr_difference_const (@0, @2, &diff))
2913     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2914 (simplify
2915  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2916  (with { poly_int64 diff; }
2917    (if (ptr_difference_const (@0, @1, &diff))
2918     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2920 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2921 (simplify
2922  (convert (pointer_diff @0 INTEGER_CST@1))
2923  (if (POINTER_TYPE_P (type))
2924   { build_fold_addr_expr_with_type
2925       (build2 (MEM_REF, char_type_node, @0,
2926                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2927                type); }))
2929 /* If arg0 is derived from the address of an object or function, we may
2930    be able to fold this expression using the object or function's
2931    alignment.  */
2932 (simplify
2933  (bit_and (convert? @0) INTEGER_CST@1)
2934  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2935       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2936   (with
2937    {
2938      unsigned int align;
2939      unsigned HOST_WIDE_INT bitpos;
2940      get_pointer_alignment_1 (@0, &align, &bitpos);
2941    }
2942    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2943     { wide_int_to_tree (type, (wi::to_wide (@1)
2944                                & (bitpos / BITS_PER_UNIT))); }))))
2946 (match min_value
2947  uniform_integer_cst_p
2948  (with {
2949    tree int_cst = uniform_integer_cst_p (t);
2950    tree inner_type = TREE_TYPE (int_cst);
2951   }
2952   (if ((INTEGRAL_TYPE_P (inner_type)
2953         || POINTER_TYPE_P (inner_type))
2954        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2956 (match max_value
2957  uniform_integer_cst_p
2958  (with {
2959    tree int_cst = uniform_integer_cst_p (t);
2960    tree itype = TREE_TYPE (int_cst);
2961   }
2962  (if ((INTEGRAL_TYPE_P (itype)
2963        || POINTER_TYPE_P (itype))
2964       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2966 /* x >  y  &&  x != XXX_MIN  -->  x > y
2967    x >  y  &&  x == XXX_MIN  -->  false . */
2968 (for eqne (eq ne)
2969  (simplify
2970   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2971    (switch
2972     (if (eqne == EQ_EXPR)
2973      { constant_boolean_node (false, type); })
2974     (if (eqne == NE_EXPR)
2975      @2)
2976     )))
2978 /* x <  y  &&  x != XXX_MAX  -->  x < y
2979    x <  y  &&  x == XXX_MAX  -->  false.  */
2980 (for eqne (eq ne)
2981  (simplify
2982   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2983    (switch
2984     (if (eqne == EQ_EXPR)
2985      { constant_boolean_node (false, type); })
2986     (if (eqne == NE_EXPR)
2987      @2)
2988     )))
2990 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2991 (simplify
2992  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2993   @2)
2995 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2996 (simplify
2997  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2998   @2)
3000 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3001 (simplify
3002  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3003   @2)
3005 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3006 (simplify
3007  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3008   { constant_boolean_node (true, type); })
3010 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3011 (simplify
3012  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3013   @2)
3015 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3016 (simplify
3017  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3018   @2)
3020 /* x >=  y  ||  x != XXX_MAX   -->  true
3021    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3022 (for eqne (eq ne)
3023  (simplify
3024   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3025    (switch
3026     (if (eqne == EQ_EXPR)
3027      @2)
3028     (if (eqne == NE_EXPR)
3029      { constant_boolean_node (true, type); }))))
3031 /* y == XXX_MIN || x < y --> x <= y - 1 */
3032 (simplify
3033  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3034   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3035        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3036   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3038 /* y != XXX_MIN && x >= y --> x > y - 1 */
3039 (simplify
3040  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3041   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3042        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3043   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3045 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3046    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3047 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3048    Similarly for (X != Y).  */
3050 (for code1 (eq ne)
3051  (for code2 (eq ne lt gt le ge)
3052   (simplify
3053    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3054    (if ((TREE_CODE (@1) == INTEGER_CST
3055          && TREE_CODE (@2) == INTEGER_CST)
3056         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3057              || POINTER_TYPE_P (TREE_TYPE (@1)))
3058             && bitwise_equal_p (@1, @2)))
3059     (with
3060      {
3061       bool one_before = false;
3062       bool one_after = false;
3063       int cmp = 0;
3064       bool allbits = true;
3065       if (TREE_CODE (@1) == INTEGER_CST
3066           && TREE_CODE (@2) == INTEGER_CST)
3067         {
3068           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3069           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3070           auto t2 = wi::to_wide (@2);
3071           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3072           if (cmp < 0
3073               && t1 == t2 - 1)
3074             one_before = true;
3075           if (cmp > 0
3076               && t1 == t2 + 1)
3077             one_after = true;
3078         }
3079       bool val;
3080       switch (code2)
3081          {
3082         case EQ_EXPR: val = (cmp == 0); break;
3083         case NE_EXPR: val = (cmp != 0); break;
3084         case LT_EXPR: val = (cmp < 0); break;
3085         case GT_EXPR: val = (cmp > 0); break;
3086         case LE_EXPR: val = (cmp <= 0); break;
3087         case GE_EXPR: val = (cmp >= 0); break;
3088         default: gcc_unreachable ();
3089         }
3090      }
3091      (switch
3092       (if (code1 == EQ_EXPR && val) @3)
3093       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3094       (if (code1 == NE_EXPR && !val && allbits) @4)
3095       (if (code1 == NE_EXPR
3096            && code2 == GE_EXPR
3097            && cmp == 0
3098            && allbits)
3099        (gt @c0 (convert @1)))
3100       (if (code1 == NE_EXPR
3101            && code2 == LE_EXPR
3102            && cmp == 0
3103            && allbits)
3104        (lt @c0 (convert @1)))
3105       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3106       (if (code1 == NE_EXPR
3107            && code2 == GT_EXPR
3108            && one_after
3109            && allbits)
3110        (gt @c0 (convert @1)))
3111       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3112       (if (code1 == NE_EXPR
3113            && code2 == LT_EXPR
3114            && one_before
3115            && allbits)
3116        (lt @c0 (convert @1)))
3117      )
3118     )
3119    )
3120   )
3124 /* Convert (X OP1 CST1) && (X OP2 CST2).
3125    Convert (X OP1 Y) && (X OP2 Y).  */
3127 (for code1 (lt le gt ge)
3128  (for code2 (lt le gt ge)
3129   (simplify
3130   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3131   (if ((TREE_CODE (@1) == INTEGER_CST
3132         && TREE_CODE (@2) == INTEGER_CST)
3133        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3134             || POINTER_TYPE_P (TREE_TYPE (@1)))
3135            && operand_equal_p (@1, @2)))
3136    (with
3137     {
3138      int cmp = 0;
3139      if (TREE_CODE (@1) == INTEGER_CST
3140          && TREE_CODE (@2) == INTEGER_CST)
3141        cmp = tree_int_cst_compare (@1, @2);
3142     }
3143     (switch
3144      /* Choose the more restrictive of two < or <= comparisons.  */
3145      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3146           && (code2 == LT_EXPR || code2 == LE_EXPR))
3147       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3148        @3
3149        @4))
3150      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3151      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3152           && (code2 == GT_EXPR || code2 == GE_EXPR))
3153       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3154        @3
3155        @4))
3156      /* Check for singleton ranges.  */
3157      (if (cmp == 0
3158           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3159             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3160       (eq @0 @1))
3161      /* Check for disjoint ranges.  */
3162      (if (cmp <= 0
3163           && (code1 == LT_EXPR || code1 == LE_EXPR)
3164           && (code2 == GT_EXPR || code2 == GE_EXPR))
3165       { constant_boolean_node (false, type); })
3166      (if (cmp >= 0
3167           && (code1 == GT_EXPR || code1 == GE_EXPR)
3168           && (code2 == LT_EXPR || code2 == LE_EXPR))
3169       { constant_boolean_node (false, type); })
3170      ))))))
3172 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3173    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3174 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3175    Similarly for (X != Y).  */
3177 (for code1 (eq ne)
3178  (for code2 (eq ne lt gt le ge)
3179   (simplify
3180    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3181    (if ((TREE_CODE (@1) == INTEGER_CST
3182          && TREE_CODE (@2) == INTEGER_CST)
3183         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3184             || POINTER_TYPE_P (TREE_TYPE (@1)))
3185             && bitwise_equal_p (@1, @2)))
3186     (with
3187      {
3188       bool one_before = false;
3189       bool one_after = false;
3190       int cmp = 0;
3191       bool allbits = true;
3192       if (TREE_CODE (@1) == INTEGER_CST
3193           && TREE_CODE (@2) == INTEGER_CST)
3194         {
3195           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3196           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3197           auto t2 = wi::to_wide (@2);
3198           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3199           if (cmp < 0
3200               && t1 == t2 - 1)
3201             one_before = true;
3202           if (cmp > 0
3203               && t1 == t2 + 1)
3204             one_after = true;
3205         }
3206       bool val;
3207       switch (code2)
3208         {
3209         case EQ_EXPR: val = (cmp == 0); break;
3210         case NE_EXPR: val = (cmp != 0); break;
3211         case LT_EXPR: val = (cmp < 0); break;
3212         case GT_EXPR: val = (cmp > 0); break;
3213         case LE_EXPR: val = (cmp <= 0); break;
3214         case GE_EXPR: val = (cmp >= 0); break;
3215         default: gcc_unreachable ();
3216         }
3217      }
3218      (switch
3219       (if (code1 == EQ_EXPR && val) @4)
3220       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3221       (if (code1 == NE_EXPR && !val && allbits) @3)
3222       (if (code1 == EQ_EXPR
3223            && code2 == GT_EXPR
3224            && cmp == 0
3225            && allbits)
3226        (ge @c0 @2))
3227       (if (code1 == EQ_EXPR
3228            && code2 == LT_EXPR
3229            && cmp == 0
3230            && allbits)
3231        (le @c0 @2))
3232       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3233       (if (code1 == EQ_EXPR
3234            && code2 == GE_EXPR
3235            && one_before
3236            && allbits)
3237        (ge @c0 (convert @1)))
3238       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3239       (if (code1 == EQ_EXPR
3240            && code2 == LE_EXPR
3241            && one_after
3242            && allbits)
3243        (le @c0 (convert @1)))
3244      )
3245     )
3246    )
3247   )
3251 /* Convert (X OP1 CST1) || (X OP2 CST2).
3252    Convert (X OP1 Y)    || (X OP2 Y).  */
3254 (for code1 (lt le gt ge)
3255  (for code2 (lt le gt ge)
3256   (simplify
3257   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3258   (if ((TREE_CODE (@1) == INTEGER_CST
3259         && TREE_CODE (@2) == INTEGER_CST)
3260        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3261             || POINTER_TYPE_P (TREE_TYPE (@1)))
3262            && operand_equal_p (@1, @2)))
3263    (with
3264     {
3265      int cmp = 0;
3266      if (TREE_CODE (@1) == INTEGER_CST
3267          && TREE_CODE (@2) == INTEGER_CST)
3268        cmp = tree_int_cst_compare (@1, @2);
3269     }
3270     (switch
3271      /* Choose the more restrictive of two < or <= comparisons.  */
3272      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3273           && (code2 == LT_EXPR || code2 == LE_EXPR))
3274       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3275        @4
3276        @3))
3277      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3278      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3279           && (code2 == GT_EXPR || code2 == GE_EXPR))
3280       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3281        @4
3282        @3))
3283      /* Check for singleton ranges.  */
3284      (if (cmp == 0
3285           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3286               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3287       (ne @0 @2))
3288      /* Check for disjoint ranges.  */
3289      (if (cmp >= 0
3290           && (code1 == LT_EXPR || code1 == LE_EXPR)
3291           && (code2 == GT_EXPR || code2 == GE_EXPR))
3292       { constant_boolean_node (true, type); })
3293      (if (cmp <= 0
3294           && (code1 == GT_EXPR || code1 == GE_EXPR)
3295           && (code2 == LT_EXPR || code2 == LE_EXPR))
3296       { constant_boolean_node (true, type); })
3297      ))))))
3299 /* Optimize (a CMP b) ^ (a CMP b)  */
3300 /* Optimize (a CMP b) != (a CMP b)  */
3301 (for op (bit_xor ne)
3302  (for cmp1 (lt lt lt le le le)
3303       cmp2 (gt eq ne ge eq ne)
3304       rcmp (ne le gt ne lt ge)
3305   (simplify
3306    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3307    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3308     (rcmp @0 @1)))))
3310 /* Optimize (a CMP b) == (a CMP b)  */
3311 (for cmp1 (lt lt lt le le le)
3312      cmp2 (gt eq ne ge eq ne)
3313      rcmp (eq gt le eq ge lt)
3314  (simplify
3315   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3316   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3317     (rcmp @0 @1))))
3319 /* We can't reassociate at all for saturating types.  */
3320 (if (!TYPE_SATURATING (type))
3322  /* Contract negates.  */
3323  /* A + (-B) -> A - B */
3324  (simplify
3325   (plus:c @0 (convert? (negate @1)))
3326   /* Apply STRIP_NOPS on the negate.  */
3327   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3328        && !TYPE_OVERFLOW_SANITIZED (type))
3329    (with
3330     {
3331      tree t1 = type;
3332      if (INTEGRAL_TYPE_P (type)
3333          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3334        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3335     }
3336     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3337  /* A - (-B) -> A + B */
3338  (simplify
3339   (minus @0 (convert? (negate @1)))
3340   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3341        && !TYPE_OVERFLOW_SANITIZED (type))
3342    (with
3343     {
3344      tree t1 = type;
3345      if (INTEGRAL_TYPE_P (type)
3346          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3347        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3348     }
3349     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3350  /* -(T)(-A) -> (T)A
3351     Sign-extension is ok except for INT_MIN, which thankfully cannot
3352     happen without overflow.  */
3353  (simplify
3354   (negate (convert (negate @1)))
3355   (if (INTEGRAL_TYPE_P (type)
3356        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3357            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3358                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3359        && !TYPE_OVERFLOW_SANITIZED (type)
3360        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3361    (convert @1)))
3362  (simplify
3363   (negate (convert negate_expr_p@1))
3364   (if (SCALAR_FLOAT_TYPE_P (type)
3365        && ((DECIMAL_FLOAT_TYPE_P (type)
3366             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3367             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3368            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3369    (convert (negate @1))))
3370  (simplify
3371   (negate (nop_convert? (negate @1)))
3372   (if (!TYPE_OVERFLOW_SANITIZED (type)
3373        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3374    (view_convert @1)))
3376  /* We can't reassociate floating-point unless -fassociative-math
3377     or fixed-point plus or minus because of saturation to +-Inf.  */
3378  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3379       && !FIXED_POINT_TYPE_P (type))
3381   /* Match patterns that allow contracting a plus-minus pair
3382      irrespective of overflow issues.  */
3383   /* (A +- B) - A       ->  +- B */
3384   /* (A +- B) -+ B      ->  A */
3385   /* A - (A +- B)       -> -+ B */
3386   /* A +- (B -+ A)      ->  +- B */
3387   (simplify
3388    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3389    (view_convert @1))
3390   (simplify
3391    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3392    (if (!ANY_INTEGRAL_TYPE_P (type)
3393         || TYPE_OVERFLOW_WRAPS (type))
3394    (negate (view_convert @1))
3395    (view_convert (negate @1))))
3396   (simplify
3397    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3398    (view_convert @0))
3399   (simplify
3400    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3401     (if (!ANY_INTEGRAL_TYPE_P (type)
3402          || TYPE_OVERFLOW_WRAPS (type))
3403      (negate (view_convert @1))
3404      (view_convert (negate @1))))
3405   (simplify
3406    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3407    (view_convert @1))
3408   /* (A +- B) + (C - A)   -> C +- B */
3409   /* (A +  B) - (A - C)   -> B + C */
3410   /* More cases are handled with comparisons.  */
3411   (simplify
3412    (plus:c (plus:c @0 @1) (minus @2 @0))
3413    (plus @2 @1))
3414   (simplify
3415    (plus:c (minus @0 @1) (minus @2 @0))
3416    (minus @2 @1))
3417   (simplify
3418    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3419    (if (TYPE_OVERFLOW_UNDEFINED (type)
3420         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3421     (pointer_diff @2 @1)))
3422   (simplify
3423    (minus (plus:c @0 @1) (minus @0 @2))
3424    (plus @1 @2))
3426   /* (A +- CST1) +- CST2 -> A + CST3
3427      Use view_convert because it is safe for vectors and equivalent for
3428      scalars.  */
3429   (for outer_op (plus minus)
3430    (for inner_op (plus minus)
3431         neg_inner_op (minus plus)
3432     (simplify
3433      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3434                CONSTANT_CLASS_P@2)
3435      /* If one of the types wraps, use that one.  */
3436      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3437       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3438          forever if something doesn't simplify into a constant.  */
3439       (if (!CONSTANT_CLASS_P (@0))
3440        (if (outer_op == PLUS_EXPR)
3441         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3442         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3443       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3444            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3445        (if (outer_op == PLUS_EXPR)
3446         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3447         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3448        /* If the constant operation overflows we cannot do the transform
3449           directly as we would introduce undefined overflow, for example
3450           with (a - 1) + INT_MIN.  */
3451        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3452         (with { tree cst = const_binop (outer_op == inner_op
3453                                         ? PLUS_EXPR : MINUS_EXPR,
3454                                         type, @1, @2); }
3455          (if (cst)
3456           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3457            (inner_op @0 { cst; } )
3458            /* X+INT_MAX+1 is X-INT_MIN.  */
3459            (if (INTEGRAL_TYPE_P (type)
3460                 && wi::to_wide (cst) == wi::min_value (type))
3461             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3462             /* Last resort, use some unsigned type.  */
3463             (with { tree utype = unsigned_type_for (type); }
3464              (if (utype)
3465               (view_convert (inner_op
3466                              (view_convert:utype @0)
3467                              (view_convert:utype
3468                               { TREE_OVERFLOW (cst)
3469                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3471   /* (CST1 - A) +- CST2 -> CST3 - A  */
3472   (for outer_op (plus minus)
3473    (simplify
3474     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3475     /* If one of the types wraps, use that one.  */
3476     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3477      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3478         forever if something doesn't simplify into a constant.  */
3479      (if (!CONSTANT_CLASS_P (@0))
3480       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3481      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3482           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3483       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3484       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3485        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3486         (if (cst && !TREE_OVERFLOW (cst))
3487          (minus { cst; } @0))))))))
3489   /* CST1 - (CST2 - A) -> CST3 + A
3490      Use view_convert because it is safe for vectors and equivalent for
3491      scalars.  */
3492   (simplify
3493    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3494    /* If one of the types wraps, use that one.  */
3495    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3496     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3497       forever if something doesn't simplify into a constant.  */
3498     (if (!CONSTANT_CLASS_P (@0))
3499      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3500     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3501          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3502      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3503      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3504       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3505        (if (cst && !TREE_OVERFLOW (cst))
3506         (plus { cst; } @0)))))))
3508 /* ((T)(A)) + CST -> (T)(A + CST)  */
3509 #if GIMPLE
3510   (simplify
3511    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3512     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3513          && TREE_CODE (type) == INTEGER_TYPE
3514          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3515          && int_fits_type_p (@1, TREE_TYPE (@0)))
3516      /* Perform binary operation inside the cast if the constant fits
3517         and (A + CST)'s range does not overflow.  */
3518      (with
3519       {
3520         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3521                           max_ovf = wi::OVF_OVERFLOW;
3522         tree inner_type = TREE_TYPE (@0);
3524         wide_int w1
3525           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3526                             TYPE_SIGN (inner_type));
3528         value_range vr;
3529         if (get_global_range_query ()->range_of_expr (vr, @0)
3530             && !vr.varying_p () && !vr.undefined_p ())
3531           {
3532             wide_int wmin0 = vr.lower_bound ();
3533             wide_int wmax0 = vr.upper_bound ();
3534             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3535             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3536           }
3537       }
3538      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3539       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3540      )))
3541 #endif
3543 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3544 #if GIMPLE
3545   (for op (plus minus)
3546    (simplify
3547     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3548      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3549           && TREE_CODE (type) == INTEGER_TYPE
3550           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3551           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3552           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3553           && TYPE_OVERFLOW_WRAPS (type))
3554        (plus (convert @0) (op @2 (convert @1))))))
3555 #endif
3557 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3558    to a simple value.  */
3559   (for op (plus minus)
3560    (simplify
3561     (op (convert @0) (convert @1))
3562      (if (INTEGRAL_TYPE_P (type)
3563           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3564           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3565           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3566           && !TYPE_OVERFLOW_TRAPS (type)
3567           && !TYPE_OVERFLOW_SANITIZED (type))
3568       (convert (op! @0 @1)))))
3570   /* ~A + A -> -1 */
3571   (simplify
3572    (plus:c (convert? (bit_not @0)) (convert? @0))
3573    (if (!TYPE_OVERFLOW_TRAPS (type))
3574     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3576   /* ~A + 1 -> -A */
3577   (simplify
3578    (plus (convert? (bit_not @0)) integer_each_onep)
3579    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3580     (negate (convert @0))))
3582   /* -A - 1 -> ~A */
3583   (simplify
3584    (minus (convert? (negate @0)) integer_each_onep)
3585    (if (!TYPE_OVERFLOW_TRAPS (type)
3586         && TREE_CODE (type) != COMPLEX_TYPE
3587         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3588     (bit_not (convert @0))))
3590   /* -1 - A -> ~A */
3591   (simplify
3592    (minus integer_all_onesp @0)
3593    (if (TREE_CODE (type) != COMPLEX_TYPE)
3594     (bit_not @0)))
3596   /* (T)(P + A) - (T)P -> (T) A */
3597   (simplify
3598    (minus (convert (plus:c @@0 @1))
3599     (convert? @0))
3600    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3601         /* For integer types, if A has a smaller type
3602            than T the result depends on the possible
3603            overflow in P + A.
3604            E.g. T=size_t, A=(unsigned)429497295, P>0.
3605            However, if an overflow in P + A would cause
3606            undefined behavior, we can assume that there
3607            is no overflow.  */
3608         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3609             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3610     (convert @1)))
3611   (simplify
3612    (minus (convert (pointer_plus @@0 @1))
3613     (convert @0))
3614    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3615         /* For pointer types, if the conversion of A to the
3616            final type requires a sign- or zero-extension,
3617            then we have to punt - it is not defined which
3618            one is correct.  */
3619         || (POINTER_TYPE_P (TREE_TYPE (@0))
3620             && TREE_CODE (@1) == INTEGER_CST
3621             && tree_int_cst_sign_bit (@1) == 0))
3622     (convert @1)))
3623    (simplify
3624     (pointer_diff (pointer_plus @@0 @1) @0)
3625     /* The second argument of pointer_plus must be interpreted as signed, and
3626        thus sign-extended if necessary.  */
3627     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3628      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3629         second arg is unsigned even when we need to consider it as signed,
3630         we don't want to diagnose overflow here.  */
3631      (convert (view_convert:stype @1))))
3633   /* (T)P - (T)(P + A) -> -(T) A */
3634   (simplify
3635    (minus (convert? @0)
3636     (convert (plus:c @@0 @1)))
3637    (if (INTEGRAL_TYPE_P (type)
3638         && TYPE_OVERFLOW_UNDEFINED (type)
3639         /* For integer literals, using an intermediate unsigned type to avoid
3640            an overflow at run time is counter-productive because it introduces
3641            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3642            the result, which may be problematic in GENERIC for some front-ends:
3643              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3644            so we use the direct path for them.  */
3645         && TREE_CODE (@1) != INTEGER_CST
3646         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3647     (with { tree utype = unsigned_type_for (type); }
3648      (convert (negate (convert:utype @1))))
3649     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3650          /* For integer types, if A has a smaller type
3651             than T the result depends on the possible
3652             overflow in P + A.
3653             E.g. T=size_t, A=(unsigned)429497295, P>0.
3654             However, if an overflow in P + A would cause
3655             undefined behavior, we can assume that there
3656             is no overflow.  */
3657          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3658              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3659      (negate (convert @1)))))
3660   (simplify
3661    (minus (convert @0)
3662     (convert (pointer_plus @@0 @1)))
3663    (if (INTEGRAL_TYPE_P (type)
3664         && TYPE_OVERFLOW_UNDEFINED (type)
3665         /* See above the rationale for this condition.  */
3666         && TREE_CODE (@1) != INTEGER_CST
3667         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3668     (with { tree utype = unsigned_type_for (type); }
3669      (convert (negate (convert:utype @1))))
3670     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3671          /* For pointer types, if the conversion of A to the
3672             final type requires a sign- or zero-extension,
3673             then we have to punt - it is not defined which
3674             one is correct.  */
3675          || (POINTER_TYPE_P (TREE_TYPE (@0))
3676              && TREE_CODE (@1) == INTEGER_CST
3677              && tree_int_cst_sign_bit (@1) == 0))
3678      (negate (convert @1)))))
3679    (simplify
3680     (pointer_diff @0 (pointer_plus @@0 @1))
3681     /* The second argument of pointer_plus must be interpreted as signed, and
3682        thus sign-extended if necessary.  */
3683     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3684      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3685         second arg is unsigned even when we need to consider it as signed,
3686         we don't want to diagnose overflow here.  */
3687      (negate (convert (view_convert:stype @1)))))
3689   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3690   (simplify
3691    (minus (convert (plus:c @@0 @1))
3692     (convert (plus:c @0 @2)))
3693    (if (INTEGRAL_TYPE_P (type)
3694         && TYPE_OVERFLOW_UNDEFINED (type)
3695         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3696         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3697     (with { tree utype = unsigned_type_for (type); }
3698      (convert (minus (convert:utype @1) (convert:utype @2))))
3699     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3700           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3701          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3702              /* For integer types, if A has a smaller type
3703                 than T the result depends on the possible
3704                 overflow in P + A.
3705                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3706                 However, if an overflow in P + A would cause
3707                 undefined behavior, we can assume that there
3708                 is no overflow.  */
3709              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3710                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3711                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3712                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3713      (minus (convert @1) (convert @2)))))
3714   (simplify
3715    (minus (convert (pointer_plus @@0 @1))
3716     (convert (pointer_plus @0 @2)))
3717    (if (INTEGRAL_TYPE_P (type)
3718         && TYPE_OVERFLOW_UNDEFINED (type)
3719         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3720     (with { tree utype = unsigned_type_for (type); }
3721      (convert (minus (convert:utype @1) (convert:utype @2))))
3722     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3723          /* For pointer types, if the conversion of A to the
3724             final type requires a sign- or zero-extension,
3725             then we have to punt - it is not defined which
3726             one is correct.  */
3727          || (POINTER_TYPE_P (TREE_TYPE (@0))
3728              && TREE_CODE (@1) == INTEGER_CST
3729              && tree_int_cst_sign_bit (@1) == 0
3730              && TREE_CODE (@2) == INTEGER_CST
3731              && tree_int_cst_sign_bit (@2) == 0))
3732      (minus (convert @1) (convert @2)))))
3733    (simplify
3734     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3735      (pointer_diff @0 @1))
3736    (simplify
3737     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3738     /* The second argument of pointer_plus must be interpreted as signed, and
3739        thus sign-extended if necessary.  */
3740     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3741      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3742         second arg is unsigned even when we need to consider it as signed,
3743         we don't want to diagnose overflow here.  */
3744      (minus (convert (view_convert:stype @1))
3745             (convert (view_convert:stype @2)))))))
3747 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3748     Modeled after fold_plusminus_mult_expr.  */
3749 (if (!TYPE_SATURATING (type)
3750      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3751  (for plusminus (plus minus)
3752   (simplify
3753    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3754    (if (!ANY_INTEGRAL_TYPE_P (type)
3755         || TYPE_OVERFLOW_WRAPS (type)
3756         || (INTEGRAL_TYPE_P (type)
3757             && tree_expr_nonzero_p (@0)
3758             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3759     (if (single_use (@3) || single_use (@4))
3760      /* If @1 +- @2 is constant require a hard single-use on either
3761         original operand (but not on both).  */
3762      (mult (plusminus @1 @2) @0)
3763      (mult! (plusminus @1 @2) @0)
3764   )))
3765   /* We cannot generate constant 1 for fract.  */
3766   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3767    (simplify
3768     (plusminus @0 (mult:c@3 @0 @2))
3769     (if ((!ANY_INTEGRAL_TYPE_P (type)
3770           || TYPE_OVERFLOW_WRAPS (type)
3771           /* For @0 + @0*@2 this transformation would introduce UB
3772              (where there was none before) for @0 in [-1,0] and @2 max.
3773              For @0 - @0*@2 this transformation would introduce UB
3774              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3775           || (INTEGRAL_TYPE_P (type)
3776               && ((tree_expr_nonzero_p (@0)
3777                    && expr_not_equal_to (@0,
3778                                 wi::minus_one (TYPE_PRECISION (type))))
3779                   || (plusminus == PLUS_EXPR
3780                       ? expr_not_equal_to (@2,
3781                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3782                       /* Let's ignore the @0 -1 and @2 min case.  */
3783                       : (expr_not_equal_to (@2,
3784                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3785                          && expr_not_equal_to (@2,
3786                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3787                                 + 1))))))
3788          && single_use (@3))
3789      (mult (plusminus { build_one_cst (type); } @2) @0)))
3790    (simplify
3791     (plusminus (mult:c@3 @0 @2) @0)
3792     (if ((!ANY_INTEGRAL_TYPE_P (type)
3793           || TYPE_OVERFLOW_WRAPS (type)
3794           /* For @0*@2 + @0 this transformation would introduce UB
3795              (where there was none before) for @0 in [-1,0] and @2 max.
3796              For @0*@2 - @0 this transformation would introduce UB
3797              for @0 0 and @2 min.  */
3798           || (INTEGRAL_TYPE_P (type)
3799               && ((tree_expr_nonzero_p (@0)
3800                    && (plusminus == MINUS_EXPR
3801                        || expr_not_equal_to (@0,
3802                                 wi::minus_one (TYPE_PRECISION (type)))))
3803                   || expr_not_equal_to (@2,
3804                         (plusminus == PLUS_EXPR
3805                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3806                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3807          && single_use (@3))
3808      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3810 #if GIMPLE
3811 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3812    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3813 (simplify
3814  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3815   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3816        && tree_fits_uhwi_p (@1)
3817        && tree_to_uhwi (@1) < element_precision (type)
3818        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3819            || optab_handler (smul_optab,
3820                              TYPE_MODE (type)) != CODE_FOR_nothing))
3821    (with { tree t = type;
3822            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3823            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3824                                              element_precision (type));
3825            w += 1;
3826            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3827                                         : t, w);
3828            cst = build_uniform_cst (t, cst); }
3829     (convert (mult (convert:t @0) { cst; })))))
3830 (simplify
3831  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3832   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3833        && tree_fits_uhwi_p (@1)
3834        && tree_to_uhwi (@1) < element_precision (type)
3835        && tree_fits_uhwi_p (@2)
3836        && tree_to_uhwi (@2) < element_precision (type)
3837        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3838            || optab_handler (smul_optab,
3839                              TYPE_MODE (type)) != CODE_FOR_nothing))
3840    (with { tree t = type;
3841            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3842            unsigned int prec = element_precision (type);
3843            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3844            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3845            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3846                                         : t, w);
3847            cst = build_uniform_cst (t, cst); }
3848     (convert (mult (convert:t @0) { cst; })))))
3849 #endif
3851 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3852    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3853    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3854 (for op (bit_ior bit_xor)
3855  (simplify
3856   (op (mult:s@0 @1 INTEGER_CST@2)
3857       (mult:s@3 @1 INTEGER_CST@4))
3858   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3859        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3860    (mult @1
3861          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3862  (simplify
3863   (op:c (mult:s@0 @1 INTEGER_CST@2)
3864         (lshift:s@3 @1 INTEGER_CST@4))
3865   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3866        && tree_int_cst_sgn (@4) > 0
3867        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3868    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3869            wide_int c = wi::add (wi::to_wide (@2),
3870                                  wi::lshift (wone, wi::to_wide (@4))); }
3871     (mult @1 { wide_int_to_tree (type, c); }))))
3872  (simplify
3873   (op:c (mult:s@0 @1 INTEGER_CST@2)
3874         @1)
3875   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3876        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3877    (mult @1
3878          { wide_int_to_tree (type,
3879                              wi::add (wi::to_wide (@2), 1)); })))
3880  (simplify
3881   (op (lshift:s@0 @1 INTEGER_CST@2)
3882       (lshift:s@3 @1 INTEGER_CST@4))
3883   (if (INTEGRAL_TYPE_P (type)
3884        && tree_int_cst_sgn (@2) > 0
3885        && tree_int_cst_sgn (@4) > 0
3886        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3887    (with { tree t = type;
3888            if (!TYPE_OVERFLOW_WRAPS (t))
3889              t = unsigned_type_for (t);
3890            wide_int wone = wi::one (TYPE_PRECISION (t));
3891            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3892                                  wi::lshift (wone, wi::to_wide (@4))); }
3893     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3894  (simplify
3895   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3896         @1)
3897   (if (INTEGRAL_TYPE_P (type)
3898        && tree_int_cst_sgn (@2) > 0
3899        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3900    (with { tree t = type;
3901            if (!TYPE_OVERFLOW_WRAPS (t))
3902              t = unsigned_type_for (t);
3903            wide_int wone = wi::one (TYPE_PRECISION (t));
3904            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3905     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3907 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3909 (for minmax (min max)
3910  (simplify
3911   (minmax @0 @0)
3912   @0)
3913 /* max(max(x,y),x) -> max(x,y)  */
3914  (simplify
3915   (minmax:c (minmax:c@2 @0 @1) @0)
3916   @2))
3917 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3918 (for minmax (FMIN_ALL FMAX_ALL)
3919  (simplify
3920   (minmax @0 @0)
3921   (if (!tree_expr_maybe_signaling_nan_p (@0))
3922     @0)))
3923 /* min(max(x,y),y) -> y.  */
3924 (simplify
3925  (min:c (max:c @0 @1) @1)
3926  @1)
3927 /* max(min(x,y),y) -> y.  */
3928 (simplify
3929  (max:c (min:c @0 @1) @1)
3930  @1)
3931 /* max(a,-a) -> abs(a).  */
3932 (simplify
3933  (max:c @0 (negate @0))
3934  (if (TREE_CODE (type) != COMPLEX_TYPE
3935       && (! ANY_INTEGRAL_TYPE_P (type)
3936           || TYPE_OVERFLOW_UNDEFINED (type)))
3937   (abs @0)))
3938 /* min(a,-a) -> -abs(a).  */
3939 (simplify
3940  (min:c @0 (negate @0))
3941  (if (TREE_CODE (type) != COMPLEX_TYPE
3942       && (! ANY_INTEGRAL_TYPE_P (type)
3943           || TYPE_OVERFLOW_UNDEFINED (type)))
3944   (negate (abs @0))))
3945 (simplify
3946  (min @0 @1)
3947  (switch
3948   (if (INTEGRAL_TYPE_P (type)
3949        && TYPE_MIN_VALUE (type)
3950        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3951    @1)
3952   (if (INTEGRAL_TYPE_P (type)
3953        && TYPE_MAX_VALUE (type)
3954        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3955    @0)))
3956 (simplify
3957  (max @0 @1)
3958  (switch
3959   (if (INTEGRAL_TYPE_P (type)
3960        && TYPE_MAX_VALUE (type)
3961        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3962    @1)
3963   (if (INTEGRAL_TYPE_P (type)
3964        && TYPE_MIN_VALUE (type)
3965        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3966    @0)))
3968 /* max (a, a + CST) -> a + CST where CST is positive.  */
3969 /* max (a, a + CST) -> a where CST is negative.  */
3970 (simplify
3971  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3972   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3973    (if (tree_int_cst_sgn (@1) > 0)
3974     @2
3975     @0)))
3977 /* min (a, a + CST) -> a where CST is positive.  */
3978 /* min (a, a + CST) -> a + CST where CST is negative. */
3979 (simplify
3980  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3981   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3982    (if (tree_int_cst_sgn (@1) > 0)
3983     @0
3984     @2)))
3986 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3987    the addresses are known to be less, equal or greater.  */
3988 (for minmax (min max)
3989      cmp (lt gt)
3990  (simplify
3991   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3992   (with
3993    {
3994      poly_int64 off0, off1;
3995      tree base0, base1;
3996      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3997                                   off0, off1, GENERIC);
3998    }
3999    (if (equal == 1)
4000     (if (minmax == MIN_EXPR)
4001      (if (known_le (off0, off1))
4002       @2
4003       (if (known_gt (off0, off1))
4004        @3))
4005      (if (known_ge (off0, off1))
4006       @2
4007       (if (known_lt (off0, off1))
4008        @3)))))))
4010 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4011    and the outer convert demotes the expression back to x's type.  */
4012 (for minmax (min max)
4013  (simplify
4014   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4015   (if (INTEGRAL_TYPE_P (type)
4016        && types_match (@1, type) && int_fits_type_p (@2, type)
4017        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4018        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4019    (minmax @1 (convert @2)))))
4021 (for minmax (FMIN_ALL FMAX_ALL)
4022  /* If either argument is NaN and other one is not sNaN, return the other
4023     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4024  (simplify
4025   (minmax:c @0 REAL_CST@1)
4026    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4027        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4028        && !tree_expr_maybe_signaling_nan_p (@0))
4029    @0)))
4030 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4031    functions to return the numeric arg if the other one is NaN.
4032    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4033    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4034    worry about it either.  */
4035 (if (flag_finite_math_only)
4036  (simplify
4037   (FMIN_ALL @0 @1)
4038   (min @0 @1))
4039  (simplify
4040   (FMAX_ALL @0 @1)
4041   (max @0 @1)))
4042 /* min (-A, -B) -> -max (A, B)  */
4043 (for minmax (min max FMIN_ALL FMAX_ALL)
4044      maxmin (max min FMAX_ALL FMIN_ALL)
4045  (simplify
4046   (minmax (negate:s@2 @0) (negate:s@3 @1))
4047   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4048        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4049            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4050    (negate (maxmin @0 @1)))))
4051 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4052    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4053 (for minmax (min max)
4054  maxmin (max min)
4055  (simplify
4056   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4057   (bit_not (maxmin @0 @1)))
4058 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4059 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4060  (simplify
4061   (bit_not (minmax:cs (bit_not @0) @1))
4062   (maxmin @0 (bit_not @1))))
4064 /* MIN (X, Y) == X -> X <= Y  */
4065 /* MIN (X, Y) < X -> X > Y  */
4066 /* MIN (X, Y) >= X -> X <= Y  */
4067 (for minmax (min min min min max max max max)
4068      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4069      out    (le  gt  gt  le  ge  lt  lt  ge )
4070  (simplify
4071   (cmp:c (minmax:c @0 @1) @0)
4072   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4073    (out @0 @1))))
4074 /* MIN (X, 5) == 0 -> X == 0
4075    MIN (X, 5) == 7 -> false  */
4076 (for cmp (eq ne)
4077  (simplify
4078   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4079   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4080                  TYPE_SIGN (TREE_TYPE (@0))))
4081    { constant_boolean_node (cmp == NE_EXPR, type); }
4082    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4083                   TYPE_SIGN (TREE_TYPE (@0))))
4084     (cmp @0 @2)))))
4085 (for cmp (eq ne)
4086  (simplify
4087   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4088   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4089                  TYPE_SIGN (TREE_TYPE (@0))))
4090    { constant_boolean_node (cmp == NE_EXPR, type); }
4091    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4092                   TYPE_SIGN (TREE_TYPE (@0))))
4093     (cmp @0 @2)))))
4095 /* X <= MAX(X, Y) -> true
4096    X > MAX(X, Y) -> false 
4097    X >= MIN(X, Y) -> true
4098    X < MIN(X, Y) -> false */
4099 (for minmax (min     min     max     max     )
4100      cmp    (ge      lt      le      gt      )
4101  (simplify
4102   (cmp:c @0 (minmax:c @0 @1))
4103   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4105 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4106 (for minmax (min     min     max     max     min     min     max     max    )
4107      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4108      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4109  (simplify
4110   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4111   (comb (cmp @0 @2) (cmp @1 @2))))
4113 /* Undo fancy ways of writing max/min or other ?: expressions, like
4114    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4115    People normally use ?: and that is what we actually try to optimize.  */
4116 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4117 (simplify
4118  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4119  (if (INTEGRAL_TYPE_P (type)
4120       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4121   (cond (convert:boolean_type_node @2) @1 @0)))
4122 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4123 (simplify
4124  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4125  (if (INTEGRAL_TYPE_P (type)
4126       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4127   (cond (convert:boolean_type_node @2) @1 @0)))
4128 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4129 (simplify
4130  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4131  (if (INTEGRAL_TYPE_P (type)
4132       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4133   (cond (convert:boolean_type_node @2) @1 @0)))
4135 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4136 (simplify
4137   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4138   (max @2 @1))
4140 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4141 (for op (bit_xor bit_ior plus)
4142  (simplify
4143   (cond (eq zero_one_valued_p@0
4144             integer_zerop)
4145         @1
4146         (op:c @2 @1))
4147   (if (INTEGRAL_TYPE_P (type)
4148        && TYPE_PRECISION (type) > 1
4149        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4150        (op (mult (convert:type @0) @2) @1))))
4152 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4153 (for op (bit_xor bit_ior plus)
4154  (simplify
4155   (cond (ne zero_one_valued_p@0
4156             integer_zerop)
4157        (op:c @2 @1)
4158         @1)
4159   (if (INTEGRAL_TYPE_P (type)
4160        && TYPE_PRECISION (type) > 1
4161        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4162        (op (mult (convert:type @0) @2) @1))))
4164 /* ?: Value replacement. */
4165 /* a == 0 ? b : b + a  -> b + a */
4166 (for op (plus bit_ior bit_xor)
4167  (simplify
4168   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4169    @2))
4170 /* a == 0 ? b : b - a  -> b - a */
4171 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4172 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4173 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4174  (simplify
4175   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4176    @2))
4178 /* a == 1 ? b : b / a  -> b / a */
4179 (for op (trunc_div ceil_div floor_div round_div exact_div)
4180  (simplify
4181   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4182    @2))
4184 /* a == 1 ? b : a * b -> a * b */
4185 (for op (mult)
4186  (simplify
4187   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4188    @2))
4190 /* a == -1 ? b : a & b -> a & b */
4191 (for op (bit_and)
4192  (simplify
4193   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4194    @2))
4196 /* Simplifications of shift and rotates.  */
4198 (for rotate (lrotate rrotate)
4199  (simplify
4200   (rotate integer_all_onesp@0 @1)
4201   @0))
4203 /* Optimize -1 >> x for arithmetic right shifts.  */
4204 (simplify
4205  (rshift integer_all_onesp@0 @1)
4206  (if (!TYPE_UNSIGNED (type))
4207   @0))
4209 /* Optimize (x >> c) << c into x & (-1<<c).  */
4210 (simplify
4211  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4212  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4213   /* It doesn't matter if the right shift is arithmetic or logical.  */
4214   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4216 (simplify
4217  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4218  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4219       /* Allow intermediate conversion to integral type with whatever sign, as
4220          long as the low TYPE_PRECISION (type)
4221          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4222       && INTEGRAL_TYPE_P (type)
4223       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4224       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4225       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4226       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4227           || wi::geu_p (wi::to_wide (@1),
4228                         TYPE_PRECISION (type)
4229                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4230   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4232 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4233    unsigned x OR truncate into the precision(type) - c lowest bits
4234    of signed x (if they have mode precision or a precision of 1).  */
4235 (simplify
4236  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4237  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4238   (if (TYPE_UNSIGNED (type))
4239    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4240    (if (INTEGRAL_TYPE_P (type))
4241     (with {
4242       int width = element_precision (type) - tree_to_uhwi (@1);
4243       tree stype = NULL_TREE;
4244       if (width <= MAX_FIXED_MODE_SIZE)
4245         stype = build_nonstandard_integer_type (width, 0);
4246      }
4247      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4248       (convert (convert:stype @0))))))))
4250 /* Optimize x >> x into 0 */
4251 (simplify
4252  (rshift @0 @0)
4253   { build_zero_cst (type); })
4255 (for shiftrotate (lrotate rrotate lshift rshift)
4256  (simplify
4257   (shiftrotate @0 integer_zerop)
4258   (non_lvalue @0))
4259  (simplify
4260   (shiftrotate integer_zerop@0 @1)
4261   @0)
4262  /* Prefer vector1 << scalar to vector1 << vector2
4263     if vector2 is uniform.  */
4264  (for vec (VECTOR_CST CONSTRUCTOR)
4265   (simplify
4266    (shiftrotate @0 vec@1)
4267    (with { tree tem = uniform_vector_p (@1); }
4268     (if (tem)
4269      (shiftrotate @0 { tem; }))))))
4271 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4272    Y is 0.  Similarly for X >> Y.  */
4273 #if GIMPLE
4274 (for shift (lshift rshift)
4275  (simplify
4276   (shift @0 SSA_NAME@1)
4277    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4278     (with {
4279       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4280       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4281      }
4282      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4283       @0)))))
4284 #endif
4286 /* Rewrite an LROTATE_EXPR by a constant into an
4287    RROTATE_EXPR by a new constant.  */
4288 (simplify
4289  (lrotate @0 INTEGER_CST@1)
4290  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4291                             build_int_cst (TREE_TYPE (@1),
4292                                            element_precision (type)), @1); }))
4294 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4295 (for op (lrotate rrotate rshift lshift)
4296  (simplify
4297   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4298   (with { unsigned int prec = element_precision (type); }
4299    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4300         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4301         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4302         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4303     (with { unsigned int low = (tree_to_uhwi (@1)
4304                                 + tree_to_uhwi (@2)); }
4305      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4306         being well defined.  */
4307      (if (low >= prec)
4308       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4309        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4310        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4311         { build_zero_cst (type); }
4312         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4313       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4316 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4317 (simplify
4318  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4319   (if ((wi::to_wide (@1) & 1) != 0)
4320    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4321    { build_zero_cst (type); }))
4323 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4324    either to false if D is smaller (unsigned comparison) than C, or to
4325    x == log2 (D) - log2 (C).  Similarly for right shifts.
4326    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4327 (for cmp (ne eq)
4328      icmp (eq ne)
4329  (simplify
4330   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4331    (with { int c1 = wi::clz (wi::to_wide (@1));
4332            int c2 = wi::clz (wi::to_wide (@2)); }
4333     (if (c1 < c2)
4334      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4335      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4336  (simplify
4337   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4338    (if (tree_int_cst_sgn (@1) > 0)
4339     (with { int c1 = wi::clz (wi::to_wide (@1));
4340             int c2 = wi::clz (wi::to_wide (@2)); }
4341      (if (c1 > c2)
4342       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4343       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4344  /* `(1 >> X) != 0` -> `X == 0` */
4345  /* `(1 >> X) == 0` -> `X != 0` */
4346  (simplify
4347   (cmp (rshift integer_onep@1 @0) integer_zerop)
4348    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4349     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4351 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4352    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4353    if CST2 != 0.  */
4354 (for cmp (ne eq)
4355  (simplify
4356   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4357   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4358    (if (cand < 0
4359         || (!integer_zerop (@2)
4360             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4361     { constant_boolean_node (cmp == NE_EXPR, type); }
4362     (if (!integer_zerop (@2)
4363          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4364      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4366 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4367         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4368 (for cmp (ne eq)
4369  (simplify
4370   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4371   (if (tree_fits_shwi_p (@1)
4372        && tree_to_shwi (@1) > 0
4373        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4374     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4375       { constant_boolean_node (cmp == NE_EXPR, type); }
4376       (with { wide_int c1 = wi::to_wide (@1);
4377               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4378               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4379         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4380              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4381  (simplify
4382   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4383   (if (tree_fits_shwi_p (@1)
4384        && tree_to_shwi (@1) > 0
4385        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4386     (with { tree t0 = TREE_TYPE (@0);
4387             unsigned int prec = TYPE_PRECISION (t0);
4388             wide_int c1 = wi::to_wide (@1);
4389             wide_int c2 = wi::to_wide (@2);
4390             wide_int c3 = wi::to_wide (@3);
4391             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4392       (if ((c2 & c3) != c3)
4393         { constant_boolean_node (cmp == NE_EXPR, type); }
4394         (if (TYPE_UNSIGNED (t0))
4395           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4396             { constant_boolean_node (cmp == NE_EXPR, type); }
4397             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4398                  { wide_int_to_tree (t0, c3 << c1); }))
4399           (with { wide_int smask = wi::arshift (sb, c1); }
4400             (switch
4401               (if ((c2 & smask) == 0)
4402                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4403                      { wide_int_to_tree (t0, c3 << c1); }))
4404               (if ((c3 & smask) == 0)
4405                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4406                      { wide_int_to_tree (t0, c3 << c1); }))
4407               (if ((c2 & smask) != (c3 & smask))
4408                 { constant_boolean_node (cmp == NE_EXPR, type); })
4409               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4410                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4412 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4413         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4414    if the new mask might be further optimized.  */
4415 (for shift (lshift rshift)
4416  (simplify
4417   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4418            INTEGER_CST@2)
4419    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4420         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4421         && tree_fits_uhwi_p (@1)
4422         && tree_to_uhwi (@1) > 0
4423         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4424     (with
4425      {
4426        unsigned int shiftc = tree_to_uhwi (@1);
4427        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4428        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4429        tree shift_type = TREE_TYPE (@3);
4430        unsigned int prec;
4432        if (shift == LSHIFT_EXPR)
4433          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4434        else if (shift == RSHIFT_EXPR
4435                 && type_has_mode_precision_p (shift_type))
4436          {
4437            prec = TYPE_PRECISION (TREE_TYPE (@3));
4438            tree arg00 = @0;
4439            /* See if more bits can be proven as zero because of
4440               zero extension.  */
4441            if (@3 != @0
4442                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4443              {
4444                tree inner_type = TREE_TYPE (@0);
4445                if (type_has_mode_precision_p (inner_type)
4446                    && TYPE_PRECISION (inner_type) < prec)
4447                  {
4448                    prec = TYPE_PRECISION (inner_type);
4449                    /* See if we can shorten the right shift.  */
4450                    if (shiftc < prec)
4451                      shift_type = inner_type;
4452                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4453                       it into (X, 0) later on by making sure zerobits
4454                       is all ones.  */
4455                  }
4456              }
4457            zerobits = HOST_WIDE_INT_M1U;
4458            if (shiftc < prec)
4459              {
4460                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4461                zerobits <<= prec - shiftc;
4462              }
4463            /* For arithmetic shift if sign bit could be set, zerobits
4464               can contain actually sign bits, so no transformation is
4465               possible, unless MASK masks them all away.  In that
4466               case the shift needs to be converted into logical shift.  */
4467            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4468                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4469              {
4470                if ((mask & zerobits) == 0)
4471                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4472                else
4473                  zerobits = 0;
4474              }
4475          }
4476      }
4477      /* ((X << 16) & 0xff00) is (X, 0).  */
4478      (if ((mask & zerobits) == mask)
4479       { build_int_cst (type, 0); }
4480       (with { newmask = mask | zerobits; }
4481        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4482         (with
4483          {
4484            /* Only do the transformation if NEWMASK is some integer
4485               mode's mask.  */
4486            for (prec = BITS_PER_UNIT;
4487                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4488              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4489                break;
4490          }
4491          (if (prec < HOST_BITS_PER_WIDE_INT
4492               || newmask == HOST_WIDE_INT_M1U)
4493           (with
4494            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4495            (if (!tree_int_cst_equal (newmaskt, @2))
4496             (if (shift_type != TREE_TYPE (@3))
4497              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4498              (bit_and @4 { newmaskt; })))))))))))))
4500 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4501 (for cmp (ne eq)
4502        icmp (eq ne)
4503  (simplify
4504   (cmp
4505    (bit_and
4506     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4507   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4508    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4509                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4511 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4512    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4513 (for shift (lshift rshift)
4514  (for bit_op (bit_and bit_xor bit_ior)
4515   (simplify
4516    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4517    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4518     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4519      (if (mask)
4520       (bit_op (shift (convert @0) @1) { mask; })))))))
4522 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4523 (simplify
4524  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4525   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4526        && (element_precision (TREE_TYPE (@0))
4527            <= element_precision (TREE_TYPE (@1))
4528            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4529    (with
4530     { tree shift_type = TREE_TYPE (@0); }
4531      (convert (rshift (convert:shift_type @1) @2)))))
4533 /* ~(~X >>r Y) -> X >>r Y
4534    ~(~X <<r Y) -> X <<r Y */
4535 (for rotate (lrotate rrotate)
4536  (simplify
4537   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4538    (if ((element_precision (TREE_TYPE (@0))
4539          <= element_precision (TREE_TYPE (@1))
4540          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4541         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4542             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4543     (with
4544      { tree rotate_type = TREE_TYPE (@0); }
4545       (convert (rotate (convert:rotate_type @1) @2))))))
4547 (for cmp (eq ne)
4548  (for rotate (lrotate rrotate)
4549       invrot (rrotate lrotate)
4550   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4551   (simplify
4552    (cmp (rotate @1 @0) (rotate @2 @0))
4553    (cmp @1 @2))
4554   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4555   (simplify
4556    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4557    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4558   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4559   (simplify
4560    (cmp (rotate @0 @1) INTEGER_CST@2)
4561     (if (integer_zerop (@2) || integer_all_onesp (@2))
4562      (cmp @0 @2)))))
4564 /* Narrow a lshift by constant.  */
4565 (simplify
4566  (convert (lshift:s@0 @1 INTEGER_CST@2))
4567  (if (INTEGRAL_TYPE_P (type)
4568       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4569       && !integer_zerop (@2)
4570       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4571   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4572        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4573    (lshift (convert @1) @2)
4574    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4575     { build_zero_cst (type); }))))
4577 /* Simplifications of conversions.  */
4579 /* Basic strip-useless-type-conversions / strip_nops.  */
4580 (for cvt (convert view_convert float fix_trunc)
4581  (simplify
4582   (cvt @0)
4583   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4584        || (GENERIC && type == TREE_TYPE (@0)))
4585    @0)))
4587 /* Contract view-conversions.  */
4588 (simplify
4589   (view_convert (view_convert @0))
4590   (view_convert @0))
4592 /* For integral conversions with the same precision or pointer
4593    conversions use a NOP_EXPR instead.  */
4594 (simplify
4595   (view_convert @0)
4596   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4597        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4598        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4599    (convert @0)))
4601 /* Strip inner integral conversions that do not change precision or size, or
4602    zero-extend while keeping the same size (for bool-to-char).  */
4603 (simplify
4604   (view_convert (convert@0 @1))
4605   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4606        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4607        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4608        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4609            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4610                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4611    (view_convert @1)))
4613 /* Simplify a view-converted empty or single-element constructor.  */
4614 (simplify
4615   (view_convert CONSTRUCTOR@0)
4616   (with
4617    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4618                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4619    (switch
4620     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4621      { build_zero_cst (type); })
4622     (if (CONSTRUCTOR_NELTS (ctor) == 1
4623          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4624          && operand_equal_p (TYPE_SIZE (type),
4625                              TYPE_SIZE (TREE_TYPE
4626                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4627      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4629 /* Re-association barriers around constants and other re-association
4630    barriers can be removed.  */
4631 (simplify
4632  (paren CONSTANT_CLASS_P@0)
4633  @0)
4634 (simplify
4635  (paren (paren@1 @0))
4636  @1)
4638 /* Handle cases of two conversions in a row.  */
4639 (for ocvt (convert float fix_trunc)
4640  (for icvt (convert float)
4641   (simplify
4642    (ocvt (icvt@1 @0))
4643    (with
4644     {
4645       tree inside_type = TREE_TYPE (@0);
4646       tree inter_type = TREE_TYPE (@1);
4647       int inside_int = INTEGRAL_TYPE_P (inside_type);
4648       int inside_ptr = POINTER_TYPE_P (inside_type);
4649       int inside_float = FLOAT_TYPE_P (inside_type);
4650       int inside_vec = VECTOR_TYPE_P (inside_type);
4651       unsigned int inside_prec = element_precision (inside_type);
4652       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4653       int inter_int = INTEGRAL_TYPE_P (inter_type);
4654       int inter_ptr = POINTER_TYPE_P (inter_type);
4655       int inter_float = FLOAT_TYPE_P (inter_type);
4656       int inter_vec = VECTOR_TYPE_P (inter_type);
4657       unsigned int inter_prec = element_precision (inter_type);
4658       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4659       int final_int = INTEGRAL_TYPE_P (type);
4660       int final_ptr = POINTER_TYPE_P (type);
4661       int final_float = FLOAT_TYPE_P (type);
4662       int final_vec = VECTOR_TYPE_P (type);
4663       unsigned int final_prec = element_precision (type);
4664       int final_unsignedp = TYPE_UNSIGNED (type);
4665     }
4666    (switch
4667     /* In addition to the cases of two conversions in a row
4668        handled below, if we are converting something to its own
4669        type via an object of identical or wider precision, neither
4670        conversion is needed.  */
4671     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4672           || (GENERIC
4673               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4674          && (((inter_int || inter_ptr) && final_int)
4675              || (inter_float && final_float))
4676          && inter_prec >= final_prec)
4677      (ocvt @0))
4679     /* Likewise, if the intermediate and initial types are either both
4680        float or both integer, we don't need the middle conversion if the
4681        former is wider than the latter and doesn't change the signedness
4682        (for integers).  Avoid this if the final type is a pointer since
4683        then we sometimes need the middle conversion.  */
4684     (if (((inter_int && inside_int) || (inter_float && inside_float))
4685          && (final_int || final_float)
4686          && inter_prec >= inside_prec
4687          && (inter_float || inter_unsignedp == inside_unsignedp))
4688      (ocvt @0))
4690     /* If we have a sign-extension of a zero-extended value, we can
4691        replace that by a single zero-extension.  Likewise if the
4692        final conversion does not change precision we can drop the
4693        intermediate conversion.  */
4694     (if (inside_int && inter_int && final_int
4695          && ((inside_prec < inter_prec && inter_prec < final_prec
4696               && inside_unsignedp && !inter_unsignedp)
4697              || final_prec == inter_prec))
4698      (ocvt @0))
4700     /* Two conversions in a row are not needed unless:
4701         - some conversion is floating-point (overstrict for now), or
4702         - some conversion is a vector (overstrict for now), or
4703         - the intermediate type is narrower than both initial and
4704           final, or
4705         - the intermediate type and innermost type differ in signedness,
4706           and the outermost type is wider than the intermediate, or
4707         - the initial type is a pointer type and the precisions of the
4708           intermediate and final types differ, or
4709         - the final type is a pointer type and the precisions of the
4710           initial and intermediate types differ.  */
4711     (if (! inside_float && ! inter_float && ! final_float
4712          && ! inside_vec && ! inter_vec && ! final_vec
4713          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4714          && ! (inside_int && inter_int
4715                && inter_unsignedp != inside_unsignedp
4716                && inter_prec < final_prec)
4717          && ((inter_unsignedp && inter_prec > inside_prec)
4718              == (final_unsignedp && final_prec > inter_prec))
4719          && ! (inside_ptr && inter_prec != final_prec)
4720          && ! (final_ptr && inside_prec != inter_prec))
4721      (ocvt @0))
4723    /* `(outer:M)(inter:N) a:O`
4724       can be converted to `(outer:M) a`
4725       if M <= O && N >= O. No matter what signedness of the casts,
4726       as the final is either a truncation from the original or just
4727       a sign change of the type. */
4728    (if (inside_int && inter_int && final_int
4729         && final_prec <= inside_prec
4730         && inter_prec >= inside_prec)
4731     (convert @0))
4733     /* A truncation to an unsigned type (a zero-extension) should be
4734        canonicalized as bitwise and of a mask.  */
4735     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4736          && final_int && inter_int && inside_int
4737          && final_prec == inside_prec
4738          && final_prec > inter_prec
4739          && inter_unsignedp)
4740      (convert (bit_and @0 { wide_int_to_tree
4741                               (inside_type,
4742                                wi::mask (inter_prec, false,
4743                                          TYPE_PRECISION (inside_type))); })))
4745     /* If we are converting an integer to a floating-point that can
4746        represent it exactly and back to an integer, we can skip the
4747        floating-point conversion.  */
4748     (if (GIMPLE /* PR66211 */
4749          && inside_int && inter_float && final_int &&
4750          (unsigned) significand_size (TYPE_MODE (inter_type))
4751          >= inside_prec - !inside_unsignedp)
4752      (convert @0)))))))
4754 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4755    float_type.  Only do the transformation if we do not need to preserve
4756    trapping behaviour, so require !flag_trapping_math. */
4757 #if GIMPLE
4758 (simplify
4759    (float (fix_trunc @0))
4760    (if (!flag_trapping_math
4761         && types_match (type, TREE_TYPE (@0))
4762         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4763                                           OPTIMIZE_FOR_BOTH))
4764       (IFN_TRUNC @0)))
4765 #endif
4767 /* If we have a narrowing conversion to an integral type that is fed by a
4768    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4769    masks off bits outside the final type (and nothing else).  */
4770 (simplify
4771   (convert (bit_and @0 INTEGER_CST@1))
4772   (if (INTEGRAL_TYPE_P (type)
4773        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4774        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4775        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4776                                                     TYPE_PRECISION (type)), 0))
4777    (convert @0)))
4780 /* (X /[ex] A) * A -> X.  */
4781 (simplify
4782   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4783   (convert @0))
4785 /* Simplify (A / B) * B + (A % B) -> A.  */
4786 (for div (trunc_div ceil_div floor_div round_div)
4787      mod (trunc_mod ceil_mod floor_mod round_mod)
4788   (simplify
4789    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4790    @0))
4792 /* x / y * y == x -> x % y == 0.  */
4793 (simplify
4794   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4795   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4796     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4798 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4799 (for op (plus minus)
4800  (simplify
4801   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4802   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4803        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4804    (with
4805      {
4806        wi::overflow_type overflow;
4807        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4808                                TYPE_SIGN (type), &overflow);
4809      }
4810      (if (types_match (type, TREE_TYPE (@2))
4811          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4812       (op @0 { wide_int_to_tree (type, mul); })
4813       (with { tree utype = unsigned_type_for (type); }
4814        (convert (op (convert:utype @0)
4815                     (mult (convert:utype @1) (convert:utype @2))))))))))
4817 /* Canonicalization of binary operations.  */
4819 /* Convert X + -C into X - C.  */
4820 (simplify
4821  (plus @0 REAL_CST@1)
4822  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4823   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4824    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4825     (minus @0 { tem; })))))
4827 /* Convert x+x into x*2.  */
4828 (simplify
4829  (plus @0 @0)
4830  (if (SCALAR_FLOAT_TYPE_P (type))
4831   (mult @0 { build_real (type, dconst2); })
4832   (if (INTEGRAL_TYPE_P (type))
4833    (mult @0 { build_int_cst (type, 2); }))))
4835 /* 0 - X  ->  -X.  */
4836 (simplify
4837  (minus integer_zerop @1)
4838  (negate @1))
4839 (simplify
4840  (pointer_diff integer_zerop @1)
4841  (negate (convert @1)))
4843 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4844    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4845    (-ARG1 + ARG0) reduces to -ARG1.  */
4846 (simplify
4847  (minus real_zerop@0 @1)
4848  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4849   (negate @1)))
4851 /* Transform x * -1 into -x.  */
4852 (simplify
4853  (mult @0 integer_minus_onep)
4854  (negate @0))
4856 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4857    signed overflow for CST != 0 && CST != -1.  */
4858 (simplify
4859  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4860  (if (TREE_CODE (@2) != INTEGER_CST
4861       && single_use (@3)
4862       && !integer_zerop (@1) && !integer_minus_onep (@1))
4863   (mult (mult @0 @2) @1)))
4865 /* True if we can easily extract the real and imaginary parts of a complex
4866    number.  */
4867 (match compositional_complex
4868  (convert? (complex @0 @1)))
4870 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4871 (simplify
4872  (complex (realpart @0) (imagpart @0))
4873  @0)
4874 (simplify
4875  (realpart (complex @0 @1))
4876  @0)
4877 (simplify
4878  (imagpart (complex @0 @1))
4879  @1)
4881 /* Sometimes we only care about half of a complex expression.  */
4882 (simplify
4883  (realpart (convert?:s (conj:s @0)))
4884  (convert (realpart @0)))
4885 (simplify
4886  (imagpart (convert?:s (conj:s @0)))
4887  (convert (negate (imagpart @0))))
4888 (for part (realpart imagpart)
4889  (for op (plus minus)
4890   (simplify
4891    (part (convert?:s@2 (op:s @0 @1)))
4892    (convert (op (part @0) (part @1))))))
4893 (simplify
4894  (realpart (convert?:s (CEXPI:s @0)))
4895  (convert (COS @0)))
4896 (simplify
4897  (imagpart (convert?:s (CEXPI:s @0)))
4898  (convert (SIN @0)))
4900 /* conj(conj(x)) -> x  */
4901 (simplify
4902  (conj (convert? (conj @0)))
4903  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4904   (convert @0)))
4906 /* conj({x,y}) -> {x,-y}  */
4907 (simplify
4908  (conj (convert?:s (complex:s @0 @1)))
4909  (with { tree itype = TREE_TYPE (type); }
4910   (complex (convert:itype @0) (negate (convert:itype @1)))))
4912 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4913 (for bswap (BSWAP)
4914  (simplify
4915   (bswap (bswap @0))
4916   @0)
4917  (simplify
4918   (bswap (bit_not (bswap @0)))
4919   (bit_not @0))
4920  (for bitop (bit_xor bit_ior bit_and)
4921   (simplify
4922    (bswap (bitop:c (bswap @0) @1))
4923    (bitop @0 (bswap @1))))
4924  (for cmp (eq ne)
4925   (simplify
4926    (cmp (bswap@2 @0) (bswap @1))
4927    (with { tree ctype = TREE_TYPE (@2); }
4928     (cmp (convert:ctype @0) (convert:ctype @1))))
4929   (simplify
4930    (cmp (bswap @0) INTEGER_CST@1)
4931    (with { tree ctype = TREE_TYPE (@1); }
4932     (cmp (convert:ctype @0) (bswap! @1)))))
4933  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4934  (simplify
4935   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4936            INTEGER_CST@3)
4937    (if (BITS_PER_UNIT == 8
4938         && tree_fits_uhwi_p (@2)
4939         && tree_fits_uhwi_p (@3))
4940     (with
4941      {
4942       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4943       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4944       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4945       unsigned HOST_WIDE_INT lo = bits & 7;
4946       unsigned HOST_WIDE_INT hi = bits - lo;
4947      }
4948      (if (bits < prec
4949           && mask < (256u>>lo)
4950           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4951       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4952        (if (ns == 0)
4953         (bit_and (convert @1) @3)
4954         (with
4955          {
4956           tree utype = unsigned_type_for (TREE_TYPE (@1));
4957           tree nst = build_int_cst (integer_type_node, ns);
4958          }
4959          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4960  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4961  (simplify
4962   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4963    (if (BITS_PER_UNIT == 8
4964         && CHAR_TYPE_SIZE == 8
4965         && tree_fits_uhwi_p (@1))
4966     (with
4967      {
4968       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4969       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4970       /* If the bswap was extended before the original shift, this
4971          byte (shift) has the sign of the extension, not the sign of
4972          the original shift.  */
4973       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4974      }
4975      /* Special case: logical right shift of sign-extended bswap.
4976         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4977      (if (TYPE_PRECISION (type) > prec
4978           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4979           && TYPE_UNSIGNED (type)
4980           && bits < prec && bits + 8 >= prec)
4981       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4982        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4983       (if (bits + 8 == prec)
4984        (if (TYPE_UNSIGNED (st))
4985         (convert (convert:unsigned_char_type_node @0))
4986         (convert (convert:signed_char_type_node @0)))
4987        (if (bits < prec && bits + 8 > prec)
4988         (with 
4989          {
4990           tree nst = build_int_cst (integer_type_node, bits & 7);
4991           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4992                                        : signed_char_type_node;
4993          }
4994          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4995  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4996  (simplify
4997   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4998    (if (BITS_PER_UNIT == 8
4999         && tree_fits_uhwi_p (@1)
5000         && tree_to_uhwi (@1) < 256)
5001     (with
5002      {
5003       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5004       tree utype = unsigned_type_for (TREE_TYPE (@0));
5005       tree nst = build_int_cst (integer_type_node, prec - 8);
5006      }
5007      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5010 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5012 /* Simplify constant conditions.
5013    Only optimize constant conditions when the selected branch
5014    has the same type as the COND_EXPR.  This avoids optimizing
5015    away "c ? x : throw", where the throw has a void type.
5016    Note that we cannot throw away the fold-const.cc variant nor
5017    this one as we depend on doing this transform before possibly
5018    A ? B : B -> B triggers and the fold-const.cc one can optimize
5019    0 ? A : B to B even if A has side-effects.  Something
5020    genmatch cannot handle.  */
5021 (simplify
5022  (cond INTEGER_CST@0 @1 @2)
5023  (if (integer_zerop (@0))
5024   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5025    @2)
5026   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5027    @1)))
5028 (simplify
5029  (vec_cond VECTOR_CST@0 @1 @2)
5030  (if (integer_all_onesp (@0))
5031   @1
5032   (if (integer_zerop (@0))
5033    @2)))
5035 /* Sink unary operations to branches, but only if we do fold both.  */
5036 (for op (negate bit_not abs absu)
5037  (simplify
5038   (op (vec_cond:s @0 @1 @2))
5039   (vec_cond @0 (op! @1) (op! @2))))
5041 /* Sink unary conversions to branches, but only if we do fold both
5042    and the target's truth type is the same as we already have.  */
5043 (simplify
5044  (convert (vec_cond:s @0 @1 @2))
5045  (if (VECTOR_TYPE_P (type)
5046       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5047   (vec_cond @0 (convert! @1) (convert! @2))))
5049 /* Likewise for view_convert of nop_conversions. */
5050 (simplify
5051  (view_convert (vec_cond:s @0 @1 @2))
5052  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5053       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5054                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5055       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5056   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5058 /* Sink binary operation to branches, but only if we can fold it.  */
5059 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5060          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5061          trunc_mod ceil_mod floor_mod round_mod min max)
5062 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5063  (simplify
5064   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5065   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5067 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5068  (simplify
5069   (op (vec_cond:s @0 @1 @2) @3)
5070   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5071  (simplify
5072   (op @3 (vec_cond:s @0 @1 @2))
5073   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5075 #if GIMPLE
5076 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5077  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5078            INTEGER_CST@1)
5079  (with {
5080          int ibit = tree_log2 (@0);
5081          int ibit2 = tree_log2 (@1);
5082        }
5083   (if (ibit == ibit2
5084       && ibit >= 0
5085       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5087 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5088  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5089           INTEGER_CST@1)
5090  (with {
5091          int ibit = tree_log2 (@0);
5092          int ibit2 = tree_log2 (@1);
5093        }
5094   (if (ibit == ibit2
5095       && ibit >= 0
5096       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5098 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5099  (bit_and:c
5100   (convert1?@4
5101    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5102   (convert2? @0))
5103  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5105 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5106  (bit_and:c
5107   (convert1?@4
5108    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5109   (convert2? @0))
5110  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5112 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5113  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5114             INTEGER_CST@1)
5115  (with {
5116          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5117                                               TYPE_PRECISION(type)));
5118          int ibit2 = tree_log2 (@1);
5119        }
5120   (if (ibit == ibit2
5121       && ibit >= 0
5122       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5124 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5125  (bit_and@4
5126   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5127   INTEGER_CST@1)
5128  (with {
5129          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5130                                               TYPE_PRECISION(type)));
5131          int ibit2 = tree_log2 (@1);
5132        }
5133   (if (ibit == ibit2
5134       && ibit >= 0
5135       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5137 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5138  (bit_and:c
5139   (convert1?@3
5140    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5141   (convert2? @0))
5142  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5144 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5145  (bit_and:c
5146   (convert1?@3
5147    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5148   (convert2? @0))
5149   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5151 #endif
5153 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5154    Currently disabled after pass lvec because ARM understands
5155    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5156 #if GIMPLE
5157 /* These can only be done in gimple as fold likes to convert:
5158    (CMP) & N into (CMP) ? N : 0
5159    and we try to match the same pattern again and again. */
5160 (simplify
5161  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5162  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5163   (vec_cond (bit_and @0 @3) @1 @2)))
5164 (simplify
5165  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5166  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5167   (vec_cond (bit_ior @0 @3) @1 @2)))
5168 (simplify
5169  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5170  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5171   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5172 (simplify
5173  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5174  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5175   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5177 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5178 (simplify
5179  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5180  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5181   (vec_cond (bit_and @0 @1) @2 @3)))
5182 (simplify
5183  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5184  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5185   (vec_cond (bit_ior @0 @1) @2 @3)))
5186 (simplify
5187  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5188  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5189   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5190 (simplify
5191  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5192  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5193   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5194 #endif
5196 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5197    types are compatible.  */
5198 (simplify
5199  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5200  (if (VECTOR_BOOLEAN_TYPE_P (type)
5201       && types_match (type, TREE_TYPE (@0)))
5202   (if (integer_zerop (@1) && integer_all_onesp (@2))
5203    (bit_not @0)
5204    (if (integer_all_onesp (@1) && integer_zerop (@2))
5205     @0))))
5207 /* A few simplifications of "a ? CST1 : CST2". */
5208 /* NOTE: Only do this on gimple as the if-chain-to-switch
5209    optimization depends on the gimple to have if statements in it. */
5210 #if GIMPLE
5211 (simplify
5212  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5213  (switch
5214   (if (integer_zerop (@2))
5215    (switch
5216     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5217     (if (integer_onep (@1))
5218      (convert (convert:boolean_type_node @0)))
5219     /* a ? -1 : 0 -> -a.  */
5220     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5221      (if (TYPE_PRECISION (type) == 1)
5222       /* For signed 1-bit precision just cast bool to the type.  */
5223       (convert (convert:boolean_type_node @0))
5224       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5225        (with {
5226           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5227                                                       TYPE_UNSIGNED (type));
5228         }
5229         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5230        (negate (convert:type (convert:boolean_type_node @0))))))
5231     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5232     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5233      (with {
5234        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5235       }
5236       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5237   (if (integer_zerop (@1))
5238    (switch
5239     /* a ? 0 : 1 -> !a.  */
5240     (if (integer_onep (@2))
5241      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5242     /* a ? 0 : -1 -> -(!a).  */
5243     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5244      (if (TYPE_PRECISION (type) == 1)
5245       /* For signed 1-bit precision just cast bool to the type.  */
5246       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5247       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5248        (with {
5249           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5250                                                       TYPE_UNSIGNED (type));
5251         }
5252         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5253                                                 { boolean_true_node; })))))
5254        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5255                                       { boolean_true_node; }))))))
5256     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5257     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5258      (with {
5259        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5260       }
5261       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5262                                 { boolean_true_node; })) { shift; })))))))
5264 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5265    for unsigned types. */
5266 (simplify
5267  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5268  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5269       && bitwise_equal_p (@0, @2))
5270   (convert (eq @0 @1))
5274 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5275    for unsigned types. */
5276 (simplify
5277  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5278  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5279       && bitwise_equal_p (@0, @2))
5280   (convert (eq @0 @1))
5284 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5285    on the first bit of the CST.  */
5286 (simplify
5287  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5288  (if ((wi::to_wide (@1) & 1) != 0)
5289   @2
5290   { build_zero_cst (type); }))
5292 /* Optimize
5293    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5294    x_5 == cstN ? cst4 : cst3
5295    # op is == or != and N is 1 or 2
5296    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5297    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5298    of cst3 and cst4 is smaller.
5299    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5300 (for eqne (ne eq)
5301  (simplify
5302   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5303   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5304        && INTEGRAL_TYPE_P (type)
5305        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5306            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5307    (with {
5308      value_range r;
5309      get_range_query (cfun)->range_of_expr (r, @0);
5310      if (r.undefined_p ())
5311        r.set_varying (TREE_TYPE (@0));
5313      wide_int min = r.lower_bound ();
5314      wide_int max = r.upper_bound ();
5315     }
5316     (if (min + 1 == max
5317          && (wi::to_wide (@1) == min
5318              || wi::to_wide (@1) == max))
5319      (with {
5320        tree arg0 = @2, arg1 = @3;
5321        tree type1;
5322        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5323          std::swap (arg0, arg1);
5324        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5325          type1 = TREE_TYPE (@0);
5326        else
5327          type1 = type;
5328        auto prec = TYPE_PRECISION (type1);
5329        auto unsign = TYPE_UNSIGNED (type1);
5330        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5331         type1 = build_nonstandard_integer_type (prec, unsign);
5332        min = wide_int::from (min, prec,
5333                              TYPE_SIGN (TREE_TYPE (@0)));
5334        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5335                                     TYPE_SIGN (type));
5336        enum tree_code code;
5337        wi::overflow_type ovf;
5338        if (tree_int_cst_lt (arg0, arg1))
5339          {
5340            code = PLUS_EXPR;
5341            a -= min;
5342            if (!unsign)
5343              {
5344                /* lhs is known to be in range [min, min+1] and we want to add a
5345                   to it.  Check if that operation can overflow for those 2 values
5346                   and if yes, force unsigned type.  */
5347                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5348                if (ovf)
5349                  type1 = unsigned_type_for (type1);
5350              }
5351          }
5352        else
5353          {
5354            code = MINUS_EXPR;
5355            a += min;
5356            if (!unsign)
5357              {
5358                /* lhs is known to be in range [min, min+1] and we want to subtract
5359                   it from a.  Check if that operation can overflow for those 2
5360                   values and if yes, force unsigned type.  */
5361                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5362                if (ovf)
5363                 type1 = unsigned_type_for (type1);
5364              }
5365          }
5366        tree arg = wide_int_to_tree (type1, a);
5367       }
5368       (if (code == PLUS_EXPR)
5369        (convert (plus (convert:type1 @0) { arg; }))
5370        (convert (minus { arg; } (convert:type1 @0))))))))))
5371 #endif
5373 (simplify
5374  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5375  (if (INTEGRAL_TYPE_P (type)
5376       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5377   (cond @1 (convert @2) (convert @3))))
5379 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5380    be extended.  */
5381 /* This pattern implements two kinds simplification:
5383    Case 1)
5384    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5385      1) Conversions are type widening from smaller type.
5386      2) Const c1 equals to c2 after canonicalizing comparison.
5387      3) Comparison has tree code LT, LE, GT or GE.
5388    This specific pattern is needed when (cmp (convert x) c) may not
5389    be simplified by comparison patterns because of multiple uses of
5390    x.  It also makes sense here because simplifying across multiple
5391    referred var is always benefitial for complicated cases.
5393    Case 2)
5394    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5395 (for cmp (lt le gt ge eq ne)
5396  (simplify
5397   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5398   (with
5399    {
5400      tree from_type = TREE_TYPE (@1);
5401      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5402      enum tree_code code = ERROR_MARK;
5404      if (INTEGRAL_TYPE_P (from_type)
5405          && int_fits_type_p (@2, from_type)
5406          && (types_match (c1_type, from_type)
5407              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5408                  && (TYPE_UNSIGNED (from_type)
5409                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5410          && (types_match (c2_type, from_type)
5411              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5412                  && (TYPE_UNSIGNED (from_type)
5413                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5414        {
5415          if (cmp != EQ_EXPR)
5416            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5417          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5418          else if (int_fits_type_p (@3, from_type))
5419            code = EQ_EXPR;
5420        }
5421    }
5422    (if (code == MAX_EXPR)
5423     (convert (max @1 (convert @2)))
5424     (if (code == MIN_EXPR)
5425      (convert (min @1 (convert @2)))
5426      (if (code == EQ_EXPR)
5427       (convert (cond (eq @1 (convert @3))
5428                      (convert:from_type @3) (convert:from_type @2)))))))))
5430 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5432      1) OP is PLUS or MINUS.
5433      2) CMP is LT, LE, GT or GE.
5434      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5436    This pattern also handles special cases like:
5438      A) Operand x is a unsigned to signed type conversion and c1 is
5439         integer zero.  In this case,
5440           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5441           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5442      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5443         check equality for (c1+1) and (c1-1) by adjusting comparison
5444         code.
5446    TODO: Though signed type is handled by this pattern, it cannot be
5447    simplified at the moment because C standard requires additional
5448    type promotion.  In order to match&simplify it here, the IR needs
5449    to be cleaned up by other optimizers, i.e, VRP.  */
5450 (for op (plus minus)
5451  (for cmp (lt le gt ge)
5452   (simplify
5453    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5454    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5455     (if (types_match (from_type, to_type)
5456          /* Check if it is special case A).  */
5457          || (TYPE_UNSIGNED (from_type)
5458              && !TYPE_UNSIGNED (to_type)
5459              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5460              && integer_zerop (@1)
5461              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5462      (with
5463       {
5464         wi::overflow_type overflow = wi::OVF_NONE;
5465         enum tree_code code, cmp_code = cmp;
5466         wide_int real_c1;
5467         wide_int c1 = wi::to_wide (@1);
5468         wide_int c2 = wi::to_wide (@2);
5469         wide_int c3 = wi::to_wide (@3);
5470         signop sgn = TYPE_SIGN (from_type);
5472         /* Handle special case A), given x of unsigned type:
5473             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5474             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5475         if (!types_match (from_type, to_type))
5476           {
5477             if (cmp_code == LT_EXPR)
5478               cmp_code = GT_EXPR;
5479             if (cmp_code == GE_EXPR)
5480               cmp_code = LE_EXPR;
5481             c1 = wi::max_value (to_type);
5482           }
5483         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5484            compute (c3 op' c2) and check if it equals to c1 with op' being
5485            the inverted operator of op.  Make sure overflow doesn't happen
5486            if it is undefined.  */
5487         if (op == PLUS_EXPR)
5488           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5489         else
5490           real_c1 = wi::add (c3, c2, sgn, &overflow);
5492         code = cmp_code;
5493         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5494           {
5495             /* Check if c1 equals to real_c1.  Boundary condition is handled
5496                by adjusting comparison operation if necessary.  */
5497             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5498                 && !overflow)
5499               {
5500                 /* X <= Y - 1 equals to X < Y.  */
5501                 if (cmp_code == LE_EXPR)
5502                   code = LT_EXPR;
5503                 /* X > Y - 1 equals to X >= Y.  */
5504                 if (cmp_code == GT_EXPR)
5505                   code = GE_EXPR;
5506               }
5507             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5508                 && !overflow)
5509               {
5510                 /* X < Y + 1 equals to X <= Y.  */
5511                 if (cmp_code == LT_EXPR)
5512                   code = LE_EXPR;
5513                 /* X >= Y + 1 equals to X > Y.  */
5514                 if (cmp_code == GE_EXPR)
5515                   code = GT_EXPR;
5516               }
5517             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5518               {
5519                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5520                   code = MIN_EXPR;
5521                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5522                   code = MAX_EXPR;
5523               }
5524           }
5525       }
5526       (if (code == MAX_EXPR)
5527        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5528            { wide_int_to_tree (from_type, c2); })
5529        (if (code == MIN_EXPR)
5530         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5531             { wide_int_to_tree (from_type, c2); })))))))))
5533 #if GIMPLE
5534 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5535    in fold_cond_expr_with_comparison for GENERIC folding with
5536    some extra constraints.  */
5537 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5538  (simplify
5539   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5540         (convert3? @0) (convert4? @1))
5541   (if (!HONOR_SIGNED_ZEROS (type)
5542        && (/* Allow widening conversions of the compare operands as data.  */
5543            (INTEGRAL_TYPE_P (type)
5544             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5545             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5546             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5547             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5548            /* Or sign conversions for the comparison.  */
5549            || (types_match (type, TREE_TYPE (@0))
5550                && types_match (type, TREE_TYPE (@1)))))
5551    (switch
5552     (if (cmp == EQ_EXPR)
5553      (if (VECTOR_TYPE_P (type))
5554       (view_convert @c1)
5555       (convert @c1)))
5556     (if (cmp == NE_EXPR)
5557      (if (VECTOR_TYPE_P (type))
5558       (view_convert @c0)
5559       (convert @c0)))
5560     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5561      (if (!HONOR_NANS (type))
5562       (if (VECTOR_TYPE_P (type))
5563        (view_convert (min @c0 @c1))
5564        (convert (min @c0 @c1)))))
5565     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5566      (if (!HONOR_NANS (type))
5567       (if (VECTOR_TYPE_P (type))
5568        (view_convert (max @c0 @c1))
5569        (convert (max @c0 @c1)))))
5570     (if (cmp == UNEQ_EXPR)
5571      (if (!HONOR_NANS (type))
5572       (if (VECTOR_TYPE_P (type))
5573        (view_convert @c1)
5574        (convert @c1))))
5575     (if (cmp == LTGT_EXPR)
5576      (if (!HONOR_NANS (type))
5577       (if (VECTOR_TYPE_P (type))
5578        (view_convert @c0)
5579        (convert @c0))))))))
5580 #endif
5582 (for cnd (cond vec_cond)
5583  /* (a != b) ? (a - b) : 0 -> (a - b) */
5584  (simplify
5585   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5586   @2)
5587  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5588  (simplify
5589   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5590   @2)
5591  /* (a != b) ? (a & b) : a -> (a & b) */
5592  /* (a != b) ? (a | b) : a -> (a | b) */
5593  /* (a != b) ? min(a,b) : a -> min(a,b) */
5594  /* (a != b) ? max(a,b) : a -> max(a,b) */
5595  (for op (bit_and bit_ior min max)
5596   (simplify
5597    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5598    @2))
5599  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5600  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5601  (for op (mult plus)
5602   (simplify
5603    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5604    (if (ANY_INTEGRAL_TYPE_P (type))
5605     @2)))
5606  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5607  (simplify
5608   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5609   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5610    @2))
5613 /* These was part of minmax phiopt.  */
5614 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5615    to minmax<min/max<a, b>, c> */
5616 (for minmax (min max)
5617  (for cmp (lt le gt ge ne)
5618   (simplify
5619    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5620    (with
5621     {
5622       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5623     }
5624     (if (code == MIN_EXPR)
5625      (minmax (min @1 @2) @4)
5626      (if (code == MAX_EXPR)
5627       (minmax (max @1 @2) @4)))))))
5629 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5630 (for cmp    (gt  ge  lt  le)
5631      minmax (min min max max)
5632  (simplify
5633   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5634    (with
5635     {
5636       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5637     }
5638     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5639          && code == MIN_EXPR
5640          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5641      (min @2 @4)
5642      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5643           && code == MAX_EXPR
5644           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5645       (max @2 @4))))))
5647 #if GIMPLE
5648 /* These patterns should be after min/max detection as simplifications
5649    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5650    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5651    Even without those, reaching min/max/and/ior faster is better.  */
5652 (simplify
5653  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5654  (switch
5655   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5656   (if (integer_zerop (@2))
5657    (bit_and (convert @0) @1))
5658   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5659   (if (integer_zerop (@1))
5660    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5661   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5662   (if (integer_onep (@1))
5663    (bit_ior (convert @0) @2))
5664   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5665   (if (integer_onep (@2))
5666    (bit_ior (bit_xor (convert @0) @2) @1))
5669 #endif
5671 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5672 (simplify
5673  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5674  (if (!TYPE_SATURATING (type)
5675       && (TYPE_OVERFLOW_WRAPS (type)
5676           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5677       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5678   @3))
5680 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5681 (simplify
5682  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5683  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5684   @3))
5686 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5687 (for op (abs absu)
5688  (simplify
5689   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5690   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5691    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5692     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5693      (convert (absu:utype @0)))
5694     @3))))
5696 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5697    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5698 (simplify
5699  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5700  (if (TYPE_UNSIGNED (type))
5701   (cond (ge @0 @1) (negate @0) @2)))
5703 (for cnd (cond vec_cond)
5704  /* A ? B : (A ? X : C) -> A ? B : C.  */
5705  (simplify
5706   (cnd @0 (cnd @0 @1 @2) @3)
5707   (cnd @0 @1 @3))
5708  (simplify
5709   (cnd @0 @1 (cnd @0 @2 @3))
5710   (cnd @0 @1 @3))
5711  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5712  /* ???  This matches embedded conditions open-coded because genmatch
5713     would generate matching code for conditions in separate stmts only.
5714     The following is still important to merge then and else arm cases
5715     from if-conversion.  */
5716  (simplify
5717   (cnd @0 @1 (cnd @2 @3 @4))
5718   (if (inverse_conditions_p (@0, @2))
5719    (cnd @0 @1 @3)))
5720  (simplify
5721   (cnd @0 (cnd @1 @2 @3) @4)
5722   (if (inverse_conditions_p (@0, @1))
5723    (cnd @0 @3 @4)))
5725  /* A ? B : B -> B.  */
5726  (simplify
5727   (cnd @0 @1 @1)
5728   @1)
5730  /* !A ? B : C -> A ? C : B.  */
5731  (simplify
5732   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5733   (cnd @0 @2 @1)))
5735 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5737    None of these transformations work for modes with signed
5738    zeros.  If A is +/-0, the first two transformations will
5739    change the sign of the result (from +0 to -0, or vice
5740    versa).  The last four will fix the sign of the result,
5741    even though the original expressions could be positive or
5742    negative, depending on the sign of A.
5744    Note that all these transformations are correct if A is
5745    NaN, since the two alternatives (A and -A) are also NaNs.  */
5747 (for cnd (cond vec_cond)
5748  /* A == 0 ? A : -A    same as -A */
5749  (for cmp (eq uneq)
5750   (simplify
5751    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5752     (if (!HONOR_SIGNED_ZEROS (type)
5753          && bitwise_equal_p (@0, @2))
5754      @1))
5755   (simplify
5756    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5757     (if (!HONOR_SIGNED_ZEROS (type)
5758          && bitwise_equal_p (@0, @2))
5759      @1))
5761  /* A != 0 ? A : -A    same as A */
5762  (for cmp (ne ltgt)
5763   (simplify
5764    (cnd (cmp @0 zerop) @1 (negate @1))
5765     (if (!HONOR_SIGNED_ZEROS (type)
5766          && bitwise_equal_p (@0, @1))
5767      @1))
5768   (simplify
5769    (cnd (cmp @0 zerop) @1 integer_zerop)
5770     (if (!HONOR_SIGNED_ZEROS (type)
5771          && bitwise_equal_p (@0, @1))
5772      @1))
5774  /* A >=/> 0 ? A : -A    same as abs (A) */
5775  (for cmp (ge gt)
5776   (simplify
5777    (cnd (cmp @0 zerop) @1 (negate @1))
5778     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5779          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5780          && bitwise_equal_p (@0, @1))
5781      (if (TYPE_UNSIGNED (type))
5782       (absu:type @0)
5783       (abs @0)))))
5784  /* A <=/< 0 ? A : -A    same as -abs (A) */
5785  (for cmp (le lt)
5786   (simplify
5787    (cnd (cmp @0 zerop) @1 (negate @1))
5788     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5789          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5790          && bitwise_equal_p (@0, @1))
5791      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5792            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5793           || TYPE_UNSIGNED (type))
5794       (with {
5795         tree utype = unsigned_type_for (TREE_TYPE(@0));
5796        }
5797        (convert (negate (absu:utype @0))))
5798        (negate (abs @0)))))
5801  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5802  (for cmp (eq uneq)
5803   (simplify
5804    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5805    (if (!HONOR_SIGNED_ZEROS (type))
5806     @3))
5807   (simplify
5808    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5809    @3)
5811  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5812  (for cmp (ne ltgt)
5813   (simplify
5814    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5815    (if (!HONOR_SIGNED_ZEROS (type))
5816     @0))
5817   (simplify
5818    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5819    @0)
5821  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5822  (for cmp (ge gt)
5823   (simplify
5824    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5825    (if (!HONOR_SIGNED_ZEROS (type)
5826         && !TYPE_UNSIGNED (type))
5827     (abs @0))))
5828  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5829  (for cmp (le lt)
5830   (simplify
5831    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5832    (if (!HONOR_SIGNED_ZEROS (type)
5833         && !TYPE_UNSIGNED (type))
5834     (if (ANY_INTEGRAL_TYPE_P (type)
5835          && !TYPE_OVERFLOW_WRAPS (type))
5836      (with {
5837         tree utype = unsigned_type_for (type);
5838       }
5839       (convert (negate (absu:utype @0))))
5840       (negate (abs @0)))))
5844 /* -(type)!A -> (type)A - 1.  */
5845 (simplify
5846  (negate (convert?:s (logical_inverted_value:s @0)))
5847  (if (INTEGRAL_TYPE_P (type)
5848       && TREE_CODE (type) != BOOLEAN_TYPE
5849       && TYPE_PRECISION (type) > 1
5850       && TREE_CODE (@0) == SSA_NAME
5851       && ssa_name_has_boolean_range (@0))
5852   (plus (convert:type @0) { build_all_ones_cst (type); })))
5854 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5855    return all -1 or all 0 results.  */
5856 /* ??? We could instead convert all instances of the vec_cond to negate,
5857    but that isn't necessarily a win on its own.  */
5858 (simplify
5859  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5860  (if (VECTOR_TYPE_P (type)
5861       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5862                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5863       && (TYPE_MODE (TREE_TYPE (type))
5864           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5865   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5867 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5868 (simplify
5869  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5870  (if (VECTOR_TYPE_P (type)
5871       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5872                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5873       && (TYPE_MODE (TREE_TYPE (type))
5874           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5875   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5878 /* Simplifications of comparisons.  */
5880 /* See if we can reduce the magnitude of a constant involved in a
5881    comparison by changing the comparison code.  This is a canonicalization
5882    formerly done by maybe_canonicalize_comparison_1.  */
5883 (for cmp  (le gt)
5884      acmp (lt ge)
5885  (simplify
5886   (cmp @0 uniform_integer_cst_p@1)
5887   (with { tree cst = uniform_integer_cst_p (@1); }
5888    (if (tree_int_cst_sgn (cst) == -1)
5889      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5890                                    wide_int_to_tree (TREE_TYPE (cst),
5891                                                      wi::to_wide (cst)
5892                                                      + 1)); })))))
5893 (for cmp  (ge lt)
5894      acmp (gt le)
5895  (simplify
5896   (cmp @0 uniform_integer_cst_p@1)
5897   (with { tree cst = uniform_integer_cst_p (@1); }
5898    (if (tree_int_cst_sgn (cst) == 1)
5899     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5900                                   wide_int_to_tree (TREE_TYPE (cst),
5901                                   wi::to_wide (cst) - 1)); })))))
5903 /* We can simplify a logical negation of a comparison to the
5904    inverted comparison.  As we cannot compute an expression
5905    operator using invert_tree_comparison we have to simulate
5906    that with expression code iteration.  */
5907 (for cmp (tcc_comparison)
5908      icmp (inverted_tcc_comparison)
5909      ncmp (inverted_tcc_comparison_with_nans)
5910  /* Ideally we'd like to combine the following two patterns
5911     and handle some more cases by using
5912       (logical_inverted_value (cmp @0 @1))
5913     here but for that genmatch would need to "inline" that.
5914     For now implement what forward_propagate_comparison did.  */
5915  (simplify
5916   (bit_not (cmp @0 @1))
5917   (if (VECTOR_TYPE_P (type)
5918        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5919    /* Comparison inversion may be impossible for trapping math,
5920       invert_tree_comparison will tell us.  But we can't use
5921       a computed operator in the replacement tree thus we have
5922       to play the trick below.  */
5923    (with { enum tree_code ic = invert_tree_comparison
5924              (cmp, HONOR_NANS (@0)); }
5925     (if (ic == icmp)
5926      (icmp @0 @1)
5927      (if (ic == ncmp)
5928       (ncmp @0 @1))))))
5929  (simplify
5930   (bit_xor (cmp @0 @1) integer_truep)
5931   (with { enum tree_code ic = invert_tree_comparison
5932             (cmp, HONOR_NANS (@0)); }
5933    (if (ic == icmp)
5934     (icmp @0 @1)
5935     (if (ic == ncmp)
5936      (ncmp @0 @1)))))
5937  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5938  (simplify
5939   (ne (cmp@2 @0 @1) integer_zerop)
5940   (if (types_match (type, TREE_TYPE (@2)))
5941    (cmp @0 @1)))
5942  (simplify
5943   (eq (cmp@2 @0 @1) integer_truep)
5944   (if (types_match (type, TREE_TYPE (@2)))
5945    (cmp @0 @1)))
5946  (simplify
5947   (ne (cmp@2 @0 @1) integer_truep)
5948   (if (types_match (type, TREE_TYPE (@2)))
5949    (with { enum tree_code ic = invert_tree_comparison
5950              (cmp, HONOR_NANS (@0)); }
5951     (if (ic == icmp)
5952      (icmp @0 @1)
5953      (if (ic == ncmp)
5954       (ncmp @0 @1))))))
5955  (simplify
5956   (eq (cmp@2 @0 @1) integer_zerop)
5957   (if (types_match (type, TREE_TYPE (@2)))
5958    (with { enum tree_code ic = invert_tree_comparison
5959              (cmp, HONOR_NANS (@0)); }
5960     (if (ic == icmp)
5961      (icmp @0 @1)
5962      (if (ic == ncmp)
5963       (ncmp @0 @1)))))))
5965 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5966    ??? The transformation is valid for the other operators if overflow
5967    is undefined for the type, but performing it here badly interacts
5968    with the transformation in fold_cond_expr_with_comparison which
5969    attempts to synthetize ABS_EXPR.  */
5970 (for cmp (eq ne)
5971  (for sub (minus pointer_diff)
5972   (simplify
5973    (cmp (sub@2 @0 @1) integer_zerop)
5974    (if (single_use (@2))
5975     (cmp @0 @1)))))
5977 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5978    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5979 (for cmp (lt ge)
5980  (simplify
5981   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5982    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5983         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5984         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5985     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5986 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5987    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5988 (simplify
5989  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5990   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5991        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5992        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5993    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5995 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5996    signed arithmetic case.  That form is created by the compiler
5997    often enough for folding it to be of value.  One example is in
5998    computing loop trip counts after Operator Strength Reduction.  */
5999 (for cmp (simple_comparison)
6000      scmp (swapped_simple_comparison)
6001  (simplify
6002   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6003   /* Handle unfolded multiplication by zero.  */
6004   (if (integer_zerop (@1))
6005    (cmp @1 @2)
6006    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6007         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6008         && single_use (@3))
6009     /* If @1 is negative we swap the sense of the comparison.  */
6010     (if (tree_int_cst_sgn (@1) < 0)
6011      (scmp @0 @2)
6012      (cmp @0 @2))))))
6014 /* For integral types with undefined overflow fold
6015    x * C1 == C2 into x == C2 / C1 or false.
6016    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6017    Z / 2^n Z.  */
6018 (for cmp (eq ne)
6019  (simplify
6020   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6021   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6022        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6023        && wi::to_wide (@1) != 0)
6024    (with { widest_int quot; }
6025     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6026                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6027      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6028      { constant_boolean_node (cmp == NE_EXPR, type); }))
6029    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6030         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6031         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6032     (cmp @0
6033      {
6034        tree itype = TREE_TYPE (@0);
6035        int p = TYPE_PRECISION (itype);
6036        wide_int m = wi::one (p + 1) << p;
6037        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6038        wide_int i = wide_int::from (wi::mod_inv (a, m),
6039                                     p, TYPE_SIGN (itype));
6040        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6041      })))))
6043 /* Simplify comparison of something with itself.  For IEEE
6044    floating-point, we can only do some of these simplifications.  */
6045 (for cmp (eq ge le)
6046  (simplify
6047   (cmp @0 @0)
6048   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6049        || ! tree_expr_maybe_nan_p (@0))
6050    { constant_boolean_node (true, type); }
6051    (if (cmp != EQ_EXPR
6052         /* With -ftrapping-math conversion to EQ loses an exception.  */
6053         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6054             || ! flag_trapping_math))
6055     (eq @0 @0)))))
6056 (for cmp (ne gt lt)
6057  (simplify
6058   (cmp @0 @0)
6059   (if (cmp != NE_EXPR
6060        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6061        || ! tree_expr_maybe_nan_p (@0))
6062    { constant_boolean_node (false, type); })))
6063 (for cmp (unle unge uneq)
6064  (simplify
6065   (cmp @0 @0)
6066   { constant_boolean_node (true, type); }))
6067 (for cmp (unlt ungt)
6068  (simplify
6069   (cmp @0 @0)
6070   (unordered @0 @0)))
6071 (simplify
6072  (ltgt @0 @0)
6073  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6074   { constant_boolean_node (false, type); }))
6076 /* x == ~x -> false */
6077 /* x != ~x -> true */
6078 (for cmp (eq ne)
6079  (simplify
6080   (cmp:c @0 (bit_not @0))
6081   { constant_boolean_node (cmp == NE_EXPR, type); }))
6083 /* Fold ~X op ~Y as Y op X.  */
6084 (for cmp (simple_comparison)
6085  (simplify
6086   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6087   (if (single_use (@2) && single_use (@3))
6088    (with { tree otype = TREE_TYPE (@4); }
6089     (cmp (convert:otype @1) (convert:otype @0))))))
6091 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6092 (for cmp (simple_comparison)
6093      scmp (swapped_simple_comparison)
6094  (simplify
6095   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6096   (if (single_use (@2)
6097        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6098    (with { tree otype = TREE_TYPE (@1); }
6099     (scmp (convert:otype @0) (bit_not @1))))))
6101 (for cmp (simple_comparison)
6102  (simplify
6103   (cmp @0 REAL_CST@1)
6104   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6105   (switch
6106    /* a CMP (-0) -> a CMP 0  */
6107    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6108     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6109    /* (-0) CMP b -> 0 CMP b.  */
6110    (if (TREE_CODE (@0) == REAL_CST
6111         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6112     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6113    /* x != NaN is always true, other ops are always false.  */
6114    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6115         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6116         && !tree_expr_signaling_nan_p (@1)
6117         && !tree_expr_maybe_signaling_nan_p (@0))
6118     { constant_boolean_node (cmp == NE_EXPR, type); })
6119    /* NaN != y is always true, other ops are always false.  */
6120    (if (TREE_CODE (@0) == REAL_CST
6121         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6122         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6123         && !tree_expr_signaling_nan_p (@0)
6124         && !tree_expr_signaling_nan_p (@1))
6125     { constant_boolean_node (cmp == NE_EXPR, type); })
6126    /* Fold comparisons against infinity.  */
6127    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6128         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6129     (with
6130      {
6131        REAL_VALUE_TYPE max;
6132        enum tree_code code = cmp;
6133        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6134        if (neg)
6135          code = swap_tree_comparison (code);
6136      }
6137      (switch
6138       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6139       (if (code == GT_EXPR
6140            && !(HONOR_NANS (@0) && flag_trapping_math))
6141        { constant_boolean_node (false, type); })
6142       (if (code == LE_EXPR)
6143        /* x <= +Inf is always true, if we don't care about NaNs.  */
6144        (if (! HONOR_NANS (@0))
6145         { constant_boolean_node (true, type); }
6146         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6147            an "invalid" exception.  */
6148         (if (!flag_trapping_math)
6149          (eq @0 @0))))
6150       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6151          for == this introduces an exception for x a NaN.  */
6152       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6153            || code == GE_EXPR)
6154        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6155         (if (neg)
6156          (lt @0 { build_real (TREE_TYPE (@0), max); })
6157          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6158       /* x < +Inf is always equal to x <= DBL_MAX.  */
6159       (if (code == LT_EXPR)
6160        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6161         (if (neg)
6162          (ge @0 { build_real (TREE_TYPE (@0), max); })
6163          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6164       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6165          an exception for x a NaN so use an unordered comparison.  */
6166       (if (code == NE_EXPR)
6167        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6168         (if (! HONOR_NANS (@0))
6169          (if (neg)
6170           (ge @0 { build_real (TREE_TYPE (@0), max); })
6171           (le @0 { build_real (TREE_TYPE (@0), max); }))
6172          (if (neg)
6173           (unge @0 { build_real (TREE_TYPE (@0), max); })
6174           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6176  /* If this is a comparison of a real constant with a PLUS_EXPR
6177     or a MINUS_EXPR of a real constant, we can convert it into a
6178     comparison with a revised real constant as long as no overflow
6179     occurs when unsafe_math_optimizations are enabled.  */
6180  (if (flag_unsafe_math_optimizations)
6181   (for op (plus minus)
6182    (simplify
6183     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6184     (with
6185      {
6186        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6187                                TREE_TYPE (@1), @2, @1);
6188      }
6189      (if (tem && !TREE_OVERFLOW (tem))
6190       (cmp @0 { tem; }))))))
6192  /* Likewise, we can simplify a comparison of a real constant with
6193     a MINUS_EXPR whose first operand is also a real constant, i.e.
6194     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6195     floating-point types only if -fassociative-math is set.  */
6196  (if (flag_associative_math)
6197   (simplify
6198    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6199    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6200     (if (tem && !TREE_OVERFLOW (tem))
6201      (cmp { tem; } @1)))))
6203  /* Fold comparisons against built-in math functions.  */
6204  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6205   (for sq (SQRT)
6206    (simplify
6207     (cmp (sq @0) REAL_CST@1)
6208     (switch
6209      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6210       (switch
6211        /* sqrt(x) < y is always false, if y is negative.  */
6212        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6213         { constant_boolean_node (false, type); })
6214        /* sqrt(x) > y is always true, if y is negative and we
6215           don't care about NaNs, i.e. negative values of x.  */
6216        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6217         { constant_boolean_node (true, type); })
6218        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6219        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6220      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6221       (switch
6222        /* sqrt(x) < 0 is always false.  */
6223        (if (cmp == LT_EXPR)
6224         { constant_boolean_node (false, type); })
6225        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6226        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6227         { constant_boolean_node (true, type); })
6228        /* sqrt(x) <= 0 -> x == 0.  */
6229        (if (cmp == LE_EXPR)
6230         (eq @0 @1))
6231        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6232           == or !=.  In the last case:
6234             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6236           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6237           the results for other x follow from natural arithmetic.  */
6238        (cmp @0 @1)))
6239      (if ((cmp == LT_EXPR
6240            || cmp == LE_EXPR
6241            || cmp == GT_EXPR
6242            || cmp == GE_EXPR)
6243           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6244           /* Give up for -frounding-math.  */
6245           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6246       (with
6247        {
6248          REAL_VALUE_TYPE c2;
6249          enum tree_code ncmp = cmp;
6250          const real_format *fmt
6251            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6252          real_arithmetic (&c2, MULT_EXPR,
6253                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6254          real_convert (&c2, fmt, &c2);
6255          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6256             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6257          if (!REAL_VALUE_ISINF (c2))
6258            {
6259              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6260                                         build_real (TREE_TYPE (@0), c2));
6261              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6262                ncmp = ERROR_MARK;
6263              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6264                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6265                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6266              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6267                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6268                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6269              else
6270                {
6271                  /* With rounding to even, sqrt of up to 3 different values
6272                     gives the same normal result, so in some cases c2 needs
6273                     to be adjusted.  */
6274                  REAL_VALUE_TYPE c2alt, tow;
6275                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6276                    tow = dconst0;
6277                  else
6278                    tow = dconstinf;
6279                  real_nextafter (&c2alt, fmt, &c2, &tow);
6280                  real_convert (&c2alt, fmt, &c2alt);
6281                  if (REAL_VALUE_ISINF (c2alt))
6282                    ncmp = ERROR_MARK;
6283                  else
6284                    {
6285                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6286                                            build_real (TREE_TYPE (@0), c2alt));
6287                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6288                        ncmp = ERROR_MARK;
6289                      else if (real_equal (&TREE_REAL_CST (c3),
6290                                           &TREE_REAL_CST (@1)))
6291                        c2 = c2alt;
6292                    }
6293                }
6294            }
6295        }
6296        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6297         (if (REAL_VALUE_ISINF (c2))
6298          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6299          (if (HONOR_INFINITIES (@0))
6300           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6301           { constant_boolean_node (false, type); })
6302          /* sqrt(x) > c is the same as x > c*c.  */
6303          (if (ncmp != ERROR_MARK)
6304           (if (ncmp == GE_EXPR)
6305            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6306            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6307         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6308         (if (REAL_VALUE_ISINF (c2))
6309          (switch
6310           /* sqrt(x) < y is always true, when y is a very large
6311              value and we don't care about NaNs or Infinities.  */
6312           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6313            { constant_boolean_node (true, type); })
6314           /* sqrt(x) < y is x != +Inf when y is very large and we
6315              don't care about NaNs.  */
6316           (if (! HONOR_NANS (@0))
6317            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6318           /* sqrt(x) < y is x >= 0 when y is very large and we
6319              don't care about Infinities.  */
6320           (if (! HONOR_INFINITIES (@0))
6321            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6322           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6323           (if (GENERIC)
6324            (truth_andif
6325             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6326             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6327          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6328          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6329           (if (ncmp == LT_EXPR)
6330            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6331            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6332           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6333           (if (ncmp != ERROR_MARK && GENERIC)
6334            (if (ncmp == LT_EXPR)
6335             (truth_andif
6336              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6337              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6338             (truth_andif
6339              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6340              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6341    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6342    (simplify
6343     (cmp (sq @0) (sq @1))
6344       (if (! HONOR_NANS (@0))
6345         (cmp @0 @1))))))
6347 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6348 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6349      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6350  (simplify
6351   (cmp (float@0 @1) (float @2))
6352    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6353         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6354     (with
6355      {
6356        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6357        tree type1 = TREE_TYPE (@1);
6358        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6359        tree type2 = TREE_TYPE (@2);
6360        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6361      }
6362      (if (fmt.can_represent_integral_type_p (type1)
6363           && fmt.can_represent_integral_type_p (type2))
6364       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6365        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6366        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6367             && type1_signed_p >= type2_signed_p)
6368         (icmp @1 (convert @2))
6369         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6370              && type1_signed_p <= type2_signed_p)
6371          (icmp (convert:type2 @1) @2)
6372          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6373               && type1_signed_p == type2_signed_p)
6374           (icmp @1 @2))))))))))
6376 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6377 (for cmp  (lt le eq ne ge gt)
6378      icmp (le le eq ne ge ge)
6379  (simplify
6380   (cmp (float @0) REAL_CST@1)
6381    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6382         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6383     (with
6384      {
6385        tree itype = TREE_TYPE (@0);
6386        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6387        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6388        /* Be careful to preserve any potential exceptions due to
6389           NaNs.  qNaNs are ok in == or != context.
6390           TODO: relax under -fno-trapping-math or
6391           -fno-signaling-nans.  */
6392        bool exception_p
6393          = real_isnan (cst) && (cst->signalling
6394                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6395      }
6396      /* TODO: allow non-fitting itype and SNaNs when
6397         -fno-trapping-math.  */
6398      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6399       (with
6400        {
6401          signop isign = TYPE_SIGN (itype);
6402          REAL_VALUE_TYPE imin, imax;
6403          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6404          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6406          REAL_VALUE_TYPE icst;
6407          if (cmp == GT_EXPR || cmp == GE_EXPR)
6408            real_ceil (&icst, fmt, cst);
6409          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6410            real_floor (&icst, fmt, cst);
6411          else
6412            real_trunc (&icst, fmt, cst);
6414          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6416          bool overflow_p = false;
6417          wide_int icst_val
6418            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6419        }
6420        (switch
6421         /* Optimize cases when CST is outside of ITYPE's range.  */
6422         (if (real_compare (LT_EXPR, cst, &imin))
6423          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6424                                   type); })
6425         (if (real_compare (GT_EXPR, cst, &imax))
6426          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6427                                   type); })
6428         /* Remove cast if CST is an integer representable by ITYPE.  */
6429         (if (cst_int_p)
6430          (cmp @0 { gcc_assert (!overflow_p);
6431                    wide_int_to_tree (itype, icst_val); })
6432         )
6433         /* When CST is fractional, optimize
6434             (FTYPE) N == CST -> 0
6435             (FTYPE) N != CST -> 1.  */
6436         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6437          { constant_boolean_node (cmp == NE_EXPR, type); })
6438         /* Otherwise replace with sensible integer constant.  */
6439         (with
6440          {
6441            gcc_checking_assert (!overflow_p);
6442          }
6443          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6445 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6446 (for cmp (eq ne)
6447  (simplify
6448   (cmp (exact_div @0 @1) INTEGER_CST@2)
6449   (if (!integer_zerop (@1))
6450    (if (wi::to_wide (@2) == 0)
6451     (cmp @0 @2)
6452     (if (TREE_CODE (@1) == INTEGER_CST)
6453      (with
6454       {
6455         wi::overflow_type ovf;
6456         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6457                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6458       }
6459       (if (ovf)
6460        { constant_boolean_node (cmp == NE_EXPR, type); }
6461        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6462 (for cmp (lt le gt ge)
6463  (simplify
6464   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6465   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6466    (with
6467     {
6468       wi::overflow_type ovf;
6469       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6470                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6471     }
6472     (if (ovf)
6473      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6474                                         TYPE_SIGN (TREE_TYPE (@2)))
6475                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6476      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6478 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6480    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6481    For large C (more than min/B+2^size), this is also true, with the
6482    multiplication computed modulo 2^size.
6483    For intermediate C, this just tests the sign of A.  */
6484 (for cmp  (lt le gt ge)
6485      cmp2 (ge ge lt lt)
6486  (simplify
6487   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6488   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6489        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6490        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6491    (with
6492     {
6493       tree utype = TREE_TYPE (@2);
6494       wide_int denom = wi::to_wide (@1);
6495       wide_int right = wi::to_wide (@2);
6496       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6497       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6498       bool small = wi::leu_p (right, smax);
6499       bool large = wi::geu_p (right, smin);
6500     }
6501     (if (small || large)
6502      (cmp (convert:utype @0) (mult @2 (convert @1)))
6503      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6505 /* Unordered tests if either argument is a NaN.  */
6506 (simplify
6507  (bit_ior (unordered @0 @0) (unordered @1 @1))
6508  (if (types_match (@0, @1))
6509   (unordered @0 @1)))
6510 (simplify
6511  (bit_and (ordered @0 @0) (ordered @1 @1))
6512  (if (types_match (@0, @1))
6513   (ordered @0 @1)))
6514 (simplify
6515  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6516  @2)
6517 (simplify
6518  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6519  @2)
6521 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6522    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6524    Note that comparisons
6525      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6526      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6527    will be canonicalized to above so there's no need to
6528    consider them here.
6529  */
6531 (for cmp (le gt)
6532      eqcmp (eq ne)
6533  (simplify
6534   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6535   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6536    (with
6537     {
6538      tree ty = TREE_TYPE (@0);
6539      unsigned prec = TYPE_PRECISION (ty);
6540      wide_int mask = wi::to_wide (@2, prec);
6541      wide_int rhs = wi::to_wide (@3, prec);
6542      signop sgn = TYPE_SIGN (ty);
6543     }
6544     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6545          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6546       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6547              { build_zero_cst (ty); }))))))
6549 /* -A CMP -B -> B CMP A.  */
6550 (for cmp (tcc_comparison)
6551      scmp (swapped_tcc_comparison)
6552  (simplify
6553   (cmp (negate @0) (negate @1))
6554   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6555        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6556            && (cmp == EQ_EXPR
6557                || cmp == NE_EXPR
6558                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6559    (scmp @0 @1)))
6560  (simplify
6561   (cmp (negate @0) CONSTANT_CLASS_P@1)
6562   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6563        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6564            && (cmp == EQ_EXPR
6565                || cmp == NE_EXPR
6566                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6567    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6568     (if (tem && !TREE_OVERFLOW (tem))
6569      (scmp @0 { tem; }))))))
6571 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6572 (for op (abs absu)
6573  (for eqne (eq ne)
6574   (simplify
6575    (eqne (op @0) zerop@1)
6576    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6578 /* From fold_sign_changed_comparison and fold_widened_comparison.
6579    FIXME: the lack of symmetry is disturbing.  */
6580 (for cmp (simple_comparison)
6581  (simplify
6582   (cmp (convert@0 @00) (convert?@1 @10))
6583   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6584        /* Disable this optimization if we're casting a function pointer
6585           type on targets that require function pointer canonicalization.  */
6586        && !(targetm.have_canonicalize_funcptr_for_compare ()
6587             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6588                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6589                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6590                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6591        && single_use (@0))
6592    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6593         && (TREE_CODE (@10) == INTEGER_CST
6594             || @1 != @10)
6595         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6596             || cmp == NE_EXPR
6597             || cmp == EQ_EXPR)
6598         && !POINTER_TYPE_P (TREE_TYPE (@00))
6599         /* (int)bool:32 != (int)uint is not the same as
6600            bool:32 != (bool:32)uint since boolean types only have two valid
6601            values independent of their precision.  */
6602         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6603             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6604     /* ???  The special-casing of INTEGER_CST conversion was in the original
6605        code and here to avoid a spurious overflow flag on the resulting
6606        constant which fold_convert produces.  */
6607     (if (TREE_CODE (@1) == INTEGER_CST)
6608      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6609                                 wide_int::from (wi::to_wide (@1),
6610                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6611                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6612                                                 TYPE_SIGN (TREE_TYPE (@1))),
6613                                 0, TREE_OVERFLOW (@1)); })
6614      (cmp @00 (convert @1)))
6616     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6617      /* If possible, express the comparison in the shorter mode.  */
6618      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6619            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6620            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6621                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6622           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6623               || ((TYPE_PRECISION (TREE_TYPE (@00))
6624                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6625                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6626                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6627               || (TREE_CODE (@10) == INTEGER_CST
6628                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6629                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6630       (cmp @00 (convert @10))
6631       (if (TREE_CODE (@10) == INTEGER_CST
6632            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6633            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6634        (with
6635         {
6636           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6637           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6638           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6639           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6640         }
6641         (if (above || below)
6642          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6643           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6644           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6645            { constant_boolean_node (above ? true : false, type); }
6646            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6647             { constant_boolean_node (above ? false : true, type); })))))))))
6648    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6649    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6650         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6651             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6652         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6653             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6654     (with
6655      {
6656        tree type1 = TREE_TYPE (@10);
6657        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6658          {
6659            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6660            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6661                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6662              type1 = float_type_node;
6663            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6664                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6665              type1 = double_type_node;
6666          }
6667       tree newtype
6668         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6669            ? TREE_TYPE (@00) : type1);
6670      }
6671      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6672       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6675 (for cmp (eq ne)
6676  (simplify
6677   /* SSA names are canonicalized to 2nd place.  */
6678   (cmp addr@0 SSA_NAME@1)
6679   (with
6680    {
6681      poly_int64 off; tree base;
6682      tree addr = (TREE_CODE (@0) == SSA_NAME
6683                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6684    }
6685    /* A local variable can never be pointed to by
6686       the default SSA name of an incoming parameter.  */
6687    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6688         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6689         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6690         && TREE_CODE (base) == VAR_DECL
6691         && auto_var_in_fn_p (base, current_function_decl))
6692     (if (cmp == NE_EXPR)
6693      { constant_boolean_node (true, type); }
6694      { constant_boolean_node (false, type); })
6695     /* If the address is based on @1 decide using the offset.  */
6696     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6697          && TREE_CODE (base) == MEM_REF
6698          && TREE_OPERAND (base, 0) == @1)
6699      (with { off += mem_ref_offset (base).force_shwi (); }
6700       (if (known_ne (off, 0))
6701        { constant_boolean_node (cmp == NE_EXPR, type); }
6702        (if (known_eq (off, 0))
6703         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6705 /* Equality compare simplifications from fold_binary  */
6706 (for cmp (eq ne)
6708  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6709     Similarly for NE_EXPR.  */
6710  (simplify
6711   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6712   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6713        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6714    { constant_boolean_node (cmp == NE_EXPR, type); }))
6716  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6717  (simplify
6718   (cmp (bit_xor @0 @1) integer_zerop)
6719   (cmp @0 @1))
6721  /* (X ^ Y) == Y becomes X == 0.
6722     Likewise (X ^ Y) == X becomes Y == 0.  */
6723  (simplify
6724   (cmp:c (bit_xor:c @0 @1) @0)
6725   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6727  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6728  (simplify
6729   (cmp:c (bit_and:c @0 @1) @0)
6730   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6731  (simplify
6732   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6733   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6734        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6735        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6736        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6737        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6738        && !wi::neg_p (wi::to_wide (@1)))
6739    (cmp (bit_and @0 (convert (bit_not @1)))
6740         { build_zero_cst (TREE_TYPE (@0)); })))
6742  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6743  (simplify
6744   (cmp:c (bit_ior:c @0 @1) @1)
6745   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6747  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6748  (simplify
6749   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6750   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6751    (cmp @0 (bit_xor @1 (convert @2)))))
6753  (simplify
6754   (cmp (nop_convert? @0) integer_zerop)
6755   (if (tree_expr_nonzero_p (@0))
6756    { constant_boolean_node (cmp == NE_EXPR, type); }))
6758  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6759  (simplify
6760   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6761   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6763 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6764    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6765    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6766    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6767 (for cmp (eq ne)
6768      ncmp (ge lt)
6769  (for sgncmp (ge lt)
6770   (simplify
6771    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6772    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6773         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6774         && types_match (@0, @1))
6775     (ncmp (bit_xor @0 @1) @2)))))
6776 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6777    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6778 (for cmp (eq ne)
6779      ncmp (lt ge)
6780  (simplify
6781   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6782    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6783         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6784         && types_match (@0, @1))
6785     (ncmp (bit_xor @0 @1) @2))))
6787 /* If we have (A & C) == C where C is a power of 2, convert this into
6788    (A & C) != 0.  Similarly for NE_EXPR.  */
6789 (for cmp (eq ne)
6790      icmp (ne eq)
6791  (simplify
6792   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6793   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6795 #if GIMPLE
6796 /* From fold_binary_op_with_conditional_arg handle the case of
6797    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6798    compares simplify.  */
6799 (for cmp (simple_comparison)
6800  (simplify
6801   (cmp:c (cond @0 @1 @2) @3)
6802   /* Do not move possibly trapping operations into the conditional as this
6803      pessimizes code and causes gimplification issues when applied late.  */
6804   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6805        || !operation_could_trap_p (cmp, true, false, @3))
6806    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6807 #endif
6809 (for cmp (ge lt)
6810 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6811 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6812  (simplify
6813   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6814    (if (INTEGRAL_TYPE_P (type)
6815         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6816         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6817         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6818     (with
6819      {
6820        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6821      }
6822     (if (cmp == LT_EXPR)
6823      (bit_xor (convert (rshift @0 {shifter;})) @1)
6824      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6825 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6826 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6827  (simplify
6828   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6829    (if (INTEGRAL_TYPE_P (type)
6830         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6831         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6832         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6833     (with
6834      {
6835        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6836      }
6837     (if (cmp == GE_EXPR)
6838      (bit_xor (convert (rshift @0 {shifter;})) @1)
6839      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6841 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6842    convert this into a shift followed by ANDing with D.  */
6843 (simplify
6844  (cond
6845   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6846   INTEGER_CST@2 integer_zerop)
6847  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6848   (with {
6849      int shift = (wi::exact_log2 (wi::to_wide (@2))
6850                   - wi::exact_log2 (wi::to_wide (@1)));
6851    }
6852    (if (shift > 0)
6853     (bit_and
6854      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6855     (bit_and
6856      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6857      @2)))))
6859 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6860    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6861 (for cmp (eq ne)
6862      ncmp (ge lt)
6863  (simplify
6864   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6865   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6866        && type_has_mode_precision_p (TREE_TYPE (@0))
6867        && element_precision (@2) >= element_precision (@0)
6868        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6869    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6870     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6872 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6873    this into a right shift or sign extension followed by ANDing with C.  */
6874 (simplify
6875  (cond
6876   (lt @0 integer_zerop)
6877   INTEGER_CST@1 integer_zerop)
6878  (if (integer_pow2p (@1)
6879       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6880   (with {
6881     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6882    }
6883    (if (shift >= 0)
6884     (bit_and
6885      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6886      @1)
6887     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6888        sign extension followed by AND with C will achieve the effect.  */
6889     (bit_and (convert @0) @1)))))
6891 /* When the addresses are not directly of decls compare base and offset.
6892    This implements some remaining parts of fold_comparison address
6893    comparisons but still no complete part of it.  Still it is good
6894    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6895 (for cmp (simple_comparison)
6896  (simplify
6897   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6898   (with
6899    {
6900      poly_int64 off0, off1;
6901      tree base0, base1;
6902      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6903                                   off0, off1, GENERIC);
6904    }
6905    (if (equal == 1)
6906     (switch
6907      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6908       { constant_boolean_node (known_eq (off0, off1), type); })
6909      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6910       { constant_boolean_node (known_ne (off0, off1), type); })
6911      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6912       { constant_boolean_node (known_lt (off0, off1), type); })
6913      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6914       { constant_boolean_node (known_le (off0, off1), type); })
6915      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6916       { constant_boolean_node (known_ge (off0, off1), type); })
6917      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6918       { constant_boolean_node (known_gt (off0, off1), type); }))
6919     (if (equal == 0)
6920      (switch
6921       (if (cmp == EQ_EXPR)
6922        { constant_boolean_node (false, type); })
6923       (if (cmp == NE_EXPR)
6924        { constant_boolean_node (true, type); })))))))
6926 #if GIMPLE
6927 /* a?~t:t -> (-(a))^t */
6928 (simplify
6929  (cond @0 @1 @2)
6930  (with { bool wascmp; }
6931   (if (INTEGRAL_TYPE_P (type)
6932        && bitwise_inverted_equal_p (@1, @2, wascmp)
6933        && (!wascmp || TYPE_PRECISION (type) == 1))
6934    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6935         || TYPE_PRECISION (type) == 1)
6936     (bit_xor (convert:type @0) @2)
6937     (bit_xor (negate (convert:type @0)) @2)))))
6938 #endif
6940 /* Simplify pointer equality compares using PTA.  */
6941 (for neeq (ne eq)
6942  (simplify
6943   (neeq @0 @1)
6944   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6945        && ptrs_compare_unequal (@0, @1))
6946    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6948 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6949    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6950    Disable the transform if either operand is pointer to function.
6951    This broke pr22051-2.c for arm where function pointer
6952    canonicalizaion is not wanted.  */
6954 (for cmp (ne eq)
6955  (simplify
6956   (cmp (convert @0) INTEGER_CST@1)
6957   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6958          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6959          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6960          /* Don't perform this optimization in GENERIC if @0 has reference
6961             type when sanitizing.  See PR101210.  */
6962          && !(GENERIC
6963               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6964               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6965         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6966             && POINTER_TYPE_P (TREE_TYPE (@1))
6967             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6968        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6969    (cmp @0 (convert @1)))))
6971 /* Non-equality compare simplifications from fold_binary  */
6972 (for cmp (lt gt le ge)
6973  /* Comparisons with the highest or lowest possible integer of
6974     the specified precision will have known values.  */
6975  (simplify
6976   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6977   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6978         || POINTER_TYPE_P (TREE_TYPE (@1))
6979         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6980        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6981    (with
6982     {
6983       tree cst = uniform_integer_cst_p (@1);
6984       tree arg1_type = TREE_TYPE (cst);
6985       unsigned int prec = TYPE_PRECISION (arg1_type);
6986       wide_int max = wi::max_value (arg1_type);
6987       wide_int signed_max = wi::max_value (prec, SIGNED);
6988       wide_int min = wi::min_value (arg1_type);
6989     }
6990     (switch
6991      (if (wi::to_wide (cst) == max)
6992       (switch
6993        (if (cmp == GT_EXPR)
6994         { constant_boolean_node (false, type); })
6995        (if (cmp == GE_EXPR)
6996         (eq @2 @1))
6997        (if (cmp == LE_EXPR)
6998         { constant_boolean_node (true, type); })
6999        (if (cmp == LT_EXPR)
7000         (ne @2 @1))))
7001      (if (wi::to_wide (cst) == min)
7002       (switch
7003        (if (cmp == LT_EXPR)
7004         { constant_boolean_node (false, type); })
7005        (if (cmp == LE_EXPR)
7006         (eq @2 @1))
7007        (if (cmp == GE_EXPR)
7008         { constant_boolean_node (true, type); })
7009        (if (cmp == GT_EXPR)
7010         (ne @2 @1))))
7011      (if (wi::to_wide (cst) == max - 1)
7012       (switch
7013        (if (cmp == GT_EXPR)
7014         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7015                                     wide_int_to_tree (TREE_TYPE (cst),
7016                                                       wi::to_wide (cst)
7017                                                       + 1)); }))
7018        (if (cmp == LE_EXPR)
7019         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7020                                     wide_int_to_tree (TREE_TYPE (cst),
7021                                                       wi::to_wide (cst)
7022                                                       + 1)); }))))
7023      (if (wi::to_wide (cst) == min + 1)
7024       (switch
7025        (if (cmp == GE_EXPR)
7026         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7027                                     wide_int_to_tree (TREE_TYPE (cst),
7028                                                       wi::to_wide (cst)
7029                                                       - 1)); }))
7030        (if (cmp == LT_EXPR)
7031         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7032                                     wide_int_to_tree (TREE_TYPE (cst),
7033                                                       wi::to_wide (cst)
7034                                                       - 1)); }))))
7035      (if (wi::to_wide (cst) == signed_max
7036           && TYPE_UNSIGNED (arg1_type)
7037           && TYPE_MODE (arg1_type) != BLKmode
7038           /* We will flip the signedness of the comparison operator
7039              associated with the mode of @1, so the sign bit is
7040              specified by this mode.  Check that @1 is the signed
7041              max associated with this sign bit.  */
7042           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7043           /* signed_type does not work on pointer types.  */
7044           && INTEGRAL_TYPE_P (arg1_type))
7045       /* The following case also applies to X < signed_max+1
7046          and X >= signed_max+1 because previous transformations.  */
7047       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7048        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7049         (switch
7050          (if (cst == @1 && cmp == LE_EXPR)
7051           (ge (convert:st @0) { build_zero_cst (st); }))
7052          (if (cst == @1 && cmp == GT_EXPR)
7053           (lt (convert:st @0) { build_zero_cst (st); }))
7054          (if (cmp == LE_EXPR)
7055           (ge (view_convert:st @0) { build_zero_cst (st); }))
7056          (if (cmp == GT_EXPR)
7057           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7059 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7060 (simplify
7061  (lt:c @0 (convert (ne @0 integer_zerop)))
7062  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7063   { constant_boolean_node (false, type); }))
7065 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7066 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7067 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7068 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7069 (for outer (ne eq)
7070  (for inner (ne eq)
7071   (simplify
7072    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7073    (with {
7074      bool cst1 = integer_onep (@1);
7075      bool cst0 = integer_zerop (@1);
7076      bool innereq = inner == EQ_EXPR;
7077      bool outereq = outer == EQ_EXPR;
7078     }
7079     (switch
7080      (if (innereq ? cst0 : cst1)
7081       { constant_boolean_node (!outereq, type); })
7082      (if (innereq ? cst1 : cst0)
7083       (with {
7084         tree utype = unsigned_type_for (TREE_TYPE (@0));
7085         tree ucst1 = build_one_cst (utype);
7086        }
7087        (if (!outereq)
7088         (gt (convert:utype @0) { ucst1; })
7089         (le (convert:utype @0) { ucst1; })
7090        )
7091       )
7092      )
7093      (with {
7094        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7095       }
7096       (if (outereq)
7097        (eq @0 { value; })
7098        (ne @0 { value; })
7099       )
7100      )
7101     )
7102    )
7103   )
7107 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7108  /* If the second operand is NaN, the result is constant.  */
7109  (simplify
7110   (cmp @0 REAL_CST@1)
7111   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7112        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7113    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7114                             ? false : true, type); })))
7116 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7117 (simplify
7118   (unordered @0 @1)
7119   (switch
7120     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7121         { constant_boolean_node (true, type); })
7122     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7123         { constant_boolean_node (false, type); })))
7125 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7126 (simplify
7127   (ordered @0 @1)
7128   (switch
7129     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7130         { constant_boolean_node (false, type); })
7131     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7132         { constant_boolean_node (true, type); })))
7134 /* bool_var != 0 becomes bool_var.  */
7135 (simplify
7136  (ne @0 integer_zerop)
7137  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7138       && types_match (type, TREE_TYPE (@0)))
7139   (non_lvalue @0)))
7140 /* bool_var == 1 becomes bool_var.  */
7141 (simplify
7142  (eq @0 integer_onep)
7143  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7144       && types_match (type, TREE_TYPE (@0)))
7145   (non_lvalue @0)))
7146 /* Do not handle
7147    bool_var == 0 becomes !bool_var or
7148    bool_var != 1 becomes !bool_var
7149    here because that only is good in assignment context as long
7150    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7151    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7152    clearly less optimal and which we'll transform again in forwprop.  */
7154 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7155    where ~Y + 1 == pow2 and Z = ~Y.  */
7156 (for cst (VECTOR_CST INTEGER_CST)
7157  (for cmp (eq ne)
7158       icmp (le gt)
7159   (simplify
7160    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7161     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7162      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7163       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7164                          ? optab_vector : optab_default;
7165               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7166        (if (target_supports_op_p (utype, icmp, optab)
7167             || (optimize_vectors_before_lowering_p ()
7168                 && (!target_supports_op_p (type, cmp, optab)
7169                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7170         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7171          (icmp @0 { csts; })
7172          (icmp (view_convert:utype @0) { csts; })))))))))
7174 /* When one argument is a constant, overflow detection can be simplified.
7175    Currently restricted to single use so as not to interfere too much with
7176    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7177    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7178 (for cmp (lt le ge gt)
7179      out (gt gt le le)
7180  (simplify
7181   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7182   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7183        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7184        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7185        && wi::to_wide (@1) != 0
7186        && single_use (@2))
7187    (with {
7188      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7189      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7190     }
7191     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7192                                 wi::max_value (prec, sign)
7193                                 - wi::to_wide (@1)); })))))
7195 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7196    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7197    expects the long form, so we restrict the transformation for now.  */
7198 (for cmp (gt le)
7199  (simplify
7200   (cmp:c (minus@2 @0 @1) @0)
7201   (if (single_use (@2)
7202        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7203        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7204    (cmp @1 @0))))
7206 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7207 (for cmp (ge lt)
7208  (simplify
7209   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7210    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7211         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7212     (cmp @1 @0))))
7214 /* Testing for overflow is unnecessary if we already know the result.  */
7215 /* A - B > A  */
7216 (for cmp (gt le)
7217      out (ne eq)
7218  (simplify
7219   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7220   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7221        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7222    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7223 /* A + B < A  */
7224 (for cmp (lt ge)
7225      out (ne eq)
7226  (simplify
7227   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7228   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7229        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7230    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7232 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7233    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7234 (for cmp (lt ge)
7235      out (ne eq)
7236  (simplify
7237   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7238   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7239    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7240     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7242 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7243    is at least twice as wide as type of A and B, simplify to
7244    __builtin_mul_overflow (A, B, <unused>).  */
7245 (for cmp (eq ne)
7246  (simplify
7247   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7248        integer_zerop)
7249   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7250        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7251        && TYPE_UNSIGNED (TREE_TYPE (@0))
7252        && (TYPE_PRECISION (TREE_TYPE (@3))
7253            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7254        && tree_fits_uhwi_p (@2)
7255        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7256        && types_match (@0, @1)
7257        && type_has_mode_precision_p (TREE_TYPE (@0))
7258        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7259            != CODE_FOR_nothing))
7260    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7261     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7263 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7264 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7265  (simplify
7266   (ovf (convert@2 @0) @1)
7267   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7268        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7269        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7270        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7271    (ovf @0 @1)))
7272  (simplify
7273   (ovf @1 (convert@2 @0))
7274   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7275        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7276        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7277        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7278    (ovf @1 @0))))
7280 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7281    are unsigned to x > (umax / cst).  Similarly for signed type, but
7282    in that case it needs to be outside of a range.  */
7283 (simplify
7284  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7285   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7286        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7287        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7288        && int_fits_type_p (@1, TREE_TYPE (@0)))
7289    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7290     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7291     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7292      (if (integer_minus_onep (@1))
7293       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7294       (with
7295        {
7296          tree div = fold_convert (TREE_TYPE (@0), @1);
7297          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7298                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7299          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7300                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7301          tree etype = range_check_type (TREE_TYPE (@0));
7302          if (etype)
7303            {
7304              if (wi::neg_p (wi::to_wide (div)))
7305                std::swap (lo, hi);
7306              lo = fold_convert (etype, lo);
7307              hi = fold_convert (etype, hi);
7308              hi = int_const_binop (MINUS_EXPR, hi, lo);
7309            }
7310        }
7311        (if (etype)
7312         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7314 /* Simplification of math builtins.  These rules must all be optimizations
7315    as well as IL simplifications.  If there is a possibility that the new
7316    form could be a pessimization, the rule should go in the canonicalization
7317    section that follows this one.
7319    Rules can generally go in this section if they satisfy one of
7320    the following:
7322    - the rule describes an identity
7324    - the rule replaces calls with something as simple as addition or
7325      multiplication
7327    - the rule contains unary calls only and simplifies the surrounding
7328      arithmetic.  (The idea here is to exclude non-unary calls in which
7329      one operand is constant and in which the call is known to be cheap
7330      when the operand has that value.)  */
7332 (if (flag_unsafe_math_optimizations)
7333  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7334  (simplify
7335   (mult (SQRT_ALL@1 @0) @1)
7336   (if (!tree_expr_maybe_signaling_nan_p (@0))
7337    @0))
7339  (for op (plus minus)
7340   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7341   (simplify
7342    (op (rdiv @0 @1)
7343        (rdiv @2 @1))
7344    (rdiv (op @0 @2) @1)))
7346  (for cmp (lt le gt ge)
7347       neg_cmp (gt ge lt le)
7348   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7349   (simplify
7350    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7351    (with
7352     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7353     (if (tem
7354          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7355               || (real_zerop (tem) && !real_zerop (@1))))
7356      (switch
7357       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7358        (cmp @0 { tem; }))
7359       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7360        (neg_cmp @0 { tem; })))))))
7362  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7363  (for root (SQRT CBRT)
7364   (simplify
7365    (mult (root:s @0) (root:s @1))
7366     (root (mult @0 @1))))
7368  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7369  (for exps (EXP EXP2 EXP10 POW10)
7370   (simplify
7371    (mult (exps:s @0) (exps:s @1))
7372     (exps (plus @0 @1))))
7374  /* Simplify a/root(b/c) into a*root(c/b).  */
7375  (for root (SQRT CBRT)
7376   (simplify
7377    (rdiv @0 (root:s (rdiv:s @1 @2)))
7378     (mult @0 (root (rdiv @2 @1)))))
7380  /* Simplify x/expN(y) into x*expN(-y).  */
7381  (for exps (EXP EXP2 EXP10 POW10)
7382   (simplify
7383    (rdiv @0 (exps:s @1))
7384     (mult @0 (exps (negate @1)))))
7386  (for logs (LOG LOG2 LOG10 LOG10)
7387       exps (EXP EXP2 EXP10 POW10)
7388   /* logN(expN(x)) -> x.  */
7389   (simplify
7390    (logs (exps @0))
7391    @0)
7392   /* expN(logN(x)) -> x.  */
7393   (simplify
7394    (exps (logs @0))
7395    @0))
7397  /* Optimize logN(func()) for various exponential functions.  We
7398     want to determine the value "x" and the power "exponent" in
7399     order to transform logN(x**exponent) into exponent*logN(x).  */
7400  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7401       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7402   (simplify
7403    (logs (exps @0))
7404    (if (SCALAR_FLOAT_TYPE_P (type))
7405     (with {
7406       tree x;
7407       switch (exps)
7408         {
7409         CASE_CFN_EXP:
7410           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7411           x = build_real_truncate (type, dconst_e ());
7412           break;
7413         CASE_CFN_EXP2:
7414           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7415           x = build_real (type, dconst2);
7416           break;
7417         CASE_CFN_EXP10:
7418         CASE_CFN_POW10:
7419           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7420           {
7421             REAL_VALUE_TYPE dconst10;
7422             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7423             x = build_real (type, dconst10);
7424           }
7425           break;
7426         default:
7427           gcc_unreachable ();
7428         }
7429       }
7430      (mult (logs { x; }) @0)))))
7432  (for logs (LOG LOG
7433             LOG2 LOG2
7434             LOG10 LOG10)
7435       exps (SQRT CBRT)
7436   (simplify
7437    (logs (exps @0))
7438    (if (SCALAR_FLOAT_TYPE_P (type))
7439     (with {
7440       tree x;
7441       switch (exps)
7442         {
7443         CASE_CFN_SQRT:
7444           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7445           x = build_real (type, dconsthalf);
7446           break;
7447         CASE_CFN_CBRT:
7448           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7449           x = build_real_truncate (type, dconst_third ());
7450           break;
7451         default:
7452           gcc_unreachable ();
7453         }
7454       }
7455      (mult { x; } (logs @0))))))
7457  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7458  (for logs (LOG LOG2 LOG10)
7459       pows (POW)
7460   (simplify
7461    (logs (pows @0 @1))
7462    (mult @1 (logs @0))))
7464  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7465     or if C is a positive power of 2,
7466     pow(C,x) -> exp2(log2(C)*x).  */
7467 #if GIMPLE
7468  (for pows (POW)
7469       exps (EXP)
7470       logs (LOG)
7471       exp2s (EXP2)
7472       log2s (LOG2)
7473   (simplify
7474    (pows REAL_CST@0 @1)
7475    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7476         && real_isfinite (TREE_REAL_CST_PTR (@0))
7477         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7478            the use_exp2 case until after vectorization.  It seems actually
7479            beneficial for all constants to postpone this until later,
7480            because exp(log(C)*x), while faster, will have worse precision
7481            and if x folds into a constant too, that is unnecessary
7482            pessimization.  */
7483         && canonicalize_math_after_vectorization_p ())
7484     (with {
7485        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7486        bool use_exp2 = false;
7487        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7488            && value->cl == rvc_normal)
7489          {
7490            REAL_VALUE_TYPE frac_rvt = *value;
7491            SET_REAL_EXP (&frac_rvt, 1);
7492            if (real_equal (&frac_rvt, &dconst1))
7493              use_exp2 = true;
7494          }
7495      }
7496      (if (!use_exp2)
7497       (if (optimize_pow_to_exp (@0, @1))
7498        (exps (mult (logs @0) @1)))
7499       (exp2s (mult (log2s @0) @1)))))))
7500 #endif
7502  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7503  (for pows (POW)
7504       exps (EXP EXP2 EXP10 POW10)
7505       logs (LOG LOG2 LOG10 LOG10)
7506   (simplify
7507    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7508    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7509         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7510     (exps (plus (mult (logs @0) @1) @2)))))
7512  (for sqrts (SQRT)
7513       cbrts (CBRT)
7514       pows (POW)
7515       exps (EXP EXP2 EXP10 POW10)
7516   /* sqrt(expN(x)) -> expN(x*0.5).  */
7517   (simplify
7518    (sqrts (exps @0))
7519    (exps (mult @0 { build_real (type, dconsthalf); })))
7520   /* cbrt(expN(x)) -> expN(x/3).  */
7521   (simplify
7522    (cbrts (exps @0))
7523    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7524   /* pow(expN(x), y) -> expN(x*y).  */
7525   (simplify
7526    (pows (exps @0) @1)
7527    (exps (mult @0 @1))))
7529  /* tan(atan(x)) -> x.  */
7530  (for tans (TAN)
7531       atans (ATAN)
7532   (simplify
7533    (tans (atans @0))
7534    @0)))
7536  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7537  (for sins (SIN)
7538       atans (ATAN)
7539       sqrts (SQRT)
7540       copysigns (COPYSIGN)
7541   (simplify
7542    (sins (atans:s @0))
7543    (with
7544      {
7545       REAL_VALUE_TYPE r_cst;
7546       build_sinatan_real (&r_cst, type);
7547       tree t_cst = build_real (type, r_cst);
7548       tree t_one = build_one_cst (type);
7549      }
7550     (if (SCALAR_FLOAT_TYPE_P (type))
7551      (cond (lt (abs @0) { t_cst; })
7552       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7553       (copysigns { t_one; } @0))))))
7555 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7556  (for coss (COS)
7557       atans (ATAN)
7558       sqrts (SQRT)
7559       copysigns (COPYSIGN)
7560   (simplify
7561    (coss (atans:s @0))
7562    (with
7563      {
7564       REAL_VALUE_TYPE r_cst;
7565       build_sinatan_real (&r_cst, type);
7566       tree t_cst = build_real (type, r_cst);
7567       tree t_one = build_one_cst (type);
7568       tree t_zero = build_zero_cst (type);
7569      }
7570     (if (SCALAR_FLOAT_TYPE_P (type))
7571      (cond (lt (abs @0) { t_cst; })
7572       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7573       (copysigns { t_zero; } @0))))))
7575  (if (!flag_errno_math)
7576   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7577   (for sinhs (SINH)
7578        atanhs (ATANH)
7579        sqrts (SQRT)
7580    (simplify
7581     (sinhs (atanhs:s @0))
7582     (with { tree t_one = build_one_cst (type); }
7583     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7585   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7586   (for coshs (COSH)
7587        atanhs (ATANH)
7588        sqrts (SQRT)
7589    (simplify
7590     (coshs (atanhs:s @0))
7591     (with { tree t_one = build_one_cst (type); }
7592     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7594 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7595 (simplify
7596  (CABS (complex:C @0 real_zerop@1))
7597  (abs @0))
7599 /* trunc(trunc(x)) -> trunc(x), etc.  */
7600 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7601  (simplify
7602   (fns (fns @0))
7603   (fns @0)))
7604 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7605 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7606  (simplify
7607   (fns integer_valued_real_p@0)
7608   @0))
7610 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7611 (simplify
7612  (HYPOT:c @0 real_zerop@1)
7613  (abs @0))
7615 /* pow(1,x) -> 1.  */
7616 (simplify
7617  (POW real_onep@0 @1)
7618  @0)
7620 (simplify
7621  /* copysign(x,x) -> x.  */
7622  (COPYSIGN_ALL @0 @0)
7623  @0)
7625 (simplify
7626  /* copysign(x,-x) -> -x.  */
7627  (COPYSIGN_ALL @0 (negate@1 @0))
7628  @1)
7630 (simplify
7631  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7632  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7633  (abs @0))
7635 (simplify
7636  /* fabs (copysign(x, y)) -> fabs (x).  */
7637  (abs (COPYSIGN_ALL @0 @1))
7638  (abs @0))
7640 (for scale (LDEXP SCALBN SCALBLN)
7641  /* ldexp(0, x) -> 0.  */
7642  (simplify
7643   (scale real_zerop@0 @1)
7644   @0)
7645  /* ldexp(x, 0) -> x.  */
7646  (simplify
7647   (scale @0 integer_zerop@1)
7648   @0)
7649  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7650  (simplify
7651   (scale REAL_CST@0 @1)
7652   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7653    @0)))
7655 /* Canonicalization of sequences of math builtins.  These rules represent
7656    IL simplifications but are not necessarily optimizations.
7658    The sincos pass is responsible for picking "optimal" implementations
7659    of math builtins, which may be more complicated and can sometimes go
7660    the other way, e.g. converting pow into a sequence of sqrts.
7661    We only want to do these canonicalizations before the pass has run.  */
7663 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7664  /* Simplify tan(x) * cos(x) -> sin(x). */
7665  (simplify
7666   (mult:c (TAN:s @0) (COS:s @0))
7667    (SIN @0))
7669  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7670  (simplify
7671   (mult:c @0 (POW:s @0 REAL_CST@1))
7672   (if (!TREE_OVERFLOW (@1))
7673    (POW @0 (plus @1 { build_one_cst (type); }))))
7675  /* Simplify sin(x) / cos(x) -> tan(x). */
7676  (simplify
7677   (rdiv (SIN:s @0) (COS:s @0))
7678    (TAN @0))
7680  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7681  (simplify
7682   (rdiv (SINH:s @0) (COSH:s @0))
7683    (TANH @0))
7685  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7686  (simplify
7687    (rdiv (TANH:s @0) (SINH:s @0))
7688    (rdiv {build_one_cst (type);} (COSH @0)))
7690  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7691  (simplify
7692   (rdiv (COS:s @0) (SIN:s @0))
7693    (rdiv { build_one_cst (type); } (TAN @0)))
7695  /* Simplify sin(x) / tan(x) -> cos(x). */
7696  (simplify
7697   (rdiv (SIN:s @0) (TAN:s @0))
7698   (if (! HONOR_NANS (@0)
7699        && ! HONOR_INFINITIES (@0))
7700    (COS @0)))
7702  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7703  (simplify
7704   (rdiv (TAN:s @0) (SIN:s @0))
7705   (if (! HONOR_NANS (@0)
7706        && ! HONOR_INFINITIES (@0))
7707    (rdiv { build_one_cst (type); } (COS @0))))
7709  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7710  (simplify
7711   (mult (POW:s @0 @1) (POW:s @0 @2))
7712    (POW @0 (plus @1 @2)))
7714  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7715  (simplify
7716   (mult (POW:s @0 @1) (POW:s @2 @1))
7717    (POW (mult @0 @2) @1))
7719  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7720  (simplify
7721   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7722    (POWI (mult @0 @2) @1))
7724  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7725  (simplify
7726   (rdiv (POW:s @0 REAL_CST@1) @0)
7727   (if (!TREE_OVERFLOW (@1))
7728    (POW @0 (minus @1 { build_one_cst (type); }))))
7730  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7731  (simplify
7732   (rdiv @0 (POW:s @1 @2))
7733    (mult @0 (POW @1 (negate @2))))
7735  (for sqrts (SQRT)
7736       cbrts (CBRT)
7737       pows (POW)
7738   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7739   (simplify
7740    (sqrts (sqrts @0))
7741    (pows @0 { build_real (type, dconst_quarter ()); }))
7742   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7743   (simplify
7744    (sqrts (cbrts @0))
7745    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7746   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7747   (simplify
7748    (cbrts (sqrts @0))
7749    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7750   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7751   (simplify
7752    (cbrts (cbrts tree_expr_nonnegative_p@0))
7753    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7754   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7755   (simplify
7756    (sqrts (pows @0 @1))
7757    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7758   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7759   (simplify
7760    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7761    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7762   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7763   (simplify
7764    (pows (sqrts @0) @1)
7765    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7766   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7767   (simplify
7768    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7769    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7770   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7771   (simplify
7772    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7773    (pows @0 (mult @1 @2))))
7775  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7776  (simplify
7777   (CABS (complex @0 @0))
7778   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7780  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7781  (simplify
7782   (HYPOT @0 @0)
7783   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7785  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7786  (for cexps (CEXP)
7787       exps (EXP)
7788       cexpis (CEXPI)
7789   (simplify
7790    (cexps compositional_complex@0)
7791    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7792     (complex
7793      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7794      (mult @1 (imagpart @2)))))))
7796 (if (canonicalize_math_p ())
7797  /* floor(x) -> trunc(x) if x is nonnegative.  */
7798  (for floors (FLOOR_ALL)
7799       truncs (TRUNC_ALL)
7800   (simplify
7801    (floors tree_expr_nonnegative_p@0)
7802    (truncs @0))))
7804 (match double_value_p
7805  @0
7806  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7807 (for froms (BUILT_IN_TRUNCL
7808             BUILT_IN_FLOORL
7809             BUILT_IN_CEILL
7810             BUILT_IN_ROUNDL
7811             BUILT_IN_NEARBYINTL
7812             BUILT_IN_RINTL)
7813      tos (BUILT_IN_TRUNC
7814           BUILT_IN_FLOOR
7815           BUILT_IN_CEIL
7816           BUILT_IN_ROUND
7817           BUILT_IN_NEARBYINT
7818           BUILT_IN_RINT)
7819  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7820  (if (optimize && canonicalize_math_p ())
7821   (simplify
7822    (froms (convert double_value_p@0))
7823    (convert (tos @0)))))
7825 (match float_value_p
7826  @0
7827  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7828 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7829             BUILT_IN_FLOORL BUILT_IN_FLOOR
7830             BUILT_IN_CEILL BUILT_IN_CEIL
7831             BUILT_IN_ROUNDL BUILT_IN_ROUND
7832             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7833             BUILT_IN_RINTL BUILT_IN_RINT)
7834      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7835           BUILT_IN_FLOORF BUILT_IN_FLOORF
7836           BUILT_IN_CEILF BUILT_IN_CEILF
7837           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7838           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7839           BUILT_IN_RINTF BUILT_IN_RINTF)
7840  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7841     if x is a float.  */
7842  (if (optimize && canonicalize_math_p ()
7843       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7844   (simplify
7845    (froms (convert float_value_p@0))
7846    (convert (tos @0)))))
7848 #if GIMPLE
7849 (match float16_value_p
7850  @0
7851  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7852 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7853             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7854             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7855             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7856             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7857             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7858             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7859             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7860      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7861           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7862           IFN_CEIL IFN_CEIL IFN_CEIL
7863           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7864           IFN_ROUND IFN_ROUND IFN_ROUND
7865           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7866           IFN_RINT IFN_RINT IFN_RINT
7867           IFN_SQRT IFN_SQRT IFN_SQRT)
7868  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7869     if x is a _Float16.  */
7870  (simplify
7871    (convert (froms (convert float16_value_p@0)))
7872      (if (optimize
7873           && types_match (type, TREE_TYPE (@0))
7874           && direct_internal_fn_supported_p (as_internal_fn (tos),
7875                                              type, OPTIMIZE_FOR_BOTH))
7876        (tos @0))))
7878 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7879    x,y is float value, similar for _Float16/double.  */
7880 (for copysigns (COPYSIGN_ALL)
7881  (simplify
7882   (convert (copysigns (convert@2 @0) (convert @1)))
7883    (if (optimize
7884        && !HONOR_SNANS (@2)
7885        && types_match (type, TREE_TYPE (@0))
7886        && types_match (type, TREE_TYPE (@1))
7887        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7888        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7889                                           type, OPTIMIZE_FOR_BOTH))
7890     (IFN_COPYSIGN @0 @1))))
7892 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7893      tos (IFN_FMA IFN_FMA IFN_FMA)
7894  (simplify
7895   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7896    (if (flag_unsafe_math_optimizations
7897        && optimize
7898        && FLOAT_TYPE_P (type)
7899        && FLOAT_TYPE_P (TREE_TYPE (@3))
7900        && types_match (type, TREE_TYPE (@0))
7901        && types_match (type, TREE_TYPE (@1))
7902        && types_match (type, TREE_TYPE (@2))
7903        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7904        && direct_internal_fn_supported_p (as_internal_fn (tos),
7905                                           type, OPTIMIZE_FOR_BOTH))
7906     (tos @0 @1 @2))))
7908 (for maxmin (max min)
7909  (simplify
7910   (convert (maxmin (convert@2 @0) (convert @1)))
7911    (if (optimize
7912        && FLOAT_TYPE_P (type)
7913        && FLOAT_TYPE_P (TREE_TYPE (@2))
7914        && types_match (type, TREE_TYPE (@0))
7915        && types_match (type, TREE_TYPE (@1))
7916        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7917     (maxmin @0 @1))))
7918 #endif
7920 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7921      tos (XFLOOR XCEIL XROUND XRINT)
7922  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7923  (if (optimize && canonicalize_math_p ())
7924   (simplify
7925    (froms (convert double_value_p@0))
7926    (tos @0))))
7928 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7929             XFLOOR XCEIL XROUND XRINT)
7930      tos (XFLOORF XCEILF XROUNDF XRINTF)
7931  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7932     if x is a float.  */
7933  (if (optimize && canonicalize_math_p ())
7934   (simplify
7935    (froms (convert float_value_p@0))
7936    (tos @0))))
7938 (if (canonicalize_math_p ())
7939  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7940  (for floors (IFLOOR LFLOOR LLFLOOR)
7941   (simplify
7942    (floors tree_expr_nonnegative_p@0)
7943    (fix_trunc @0))))
7945 (if (canonicalize_math_p ())
7946  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7947  (for fns (IFLOOR LFLOOR LLFLOOR
7948            ICEIL LCEIL LLCEIL
7949            IROUND LROUND LLROUND)
7950   (simplify
7951    (fns integer_valued_real_p@0)
7952    (fix_trunc @0)))
7953  (if (!flag_errno_math)
7954   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7955   (for rints (IRINT LRINT LLRINT)
7956    (simplify
7957     (rints integer_valued_real_p@0)
7958     (fix_trunc @0)))))
7960 (if (canonicalize_math_p ())
7961  (for ifn (IFLOOR ICEIL IROUND IRINT)
7962       lfn (LFLOOR LCEIL LROUND LRINT)
7963       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7964   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7965      sizeof (int) == sizeof (long).  */
7966   (if (TYPE_PRECISION (integer_type_node)
7967        == TYPE_PRECISION (long_integer_type_node))
7968    (simplify
7969     (ifn @0)
7970     (lfn:long_integer_type_node @0)))
7971   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7972      sizeof (long long) == sizeof (long).  */
7973   (if (TYPE_PRECISION (long_long_integer_type_node)
7974        == TYPE_PRECISION (long_integer_type_node))
7975    (simplify
7976     (llfn @0)
7977     (lfn:long_integer_type_node @0)))))
7979 /* cproj(x) -> x if we're ignoring infinities.  */
7980 (simplify
7981  (CPROJ @0)
7982  (if (!HONOR_INFINITIES (type))
7983    @0))
7985 /* If the real part is inf and the imag part is known to be
7986    nonnegative, return (inf + 0i).  */
7987 (simplify
7988  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7989  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7990   { build_complex_inf (type, false); }))
7992 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7993 (simplify
7994  (CPROJ (complex @0 REAL_CST@1))
7995  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7996   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7998 (for pows (POW)
7999      sqrts (SQRT)
8000      cbrts (CBRT)
8001  (simplify
8002   (pows @0 REAL_CST@1)
8003   (with {
8004     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8005     REAL_VALUE_TYPE tmp;
8006    }
8007    (switch
8008     /* pow(x,0) -> 1.  */
8009     (if (real_equal (value, &dconst0))
8010      { build_real (type, dconst1); })
8011     /* pow(x,1) -> x.  */
8012     (if (real_equal (value, &dconst1))
8013      @0)
8014     /* pow(x,-1) -> 1/x.  */
8015     (if (real_equal (value, &dconstm1))
8016      (rdiv { build_real (type, dconst1); } @0))
8017     /* pow(x,0.5) -> sqrt(x).  */
8018     (if (flag_unsafe_math_optimizations
8019          && canonicalize_math_p ()
8020          && real_equal (value, &dconsthalf))
8021      (sqrts @0))
8022     /* pow(x,1/3) -> cbrt(x).  */
8023     (if (flag_unsafe_math_optimizations
8024          && canonicalize_math_p ()
8025          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8026              real_equal (value, &tmp)))
8027      (cbrts @0))))))
8029 /* powi(1,x) -> 1.  */
8030 (simplify
8031  (POWI real_onep@0 @1)
8032  @0)
8034 (simplify
8035  (POWI @0 INTEGER_CST@1)
8036  (switch
8037   /* powi(x,0) -> 1.  */
8038   (if (wi::to_wide (@1) == 0)
8039    { build_real (type, dconst1); })
8040   /* powi(x,1) -> x.  */
8041   (if (wi::to_wide (@1) == 1)
8042    @0)
8043   /* powi(x,-1) -> 1/x.  */
8044   (if (wi::to_wide (@1) == -1)
8045    (rdiv { build_real (type, dconst1); } @0))))
8047 /* Narrowing of arithmetic and logical operations.
8049    These are conceptually similar to the transformations performed for
8050    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8051    term we want to move all that code out of the front-ends into here.  */
8053 /* Convert (outertype)((innertype0)a+(innertype1)b)
8054    into ((newtype)a+(newtype)b) where newtype
8055    is the widest mode from all of these.  */
8056 (for op (plus minus mult rdiv)
8057  (simplify
8058    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8059    /* If we have a narrowing conversion of an arithmetic operation where
8060       both operands are widening conversions from the same type as the outer
8061       narrowing conversion.  Then convert the innermost operands to a
8062       suitable unsigned type (to avoid introducing undefined behavior),
8063       perform the operation and convert the result to the desired type.  */
8064    (if (INTEGRAL_TYPE_P (type)
8065         && op != MULT_EXPR
8066         && op != RDIV_EXPR
8067         /* We check for type compatibility between @0 and @1 below,
8068            so there's no need to check that @2/@4 are integral types.  */
8069         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8070         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8071         /* The precision of the type of each operand must match the
8072            precision of the mode of each operand, similarly for the
8073            result.  */
8074         && type_has_mode_precision_p (TREE_TYPE (@1))
8075         && type_has_mode_precision_p (TREE_TYPE (@2))
8076         && type_has_mode_precision_p (type)
8077         /* The inner conversion must be a widening conversion.  */
8078         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8079         && types_match (@1, type)
8080         && (types_match (@1, @2)
8081             /* Or the second operand is const integer or converted const
8082                integer from valueize.  */
8083             || poly_int_tree_p (@4)))
8084      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8085        (op @1 (convert @2))
8086        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8087         (convert (op (convert:utype @1)
8088                      (convert:utype @2)))))
8089      (if (FLOAT_TYPE_P (type)
8090           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8091                == DECIMAL_FLOAT_TYPE_P (type))
8092       (with { tree arg0 = strip_float_extensions (@1);
8093               tree arg1 = strip_float_extensions (@2);
8094               tree itype = TREE_TYPE (@0);
8095               tree ty1 = TREE_TYPE (arg0);
8096               tree ty2 = TREE_TYPE (arg1);
8097               enum tree_code code = TREE_CODE (itype); }
8098         (if (FLOAT_TYPE_P (ty1)
8099              && FLOAT_TYPE_P (ty2))
8100          (with { tree newtype = type;
8101                  if (TYPE_MODE (ty1) == SDmode
8102                      || TYPE_MODE (ty2) == SDmode
8103                      || TYPE_MODE (type) == SDmode)
8104                    newtype = dfloat32_type_node;
8105                  if (TYPE_MODE (ty1) == DDmode
8106                      || TYPE_MODE (ty2) == DDmode
8107                      || TYPE_MODE (type) == DDmode)
8108                    newtype = dfloat64_type_node;
8109                  if (TYPE_MODE (ty1) == TDmode
8110                      || TYPE_MODE (ty2) == TDmode
8111                      || TYPE_MODE (type) == TDmode)
8112                    newtype = dfloat128_type_node; }
8113           (if ((newtype == dfloat32_type_node
8114                 || newtype == dfloat64_type_node
8115                 || newtype == dfloat128_type_node)
8116               && newtype == type
8117               && types_match (newtype, type))
8118             (op (convert:newtype @1) (convert:newtype @2))
8119             (with { if (element_precision (ty1) > element_precision (newtype))
8120                       newtype = ty1;
8121                     if (element_precision (ty2) > element_precision (newtype))
8122                       newtype = ty2; }
8123                /* Sometimes this transformation is safe (cannot
8124                   change results through affecting double rounding
8125                   cases) and sometimes it is not.  If NEWTYPE is
8126                   wider than TYPE, e.g. (float)((long double)double
8127                   + (long double)double) converted to
8128                   (float)(double + double), the transformation is
8129                   unsafe regardless of the details of the types
8130                   involved; double rounding can arise if the result
8131                   of NEWTYPE arithmetic is a NEWTYPE value half way
8132                   between two representable TYPE values but the
8133                   exact value is sufficiently different (in the
8134                   right direction) for this difference to be
8135                   visible in ITYPE arithmetic.  If NEWTYPE is the
8136                   same as TYPE, however, the transformation may be
8137                   safe depending on the types involved: it is safe
8138                   if the ITYPE has strictly more than twice as many
8139                   mantissa bits as TYPE, can represent infinities
8140                   and NaNs if the TYPE can, and has sufficient
8141                   exponent range for the product or ratio of two
8142                   values representable in the TYPE to be within the
8143                   range of normal values of ITYPE.  */
8144               (if (element_precision (newtype) < element_precision (itype)
8145                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8146                        || target_supports_op_p (newtype, op, optab_default))
8147                    && (flag_unsafe_math_optimizations
8148                        || (element_precision (newtype) == element_precision (type)
8149                            && real_can_shorten_arithmetic (element_mode (itype),
8150                                                            element_mode (type))
8151                            && !excess_precision_type (newtype)))
8152                    && !types_match (itype, newtype))
8153                  (convert:type (op (convert:newtype @1)
8154                                    (convert:newtype @2)))
8155          )))) )
8156    ))
8159 /* This is another case of narrowing, specifically when there's an outer
8160    BIT_AND_EXPR which masks off bits outside the type of the innermost
8161    operands.   Like the previous case we have to convert the operands
8162    to unsigned types to avoid introducing undefined behavior for the
8163    arithmetic operation.  */
8164 (for op (minus plus)
8165  (simplify
8166   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8167   (if (INTEGRAL_TYPE_P (type)
8168        /* We check for type compatibility between @0 and @1 below,
8169           so there's no need to check that @1/@3 are integral types.  */
8170        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8171        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8172        /* The precision of the type of each operand must match the
8173           precision of the mode of each operand, similarly for the
8174           result.  */
8175        && type_has_mode_precision_p (TREE_TYPE (@0))
8176        && type_has_mode_precision_p (TREE_TYPE (@1))
8177        && type_has_mode_precision_p (type)
8178        /* The inner conversion must be a widening conversion.  */
8179        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8180        && types_match (@0, @1)
8181        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8182            <= TYPE_PRECISION (TREE_TYPE (@0)))
8183        && (wi::to_wide (@4)
8184            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8185                        true, TYPE_PRECISION (type))) == 0)
8186    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8187     (with { tree ntype = TREE_TYPE (@0); }
8188      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8189     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8190      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8191                (convert:utype @4))))))))
8193 /* Transform (@0 < @1 and @0 < @2) to use min,
8194    (@0 > @1 and @0 > @2) to use max */
8195 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8196      op    (lt      le      gt      ge      lt      le      gt      ge     )
8197      ext   (min     min     max     max     max     max     min     min    )
8198  (simplify
8199   (logic (op:cs @0 @1) (op:cs @0 @2))
8200   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8201        && TREE_CODE (@0) != INTEGER_CST)
8202    (op @0 (ext @1 @2)))))
8204 /* Max<bool0, bool1> -> bool0 | bool1
8205    Min<bool0, bool1> -> bool0 & bool1 */
8206 (for op    (max     min)
8207      logic (bit_ior bit_and)
8208  (simplify
8209   (op zero_one_valued_p@0 zero_one_valued_p@1)
8210   (logic @0 @1)))
8212 /* signbit(x) != 0 ? -x : x -> abs(x)
8213    signbit(x) == 0 ? -x : x -> -abs(x) */
8214 (for sign (SIGNBIT)
8215  (for neeq (ne eq)
8216   (simplify
8217    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8218     (if (neeq == NE_EXPR)
8219      (abs @0)
8220      (negate (abs @0))))))
8222 (simplify
8223  /* signbit(x) -> 0 if x is nonnegative.  */
8224  (SIGNBIT tree_expr_nonnegative_p@0)
8225  { integer_zero_node; })
8227 (simplify
8228  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8229  (SIGNBIT @0)
8230  (if (!HONOR_SIGNED_ZEROS (@0))
8231   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8233 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8234 (for cmp (eq ne)
8235  (for op (plus minus)
8236       rop (minus plus)
8237   (simplify
8238    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8239    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8240         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8241         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8242         && !TYPE_SATURATING (TREE_TYPE (@0)))
8243     (with { tree res = int_const_binop (rop, @2, @1); }
8244      (if (TREE_OVERFLOW (res)
8245           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8246       { constant_boolean_node (cmp == NE_EXPR, type); }
8247       (if (single_use (@3))
8248        (cmp @0 { TREE_OVERFLOW (res)
8249                  ? drop_tree_overflow (res) : res; }))))))))
8250 (for cmp (lt le gt ge)
8251  (for op (plus minus)
8252       rop (minus plus)
8253   (simplify
8254    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8255    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8256         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8257     (with { tree res = int_const_binop (rop, @2, @1); }
8258      (if (TREE_OVERFLOW (res))
8259       {
8260         fold_overflow_warning (("assuming signed overflow does not occur "
8261                                 "when simplifying conditional to constant"),
8262                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8263         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8264         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8265         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8266                                   TYPE_SIGN (TREE_TYPE (@1)))
8267                         != (op == MINUS_EXPR);
8268         constant_boolean_node (less == ovf_high, type);
8269       }
8270       (if (single_use (@3))
8271        (with
8272         {
8273           fold_overflow_warning (("assuming signed overflow does not occur "
8274                                   "when changing X +- C1 cmp C2 to "
8275                                   "X cmp C2 -+ C1"),
8276                                  WARN_STRICT_OVERFLOW_COMPARISON);
8277         }
8278         (cmp @0 { res; })))))))))
8280 /* Canonicalizations of BIT_FIELD_REFs.  */
8282 (simplify
8283  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8284  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8286 (simplify
8287  (BIT_FIELD_REF (view_convert @0) @1 @2)
8288  (BIT_FIELD_REF @0 @1 @2))
8290 (simplify
8291  (BIT_FIELD_REF @0 @1 integer_zerop)
8292  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8293   (view_convert @0)))
8295 (simplify
8296  (BIT_FIELD_REF @0 @1 @2)
8297  (switch
8298   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8299        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8300    (switch
8301     (if (integer_zerop (@2))
8302      (view_convert (realpart @0)))
8303     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8304      (view_convert (imagpart @0)))))
8305   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8306        && INTEGRAL_TYPE_P (type)
8307        /* On GIMPLE this should only apply to register arguments.  */
8308        && (! GIMPLE || is_gimple_reg (@0))
8309        /* A bit-field-ref that referenced the full argument can be stripped.  */
8310        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8311             && integer_zerop (@2))
8312            /* Low-parts can be reduced to integral conversions.
8313               ???  The following doesn't work for PDP endian.  */
8314            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8315                /* But only do this after vectorization.  */
8316                && canonicalize_math_after_vectorization_p ()
8317                /* Don't even think about BITS_BIG_ENDIAN.  */
8318                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8319                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8320                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8321                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8322                                             - TYPE_PRECISION (type))
8323                                          : 0)) == 0)))
8324    (convert @0))))
8326 /* Simplify vector extracts.  */
8328 (simplify
8329  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8330  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8331       && tree_fits_uhwi_p (TYPE_SIZE (type))
8332       && ((tree_to_uhwi (TYPE_SIZE (type))
8333            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8334           || (VECTOR_TYPE_P (type)
8335               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8336                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8337   (with
8338    {
8339      tree ctor = (TREE_CODE (@0) == SSA_NAME
8340                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8341      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8342      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8343      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8344      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8345    }
8346    (if (n != 0
8347         && (idx % width) == 0
8348         && (n % width) == 0
8349         && known_le ((idx + n) / width,
8350                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8351     (with
8352      {
8353        idx = idx / width;
8354        n = n / width;
8355        /* Constructor elements can be subvectors.  */
8356        poly_uint64 k = 1;
8357        if (CONSTRUCTOR_NELTS (ctor) != 0)
8358          {
8359            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8360            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8361              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8362          }
8363        unsigned HOST_WIDE_INT elt, count, const_k;
8364      }
8365      (switch
8366       /* We keep an exact subset of the constructor elements.  */
8367       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8368        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8369         { build_zero_cst (type); }
8370         (if (count == 1)
8371          (if (elt < CONSTRUCTOR_NELTS (ctor))
8372           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8373           { build_zero_cst (type); })
8374          /* We don't want to emit new CTORs unless the old one goes away.
8375             ???  Eventually allow this if the CTOR ends up constant or
8376             uniform.  */
8377          (if (single_use (@0))
8378           (with
8379             {
8380               vec<constructor_elt, va_gc> *vals;
8381               vec_alloc (vals, count);
8382               bool constant_p = true;
8383               tree res;
8384               for (unsigned i = 0;
8385                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8386                 {
8387                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8388                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8389                   if (!CONSTANT_CLASS_P (e))
8390                     constant_p = false;
8391                 }
8392               tree evtype = (types_match (TREE_TYPE (type),
8393                                           TREE_TYPE (TREE_TYPE (ctor)))
8394                              ? type
8395                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8396                                                   count * k));
8397               /* We used to build a CTOR in the non-constant case here
8398                  but that's not a GIMPLE value.  We'd have to expose this
8399                  operation somehow so the code generation can properly
8400                  split it out to a separate stmt.  */
8401               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8402                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8403             }
8404             (if (res)
8405              (view_convert { res; })))))))
8406       /* The bitfield references a single constructor element.  */
8407       (if (k.is_constant (&const_k)
8408            && idx + n <= (idx / const_k + 1) * const_k)
8409        (switch
8410         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8411          { build_zero_cst (type); })
8412         (if (n == const_k)
8413          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8414         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8415                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8417 /* Simplify a bit extraction from a bit insertion for the cases with
8418    the inserted element fully covering the extraction or the insertion
8419    not touching the extraction.  */
8420 (simplify
8421  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8422  (with
8423   {
8424     unsigned HOST_WIDE_INT isize;
8425     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8426       isize = TYPE_PRECISION (TREE_TYPE (@1));
8427     else
8428       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8429   }
8430   (switch
8431    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8432          || type_has_mode_precision_p (TREE_TYPE (@1)))
8433         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8434         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8435                       wi::to_wide (@ipos) + isize))
8436     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8437                                                  wi::to_wide (@rpos)
8438                                                  - wi::to_wide (@ipos)); }))
8439    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8440         && compare_tree_int (@rsize, isize) == 0)
8441     (convert @1))
8442    (if (wi::geu_p (wi::to_wide (@ipos),
8443                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8444         || wi::geu_p (wi::to_wide (@rpos),
8445                       wi::to_wide (@ipos) + isize))
8446     (BIT_FIELD_REF @0 @rsize @rpos)))))
8448 /* Simplify vector inserts of other vector extracts to a permute.  */
8449 (simplify
8450  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8451  (if (VECTOR_TYPE_P (type)
8452       && types_match (@0, @1)
8453       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8454       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8455   (with
8456    {
8457      unsigned HOST_WIDE_INT elsz
8458        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8459      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8460      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8461      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8462      vec_perm_builder builder;
8463      builder.new_vector (nunits, nunits, 1);
8464      for (unsigned i = 0; i < nunits; ++i)
8465        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8466      vec_perm_indices sel (builder, 2, nunits);
8467    }
8468    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8469         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8470     (vec_perm @0 @1 { vec_perm_indices_to_tree
8471                         (build_vector_type (ssizetype, nunits), sel); })))))
8473 (if (canonicalize_math_after_vectorization_p ())
8474  (for fmas (FMA)
8475   (simplify
8476    (fmas:c (negate @0) @1 @2)
8477    (IFN_FNMA @0 @1 @2))
8478   (simplify
8479    (fmas @0 @1 (negate @2))
8480    (IFN_FMS @0 @1 @2))
8481   (simplify
8482    (fmas:c (negate @0) @1 (negate @2))
8483    (IFN_FNMS @0 @1 @2))
8484   (simplify
8485    (negate (fmas@3 @0 @1 @2))
8486    (if (single_use (@3))
8487     (IFN_FNMS @0 @1 @2))))
8489  (simplify
8490   (IFN_FMS:c (negate @0) @1 @2)
8491   (IFN_FNMS @0 @1 @2))
8492  (simplify
8493   (IFN_FMS @0 @1 (negate @2))
8494   (IFN_FMA @0 @1 @2))
8495  (simplify
8496   (IFN_FMS:c (negate @0) @1 (negate @2))
8497   (IFN_FNMA @0 @1 @2))
8498  (simplify
8499   (negate (IFN_FMS@3 @0 @1 @2))
8500    (if (single_use (@3))
8501     (IFN_FNMA @0 @1 @2)))
8503  (simplify
8504   (IFN_FNMA:c (negate @0) @1 @2)
8505   (IFN_FMA @0 @1 @2))
8506  (simplify
8507   (IFN_FNMA @0 @1 (negate @2))
8508   (IFN_FNMS @0 @1 @2))
8509  (simplify
8510   (IFN_FNMA:c (negate @0) @1 (negate @2))
8511   (IFN_FMS @0 @1 @2))
8512  (simplify
8513   (negate (IFN_FNMA@3 @0 @1 @2))
8514   (if (single_use (@3))
8515    (IFN_FMS @0 @1 @2)))
8517  (simplify
8518   (IFN_FNMS:c (negate @0) @1 @2)
8519   (IFN_FMS @0 @1 @2))
8520  (simplify
8521   (IFN_FNMS @0 @1 (negate @2))
8522   (IFN_FNMA @0 @1 @2))
8523  (simplify
8524   (IFN_FNMS:c (negate @0) @1 (negate @2))
8525   (IFN_FMA @0 @1 @2))
8526  (simplify
8527   (negate (IFN_FNMS@3 @0 @1 @2))
8528   (if (single_use (@3))
8529    (IFN_FMA @0 @1 @2))))
8531 /* CLZ simplifications.  */
8532 (for clz (CLZ)
8533  (for op (eq ne)
8534       cmp (lt ge)
8535   (simplify
8536    (op (clz:s@2 @0) INTEGER_CST@1)
8537    (if (integer_zerop (@1) && single_use (@2))
8538     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8539     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8540      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8541     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8542     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8543      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8544 (for op (eq ne)
8545      cmp (lt ge)
8546  (simplify
8547   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8548   (if (integer_zerop (@1) && single_use (@2))
8549    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8550    (with { tree type0 = TREE_TYPE (@0);
8551            tree stype = signed_type_for (TREE_TYPE (@0));
8552            /* Punt if clz(0) == 0.  */
8553            if (integer_zerop (@3))
8554              stype = NULL_TREE;
8555          }
8556     (if (stype)
8557      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8558    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8559    (with { bool ok = true;
8560            tree type0 = TREE_TYPE (@0);
8561            /* Punt if clz(0) == prec - 1.  */
8562            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8563              ok = false;
8564          }
8565     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8566      (op @0 { build_one_cst (type0); }))))))
8568 /* CTZ simplifications.  */
8569 (for ctz (CTZ)
8570  (for op (ge gt le lt)
8571       cmp (eq eq ne ne)
8572   (simplify
8573    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8574    (op (ctz:s @0) INTEGER_CST@1)
8575     (with { bool ok = true;
8576             HOST_WIDE_INT val = 0;
8577             if (!tree_fits_shwi_p (@1))
8578               ok = false;
8579             else
8580               {
8581                 val = tree_to_shwi (@1);
8582                 /* Canonicalize to >= or <.  */
8583                 if (op == GT_EXPR || op == LE_EXPR)
8584                   {
8585                     if (val == HOST_WIDE_INT_MAX)
8586                       ok = false;
8587                     else
8588                       val++;
8589                   }
8590               }
8591             tree type0 = TREE_TYPE (@0);
8592             int prec = TYPE_PRECISION (type0);
8593           }
8594      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8595       (if (val <= 0)
8596        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8597        (if (val >= prec)
8598         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8599         (cmp (bit_and @0 { wide_int_to_tree (type0,
8600                                              wi::mask (val, false, prec)); })
8601              { build_zero_cst (type0); })))))))
8602  (for op (eq ne)
8603   (simplify
8604    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8605    (op (ctz:s @0) INTEGER_CST@1)
8606     (with { tree type0 = TREE_TYPE (@0);
8607             int prec = TYPE_PRECISION (type0);
8608           }
8609      (if (prec <= MAX_FIXED_MODE_SIZE)
8610       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8611        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8612        (op (bit_and @0 { wide_int_to_tree (type0,
8613                                            wi::mask (tree_to_uhwi (@1) + 1,
8614                                                      false, prec)); })
8615            { wide_int_to_tree (type0,
8616                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8617                                                  false, prec)); })))))))
8618 (for op (ge gt le lt)
8619      cmp (eq eq ne ne)
8620  (simplify
8621   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8622   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8623    (with { bool ok = true;
8624            HOST_WIDE_INT val = 0;
8625            if (!tree_fits_shwi_p (@1))
8626              ok = false;
8627            else
8628              {
8629                val = tree_to_shwi (@1);
8630                /* Canonicalize to >= or <.  */
8631                if (op == GT_EXPR || op == LE_EXPR)
8632                  {
8633                    if (val == HOST_WIDE_INT_MAX)
8634                      ok = false;
8635                    else
8636                      val++;
8637                  }
8638              }
8639            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8640            tree type0 = TREE_TYPE (@0);
8641            int prec = TYPE_PRECISION (type0);
8642            if (prec > MAX_FIXED_MODE_SIZE)
8643              ok = false;
8644           }
8645      (if (val <= 0)
8646       (if (ok && zero_val >= val)
8647        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8648       (if (val >= prec)
8649        (if (ok && zero_val < val)
8650         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8651        (if (ok && (zero_val < 0 || zero_val >= prec))
8652         (cmp (bit_and @0 { wide_int_to_tree (type0,
8653                                              wi::mask (val, false, prec)); })
8654              { build_zero_cst (type0); })))))))
8655 (for op (eq ne)
8656  (simplify
8657   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8658   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8659    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8660            tree type0 = TREE_TYPE (@0);
8661            int prec = TYPE_PRECISION (type0);
8662          }
8663     (if (prec <= MAX_FIXED_MODE_SIZE)
8664      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8665       (if (zero_val != wi::to_widest (@1))
8666        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8667       (if (zero_val < 0 || zero_val >= prec)
8668        (op (bit_and @0 { wide_int_to_tree (type0,
8669                                            wi::mask (tree_to_uhwi (@1) + 1,
8670                                                      false, prec)); })
8671            { wide_int_to_tree (type0,
8672                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8673                                                  false, prec)); })))))))
8675 /* POPCOUNT simplifications.  */
8676 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8677 (simplify
8678   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8679   (if (INTEGRAL_TYPE_P (type)
8680        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8681     (POPCOUNT (bit_ior @0 @1))))
8683 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8684 (for popcount (POPCOUNT)
8685   (for cmp (le eq ne gt)
8686        rep (eq eq ne ne)
8687     (simplify
8688       (cmp (popcount @0) integer_zerop)
8689       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8691 /* popcount(bswap(x)) is popcount(x).  */
8692 (for popcount (POPCOUNT)
8693   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8694               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8695     (simplify
8696       (popcount (convert?@0 (bswap:s@1 @2)))
8697       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8698            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8699         (with { tree type0 = TREE_TYPE (@0);
8700                 tree type1 = TREE_TYPE (@1);
8701                 unsigned int prec0 = TYPE_PRECISION (type0);
8702                 unsigned int prec1 = TYPE_PRECISION (type1); }
8703           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8704             (popcount (convert:type0 (convert:type1 @2)))))))))
8706 /* popcount(rotate(X Y)) is popcount(X).  */
8707 (for popcount (POPCOUNT)
8708   (for rot (lrotate rrotate)
8709     (simplify
8710       (popcount (convert?@0 (rot:s@1 @2 @3)))
8711       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8712            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8713            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8714         (with { tree type0 = TREE_TYPE (@0);
8715                 tree type1 = TREE_TYPE (@1);
8716                 unsigned int prec0 = TYPE_PRECISION (type0);
8717                 unsigned int prec1 = TYPE_PRECISION (type1); }
8718           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8719             (popcount (convert:type0 @2))))))))
8721 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8722 (simplify
8723   (bit_and (POPCOUNT @0) integer_onep)
8724   (PARITY @0))
8726 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8727 (simplify
8728   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8729   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8731 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8732 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8733 (for popcount (POPCOUNT)
8734   (for log1 (bit_and bit_ior)
8735        log2 (bit_ior bit_and)
8736     (simplify
8737       (minus (plus:s (popcount:s @0) (popcount:s @1))
8738              (popcount:s (log1:cs @0 @1)))
8739       (popcount (log2 @0 @1)))
8740     (simplify
8741       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8742               (popcount:s @1))
8743       (popcount (log2 @0 @1)))))
8745 /* PARITY simplifications.  */
8746 /* parity(~X) is parity(X).  */
8747 (simplify
8748   (PARITY (bit_not @0))
8749   (PARITY @0))
8751 /* parity(bswap(x)) is parity(x).  */
8752 (for parity (PARITY)
8753   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8754               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8755     (simplify
8756       (parity (convert?@0 (bswap:s@1 @2)))
8757       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8758            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8759            && TYPE_PRECISION (TREE_TYPE (@0))
8760               >= TYPE_PRECISION (TREE_TYPE (@1)))
8761         (with { tree type0 = TREE_TYPE (@0);
8762                 tree type1 = TREE_TYPE (@1); }
8763           (parity (convert:type0 (convert:type1 @2))))))))
8765 /* parity(rotate(X Y)) is parity(X).  */
8766 (for parity (PARITY)
8767   (for rot (lrotate rrotate)
8768     (simplify
8769       (parity (convert?@0 (rot:s@1 @2 @3)))
8770       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8771            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8772            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8773            && TYPE_PRECISION (TREE_TYPE (@0))
8774               >= TYPE_PRECISION (TREE_TYPE (@1)))
8775         (with { tree type0 = TREE_TYPE (@0); }
8776           (parity (convert:type0 @2)))))))
8778 /* parity(X)^parity(Y) is parity(X^Y).  */
8779 (simplify
8780   (bit_xor (PARITY:s @0) (PARITY:s @1))
8781   (PARITY (bit_xor @0 @1)))
8783 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8784 (for func (POPCOUNT BSWAP FFS PARITY)
8785  (simplify
8786   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8787   @3))
8789 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8790    where CST is precision-1. */
8791 (for func (CLRSB)
8792  (simplify
8793   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8794   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8795    @4)))
8797 #if GIMPLE
8798 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8799 (for func (CLZ)
8800  (simplify
8801   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8802   (with { int val;
8803           internal_fn ifn = IFN_LAST;
8804           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8805             {
8806               if (tree_fits_shwi_p (@2))
8807                 {
8808                   HOST_WIDE_INT valw = tree_to_shwi (@2);
8809                   if ((int) valw == valw)
8810                     {
8811                       val = valw;
8812                       ifn = IFN_CLZ;
8813                     }
8814                 }
8815             }
8816           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8817                                                    OPTIMIZE_FOR_BOTH)
8818                    && CLZ_DEFINED_VALUE_AT_ZERO
8819                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8820             ifn = IFN_CLZ;
8821         }
8822    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8823     (IFN_CLZ @3 @2)))))
8824 (simplify
8825  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
8826   (with { int val;
8827           internal_fn ifn = IFN_LAST;
8828           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8829             ifn = IFN_CLZ;
8830           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8831                                                    OPTIMIZE_FOR_BOTH))
8832             ifn = IFN_CLZ;
8833         }
8834    (if (ifn == IFN_CLZ)
8835     (IFN_CLZ @3 @2))))
8837 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8838 (for func (CTZ)
8839  (simplify
8840   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8841   (with { int val;
8842           internal_fn ifn = IFN_LAST;
8843           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8844             {
8845               if (tree_fits_shwi_p (@2))
8846                 {
8847                   HOST_WIDE_INT valw = tree_to_shwi (@2);
8848                   if ((int) valw == valw)
8849                     {
8850                       val = valw;
8851                       ifn = IFN_CTZ;
8852                     }
8853                 }
8854             }
8855           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
8856                                                    OPTIMIZE_FOR_BOTH)
8857                    && CTZ_DEFINED_VALUE_AT_ZERO
8858                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8859             ifn = IFN_CTZ;
8860         }
8861    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8862     (IFN_CTZ @3 @2)))))
8863 (simplify
8864  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
8865   (with { int val;
8866           internal_fn ifn = IFN_LAST;
8867           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8868             ifn = IFN_CTZ;
8869           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
8870                                                    OPTIMIZE_FOR_BOTH))
8871             ifn = IFN_CTZ;
8872         }
8873    (if (ifn == IFN_CTZ)
8874     (IFN_CTZ @3 @2))))
8875 #endif
8877 /* Common POPCOUNT/PARITY simplifications.  */
8878 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8879 (for pfun (POPCOUNT PARITY)
8880   (simplify
8881     (pfun @0)
8882     (if (INTEGRAL_TYPE_P (type))
8883      (with { wide_int nz = tree_nonzero_bits (@0); }
8884        (switch
8885          (if (nz == 1)
8886            (convert @0))
8887          (if (wi::popcount (nz) == 1)
8888            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8889              (convert (rshift:utype (convert:utype @0)
8890                                     { build_int_cst (integer_type_node,
8891                                                      wi::ctz (nz)); })))))))))
8893 #if GIMPLE
8894 /* 64- and 32-bits branchless implementations of popcount are detected:
8896    int popcount64c (uint64_t x)
8897    {
8898      x -= (x >> 1) & 0x5555555555555555ULL;
8899      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8900      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8901      return (x * 0x0101010101010101ULL) >> 56;
8902    }
8904    int popcount32c (uint32_t x)
8905    {
8906      x -= (x >> 1) & 0x55555555;
8907      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8908      x = (x + (x >> 4)) & 0x0f0f0f0f;
8909      return (x * 0x01010101) >> 24;
8910    }  */
8911 (simplify
8912  (rshift
8913   (mult
8914    (bit_and
8915     (plus:c
8916      (rshift @8 INTEGER_CST@5)
8917       (plus:c@8
8918        (bit_and @6 INTEGER_CST@7)
8919         (bit_and
8920          (rshift
8921           (minus@6 @0
8922            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8923           INTEGER_CST@10)
8924          INTEGER_CST@9)))
8925     INTEGER_CST@3)
8926    INTEGER_CST@2)
8927   INTEGER_CST@1)
8928   /* Check constants and optab.  */
8929   (with { unsigned prec = TYPE_PRECISION (type);
8930           int shift = (64 - prec) & 63;
8931           unsigned HOST_WIDE_INT c1
8932             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8933           unsigned HOST_WIDE_INT c2
8934             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8935           unsigned HOST_WIDE_INT c3
8936             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8937           unsigned HOST_WIDE_INT c4
8938             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8939    }
8940    (if (prec >= 16
8941         && prec <= 64
8942         && pow2p_hwi (prec)
8943         && TYPE_UNSIGNED (type)
8944         && integer_onep (@4)
8945         && wi::to_widest (@10) == 2
8946         && wi::to_widest (@5) == 4
8947         && wi::to_widest (@1) == prec - 8
8948         && tree_to_uhwi (@2) == c1
8949         && tree_to_uhwi (@3) == c2
8950         && tree_to_uhwi (@9) == c3
8951         && tree_to_uhwi (@7) == c3
8952         && tree_to_uhwi (@11) == c4)
8953     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8954                                          OPTIMIZE_FOR_BOTH))
8955      (convert (IFN_POPCOUNT:type @0))
8956      /* Try to do popcount in two halves.  PREC must be at least
8957         five bits for this to work without extension before adding.  */
8958      (with {
8959        tree half_type = NULL_TREE;
8960        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8961        int half_prec = 8;
8962        if (m.exists ()
8963            && m.require () != TYPE_MODE (type))
8964          {
8965            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8966            half_type = build_nonstandard_integer_type (half_prec, 1);
8967          }
8968        gcc_assert (half_prec > 2);
8969       }
8970       (if (half_type != NULL_TREE
8971            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8972                                               OPTIMIZE_FOR_BOTH))
8973        (convert (plus
8974          (IFN_POPCOUNT:half_type (convert @0))
8975          (IFN_POPCOUNT:half_type (convert (rshift @0
8976             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8978 /* __builtin_ffs needs to deal on many targets with the possible zero
8979    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8980    should lead to better code.  */
8981 (simplify
8982  (FFS tree_expr_nonzero_p@0)
8983  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8984       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8985                                          OPTIMIZE_FOR_SPEED))
8986   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8987    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8988 #endif
8990 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8991           BUILT_IN_FFSIMAX)
8992  /* __builtin_ffs (X) == 0 -> X == 0.
8993     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8994  (for cmp (eq ne)
8995   (simplify
8996    (cmp (ffs@2 @0) INTEGER_CST@1)
8997     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8998      (switch
8999       (if (integer_zerop (@1))
9000        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9001       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9002        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9003       (if (single_use (@2))
9004        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9005                                             wi::mask (tree_to_uhwi (@1),
9006                                                       false, prec)); })
9007             { wide_int_to_tree (TREE_TYPE (@0),
9008                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9009                                                   false, prec)); }))))))
9011  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9012  (for cmp (gt le)
9013       cmp2 (ne eq)
9014       cmp3 (eq ne)
9015       bit_op (bit_and bit_ior)
9016   (simplify
9017    (cmp (ffs@2 @0) INTEGER_CST@1)
9018     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9019      (switch
9020       (if (integer_zerop (@1))
9021        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9022       (if (tree_int_cst_sgn (@1) < 0)
9023        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9024       (if (wi::to_widest (@1) >= prec)
9025        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9026       (if (wi::to_widest (@1) == prec - 1)
9027        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9028                                     wi::shifted_mask (prec - 1, 1,
9029                                                       false, prec)); }))
9030       (if (single_use (@2))
9031        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9032                (cmp3 (bit_and @0
9033                               { wide_int_to_tree (TREE_TYPE (@0),
9034                                                   wi::mask (tree_to_uhwi (@1),
9035                                                   false, prec)); })
9036                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9038 #if GIMPLE
9040 /* Simplify:
9041      a = op a1
9042      r = cond ? a : b
9043      --> r = .COND_FN (cond, a, b)
9044 and,
9045     a = op a1
9046     r = cond ? b : a
9047     --> r = .COND_FN (~cond, b, a).  */
9049 (for uncond_op (UNCOND_UNARY)
9050      cond_op (COND_UNARY)
9051  (simplify
9052   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9053    (with { tree op_type = TREE_TYPE (@3); }
9054     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9055         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9056      (cond_op @0 (view_convert @1) @2))))
9057  (simplify
9058   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9059    (with { tree op_type = TREE_TYPE (@3); }
9060     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9061         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9062      (cond_op (bit_not @0) (view_convert @2) @1)))))
9064 (for uncond_op (UNCOND_UNARY)
9065      cond_op (COND_LEN_UNARY)
9066  (simplify
9067   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9068    (with { tree op_type = TREE_TYPE (@3); }
9069     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9070         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9071      (cond_op @0 (view_convert @1) @2 @4 @5))))
9072  (simplify
9073   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9074    (with { tree op_type = TREE_TYPE (@3); }
9075     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9076         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9077      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9079 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9080 (simplify
9081  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9082  (if (canonicalize_math_after_vectorization_p ()
9083       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9084       && is_truth_type_for (type, TREE_TYPE (@0)))
9085  (if (integer_all_onesp (@1) && integer_zerop (@2))
9086   (IFN_COND_NOT @0 @3 @3))
9087   (if (integer_all_onesp (@2) && integer_zerop (@1))
9088    (IFN_COND_NOT (bit_not @0) @3 @3))))
9090 /* Simplify:
9092      a = a1 op a2
9093      r = c ? a : b;
9095    to:
9097      r = c ? a1 op a2 : b;
9099    if the target can do it in one go.  This makes the operation conditional
9100    on c, so could drop potentially-trapping arithmetic, but that's a valid
9101    simplification if the result of the operation isn't needed.
9103    Avoid speculatively generating a stand-alone vector comparison
9104    on targets that might not support them.  Any target implementing
9105    conditional internal functions must support the same comparisons
9106    inside and outside a VEC_COND_EXPR.  */
9108 (for uncond_op (UNCOND_BINARY)
9109      cond_op (COND_BINARY)
9110  (simplify
9111   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9112   (with { tree op_type = TREE_TYPE (@4); }
9113    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9114         && is_truth_type_for (op_type, TREE_TYPE (@0))
9115         && single_use (@4))
9116     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9117  (simplify
9118   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9119   (with { tree op_type = TREE_TYPE (@4); }
9120    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9121         && is_truth_type_for (op_type, TREE_TYPE (@0))
9122         && single_use (@4))
9123     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9125 (for uncond_op (UNCOND_BINARY)
9126      cond_op (COND_LEN_BINARY)
9127  (simplify
9128   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9129   (with { tree op_type = TREE_TYPE (@4); }
9130    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9131         && is_truth_type_for (op_type, TREE_TYPE (@0))
9132         && single_use (@4))
9133     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9134  (simplify
9135   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9136   (with { tree op_type = TREE_TYPE (@4); }
9137    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9138         && is_truth_type_for (op_type, TREE_TYPE (@0))
9139         && single_use (@4))
9140     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9142 /* Same for ternary operations.  */
9143 (for uncond_op (UNCOND_TERNARY)
9144      cond_op (COND_TERNARY)
9145  (simplify
9146   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9147   (with { tree op_type = TREE_TYPE (@5); }
9148    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9149         && is_truth_type_for (op_type, TREE_TYPE (@0))
9150         && single_use (@5))
9151     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9152  (simplify
9153   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9154   (with { tree op_type = TREE_TYPE (@5); }
9155    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9156         && is_truth_type_for (op_type, TREE_TYPE (@0))
9157         && single_use (@5))
9158     (view_convert (cond_op (bit_not @0) @2 @3 @4
9159                   (view_convert:op_type @1)))))))
9161 (for uncond_op (UNCOND_TERNARY)
9162      cond_op (COND_LEN_TERNARY)
9163  (simplify
9164   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9165   (with { tree op_type = TREE_TYPE (@5); }
9166    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9167         && is_truth_type_for (op_type, TREE_TYPE (@0))
9168         && single_use (@5))
9169     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9170  (simplify
9171   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9172   (with { tree op_type = TREE_TYPE (@5); }
9173    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9174         && is_truth_type_for (op_type, TREE_TYPE (@0))
9175         && single_use (@5))
9176     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9177 #endif
9179 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9180    "else" value of an IFN_COND_*.  */
9181 (for cond_op (COND_BINARY)
9182  (simplify
9183   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9184   (with { tree op_type = TREE_TYPE (@3); }
9185    (if (element_precision (type) == element_precision (op_type))
9186     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9187  (simplify
9188   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9189   (with { tree op_type = TREE_TYPE (@5); }
9190    (if (inverse_conditions_p (@0, @2)
9191         && element_precision (type) == element_precision (op_type))
9192     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9194 /* Same for ternary operations.  */
9195 (for cond_op (COND_TERNARY)
9196  (simplify
9197   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9198   (with { tree op_type = TREE_TYPE (@4); }
9199    (if (element_precision (type) == element_precision (op_type))
9200     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9201  (simplify
9202   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9203   (with { tree op_type = TREE_TYPE (@6); }
9204    (if (inverse_conditions_p (@0, @2)
9205         && element_precision (type) == element_precision (op_type))
9206     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9208 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9209    "else" value of an IFN_COND_LEN_*.  */
9210 (for cond_len_op (COND_LEN_BINARY)
9211  (simplify
9212   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9213   (with { tree op_type = TREE_TYPE (@3); }
9214    (if (element_precision (type) == element_precision (op_type))
9215     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9216  (simplify
9217   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9218   (with { tree op_type = TREE_TYPE (@5); }
9219    (if (inverse_conditions_p (@0, @2)
9220         && element_precision (type) == element_precision (op_type))
9221     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9223 /* Same for ternary operations.  */
9224 (for cond_len_op (COND_LEN_TERNARY)
9225  (simplify
9226   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9227   (with { tree op_type = TREE_TYPE (@4); }
9228    (if (element_precision (type) == element_precision (op_type))
9229     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9230  (simplify
9231   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9232   (with { tree op_type = TREE_TYPE (@6); }
9233    (if (inverse_conditions_p (@0, @2)
9234         && element_precision (type) == element_precision (op_type))
9235     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9237 /* Detect simplication for a conditional reduction where
9239    a = mask1 ? b : 0
9240    c = mask2 ? d + a : d
9242    is turned into
9244    c = mask1 && mask2 ? d + b : d.  */
9245 (simplify
9246   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9247    (if (ANY_INTEGRAL_TYPE_P (type)
9248         || (FLOAT_TYPE_P (type)
9249             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9250    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9252 /* Detect simplication for a conditional length reduction where
9254    a = mask ? b : 0
9255    c = i < len + bias ? d + a : d
9257    is turned into
9259    c = mask && i < len + bias ? d + b : d.  */
9260 (simplify
9261   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9262    (if (ANY_INTEGRAL_TYPE_P (type)
9263         || (FLOAT_TYPE_P (type)
9264             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9265     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9267 /* Detect simplification for vector condition folding where
9269   c = mask1 ? (masked_op mask2 a b) : b
9271   into
9273   c = masked_op (mask1 & mask2) a b
9275   where the operation can be partially applied to one operand. */
9277 (for cond_op (COND_BINARY)
9278  (simplify
9279   (vec_cond @0
9280    (cond_op:s @1 @2 @3 @4) @3)
9281   (cond_op (bit_and @1 @0) @2 @3 @4)))
9283 /* And same for ternary expressions.  */
9285 (for cond_op (COND_TERNARY)
9286  (simplify
9287   (vec_cond @0
9288    (cond_op:s @1 @2 @3 @4 @5) @4)
9289   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9291 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9292    expressions like:
9294    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9295    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9297    If pointers are known not to wrap, B checks whether @1 bytes starting
9298    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9299    bytes.  A is more efficiently tested as:
9301    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9303    The equivalent expression for B is given by replacing @1 with @1 - 1:
9305    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9307    @0 and @2 can be swapped in both expressions without changing the result.
9309    The folds rely on sizetype's being unsigned (which is always true)
9310    and on its being the same width as the pointer (which we have to check).
9312    The fold replaces two pointer_plus expressions, two comparisons and
9313    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9314    the best case it's a saving of two operations.  The A fold retains one
9315    of the original pointer_pluses, so is a win even if both pointer_pluses
9316    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9317    used elsewhere, since all we end up doing is replacing a comparison with
9318    a pointer_plus.  We do still apply the fold under those circumstances
9319    though, in case applying it to other conditions eventually makes one of the
9320    pointer_pluses dead.  */
9321 (for ior (truth_orif truth_or bit_ior)
9322  (for cmp (le lt)
9323   (simplify
9324    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9325         (cmp:cs (pointer_plus@4 @2 @1) @0))
9326    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9327         && TYPE_OVERFLOW_WRAPS (sizetype)
9328         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9329     /* Calculate the rhs constant.  */
9330     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9331             offset_int rhs = off * 2; }
9332      /* Always fails for negative values.  */
9333      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9334       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9335          pick a canonical order.  This increases the chances of using the
9336          same pointer_plus in multiple checks.  */
9337       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9338               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9339        (if (cmp == LT_EXPR)
9340         (gt (convert:sizetype
9341              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9342                                      { swap_p ? @0 : @2; }))
9343             { rhs_tree; })
9344         (gt (convert:sizetype
9345              (pointer_diff:ssizetype
9346               (pointer_plus { swap_p ? @2 : @0; }
9347                             { wide_int_to_tree (sizetype, off); })
9348               { swap_p ? @0 : @2; }))
9349             { rhs_tree; })))))))))
9351 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9352    element of @1.  */
9353 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9354  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9355   (with { int i = single_nonzero_element (@1); }
9356    (if (i >= 0)
9357     (with { tree elt = vector_cst_elt (@1, i);
9358             tree elt_type = TREE_TYPE (elt);
9359             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9360             tree size = bitsize_int (elt_bits);
9361             tree pos = bitsize_int (elt_bits * i); }
9362      (view_convert
9363       (bit_and:elt_type
9364        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9365        { elt; })))))))
9367 /* Fold reduction of a single nonzero element constructor.  */
9368 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9369   (simplify (reduc (CONSTRUCTOR@0))
9370     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9371                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9372             tree elt = ctor_single_nonzero_element (ctor); }
9373       (if (elt
9374            && !HONOR_SNANS (type)
9375            && !HONOR_SIGNED_ZEROS (type))
9376         { elt; }))))
9378 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9379 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9380             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9381      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9382   (simplify (reduc (op @0 VECTOR_CST@1))
9383     (op (reduc:type @0) (reduc:type @1))))
9385 /* Simplify vector floating point operations of alternating sub/add pairs
9386    into using an fneg of a wider element type followed by a normal add.
9387    under IEEE 754 the fneg of the wider type will negate every even entry
9388    and when doing an add we get a sub of the even and add of every odd
9389    elements.  */
9390 (for plusminus (plus minus)
9391      minusplus (minus plus)
9392  (simplify
9393   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9394    (if (!VECTOR_INTEGER_TYPE_P (type)
9395         && !FLOAT_WORDS_BIG_ENDIAN
9396         /* plus is commutative, while minus is not, so :c can't be used.
9397            Do equality comparisons by hand and at the end pick the operands
9398            from the minus.  */
9399         && (operand_equal_p (@0, @2, 0)
9400             ? operand_equal_p (@1, @3, 0)
9401             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9402    (with
9403     {
9404       /* Build a vector of integers from the tree mask.  */
9405       vec_perm_builder builder;
9406     }
9407     (if (tree_to_vec_perm_builder (&builder, @4))
9408      (with
9409       {
9410         /* Create a vec_perm_indices for the integer vector.  */
9411         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9412         vec_perm_indices sel (builder, 2, nelts);
9413         machine_mode vec_mode = TYPE_MODE (type);
9414         machine_mode wide_mode;
9415         scalar_mode wide_elt_mode;
9416         poly_uint64 wide_nunits;
9417         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9418       }
9419       (if (VECTOR_MODE_P (vec_mode)
9420            && sel.series_p (0, 2, 0, 2)
9421            && sel.series_p (1, 2, nelts + 1, 2)
9422            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9423            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9424            && related_vector_mode (vec_mode, wide_elt_mode,
9425                                    wide_nunits).exists (&wide_mode))
9426        (with
9427         {
9428           tree stype
9429             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9430                                               TYPE_UNSIGNED (type));
9431           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9433           /* The format has to be a non-extended ieee format.  */
9434           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9435           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9436         }
9437         (if (TYPE_MODE (stype) != BLKmode
9438              && VECTOR_TYPE_P (ntype)
9439              && fmt_old != NULL
9440              && fmt_new != NULL)
9441          (with
9442           {
9443             /* If the target doesn't support v1xx vectors, try using
9444                scalar mode xx instead.  */
9445             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9446                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9447               ntype = stype;
9448           }
9449           (if (fmt_new->signbit_rw
9450                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9451                && fmt_new->signbit_rw == fmt_new->signbit_ro
9452                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9453                                                  TYPE_MODE (type), ALL_REGS)
9454                && ((optimize_vectors_before_lowering_p ()
9455                     && VECTOR_TYPE_P (ntype))
9456                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9457            (if (plusminus == PLUS_EXPR)
9458             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9459             (minus @0 (view_convert:type
9460                         (negate (view_convert:ntype @1))))))))))))))))
9462 (simplify
9463  (vec_perm @0 @1 VECTOR_CST@2)
9464  (with
9465   {
9466     tree op0 = @0, op1 = @1, op2 = @2;
9467     machine_mode result_mode = TYPE_MODE (type);
9468     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9470     /* Build a vector of integers from the tree mask.  */
9471     vec_perm_builder builder;
9472   }
9473   (if (tree_to_vec_perm_builder (&builder, op2))
9474    (with
9475     {
9476       /* Create a vec_perm_indices for the integer vector.  */
9477       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9478       bool single_arg = (op0 == op1);
9479       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9480     }
9481     (if (sel.series_p (0, 1, 0, 1))
9482      { op0; }
9483      (if (sel.series_p (0, 1, nelts, 1))
9484       { op1; }
9485       (with
9486        {
9487          if (!single_arg)
9488            {
9489              if (sel.all_from_input_p (0))
9490                op1 = op0;
9491              else if (sel.all_from_input_p (1))
9492                {
9493                  op0 = op1;
9494                  sel.rotate_inputs (1);
9495                }
9496              else if (known_ge (poly_uint64 (sel[0]), nelts))
9497                {
9498                  std::swap (op0, op1);
9499                  sel.rotate_inputs (1);
9500                }
9501            }
9502          gassign *def;
9503          tree cop0 = op0, cop1 = op1;
9504          if (TREE_CODE (op0) == SSA_NAME
9505              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9506              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9507            cop0 = gimple_assign_rhs1 (def);
9508          if (TREE_CODE (op1) == SSA_NAME
9509              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9510              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9511            cop1 = gimple_assign_rhs1 (def);
9512          tree t;
9513        }
9514        (if ((TREE_CODE (cop0) == VECTOR_CST
9515              || TREE_CODE (cop0) == CONSTRUCTOR)
9516             && (TREE_CODE (cop1) == VECTOR_CST
9517                 || TREE_CODE (cop1) == CONSTRUCTOR)
9518             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9519         { t; }
9520         (with
9521          {
9522            bool changed = (op0 == op1 && !single_arg);
9523            tree ins = NULL_TREE;
9524            unsigned at = 0;
9526            /* See if the permutation is performing a single element
9527               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9528               in that case.  But only if the vector mode is supported,
9529               otherwise this is invalid GIMPLE.  */
9530            if (op_mode != BLKmode
9531                && (TREE_CODE (cop0) == VECTOR_CST
9532                    || TREE_CODE (cop0) == CONSTRUCTOR
9533                    || TREE_CODE (cop1) == VECTOR_CST
9534                    || TREE_CODE (cop1) == CONSTRUCTOR))
9535              {
9536                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9537                if (insert_first_p)
9538                  {
9539                    /* After canonicalizing the first elt to come from the
9540                       first vector we only can insert the first elt from
9541                       the first vector.  */
9542                    at = 0;
9543                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9544                      op0 = op1;
9545                  }
9546                /* The above can fail for two-element vectors which always
9547                   appear to insert the first element, so try inserting
9548                   into the second lane as well.  For more than two
9549                   elements that's wasted time.  */
9550                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9551                  {
9552                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9553                    for (at = 0; at < encoded_nelts; ++at)
9554                      if (maybe_ne (sel[at], at))
9555                        break;
9556                    if (at < encoded_nelts
9557                        && (known_eq (at + 1, nelts)
9558                            || sel.series_p (at + 1, 1, at + 1, 1)))
9559                      {
9560                        if (known_lt (poly_uint64 (sel[at]), nelts))
9561                          ins = fold_read_from_vector (cop0, sel[at]);
9562                        else
9563                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9564                      }
9565                  }
9566              }
9568            /* Generate a canonical form of the selector.  */
9569            if (!ins && sel.encoding () != builder)
9570              {
9571                /* Some targets are deficient and fail to expand a single
9572                   argument permutation while still allowing an equivalent
9573                   2-argument version.  */
9574                tree oldop2 = op2;
9575                if (sel.ninputs () == 2
9576                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9577                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9578                else
9579                  {
9580                    vec_perm_indices sel2 (builder, 2, nelts);
9581                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9582                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9583                    else
9584                      /* Not directly supported with either encoding,
9585                         so use the preferred form.  */
9586                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9587                  }
9588                if (!operand_equal_p (op2, oldop2, 0))
9589                  changed = true;
9590              }
9591          }
9592          (if (ins)
9593           (bit_insert { op0; } { ins; }
9594            { bitsize_int (at * vector_element_bits (type)); })
9595           (if (changed)
9596            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9598 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9600 (match vec_same_elem_p
9601  (vec_duplicate @0))
9603 (match vec_same_elem_p
9604  CONSTRUCTOR@0
9605  (if (TREE_CODE (@0) == SSA_NAME
9606       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9608 (match vec_same_elem_p
9609  @0
9610  (if (uniform_vector_p (@0))))
9613 (simplify
9614  (vec_perm vec_same_elem_p@0 @0 @1)
9615  (if (types_match (type, TREE_TYPE (@0)))
9616   @0
9617   (with
9618    {
9619      tree elem = uniform_vector_p (@0);
9620    }
9621    (if (elem)
9622     { build_vector_from_val (type, elem); }))))
9624 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9625 (simplify
9626  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9627  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9628   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9629 (simplify
9630  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9631  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9632   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9635 /* Merge
9636      c = VEC_PERM_EXPR <a, b, VCST0>;
9637      d = VEC_PERM_EXPR <c, c, VCST1>;
9638    to
9639      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9641 (simplify
9642  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9643  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9644   (with
9645    {
9646      machine_mode result_mode = TYPE_MODE (type);
9647      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9648      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9649      vec_perm_builder builder0;
9650      vec_perm_builder builder1;
9651      vec_perm_builder builder2 (nelts, nelts, 1);
9652    }
9653    (if (tree_to_vec_perm_builder (&builder0, @3)
9654         && tree_to_vec_perm_builder (&builder1, @4))
9655     (with
9656      {
9657        vec_perm_indices sel0 (builder0, 2, nelts);
9658        vec_perm_indices sel1 (builder1, 1, nelts);
9660        for (int i = 0; i < nelts; i++)
9661          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9663        vec_perm_indices sel2 (builder2, 2, nelts);
9665        tree op0 = NULL_TREE;
9666        /* If the new VEC_PERM_EXPR can't be handled but both
9667           original VEC_PERM_EXPRs can, punt.
9668           If one or both of the original VEC_PERM_EXPRs can't be
9669           handled and the new one can't be either, don't increase
9670           number of VEC_PERM_EXPRs that can't be handled.  */
9671        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9672            || (single_use (@0)
9673                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9674                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9675                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9676          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9677      }
9678      (if (op0)
9679       (vec_perm @1 @2 { op0; })))))))
9681 /* Merge
9682      c = VEC_PERM_EXPR <a, b, VCST0>;
9683      d = VEC_PERM_EXPR <x, c, VCST1>;
9684    to
9685      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9686    when all elements from a or b are replaced by the later
9687    permutation.  */
9689 (simplify
9690  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9691  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9692   (with
9693    {
9694      machine_mode result_mode = TYPE_MODE (type);
9695      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9696      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9697      vec_perm_builder builder0;
9698      vec_perm_builder builder1;
9699      vec_perm_builder builder2 (nelts, nelts, 2);
9700    }
9701    (if (tree_to_vec_perm_builder (&builder0, @3)
9702         && tree_to_vec_perm_builder (&builder1, @4))
9703     (with
9704      {
9705        vec_perm_indices sel0 (builder0, 2, nelts);
9706        vec_perm_indices sel1 (builder1, 2, nelts);
9707        bool use_1 = false, use_2 = false;
9709        for (int i = 0; i < nelts; i++)
9710          {
9711            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9712              builder2.quick_push (sel1[i]);
9713            else
9714              {
9715                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9716                                     .to_constant ()];
9717                if (known_lt (j, sel0.nelts_per_input ()))
9718                  use_1 = true;
9719                else
9720                  {
9721                    use_2 = true;
9722                    j -= sel0.nelts_per_input ();
9723                  }
9724                builder2.quick_push (j + sel1.nelts_per_input ());
9725              }
9726          }
9727      }
9728      (if (use_1 ^ use_2)
9729       (with
9730        {
9731          vec_perm_indices sel2 (builder2, 2, nelts);
9732          tree op0 = NULL_TREE;
9733          /* If the new VEC_PERM_EXPR can't be handled but both
9734             original VEC_PERM_EXPRs can, punt.
9735             If one or both of the original VEC_PERM_EXPRs can't be
9736             handled and the new one can't be either, don't increase
9737             number of VEC_PERM_EXPRs that can't be handled.  */
9738          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9739              || (single_use (@0)
9740                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9741                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9742                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9743            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9744        }
9745        (if (op0)
9746         (switch
9747          (if (use_1)
9748           (vec_perm @5 @1 { op0; }))
9749          (if (use_2)
9750           (vec_perm @5 @2 { op0; })))))))))))
9752 /* And the case with swapped outer permute sources.  */
9754 (simplify
9755  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9756  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9757   (with
9758    {
9759      machine_mode result_mode = TYPE_MODE (type);
9760      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9761      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9762      vec_perm_builder builder0;
9763      vec_perm_builder builder1;
9764      vec_perm_builder builder2 (nelts, nelts, 2);
9765    }
9766    (if (tree_to_vec_perm_builder (&builder0, @3)
9767         && tree_to_vec_perm_builder (&builder1, @4))
9768     (with
9769      {
9770        vec_perm_indices sel0 (builder0, 2, nelts);
9771        vec_perm_indices sel1 (builder1, 2, nelts);
9772        bool use_1 = false, use_2 = false;
9774        for (int i = 0; i < nelts; i++)
9775          {
9776            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9777              builder2.quick_push (sel1[i]);
9778            else
9779              {
9780                poly_uint64 j = sel0[sel1[i].to_constant ()];
9781                if (known_lt (j, sel0.nelts_per_input ()))
9782                  use_1 = true;
9783                else
9784                  {
9785                    use_2 = true;
9786                    j -= sel0.nelts_per_input ();
9787                  }
9788                builder2.quick_push (j);
9789              }
9790          }
9791      }
9792      (if (use_1 ^ use_2)
9793       (with
9794        {
9795          vec_perm_indices sel2 (builder2, 2, nelts);
9796          tree op0 = NULL_TREE;
9797          /* If the new VEC_PERM_EXPR can't be handled but both
9798             original VEC_PERM_EXPRs can, punt.
9799             If one or both of the original VEC_PERM_EXPRs can't be
9800             handled and the new one can't be either, don't increase
9801             number of VEC_PERM_EXPRs that can't be handled.  */
9802          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9803              || (single_use (@0)
9804                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9805                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9806                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9807            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9808        }
9809        (if (op0)
9810         (switch
9811          (if (use_1)
9812           (vec_perm @1 @5 { op0; }))
9813          (if (use_2)
9814           (vec_perm @2 @5 { op0; })))))))))))
9817 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9818    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9819    constant which when multiplied by a power of 2 contains a unique value
9820    in the top 5 or 6 bits.  This is then indexed into a table which maps it
9821    to the number of trailing zeroes.  */
9822 (match (ctz_table_index @1 @2 @3)
9823   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9825 (match (cond_expr_convert_p @0 @2 @3 @6)
9826  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9827   (if (INTEGRAL_TYPE_P (type)
9828        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9829        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9830        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9831        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9832        && TYPE_PRECISION (TREE_TYPE (@0))
9833           == TYPE_PRECISION (TREE_TYPE (@2))
9834        && TYPE_PRECISION (TREE_TYPE (@0))
9835           == TYPE_PRECISION (TREE_TYPE (@3))
9836        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9837           signess when convert is truncation, but not ok for extension since
9838           it's sign_extend vs zero_extend.  */
9839        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9840            || (TYPE_UNSIGNED (TREE_TYPE (@2))
9841                == TYPE_UNSIGNED (TREE_TYPE (@3))))
9842        && single_use (@4)
9843        && single_use (@5))))
9845 (for bit_op (bit_and bit_ior bit_xor)
9846  (match (bitwise_induction_p @0 @2 @3)
9847   (bit_op:c
9848    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9849    @3)))
9851 (match (bitwise_induction_p @0 @2 @3)
9852  (bit_not
9853   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9855 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
9856    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
9857 (simplify
9858   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9859   (with { auto i = wi::neg (wi::to_wide (@2)); }
9860   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
9861     (if (wi::popcount (i) == 1
9862          && (wi::to_wide (@1)) == (i - 1))
9863       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9864         (bit_and @0 @1)
9865       (cond (le @0 @1) @0 (bit_and @0 @1))))))
9867 /* -x & 1 -> x & 1.  */
9868 (simplify 
9869  (bit_and (negate @0) integer_onep@1)
9870  (if (!TYPE_OVERFLOW_SANITIZED (type))
9871   (bit_and @0 @1)))
9873 /* `-a` is just `a` if the type is 1bit wide or when converting
9874    to a 1bit type; similar to the above transformation of `(-x)&1`.
9875    This is used mostly with the transformation of
9876    `a ? ~b : b` into `(-a)^b`.
9877    It also can show up with bitfields.  */
9878 (simplify
9879  (convert? (negate @0))
9880  (if (INTEGRAL_TYPE_P (type)
9881       && TYPE_PRECISION (type) == 1
9882       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9883   (convert @0)))
9885 /* Optimize
9886    c1 = VEC_PERM_EXPR (a, a, mask)
9887    c2 = VEC_PERM_EXPR (b, b, mask)
9888    c3 = c1 op c2
9889    -->
9890    c = a op b
9891    c3 = VEC_PERM_EXPR (c, c, mask)
9892    For all integer non-div operations.  */
9893 (for op (plus minus mult bit_and bit_ior bit_xor
9894          lshift rshift)
9895  (simplify
9896   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9897    (if (VECTOR_INTEGER_TYPE_P (type))
9898     (vec_perm (op@3 @0 @1) @3 @2))))
9900 /* Similar for float arithmetic when permutation constant covers
9901    all vector elements.  */
9902 (for op (plus minus mult)
9903  (simplify
9904   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9905    (if (VECTOR_FLOAT_TYPE_P (type)
9906         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9907     (with
9908      {
9909        tree perm_cst = @2;
9910        vec_perm_builder builder;
9911        bool full_perm_p = false;
9912        if (tree_to_vec_perm_builder (&builder, perm_cst))
9913          {
9914            unsigned HOST_WIDE_INT nelts;
9916            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9917            /* Create a vec_perm_indices for the VECTOR_CST.  */
9918            vec_perm_indices sel (builder, 1, nelts);
9920            /* Check if perm indices covers all vector elements.  */
9921            if (sel.encoding ().encoded_full_vector_p ())
9922              {
9923                auto_sbitmap seen (nelts);
9924                bitmap_clear (seen);
9926                unsigned HOST_WIDE_INT count = 0, i;
9928                for (i = 0; i < nelts; i++)
9929                  {
9930                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9931                     break;
9932                    count++;
9933                  }
9934                full_perm_p = count == nelts;
9935              }
9936          }
9937       }
9938       (if (full_perm_p)
9939         (vec_perm (op@3 @0 @1) @3 @2))))))