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