ada: Reorder components in Ada.Containers.Bounded_Doubly_Linked_Lists
[official-gcc.git] / gcc / match.pd
blob6e32f476e973c91259bde36054dd5548a756d1d4
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        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
1857        /* ???  This transform conflicts with fold-const.cc doing
1858           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1859           constants (if x has signed type, the sign bit cannot be set
1860           in c).  This folds extension into the BIT_AND_EXPR.
1861           Restrict it to GIMPLE to avoid endless recursions.  */
1862        && (bitop != BIT_AND_EXPR || GIMPLE)
1863        && (/* That's a good idea if the conversion widens the operand, thus
1864               after hoisting the conversion the operation will be narrower.
1865               It is also a good if the conversion is a nop as moves the
1866               conversion to one side; allowing for combining of the conversions.  */
1867            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1868            /* The conversion check for being a nop can only be done at the gimple
1869               level as fold_binary has some re-association code which can conflict
1870               with this if there is a "constant" which is not a full INTEGER_CST.  */
1871            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1872            /* It's also a good idea if the conversion is to a non-integer
1873               mode.  */
1874            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1875            /* Or if the precision of TO is not the same as the precision
1876               of its mode.  */
1877            || !type_has_mode_precision_p (type)
1878            /* In GIMPLE, getting rid of 2 conversions for one new results
1879               in smaller IL.  */
1880            || (GIMPLE
1881                && TREE_CODE (@1) != INTEGER_CST
1882                && tree_nop_conversion_p (type, TREE_TYPE (@0))
1883                && single_use (@2)
1884                && single_use (@3))))
1885    (convert (bitop @0 (convert @1)))))
1886  /* In GIMPLE, getting rid of 2 conversions for one new results
1887     in smaller IL.  */
1888  (simplify
1889   (convert (bitop:cs@2 (nop_convert:s @0) @1))
1890   (if (GIMPLE
1891        && TREE_CODE (@1) != INTEGER_CST
1892        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1893        && types_match (type, @0)
1894        && !POINTER_TYPE_P (TREE_TYPE (@0))
1895        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
1896    (bitop @0 (convert @1)))))
1898 (for bitop (bit_and bit_ior)
1899      rbitop (bit_ior bit_and)
1900   /* (x | y) & x -> x */
1901   /* (x & y) | x -> x */
1902  (simplify
1903   (bitop:c (rbitop:c @0 @1) @0)
1904   @0)
1905  /* (~x | y) & x -> x & y */
1906  /* (~x & y) | x -> x | y */
1907  (simplify
1908   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1909   (bitop @0 @1)))
1911 /* ((x | y) & z) | x -> (z & y) | x */
1912 (simplify
1913   (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1914   (bit_ior (bit_and @2 @1) @0))
1916 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1917 (simplify
1918   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1919   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
1921 /* Combine successive equal operations with constants.  */
1922 (for bitop (bit_and bit_ior bit_xor)
1923  (simplify
1924   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1925   (if (!CONSTANT_CLASS_P (@0))
1926    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1927       folded to a constant.  */
1928    (bitop @0 (bitop! @1 @2))
1929    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1930       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1931       the values involved are such that the operation can't be decided at
1932       compile time.  Try folding one of @0 or @1 with @2 to see whether
1933       that combination can be decided at compile time.
1935       Keep the existing form if both folds fail, to avoid endless
1936       oscillation.  */
1937    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1938     (if (cst1)
1939      (bitop @1 { cst1; })
1940      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1941       (if (cst2)
1942        (bitop @0 { cst2; }))))))))
1944 /* Try simple folding for X op !X, and X op X with the help
1945    of the truth_valued_p and logical_inverted_value predicates.  */
1946 (match truth_valued_p
1947  @0
1948  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1949 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1950  (match truth_valued_p
1951   (op @0 @1)))
1952 (match truth_valued_p
1953   (truth_not @0))
1955 (match (logical_inverted_value @0)
1956  (truth_not @0))
1957 (match (logical_inverted_value @0)
1958  (bit_not truth_valued_p@0))
1959 (match (logical_inverted_value @0)
1960  (eq @0 integer_zerop))
1961 (match (logical_inverted_value @0)
1962  (ne truth_valued_p@0 integer_truep))
1963 (match (logical_inverted_value @0)
1964  (bit_xor truth_valued_p@0 integer_truep))
1966 /* X & !X -> 0.  */
1967 (simplify
1968  (bit_and:c @0 (logical_inverted_value @0))
1969  { build_zero_cst (type); })
1970 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1971 (for op (bit_ior bit_xor)
1972  (simplify
1973   (op:c truth_valued_p@0 (logical_inverted_value @0))
1974   { constant_boolean_node (true, type); }))
1975 /* X ==/!= !X is false/true.  */
1976 (for op (eq ne)
1977  (simplify
1978   (op:c truth_valued_p@0 (logical_inverted_value @0))
1979   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1981 /* ~~x -> x */
1982 (simplify
1983   (bit_not (bit_not @0))
1984   @0)
1986 (match zero_one_valued_p
1987  @0
1988  (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1989 (match zero_one_valued_p
1990  truth_valued_p@0)
1992 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
1993 (simplify
1994  (mult zero_one_valued_p@0 zero_one_valued_p@1)
1995  (if (INTEGRAL_TYPE_P (type))
1996   (bit_and @0 @1)))
1998 (for cmp (tcc_comparison)
1999      icmp (inverted_tcc_comparison)
2000  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2001  (simplify
2002   (bit_ior
2003    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2004    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2005     (if (INTEGRAL_TYPE_P (type)
2006          /* The scalar version has to be canonicalized after vectorization
2007             because it makes unconditional loads conditional ones, which
2008             means we lose vectorization because the loads may trap.  */
2009          && canonicalize_math_after_vectorization_p ())
2010      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2012  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2013     canonicalized further and we recognize the conditional form:
2014     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2015  (simplify
2016   (bit_ior
2017    (cond (cmp@0  @01 @02) @3 zerop)
2018    (cond (icmp@4 @01 @02) @5 zerop))
2019     (if (INTEGRAL_TYPE_P (type)
2020          /* The scalar version has to be canonicalized after vectorization
2021             because it makes unconditional loads conditional ones, which
2022             means we lose vectorization because the loads may trap.  */
2023          && canonicalize_math_after_vectorization_p ())
2024     (cond @0 @3 @5)))
2026  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2027     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2028  (simplify
2029   (bit_ior
2030    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2031    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2032     (if (integer_zerop (@5))
2033      (switch
2034       (if (integer_onep (@4))
2035        (bit_and (vec_cond @0 @2 @3) @4))
2036         (if (integer_minus_onep (@4))
2037          (vec_cond @0 @2 @3)))
2038     (if (integer_zerop (@4))
2039      (switch
2040       (if (integer_onep (@5))
2041        (bit_and (vec_cond @0 @3 @2) @5))
2042       (if (integer_minus_onep (@5))
2043        (vec_cond @0 @3 @2))))))
2045  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2046     into a < b ? d : c.  */
2047  (simplify
2048   (bit_ior
2049    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2050    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2051     (vec_cond @0 @2 @3)))
2053 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2054 (simplify
2055  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2056  (if (INTEGRAL_TYPE_P (type)
2057       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2058       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2059       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
2060   (mult (convert @0) @1)))
2062 /* Narrow integer multiplication by a zero_one_valued_p operand.
2063    Multiplication by [0,1] is guaranteed not to overflow.  */
2064 (simplify
2065  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2066  (if (INTEGRAL_TYPE_P (type)
2067       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2068       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2069   (mult (convert @1) (convert @2))))
2071 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2072    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2073    as some targets (such as x86's SSE) may return zero for larger C.  */
2074 (simplify
2075   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2076   (if (tree_fits_shwi_p (@1)
2077        && tree_to_shwi (@1) > 0
2078        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2079     (convert @0)))
2081 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2082    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2083    as some targets (such as x86's SSE) may return zero for larger C.  */
2084 (simplify
2085   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2086   (if (tree_fits_shwi_p (@1)
2087        && tree_to_shwi (@1) > 0
2088        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2089     (eq @0 @2)))
2091 /* Convert ~ (-A) to A - 1.  */
2092 (simplify
2093  (bit_not (convert? (negate @0)))
2094  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2095       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2096   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2098 /* Convert - (~A) to A + 1.  */
2099 (simplify
2100  (negate (nop_convert? (bit_not @0)))
2101  (plus (view_convert @0) { build_each_one_cst (type); }))
2103 /* (a & b) ^ (a == b) -> !(a | b) */
2104 /* (a & b) == (a ^ b) -> !(a | b) */
2105 (for first_op (bit_xor eq)
2106      second_op (eq bit_xor)
2107  (simplify
2108   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2109     (bit_not (bit_ior @0 @1))))
2111 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2112 (simplify
2113  (bit_not (convert? (minus @0 integer_each_onep)))
2114  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2115       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2116   (convert (negate @0))))
2117 (simplify
2118  (bit_not (convert? (plus @0 integer_all_onesp)))
2119  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2120       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2121   (convert (negate @0))))
2123 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2124 (simplify
2125  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2126  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2127   (convert (bit_xor @0 (bit_not @1)))))
2128 (simplify
2129  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2130  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2131   (convert (bit_xor @0 @1))))
2133 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2134 (simplify
2135  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2136  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2137   (bit_not (bit_xor (view_convert @0) @1))))
2139 /* ~(a ^ b) is a == b for truth valued a and b.  */
2140 (simplify
2141  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2142  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2143       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2144   (convert (eq @0 @1))))
2146 /* (~a) == b is a ^ b for truth valued a and b.  */
2147 (simplify
2148  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2149  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2150       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2151   (convert (bit_xor @0 @1))))
2153 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2154 (simplify
2155  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2156  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2158 /* Fold A - (A & B) into ~B & A.  */
2159 (simplify
2160  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2161  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2162       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2163   (convert (bit_and (bit_not @1) @0))))
2165 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2166 (if (!canonicalize_math_p ())
2167  (for cmp (tcc_comparison)
2168   (simplify
2169    (mult:c (convert (cmp@0 @1 @2)) @3)
2170    (if (INTEGRAL_TYPE_P (type)
2171         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2172      (cond @0 @3 { build_zero_cst (type); })))
2173 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2174   (simplify
2175    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2176    (if (INTEGRAL_TYPE_P (type)
2177         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2178      (cond @0 @3 { build_zero_cst (type); })))
2182 /* For integral types with undefined overflow and C != 0 fold
2183    x * C EQ/NE y * C into x EQ/NE y.  */
2184 (for cmp (eq ne)
2185  (simplify
2186   (cmp (mult:c @0 @1) (mult:c @2 @1))
2187   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2188        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2189        && tree_expr_nonzero_p (@1))
2190    (cmp @0 @2))))
2192 /* For integral types with wrapping overflow and C odd fold
2193    x * C EQ/NE y * C into x EQ/NE y.  */
2194 (for cmp (eq ne)
2195  (simplify
2196   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2197   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2198        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2199        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2200    (cmp @0 @2))))
2202 /* For integral types with undefined overflow and C != 0 fold
2203    x * C RELOP y * C into:
2205    x RELOP y for nonnegative C
2206    y RELOP x for negative C  */
2207 (for cmp (lt gt le ge)
2208  (simplify
2209   (cmp (mult:c @0 @1) (mult:c @2 @1))
2210   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2211        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2212    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2213     (cmp @0 @2)
2214    (if (TREE_CODE (@1) == INTEGER_CST
2215         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2216     (cmp @2 @0))))))
2218 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2219 (for cmp (le gt)
2220      icmp (gt le)
2221  (simplify
2222   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2223    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2224         && TYPE_UNSIGNED (TREE_TYPE (@0))
2225         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2226         && (wi::to_wide (@2)
2227             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2228     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2229      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2231 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2232 (for cmp (simple_comparison)
2233  (simplify
2234   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2235   (if (element_precision (@3) >= element_precision (@0)
2236        && types_match (@0, @1))
2237    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2238     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2239      (cmp @1 @0)
2240      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2241       (with
2242        {
2243         tree utype = unsigned_type_for (TREE_TYPE (@0));
2244        }
2245        (cmp (convert:utype @1) (convert:utype @0)))))
2246     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2247      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2248       (cmp @0 @1)
2249       (with
2250        {
2251         tree utype = unsigned_type_for (TREE_TYPE (@0));
2252        }
2253        (cmp (convert:utype @0) (convert:utype @1)))))))))
2255 /* X / C1 op C2 into a simple range test.  */
2256 (for cmp (simple_comparison)
2257  (simplify
2258   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2259   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2260        && integer_nonzerop (@1)
2261        && !TREE_OVERFLOW (@1)
2262        && !TREE_OVERFLOW (@2))
2263    (with { tree lo, hi; bool neg_overflow;
2264            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2265                                                    &neg_overflow); }
2266     (switch
2267      (if (code == LT_EXPR || code == GE_EXPR)
2268        (if (TREE_OVERFLOW (lo))
2269         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2270         (if (code == LT_EXPR)
2271          (lt @0 { lo; })
2272          (ge @0 { lo; }))))
2273      (if (code == LE_EXPR || code == GT_EXPR)
2274        (if (TREE_OVERFLOW (hi))
2275         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2276         (if (code == LE_EXPR)
2277          (le @0 { hi; })
2278          (gt @0 { hi; }))))
2279      (if (!lo && !hi)
2280       { build_int_cst (type, code == NE_EXPR); })
2281      (if (code == EQ_EXPR && !hi)
2282       (ge @0 { lo; }))
2283      (if (code == EQ_EXPR && !lo)
2284       (le @0 { hi; }))
2285      (if (code == NE_EXPR && !hi)
2286       (lt @0 { lo; }))
2287      (if (code == NE_EXPR && !lo)
2288       (gt @0 { hi; }))
2289      (if (GENERIC)
2290       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2291                            lo, hi); })
2292      (with
2293       {
2294         tree etype = range_check_type (TREE_TYPE (@0));
2295         if (etype)
2296           {
2297             hi = fold_convert (etype, hi);
2298             lo = fold_convert (etype, lo);
2299             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2300           }
2301       }
2302       (if (etype && hi && !TREE_OVERFLOW (hi))
2303        (if (code == EQ_EXPR)
2304         (le (minus (convert:etype @0) { lo; }) { hi; })
2305         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2307 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2308 (for op (lt le ge gt)
2309  (simplify
2310   (op (plus:c @0 @2) (plus:c @1 @2))
2311   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2312        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2313    (op @0 @1))))
2315 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2316    when C is an unsigned integer constant with only the MSB set, and X and
2317    Y have types of equal or lower integer conversion rank than C's.  */
2318 (for op (lt le ge gt)
2319  (simplify
2320   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2321   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2322        && TYPE_UNSIGNED (TREE_TYPE (@0))
2323        && wi::only_sign_bit_p (wi::to_wide (@0)))
2324    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2325     (op (convert:stype @1) (convert:stype @2))))))
2327 /* For equality and subtraction, this is also true with wrapping overflow.  */
2328 (for op (eq ne minus)
2329  (simplify
2330   (op (plus:c @0 @2) (plus:c @1 @2))
2331   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2333            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2334    (op @0 @1))))
2336 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2337 (for op (lt le ge gt)
2338  (simplify
2339   (op (minus @0 @2) (minus @1 @2))
2340   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2341        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2342    (op @0 @1))))
2343 /* For equality and subtraction, this is also true with wrapping overflow.  */
2344 (for op (eq ne minus)
2345  (simplify
2346   (op (minus @0 @2) (minus @1 @2))
2347   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2348        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2349            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2350    (op @0 @1))))
2351 /* And for pointers...  */
2352 (for op (simple_comparison)
2353  (simplify
2354   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2355   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2356    (op @0 @1))))
2357 (simplify
2358  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2359  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2360       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2361   (pointer_diff @0 @1)))
2363 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2364 (for op (lt le ge gt)
2365  (simplify
2366   (op (minus @2 @0) (minus @2 @1))
2367   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2368        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2369    (op @1 @0))))
2370 /* For equality and subtraction, this is also true with wrapping overflow.  */
2371 (for op (eq ne minus)
2372  (simplify
2373   (op (minus @2 @0) (minus @2 @1))
2374   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2375        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2376            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2377    (op @1 @0))))
2378 /* And for pointers...  */
2379 (for op (simple_comparison)
2380  (simplify
2381   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2382   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2383    (op @1 @0))))
2384 (simplify
2385  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2386  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2387       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2388   (pointer_diff @1 @0)))
2390 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2391 (for op (lt le gt ge)
2392  (simplify
2393   (op:c (plus:c@2 @0 @1) @1)
2394   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2395        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2396        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2397        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2398    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2399 /* For equality, this is also true with wrapping overflow.  */
2400 (for op (eq ne)
2401  (simplify
2402   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2403   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2404        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2405            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2406        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2407        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2408        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2409    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2410  (simplify
2411   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2412   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2413        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2414        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2415    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2417 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2418 (for neeq (ne eq)
2419  (simplify
2420   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2421    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2422     (if (ptr_difference_const (@0, @2, &diff))
2423      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2424  (simplify
2425   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2426    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2427     (if (ptr_difference_const (@0, @2, &diff))
2428      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2430 /* X - Y < X is the same as Y > 0 when there is no overflow.
2431    For equality, this is also true with wrapping overflow.  */
2432 (for op (simple_comparison)
2433  (simplify
2434   (op:c @0 (minus@2 @0 @1))
2435   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2436        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2437            || ((op == EQ_EXPR || op == NE_EXPR)
2438                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2439        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2440    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2442 /* Transform:
2443    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2444    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2445 (for cmp (eq ne)
2446      ocmp (lt ge)
2447  (simplify
2448   (cmp (trunc_div @0 @1) integer_zerop)
2449   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2450        /* Complex ==/!= is allowed, but not </>=.  */
2451        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2452        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2453    (ocmp @0 @1))))
2455 /* X == C - X can never be true if C is odd.  */
2456 (for cmp (eq ne)
2457  (simplify
2458   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2459   (if (TREE_INT_CST_LOW (@1) & 1)
2460    { constant_boolean_node (cmp == NE_EXPR, type); })))
2462 /* Arguments on which one can call get_nonzero_bits to get the bits
2463    possibly set.  */
2464 (match with_possible_nonzero_bits
2465  INTEGER_CST@0)
2466 (match with_possible_nonzero_bits
2467  SSA_NAME@0
2468  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2469 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2470 (match (with_possible_nonzero_bits2 @0)
2471  with_possible_nonzero_bits@0)
2472 (match (with_possible_nonzero_bits2 @0)
2473  (bit_and:c with_possible_nonzero_bits@0 @2))
2475 /* Same for bits that are known to be set, but we do not have
2476    an equivalent to get_nonzero_bits yet.  */
2477 (match (with_certain_nonzero_bits2 @0)
2478  INTEGER_CST@0)
2479 (match (with_certain_nonzero_bits2 @0)
2480  (bit_ior @1 INTEGER_CST@0))
2482 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2483 (for cmp (eq ne)
2484  (simplify
2485   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2486   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2487    { constant_boolean_node (cmp == NE_EXPR, type); })))
2489 /* ((X inner_op C0) outer_op C1)
2490    With X being a tree where value_range has reasoned certain bits to always be
2491    zero throughout its computed value range,
2492    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2493    where zero_mask has 1's for all bits that are sure to be 0 in
2494    and 0's otherwise.
2495    if (inner_op == '^') C0 &= ~C1;
2496    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2497    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2499 (for inner_op (bit_ior bit_xor)
2500      outer_op (bit_xor bit_ior)
2501 (simplify
2502  (outer_op
2503   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2504  (with
2505   {
2506     bool fail = false;
2507     wide_int zero_mask_not;
2508     wide_int C0;
2509     wide_int cst_emit;
2511     if (TREE_CODE (@2) == SSA_NAME)
2512       zero_mask_not = get_nonzero_bits (@2);
2513     else
2514       fail = true;
2516     if (inner_op == BIT_XOR_EXPR)
2517       {
2518         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2519         cst_emit = C0 | wi::to_wide (@1);
2520       }
2521     else
2522       {
2523         C0 = wi::to_wide (@0);
2524         cst_emit = C0 ^ wi::to_wide (@1);
2525       }
2526   }
2527   (if (!fail && (C0 & zero_mask_not) == 0)
2528    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2529    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2530     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2532 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2533 (simplify
2534   (pointer_plus (pointer_plus:s @0 @1) @3)
2535   (pointer_plus @0 (plus @1 @3)))
2536 #if GENERIC
2537 (simplify
2538   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2539   (convert:type (pointer_plus @0 (plus @1 @3))))
2540 #endif
2542 /* Pattern match
2543      tem1 = (long) ptr1;
2544      tem2 = (long) ptr2;
2545      tem3 = tem2 - tem1;
2546      tem4 = (unsigned long) tem3;
2547      tem5 = ptr1 + tem4;
2548    and produce
2549      tem5 = ptr2;  */
2550 (simplify
2551   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2552   /* Conditionally look through a sign-changing conversion.  */
2553   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2554        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2555             || (GENERIC && type == TREE_TYPE (@1))))
2556    @1))
2557 (simplify
2558   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2559   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2560    (convert @1)))
2562 /* Pattern match
2563      tem = (sizetype) ptr;
2564      tem = tem & algn;
2565      tem = -tem;
2566      ... = ptr p+ tem;
2567    and produce the simpler and easier to analyze with respect to alignment
2568      ... = ptr & ~algn;  */
2569 (simplify
2570   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2571   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2572    (bit_and @0 { algn; })))
2574 /* Try folding difference of addresses.  */
2575 (simplify
2576  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2577  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2578   (with { poly_int64 diff; }
2579    (if (ptr_difference_const (@0, @1, &diff))
2580     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2581 (simplify
2582  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2583  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2584   (with { poly_int64 diff; }
2585    (if (ptr_difference_const (@0, @1, &diff))
2586     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2587 (simplify
2588  (minus (convert ADDR_EXPR@0) (convert @1))
2589  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2590   (with { poly_int64 diff; }
2591    (if (ptr_difference_const (@0, @1, &diff))
2592     { build_int_cst_type (type, diff); }))))
2593 (simplify
2594  (minus (convert @0) (convert ADDR_EXPR@1))
2595  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2596   (with { poly_int64 diff; }
2597    (if (ptr_difference_const (@0, @1, &diff))
2598     { build_int_cst_type (type, diff); }))))
2599 (simplify
2600  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2601  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2602       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2603   (with { poly_int64 diff; }
2604    (if (ptr_difference_const (@0, @1, &diff))
2605     { build_int_cst_type (type, diff); }))))
2606 (simplify
2607  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2608  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2609       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2610   (with { poly_int64 diff; }
2611    (if (ptr_difference_const (@0, @1, &diff))
2612     { build_int_cst_type (type, diff); }))))
2614 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2615 (simplify
2616  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2617  (with { poly_int64 diff; }
2618    (if (ptr_difference_const (@0, @2, &diff))
2619     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2620 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2621 (simplify
2622  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2623  (with { poly_int64 diff; }
2624    (if (ptr_difference_const (@0, @2, &diff))
2625     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2626 (simplify
2627  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2628  (with { poly_int64 diff; }
2629    (if (ptr_difference_const (@0, @1, &diff))
2630     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2632 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2633 (simplify
2634  (convert (pointer_diff @0 INTEGER_CST@1))
2635  (if (POINTER_TYPE_P (type))
2636   { build_fold_addr_expr_with_type
2637       (build2 (MEM_REF, char_type_node, @0,
2638                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2639                type); }))
2641 /* If arg0 is derived from the address of an object or function, we may
2642    be able to fold this expression using the object or function's
2643    alignment.  */
2644 (simplify
2645  (bit_and (convert? @0) INTEGER_CST@1)
2646  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2647       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2648   (with
2649    {
2650      unsigned int align;
2651      unsigned HOST_WIDE_INT bitpos;
2652      get_pointer_alignment_1 (@0, &align, &bitpos);
2653    }
2654    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2655     { wide_int_to_tree (type, (wi::to_wide (@1)
2656                                & (bitpos / BITS_PER_UNIT))); }))))
2658 (match min_value
2659  INTEGER_CST
2660  (if (INTEGRAL_TYPE_P (type)
2661       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2663 (match max_value
2664  INTEGER_CST
2665  (if (INTEGRAL_TYPE_P (type)
2666       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2668 /* x >  y  &&  x != XXX_MIN  -->  x > y
2669    x >  y  &&  x == XXX_MIN  -->  false . */
2670 (for eqne (eq ne)
2671  (simplify
2672   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2673    (switch
2674     (if (eqne == EQ_EXPR)
2675      { constant_boolean_node (false, type); })
2676     (if (eqne == NE_EXPR)
2677      @2)
2678     )))
2680 /* x <  y  &&  x != XXX_MAX  -->  x < y
2681    x <  y  &&  x == XXX_MAX  -->  false.  */
2682 (for eqne (eq ne)
2683  (simplify
2684   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2685    (switch
2686     (if (eqne == EQ_EXPR)
2687      { constant_boolean_node (false, type); })
2688     (if (eqne == NE_EXPR)
2689      @2)
2690     )))
2692 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2693 (simplify
2694  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2695   @2)
2697 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2698 (simplify
2699  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2700   @2)
2702 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2703 (simplify
2704  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2705   @2)
2707 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2708 (simplify
2709  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2710   { constant_boolean_node (true, type); })
2712 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2713 (simplify
2714  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2715   @2)
2717 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2718 (simplify
2719  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2720   @2)
2722 /* x >=  y  ||  x != XXX_MAX   -->  true
2723    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2724 (for eqne (eq ne)
2725  (simplify
2726   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2727    (switch
2728     (if (eqne == EQ_EXPR)
2729      @2)
2730     (if (eqne == NE_EXPR)
2731      { constant_boolean_node (true, type); }))))
2733 /* y == XXX_MIN || x < y --> x <= y - 1 */
2734 (simplify
2735  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2736   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2737        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2738   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2740 /* y != XXX_MIN && x >= y --> x > y - 1 */
2741 (simplify
2742  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2743   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2744        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2745   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2747 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2748    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2750 (for code1 (eq ne)
2751  (for code2 (eq ne lt gt le ge)
2752   (simplify
2753    (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2754     (with
2755      {
2756       int cmp = tree_int_cst_compare (@1, @2);
2757       bool val;
2758       switch (code2)
2759          {
2760         case EQ_EXPR: val = (cmp == 0); break;
2761         case NE_EXPR: val = (cmp != 0); break;
2762         case LT_EXPR: val = (cmp < 0); break;
2763         case GT_EXPR: val = (cmp > 0); break;
2764         case LE_EXPR: val = (cmp <= 0); break;
2765         case GE_EXPR: val = (cmp >= 0); break;
2766         default: gcc_unreachable ();
2767         }
2768      }
2769      (switch
2770       (if (code1 == EQ_EXPR && val) @3)
2771       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2772       (if (code1 == NE_EXPR && !val) @4))))))
2774 /* Convert (X OP1 CST1) && (X OP2 CST2).  */
2776 (for code1 (lt le gt ge)
2777  (for code2 (lt le gt ge)
2778   (simplify
2779   (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2780    (with
2781     {
2782      int cmp = tree_int_cst_compare (@1, @2);
2783     }
2784     (switch
2785      /* Choose the more restrictive of two < or <= comparisons.  */
2786      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2787           && (code2 == LT_EXPR || code2 == LE_EXPR))
2788       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2789        @3
2790        @4))
2791      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2792      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2793           && (code2 == GT_EXPR || code2 == GE_EXPR))
2794       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2795        @3
2796        @4))
2797      /* Check for singleton ranges.  */
2798      (if (cmp == 0
2799           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2800             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2801       (eq @0 @1))
2802      /* Check for disjoint ranges.  */
2803      (if (cmp <= 0
2804           && (code1 == LT_EXPR || code1 == LE_EXPR)
2805           && (code2 == GT_EXPR || code2 == GE_EXPR))
2806       { constant_boolean_node (false, type); })
2807      (if (cmp >= 0
2808           && (code1 == GT_EXPR || code1 == GE_EXPR)
2809           && (code2 == LT_EXPR || code2 == LE_EXPR))
2810       { constant_boolean_node (false, type); })
2811      )))))
2813 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2814    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2816 (for code1 (eq ne)
2817  (for code2 (eq ne lt gt le ge)
2818   (simplify
2819    (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2820     (with
2821      {
2822       int cmp = tree_int_cst_compare (@1, @2);
2823       bool val;
2824       switch (code2)
2825         {
2826         case EQ_EXPR: val = (cmp == 0); break;
2827         case NE_EXPR: val = (cmp != 0); break;
2828         case LT_EXPR: val = (cmp < 0); break;
2829         case GT_EXPR: val = (cmp > 0); break;
2830         case LE_EXPR: val = (cmp <= 0); break;
2831         case GE_EXPR: val = (cmp >= 0); break;
2832         default: gcc_unreachable ();
2833         }
2834      }
2835      (switch
2836       (if (code1 == EQ_EXPR && val) @4)
2837       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2838       (if (code1 == NE_EXPR && !val) @3))))))
2840 /* Convert (X OP1 CST1) || (X OP2 CST2).  */
2842 (for code1 (lt le gt ge)
2843  (for code2 (lt le gt ge)
2844   (simplify
2845   (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2846    (with
2847     {
2848      int cmp = tree_int_cst_compare (@1, @2);
2849     }
2850     (switch
2851      /* Choose the more restrictive of two < or <= comparisons.  */
2852      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2853           && (code2 == LT_EXPR || code2 == LE_EXPR))
2854       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2855        @4
2856        @3))
2857      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2858      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2859           && (code2 == GT_EXPR || code2 == GE_EXPR))
2860       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2861        @4
2862        @3))
2863      /* Check for singleton ranges.  */
2864      (if (cmp == 0
2865           && ((code1 == LT_EXPR && code2 == GT_EXPR)
2866               || (code1 == GT_EXPR && code2 == LT_EXPR)))
2867       (ne @0 @2))
2868      /* Check for disjoint ranges.  */
2869      (if (cmp >= 0
2870           && (code1 == LT_EXPR || code1 == LE_EXPR)
2871           && (code2 == GT_EXPR || code2 == GE_EXPR))
2872       { constant_boolean_node (true, type); })
2873      (if (cmp <= 0
2874           && (code1 == GT_EXPR || code1 == GE_EXPR)
2875           && (code2 == LT_EXPR || code2 == LE_EXPR))
2876       { constant_boolean_node (true, type); })
2877      )))))
2879 /* We can't reassociate at all for saturating types.  */
2880 (if (!TYPE_SATURATING (type))
2882  /* Contract negates.  */
2883  /* A + (-B) -> A - B */
2884  (simplify
2885   (plus:c @0 (convert? (negate @1)))
2886   /* Apply STRIP_NOPS on the negate.  */
2887   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2888        && !TYPE_OVERFLOW_SANITIZED (type))
2889    (with
2890     {
2891      tree t1 = type;
2892      if (INTEGRAL_TYPE_P (type)
2893          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2894        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2895     }
2896     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2897  /* A - (-B) -> A + B */
2898  (simplify
2899   (minus @0 (convert? (negate @1)))
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 (plus (convert:t1 @0) (convert:t1 @1))))))
2910  /* -(T)(-A) -> (T)A
2911     Sign-extension is ok except for INT_MIN, which thankfully cannot
2912     happen without overflow.  */
2913  (simplify
2914   (negate (convert (negate @1)))
2915   (if (INTEGRAL_TYPE_P (type)
2916        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2917            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2918                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2919        && !TYPE_OVERFLOW_SANITIZED (type)
2920        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2921    (convert @1)))
2922  (simplify
2923   (negate (convert negate_expr_p@1))
2924   (if (SCALAR_FLOAT_TYPE_P (type)
2925        && ((DECIMAL_FLOAT_TYPE_P (type)
2926             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2927             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2928            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2929    (convert (negate @1))))
2930  (simplify
2931   (negate (nop_convert? (negate @1)))
2932   (if (!TYPE_OVERFLOW_SANITIZED (type)
2933        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2934    (view_convert @1)))
2936  /* We can't reassociate floating-point unless -fassociative-math
2937     or fixed-point plus or minus because of saturation to +-Inf.  */
2938  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2939       && !FIXED_POINT_TYPE_P (type))
2941   /* Match patterns that allow contracting a plus-minus pair
2942      irrespective of overflow issues.  */
2943   /* (A +- B) - A       ->  +- B */
2944   /* (A +- B) -+ B      ->  A */
2945   /* A - (A +- B)       -> -+ B */
2946   /* A +- (B -+ A)      ->  +- B */
2947   (simplify
2948    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2949    (view_convert @1))
2950   (simplify
2951    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2952    (if (!ANY_INTEGRAL_TYPE_P (type)
2953         || TYPE_OVERFLOW_WRAPS (type))
2954    (negate (view_convert @1))
2955    (view_convert (negate @1))))
2956   (simplify
2957    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2958    (view_convert @0))
2959   (simplify
2960    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2961     (if (!ANY_INTEGRAL_TYPE_P (type)
2962          || TYPE_OVERFLOW_WRAPS (type))
2963      (negate (view_convert @1))
2964      (view_convert (negate @1))))
2965   (simplify
2966    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2967    (view_convert @1))
2968   /* (A +- B) + (C - A)   -> C +- B */
2969   /* (A +  B) - (A - C)   -> B + C */
2970   /* More cases are handled with comparisons.  */
2971   (simplify
2972    (plus:c (plus:c @0 @1) (minus @2 @0))
2973    (plus @2 @1))
2974   (simplify
2975    (plus:c (minus @0 @1) (minus @2 @0))
2976    (minus @2 @1))
2977   (simplify
2978    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2979    (if (TYPE_OVERFLOW_UNDEFINED (type)
2980         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2981     (pointer_diff @2 @1)))
2982   (simplify
2983    (minus (plus:c @0 @1) (minus @0 @2))
2984    (plus @1 @2))
2986   /* (A +- CST1) +- CST2 -> A + CST3
2987      Use view_convert because it is safe for vectors and equivalent for
2988      scalars.  */
2989   (for outer_op (plus minus)
2990    (for inner_op (plus minus)
2991         neg_inner_op (minus plus)
2992     (simplify
2993      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2994                CONSTANT_CLASS_P@2)
2995      /* If one of the types wraps, use that one.  */
2996      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2997       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2998          forever if something doesn't simplify into a constant.  */
2999       (if (!CONSTANT_CLASS_P (@0))
3000        (if (outer_op == PLUS_EXPR)
3001         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3002         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3003       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3004            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3005        (if (outer_op == PLUS_EXPR)
3006         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3007         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3008        /* If the constant operation overflows we cannot do the transform
3009           directly as we would introduce undefined overflow, for example
3010           with (a - 1) + INT_MIN.  */
3011        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3012         (with { tree cst = const_binop (outer_op == inner_op
3013                                         ? PLUS_EXPR : MINUS_EXPR,
3014                                         type, @1, @2); }
3015          (if (cst && !TREE_OVERFLOW (cst))
3016           (inner_op @0 { cst; } )
3017           /* X+INT_MAX+1 is X-INT_MIN.  */
3018           (if (INTEGRAL_TYPE_P (type) && cst
3019                && wi::to_wide (cst) == wi::min_value (type))
3020            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3021            /* Last resort, use some unsigned type.  */
3022            (with { tree utype = unsigned_type_for (type); }
3023             (if (utype)
3024              (view_convert (inner_op
3025                             (view_convert:utype @0)
3026                             (view_convert:utype
3027                              { drop_tree_overflow (cst); }))))))))))))))
3029   /* (CST1 - A) +- CST2 -> CST3 - A  */
3030   (for outer_op (plus minus)
3031    (simplify
3032     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3033     /* If one of the types wraps, use that one.  */
3034     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3035      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3036         forever if something doesn't simplify into a constant.  */
3037      (if (!CONSTANT_CLASS_P (@0))
3038       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3039      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3040           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3041       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3042       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3043        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3044         (if (cst && !TREE_OVERFLOW (cst))
3045          (minus { cst; } @0))))))))
3047   /* CST1 - (CST2 - A) -> CST3 + A
3048      Use view_convert because it is safe for vectors and equivalent for
3049      scalars.  */
3050   (simplify
3051    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3052    /* If one of the types wraps, use that one.  */
3053    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3054     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3055       forever if something doesn't simplify into a constant.  */
3056     (if (!CONSTANT_CLASS_P (@0))
3057      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3058     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3059          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3060      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3061      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3062       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3063        (if (cst && !TREE_OVERFLOW (cst))
3064         (plus { cst; } @0)))))))
3066 /* ((T)(A)) + CST -> (T)(A + CST)  */
3067 #if GIMPLE
3068   (simplify
3069    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3070     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3071          && TREE_CODE (type) == INTEGER_TYPE
3072          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3073          && int_fits_type_p (@1, TREE_TYPE (@0)))
3074      /* Perform binary operation inside the cast if the constant fits
3075         and (A + CST)'s range does not overflow.  */
3076      (with
3077       {
3078         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3079                           max_ovf = wi::OVF_OVERFLOW;
3080         tree inner_type = TREE_TYPE (@0);
3082         wide_int w1
3083           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3084                             TYPE_SIGN (inner_type));
3086         value_range vr;
3087         if (get_global_range_query ()->range_of_expr (vr, @0)
3088             && !vr.varying_p () && !vr.undefined_p ())
3089           {
3090             wide_int wmin0 = vr.lower_bound ();
3091             wide_int wmax0 = vr.upper_bound ();
3092             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3093             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3094           }
3095       }
3096      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3097       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3098      )))
3099 #endif
3101 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3102 #if GIMPLE
3103   (for op (plus minus)
3104    (simplify
3105     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3106      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3107           && TREE_CODE (type) == INTEGER_TYPE
3108           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3109           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3110           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3111           && TYPE_OVERFLOW_WRAPS (type))
3112        (plus (convert @0) (op @2 (convert @1))))))
3113 #endif
3115 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3116    to a simple value.  */
3117   (for op (plus minus)
3118    (simplify
3119     (op (convert @0) (convert @1))
3120      (if (INTEGRAL_TYPE_P (type)
3121           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3122           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3123           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3124           && !TYPE_OVERFLOW_TRAPS (type)
3125           && !TYPE_OVERFLOW_SANITIZED (type))
3126       (convert (op! @0 @1)))))
3128   /* ~A + A -> -1 */
3129   (simplify
3130    (plus:c (convert? (bit_not @0)) (convert? @0))
3131    (if (!TYPE_OVERFLOW_TRAPS (type))
3132     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3134   /* ~A + 1 -> -A */
3135   (simplify
3136    (plus (convert? (bit_not @0)) integer_each_onep)
3137    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3138     (negate (convert @0))))
3140   /* -A - 1 -> ~A */
3141   (simplify
3142    (minus (convert? (negate @0)) integer_each_onep)
3143    (if (!TYPE_OVERFLOW_TRAPS (type)
3144         && TREE_CODE (type) != COMPLEX_TYPE
3145         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3146     (bit_not (convert @0))))
3148   /* -1 - A -> ~A */
3149   (simplify
3150    (minus integer_all_onesp @0)
3151    (if (TREE_CODE (type) != COMPLEX_TYPE)
3152     (bit_not @0)))
3154   /* (T)(P + A) - (T)P -> (T) A */
3155   (simplify
3156    (minus (convert (plus:c @@0 @1))
3157     (convert? @0))
3158    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3159         /* For integer types, if A has a smaller type
3160            than T the result depends on the possible
3161            overflow in P + A.
3162            E.g. T=size_t, A=(unsigned)429497295, P>0.
3163            However, if an overflow in P + A would cause
3164            undefined behavior, we can assume that there
3165            is no overflow.  */
3166         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3167             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3168     (convert @1)))
3169   (simplify
3170    (minus (convert (pointer_plus @@0 @1))
3171     (convert @0))
3172    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3173         /* For pointer types, if the conversion of A to the
3174            final type requires a sign- or zero-extension,
3175            then we have to punt - it is not defined which
3176            one is correct.  */
3177         || (POINTER_TYPE_P (TREE_TYPE (@0))
3178             && TREE_CODE (@1) == INTEGER_CST
3179             && tree_int_cst_sign_bit (@1) == 0))
3180     (convert @1)))
3181    (simplify
3182     (pointer_diff (pointer_plus @@0 @1) @0)
3183     /* The second argument of pointer_plus must be interpreted as signed, and
3184        thus sign-extended if necessary.  */
3185     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3186      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3187         second arg is unsigned even when we need to consider it as signed,
3188         we don't want to diagnose overflow here.  */
3189      (convert (view_convert:stype @1))))
3191   /* (T)P - (T)(P + A) -> -(T) A */
3192   (simplify
3193    (minus (convert? @0)
3194     (convert (plus:c @@0 @1)))
3195    (if (INTEGRAL_TYPE_P (type)
3196         && TYPE_OVERFLOW_UNDEFINED (type)
3197         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3198     (with { tree utype = unsigned_type_for (type); }
3199      (convert (negate (convert:utype @1))))
3200     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3201          /* For integer types, if A has a smaller type
3202             than T the result depends on the possible
3203             overflow in P + A.
3204             E.g. T=size_t, A=(unsigned)429497295, P>0.
3205             However, if an overflow in P + A would cause
3206             undefined behavior, we can assume that there
3207             is no overflow.  */
3208          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3209              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3210      (negate (convert @1)))))
3211   (simplify
3212    (minus (convert @0)
3213     (convert (pointer_plus @@0 @1)))
3214    (if (INTEGRAL_TYPE_P (type)
3215         && TYPE_OVERFLOW_UNDEFINED (type)
3216         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3217     (with { tree utype = unsigned_type_for (type); }
3218      (convert (negate (convert:utype @1))))
3219     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3220          /* For pointer types, if the conversion of A to the
3221             final type requires a sign- or zero-extension,
3222             then we have to punt - it is not defined which
3223             one is correct.  */
3224          || (POINTER_TYPE_P (TREE_TYPE (@0))
3225              && TREE_CODE (@1) == INTEGER_CST
3226              && tree_int_cst_sign_bit (@1) == 0))
3227      (negate (convert @1)))))
3228    (simplify
3229     (pointer_diff @0 (pointer_plus @@0 @1))
3230     /* The second argument of pointer_plus must be interpreted as signed, and
3231        thus sign-extended if necessary.  */
3232     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3233      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3234         second arg is unsigned even when we need to consider it as signed,
3235         we don't want to diagnose overflow here.  */
3236      (negate (convert (view_convert:stype @1)))))
3238   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3239   (simplify
3240    (minus (convert (plus:c @@0 @1))
3241     (convert (plus:c @0 @2)))
3242    (if (INTEGRAL_TYPE_P (type)
3243         && TYPE_OVERFLOW_UNDEFINED (type)
3244         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3245         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3246     (with { tree utype = unsigned_type_for (type); }
3247      (convert (minus (convert:utype @1) (convert:utype @2))))
3248     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3249           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3250          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3251              /* For integer types, if A has a smaller type
3252                 than T the result depends on the possible
3253                 overflow in P + A.
3254                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3255                 However, if an overflow in P + A would cause
3256                 undefined behavior, we can assume that there
3257                 is no overflow.  */
3258              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3259                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3260                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3261                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3262      (minus (convert @1) (convert @2)))))
3263   (simplify
3264    (minus (convert (pointer_plus @@0 @1))
3265     (convert (pointer_plus @0 @2)))
3266    (if (INTEGRAL_TYPE_P (type)
3267         && TYPE_OVERFLOW_UNDEFINED (type)
3268         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3269     (with { tree utype = unsigned_type_for (type); }
3270      (convert (minus (convert:utype @1) (convert:utype @2))))
3271     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3272          /* For pointer types, if the conversion of A to the
3273             final type requires a sign- or zero-extension,
3274             then we have to punt - it is not defined which
3275             one is correct.  */
3276          || (POINTER_TYPE_P (TREE_TYPE (@0))
3277              && TREE_CODE (@1) == INTEGER_CST
3278              && tree_int_cst_sign_bit (@1) == 0
3279              && TREE_CODE (@2) == INTEGER_CST
3280              && tree_int_cst_sign_bit (@2) == 0))
3281      (minus (convert @1) (convert @2)))))
3282    (simplify
3283     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3284      (pointer_diff @0 @1))
3285    (simplify
3286     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3287     /* The second argument of pointer_plus must be interpreted as signed, and
3288        thus sign-extended if necessary.  */
3289     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3290      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3291         second arg is unsigned even when we need to consider it as signed,
3292         we don't want to diagnose overflow here.  */
3293      (minus (convert (view_convert:stype @1))
3294             (convert (view_convert:stype @2)))))))
3296 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3297     Modeled after fold_plusminus_mult_expr.  */
3298 (if (!TYPE_SATURATING (type)
3299      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3300  (for plusminus (plus minus)
3301   (simplify
3302    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3303    (if (!ANY_INTEGRAL_TYPE_P (type)
3304         || TYPE_OVERFLOW_WRAPS (type)
3305         || (INTEGRAL_TYPE_P (type)
3306             && tree_expr_nonzero_p (@0)
3307             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3308     (if (single_use (@3) || single_use (@4))
3309      /* If @1 +- @2 is constant require a hard single-use on either
3310         original operand (but not on both).  */
3311      (mult (plusminus @1 @2) @0)
3312      (mult! (plusminus @1 @2) @0)
3313   )))
3314   /* We cannot generate constant 1 for fract.  */
3315   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3316    (simplify
3317     (plusminus @0 (mult:c@3 @0 @2))
3318     (if ((!ANY_INTEGRAL_TYPE_P (type)
3319           || TYPE_OVERFLOW_WRAPS (type)
3320           /* For @0 + @0*@2 this transformation would introduce UB
3321              (where there was none before) for @0 in [-1,0] and @2 max.
3322              For @0 - @0*@2 this transformation would introduce UB
3323              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3324           || (INTEGRAL_TYPE_P (type)
3325               && ((tree_expr_nonzero_p (@0)
3326                    && expr_not_equal_to (@0,
3327                                 wi::minus_one (TYPE_PRECISION (type))))
3328                   || (plusminus == PLUS_EXPR
3329                       ? expr_not_equal_to (@2,
3330                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3331                       /* Let's ignore the @0 -1 and @2 min case.  */
3332                       : (expr_not_equal_to (@2,
3333                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3334                          && expr_not_equal_to (@2,
3335                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3336                                 + 1))))))
3337          && single_use (@3))
3338      (mult (plusminus { build_one_cst (type); } @2) @0)))
3339    (simplify
3340     (plusminus (mult:c@3 @0 @2) @0)
3341     (if ((!ANY_INTEGRAL_TYPE_P (type)
3342           || TYPE_OVERFLOW_WRAPS (type)
3343           /* For @0*@2 + @0 this transformation would introduce UB
3344              (where there was none before) for @0 in [-1,0] and @2 max.
3345              For @0*@2 - @0 this transformation would introduce UB
3346              for @0 0 and @2 min.  */
3347           || (INTEGRAL_TYPE_P (type)
3348               && ((tree_expr_nonzero_p (@0)
3349                    && (plusminus == MINUS_EXPR
3350                        || expr_not_equal_to (@0,
3351                                 wi::minus_one (TYPE_PRECISION (type)))))
3352                   || expr_not_equal_to (@2,
3353                         (plusminus == PLUS_EXPR
3354                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3355                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3356          && single_use (@3))
3357      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3359 #if GIMPLE
3360 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3361    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3362 (simplify
3363  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3364   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3365        && tree_fits_uhwi_p (@1)
3366        && tree_to_uhwi (@1) < element_precision (type)
3367        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3368            || optab_handler (smul_optab,
3369                              TYPE_MODE (type)) != CODE_FOR_nothing))
3370    (with { tree t = type;
3371            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3372            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3373                                              element_precision (type));
3374            w += 1;
3375            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3376                                         : t, w);
3377            cst = build_uniform_cst (t, cst); }
3378     (convert (mult (convert:t @0) { cst; })))))
3379 (simplify
3380  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3381   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3382        && tree_fits_uhwi_p (@1)
3383        && tree_to_uhwi (@1) < element_precision (type)
3384        && tree_fits_uhwi_p (@2)
3385        && tree_to_uhwi (@2) < element_precision (type)
3386        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3387            || optab_handler (smul_optab,
3388                              TYPE_MODE (type)) != CODE_FOR_nothing))
3389    (with { tree t = type;
3390            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3391            unsigned int prec = element_precision (type);
3392            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3393            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3394            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3395                                         : t, w);
3396            cst = build_uniform_cst (t, cst); }
3397     (convert (mult (convert:t @0) { cst; })))))
3398 #endif
3400 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3401    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3402    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3403 (for op (bit_ior bit_xor)
3404  (simplify
3405   (op (mult:s@0 @1 INTEGER_CST@2)
3406       (mult:s@3 @1 INTEGER_CST@4))
3407   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3408        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3409    (mult @1
3410          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3411  (simplify
3412   (op:c (mult:s@0 @1 INTEGER_CST@2)
3413         (lshift:s@3 @1 INTEGER_CST@4))
3414   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3415        && tree_int_cst_sgn (@4) > 0
3416        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3417    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3418            wide_int c = wi::add (wi::to_wide (@2),
3419                                  wi::lshift (wone, wi::to_wide (@4))); }
3420     (mult @1 { wide_int_to_tree (type, c); }))))
3421  (simplify
3422   (op:c (mult:s@0 @1 INTEGER_CST@2)
3423         @1)
3424   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3425        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3426    (mult @1
3427          { wide_int_to_tree (type,
3428                              wi::add (wi::to_wide (@2), 1)); })))
3429  (simplify
3430   (op (lshift:s@0 @1 INTEGER_CST@2)
3431       (lshift:s@3 @1 INTEGER_CST@4))
3432   (if (INTEGRAL_TYPE_P (type)
3433        && tree_int_cst_sgn (@2) > 0
3434        && tree_int_cst_sgn (@4) > 0
3435        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3436    (with { tree t = type;
3437            if (!TYPE_OVERFLOW_WRAPS (t))
3438              t = unsigned_type_for (t);
3439            wide_int wone = wi::one (TYPE_PRECISION (t));
3440            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3441                                  wi::lshift (wone, wi::to_wide (@4))); }
3442     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3443  (simplify
3444   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3445         @1)
3446   (if (INTEGRAL_TYPE_P (type)
3447        && tree_int_cst_sgn (@2) > 0
3448        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3449    (with { tree t = type;
3450            if (!TYPE_OVERFLOW_WRAPS (t))
3451              t = unsigned_type_for (t);
3452            wide_int wone = wi::one (TYPE_PRECISION (t));
3453            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3454     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3456 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3458 (for minmax (min max)
3459  (simplify
3460   (minmax @0 @0)
3461   @0))
3462 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3463 (for minmax (FMIN_ALL FMAX_ALL)
3464  (simplify
3465   (minmax @0 @0)
3466   (if (!tree_expr_maybe_signaling_nan_p (@0))
3467     @0)))
3468 /* min(max(x,y),y) -> y.  */
3469 (simplify
3470  (min:c (max:c @0 @1) @1)
3471  @1)
3472 /* max(min(x,y),y) -> y.  */
3473 (simplify
3474  (max:c (min:c @0 @1) @1)
3475  @1)
3476 /* max(a,-a) -> abs(a).  */
3477 (simplify
3478  (max:c @0 (negate @0))
3479  (if (TREE_CODE (type) != COMPLEX_TYPE
3480       && (! ANY_INTEGRAL_TYPE_P (type)
3481           || TYPE_OVERFLOW_UNDEFINED (type)))
3482   (abs @0)))
3483 /* min(a,-a) -> -abs(a).  */
3484 (simplify
3485  (min:c @0 (negate @0))
3486  (if (TREE_CODE (type) != COMPLEX_TYPE
3487       && (! ANY_INTEGRAL_TYPE_P (type)
3488           || TYPE_OVERFLOW_UNDEFINED (type)))
3489   (negate (abs @0))))
3490 (simplify
3491  (min @0 @1)
3492  (switch
3493   (if (INTEGRAL_TYPE_P (type)
3494        && TYPE_MIN_VALUE (type)
3495        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3496    @1)
3497   (if (INTEGRAL_TYPE_P (type)
3498        && TYPE_MAX_VALUE (type)
3499        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3500    @0)))
3501 (simplify
3502  (max @0 @1)
3503  (switch
3504   (if (INTEGRAL_TYPE_P (type)
3505        && TYPE_MAX_VALUE (type)
3506        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3507    @1)
3508   (if (INTEGRAL_TYPE_P (type)
3509        && TYPE_MIN_VALUE (type)
3510        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3511    @0)))
3513 /* max (a, a + CST) -> a + CST where CST is positive.  */
3514 /* max (a, a + CST) -> a where CST is negative.  */
3515 (simplify
3516  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3517   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3518    (if (tree_int_cst_sgn (@1) > 0)
3519     @2
3520     @0)))
3522 /* min (a, a + CST) -> a where CST is positive.  */
3523 /* min (a, a + CST) -> a + CST where CST is negative. */
3524 (simplify
3525  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3526   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3527    (if (tree_int_cst_sgn (@1) > 0)
3528     @0
3529     @2)))
3531 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3532    the addresses are known to be less, equal or greater.  */
3533 (for minmax (min max)
3534      cmp (lt gt)
3535  (simplify
3536   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3537   (with
3538    {
3539      poly_int64 off0, off1;
3540      tree base0, base1;
3541      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3542                                   off0, off1, GENERIC);
3543    }
3544    (if (equal == 1)
3545     (if (minmax == MIN_EXPR)
3546      (if (known_le (off0, off1))
3547       @2
3548       (if (known_gt (off0, off1))
3549        @3))
3550      (if (known_ge (off0, off1))
3551       @2
3552       (if (known_lt (off0, off1))
3553        @3)))))))
3555 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3556    and the outer convert demotes the expression back to x's type.  */
3557 (for minmax (min max)
3558  (simplify
3559   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3560   (if (INTEGRAL_TYPE_P (type)
3561        && types_match (@1, type) && int_fits_type_p (@2, type)
3562        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3563        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3564    (minmax @1 (convert @2)))))
3566 (for minmax (FMIN_ALL FMAX_ALL)
3567  /* If either argument is NaN and other one is not sNaN, return the other
3568     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3569  (simplify
3570   (minmax:c @0 REAL_CST@1)
3571    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3572        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3573        && !tree_expr_maybe_signaling_nan_p (@0))
3574    @0)))
3575 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3576    functions to return the numeric arg if the other one is NaN.
3577    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3578    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3579    worry about it either.  */
3580 (if (flag_finite_math_only)
3581  (simplify
3582   (FMIN_ALL @0 @1)
3583   (min @0 @1))
3584  (simplify
3585   (FMAX_ALL @0 @1)
3586   (max @0 @1)))
3587 /* min (-A, -B) -> -max (A, B)  */
3588 (for minmax (min max FMIN_ALL FMAX_ALL)
3589      maxmin (max min FMAX_ALL FMIN_ALL)
3590  (simplify
3591   (minmax (negate:s@2 @0) (negate:s@3 @1))
3592   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3593        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3594            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3595    (negate (maxmin @0 @1)))))
3596 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3597    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3598 (for minmax (min max)
3599  maxmin (max min)
3600  (simplify
3601   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3602   (bit_not (maxmin @0 @1))))
3604 /* MIN (X, Y) == X -> X <= Y  */
3605 (for minmax (min min max max)
3606      cmp    (eq  ne  eq  ne )
3607      out    (le  gt  ge  lt )
3608  (simplify
3609   (cmp:c (minmax:c @0 @1) @0)
3610   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3611    (out @0 @1))))
3612 /* MIN (X, 5) == 0 -> X == 0
3613    MIN (X, 5) == 7 -> false  */
3614 (for cmp (eq ne)
3615  (simplify
3616   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3617   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3618                  TYPE_SIGN (TREE_TYPE (@0))))
3619    { constant_boolean_node (cmp == NE_EXPR, type); }
3620    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3621                   TYPE_SIGN (TREE_TYPE (@0))))
3622     (cmp @0 @2)))))
3623 (for cmp (eq ne)
3624  (simplify
3625   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3626   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3627                  TYPE_SIGN (TREE_TYPE (@0))))
3628    { constant_boolean_node (cmp == NE_EXPR, type); }
3629    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3630                   TYPE_SIGN (TREE_TYPE (@0))))
3631     (cmp @0 @2)))))
3632 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
3633 (for minmax (min     min     max     max     min     min     max     max    )
3634      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
3635      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3636  (simplify
3637   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3638   (comb (cmp @0 @2) (cmp @1 @2))))
3640 /* X <= MAX(X, Y) -> true
3641    X > MAX(X, Y) -> false 
3642    X >= MIN(X, Y) -> true
3643    X < MIN(X, Y) -> false */
3644 (for minmax (min     min     max     max     )
3645      cmp    (ge      lt      le      gt      )
3646  (simplify
3647   (cmp @0 (minmax:c @0 @1))
3648   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3650 /* Undo fancy ways of writing max/min or other ?: expressions, like
3651    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
3652    People normally use ?: and that is what we actually try to optimize.  */
3653 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
3654 (simplify
3655  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3656  (if (INTEGRAL_TYPE_P (type)
3657       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3658   (cond (convert:boolean_type_node @2) @1 @0)))
3659 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
3660 (simplify
3661  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3662  (if (INTEGRAL_TYPE_P (type)
3663       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3664   (cond (convert:boolean_type_node @2) @1 @0)))
3665 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
3666 (simplify
3667  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3668  (if (INTEGRAL_TYPE_P (type)
3669       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3670   (cond (convert:boolean_type_node @2) @1 @0)))
3672 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
3673 (simplify
3674   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3675   (max @2 @1))
3677 /* ((x & 0x1) == 0) ? y : z <op> y -> (-(typeof(y))(x & 0x1) & z) <op> y */
3678 (for op (bit_xor bit_ior)
3679  (simplify
3680   (cond (eq zero_one_valued_p@0
3681             integer_zerop)
3682         @1
3683         (op:c @2 @1))
3684   (if (INTEGRAL_TYPE_P (type)
3685        && TYPE_PRECISION (type) > 1
3686        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3687        (op (bit_and (negate (convert:type @0)) @2) @1))))
3689 /* ((x & 0x1) == 0) ? z <op> y : y -> (-(typeof(y))(x & 0x1) & z) <op> y */
3690 (for op (bit_xor bit_ior)
3691  (simplify
3692   (cond (ne zero_one_valued_p@0
3693             integer_zerop)
3694        (op:c @2 @1)
3695         @1)
3696   (if (INTEGRAL_TYPE_P (type)
3697        && TYPE_PRECISION (type) > 1
3698        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3699        (op (bit_and (negate (convert:type @0)) @2) @1))))
3701 /* Simplifications of shift and rotates.  */
3703 (for rotate (lrotate rrotate)
3704  (simplify
3705   (rotate integer_all_onesp@0 @1)
3706   @0))
3708 /* Optimize -1 >> x for arithmetic right shifts.  */
3709 (simplify
3710  (rshift integer_all_onesp@0 @1)
3711  (if (!TYPE_UNSIGNED (type))
3712   @0))
3714 /* Optimize (x >> c) << c into x & (-1<<c).  */
3715 (simplify
3716  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3717  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3718   /* It doesn't matter if the right shift is arithmetic or logical.  */
3719   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3721 (simplify
3722  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3723  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3724       /* Allow intermediate conversion to integral type with whatever sign, as
3725          long as the low TYPE_PRECISION (type)
3726          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3727       && INTEGRAL_TYPE_P (type)
3728       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3729       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3730       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3731       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3732           || wi::geu_p (wi::to_wide (@1),
3733                         TYPE_PRECISION (type)
3734                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3735   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3737 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3738    types.  */
3739 (simplify
3740  (rshift (lshift @0 INTEGER_CST@1) @1)
3741  (if (TYPE_UNSIGNED (type)
3742       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3743   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3745 /* Optimize x >> x into 0 */
3746 (simplify
3747  (rshift @0 @0)
3748   { build_zero_cst (type); })
3750 (for shiftrotate (lrotate rrotate lshift rshift)
3751  (simplify
3752   (shiftrotate @0 integer_zerop)
3753   (non_lvalue @0))
3754  (simplify
3755   (shiftrotate integer_zerop@0 @1)
3756   @0)
3757  /* Prefer vector1 << scalar to vector1 << vector2
3758     if vector2 is uniform.  */
3759  (for vec (VECTOR_CST CONSTRUCTOR)
3760   (simplify
3761    (shiftrotate @0 vec@1)
3762    (with { tree tem = uniform_vector_p (@1); }
3763     (if (tem)
3764      (shiftrotate @0 { tem; }))))))
3766 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3767    Y is 0.  Similarly for X >> Y.  */
3768 #if GIMPLE
3769 (for shift (lshift rshift)
3770  (simplify
3771   (shift @0 SSA_NAME@1)
3772    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3773     (with {
3774       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3775       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3776      }
3777      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3778       @0)))))
3779 #endif
3781 /* Rewrite an LROTATE_EXPR by a constant into an
3782    RROTATE_EXPR by a new constant.  */
3783 (simplify
3784  (lrotate @0 INTEGER_CST@1)
3785  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3786                             build_int_cst (TREE_TYPE (@1),
3787                                            element_precision (type)), @1); }))
3789 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3790 (for op (lrotate rrotate rshift lshift)
3791  (simplify
3792   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3793   (with { unsigned int prec = element_precision (type); }
3794    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3795         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3796         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3797         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3798     (with { unsigned int low = (tree_to_uhwi (@1)
3799                                 + tree_to_uhwi (@2)); }
3800      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3801         being well defined.  */
3802      (if (low >= prec)
3803       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3804        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3805        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3806         { build_zero_cst (type); }
3807         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3808       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3811 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3812 (simplify
3813  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3814   (if ((wi::to_wide (@1) & 1) != 0)
3815    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3816    { build_zero_cst (type); }))
3818 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3819    either to false if D is smaller (unsigned comparison) than C, or to
3820    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
3821 (for cmp (ne eq)
3822      icmp (eq ne)
3823  (simplify
3824   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3825    (with { int c1 = wi::clz (wi::to_wide (@1));
3826            int c2 = wi::clz (wi::to_wide (@2)); }
3827     (if (c1 < c2)
3828      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3829      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3830  (simplify
3831   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3832    (if (tree_int_cst_sgn (@1) > 0)
3833     (with { int c1 = wi::clz (wi::to_wide (@1));
3834             int c2 = wi::clz (wi::to_wide (@2)); }
3835      (if (c1 > c2)
3836       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3837       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3839 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3840    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3841    if CST2 != 0.  */
3842 (for cmp (ne eq)
3843  (simplify
3844   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3845   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3846    (if (cand < 0
3847         || (!integer_zerop (@2)
3848             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3849     { constant_boolean_node (cmp == NE_EXPR, type); }
3850     (if (!integer_zerop (@2)
3851          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3852      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3854 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3855         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
3856 (for cmp (ne eq)
3857  (simplify
3858   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3859   (if (tree_fits_shwi_p (@1)
3860        && tree_to_shwi (@1) > 0
3861        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3862     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
3863       { constant_boolean_node (cmp == NE_EXPR, type); }
3864       (with { wide_int c1 = wi::to_wide (@1);
3865               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3866               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3867         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3868              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
3869  (simplify
3870   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3871   (if (tree_fits_shwi_p (@1)
3872        && tree_to_shwi (@1) > 0
3873        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3874     (with { tree t0 = TREE_TYPE (@0);
3875             unsigned int prec = TYPE_PRECISION (t0);
3876             wide_int c1 = wi::to_wide (@1);
3877             wide_int c2 = wi::to_wide (@2);
3878             wide_int c3 = wi::to_wide (@3);
3879             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
3880       (if ((c2 & c3) != c3)
3881         { constant_boolean_node (cmp == NE_EXPR, type); }
3882         (if (TYPE_UNSIGNED (t0))
3883           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
3884             { constant_boolean_node (cmp == NE_EXPR, type); }
3885             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3886                  { wide_int_to_tree (t0, c3 << c1); }))
3887           (with { wide_int smask = wi::arshift (sb, c1); }
3888             (switch
3889               (if ((c2 & smask) == 0)
3890                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3891                      { wide_int_to_tree (t0, c3 << c1); }))
3892               (if ((c3 & smask) == 0)
3893                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3894                      { wide_int_to_tree (t0, c3 << c1); }))
3895               (if ((c2 & smask) != (c3 & smask))
3896                 { constant_boolean_node (cmp == NE_EXPR, type); })
3897               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3898                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
3900 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3901         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3902    if the new mask might be further optimized.  */
3903 (for shift (lshift rshift)
3904  (simplify
3905   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3906            INTEGER_CST@2)
3907    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3908         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3909         && tree_fits_uhwi_p (@1)
3910         && tree_to_uhwi (@1) > 0
3911         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3912     (with
3913      {
3914        unsigned int shiftc = tree_to_uhwi (@1);
3915        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3916        unsigned HOST_WIDE_INT newmask, zerobits = 0;
3917        tree shift_type = TREE_TYPE (@3);
3918        unsigned int prec;
3920        if (shift == LSHIFT_EXPR)
3921          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3922        else if (shift == RSHIFT_EXPR
3923                 && type_has_mode_precision_p (shift_type))
3924          {
3925            prec = TYPE_PRECISION (TREE_TYPE (@3));
3926            tree arg00 = @0;
3927            /* See if more bits can be proven as zero because of
3928               zero extension.  */
3929            if (@3 != @0
3930                && TYPE_UNSIGNED (TREE_TYPE (@0)))
3931              {
3932                tree inner_type = TREE_TYPE (@0);
3933                if (type_has_mode_precision_p (inner_type)
3934                    && TYPE_PRECISION (inner_type) < prec)
3935                  {
3936                    prec = TYPE_PRECISION (inner_type);
3937                    /* See if we can shorten the right shift.  */
3938                    if (shiftc < prec)
3939                      shift_type = inner_type;
3940                    /* Otherwise X >> C1 is all zeros, so we'll optimize
3941                       it into (X, 0) later on by making sure zerobits
3942                       is all ones.  */
3943                  }
3944              }
3945            zerobits = HOST_WIDE_INT_M1U;
3946            if (shiftc < prec)
3947              {
3948                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3949                zerobits <<= prec - shiftc;
3950              }
3951            /* For arithmetic shift if sign bit could be set, zerobits
3952               can contain actually sign bits, so no transformation is
3953               possible, unless MASK masks them all away.  In that
3954               case the shift needs to be converted into logical shift.  */
3955            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3956                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3957              {
3958                if ((mask & zerobits) == 0)
3959                  shift_type = unsigned_type_for (TREE_TYPE (@3));
3960                else
3961                  zerobits = 0;
3962              }
3963          }
3964      }
3965      /* ((X << 16) & 0xff00) is (X, 0).  */
3966      (if ((mask & zerobits) == mask)
3967       { build_int_cst (type, 0); }
3968       (with { newmask = mask | zerobits; }
3969        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3970         (with
3971          {
3972            /* Only do the transformation if NEWMASK is some integer
3973               mode's mask.  */
3974            for (prec = BITS_PER_UNIT;
3975                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3976              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3977                break;
3978          }
3979          (if (prec < HOST_BITS_PER_WIDE_INT
3980               || newmask == HOST_WIDE_INT_M1U)
3981           (with
3982            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3983            (if (!tree_int_cst_equal (newmaskt, @2))
3984             (if (shift_type != TREE_TYPE (@3))
3985              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3986              (bit_and @4 { newmaskt; })))))))))))))
3988 /* ((1 << n) & M) != 0  -> n == log2 (M) */
3989 (for cmp (ne eq)
3990        icmp (eq ne)
3991  (simplify
3992   (cmp
3993    (bit_and
3994     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3995   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3996    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3997                                 wi::exact_log2 (wi::to_wide (@1))); }))))
3999 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4000    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4001 (for shift (lshift rshift)
4002  (for bit_op (bit_and bit_xor bit_ior)
4003   (simplify
4004    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4005    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4006     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4007      (if (mask)
4008       (bit_op (shift (convert @0) @1) { mask; })))))))
4010 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4011 (simplify
4012  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4013   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4014        && (element_precision (TREE_TYPE (@0))
4015            <= element_precision (TREE_TYPE (@1))
4016            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4017    (with
4018     { tree shift_type = TREE_TYPE (@0); }
4019      (convert (rshift (convert:shift_type @1) @2)))))
4021 /* ~(~X >>r Y) -> X >>r Y
4022    ~(~X <<r Y) -> X <<r Y */
4023 (for rotate (lrotate rrotate)
4024  (simplify
4025   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4026    (if ((element_precision (TREE_TYPE (@0))
4027          <= element_precision (TREE_TYPE (@1))
4028          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4029         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4030             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4031     (with
4032      { tree rotate_type = TREE_TYPE (@0); }
4033       (convert (rotate (convert:rotate_type @1) @2))))))
4035 (for cmp (eq ne)
4036  (for rotate (lrotate rrotate)
4037       invrot (rrotate lrotate)
4038   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4039   (simplify
4040    (cmp (rotate @1 @0) (rotate @2 @0))
4041    (cmp @1 @2))
4042   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4043   (simplify
4044    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4045    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4046   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4047   (simplify
4048    (cmp (rotate @0 @1) INTEGER_CST@2)
4049     (if (integer_zerop (@2) || integer_all_onesp (@2))
4050      (cmp @0 @2)))))
4052 /* Narrow a lshift by constant.  */
4053 (simplify
4054  (convert (lshift:s@0 @1 INTEGER_CST@2))
4055  (if (INTEGRAL_TYPE_P (type)
4056       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4057       && !integer_zerop (@2)
4058       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4059   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4060        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4061    (lshift (convert @1) @2)
4062    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4063     { build_zero_cst (type); }))))
4065 /* Simplifications of conversions.  */
4067 /* Basic strip-useless-type-conversions / strip_nops.  */
4068 (for cvt (convert view_convert float fix_trunc)
4069  (simplify
4070   (cvt @0)
4071   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4072        || (GENERIC && type == TREE_TYPE (@0)))
4073    @0)))
4075 /* Contract view-conversions.  */
4076 (simplify
4077   (view_convert (view_convert @0))
4078   (view_convert @0))
4080 /* For integral conversions with the same precision or pointer
4081    conversions use a NOP_EXPR instead.  */
4082 (simplify
4083   (view_convert @0)
4084   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4085        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4086        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4087    (convert @0)))
4089 /* Strip inner integral conversions that do not change precision or size, or
4090    zero-extend while keeping the same size (for bool-to-char).  */
4091 (simplify
4092   (view_convert (convert@0 @1))
4093   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4094        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4095        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4096        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4097            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4098                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4099    (view_convert @1)))
4101 /* Simplify a view-converted empty or single-element constructor.  */
4102 (simplify
4103   (view_convert CONSTRUCTOR@0)
4104   (with
4105    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4106                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4107    (switch
4108     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4109      { build_zero_cst (type); })
4110     (if (CONSTRUCTOR_NELTS (ctor) == 1
4111          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4112          && operand_equal_p (TYPE_SIZE (type),
4113                              TYPE_SIZE (TREE_TYPE
4114                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4115      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4117 /* Re-association barriers around constants and other re-association
4118    barriers can be removed.  */
4119 (simplify
4120  (paren CONSTANT_CLASS_P@0)
4121  @0)
4122 (simplify
4123  (paren (paren@1 @0))
4124  @1)
4126 /* Handle cases of two conversions in a row.  */
4127 (for ocvt (convert float fix_trunc)
4128  (for icvt (convert float)
4129   (simplify
4130    (ocvt (icvt@1 @0))
4131    (with
4132     {
4133       tree inside_type = TREE_TYPE (@0);
4134       tree inter_type = TREE_TYPE (@1);
4135       int inside_int = INTEGRAL_TYPE_P (inside_type);
4136       int inside_ptr = POINTER_TYPE_P (inside_type);
4137       int inside_float = FLOAT_TYPE_P (inside_type);
4138       int inside_vec = VECTOR_TYPE_P (inside_type);
4139       unsigned int inside_prec = TYPE_PRECISION (inside_type);
4140       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4141       int inter_int = INTEGRAL_TYPE_P (inter_type);
4142       int inter_ptr = POINTER_TYPE_P (inter_type);
4143       int inter_float = FLOAT_TYPE_P (inter_type);
4144       int inter_vec = VECTOR_TYPE_P (inter_type);
4145       unsigned int inter_prec = TYPE_PRECISION (inter_type);
4146       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4147       int final_int = INTEGRAL_TYPE_P (type);
4148       int final_ptr = POINTER_TYPE_P (type);
4149       int final_float = FLOAT_TYPE_P (type);
4150       int final_vec = VECTOR_TYPE_P (type);
4151       unsigned int final_prec = TYPE_PRECISION (type);
4152       int final_unsignedp = TYPE_UNSIGNED (type);
4153     }
4154    (switch
4155     /* In addition to the cases of two conversions in a row
4156        handled below, if we are converting something to its own
4157        type via an object of identical or wider precision, neither
4158        conversion is needed.  */
4159     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4160           || (GENERIC
4161               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4162          && (((inter_int || inter_ptr) && final_int)
4163              || (inter_float && final_float))
4164          && inter_prec >= final_prec)
4165      (ocvt @0))
4167     /* Likewise, if the intermediate and initial types are either both
4168        float or both integer, we don't need the middle conversion if the
4169        former is wider than the latter and doesn't change the signedness
4170        (for integers).  Avoid this if the final type is a pointer since
4171        then we sometimes need the middle conversion.  */
4172     (if (((inter_int && inside_int) || (inter_float && inside_float))
4173          && (final_int || final_float)
4174          && inter_prec >= inside_prec
4175          && (inter_float || inter_unsignedp == inside_unsignedp))
4176      (ocvt @0))
4178     /* If we have a sign-extension of a zero-extended value, we can
4179        replace that by a single zero-extension.  Likewise if the
4180        final conversion does not change precision we can drop the
4181        intermediate conversion.  */
4182     (if (inside_int && inter_int && final_int
4183          && ((inside_prec < inter_prec && inter_prec < final_prec
4184               && inside_unsignedp && !inter_unsignedp)
4185              || final_prec == inter_prec))
4186      (ocvt @0))
4188     /* Two conversions in a row are not needed unless:
4189         - some conversion is floating-point (overstrict for now), or
4190         - some conversion is a vector (overstrict for now), or
4191         - the intermediate type is narrower than both initial and
4192           final, or
4193         - the intermediate type and innermost type differ in signedness,
4194           and the outermost type is wider than the intermediate, or
4195         - the initial type is a pointer type and the precisions of the
4196           intermediate and final types differ, or
4197         - the final type is a pointer type and the precisions of the
4198           initial and intermediate types differ.  */
4199     (if (! inside_float && ! inter_float && ! final_float
4200          && ! inside_vec && ! inter_vec && ! final_vec
4201          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4202          && ! (inside_int && inter_int
4203                && inter_unsignedp != inside_unsignedp
4204                && inter_prec < final_prec)
4205          && ((inter_unsignedp && inter_prec > inside_prec)
4206              == (final_unsignedp && final_prec > inter_prec))
4207          && ! (inside_ptr && inter_prec != final_prec)
4208          && ! (final_ptr && inside_prec != inter_prec))
4209      (ocvt @0))
4211     /* A truncation to an unsigned type (a zero-extension) should be
4212        canonicalized as bitwise and of a mask.  */
4213     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4214          && final_int && inter_int && inside_int
4215          && final_prec == inside_prec
4216          && final_prec > inter_prec
4217          && inter_unsignedp)
4218      (convert (bit_and @0 { wide_int_to_tree
4219                               (inside_type,
4220                                wi::mask (inter_prec, false,
4221                                          TYPE_PRECISION (inside_type))); })))
4223     /* If we are converting an integer to a floating-point that can
4224        represent it exactly and back to an integer, we can skip the
4225        floating-point conversion.  */
4226     (if (GIMPLE /* PR66211 */
4227          && inside_int && inter_float && final_int &&
4228          (unsigned) significand_size (TYPE_MODE (inter_type))
4229          >= inside_prec - !inside_unsignedp)
4230      (convert @0)))))))
4232 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4233    float_type.  Only do the transformation if we do not need to preserve
4234    trapping behaviour, so require !flag_trapping_math. */
4235 #if GIMPLE
4236 (simplify
4237    (float (fix_trunc @0))
4238    (if (!flag_trapping_math
4239         && types_match (type, TREE_TYPE (@0))
4240         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4241                                           OPTIMIZE_FOR_BOTH))
4242       (IFN_TRUNC @0)))
4243 #endif
4245 /* If we have a narrowing conversion to an integral type that is fed by a
4246    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4247    masks off bits outside the final type (and nothing else).  */
4248 (simplify
4249   (convert (bit_and @0 INTEGER_CST@1))
4250   (if (INTEGRAL_TYPE_P (type)
4251        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4252        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4253        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4254                                                     TYPE_PRECISION (type)), 0))
4255    (convert @0)))
4258 /* (X /[ex] A) * A -> X.  */
4259 (simplify
4260   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4261   (convert @0))
4263 /* Simplify (A / B) * B + (A % B) -> A.  */
4264 (for div (trunc_div ceil_div floor_div round_div)
4265      mod (trunc_mod ceil_mod floor_mod round_mod)
4266   (simplify
4267    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4268    @0))
4270 /* x / y * y == x -> x % y == 0.  */
4271 (simplify
4272   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4273   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4274     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4276 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4277 (for op (plus minus)
4278  (simplify
4279   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4280   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4281        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4282    (with
4283      {
4284        wi::overflow_type overflow;
4285        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4286                                TYPE_SIGN (type), &overflow);
4287      }
4288      (if (types_match (type, TREE_TYPE (@2))
4289          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4290       (op @0 { wide_int_to_tree (type, mul); })
4291       (with { tree utype = unsigned_type_for (type); }
4292        (convert (op (convert:utype @0)
4293                     (mult (convert:utype @1) (convert:utype @2))))))))))
4295 /* Canonicalization of binary operations.  */
4297 /* Convert X + -C into X - C.  */
4298 (simplify
4299  (plus @0 REAL_CST@1)
4300  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4301   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4302    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4303     (minus @0 { tem; })))))
4305 /* Convert x+x into x*2.  */
4306 (simplify
4307  (plus @0 @0)
4308  (if (SCALAR_FLOAT_TYPE_P (type))
4309   (mult @0 { build_real (type, dconst2); })
4310   (if (INTEGRAL_TYPE_P (type))
4311    (mult @0 { build_int_cst (type, 2); }))))
4313 /* 0 - X  ->  -X.  */
4314 (simplify
4315  (minus integer_zerop @1)
4316  (negate @1))
4317 (simplify
4318  (pointer_diff integer_zerop @1)
4319  (negate (convert @1)))
4321 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4322    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4323    (-ARG1 + ARG0) reduces to -ARG1.  */
4324 (simplify
4325  (minus real_zerop@0 @1)
4326  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4327   (negate @1)))
4329 /* Transform x * -1 into -x.  */
4330 (simplify
4331  (mult @0 integer_minus_onep)
4332  (negate @0))
4334 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4335    signed overflow for CST != 0 && CST != -1.  */
4336 (simplify
4337  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4338  (if (TREE_CODE (@2) != INTEGER_CST
4339       && single_use (@3)
4340       && !integer_zerop (@1) && !integer_minus_onep (@1))
4341   (mult (mult @0 @2) @1)))
4343 /* True if we can easily extract the real and imaginary parts of a complex
4344    number.  */
4345 (match compositional_complex
4346  (convert? (complex @0 @1)))
4348 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4349 (simplify
4350  (complex (realpart @0) (imagpart @0))
4351  @0)
4352 (simplify
4353  (realpart (complex @0 @1))
4354  @0)
4355 (simplify
4356  (imagpart (complex @0 @1))
4357  @1)
4359 /* Sometimes we only care about half of a complex expression.  */
4360 (simplify
4361  (realpart (convert?:s (conj:s @0)))
4362  (convert (realpart @0)))
4363 (simplify
4364  (imagpart (convert?:s (conj:s @0)))
4365  (convert (negate (imagpart @0))))
4366 (for part (realpart imagpart)
4367  (for op (plus minus)
4368   (simplify
4369    (part (convert?:s@2 (op:s @0 @1)))
4370    (convert (op (part @0) (part @1))))))
4371 (simplify
4372  (realpart (convert?:s (CEXPI:s @0)))
4373  (convert (COS @0)))
4374 (simplify
4375  (imagpart (convert?:s (CEXPI:s @0)))
4376  (convert (SIN @0)))
4378 /* conj(conj(x)) -> x  */
4379 (simplify
4380  (conj (convert? (conj @0)))
4381  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4382   (convert @0)))
4384 /* conj({x,y}) -> {x,-y}  */
4385 (simplify
4386  (conj (convert?:s (complex:s @0 @1)))
4387  (with { tree itype = TREE_TYPE (type); }
4388   (complex (convert:itype @0) (negate (convert:itype @1)))))
4390 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4391 (for bswap (BSWAP)
4392  (simplify
4393   (bswap (bswap @0))
4394   @0)
4395  (simplify
4396   (bswap (bit_not (bswap @0)))
4397   (bit_not @0))
4398  (for bitop (bit_xor bit_ior bit_and)
4399   (simplify
4400    (bswap (bitop:c (bswap @0) @1))
4401    (bitop @0 (bswap @1))))
4402  (for cmp (eq ne)
4403   (simplify
4404    (cmp (bswap@2 @0) (bswap @1))
4405    (with { tree ctype = TREE_TYPE (@2); }
4406     (cmp (convert:ctype @0) (convert:ctype @1))))
4407   (simplify
4408    (cmp (bswap @0) INTEGER_CST@1)
4409    (with { tree ctype = TREE_TYPE (@1); }
4410     (cmp (convert:ctype @0) (bswap! @1)))))
4411  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4412  (simplify
4413   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4414            INTEGER_CST@3)
4415    (if (BITS_PER_UNIT == 8
4416         && tree_fits_uhwi_p (@2)
4417         && tree_fits_uhwi_p (@3))
4418     (with
4419      {
4420       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4421       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4422       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4423       unsigned HOST_WIDE_INT lo = bits & 7;
4424       unsigned HOST_WIDE_INT hi = bits - lo;
4425      }
4426      (if (bits < prec
4427           && mask < (256u>>lo)
4428           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4429       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4430        (if (ns == 0)
4431         (bit_and (convert @1) @3)
4432         (with
4433          {
4434           tree utype = unsigned_type_for (TREE_TYPE (@1));
4435           tree nst = build_int_cst (integer_type_node, ns);
4436          }
4437          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4438  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4439  (simplify
4440   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4441    (if (BITS_PER_UNIT == 8
4442         && CHAR_TYPE_SIZE == 8
4443         && tree_fits_uhwi_p (@1))
4444     (with
4445      {
4446       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4447       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4448       /* If the bswap was extended before the original shift, this
4449          byte (shift) has the sign of the extension, not the sign of
4450          the original shift.  */
4451       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4452      }
4453      /* Special case: logical right shift of sign-extended bswap.
4454         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4455      (if (TYPE_PRECISION (type) > prec
4456           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4457           && TYPE_UNSIGNED (type)
4458           && bits < prec && bits + 8 >= prec)
4459       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4460        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4461       (if (bits + 8 == prec)
4462        (if (TYPE_UNSIGNED (st))
4463         (convert (convert:unsigned_char_type_node @0))
4464         (convert (convert:signed_char_type_node @0)))
4465        (if (bits < prec && bits + 8 > prec)
4466         (with 
4467          {
4468           tree nst = build_int_cst (integer_type_node, bits & 7);
4469           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4470                                        : signed_char_type_node;
4471          }
4472          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4473  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4474  (simplify
4475   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4476    (if (BITS_PER_UNIT == 8
4477         && tree_fits_uhwi_p (@1)
4478         && tree_to_uhwi (@1) < 256)
4479     (with
4480      {
4481       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4482       tree utype = unsigned_type_for (TREE_TYPE (@0));
4483       tree nst = build_int_cst (integer_type_node, prec - 8);
4484      }
4485      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4488 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4490 /* Simplify constant conditions.
4491    Only optimize constant conditions when the selected branch
4492    has the same type as the COND_EXPR.  This avoids optimizing
4493    away "c ? x : throw", where the throw has a void type.
4494    Note that we cannot throw away the fold-const.cc variant nor
4495    this one as we depend on doing this transform before possibly
4496    A ? B : B -> B triggers and the fold-const.cc one can optimize
4497    0 ? A : B to B even if A has side-effects.  Something
4498    genmatch cannot handle.  */
4499 (simplify
4500  (cond INTEGER_CST@0 @1 @2)
4501  (if (integer_zerop (@0))
4502   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4503    @2)
4504   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4505    @1)))
4506 (simplify
4507  (vec_cond VECTOR_CST@0 @1 @2)
4508  (if (integer_all_onesp (@0))
4509   @1
4510   (if (integer_zerop (@0))
4511    @2)))
4513 /* Sink unary operations to branches, but only if we do fold both.  */
4514 (for op (negate bit_not abs absu)
4515  (simplify
4516   (op (vec_cond:s @0 @1 @2))
4517   (vec_cond @0 (op! @1) (op! @2))))
4519 /* Sink binary operation to branches, but only if we can fold it.  */
4520 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4521          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4522          trunc_mod ceil_mod floor_mod round_mod min max)
4523 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4524  (simplify
4525   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4526   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4528 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4529  (simplify
4530   (op (vec_cond:s @0 @1 @2) @3)
4531   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4532  (simplify
4533   (op @3 (vec_cond:s @0 @1 @2))
4534   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4536 #if GIMPLE
4537 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4538  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4539            INTEGER_CST@1)
4540  (with {
4541          int ibit = tree_log2 (@0);
4542          int ibit2 = tree_log2 (@1);
4543        }
4544   (if (ibit == ibit2
4545       && ibit >= 0
4546       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4548 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4549  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4550           INTEGER_CST@1)
4551  (with {
4552          int ibit = tree_log2 (@0);
4553          int ibit2 = tree_log2 (@1);
4554        }
4555   (if (ibit == ibit2
4556       && ibit >= 0
4557       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4559 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4560  (bit_and:c
4561   (convert1?@4
4562    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4563   (convert2? @0))
4564  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4566 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4567  (bit_and:c
4568   (convert1?@4
4569    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4570   (convert2? @0))
4571  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4573 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4574  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4575             INTEGER_CST@1)
4576  (with {
4577          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4578                                               TYPE_PRECISION(type)));
4579          int ibit2 = tree_log2 (@1);
4580        }
4581   (if (ibit == ibit2
4582       && ibit >= 0
4583       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4585 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4586  (bit_and@4
4587   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4588   INTEGER_CST@1)
4589  (with {
4590          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4591                                               TYPE_PRECISION(type)));
4592          int ibit2 = tree_log2 (@1);
4593        }
4594   (if (ibit == ibit2
4595       && ibit >= 0
4596       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4598 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4599  (bit_and:c
4600   (convert1?@3
4601    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4602   (convert2? @0))
4603  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4605 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4606  (bit_and:c
4607   (convert1?@3
4608    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4609   (convert2? @0))
4610   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4612 #endif
4614 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4615    Currently disabled after pass lvec because ARM understands
4616    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
4617 (simplify
4618  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4619  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4620   (vec_cond (bit_and @0 @3) @1 @2)))
4621 (simplify
4622  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4623  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4624   (vec_cond (bit_ior @0 @3) @1 @2)))
4625 (simplify
4626  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4627  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4628   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4629 (simplify
4630  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4631  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4632   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4634 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
4635 (simplify
4636  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4637  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4638   (vec_cond (bit_and @0 @1) @2 @3)))
4639 (simplify
4640  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4641  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4642   (vec_cond (bit_ior @0 @1) @2 @3)))
4643 (simplify
4644  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4645  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4646   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4647 (simplify
4648  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4649  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4650   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4652 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4653    types are compatible.  */
4654 (simplify
4655  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4656  (if (VECTOR_BOOLEAN_TYPE_P (type)
4657       && types_match (type, TREE_TYPE (@0)))
4658   (if (integer_zerop (@1) && integer_all_onesp (@2))
4659    (bit_not @0)
4660    (if (integer_all_onesp (@1) && integer_zerop (@2))
4661     @0))))
4663 /* A few simplifications of "a ? CST1 : CST2". */
4664 /* NOTE: Only do this on gimple as the if-chain-to-switch
4665    optimization depends on the gimple to have if statements in it. */
4666 #if GIMPLE
4667 (simplify
4668  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4669  (switch
4670   (if (integer_zerop (@2))
4671    (switch
4672     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4673     (if (integer_onep (@1))
4674      (convert (convert:boolean_type_node @0)))
4675     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4676     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4677      (with {
4678        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4679       }
4680       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4681     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
4682        here as the powerof2cst case above will handle that case correctly.  */
4683     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4684      (negate (convert (convert:boolean_type_node @0))))))
4685   (if (integer_zerop (@1))
4686    (with {
4687       tree booltrue = constant_boolean_node (true, boolean_type_node);
4688     }
4689     (switch
4690      /* a ? 0 : 1 -> !a. */
4691      (if (integer_onep (@2))
4692       (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4693      /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4694      (if (INTEGRAL_TYPE_P (type) &&  integer_pow2p (@2))
4695       (with {
4696         tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4697        }
4698        (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4699         { shift; })))
4700      /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
4701        here as the powerof2cst case above will handle that case correctly.  */
4702      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4703       (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4704     )
4705    )
4706   )
4710 /* Optimize
4711    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
4712    x_5 ? cstN ? cst4 : cst3
4713    # op is == or != and N is 1 or 2
4714    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
4715    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
4716    of cst3 and cst4 is smaller.
4717    This was originally done by two_value_replacement in phiopt (PR 88676).  */
4718 (for eqne (ne eq)
4719  (simplify
4720   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
4721   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4722        && INTEGRAL_TYPE_P (type)
4723        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
4724            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
4725    (with {
4726      value_range r;
4727      get_range_query (cfun)->range_of_expr (r, @0);
4728      if (r.undefined_p ())
4729        r.set_varying (TREE_TYPE (@0));
4731      wide_int min = r.lower_bound ();
4732      wide_int max = r.upper_bound ();
4733     }
4734     (if (min + 1 == max
4735          && (wi::to_wide (@1) == min
4736              || wi::to_wide (@1) == max))
4737      (with {
4738        tree arg0 = @2, arg1 = @3;
4739        tree type1;
4740        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
4741          std::swap (arg0, arg1);
4742        if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
4743          {
4744            /* Avoid performing the arithmetics in bool type which has different
4745               semantics, otherwise prefer unsigned types from the two with
4746              the same precision.  */
4747            if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE
4748                || !TYPE_UNSIGNED (type))
4749              type1 = TREE_TYPE (@0);
4750            else
4751              type1 = TREE_TYPE (arg0);
4752          }
4753        else if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4754          type1 = TREE_TYPE (@0);
4755        else
4756          type1 = type;
4757        min = wide_int::from (min, TYPE_PRECISION (type1),
4758                              TYPE_SIGN (TREE_TYPE (@0)));
4759        wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type1),
4760                                     TYPE_SIGN (type));
4761        enum tree_code code;
4762        wi::overflow_type ovf;
4763        if (tree_int_cst_lt (arg0, arg1))
4764          {
4765            code = PLUS_EXPR;
4766            a -= min;
4767            if (!TYPE_UNSIGNED (type1))
4768              {
4769                /* lhs is known to be in range [min, min+1] and we want to add a
4770                   to it.  Check if that operation can overflow for those 2 values
4771                   and if yes, force unsigned type.  */
4772                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
4773                if (ovf)
4774                  type1 = unsigned_type_for (type1);
4775              }
4776          }
4777        else
4778          {
4779            code = MINUS_EXPR;
4780            a += min;
4781            if (!TYPE_UNSIGNED (type1))
4782              {
4783                /* lhs is known to be in range [min, min+1] and we want to subtract
4784                   it from a.  Check if that operation can overflow for those 2
4785                   values and if yes, force unsigned type.  */
4786                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
4787                if (ovf)
4788                 type1 = unsigned_type_for (type1);
4789              }
4790          }
4791        tree arg = wide_int_to_tree (type1, a);
4792       }
4793       (if (code == PLUS_EXPR)
4794        (convert (plus (convert:type1 @0) { arg; }))
4795        (convert (minus { arg; } (convert:type1 @0)))
4796       )
4797      )
4798     )
4799    )
4800   )
4803 #endif
4805 (simplify
4806  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4807  (if (INTEGRAL_TYPE_P (type)
4808       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4809   (cond @1 (convert @2) (convert @3))))
4811 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
4812    be extended.  */
4813 /* This pattern implements two kinds simplification:
4815    Case 1)
4816    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4817      1) Conversions are type widening from smaller type.
4818      2) Const c1 equals to c2 after canonicalizing comparison.
4819      3) Comparison has tree code LT, LE, GT or GE.
4820    This specific pattern is needed when (cmp (convert x) c) may not
4821    be simplified by comparison patterns because of multiple uses of
4822    x.  It also makes sense here because simplifying across multiple
4823    referred var is always benefitial for complicated cases.
4825    Case 2)
4826    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
4827 (for cmp (lt le gt ge eq)
4828  (simplify
4829   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4830   (with
4831    {
4832      tree from_type = TREE_TYPE (@1);
4833      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4834      enum tree_code code = ERROR_MARK;
4836      if (INTEGRAL_TYPE_P (from_type)
4837          && int_fits_type_p (@2, from_type)
4838          && (types_match (c1_type, from_type)
4839              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4840                  && (TYPE_UNSIGNED (from_type)
4841                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4842          && (types_match (c2_type, from_type)
4843              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4844                  && (TYPE_UNSIGNED (from_type)
4845                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4846        {
4847          if (cmp != EQ_EXPR)
4848            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
4849          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
4850          else if (int_fits_type_p (@3, from_type))
4851            code = EQ_EXPR;
4852        }
4853    }
4854    (if (code == MAX_EXPR)
4855     (convert (max @1 (convert @2)))
4856     (if (code == MIN_EXPR)
4857      (convert (min @1 (convert @2)))
4858      (if (code == EQ_EXPR)
4859       (convert (cond (eq @1 (convert @3))
4860                      (convert:from_type @3) (convert:from_type @2)))))))))
4862 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4864      1) OP is PLUS or MINUS.
4865      2) CMP is LT, LE, GT or GE.
4866      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4868    This pattern also handles special cases like:
4870      A) Operand x is a unsigned to signed type conversion and c1 is
4871         integer zero.  In this case,
4872           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
4873           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
4874      B) Const c1 may not equal to (C3 op' C2).  In this case we also
4875         check equality for (c1+1) and (c1-1) by adjusting comparison
4876         code.
4878    TODO: Though signed type is handled by this pattern, it cannot be
4879    simplified at the moment because C standard requires additional
4880    type promotion.  In order to match&simplify it here, the IR needs
4881    to be cleaned up by other optimizers, i.e, VRP.  */
4882 (for op (plus minus)
4883  (for cmp (lt le gt ge)
4884   (simplify
4885    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4886    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4887     (if (types_match (from_type, to_type)
4888          /* Check if it is special case A).  */
4889          || (TYPE_UNSIGNED (from_type)
4890              && !TYPE_UNSIGNED (to_type)
4891              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4892              && integer_zerop (@1)
4893              && (cmp == LT_EXPR || cmp == GE_EXPR)))
4894      (with
4895       {
4896         wi::overflow_type overflow = wi::OVF_NONE;
4897         enum tree_code code, cmp_code = cmp;
4898         wide_int real_c1;
4899         wide_int c1 = wi::to_wide (@1);
4900         wide_int c2 = wi::to_wide (@2);
4901         wide_int c3 = wi::to_wide (@3);
4902         signop sgn = TYPE_SIGN (from_type);
4904         /* Handle special case A), given x of unsigned type:
4905             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
4906             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
4907         if (!types_match (from_type, to_type))
4908           {
4909             if (cmp_code == LT_EXPR)
4910               cmp_code = GT_EXPR;
4911             if (cmp_code == GE_EXPR)
4912               cmp_code = LE_EXPR;
4913             c1 = wi::max_value (to_type);
4914           }
4915         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
4916            compute (c3 op' c2) and check if it equals to c1 with op' being
4917            the inverted operator of op.  Make sure overflow doesn't happen
4918            if it is undefined.  */
4919         if (op == PLUS_EXPR)
4920           real_c1 = wi::sub (c3, c2, sgn, &overflow);
4921         else
4922           real_c1 = wi::add (c3, c2, sgn, &overflow);
4924         code = cmp_code;
4925         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4926           {
4927             /* Check if c1 equals to real_c1.  Boundary condition is handled
4928                by adjusting comparison operation if necessary.  */
4929             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4930                 && !overflow)
4931               {
4932                 /* X <= Y - 1 equals to X < Y.  */
4933                 if (cmp_code == LE_EXPR)
4934                   code = LT_EXPR;
4935                 /* X > Y - 1 equals to X >= Y.  */
4936                 if (cmp_code == GT_EXPR)
4937                   code = GE_EXPR;
4938               }
4939             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4940                 && !overflow)
4941               {
4942                 /* X < Y + 1 equals to X <= Y.  */
4943                 if (cmp_code == LT_EXPR)
4944                   code = LE_EXPR;
4945                 /* X >= Y + 1 equals to X > Y.  */
4946                 if (cmp_code == GE_EXPR)
4947                   code = GT_EXPR;
4948               }
4949             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4950               {
4951                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4952                   code = MIN_EXPR;
4953                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4954                   code = MAX_EXPR;
4955               }
4956           }
4957       }
4958       (if (code == MAX_EXPR)
4959        (op (max @X { wide_int_to_tree (from_type, real_c1); })
4960            { wide_int_to_tree (from_type, c2); })
4961        (if (code == MIN_EXPR)
4962         (op (min @X { wide_int_to_tree (from_type, real_c1); })
4963             { wide_int_to_tree (from_type, c2); })))))))))
4965 #if GIMPLE
4966 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
4967    in fold_cond_expr_with_comparison for GENERIC folding with
4968    some extra constraints.  */
4969 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4970  (simplify
4971   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4972         (convert3? @0) (convert4? @1))
4973   (if (!HONOR_SIGNED_ZEROS (type)
4974        && (/* Allow widening conversions of the compare operands as data.  */
4975            (INTEGRAL_TYPE_P (type)
4976             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
4977             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
4978             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4979             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4980            /* Or sign conversions for the comparison.  */
4981            || (types_match (type, TREE_TYPE (@0))
4982                && types_match (type, TREE_TYPE (@1)))))
4983    (switch
4984     (if (cmp == EQ_EXPR)
4985      (if (VECTOR_TYPE_P (type))
4986       (view_convert @c1)
4987       (convert @c1)))
4988     (if (cmp == NE_EXPR)
4989      (if (VECTOR_TYPE_P (type))
4990       (view_convert @c0)
4991       (convert @c0)))
4992     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4993      (if (!HONOR_NANS (type))
4994       (if (VECTOR_TYPE_P (type))
4995        (view_convert (min @c0 @c1))
4996        (convert (min @c0 @c1)))))
4997     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4998      (if (!HONOR_NANS (type))
4999       (if (VECTOR_TYPE_P (type))
5000        (view_convert (max @c0 @c1))
5001        (convert (max @c0 @c1)))))
5002     (if (cmp == UNEQ_EXPR)
5003      (if (!HONOR_NANS (type))
5004       (if (VECTOR_TYPE_P (type))
5005        (view_convert @c1)
5006        (convert @c1))))
5007     (if (cmp == LTGT_EXPR)
5008      (if (!HONOR_NANS (type))
5009       (if (VECTOR_TYPE_P (type))
5010        (view_convert @c0)
5011        (convert @c0))))))))
5012 #endif
5014 /* These was part of minmax phiopt.  */
5015 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5016    to minmax<min/max<a, b>, c> */
5017 (for minmax (min max)
5018  (for cmp (lt le gt ge)
5019   (simplify
5020    (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5021    (with
5022     {
5023       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5024     }
5025     (if (code == MIN_EXPR)
5026      (minmax (min @1 @2) @4)
5027      (if (code == MAX_EXPR)
5028       (minmax (max @1 @2) @4)))))))
5030 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5031 (simplify
5032  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5033  (if (!TYPE_SATURATING (type)
5034       && (TYPE_OVERFLOW_WRAPS (type)
5035           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5036       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5037   @3))
5039 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5040 (simplify
5041  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5042  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5043   @3))
5045 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5046    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5047 (simplify
5048  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5049  (if (TYPE_UNSIGNED (type))
5050   (cond (ge @0 @1) (negate @0) @2)))
5052 (for cnd (cond vec_cond)
5053  /* A ? B : (A ? X : C) -> A ? B : C.  */
5054  (simplify
5055   (cnd @0 (cnd @0 @1 @2) @3)
5056   (cnd @0 @1 @3))
5057  (simplify
5058   (cnd @0 @1 (cnd @0 @2 @3))
5059   (cnd @0 @1 @3))
5060  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5061  /* ???  This matches embedded conditions open-coded because genmatch
5062     would generate matching code for conditions in separate stmts only.
5063     The following is still important to merge then and else arm cases
5064     from if-conversion.  */
5065  (simplify
5066   (cnd @0 @1 (cnd @2 @3 @4))
5067   (if (inverse_conditions_p (@0, @2))
5068    (cnd @0 @1 @3)))
5069  (simplify
5070   (cnd @0 (cnd @1 @2 @3) @4)
5071   (if (inverse_conditions_p (@0, @1))
5072    (cnd @0 @3 @4)))
5074  /* A ? B : B -> B.  */
5075  (simplify
5076   (cnd @0 @1 @1)
5077   @1)
5079  /* !A ? B : C -> A ? C : B.  */
5080  (simplify
5081   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5082   (cnd @0 @2 @1)))
5084 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5085    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5086    Need to handle UN* comparisons.
5088    None of these transformations work for modes with signed
5089    zeros.  If A is +/-0, the first two transformations will
5090    change the sign of the result (from +0 to -0, or vice
5091    versa).  The last four will fix the sign of the result,
5092    even though the original expressions could be positive or
5093    negative, depending on the sign of A.
5095    Note that all these transformations are correct if A is
5096    NaN, since the two alternatives (A and -A) are also NaNs.  */
5098 (for cnd (cond vec_cond)
5099  /* A == 0 ? A : -A    same as -A */
5100  (for cmp (eq uneq)
5101   (simplify
5102    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5103     (if (!HONOR_SIGNED_ZEROS (type))
5104      @1))
5105   (simplify
5106    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5107     (if (!HONOR_SIGNED_ZEROS (type))
5108      @1))
5110  /* A != 0 ? A : -A    same as A */
5111  (for cmp (ne ltgt)
5112   (simplify
5113    (cnd (cmp @0 zerop) @0 (negate @0))
5114     (if (!HONOR_SIGNED_ZEROS (type))
5115      @0))
5116   (simplify
5117    (cnd (cmp @0 zerop) @0 integer_zerop)
5118     (if (!HONOR_SIGNED_ZEROS (type))
5119      @0))
5121  /* A >=/> 0 ? A : -A    same as abs (A) */
5122  (for cmp (ge gt)
5123   (simplify
5124    (cnd (cmp @0 zerop) @0 (negate @0))
5125     (if (!HONOR_SIGNED_ZEROS (type)
5126          && !TYPE_UNSIGNED (type))
5127      (abs @0))))
5128  /* A <=/< 0 ? A : -A    same as -abs (A) */
5129  (for cmp (le lt)
5130   (simplify
5131    (cnd (cmp @0 zerop) @0 (negate @0))
5132     (if (!HONOR_SIGNED_ZEROS (type)
5133          && !TYPE_UNSIGNED (type))
5134      (if (ANY_INTEGRAL_TYPE_P (type)
5135           && !TYPE_OVERFLOW_WRAPS (type))
5136       (with {
5137         tree utype = unsigned_type_for (type);
5138        }
5139        (convert (negate (absu:utype @0))))
5140        (negate (abs @0)))))
5144 /* -(type)!A -> (type)A - 1.  */
5145 (simplify
5146  (negate (convert?:s (logical_inverted_value:s @0)))
5147  (if (INTEGRAL_TYPE_P (type)
5148       && TREE_CODE (type) != BOOLEAN_TYPE
5149       && TYPE_PRECISION (type) > 1
5150       && TREE_CODE (@0) == SSA_NAME
5151       && ssa_name_has_boolean_range (@0))
5152   (plus (convert:type @0) { build_all_ones_cst (type); })))
5154 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5155    return all -1 or all 0 results.  */
5156 /* ??? We could instead convert all instances of the vec_cond to negate,
5157    but that isn't necessarily a win on its own.  */
5158 (simplify
5159  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5160  (if (VECTOR_TYPE_P (type)
5161       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5162                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5163       && (TYPE_MODE (TREE_TYPE (type))
5164           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5165   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5167 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5168 (simplify
5169  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5170  (if (VECTOR_TYPE_P (type)
5171       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5172                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5173       && (TYPE_MODE (TREE_TYPE (type))
5174           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5175   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5178 /* Simplifications of comparisons.  */
5180 /* See if we can reduce the magnitude of a constant involved in a
5181    comparison by changing the comparison code.  This is a canonicalization
5182    formerly done by maybe_canonicalize_comparison_1.  */
5183 (for cmp  (le gt)
5184      acmp (lt ge)
5185  (simplify
5186   (cmp @0 uniform_integer_cst_p@1)
5187   (with { tree cst = uniform_integer_cst_p (@1); }
5188    (if (tree_int_cst_sgn (cst) == -1)
5189      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5190                                    wide_int_to_tree (TREE_TYPE (cst),
5191                                                      wi::to_wide (cst)
5192                                                      + 1)); })))))
5193 (for cmp  (ge lt)
5194      acmp (gt le)
5195  (simplify
5196   (cmp @0 uniform_integer_cst_p@1)
5197   (with { tree cst = uniform_integer_cst_p (@1); }
5198    (if (tree_int_cst_sgn (cst) == 1)
5199     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5200                                   wide_int_to_tree (TREE_TYPE (cst),
5201                                   wi::to_wide (cst) - 1)); })))))
5203 /* We can simplify a logical negation of a comparison to the
5204    inverted comparison.  As we cannot compute an expression
5205    operator using invert_tree_comparison we have to simulate
5206    that with expression code iteration.  */
5207 (for cmp (tcc_comparison)
5208      icmp (inverted_tcc_comparison)
5209      ncmp (inverted_tcc_comparison_with_nans)
5210  /* Ideally we'd like to combine the following two patterns
5211     and handle some more cases by using
5212       (logical_inverted_value (cmp @0 @1))
5213     here but for that genmatch would need to "inline" that.
5214     For now implement what forward_propagate_comparison did.  */
5215  (simplify
5216   (bit_not (cmp @0 @1))
5217   (if (VECTOR_TYPE_P (type)
5218        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5219    /* Comparison inversion may be impossible for trapping math,
5220       invert_tree_comparison will tell us.  But we can't use
5221       a computed operator in the replacement tree thus we have
5222       to play the trick below.  */
5223    (with { enum tree_code ic = invert_tree_comparison
5224              (cmp, HONOR_NANS (@0)); }
5225     (if (ic == icmp)
5226      (icmp @0 @1)
5227      (if (ic == ncmp)
5228       (ncmp @0 @1))))))
5229  (simplify
5230   (bit_xor (cmp @0 @1) integer_truep)
5231   (with { enum tree_code ic = invert_tree_comparison
5232             (cmp, HONOR_NANS (@0)); }
5233    (if (ic == icmp)
5234     (icmp @0 @1)
5235     (if (ic == ncmp)
5236      (ncmp @0 @1)))))
5237  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5238  (simplify
5239   (ne (cmp@2 @0 @1) integer_zerop)
5240   (if (types_match (type, TREE_TYPE (@2)))
5241    (cmp @0 @1)))
5242  (simplify
5243   (eq (cmp@2 @0 @1) integer_truep)
5244   (if (types_match (type, TREE_TYPE (@2)))
5245    (cmp @0 @1)))
5246  (simplify
5247   (ne (cmp@2 @0 @1) integer_truep)
5248   (if (types_match (type, TREE_TYPE (@2)))
5249    (with { enum tree_code ic = invert_tree_comparison
5250              (cmp, HONOR_NANS (@0)); }
5251     (if (ic == icmp)
5252      (icmp @0 @1)
5253      (if (ic == ncmp)
5254       (ncmp @0 @1))))))
5255  (simplify
5256   (eq (cmp@2 @0 @1) integer_zerop)
5257   (if (types_match (type, TREE_TYPE (@2)))
5258    (with { enum tree_code ic = invert_tree_comparison
5259              (cmp, HONOR_NANS (@0)); }
5260     (if (ic == icmp)
5261      (icmp @0 @1)
5262      (if (ic == ncmp)
5263       (ncmp @0 @1)))))))
5265 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5266    ??? The transformation is valid for the other operators if overflow
5267    is undefined for the type, but performing it here badly interacts
5268    with the transformation in fold_cond_expr_with_comparison which
5269    attempts to synthetize ABS_EXPR.  */
5270 (for cmp (eq ne)
5271  (for sub (minus pointer_diff)
5272   (simplify
5273    (cmp (sub@2 @0 @1) integer_zerop)
5274    (if (single_use (@2))
5275     (cmp @0 @1)))))
5277 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5278    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5279 (for cmp (lt ge)
5280  (simplify
5281   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5282    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5283         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5284         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5285     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5286 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5287    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5288 (simplify
5289  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5290   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5291        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5292        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5293    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5295 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5296    signed arithmetic case.  That form is created by the compiler
5297    often enough for folding it to be of value.  One example is in
5298    computing loop trip counts after Operator Strength Reduction.  */
5299 (for cmp (simple_comparison)
5300      scmp (swapped_simple_comparison)
5301  (simplify
5302   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5303   /* Handle unfolded multiplication by zero.  */
5304   (if (integer_zerop (@1))
5305    (cmp @1 @2)
5306    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5307         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5308         && single_use (@3))
5309     /* If @1 is negative we swap the sense of the comparison.  */
5310     (if (tree_int_cst_sgn (@1) < 0)
5311      (scmp @0 @2)
5312      (cmp @0 @2))))))
5314 /* For integral types with undefined overflow fold
5315    x * C1 == C2 into x == C2 / C1 or false.
5316    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5317    Z / 2^n Z.  */
5318 (for cmp (eq ne)
5319  (simplify
5320   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5321   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5322        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5323        && wi::to_wide (@1) != 0)
5324    (with { widest_int quot; }
5325     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5326                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5327      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5328      { constant_boolean_node (cmp == NE_EXPR, type); }))
5329    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5330         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5331         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5332     (cmp @0
5333      {
5334        tree itype = TREE_TYPE (@0);
5335        int p = TYPE_PRECISION (itype);
5336        wide_int m = wi::one (p + 1) << p;
5337        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5338        wide_int i = wide_int::from (wi::mod_inv (a, m),
5339                                     p, TYPE_SIGN (itype));
5340        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5341      })))))
5343 /* Simplify comparison of something with itself.  For IEEE
5344    floating-point, we can only do some of these simplifications.  */
5345 (for cmp (eq ge le)
5346  (simplify
5347   (cmp @0 @0)
5348   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5349        || ! tree_expr_maybe_nan_p (@0))
5350    { constant_boolean_node (true, type); }
5351    (if (cmp != EQ_EXPR
5352         /* With -ftrapping-math conversion to EQ loses an exception.  */
5353         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5354             || ! flag_trapping_math))
5355     (eq @0 @0)))))
5356 (for cmp (ne gt lt)
5357  (simplify
5358   (cmp @0 @0)
5359   (if (cmp != NE_EXPR
5360        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5361        || ! tree_expr_maybe_nan_p (@0))
5362    { constant_boolean_node (false, type); })))
5363 (for cmp (unle unge uneq)
5364  (simplify
5365   (cmp @0 @0)
5366   { constant_boolean_node (true, type); }))
5367 (for cmp (unlt ungt)
5368  (simplify
5369   (cmp @0 @0)
5370   (unordered @0 @0)))
5371 (simplify
5372  (ltgt @0 @0)
5373  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5374   { constant_boolean_node (false, type); }))
5376 /* x == ~x -> false */
5377 /* x != ~x -> true */
5378 (for cmp (eq ne)
5379  (simplify
5380   (cmp:c @0 (bit_not @0))
5381   { constant_boolean_node (cmp == NE_EXPR, type); }))
5383 /* Fold ~X op ~Y as Y op X.  */
5384 (for cmp (simple_comparison)
5385  (simplify
5386   (cmp (bit_not@2 @0) (bit_not@3 @1))
5387   (if (single_use (@2) && single_use (@3))
5388    (cmp @1 @0))))
5390 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5391 (for cmp (simple_comparison)
5392      scmp (swapped_simple_comparison)
5393  (simplify
5394   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5395   (if (single_use (@2)
5396        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5397    (scmp @0 (bit_not @1)))))
5399 (for cmp (simple_comparison)
5400  (simplify
5401   (cmp @0 REAL_CST@1)
5402   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5403   (switch
5404    /* a CMP (-0) -> a CMP 0  */
5405    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5406     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5407    /* (-0) CMP b -> 0 CMP b.  */
5408    (if (TREE_CODE (@0) == REAL_CST
5409         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5410     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5411    /* x != NaN is always true, other ops are always false.  */
5412    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5413         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5414         && !tree_expr_signaling_nan_p (@1)
5415         && !tree_expr_maybe_signaling_nan_p (@0))
5416     { constant_boolean_node (cmp == NE_EXPR, type); })
5417    /* NaN != y is always true, other ops are always false.  */
5418    (if (TREE_CODE (@0) == REAL_CST
5419         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5420         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5421         && !tree_expr_signaling_nan_p (@0)
5422         && !tree_expr_signaling_nan_p (@1))
5423     { constant_boolean_node (cmp == NE_EXPR, type); })
5424    /* Fold comparisons against infinity.  */
5425    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5426         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5427     (with
5428      {
5429        REAL_VALUE_TYPE max;
5430        enum tree_code code = cmp;
5431        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5432        if (neg)
5433          code = swap_tree_comparison (code);
5434      }
5435      (switch
5436       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5437       (if (code == GT_EXPR
5438            && !(HONOR_NANS (@0) && flag_trapping_math))
5439        { constant_boolean_node (false, type); })
5440       (if (code == LE_EXPR)
5441        /* x <= +Inf is always true, if we don't care about NaNs.  */
5442        (if (! HONOR_NANS (@0))
5443         { constant_boolean_node (true, type); }
5444         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5445            an "invalid" exception.  */
5446         (if (!flag_trapping_math)
5447          (eq @0 @0))))
5448       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5449          for == this introduces an exception for x a NaN.  */
5450       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5451            || code == GE_EXPR)
5452        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5453         (if (neg)
5454          (lt @0 { build_real (TREE_TYPE (@0), max); })
5455          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5456       /* x < +Inf is always equal to x <= DBL_MAX.  */
5457       (if (code == LT_EXPR)
5458        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5459         (if (neg)
5460          (ge @0 { build_real (TREE_TYPE (@0), max); })
5461          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5462       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5463          an exception for x a NaN so use an unordered comparison.  */
5464       (if (code == NE_EXPR)
5465        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5466         (if (! HONOR_NANS (@0))
5467          (if (neg)
5468           (ge @0 { build_real (TREE_TYPE (@0), max); })
5469           (le @0 { build_real (TREE_TYPE (@0), max); }))
5470          (if (neg)
5471           (unge @0 { build_real (TREE_TYPE (@0), max); })
5472           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5474  /* If this is a comparison of a real constant with a PLUS_EXPR
5475     or a MINUS_EXPR of a real constant, we can convert it into a
5476     comparison with a revised real constant as long as no overflow
5477     occurs when unsafe_math_optimizations are enabled.  */
5478  (if (flag_unsafe_math_optimizations)
5479   (for op (plus minus)
5480    (simplify
5481     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5482     (with
5483      {
5484        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5485                                TREE_TYPE (@1), @2, @1);
5486      }
5487      (if (tem && !TREE_OVERFLOW (tem))
5488       (cmp @0 { tem; }))))))
5490  /* Likewise, we can simplify a comparison of a real constant with
5491     a MINUS_EXPR whose first operand is also a real constant, i.e.
5492     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
5493     floating-point types only if -fassociative-math is set.  */
5494  (if (flag_associative_math)
5495   (simplify
5496    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5497    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5498     (if (tem && !TREE_OVERFLOW (tem))
5499      (cmp { tem; } @1)))))
5501  /* Fold comparisons against built-in math functions.  */
5502  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5503   (for sq (SQRT)
5504    (simplify
5505     (cmp (sq @0) REAL_CST@1)
5506     (switch
5507      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5508       (switch
5509        /* sqrt(x) < y is always false, if y is negative.  */
5510        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5511         { constant_boolean_node (false, type); })
5512        /* sqrt(x) > y is always true, if y is negative and we
5513           don't care about NaNs, i.e. negative values of x.  */
5514        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5515         { constant_boolean_node (true, type); })
5516        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5517        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5518      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5519       (switch
5520        /* sqrt(x) < 0 is always false.  */
5521        (if (cmp == LT_EXPR)
5522         { constant_boolean_node (false, type); })
5523        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
5524        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5525         { constant_boolean_node (true, type); })
5526        /* sqrt(x) <= 0 -> x == 0.  */
5527        (if (cmp == LE_EXPR)
5528         (eq @0 @1))
5529        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
5530           == or !=.  In the last case:
5532             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5534           if x is negative or NaN.  Due to -funsafe-math-optimizations,
5535           the results for other x follow from natural arithmetic.  */
5536        (cmp @0 @1)))
5537      (if ((cmp == LT_EXPR
5538            || cmp == LE_EXPR
5539            || cmp == GT_EXPR
5540            || cmp == GE_EXPR)
5541           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5542           /* Give up for -frounding-math.  */
5543           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5544       (with
5545        {
5546          REAL_VALUE_TYPE c2;
5547          enum tree_code ncmp = cmp;
5548          const real_format *fmt
5549            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5550          real_arithmetic (&c2, MULT_EXPR,
5551                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5552          real_convert (&c2, fmt, &c2);
5553          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5554             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
5555          if (!REAL_VALUE_ISINF (c2))
5556            {
5557              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5558                                         build_real (TREE_TYPE (@0), c2));
5559              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5560                ncmp = ERROR_MARK;
5561              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5562                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5563                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5564              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5565                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5566                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5567              else
5568                {
5569                  /* With rounding to even, sqrt of up to 3 different values
5570                     gives the same normal result, so in some cases c2 needs
5571                     to be adjusted.  */
5572                  REAL_VALUE_TYPE c2alt, tow;
5573                  if (cmp == LT_EXPR || cmp == GE_EXPR)
5574                    tow = dconst0;
5575                  else
5576                    tow = dconstinf;
5577                  real_nextafter (&c2alt, fmt, &c2, &tow);
5578                  real_convert (&c2alt, fmt, &c2alt);
5579                  if (REAL_VALUE_ISINF (c2alt))
5580                    ncmp = ERROR_MARK;
5581                  else
5582                    {
5583                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5584                                            build_real (TREE_TYPE (@0), c2alt));
5585                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5586                        ncmp = ERROR_MARK;
5587                      else if (real_equal (&TREE_REAL_CST (c3),
5588                                           &TREE_REAL_CST (@1)))
5589                        c2 = c2alt;
5590                    }
5591                }
5592            }
5593        }
5594        (if (cmp == GT_EXPR || cmp == GE_EXPR)
5595         (if (REAL_VALUE_ISINF (c2))
5596          /* sqrt(x) > y is x == +Inf, when y is very large.  */
5597          (if (HONOR_INFINITIES (@0))
5598           (eq @0 { build_real (TREE_TYPE (@0), c2); })
5599           { constant_boolean_node (false, type); })
5600          /* sqrt(x) > c is the same as x > c*c.  */
5601          (if (ncmp != ERROR_MARK)
5602           (if (ncmp == GE_EXPR)
5603            (ge @0 { build_real (TREE_TYPE (@0), c2); })
5604            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5605         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
5606         (if (REAL_VALUE_ISINF (c2))
5607          (switch
5608           /* sqrt(x) < y is always true, when y is a very large
5609              value and we don't care about NaNs or Infinities.  */
5610           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5611            { constant_boolean_node (true, type); })
5612           /* sqrt(x) < y is x != +Inf when y is very large and we
5613              don't care about NaNs.  */
5614           (if (! HONOR_NANS (@0))
5615            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5616           /* sqrt(x) < y is x >= 0 when y is very large and we
5617              don't care about Infinities.  */
5618           (if (! HONOR_INFINITIES (@0))
5619            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5620           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5621           (if (GENERIC)
5622            (truth_andif
5623             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5624             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5625          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5626          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5627           (if (ncmp == LT_EXPR)
5628            (lt @0 { build_real (TREE_TYPE (@0), c2); })
5629            (le @0 { build_real (TREE_TYPE (@0), c2); }))
5630           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5631           (if (ncmp != ERROR_MARK && GENERIC)
5632            (if (ncmp == LT_EXPR)
5633             (truth_andif
5634              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5635              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5636             (truth_andif
5637              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5638              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5639    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
5640    (simplify
5641     (cmp (sq @0) (sq @1))
5642       (if (! HONOR_NANS (@0))
5643         (cmp @0 @1))))))
5645 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
5646 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5647      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
5648  (simplify
5649   (cmp (float@0 @1) (float @2))
5650    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5651         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5652     (with
5653      {
5654        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5655        tree type1 = TREE_TYPE (@1);
5656        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5657        tree type2 = TREE_TYPE (@2);
5658        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5659      }
5660      (if (fmt.can_represent_integral_type_p (type1)
5661           && fmt.can_represent_integral_type_p (type2))
5662       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5663        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5664        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5665             && type1_signed_p >= type2_signed_p)
5666         (icmp @1 (convert @2))
5667         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5668              && type1_signed_p <= type2_signed_p)
5669          (icmp (convert:type2 @1) @2)
5670          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5671               && type1_signed_p == type2_signed_p)
5672           (icmp @1 @2))))))))))
5674 /* Optimize various special cases of (FTYPE) N CMP CST.  */
5675 (for cmp  (lt le eq ne ge gt)
5676      icmp (le le eq ne ge ge)
5677  (simplify
5678   (cmp (float @0) REAL_CST@1)
5679    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5680         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5681     (with
5682      {
5683        tree itype = TREE_TYPE (@0);
5684        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5685        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5686        /* Be careful to preserve any potential exceptions due to
5687           NaNs.  qNaNs are ok in == or != context.
5688           TODO: relax under -fno-trapping-math or
5689           -fno-signaling-nans.  */
5690        bool exception_p
5691          = real_isnan (cst) && (cst->signalling
5692                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5693      }
5694      /* TODO: allow non-fitting itype and SNaNs when
5695         -fno-trapping-math.  */
5696      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5697       (with
5698        {
5699          signop isign = TYPE_SIGN (itype);
5700          REAL_VALUE_TYPE imin, imax;
5701          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5702          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5704          REAL_VALUE_TYPE icst;
5705          if (cmp == GT_EXPR || cmp == GE_EXPR)
5706            real_ceil (&icst, fmt, cst);
5707          else if (cmp == LT_EXPR || cmp == LE_EXPR)
5708            real_floor (&icst, fmt, cst);
5709          else
5710            real_trunc (&icst, fmt, cst);
5712          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5714          bool overflow_p = false;
5715          wide_int icst_val
5716            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5717        }
5718        (switch
5719         /* Optimize cases when CST is outside of ITYPE's range.  */
5720         (if (real_compare (LT_EXPR, cst, &imin))
5721          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5722                                   type); })
5723         (if (real_compare (GT_EXPR, cst, &imax))
5724          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5725                                   type); })
5726         /* Remove cast if CST is an integer representable by ITYPE.  */
5727         (if (cst_int_p)
5728          (cmp @0 { gcc_assert (!overflow_p);
5729                    wide_int_to_tree (itype, icst_val); })
5730         )
5731         /* When CST is fractional, optimize
5732             (FTYPE) N == CST -> 0
5733             (FTYPE) N != CST -> 1.  */
5734         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5735          { constant_boolean_node (cmp == NE_EXPR, type); })
5736         /* Otherwise replace with sensible integer constant.  */
5737         (with
5738          {
5739            gcc_checking_assert (!overflow_p);
5740          }
5741          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5743 /* Fold A /[ex] B CMP C to A CMP B * C.  */
5744 (for cmp (eq ne)
5745  (simplify
5746   (cmp (exact_div @0 @1) INTEGER_CST@2)
5747   (if (!integer_zerop (@1))
5748    (if (wi::to_wide (@2) == 0)
5749     (cmp @0 @2)
5750     (if (TREE_CODE (@1) == INTEGER_CST)
5751      (with
5752       {
5753         wi::overflow_type ovf;
5754         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5755                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5756       }
5757       (if (ovf)
5758        { constant_boolean_node (cmp == NE_EXPR, type); }
5759        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5760 (for cmp (lt le gt ge)
5761  (simplify
5762   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5763   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5764    (with
5765     {
5766       wi::overflow_type ovf;
5767       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5768                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5769     }
5770     (if (ovf)
5771      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5772                                         TYPE_SIGN (TREE_TYPE (@2)))
5773                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5774      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5776 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5778    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5779    For large C (more than min/B+2^size), this is also true, with the
5780    multiplication computed modulo 2^size.
5781    For intermediate C, this just tests the sign of A.  */
5782 (for cmp  (lt le gt ge)
5783      cmp2 (ge ge lt lt)
5784  (simplify
5785   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5786   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5787        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5788        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5789    (with
5790     {
5791       tree utype = TREE_TYPE (@2);
5792       wide_int denom = wi::to_wide (@1);
5793       wide_int right = wi::to_wide (@2);
5794       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5795       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5796       bool small = wi::leu_p (right, smax);
5797       bool large = wi::geu_p (right, smin);
5798     }
5799     (if (small || large)
5800      (cmp (convert:utype @0) (mult @2 (convert @1)))
5801      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5803 /* Unordered tests if either argument is a NaN.  */
5804 (simplify
5805  (bit_ior (unordered @0 @0) (unordered @1 @1))
5806  (if (types_match (@0, @1))
5807   (unordered @0 @1)))
5808 (simplify
5809  (bit_and (ordered @0 @0) (ordered @1 @1))
5810  (if (types_match (@0, @1))
5811   (ordered @0 @1)))
5812 (simplify
5813  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5814  @2)
5815 (simplify
5816  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5817  @2)
5819 /* Simple range test simplifications.  */
5820 /* A < B || A >= B -> true.  */
5821 (for test1 (lt le le le ne ge)
5822      test2 (ge gt ge ne eq ne)
5823  (simplify
5824   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5825   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5826        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5827    { constant_boolean_node (true, type); })))
5828 /* A < B && A >= B -> false.  */
5829 (for test1 (lt lt lt le ne eq)
5830      test2 (ge gt eq gt eq gt)
5831  (simplify
5832   (bit_and:c (test1 @0 @1) (test2 @0 @1))
5833   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5834        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5835    { constant_boolean_node (false, type); })))
5837 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5838    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
5840    Note that comparisons
5841      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
5842      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
5843    will be canonicalized to above so there's no need to
5844    consider them here.
5845  */
5847 (for cmp (le gt)
5848      eqcmp (eq ne)
5849  (simplify
5850   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5851   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5852    (with
5853     {
5854      tree ty = TREE_TYPE (@0);
5855      unsigned prec = TYPE_PRECISION (ty);
5856      wide_int mask = wi::to_wide (@2, prec);
5857      wide_int rhs = wi::to_wide (@3, prec);
5858      signop sgn = TYPE_SIGN (ty);
5859     }
5860     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5861          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5862       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5863              { build_zero_cst (ty); }))))))
5865 /* -A CMP -B -> B CMP A.  */
5866 (for cmp (tcc_comparison)
5867      scmp (swapped_tcc_comparison)
5868  (simplify
5869   (cmp (negate @0) (negate @1))
5870   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5871        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5872            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5873    (scmp @0 @1)))
5874  (simplify
5875   (cmp (negate @0) CONSTANT_CLASS_P@1)
5876   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5877        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5878            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5879    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5880     (if (tem && !TREE_OVERFLOW (tem))
5881      (scmp @0 { tem; }))))))
5883 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
5884 (for op (abs absu)
5885  (for eqne (eq ne)
5886   (simplify
5887    (eqne (op @0) zerop@1)
5888    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5890 /* From fold_sign_changed_comparison and fold_widened_comparison.
5891    FIXME: the lack of symmetry is disturbing.  */
5892 (for cmp (simple_comparison)
5893  (simplify
5894   (cmp (convert@0 @00) (convert?@1 @10))
5895   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5896        /* Disable this optimization if we're casting a function pointer
5897           type on targets that require function pointer canonicalization.  */
5898        && !(targetm.have_canonicalize_funcptr_for_compare ()
5899             && ((POINTER_TYPE_P (TREE_TYPE (@00))
5900                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5901                 || (POINTER_TYPE_P (TREE_TYPE (@10))
5902                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5903        && single_use (@0))
5904    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5905         && (TREE_CODE (@10) == INTEGER_CST
5906             || @1 != @10)
5907         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5908             || cmp == NE_EXPR
5909             || cmp == EQ_EXPR)
5910         && !POINTER_TYPE_P (TREE_TYPE (@00))
5911         /* (int)bool:32 != (int)uint is not the same as
5912            bool:32 != (bool:32)uint since boolean types only have two valid
5913            values independent of their precision.  */
5914         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5915             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5916     /* ???  The special-casing of INTEGER_CST conversion was in the original
5917        code and here to avoid a spurious overflow flag on the resulting
5918        constant which fold_convert produces.  */
5919     (if (TREE_CODE (@1) == INTEGER_CST)
5920      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5921                                 TREE_OVERFLOW (@1)); })
5922      (cmp @00 (convert @1)))
5924     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5925      /* If possible, express the comparison in the shorter mode.  */
5926      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5927            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5928            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5929                && TYPE_UNSIGNED (TREE_TYPE (@00))))
5930           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5931               || ((TYPE_PRECISION (TREE_TYPE (@00))
5932                    >= TYPE_PRECISION (TREE_TYPE (@10)))
5933                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
5934                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
5935               || (TREE_CODE (@10) == INTEGER_CST
5936                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5937                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
5938       (cmp @00 (convert @10))
5939       (if (TREE_CODE (@10) == INTEGER_CST
5940            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5941            && !int_fits_type_p (@10, TREE_TYPE (@00)))
5942        (with
5943         {
5944           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5945           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5946           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5947           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5948         }
5949         (if (above || below)
5950          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5951           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5952           (if (cmp == LT_EXPR || cmp == LE_EXPR)
5953            { constant_boolean_node (above ? true : false, type); }
5954            (if (cmp == GT_EXPR || cmp == GE_EXPR)
5955             { constant_boolean_node (above ? false : true, type); })))))))))
5956    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
5957    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
5958         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5959             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
5960         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5961             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
5962     (with
5963      {
5964        tree type1 = TREE_TYPE (@10);
5965        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
5966          {
5967            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
5968            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
5969                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
5970              type1 = float_type_node;
5971            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
5972                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
5973              type1 = double_type_node;
5974          }
5975       tree newtype
5976         = (TYPE_PRECISION (TREE_TYPE (@00)) > TYPE_PRECISION (type1)
5977            ? TREE_TYPE (@00) : type1);
5978      }
5979      (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (newtype))
5980       (cmp (convert:newtype @00) (convert:newtype @10))))))))
5983 (for cmp (eq ne)
5984  (simplify
5985   /* SSA names are canonicalized to 2nd place.  */
5986   (cmp addr@0 SSA_NAME@1)
5987   (with
5988    {
5989      poly_int64 off; tree base;
5990      tree addr = (TREE_CODE (@0) == SSA_NAME
5991                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5992    }
5993    /* A local variable can never be pointed to by
5994       the default SSA name of an incoming parameter.  */
5995    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5996         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5997         && (base = get_base_address (TREE_OPERAND (addr, 0)))
5998         && TREE_CODE (base) == VAR_DECL
5999         && auto_var_in_fn_p (base, current_function_decl))
6000     (if (cmp == NE_EXPR)
6001      { constant_boolean_node (true, type); }
6002      { constant_boolean_node (false, type); })
6003     /* If the address is based on @1 decide using the offset.  */
6004     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6005          && TREE_CODE (base) == MEM_REF
6006          && TREE_OPERAND (base, 0) == @1)
6007      (with { off += mem_ref_offset (base).force_shwi (); }
6008       (if (known_ne (off, 0))
6009        { constant_boolean_node (cmp == NE_EXPR, type); }
6010        (if (known_eq (off, 0))
6011         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6013 /* Equality compare simplifications from fold_binary  */
6014 (for cmp (eq ne)
6016  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6017     Similarly for NE_EXPR.  */
6018  (simplify
6019   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6020   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6021        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6022    { constant_boolean_node (cmp == NE_EXPR, type); }))
6024  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6025  (simplify
6026   (cmp (bit_xor @0 @1) integer_zerop)
6027   (cmp @0 @1))
6029  /* (X ^ Y) == Y becomes X == 0.
6030     Likewise (X ^ Y) == X becomes Y == 0.  */
6031  (simplify
6032   (cmp:c (bit_xor:c @0 @1) @0)
6033   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6035  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6036  (simplify
6037   (cmp:c (bit_and:c @0 @1) @0)
6038   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6039  (simplify
6040   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6041   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6042        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6043        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6044        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6045        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6046        && !wi::neg_p (wi::to_wide (@1)))
6047    (cmp (bit_and @0 (convert (bit_not @1)))
6048         { build_zero_cst (TREE_TYPE (@0)); })))
6050  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6051  (simplify
6052   (cmp:c (bit_ior:c @0 @1) @1)
6053   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6055  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6056  (simplify
6057   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6058   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6059    (cmp @0 (bit_xor @1 (convert @2)))))
6061  (simplify
6062   (cmp (convert? addr@0) integer_zerop)
6063   (if (tree_single_nonzero_warnv_p (@0, NULL))
6064    { constant_boolean_node (cmp == NE_EXPR, type); }))
6066  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6067  (simplify
6068   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6069   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6071 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6072    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6073    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6074    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6075 (for cmp (eq ne)
6076      ncmp (ge lt)
6077  (for sgncmp (ge lt)
6078   (simplify
6079    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6080    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6081         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6082         && types_match (@0, @1))
6083     (ncmp (bit_xor @0 @1) @2)))))
6084 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6085    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6086 (for cmp (eq ne)
6087      ncmp (lt ge)
6088  (simplify
6089   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6090    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6091         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6092         && types_match (@0, @1))
6093     (ncmp (bit_xor @0 @1) @2))))
6095 /* If we have (A & C) == C where C is a power of 2, convert this into
6096    (A & C) != 0.  Similarly for NE_EXPR.  */
6097 (for cmp (eq ne)
6098      icmp (ne eq)
6099  (simplify
6100   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6101   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6103 #if GIMPLE
6104 /* From fold_binary_op_with_conditional_arg handle the case of
6105    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6106    compares simplify.  */
6107 (for cmp (simple_comparison)
6108  (simplify
6109   (cmp:c (cond @0 @1 @2) @3)
6110   /* Do not move possibly trapping operations into the conditional as this
6111      pessimizes code and causes gimplification issues when applied late.  */
6112   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6113        || !operation_could_trap_p (cmp, true, false, @3))
6114    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6115 #endif
6117 (for cmp (ge lt)
6118 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6119 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6120  (simplify
6121   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6122    (if (INTEGRAL_TYPE_P (type)
6123         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6124         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6125         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6126     (with
6127      {
6128        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6129      }
6130     (if (cmp == LT_EXPR)
6131      (bit_xor (convert (rshift @0 {shifter;})) @1)
6132      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6133 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6134 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6135  (simplify
6136   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6137    (if (INTEGRAL_TYPE_P (type)
6138         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6139         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6140         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6141     (with
6142      {
6143        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6144      }
6145     (if (cmp == GE_EXPR)
6146      (bit_xor (convert (rshift @0 {shifter;})) @1)
6147      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6149 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6150    convert this into a shift followed by ANDing with D.  */
6151 (simplify
6152  (cond
6153   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6154   INTEGER_CST@2 integer_zerop)
6155  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6156   (with {
6157      int shift = (wi::exact_log2 (wi::to_wide (@2))
6158                   - wi::exact_log2 (wi::to_wide (@1)));
6159    }
6160    (if (shift > 0)
6161     (bit_and
6162      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6163     (bit_and
6164      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6165      @2)))))
6167 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6168    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6169 (for cmp (eq ne)
6170      ncmp (ge lt)
6171  (simplify
6172   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6173   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6174        && type_has_mode_precision_p (TREE_TYPE (@0))
6175        && element_precision (@2) >= element_precision (@0)
6176        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6177    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6178     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6180 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6181    this into a right shift or sign extension followed by ANDing with C.  */
6182 (simplify
6183  (cond
6184   (lt @0 integer_zerop)
6185   INTEGER_CST@1 integer_zerop)
6186  (if (integer_pow2p (@1)
6187       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6188   (with {
6189     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6190    }
6191    (if (shift >= 0)
6192     (bit_and
6193      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6194      @1)
6195     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6196        sign extension followed by AND with C will achieve the effect.  */
6197     (bit_and (convert @0) @1)))))
6199 /* When the addresses are not directly of decls compare base and offset.
6200    This implements some remaining parts of fold_comparison address
6201    comparisons but still no complete part of it.  Still it is good
6202    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6203 (for cmp (simple_comparison)
6204  (simplify
6205   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6206   (with
6207    {
6208      poly_int64 off0, off1;
6209      tree base0, base1;
6210      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6211                                   off0, off1, GENERIC);
6212    }
6213    (if (equal == 1)
6214     (switch
6215      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6216       { constant_boolean_node (known_eq (off0, off1), type); })
6217      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6218       { constant_boolean_node (known_ne (off0, off1), type); })
6219      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6220       { constant_boolean_node (known_lt (off0, off1), type); })
6221      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6222       { constant_boolean_node (known_le (off0, off1), type); })
6223      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6224       { constant_boolean_node (known_ge (off0, off1), type); })
6225      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6226       { constant_boolean_node (known_gt (off0, off1), type); }))
6227     (if (equal == 0)
6228      (switch
6229       (if (cmp == EQ_EXPR)
6230        { constant_boolean_node (false, type); })
6231       (if (cmp == NE_EXPR)
6232        { constant_boolean_node (true, type); })))))))
6234 /* Simplify pointer equality compares using PTA.  */
6235 (for neeq (ne eq)
6236  (simplify
6237   (neeq @0 @1)
6238   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6239        && ptrs_compare_unequal (@0, @1))
6240    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6242 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6243    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6244    Disable the transform if either operand is pointer to function.
6245    This broke pr22051-2.c for arm where function pointer
6246    canonicalizaion is not wanted.  */
6248 (for cmp (ne eq)
6249  (simplify
6250   (cmp (convert @0) INTEGER_CST@1)
6251   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6252          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6253          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6254          /* Don't perform this optimization in GENERIC if @0 has reference
6255             type when sanitizing.  See PR101210.  */
6256          && !(GENERIC
6257               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6258               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6259         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6260             && POINTER_TYPE_P (TREE_TYPE (@1))
6261             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6262        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6263    (cmp @0 (convert @1)))))
6265 /* Non-equality compare simplifications from fold_binary  */
6266 (for cmp (lt gt le ge)
6267  /* Comparisons with the highest or lowest possible integer of
6268     the specified precision will have known values.  */
6269  (simplify
6270   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6271   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6272         || POINTER_TYPE_P (TREE_TYPE (@1))
6273         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6274        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6275    (with
6276     {
6277       tree cst = uniform_integer_cst_p (@1);
6278       tree arg1_type = TREE_TYPE (cst);
6279       unsigned int prec = TYPE_PRECISION (arg1_type);
6280       wide_int max = wi::max_value (arg1_type);
6281       wide_int signed_max = wi::max_value (prec, SIGNED);
6282       wide_int min = wi::min_value (arg1_type);
6283     }
6284     (switch
6285      (if (wi::to_wide (cst) == max)
6286       (switch
6287        (if (cmp == GT_EXPR)
6288         { constant_boolean_node (false, type); })
6289        (if (cmp == GE_EXPR)
6290         (eq @2 @1))
6291        (if (cmp == LE_EXPR)
6292         { constant_boolean_node (true, type); })
6293        (if (cmp == LT_EXPR)
6294         (ne @2 @1))))
6295      (if (wi::to_wide (cst) == min)
6296       (switch
6297        (if (cmp == LT_EXPR)
6298         { constant_boolean_node (false, type); })
6299        (if (cmp == LE_EXPR)
6300         (eq @2 @1))
6301        (if (cmp == GE_EXPR)
6302         { constant_boolean_node (true, type); })
6303        (if (cmp == GT_EXPR)
6304         (ne @2 @1))))
6305      (if (wi::to_wide (cst) == max - 1)
6306       (switch
6307        (if (cmp == GT_EXPR)
6308         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6309                                     wide_int_to_tree (TREE_TYPE (cst),
6310                                                       wi::to_wide (cst)
6311                                                       + 1)); }))
6312        (if (cmp == LE_EXPR)
6313         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6314                                     wide_int_to_tree (TREE_TYPE (cst),
6315                                                       wi::to_wide (cst)
6316                                                       + 1)); }))))
6317      (if (wi::to_wide (cst) == min + 1)
6318       (switch
6319        (if (cmp == GE_EXPR)
6320         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6321                                     wide_int_to_tree (TREE_TYPE (cst),
6322                                                       wi::to_wide (cst)
6323                                                       - 1)); }))
6324        (if (cmp == LT_EXPR)
6325         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6326                                     wide_int_to_tree (TREE_TYPE (cst),
6327                                                       wi::to_wide (cst)
6328                                                       - 1)); }))))
6329      (if (wi::to_wide (cst) == signed_max
6330           && TYPE_UNSIGNED (arg1_type)
6331           /* We will flip the signedness of the comparison operator
6332              associated with the mode of @1, so the sign bit is
6333              specified by this mode.  Check that @1 is the signed
6334              max associated with this sign bit.  */
6335           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6336           /* signed_type does not work on pointer types.  */
6337           && INTEGRAL_TYPE_P (arg1_type))
6338       /* The following case also applies to X < signed_max+1
6339          and X >= signed_max+1 because previous transformations.  */
6340       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6341        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6342         (switch
6343          (if (cst == @1 && cmp == LE_EXPR)
6344           (ge (convert:st @0) { build_zero_cst (st); }))
6345          (if (cst == @1 && cmp == GT_EXPR)
6346           (lt (convert:st @0) { build_zero_cst (st); }))
6347          (if (cmp == LE_EXPR)
6348           (ge (view_convert:st @0) { build_zero_cst (st); }))
6349          (if (cmp == GT_EXPR)
6350           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6352 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6353  /* If the second operand is NaN, the result is constant.  */
6354  (simplify
6355   (cmp @0 REAL_CST@1)
6356   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6357        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6358    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6359                             ? false : true, type); })))
6361 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6362 (simplify
6363   (unordered @0 @1)
6364   (switch
6365     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6366         { constant_boolean_node (true, type); })
6367     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6368         { constant_boolean_node (false, type); })))
6370 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6371 (simplify
6372   (ordered @0 @1)
6373   (switch
6374     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6375         { constant_boolean_node (false, type); })
6376     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6377         { constant_boolean_node (true, type); })))
6379 /* bool_var != 0 becomes bool_var.  */
6380 (simplify
6381  (ne @0 integer_zerop)
6382  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6383       && types_match (type, TREE_TYPE (@0)))
6384   (non_lvalue @0)))
6385 /* bool_var == 1 becomes bool_var.  */
6386 (simplify
6387  (eq @0 integer_onep)
6388  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6389       && types_match (type, TREE_TYPE (@0)))
6390   (non_lvalue @0)))
6391 /* Do not handle
6392    bool_var == 0 becomes !bool_var or
6393    bool_var != 1 becomes !bool_var
6394    here because that only is good in assignment context as long
6395    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6396    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6397    clearly less optimal and which we'll transform again in forwprop.  */
6399 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6400    where ~Y + 1 == pow2 and Z = ~Y.  */
6401 (for cst (VECTOR_CST INTEGER_CST)
6402  (for cmp (eq ne)
6403       icmp (le gt)
6404   (simplify
6405    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6406     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6407      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6408       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6409                          ? optab_vector : optab_default;
6410               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6411        (if (target_supports_op_p (utype, icmp, optab)
6412             || (optimize_vectors_before_lowering_p ()
6413                 && (!target_supports_op_p (type, cmp, optab)
6414                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6415         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6416          (icmp @0 { csts; })
6417          (icmp (view_convert:utype @0) { csts; })))))))))
6419 /* When one argument is a constant, overflow detection can be simplified.
6420    Currently restricted to single use so as not to interfere too much with
6421    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6422    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6423 (for cmp (lt le ge gt)
6424      out (gt gt le le)
6425  (simplify
6426   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6427   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6428        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6429        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6430        && wi::to_wide (@1) != 0
6431        && single_use (@2))
6432    (with {
6433      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6434      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6435     }
6436     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6437                                 wi::max_value (prec, sign)
6438                                 - wi::to_wide (@1)); })))))
6440 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6441    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6442    expects the long form, so we restrict the transformation for now.  */
6443 (for cmp (gt le)
6444  (simplify
6445   (cmp:c (minus@2 @0 @1) @0)
6446   (if (single_use (@2)
6447        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6448        && TYPE_UNSIGNED (TREE_TYPE (@0)))
6449    (cmp @1 @0))))
6451 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
6452 (for cmp (ge lt)
6453  (simplify
6454   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6455    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6456         && TYPE_UNSIGNED (TREE_TYPE (@0)))
6457     (cmp @1 @0))))
6459 /* Testing for overflow is unnecessary if we already know the result.  */
6460 /* A - B > A  */
6461 (for cmp (gt le)
6462      out (ne eq)
6463  (simplify
6464   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6465   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6466        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6467    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6468 /* A + B < A  */
6469 (for cmp (lt ge)
6470      out (ne eq)
6471  (simplify
6472   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6473   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6474        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6475    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6477 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6478    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
6479 (for cmp (lt ge)
6480      out (ne eq)
6481  (simplify
6482   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6483   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6484    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6485     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6487 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6488    is at least twice as wide as type of A and B, simplify to
6489    __builtin_mul_overflow (A, B, <unused>).  */
6490 (for cmp (eq ne)
6491  (simplify
6492   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6493        integer_zerop)
6494   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6495        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6496        && TYPE_UNSIGNED (TREE_TYPE (@0))
6497        && (TYPE_PRECISION (TREE_TYPE (@3))
6498            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6499        && tree_fits_uhwi_p (@2)
6500        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6501        && types_match (@0, @1)
6502        && type_has_mode_precision_p (TREE_TYPE (@0))
6503        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6504            != CODE_FOR_nothing))
6505    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6506     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6508 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
6509 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6510  (simplify
6511   (ovf (convert@2 @0) @1)
6512   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6513        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6514        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6515        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6516    (ovf @0 @1)))
6517  (simplify
6518   (ovf @1 (convert@2 @0))
6519   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6520        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6521        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6522        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6523    (ovf @1 @0))))
6525 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6526    are unsigned to x > (umax / cst).  Similarly for signed type, but
6527    in that case it needs to be outside of a range.  */
6528 (simplify
6529  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6530   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6531        && TYPE_MAX_VALUE (TREE_TYPE (@0))
6532        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6533        && int_fits_type_p (@1, TREE_TYPE (@0)))
6534    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6535     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6536     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6537      (if (integer_minus_onep (@1))
6538       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6539       (with
6540        {
6541          tree div = fold_convert (TREE_TYPE (@0), @1);
6542          tree lo = int_const_binop (TRUNC_DIV_EXPR,
6543                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6544          tree hi = int_const_binop (TRUNC_DIV_EXPR,
6545                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6546          tree etype = range_check_type (TREE_TYPE (@0));
6547          if (etype)
6548            {
6549              if (wi::neg_p (wi::to_wide (div)))
6550                std::swap (lo, hi);
6551              lo = fold_convert (etype, lo);
6552              hi = fold_convert (etype, hi);
6553              hi = int_const_binop (MINUS_EXPR, hi, lo);
6554            }
6555        }
6556        (if (etype)
6557         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6559 /* Simplification of math builtins.  These rules must all be optimizations
6560    as well as IL simplifications.  If there is a possibility that the new
6561    form could be a pessimization, the rule should go in the canonicalization
6562    section that follows this one.
6564    Rules can generally go in this section if they satisfy one of
6565    the following:
6567    - the rule describes an identity
6569    - the rule replaces calls with something as simple as addition or
6570      multiplication
6572    - the rule contains unary calls only and simplifies the surrounding
6573      arithmetic.  (The idea here is to exclude non-unary calls in which
6574      one operand is constant and in which the call is known to be cheap
6575      when the operand has that value.)  */
6577 (if (flag_unsafe_math_optimizations)
6578  /* Simplify sqrt(x) * sqrt(x) -> x.  */
6579  (simplify
6580   (mult (SQRT_ALL@1 @0) @1)
6581   (if (!tree_expr_maybe_signaling_nan_p (@0))
6582    @0))
6584  (for op (plus minus)
6585   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
6586   (simplify
6587    (op (rdiv @0 @1)
6588        (rdiv @2 @1))
6589    (rdiv (op @0 @2) @1)))
6591  (for cmp (lt le gt ge)
6592       neg_cmp (gt ge lt le)
6593   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
6594   (simplify
6595    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6596    (with
6597     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6598     (if (tem
6599          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6600               || (real_zerop (tem) && !real_zerop (@1))))
6601      (switch
6602       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6603        (cmp @0 { tem; }))
6604       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6605        (neg_cmp @0 { tem; })))))))
6607  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
6608  (for root (SQRT CBRT)
6609   (simplify
6610    (mult (root:s @0) (root:s @1))
6611     (root (mult @0 @1))))
6613  /* Simplify expN(x) * expN(y) -> expN(x+y). */
6614  (for exps (EXP EXP2 EXP10 POW10)
6615   (simplify
6616    (mult (exps:s @0) (exps:s @1))
6617     (exps (plus @0 @1))))
6619  /* Simplify a/root(b/c) into a*root(c/b).  */
6620  (for root (SQRT CBRT)
6621   (simplify
6622    (rdiv @0 (root:s (rdiv:s @1 @2)))
6623     (mult @0 (root (rdiv @2 @1)))))
6625  /* Simplify x/expN(y) into x*expN(-y).  */
6626  (for exps (EXP EXP2 EXP10 POW10)
6627   (simplify
6628    (rdiv @0 (exps:s @1))
6629     (mult @0 (exps (negate @1)))))
6631  (for logs (LOG LOG2 LOG10 LOG10)
6632       exps (EXP EXP2 EXP10 POW10)
6633   /* logN(expN(x)) -> x.  */
6634   (simplify
6635    (logs (exps @0))
6636    @0)
6637   /* expN(logN(x)) -> x.  */
6638   (simplify
6639    (exps (logs @0))
6640    @0))
6642  /* Optimize logN(func()) for various exponential functions.  We
6643     want to determine the value "x" and the power "exponent" in
6644     order to transform logN(x**exponent) into exponent*logN(x).  */
6645  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
6646       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
6647   (simplify
6648    (logs (exps @0))
6649    (if (SCALAR_FLOAT_TYPE_P (type))
6650     (with {
6651       tree x;
6652       switch (exps)
6653         {
6654         CASE_CFN_EXP:
6655           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
6656           x = build_real_truncate (type, dconst_e ());
6657           break;
6658         CASE_CFN_EXP2:
6659           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
6660           x = build_real (type, dconst2);
6661           break;
6662         CASE_CFN_EXP10:
6663         CASE_CFN_POW10:
6664           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
6665           {
6666             REAL_VALUE_TYPE dconst10;
6667             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6668             x = build_real (type, dconst10);
6669           }
6670           break;
6671         default:
6672           gcc_unreachable ();
6673         }
6674       }
6675      (mult (logs { x; }) @0)))))
6677  (for logs (LOG LOG
6678             LOG2 LOG2
6679             LOG10 LOG10)
6680       exps (SQRT CBRT)
6681   (simplify
6682    (logs (exps @0))
6683    (if (SCALAR_FLOAT_TYPE_P (type))
6684     (with {
6685       tree x;
6686       switch (exps)
6687         {
6688         CASE_CFN_SQRT:
6689           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
6690           x = build_real (type, dconsthalf);
6691           break;
6692         CASE_CFN_CBRT:
6693           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
6694           x = build_real_truncate (type, dconst_third ());
6695           break;
6696         default:
6697           gcc_unreachable ();
6698         }
6699       }
6700      (mult { x; } (logs @0))))))
6702  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
6703  (for logs (LOG LOG2 LOG10)
6704       pows (POW)
6705   (simplify
6706    (logs (pows @0 @1))
6707    (mult @1 (logs @0))))
6709  /* pow(C,x) -> exp(log(C)*x) if C > 0,
6710     or if C is a positive power of 2,
6711     pow(C,x) -> exp2(log2(C)*x).  */
6712 #if GIMPLE
6713  (for pows (POW)
6714       exps (EXP)
6715       logs (LOG)
6716       exp2s (EXP2)
6717       log2s (LOG2)
6718   (simplify
6719    (pows REAL_CST@0 @1)
6720    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6721         && real_isfinite (TREE_REAL_CST_PTR (@0))
6722         /* As libmvec doesn't have a vectorized exp2, defer optimizing
6723            the use_exp2 case until after vectorization.  It seems actually
6724            beneficial for all constants to postpone this until later,
6725            because exp(log(C)*x), while faster, will have worse precision
6726            and if x folds into a constant too, that is unnecessary
6727            pessimization.  */
6728         && canonicalize_math_after_vectorization_p ())
6729     (with {
6730        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6731        bool use_exp2 = false;
6732        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6733            && value->cl == rvc_normal)
6734          {
6735            REAL_VALUE_TYPE frac_rvt = *value;
6736            SET_REAL_EXP (&frac_rvt, 1);
6737            if (real_equal (&frac_rvt, &dconst1))
6738              use_exp2 = true;
6739          }
6740      }
6741      (if (!use_exp2)
6742       (if (optimize_pow_to_exp (@0, @1))
6743        (exps (mult (logs @0) @1)))
6744       (exp2s (mult (log2s @0) @1)))))))
6745 #endif
6747  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
6748  (for pows (POW)
6749       exps (EXP EXP2 EXP10 POW10)
6750       logs (LOG LOG2 LOG10 LOG10)
6751   (simplify
6752    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6753    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6754         && real_isfinite (TREE_REAL_CST_PTR (@0)))
6755     (exps (plus (mult (logs @0) @1) @2)))))
6757  (for sqrts (SQRT)
6758       cbrts (CBRT)
6759       pows (POW)
6760       exps (EXP EXP2 EXP10 POW10)
6761   /* sqrt(expN(x)) -> expN(x*0.5).  */
6762   (simplify
6763    (sqrts (exps @0))
6764    (exps (mult @0 { build_real (type, dconsthalf); })))
6765   /* cbrt(expN(x)) -> expN(x/3).  */
6766   (simplify
6767    (cbrts (exps @0))
6768    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6769   /* pow(expN(x), y) -> expN(x*y).  */
6770   (simplify
6771    (pows (exps @0) @1)
6772    (exps (mult @0 @1))))
6774  /* tan(atan(x)) -> x.  */
6775  (for tans (TAN)
6776       atans (ATAN)
6777   (simplify
6778    (tans (atans @0))
6779    @0)))
6781  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6782  (for sins (SIN)
6783       atans (ATAN)
6784       sqrts (SQRT)
6785       copysigns (COPYSIGN)
6786   (simplify
6787    (sins (atans:s @0))
6788    (with
6789      {
6790       REAL_VALUE_TYPE r_cst;
6791       build_sinatan_real (&r_cst, type);
6792       tree t_cst = build_real (type, r_cst);
6793       tree t_one = build_one_cst (type);
6794      }
6795     (if (SCALAR_FLOAT_TYPE_P (type))
6796      (cond (lt (abs @0) { t_cst; })
6797       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6798       (copysigns { t_one; } @0))))))
6800 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6801  (for coss (COS)
6802       atans (ATAN)
6803       sqrts (SQRT)
6804       copysigns (COPYSIGN)
6805   (simplify
6806    (coss (atans:s @0))
6807    (with
6808      {
6809       REAL_VALUE_TYPE r_cst;
6810       build_sinatan_real (&r_cst, type);
6811       tree t_cst = build_real (type, r_cst);
6812       tree t_one = build_one_cst (type);
6813       tree t_zero = build_zero_cst (type);
6814      }
6815     (if (SCALAR_FLOAT_TYPE_P (type))
6816      (cond (lt (abs @0) { t_cst; })
6817       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6818       (copysigns { t_zero; } @0))))))
6820  (if (!flag_errno_math)
6821   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6822   (for sinhs (SINH)
6823        atanhs (ATANH)
6824        sqrts (SQRT)
6825    (simplify
6826     (sinhs (atanhs:s @0))
6827     (with { tree t_one = build_one_cst (type); }
6828     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6830   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6831   (for coshs (COSH)
6832        atanhs (ATANH)
6833        sqrts (SQRT)
6834    (simplify
6835     (coshs (atanhs:s @0))
6836     (with { tree t_one = build_one_cst (type); }
6837     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6839 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
6840 (simplify
6841  (CABS (complex:C @0 real_zerop@1))
6842  (abs @0))
6844 /* trunc(trunc(x)) -> trunc(x), etc.  */
6845 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6846  (simplify
6847   (fns (fns @0))
6848   (fns @0)))
6849 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
6850 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6851  (simplify
6852   (fns integer_valued_real_p@0)
6853   @0))
6855 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
6856 (simplify
6857  (HYPOT:c @0 real_zerop@1)
6858  (abs @0))
6860 /* pow(1,x) -> 1.  */
6861 (simplify
6862  (POW real_onep@0 @1)
6863  @0)
6865 (simplify
6866  /* copysign(x,x) -> x.  */
6867  (COPYSIGN_ALL @0 @0)
6868  @0)
6870 (simplify
6871  /* copysign(x,-x) -> -x.  */
6872  (COPYSIGN_ALL @0 (negate@1 @0))
6873  @1)
6875 (simplify
6876  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
6877  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6878  (abs @0))
6880 (for scale (LDEXP SCALBN SCALBLN)
6881  /* ldexp(0, x) -> 0.  */
6882  (simplify
6883   (scale real_zerop@0 @1)
6884   @0)
6885  /* ldexp(x, 0) -> x.  */
6886  (simplify
6887   (scale @0 integer_zerop@1)
6888   @0)
6889  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
6890  (simplify
6891   (scale REAL_CST@0 @1)
6892   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6893    @0)))
6895 /* Canonicalization of sequences of math builtins.  These rules represent
6896    IL simplifications but are not necessarily optimizations.
6898    The sincos pass is responsible for picking "optimal" implementations
6899    of math builtins, which may be more complicated and can sometimes go
6900    the other way, e.g. converting pow into a sequence of sqrts.
6901    We only want to do these canonicalizations before the pass has run.  */
6903 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6904  /* Simplify tan(x) * cos(x) -> sin(x). */
6905  (simplify
6906   (mult:c (TAN:s @0) (COS:s @0))
6907    (SIN @0))
6909  /* Simplify x * pow(x,c) -> pow(x,c+1). */
6910  (simplify
6911   (mult:c @0 (POW:s @0 REAL_CST@1))
6912   (if (!TREE_OVERFLOW (@1))
6913    (POW @0 (plus @1 { build_one_cst (type); }))))
6915  /* Simplify sin(x) / cos(x) -> tan(x). */
6916  (simplify
6917   (rdiv (SIN:s @0) (COS:s @0))
6918    (TAN @0))
6920  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6921  (simplify
6922   (rdiv (SINH:s @0) (COSH:s @0))
6923    (TANH @0))
6925  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6926  (simplify
6927    (rdiv (TANH:s @0) (SINH:s @0))
6928    (rdiv {build_one_cst (type);} (COSH @0)))
6930  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6931  (simplify
6932   (rdiv (COS:s @0) (SIN:s @0))
6933    (rdiv { build_one_cst (type); } (TAN @0)))
6935  /* Simplify sin(x) / tan(x) -> cos(x). */
6936  (simplify
6937   (rdiv (SIN:s @0) (TAN:s @0))
6938   (if (! HONOR_NANS (@0)
6939        && ! HONOR_INFINITIES (@0))
6940    (COS @0)))
6942  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6943  (simplify
6944   (rdiv (TAN:s @0) (SIN:s @0))
6945   (if (! HONOR_NANS (@0)
6946        && ! HONOR_INFINITIES (@0))
6947    (rdiv { build_one_cst (type); } (COS @0))))
6949  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6950  (simplify
6951   (mult (POW:s @0 @1) (POW:s @0 @2))
6952    (POW @0 (plus @1 @2)))
6954  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6955  (simplify
6956   (mult (POW:s @0 @1) (POW:s @2 @1))
6957    (POW (mult @0 @2) @1))
6959  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6960  (simplify
6961   (mult (POWI:s @0 @1) (POWI:s @2 @1))
6962    (POWI (mult @0 @2) @1))
6964  /* Simplify pow(x,c) / x -> pow(x,c-1). */
6965  (simplify
6966   (rdiv (POW:s @0 REAL_CST@1) @0)
6967   (if (!TREE_OVERFLOW (@1))
6968    (POW @0 (minus @1 { build_one_cst (type); }))))
6970  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6971  (simplify
6972   (rdiv @0 (POW:s @1 @2))
6973    (mult @0 (POW @1 (negate @2))))
6975  (for sqrts (SQRT)
6976       cbrts (CBRT)
6977       pows (POW)
6978   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
6979   (simplify
6980    (sqrts (sqrts @0))
6981    (pows @0 { build_real (type, dconst_quarter ()); }))
6982   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
6983   (simplify
6984    (sqrts (cbrts @0))
6985    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6986   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
6987   (simplify
6988    (cbrts (sqrts @0))
6989    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6990   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
6991   (simplify
6992    (cbrts (cbrts tree_expr_nonnegative_p@0))
6993    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6994   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
6995   (simplify
6996    (sqrts (pows @0 @1))
6997    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6998   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
6999   (simplify
7000    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7001    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7002   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7003   (simplify
7004    (pows (sqrts @0) @1)
7005    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7006   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7007   (simplify
7008    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7009    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7010   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7011   (simplify
7012    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7013    (pows @0 (mult @1 @2))))
7015  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7016  (simplify
7017   (CABS (complex @0 @0))
7018   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7020  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7021  (simplify
7022   (HYPOT @0 @0)
7023   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7025  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7026  (for cexps (CEXP)
7027       exps (EXP)
7028       cexpis (CEXPI)
7029   (simplify
7030    (cexps compositional_complex@0)
7031    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7032     (complex
7033      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7034      (mult @1 (imagpart @2)))))))
7036 (if (canonicalize_math_p ())
7037  /* floor(x) -> trunc(x) if x is nonnegative.  */
7038  (for floors (FLOOR_ALL)
7039       truncs (TRUNC_ALL)
7040   (simplify
7041    (floors tree_expr_nonnegative_p@0)
7042    (truncs @0))))
7044 (match double_value_p
7045  @0
7046  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7047 (for froms (BUILT_IN_TRUNCL
7048             BUILT_IN_FLOORL
7049             BUILT_IN_CEILL
7050             BUILT_IN_ROUNDL
7051             BUILT_IN_NEARBYINTL
7052             BUILT_IN_RINTL)
7053      tos (BUILT_IN_TRUNC
7054           BUILT_IN_FLOOR
7055           BUILT_IN_CEIL
7056           BUILT_IN_ROUND
7057           BUILT_IN_NEARBYINT
7058           BUILT_IN_RINT)
7059  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7060  (if (optimize && canonicalize_math_p ())
7061   (simplify
7062    (froms (convert double_value_p@0))
7063    (convert (tos @0)))))
7065 (match float_value_p
7066  @0
7067  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7068 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7069             BUILT_IN_FLOORL BUILT_IN_FLOOR
7070             BUILT_IN_CEILL BUILT_IN_CEIL
7071             BUILT_IN_ROUNDL BUILT_IN_ROUND
7072             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7073             BUILT_IN_RINTL BUILT_IN_RINT)
7074      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7075           BUILT_IN_FLOORF BUILT_IN_FLOORF
7076           BUILT_IN_CEILF BUILT_IN_CEILF
7077           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7078           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7079           BUILT_IN_RINTF BUILT_IN_RINTF)
7080  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7081     if x is a float.  */
7082  (if (optimize && canonicalize_math_p ()
7083       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7084   (simplify
7085    (froms (convert float_value_p@0))
7086    (convert (tos @0)))))
7088 #if GIMPLE
7089 (match float16_value_p
7090  @0
7091  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7092 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7093             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7094             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7095             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7096             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7097             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7098             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7099             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7100      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7101           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7102           IFN_CEIL IFN_CEIL IFN_CEIL
7103           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7104           IFN_ROUND IFN_ROUND IFN_ROUND
7105           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7106           IFN_RINT IFN_RINT IFN_RINT
7107           IFN_SQRT IFN_SQRT IFN_SQRT)
7108  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7109     if x is a _Float16.  */
7110  (simplify
7111    (convert (froms (convert float16_value_p@0)))
7112      (if (optimize
7113           && types_match (type, TREE_TYPE (@0))
7114           && direct_internal_fn_supported_p (as_internal_fn (tos),
7115                                              type, OPTIMIZE_FOR_BOTH))
7116        (tos @0))))
7118 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7119    x,y is float value, similar for _Float16/double.  */
7120 (for copysigns (COPYSIGN_ALL)
7121  (simplify
7122   (convert (copysigns (convert@2 @0) (convert @1)))
7123    (if (optimize
7124        && !HONOR_SNANS (@2)
7125        && types_match (type, TREE_TYPE (@0))
7126        && types_match (type, TREE_TYPE (@1))
7127        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7128        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7129                                           type, OPTIMIZE_FOR_BOTH))
7130     (IFN_COPYSIGN @0 @1))))
7132 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7133      tos (IFN_FMA IFN_FMA IFN_FMA)
7134  (simplify
7135   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7136    (if (flag_unsafe_math_optimizations
7137        && optimize
7138        && FLOAT_TYPE_P (type)
7139        && FLOAT_TYPE_P (TREE_TYPE (@3))
7140        && types_match (type, TREE_TYPE (@0))
7141        && types_match (type, TREE_TYPE (@1))
7142        && types_match (type, TREE_TYPE (@2))
7143        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7144        && direct_internal_fn_supported_p (as_internal_fn (tos),
7145                                           type, OPTIMIZE_FOR_BOTH))
7146     (tos @0 @1 @2))))
7148 (for maxmin (max min)
7149  (simplify
7150   (convert (maxmin (convert@2 @0) (convert @1)))
7151    (if (optimize
7152        && FLOAT_TYPE_P (type)
7153        && FLOAT_TYPE_P (TREE_TYPE (@2))
7154        && types_match (type, TREE_TYPE (@0))
7155        && types_match (type, TREE_TYPE (@1))
7156        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7157     (maxmin @0 @1))))
7158 #endif
7160 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7161      tos (XFLOOR XCEIL XROUND XRINT)
7162  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7163  (if (optimize && canonicalize_math_p ())
7164   (simplify
7165    (froms (convert double_value_p@0))
7166    (tos @0))))
7168 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7169             XFLOOR XCEIL XROUND XRINT)
7170      tos (XFLOORF XCEILF XROUNDF XRINTF)
7171  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7172     if x is a float.  */
7173  (if (optimize && canonicalize_math_p ())
7174   (simplify
7175    (froms (convert float_value_p@0))
7176    (tos @0))))
7178 (if (canonicalize_math_p ())
7179  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7180  (for floors (IFLOOR LFLOOR LLFLOOR)
7181   (simplify
7182    (floors tree_expr_nonnegative_p@0)
7183    (fix_trunc @0))))
7185 (if (canonicalize_math_p ())
7186  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7187  (for fns (IFLOOR LFLOOR LLFLOOR
7188            ICEIL LCEIL LLCEIL
7189            IROUND LROUND LLROUND)
7190   (simplify
7191    (fns integer_valued_real_p@0)
7192    (fix_trunc @0)))
7193  (if (!flag_errno_math)
7194   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7195   (for rints (IRINT LRINT LLRINT)
7196    (simplify
7197     (rints integer_valued_real_p@0)
7198     (fix_trunc @0)))))
7200 (if (canonicalize_math_p ())
7201  (for ifn (IFLOOR ICEIL IROUND IRINT)
7202       lfn (LFLOOR LCEIL LROUND LRINT)
7203       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7204   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7205      sizeof (int) == sizeof (long).  */
7206   (if (TYPE_PRECISION (integer_type_node)
7207        == TYPE_PRECISION (long_integer_type_node))
7208    (simplify
7209     (ifn @0)
7210     (lfn:long_integer_type_node @0)))
7211   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7212      sizeof (long long) == sizeof (long).  */
7213   (if (TYPE_PRECISION (long_long_integer_type_node)
7214        == TYPE_PRECISION (long_integer_type_node))
7215    (simplify
7216     (llfn @0)
7217     (lfn:long_integer_type_node @0)))))
7219 /* cproj(x) -> x if we're ignoring infinities.  */
7220 (simplify
7221  (CPROJ @0)
7222  (if (!HONOR_INFINITIES (type))
7223    @0))
7225 /* If the real part is inf and the imag part is known to be
7226    nonnegative, return (inf + 0i).  */
7227 (simplify
7228  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7229  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7230   { build_complex_inf (type, false); }))
7232 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7233 (simplify
7234  (CPROJ (complex @0 REAL_CST@1))
7235  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7236   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7238 (for pows (POW)
7239      sqrts (SQRT)
7240      cbrts (CBRT)
7241  (simplify
7242   (pows @0 REAL_CST@1)
7243   (with {
7244     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7245     REAL_VALUE_TYPE tmp;
7246    }
7247    (switch
7248     /* pow(x,0) -> 1.  */
7249     (if (real_equal (value, &dconst0))
7250      { build_real (type, dconst1); })
7251     /* pow(x,1) -> x.  */
7252     (if (real_equal (value, &dconst1))
7253      @0)
7254     /* pow(x,-1) -> 1/x.  */
7255     (if (real_equal (value, &dconstm1))
7256      (rdiv { build_real (type, dconst1); } @0))
7257     /* pow(x,0.5) -> sqrt(x).  */
7258     (if (flag_unsafe_math_optimizations
7259          && canonicalize_math_p ()
7260          && real_equal (value, &dconsthalf))
7261      (sqrts @0))
7262     /* pow(x,1/3) -> cbrt(x).  */
7263     (if (flag_unsafe_math_optimizations
7264          && canonicalize_math_p ()
7265          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7266              real_equal (value, &tmp)))
7267      (cbrts @0))))))
7269 /* powi(1,x) -> 1.  */
7270 (simplify
7271  (POWI real_onep@0 @1)
7272  @0)
7274 (simplify
7275  (POWI @0 INTEGER_CST@1)
7276  (switch
7277   /* powi(x,0) -> 1.  */
7278   (if (wi::to_wide (@1) == 0)
7279    { build_real (type, dconst1); })
7280   /* powi(x,1) -> x.  */
7281   (if (wi::to_wide (@1) == 1)
7282    @0)
7283   /* powi(x,-1) -> 1/x.  */
7284   (if (wi::to_wide (@1) == -1)
7285    (rdiv { build_real (type, dconst1); } @0))))
7287 /* Narrowing of arithmetic and logical operations.
7289    These are conceptually similar to the transformations performed for
7290    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7291    term we want to move all that code out of the front-ends into here.  */
7293 /* Convert (outertype)((innertype0)a+(innertype1)b)
7294    into ((newtype)a+(newtype)b) where newtype
7295    is the widest mode from all of these.  */
7296 (for op (plus minus mult rdiv)
7297  (simplify
7298    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7299    /* If we have a narrowing conversion of an arithmetic operation where
7300       both operands are widening conversions from the same type as the outer
7301       narrowing conversion.  Then convert the innermost operands to a
7302       suitable unsigned type (to avoid introducing undefined behavior),
7303       perform the operation and convert the result to the desired type.  */
7304    (if (INTEGRAL_TYPE_P (type)
7305         && op != MULT_EXPR
7306         && op != RDIV_EXPR
7307         /* We check for type compatibility between @0 and @1 below,
7308            so there's no need to check that @2/@4 are integral types.  */
7309         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7310         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7311         /* The precision of the type of each operand must match the
7312            precision of the mode of each operand, similarly for the
7313            result.  */
7314         && type_has_mode_precision_p (TREE_TYPE (@1))
7315         && type_has_mode_precision_p (TREE_TYPE (@2))
7316         && type_has_mode_precision_p (type)
7317         /* The inner conversion must be a widening conversion.  */
7318         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7319         && types_match (@1, type)
7320         && (types_match (@1, @2)
7321             /* Or the second operand is const integer or converted const
7322                integer from valueize.  */
7323             || poly_int_tree_p (@4)))
7324      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7325        (op @1 (convert @2))
7326        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7327         (convert (op (convert:utype @1)
7328                      (convert:utype @2)))))
7329      (if (FLOAT_TYPE_P (type)
7330           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7331                == DECIMAL_FLOAT_TYPE_P (type))
7332       (with { tree arg0 = strip_float_extensions (@1);
7333               tree arg1 = strip_float_extensions (@2);
7334               tree itype = TREE_TYPE (@0);
7335               tree ty1 = TREE_TYPE (arg0);
7336               tree ty2 = TREE_TYPE (arg1);
7337               enum tree_code code = TREE_CODE (itype); }
7338         (if (FLOAT_TYPE_P (ty1)
7339              && FLOAT_TYPE_P (ty2))
7340          (with { tree newtype = type;
7341                  if (TYPE_MODE (ty1) == SDmode
7342                      || TYPE_MODE (ty2) == SDmode
7343                      || TYPE_MODE (type) == SDmode)
7344                    newtype = dfloat32_type_node;
7345                  if (TYPE_MODE (ty1) == DDmode
7346                      || TYPE_MODE (ty2) == DDmode
7347                      || TYPE_MODE (type) == DDmode)
7348                    newtype = dfloat64_type_node;
7349                  if (TYPE_MODE (ty1) == TDmode
7350                      || TYPE_MODE (ty2) == TDmode
7351                      || TYPE_MODE (type) == TDmode)
7352                    newtype = dfloat128_type_node; }
7353           (if ((newtype == dfloat32_type_node
7354                 || newtype == dfloat64_type_node
7355                 || newtype == dfloat128_type_node)
7356               && newtype == type
7357               && types_match (newtype, type))
7358             (op (convert:newtype @1) (convert:newtype @2))
7359             (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
7360                       newtype = ty1;
7361                     if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
7362                       newtype = ty2; }
7363                /* Sometimes this transformation is safe (cannot
7364                   change results through affecting double rounding
7365                   cases) and sometimes it is not.  If NEWTYPE is
7366                   wider than TYPE, e.g. (float)((long double)double
7367                   + (long double)double) converted to
7368                   (float)(double + double), the transformation is
7369                   unsafe regardless of the details of the types
7370                   involved; double rounding can arise if the result
7371                   of NEWTYPE arithmetic is a NEWTYPE value half way
7372                   between two representable TYPE values but the
7373                   exact value is sufficiently different (in the
7374                   right direction) for this difference to be
7375                   visible in ITYPE arithmetic.  If NEWTYPE is the
7376                   same as TYPE, however, the transformation may be
7377                   safe depending on the types involved: it is safe
7378                   if the ITYPE has strictly more than twice as many
7379                   mantissa bits as TYPE, can represent infinities
7380                   and NaNs if the TYPE can, and has sufficient
7381                   exponent range for the product or ratio of two
7382                   values representable in the TYPE to be within the
7383                   range of normal values of ITYPE.  */
7384               (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
7385                    && (flag_unsafe_math_optimizations
7386                        || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
7387                            && real_can_shorten_arithmetic (TYPE_MODE (itype),
7388                                                            TYPE_MODE (type))
7389                            && !excess_precision_type (newtype)))
7390                    && !types_match (itype, newtype))
7391                  (convert:type (op (convert:newtype @1)
7392                                    (convert:newtype @2)))
7393          )))) )
7394    ))
7397 /* This is another case of narrowing, specifically when there's an outer
7398    BIT_AND_EXPR which masks off bits outside the type of the innermost
7399    operands.   Like the previous case we have to convert the operands
7400    to unsigned types to avoid introducing undefined behavior for the
7401    arithmetic operation.  */
7402 (for op (minus plus)
7403  (simplify
7404   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7405   (if (INTEGRAL_TYPE_P (type)
7406        /* We check for type compatibility between @0 and @1 below,
7407           so there's no need to check that @1/@3 are integral types.  */
7408        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7409        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7410        /* The precision of the type of each operand must match the
7411           precision of the mode of each operand, similarly for the
7412           result.  */
7413        && type_has_mode_precision_p (TREE_TYPE (@0))
7414        && type_has_mode_precision_p (TREE_TYPE (@1))
7415        && type_has_mode_precision_p (type)
7416        /* The inner conversion must be a widening conversion.  */
7417        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7418        && types_match (@0, @1)
7419        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7420            <= TYPE_PRECISION (TREE_TYPE (@0)))
7421        && (wi::to_wide (@4)
7422            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7423                        true, TYPE_PRECISION (type))) == 0)
7424    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7425     (with { tree ntype = TREE_TYPE (@0); }
7426      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7427     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7428      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7429                (convert:utype @4))))))))
7431 /* Transform (@0 < @1 and @0 < @2) to use min,
7432    (@0 > @1 and @0 > @2) to use max */
7433 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7434      op    (lt      le      gt      ge      lt      le      gt      ge     )
7435      ext   (min     min     max     max     max     max     min     min    )
7436  (simplify
7437   (logic (op:cs @0 @1) (op:cs @0 @2))
7438   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7439        && TREE_CODE (@0) != INTEGER_CST)
7440    (op @0 (ext @1 @2)))))
7442 /* Max<bool0, bool1> -> bool0 | bool1
7443    Min<bool0, bool1> -> bool0 & bool1 */
7444 (for op    (max     min)
7445      logic (bit_ior bit_and)
7446  (simplify
7447   (op zero_one_valued_p@0 zero_one_valued_p@1)
7448   (logic @0 @1)))
7450 /* signbit(x) != 0 ? -x : x -> abs(x)
7451    signbit(x) == 0 ? -x : x -> -abs(x) */
7452 (for sign (SIGNBIT)
7453  (for neeq (ne eq)
7454   (simplify
7455    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
7456     (if (neeq == NE_EXPR)
7457      (abs @0)
7458      (negate (abs @0))))))
7460 (simplify
7461  /* signbit(x) -> 0 if x is nonnegative.  */
7462  (SIGNBIT tree_expr_nonnegative_p@0)
7463  { integer_zero_node; })
7465 (simplify
7466  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
7467  (SIGNBIT @0)
7468  (if (!HONOR_SIGNED_ZEROS (@0))
7469   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7471 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
7472 (for cmp (eq ne)
7473  (for op (plus minus)
7474       rop (minus plus)
7475   (simplify
7476    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7477    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7478         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7479         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7480         && !TYPE_SATURATING (TREE_TYPE (@0)))
7481     (with { tree res = int_const_binop (rop, @2, @1); }
7482      (if (TREE_OVERFLOW (res)
7483           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7484       { constant_boolean_node (cmp == NE_EXPR, type); }
7485       (if (single_use (@3))
7486        (cmp @0 { TREE_OVERFLOW (res)
7487                  ? drop_tree_overflow (res) : res; }))))))))
7488 (for cmp (lt le gt ge)
7489  (for op (plus minus)
7490       rop (minus plus)
7491   (simplify
7492    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7493    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7494         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7495     (with { tree res = int_const_binop (rop, @2, @1); }
7496      (if (TREE_OVERFLOW (res))
7497       {
7498         fold_overflow_warning (("assuming signed overflow does not occur "
7499                                 "when simplifying conditional to constant"),
7500                                WARN_STRICT_OVERFLOW_CONDITIONAL);
7501         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7502         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
7503         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7504                                   TYPE_SIGN (TREE_TYPE (@1)))
7505                         != (op == MINUS_EXPR);
7506         constant_boolean_node (less == ovf_high, type);
7507       }
7508       (if (single_use (@3))
7509        (with
7510         {
7511           fold_overflow_warning (("assuming signed overflow does not occur "
7512                                   "when changing X +- C1 cmp C2 to "
7513                                   "X cmp C2 -+ C1"),
7514                                  WARN_STRICT_OVERFLOW_COMPARISON);
7515         }
7516         (cmp @0 { res; })))))))))
7518 /* Canonicalizations of BIT_FIELD_REFs.  */
7520 (simplify
7521  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7522  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7524 (simplify
7525  (BIT_FIELD_REF (view_convert @0) @1 @2)
7526  (BIT_FIELD_REF @0 @1 @2))
7528 (simplify
7529  (BIT_FIELD_REF @0 @1 integer_zerop)
7530  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7531   (view_convert @0)))
7533 (simplify
7534  (BIT_FIELD_REF @0 @1 @2)
7535  (switch
7536   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7537        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7538    (switch
7539     (if (integer_zerop (@2))
7540      (view_convert (realpart @0)))
7541     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7542      (view_convert (imagpart @0)))))
7543   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7544        && INTEGRAL_TYPE_P (type)
7545        /* On GIMPLE this should only apply to register arguments.  */
7546        && (! GIMPLE || is_gimple_reg (@0))
7547        /* A bit-field-ref that referenced the full argument can be stripped.  */
7548        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7549             && integer_zerop (@2))
7550            /* Low-parts can be reduced to integral conversions.
7551               ???  The following doesn't work for PDP endian.  */
7552            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7553                /* But only do this after vectorization.  */
7554                && canonicalize_math_after_vectorization_p ()
7555                /* Don't even think about BITS_BIG_ENDIAN.  */
7556                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7557                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7558                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7559                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
7560                                             - TYPE_PRECISION (type))
7561                                          : 0)) == 0)))
7562    (convert @0))))
7564 /* Simplify vector extracts.  */
7566 (simplify
7567  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7568  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7569       && tree_fits_uhwi_p (TYPE_SIZE (type))
7570       && ((tree_to_uhwi (TYPE_SIZE (type))
7571            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7572           || (VECTOR_TYPE_P (type)
7573               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7574                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7575   (with
7576    {
7577      tree ctor = (TREE_CODE (@0) == SSA_NAME
7578                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7579      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7580      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7581      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7582      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7583    }
7584    (if (n != 0
7585         && (idx % width) == 0
7586         && (n % width) == 0
7587         && known_le ((idx + n) / width,
7588                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7589     (with
7590      {
7591        idx = idx / width;
7592        n = n / width;
7593        /* Constructor elements can be subvectors.  */
7594        poly_uint64 k = 1;
7595        if (CONSTRUCTOR_NELTS (ctor) != 0)
7596          {
7597            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7598            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7599              k = TYPE_VECTOR_SUBPARTS (cons_elem);
7600          }
7601        unsigned HOST_WIDE_INT elt, count, const_k;
7602      }
7603      (switch
7604       /* We keep an exact subset of the constructor elements.  */
7605       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7606        (if (CONSTRUCTOR_NELTS (ctor) == 0)
7607         { build_zero_cst (type); }
7608         (if (count == 1)
7609          (if (elt < CONSTRUCTOR_NELTS (ctor))
7610           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7611           { build_zero_cst (type); })
7612          /* We don't want to emit new CTORs unless the old one goes away.
7613             ???  Eventually allow this if the CTOR ends up constant or
7614             uniform.  */
7615          (if (single_use (@0))
7616           (with
7617             {
7618               vec<constructor_elt, va_gc> *vals;
7619               vec_alloc (vals, count);
7620               bool constant_p = true;
7621               tree res;
7622               for (unsigned i = 0;
7623                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7624                 {
7625                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7626                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7627                   if (!CONSTANT_CLASS_P (e))
7628                     constant_p = false;
7629                 }
7630               tree evtype = (types_match (TREE_TYPE (type),
7631                                           TREE_TYPE (TREE_TYPE (ctor)))
7632                              ? type
7633                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7634                                                   count * k));
7635               /* We used to build a CTOR in the non-constant case here
7636                  but that's not a GIMPLE value.  We'd have to expose this
7637                  operation somehow so the code generation can properly
7638                  split it out to a separate stmt.  */
7639               res = (constant_p ? build_vector_from_ctor (evtype, vals)
7640                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
7641             }
7642             (if (res)
7643              (view_convert { res; })))))))
7644       /* The bitfield references a single constructor element.  */
7645       (if (k.is_constant (&const_k)
7646            && idx + n <= (idx / const_k + 1) * const_k)
7647        (switch
7648         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7649          { build_zero_cst (type); })
7650         (if (n == const_k)
7651          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7652         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7653                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
7655 /* Simplify a bit extraction from a bit insertion for the cases with
7656    the inserted element fully covering the extraction or the insertion
7657    not touching the extraction.  */
7658 (simplify
7659  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7660  (with
7661   {
7662     unsigned HOST_WIDE_INT isize;
7663     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7664       isize = TYPE_PRECISION (TREE_TYPE (@1));
7665     else
7666       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7667   }
7668   (switch
7669    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
7670          || type_has_mode_precision_p (TREE_TYPE (@1)))
7671         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7672         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7673                       wi::to_wide (@ipos) + isize))
7674     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7675                                                  wi::to_wide (@rpos)
7676                                                  - wi::to_wide (@ipos)); }))
7677    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7678         && compare_tree_int (@rsize, isize) == 0)
7679     (convert @1))
7680    (if (wi::geu_p (wi::to_wide (@ipos),
7681                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
7682         || wi::geu_p (wi::to_wide (@rpos),
7683                       wi::to_wide (@ipos) + isize))
7684     (BIT_FIELD_REF @0 @rsize @rpos)))))
7686 (if (canonicalize_math_after_vectorization_p ())
7687  (for fmas (FMA)
7688   (simplify
7689    (fmas:c (negate @0) @1 @2)
7690    (IFN_FNMA @0 @1 @2))
7691   (simplify
7692    (fmas @0 @1 (negate @2))
7693    (IFN_FMS @0 @1 @2))
7694   (simplify
7695    (fmas:c (negate @0) @1 (negate @2))
7696    (IFN_FNMS @0 @1 @2))
7697   (simplify
7698    (negate (fmas@3 @0 @1 @2))
7699    (if (single_use (@3))
7700     (IFN_FNMS @0 @1 @2))))
7702  (simplify
7703   (IFN_FMS:c (negate @0) @1 @2)
7704   (IFN_FNMS @0 @1 @2))
7705  (simplify
7706   (IFN_FMS @0 @1 (negate @2))
7707   (IFN_FMA @0 @1 @2))
7708  (simplify
7709   (IFN_FMS:c (negate @0) @1 (negate @2))
7710   (IFN_FNMA @0 @1 @2))
7711  (simplify
7712   (negate (IFN_FMS@3 @0 @1 @2))
7713    (if (single_use (@3))
7714     (IFN_FNMA @0 @1 @2)))
7716  (simplify
7717   (IFN_FNMA:c (negate @0) @1 @2)
7718   (IFN_FMA @0 @1 @2))
7719  (simplify
7720   (IFN_FNMA @0 @1 (negate @2))
7721   (IFN_FNMS @0 @1 @2))
7722  (simplify
7723   (IFN_FNMA:c (negate @0) @1 (negate @2))
7724   (IFN_FMS @0 @1 @2))
7725  (simplify
7726   (negate (IFN_FNMA@3 @0 @1 @2))
7727   (if (single_use (@3))
7728    (IFN_FMS @0 @1 @2)))
7730  (simplify
7731   (IFN_FNMS:c (negate @0) @1 @2)
7732   (IFN_FMS @0 @1 @2))
7733  (simplify
7734   (IFN_FNMS @0 @1 (negate @2))
7735   (IFN_FNMA @0 @1 @2))
7736  (simplify
7737   (IFN_FNMS:c (negate @0) @1 (negate @2))
7738   (IFN_FMA @0 @1 @2))
7739  (simplify
7740   (negate (IFN_FNMS@3 @0 @1 @2))
7741   (if (single_use (@3))
7742    (IFN_FMA @0 @1 @2))))
7744 /* CLZ simplifications.  */
7745 (for clz (CLZ)
7746  (for op (eq ne)
7747       cmp (lt ge)
7748   (simplify
7749    (op (clz:s@2 @0) INTEGER_CST@1)
7750    (if (integer_zerop (@1) && single_use (@2))
7751     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
7752     (with { tree type0 = TREE_TYPE (@0);
7753             tree stype = signed_type_for (type0);
7754             HOST_WIDE_INT val = 0;
7755             /* Punt on hypothetical weird targets.  */
7756             if (clz == CFN_CLZ
7757                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7758                                               val) == 2
7759                 && val == 0)
7760               stype = NULL_TREE;
7761           }
7762      (if (stype)
7763       (cmp (convert:stype @0) { build_zero_cst (stype); })))
7764     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
7765     (with { bool ok = true;
7766             HOST_WIDE_INT val = 0;
7767             tree type0 = TREE_TYPE (@0);
7768             /* Punt on hypothetical weird targets.  */
7769             if (clz == CFN_CLZ
7770                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7771                                               val) == 2
7772                 && val == TYPE_PRECISION (type0) - 1)
7773               ok = false;
7774           }
7775      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7776       (op @0 { build_one_cst (type0); })))))))
7778 /* CTZ simplifications.  */
7779 (for ctz (CTZ)
7780  (for op (ge gt le lt)
7781       cmp (eq eq ne ne)
7782   (simplify
7783    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
7784    (op (ctz:s @0) INTEGER_CST@1)
7785     (with { bool ok = true;
7786             HOST_WIDE_INT val = 0;
7787             if (!tree_fits_shwi_p (@1))
7788               ok = false;
7789             else
7790               {
7791                 val = tree_to_shwi (@1);
7792                 /* Canonicalize to >= or <.  */
7793                 if (op == GT_EXPR || op == LE_EXPR)
7794                   {
7795                     if (val == HOST_WIDE_INT_MAX)
7796                       ok = false;
7797                     else
7798                       val++;
7799                   }
7800               }
7801             bool zero_res = false;
7802             HOST_WIDE_INT zero_val = 0;
7803             tree type0 = TREE_TYPE (@0);
7804             int prec = TYPE_PRECISION (type0);
7805             if (ctz == CFN_CTZ
7806                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7807                                               zero_val) == 2)
7808               zero_res = true;
7809           }
7810      (if (val <= 0)
7811       (if (ok && (!zero_res || zero_val >= val))
7812        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7813       (if (val >= prec)
7814        (if (ok && (!zero_res || zero_val < val))
7815         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7816        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7817         (cmp (bit_and @0 { wide_int_to_tree (type0,
7818                                              wi::mask (val, false, prec)); })
7819              { build_zero_cst (type0); })))))))
7820  (for op (eq ne)
7821   (simplify
7822    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
7823    (op (ctz:s @0) INTEGER_CST@1)
7824     (with { bool zero_res = false;
7825             HOST_WIDE_INT zero_val = 0;
7826             tree type0 = TREE_TYPE (@0);
7827             int prec = TYPE_PRECISION (type0);
7828             if (ctz == CFN_CTZ
7829                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7830                                               zero_val) == 2)
7831               zero_res = true;
7832           }
7833      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7834       (if (!zero_res || zero_val != wi::to_widest (@1))
7835        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7836       (if (!zero_res || zero_val < 0 || zero_val >= prec)
7837        (op (bit_and @0 { wide_int_to_tree (type0,
7838                                            wi::mask (tree_to_uhwi (@1) + 1,
7839                                                      false, prec)); })
7840            { wide_int_to_tree (type0,
7841                                wi::shifted_mask (tree_to_uhwi (@1), 1,
7842                                                  false, prec)); })))))))
7844 /* POPCOUNT simplifications.  */
7845 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
7846 (simplify
7847   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7848   (if (INTEGRAL_TYPE_P (type)
7849        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7850     (POPCOUNT (bit_ior @0 @1))))
7852 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
7853 (for popcount (POPCOUNT)
7854   (for cmp (le eq ne gt)
7855        rep (eq eq ne ne)
7856     (simplify
7857       (cmp (popcount @0) integer_zerop)
7858       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7860 /* popcount(bswap(x)) is popcount(x).  */
7861 (for popcount (POPCOUNT)
7862   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
7863               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
7864     (simplify
7865       (popcount (convert?@0 (bswap:s@1 @2)))
7866       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7867            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7868         (with { tree type0 = TREE_TYPE (@0);
7869                 tree type1 = TREE_TYPE (@1);
7870                 unsigned int prec0 = TYPE_PRECISION (type0);
7871                 unsigned int prec1 = TYPE_PRECISION (type1); }
7872           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
7873             (popcount (convert:type0 (convert:type1 @2)))))))))
7875 /* popcount(rotate(X Y)) is popcount(X).  */
7876 (for popcount (POPCOUNT)
7877   (for rot (lrotate rrotate)
7878     (simplify
7879       (popcount (convert?@0 (rot:s@1 @2 @3)))
7880       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7881            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
7882            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
7883         (with { tree type0 = TREE_TYPE (@0);
7884                 tree type1 = TREE_TYPE (@1);
7885                 unsigned int prec0 = TYPE_PRECISION (type0);
7886                 unsigned int prec1 = TYPE_PRECISION (type1); }
7887           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
7888             (popcount (convert:type0 @2))))))))
7890 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
7891 (simplify
7892   (bit_and (POPCOUNT @0) integer_onep)
7893   (PARITY @0))
7895 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
7896 (simplify
7897   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
7898   (plus (POPCOUNT @0) (POPCOUNT @1)))
7900 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
7901 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
7902 (for popcount (POPCOUNT)
7903   (for log1 (bit_and bit_ior)
7904        log2 (bit_ior bit_and)
7905     (simplify
7906       (minus (plus:s (popcount:s @0) (popcount:s @1))
7907              (popcount:s (log1:cs @0 @1)))
7908       (popcount (log2 @0 @1)))
7909     (simplify
7910       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
7911               (popcount:s @1))
7912       (popcount (log2 @0 @1)))))
7914 /* PARITY simplifications.  */
7915 /* parity(~X) is parity(X).  */
7916 (simplify
7917   (PARITY (bit_not @0))
7918   (PARITY @0))
7920 /* parity(bswap(x)) is parity(x).  */
7921 (for parity (PARITY)
7922   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
7923               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
7924     (simplify
7925       (parity (convert?@0 (bswap:s@1 @2)))
7926       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7927            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7928            && TYPE_PRECISION (TREE_TYPE (@0))
7929               >= TYPE_PRECISION (TREE_TYPE (@1)))
7930         (with { tree type0 = TREE_TYPE (@0);
7931                 tree type1 = TREE_TYPE (@1); }
7932           (parity (convert:type0 (convert:type1 @2))))))))
7934 /* parity(rotate(X Y)) is parity(X).  */
7935 (for parity (PARITY)
7936   (for rot (lrotate rrotate)
7937     (simplify
7938       (parity (convert?@0 (rot:s@1 @2 @3)))
7939       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7940            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7941            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
7942            && TYPE_PRECISION (TREE_TYPE (@0))
7943               >= TYPE_PRECISION (TREE_TYPE (@1)))
7944         (with { tree type0 = TREE_TYPE (@0); }
7945           (parity (convert:type0 @2)))))))
7947 /* parity(X)^parity(Y) is parity(X^Y).  */
7948 (simplify
7949   (bit_xor (PARITY:s @0) (PARITY:s @1))
7950   (PARITY (bit_xor @0 @1)))
7952 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
7953 (for func (POPCOUNT BSWAP FFS PARITY)
7954  (simplify
7955   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
7956   @3))
7958 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
7959    where CST is precision-1. */
7960 (for func (CLRSB)
7961  (simplify
7962   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
7963   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
7964    @4)))
7966 #if GIMPLE
7967 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
7968 (for func (CLZ)
7969  (simplify
7970   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
7971   (with { int val;
7972           internal_fn ifn = IFN_LAST;
7973           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
7974               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
7975                                             val) == 2)
7976             ifn = IFN_CLZ;
7977         }
7978    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
7979     (IFN_CLZ @3)))))
7981 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
7982 (for func (CTZ)
7983  (simplify
7984   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
7985   (with { int val;
7986           internal_fn ifn = IFN_LAST;
7987           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
7988               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
7989                                             val) == 2)
7990             ifn = IFN_CTZ;
7991         }
7992    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
7993     (IFN_CTZ @3)))))
7994 #endif
7996 /* Common POPCOUNT/PARITY simplifications.  */
7997 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
7998 (for pfun (POPCOUNT PARITY)
7999   (simplify
8000     (pfun @0)
8001     (if (INTEGRAL_TYPE_P (type))
8002      (with { wide_int nz = tree_nonzero_bits (@0); }
8003        (switch
8004          (if (nz == 1)
8005            (convert @0))
8006          (if (wi::popcount (nz) == 1)
8007            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8008              (convert (rshift:utype (convert:utype @0)
8009                                     { build_int_cst (integer_type_node,
8010                                                      wi::ctz (nz)); })))))))))
8012 #if GIMPLE
8013 /* 64- and 32-bits branchless implementations of popcount are detected:
8015    int popcount64c (uint64_t x)
8016    {
8017      x -= (x >> 1) & 0x5555555555555555ULL;
8018      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8019      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8020      return (x * 0x0101010101010101ULL) >> 56;
8021    }
8023    int popcount32c (uint32_t x)
8024    {
8025      x -= (x >> 1) & 0x55555555;
8026      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8027      x = (x + (x >> 4)) & 0x0f0f0f0f;
8028      return (x * 0x01010101) >> 24;
8029    }  */
8030 (simplify
8031  (rshift
8032   (mult
8033    (bit_and
8034     (plus:c
8035      (rshift @8 INTEGER_CST@5)
8036       (plus:c@8
8037        (bit_and @6 INTEGER_CST@7)
8038         (bit_and
8039          (rshift
8040           (minus@6 @0
8041            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8042           INTEGER_CST@10)
8043          INTEGER_CST@9)))
8044     INTEGER_CST@3)
8045    INTEGER_CST@2)
8046   INTEGER_CST@1)
8047   /* Check constants and optab.  */
8048   (with { unsigned prec = TYPE_PRECISION (type);
8049           int shift = (64 - prec) & 63;
8050           unsigned HOST_WIDE_INT c1
8051             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8052           unsigned HOST_WIDE_INT c2
8053             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8054           unsigned HOST_WIDE_INT c3
8055             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8056           unsigned HOST_WIDE_INT c4
8057             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8058    }
8059    (if (prec >= 16
8060         && prec <= 64
8061         && pow2p_hwi (prec)
8062         && TYPE_UNSIGNED (type)
8063         && integer_onep (@4)
8064         && wi::to_widest (@10) == 2
8065         && wi::to_widest (@5) == 4
8066         && wi::to_widest (@1) == prec - 8
8067         && tree_to_uhwi (@2) == c1
8068         && tree_to_uhwi (@3) == c2
8069         && tree_to_uhwi (@9) == c3
8070         && tree_to_uhwi (@7) == c3
8071         && tree_to_uhwi (@11) == c4)
8072     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8073                                          OPTIMIZE_FOR_BOTH))
8074      (convert (IFN_POPCOUNT:type @0))
8075      /* Try to do popcount in two halves.  PREC must be at least
8076         five bits for this to work without extension before adding.  */
8077      (with {
8078        tree half_type = NULL_TREE;
8079        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8080        int half_prec = 8;
8081        if (m.exists ()
8082            && m.require () != TYPE_MODE (type))
8083          {
8084            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8085            half_type = build_nonstandard_integer_type (half_prec, 1);
8086          }
8087        gcc_assert (half_prec > 2);
8088       }
8089       (if (half_type != NULL_TREE
8090            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8091                                               OPTIMIZE_FOR_BOTH))
8092        (convert (plus
8093          (IFN_POPCOUNT:half_type (convert @0))
8094          (IFN_POPCOUNT:half_type (convert (rshift @0
8095             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8097 /* __builtin_ffs needs to deal on many targets with the possible zero
8098    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8099    should lead to better code.  */
8100 (simplify
8101  (FFS tree_expr_nonzero_p@0)
8102  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8103       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8104                                          OPTIMIZE_FOR_SPEED))
8105   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8106    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8107 #endif
8109 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8110           BUILT_IN_FFSIMAX)
8111  /* __builtin_ffs (X) == 0 -> X == 0.
8112     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8113  (for cmp (eq ne)
8114   (simplify
8115    (cmp (ffs@2 @0) INTEGER_CST@1)
8116     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8117      (switch
8118       (if (integer_zerop (@1))
8119        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8120       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8121        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8122       (if (single_use (@2))
8123        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8124                                             wi::mask (tree_to_uhwi (@1),
8125                                                       false, prec)); })
8126             { wide_int_to_tree (TREE_TYPE (@0),
8127                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8128                                                   false, prec)); }))))))
8130  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8131  (for cmp (gt le)
8132       cmp2 (ne eq)
8133       cmp3 (eq ne)
8134       bit_op (bit_and bit_ior)
8135   (simplify
8136    (cmp (ffs@2 @0) INTEGER_CST@1)
8137     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8138      (switch
8139       (if (integer_zerop (@1))
8140        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8141       (if (tree_int_cst_sgn (@1) < 0)
8142        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8143       (if (wi::to_widest (@1) >= prec)
8144        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8145       (if (wi::to_widest (@1) == prec - 1)
8146        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8147                                     wi::shifted_mask (prec - 1, 1,
8148                                                       false, prec)); }))
8149       (if (single_use (@2))
8150        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8151                (cmp3 (bit_and @0
8152                               { wide_int_to_tree (TREE_TYPE (@0),
8153                                                   wi::mask (tree_to_uhwi (@1),
8154                                                   false, prec)); })
8155                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8157 #if GIMPLE
8159 /* Simplify:
8160      a = op a1
8161      r = cond ? a : b
8162      --> r = .COND_FN (cond, a, b)
8163 and,
8164     a = op a1
8165     r = cond ? b : a
8166     --> r = .COND_FN (~cond, b, a).  */
8168 (for uncond_op (UNCOND_UNARY)
8169      cond_op (COND_UNARY)
8170  (simplify
8171   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8172    (with { tree op_type = TREE_TYPE (@3); }
8173     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8174         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8175      (cond_op @0 @1 @2))))
8176  (simplify
8177   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8178    (with { tree op_type = TREE_TYPE (@3); }
8179     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8180         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8181      (cond_op (bit_not @0) @2 @1)))))
8183 /* Simplify:
8185      a = a1 op a2
8186      r = c ? a : b;
8188    to:
8190      r = c ? a1 op a2 : b;
8192    if the target can do it in one go.  This makes the operation conditional
8193    on c, so could drop potentially-trapping arithmetic, but that's a valid
8194    simplification if the result of the operation isn't needed.
8196    Avoid speculatively generating a stand-alone vector comparison
8197    on targets that might not support them.  Any target implementing
8198    conditional internal functions must support the same comparisons
8199    inside and outside a VEC_COND_EXPR.  */
8201 (for uncond_op (UNCOND_BINARY)
8202      cond_op (COND_BINARY)
8203  (simplify
8204   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8205   (with { tree op_type = TREE_TYPE (@4); }
8206    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8207         && is_truth_type_for (op_type, TREE_TYPE (@0))
8208         && single_use (@4))
8209     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8210  (simplify
8211   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8212   (with { tree op_type = TREE_TYPE (@4); }
8213    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8214         && is_truth_type_for (op_type, TREE_TYPE (@0))
8215         && single_use (@4))
8216     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8218 /* Same for ternary operations.  */
8219 (for uncond_op (UNCOND_TERNARY)
8220      cond_op (COND_TERNARY)
8221  (simplify
8222   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8223   (with { tree op_type = TREE_TYPE (@5); }
8224    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8225         && is_truth_type_for (op_type, TREE_TYPE (@0))
8226         && single_use (@5))
8227     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8228  (simplify
8229   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8230   (with { tree op_type = TREE_TYPE (@5); }
8231    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8232         && is_truth_type_for (op_type, TREE_TYPE (@0))
8233         && single_use (@5))
8234     (view_convert (cond_op (bit_not @0) @2 @3 @4
8235                   (view_convert:op_type @1)))))))
8236 #endif
8238 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8239    "else" value of an IFN_COND_*.  */
8240 (for cond_op (COND_BINARY)
8241  (simplify
8242   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8243   (with { tree op_type = TREE_TYPE (@3); }
8244    (if (element_precision (type) == element_precision (op_type))
8245     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8246  (simplify
8247   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8248   (with { tree op_type = TREE_TYPE (@5); }
8249    (if (inverse_conditions_p (@0, @2)
8250         && element_precision (type) == element_precision (op_type))
8251     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8253 /* Same for ternary operations.  */
8254 (for cond_op (COND_TERNARY)
8255  (simplify
8256   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8257   (with { tree op_type = TREE_TYPE (@4); }
8258    (if (element_precision (type) == element_precision (op_type))
8259     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8260  (simplify
8261   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8262   (with { tree op_type = TREE_TYPE (@6); }
8263    (if (inverse_conditions_p (@0, @2)
8264         && element_precision (type) == element_precision (op_type))
8265     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8267 /* Detect simplication for a conditional reduction where
8269    a = mask1 ? b : 0
8270    c = mask2 ? d + a : d
8272    is turned into
8274    c = mask1 && mask2 ? d + b : d.  */
8275 (simplify
8276   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8277    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8279 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8280    expressions like:
8282    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8283    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8285    If pointers are known not to wrap, B checks whether @1 bytes starting
8286    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8287    bytes.  A is more efficiently tested as:
8289    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8291    The equivalent expression for B is given by replacing @1 with @1 - 1:
8293    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8295    @0 and @2 can be swapped in both expressions without changing the result.
8297    The folds rely on sizetype's being unsigned (which is always true)
8298    and on its being the same width as the pointer (which we have to check).
8300    The fold replaces two pointer_plus expressions, two comparisons and
8301    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8302    the best case it's a saving of two operations.  The A fold retains one
8303    of the original pointer_pluses, so is a win even if both pointer_pluses
8304    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8305    used elsewhere, since all we end up doing is replacing a comparison with
8306    a pointer_plus.  We do still apply the fold under those circumstances
8307    though, in case applying it to other conditions eventually makes one of the
8308    pointer_pluses dead.  */
8309 (for ior (truth_orif truth_or bit_ior)
8310  (for cmp (le lt)
8311   (simplify
8312    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8313         (cmp:cs (pointer_plus@4 @2 @1) @0))
8314    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8315         && TYPE_OVERFLOW_WRAPS (sizetype)
8316         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8317     /* Calculate the rhs constant.  */
8318     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8319             offset_int rhs = off * 2; }
8320      /* Always fails for negative values.  */
8321      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8322       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8323          pick a canonical order.  This increases the chances of using the
8324          same pointer_plus in multiple checks.  */
8325       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8326               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8327        (if (cmp == LT_EXPR)
8328         (gt (convert:sizetype
8329              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8330                                      { swap_p ? @0 : @2; }))
8331             { rhs_tree; })
8332         (gt (convert:sizetype
8333              (pointer_diff:ssizetype
8334               (pointer_plus { swap_p ? @2 : @0; }
8335                             { wide_int_to_tree (sizetype, off); })
8336               { swap_p ? @0 : @2; }))
8337             { rhs_tree; })))))))))
8339 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8340    element of @1.  */
8341 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8342  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8343   (with { int i = single_nonzero_element (@1); }
8344    (if (i >= 0)
8345     (with { tree elt = vector_cst_elt (@1, i);
8346             tree elt_type = TREE_TYPE (elt);
8347             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8348             tree size = bitsize_int (elt_bits);
8349             tree pos = bitsize_int (elt_bits * i); }
8350      (view_convert
8351       (bit_and:elt_type
8352        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8353        { elt; })))))))
8355 /* Fold reduction of a single nonzero element constructor.  */
8356 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8357   (simplify (reduc (CONSTRUCTOR@0))
8358     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8359                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8360             tree elt = ctor_single_nonzero_element (ctor); }
8361       (if (elt
8362            && !HONOR_SNANS (type)
8363            && !HONOR_SIGNED_ZEROS (type))
8364         { elt; }))))
8366 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8367 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8368             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8369      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8370   (simplify (reduc (op @0 VECTOR_CST@1))
8371     (op (reduc:type @0) (reduc:type @1))))
8373 /* Simplify vector floating point operations of alternating sub/add pairs
8374    into using an fneg of a wider element type followed by a normal add.
8375    under IEEE 754 the fneg of the wider type will negate every even entry
8376    and when doing an add we get a sub of the even and add of every odd
8377    elements.  */
8378 (for plusminus (plus minus)
8379      minusplus (minus plus)
8380  (simplify
8381   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8382    (if (!VECTOR_INTEGER_TYPE_P (type)
8383         && !FLOAT_WORDS_BIG_ENDIAN
8384         /* plus is commutative, while minus is not, so :c can't be used.
8385            Do equality comparisons by hand and at the end pick the operands
8386            from the minus.  */
8387         && (operand_equal_p (@0, @2, 0)
8388             ? operand_equal_p (@1, @3, 0)
8389             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8390    (with
8391     {
8392       /* Build a vector of integers from the tree mask.  */
8393       vec_perm_builder builder;
8394     }
8395     (if (tree_to_vec_perm_builder (&builder, @4))
8396      (with
8397       {
8398         /* Create a vec_perm_indices for the integer vector.  */
8399         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8400         vec_perm_indices sel (builder, 2, nelts);
8401         machine_mode vec_mode = TYPE_MODE (type);
8402         machine_mode wide_mode;
8403         scalar_mode wide_elt_mode;
8404         poly_uint64 wide_nunits;
8405         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8406       }
8407       (if (VECTOR_MODE_P (vec_mode)
8408            && sel.series_p (0, 2, 0, 2)
8409            && sel.series_p (1, 2, nelts + 1, 2)
8410            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
8411            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
8412            && related_vector_mode (vec_mode, wide_elt_mode,
8413                                    wide_nunits).exists (&wide_mode))
8414        (with
8415         {
8416           tree stype
8417             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
8418                                               TYPE_UNSIGNED (type));
8419           tree ntype = build_vector_type_for_mode (stype, wide_mode);
8421           /* The format has to be a non-extended ieee format.  */
8422           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
8423           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
8424         }
8425         (if (TYPE_MODE (stype) != BLKmode
8426              && VECTOR_TYPE_P (ntype)
8427              && fmt_old != NULL
8428              && fmt_new != NULL)
8429          (with
8430           {
8431             /* If the target doesn't support v1xx vectors, try using
8432                scalar mode xx instead.  */
8433             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
8434                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
8435               ntype = stype;
8436           }
8437           (if (fmt_new->signbit_rw
8438                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
8439                && fmt_new->signbit_rw == fmt_new->signbit_ro
8440                && targetm.can_change_mode_class (TYPE_MODE (ntype),
8441                                                  TYPE_MODE (type), ALL_REGS)
8442                && ((optimize_vectors_before_lowering_p ()
8443                     && VECTOR_TYPE_P (ntype))
8444                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
8445            (if (plusminus == PLUS_EXPR)
8446             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
8447             (minus @0 (view_convert:type
8448                         (negate (view_convert:ntype @1))))))))))))))))
8450 (simplify
8451  (vec_perm @0 @1 VECTOR_CST@2)
8452  (with
8453   {
8454     tree op0 = @0, op1 = @1, op2 = @2;
8455     machine_mode result_mode = TYPE_MODE (type);
8456     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
8458     /* Build a vector of integers from the tree mask.  */
8459     vec_perm_builder builder;
8460   }
8461   (if (tree_to_vec_perm_builder (&builder, op2))
8462    (with
8463     {
8464       /* Create a vec_perm_indices for the integer vector.  */
8465       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8466       bool single_arg = (op0 == op1);
8467       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8468     }
8469     (if (sel.series_p (0, 1, 0, 1))
8470      { op0; }
8471      (if (sel.series_p (0, 1, nelts, 1))
8472       { op1; }
8473       (with
8474        {
8475          if (!single_arg)
8476            {
8477              if (sel.all_from_input_p (0))
8478                op1 = op0;
8479              else if (sel.all_from_input_p (1))
8480                {
8481                  op0 = op1;
8482                  sel.rotate_inputs (1);
8483                }
8484              else if (known_ge (poly_uint64 (sel[0]), nelts))
8485                {
8486                  std::swap (op0, op1);
8487                  sel.rotate_inputs (1);
8488                }
8489            }
8490          gassign *def;
8491          tree cop0 = op0, cop1 = op1;
8492          if (TREE_CODE (op0) == SSA_NAME
8493              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
8494              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8495            cop0 = gimple_assign_rhs1 (def);
8496          if (TREE_CODE (op1) == SSA_NAME
8497              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
8498              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8499            cop1 = gimple_assign_rhs1 (def);
8500          tree t;
8501        }
8502        (if ((TREE_CODE (cop0) == VECTOR_CST
8503              || TREE_CODE (cop0) == CONSTRUCTOR)
8504             && (TREE_CODE (cop1) == VECTOR_CST
8505                 || TREE_CODE (cop1) == CONSTRUCTOR)
8506             && (t = fold_vec_perm (type, cop0, cop1, sel)))
8507         { t; }
8508         (with
8509          {
8510            bool changed = (op0 == op1 && !single_arg);
8511            tree ins = NULL_TREE;
8512            unsigned at = 0;
8514            /* See if the permutation is performing a single element
8515               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
8516               in that case.  But only if the vector mode is supported,
8517               otherwise this is invalid GIMPLE.  */
8518            if (op_mode != BLKmode
8519                && (TREE_CODE (cop0) == VECTOR_CST
8520                    || TREE_CODE (cop0) == CONSTRUCTOR
8521                    || TREE_CODE (cop1) == VECTOR_CST
8522                    || TREE_CODE (cop1) == CONSTRUCTOR))
8523              {
8524                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
8525                if (insert_first_p)
8526                  {
8527                    /* After canonicalizing the first elt to come from the
8528                       first vector we only can insert the first elt from
8529                       the first vector.  */
8530                    at = 0;
8531                    if ((ins = fold_read_from_vector (cop0, sel[0])))
8532                      op0 = op1;
8533                  }
8534                /* The above can fail for two-element vectors which always
8535                   appear to insert the first element, so try inserting
8536                   into the second lane as well.  For more than two
8537                   elements that's wasted time.  */
8538                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
8539                  {
8540                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
8541                    for (at = 0; at < encoded_nelts; ++at)
8542                      if (maybe_ne (sel[at], at))
8543                        break;
8544                    if (at < encoded_nelts
8545                        && (known_eq (at + 1, nelts)
8546                            || sel.series_p (at + 1, 1, at + 1, 1)))
8547                      {
8548                        if (known_lt (poly_uint64 (sel[at]), nelts))
8549                          ins = fold_read_from_vector (cop0, sel[at]);
8550                        else
8551                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
8552                      }
8553                  }
8554              }
8556            /* Generate a canonical form of the selector.  */
8557            if (!ins && sel.encoding () != builder)
8558              {
8559                /* Some targets are deficient and fail to expand a single
8560                   argument permutation while still allowing an equivalent
8561                   2-argument version.  */
8562                tree oldop2 = op2;
8563                if (sel.ninputs () == 2
8564                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
8565                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8566                else
8567                  {
8568                    vec_perm_indices sel2 (builder, 2, nelts);
8569                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
8570                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
8571                    else
8572                      /* Not directly supported with either encoding,
8573                         so use the preferred form.  */
8574                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8575                  }
8576                if (!operand_equal_p (op2, oldop2, 0))
8577                  changed = true;
8578              }
8579          }
8580          (if (ins)
8581           (bit_insert { op0; } { ins; }
8582            { bitsize_int (at * vector_element_bits (type)); })
8583           (if (changed)
8584            (vec_perm { op0; } { op1; } { op2; }))))))))))))
8586 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
8588 (match vec_same_elem_p
8589  (vec_duplicate @0))
8591 (match vec_same_elem_p
8592  CONSTRUCTOR@0
8593  (if (TREE_CODE (@0) == SSA_NAME
8594       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
8596 (match vec_same_elem_p
8597  @0
8598  (if (uniform_vector_p (@0))))
8601 (simplify
8602  (vec_perm vec_same_elem_p@0 @0 @1)
8603  @0)
8605 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
8606 (simplify
8607  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8608  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8609   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8610 (simplify
8611  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8612  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8613   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8616 /* Merge
8617    c = VEC_PERM_EXPR <a, b, VCST0>;
8618    d = VEC_PERM_EXPR <c, c, VCST1>;
8619    to
8620    d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
8622 (simplify
8623  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
8624  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
8625   (with
8626    {
8627      machine_mode result_mode = TYPE_MODE (type);
8628      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
8629      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8630      vec_perm_builder builder0;
8631      vec_perm_builder builder1;
8632      vec_perm_builder builder2 (nelts, nelts, 1);
8633    }
8634    (if (tree_to_vec_perm_builder (&builder0, @3)
8635         && tree_to_vec_perm_builder (&builder1, @4))
8636     (with
8637      {
8638        vec_perm_indices sel0 (builder0, 2, nelts);
8639        vec_perm_indices sel1 (builder1, 1, nelts);
8641        for (int i = 0; i < nelts; i++)
8642          builder2.quick_push (sel0[sel1[i].to_constant ()]);
8644        vec_perm_indices sel2 (builder2, 2, nelts);
8646        tree op0 = NULL_TREE;
8647        /* If the new VEC_PERM_EXPR can't be handled but both
8648           original VEC_PERM_EXPRs can, punt.
8649           If one or both of the original VEC_PERM_EXPRs can't be
8650           handled and the new one can't be either, don't increase
8651           number of VEC_PERM_EXPRs that can't be handled.  */
8652        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
8653            || (single_use (@0)
8654                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
8655                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
8656                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
8657          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
8658      }
8659      (if (op0)
8660       (vec_perm @1 @2 { op0; })))))))
8663 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
8664    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
8665    constant which when multiplied by a power of 2 contains a unique value
8666    in the top 5 or 6 bits.  This is then indexed into a table which maps it
8667    to the number of trailing zeroes.  */
8668 (match (ctz_table_index @1 @2 @3)
8669   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
8671 (match (cond_expr_convert_p @0 @2 @3 @6)
8672  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
8673   (if (INTEGRAL_TYPE_P (type)
8674        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8675        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8676        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8677        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8678        && TYPE_PRECISION (TREE_TYPE (@0))
8679           == TYPE_PRECISION (TREE_TYPE (@2))
8680        && TYPE_PRECISION (TREE_TYPE (@0))
8681           == TYPE_PRECISION (TREE_TYPE (@3))
8682        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8683           signess when convert is truncation, but not ok for extension since
8684           it's sign_extend vs zero_extend.  */
8685        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8686            || (TYPE_UNSIGNED (TREE_TYPE (@2))
8687                == TYPE_UNSIGNED (TREE_TYPE (@3))))
8688        && single_use (@4)
8689        && single_use (@5))))
8691 (for bit_op (bit_and bit_ior bit_xor)
8692  (match (bitwise_induction_p @0 @2 @3)
8693   (bit_op:c
8694    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8695    @3)))
8697 (match (bitwise_induction_p @0 @2 @3)
8698  (bit_not
8699   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
8701 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
8702    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
8703 (simplify
8704   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
8705   (with { auto i = wi::neg (wi::to_wide (@2)); }
8706   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
8707     (if (wi::popcount (i) == 1
8708          && (wi::to_wide (@1)) == (i - 1))
8709       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8710         (bit_and @0 @1)
8711       (cond (le @0 @1) @0 (bit_and @0 @1))))))
8713 /* -x & 1 -> x & 1.  */
8714 (simplify 
8715  (bit_and (negate @0) integer_onep@1)
8716  (if (!TYPE_OVERFLOW_SANITIZED (type))
8717   (bit_and @0 @1)))
8719 /* Optimize
8720    c1 = VEC_PERM_EXPR (a, a, mask)
8721    c2 = VEC_PERM_EXPR (b, b, mask)
8722    c3 = c1 op c2
8723    -->
8724    c = a op b
8725    c3 = VEC_PERM_EXPR (c, c, mask)
8726    For all integer non-div operations.  */
8727 (for op (plus minus mult bit_and bit_ior bit_xor
8728          lshift rshift)
8729  (simplify
8730   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
8731    (if (VECTOR_INTEGER_TYPE_P (type))
8732     (vec_perm (op@3 @0 @1) @3 @2))))
8734 /* Similar for float arithmetic when permutation constant covers
8735    all vector elements.  */
8736 (for op (plus minus mult)
8737  (simplify
8738   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
8739    (if (VECTOR_FLOAT_TYPE_P (type)
8740         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8741     (with
8742      {
8743        tree perm_cst = @2;
8744        vec_perm_builder builder;
8745        bool full_perm_p = false;
8746        if (tree_to_vec_perm_builder (&builder, perm_cst))
8747          {
8748            unsigned HOST_WIDE_INT nelts;
8750            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8751            /* Create a vec_perm_indices for the VECTOR_CST.  */
8752            vec_perm_indices sel (builder, 1, nelts);
8754            /* Check if perm indices covers all vector elements.  */
8755            if (sel.encoding ().encoded_full_vector_p ())
8756              {
8757                auto_sbitmap seen (nelts);
8758                bitmap_clear (seen);
8760                unsigned HOST_WIDE_INT count = 0, i;
8762                for (i = 0; i < nelts; i++)
8763                  {
8764                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
8765                     break;
8766                    count++;
8767                  }
8768                full_perm_p = count == nelts;
8769              }
8770          }
8771       }
8772       (if (full_perm_p)
8773         (vec_perm (op@3 @0 @1) @3 @2))))))