d: Add testcase from PR108962
[official-gcc.git] / gcc / match.pd
blobf09583bbcac491cdb0e193f7465914fa4f843cda
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)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG)
91 /* Binary operations and their associated IFN_COND_* function.  */
92 (define_operator_list UNCOND_BINARY
93   plus minus
94   mult trunc_div trunc_mod rdiv
95   min max
96   IFN_FMIN IFN_FMAX
97   bit_and bit_ior bit_xor
98   lshift rshift)
99 (define_operator_list COND_BINARY
100   IFN_COND_ADD IFN_COND_SUB
101   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102   IFN_COND_MIN IFN_COND_MAX
103   IFN_COND_FMIN IFN_COND_FMAX
104   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105   IFN_COND_SHL IFN_COND_SHR)
107 /* Same for ternary operations.  */
108 (define_operator_list UNCOND_TERNARY
109   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117   BUILT_IN_ATOMIC_FETCH_OR_16
118   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120   BUILT_IN_ATOMIC_FETCH_XOR_16
121   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123   BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128   BUILT_IN_SYNC_FETCH_AND_OR_16
129   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131   BUILT_IN_SYNC_FETCH_AND_XOR_16
132   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134   BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*.  */
136 (define_operator_list ATOMIC_FETCH_AND_N
137   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139   BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*.  */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144   BUILT_IN_SYNC_FETCH_AND_AND_16)
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147    plus conditionalize on tree_nop_conversion_p conversions.  */
148 (match (nop_convert @0)
149  (convert @0)
150  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152  (view_convert @0)
153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
158 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
159    ABSU_EXPR returns unsigned absolute value of the operand and the operand
160    of the ABSU_EXPR will have the corresponding signed type.  */
161 (simplify (abs (convert @0))
162  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
163       && !TYPE_UNSIGNED (TREE_TYPE (@0))
164       && element_precision (type) > element_precision (TREE_TYPE (@0)))
165   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
166    (convert (absu:utype @0)))))
168 #if GIMPLE
169 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
170 (simplify
171  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
172  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
173       && !TYPE_UNSIGNED (TREE_TYPE (@0))
174       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
175   (abs @0)))
176 #endif
178 /* Simplifications of operations with one constant operand and
179    simplifications to constants or single values.  */
181 (for op (plus pointer_plus minus bit_ior bit_xor)
182   (simplify
183     (op @0 integer_zerop)
184     (non_lvalue @0)))
186 /* 0 +p index -> (type)index */
187 (simplify
188  (pointer_plus integer_zerop @1)
189  (non_lvalue (convert @1)))
191 /* ptr - 0 -> (type)ptr */
192 (simplify
193  (pointer_diff @0 integer_zerop)
194  (convert @0))
196 /* See if ARG1 is zero and X + ARG1 reduces to X.
197    Likewise if the operands are reversed.  */
198 (simplify
199  (plus:c @0 real_zerop@1)
200  (if (fold_real_zero_addition_p (type, @0, @1, 0))
201   (non_lvalue @0)))
203 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
204 (simplify
205  (minus @0 real_zerop@1)
206  (if (fold_real_zero_addition_p (type, @0, @1, 1))
207   (non_lvalue @0)))
209 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
210    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
211    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
212    if not -frounding-math.  For sNaNs the first operation would raise
213    exceptions but turn the result into qNan, so the second operation
214    would not raise it.   */
215 (for inner_op (plus minus)
216  (for outer_op (plus minus)
217   (simplify
218    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
219     (if (real_zerop (@1)
220          && real_zerop (@2)
221          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
222      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
223                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
224              bool outer_plus
225                = ((outer_op == PLUS_EXPR)
226                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
227       (if (outer_plus && !inner_plus)
228        (outer_op @0 @2)
229        @3))))))
231 /* Simplify x - x.
232    This is unsafe for certain floats even in non-IEEE formats.
233    In IEEE, it is unsafe because it does wrong for NaNs.
234    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
235    Also note that operand_equal_p is always false if an operand
236    is volatile.  */
237 (simplify
238  (minus @0 @0)
239  (if (!FLOAT_TYPE_P (type)
240       || (!tree_expr_maybe_nan_p (@0)
241           && !tree_expr_maybe_infinite_p (@0)
242           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
243               || !HONOR_SIGNED_ZEROS (type))))
244   { build_zero_cst (type); }))
245 (simplify
246  (pointer_diff @@0 @0)
247  { build_zero_cst (type); })
249 (simplify
250  (mult @0 integer_zerop@1)
251  @1)
253 /* -x == x -> x == 0 */
254 (for cmp (eq ne)
255  (simplify
256   (cmp:c @0 (negate @0))
257    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
258         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
259     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
261 /* Maybe fold x * 0 to 0.  The expressions aren't the same
262    when x is NaN, since x * 0 is also NaN.  Nor are they the
263    same in modes with signed zeros, since multiplying a
264    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
265    since x * 0 is NaN.  */
266 (simplify
267  (mult @0 real_zerop@1)
268  (if (!tree_expr_maybe_nan_p (@0)
269       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
270       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
271   @1))
273 /* In IEEE floating point, x*1 is not equivalent to x for snans.
274    Likewise for complex arithmetic with signed zeros.  */
275 (simplify
276  (mult @0 real_onep)
277  (if (!tree_expr_maybe_signaling_nan_p (@0)
278       && (!HONOR_SIGNED_ZEROS (type)
279           || !COMPLEX_FLOAT_TYPE_P (type)))
280   (non_lvalue @0)))
282 /* Transform x * -1.0 into -x.  */
283 (simplify
284  (mult @0 real_minus_onep)
285   (if (!tree_expr_maybe_signaling_nan_p (@0)
286        && (!HONOR_SIGNED_ZEROS (type)
287            || !COMPLEX_FLOAT_TYPE_P (type)))
288    (negate @0)))
290 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
291    unless the target has native support for the former but not the latter.  */
292 (simplify
293  (mult @0 VECTOR_CST@1)
294  (if (initializer_each_zero_or_onep (@1)
295       && !HONOR_SNANS (type)
296       && !HONOR_SIGNED_ZEROS (type))
297   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
298    (if (itype
299         && (!VECTOR_MODE_P (TYPE_MODE (type))
300             || (VECTOR_MODE_P (TYPE_MODE (itype))
301                 && optab_handler (and_optab,
302                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
303     (view_convert (bit_and:itype (view_convert @0)
304                                  (ne @1 { build_zero_cst (type); })))))))
306 /* In SWAR (SIMD within a register) code a signed comparison of packed data
307    can be constructed with a particular combination of shift, bitwise and,
308    and multiplication by constants.  If that code is vectorized we can
309    convert this pattern into a more efficient vector comparison.  */
310 (simplify
311  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
312             uniform_integer_cst_p@2)
313     uniform_integer_cst_p@3)
314  (with {
315    tree rshift_cst = uniform_integer_cst_p (@1);
316    tree bit_and_cst = uniform_integer_cst_p (@2);
317    tree mult_cst = uniform_integer_cst_p (@3);
318   }
319   /* Make sure we're working with vectors and uniform vector constants.  */
320   (if (VECTOR_TYPE_P (type)
321        && tree_fits_uhwi_p (rshift_cst)
322        && tree_fits_uhwi_p (mult_cst)
323        && tree_fits_uhwi_p (bit_and_cst))
324    /* Compute what constants would be needed for this to represent a packed
325       comparison based on the shift amount denoted by RSHIFT_CST.  */
326    (with {
327      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
328      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
329      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
330      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
331      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
332      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
333      mult_i = tree_to_uhwi (mult_cst);
334      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
335      bit_and_i = tree_to_uhwi (bit_and_cst);
336      target_bit_and_i = 0;
338      /* The bit pattern in BIT_AND_I should be a mask for the least
339         significant bit of each packed element that is CMP_BITS wide.  */
340      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
341        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
342     }
343     (if ((exact_log2 (cmp_bits_i)) >= 0
344          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
345          && multiple_p (vec_bits, cmp_bits_i)
346          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
347          && target_mult_i == mult_i
348          && target_bit_and_i == bit_and_i)
349      /* Compute the vector shape for the comparison and check if the target is
350         able to expand the comparison with that type.  */
351      (with {
352        /* We're doing a signed comparison.  */
353        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
354        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
355        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
356        tree vec_truth_type = truth_type_for (vec_cmp_type);
357        tree zeros = build_zero_cst (vec_cmp_type);
358        tree ones = build_all_ones_cst (vec_cmp_type);
359       }
360       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
361            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
362        (view_convert:type (vec_cond (lt:vec_truth_type
363                                      (view_convert:vec_cmp_type @0)
364                                      { zeros; })
365                            { ones; } { zeros; })))))))))
367 (for cmp (gt ge lt le)
368      outp (convert convert negate negate)
369      outn (negate negate convert convert)
370  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
371  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
372  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
373  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
374  (simplify
375   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
376   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
377    (outp (abs @0))))
378  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
379  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
380  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
381  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
382  (simplify
383   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
384   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
385    (outn (abs @0)))))
387 /* Transform X * copysign (1.0, X) into abs(X). */
388 (simplify
389  (mult:c @0 (COPYSIGN_ALL real_onep @0))
390  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
391   (abs @0)))
393 /* Transform X * copysign (1.0, -X) into -abs(X). */
394 (simplify
395  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
396  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
397   (negate (abs @0))))
399 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
400 (simplify
401  (COPYSIGN_ALL REAL_CST@0 @1)
402  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
403   (COPYSIGN_ALL (negate @0) @1)))
405 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
406    tree-ssa-math-opts.cc does the corresponding optimization for
407    unconditional multiplications (via xorsign).  */
408 (simplify
409  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
410  (with { tree signs = sign_mask_for (type); }
411   (if (signs)
412    (with { tree inttype = TREE_TYPE (signs); }
413     (view_convert:type
414      (IFN_COND_XOR:inttype @0
415       (view_convert:inttype @1)
416       (bit_and (view_convert:inttype @2) { signs; })
417       (view_convert:inttype @3)))))))
419 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
420 (simplify
421   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
422   (abs @0))
424 /* X * 1, X / 1 -> X.  */
425 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
426   (simplify
427     (op @0 integer_onep)
428     (non_lvalue @0)))
430 /* (A / (1 << B)) -> (A >> B).
431    Only for unsigned A.  For signed A, this would not preserve rounding
432    toward zero.
433    For example: (-1 / ( 1 << B)) !=  -1 >> B.
434    Also handle widening conversions, like:
435    (A / (unsigned long long) (1U << B)) -> (A >> B)
436    or
437    (A / (unsigned long long) (1 << B)) -> (A >> B).
438    If the left shift is signed, it can be done only if the upper bits
439    of A starting from shift's type sign bit are zero, as
440    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
441    so it is valid only if A >> 31 is zero.  */
442 (simplify
443  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
444  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
445       && (!VECTOR_TYPE_P (type)
446           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
447           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
448       && (useless_type_conversion_p (type, TREE_TYPE (@1))
449           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
450               && (TYPE_UNSIGNED (TREE_TYPE (@1))
451                   || (element_precision (type)
452                       == element_precision (TREE_TYPE (@1)))
453                   || (INTEGRAL_TYPE_P (type)
454                       && (tree_nonzero_bits (@0)
455                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
456                                       true,
457                                       element_precision (type))) == 0)))))
458    (if (!VECTOR_TYPE_P (type)
459         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
460         && element_precision (TREE_TYPE (@3)) < element_precision (type))
461     (convert (rshift @3 @2))
462     (rshift @0 @2))))
464 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
465    undefined behavior in constexpr evaluation, and assuming that the division
466    traps enables better optimizations than these anyway.  */
467 (for div (trunc_div ceil_div floor_div round_div exact_div)
468  /* 0 / X is always zero.  */
469  (simplify
470   (div integer_zerop@0 @1)
471   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
472   (if (!integer_zerop (@1))
473    @0))
474  /* X / -1 is -X.  */
475  (simplify
476   (div @0 integer_minus_onep@1)
477   (if (!TYPE_UNSIGNED (type))
478    (negate @0)))
479  /* X / bool_range_Y is X.  */ 
480  (simplify
481   (div @0 SSA_NAME@1)
482   (if (INTEGRAL_TYPE_P (type)
483        && ssa_name_has_boolean_range (@1)
484        && !flag_non_call_exceptions)
485    @0))
486  /* X / X is one.  */
487  (simplify
488   (div @0 @0)
489   /* But not for 0 / 0 so that we can get the proper warnings and errors.
490      And not for _Fract types where we can't build 1.  */
491   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
492        && !integer_zerop (@0)
493        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
494    { build_one_cst (type); }))
495  /* X / abs (X) is X < 0 ? -1 : 1.  */
496  (simplify
497    (div:C @0 (abs @0))
498    (if (INTEGRAL_TYPE_P (type)
499         && TYPE_OVERFLOW_UNDEFINED (type)
500         && !integer_zerop (@0)
501         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
502     (cond (lt @0 { build_zero_cst (type); })
503           { build_minus_one_cst (type); } { build_one_cst (type); })))
504  /* X / -X is -1.  */
505  (simplify
506    (div:C @0 (negate @0))
507    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
508         && TYPE_OVERFLOW_UNDEFINED (type)
509         && !integer_zerop (@0)
510         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511     { build_minus_one_cst (type); })))
513 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
514    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
515    for MOD instead of DIV.  */
516 (for floor_divmod (floor_div floor_mod)
517      trunc_divmod (trunc_div trunc_mod)
518  (simplify
519   (floor_divmod @0 @1)
520   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
521        && TYPE_UNSIGNED (type))
522    (trunc_divmod @0 @1))))
524 /* 1 / X -> X == 1 for unsigned integer X.
525    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
526    But not for 1 / 0 so that we can get proper warnings and errors,
527    and not for 1-bit integers as they are edge cases better handled
528    elsewhere.  */
529 (simplify
530  (trunc_div integer_onep@0 @1)
531  (if (INTEGRAL_TYPE_P (type)
532       && TYPE_PRECISION (type) > 1
533       && !integer_zerop (@1)
534       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
535   (if (TYPE_UNSIGNED (type))
536    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
537    (with { tree utype = unsigned_type_for (type); }
538     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
539               { build_int_cst (utype, 2); })
540      @1 { build_zero_cst (type); })))))
542 /* Combine two successive divisions.  Note that combining ceil_div
543    and floor_div is trickier and combining round_div even more so.  */
544 (for div (trunc_div exact_div)
545  (simplify
546   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
547   (with {
548     wi::overflow_type overflow;
549     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
550                             TYPE_SIGN (type), &overflow);
551    }
552    (if (div == EXACT_DIV_EXPR
553         || optimize_successive_divisions_p (@2, @3))
554     (if (!overflow)
555      (div @0 { wide_int_to_tree (type, mul); })
556      (if (TYPE_UNSIGNED (type)
557           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
558       { build_zero_cst (type); }))))))
560 /* Combine successive multiplications.  Similar to above, but handling
561    overflow is different.  */
562 (simplify
563  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
564  (with {
565    wi::overflow_type overflow;
566    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567                            TYPE_SIGN (type), &overflow);
568   }
569   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
570      otherwise undefined overflow implies that @0 must be zero.  */
571   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
572    (mult @0 { wide_int_to_tree (type, mul); }))))
574 /* Similar to above, but there could be an extra add/sub between
575    successive multuiplications.  */
576 (simplify
577  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
578  (with {
579    bool overflowed = true;
580    wi::overflow_type ovf1, ovf2;
581    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
582                            TYPE_SIGN (type), &ovf1);
583    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
584                            TYPE_SIGN (type), &ovf2);
585   if (TYPE_OVERFLOW_UNDEFINED (type))
586     {
587 #if GIMPLE
588       value_range vr0;
589       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
590           && get_global_range_query ()->range_of_expr (vr0, @4)
591           && !vr0.varying_p () && !vr0.undefined_p ())
592         {
593           wide_int wmin0 = vr0.lower_bound ();
594           wide_int wmax0 = vr0.upper_bound ();
595           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
596           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
597           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
598             {
599               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
600               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
601               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
602                 overflowed = false;
603             }
604         }
605 #endif
606     }
607   else
608    overflowed = false;
610   /* Skip folding on overflow.  */
611   (if (!overflowed)
612    (plus (mult @0 { wide_int_to_tree (type, mul); })
613          { wide_int_to_tree (type, add); }))))
615 /* Similar to above, but a multiplication between successive additions.  */
616 (simplify
617  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
618  (with {
619    bool overflowed = true;
620    wi::overflow_type ovf1;
621    wi::overflow_type ovf2;
622    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
623                            TYPE_SIGN (type), &ovf1);
624    wide_int add = wi::add (mul, wi::to_wide (@3),
625                            TYPE_SIGN (type), &ovf2);
626   if (TYPE_OVERFLOW_UNDEFINED (type))
627     {
628 #if GIMPLE
629       value_range vr0;
630       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
631           && get_global_range_query ()->range_of_expr (vr0, @0)
632           && !vr0.varying_p () && !vr0.undefined_p ())
633         {
634           wide_int wmin0 = vr0.lower_bound ();
635           wide_int wmax0 = vr0.upper_bound ();
636           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
637           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
638           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
639             {
640               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
641               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
642               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
643                 overflowed = false;
644             }
645         }
646 #endif
647     }
648   else
649    overflowed = false;
651   /* Skip folding on overflow.  */
652   (if (!overflowed)
653    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
655 /* Optimize A / A to 1.0 if we don't care about
656    NaNs or Infinities.  */
657 (simplify
658  (rdiv @0 @0)
659  (if (FLOAT_TYPE_P (type)
660       && ! HONOR_NANS (type)
661       && ! HONOR_INFINITIES (type))
662   { build_one_cst (type); }))
664 /* Optimize -A / A to -1.0 if we don't care about
665    NaNs or Infinities.  */
666 (simplify
667  (rdiv:C @0 (negate @0))
668  (if (FLOAT_TYPE_P (type)
669       && ! HONOR_NANS (type)
670       && ! HONOR_INFINITIES (type))
671   { build_minus_one_cst (type); }))
673 /* PR71078: x / abs(x) -> copysign (1.0, x) */
674 (simplify
675  (rdiv:C (convert? @0) (convert? (abs @0)))
676   (if (SCALAR_FLOAT_TYPE_P (type)
677        && ! HONOR_NANS (type)
678        && ! HONOR_INFINITIES (type))
679    (switch
680     (if (types_match (type, float_type_node))
681      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
682     (if (types_match (type, double_type_node))
683      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
684     (if (types_match (type, long_double_type_node))
685      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
687 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
688 (simplify
689  (rdiv @0 real_onep)
690  (if (!tree_expr_maybe_signaling_nan_p (@0))
691   (non_lvalue @0)))
693 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
694 (simplify
695  (rdiv @0 real_minus_onep)
696  (if (!tree_expr_maybe_signaling_nan_p (@0))
697   (negate @0)))
699 (if (flag_reciprocal_math)
700  /* Convert (A/B)/C to A/(B*C). */
701  (simplify
702   (rdiv (rdiv:s @0 @1) @2)
703   (rdiv @0 (mult @1 @2)))
705  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
706  (simplify
707   (rdiv @0 (mult:s @1 REAL_CST@2))
708   (with
709    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
710    (if (tem)
711     (rdiv (mult @0 { tem; } ) @1))))
713  /* Convert A/(B/C) to (A/B)*C  */
714  (simplify
715   (rdiv @0 (rdiv:s @1 @2))
716    (mult (rdiv @0 @1) @2)))
718 /* Simplify x / (- y) to -x / y.  */
719 (simplify
720  (rdiv @0 (negate @1))
721  (rdiv (negate @0) @1))
723 (if (flag_unsafe_math_optimizations)
724  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
725     Since C / x may underflow to zero, do this only for unsafe math.  */
726  (for op (lt le gt ge)
727       neg_op (gt ge lt le)
728   (simplify
729    (op (rdiv REAL_CST@0 @1) real_zerop@2)
730    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
731     (switch
732      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
733       (op @1 @2))
734      /* For C < 0, use the inverted operator.  */
735      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
736       (neg_op @1 @2)))))))
738 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
739 (for div (trunc_div ceil_div floor_div round_div exact_div)
740  (simplify
741   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
742   (if (integer_pow2p (@2)
743        && tree_int_cst_sgn (@2) > 0
744        && tree_nop_conversion_p (type, TREE_TYPE (@0))
745        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
746    (rshift (convert @0)
747            { build_int_cst (integer_type_node,
748                             wi::exact_log2 (wi::to_wide (@2))); }))))
750 /* If ARG1 is a constant, we can convert this to a multiply by the
751    reciprocal.  This does not have the same rounding properties,
752    so only do this if -freciprocal-math.  We can actually
753    always safely do it if ARG1 is a power of two, but it's hard to
754    tell if it is or not in a portable manner.  */
755 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
756  (simplify
757   (rdiv @0 cst@1)
758   (if (optimize)
759    (if (flag_reciprocal_math
760         && !real_zerop (@1))
761     (with
762      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
763      (if (tem)
764       (mult @0 { tem; } )))
765     (if (cst != COMPLEX_CST)
766      (with { tree inverse = exact_inverse (type, @1); }
767       (if (inverse)
768        (mult @0 { inverse; } ))))))))
770 (for mod (ceil_mod floor_mod round_mod trunc_mod)
771  /* 0 % X is always zero.  */
772  (simplify
773   (mod integer_zerop@0 @1)
774   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
775   (if (!integer_zerop (@1))
776    @0))
777  /* X % 1 is always zero.  */
778  (simplify
779   (mod @0 integer_onep)
780   { build_zero_cst (type); })
781  /* X % -1 is zero.  */
782  (simplify
783   (mod @0 integer_minus_onep@1)
784   (if (!TYPE_UNSIGNED (type))
785    { build_zero_cst (type); }))
786  /* X % X is zero.  */
787  (simplify
788   (mod @0 @0)
789   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
790   (if (!integer_zerop (@0))
791    { build_zero_cst (type); }))
792  /* (X % Y) % Y is just X % Y.  */
793  (simplify
794   (mod (mod@2 @0 @1) @1)
795   @2)
796  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
797  (simplify
798   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
799   (if (ANY_INTEGRAL_TYPE_P (type)
800        && TYPE_OVERFLOW_UNDEFINED (type)
801        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
802                              TYPE_SIGN (type)))
803    { build_zero_cst (type); }))
804  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
805     modulo and comparison, since it is simpler and equivalent.  */
806  (for cmp (eq ne)
807   (simplify
808    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
809    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
810     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
811      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
813 /* X % -C is the same as X % C.  */
814 (simplify
815  (trunc_mod @0 INTEGER_CST@1)
816   (if (TYPE_SIGN (type) == SIGNED
817        && !TREE_OVERFLOW (@1)
818        && wi::neg_p (wi::to_wide (@1))
819        && !TYPE_OVERFLOW_TRAPS (type)
820        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
821        && !sign_bit_p (@1, @1))
822    (trunc_mod @0 (negate @1))))
824 /* X % -Y is the same as X % Y.  */
825 (simplify
826  (trunc_mod @0 (convert? (negate @1)))
827  (if (INTEGRAL_TYPE_P (type)
828       && !TYPE_UNSIGNED (type)
829       && !TYPE_OVERFLOW_TRAPS (type)
830       && tree_nop_conversion_p (type, TREE_TYPE (@1))
831       /* Avoid this transformation if X might be INT_MIN or
832          Y might be -1, because we would then change valid
833          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
834       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
835           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
836                                                         (TREE_TYPE (@1))))))
837   (trunc_mod @0 (convert @1))))
839 /* X - (X / Y) * Y is the same as X % Y.  */
840 (simplify
841  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
842  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
843   (convert (trunc_mod @0 @1))))
845 /* x * (1 + y / x) - y -> x - y % x */
846 (simplify
847  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
848  (if (INTEGRAL_TYPE_P (type))
849   (minus @0 (trunc_mod @1 @0))))
851 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
852    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
853    Also optimize A % (C << N)  where C is a power of 2,
854    to A & ((C << N) - 1).
855    Also optimize "A shift (B % C)", if C is a power of 2, to
856    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
857    and assume (B % C) is nonnegative as shifts negative values would
858    be UB.  */
859 (match (power_of_two_cand @1)
860  INTEGER_CST@1)
861 (match (power_of_two_cand @1)
862  (lshift INTEGER_CST@1 @2))
863 (for mod (trunc_mod floor_mod)
864  (for shift (lshift rshift)
865   (simplify
866    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
867    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
868     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
869                                                       1); }))))))
870  (simplify
871   (mod @0 (convert? (power_of_two_cand@1 @2)))
872   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
873        /* Allow any integral conversions of the divisor, except
874           conversion from narrower signed to wider unsigned type
875           where if @1 would be negative power of two, the divisor
876           would not be a power of two.  */
877        && INTEGRAL_TYPE_P (type)
878        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
879        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
880            || TYPE_UNSIGNED (TREE_TYPE (@1))
881            || !TYPE_UNSIGNED (type))
882        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
883    (with { tree utype = TREE_TYPE (@1);
884            if (!TYPE_OVERFLOW_WRAPS (utype))
885              utype = unsigned_type_for (utype); }
886     (bit_and @0 (convert (minus (convert:utype @1)
887                                 { build_one_cst (utype); })))))))
889 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
890 (simplify
891  (trunc_div (mult @0 integer_pow2p@1) @1)
892  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
893   (bit_and @0 { wide_int_to_tree
894                 (type, wi::mask (TYPE_PRECISION (type)
895                                  - wi::exact_log2 (wi::to_wide (@1)),
896                                  false, TYPE_PRECISION (type))); })))
898 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
899 (simplify
900  (mult (trunc_div @0 integer_pow2p@1) @1)
901  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
902   (bit_and @0 (negate @1))))
904 /* Simplify (t * 2) / 2) -> t.  */
905 (for div (trunc_div ceil_div floor_div round_div exact_div)
906  (simplify
907   (div (mult:c @0 @1) @1)
908   (if (ANY_INTEGRAL_TYPE_P (type))
909    (if (TYPE_OVERFLOW_UNDEFINED (type))
910     @0
911 #if GIMPLE
912     (with
913      {
914        bool overflowed = true;
915        value_range vr0, vr1;
916        if (INTEGRAL_TYPE_P (type)
917            && get_global_range_query ()->range_of_expr (vr0, @0)
918            && get_global_range_query ()->range_of_expr (vr1, @1)
919            && !vr0.varying_p () && !vr0.undefined_p ()
920            && !vr1.varying_p () && !vr1.undefined_p ())
921          {
922            wide_int wmin0 = vr0.lower_bound ();
923            wide_int wmax0 = vr0.upper_bound ();
924            wide_int wmin1 = vr1.lower_bound ();
925            wide_int wmax1 = vr1.upper_bound ();
926            /* If the multiplication can't overflow/wrap around, then
927               it can be optimized too.  */
928            wi::overflow_type min_ovf, max_ovf;
929            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
930            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
931            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
932              {
933                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
934                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
935                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
936                  overflowed = false;
937              }
938          }
939      }
940     (if (!overflowed)
941      @0))
942 #endif
943    ))))
945 (for op (negate abs)
946  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
947  (for coss (COS COSH)
948   (simplify
949    (coss (op @0))
950     (coss @0)))
951  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
952  (for pows (POW)
953   (simplify
954    (pows (op @0) REAL_CST@1)
955    (with { HOST_WIDE_INT n; }
956     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
957      (pows @0 @1)))))
958  /* Likewise for powi.  */
959  (for pows (POWI)
960   (simplify
961    (pows (op @0) INTEGER_CST@1)
962    (if ((wi::to_wide (@1) & 1) == 0)
963     (pows @0 @1))))
964  /* Strip negate and abs from both operands of hypot.  */
965  (for hypots (HYPOT)
966   (simplify
967    (hypots (op @0) @1)
968    (hypots @0 @1))
969   (simplify
970    (hypots @0 (op @1))
971    (hypots @0 @1)))
972  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
973  (for copysigns (COPYSIGN_ALL)
974   (simplify
975    (copysigns (op @0) @1)
976    (copysigns @0 @1))))
978 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
979 (simplify
980  (mult (abs@1 @0) @1)
981  (mult @0 @0))
983 /* Convert absu(x)*absu(x) -> x*x.  */
984 (simplify
985  (mult (absu@1 @0) @1)
986  (mult (convert@2 @0) @2))
988 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
989 (for coss (COS COSH)
990      copysigns (COPYSIGN)
991  (simplify
992   (coss (copysigns @0 @1))
993    (coss @0)))
995 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
996 (for pows (POW)
997      copysigns (COPYSIGN)
998  (simplify
999   (pows (copysigns @0 @2) REAL_CST@1)
1000   (with { HOST_WIDE_INT n; }
1001    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1002     (pows @0 @1)))))
1003 /* Likewise for powi.  */
1004 (for pows (POWI)
1005      copysigns (COPYSIGN)
1006  (simplify
1007   (pows (copysigns @0 @2) INTEGER_CST@1)
1008   (if ((wi::to_wide (@1) & 1) == 0)
1009    (pows @0 @1))))
1011 (for hypots (HYPOT)
1012      copysigns (COPYSIGN)
1013  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1014  (simplify
1015   (hypots (copysigns @0 @1) @2)
1016   (hypots @0 @2))
1017  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1018  (simplify
1019   (hypots @0 (copysigns @1 @2))
1020   (hypots @0 @1)))
1022 /* copysign(x, CST) -> [-]abs (x).  */
1023 (for copysigns (COPYSIGN_ALL)
1024  (simplify
1025   (copysigns @0 REAL_CST@1)
1026   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1027    (negate (abs @0))
1028    (abs @0))))
1030 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1031 (for copysigns (COPYSIGN_ALL)
1032  (simplify
1033   (copysigns (copysigns @0 @1) @2)
1034   (copysigns @0 @2)))
1036 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1037 (for copysigns (COPYSIGN_ALL)
1038  (simplify
1039   (mult (copysigns@2 @0 @1) @2)
1040   (mult @0 @0)))
1042 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1043 (for ccoss (CCOS CCOSH)
1044  (simplify
1045   (ccoss (negate @0))
1046    (ccoss @0)))
1048 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1049 (for ops (conj negate)
1050  (for cabss (CABS)
1051   (simplify
1052    (cabss (ops @0))
1053    (cabss @0))))
1055 /* Fold (a * (1 << b)) into (a << b)  */
1056 (simplify
1057  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1058   (if (! FLOAT_TYPE_P (type)
1059        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1060    (lshift @0 @2)))
1062 /* Shifts by constants distribute over several binary operations,
1063    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1064 (for op (plus minus)
1065   (simplify
1066     (op (lshift:s @0 @1) (lshift:s @2 @1))
1067     (if (INTEGRAL_TYPE_P (type)
1068          && TYPE_OVERFLOW_WRAPS (type)
1069          && !TYPE_SATURATING (type))
1070       (lshift (op @0 @2) @1))))
1072 (for op (bit_and bit_ior bit_xor)
1073   (simplify
1074     (op (lshift:s @0 @1) (lshift:s @2 @1))
1075     (if (INTEGRAL_TYPE_P (type))
1076       (lshift (op @0 @2) @1)))
1077   (simplify
1078     (op (rshift:s @0 @1) (rshift:s @2 @1))
1079     (if (INTEGRAL_TYPE_P (type))
1080       (rshift (op @0 @2) @1))))
1082 /* Fold (1 << (C - x)) where C = precision(type) - 1
1083    into ((1 << C) >> x). */
1084 (simplify
1085  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1086   (if (INTEGRAL_TYPE_P (type)
1087        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1088        && single_use (@1))
1089    (if (TYPE_UNSIGNED (type))
1090      (rshift (lshift @0 @2) @3)
1091    (with
1092     { tree utype = unsigned_type_for (type); }
1093     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1095 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1096 (simplify
1097  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1098  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1099       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1100   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1101    (bit_and (convert @0)
1102             { wide_int_to_tree (type,
1103                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1105 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1106 (for cst (INTEGER_CST VECTOR_CST)
1107  (simplify
1108   (rshift (negate:s @0) cst@1)
1109    (if (!TYPE_UNSIGNED (type)
1110         && TYPE_OVERFLOW_UNDEFINED (type))
1111     (with { tree stype = TREE_TYPE (@1);
1112             tree bt = truth_type_for (type);
1113             tree zeros = build_zero_cst (type);
1114             tree cst = NULL_TREE; }
1115      (switch
1116       /* Handle scalar case.  */
1117       (if (INTEGRAL_TYPE_P (type)
1118            /* If we apply the rule to the scalar type before vectorization
1119               we will enforce the result of the comparison being a bool
1120               which will require an extra AND on the result that will be
1121               indistinguishable from when the user did actually want 0
1122               or 1 as the result so it can't be removed.  */
1123            && canonicalize_math_after_vectorization_p ()
1124            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1125        (negate (convert (gt @0 { zeros; }))))
1126       /* Handle vector case.  */
1127       (if (VECTOR_INTEGER_TYPE_P (type)
1128            /* First check whether the target has the same mode for vector
1129               comparison results as it's operands do.  */
1130            && TYPE_MODE (bt) == TYPE_MODE (type)
1131            /* Then check to see if the target is able to expand the comparison
1132               with the given type later on, otherwise we may ICE.  */
1133            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1134            && (cst = uniform_integer_cst_p (@1)) != NULL
1135            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1136        (view_convert (gt:bt @0 { zeros; }))))))))
1138 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1139 (simplify
1140  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1141   (if (flag_associative_math
1142        && single_use (@3))
1143    (with
1144     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1145     (if (tem)
1146      (rdiv { tem; } @1)))))
1148 /* Simplify ~X & X as zero.  */
1149 (simplify
1150  (bit_and:c (convert? @0) (convert? (bit_not @0)))
1151   { build_zero_cst (type); })
1153 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1154 (simplify
1155   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1156   (if (TYPE_UNSIGNED (type))
1157     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1159 (for bitop (bit_and bit_ior)
1160      cmp (eq ne)
1161  /* PR35691: Transform
1162     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1163     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1164  (simplify
1165   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1166    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1167         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1168         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1169     (cmp (bit_ior @0 (convert @1)) @2)))
1170  /* Transform:
1171     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1172     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1173  (simplify
1174   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1175    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1176         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1177         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1178     (cmp (bit_and @0 (convert @1)) @2))))
1180 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1181 (simplify
1182  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1183   (minus (bit_xor @0 @1) @1))
1184 (simplify
1185  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1186  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1187   (minus (bit_xor @0 @1) @1)))
1189 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1190 (simplify
1191  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1192   (minus @1 (bit_xor @0 @1)))
1194 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1195 (for op (bit_ior bit_xor plus)
1196  (simplify
1197   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1198    (bit_xor @0 @1))
1199  (simplify
1200   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1201   (if (~wi::to_wide (@2) == wi::to_wide (@1))
1202    (bit_xor @0 @1))))
1204 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1205 (simplify
1206   (bit_ior:c (bit_xor:c @0 @1) @0)
1207   (bit_ior @0 @1))
1209 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1210 (simplify
1211  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1212  @2)
1214 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1215 (simplify
1216  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1217  (bit_not (bit_and @0 @1)))
1219 /* (~a & b) ^ a  -->   (a | b)   */
1220 (simplify
1221  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1222  (bit_ior @0 @1))
1224 /* (a | b) & ~(a ^ b)  -->  a & b  */
1225 (simplify
1226  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1227  (bit_and @0 @1))
1229 /* a | ~(a ^ b)  -->  a | ~b  */
1230 (simplify
1231  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1232  (bit_ior @0 (bit_not @1)))
1234 /* (a | b) | (a &^ b)  -->  a | b  */
1235 (for op (bit_and bit_xor)
1236  (simplify
1237   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1238   @2))
1240 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1241 (simplify
1242  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1243  @2)
1245 /* ~(~a & b)  -->  a | ~b  */
1246 (simplify
1247  (bit_not (bit_and:cs (bit_not @0) @1))
1248  (bit_ior @0 (bit_not @1)))
1250 /* ~(~a | b) --> a & ~b */
1251 (simplify
1252  (bit_not (bit_ior:cs (bit_not @0) @1))
1253  (bit_and @0 (bit_not @1)))
1255 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1256 (simplify
1257  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1258  (bit_and @3 (bit_not @2)))
1260 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1261 (simplify
1262  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1263  (bit_ior @3 @2))
1265 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1266 (simplify
1267  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1268   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1270 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1271 (simplify
1272  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1273   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1275 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1276 (simplify
1277  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1278  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1279       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1280   (bit_xor @0 @1)))
1282 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1283    ((A & N) + B) & M -> (A + B) & M
1284    Similarly if (N & M) == 0,
1285    ((A | N) + B) & M -> (A + B) & M
1286    and for - instead of + (or unary - instead of +)
1287    and/or ^ instead of |.
1288    If B is constant and (B & M) == 0, fold into A & M.  */
1289 (for op (plus minus)
1290  (for bitop (bit_and bit_ior bit_xor)
1291   (simplify
1292    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1293     (with
1294      { tree pmop[2];
1295        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1296                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1297                                        NULL_TREE, pmop); }
1298      (if (utype)
1299       (convert (bit_and (op (convert:utype { pmop[0]; })
1300                             (convert:utype { pmop[1]; }))
1301                         (convert:utype @2))))))
1302   (simplify
1303    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1304     (with
1305      { tree pmop[2];
1306        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1307                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1308                                        @4, pmop); }
1309      (if (utype)
1310       (convert (bit_and (op (convert:utype { pmop[0]; })
1311                             (convert:utype { pmop[1]; }))
1312                         (convert:utype @2)))))))
1313  (simplify
1314   (bit_and (op:s @0 @1) INTEGER_CST@2)
1315    (with
1316     { tree pmop[2];
1317       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1318                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1319                                       NULL_TREE, NULL_TREE, pmop); }
1320     (if (utype)
1321      (convert (bit_and (op (convert:utype { pmop[0]; })
1322                            (convert:utype { pmop[1]; }))
1323                        (convert:utype @2)))))))
1324 (for bitop (bit_and bit_ior bit_xor)
1325  (simplify
1326   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1327    (with
1328     { tree pmop[2];
1329       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1330                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1331                                       NULL_TREE, NULL_TREE, pmop); }
1332     (if (utype)
1333      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1334                        (convert:utype @1)))))))
1336 /* X % Y is smaller than Y.  */
1337 (for cmp (lt ge)
1338  (simplify
1339   (cmp (trunc_mod @0 @1) @1)
1340   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1341    { constant_boolean_node (cmp == LT_EXPR, type); })))
1342 (for cmp (gt le)
1343  (simplify
1344   (cmp @1 (trunc_mod @0 @1))
1345   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1346    { constant_boolean_node (cmp == GT_EXPR, type); })))
1348 /* x | ~0 -> ~0  */
1349 (simplify
1350  (bit_ior @0 integer_all_onesp@1)
1351  @1)
1353 /* x | 0 -> x  */
1354 (simplify
1355  (bit_ior @0 integer_zerop)
1356  @0)
1358 /* x & 0 -> 0  */
1359 (simplify
1360  (bit_and @0 integer_zerop@1)
1361  @1)
1363 /* ~x | x -> -1 */
1364 /* ~x ^ x -> -1 */
1365 (for op (bit_ior bit_xor)
1366  (simplify
1367   (op:c (convert? @0) (convert? (bit_not @0)))
1368   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1370 /* x ^ x -> 0 */
1371 (simplify
1372   (bit_xor @0 @0)
1373   { build_zero_cst (type); })
1375 /* Canonicalize X ^ ~0 to ~X.  */
1376 (simplify
1377   (bit_xor @0 integer_all_onesp@1)
1378   (bit_not @0))
1380 /* x & ~0 -> x  */
1381 (simplify
1382  (bit_and @0 integer_all_onesp)
1383   (non_lvalue @0))
1385 /* x & x -> x,  x | x -> x  */
1386 (for bitop (bit_and bit_ior)
1387  (simplify
1388   (bitop @0 @0)
1389   (non_lvalue @0)))
1391 /* x & C -> x if we know that x & ~C == 0.  */
1392 #if GIMPLE
1393 (simplify
1394  (bit_and SSA_NAME@0 INTEGER_CST@1)
1395  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1396       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1397   @0))
1398 #endif
1400 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1401 (simplify
1402  (bit_not (minus (bit_not @0) @1))
1403  (plus @0 @1))
1404 (simplify
1405  (bit_not (plus:c (bit_not @0) @1))
1406  (minus @0 @1))
1407 /* (~X - ~Y) -> Y - X.  */
1408 (simplify
1409  (minus (bit_not @0) (bit_not @1))
1410   (if (!TYPE_OVERFLOW_SANITIZED (type))
1411    (with { tree utype = unsigned_type_for (type); }
1412     (convert (minus (convert:utype @1) (convert:utype @0))))))
1414 /* ~(X - Y) -> ~X + Y.  */
1415 (simplify
1416  (bit_not (minus:s @0 @1))
1417  (plus (bit_not @0) @1))
1418 (simplify
1419  (bit_not (plus:s @0 INTEGER_CST@1))
1420  (if ((INTEGRAL_TYPE_P (type)
1421        && TYPE_UNSIGNED (type))
1422       || (!TYPE_OVERFLOW_SANITIZED (type)
1423           && may_negate_without_overflow_p (@1)))
1424   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1426 #if GIMPLE
1427 /* ~X + Y -> (Y - X) - 1.  */
1428 (simplify
1429  (plus:c (bit_not @0) @1)
1430   (if (ANY_INTEGRAL_TYPE_P (type)
1431        && TYPE_OVERFLOW_WRAPS (type)
1432        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1433        && !integer_all_onesp (@1))
1434    (plus (minus @1 @0) { build_minus_one_cst (type); })
1435    (if (INTEGRAL_TYPE_P (type)
1436         && TREE_CODE (@1) == INTEGER_CST
1437         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1438                                               SIGNED))
1439     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1440 #endif
1442 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1443 (simplify
1444  (bit_not (rshift:s @0 @1))
1445   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1446    (rshift (bit_not! @0) @1)
1447    /* For logical right shifts, this is possible only if @0 doesn't
1448       have MSB set and the logical right shift is changed into
1449       arithmetic shift.  */
1450    (if (INTEGRAL_TYPE_P (type)
1451         && !wi::neg_p (tree_nonzero_bits (@0)))
1452     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1453      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1455 /* x + (x & 1) -> (x + 1) & ~1 */
1456 (simplify
1457  (plus:c @0 (bit_and:s @0 integer_onep@1))
1458  (bit_and (plus @0 @1) (bit_not @1)))
1460 /* x & ~(x & y) -> x & ~y */
1461 /* x | ~(x | y) -> x | ~y  */
1462 (for bitop (bit_and bit_ior)
1463  (simplify
1464   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1465   (bitop @0 (bit_not @1))))
1467 /* (~x & y) | ~(x | y) -> ~x */
1468 (simplify
1469  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1470  @2)
1472 /* (x | y) ^ (x | ~y) -> ~x */
1473 (simplify
1474  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1475  (bit_not @0))
1477 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1478 (simplify
1479  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1480  (bit_not (bit_xor @0 @1)))
1482 /* (~x | y) ^ (x ^ y) -> x | ~y */
1483 (simplify
1484  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1485  (bit_ior @0 (bit_not @1)))
1487 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1488 (simplify
1489  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1490  (bit_not (bit_and @0 @1)))
1492 /* (x | y) & ~x -> y & ~x */
1493 /* (x & y) | ~x -> y | ~x */
1494 (for bitop (bit_and bit_ior)
1495      rbitop (bit_ior bit_and)
1496  (simplify
1497   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1498   (bitop @1 @2)))
1500 /* (x & y) ^ (x | y) -> x ^ y */
1501 (simplify
1502  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1503  (bit_xor @0 @1))
1505 /* (x ^ y) ^ (x | y) -> x & y */
1506 (simplify
1507  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1508  (bit_and @0 @1))
1510 /* (x & y) + (x ^ y) -> x | y */
1511 /* (x & y) | (x ^ y) -> x | y */
1512 /* (x & y) ^ (x ^ y) -> x | y */
1513 (for op (plus bit_ior bit_xor)
1514  (simplify
1515   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1516   (bit_ior @0 @1)))
1518 /* (x & y) + (x | y) -> x + y */
1519 (simplify
1520  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1521  (plus @0 @1))
1523 /* (x + y) - (x | y) -> x & y */
1524 (simplify
1525  (minus (plus @0 @1) (bit_ior @0 @1))
1526  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1527       && !TYPE_SATURATING (type))
1528   (bit_and @0 @1)))
1530 /* (x + y) - (x & y) -> x | y */
1531 (simplify
1532  (minus (plus @0 @1) (bit_and @0 @1))
1533  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1534       && !TYPE_SATURATING (type))
1535   (bit_ior @0 @1)))
1537 /* (x | y) - y -> (x & ~y) */
1538 (simplify
1539  (minus (bit_ior:cs @0 @1) @1)
1540  (bit_and @0 (bit_not @1)))
1542 /* (x | y) - (x ^ y) -> x & y */
1543 (simplify
1544  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1545  (bit_and @0 @1))
1547 /* (x | y) - (x & y) -> x ^ y */
1548 (simplify
1549  (minus (bit_ior @0 @1) (bit_and @0 @1))
1550  (bit_xor @0 @1))
1552 /* (x | y) & ~(x & y) -> x ^ y */
1553 (simplify
1554  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1555  (bit_xor @0 @1))
1557 /* (x | y) & (~x ^ y) -> x & y */
1558 (simplify
1559  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1560  (bit_and @0 @1))
1562 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1563 (simplify
1564  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1565  (bit_not (bit_xor @0 @1)))
1567 /* (~x | y) ^ (x | ~y) -> x ^ y */
1568 (simplify
1569  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1570  (bit_xor @0 @1))
1572 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1573 (simplify
1574  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1575                               (nop_convert2? (bit_ior @0 @1))))
1576        integer_all_onesp)
1577  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1578       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1579       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1580       && !TYPE_SATURATING (TREE_TYPE (@2)))
1581  (bit_not (convert (bit_xor @0 @1)))))
1582 (simplify
1583  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1584                                integer_all_onesp))
1585        (nop_convert3? (bit_ior @0 @1)))
1586  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1587       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1588       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1589       && !TYPE_SATURATING (TREE_TYPE (@2)))
1590  (bit_not (convert (bit_xor @0 @1)))))
1591 (simplify
1592  (minus (nop_convert1? (bit_and @0 @1))
1593        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1594                                integer_onep)))
1595  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1596       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1597       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1598       && !TYPE_SATURATING (TREE_TYPE (@2)))
1599  (bit_not (convert (bit_xor @0 @1)))))
1601 /* ~x & ~y -> ~(x | y)
1602    ~x | ~y -> ~(x & y) */
1603 (for op (bit_and bit_ior)
1604      rop (bit_ior bit_and)
1605  (simplify
1606   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1607   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1608        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1609    (bit_not (rop (convert @0) (convert @1))))))
1611 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1612    with a constant, and the two constants have no bits in common,
1613    we should treat this as a BIT_IOR_EXPR since this may produce more
1614    simplifications.  */
1615 (for op (bit_xor plus)
1616  (simplify
1617   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1618       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1619   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1620        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1621        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1622    (bit_ior (convert @4) (convert @5)))))
1624 /* (X | Y) ^ X -> Y & ~ X*/
1625 (simplify
1626  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1627  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1628   (convert (bit_and @1 (bit_not @0)))))
1630 /* Convert ~X ^ ~Y to X ^ Y.  */
1631 (simplify
1632  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1633  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1634       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1635   (bit_xor (convert @0) (convert @1))))
1637 /* Convert ~X ^ C to X ^ ~C.  */
1638 (simplify
1639  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1640  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1641   (bit_xor (convert @0) (bit_not @1))))
1643 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1644 (for opo (bit_and bit_xor)
1645      opi (bit_xor bit_and)
1646  (simplify
1647   (opo:c (opi:cs @0 @1) @1)
1648   (bit_and (bit_not @0) @1)))
1650 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1651    operands are another bit-wise operation with a common input.  If so,
1652    distribute the bit operations to save an operation and possibly two if
1653    constants are involved.  For example, convert
1654      (A | B) & (A | C) into A | (B & C)
1655    Further simplification will occur if B and C are constants.  */
1656 (for op (bit_and bit_ior bit_xor)
1657      rop (bit_ior bit_and bit_and)
1658  (simplify
1659   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1660   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1661        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1662    (rop (convert @0) (op (convert @1) (convert @2))))))
1664 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1665 /* (X & Y) & Y -> X & Y
1666    (X | Y) | Y -> X | Y  */
1667 (for op (bit_and bit_ior)
1668  (simplify
1669   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1670   @2))
1671 /* (X ^ Y) ^ Y -> X  */
1672 (simplify
1673  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1674  (convert @0))
1675 /* (X & Y) & (X & Z) -> (X & Y) & Z
1676    (X | Y) | (X | Z) -> (X | Y) | Z  */
1677 (for op (bit_and bit_ior)
1678  (simplify
1679   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1680   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1681        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1682    (if (single_use (@5) && single_use (@6))
1683     (op @3 (convert @2))
1684     (if (single_use (@3) && single_use (@4))
1685      (op (convert @1) @5))))))
1686 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1687 (simplify
1688  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1689  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1690       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1691   (bit_xor (convert @1) (convert @2))))
1693 /* Convert abs (abs (X)) into abs (X).
1694    also absu (absu (X)) into absu (X).  */
1695 (simplify
1696  (abs (abs@1 @0))
1697  @1)
1699 (simplify
1700  (absu (convert@2 (absu@1 @0)))
1701  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1702   @1))
1704 /* Convert abs[u] (-X) -> abs[u] (X).  */
1705 (simplify
1706  (abs (negate @0))
1707  (abs @0))
1709 (simplify
1710  (absu (negate @0))
1711  (absu @0))
1713 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1714 (simplify
1715  (abs tree_expr_nonnegative_p@0)
1716  @0)
1718 (simplify
1719  (absu tree_expr_nonnegative_p@0)
1720  (convert @0))
1722 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1723 (simplify
1724  (mult:c (nop_convert1?
1725           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1726                     integer_onep))
1727          (nop_convert3? @0))
1728  (if (INTEGRAL_TYPE_P (type)
1729       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1730       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1731   (if (TYPE_UNSIGNED (type))
1732    (absu @0)
1733    (abs @0)
1734   )
1738 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1739 (match negate_expr_p
1740  INTEGER_CST
1741  (if ((INTEGRAL_TYPE_P (type)
1742        && TYPE_UNSIGNED (type))
1743       || (!TYPE_OVERFLOW_SANITIZED (type)
1744           && may_negate_without_overflow_p (t)))))
1745 (match negate_expr_p
1746  FIXED_CST)
1747 (match negate_expr_p
1748  (negate @0)
1749  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1750 (match negate_expr_p
1751  REAL_CST
1752  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1753 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1754    ways.  */
1755 (match negate_expr_p
1756  VECTOR_CST
1757  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1758 (match negate_expr_p
1759  (minus @0 @1)
1760  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1761       || (FLOAT_TYPE_P (type)
1762           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1763           && !HONOR_SIGNED_ZEROS (type)))))
1765 /* (-A) * (-B) -> A * B  */
1766 (simplify
1767  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1768   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1769        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1770    (mult (convert @0) (convert (negate @1)))))
1772 /* -(A + B) -> (-B) - A.  */
1773 (simplify
1774  (negate (plus:c @0 negate_expr_p@1))
1775  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1776       && !HONOR_SIGNED_ZEROS (type))
1777   (minus (negate @1) @0)))
1779 /* -(A - B) -> B - A.  */
1780 (simplify
1781  (negate (minus @0 @1))
1782  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1783       || (FLOAT_TYPE_P (type)
1784           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1785           && !HONOR_SIGNED_ZEROS (type)))
1786   (minus @1 @0)))
1787 (simplify
1788  (negate (pointer_diff @0 @1))
1789  (if (TYPE_OVERFLOW_UNDEFINED (type))
1790   (pointer_diff @1 @0)))
1792 /* A - B -> A + (-B) if B is easily negatable.  */
1793 (simplify
1794  (minus @0 negate_expr_p@1)
1795  (if (!FIXED_POINT_TYPE_P (type))
1796  (plus @0 (negate @1))))
1798 /* 1 - a is a ^ 1 if a had a bool range. */
1799 /* This is only enabled for gimple as sometimes
1800    cfun is not set for the function which contains
1801    the SSA_NAME (e.g. while IPA passes are happening,
1802    fold might be called).  */
1803 (simplify
1804  (minus integer_onep@0 SSA_NAME@1)
1805   (if (INTEGRAL_TYPE_P (type)
1806        && ssa_name_has_boolean_range (@1))
1807    (bit_xor @1 @0)))
1809 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1810 (simplify
1811  (negate (mult:c@0 @1 negate_expr_p@2))
1812  (if (! TYPE_UNSIGNED (type)
1813       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1814       && single_use (@0))
1815   (mult @1 (negate @2))))
1817 (simplify
1818  (negate (rdiv@0 @1 negate_expr_p@2))
1819  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1820       && single_use (@0))
1821   (rdiv @1 (negate @2))))
1823 (simplify
1824  (negate (rdiv@0 negate_expr_p@1 @2))
1825  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1826       && single_use (@0))
1827   (rdiv (negate @1) @2)))
1829 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1830 (simplify
1831  (negate (convert? (rshift @0 INTEGER_CST@1)))
1832  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1833       && wi::to_wide (@1) == element_precision (type) - 1)
1834   (with { tree stype = TREE_TYPE (@0);
1835           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1836                                              : unsigned_type_for (stype); }
1837    (if (VECTOR_TYPE_P (type))
1838     (view_convert (rshift (view_convert:ntype @0) @1))
1839     (convert (rshift (convert:ntype @0) @1))))))
1841 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1842    when profitable.
1843    For bitwise binary operations apply operand conversions to the
1844    binary operation result instead of to the operands.  This allows
1845    to combine successive conversions and bitwise binary operations.
1846    We combine the above two cases by using a conditional convert.  */
1847 (for bitop (bit_and bit_ior bit_xor)
1848  (simplify
1849   (bitop (convert@2 @0) (convert?@3 @1))
1850   (if (((TREE_CODE (@1) == INTEGER_CST
1851          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1852          && (int_fits_type_p (@1, TREE_TYPE (@0))
1853              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1854         || types_match (@0, @1))
1855        && !POINTER_TYPE_P (TREE_TYPE (@0))
1856        && !VECTOR_TYPE_P (TREE_TYPE (@0))
1857        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
1858        /* ???  This transform conflicts with fold-const.cc doing
1859           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1860           constants (if x has signed type, the sign bit cannot be set
1861           in c).  This folds extension into the BIT_AND_EXPR.
1862           Restrict it to GIMPLE to avoid endless recursions.  */
1863        && (bitop != BIT_AND_EXPR || GIMPLE)
1864        && (/* That's a good idea if the conversion widens the operand, thus
1865               after hoisting the conversion the operation will be narrower.
1866               It is also a good if the conversion is a nop as moves the
1867               conversion to one side; allowing for combining of the conversions.  */
1868            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1869            /* The conversion check for being a nop can only be done at the gimple
1870               level as fold_binary has some re-association code which can conflict
1871               with this if there is a "constant" which is not a full INTEGER_CST.  */
1872            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1873            /* It's also a good idea if the conversion is to a non-integer
1874               mode.  */
1875            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1876            /* Or if the precision of TO is not the same as the precision
1877               of its mode.  */
1878            || !type_has_mode_precision_p (type)
1879            /* In GIMPLE, getting rid of 2 conversions for one new results
1880               in smaller IL.  */
1881            || (GIMPLE
1882                && TREE_CODE (@1) != INTEGER_CST
1883                && tree_nop_conversion_p (type, TREE_TYPE (@0))
1884                && single_use (@2)
1885                && single_use (@3))))
1886    (convert (bitop @0 (convert @1)))))
1887  /* In GIMPLE, getting rid of 2 conversions for one new results
1888     in smaller IL.  */
1889  (simplify
1890   (convert (bitop:cs@2 (nop_convert:s @0) @1))
1891   (if (GIMPLE
1892        && TREE_CODE (@1) != INTEGER_CST
1893        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1894        && types_match (type, @0)
1895        && !POINTER_TYPE_P (TREE_TYPE (@0))
1896        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
1897    (bitop @0 (convert @1)))))
1899 (for bitop (bit_and bit_ior)
1900      rbitop (bit_ior bit_and)
1901   /* (x | y) & x -> x */
1902   /* (x & y) | x -> x */
1903  (simplify
1904   (bitop:c (rbitop:c @0 @1) @0)
1905   @0)
1906  /* (~x | y) & x -> x & y */
1907  /* (~x & y) | x -> x | y */
1908  (simplify
1909   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1910   (bitop @0 @1)))
1912 /* ((x | y) & z) | x -> (z & y) | x */
1913 (simplify
1914   (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1915   (bit_ior (bit_and @2 @1) @0))
1917 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1918 (simplify
1919   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1920   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
1922 /* Combine successive equal operations with constants.  */
1923 (for bitop (bit_and bit_ior bit_xor)
1924  (simplify
1925   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1926   (if (!CONSTANT_CLASS_P (@0))
1927    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1928       folded to a constant.  */
1929    (bitop @0 (bitop! @1 @2))
1930    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1931       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1932       the values involved are such that the operation can't be decided at
1933       compile time.  Try folding one of @0 or @1 with @2 to see whether
1934       that combination can be decided at compile time.
1936       Keep the existing form if both folds fail, to avoid endless
1937       oscillation.  */
1938    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1939     (if (cst1)
1940      (bitop @1 { cst1; })
1941      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1942       (if (cst2)
1943        (bitop @0 { cst2; }))))))))
1945 /* Try simple folding for X op !X, and X op X with the help
1946    of the truth_valued_p and logical_inverted_value predicates.  */
1947 (match truth_valued_p
1948  @0
1949  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1950 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1951  (match truth_valued_p
1952   (op @0 @1)))
1953 (match truth_valued_p
1954   (truth_not @0))
1956 (match (logical_inverted_value @0)
1957  (truth_not @0))
1958 (match (logical_inverted_value @0)
1959  (bit_not truth_valued_p@0))
1960 (match (logical_inverted_value @0)
1961  (eq @0 integer_zerop))
1962 (match (logical_inverted_value @0)
1963  (ne truth_valued_p@0 integer_truep))
1964 (match (logical_inverted_value @0)
1965  (bit_xor truth_valued_p@0 integer_truep))
1967 /* X & !X -> 0.  */
1968 (simplify
1969  (bit_and:c @0 (logical_inverted_value @0))
1970  { build_zero_cst (type); })
1971 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1972 (for op (bit_ior bit_xor)
1973  (simplify
1974   (op:c truth_valued_p@0 (logical_inverted_value @0))
1975   { constant_boolean_node (true, type); }))
1976 /* X ==/!= !X is false/true.  */
1977 (for op (eq ne)
1978  (simplify
1979   (op:c truth_valued_p@0 (logical_inverted_value @0))
1980   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1982 /* ~~x -> x */
1983 (simplify
1984   (bit_not (bit_not @0))
1985   @0)
1987 /* zero_one_valued_p will match when a value is known to be either
1988    0 or 1 including constants 0 or 1.
1989    Signed 1-bits includes -1 so they cannot match here. */
1990 (match zero_one_valued_p
1991  @0
1992  (if (INTEGRAL_TYPE_P (type)
1993       && (TYPE_UNSIGNED (type)
1994           || TYPE_PRECISION (type) > 1)
1995       && wi::leu_p (tree_nonzero_bits (@0), 1))))
1996 (match zero_one_valued_p
1997  truth_valued_p@0
1998  (if (INTEGRAL_TYPE_P (type)
1999       && (TYPE_UNSIGNED (type)
2000           || TYPE_PRECISION (type) > 1))))
2002 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2003 (simplify
2004  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2005  (if (INTEGRAL_TYPE_P (type))
2006   (bit_and @0 @1)))
2008 (for cmp (tcc_comparison)
2009      icmp (inverted_tcc_comparison)
2010  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2011  (simplify
2012   (bit_ior
2013    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2014    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2015     (if (INTEGRAL_TYPE_P (type)
2016          /* The scalar version has to be canonicalized after vectorization
2017             because it makes unconditional loads conditional ones, which
2018             means we lose vectorization because the loads may trap.  */
2019          && canonicalize_math_after_vectorization_p ())
2020      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2022  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2023     canonicalized further and we recognize the conditional form:
2024     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2025  (simplify
2026   (bit_ior
2027    (cond (cmp@0  @01 @02) @3 zerop)
2028    (cond (icmp@4 @01 @02) @5 zerop))
2029     (if (INTEGRAL_TYPE_P (type)
2030          /* The scalar version has to be canonicalized after vectorization
2031             because it makes unconditional loads conditional ones, which
2032             means we lose vectorization because the loads may trap.  */
2033          && canonicalize_math_after_vectorization_p ())
2034     (cond @0 @3 @5)))
2036  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2037     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2038  (simplify
2039   (bit_ior
2040    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2041    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2042     (if (integer_zerop (@5))
2043      (switch
2044       (if (integer_onep (@4))
2045        (bit_and (vec_cond @0 @2 @3) @4))
2046         (if (integer_minus_onep (@4))
2047          (vec_cond @0 @2 @3)))
2048     (if (integer_zerop (@4))
2049      (switch
2050       (if (integer_onep (@5))
2051        (bit_and (vec_cond @0 @3 @2) @5))
2052       (if (integer_minus_onep (@5))
2053        (vec_cond @0 @3 @2))))))
2055  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2056     into a < b ? d : c.  */
2057  (simplify
2058   (bit_ior
2059    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2060    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2061     (vec_cond @0 @2 @3)))
2063 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2064 (simplify
2065  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2066  (if (INTEGRAL_TYPE_P (type)
2067       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2068       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2069       /* Sign extending of the neg or a truncation of the neg
2070          is needed. */
2071       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2072           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2073   (mult (convert @0) @1)))
2075 /* Narrow integer multiplication by a zero_one_valued_p operand.
2076    Multiplication by [0,1] is guaranteed not to overflow.  */
2077 (simplify
2078  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2079  (if (INTEGRAL_TYPE_P (type)
2080       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2081       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2082   (mult (convert @1) (convert @2))))
2084 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2085    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2086    as some targets (such as x86's SSE) may return zero for larger C.  */
2087 (simplify
2088   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2089   (if (tree_fits_shwi_p (@1)
2090        && tree_to_shwi (@1) > 0
2091        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2092     (convert @0)))
2094 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2095    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2096    as some targets (such as x86's SSE) may return zero for larger C.  */
2097 (simplify
2098   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2099   (if (tree_fits_shwi_p (@1)
2100        && tree_to_shwi (@1) > 0
2101        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2102     (eq @0 @2)))
2104 /* Convert ~ (-A) to A - 1.  */
2105 (simplify
2106  (bit_not (convert? (negate @0)))
2107  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2108       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2109   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2111 /* Convert - (~A) to A + 1.  */
2112 (simplify
2113  (negate (nop_convert? (bit_not @0)))
2114  (plus (view_convert @0) { build_each_one_cst (type); }))
2116 /* (a & b) ^ (a == b) -> !(a | b) */
2117 /* (a & b) == (a ^ b) -> !(a | b) */
2118 (for first_op (bit_xor eq)
2119      second_op (eq bit_xor)
2120  (simplify
2121   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2122     (bit_not (bit_ior @0 @1))))
2124 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2125 (simplify
2126  (bit_not (convert? (minus @0 integer_each_onep)))
2127  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2128       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2129   (convert (negate @0))))
2130 (simplify
2131  (bit_not (convert? (plus @0 integer_all_onesp)))
2132  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2133       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2134   (convert (negate @0))))
2136 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2137 (simplify
2138  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2139  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2140   (convert (bit_xor @0 (bit_not @1)))))
2141 (simplify
2142  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2143  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2144   (convert (bit_xor @0 @1))))
2146 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2147 (simplify
2148  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2149  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2150   (bit_not (bit_xor (view_convert @0) @1))))
2152 /* ~(a ^ b) is a == b for truth valued a and b.  */
2153 (simplify
2154  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2155  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2156       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2157   (convert (eq @0 @1))))
2159 /* (~a) == b is a ^ b for truth valued a and b.  */
2160 (simplify
2161  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2162  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2163       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2164   (convert (bit_xor @0 @1))))
2166 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2167 (simplify
2168  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2169  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2171 /* Fold A - (A & B) into ~B & A.  */
2172 (simplify
2173  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2174  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2175       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2176   (convert (bit_and (bit_not @1) @0))))
2178 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2179 (if (!canonicalize_math_p ())
2180  (for cmp (tcc_comparison)
2181   (simplify
2182    (mult:c (convert (cmp@0 @1 @2)) @3)
2183    (if (INTEGRAL_TYPE_P (type)
2184         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2185      (cond @0 @3 { build_zero_cst (type); })))
2186 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2187   (simplify
2188    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2189    (if (INTEGRAL_TYPE_P (type)
2190         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2191      (cond @0 @3 { build_zero_cst (type); })))
2195 /* For integral types with undefined overflow and C != 0 fold
2196    x * C EQ/NE y * C into x EQ/NE y.  */
2197 (for cmp (eq ne)
2198  (simplify
2199   (cmp (mult:c @0 @1) (mult:c @2 @1))
2200   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2201        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2202        && tree_expr_nonzero_p (@1))
2203    (cmp @0 @2))))
2205 /* For integral types with wrapping overflow and C odd fold
2206    x * C EQ/NE y * C into x EQ/NE y.  */
2207 (for cmp (eq ne)
2208  (simplify
2209   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2210   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2211        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2212        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2213    (cmp @0 @2))))
2215 /* For integral types with undefined overflow and C != 0 fold
2216    x * C RELOP y * C into:
2218    x RELOP y for nonnegative C
2219    y RELOP x for negative C  */
2220 (for cmp (lt gt le ge)
2221  (simplify
2222   (cmp (mult:c @0 @1) (mult:c @2 @1))
2223   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2224        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2225    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2226     (cmp @0 @2)
2227    (if (TREE_CODE (@1) == INTEGER_CST
2228         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2229     (cmp @2 @0))))))
2231 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2232 (for cmp (le gt)
2233      icmp (gt le)
2234  (simplify
2235   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2236    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2237         && TYPE_UNSIGNED (TREE_TYPE (@0))
2238         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2239         && (wi::to_wide (@2)
2240             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2241     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2242      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2244 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2245 (for cmp (simple_comparison)
2246  (simplify
2247   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2248   (if (element_precision (@3) >= element_precision (@0)
2249        && types_match (@0, @1))
2250    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2251     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2252      (cmp @1 @0)
2253      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2254       (with
2255        {
2256         tree utype = unsigned_type_for (TREE_TYPE (@0));
2257        }
2258        (cmp (convert:utype @1) (convert:utype @0)))))
2259     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2260      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2261       (cmp @0 @1)
2262       (with
2263        {
2264         tree utype = unsigned_type_for (TREE_TYPE (@0));
2265        }
2266        (cmp (convert:utype @0) (convert:utype @1)))))))))
2268 /* X / C1 op C2 into a simple range test.  */
2269 (for cmp (simple_comparison)
2270  (simplify
2271   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2272   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2273        && integer_nonzerop (@1)
2274        && !TREE_OVERFLOW (@1)
2275        && !TREE_OVERFLOW (@2))
2276    (with { tree lo, hi; bool neg_overflow;
2277            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2278                                                    &neg_overflow); }
2279     (switch
2280      (if (code == LT_EXPR || code == GE_EXPR)
2281        (if (TREE_OVERFLOW (lo))
2282         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2283         (if (code == LT_EXPR)
2284          (lt @0 { lo; })
2285          (ge @0 { lo; }))))
2286      (if (code == LE_EXPR || code == GT_EXPR)
2287        (if (TREE_OVERFLOW (hi))
2288         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2289         (if (code == LE_EXPR)
2290          (le @0 { hi; })
2291          (gt @0 { hi; }))))
2292      (if (!lo && !hi)
2293       { build_int_cst (type, code == NE_EXPR); })
2294      (if (code == EQ_EXPR && !hi)
2295       (ge @0 { lo; }))
2296      (if (code == EQ_EXPR && !lo)
2297       (le @0 { hi; }))
2298      (if (code == NE_EXPR && !hi)
2299       (lt @0 { lo; }))
2300      (if (code == NE_EXPR && !lo)
2301       (gt @0 { hi; }))
2302      (if (GENERIC)
2303       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2304                            lo, hi); })
2305      (with
2306       {
2307         tree etype = range_check_type (TREE_TYPE (@0));
2308         if (etype)
2309           {
2310             hi = fold_convert (etype, hi);
2311             lo = fold_convert (etype, lo);
2312             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2313           }
2314       }
2315       (if (etype && hi && !TREE_OVERFLOW (hi))
2316        (if (code == EQ_EXPR)
2317         (le (minus (convert:etype @0) { lo; }) { hi; })
2318         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2320 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2321 (for op (lt le ge gt)
2322  (simplify
2323   (op (plus:c @0 @2) (plus:c @1 @2))
2324   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2325        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2326    (op @0 @1))))
2328 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2329    when C is an unsigned integer constant with only the MSB set, and X and
2330    Y have types of equal or lower integer conversion rank than C's.  */
2331 (for op (lt le ge gt)
2332  (simplify
2333   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2334   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2335        && TYPE_UNSIGNED (TREE_TYPE (@0))
2336        && wi::only_sign_bit_p (wi::to_wide (@0)))
2337    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2338     (op (convert:stype @1) (convert:stype @2))))))
2340 /* For equality and subtraction, this is also true with wrapping overflow.  */
2341 (for op (eq ne minus)
2342  (simplify
2343   (op (plus:c @0 @2) (plus:c @1 @2))
2344   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2345        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2346            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2347    (op @0 @1))))
2349 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2350 (for op (lt le ge gt)
2351  (simplify
2352   (op (minus @0 @2) (minus @1 @2))
2353   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2354        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2355    (op @0 @1))))
2356 /* For equality and subtraction, this is also true with wrapping overflow.  */
2357 (for op (eq ne minus)
2358  (simplify
2359   (op (minus @0 @2) (minus @1 @2))
2360   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2361        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2362            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2363    (op @0 @1))))
2364 /* And for pointers...  */
2365 (for op (simple_comparison)
2366  (simplify
2367   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2368   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2369    (op @0 @1))))
2370 (simplify
2371  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2372  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2373       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2374   (pointer_diff @0 @1)))
2376 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2377 (for op (lt le ge gt)
2378  (simplify
2379   (op (minus @2 @0) (minus @2 @1))
2380   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2381        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2382    (op @1 @0))))
2383 /* For equality and subtraction, this is also true with wrapping overflow.  */
2384 (for op (eq ne minus)
2385  (simplify
2386   (op (minus @2 @0) (minus @2 @1))
2387   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2388        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2389            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2390    (op @1 @0))))
2391 /* And for pointers...  */
2392 (for op (simple_comparison)
2393  (simplify
2394   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2395   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2396    (op @1 @0))))
2397 (simplify
2398  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2399  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2400       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2401   (pointer_diff @1 @0)))
2403 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2404 (for op (lt le gt ge)
2405  (simplify
2406   (op:c (plus:c@2 @0 @1) @1)
2407   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2408        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2409        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2410        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2411    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2412 /* For equality, this is also true with wrapping overflow.  */
2413 (for op (eq ne)
2414  (simplify
2415   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2416   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2417        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2418            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2419        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2420        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2421        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2422    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2423  (simplify
2424   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2425   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2426        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2427        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2428    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2430 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2431 (for neeq (ne eq)
2432  (simplify
2433   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2434    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2435     (if (ptr_difference_const (@0, @2, &diff))
2436      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2437  (simplify
2438   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2439    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2440     (if (ptr_difference_const (@0, @2, &diff))
2441      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2443 /* X - Y < X is the same as Y > 0 when there is no overflow.
2444    For equality, this is also true with wrapping overflow.  */
2445 (for op (simple_comparison)
2446  (simplify
2447   (op:c @0 (minus@2 @0 @1))
2448   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2449        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2450            || ((op == EQ_EXPR || op == NE_EXPR)
2451                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2452        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2453    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2455 /* Transform:
2456    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2457    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2458 (for cmp (eq ne)
2459      ocmp (lt ge)
2460  (simplify
2461   (cmp (trunc_div @0 @1) integer_zerop)
2462   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2463        /* Complex ==/!= is allowed, but not </>=.  */
2464        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2465        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2466    (ocmp @0 @1))))
2468 /* X == C - X can never be true if C is odd.  */
2469 (for cmp (eq ne)
2470  (simplify
2471   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2472   (if (TREE_INT_CST_LOW (@1) & 1)
2473    { constant_boolean_node (cmp == NE_EXPR, type); })))
2475 /* Arguments on which one can call get_nonzero_bits to get the bits
2476    possibly set.  */
2477 (match with_possible_nonzero_bits
2478  INTEGER_CST@0)
2479 (match with_possible_nonzero_bits
2480  SSA_NAME@0
2481  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2482 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2483 (match (with_possible_nonzero_bits2 @0)
2484  with_possible_nonzero_bits@0)
2485 (match (with_possible_nonzero_bits2 @0)
2486  (bit_and:c with_possible_nonzero_bits@0 @2))
2488 /* Same for bits that are known to be set, but we do not have
2489    an equivalent to get_nonzero_bits yet.  */
2490 (match (with_certain_nonzero_bits2 @0)
2491  INTEGER_CST@0)
2492 (match (with_certain_nonzero_bits2 @0)
2493  (bit_ior @1 INTEGER_CST@0))
2495 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2496 (for cmp (eq ne)
2497  (simplify
2498   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2499   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2500    { constant_boolean_node (cmp == NE_EXPR, type); })))
2502 /* ((X inner_op C0) outer_op C1)
2503    With X being a tree where value_range has reasoned certain bits to always be
2504    zero throughout its computed value range,
2505    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2506    where zero_mask has 1's for all bits that are sure to be 0 in
2507    and 0's otherwise.
2508    if (inner_op == '^') C0 &= ~C1;
2509    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2510    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2512 (for inner_op (bit_ior bit_xor)
2513      outer_op (bit_xor bit_ior)
2514 (simplify
2515  (outer_op
2516   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2517  (with
2518   {
2519     bool fail = false;
2520     wide_int zero_mask_not;
2521     wide_int C0;
2522     wide_int cst_emit;
2524     if (TREE_CODE (@2) == SSA_NAME)
2525       zero_mask_not = get_nonzero_bits (@2);
2526     else
2527       fail = true;
2529     if (inner_op == BIT_XOR_EXPR)
2530       {
2531         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2532         cst_emit = C0 | wi::to_wide (@1);
2533       }
2534     else
2535       {
2536         C0 = wi::to_wide (@0);
2537         cst_emit = C0 ^ wi::to_wide (@1);
2538       }
2539   }
2540   (if (!fail && (C0 & zero_mask_not) == 0)
2541    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2542    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2543     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2545 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2546 (simplify
2547   (pointer_plus (pointer_plus:s @0 @1) @3)
2548   (pointer_plus @0 (plus @1 @3)))
2549 #if GENERIC
2550 (simplify
2551   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2552   (convert:type (pointer_plus @0 (plus @1 @3))))
2553 #endif
2555 /* Pattern match
2556      tem1 = (long) ptr1;
2557      tem2 = (long) ptr2;
2558      tem3 = tem2 - tem1;
2559      tem4 = (unsigned long) tem3;
2560      tem5 = ptr1 + tem4;
2561    and produce
2562      tem5 = ptr2;  */
2563 (simplify
2564   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2565   /* Conditionally look through a sign-changing conversion.  */
2566   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2567        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2568             || (GENERIC && type == TREE_TYPE (@1))))
2569    @1))
2570 (simplify
2571   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2572   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2573    (convert @1)))
2575 /* Pattern match
2576      tem = (sizetype) ptr;
2577      tem = tem & algn;
2578      tem = -tem;
2579      ... = ptr p+ tem;
2580    and produce the simpler and easier to analyze with respect to alignment
2581      ... = ptr & ~algn;  */
2582 (simplify
2583   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2584   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2585    (bit_and @0 { algn; })))
2587 /* Try folding difference of addresses.  */
2588 (simplify
2589  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2590  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2591   (with { poly_int64 diff; }
2592    (if (ptr_difference_const (@0, @1, &diff))
2593     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2594 (simplify
2595  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2596  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2597   (with { poly_int64 diff; }
2598    (if (ptr_difference_const (@0, @1, &diff))
2599     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2600 (simplify
2601  (minus (convert ADDR_EXPR@0) (convert @1))
2602  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2603   (with { poly_int64 diff; }
2604    (if (ptr_difference_const (@0, @1, &diff))
2605     { build_int_cst_type (type, diff); }))))
2606 (simplify
2607  (minus (convert @0) (convert ADDR_EXPR@1))
2608  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2609   (with { poly_int64 diff; }
2610    (if (ptr_difference_const (@0, @1, &diff))
2611     { build_int_cst_type (type, diff); }))))
2612 (simplify
2613  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2614  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2615       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2616   (with { poly_int64 diff; }
2617    (if (ptr_difference_const (@0, @1, &diff))
2618     { build_int_cst_type (type, diff); }))))
2619 (simplify
2620  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2621  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2622       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2623   (with { poly_int64 diff; }
2624    (if (ptr_difference_const (@0, @1, &diff))
2625     { build_int_cst_type (type, diff); }))))
2627 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2628 (simplify
2629  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2630  (with { poly_int64 diff; }
2631    (if (ptr_difference_const (@0, @2, &diff))
2632     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2633 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2634 (simplify
2635  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2636  (with { poly_int64 diff; }
2637    (if (ptr_difference_const (@0, @2, &diff))
2638     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2639 (simplify
2640  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2641  (with { poly_int64 diff; }
2642    (if (ptr_difference_const (@0, @1, &diff))
2643     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2645 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2646 (simplify
2647  (convert (pointer_diff @0 INTEGER_CST@1))
2648  (if (POINTER_TYPE_P (type))
2649   { build_fold_addr_expr_with_type
2650       (build2 (MEM_REF, char_type_node, @0,
2651                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2652                type); }))
2654 /* If arg0 is derived from the address of an object or function, we may
2655    be able to fold this expression using the object or function's
2656    alignment.  */
2657 (simplify
2658  (bit_and (convert? @0) INTEGER_CST@1)
2659  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2660       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2661   (with
2662    {
2663      unsigned int align;
2664      unsigned HOST_WIDE_INT bitpos;
2665      get_pointer_alignment_1 (@0, &align, &bitpos);
2666    }
2667    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2668     { wide_int_to_tree (type, (wi::to_wide (@1)
2669                                & (bitpos / BITS_PER_UNIT))); }))))
2671 (match min_value
2672  INTEGER_CST
2673  (if (INTEGRAL_TYPE_P (type)
2674       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2676 (match max_value
2677  INTEGER_CST
2678  (if (INTEGRAL_TYPE_P (type)
2679       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2681 /* x >  y  &&  x != XXX_MIN  -->  x > y
2682    x >  y  &&  x == XXX_MIN  -->  false . */
2683 (for eqne (eq ne)
2684  (simplify
2685   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2686    (switch
2687     (if (eqne == EQ_EXPR)
2688      { constant_boolean_node (false, type); })
2689     (if (eqne == NE_EXPR)
2690      @2)
2691     )))
2693 /* x <  y  &&  x != XXX_MAX  -->  x < y
2694    x <  y  &&  x == XXX_MAX  -->  false.  */
2695 (for eqne (eq ne)
2696  (simplify
2697   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2698    (switch
2699     (if (eqne == EQ_EXPR)
2700      { constant_boolean_node (false, type); })
2701     (if (eqne == NE_EXPR)
2702      @2)
2703     )))
2705 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2706 (simplify
2707  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2708   @2)
2710 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2711 (simplify
2712  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2713   @2)
2715 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2716 (simplify
2717  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2718   @2)
2720 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2721 (simplify
2722  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2723   { constant_boolean_node (true, type); })
2725 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2726 (simplify
2727  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2728   @2)
2730 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2731 (simplify
2732  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2733   @2)
2735 /* x >=  y  ||  x != XXX_MAX   -->  true
2736    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2737 (for eqne (eq ne)
2738  (simplify
2739   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2740    (switch
2741     (if (eqne == EQ_EXPR)
2742      @2)
2743     (if (eqne == NE_EXPR)
2744      { constant_boolean_node (true, type); }))))
2746 /* y == XXX_MIN || x < y --> x <= y - 1 */
2747 (simplify
2748  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2749   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2750        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2751   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2753 /* y != XXX_MIN && x >= y --> x > y - 1 */
2754 (simplify
2755  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2756   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2757        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2758   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2760 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2761    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2763 (for code1 (eq ne)
2764  (for code2 (eq ne lt gt le ge)
2765   (simplify
2766    (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2767     (with
2768      {
2769       int cmp = tree_int_cst_compare (@1, @2);
2770       bool val;
2771       switch (code2)
2772          {
2773         case EQ_EXPR: val = (cmp == 0); break;
2774         case NE_EXPR: val = (cmp != 0); break;
2775         case LT_EXPR: val = (cmp < 0); break;
2776         case GT_EXPR: val = (cmp > 0); break;
2777         case LE_EXPR: val = (cmp <= 0); break;
2778         case GE_EXPR: val = (cmp >= 0); break;
2779         default: gcc_unreachable ();
2780         }
2781      }
2782      (switch
2783       (if (code1 == EQ_EXPR && val) @3)
2784       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2785       (if (code1 == NE_EXPR && !val) @4))))))
2787 /* Convert (X OP1 CST1) && (X OP2 CST2).  */
2789 (for code1 (lt le gt ge)
2790  (for code2 (lt le gt ge)
2791   (simplify
2792   (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2793    (with
2794     {
2795      int cmp = tree_int_cst_compare (@1, @2);
2796     }
2797     (switch
2798      /* Choose the more restrictive of two < or <= comparisons.  */
2799      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2800           && (code2 == LT_EXPR || code2 == LE_EXPR))
2801       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2802        @3
2803        @4))
2804      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2805      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2806           && (code2 == GT_EXPR || code2 == GE_EXPR))
2807       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2808        @3
2809        @4))
2810      /* Check for singleton ranges.  */
2811      (if (cmp == 0
2812           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2813             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2814       (eq @0 @1))
2815      /* Check for disjoint ranges.  */
2816      (if (cmp <= 0
2817           && (code1 == LT_EXPR || code1 == LE_EXPR)
2818           && (code2 == GT_EXPR || code2 == GE_EXPR))
2819       { constant_boolean_node (false, type); })
2820      (if (cmp >= 0
2821           && (code1 == GT_EXPR || code1 == GE_EXPR)
2822           && (code2 == LT_EXPR || code2 == LE_EXPR))
2823       { constant_boolean_node (false, type); })
2824      )))))
2826 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2827    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2829 (for code1 (eq ne)
2830  (for code2 (eq ne lt gt le ge)
2831   (simplify
2832    (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2833     (with
2834      {
2835       int cmp = tree_int_cst_compare (@1, @2);
2836       bool val;
2837       switch (code2)
2838         {
2839         case EQ_EXPR: val = (cmp == 0); break;
2840         case NE_EXPR: val = (cmp != 0); break;
2841         case LT_EXPR: val = (cmp < 0); break;
2842         case GT_EXPR: val = (cmp > 0); break;
2843         case LE_EXPR: val = (cmp <= 0); break;
2844         case GE_EXPR: val = (cmp >= 0); break;
2845         default: gcc_unreachable ();
2846         }
2847      }
2848      (switch
2849       (if (code1 == EQ_EXPR && val) @4)
2850       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2851       (if (code1 == NE_EXPR && !val) @3))))))
2853 /* Convert (X OP1 CST1) || (X OP2 CST2).  */
2855 (for code1 (lt le gt ge)
2856  (for code2 (lt le gt ge)
2857   (simplify
2858   (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2859    (with
2860     {
2861      int cmp = tree_int_cst_compare (@1, @2);
2862     }
2863     (switch
2864      /* Choose the more restrictive of two < or <= comparisons.  */
2865      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2866           && (code2 == LT_EXPR || code2 == LE_EXPR))
2867       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2868        @4
2869        @3))
2870      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2871      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2872           && (code2 == GT_EXPR || code2 == GE_EXPR))
2873       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2874        @4
2875        @3))
2876      /* Check for singleton ranges.  */
2877      (if (cmp == 0
2878           && ((code1 == LT_EXPR && code2 == GT_EXPR)
2879               || (code1 == GT_EXPR && code2 == LT_EXPR)))
2880       (ne @0 @2))
2881      /* Check for disjoint ranges.  */
2882      (if (cmp >= 0
2883           && (code1 == LT_EXPR || code1 == LE_EXPR)
2884           && (code2 == GT_EXPR || code2 == GE_EXPR))
2885       { constant_boolean_node (true, type); })
2886      (if (cmp <= 0
2887           && (code1 == GT_EXPR || code1 == GE_EXPR)
2888           && (code2 == LT_EXPR || code2 == LE_EXPR))
2889       { constant_boolean_node (true, type); })
2890      )))))
2892 /* We can't reassociate at all for saturating types.  */
2893 (if (!TYPE_SATURATING (type))
2895  /* Contract negates.  */
2896  /* A + (-B) -> A - B */
2897  (simplify
2898   (plus:c @0 (convert? (negate @1)))
2899   /* Apply STRIP_NOPS on the negate.  */
2900   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2901        && !TYPE_OVERFLOW_SANITIZED (type))
2902    (with
2903     {
2904      tree t1 = type;
2905      if (INTEGRAL_TYPE_P (type)
2906          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2907        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2908     }
2909     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2910  /* A - (-B) -> A + B */
2911  (simplify
2912   (minus @0 (convert? (negate @1)))
2913   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2914        && !TYPE_OVERFLOW_SANITIZED (type))
2915    (with
2916     {
2917      tree t1 = type;
2918      if (INTEGRAL_TYPE_P (type)
2919          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2920        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2921     }
2922     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2923  /* -(T)(-A) -> (T)A
2924     Sign-extension is ok except for INT_MIN, which thankfully cannot
2925     happen without overflow.  */
2926  (simplify
2927   (negate (convert (negate @1)))
2928   (if (INTEGRAL_TYPE_P (type)
2929        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2930            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2931                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2932        && !TYPE_OVERFLOW_SANITIZED (type)
2933        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2934    (convert @1)))
2935  (simplify
2936   (negate (convert negate_expr_p@1))
2937   (if (SCALAR_FLOAT_TYPE_P (type)
2938        && ((DECIMAL_FLOAT_TYPE_P (type)
2939             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2940             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2941            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2942    (convert (negate @1))))
2943  (simplify
2944   (negate (nop_convert? (negate @1)))
2945   (if (!TYPE_OVERFLOW_SANITIZED (type)
2946        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2947    (view_convert @1)))
2949  /* We can't reassociate floating-point unless -fassociative-math
2950     or fixed-point plus or minus because of saturation to +-Inf.  */
2951  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2952       && !FIXED_POINT_TYPE_P (type))
2954   /* Match patterns that allow contracting a plus-minus pair
2955      irrespective of overflow issues.  */
2956   /* (A +- B) - A       ->  +- B */
2957   /* (A +- B) -+ B      ->  A */
2958   /* A - (A +- B)       -> -+ B */
2959   /* A +- (B -+ A)      ->  +- B */
2960   (simplify
2961    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2962    (view_convert @1))
2963   (simplify
2964    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2965    (if (!ANY_INTEGRAL_TYPE_P (type)
2966         || TYPE_OVERFLOW_WRAPS (type))
2967    (negate (view_convert @1))
2968    (view_convert (negate @1))))
2969   (simplify
2970    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2971    (view_convert @0))
2972   (simplify
2973    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2974     (if (!ANY_INTEGRAL_TYPE_P (type)
2975          || TYPE_OVERFLOW_WRAPS (type))
2976      (negate (view_convert @1))
2977      (view_convert (negate @1))))
2978   (simplify
2979    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2980    (view_convert @1))
2981   /* (A +- B) + (C - A)   -> C +- B */
2982   /* (A +  B) - (A - C)   -> B + C */
2983   /* More cases are handled with comparisons.  */
2984   (simplify
2985    (plus:c (plus:c @0 @1) (minus @2 @0))
2986    (plus @2 @1))
2987   (simplify
2988    (plus:c (minus @0 @1) (minus @2 @0))
2989    (minus @2 @1))
2990   (simplify
2991    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2992    (if (TYPE_OVERFLOW_UNDEFINED (type)
2993         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2994     (pointer_diff @2 @1)))
2995   (simplify
2996    (minus (plus:c @0 @1) (minus @0 @2))
2997    (plus @1 @2))
2999   /* (A +- CST1) +- CST2 -> A + CST3
3000      Use view_convert because it is safe for vectors and equivalent for
3001      scalars.  */
3002   (for outer_op (plus minus)
3003    (for inner_op (plus minus)
3004         neg_inner_op (minus plus)
3005     (simplify
3006      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3007                CONSTANT_CLASS_P@2)
3008      /* If one of the types wraps, use that one.  */
3009      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3010       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3011          forever if something doesn't simplify into a constant.  */
3012       (if (!CONSTANT_CLASS_P (@0))
3013        (if (outer_op == PLUS_EXPR)
3014         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3015         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3016       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3017            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3018        (if (outer_op == PLUS_EXPR)
3019         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3020         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3021        /* If the constant operation overflows we cannot do the transform
3022           directly as we would introduce undefined overflow, for example
3023           with (a - 1) + INT_MIN.  */
3024        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3025         (with { tree cst = const_binop (outer_op == inner_op
3026                                         ? PLUS_EXPR : MINUS_EXPR,
3027                                         type, @1, @2); }
3028          (if (cst && !TREE_OVERFLOW (cst))
3029           (inner_op @0 { cst; } )
3030           /* X+INT_MAX+1 is X-INT_MIN.  */
3031           (if (INTEGRAL_TYPE_P (type) && cst
3032                && wi::to_wide (cst) == wi::min_value (type))
3033            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3034            /* Last resort, use some unsigned type.  */
3035            (with { tree utype = unsigned_type_for (type); }
3036             (if (utype)
3037              (view_convert (inner_op
3038                             (view_convert:utype @0)
3039                             (view_convert:utype
3040                              { drop_tree_overflow (cst); }))))))))))))))
3042   /* (CST1 - A) +- CST2 -> CST3 - A  */
3043   (for outer_op (plus minus)
3044    (simplify
3045     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3046     /* If one of the types wraps, use that one.  */
3047     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3048      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3049         forever if something doesn't simplify into a constant.  */
3050      (if (!CONSTANT_CLASS_P (@0))
3051       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3052      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3053           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3054       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3055       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3056        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3057         (if (cst && !TREE_OVERFLOW (cst))
3058          (minus { cst; } @0))))))))
3060   /* CST1 - (CST2 - A) -> CST3 + A
3061      Use view_convert because it is safe for vectors and equivalent for
3062      scalars.  */
3063   (simplify
3064    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3065    /* If one of the types wraps, use that one.  */
3066    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3067     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3068       forever if something doesn't simplify into a constant.  */
3069     (if (!CONSTANT_CLASS_P (@0))
3070      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3071     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3072          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3073      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3074      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3075       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3076        (if (cst && !TREE_OVERFLOW (cst))
3077         (plus { cst; } @0)))))))
3079 /* ((T)(A)) + CST -> (T)(A + CST)  */
3080 #if GIMPLE
3081   (simplify
3082    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3083     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3084          && TREE_CODE (type) == INTEGER_TYPE
3085          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3086          && int_fits_type_p (@1, TREE_TYPE (@0)))
3087      /* Perform binary operation inside the cast if the constant fits
3088         and (A + CST)'s range does not overflow.  */
3089      (with
3090       {
3091         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3092                           max_ovf = wi::OVF_OVERFLOW;
3093         tree inner_type = TREE_TYPE (@0);
3095         wide_int w1
3096           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3097                             TYPE_SIGN (inner_type));
3099         value_range vr;
3100         if (get_global_range_query ()->range_of_expr (vr, @0)
3101             && !vr.varying_p () && !vr.undefined_p ())
3102           {
3103             wide_int wmin0 = vr.lower_bound ();
3104             wide_int wmax0 = vr.upper_bound ();
3105             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3106             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3107           }
3108       }
3109      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3110       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3111      )))
3112 #endif
3114 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3115 #if GIMPLE
3116   (for op (plus minus)
3117    (simplify
3118     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3119      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3120           && TREE_CODE (type) == INTEGER_TYPE
3121           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3122           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3123           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3124           && TYPE_OVERFLOW_WRAPS (type))
3125        (plus (convert @0) (op @2 (convert @1))))))
3126 #endif
3128 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3129    to a simple value.  */
3130   (for op (plus minus)
3131    (simplify
3132     (op (convert @0) (convert @1))
3133      (if (INTEGRAL_TYPE_P (type)
3134           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3135           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3136           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3137           && !TYPE_OVERFLOW_TRAPS (type)
3138           && !TYPE_OVERFLOW_SANITIZED (type))
3139       (convert (op! @0 @1)))))
3141   /* ~A + A -> -1 */
3142   (simplify
3143    (plus:c (convert? (bit_not @0)) (convert? @0))
3144    (if (!TYPE_OVERFLOW_TRAPS (type))
3145     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3147   /* ~A + 1 -> -A */
3148   (simplify
3149    (plus (convert? (bit_not @0)) integer_each_onep)
3150    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3151     (negate (convert @0))))
3153   /* -A - 1 -> ~A */
3154   (simplify
3155    (minus (convert? (negate @0)) integer_each_onep)
3156    (if (!TYPE_OVERFLOW_TRAPS (type)
3157         && TREE_CODE (type) != COMPLEX_TYPE
3158         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3159     (bit_not (convert @0))))
3161   /* -1 - A -> ~A */
3162   (simplify
3163    (minus integer_all_onesp @0)
3164    (if (TREE_CODE (type) != COMPLEX_TYPE)
3165     (bit_not @0)))
3167   /* (T)(P + A) - (T)P -> (T) A */
3168   (simplify
3169    (minus (convert (plus:c @@0 @1))
3170     (convert? @0))
3171    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3172         /* For integer types, if A has a smaller type
3173            than T the result depends on the possible
3174            overflow in P + A.
3175            E.g. T=size_t, A=(unsigned)429497295, P>0.
3176            However, if an overflow in P + A would cause
3177            undefined behavior, we can assume that there
3178            is no overflow.  */
3179         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3180             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3181     (convert @1)))
3182   (simplify
3183    (minus (convert (pointer_plus @@0 @1))
3184     (convert @0))
3185    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3186         /* For pointer types, if the conversion of A to the
3187            final type requires a sign- or zero-extension,
3188            then we have to punt - it is not defined which
3189            one is correct.  */
3190         || (POINTER_TYPE_P (TREE_TYPE (@0))
3191             && TREE_CODE (@1) == INTEGER_CST
3192             && tree_int_cst_sign_bit (@1) == 0))
3193     (convert @1)))
3194    (simplify
3195     (pointer_diff (pointer_plus @@0 @1) @0)
3196     /* The second argument of pointer_plus must be interpreted as signed, and
3197        thus sign-extended if necessary.  */
3198     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3199      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3200         second arg is unsigned even when we need to consider it as signed,
3201         we don't want to diagnose overflow here.  */
3202      (convert (view_convert:stype @1))))
3204   /* (T)P - (T)(P + A) -> -(T) A */
3205   (simplify
3206    (minus (convert? @0)
3207     (convert (plus:c @@0 @1)))
3208    (if (INTEGRAL_TYPE_P (type)
3209         && TYPE_OVERFLOW_UNDEFINED (type)
3210         /* For integer literals, using an intermediate unsigned type to avoid
3211            an overflow at run time is counter-productive because it introduces
3212            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3213            the result, which may be problematic in GENERIC for some front-ends:
3214              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3215            so we use the direct path for them.  */
3216         && TREE_CODE (@1) != INTEGER_CST
3217         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3218     (with { tree utype = unsigned_type_for (type); }
3219      (convert (negate (convert:utype @1))))
3220     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3221          /* For integer types, if A has a smaller type
3222             than T the result depends on the possible
3223             overflow in P + A.
3224             E.g. T=size_t, A=(unsigned)429497295, P>0.
3225             However, if an overflow in P + A would cause
3226             undefined behavior, we can assume that there
3227             is no overflow.  */
3228          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3229              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3230      (negate (convert @1)))))
3231   (simplify
3232    (minus (convert @0)
3233     (convert (pointer_plus @@0 @1)))
3234    (if (INTEGRAL_TYPE_P (type)
3235         && TYPE_OVERFLOW_UNDEFINED (type)
3236         /* See above the rationale for this condition.  */
3237         && TREE_CODE (@1) != INTEGER_CST
3238         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3239     (with { tree utype = unsigned_type_for (type); }
3240      (convert (negate (convert:utype @1))))
3241     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3242          /* For pointer types, if the conversion of A to the
3243             final type requires a sign- or zero-extension,
3244             then we have to punt - it is not defined which
3245             one is correct.  */
3246          || (POINTER_TYPE_P (TREE_TYPE (@0))
3247              && TREE_CODE (@1) == INTEGER_CST
3248              && tree_int_cst_sign_bit (@1) == 0))
3249      (negate (convert @1)))))
3250    (simplify
3251     (pointer_diff @0 (pointer_plus @@0 @1))
3252     /* The second argument of pointer_plus must be interpreted as signed, and
3253        thus sign-extended if necessary.  */
3254     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3255      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3256         second arg is unsigned even when we need to consider it as signed,
3257         we don't want to diagnose overflow here.  */
3258      (negate (convert (view_convert:stype @1)))))
3260   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3261   (simplify
3262    (minus (convert (plus:c @@0 @1))
3263     (convert (plus:c @0 @2)))
3264    (if (INTEGRAL_TYPE_P (type)
3265         && TYPE_OVERFLOW_UNDEFINED (type)
3266         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3267         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3268     (with { tree utype = unsigned_type_for (type); }
3269      (convert (minus (convert:utype @1) (convert:utype @2))))
3270     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3271           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3272          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3273              /* For integer types, if A has a smaller type
3274                 than T the result depends on the possible
3275                 overflow in P + A.
3276                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3277                 However, if an overflow in P + A would cause
3278                 undefined behavior, we can assume that there
3279                 is no overflow.  */
3280              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3281                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3282                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3283                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3284      (minus (convert @1) (convert @2)))))
3285   (simplify
3286    (minus (convert (pointer_plus @@0 @1))
3287     (convert (pointer_plus @0 @2)))
3288    (if (INTEGRAL_TYPE_P (type)
3289         && TYPE_OVERFLOW_UNDEFINED (type)
3290         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3291     (with { tree utype = unsigned_type_for (type); }
3292      (convert (minus (convert:utype @1) (convert:utype @2))))
3293     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3294          /* For pointer types, if the conversion of A to the
3295             final type requires a sign- or zero-extension,
3296             then we have to punt - it is not defined which
3297             one is correct.  */
3298          || (POINTER_TYPE_P (TREE_TYPE (@0))
3299              && TREE_CODE (@1) == INTEGER_CST
3300              && tree_int_cst_sign_bit (@1) == 0
3301              && TREE_CODE (@2) == INTEGER_CST
3302              && tree_int_cst_sign_bit (@2) == 0))
3303      (minus (convert @1) (convert @2)))))
3304    (simplify
3305     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3306      (pointer_diff @0 @1))
3307    (simplify
3308     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3309     /* The second argument of pointer_plus must be interpreted as signed, and
3310        thus sign-extended if necessary.  */
3311     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3312      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3313         second arg is unsigned even when we need to consider it as signed,
3314         we don't want to diagnose overflow here.  */
3315      (minus (convert (view_convert:stype @1))
3316             (convert (view_convert:stype @2)))))))
3318 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3319     Modeled after fold_plusminus_mult_expr.  */
3320 (if (!TYPE_SATURATING (type)
3321      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3322  (for plusminus (plus minus)
3323   (simplify
3324    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3325    (if (!ANY_INTEGRAL_TYPE_P (type)
3326         || TYPE_OVERFLOW_WRAPS (type)
3327         || (INTEGRAL_TYPE_P (type)
3328             && tree_expr_nonzero_p (@0)
3329             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3330     (if (single_use (@3) || single_use (@4))
3331      /* If @1 +- @2 is constant require a hard single-use on either
3332         original operand (but not on both).  */
3333      (mult (plusminus @1 @2) @0)
3334      (mult! (plusminus @1 @2) @0)
3335   )))
3336   /* We cannot generate constant 1 for fract.  */
3337   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3338    (simplify
3339     (plusminus @0 (mult:c@3 @0 @2))
3340     (if ((!ANY_INTEGRAL_TYPE_P (type)
3341           || TYPE_OVERFLOW_WRAPS (type)
3342           /* For @0 + @0*@2 this transformation would introduce UB
3343              (where there was none before) for @0 in [-1,0] and @2 max.
3344              For @0 - @0*@2 this transformation would introduce UB
3345              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3346           || (INTEGRAL_TYPE_P (type)
3347               && ((tree_expr_nonzero_p (@0)
3348                    && expr_not_equal_to (@0,
3349                                 wi::minus_one (TYPE_PRECISION (type))))
3350                   || (plusminus == PLUS_EXPR
3351                       ? expr_not_equal_to (@2,
3352                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3353                       /* Let's ignore the @0 -1 and @2 min case.  */
3354                       : (expr_not_equal_to (@2,
3355                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3356                          && expr_not_equal_to (@2,
3357                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3358                                 + 1))))))
3359          && single_use (@3))
3360      (mult (plusminus { build_one_cst (type); } @2) @0)))
3361    (simplify
3362     (plusminus (mult:c@3 @0 @2) @0)
3363     (if ((!ANY_INTEGRAL_TYPE_P (type)
3364           || TYPE_OVERFLOW_WRAPS (type)
3365           /* For @0*@2 + @0 this transformation would introduce UB
3366              (where there was none before) for @0 in [-1,0] and @2 max.
3367              For @0*@2 - @0 this transformation would introduce UB
3368              for @0 0 and @2 min.  */
3369           || (INTEGRAL_TYPE_P (type)
3370               && ((tree_expr_nonzero_p (@0)
3371                    && (plusminus == MINUS_EXPR
3372                        || expr_not_equal_to (@0,
3373                                 wi::minus_one (TYPE_PRECISION (type)))))
3374                   || expr_not_equal_to (@2,
3375                         (plusminus == PLUS_EXPR
3376                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3377                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3378          && single_use (@3))
3379      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3381 #if GIMPLE
3382 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3383    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3384 (simplify
3385  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3386   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3387        && tree_fits_uhwi_p (@1)
3388        && tree_to_uhwi (@1) < element_precision (type)
3389        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3390            || optab_handler (smul_optab,
3391                              TYPE_MODE (type)) != CODE_FOR_nothing))
3392    (with { tree t = type;
3393            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3394            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3395                                              element_precision (type));
3396            w += 1;
3397            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3398                                         : t, w);
3399            cst = build_uniform_cst (t, cst); }
3400     (convert (mult (convert:t @0) { cst; })))))
3401 (simplify
3402  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3403   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3404        && tree_fits_uhwi_p (@1)
3405        && tree_to_uhwi (@1) < element_precision (type)
3406        && tree_fits_uhwi_p (@2)
3407        && tree_to_uhwi (@2) < element_precision (type)
3408        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3409            || optab_handler (smul_optab,
3410                              TYPE_MODE (type)) != CODE_FOR_nothing))
3411    (with { tree t = type;
3412            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3413            unsigned int prec = element_precision (type);
3414            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3415            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3416            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3417                                         : t, w);
3418            cst = build_uniform_cst (t, cst); }
3419     (convert (mult (convert:t @0) { cst; })))))
3420 #endif
3422 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3423    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3424    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3425 (for op (bit_ior bit_xor)
3426  (simplify
3427   (op (mult:s@0 @1 INTEGER_CST@2)
3428       (mult:s@3 @1 INTEGER_CST@4))
3429   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3430        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3431    (mult @1
3432          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3433  (simplify
3434   (op:c (mult:s@0 @1 INTEGER_CST@2)
3435         (lshift:s@3 @1 INTEGER_CST@4))
3436   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3437        && tree_int_cst_sgn (@4) > 0
3438        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3439    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3440            wide_int c = wi::add (wi::to_wide (@2),
3441                                  wi::lshift (wone, wi::to_wide (@4))); }
3442     (mult @1 { wide_int_to_tree (type, c); }))))
3443  (simplify
3444   (op:c (mult:s@0 @1 INTEGER_CST@2)
3445         @1)
3446   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3447        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3448    (mult @1
3449          { wide_int_to_tree (type,
3450                              wi::add (wi::to_wide (@2), 1)); })))
3451  (simplify
3452   (op (lshift:s@0 @1 INTEGER_CST@2)
3453       (lshift:s@3 @1 INTEGER_CST@4))
3454   (if (INTEGRAL_TYPE_P (type)
3455        && tree_int_cst_sgn (@2) > 0
3456        && tree_int_cst_sgn (@4) > 0
3457        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3458    (with { tree t = type;
3459            if (!TYPE_OVERFLOW_WRAPS (t))
3460              t = unsigned_type_for (t);
3461            wide_int wone = wi::one (TYPE_PRECISION (t));
3462            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3463                                  wi::lshift (wone, wi::to_wide (@4))); }
3464     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3465  (simplify
3466   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3467         @1)
3468   (if (INTEGRAL_TYPE_P (type)
3469        && tree_int_cst_sgn (@2) > 0
3470        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3471    (with { tree t = type;
3472            if (!TYPE_OVERFLOW_WRAPS (t))
3473              t = unsigned_type_for (t);
3474            wide_int wone = wi::one (TYPE_PRECISION (t));
3475            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3476     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3478 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3480 (for minmax (min max)
3481  (simplify
3482   (minmax @0 @0)
3483   @0))
3484 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3485 (for minmax (FMIN_ALL FMAX_ALL)
3486  (simplify
3487   (minmax @0 @0)
3488   (if (!tree_expr_maybe_signaling_nan_p (@0))
3489     @0)))
3490 /* min(max(x,y),y) -> y.  */
3491 (simplify
3492  (min:c (max:c @0 @1) @1)
3493  @1)
3494 /* max(min(x,y),y) -> y.  */
3495 (simplify
3496  (max:c (min:c @0 @1) @1)
3497  @1)
3498 /* max(a,-a) -> abs(a).  */
3499 (simplify
3500  (max:c @0 (negate @0))
3501  (if (TREE_CODE (type) != COMPLEX_TYPE
3502       && (! ANY_INTEGRAL_TYPE_P (type)
3503           || TYPE_OVERFLOW_UNDEFINED (type)))
3504   (abs @0)))
3505 /* min(a,-a) -> -abs(a).  */
3506 (simplify
3507  (min:c @0 (negate @0))
3508  (if (TREE_CODE (type) != COMPLEX_TYPE
3509       && (! ANY_INTEGRAL_TYPE_P (type)
3510           || TYPE_OVERFLOW_UNDEFINED (type)))
3511   (negate (abs @0))))
3512 (simplify
3513  (min @0 @1)
3514  (switch
3515   (if (INTEGRAL_TYPE_P (type)
3516        && TYPE_MIN_VALUE (type)
3517        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3518    @1)
3519   (if (INTEGRAL_TYPE_P (type)
3520        && TYPE_MAX_VALUE (type)
3521        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3522    @0)))
3523 (simplify
3524  (max @0 @1)
3525  (switch
3526   (if (INTEGRAL_TYPE_P (type)
3527        && TYPE_MAX_VALUE (type)
3528        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3529    @1)
3530   (if (INTEGRAL_TYPE_P (type)
3531        && TYPE_MIN_VALUE (type)
3532        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3533    @0)))
3535 /* max (a, a + CST) -> a + CST where CST is positive.  */
3536 /* max (a, a + CST) -> a where CST is negative.  */
3537 (simplify
3538  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3539   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3540    (if (tree_int_cst_sgn (@1) > 0)
3541     @2
3542     @0)))
3544 /* min (a, a + CST) -> a where CST is positive.  */
3545 /* min (a, a + CST) -> a + CST where CST is negative. */
3546 (simplify
3547  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3548   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3549    (if (tree_int_cst_sgn (@1) > 0)
3550     @0
3551     @2)))
3553 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3554    the addresses are known to be less, equal or greater.  */
3555 (for minmax (min max)
3556      cmp (lt gt)
3557  (simplify
3558   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3559   (with
3560    {
3561      poly_int64 off0, off1;
3562      tree base0, base1;
3563      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3564                                   off0, off1, GENERIC);
3565    }
3566    (if (equal == 1)
3567     (if (minmax == MIN_EXPR)
3568      (if (known_le (off0, off1))
3569       @2
3570       (if (known_gt (off0, off1))
3571        @3))
3572      (if (known_ge (off0, off1))
3573       @2
3574       (if (known_lt (off0, off1))
3575        @3)))))))
3577 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3578    and the outer convert demotes the expression back to x's type.  */
3579 (for minmax (min max)
3580  (simplify
3581   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3582   (if (INTEGRAL_TYPE_P (type)
3583        && types_match (@1, type) && int_fits_type_p (@2, type)
3584        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3585        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3586    (minmax @1 (convert @2)))))
3588 (for minmax (FMIN_ALL FMAX_ALL)
3589  /* If either argument is NaN and other one is not sNaN, return the other
3590     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3591  (simplify
3592   (minmax:c @0 REAL_CST@1)
3593    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3594        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3595        && !tree_expr_maybe_signaling_nan_p (@0))
3596    @0)))
3597 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3598    functions to return the numeric arg if the other one is NaN.
3599    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3600    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3601    worry about it either.  */
3602 (if (flag_finite_math_only)
3603  (simplify
3604   (FMIN_ALL @0 @1)
3605   (min @0 @1))
3606  (simplify
3607   (FMAX_ALL @0 @1)
3608   (max @0 @1)))
3609 /* min (-A, -B) -> -max (A, B)  */
3610 (for minmax (min max FMIN_ALL FMAX_ALL)
3611      maxmin (max min FMAX_ALL FMIN_ALL)
3612  (simplify
3613   (minmax (negate:s@2 @0) (negate:s@3 @1))
3614   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3615        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3616            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3617    (negate (maxmin @0 @1)))))
3618 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3619    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3620 (for minmax (min max)
3621  maxmin (max min)
3622  (simplify
3623   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3624   (bit_not (maxmin @0 @1))))
3626 /* MIN (X, Y) == X -> X <= Y  */
3627 (for minmax (min min max max)
3628      cmp    (eq  ne  eq  ne )
3629      out    (le  gt  ge  lt )
3630  (simplify
3631   (cmp:c (minmax:c @0 @1) @0)
3632   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3633    (out @0 @1))))
3634 /* MIN (X, 5) == 0 -> X == 0
3635    MIN (X, 5) == 7 -> false  */
3636 (for cmp (eq ne)
3637  (simplify
3638   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3639   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3640                  TYPE_SIGN (TREE_TYPE (@0))))
3641    { constant_boolean_node (cmp == NE_EXPR, type); }
3642    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3643                   TYPE_SIGN (TREE_TYPE (@0))))
3644     (cmp @0 @2)))))
3645 (for cmp (eq ne)
3646  (simplify
3647   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3648   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3649                  TYPE_SIGN (TREE_TYPE (@0))))
3650    { constant_boolean_node (cmp == NE_EXPR, type); }
3651    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3652                   TYPE_SIGN (TREE_TYPE (@0))))
3653     (cmp @0 @2)))))
3654 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
3655 (for minmax (min     min     max     max     min     min     max     max    )
3656      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
3657      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3658  (simplify
3659   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3660   (comb (cmp @0 @2) (cmp @1 @2))))
3662 /* X <= MAX(X, Y) -> true
3663    X > MAX(X, Y) -> false 
3664    X >= MIN(X, Y) -> true
3665    X < MIN(X, Y) -> false */
3666 (for minmax (min     min     max     max     )
3667      cmp    (ge      lt      le      gt      )
3668  (simplify
3669   (cmp @0 (minmax:c @0 @1))
3670   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3672 /* Undo fancy ways of writing max/min or other ?: expressions, like
3673    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
3674    People normally use ?: and that is what we actually try to optimize.  */
3675 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
3676 (simplify
3677  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3678  (if (INTEGRAL_TYPE_P (type)
3679       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3680   (cond (convert:boolean_type_node @2) @1 @0)))
3681 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
3682 (simplify
3683  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3684  (if (INTEGRAL_TYPE_P (type)
3685       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3686   (cond (convert:boolean_type_node @2) @1 @0)))
3687 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
3688 (simplify
3689  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3690  (if (INTEGRAL_TYPE_P (type)
3691       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3692   (cond (convert:boolean_type_node @2) @1 @0)))
3694 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
3695 (simplify
3696   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3697   (max @2 @1))
3699 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
3700 (for op (bit_xor bit_ior plus)
3701  (simplify
3702   (cond (eq zero_one_valued_p@0
3703             integer_zerop)
3704         @1
3705         (op:c @2 @1))
3706   (if (INTEGRAL_TYPE_P (type)
3707        && TYPE_PRECISION (type) > 1
3708        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3709        (op (mult (convert:type @0) @2) @1))))
3711 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
3712 (for op (bit_xor bit_ior plus)
3713  (simplify
3714   (cond (ne zero_one_valued_p@0
3715             integer_zerop)
3716        (op:c @2 @1)
3717         @1)
3718   (if (INTEGRAL_TYPE_P (type)
3719        && TYPE_PRECISION (type) > 1
3720        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3721        (op (mult (convert:type @0) @2) @1))))
3723 /* Simplifications of shift and rotates.  */
3725 (for rotate (lrotate rrotate)
3726  (simplify
3727   (rotate integer_all_onesp@0 @1)
3728   @0))
3730 /* Optimize -1 >> x for arithmetic right shifts.  */
3731 (simplify
3732  (rshift integer_all_onesp@0 @1)
3733  (if (!TYPE_UNSIGNED (type))
3734   @0))
3736 /* Optimize (x >> c) << c into x & (-1<<c).  */
3737 (simplify
3738  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3739  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3740   /* It doesn't matter if the right shift is arithmetic or logical.  */
3741   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3743 (simplify
3744  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3745  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3746       /* Allow intermediate conversion to integral type with whatever sign, as
3747          long as the low TYPE_PRECISION (type)
3748          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3749       && INTEGRAL_TYPE_P (type)
3750       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3751       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3752       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3753       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3754           || wi::geu_p (wi::to_wide (@1),
3755                         TYPE_PRECISION (type)
3756                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3757   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3759 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3760    types.  */
3761 (simplify
3762  (rshift (lshift @0 INTEGER_CST@1) @1)
3763  (if (TYPE_UNSIGNED (type)
3764       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3765   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3767 /* Optimize x >> x into 0 */
3768 (simplify
3769  (rshift @0 @0)
3770   { build_zero_cst (type); })
3772 (for shiftrotate (lrotate rrotate lshift rshift)
3773  (simplify
3774   (shiftrotate @0 integer_zerop)
3775   (non_lvalue @0))
3776  (simplify
3777   (shiftrotate integer_zerop@0 @1)
3778   @0)
3779  /* Prefer vector1 << scalar to vector1 << vector2
3780     if vector2 is uniform.  */
3781  (for vec (VECTOR_CST CONSTRUCTOR)
3782   (simplify
3783    (shiftrotate @0 vec@1)
3784    (with { tree tem = uniform_vector_p (@1); }
3785     (if (tem)
3786      (shiftrotate @0 { tem; }))))))
3788 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3789    Y is 0.  Similarly for X >> Y.  */
3790 #if GIMPLE
3791 (for shift (lshift rshift)
3792  (simplify
3793   (shift @0 SSA_NAME@1)
3794    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3795     (with {
3796       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3797       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3798      }
3799      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3800       @0)))))
3801 #endif
3803 /* Rewrite an LROTATE_EXPR by a constant into an
3804    RROTATE_EXPR by a new constant.  */
3805 (simplify
3806  (lrotate @0 INTEGER_CST@1)
3807  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3808                             build_int_cst (TREE_TYPE (@1),
3809                                            element_precision (type)), @1); }))
3811 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3812 (for op (lrotate rrotate rshift lshift)
3813  (simplify
3814   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3815   (with { unsigned int prec = element_precision (type); }
3816    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3817         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3818         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3819         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3820     (with { unsigned int low = (tree_to_uhwi (@1)
3821                                 + tree_to_uhwi (@2)); }
3822      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3823         being well defined.  */
3824      (if (low >= prec)
3825       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3826        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3827        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3828         { build_zero_cst (type); }
3829         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3830       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3833 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3834 (simplify
3835  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3836   (if ((wi::to_wide (@1) & 1) != 0)
3837    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3838    { build_zero_cst (type); }))
3840 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3841    either to false if D is smaller (unsigned comparison) than C, or to
3842    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
3843 (for cmp (ne eq)
3844      icmp (eq ne)
3845  (simplify
3846   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3847    (with { int c1 = wi::clz (wi::to_wide (@1));
3848            int c2 = wi::clz (wi::to_wide (@2)); }
3849     (if (c1 < c2)
3850      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3851      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3852  (simplify
3853   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3854    (if (tree_int_cst_sgn (@1) > 0)
3855     (with { int c1 = wi::clz (wi::to_wide (@1));
3856             int c2 = wi::clz (wi::to_wide (@2)); }
3857      (if (c1 > c2)
3858       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3859       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3861 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3862    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3863    if CST2 != 0.  */
3864 (for cmp (ne eq)
3865  (simplify
3866   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3867   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3868    (if (cand < 0
3869         || (!integer_zerop (@2)
3870             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3871     { constant_boolean_node (cmp == NE_EXPR, type); }
3872     (if (!integer_zerop (@2)
3873          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3874      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3876 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3877         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
3878 (for cmp (ne eq)
3879  (simplify
3880   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3881   (if (tree_fits_shwi_p (@1)
3882        && tree_to_shwi (@1) > 0
3883        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3884     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
3885       { constant_boolean_node (cmp == NE_EXPR, type); }
3886       (with { wide_int c1 = wi::to_wide (@1);
3887               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3888               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3889         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3890              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
3891  (simplify
3892   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3893   (if (tree_fits_shwi_p (@1)
3894        && tree_to_shwi (@1) > 0
3895        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3896     (with { tree t0 = TREE_TYPE (@0);
3897             unsigned int prec = TYPE_PRECISION (t0);
3898             wide_int c1 = wi::to_wide (@1);
3899             wide_int c2 = wi::to_wide (@2);
3900             wide_int c3 = wi::to_wide (@3);
3901             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
3902       (if ((c2 & c3) != c3)
3903         { constant_boolean_node (cmp == NE_EXPR, type); }
3904         (if (TYPE_UNSIGNED (t0))
3905           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
3906             { constant_boolean_node (cmp == NE_EXPR, type); }
3907             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3908                  { wide_int_to_tree (t0, c3 << c1); }))
3909           (with { wide_int smask = wi::arshift (sb, c1); }
3910             (switch
3911               (if ((c2 & smask) == 0)
3912                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3913                      { wide_int_to_tree (t0, c3 << c1); }))
3914               (if ((c3 & smask) == 0)
3915                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3916                      { wide_int_to_tree (t0, c3 << c1); }))
3917               (if ((c2 & smask) != (c3 & smask))
3918                 { constant_boolean_node (cmp == NE_EXPR, type); })
3919               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3920                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
3922 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3923         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3924    if the new mask might be further optimized.  */
3925 (for shift (lshift rshift)
3926  (simplify
3927   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3928            INTEGER_CST@2)
3929    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3930         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3931         && tree_fits_uhwi_p (@1)
3932         && tree_to_uhwi (@1) > 0
3933         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3934     (with
3935      {
3936        unsigned int shiftc = tree_to_uhwi (@1);
3937        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3938        unsigned HOST_WIDE_INT newmask, zerobits = 0;
3939        tree shift_type = TREE_TYPE (@3);
3940        unsigned int prec;
3942        if (shift == LSHIFT_EXPR)
3943          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3944        else if (shift == RSHIFT_EXPR
3945                 && type_has_mode_precision_p (shift_type))
3946          {
3947            prec = TYPE_PRECISION (TREE_TYPE (@3));
3948            tree arg00 = @0;
3949            /* See if more bits can be proven as zero because of
3950               zero extension.  */
3951            if (@3 != @0
3952                && TYPE_UNSIGNED (TREE_TYPE (@0)))
3953              {
3954                tree inner_type = TREE_TYPE (@0);
3955                if (type_has_mode_precision_p (inner_type)
3956                    && TYPE_PRECISION (inner_type) < prec)
3957                  {
3958                    prec = TYPE_PRECISION (inner_type);
3959                    /* See if we can shorten the right shift.  */
3960                    if (shiftc < prec)
3961                      shift_type = inner_type;
3962                    /* Otherwise X >> C1 is all zeros, so we'll optimize
3963                       it into (X, 0) later on by making sure zerobits
3964                       is all ones.  */
3965                  }
3966              }
3967            zerobits = HOST_WIDE_INT_M1U;
3968            if (shiftc < prec)
3969              {
3970                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3971                zerobits <<= prec - shiftc;
3972              }
3973            /* For arithmetic shift if sign bit could be set, zerobits
3974               can contain actually sign bits, so no transformation is
3975               possible, unless MASK masks them all away.  In that
3976               case the shift needs to be converted into logical shift.  */
3977            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3978                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3979              {
3980                if ((mask & zerobits) == 0)
3981                  shift_type = unsigned_type_for (TREE_TYPE (@3));
3982                else
3983                  zerobits = 0;
3984              }
3985          }
3986      }
3987      /* ((X << 16) & 0xff00) is (X, 0).  */
3988      (if ((mask & zerobits) == mask)
3989       { build_int_cst (type, 0); }
3990       (with { newmask = mask | zerobits; }
3991        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3992         (with
3993          {
3994            /* Only do the transformation if NEWMASK is some integer
3995               mode's mask.  */
3996            for (prec = BITS_PER_UNIT;
3997                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3998              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3999                break;
4000          }
4001          (if (prec < HOST_BITS_PER_WIDE_INT
4002               || newmask == HOST_WIDE_INT_M1U)
4003           (with
4004            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4005            (if (!tree_int_cst_equal (newmaskt, @2))
4006             (if (shift_type != TREE_TYPE (@3))
4007              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4008              (bit_and @4 { newmaskt; })))))))))))))
4010 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4011 (for cmp (ne eq)
4012        icmp (eq ne)
4013  (simplify
4014   (cmp
4015    (bit_and
4016     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4017   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4018    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4019                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4021 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4022    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4023 (for shift (lshift rshift)
4024  (for bit_op (bit_and bit_xor bit_ior)
4025   (simplify
4026    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4027    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4028     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4029      (if (mask)
4030       (bit_op (shift (convert @0) @1) { mask; })))))))
4032 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4033 (simplify
4034  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4035   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4036        && (element_precision (TREE_TYPE (@0))
4037            <= element_precision (TREE_TYPE (@1))
4038            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4039    (with
4040     { tree shift_type = TREE_TYPE (@0); }
4041      (convert (rshift (convert:shift_type @1) @2)))))
4043 /* ~(~X >>r Y) -> X >>r Y
4044    ~(~X <<r Y) -> X <<r Y */
4045 (for rotate (lrotate rrotate)
4046  (simplify
4047   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4048    (if ((element_precision (TREE_TYPE (@0))
4049          <= element_precision (TREE_TYPE (@1))
4050          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4051         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4052             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4053     (with
4054      { tree rotate_type = TREE_TYPE (@0); }
4055       (convert (rotate (convert:rotate_type @1) @2))))))
4057 (for cmp (eq ne)
4058  (for rotate (lrotate rrotate)
4059       invrot (rrotate lrotate)
4060   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4061   (simplify
4062    (cmp (rotate @1 @0) (rotate @2 @0))
4063    (cmp @1 @2))
4064   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4065   (simplify
4066    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4067    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4068   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4069   (simplify
4070    (cmp (rotate @0 @1) INTEGER_CST@2)
4071     (if (integer_zerop (@2) || integer_all_onesp (@2))
4072      (cmp @0 @2)))))
4074 /* Narrow a lshift by constant.  */
4075 (simplify
4076  (convert (lshift:s@0 @1 INTEGER_CST@2))
4077  (if (INTEGRAL_TYPE_P (type)
4078       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4079       && !integer_zerop (@2)
4080       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4081   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4082        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4083    (lshift (convert @1) @2)
4084    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4085     { build_zero_cst (type); }))))
4087 /* Simplifications of conversions.  */
4089 /* Basic strip-useless-type-conversions / strip_nops.  */
4090 (for cvt (convert view_convert float fix_trunc)
4091  (simplify
4092   (cvt @0)
4093   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4094        || (GENERIC && type == TREE_TYPE (@0)))
4095    @0)))
4097 /* Contract view-conversions.  */
4098 (simplify
4099   (view_convert (view_convert @0))
4100   (view_convert @0))
4102 /* For integral conversions with the same precision or pointer
4103    conversions use a NOP_EXPR instead.  */
4104 (simplify
4105   (view_convert @0)
4106   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4107        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4108        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4109    (convert @0)))
4111 /* Strip inner integral conversions that do not change precision or size, or
4112    zero-extend while keeping the same size (for bool-to-char).  */
4113 (simplify
4114   (view_convert (convert@0 @1))
4115   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4116        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4117        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4118        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4119            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4120                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4121    (view_convert @1)))
4123 /* Simplify a view-converted empty or single-element constructor.  */
4124 (simplify
4125   (view_convert CONSTRUCTOR@0)
4126   (with
4127    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4128                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4129    (switch
4130     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4131      { build_zero_cst (type); })
4132     (if (CONSTRUCTOR_NELTS (ctor) == 1
4133          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4134          && operand_equal_p (TYPE_SIZE (type),
4135                              TYPE_SIZE (TREE_TYPE
4136                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4137      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4139 /* Re-association barriers around constants and other re-association
4140    barriers can be removed.  */
4141 (simplify
4142  (paren CONSTANT_CLASS_P@0)
4143  @0)
4144 (simplify
4145  (paren (paren@1 @0))
4146  @1)
4148 /* Handle cases of two conversions in a row.  */
4149 (for ocvt (convert float fix_trunc)
4150  (for icvt (convert float)
4151   (simplify
4152    (ocvt (icvt@1 @0))
4153    (with
4154     {
4155       tree inside_type = TREE_TYPE (@0);
4156       tree inter_type = TREE_TYPE (@1);
4157       int inside_int = INTEGRAL_TYPE_P (inside_type);
4158       int inside_ptr = POINTER_TYPE_P (inside_type);
4159       int inside_float = FLOAT_TYPE_P (inside_type);
4160       int inside_vec = VECTOR_TYPE_P (inside_type);
4161       unsigned int inside_prec = element_precision (inside_type);
4162       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4163       int inter_int = INTEGRAL_TYPE_P (inter_type);
4164       int inter_ptr = POINTER_TYPE_P (inter_type);
4165       int inter_float = FLOAT_TYPE_P (inter_type);
4166       int inter_vec = VECTOR_TYPE_P (inter_type);
4167       unsigned int inter_prec = element_precision (inter_type);
4168       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4169       int final_int = INTEGRAL_TYPE_P (type);
4170       int final_ptr = POINTER_TYPE_P (type);
4171       int final_float = FLOAT_TYPE_P (type);
4172       int final_vec = VECTOR_TYPE_P (type);
4173       unsigned int final_prec = element_precision (type);
4174       int final_unsignedp = TYPE_UNSIGNED (type);
4175     }
4176    (switch
4177     /* In addition to the cases of two conversions in a row
4178        handled below, if we are converting something to its own
4179        type via an object of identical or wider precision, neither
4180        conversion is needed.  */
4181     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4182           || (GENERIC
4183               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4184          && (((inter_int || inter_ptr) && final_int)
4185              || (inter_float && final_float))
4186          && inter_prec >= final_prec)
4187      (ocvt @0))
4189     /* Likewise, if the intermediate and initial types are either both
4190        float or both integer, we don't need the middle conversion if the
4191        former is wider than the latter and doesn't change the signedness
4192        (for integers).  Avoid this if the final type is a pointer since
4193        then we sometimes need the middle conversion.  */
4194     (if (((inter_int && inside_int) || (inter_float && inside_float))
4195          && (final_int || final_float)
4196          && inter_prec >= inside_prec
4197          && (inter_float || inter_unsignedp == inside_unsignedp))
4198      (ocvt @0))
4200     /* If we have a sign-extension of a zero-extended value, we can
4201        replace that by a single zero-extension.  Likewise if the
4202        final conversion does not change precision we can drop the
4203        intermediate conversion.  */
4204     (if (inside_int && inter_int && final_int
4205          && ((inside_prec < inter_prec && inter_prec < final_prec
4206               && inside_unsignedp && !inter_unsignedp)
4207              || final_prec == inter_prec))
4208      (ocvt @0))
4210     /* Two conversions in a row are not needed unless:
4211         - some conversion is floating-point (overstrict for now), or
4212         - some conversion is a vector (overstrict for now), or
4213         - the intermediate type is narrower than both initial and
4214           final, or
4215         - the intermediate type and innermost type differ in signedness,
4216           and the outermost type is wider than the intermediate, or
4217         - the initial type is a pointer type and the precisions of the
4218           intermediate and final types differ, or
4219         - the final type is a pointer type and the precisions of the
4220           initial and intermediate types differ.  */
4221     (if (! inside_float && ! inter_float && ! final_float
4222          && ! inside_vec && ! inter_vec && ! final_vec
4223          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4224          && ! (inside_int && inter_int
4225                && inter_unsignedp != inside_unsignedp
4226                && inter_prec < final_prec)
4227          && ((inter_unsignedp && inter_prec > inside_prec)
4228              == (final_unsignedp && final_prec > inter_prec))
4229          && ! (inside_ptr && inter_prec != final_prec)
4230          && ! (final_ptr && inside_prec != inter_prec))
4231      (ocvt @0))
4233     /* A truncation to an unsigned type (a zero-extension) should be
4234        canonicalized as bitwise and of a mask.  */
4235     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4236          && final_int && inter_int && inside_int
4237          && final_prec == inside_prec
4238          && final_prec > inter_prec
4239          && inter_unsignedp)
4240      (convert (bit_and @0 { wide_int_to_tree
4241                               (inside_type,
4242                                wi::mask (inter_prec, false,
4243                                          TYPE_PRECISION (inside_type))); })))
4245     /* If we are converting an integer to a floating-point that can
4246        represent it exactly and back to an integer, we can skip the
4247        floating-point conversion.  */
4248     (if (GIMPLE /* PR66211 */
4249          && inside_int && inter_float && final_int &&
4250          (unsigned) significand_size (TYPE_MODE (inter_type))
4251          >= inside_prec - !inside_unsignedp)
4252      (convert @0)))))))
4254 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4255    float_type.  Only do the transformation if we do not need to preserve
4256    trapping behaviour, so require !flag_trapping_math. */
4257 #if GIMPLE
4258 (simplify
4259    (float (fix_trunc @0))
4260    (if (!flag_trapping_math
4261         && types_match (type, TREE_TYPE (@0))
4262         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4263                                           OPTIMIZE_FOR_BOTH))
4264       (IFN_TRUNC @0)))
4265 #endif
4267 /* If we have a narrowing conversion to an integral type that is fed by a
4268    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4269    masks off bits outside the final type (and nothing else).  */
4270 (simplify
4271   (convert (bit_and @0 INTEGER_CST@1))
4272   (if (INTEGRAL_TYPE_P (type)
4273        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4274        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4275        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4276                                                     TYPE_PRECISION (type)), 0))
4277    (convert @0)))
4280 /* (X /[ex] A) * A -> X.  */
4281 (simplify
4282   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4283   (convert @0))
4285 /* Simplify (A / B) * B + (A % B) -> A.  */
4286 (for div (trunc_div ceil_div floor_div round_div)
4287      mod (trunc_mod ceil_mod floor_mod round_mod)
4288   (simplify
4289    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4290    @0))
4292 /* x / y * y == x -> x % y == 0.  */
4293 (simplify
4294   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4295   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4296     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4298 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4299 (for op (plus minus)
4300  (simplify
4301   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4302   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4303        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4304    (with
4305      {
4306        wi::overflow_type overflow;
4307        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4308                                TYPE_SIGN (type), &overflow);
4309      }
4310      (if (types_match (type, TREE_TYPE (@2))
4311          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4312       (op @0 { wide_int_to_tree (type, mul); })
4313       (with { tree utype = unsigned_type_for (type); }
4314        (convert (op (convert:utype @0)
4315                     (mult (convert:utype @1) (convert:utype @2))))))))))
4317 /* Canonicalization of binary operations.  */
4319 /* Convert X + -C into X - C.  */
4320 (simplify
4321  (plus @0 REAL_CST@1)
4322  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4323   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4324    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4325     (minus @0 { tem; })))))
4327 /* Convert x+x into x*2.  */
4328 (simplify
4329  (plus @0 @0)
4330  (if (SCALAR_FLOAT_TYPE_P (type))
4331   (mult @0 { build_real (type, dconst2); })
4332   (if (INTEGRAL_TYPE_P (type))
4333    (mult @0 { build_int_cst (type, 2); }))))
4335 /* 0 - X  ->  -X.  */
4336 (simplify
4337  (minus integer_zerop @1)
4338  (negate @1))
4339 (simplify
4340  (pointer_diff integer_zerop @1)
4341  (negate (convert @1)))
4343 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4344    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4345    (-ARG1 + ARG0) reduces to -ARG1.  */
4346 (simplify
4347  (minus real_zerop@0 @1)
4348  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4349   (negate @1)))
4351 /* Transform x * -1 into -x.  */
4352 (simplify
4353  (mult @0 integer_minus_onep)
4354  (negate @0))
4356 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4357    signed overflow for CST != 0 && CST != -1.  */
4358 (simplify
4359  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4360  (if (TREE_CODE (@2) != INTEGER_CST
4361       && single_use (@3)
4362       && !integer_zerop (@1) && !integer_minus_onep (@1))
4363   (mult (mult @0 @2) @1)))
4365 /* True if we can easily extract the real and imaginary parts of a complex
4366    number.  */
4367 (match compositional_complex
4368  (convert? (complex @0 @1)))
4370 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4371 (simplify
4372  (complex (realpart @0) (imagpart @0))
4373  @0)
4374 (simplify
4375  (realpart (complex @0 @1))
4376  @0)
4377 (simplify
4378  (imagpart (complex @0 @1))
4379  @1)
4381 /* Sometimes we only care about half of a complex expression.  */
4382 (simplify
4383  (realpart (convert?:s (conj:s @0)))
4384  (convert (realpart @0)))
4385 (simplify
4386  (imagpart (convert?:s (conj:s @0)))
4387  (convert (negate (imagpart @0))))
4388 (for part (realpart imagpart)
4389  (for op (plus minus)
4390   (simplify
4391    (part (convert?:s@2 (op:s @0 @1)))
4392    (convert (op (part @0) (part @1))))))
4393 (simplify
4394  (realpart (convert?:s (CEXPI:s @0)))
4395  (convert (COS @0)))
4396 (simplify
4397  (imagpart (convert?:s (CEXPI:s @0)))
4398  (convert (SIN @0)))
4400 /* conj(conj(x)) -> x  */
4401 (simplify
4402  (conj (convert? (conj @0)))
4403  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4404   (convert @0)))
4406 /* conj({x,y}) -> {x,-y}  */
4407 (simplify
4408  (conj (convert?:s (complex:s @0 @1)))
4409  (with { tree itype = TREE_TYPE (type); }
4410   (complex (convert:itype @0) (negate (convert:itype @1)))))
4412 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4413 (for bswap (BSWAP)
4414  (simplify
4415   (bswap (bswap @0))
4416   @0)
4417  (simplify
4418   (bswap (bit_not (bswap @0)))
4419   (bit_not @0))
4420  (for bitop (bit_xor bit_ior bit_and)
4421   (simplify
4422    (bswap (bitop:c (bswap @0) @1))
4423    (bitop @0 (bswap @1))))
4424  (for cmp (eq ne)
4425   (simplify
4426    (cmp (bswap@2 @0) (bswap @1))
4427    (with { tree ctype = TREE_TYPE (@2); }
4428     (cmp (convert:ctype @0) (convert:ctype @1))))
4429   (simplify
4430    (cmp (bswap @0) INTEGER_CST@1)
4431    (with { tree ctype = TREE_TYPE (@1); }
4432     (cmp (convert:ctype @0) (bswap! @1)))))
4433  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4434  (simplify
4435   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4436            INTEGER_CST@3)
4437    (if (BITS_PER_UNIT == 8
4438         && tree_fits_uhwi_p (@2)
4439         && tree_fits_uhwi_p (@3))
4440     (with
4441      {
4442       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4443       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4444       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4445       unsigned HOST_WIDE_INT lo = bits & 7;
4446       unsigned HOST_WIDE_INT hi = bits - lo;
4447      }
4448      (if (bits < prec
4449           && mask < (256u>>lo)
4450           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4451       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4452        (if (ns == 0)
4453         (bit_and (convert @1) @3)
4454         (with
4455          {
4456           tree utype = unsigned_type_for (TREE_TYPE (@1));
4457           tree nst = build_int_cst (integer_type_node, ns);
4458          }
4459          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4460  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4461  (simplify
4462   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4463    (if (BITS_PER_UNIT == 8
4464         && CHAR_TYPE_SIZE == 8
4465         && tree_fits_uhwi_p (@1))
4466     (with
4467      {
4468       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4469       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4470       /* If the bswap was extended before the original shift, this
4471          byte (shift) has the sign of the extension, not the sign of
4472          the original shift.  */
4473       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4474      }
4475      /* Special case: logical right shift of sign-extended bswap.
4476         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4477      (if (TYPE_PRECISION (type) > prec
4478           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4479           && TYPE_UNSIGNED (type)
4480           && bits < prec && bits + 8 >= prec)
4481       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4482        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4483       (if (bits + 8 == prec)
4484        (if (TYPE_UNSIGNED (st))
4485         (convert (convert:unsigned_char_type_node @0))
4486         (convert (convert:signed_char_type_node @0)))
4487        (if (bits < prec && bits + 8 > prec)
4488         (with 
4489          {
4490           tree nst = build_int_cst (integer_type_node, bits & 7);
4491           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4492                                        : signed_char_type_node;
4493          }
4494          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4495  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4496  (simplify
4497   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4498    (if (BITS_PER_UNIT == 8
4499         && tree_fits_uhwi_p (@1)
4500         && tree_to_uhwi (@1) < 256)
4501     (with
4502      {
4503       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4504       tree utype = unsigned_type_for (TREE_TYPE (@0));
4505       tree nst = build_int_cst (integer_type_node, prec - 8);
4506      }
4507      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4510 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4512 /* Simplify constant conditions.
4513    Only optimize constant conditions when the selected branch
4514    has the same type as the COND_EXPR.  This avoids optimizing
4515    away "c ? x : throw", where the throw has a void type.
4516    Note that we cannot throw away the fold-const.cc variant nor
4517    this one as we depend on doing this transform before possibly
4518    A ? B : B -> B triggers and the fold-const.cc one can optimize
4519    0 ? A : B to B even if A has side-effects.  Something
4520    genmatch cannot handle.  */
4521 (simplify
4522  (cond INTEGER_CST@0 @1 @2)
4523  (if (integer_zerop (@0))
4524   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4525    @2)
4526   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4527    @1)))
4528 (simplify
4529  (vec_cond VECTOR_CST@0 @1 @2)
4530  (if (integer_all_onesp (@0))
4531   @1
4532   (if (integer_zerop (@0))
4533    @2)))
4535 /* Sink unary operations to branches, but only if we do fold both.  */
4536 (for op (negate bit_not abs absu)
4537  (simplify
4538   (op (vec_cond:s @0 @1 @2))
4539   (vec_cond @0 (op! @1) (op! @2))))
4541 /* Sink binary operation to branches, but only if we can fold it.  */
4542 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4543          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4544          trunc_mod ceil_mod floor_mod round_mod min max)
4545 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4546  (simplify
4547   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4548   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4550 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4551  (simplify
4552   (op (vec_cond:s @0 @1 @2) @3)
4553   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4554  (simplify
4555   (op @3 (vec_cond:s @0 @1 @2))
4556   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4558 #if GIMPLE
4559 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4560  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4561            INTEGER_CST@1)
4562  (with {
4563          int ibit = tree_log2 (@0);
4564          int ibit2 = tree_log2 (@1);
4565        }
4566   (if (ibit == ibit2
4567       && ibit >= 0
4568       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4570 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4571  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4572           INTEGER_CST@1)
4573  (with {
4574          int ibit = tree_log2 (@0);
4575          int ibit2 = tree_log2 (@1);
4576        }
4577   (if (ibit == ibit2
4578       && ibit >= 0
4579       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4581 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4582  (bit_and:c
4583   (convert1?@4
4584    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4585   (convert2? @0))
4586  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4588 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4589  (bit_and:c
4590   (convert1?@4
4591    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4592   (convert2? @0))
4593  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4595 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4596  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4597             INTEGER_CST@1)
4598  (with {
4599          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4600                                               TYPE_PRECISION(type)));
4601          int ibit2 = tree_log2 (@1);
4602        }
4603   (if (ibit == ibit2
4604       && ibit >= 0
4605       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4607 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4608  (bit_and@4
4609   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4610   INTEGER_CST@1)
4611  (with {
4612          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4613                                               TYPE_PRECISION(type)));
4614          int ibit2 = tree_log2 (@1);
4615        }
4616   (if (ibit == ibit2
4617       && ibit >= 0
4618       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4620 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4621  (bit_and:c
4622   (convert1?@3
4623    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4624   (convert2? @0))
4625  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4627 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4628  (bit_and:c
4629   (convert1?@3
4630    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4631   (convert2? @0))
4632   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4634 #endif
4636 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4637    Currently disabled after pass lvec because ARM understands
4638    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
4639 (simplify
4640  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4641  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4642   (vec_cond (bit_and @0 @3) @1 @2)))
4643 (simplify
4644  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4645  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4646   (vec_cond (bit_ior @0 @3) @1 @2)))
4647 (simplify
4648  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4649  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4650   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4651 (simplify
4652  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4653  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4654   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4656 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
4657 (simplify
4658  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4659  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4660   (vec_cond (bit_and @0 @1) @2 @3)))
4661 (simplify
4662  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4663  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4664   (vec_cond (bit_ior @0 @1) @2 @3)))
4665 (simplify
4666  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4667  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4668   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4669 (simplify
4670  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4671  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4672   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4674 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4675    types are compatible.  */
4676 (simplify
4677  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4678  (if (VECTOR_BOOLEAN_TYPE_P (type)
4679       && types_match (type, TREE_TYPE (@0)))
4680   (if (integer_zerop (@1) && integer_all_onesp (@2))
4681    (bit_not @0)
4682    (if (integer_all_onesp (@1) && integer_zerop (@2))
4683     @0))))
4685 /* A few simplifications of "a ? CST1 : CST2". */
4686 /* NOTE: Only do this on gimple as the if-chain-to-switch
4687    optimization depends on the gimple to have if statements in it. */
4688 #if GIMPLE
4689 (simplify
4690  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4691  (switch
4692   (if (integer_zerop (@2))
4693    (switch
4694     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4695     (if (integer_onep (@1))
4696      (convert (convert:boolean_type_node @0)))
4697     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4698     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4699      (with {
4700        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4701       }
4702       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4703     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
4704        here as the powerof2cst case above will handle that case correctly.  */
4705     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4706      (negate (convert (convert:boolean_type_node @0))))))
4707   (if (integer_zerop (@1))
4708    (with {
4709       tree booltrue = constant_boolean_node (true, boolean_type_node);
4710     }
4711     (switch
4712      /* a ? 0 : 1 -> !a. */
4713      (if (integer_onep (@2))
4714       (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4715      /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4716      (if (INTEGRAL_TYPE_P (type) &&  integer_pow2p (@2))
4717       (with {
4718         tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4719        }
4720        (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4721         { shift; })))
4722      /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
4723        here as the powerof2cst case above will handle that case correctly.  */
4724      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4725       (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4726     )
4727    )
4728   )
4732 (simplify
4733  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
4734  (switch
4735   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
4736   (if (integer_zerop (@2))
4737    (bit_and (convert @0) @1))
4738   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
4739   (if (integer_zerop (@1))
4740    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
4741   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
4742   (if (integer_onep (@1))
4743    (bit_ior (convert @0) @2))
4744   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
4745   (if (integer_onep (@2))
4746    (bit_ior (bit_xor (convert @0) @2) @1))
4750 /* Optimize
4751    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
4752    x_5 ? cstN ? cst4 : cst3
4753    # op is == or != and N is 1 or 2
4754    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
4755    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
4756    of cst3 and cst4 is smaller.
4757    This was originally done by two_value_replacement in phiopt (PR 88676).  */
4758 (for eqne (ne eq)
4759  (simplify
4760   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
4761   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4762        && INTEGRAL_TYPE_P (type)
4763        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
4764            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
4765    (with {
4766      value_range r;
4767      get_range_query (cfun)->range_of_expr (r, @0);
4768      if (r.undefined_p ())
4769        r.set_varying (TREE_TYPE (@0));
4771      wide_int min = r.lower_bound ();
4772      wide_int max = r.upper_bound ();
4773     }
4774     (if (min + 1 == max
4775          && (wi::to_wide (@1) == min
4776              || wi::to_wide (@1) == max))
4777      (with {
4778        tree arg0 = @2, arg1 = @3;
4779        tree type1;
4780        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
4781          std::swap (arg0, arg1);
4782        if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
4783          {
4784            /* Avoid performing the arithmetics in bool type which has different
4785               semantics, otherwise prefer unsigned types from the two with
4786              the same precision.  */
4787            if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE
4788                || !TYPE_UNSIGNED (type))
4789              type1 = TREE_TYPE (@0);
4790            else
4791              type1 = TREE_TYPE (arg0);
4792          }
4793        else if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4794          type1 = TREE_TYPE (@0);
4795        else
4796          type1 = type;
4797        min = wide_int::from (min, TYPE_PRECISION (type1),
4798                              TYPE_SIGN (TREE_TYPE (@0)));
4799        wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type1),
4800                                     TYPE_SIGN (type));
4801        enum tree_code code;
4802        wi::overflow_type ovf;
4803        if (tree_int_cst_lt (arg0, arg1))
4804          {
4805            code = PLUS_EXPR;
4806            a -= min;
4807            if (!TYPE_UNSIGNED (type1))
4808              {
4809                /* lhs is known to be in range [min, min+1] and we want to add a
4810                   to it.  Check if that operation can overflow for those 2 values
4811                   and if yes, force unsigned type.  */
4812                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
4813                if (ovf)
4814                  type1 = unsigned_type_for (type1);
4815              }
4816          }
4817        else
4818          {
4819            code = MINUS_EXPR;
4820            a += min;
4821            if (!TYPE_UNSIGNED (type1))
4822              {
4823                /* lhs is known to be in range [min, min+1] and we want to subtract
4824                   it from a.  Check if that operation can overflow for those 2
4825                   values and if yes, force unsigned type.  */
4826                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
4827                if (ovf)
4828                 type1 = unsigned_type_for (type1);
4829              }
4830          }
4831        tree arg = wide_int_to_tree (type1, a);
4832       }
4833       (if (code == PLUS_EXPR)
4834        (convert (plus (convert:type1 @0) { arg; }))
4835        (convert (minus { arg; } (convert:type1 @0)))
4836       )
4837      )
4838     )
4839    )
4840   )
4843 #endif
4845 (simplify
4846  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4847  (if (INTEGRAL_TYPE_P (type)
4848       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4849   (cond @1 (convert @2) (convert @3))))
4851 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
4852    be extended.  */
4853 /* This pattern implements two kinds simplification:
4855    Case 1)
4856    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4857      1) Conversions are type widening from smaller type.
4858      2) Const c1 equals to c2 after canonicalizing comparison.
4859      3) Comparison has tree code LT, LE, GT or GE.
4860    This specific pattern is needed when (cmp (convert x) c) may not
4861    be simplified by comparison patterns because of multiple uses of
4862    x.  It also makes sense here because simplifying across multiple
4863    referred var is always benefitial for complicated cases.
4865    Case 2)
4866    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
4867 (for cmp (lt le gt ge eq ne)
4868  (simplify
4869   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4870   (with
4871    {
4872      tree from_type = TREE_TYPE (@1);
4873      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4874      enum tree_code code = ERROR_MARK;
4876      if (INTEGRAL_TYPE_P (from_type)
4877          && int_fits_type_p (@2, from_type)
4878          && (types_match (c1_type, from_type)
4879              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4880                  && (TYPE_UNSIGNED (from_type)
4881                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4882          && (types_match (c2_type, from_type)
4883              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4884                  && (TYPE_UNSIGNED (from_type)
4885                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4886        {
4887          if (cmp != EQ_EXPR)
4888            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
4889          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
4890          else if (int_fits_type_p (@3, from_type))
4891            code = EQ_EXPR;
4892        }
4893    }
4894    (if (code == MAX_EXPR)
4895     (convert (max @1 (convert @2)))
4896     (if (code == MIN_EXPR)
4897      (convert (min @1 (convert @2)))
4898      (if (code == EQ_EXPR)
4899       (convert (cond (eq @1 (convert @3))
4900                      (convert:from_type @3) (convert:from_type @2)))))))))
4902 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4904      1) OP is PLUS or MINUS.
4905      2) CMP is LT, LE, GT or GE.
4906      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4908    This pattern also handles special cases like:
4910      A) Operand x is a unsigned to signed type conversion and c1 is
4911         integer zero.  In this case,
4912           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
4913           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
4914      B) Const c1 may not equal to (C3 op' C2).  In this case we also
4915         check equality for (c1+1) and (c1-1) by adjusting comparison
4916         code.
4918    TODO: Though signed type is handled by this pattern, it cannot be
4919    simplified at the moment because C standard requires additional
4920    type promotion.  In order to match&simplify it here, the IR needs
4921    to be cleaned up by other optimizers, i.e, VRP.  */
4922 (for op (plus minus)
4923  (for cmp (lt le gt ge)
4924   (simplify
4925    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4926    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4927     (if (types_match (from_type, to_type)
4928          /* Check if it is special case A).  */
4929          || (TYPE_UNSIGNED (from_type)
4930              && !TYPE_UNSIGNED (to_type)
4931              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4932              && integer_zerop (@1)
4933              && (cmp == LT_EXPR || cmp == GE_EXPR)))
4934      (with
4935       {
4936         wi::overflow_type overflow = wi::OVF_NONE;
4937         enum tree_code code, cmp_code = cmp;
4938         wide_int real_c1;
4939         wide_int c1 = wi::to_wide (@1);
4940         wide_int c2 = wi::to_wide (@2);
4941         wide_int c3 = wi::to_wide (@3);
4942         signop sgn = TYPE_SIGN (from_type);
4944         /* Handle special case A), given x of unsigned type:
4945             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
4946             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
4947         if (!types_match (from_type, to_type))
4948           {
4949             if (cmp_code == LT_EXPR)
4950               cmp_code = GT_EXPR;
4951             if (cmp_code == GE_EXPR)
4952               cmp_code = LE_EXPR;
4953             c1 = wi::max_value (to_type);
4954           }
4955         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
4956            compute (c3 op' c2) and check if it equals to c1 with op' being
4957            the inverted operator of op.  Make sure overflow doesn't happen
4958            if it is undefined.  */
4959         if (op == PLUS_EXPR)
4960           real_c1 = wi::sub (c3, c2, sgn, &overflow);
4961         else
4962           real_c1 = wi::add (c3, c2, sgn, &overflow);
4964         code = cmp_code;
4965         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4966           {
4967             /* Check if c1 equals to real_c1.  Boundary condition is handled
4968                by adjusting comparison operation if necessary.  */
4969             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4970                 && !overflow)
4971               {
4972                 /* X <= Y - 1 equals to X < Y.  */
4973                 if (cmp_code == LE_EXPR)
4974                   code = LT_EXPR;
4975                 /* X > Y - 1 equals to X >= Y.  */
4976                 if (cmp_code == GT_EXPR)
4977                   code = GE_EXPR;
4978               }
4979             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4980                 && !overflow)
4981               {
4982                 /* X < Y + 1 equals to X <= Y.  */
4983                 if (cmp_code == LT_EXPR)
4984                   code = LE_EXPR;
4985                 /* X >= Y + 1 equals to X > Y.  */
4986                 if (cmp_code == GE_EXPR)
4987                   code = GT_EXPR;
4988               }
4989             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4990               {
4991                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4992                   code = MIN_EXPR;
4993                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4994                   code = MAX_EXPR;
4995               }
4996           }
4997       }
4998       (if (code == MAX_EXPR)
4999        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5000            { wide_int_to_tree (from_type, c2); })
5001        (if (code == MIN_EXPR)
5002         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5003             { wide_int_to_tree (from_type, c2); })))))))))
5005 #if GIMPLE
5006 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5007    in fold_cond_expr_with_comparison for GENERIC folding with
5008    some extra constraints.  */
5009 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5010  (simplify
5011   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5012         (convert3? @0) (convert4? @1))
5013   (if (!HONOR_SIGNED_ZEROS (type)
5014        && (/* Allow widening conversions of the compare operands as data.  */
5015            (INTEGRAL_TYPE_P (type)
5016             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5017             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5018             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5019             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5020            /* Or sign conversions for the comparison.  */
5021            || (types_match (type, TREE_TYPE (@0))
5022                && types_match (type, TREE_TYPE (@1)))))
5023    (switch
5024     (if (cmp == EQ_EXPR)
5025      (if (VECTOR_TYPE_P (type))
5026       (view_convert @c1)
5027       (convert @c1)))
5028     (if (cmp == NE_EXPR)
5029      (if (VECTOR_TYPE_P (type))
5030       (view_convert @c0)
5031       (convert @c0)))
5032     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5033      (if (!HONOR_NANS (type))
5034       (if (VECTOR_TYPE_P (type))
5035        (view_convert (min @c0 @c1))
5036        (convert (min @c0 @c1)))))
5037     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5038      (if (!HONOR_NANS (type))
5039       (if (VECTOR_TYPE_P (type))
5040        (view_convert (max @c0 @c1))
5041        (convert (max @c0 @c1)))))
5042     (if (cmp == UNEQ_EXPR)
5043      (if (!HONOR_NANS (type))
5044       (if (VECTOR_TYPE_P (type))
5045        (view_convert @c1)
5046        (convert @c1))))
5047     (if (cmp == LTGT_EXPR)
5048      (if (!HONOR_NANS (type))
5049       (if (VECTOR_TYPE_P (type))
5050        (view_convert @c0)
5051        (convert @c0))))))))
5052 #endif
5054 /* These was part of minmax phiopt.  */
5055 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5056    to minmax<min/max<a, b>, c> */
5057 (for minmax (min max)
5058  (for cmp (lt le gt ge ne)
5059   (simplify
5060    (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5061    (with
5062     {
5063       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5064     }
5065     (if (code == MIN_EXPR)
5066      (minmax (min @1 @2) @4)
5067      (if (code == MAX_EXPR)
5068       (minmax (max @1 @2) @4)))))))
5070 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5071 (for cmp    (gt  ge  lt  le)
5072      minmax (min min max max)
5073  (simplify
5074   (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4)
5075    (with
5076     {
5077       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5078     }
5079     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5080          && code == MIN_EXPR
5081          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @1)))
5082      (min @2 @4)
5083      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5084           && code == MAX_EXPR
5085           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1)))
5086       (max @2 @4))))))
5088 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5089 (simplify
5090  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5091  (if (!TYPE_SATURATING (type)
5092       && (TYPE_OVERFLOW_WRAPS (type)
5093           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5094       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5095   @3))
5097 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5098 (simplify
5099  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5100  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5101   @3))
5103 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5104    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5105 (simplify
5106  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5107  (if (TYPE_UNSIGNED (type))
5108   (cond (ge @0 @1) (negate @0) @2)))
5110 (for cnd (cond vec_cond)
5111  /* A ? B : (A ? X : C) -> A ? B : C.  */
5112  (simplify
5113   (cnd @0 (cnd @0 @1 @2) @3)
5114   (cnd @0 @1 @3))
5115  (simplify
5116   (cnd @0 @1 (cnd @0 @2 @3))
5117   (cnd @0 @1 @3))
5118  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5119  /* ???  This matches embedded conditions open-coded because genmatch
5120     would generate matching code for conditions in separate stmts only.
5121     The following is still important to merge then and else arm cases
5122     from if-conversion.  */
5123  (simplify
5124   (cnd @0 @1 (cnd @2 @3 @4))
5125   (if (inverse_conditions_p (@0, @2))
5126    (cnd @0 @1 @3)))
5127  (simplify
5128   (cnd @0 (cnd @1 @2 @3) @4)
5129   (if (inverse_conditions_p (@0, @1))
5130    (cnd @0 @3 @4)))
5132  /* A ? B : B -> B.  */
5133  (simplify
5134   (cnd @0 @1 @1)
5135   @1)
5137  /* !A ? B : C -> A ? C : B.  */
5138  (simplify
5139   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5140   (cnd @0 @2 @1)))
5142 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5143    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5144    Need to handle UN* comparisons.
5146    None of these transformations work for modes with signed
5147    zeros.  If A is +/-0, the first two transformations will
5148    change the sign of the result (from +0 to -0, or vice
5149    versa).  The last four will fix the sign of the result,
5150    even though the original expressions could be positive or
5151    negative, depending on the sign of A.
5153    Note that all these transformations are correct if A is
5154    NaN, since the two alternatives (A and -A) are also NaNs.  */
5156 (for cnd (cond vec_cond)
5157  /* A == 0 ? A : -A    same as -A */
5158  (for cmp (eq uneq)
5159   (simplify
5160    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5161     (if (!HONOR_SIGNED_ZEROS (type))
5162      @1))
5163   (simplify
5164    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5165     (if (!HONOR_SIGNED_ZEROS (type))
5166      @1))
5168  /* A != 0 ? A : -A    same as A */
5169  (for cmp (ne ltgt)
5170   (simplify
5171    (cnd (cmp @0 zerop) @0 (negate @0))
5172     (if (!HONOR_SIGNED_ZEROS (type))
5173      @0))
5174   (simplify
5175    (cnd (cmp @0 zerop) @0 integer_zerop)
5176     (if (!HONOR_SIGNED_ZEROS (type))
5177      @0))
5179  /* A >=/> 0 ? A : -A    same as abs (A) */
5180  (for cmp (ge gt)
5181   (simplify
5182    (cnd (cmp @0 zerop) @0 (negate @0))
5183     (if (!HONOR_SIGNED_ZEROS (type)
5184          && !TYPE_UNSIGNED (type))
5185      (abs @0))))
5186  /* A <=/< 0 ? A : -A    same as -abs (A) */
5187  (for cmp (le lt)
5188   (simplify
5189    (cnd (cmp @0 zerop) @0 (negate @0))
5190     (if (!HONOR_SIGNED_ZEROS (type)
5191          && !TYPE_UNSIGNED (type))
5192      (if (ANY_INTEGRAL_TYPE_P (type)
5193           && !TYPE_OVERFLOW_WRAPS (type))
5194       (with {
5195         tree utype = unsigned_type_for (type);
5196        }
5197        (convert (negate (absu:utype @0))))
5198        (negate (abs @0)))))
5202 /* -(type)!A -> (type)A - 1.  */
5203 (simplify
5204  (negate (convert?:s (logical_inverted_value:s @0)))
5205  (if (INTEGRAL_TYPE_P (type)
5206       && TREE_CODE (type) != BOOLEAN_TYPE
5207       && TYPE_PRECISION (type) > 1
5208       && TREE_CODE (@0) == SSA_NAME
5209       && ssa_name_has_boolean_range (@0))
5210   (plus (convert:type @0) { build_all_ones_cst (type); })))
5212 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5213    return all -1 or all 0 results.  */
5214 /* ??? We could instead convert all instances of the vec_cond to negate,
5215    but that isn't necessarily a win on its own.  */
5216 (simplify
5217  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5218  (if (VECTOR_TYPE_P (type)
5219       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5220                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5221       && (TYPE_MODE (TREE_TYPE (type))
5222           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5223   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5225 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5226 (simplify
5227  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5228  (if (VECTOR_TYPE_P (type)
5229       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5230                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5231       && (TYPE_MODE (TREE_TYPE (type))
5232           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5233   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5236 /* Simplifications of comparisons.  */
5238 /* See if we can reduce the magnitude of a constant involved in a
5239    comparison by changing the comparison code.  This is a canonicalization
5240    formerly done by maybe_canonicalize_comparison_1.  */
5241 (for cmp  (le gt)
5242      acmp (lt ge)
5243  (simplify
5244   (cmp @0 uniform_integer_cst_p@1)
5245   (with { tree cst = uniform_integer_cst_p (@1); }
5246    (if (tree_int_cst_sgn (cst) == -1)
5247      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5248                                    wide_int_to_tree (TREE_TYPE (cst),
5249                                                      wi::to_wide (cst)
5250                                                      + 1)); })))))
5251 (for cmp  (ge lt)
5252      acmp (gt le)
5253  (simplify
5254   (cmp @0 uniform_integer_cst_p@1)
5255   (with { tree cst = uniform_integer_cst_p (@1); }
5256    (if (tree_int_cst_sgn (cst) == 1)
5257     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5258                                   wide_int_to_tree (TREE_TYPE (cst),
5259                                   wi::to_wide (cst) - 1)); })))))
5261 /* We can simplify a logical negation of a comparison to the
5262    inverted comparison.  As we cannot compute an expression
5263    operator using invert_tree_comparison we have to simulate
5264    that with expression code iteration.  */
5265 (for cmp (tcc_comparison)
5266      icmp (inverted_tcc_comparison)
5267      ncmp (inverted_tcc_comparison_with_nans)
5268  /* Ideally we'd like to combine the following two patterns
5269     and handle some more cases by using
5270       (logical_inverted_value (cmp @0 @1))
5271     here but for that genmatch would need to "inline" that.
5272     For now implement what forward_propagate_comparison did.  */
5273  (simplify
5274   (bit_not (cmp @0 @1))
5275   (if (VECTOR_TYPE_P (type)
5276        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5277    /* Comparison inversion may be impossible for trapping math,
5278       invert_tree_comparison will tell us.  But we can't use
5279       a computed operator in the replacement tree thus we have
5280       to play the trick below.  */
5281    (with { enum tree_code ic = invert_tree_comparison
5282              (cmp, HONOR_NANS (@0)); }
5283     (if (ic == icmp)
5284      (icmp @0 @1)
5285      (if (ic == ncmp)
5286       (ncmp @0 @1))))))
5287  (simplify
5288   (bit_xor (cmp @0 @1) integer_truep)
5289   (with { enum tree_code ic = invert_tree_comparison
5290             (cmp, HONOR_NANS (@0)); }
5291    (if (ic == icmp)
5292     (icmp @0 @1)
5293     (if (ic == ncmp)
5294      (ncmp @0 @1)))))
5295  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5296  (simplify
5297   (ne (cmp@2 @0 @1) integer_zerop)
5298   (if (types_match (type, TREE_TYPE (@2)))
5299    (cmp @0 @1)))
5300  (simplify
5301   (eq (cmp@2 @0 @1) integer_truep)
5302   (if (types_match (type, TREE_TYPE (@2)))
5303    (cmp @0 @1)))
5304  (simplify
5305   (ne (cmp@2 @0 @1) integer_truep)
5306   (if (types_match (type, TREE_TYPE (@2)))
5307    (with { enum tree_code ic = invert_tree_comparison
5308              (cmp, HONOR_NANS (@0)); }
5309     (if (ic == icmp)
5310      (icmp @0 @1)
5311      (if (ic == ncmp)
5312       (ncmp @0 @1))))))
5313  (simplify
5314   (eq (cmp@2 @0 @1) integer_zerop)
5315   (if (types_match (type, TREE_TYPE (@2)))
5316    (with { enum tree_code ic = invert_tree_comparison
5317              (cmp, HONOR_NANS (@0)); }
5318     (if (ic == icmp)
5319      (icmp @0 @1)
5320      (if (ic == ncmp)
5321       (ncmp @0 @1)))))))
5323 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5324    ??? The transformation is valid for the other operators if overflow
5325    is undefined for the type, but performing it here badly interacts
5326    with the transformation in fold_cond_expr_with_comparison which
5327    attempts to synthetize ABS_EXPR.  */
5328 (for cmp (eq ne)
5329  (for sub (minus pointer_diff)
5330   (simplify
5331    (cmp (sub@2 @0 @1) integer_zerop)
5332    (if (single_use (@2))
5333     (cmp @0 @1)))))
5335 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5336    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5337 (for cmp (lt ge)
5338  (simplify
5339   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5340    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5341         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5342         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5343     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5344 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5345    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5346 (simplify
5347  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5348   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5349        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5350        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5351    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5353 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5354    signed arithmetic case.  That form is created by the compiler
5355    often enough for folding it to be of value.  One example is in
5356    computing loop trip counts after Operator Strength Reduction.  */
5357 (for cmp (simple_comparison)
5358      scmp (swapped_simple_comparison)
5359  (simplify
5360   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5361   /* Handle unfolded multiplication by zero.  */
5362   (if (integer_zerop (@1))
5363    (cmp @1 @2)
5364    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5365         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5366         && single_use (@3))
5367     /* If @1 is negative we swap the sense of the comparison.  */
5368     (if (tree_int_cst_sgn (@1) < 0)
5369      (scmp @0 @2)
5370      (cmp @0 @2))))))
5372 /* For integral types with undefined overflow fold
5373    x * C1 == C2 into x == C2 / C1 or false.
5374    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5375    Z / 2^n Z.  */
5376 (for cmp (eq ne)
5377  (simplify
5378   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5379   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5380        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5381        && wi::to_wide (@1) != 0)
5382    (with { widest_int quot; }
5383     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5384                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5385      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5386      { constant_boolean_node (cmp == NE_EXPR, type); }))
5387    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5388         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5389         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5390     (cmp @0
5391      {
5392        tree itype = TREE_TYPE (@0);
5393        int p = TYPE_PRECISION (itype);
5394        wide_int m = wi::one (p + 1) << p;
5395        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5396        wide_int i = wide_int::from (wi::mod_inv (a, m),
5397                                     p, TYPE_SIGN (itype));
5398        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5399      })))))
5401 /* Simplify comparison of something with itself.  For IEEE
5402    floating-point, we can only do some of these simplifications.  */
5403 (for cmp (eq ge le)
5404  (simplify
5405   (cmp @0 @0)
5406   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5407        || ! tree_expr_maybe_nan_p (@0))
5408    { constant_boolean_node (true, type); }
5409    (if (cmp != EQ_EXPR
5410         /* With -ftrapping-math conversion to EQ loses an exception.  */
5411         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5412             || ! flag_trapping_math))
5413     (eq @0 @0)))))
5414 (for cmp (ne gt lt)
5415  (simplify
5416   (cmp @0 @0)
5417   (if (cmp != NE_EXPR
5418        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5419        || ! tree_expr_maybe_nan_p (@0))
5420    { constant_boolean_node (false, type); })))
5421 (for cmp (unle unge uneq)
5422  (simplify
5423   (cmp @0 @0)
5424   { constant_boolean_node (true, type); }))
5425 (for cmp (unlt ungt)
5426  (simplify
5427   (cmp @0 @0)
5428   (unordered @0 @0)))
5429 (simplify
5430  (ltgt @0 @0)
5431  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5432   { constant_boolean_node (false, type); }))
5434 /* x == ~x -> false */
5435 /* x != ~x -> true */
5436 (for cmp (eq ne)
5437  (simplify
5438   (cmp:c @0 (bit_not @0))
5439   { constant_boolean_node (cmp == NE_EXPR, type); }))
5441 /* Fold ~X op ~Y as Y op X.  */
5442 (for cmp (simple_comparison)
5443  (simplify
5444   (cmp (bit_not@2 @0) (bit_not@3 @1))
5445   (if (single_use (@2) && single_use (@3))
5446    (cmp @1 @0))))
5448 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5449 (for cmp (simple_comparison)
5450      scmp (swapped_simple_comparison)
5451  (simplify
5452   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5453   (if (single_use (@2)
5454        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5455    (scmp @0 (bit_not @1)))))
5457 (for cmp (simple_comparison)
5458  (simplify
5459   (cmp @0 REAL_CST@1)
5460   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5461   (switch
5462    /* a CMP (-0) -> a CMP 0  */
5463    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5464     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5465    /* (-0) CMP b -> 0 CMP b.  */
5466    (if (TREE_CODE (@0) == REAL_CST
5467         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5468     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5469    /* x != NaN is always true, other ops are always false.  */
5470    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5471         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5472         && !tree_expr_signaling_nan_p (@1)
5473         && !tree_expr_maybe_signaling_nan_p (@0))
5474     { constant_boolean_node (cmp == NE_EXPR, type); })
5475    /* NaN != y is always true, other ops are always false.  */
5476    (if (TREE_CODE (@0) == REAL_CST
5477         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5478         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5479         && !tree_expr_signaling_nan_p (@0)
5480         && !tree_expr_signaling_nan_p (@1))
5481     { constant_boolean_node (cmp == NE_EXPR, type); })
5482    /* Fold comparisons against infinity.  */
5483    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5484         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5485     (with
5486      {
5487        REAL_VALUE_TYPE max;
5488        enum tree_code code = cmp;
5489        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5490        if (neg)
5491          code = swap_tree_comparison (code);
5492      }
5493      (switch
5494       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5495       (if (code == GT_EXPR
5496            && !(HONOR_NANS (@0) && flag_trapping_math))
5497        { constant_boolean_node (false, type); })
5498       (if (code == LE_EXPR)
5499        /* x <= +Inf is always true, if we don't care about NaNs.  */
5500        (if (! HONOR_NANS (@0))
5501         { constant_boolean_node (true, type); }
5502         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5503            an "invalid" exception.  */
5504         (if (!flag_trapping_math)
5505          (eq @0 @0))))
5506       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5507          for == this introduces an exception for x a NaN.  */
5508       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5509            || code == GE_EXPR)
5510        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5511         (if (neg)
5512          (lt @0 { build_real (TREE_TYPE (@0), max); })
5513          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5514       /* x < +Inf is always equal to x <= DBL_MAX.  */
5515       (if (code == LT_EXPR)
5516        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5517         (if (neg)
5518          (ge @0 { build_real (TREE_TYPE (@0), max); })
5519          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5520       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5521          an exception for x a NaN so use an unordered comparison.  */
5522       (if (code == NE_EXPR)
5523        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5524         (if (! HONOR_NANS (@0))
5525          (if (neg)
5526           (ge @0 { build_real (TREE_TYPE (@0), max); })
5527           (le @0 { build_real (TREE_TYPE (@0), max); }))
5528          (if (neg)
5529           (unge @0 { build_real (TREE_TYPE (@0), max); })
5530           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5532  /* If this is a comparison of a real constant with a PLUS_EXPR
5533     or a MINUS_EXPR of a real constant, we can convert it into a
5534     comparison with a revised real constant as long as no overflow
5535     occurs when unsafe_math_optimizations are enabled.  */
5536  (if (flag_unsafe_math_optimizations)
5537   (for op (plus minus)
5538    (simplify
5539     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5540     (with
5541      {
5542        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5543                                TREE_TYPE (@1), @2, @1);
5544      }
5545      (if (tem && !TREE_OVERFLOW (tem))
5546       (cmp @0 { tem; }))))))
5548  /* Likewise, we can simplify a comparison of a real constant with
5549     a MINUS_EXPR whose first operand is also a real constant, i.e.
5550     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
5551     floating-point types only if -fassociative-math is set.  */
5552  (if (flag_associative_math)
5553   (simplify
5554    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5555    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5556     (if (tem && !TREE_OVERFLOW (tem))
5557      (cmp { tem; } @1)))))
5559  /* Fold comparisons against built-in math functions.  */
5560  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5561   (for sq (SQRT)
5562    (simplify
5563     (cmp (sq @0) REAL_CST@1)
5564     (switch
5565      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5566       (switch
5567        /* sqrt(x) < y is always false, if y is negative.  */
5568        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5569         { constant_boolean_node (false, type); })
5570        /* sqrt(x) > y is always true, if y is negative and we
5571           don't care about NaNs, i.e. negative values of x.  */
5572        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5573         { constant_boolean_node (true, type); })
5574        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5575        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5576      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5577       (switch
5578        /* sqrt(x) < 0 is always false.  */
5579        (if (cmp == LT_EXPR)
5580         { constant_boolean_node (false, type); })
5581        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
5582        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5583         { constant_boolean_node (true, type); })
5584        /* sqrt(x) <= 0 -> x == 0.  */
5585        (if (cmp == LE_EXPR)
5586         (eq @0 @1))
5587        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
5588           == or !=.  In the last case:
5590             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5592           if x is negative or NaN.  Due to -funsafe-math-optimizations,
5593           the results for other x follow from natural arithmetic.  */
5594        (cmp @0 @1)))
5595      (if ((cmp == LT_EXPR
5596            || cmp == LE_EXPR
5597            || cmp == GT_EXPR
5598            || cmp == GE_EXPR)
5599           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5600           /* Give up for -frounding-math.  */
5601           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5602       (with
5603        {
5604          REAL_VALUE_TYPE c2;
5605          enum tree_code ncmp = cmp;
5606          const real_format *fmt
5607            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5608          real_arithmetic (&c2, MULT_EXPR,
5609                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5610          real_convert (&c2, fmt, &c2);
5611          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5612             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
5613          if (!REAL_VALUE_ISINF (c2))
5614            {
5615              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5616                                         build_real (TREE_TYPE (@0), c2));
5617              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5618                ncmp = ERROR_MARK;
5619              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5620                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5621                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5622              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5623                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5624                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5625              else
5626                {
5627                  /* With rounding to even, sqrt of up to 3 different values
5628                     gives the same normal result, so in some cases c2 needs
5629                     to be adjusted.  */
5630                  REAL_VALUE_TYPE c2alt, tow;
5631                  if (cmp == LT_EXPR || cmp == GE_EXPR)
5632                    tow = dconst0;
5633                  else
5634                    tow = dconstinf;
5635                  real_nextafter (&c2alt, fmt, &c2, &tow);
5636                  real_convert (&c2alt, fmt, &c2alt);
5637                  if (REAL_VALUE_ISINF (c2alt))
5638                    ncmp = ERROR_MARK;
5639                  else
5640                    {
5641                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5642                                            build_real (TREE_TYPE (@0), c2alt));
5643                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5644                        ncmp = ERROR_MARK;
5645                      else if (real_equal (&TREE_REAL_CST (c3),
5646                                           &TREE_REAL_CST (@1)))
5647                        c2 = c2alt;
5648                    }
5649                }
5650            }
5651        }
5652        (if (cmp == GT_EXPR || cmp == GE_EXPR)
5653         (if (REAL_VALUE_ISINF (c2))
5654          /* sqrt(x) > y is x == +Inf, when y is very large.  */
5655          (if (HONOR_INFINITIES (@0))
5656           (eq @0 { build_real (TREE_TYPE (@0), c2); })
5657           { constant_boolean_node (false, type); })
5658          /* sqrt(x) > c is the same as x > c*c.  */
5659          (if (ncmp != ERROR_MARK)
5660           (if (ncmp == GE_EXPR)
5661            (ge @0 { build_real (TREE_TYPE (@0), c2); })
5662            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5663         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
5664         (if (REAL_VALUE_ISINF (c2))
5665          (switch
5666           /* sqrt(x) < y is always true, when y is a very large
5667              value and we don't care about NaNs or Infinities.  */
5668           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5669            { constant_boolean_node (true, type); })
5670           /* sqrt(x) < y is x != +Inf when y is very large and we
5671              don't care about NaNs.  */
5672           (if (! HONOR_NANS (@0))
5673            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5674           /* sqrt(x) < y is x >= 0 when y is very large and we
5675              don't care about Infinities.  */
5676           (if (! HONOR_INFINITIES (@0))
5677            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5678           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5679           (if (GENERIC)
5680            (truth_andif
5681             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5682             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5683          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5684          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5685           (if (ncmp == LT_EXPR)
5686            (lt @0 { build_real (TREE_TYPE (@0), c2); })
5687            (le @0 { build_real (TREE_TYPE (@0), c2); }))
5688           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5689           (if (ncmp != ERROR_MARK && GENERIC)
5690            (if (ncmp == LT_EXPR)
5691             (truth_andif
5692              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5693              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5694             (truth_andif
5695              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5696              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5697    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
5698    (simplify
5699     (cmp (sq @0) (sq @1))
5700       (if (! HONOR_NANS (@0))
5701         (cmp @0 @1))))))
5703 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
5704 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5705      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
5706  (simplify
5707   (cmp (float@0 @1) (float @2))
5708    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5709         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5710     (with
5711      {
5712        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5713        tree type1 = TREE_TYPE (@1);
5714        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5715        tree type2 = TREE_TYPE (@2);
5716        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5717      }
5718      (if (fmt.can_represent_integral_type_p (type1)
5719           && fmt.can_represent_integral_type_p (type2))
5720       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5721        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5722        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5723             && type1_signed_p >= type2_signed_p)
5724         (icmp @1 (convert @2))
5725         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5726              && type1_signed_p <= type2_signed_p)
5727          (icmp (convert:type2 @1) @2)
5728          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5729               && type1_signed_p == type2_signed_p)
5730           (icmp @1 @2))))))))))
5732 /* Optimize various special cases of (FTYPE) N CMP CST.  */
5733 (for cmp  (lt le eq ne ge gt)
5734      icmp (le le eq ne ge ge)
5735  (simplify
5736   (cmp (float @0) REAL_CST@1)
5737    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5738         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5739     (with
5740      {
5741        tree itype = TREE_TYPE (@0);
5742        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5743        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5744        /* Be careful to preserve any potential exceptions due to
5745           NaNs.  qNaNs are ok in == or != context.
5746           TODO: relax under -fno-trapping-math or
5747           -fno-signaling-nans.  */
5748        bool exception_p
5749          = real_isnan (cst) && (cst->signalling
5750                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5751      }
5752      /* TODO: allow non-fitting itype and SNaNs when
5753         -fno-trapping-math.  */
5754      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5755       (with
5756        {
5757          signop isign = TYPE_SIGN (itype);
5758          REAL_VALUE_TYPE imin, imax;
5759          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5760          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5762          REAL_VALUE_TYPE icst;
5763          if (cmp == GT_EXPR || cmp == GE_EXPR)
5764            real_ceil (&icst, fmt, cst);
5765          else if (cmp == LT_EXPR || cmp == LE_EXPR)
5766            real_floor (&icst, fmt, cst);
5767          else
5768            real_trunc (&icst, fmt, cst);
5770          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5772          bool overflow_p = false;
5773          wide_int icst_val
5774            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5775        }
5776        (switch
5777         /* Optimize cases when CST is outside of ITYPE's range.  */
5778         (if (real_compare (LT_EXPR, cst, &imin))
5779          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5780                                   type); })
5781         (if (real_compare (GT_EXPR, cst, &imax))
5782          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5783                                   type); })
5784         /* Remove cast if CST is an integer representable by ITYPE.  */
5785         (if (cst_int_p)
5786          (cmp @0 { gcc_assert (!overflow_p);
5787                    wide_int_to_tree (itype, icst_val); })
5788         )
5789         /* When CST is fractional, optimize
5790             (FTYPE) N == CST -> 0
5791             (FTYPE) N != CST -> 1.  */
5792         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5793          { constant_boolean_node (cmp == NE_EXPR, type); })
5794         /* Otherwise replace with sensible integer constant.  */
5795         (with
5796          {
5797            gcc_checking_assert (!overflow_p);
5798          }
5799          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5801 /* Fold A /[ex] B CMP C to A CMP B * C.  */
5802 (for cmp (eq ne)
5803  (simplify
5804   (cmp (exact_div @0 @1) INTEGER_CST@2)
5805   (if (!integer_zerop (@1))
5806    (if (wi::to_wide (@2) == 0)
5807     (cmp @0 @2)
5808     (if (TREE_CODE (@1) == INTEGER_CST)
5809      (with
5810       {
5811         wi::overflow_type ovf;
5812         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5813                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5814       }
5815       (if (ovf)
5816        { constant_boolean_node (cmp == NE_EXPR, type); }
5817        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5818 (for cmp (lt le gt ge)
5819  (simplify
5820   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5821   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5822    (with
5823     {
5824       wi::overflow_type ovf;
5825       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5826                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5827     }
5828     (if (ovf)
5829      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5830                                         TYPE_SIGN (TREE_TYPE (@2)))
5831                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5832      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5834 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5836    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5837    For large C (more than min/B+2^size), this is also true, with the
5838    multiplication computed modulo 2^size.
5839    For intermediate C, this just tests the sign of A.  */
5840 (for cmp  (lt le gt ge)
5841      cmp2 (ge ge lt lt)
5842  (simplify
5843   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5844   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5845        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5846        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5847    (with
5848     {
5849       tree utype = TREE_TYPE (@2);
5850       wide_int denom = wi::to_wide (@1);
5851       wide_int right = wi::to_wide (@2);
5852       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5853       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5854       bool small = wi::leu_p (right, smax);
5855       bool large = wi::geu_p (right, smin);
5856     }
5857     (if (small || large)
5858      (cmp (convert:utype @0) (mult @2 (convert @1)))
5859      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5861 /* Unordered tests if either argument is a NaN.  */
5862 (simplify
5863  (bit_ior (unordered @0 @0) (unordered @1 @1))
5864  (if (types_match (@0, @1))
5865   (unordered @0 @1)))
5866 (simplify
5867  (bit_and (ordered @0 @0) (ordered @1 @1))
5868  (if (types_match (@0, @1))
5869   (ordered @0 @1)))
5870 (simplify
5871  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5872  @2)
5873 (simplify
5874  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5875  @2)
5877 /* Simple range test simplifications.  */
5878 /* A < B || A >= B -> true.  */
5879 (for test1 (lt le le le ne ge)
5880      test2 (ge gt ge ne eq ne)
5881  (simplify
5882   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5883   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5884        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5885    { constant_boolean_node (true, type); })))
5886 /* A < B && A >= B -> false.  */
5887 (for test1 (lt lt lt le ne eq)
5888      test2 (ge gt eq gt eq gt)
5889  (simplify
5890   (bit_and:c (test1 @0 @1) (test2 @0 @1))
5891   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5892        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5893    { constant_boolean_node (false, type); })))
5895 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5896    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
5898    Note that comparisons
5899      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
5900      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
5901    will be canonicalized to above so there's no need to
5902    consider them here.
5903  */
5905 (for cmp (le gt)
5906      eqcmp (eq ne)
5907  (simplify
5908   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5909   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5910    (with
5911     {
5912      tree ty = TREE_TYPE (@0);
5913      unsigned prec = TYPE_PRECISION (ty);
5914      wide_int mask = wi::to_wide (@2, prec);
5915      wide_int rhs = wi::to_wide (@3, prec);
5916      signop sgn = TYPE_SIGN (ty);
5917     }
5918     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5919          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5920       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5921              { build_zero_cst (ty); }))))))
5923 /* -A CMP -B -> B CMP A.  */
5924 (for cmp (tcc_comparison)
5925      scmp (swapped_tcc_comparison)
5926  (simplify
5927   (cmp (negate @0) (negate @1))
5928   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5929        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5930            && (cmp == EQ_EXPR
5931                || cmp == NE_EXPR
5932                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
5933    (scmp @0 @1)))
5934  (simplify
5935   (cmp (negate @0) CONSTANT_CLASS_P@1)
5936   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5937        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5938            && (cmp == EQ_EXPR
5939                || cmp == NE_EXPR
5940                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
5941    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5942     (if (tem && !TREE_OVERFLOW (tem))
5943      (scmp @0 { tem; }))))))
5945 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
5946 (for op (abs absu)
5947  (for eqne (eq ne)
5948   (simplify
5949    (eqne (op @0) zerop@1)
5950    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5952 /* From fold_sign_changed_comparison and fold_widened_comparison.
5953    FIXME: the lack of symmetry is disturbing.  */
5954 (for cmp (simple_comparison)
5955  (simplify
5956   (cmp (convert@0 @00) (convert?@1 @10))
5957   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5958        /* Disable this optimization if we're casting a function pointer
5959           type on targets that require function pointer canonicalization.  */
5960        && !(targetm.have_canonicalize_funcptr_for_compare ()
5961             && ((POINTER_TYPE_P (TREE_TYPE (@00))
5962                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5963                 || (POINTER_TYPE_P (TREE_TYPE (@10))
5964                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5965        && single_use (@0))
5966    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5967         && (TREE_CODE (@10) == INTEGER_CST
5968             || @1 != @10)
5969         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5970             || cmp == NE_EXPR
5971             || cmp == EQ_EXPR)
5972         && !POINTER_TYPE_P (TREE_TYPE (@00))
5973         /* (int)bool:32 != (int)uint is not the same as
5974            bool:32 != (bool:32)uint since boolean types only have two valid
5975            values independent of their precision.  */
5976         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5977             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5978     /* ???  The special-casing of INTEGER_CST conversion was in the original
5979        code and here to avoid a spurious overflow flag on the resulting
5980        constant which fold_convert produces.  */
5981     (if (TREE_CODE (@1) == INTEGER_CST)
5982      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5983                                 TREE_OVERFLOW (@1)); })
5984      (cmp @00 (convert @1)))
5986     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5987      /* If possible, express the comparison in the shorter mode.  */
5988      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5989            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5990            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5991                && TYPE_UNSIGNED (TREE_TYPE (@00))))
5992           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5993               || ((TYPE_PRECISION (TREE_TYPE (@00))
5994                    >= TYPE_PRECISION (TREE_TYPE (@10)))
5995                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
5996                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
5997               || (TREE_CODE (@10) == INTEGER_CST
5998                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5999                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6000       (cmp @00 (convert @10))
6001       (if (TREE_CODE (@10) == INTEGER_CST
6002            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6003            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6004        (with
6005         {
6006           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6007           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6008           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6009           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6010         }
6011         (if (above || below)
6012          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6013           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6014           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6015            { constant_boolean_node (above ? true : false, type); }
6016            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6017             { constant_boolean_node (above ? false : true, type); })))))))))
6018    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6019    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6020         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6021             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6022         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6023             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6024     (with
6025      {
6026        tree type1 = TREE_TYPE (@10);
6027        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6028          {
6029            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6030            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6031                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6032              type1 = float_type_node;
6033            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6034                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6035              type1 = double_type_node;
6036          }
6037       tree newtype
6038         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6039            ? TREE_TYPE (@00) : type1);
6040      }
6041      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6042       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6045 (for cmp (eq ne)
6046  (simplify
6047   /* SSA names are canonicalized to 2nd place.  */
6048   (cmp addr@0 SSA_NAME@1)
6049   (with
6050    {
6051      poly_int64 off; tree base;
6052      tree addr = (TREE_CODE (@0) == SSA_NAME
6053                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6054    }
6055    /* A local variable can never be pointed to by
6056       the default SSA name of an incoming parameter.  */
6057    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6058         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6059         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6060         && TREE_CODE (base) == VAR_DECL
6061         && auto_var_in_fn_p (base, current_function_decl))
6062     (if (cmp == NE_EXPR)
6063      { constant_boolean_node (true, type); }
6064      { constant_boolean_node (false, type); })
6065     /* If the address is based on @1 decide using the offset.  */
6066     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6067          && TREE_CODE (base) == MEM_REF
6068          && TREE_OPERAND (base, 0) == @1)
6069      (with { off += mem_ref_offset (base).force_shwi (); }
6070       (if (known_ne (off, 0))
6071        { constant_boolean_node (cmp == NE_EXPR, type); }
6072        (if (known_eq (off, 0))
6073         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6075 /* Equality compare simplifications from fold_binary  */
6076 (for cmp (eq ne)
6078  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6079     Similarly for NE_EXPR.  */
6080  (simplify
6081   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6082   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6083        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6084    { constant_boolean_node (cmp == NE_EXPR, type); }))
6086  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6087  (simplify
6088   (cmp (bit_xor @0 @1) integer_zerop)
6089   (cmp @0 @1))
6091  /* (X ^ Y) == Y becomes X == 0.
6092     Likewise (X ^ Y) == X becomes Y == 0.  */
6093  (simplify
6094   (cmp:c (bit_xor:c @0 @1) @0)
6095   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6097  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6098  (simplify
6099   (cmp:c (bit_and:c @0 @1) @0)
6100   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6101  (simplify
6102   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6103   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6104        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6105        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6106        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6107        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6108        && !wi::neg_p (wi::to_wide (@1)))
6109    (cmp (bit_and @0 (convert (bit_not @1)))
6110         { build_zero_cst (TREE_TYPE (@0)); })))
6112  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6113  (simplify
6114   (cmp:c (bit_ior:c @0 @1) @1)
6115   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6117  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6118  (simplify
6119   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6120   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6121    (cmp @0 (bit_xor @1 (convert @2)))))
6123  (simplify
6124   (cmp (nop_convert? @0) integer_zerop)
6125   (if (tree_expr_nonzero_p (@0))
6126    { constant_boolean_node (cmp == NE_EXPR, type); }))
6128  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6129  (simplify
6130   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6131   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6133 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6134    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6135    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6136    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6137 (for cmp (eq ne)
6138      ncmp (ge lt)
6139  (for sgncmp (ge lt)
6140   (simplify
6141    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6142    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6143         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6144         && types_match (@0, @1))
6145     (ncmp (bit_xor @0 @1) @2)))))
6146 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6147    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6148 (for cmp (eq ne)
6149      ncmp (lt ge)
6150  (simplify
6151   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6152    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6153         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6154         && types_match (@0, @1))
6155     (ncmp (bit_xor @0 @1) @2))))
6157 /* If we have (A & C) == C where C is a power of 2, convert this into
6158    (A & C) != 0.  Similarly for NE_EXPR.  */
6159 (for cmp (eq ne)
6160      icmp (ne eq)
6161  (simplify
6162   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6163   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6165 #if GIMPLE
6166 /* From fold_binary_op_with_conditional_arg handle the case of
6167    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6168    compares simplify.  */
6169 (for cmp (simple_comparison)
6170  (simplify
6171   (cmp:c (cond @0 @1 @2) @3)
6172   /* Do not move possibly trapping operations into the conditional as this
6173      pessimizes code and causes gimplification issues when applied late.  */
6174   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6175        || !operation_could_trap_p (cmp, true, false, @3))
6176    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6177 #endif
6179 (for cmp (ge lt)
6180 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6181 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6182  (simplify
6183   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6184    (if (INTEGRAL_TYPE_P (type)
6185         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6186         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6187         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6188     (with
6189      {
6190        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6191      }
6192     (if (cmp == LT_EXPR)
6193      (bit_xor (convert (rshift @0 {shifter;})) @1)
6194      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6195 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6196 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6197  (simplify
6198   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6199    (if (INTEGRAL_TYPE_P (type)
6200         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6201         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6202         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6203     (with
6204      {
6205        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6206      }
6207     (if (cmp == GE_EXPR)
6208      (bit_xor (convert (rshift @0 {shifter;})) @1)
6209      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6211 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6212    convert this into a shift followed by ANDing with D.  */
6213 (simplify
6214  (cond
6215   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6216   INTEGER_CST@2 integer_zerop)
6217  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6218   (with {
6219      int shift = (wi::exact_log2 (wi::to_wide (@2))
6220                   - wi::exact_log2 (wi::to_wide (@1)));
6221    }
6222    (if (shift > 0)
6223     (bit_and
6224      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6225     (bit_and
6226      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6227      @2)))))
6229 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6230    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6231 (for cmp (eq ne)
6232      ncmp (ge lt)
6233  (simplify
6234   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6235   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6236        && type_has_mode_precision_p (TREE_TYPE (@0))
6237        && element_precision (@2) >= element_precision (@0)
6238        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6239    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6240     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6242 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6243    this into a right shift or sign extension followed by ANDing with C.  */
6244 (simplify
6245  (cond
6246   (lt @0 integer_zerop)
6247   INTEGER_CST@1 integer_zerop)
6248  (if (integer_pow2p (@1)
6249       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6250   (with {
6251     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6252    }
6253    (if (shift >= 0)
6254     (bit_and
6255      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6256      @1)
6257     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6258        sign extension followed by AND with C will achieve the effect.  */
6259     (bit_and (convert @0) @1)))))
6261 /* When the addresses are not directly of decls compare base and offset.
6262    This implements some remaining parts of fold_comparison address
6263    comparisons but still no complete part of it.  Still it is good
6264    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6265 (for cmp (simple_comparison)
6266  (simplify
6267   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6268   (with
6269    {
6270      poly_int64 off0, off1;
6271      tree base0, base1;
6272      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6273                                   off0, off1, GENERIC);
6274    }
6275    (if (equal == 1)
6276     (switch
6277      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6278       { constant_boolean_node (known_eq (off0, off1), type); })
6279      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6280       { constant_boolean_node (known_ne (off0, off1), type); })
6281      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6282       { constant_boolean_node (known_lt (off0, off1), type); })
6283      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6284       { constant_boolean_node (known_le (off0, off1), type); })
6285      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6286       { constant_boolean_node (known_ge (off0, off1), type); })
6287      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6288       { constant_boolean_node (known_gt (off0, off1), type); }))
6289     (if (equal == 0)
6290      (switch
6291       (if (cmp == EQ_EXPR)
6292        { constant_boolean_node (false, type); })
6293       (if (cmp == NE_EXPR)
6294        { constant_boolean_node (true, type); })))))))
6296 /* Simplify pointer equality compares using PTA.  */
6297 (for neeq (ne eq)
6298  (simplify
6299   (neeq @0 @1)
6300   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6301        && ptrs_compare_unequal (@0, @1))
6302    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6304 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6305    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6306    Disable the transform if either operand is pointer to function.
6307    This broke pr22051-2.c for arm where function pointer
6308    canonicalizaion is not wanted.  */
6310 (for cmp (ne eq)
6311  (simplify
6312   (cmp (convert @0) INTEGER_CST@1)
6313   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6314          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6315          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6316          /* Don't perform this optimization in GENERIC if @0 has reference
6317             type when sanitizing.  See PR101210.  */
6318          && !(GENERIC
6319               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6320               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6321         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6322             && POINTER_TYPE_P (TREE_TYPE (@1))
6323             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6324        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6325    (cmp @0 (convert @1)))))
6327 /* Non-equality compare simplifications from fold_binary  */
6328 (for cmp (lt gt le ge)
6329  /* Comparisons with the highest or lowest possible integer of
6330     the specified precision will have known values.  */
6331  (simplify
6332   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6333   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6334         || POINTER_TYPE_P (TREE_TYPE (@1))
6335         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6336        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6337    (with
6338     {
6339       tree cst = uniform_integer_cst_p (@1);
6340       tree arg1_type = TREE_TYPE (cst);
6341       unsigned int prec = TYPE_PRECISION (arg1_type);
6342       wide_int max = wi::max_value (arg1_type);
6343       wide_int signed_max = wi::max_value (prec, SIGNED);
6344       wide_int min = wi::min_value (arg1_type);
6345     }
6346     (switch
6347      (if (wi::to_wide (cst) == max)
6348       (switch
6349        (if (cmp == GT_EXPR)
6350         { constant_boolean_node (false, type); })
6351        (if (cmp == GE_EXPR)
6352         (eq @2 @1))
6353        (if (cmp == LE_EXPR)
6354         { constant_boolean_node (true, type); })
6355        (if (cmp == LT_EXPR)
6356         (ne @2 @1))))
6357      (if (wi::to_wide (cst) == min)
6358       (switch
6359        (if (cmp == LT_EXPR)
6360         { constant_boolean_node (false, type); })
6361        (if (cmp == LE_EXPR)
6362         (eq @2 @1))
6363        (if (cmp == GE_EXPR)
6364         { constant_boolean_node (true, type); })
6365        (if (cmp == GT_EXPR)
6366         (ne @2 @1))))
6367      (if (wi::to_wide (cst) == max - 1)
6368       (switch
6369        (if (cmp == GT_EXPR)
6370         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6371                                     wide_int_to_tree (TREE_TYPE (cst),
6372                                                       wi::to_wide (cst)
6373                                                       + 1)); }))
6374        (if (cmp == LE_EXPR)
6375         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6376                                     wide_int_to_tree (TREE_TYPE (cst),
6377                                                       wi::to_wide (cst)
6378                                                       + 1)); }))))
6379      (if (wi::to_wide (cst) == min + 1)
6380       (switch
6381        (if (cmp == GE_EXPR)
6382         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6383                                     wide_int_to_tree (TREE_TYPE (cst),
6384                                                       wi::to_wide (cst)
6385                                                       - 1)); }))
6386        (if (cmp == LT_EXPR)
6387         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6388                                     wide_int_to_tree (TREE_TYPE (cst),
6389                                                       wi::to_wide (cst)
6390                                                       - 1)); }))))
6391      (if (wi::to_wide (cst) == signed_max
6392           && TYPE_UNSIGNED (arg1_type)
6393           /* We will flip the signedness of the comparison operator
6394              associated with the mode of @1, so the sign bit is
6395              specified by this mode.  Check that @1 is the signed
6396              max associated with this sign bit.  */
6397           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6398           /* signed_type does not work on pointer types.  */
6399           && INTEGRAL_TYPE_P (arg1_type))
6400       /* The following case also applies to X < signed_max+1
6401          and X >= signed_max+1 because previous transformations.  */
6402       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6403        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6404         (switch
6405          (if (cst == @1 && cmp == LE_EXPR)
6406           (ge (convert:st @0) { build_zero_cst (st); }))
6407          (if (cst == @1 && cmp == GT_EXPR)
6408           (lt (convert:st @0) { build_zero_cst (st); }))
6409          (if (cmp == LE_EXPR)
6410           (ge (view_convert:st @0) { build_zero_cst (st); }))
6411          (if (cmp == GT_EXPR)
6412           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6414 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
6415 (simplify
6416  (lt:c @0 (convert (ne @0 integer_zerop)))
6417  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6418   { constant_boolean_node (false, type); }))
6420 /* x != (typeof x)(x == 0) is always true.  */
6421 (simplify
6422  (ne:c @0 (convert (eq @0 integer_zerop)))
6423  { constant_boolean_node (true, type); })
6425 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6426  /* If the second operand is NaN, the result is constant.  */
6427  (simplify
6428   (cmp @0 REAL_CST@1)
6429   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6430        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6431    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6432                             ? false : true, type); })))
6434 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6435 (simplify
6436   (unordered @0 @1)
6437   (switch
6438     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6439         { constant_boolean_node (true, type); })
6440     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6441         { constant_boolean_node (false, type); })))
6443 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6444 (simplify
6445   (ordered @0 @1)
6446   (switch
6447     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6448         { constant_boolean_node (false, type); })
6449     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6450         { constant_boolean_node (true, type); })))
6452 /* bool_var != 0 becomes bool_var.  */
6453 (simplify
6454  (ne @0 integer_zerop)
6455  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6456       && types_match (type, TREE_TYPE (@0)))
6457   (non_lvalue @0)))
6458 /* bool_var == 1 becomes bool_var.  */
6459 (simplify
6460  (eq @0 integer_onep)
6461  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6462       && types_match (type, TREE_TYPE (@0)))
6463   (non_lvalue @0)))
6464 /* Do not handle
6465    bool_var == 0 becomes !bool_var or
6466    bool_var != 1 becomes !bool_var
6467    here because that only is good in assignment context as long
6468    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6469    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6470    clearly less optimal and which we'll transform again in forwprop.  */
6472 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6473    where ~Y + 1 == pow2 and Z = ~Y.  */
6474 (for cst (VECTOR_CST INTEGER_CST)
6475  (for cmp (eq ne)
6476       icmp (le gt)
6477   (simplify
6478    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6479     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6480      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6481       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6482                          ? optab_vector : optab_default;
6483               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6484        (if (target_supports_op_p (utype, icmp, optab)
6485             || (optimize_vectors_before_lowering_p ()
6486                 && (!target_supports_op_p (type, cmp, optab)
6487                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6488         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6489          (icmp @0 { csts; })
6490          (icmp (view_convert:utype @0) { csts; })))))))))
6492 /* When one argument is a constant, overflow detection can be simplified.
6493    Currently restricted to single use so as not to interfere too much with
6494    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6495    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6496 (for cmp (lt le ge gt)
6497      out (gt gt le le)
6498  (simplify
6499   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6500   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6501        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6502        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6503        && wi::to_wide (@1) != 0
6504        && single_use (@2))
6505    (with {
6506      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6507      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6508     }
6509     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6510                                 wi::max_value (prec, sign)
6511                                 - wi::to_wide (@1)); })))))
6513 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6514    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6515    expects the long form, so we restrict the transformation for now.  */
6516 (for cmp (gt le)
6517  (simplify
6518   (cmp:c (minus@2 @0 @1) @0)
6519   (if (single_use (@2)
6520        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6521        && TYPE_UNSIGNED (TREE_TYPE (@0)))
6522    (cmp @1 @0))))
6524 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
6525 (for cmp (ge lt)
6526  (simplify
6527   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6528    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6529         && TYPE_UNSIGNED (TREE_TYPE (@0)))
6530     (cmp @1 @0))))
6532 /* Testing for overflow is unnecessary if we already know the result.  */
6533 /* A - B > A  */
6534 (for cmp (gt le)
6535      out (ne eq)
6536  (simplify
6537   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6538   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6539        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6540    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6541 /* A + B < A  */
6542 (for cmp (lt ge)
6543      out (ne eq)
6544  (simplify
6545   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6546   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6547        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6548    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6550 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6551    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
6552 (for cmp (lt ge)
6553      out (ne eq)
6554  (simplify
6555   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6556   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6557    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6558     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6560 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6561    is at least twice as wide as type of A and B, simplify to
6562    __builtin_mul_overflow (A, B, <unused>).  */
6563 (for cmp (eq ne)
6564  (simplify
6565   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6566        integer_zerop)
6567   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6568        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6569        && TYPE_UNSIGNED (TREE_TYPE (@0))
6570        && (TYPE_PRECISION (TREE_TYPE (@3))
6571            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6572        && tree_fits_uhwi_p (@2)
6573        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6574        && types_match (@0, @1)
6575        && type_has_mode_precision_p (TREE_TYPE (@0))
6576        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6577            != CODE_FOR_nothing))
6578    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6579     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6581 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
6582 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6583  (simplify
6584   (ovf (convert@2 @0) @1)
6585   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6586        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6587        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6588        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6589    (ovf @0 @1)))
6590  (simplify
6591   (ovf @1 (convert@2 @0))
6592   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6593        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6594        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6595        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6596    (ovf @1 @0))))
6598 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6599    are unsigned to x > (umax / cst).  Similarly for signed type, but
6600    in that case it needs to be outside of a range.  */
6601 (simplify
6602  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6603   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6604        && TYPE_MAX_VALUE (TREE_TYPE (@0))
6605        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6606        && int_fits_type_p (@1, TREE_TYPE (@0)))
6607    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6608     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6609     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6610      (if (integer_minus_onep (@1))
6611       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6612       (with
6613        {
6614          tree div = fold_convert (TREE_TYPE (@0), @1);
6615          tree lo = int_const_binop (TRUNC_DIV_EXPR,
6616                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6617          tree hi = int_const_binop (TRUNC_DIV_EXPR,
6618                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6619          tree etype = range_check_type (TREE_TYPE (@0));
6620          if (etype)
6621            {
6622              if (wi::neg_p (wi::to_wide (div)))
6623                std::swap (lo, hi);
6624              lo = fold_convert (etype, lo);
6625              hi = fold_convert (etype, hi);
6626              hi = int_const_binop (MINUS_EXPR, hi, lo);
6627            }
6628        }
6629        (if (etype)
6630         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6632 /* Simplification of math builtins.  These rules must all be optimizations
6633    as well as IL simplifications.  If there is a possibility that the new
6634    form could be a pessimization, the rule should go in the canonicalization
6635    section that follows this one.
6637    Rules can generally go in this section if they satisfy one of
6638    the following:
6640    - the rule describes an identity
6642    - the rule replaces calls with something as simple as addition or
6643      multiplication
6645    - the rule contains unary calls only and simplifies the surrounding
6646      arithmetic.  (The idea here is to exclude non-unary calls in which
6647      one operand is constant and in which the call is known to be cheap
6648      when the operand has that value.)  */
6650 (if (flag_unsafe_math_optimizations)
6651  /* Simplify sqrt(x) * sqrt(x) -> x.  */
6652  (simplify
6653   (mult (SQRT_ALL@1 @0) @1)
6654   (if (!tree_expr_maybe_signaling_nan_p (@0))
6655    @0))
6657  (for op (plus minus)
6658   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
6659   (simplify
6660    (op (rdiv @0 @1)
6661        (rdiv @2 @1))
6662    (rdiv (op @0 @2) @1)))
6664  (for cmp (lt le gt ge)
6665       neg_cmp (gt ge lt le)
6666   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
6667   (simplify
6668    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6669    (with
6670     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6671     (if (tem
6672          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6673               || (real_zerop (tem) && !real_zerop (@1))))
6674      (switch
6675       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6676        (cmp @0 { tem; }))
6677       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6678        (neg_cmp @0 { tem; })))))))
6680  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
6681  (for root (SQRT CBRT)
6682   (simplify
6683    (mult (root:s @0) (root:s @1))
6684     (root (mult @0 @1))))
6686  /* Simplify expN(x) * expN(y) -> expN(x+y). */
6687  (for exps (EXP EXP2 EXP10 POW10)
6688   (simplify
6689    (mult (exps:s @0) (exps:s @1))
6690     (exps (plus @0 @1))))
6692  /* Simplify a/root(b/c) into a*root(c/b).  */
6693  (for root (SQRT CBRT)
6694   (simplify
6695    (rdiv @0 (root:s (rdiv:s @1 @2)))
6696     (mult @0 (root (rdiv @2 @1)))))
6698  /* Simplify x/expN(y) into x*expN(-y).  */
6699  (for exps (EXP EXP2 EXP10 POW10)
6700   (simplify
6701    (rdiv @0 (exps:s @1))
6702     (mult @0 (exps (negate @1)))))
6704  (for logs (LOG LOG2 LOG10 LOG10)
6705       exps (EXP EXP2 EXP10 POW10)
6706   /* logN(expN(x)) -> x.  */
6707   (simplify
6708    (logs (exps @0))
6709    @0)
6710   /* expN(logN(x)) -> x.  */
6711   (simplify
6712    (exps (logs @0))
6713    @0))
6715  /* Optimize logN(func()) for various exponential functions.  We
6716     want to determine the value "x" and the power "exponent" in
6717     order to transform logN(x**exponent) into exponent*logN(x).  */
6718  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
6719       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
6720   (simplify
6721    (logs (exps @0))
6722    (if (SCALAR_FLOAT_TYPE_P (type))
6723     (with {
6724       tree x;
6725       switch (exps)
6726         {
6727         CASE_CFN_EXP:
6728           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
6729           x = build_real_truncate (type, dconst_e ());
6730           break;
6731         CASE_CFN_EXP2:
6732           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
6733           x = build_real (type, dconst2);
6734           break;
6735         CASE_CFN_EXP10:
6736         CASE_CFN_POW10:
6737           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
6738           {
6739             REAL_VALUE_TYPE dconst10;
6740             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6741             x = build_real (type, dconst10);
6742           }
6743           break;
6744         default:
6745           gcc_unreachable ();
6746         }
6747       }
6748      (mult (logs { x; }) @0)))))
6750  (for logs (LOG LOG
6751             LOG2 LOG2
6752             LOG10 LOG10)
6753       exps (SQRT CBRT)
6754   (simplify
6755    (logs (exps @0))
6756    (if (SCALAR_FLOAT_TYPE_P (type))
6757     (with {
6758       tree x;
6759       switch (exps)
6760         {
6761         CASE_CFN_SQRT:
6762           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
6763           x = build_real (type, dconsthalf);
6764           break;
6765         CASE_CFN_CBRT:
6766           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
6767           x = build_real_truncate (type, dconst_third ());
6768           break;
6769         default:
6770           gcc_unreachable ();
6771         }
6772       }
6773      (mult { x; } (logs @0))))))
6775  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
6776  (for logs (LOG LOG2 LOG10)
6777       pows (POW)
6778   (simplify
6779    (logs (pows @0 @1))
6780    (mult @1 (logs @0))))
6782  /* pow(C,x) -> exp(log(C)*x) if C > 0,
6783     or if C is a positive power of 2,
6784     pow(C,x) -> exp2(log2(C)*x).  */
6785 #if GIMPLE
6786  (for pows (POW)
6787       exps (EXP)
6788       logs (LOG)
6789       exp2s (EXP2)
6790       log2s (LOG2)
6791   (simplify
6792    (pows REAL_CST@0 @1)
6793    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6794         && real_isfinite (TREE_REAL_CST_PTR (@0))
6795         /* As libmvec doesn't have a vectorized exp2, defer optimizing
6796            the use_exp2 case until after vectorization.  It seems actually
6797            beneficial for all constants to postpone this until later,
6798            because exp(log(C)*x), while faster, will have worse precision
6799            and if x folds into a constant too, that is unnecessary
6800            pessimization.  */
6801         && canonicalize_math_after_vectorization_p ())
6802     (with {
6803        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6804        bool use_exp2 = false;
6805        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6806            && value->cl == rvc_normal)
6807          {
6808            REAL_VALUE_TYPE frac_rvt = *value;
6809            SET_REAL_EXP (&frac_rvt, 1);
6810            if (real_equal (&frac_rvt, &dconst1))
6811              use_exp2 = true;
6812          }
6813      }
6814      (if (!use_exp2)
6815       (if (optimize_pow_to_exp (@0, @1))
6816        (exps (mult (logs @0) @1)))
6817       (exp2s (mult (log2s @0) @1)))))))
6818 #endif
6820  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
6821  (for pows (POW)
6822       exps (EXP EXP2 EXP10 POW10)
6823       logs (LOG LOG2 LOG10 LOG10)
6824   (simplify
6825    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6826    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6827         && real_isfinite (TREE_REAL_CST_PTR (@0)))
6828     (exps (plus (mult (logs @0) @1) @2)))))
6830  (for sqrts (SQRT)
6831       cbrts (CBRT)
6832       pows (POW)
6833       exps (EXP EXP2 EXP10 POW10)
6834   /* sqrt(expN(x)) -> expN(x*0.5).  */
6835   (simplify
6836    (sqrts (exps @0))
6837    (exps (mult @0 { build_real (type, dconsthalf); })))
6838   /* cbrt(expN(x)) -> expN(x/3).  */
6839   (simplify
6840    (cbrts (exps @0))
6841    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6842   /* pow(expN(x), y) -> expN(x*y).  */
6843   (simplify
6844    (pows (exps @0) @1)
6845    (exps (mult @0 @1))))
6847  /* tan(atan(x)) -> x.  */
6848  (for tans (TAN)
6849       atans (ATAN)
6850   (simplify
6851    (tans (atans @0))
6852    @0)))
6854  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6855  (for sins (SIN)
6856       atans (ATAN)
6857       sqrts (SQRT)
6858       copysigns (COPYSIGN)
6859   (simplify
6860    (sins (atans:s @0))
6861    (with
6862      {
6863       REAL_VALUE_TYPE r_cst;
6864       build_sinatan_real (&r_cst, type);
6865       tree t_cst = build_real (type, r_cst);
6866       tree t_one = build_one_cst (type);
6867      }
6868     (if (SCALAR_FLOAT_TYPE_P (type))
6869      (cond (lt (abs @0) { t_cst; })
6870       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6871       (copysigns { t_one; } @0))))))
6873 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6874  (for coss (COS)
6875       atans (ATAN)
6876       sqrts (SQRT)
6877       copysigns (COPYSIGN)
6878   (simplify
6879    (coss (atans:s @0))
6880    (with
6881      {
6882       REAL_VALUE_TYPE r_cst;
6883       build_sinatan_real (&r_cst, type);
6884       tree t_cst = build_real (type, r_cst);
6885       tree t_one = build_one_cst (type);
6886       tree t_zero = build_zero_cst (type);
6887      }
6888     (if (SCALAR_FLOAT_TYPE_P (type))
6889      (cond (lt (abs @0) { t_cst; })
6890       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6891       (copysigns { t_zero; } @0))))))
6893  (if (!flag_errno_math)
6894   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6895   (for sinhs (SINH)
6896        atanhs (ATANH)
6897        sqrts (SQRT)
6898    (simplify
6899     (sinhs (atanhs:s @0))
6900     (with { tree t_one = build_one_cst (type); }
6901     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6903   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6904   (for coshs (COSH)
6905        atanhs (ATANH)
6906        sqrts (SQRT)
6907    (simplify
6908     (coshs (atanhs:s @0))
6909     (with { tree t_one = build_one_cst (type); }
6910     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6912 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
6913 (simplify
6914  (CABS (complex:C @0 real_zerop@1))
6915  (abs @0))
6917 /* trunc(trunc(x)) -> trunc(x), etc.  */
6918 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6919  (simplify
6920   (fns (fns @0))
6921   (fns @0)))
6922 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
6923 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6924  (simplify
6925   (fns integer_valued_real_p@0)
6926   @0))
6928 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
6929 (simplify
6930  (HYPOT:c @0 real_zerop@1)
6931  (abs @0))
6933 /* pow(1,x) -> 1.  */
6934 (simplify
6935  (POW real_onep@0 @1)
6936  @0)
6938 (simplify
6939  /* copysign(x,x) -> x.  */
6940  (COPYSIGN_ALL @0 @0)
6941  @0)
6943 (simplify
6944  /* copysign(x,-x) -> -x.  */
6945  (COPYSIGN_ALL @0 (negate@1 @0))
6946  @1)
6948 (simplify
6949  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
6950  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6951  (abs @0))
6953 (for scale (LDEXP SCALBN SCALBLN)
6954  /* ldexp(0, x) -> 0.  */
6955  (simplify
6956   (scale real_zerop@0 @1)
6957   @0)
6958  /* ldexp(x, 0) -> x.  */
6959  (simplify
6960   (scale @0 integer_zerop@1)
6961   @0)
6962  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
6963  (simplify
6964   (scale REAL_CST@0 @1)
6965   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6966    @0)))
6968 /* Canonicalization of sequences of math builtins.  These rules represent
6969    IL simplifications but are not necessarily optimizations.
6971    The sincos pass is responsible for picking "optimal" implementations
6972    of math builtins, which may be more complicated and can sometimes go
6973    the other way, e.g. converting pow into a sequence of sqrts.
6974    We only want to do these canonicalizations before the pass has run.  */
6976 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6977  /* Simplify tan(x) * cos(x) -> sin(x). */
6978  (simplify
6979   (mult:c (TAN:s @0) (COS:s @0))
6980    (SIN @0))
6982  /* Simplify x * pow(x,c) -> pow(x,c+1). */
6983  (simplify
6984   (mult:c @0 (POW:s @0 REAL_CST@1))
6985   (if (!TREE_OVERFLOW (@1))
6986    (POW @0 (plus @1 { build_one_cst (type); }))))
6988  /* Simplify sin(x) / cos(x) -> tan(x). */
6989  (simplify
6990   (rdiv (SIN:s @0) (COS:s @0))
6991    (TAN @0))
6993  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6994  (simplify
6995   (rdiv (SINH:s @0) (COSH:s @0))
6996    (TANH @0))
6998  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6999  (simplify
7000    (rdiv (TANH:s @0) (SINH:s @0))
7001    (rdiv {build_one_cst (type);} (COSH @0)))
7003  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7004  (simplify
7005   (rdiv (COS:s @0) (SIN:s @0))
7006    (rdiv { build_one_cst (type); } (TAN @0)))
7008  /* Simplify sin(x) / tan(x) -> cos(x). */
7009  (simplify
7010   (rdiv (SIN:s @0) (TAN:s @0))
7011   (if (! HONOR_NANS (@0)
7012        && ! HONOR_INFINITIES (@0))
7013    (COS @0)))
7015  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7016  (simplify
7017   (rdiv (TAN:s @0) (SIN:s @0))
7018   (if (! HONOR_NANS (@0)
7019        && ! HONOR_INFINITIES (@0))
7020    (rdiv { build_one_cst (type); } (COS @0))))
7022  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7023  (simplify
7024   (mult (POW:s @0 @1) (POW:s @0 @2))
7025    (POW @0 (plus @1 @2)))
7027  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7028  (simplify
7029   (mult (POW:s @0 @1) (POW:s @2 @1))
7030    (POW (mult @0 @2) @1))
7032  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7033  (simplify
7034   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7035    (POWI (mult @0 @2) @1))
7037  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7038  (simplify
7039   (rdiv (POW:s @0 REAL_CST@1) @0)
7040   (if (!TREE_OVERFLOW (@1))
7041    (POW @0 (minus @1 { build_one_cst (type); }))))
7043  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7044  (simplify
7045   (rdiv @0 (POW:s @1 @2))
7046    (mult @0 (POW @1 (negate @2))))
7048  (for sqrts (SQRT)
7049       cbrts (CBRT)
7050       pows (POW)
7051   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7052   (simplify
7053    (sqrts (sqrts @0))
7054    (pows @0 { build_real (type, dconst_quarter ()); }))
7055   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7056   (simplify
7057    (sqrts (cbrts @0))
7058    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7059   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7060   (simplify
7061    (cbrts (sqrts @0))
7062    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7063   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7064   (simplify
7065    (cbrts (cbrts tree_expr_nonnegative_p@0))
7066    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7067   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7068   (simplify
7069    (sqrts (pows @0 @1))
7070    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7071   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7072   (simplify
7073    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7074    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7075   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7076   (simplify
7077    (pows (sqrts @0) @1)
7078    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7079   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7080   (simplify
7081    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7082    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7083   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7084   (simplify
7085    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7086    (pows @0 (mult @1 @2))))
7088  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7089  (simplify
7090   (CABS (complex @0 @0))
7091   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7093  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7094  (simplify
7095   (HYPOT @0 @0)
7096   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7098  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7099  (for cexps (CEXP)
7100       exps (EXP)
7101       cexpis (CEXPI)
7102   (simplify
7103    (cexps compositional_complex@0)
7104    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7105     (complex
7106      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7107      (mult @1 (imagpart @2)))))))
7109 (if (canonicalize_math_p ())
7110  /* floor(x) -> trunc(x) if x is nonnegative.  */
7111  (for floors (FLOOR_ALL)
7112       truncs (TRUNC_ALL)
7113   (simplify
7114    (floors tree_expr_nonnegative_p@0)
7115    (truncs @0))))
7117 (match double_value_p
7118  @0
7119  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7120 (for froms (BUILT_IN_TRUNCL
7121             BUILT_IN_FLOORL
7122             BUILT_IN_CEILL
7123             BUILT_IN_ROUNDL
7124             BUILT_IN_NEARBYINTL
7125             BUILT_IN_RINTL)
7126      tos (BUILT_IN_TRUNC
7127           BUILT_IN_FLOOR
7128           BUILT_IN_CEIL
7129           BUILT_IN_ROUND
7130           BUILT_IN_NEARBYINT
7131           BUILT_IN_RINT)
7132  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7133  (if (optimize && canonicalize_math_p ())
7134   (simplify
7135    (froms (convert double_value_p@0))
7136    (convert (tos @0)))))
7138 (match float_value_p
7139  @0
7140  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7141 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7142             BUILT_IN_FLOORL BUILT_IN_FLOOR
7143             BUILT_IN_CEILL BUILT_IN_CEIL
7144             BUILT_IN_ROUNDL BUILT_IN_ROUND
7145             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7146             BUILT_IN_RINTL BUILT_IN_RINT)
7147      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7148           BUILT_IN_FLOORF BUILT_IN_FLOORF
7149           BUILT_IN_CEILF BUILT_IN_CEILF
7150           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7151           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7152           BUILT_IN_RINTF BUILT_IN_RINTF)
7153  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7154     if x is a float.  */
7155  (if (optimize && canonicalize_math_p ()
7156       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7157   (simplify
7158    (froms (convert float_value_p@0))
7159    (convert (tos @0)))))
7161 #if GIMPLE
7162 (match float16_value_p
7163  @0
7164  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7165 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7166             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7167             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7168             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7169             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7170             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7171             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7172             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7173      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7174           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7175           IFN_CEIL IFN_CEIL IFN_CEIL
7176           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7177           IFN_ROUND IFN_ROUND IFN_ROUND
7178           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7179           IFN_RINT IFN_RINT IFN_RINT
7180           IFN_SQRT IFN_SQRT IFN_SQRT)
7181  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7182     if x is a _Float16.  */
7183  (simplify
7184    (convert (froms (convert float16_value_p@0)))
7185      (if (optimize
7186           && types_match (type, TREE_TYPE (@0))
7187           && direct_internal_fn_supported_p (as_internal_fn (tos),
7188                                              type, OPTIMIZE_FOR_BOTH))
7189        (tos @0))))
7191 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7192    x,y is float value, similar for _Float16/double.  */
7193 (for copysigns (COPYSIGN_ALL)
7194  (simplify
7195   (convert (copysigns (convert@2 @0) (convert @1)))
7196    (if (optimize
7197        && !HONOR_SNANS (@2)
7198        && types_match (type, TREE_TYPE (@0))
7199        && types_match (type, TREE_TYPE (@1))
7200        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7201        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7202                                           type, OPTIMIZE_FOR_BOTH))
7203     (IFN_COPYSIGN @0 @1))))
7205 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7206      tos (IFN_FMA IFN_FMA IFN_FMA)
7207  (simplify
7208   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7209    (if (flag_unsafe_math_optimizations
7210        && optimize
7211        && FLOAT_TYPE_P (type)
7212        && FLOAT_TYPE_P (TREE_TYPE (@3))
7213        && types_match (type, TREE_TYPE (@0))
7214        && types_match (type, TREE_TYPE (@1))
7215        && types_match (type, TREE_TYPE (@2))
7216        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7217        && direct_internal_fn_supported_p (as_internal_fn (tos),
7218                                           type, OPTIMIZE_FOR_BOTH))
7219     (tos @0 @1 @2))))
7221 (for maxmin (max min)
7222  (simplify
7223   (convert (maxmin (convert@2 @0) (convert @1)))
7224    (if (optimize
7225        && FLOAT_TYPE_P (type)
7226        && FLOAT_TYPE_P (TREE_TYPE (@2))
7227        && types_match (type, TREE_TYPE (@0))
7228        && types_match (type, TREE_TYPE (@1))
7229        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7230     (maxmin @0 @1))))
7231 #endif
7233 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7234      tos (XFLOOR XCEIL XROUND XRINT)
7235  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7236  (if (optimize && canonicalize_math_p ())
7237   (simplify
7238    (froms (convert double_value_p@0))
7239    (tos @0))))
7241 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7242             XFLOOR XCEIL XROUND XRINT)
7243      tos (XFLOORF XCEILF XROUNDF XRINTF)
7244  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7245     if x is a float.  */
7246  (if (optimize && canonicalize_math_p ())
7247   (simplify
7248    (froms (convert float_value_p@0))
7249    (tos @0))))
7251 (if (canonicalize_math_p ())
7252  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7253  (for floors (IFLOOR LFLOOR LLFLOOR)
7254   (simplify
7255    (floors tree_expr_nonnegative_p@0)
7256    (fix_trunc @0))))
7258 (if (canonicalize_math_p ())
7259  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7260  (for fns (IFLOOR LFLOOR LLFLOOR
7261            ICEIL LCEIL LLCEIL
7262            IROUND LROUND LLROUND)
7263   (simplify
7264    (fns integer_valued_real_p@0)
7265    (fix_trunc @0)))
7266  (if (!flag_errno_math)
7267   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7268   (for rints (IRINT LRINT LLRINT)
7269    (simplify
7270     (rints integer_valued_real_p@0)
7271     (fix_trunc @0)))))
7273 (if (canonicalize_math_p ())
7274  (for ifn (IFLOOR ICEIL IROUND IRINT)
7275       lfn (LFLOOR LCEIL LROUND LRINT)
7276       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7277   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7278      sizeof (int) == sizeof (long).  */
7279   (if (TYPE_PRECISION (integer_type_node)
7280        == TYPE_PRECISION (long_integer_type_node))
7281    (simplify
7282     (ifn @0)
7283     (lfn:long_integer_type_node @0)))
7284   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7285      sizeof (long long) == sizeof (long).  */
7286   (if (TYPE_PRECISION (long_long_integer_type_node)
7287        == TYPE_PRECISION (long_integer_type_node))
7288    (simplify
7289     (llfn @0)
7290     (lfn:long_integer_type_node @0)))))
7292 /* cproj(x) -> x if we're ignoring infinities.  */
7293 (simplify
7294  (CPROJ @0)
7295  (if (!HONOR_INFINITIES (type))
7296    @0))
7298 /* If the real part is inf and the imag part is known to be
7299    nonnegative, return (inf + 0i).  */
7300 (simplify
7301  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7302  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7303   { build_complex_inf (type, false); }))
7305 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7306 (simplify
7307  (CPROJ (complex @0 REAL_CST@1))
7308  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7309   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7311 (for pows (POW)
7312      sqrts (SQRT)
7313      cbrts (CBRT)
7314  (simplify
7315   (pows @0 REAL_CST@1)
7316   (with {
7317     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7318     REAL_VALUE_TYPE tmp;
7319    }
7320    (switch
7321     /* pow(x,0) -> 1.  */
7322     (if (real_equal (value, &dconst0))
7323      { build_real (type, dconst1); })
7324     /* pow(x,1) -> x.  */
7325     (if (real_equal (value, &dconst1))
7326      @0)
7327     /* pow(x,-1) -> 1/x.  */
7328     (if (real_equal (value, &dconstm1))
7329      (rdiv { build_real (type, dconst1); } @0))
7330     /* pow(x,0.5) -> sqrt(x).  */
7331     (if (flag_unsafe_math_optimizations
7332          && canonicalize_math_p ()
7333          && real_equal (value, &dconsthalf))
7334      (sqrts @0))
7335     /* pow(x,1/3) -> cbrt(x).  */
7336     (if (flag_unsafe_math_optimizations
7337          && canonicalize_math_p ()
7338          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7339              real_equal (value, &tmp)))
7340      (cbrts @0))))))
7342 /* powi(1,x) -> 1.  */
7343 (simplify
7344  (POWI real_onep@0 @1)
7345  @0)
7347 (simplify
7348  (POWI @0 INTEGER_CST@1)
7349  (switch
7350   /* powi(x,0) -> 1.  */
7351   (if (wi::to_wide (@1) == 0)
7352    { build_real (type, dconst1); })
7353   /* powi(x,1) -> x.  */
7354   (if (wi::to_wide (@1) == 1)
7355    @0)
7356   /* powi(x,-1) -> 1/x.  */
7357   (if (wi::to_wide (@1) == -1)
7358    (rdiv { build_real (type, dconst1); } @0))))
7360 /* Narrowing of arithmetic and logical operations.
7362    These are conceptually similar to the transformations performed for
7363    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7364    term we want to move all that code out of the front-ends into here.  */
7366 /* Convert (outertype)((innertype0)a+(innertype1)b)
7367    into ((newtype)a+(newtype)b) where newtype
7368    is the widest mode from all of these.  */
7369 (for op (plus minus mult rdiv)
7370  (simplify
7371    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7372    /* If we have a narrowing conversion of an arithmetic operation where
7373       both operands are widening conversions from the same type as the outer
7374       narrowing conversion.  Then convert the innermost operands to a
7375       suitable unsigned type (to avoid introducing undefined behavior),
7376       perform the operation and convert the result to the desired type.  */
7377    (if (INTEGRAL_TYPE_P (type)
7378         && op != MULT_EXPR
7379         && op != RDIV_EXPR
7380         /* We check for type compatibility between @0 and @1 below,
7381            so there's no need to check that @2/@4 are integral types.  */
7382         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7383         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7384         /* The precision of the type of each operand must match the
7385            precision of the mode of each operand, similarly for the
7386            result.  */
7387         && type_has_mode_precision_p (TREE_TYPE (@1))
7388         && type_has_mode_precision_p (TREE_TYPE (@2))
7389         && type_has_mode_precision_p (type)
7390         /* The inner conversion must be a widening conversion.  */
7391         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7392         && types_match (@1, type)
7393         && (types_match (@1, @2)
7394             /* Or the second operand is const integer or converted const
7395                integer from valueize.  */
7396             || poly_int_tree_p (@4)))
7397      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7398        (op @1 (convert @2))
7399        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7400         (convert (op (convert:utype @1)
7401                      (convert:utype @2)))))
7402      (if (FLOAT_TYPE_P (type)
7403           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7404                == DECIMAL_FLOAT_TYPE_P (type))
7405       (with { tree arg0 = strip_float_extensions (@1);
7406               tree arg1 = strip_float_extensions (@2);
7407               tree itype = TREE_TYPE (@0);
7408               tree ty1 = TREE_TYPE (arg0);
7409               tree ty2 = TREE_TYPE (arg1);
7410               enum tree_code code = TREE_CODE (itype); }
7411         (if (FLOAT_TYPE_P (ty1)
7412              && FLOAT_TYPE_P (ty2))
7413          (with { tree newtype = type;
7414                  if (TYPE_MODE (ty1) == SDmode
7415                      || TYPE_MODE (ty2) == SDmode
7416                      || TYPE_MODE (type) == SDmode)
7417                    newtype = dfloat32_type_node;
7418                  if (TYPE_MODE (ty1) == DDmode
7419                      || TYPE_MODE (ty2) == DDmode
7420                      || TYPE_MODE (type) == DDmode)
7421                    newtype = dfloat64_type_node;
7422                  if (TYPE_MODE (ty1) == TDmode
7423                      || TYPE_MODE (ty2) == TDmode
7424                      || TYPE_MODE (type) == TDmode)
7425                    newtype = dfloat128_type_node; }
7426           (if ((newtype == dfloat32_type_node
7427                 || newtype == dfloat64_type_node
7428                 || newtype == dfloat128_type_node)
7429               && newtype == type
7430               && types_match (newtype, type))
7431             (op (convert:newtype @1) (convert:newtype @2))
7432             (with { if (element_precision (ty1) > element_precision (newtype))
7433                       newtype = ty1;
7434                     if (element_precision (ty2) > element_precision (newtype))
7435                       newtype = ty2; }
7436                /* Sometimes this transformation is safe (cannot
7437                   change results through affecting double rounding
7438                   cases) and sometimes it is not.  If NEWTYPE is
7439                   wider than TYPE, e.g. (float)((long double)double
7440                   + (long double)double) converted to
7441                   (float)(double + double), the transformation is
7442                   unsafe regardless of the details of the types
7443                   involved; double rounding can arise if the result
7444                   of NEWTYPE arithmetic is a NEWTYPE value half way
7445                   between two representable TYPE values but the
7446                   exact value is sufficiently different (in the
7447                   right direction) for this difference to be
7448                   visible in ITYPE arithmetic.  If NEWTYPE is the
7449                   same as TYPE, however, the transformation may be
7450                   safe depending on the types involved: it is safe
7451                   if the ITYPE has strictly more than twice as many
7452                   mantissa bits as TYPE, can represent infinities
7453                   and NaNs if the TYPE can, and has sufficient
7454                   exponent range for the product or ratio of two
7455                   values representable in the TYPE to be within the
7456                   range of normal values of ITYPE.  */
7457               (if (element_precision (newtype) < element_precision (itype)
7458                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7459                        || target_supports_op_p (newtype, op, optab_default))
7460                    && (flag_unsafe_math_optimizations
7461                        || (element_precision (newtype) == element_precision (type)
7462                            && real_can_shorten_arithmetic (element_mode (itype),
7463                                                            element_mode (type))
7464                            && !excess_precision_type (newtype)))
7465                    && !types_match (itype, newtype))
7466                  (convert:type (op (convert:newtype @1)
7467                                    (convert:newtype @2)))
7468          )))) )
7469    ))
7472 /* This is another case of narrowing, specifically when there's an outer
7473    BIT_AND_EXPR which masks off bits outside the type of the innermost
7474    operands.   Like the previous case we have to convert the operands
7475    to unsigned types to avoid introducing undefined behavior for the
7476    arithmetic operation.  */
7477 (for op (minus plus)
7478  (simplify
7479   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7480   (if (INTEGRAL_TYPE_P (type)
7481        /* We check for type compatibility between @0 and @1 below,
7482           so there's no need to check that @1/@3 are integral types.  */
7483        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7484        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7485        /* The precision of the type of each operand must match the
7486           precision of the mode of each operand, similarly for the
7487           result.  */
7488        && type_has_mode_precision_p (TREE_TYPE (@0))
7489        && type_has_mode_precision_p (TREE_TYPE (@1))
7490        && type_has_mode_precision_p (type)
7491        /* The inner conversion must be a widening conversion.  */
7492        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7493        && types_match (@0, @1)
7494        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7495            <= TYPE_PRECISION (TREE_TYPE (@0)))
7496        && (wi::to_wide (@4)
7497            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7498                        true, TYPE_PRECISION (type))) == 0)
7499    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7500     (with { tree ntype = TREE_TYPE (@0); }
7501      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7502     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7503      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7504                (convert:utype @4))))))))
7506 /* Transform (@0 < @1 and @0 < @2) to use min,
7507    (@0 > @1 and @0 > @2) to use max */
7508 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7509      op    (lt      le      gt      ge      lt      le      gt      ge     )
7510      ext   (min     min     max     max     max     max     min     min    )
7511  (simplify
7512   (logic (op:cs @0 @1) (op:cs @0 @2))
7513   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7514        && TREE_CODE (@0) != INTEGER_CST)
7515    (op @0 (ext @1 @2)))))
7517 /* Max<bool0, bool1> -> bool0 | bool1
7518    Min<bool0, bool1> -> bool0 & bool1 */
7519 (for op    (max     min)
7520      logic (bit_ior bit_and)
7521  (simplify
7522   (op zero_one_valued_p@0 zero_one_valued_p@1)
7523   (logic @0 @1)))
7525 /* signbit(x) != 0 ? -x : x -> abs(x)
7526    signbit(x) == 0 ? -x : x -> -abs(x) */
7527 (for sign (SIGNBIT)
7528  (for neeq (ne eq)
7529   (simplify
7530    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
7531     (if (neeq == NE_EXPR)
7532      (abs @0)
7533      (negate (abs @0))))))
7535 (simplify
7536  /* signbit(x) -> 0 if x is nonnegative.  */
7537  (SIGNBIT tree_expr_nonnegative_p@0)
7538  { integer_zero_node; })
7540 (simplify
7541  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
7542  (SIGNBIT @0)
7543  (if (!HONOR_SIGNED_ZEROS (@0))
7544   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7546 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
7547 (for cmp (eq ne)
7548  (for op (plus minus)
7549       rop (minus plus)
7550   (simplify
7551    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7552    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7553         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7554         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7555         && !TYPE_SATURATING (TREE_TYPE (@0)))
7556     (with { tree res = int_const_binop (rop, @2, @1); }
7557      (if (TREE_OVERFLOW (res)
7558           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7559       { constant_boolean_node (cmp == NE_EXPR, type); }
7560       (if (single_use (@3))
7561        (cmp @0 { TREE_OVERFLOW (res)
7562                  ? drop_tree_overflow (res) : res; }))))))))
7563 (for cmp (lt le gt ge)
7564  (for op (plus minus)
7565       rop (minus plus)
7566   (simplify
7567    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7568    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7569         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7570     (with { tree res = int_const_binop (rop, @2, @1); }
7571      (if (TREE_OVERFLOW (res))
7572       {
7573         fold_overflow_warning (("assuming signed overflow does not occur "
7574                                 "when simplifying conditional to constant"),
7575                                WARN_STRICT_OVERFLOW_CONDITIONAL);
7576         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7577         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
7578         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7579                                   TYPE_SIGN (TREE_TYPE (@1)))
7580                         != (op == MINUS_EXPR);
7581         constant_boolean_node (less == ovf_high, type);
7582       }
7583       (if (single_use (@3))
7584        (with
7585         {
7586           fold_overflow_warning (("assuming signed overflow does not occur "
7587                                   "when changing X +- C1 cmp C2 to "
7588                                   "X cmp C2 -+ C1"),
7589                                  WARN_STRICT_OVERFLOW_COMPARISON);
7590         }
7591         (cmp @0 { res; })))))))))
7593 /* Canonicalizations of BIT_FIELD_REFs.  */
7595 (simplify
7596  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7597  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7599 (simplify
7600  (BIT_FIELD_REF (view_convert @0) @1 @2)
7601  (BIT_FIELD_REF @0 @1 @2))
7603 (simplify
7604  (BIT_FIELD_REF @0 @1 integer_zerop)
7605  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7606   (view_convert @0)))
7608 (simplify
7609  (BIT_FIELD_REF @0 @1 @2)
7610  (switch
7611   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7612        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7613    (switch
7614     (if (integer_zerop (@2))
7615      (view_convert (realpart @0)))
7616     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7617      (view_convert (imagpart @0)))))
7618   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7619        && INTEGRAL_TYPE_P (type)
7620        /* On GIMPLE this should only apply to register arguments.  */
7621        && (! GIMPLE || is_gimple_reg (@0))
7622        /* A bit-field-ref that referenced the full argument can be stripped.  */
7623        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7624             && integer_zerop (@2))
7625            /* Low-parts can be reduced to integral conversions.
7626               ???  The following doesn't work for PDP endian.  */
7627            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7628                /* But only do this after vectorization.  */
7629                && canonicalize_math_after_vectorization_p ()
7630                /* Don't even think about BITS_BIG_ENDIAN.  */
7631                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7632                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7633                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7634                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
7635                                             - TYPE_PRECISION (type))
7636                                          : 0)) == 0)))
7637    (convert @0))))
7639 /* Simplify vector extracts.  */
7641 (simplify
7642  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7643  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7644       && tree_fits_uhwi_p (TYPE_SIZE (type))
7645       && ((tree_to_uhwi (TYPE_SIZE (type))
7646            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7647           || (VECTOR_TYPE_P (type)
7648               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7649                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7650   (with
7651    {
7652      tree ctor = (TREE_CODE (@0) == SSA_NAME
7653                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7654      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7655      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7656      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7657      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7658    }
7659    (if (n != 0
7660         && (idx % width) == 0
7661         && (n % width) == 0
7662         && known_le ((idx + n) / width,
7663                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7664     (with
7665      {
7666        idx = idx / width;
7667        n = n / width;
7668        /* Constructor elements can be subvectors.  */
7669        poly_uint64 k = 1;
7670        if (CONSTRUCTOR_NELTS (ctor) != 0)
7671          {
7672            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7673            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7674              k = TYPE_VECTOR_SUBPARTS (cons_elem);
7675          }
7676        unsigned HOST_WIDE_INT elt, count, const_k;
7677      }
7678      (switch
7679       /* We keep an exact subset of the constructor elements.  */
7680       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7681        (if (CONSTRUCTOR_NELTS (ctor) == 0)
7682         { build_zero_cst (type); }
7683         (if (count == 1)
7684          (if (elt < CONSTRUCTOR_NELTS (ctor))
7685           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7686           { build_zero_cst (type); })
7687          /* We don't want to emit new CTORs unless the old one goes away.
7688             ???  Eventually allow this if the CTOR ends up constant or
7689             uniform.  */
7690          (if (single_use (@0))
7691           (with
7692             {
7693               vec<constructor_elt, va_gc> *vals;
7694               vec_alloc (vals, count);
7695               bool constant_p = true;
7696               tree res;
7697               for (unsigned i = 0;
7698                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7699                 {
7700                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7701                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7702                   if (!CONSTANT_CLASS_P (e))
7703                     constant_p = false;
7704                 }
7705               tree evtype = (types_match (TREE_TYPE (type),
7706                                           TREE_TYPE (TREE_TYPE (ctor)))
7707                              ? type
7708                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7709                                                   count * k));
7710               /* We used to build a CTOR in the non-constant case here
7711                  but that's not a GIMPLE value.  We'd have to expose this
7712                  operation somehow so the code generation can properly
7713                  split it out to a separate stmt.  */
7714               res = (constant_p ? build_vector_from_ctor (evtype, vals)
7715                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
7716             }
7717             (if (res)
7718              (view_convert { res; })))))))
7719       /* The bitfield references a single constructor element.  */
7720       (if (k.is_constant (&const_k)
7721            && idx + n <= (idx / const_k + 1) * const_k)
7722        (switch
7723         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7724          { build_zero_cst (type); })
7725         (if (n == const_k)
7726          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7727         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7728                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
7730 /* Simplify a bit extraction from a bit insertion for the cases with
7731    the inserted element fully covering the extraction or the insertion
7732    not touching the extraction.  */
7733 (simplify
7734  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7735  (with
7736   {
7737     unsigned HOST_WIDE_INT isize;
7738     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7739       isize = TYPE_PRECISION (TREE_TYPE (@1));
7740     else
7741       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7742   }
7743   (switch
7744    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
7745          || type_has_mode_precision_p (TREE_TYPE (@1)))
7746         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7747         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7748                       wi::to_wide (@ipos) + isize))
7749     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7750                                                  wi::to_wide (@rpos)
7751                                                  - wi::to_wide (@ipos)); }))
7752    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7753         && compare_tree_int (@rsize, isize) == 0)
7754     (convert @1))
7755    (if (wi::geu_p (wi::to_wide (@ipos),
7756                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
7757         || wi::geu_p (wi::to_wide (@rpos),
7758                       wi::to_wide (@ipos) + isize))
7759     (BIT_FIELD_REF @0 @rsize @rpos)))))
7761 (if (canonicalize_math_after_vectorization_p ())
7762  (for fmas (FMA)
7763   (simplify
7764    (fmas:c (negate @0) @1 @2)
7765    (IFN_FNMA @0 @1 @2))
7766   (simplify
7767    (fmas @0 @1 (negate @2))
7768    (IFN_FMS @0 @1 @2))
7769   (simplify
7770    (fmas:c (negate @0) @1 (negate @2))
7771    (IFN_FNMS @0 @1 @2))
7772   (simplify
7773    (negate (fmas@3 @0 @1 @2))
7774    (if (single_use (@3))
7775     (IFN_FNMS @0 @1 @2))))
7777  (simplify
7778   (IFN_FMS:c (negate @0) @1 @2)
7779   (IFN_FNMS @0 @1 @2))
7780  (simplify
7781   (IFN_FMS @0 @1 (negate @2))
7782   (IFN_FMA @0 @1 @2))
7783  (simplify
7784   (IFN_FMS:c (negate @0) @1 (negate @2))
7785   (IFN_FNMA @0 @1 @2))
7786  (simplify
7787   (negate (IFN_FMS@3 @0 @1 @2))
7788    (if (single_use (@3))
7789     (IFN_FNMA @0 @1 @2)))
7791  (simplify
7792   (IFN_FNMA:c (negate @0) @1 @2)
7793   (IFN_FMA @0 @1 @2))
7794  (simplify
7795   (IFN_FNMA @0 @1 (negate @2))
7796   (IFN_FNMS @0 @1 @2))
7797  (simplify
7798   (IFN_FNMA:c (negate @0) @1 (negate @2))
7799   (IFN_FMS @0 @1 @2))
7800  (simplify
7801   (negate (IFN_FNMA@3 @0 @1 @2))
7802   (if (single_use (@3))
7803    (IFN_FMS @0 @1 @2)))
7805  (simplify
7806   (IFN_FNMS:c (negate @0) @1 @2)
7807   (IFN_FMS @0 @1 @2))
7808  (simplify
7809   (IFN_FNMS @0 @1 (negate @2))
7810   (IFN_FNMA @0 @1 @2))
7811  (simplify
7812   (IFN_FNMS:c (negate @0) @1 (negate @2))
7813   (IFN_FMA @0 @1 @2))
7814  (simplify
7815   (negate (IFN_FNMS@3 @0 @1 @2))
7816   (if (single_use (@3))
7817    (IFN_FMA @0 @1 @2))))
7819 /* CLZ simplifications.  */
7820 (for clz (CLZ)
7821  (for op (eq ne)
7822       cmp (lt ge)
7823   (simplify
7824    (op (clz:s@2 @0) INTEGER_CST@1)
7825    (if (integer_zerop (@1) && single_use (@2))
7826     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
7827     (with { tree type0 = TREE_TYPE (@0);
7828             tree stype = signed_type_for (type0);
7829             HOST_WIDE_INT val = 0;
7830             /* Punt on hypothetical weird targets.  */
7831             if (clz == CFN_CLZ
7832                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7833                                               val) == 2
7834                 && val == 0)
7835               stype = NULL_TREE;
7836           }
7837      (if (stype)
7838       (cmp (convert:stype @0) { build_zero_cst (stype); })))
7839     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
7840     (with { bool ok = true;
7841             HOST_WIDE_INT val = 0;
7842             tree type0 = TREE_TYPE (@0);
7843             /* Punt on hypothetical weird targets.  */
7844             if (clz == CFN_CLZ
7845                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7846                                               val) == 2
7847                 && val == TYPE_PRECISION (type0) - 1)
7848               ok = false;
7849           }
7850      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7851       (op @0 { build_one_cst (type0); })))))))
7853 /* CTZ simplifications.  */
7854 (for ctz (CTZ)
7855  (for op (ge gt le lt)
7856       cmp (eq eq ne ne)
7857   (simplify
7858    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
7859    (op (ctz:s @0) INTEGER_CST@1)
7860     (with { bool ok = true;
7861             HOST_WIDE_INT val = 0;
7862             if (!tree_fits_shwi_p (@1))
7863               ok = false;
7864             else
7865               {
7866                 val = tree_to_shwi (@1);
7867                 /* Canonicalize to >= or <.  */
7868                 if (op == GT_EXPR || op == LE_EXPR)
7869                   {
7870                     if (val == HOST_WIDE_INT_MAX)
7871                       ok = false;
7872                     else
7873                       val++;
7874                   }
7875               }
7876             bool zero_res = false;
7877             HOST_WIDE_INT zero_val = 0;
7878             tree type0 = TREE_TYPE (@0);
7879             int prec = TYPE_PRECISION (type0);
7880             if (ctz == CFN_CTZ
7881                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7882                                               zero_val) == 2)
7883               zero_res = true;
7884           }
7885      (if (val <= 0)
7886       (if (ok && (!zero_res || zero_val >= val))
7887        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7888       (if (val >= prec)
7889        (if (ok && (!zero_res || zero_val < val))
7890         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7891        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7892         (cmp (bit_and @0 { wide_int_to_tree (type0,
7893                                              wi::mask (val, false, prec)); })
7894              { build_zero_cst (type0); })))))))
7895  (for op (eq ne)
7896   (simplify
7897    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
7898    (op (ctz:s @0) INTEGER_CST@1)
7899     (with { bool zero_res = false;
7900             HOST_WIDE_INT zero_val = 0;
7901             tree type0 = TREE_TYPE (@0);
7902             int prec = TYPE_PRECISION (type0);
7903             if (ctz == CFN_CTZ
7904                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7905                                               zero_val) == 2)
7906               zero_res = true;
7907           }
7908      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7909       (if (!zero_res || zero_val != wi::to_widest (@1))
7910        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7911       (if (!zero_res || zero_val < 0 || zero_val >= prec)
7912        (op (bit_and @0 { wide_int_to_tree (type0,
7913                                            wi::mask (tree_to_uhwi (@1) + 1,
7914                                                      false, prec)); })
7915            { wide_int_to_tree (type0,
7916                                wi::shifted_mask (tree_to_uhwi (@1), 1,
7917                                                  false, prec)); })))))))
7919 /* POPCOUNT simplifications.  */
7920 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
7921 (simplify
7922   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7923   (if (INTEGRAL_TYPE_P (type)
7924        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7925     (POPCOUNT (bit_ior @0 @1))))
7927 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
7928 (for popcount (POPCOUNT)
7929   (for cmp (le eq ne gt)
7930        rep (eq eq ne ne)
7931     (simplify
7932       (cmp (popcount @0) integer_zerop)
7933       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7935 /* popcount(bswap(x)) is popcount(x).  */
7936 (for popcount (POPCOUNT)
7937   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
7938               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
7939     (simplify
7940       (popcount (convert?@0 (bswap:s@1 @2)))
7941       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7942            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7943         (with { tree type0 = TREE_TYPE (@0);
7944                 tree type1 = TREE_TYPE (@1);
7945                 unsigned int prec0 = TYPE_PRECISION (type0);
7946                 unsigned int prec1 = TYPE_PRECISION (type1); }
7947           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
7948             (popcount (convert:type0 (convert:type1 @2)))))))))
7950 /* popcount(rotate(X Y)) is popcount(X).  */
7951 (for popcount (POPCOUNT)
7952   (for rot (lrotate rrotate)
7953     (simplify
7954       (popcount (convert?@0 (rot:s@1 @2 @3)))
7955       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7956            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
7957            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
7958         (with { tree type0 = TREE_TYPE (@0);
7959                 tree type1 = TREE_TYPE (@1);
7960                 unsigned int prec0 = TYPE_PRECISION (type0);
7961                 unsigned int prec1 = TYPE_PRECISION (type1); }
7962           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
7963             (popcount (convert:type0 @2))))))))
7965 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
7966 (simplify
7967   (bit_and (POPCOUNT @0) integer_onep)
7968   (PARITY @0))
7970 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
7971 (simplify
7972   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
7973   (plus (POPCOUNT @0) (POPCOUNT @1)))
7975 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
7976 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
7977 (for popcount (POPCOUNT)
7978   (for log1 (bit_and bit_ior)
7979        log2 (bit_ior bit_and)
7980     (simplify
7981       (minus (plus:s (popcount:s @0) (popcount:s @1))
7982              (popcount:s (log1:cs @0 @1)))
7983       (popcount (log2 @0 @1)))
7984     (simplify
7985       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
7986               (popcount:s @1))
7987       (popcount (log2 @0 @1)))))
7989 /* PARITY simplifications.  */
7990 /* parity(~X) is parity(X).  */
7991 (simplify
7992   (PARITY (bit_not @0))
7993   (PARITY @0))
7995 /* parity(bswap(x)) is parity(x).  */
7996 (for parity (PARITY)
7997   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
7998               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
7999     (simplify
8000       (parity (convert?@0 (bswap:s@1 @2)))
8001       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8002            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8003            && TYPE_PRECISION (TREE_TYPE (@0))
8004               >= TYPE_PRECISION (TREE_TYPE (@1)))
8005         (with { tree type0 = TREE_TYPE (@0);
8006                 tree type1 = TREE_TYPE (@1); }
8007           (parity (convert:type0 (convert:type1 @2))))))))
8009 /* parity(rotate(X Y)) is parity(X).  */
8010 (for parity (PARITY)
8011   (for rot (lrotate rrotate)
8012     (simplify
8013       (parity (convert?@0 (rot:s@1 @2 @3)))
8014       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8015            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8016            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8017            && TYPE_PRECISION (TREE_TYPE (@0))
8018               >= TYPE_PRECISION (TREE_TYPE (@1)))
8019         (with { tree type0 = TREE_TYPE (@0); }
8020           (parity (convert:type0 @2)))))))
8022 /* parity(X)^parity(Y) is parity(X^Y).  */
8023 (simplify
8024   (bit_xor (PARITY:s @0) (PARITY:s @1))
8025   (PARITY (bit_xor @0 @1)))
8027 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8028 (for func (POPCOUNT BSWAP FFS PARITY)
8029  (simplify
8030   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8031   @3))
8033 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8034    where CST is precision-1. */
8035 (for func (CLRSB)
8036  (simplify
8037   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8038   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8039    @4)))
8041 #if GIMPLE
8042 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8043 (for func (CLZ)
8044  (simplify
8045   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8046   (with { int val;
8047           internal_fn ifn = IFN_LAST;
8048           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8049               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8050                                             val) == 2)
8051             ifn = IFN_CLZ;
8052         }
8053    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8054     (IFN_CLZ @3)))))
8056 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8057 (for func (CTZ)
8058  (simplify
8059   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8060   (with { int val;
8061           internal_fn ifn = IFN_LAST;
8062           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8063               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8064                                             val) == 2)
8065             ifn = IFN_CTZ;
8066         }
8067    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8068     (IFN_CTZ @3)))))
8069 #endif
8071 /* Common POPCOUNT/PARITY simplifications.  */
8072 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8073 (for pfun (POPCOUNT PARITY)
8074   (simplify
8075     (pfun @0)
8076     (if (INTEGRAL_TYPE_P (type))
8077      (with { wide_int nz = tree_nonzero_bits (@0); }
8078        (switch
8079          (if (nz == 1)
8080            (convert @0))
8081          (if (wi::popcount (nz) == 1)
8082            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8083              (convert (rshift:utype (convert:utype @0)
8084                                     { build_int_cst (integer_type_node,
8085                                                      wi::ctz (nz)); })))))))))
8087 #if GIMPLE
8088 /* 64- and 32-bits branchless implementations of popcount are detected:
8090    int popcount64c (uint64_t x)
8091    {
8092      x -= (x >> 1) & 0x5555555555555555ULL;
8093      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8094      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8095      return (x * 0x0101010101010101ULL) >> 56;
8096    }
8098    int popcount32c (uint32_t x)
8099    {
8100      x -= (x >> 1) & 0x55555555;
8101      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8102      x = (x + (x >> 4)) & 0x0f0f0f0f;
8103      return (x * 0x01010101) >> 24;
8104    }  */
8105 (simplify
8106  (rshift
8107   (mult
8108    (bit_and
8109     (plus:c
8110      (rshift @8 INTEGER_CST@5)
8111       (plus:c@8
8112        (bit_and @6 INTEGER_CST@7)
8113         (bit_and
8114          (rshift
8115           (minus@6 @0
8116            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8117           INTEGER_CST@10)
8118          INTEGER_CST@9)))
8119     INTEGER_CST@3)
8120    INTEGER_CST@2)
8121   INTEGER_CST@1)
8122   /* Check constants and optab.  */
8123   (with { unsigned prec = TYPE_PRECISION (type);
8124           int shift = (64 - prec) & 63;
8125           unsigned HOST_WIDE_INT c1
8126             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8127           unsigned HOST_WIDE_INT c2
8128             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8129           unsigned HOST_WIDE_INT c3
8130             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8131           unsigned HOST_WIDE_INT c4
8132             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8133    }
8134    (if (prec >= 16
8135         && prec <= 64
8136         && pow2p_hwi (prec)
8137         && TYPE_UNSIGNED (type)
8138         && integer_onep (@4)
8139         && wi::to_widest (@10) == 2
8140         && wi::to_widest (@5) == 4
8141         && wi::to_widest (@1) == prec - 8
8142         && tree_to_uhwi (@2) == c1
8143         && tree_to_uhwi (@3) == c2
8144         && tree_to_uhwi (@9) == c3
8145         && tree_to_uhwi (@7) == c3
8146         && tree_to_uhwi (@11) == c4)
8147     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8148                                          OPTIMIZE_FOR_BOTH))
8149      (convert (IFN_POPCOUNT:type @0))
8150      /* Try to do popcount in two halves.  PREC must be at least
8151         five bits for this to work without extension before adding.  */
8152      (with {
8153        tree half_type = NULL_TREE;
8154        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8155        int half_prec = 8;
8156        if (m.exists ()
8157            && m.require () != TYPE_MODE (type))
8158          {
8159            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8160            half_type = build_nonstandard_integer_type (half_prec, 1);
8161          }
8162        gcc_assert (half_prec > 2);
8163       }
8164       (if (half_type != NULL_TREE
8165            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8166                                               OPTIMIZE_FOR_BOTH))
8167        (convert (plus
8168          (IFN_POPCOUNT:half_type (convert @0))
8169          (IFN_POPCOUNT:half_type (convert (rshift @0
8170             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8172 /* __builtin_ffs needs to deal on many targets with the possible zero
8173    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8174    should lead to better code.  */
8175 (simplify
8176  (FFS tree_expr_nonzero_p@0)
8177  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8178       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8179                                          OPTIMIZE_FOR_SPEED))
8180   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8181    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8182 #endif
8184 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8185           BUILT_IN_FFSIMAX)
8186  /* __builtin_ffs (X) == 0 -> X == 0.
8187     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8188  (for cmp (eq ne)
8189   (simplify
8190    (cmp (ffs@2 @0) INTEGER_CST@1)
8191     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8192      (switch
8193       (if (integer_zerop (@1))
8194        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8195       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8196        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8197       (if (single_use (@2))
8198        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8199                                             wi::mask (tree_to_uhwi (@1),
8200                                                       false, prec)); })
8201             { wide_int_to_tree (TREE_TYPE (@0),
8202                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8203                                                   false, prec)); }))))))
8205  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8206  (for cmp (gt le)
8207       cmp2 (ne eq)
8208       cmp3 (eq ne)
8209       bit_op (bit_and bit_ior)
8210   (simplify
8211    (cmp (ffs@2 @0) INTEGER_CST@1)
8212     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8213      (switch
8214       (if (integer_zerop (@1))
8215        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8216       (if (tree_int_cst_sgn (@1) < 0)
8217        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8218       (if (wi::to_widest (@1) >= prec)
8219        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8220       (if (wi::to_widest (@1) == prec - 1)
8221        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8222                                     wi::shifted_mask (prec - 1, 1,
8223                                                       false, prec)); }))
8224       (if (single_use (@2))
8225        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8226                (cmp3 (bit_and @0
8227                               { wide_int_to_tree (TREE_TYPE (@0),
8228                                                   wi::mask (tree_to_uhwi (@1),
8229                                                   false, prec)); })
8230                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8232 #if GIMPLE
8234 /* Simplify:
8235      a = op a1
8236      r = cond ? a : b
8237      --> r = .COND_FN (cond, a, b)
8238 and,
8239     a = op a1
8240     r = cond ? b : a
8241     --> r = .COND_FN (~cond, b, a).  */
8243 (for uncond_op (UNCOND_UNARY)
8244      cond_op (COND_UNARY)
8245  (simplify
8246   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8247    (with { tree op_type = TREE_TYPE (@3); }
8248     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8249         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8250      (cond_op @0 @1 @2))))
8251  (simplify
8252   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8253    (with { tree op_type = TREE_TYPE (@3); }
8254     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8255         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8256      (cond_op (bit_not @0) @2 @1)))))
8258 /* Simplify:
8260      a = a1 op a2
8261      r = c ? a : b;
8263    to:
8265      r = c ? a1 op a2 : b;
8267    if the target can do it in one go.  This makes the operation conditional
8268    on c, so could drop potentially-trapping arithmetic, but that's a valid
8269    simplification if the result of the operation isn't needed.
8271    Avoid speculatively generating a stand-alone vector comparison
8272    on targets that might not support them.  Any target implementing
8273    conditional internal functions must support the same comparisons
8274    inside and outside a VEC_COND_EXPR.  */
8276 (for uncond_op (UNCOND_BINARY)
8277      cond_op (COND_BINARY)
8278  (simplify
8279   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8280   (with { tree op_type = TREE_TYPE (@4); }
8281    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8282         && is_truth_type_for (op_type, TREE_TYPE (@0))
8283         && single_use (@4))
8284     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8285  (simplify
8286   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8287   (with { tree op_type = TREE_TYPE (@4); }
8288    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8289         && is_truth_type_for (op_type, TREE_TYPE (@0))
8290         && single_use (@4))
8291     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8293 /* Same for ternary operations.  */
8294 (for uncond_op (UNCOND_TERNARY)
8295      cond_op (COND_TERNARY)
8296  (simplify
8297   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8298   (with { tree op_type = TREE_TYPE (@5); }
8299    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8300         && is_truth_type_for (op_type, TREE_TYPE (@0))
8301         && single_use (@5))
8302     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8303  (simplify
8304   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8305   (with { tree op_type = TREE_TYPE (@5); }
8306    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8307         && is_truth_type_for (op_type, TREE_TYPE (@0))
8308         && single_use (@5))
8309     (view_convert (cond_op (bit_not @0) @2 @3 @4
8310                   (view_convert:op_type @1)))))))
8311 #endif
8313 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8314    "else" value of an IFN_COND_*.  */
8315 (for cond_op (COND_BINARY)
8316  (simplify
8317   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8318   (with { tree op_type = TREE_TYPE (@3); }
8319    (if (element_precision (type) == element_precision (op_type))
8320     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8321  (simplify
8322   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8323   (with { tree op_type = TREE_TYPE (@5); }
8324    (if (inverse_conditions_p (@0, @2)
8325         && element_precision (type) == element_precision (op_type))
8326     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8328 /* Same for ternary operations.  */
8329 (for cond_op (COND_TERNARY)
8330  (simplify
8331   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8332   (with { tree op_type = TREE_TYPE (@4); }
8333    (if (element_precision (type) == element_precision (op_type))
8334     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8335  (simplify
8336   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8337   (with { tree op_type = TREE_TYPE (@6); }
8338    (if (inverse_conditions_p (@0, @2)
8339         && element_precision (type) == element_precision (op_type))
8340     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8342 /* Detect simplication for a conditional reduction where
8344    a = mask1 ? b : 0
8345    c = mask2 ? d + a : d
8347    is turned into
8349    c = mask1 && mask2 ? d + b : d.  */
8350 (simplify
8351   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8352    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8354 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8355    expressions like:
8357    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8358    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8360    If pointers are known not to wrap, B checks whether @1 bytes starting
8361    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8362    bytes.  A is more efficiently tested as:
8364    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8366    The equivalent expression for B is given by replacing @1 with @1 - 1:
8368    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8370    @0 and @2 can be swapped in both expressions without changing the result.
8372    The folds rely on sizetype's being unsigned (which is always true)
8373    and on its being the same width as the pointer (which we have to check).
8375    The fold replaces two pointer_plus expressions, two comparisons and
8376    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8377    the best case it's a saving of two operations.  The A fold retains one
8378    of the original pointer_pluses, so is a win even if both pointer_pluses
8379    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8380    used elsewhere, since all we end up doing is replacing a comparison with
8381    a pointer_plus.  We do still apply the fold under those circumstances
8382    though, in case applying it to other conditions eventually makes one of the
8383    pointer_pluses dead.  */
8384 (for ior (truth_orif truth_or bit_ior)
8385  (for cmp (le lt)
8386   (simplify
8387    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8388         (cmp:cs (pointer_plus@4 @2 @1) @0))
8389    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8390         && TYPE_OVERFLOW_WRAPS (sizetype)
8391         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8392     /* Calculate the rhs constant.  */
8393     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8394             offset_int rhs = off * 2; }
8395      /* Always fails for negative values.  */
8396      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8397       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8398          pick a canonical order.  This increases the chances of using the
8399          same pointer_plus in multiple checks.  */
8400       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8401               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8402        (if (cmp == LT_EXPR)
8403         (gt (convert:sizetype
8404              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8405                                      { swap_p ? @0 : @2; }))
8406             { rhs_tree; })
8407         (gt (convert:sizetype
8408              (pointer_diff:ssizetype
8409               (pointer_plus { swap_p ? @2 : @0; }
8410                             { wide_int_to_tree (sizetype, off); })
8411               { swap_p ? @0 : @2; }))
8412             { rhs_tree; })))))))))
8414 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8415    element of @1.  */
8416 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8417  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8418   (with { int i = single_nonzero_element (@1); }
8419    (if (i >= 0)
8420     (with { tree elt = vector_cst_elt (@1, i);
8421             tree elt_type = TREE_TYPE (elt);
8422             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8423             tree size = bitsize_int (elt_bits);
8424             tree pos = bitsize_int (elt_bits * i); }
8425      (view_convert
8426       (bit_and:elt_type
8427        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8428        { elt; })))))))
8430 /* Fold reduction of a single nonzero element constructor.  */
8431 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8432   (simplify (reduc (CONSTRUCTOR@0))
8433     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8434                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8435             tree elt = ctor_single_nonzero_element (ctor); }
8436       (if (elt
8437            && !HONOR_SNANS (type)
8438            && !HONOR_SIGNED_ZEROS (type))
8439         { elt; }))))
8441 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8442 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8443             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8444      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8445   (simplify (reduc (op @0 VECTOR_CST@1))
8446     (op (reduc:type @0) (reduc:type @1))))
8448 /* Simplify vector floating point operations of alternating sub/add pairs
8449    into using an fneg of a wider element type followed by a normal add.
8450    under IEEE 754 the fneg of the wider type will negate every even entry
8451    and when doing an add we get a sub of the even and add of every odd
8452    elements.  */
8453 (for plusminus (plus minus)
8454      minusplus (minus plus)
8455  (simplify
8456   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8457    (if (!VECTOR_INTEGER_TYPE_P (type)
8458         && !FLOAT_WORDS_BIG_ENDIAN
8459         /* plus is commutative, while minus is not, so :c can't be used.
8460            Do equality comparisons by hand and at the end pick the operands
8461            from the minus.  */
8462         && (operand_equal_p (@0, @2, 0)
8463             ? operand_equal_p (@1, @3, 0)
8464             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8465    (with
8466     {
8467       /* Build a vector of integers from the tree mask.  */
8468       vec_perm_builder builder;
8469     }
8470     (if (tree_to_vec_perm_builder (&builder, @4))
8471      (with
8472       {
8473         /* Create a vec_perm_indices for the integer vector.  */
8474         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8475         vec_perm_indices sel (builder, 2, nelts);
8476         machine_mode vec_mode = TYPE_MODE (type);
8477         machine_mode wide_mode;
8478         scalar_mode wide_elt_mode;
8479         poly_uint64 wide_nunits;
8480         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8481       }
8482       (if (VECTOR_MODE_P (vec_mode)
8483            && sel.series_p (0, 2, 0, 2)
8484            && sel.series_p (1, 2, nelts + 1, 2)
8485            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
8486            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
8487            && related_vector_mode (vec_mode, wide_elt_mode,
8488                                    wide_nunits).exists (&wide_mode))
8489        (with
8490         {
8491           tree stype
8492             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
8493                                               TYPE_UNSIGNED (type));
8494           tree ntype = build_vector_type_for_mode (stype, wide_mode);
8496           /* The format has to be a non-extended ieee format.  */
8497           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
8498           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
8499         }
8500         (if (TYPE_MODE (stype) != BLKmode
8501              && VECTOR_TYPE_P (ntype)
8502              && fmt_old != NULL
8503              && fmt_new != NULL)
8504          (with
8505           {
8506             /* If the target doesn't support v1xx vectors, try using
8507                scalar mode xx instead.  */
8508             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
8509                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
8510               ntype = stype;
8511           }
8512           (if (fmt_new->signbit_rw
8513                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
8514                && fmt_new->signbit_rw == fmt_new->signbit_ro
8515                && targetm.can_change_mode_class (TYPE_MODE (ntype),
8516                                                  TYPE_MODE (type), ALL_REGS)
8517                && ((optimize_vectors_before_lowering_p ()
8518                     && VECTOR_TYPE_P (ntype))
8519                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
8520            (if (plusminus == PLUS_EXPR)
8521             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
8522             (minus @0 (view_convert:type
8523                         (negate (view_convert:ntype @1))))))))))))))))
8525 (simplify
8526  (vec_perm @0 @1 VECTOR_CST@2)
8527  (with
8528   {
8529     tree op0 = @0, op1 = @1, op2 = @2;
8530     machine_mode result_mode = TYPE_MODE (type);
8531     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
8533     /* Build a vector of integers from the tree mask.  */
8534     vec_perm_builder builder;
8535   }
8536   (if (tree_to_vec_perm_builder (&builder, op2))
8537    (with
8538     {
8539       /* Create a vec_perm_indices for the integer vector.  */
8540       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8541       bool single_arg = (op0 == op1);
8542       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8543     }
8544     (if (sel.series_p (0, 1, 0, 1))
8545      { op0; }
8546      (if (sel.series_p (0, 1, nelts, 1))
8547       { op1; }
8548       (with
8549        {
8550          if (!single_arg)
8551            {
8552              if (sel.all_from_input_p (0))
8553                op1 = op0;
8554              else if (sel.all_from_input_p (1))
8555                {
8556                  op0 = op1;
8557                  sel.rotate_inputs (1);
8558                }
8559              else if (known_ge (poly_uint64 (sel[0]), nelts))
8560                {
8561                  std::swap (op0, op1);
8562                  sel.rotate_inputs (1);
8563                }
8564            }
8565          gassign *def;
8566          tree cop0 = op0, cop1 = op1;
8567          if (TREE_CODE (op0) == SSA_NAME
8568              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
8569              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8570            cop0 = gimple_assign_rhs1 (def);
8571          if (TREE_CODE (op1) == SSA_NAME
8572              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
8573              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8574            cop1 = gimple_assign_rhs1 (def);
8575          tree t;
8576        }
8577        (if ((TREE_CODE (cop0) == VECTOR_CST
8578              || TREE_CODE (cop0) == CONSTRUCTOR)
8579             && (TREE_CODE (cop1) == VECTOR_CST
8580                 || TREE_CODE (cop1) == CONSTRUCTOR)
8581             && (t = fold_vec_perm (type, cop0, cop1, sel)))
8582         { t; }
8583         (with
8584          {
8585            bool changed = (op0 == op1 && !single_arg);
8586            tree ins = NULL_TREE;
8587            unsigned at = 0;
8589            /* See if the permutation is performing a single element
8590               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
8591               in that case.  But only if the vector mode is supported,
8592               otherwise this is invalid GIMPLE.  */
8593            if (op_mode != BLKmode
8594                && (TREE_CODE (cop0) == VECTOR_CST
8595                    || TREE_CODE (cop0) == CONSTRUCTOR
8596                    || TREE_CODE (cop1) == VECTOR_CST
8597                    || TREE_CODE (cop1) == CONSTRUCTOR))
8598              {
8599                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
8600                if (insert_first_p)
8601                  {
8602                    /* After canonicalizing the first elt to come from the
8603                       first vector we only can insert the first elt from
8604                       the first vector.  */
8605                    at = 0;
8606                    if ((ins = fold_read_from_vector (cop0, sel[0])))
8607                      op0 = op1;
8608                  }
8609                /* The above can fail for two-element vectors which always
8610                   appear to insert the first element, so try inserting
8611                   into the second lane as well.  For more than two
8612                   elements that's wasted time.  */
8613                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
8614                  {
8615                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
8616                    for (at = 0; at < encoded_nelts; ++at)
8617                      if (maybe_ne (sel[at], at))
8618                        break;
8619                    if (at < encoded_nelts
8620                        && (known_eq (at + 1, nelts)
8621                            || sel.series_p (at + 1, 1, at + 1, 1)))
8622                      {
8623                        if (known_lt (poly_uint64 (sel[at]), nelts))
8624                          ins = fold_read_from_vector (cop0, sel[at]);
8625                        else
8626                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
8627                      }
8628                  }
8629              }
8631            /* Generate a canonical form of the selector.  */
8632            if (!ins && sel.encoding () != builder)
8633              {
8634                /* Some targets are deficient and fail to expand a single
8635                   argument permutation while still allowing an equivalent
8636                   2-argument version.  */
8637                tree oldop2 = op2;
8638                if (sel.ninputs () == 2
8639                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
8640                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8641                else
8642                  {
8643                    vec_perm_indices sel2 (builder, 2, nelts);
8644                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
8645                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
8646                    else
8647                      /* Not directly supported with either encoding,
8648                         so use the preferred form.  */
8649                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8650                  }
8651                if (!operand_equal_p (op2, oldop2, 0))
8652                  changed = true;
8653              }
8654          }
8655          (if (ins)
8656           (bit_insert { op0; } { ins; }
8657            { bitsize_int (at * vector_element_bits (type)); })
8658           (if (changed)
8659            (vec_perm { op0; } { op1; } { op2; }))))))))))))
8661 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
8663 (match vec_same_elem_p
8664  (vec_duplicate @0))
8666 (match vec_same_elem_p
8667  CONSTRUCTOR@0
8668  (if (TREE_CODE (@0) == SSA_NAME
8669       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
8671 (match vec_same_elem_p
8672  @0
8673  (if (uniform_vector_p (@0))))
8676 (simplify
8677  (vec_perm vec_same_elem_p@0 @0 @1)
8678  (if (types_match (type, TREE_TYPE (@0)))
8679   @0
8680   (with
8681    {
8682      tree elem = uniform_vector_p (@0);
8683    }
8684    (if (elem)
8685     { build_vector_from_val (type, elem); }))))
8687 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
8688 (simplify
8689  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8690  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8691   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8692 (simplify
8693  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8694  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8695   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8698 /* Merge
8699    c = VEC_PERM_EXPR <a, b, VCST0>;
8700    d = VEC_PERM_EXPR <c, c, VCST1>;
8701    to
8702    d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
8704 (simplify
8705  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
8706  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
8707   (with
8708    {
8709      machine_mode result_mode = TYPE_MODE (type);
8710      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
8711      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8712      vec_perm_builder builder0;
8713      vec_perm_builder builder1;
8714      vec_perm_builder builder2 (nelts, nelts, 1);
8715    }
8716    (if (tree_to_vec_perm_builder (&builder0, @3)
8717         && tree_to_vec_perm_builder (&builder1, @4))
8718     (with
8719      {
8720        vec_perm_indices sel0 (builder0, 2, nelts);
8721        vec_perm_indices sel1 (builder1, 1, nelts);
8723        for (int i = 0; i < nelts; i++)
8724          builder2.quick_push (sel0[sel1[i].to_constant ()]);
8726        vec_perm_indices sel2 (builder2, 2, nelts);
8728        tree op0 = NULL_TREE;
8729        /* If the new VEC_PERM_EXPR can't be handled but both
8730           original VEC_PERM_EXPRs can, punt.
8731           If one or both of the original VEC_PERM_EXPRs can't be
8732           handled and the new one can't be either, don't increase
8733           number of VEC_PERM_EXPRs that can't be handled.  */
8734        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
8735            || (single_use (@0)
8736                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
8737                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
8738                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
8739          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
8740      }
8741      (if (op0)
8742       (vec_perm @1 @2 { op0; })))))))
8745 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
8746    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
8747    constant which when multiplied by a power of 2 contains a unique value
8748    in the top 5 or 6 bits.  This is then indexed into a table which maps it
8749    to the number of trailing zeroes.  */
8750 (match (ctz_table_index @1 @2 @3)
8751   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
8753 (match (cond_expr_convert_p @0 @2 @3 @6)
8754  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
8755   (if (INTEGRAL_TYPE_P (type)
8756        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8757        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8758        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8759        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8760        && TYPE_PRECISION (TREE_TYPE (@0))
8761           == TYPE_PRECISION (TREE_TYPE (@2))
8762        && TYPE_PRECISION (TREE_TYPE (@0))
8763           == TYPE_PRECISION (TREE_TYPE (@3))
8764        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8765           signess when convert is truncation, but not ok for extension since
8766           it's sign_extend vs zero_extend.  */
8767        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8768            || (TYPE_UNSIGNED (TREE_TYPE (@2))
8769                == TYPE_UNSIGNED (TREE_TYPE (@3))))
8770        && single_use (@4)
8771        && single_use (@5))))
8773 (for bit_op (bit_and bit_ior bit_xor)
8774  (match (bitwise_induction_p @0 @2 @3)
8775   (bit_op:c
8776    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8777    @3)))
8779 (match (bitwise_induction_p @0 @2 @3)
8780  (bit_not
8781   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
8783 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
8784    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
8785 (simplify
8786   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
8787   (with { auto i = wi::neg (wi::to_wide (@2)); }
8788   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
8789     (if (wi::popcount (i) == 1
8790          && (wi::to_wide (@1)) == (i - 1))
8791       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8792         (bit_and @0 @1)
8793       (cond (le @0 @1) @0 (bit_and @0 @1))))))
8795 /* -x & 1 -> x & 1.  */
8796 (simplify 
8797  (bit_and (negate @0) integer_onep@1)
8798  (if (!TYPE_OVERFLOW_SANITIZED (type))
8799   (bit_and @0 @1)))
8801 /* Optimize
8802    c1 = VEC_PERM_EXPR (a, a, mask)
8803    c2 = VEC_PERM_EXPR (b, b, mask)
8804    c3 = c1 op c2
8805    -->
8806    c = a op b
8807    c3 = VEC_PERM_EXPR (c, c, mask)
8808    For all integer non-div operations.  */
8809 (for op (plus minus mult bit_and bit_ior bit_xor
8810          lshift rshift)
8811  (simplify
8812   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
8813    (if (VECTOR_INTEGER_TYPE_P (type))
8814     (vec_perm (op@3 @0 @1) @3 @2))))
8816 /* Similar for float arithmetic when permutation constant covers
8817    all vector elements.  */
8818 (for op (plus minus mult)
8819  (simplify
8820   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
8821    (if (VECTOR_FLOAT_TYPE_P (type)
8822         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8823     (with
8824      {
8825        tree perm_cst = @2;
8826        vec_perm_builder builder;
8827        bool full_perm_p = false;
8828        if (tree_to_vec_perm_builder (&builder, perm_cst))
8829          {
8830            unsigned HOST_WIDE_INT nelts;
8832            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8833            /* Create a vec_perm_indices for the VECTOR_CST.  */
8834            vec_perm_indices sel (builder, 1, nelts);
8836            /* Check if perm indices covers all vector elements.  */
8837            if (sel.encoding ().encoded_full_vector_p ())
8838              {
8839                auto_sbitmap seen (nelts);
8840                bitmap_clear (seen);
8842                unsigned HOST_WIDE_INT count = 0, i;
8844                for (i = 0; i < nelts; i++)
8845                  {
8846                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
8847                     break;
8848                    count++;
8849                  }
8850                full_perm_p = count == nelts;
8851              }
8852          }
8853       }
8854       (if (full_perm_p)
8855         (vec_perm (op@3 @0 @1) @3 @2))))))