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