testsuite: powerpc: fix dg-do run typo
[official-gcc.git] / gcc / match.pd
blobcf359b0ec0f0dd490854d6813a64f36a2edb3e79
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-2024 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91   IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function.  */
94 (define_operator_list UNCOND_BINARY
95   plus minus
96   mult trunc_div trunc_mod rdiv
97   min max
98   IFN_FMIN IFN_FMAX IFN_COPYSIGN
99   bit_and bit_ior bit_xor
100   lshift rshift)
101 (define_operator_list COND_BINARY
102   IFN_COND_ADD IFN_COND_SUB
103   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104   IFN_COND_MIN IFN_COND_MAX
105   IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107   IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109   IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110   IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111   IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112   IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113   IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114   IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115   IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations.  */
118 (define_operator_list UNCOND_TERNARY
119   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123   IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129   BUILT_IN_ATOMIC_FETCH_OR_16
130   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132   BUILT_IN_ATOMIC_FETCH_XOR_16
133   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135   BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140   BUILT_IN_SYNC_FETCH_AND_OR_16
141   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143   BUILT_IN_SYNC_FETCH_AND_XOR_16
144   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146   BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*.  */
148 (define_operator_list ATOMIC_FETCH_AND_N
149   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151   BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*.  */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156   BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159    plus conditionalize on tree_nop_conversion_p conversions.  */
160 (match (nop_convert @0)
161  (convert @0)
162  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
164  (view_convert @0)
165  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166       && known_eq (TYPE_VECTOR_SUBPARTS (type),
167                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
170 /* These are used by gimple_bitwise_inverted_equal_p to simplify
171    detection of BIT_NOT and comparisons. */
172 (match (bit_not_with_nop @0)
173  (bit_not @0))
174 (match (bit_not_with_nop @0)
175  (convert (bit_not @0))
176  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
177 (match (bit_xor_cst @0 @1)
178  (bit_xor @0 uniform_integer_cst_p@1))
179 (for cmp (tcc_comparison)
180  (match (maybe_cmp @0)
181   (cmp@0 @1 @2))
182  (match (maybe_cmp @0)
183   (convert (cmp@0 @1 @2))
184    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
186 /* `a ^ b` is another form of `a != b` when the type
187     is a 1bit precission integer.  */
188 (match (maybe_cmp @0)
189  (bit_xor@0 @1 @2)
190  (if (INTEGRAL_TYPE_P (type)
191       && TYPE_PRECISION (type) == 1)))
192 /* maybe_bit_not is used to match what
193    is acceptable for bitwise_inverted_equal_p. */
194 (match (maybe_bit_not @0)
195  (bit_not_with_nop@0 @1))
196 (match (maybe_bit_not @0)
197  (INTEGER_CST@0))
198 (match (maybe_bit_not @0)
199  (maybe_cmp@0 @1))
200 (match (maybe_bit_not @0)
201  (bit_xor_cst@0 @1 @2))
203 #if GIMPLE
204 (match (maybe_truncate @0)
205  (convert @0)
206  (if (INTEGRAL_TYPE_P (type)
207       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))))
208 #endif
210 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
211    ABSU_EXPR returns unsigned absolute value of the operand and the operand
212    of the ABSU_EXPR will have the corresponding signed type.  */
213 (simplify (abs (convert @0))
214  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
215       && !TYPE_UNSIGNED (TREE_TYPE (@0))
216       && element_precision (type) > element_precision (TREE_TYPE (@0)))
217   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
218    (convert (absu:utype @0)))))
220 #if GIMPLE
221 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
222 (simplify
223  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
224  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
225       && !TYPE_UNSIGNED (TREE_TYPE (@0))
226       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
227   (abs @0)))
228 #endif
230 /* Simplifications of operations with one constant operand and
231    simplifications to constants or single values.  */
233 (for op (plus pointer_plus minus bit_ior bit_xor)
234   (simplify
235     (op @0 integer_zerop)
236     (non_lvalue @0)))
238 /* 0 +p index -> (type)index */
239 (simplify
240  (pointer_plus integer_zerop @1)
241  (non_lvalue (convert @1)))
243 /* ptr - 0 -> (type)ptr */
244 (simplify
245  (pointer_diff @0 integer_zerop)
246  (convert @0))
248 /* See if ARG1 is zero and X + ARG1 reduces to X.
249    Likewise if the operands are reversed.  */
250 (simplify
251  (plus:c @0 real_zerop@1)
252  (if (fold_real_zero_addition_p (type, @0, @1, 0))
253   (non_lvalue @0)))
255 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
256 (simplify
257  (minus @0 real_zerop@1)
258  (if (fold_real_zero_addition_p (type, @0, @1, 1))
259   (non_lvalue @0)))
261 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
262    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
263    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
264    if not -frounding-math.  For sNaNs the first operation would raise
265    exceptions but turn the result into qNan, so the second operation
266    would not raise it.   */
267 (for inner_op (plus minus)
268  (for outer_op (plus minus)
269   (simplify
270    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
271     (if (real_zerop (@1)
272          && real_zerop (@2)
273          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
274      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
275                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
276              bool outer_plus
277                = ((outer_op == PLUS_EXPR)
278                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
279       (if (outer_plus && !inner_plus)
280        (outer_op @0 @2)
281        @3))))))
283 /* Simplify x - x.
284    This is unsafe for certain floats even in non-IEEE formats.
285    In IEEE, it is unsafe because it does wrong for NaNs.
286    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
287    Also note that operand_equal_p is always false if an operand
288    is volatile.  */
289 (simplify
290  (minus @0 @0)
291  (if (!FLOAT_TYPE_P (type)
292       || (!tree_expr_maybe_nan_p (@0)
293           && !tree_expr_maybe_infinite_p (@0)
294           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
295               || !HONOR_SIGNED_ZEROS (type))))
296   { build_zero_cst (type); }))
297 (simplify
298  (pointer_diff @@0 @0)
299  { build_zero_cst (type); })
301 (simplify
302  (mult @0 integer_zerop@1)
303  @1)
305 /* -x == x -> x == 0 */
306 (for cmp (eq ne)
307  (simplify
308   (cmp:c @0 (negate @0))
309    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
310         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
311     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
313 /* Maybe fold x * 0 to 0.  The expressions aren't the same
314    when x is NaN, since x * 0 is also NaN.  Nor are they the
315    same in modes with signed zeros, since multiplying a
316    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
317    since x * 0 is NaN.  */
318 (simplify
319  (mult @0 real_zerop@1)
320  (if (!tree_expr_maybe_nan_p (@0)
321       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
322       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
323   @1))
325 /* In IEEE floating point, x*1 is not equivalent to x for snans.
326    Likewise for complex arithmetic with signed zeros.  */
327 (simplify
328  (mult @0 real_onep)
329  (if (!tree_expr_maybe_signaling_nan_p (@0)
330       && (!HONOR_SIGNED_ZEROS (type)
331           || !COMPLEX_FLOAT_TYPE_P (type)))
332   (non_lvalue @0)))
334 /* Transform x * -1.0 into -x.  */
335 (simplify
336  (mult @0 real_minus_onep)
337   (if (!tree_expr_maybe_signaling_nan_p (@0)
338        && (!HONOR_SIGNED_ZEROS (type)
339            || !COMPLEX_FLOAT_TYPE_P (type)))
340    (negate @0)))
342 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
343    unless the target has native support for the former but not the latter.  */
344 (simplify
345  (mult @0 VECTOR_CST@1)
346  (if (initializer_each_zero_or_onep (@1)
347       && !HONOR_SNANS (type)
348       && !HONOR_SIGNED_ZEROS (type))
349   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
350    (if (itype
351         && (!VECTOR_MODE_P (TYPE_MODE (type))
352             || (VECTOR_MODE_P (TYPE_MODE (itype))
353                 && optab_handler (and_optab,
354                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
355     (view_convert (bit_and:itype (view_convert @0)
356                                  (ne @1 { build_zero_cst (type); })))))))
358 /* In SWAR (SIMD within a register) code a signed comparison of packed data
359    can be constructed with a particular combination of shift, bitwise and,
360    and multiplication by constants.  If that code is vectorized we can
361    convert this pattern into a more efficient vector comparison.  */
362 (simplify
363  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
364             uniform_integer_cst_p@2)
365     uniform_integer_cst_p@3)
366  (with {
367    tree rshift_cst = uniform_integer_cst_p (@1);
368    tree bit_and_cst = uniform_integer_cst_p (@2);
369    tree mult_cst = uniform_integer_cst_p (@3);
370   }
371   /* Make sure we're working with vectors and uniform vector constants.  */
372   (if (VECTOR_TYPE_P (type)
373        && tree_fits_uhwi_p (rshift_cst)
374        && tree_fits_uhwi_p (mult_cst)
375        && tree_fits_uhwi_p (bit_and_cst))
376    /* Compute what constants would be needed for this to represent a packed
377       comparison based on the shift amount denoted by RSHIFT_CST.  */
378    (with {
379      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
380      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
381      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
382      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
383      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
384      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
385      mult_i = tree_to_uhwi (mult_cst);
386      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
387      bit_and_i = tree_to_uhwi (bit_and_cst);
388      target_bit_and_i = 0;
390      /* The bit pattern in BIT_AND_I should be a mask for the least
391         significant bit of each packed element that is CMP_BITS wide.  */
392      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
393        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
394     }
395     (if ((exact_log2 (cmp_bits_i)) >= 0
396          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
397          && multiple_p (vec_bits, cmp_bits_i)
398          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
399          && target_mult_i == mult_i
400          && target_bit_and_i == bit_and_i)
401      /* Compute the vector shape for the comparison and check if the target is
402         able to expand the comparison with that type.  */
403      (with {
404        /* We're doing a signed comparison.  */
405        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
406        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
407        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
408        tree vec_truth_type = truth_type_for (vec_cmp_type);
409        tree zeros = build_zero_cst (vec_cmp_type);
410        tree ones = build_all_ones_cst (vec_cmp_type);
411       }
412       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
413            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
414        (view_convert:type (vec_cond (lt:vec_truth_type
415                                      (view_convert:vec_cmp_type @0)
416                                      { zeros; })
417                            { ones; } { zeros; })))))))))
419 (for cmp (gt ge lt le)
420      outp (convert convert negate negate)
421      outn (negate negate convert convert)
422  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
423  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
424  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
425  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
426  (simplify
427   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
428   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
429    (outp (abs @0))))
430  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
431  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
432  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
433  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
434  (simplify
435   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
436   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
437    (outn (abs @0)))))
439 /* Transform X * copysign (1.0, X) into abs(X). */
440 (simplify
441  (mult:c @0 (COPYSIGN_ALL real_onep @0))
442  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
443   (abs @0)))
445 /* Transform X * copysign (1.0, -X) into -abs(X). */
446 (simplify
447  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
448  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
449   (negate (abs @0))))
451 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
452 (simplify
453  (COPYSIGN_ALL REAL_CST@0 @1)
454  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
455   (COPYSIGN_ALL (negate @0) @1)))
457 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
458    tree-ssa-math-opts.cc does the corresponding optimization for
459    unconditional multiplications (via xorsign).  */
460 (simplify
461  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
462  (with { tree signs = sign_mask_for (type); }
463   (if (signs)
464    (with { tree inttype = TREE_TYPE (signs); }
465     (view_convert:type
466      (IFN_COND_XOR:inttype @0
467       (view_convert:inttype @1)
468       (bit_and (view_convert:inttype @2) { signs; })
469       (view_convert:inttype @3)))))))
471 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
472 (simplify
473  (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
474  (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
475   (abs @0)))
477 /* X * 1, X / 1 -> X.  */
478 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
479   (simplify
480     (op @0 integer_onep)
481     (non_lvalue @0)))
483 /* (A / (1 << B)) -> (A >> B).
484    Only for unsigned A.  For signed A, this would not preserve rounding
485    toward zero.
486    For example: (-1 / ( 1 << B)) !=  -1 >> B.
487    Also handle widening conversions, like:
488    (A / (unsigned long long) (1U << B)) -> (A >> B)
489    or
490    (A / (unsigned long long) (1 << B)) -> (A >> B).
491    If the left shift is signed, it can be done only if the upper bits
492    of A starting from shift's type sign bit are zero, as
493    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
494    so it is valid only if A >> 31 is zero.  */
495 (simplify
496  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
497  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
498       && (!VECTOR_TYPE_P (type)
499           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
500           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
501       && (useless_type_conversion_p (type, TREE_TYPE (@1))
502           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
503               && (TYPE_UNSIGNED (TREE_TYPE (@1))
504                   || (element_precision (type)
505                       == element_precision (TREE_TYPE (@1)))
506                   || (INTEGRAL_TYPE_P (type)
507                       && (tree_nonzero_bits (@0)
508                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
509                                       true,
510                                       element_precision (type))) == 0)))))
511    (if (!VECTOR_TYPE_P (type)
512         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
513         && element_precision (TREE_TYPE (@3)) < element_precision (type))
514     (convert (rshift @3 @2))
515     (rshift @0 @2))))
517 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
518    undefined behavior in constexpr evaluation, and assuming that the division
519    traps enables better optimizations than these anyway.  */
520 (for div (trunc_div ceil_div floor_div round_div exact_div)
521  /* 0 / X is always zero.  */
522  (simplify
523   (div integer_zerop@0 @1)
524   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
525   (if (!integer_zerop (@1))
526    @0))
527  /* X / -1 is -X.  */
528  (simplify
529   (div @0 integer_minus_onep@1)
530   (if (!TYPE_UNSIGNED (type))
531    (negate @0)))
532  /* X / bool_range_Y is X.  */ 
533  (simplify
534   (div @0 SSA_NAME@1)
535   (if (INTEGRAL_TYPE_P (type)
536        && ssa_name_has_boolean_range (@1)
537        && !flag_non_call_exceptions)
538    @0))
539  /* X / X is one.  */
540  (simplify
541   (div @0 @0)
542   /* But not for 0 / 0 so that we can get the proper warnings and errors.
543      And not for _Fract types where we can't build 1.  */
544   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
545        && !integer_zerop (@0)
546        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
547    { build_one_cst (type); }))
548  /* X / abs (X) is X < 0 ? -1 : 1.  */
549  (simplify
550    (div:C @0 (abs @0))
551    (if (INTEGRAL_TYPE_P (type)
552         && TYPE_OVERFLOW_UNDEFINED (type)
553         && !integer_zerop (@0)
554         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
555     (cond (lt @0 { build_zero_cst (type); })
556           { build_minus_one_cst (type); } { build_one_cst (type); })))
557  /* X / -X is -1.  */
558  (simplify
559    (div:C @0 (negate @0))
560    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
561         && TYPE_OVERFLOW_UNDEFINED (type)
562         && !integer_zerop (@0)
563         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
564     { build_minus_one_cst (type); })))
566 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
567    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
568    for MOD instead of DIV.  */
569 (for floor_divmod (floor_div floor_mod)
570      trunc_divmod (trunc_div trunc_mod)
571  (simplify
572   (floor_divmod @0 @1)
573   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
574        && TYPE_UNSIGNED (type))
575    (trunc_divmod @0 @1))))
577 /* 1 / X -> X == 1 for unsigned integer X.
578    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
579    But not for 1 / 0 so that we can get proper warnings and errors,
580    and not for 1-bit integers as they are edge cases better handled
581    elsewhere.  Delay the conversion of the signed division until late
582    because `1 / X` is simplier to handle than the resulting COND_EXPR.  */
583 (simplify
584  (trunc_div integer_onep@0 @1)
585  (if (INTEGRAL_TYPE_P (type)
586       && TYPE_PRECISION (type) > 1
587       && !integer_zerop (@1)
588       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
589   (if (TYPE_UNSIGNED (type))
590    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
591    (if (!canonicalize_math_p ())
592     (with { tree utype = unsigned_type_for (type); }
593      (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
594                 { build_int_cst (utype, 2); })
595       @1 { build_zero_cst (type); }))))))
597 /* Combine two successive divisions.  Note that combining ceil_div
598    and floor_div is trickier and combining round_div even more so.  */
599 (for div (trunc_div exact_div)
600  (simplify
601   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
602   (with {
603     wi::overflow_type overflow;
604     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
605                             TYPE_SIGN (type), &overflow);
606    }
607    (if (div == EXACT_DIV_EXPR
608         || optimize_successive_divisions_p (@2, @3))
609     (if (!overflow)
610      (div @0 { wide_int_to_tree (type, mul); })
611      (if (TYPE_UNSIGNED (type)
612           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
613       { build_zero_cst (type); }))))))
615 /* Combine successive multiplications.  Similar to above, but handling
616    overflow is different.  */
617 (simplify
618  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
619  (with {
620    wi::overflow_type overflow;
621    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
622                            TYPE_SIGN (type), &overflow);
623   }
624   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
625      otherwise undefined overflow implies that @0 must be zero.  */
626   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
627    (mult @0 { wide_int_to_tree (type, mul); }))))
629 /* Similar to above, but there could be an extra add/sub between
630    successive multuiplications.  */
631 (simplify
632  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
633  (with {
634    bool overflowed = true;
635    wi::overflow_type ovf1, ovf2;
636    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
637                            TYPE_SIGN (type), &ovf1);
638    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
639                            TYPE_SIGN (type), &ovf2);
640   if (TYPE_OVERFLOW_UNDEFINED (type))
641     {
642 #if GIMPLE
643       int_range_max vr0;
644       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
645           && get_global_range_query ()->range_of_expr (vr0, @4)
646           && !vr0.varying_p () && !vr0.undefined_p ())
647         {
648           wide_int wmin0 = vr0.lower_bound ();
649           wide_int wmax0 = vr0.upper_bound ();
650           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
651           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
652           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
653             {
654               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
655               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
656               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
657                 overflowed = false;
658             }
659         }
660 #endif
661     }
662   else
663    overflowed = false;
665   /* Skip folding on overflow.  */
666   (if (!overflowed)
667    (plus (mult @0 { wide_int_to_tree (type, mul); })
668          { wide_int_to_tree (type, add); }))))
670 /* Similar to above, but a multiplication between successive additions.  */
671 (simplify
672  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
673  (with {
674    bool overflowed = true;
675    wi::overflow_type ovf1;
676    wi::overflow_type ovf2;
677    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
678                            TYPE_SIGN (type), &ovf1);
679    wide_int add = wi::add (mul, wi::to_wide (@3),
680                            TYPE_SIGN (type), &ovf2);
681   if (TYPE_OVERFLOW_UNDEFINED (type))
682     {
683 #if GIMPLE
684       int_range_max vr0;
685       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
686           && get_global_range_query ()->range_of_expr (vr0, @0)
687           && !vr0.varying_p () && !vr0.undefined_p ())
688         {
689           wide_int wmin0 = vr0.lower_bound ();
690           wide_int wmax0 = vr0.upper_bound ();
691           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
692           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
693           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
694             {
695               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
696               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
697               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
698                 overflowed = false;
699             }
700         }
701 #endif
702     }
703   else
704    overflowed = false;
706   /* Skip folding on overflow.  */
707   (if (!overflowed)
708    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
710 /* Optimize A / A to 1.0 if we don't care about
711    NaNs or Infinities.  */
712 (simplify
713  (rdiv @0 @0)
714  (if (FLOAT_TYPE_P (type)
715       && ! HONOR_NANS (type)
716       && ! HONOR_INFINITIES (type))
717   { build_one_cst (type); }))
719 /* Optimize -A / A to -1.0 if we don't care about
720    NaNs or Infinities.  */
721 (simplify
722  (rdiv:C @0 (negate @0))
723  (if (FLOAT_TYPE_P (type)
724       && ! HONOR_NANS (type)
725       && ! HONOR_INFINITIES (type))
726   { build_minus_one_cst (type); }))
728 /* PR71078: x / abs(x) -> copysign (1.0, x) */
729 (simplify
730  (rdiv:C (convert? @0) (convert? (abs @0)))
731   (if (SCALAR_FLOAT_TYPE_P (type)
732        && ! HONOR_NANS (type)
733        && ! HONOR_INFINITIES (type))
734    (switch
735     (if (types_match (type, float_type_node))
736      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
737     (if (types_match (type, double_type_node))
738      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
739     (if (types_match (type, long_double_type_node))
740      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
742 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
743 (simplify
744  (rdiv @0 real_onep)
745  (if (!tree_expr_maybe_signaling_nan_p (@0))
746   (non_lvalue @0)))
748 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
749 (simplify
750  (rdiv @0 real_minus_onep)
751  (if (!tree_expr_maybe_signaling_nan_p (@0))
752   (negate @0)))
754 (if (flag_reciprocal_math)
755  /* Convert (A/B)/C to A/(B*C). */
756  (simplify
757   (rdiv (rdiv:s @0 @1) @2)
758   (rdiv @0 (mult @1 @2)))
760  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
761  (simplify
762   (rdiv @0 (mult:s @1 REAL_CST@2))
763   (with
764    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
765    (if (tem)
766     (rdiv (mult @0 { tem; } ) @1))))
768  /* Convert A/(B/C) to (A/B)*C  */
769  (simplify
770   (rdiv @0 (rdiv:s @1 @2))
771    (mult (rdiv @0 @1) @2)))
773 /* Simplify x / (- y) to -x / y.  */
774 (simplify
775  (rdiv @0 (negate @1))
776  (rdiv (negate @0) @1))
778 (if (flag_unsafe_math_optimizations)
779  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
780     Since C / x may underflow to zero, do this only for unsafe math.  */
781  (for op (lt le gt ge)
782       neg_op (gt ge lt le)
783   (simplify
784    (op (rdiv REAL_CST@0 @1) real_zerop@2)
785    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
786     (switch
787      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
788       (op @1 @2))
789      /* For C < 0, use the inverted operator.  */
790      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
791       (neg_op @1 @2)))))))
793 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
794 (for div (trunc_div ceil_div floor_div round_div exact_div)
795  (simplify
796   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
797   (if (integer_pow2p (@2)
798        && tree_int_cst_sgn (@2) > 0
799        && tree_nop_conversion_p (type, TREE_TYPE (@0))
800        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
801    (rshift (convert @0)
802            { build_int_cst (integer_type_node,
803                             wi::exact_log2 (wi::to_wide (@2))); }))))
805 /* If ARG1 is a constant, we can convert this to a multiply by the
806    reciprocal.  This does not have the same rounding properties,
807    so only do this if -freciprocal-math.  We can actually
808    always safely do it if ARG1 is a power of two, but it's hard to
809    tell if it is or not in a portable manner.  */
810 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
811  (simplify
812   (rdiv @0 cst@1)
813   (if (optimize)
814    (if (flag_reciprocal_math
815         && !real_zerop (@1))
816     (with
817      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
818      (if (tem)
819       (mult @0 { tem; } )))
820     (if (cst != COMPLEX_CST)
821      (with { tree inverse = exact_inverse (type, @1); }
822       (if (inverse)
823        (mult @0 { inverse; } ))))))))
825 (for mod (ceil_mod floor_mod round_mod trunc_mod)
826  /* 0 % X is always zero.  */
827  (simplify
828   (mod integer_zerop@0 @1)
829   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
830   (if (!integer_zerop (@1))
831    @0))
832  /* X % 1 is always zero.  */
833  (simplify
834   (mod @0 integer_onep)
835   { build_zero_cst (type); })
836  /* X % -1 is zero.  */
837  (simplify
838   (mod @0 integer_minus_onep@1)
839   (if (!TYPE_UNSIGNED (type))
840    { build_zero_cst (type); }))
841  /* X % X is zero.  */
842  (simplify
843   (mod @0 @0)
844   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
845   (if (!integer_zerop (@0))
846    { build_zero_cst (type); }))
847  /* (X % Y) % Y is just X % Y.  */
848  (simplify
849   (mod (mod@2 @0 @1) @1)
850   @2)
851  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
852  (simplify
853   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
854   (if (ANY_INTEGRAL_TYPE_P (type)
855        && TYPE_OVERFLOW_UNDEFINED (type)
856        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
857                              TYPE_SIGN (type)))
858    { build_zero_cst (type); }))
859  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
860     modulo and comparison, since it is simpler and equivalent.  */
861  (for cmp (eq ne)
862   (simplify
863    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
864    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
865     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
866      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
868 /* X % -C is the same as X % C.  */
869 (simplify
870  (trunc_mod @0 INTEGER_CST@1)
871   (if (TYPE_SIGN (type) == SIGNED
872        && !TREE_OVERFLOW (@1)
873        && wi::neg_p (wi::to_wide (@1))
874        && !TYPE_OVERFLOW_TRAPS (type)
875        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
876        && !sign_bit_p (@1, @1))
877    (trunc_mod @0 (negate @1))))
879 /* X % -Y is the same as X % Y.  */
880 (simplify
881  (trunc_mod @0 (convert? (negate @1)))
882  (if (INTEGRAL_TYPE_P (type)
883       && !TYPE_UNSIGNED (type)
884       && !TYPE_OVERFLOW_TRAPS (type)
885       && tree_nop_conversion_p (type, TREE_TYPE (@1))
886       /* Avoid this transformation if X might be INT_MIN or
887          Y might be -1, because we would then change valid
888          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
889       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
890           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
891                                                         (TREE_TYPE (@1))))))
892   (trunc_mod @0 (convert @1))))
894 /* X - (X / Y) * Y is the same as X % Y.  */
895 (simplify
896  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
897  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
898   (convert (trunc_mod @0 @1))))
900 /* x * (1 + y / x) - y -> x - y % x */
901 (simplify
902  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
903  (if (INTEGRAL_TYPE_P (type))
904   (minus @0 (trunc_mod @1 @0))))
906 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
907    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
908    Also optimize A % (C << N)  where C is a power of 2,
909    to A & ((C << N) - 1).
910    Also optimize "A shift (B % C)", if C is a power of 2, to
911    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
912    and assume (B % C) is nonnegative as shifts negative values would
913    be UB.  */
914 (match (power_of_two_cand @1)
915  INTEGER_CST@1)
916 (match (power_of_two_cand @1)
917  (lshift INTEGER_CST@1 @2))
918 (for mod (trunc_mod floor_mod)
919  (for shift (lshift rshift)
920   (simplify
921    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
922    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
923     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
924                                                       1); }))))))
925  (simplify
926   (mod @0 (convert? (power_of_two_cand@1 @2)))
927   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
928        /* Allow any integral conversions of the divisor, except
929           conversion from narrower signed to wider unsigned type
930           where if @1 would be negative power of two, the divisor
931           would not be a power of two.  */
932        && INTEGRAL_TYPE_P (type)
933        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
934        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
935            || TYPE_UNSIGNED (TREE_TYPE (@1))
936            || !TYPE_UNSIGNED (type))
937        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
938    (with { tree utype = TREE_TYPE (@1);
939            if (!TYPE_OVERFLOW_WRAPS (utype))
940              utype = unsigned_type_for (utype); }
941     (bit_and @0 (convert (minus (convert:utype @1)
942                                 { build_one_cst (utype); })))))))
944 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
945 (simplify
946  (trunc_div (mult @0 integer_pow2p@1) @1)
947  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
948   (bit_and @0 { wide_int_to_tree
949                 (type, wi::mask (TYPE_PRECISION (type)
950                                  - wi::exact_log2 (wi::to_wide (@1)),
951                                  false, TYPE_PRECISION (type))); })))
953 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
954 (simplify
955  (mult (trunc_div @0 integer_pow2p@1) @1)
956  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
957   (bit_and @0 (negate @1))))
959 (for div (trunc_div ceil_div floor_div round_div exact_div)
960  /* Simplify (t * u) / u -> t.  */
961  (simplify
962   (div (mult:c @0 @1) @1)
963   (if (ANY_INTEGRAL_TYPE_P (type))
964    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
965     @0
966 #if GIMPLE
967     (with {int_range_max vr0, vr1;}
968      (if (INTEGRAL_TYPE_P (type)
969           && get_range_query (cfun)->range_of_expr (vr0, @0)
970           && get_range_query (cfun)->range_of_expr (vr1, @1)
971           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
972       @0))
973 #endif
974    )))
975 #if GIMPLE
976  /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v.  */
977  (simplify
978   (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
979   (if (INTEGRAL_TYPE_P (type)
980        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
981    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
982     (mult @0 (div! @1 @2))
983     (with {int_range_max vr0, vr1;}
984      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
985           && get_range_query (cfun)->range_of_expr (vr1, @1)
986           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
987       (mult @0 (div! @1 @2))))
988    )))
989 #endif
990  /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v.  */
991  (simplify
992   (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
993   (if (INTEGRAL_TYPE_P (type)
994        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
995    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
996     (div @1 @2)
997 #if GIMPLE
998     (with {int_range_max vr0, vr1, vr2;}
999      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
1000           && get_range_query (cfun)->range_of_expr (vr1, @1)
1001           && get_range_query (cfun)->range_of_expr (vr2, @2)
1002           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
1003           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
1004       (div @1 @2)))
1005 #endif
1006    ))))
1008 #if GIMPLE
1009 (for div (trunc_div exact_div)
1010  /* Simplify (X + M*N) / N -> X / N + M.  */
1011  (simplify
1012   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
1013   (with {int_range_max vr0, vr1, vr2, vr3, vr4;}
1014   (if (INTEGRAL_TYPE_P (type)
1015        && get_range_query (cfun)->range_of_expr (vr1, @1)
1016        && get_range_query (cfun)->range_of_expr (vr2, @2)
1017        /* "N*M" doesn't overflow.  */
1018        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1019        && get_range_query (cfun)->range_of_expr (vr0, @0)
1020        && get_range_query (cfun)->range_of_expr (vr3, @3)
1021        /* "X+(N*M)" doesn't overflow.  */
1022        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1023        && get_range_query (cfun)->range_of_expr (vr4, @4)
1024        && !vr4.undefined_p ()
1025        /* "X+N*M" is not with opposite sign as "X".  */
1026        && (TYPE_UNSIGNED (type)
1027            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1028            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1029   (plus (div @0 @2) @1))))
1031  /* Simplify (X - M*N) / N -> X / N - M.  */
1032  (simplify
1033   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1034   (with {int_range_max vr0, vr1, vr2, vr3, vr4;}
1035   (if (INTEGRAL_TYPE_P (type)
1036        && get_range_query (cfun)->range_of_expr (vr1, @1)
1037        && get_range_query (cfun)->range_of_expr (vr2, @2)
1038        /* "N * M" doesn't overflow.  */
1039        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1040        && get_range_query (cfun)->range_of_expr (vr0, @0)
1041        && get_range_query (cfun)->range_of_expr (vr3, @3)
1042        /* "X - (N*M)" doesn't overflow.  */
1043        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1044        && get_range_query (cfun)->range_of_expr (vr4, @4)
1045        && !vr4.undefined_p ()
1046        /* "X-N*M" is not with opposite sign as "X".  */
1047        && (TYPE_UNSIGNED (type)
1048            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1049            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1050   (minus (div @0 @2) @1)))))
1052 /* Simplify
1053    (X + C) / N -> X / N + C / N where C is multiple of N.
1054    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
1055 (for op (trunc_div exact_div rshift)
1056  (simplify
1057   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1058    (with
1059     {
1060       wide_int c = wi::to_wide (@1);
1061       wide_int n = wi::to_wide (@2);
1062       bool shift = op == RSHIFT_EXPR;
1063 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1064                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1065 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1066                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1067       int_range_max vr0, vr1, vr3;
1068     }
1069     (if (INTEGRAL_TYPE_P (type)
1070          && get_range_query (cfun)->range_of_expr (vr0, @0))
1071      (if (exact_mod (c)
1072           && get_range_query (cfun)->range_of_expr (vr1, @1)
1073           /* "X+C" doesn't overflow.  */
1074           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1075           && get_range_query (cfun)->range_of_expr (vr3, @3)
1076           && !vr3.undefined_p ()
1077           /* "X+C" and "X" are not of opposite sign.  */
1078           && (TYPE_UNSIGNED (type)
1079               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1080               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1081        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1082        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1083             && exact_mod (-c)
1084             /* unsigned "X-(-C)" doesn't underflow.  */
1085             && wi::geu_p (vr0.lower_bound (), -c))
1086          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1087 #undef plus_op1
1088 #undef exact_mod
1089 #endif
1091 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1092    if var is smaller in precision.
1093    This is always safe for both doing the negative in signed or unsigned
1094    as the value for undefined will not show up.
1095    Note the outer cast cannot be a boolean type as the only valid values
1096    are 0,-1/1 (depending on the signedness of the boolean) and the negative
1097    is there to get the correct value.  */
1098 (simplify
1099  (convert (negate:s@1 (convert:s @0)))
1100  (if (INTEGRAL_TYPE_P (type)
1101       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1102       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1103       && TREE_CODE (type) != BOOLEAN_TYPE)
1104     (negate (convert @0))))
1106 (for op (negate abs)
1107  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1108  (for coss (COS COSH)
1109   (simplify
1110    (coss (op @0))
1111     (coss @0)))
1112  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1113  (for pows (POW)
1114   (simplify
1115    (pows (op @0) REAL_CST@1)
1116    (with { HOST_WIDE_INT n; }
1117     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1118      (pows @0 @1)))))
1119  /* Likewise for powi.  */
1120  (for pows (POWI)
1121   (simplify
1122    (pows (op @0) INTEGER_CST@1)
1123    (if ((wi::to_wide (@1) & 1) == 0)
1124     (pows @0 @1))))
1125  /* Strip negate and abs from both operands of hypot.  */
1126  (for hypots (HYPOT)
1127   (simplify
1128    (hypots (op @0) @1)
1129    (hypots @0 @1))
1130   (simplify
1131    (hypots @0 (op @1))
1132    (hypots @0 @1)))
1133  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1134  (for copysigns (COPYSIGN_ALL)
1135   (simplify
1136    (copysigns (op @0) @1)
1137    (copysigns @0 @1))))
1139 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1140 (simplify
1141  (mult (abs@1 @0) @1)
1142  (mult @0 @0))
1144 /* Convert absu(x)*absu(x) -> x*x.  */
1145 (simplify
1146  (mult (absu@1 @0) @1)
1147  (mult (convert@2 @0) @2))
1149 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1150 (for coss (COS COSH)
1151  (for copysigns (COPYSIGN)
1152   (simplify
1153    (coss (copysigns @0 @1))
1154     (coss @0))))
1156 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1157 (for pows (POW)
1158  (for copysigns (COPYSIGN)
1159   (simplify
1160    (pows (copysigns @0 @2) REAL_CST@1)
1161    (with { HOST_WIDE_INT n; }
1162     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1163      (pows @0 @1))))))
1164 /* Likewise for powi.  */
1165 (for pows (POWI)
1166  (for copysigns (COPYSIGN)
1167   (simplify
1168    (pows (copysigns @0 @2) INTEGER_CST@1)
1169    (if ((wi::to_wide (@1) & 1) == 0)
1170     (pows @0 @1)))))
1172 (for hypots (HYPOT)
1173  (for copysigns (COPYSIGN)
1174   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1175   (simplify
1176    (hypots (copysigns @0 @1) @2)
1177    (hypots @0 @2))
1178   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1179   (simplify
1180    (hypots @0 (copysigns @1 @2))
1181    (hypots @0 @1))))
1183 /* copysign(x, CST) -> abs (x).  If the target does not
1184    support the copysign optab then canonicalize
1185    copysign(x, -CST) -> fneg (abs (x)).   */
1186 (for copysigns (COPYSIGN_ALL)
1187  (simplify
1188   (copysigns @0 REAL_CST@1)
1189   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1190    (abs @0)
1191 #if GIMPLE
1192    (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1193                                          OPTIMIZE_FOR_BOTH))
1194     (negate (abs @0)))
1195 #endif
1196    )))
1198 #if GIMPLE
1199 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1200    representation if the target supports the copysign optab.  */
1201 (simplify
1202  (negate (abs @0))
1203  (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1204                                       OPTIMIZE_FOR_BOTH))
1205    (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1206 #endif
1207 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1208 (for copysigns (COPYSIGN_ALL)
1209  (simplify
1210   (copysigns (copysigns @0 @1) @2)
1211   (copysigns @0 @2)))
1213 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1214 (for copysigns (COPYSIGN_ALL)
1215  (simplify
1216   (mult (copysigns@2 @0 @1) @2)
1217   (mult @0 @0)))
1219 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1220 (for ccoss (CCOS CCOSH)
1221  (simplify
1222   (ccoss (negate @0))
1223    (ccoss @0)))
1225 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1226 (for ops (conj negate)
1227  (for cabss (CABS)
1228   (simplify
1229    (cabss (ops @0))
1230    (cabss @0))))
1232 /* Fold (a * (1 << b)) into (a << b)  */
1233 (simplify
1234  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1235   (if (! FLOAT_TYPE_P (type)
1236        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1237    (lshift @0 @2)))
1239 /* Fold a * !a into 0.  */
1240 (simplify
1241  (mult:c @0 (convert? (eq @0 integer_zerop)))
1242   { build_zero_cst (type); })
1243 (simplify
1244  (mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
1245   { build_zero_cst (type); })
1246 (simplify
1247  (mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
1248   { build_zero_cst (type); })
1250 /* Shifts by precision or greater result in zero.  */
1251 (for shift (lshift rshift)
1252  (simplify
1253   (shift @0 uniform_integer_cst_p@1)
1254   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1255        /* Leave arithmetic right shifts of possibly negative values alone.  */
1256        && (TYPE_UNSIGNED (type)
1257            || shift == LSHIFT_EXPR
1258            || tree_expr_nonnegative_p (@0))
1259        /* Use a signed compare to leave negative shift counts alone.  */
1260        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1261                      element_precision (type)))
1262    { build_zero_cst (type); })))
1264 /* Shifts by constants distribute over several binary operations,
1265    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1266 (for op (plus minus)
1267   (simplify
1268     (op (lshift:s @0 @1) (lshift:s @2 @1))
1269     (if (INTEGRAL_TYPE_P (type)
1270          && TYPE_OVERFLOW_WRAPS (type)
1271          && !TYPE_SATURATING (type))
1272       (lshift (op @0 @2) @1))))
1274 (for op (bit_and bit_ior bit_xor)
1275   (simplify
1276     (op (lshift:s @0 @1) (lshift:s @2 @1))
1277     (if (INTEGRAL_TYPE_P (type))
1278       (lshift (op @0 @2) @1)))
1279   (simplify
1280     (op (rshift:s @0 @1) (rshift:s @2 @1))
1281     (if (INTEGRAL_TYPE_P (type))
1282       (rshift (op @0 @2) @1))))
1284 /* Fold (1 << (C - x)) where C = precision(type) - 1
1285    into ((1 << C) >> x). */
1286 (simplify
1287  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1288   (if (INTEGRAL_TYPE_P (type)
1289        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1290        && single_use (@1))
1291    (if (TYPE_UNSIGNED (type))
1292      (rshift (lshift @0 @2) @3)
1293    (with
1294     { tree utype = unsigned_type_for (type); }
1295     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1297 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1298 (simplify
1299  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1300  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1301       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1302   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1303    (bit_and (convert @0)
1304             { wide_int_to_tree (type,
1305                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1307 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1308 (for cst (INTEGER_CST VECTOR_CST)
1309  (simplify
1310   (rshift (negate:s @0) cst@1)
1311    (if (!TYPE_UNSIGNED (type)
1312         && TYPE_OVERFLOW_UNDEFINED (type))
1313     (with { tree stype = TREE_TYPE (@1);
1314             tree bt = truth_type_for (type);
1315             tree zeros = build_zero_cst (type);
1316             tree cst = NULL_TREE; }
1317      (switch
1318       /* Handle scalar case.  */
1319       (if (INTEGRAL_TYPE_P (type)
1320            /* If we apply the rule to the scalar type before vectorization
1321               we will enforce the result of the comparison being a bool
1322               which will require an extra AND on the result that will be
1323               indistinguishable from when the user did actually want 0
1324               or 1 as the result so it can't be removed.  */
1325            && canonicalize_math_after_vectorization_p ()
1326            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1327        (negate (convert (gt @0 { zeros; }))))
1328       /* Handle vector case.  */
1329       (if (VECTOR_INTEGER_TYPE_P (type)
1330            /* First check whether the target has the same mode for vector
1331               comparison results as it's operands do.  */
1332            && TYPE_MODE (bt) == TYPE_MODE (type)
1333            /* Then check to see if the target is able to expand the comparison
1334               with the given type later on, otherwise we may ICE.  */
1335            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1336            && (cst = uniform_integer_cst_p (@1)) != NULL
1337            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1338        (view_convert (gt:bt @0 { zeros; }))))))))
1340 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1341 (simplify
1342  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1343   (if (flag_associative_math
1344        && single_use (@3))
1345    (with
1346     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1347     (if (tem)
1348      (rdiv { tem; } @1)))))
1350 /* Simplify ~X & X as zero.  */
1351 (simplify
1352  (bit_and:c (convert? @0) (convert? (maybe_bit_not @1)))
1353  (with { bool wascmp; }
1354   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1355        && bitwise_inverted_equal_p (@0, @1, wascmp))
1356    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1358 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1359 (simplify
1360   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1361   (if (TYPE_UNSIGNED (type))
1362     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1364 (for bitop (bit_and bit_ior)
1365      cmp (eq ne)
1366  /* PR35691: Transform
1367     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1368     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1369  (simplify
1370   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1371    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1372         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1373         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1374     (cmp (bit_ior @0 (convert @1)) @2)))
1375  /* Transform:
1376     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1377     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1378  (simplify
1379   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1380    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1381         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1382         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1383     (cmp (bit_and @0 (convert @1)) @2))))
1385 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1386 (simplify
1387  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1388   (minus (bit_xor @0 @1) @1))
1389 (simplify
1390  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1391  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1392   (minus (bit_xor @0 @1) @1)))
1394 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1395 (simplify
1396  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1397   (minus @1 (bit_xor @0 @1)))
1399 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1400 (for op (bit_ior bit_xor plus)
1401  (simplify
1402   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1403   (with { bool wascmp0, wascmp1; }
1404    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1405         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1406         && ((!wascmp0 && !wascmp1)
1407             || element_precision (type) == 1))
1408    (bit_xor @0 @1)))))
1410 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1411 (simplify
1412   (bit_ior:c (bit_xor:c @0 @1) @0)
1413   (bit_ior @0 @1))
1415 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1416 (simplify
1417  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1418  @2)
1420 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1421 (simplify
1422  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1423  (bit_not (bit_and @0 @1)))
1425 /* (~a & b) ^ a  -->   (a | b)   */
1426 (simplify
1427  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1428  (bit_ior @0 @1))
1430 /* (a | b) & ~(a ^ b)  -->  a & b  */
1431 (simplify
1432  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1433  (bit_and @0 @1))
1435 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1436 (simplify
1437  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1438  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1439       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1440   (bit_and @0 @1)))
1442 /* a | ~(a ^ b)  -->  a | ~b  */
1443 (simplify
1444  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1445  (bit_ior @0 (bit_not @1)))
1447 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1448 (simplify
1449  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1450  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1451       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1452   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1454 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1455 (simplify
1456  (bit_ior:c @0 (bit_xor:cs @1 @2))
1457  (with { bool wascmp; }
1458  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1459       && (!wascmp || element_precision (type) == 1))
1460   (bit_ior @0 (bit_not @2)))))
1462 /* a & ~(a ^ b)  -->  a & b  */
1463 (simplify
1464  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1465  (bit_and @0 @1))
1467 /* a & (a == b)  -->  a & b (boolean version of the above). */
1468 (simplify
1469  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1470  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1471       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1472   (bit_and @0 @1)))
1474 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1475 (simplify
1476  (bit_and:c @0 (bit_xor:c @1 @2))
1477  (with { bool wascmp; }
1478  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1479       && (!wascmp || element_precision (type) == 1))
1480   (bit_and @0 @2))))
1482 /* (a | b) | (a &^ b)  -->  a | b  */
1483 (for op (bit_and bit_xor)
1484  (simplify
1485   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1486   @2))
1488 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1489 (simplify
1490  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1491  @2)
1493 /* (a & b) | (a == b)  -->  a == b  */
1494 (simplify
1495  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1496  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1497       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1498   @2))
1500 /* ~(~a & b)  -->  a | ~b  */
1501 (simplify
1502  (bit_not (bit_and:cs (bit_not @0) @1))
1503  (bit_ior @0 (bit_not @1)))
1505 /* ~(~a | b) --> a & ~b */
1506 (simplify
1507  (bit_not (bit_ior:cs (bit_not @0) @1))
1508  (bit_and @0 (bit_not @1)))
1510 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1511 (simplify
1512  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1513  (bit_and @3 (bit_not @2)))
1515 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1516 (simplify
1517  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1518  (bit_ior @3 @2))
1520 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1521 (simplify
1522  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1523   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1525 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1526 (simplify
1527  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1528   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1530 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1531 (simplify
1532  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1533  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1534       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1535   (bit_xor @0 @1)))
1537 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1538    ((A & N) + B) & M -> (A + B) & M
1539    Similarly if (N & M) == 0,
1540    ((A | N) + B) & M -> (A + B) & M
1541    and for - instead of + (or unary - instead of +)
1542    and/or ^ instead of |.
1543    If B is constant and (B & M) == 0, fold into A & M.  */
1544 (for op (plus minus)
1545  (for bitop (bit_and bit_ior bit_xor)
1546   (simplify
1547    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1548     (with
1549      { tree pmop[2];
1550        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1551                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1552                                        NULL_TREE, pmop); }
1553      (if (utype)
1554       (convert (bit_and (op (convert:utype { pmop[0]; })
1555                             (convert:utype { pmop[1]; }))
1556                         (convert:utype @2))))))
1557   (simplify
1558    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1559     (with
1560      { tree pmop[2];
1561        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1562                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1563                                        @4, pmop); }
1564      (if (utype)
1565       (convert (bit_and (op (convert:utype { pmop[0]; })
1566                             (convert:utype { pmop[1]; }))
1567                         (convert:utype @2)))))))
1568  (simplify
1569   (bit_and (op:s @0 @1) INTEGER_CST@2)
1570    (with
1571     { tree pmop[2];
1572       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1573                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1574                                       NULL_TREE, NULL_TREE, pmop); }
1575     (if (utype)
1576      (convert (bit_and (op (convert:utype { pmop[0]; })
1577                            (convert:utype { pmop[1]; }))
1578                        (convert:utype @2)))))))
1579 (for bitop (bit_and bit_ior bit_xor)
1580  (simplify
1581   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1582    (with
1583     { tree pmop[2];
1584       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1585                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1586                                       NULL_TREE, NULL_TREE, pmop); }
1587     (if (utype)
1588      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1589                        (convert:utype @1)))))))
1591 /* X % Y is smaller than Y.  */
1592 (for cmp (lt ge)
1593  (simplify
1594   (cmp:c (trunc_mod @0 @1) @1)
1595   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1596    { constant_boolean_node (cmp == LT_EXPR, type); })))
1598 /* x | ~0 -> ~0  */
1599 (simplify
1600  (bit_ior @0 integer_all_onesp@1)
1601  @1)
1603 /* x | 0 -> x  */
1604 (simplify
1605  (bit_ior @0 integer_zerop)
1606  @0)
1608 /* x & 0 -> 0  */
1609 (simplify
1610  (bit_and @0 integer_zerop@1)
1611  @1)
1613 /* ~x | x -> -1 */
1614 /* ~x ^ x -> -1 */
1615 (for op (bit_ior bit_xor)
1616  (simplify
1617   (op:c (convert? @0) (convert? (maybe_bit_not @1)))
1618   (with { bool wascmp; }
1619    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1620         && bitwise_inverted_equal_p (@0, @1, wascmp))
1621     (convert
1622      { wascmp
1623         ? constant_boolean_node (true, type)
1624         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1626 /* x ^ x -> 0 */
1627 (simplify
1628   (bit_xor @0 @0)
1629   { build_zero_cst (type); })
1631 /* Canonicalize X ^ ~0 to ~X.  */
1632 (simplify
1633   (bit_xor @0 integer_all_onesp@1)
1634   (bit_not @0))
1636 /* x & ~0 -> x  */
1637 (simplify
1638  (bit_and @0 integer_all_onesp)
1639   (non_lvalue @0))
1641 /* x & x -> x,  x | x -> x  */
1642 (for bitop (bit_and bit_ior)
1643  (simplify
1644   (bitop @0 @0)
1645   (non_lvalue @0)))
1647 /* x & C -> x if we know that x & ~C == 0.  */
1648 #if GIMPLE
1649 (simplify
1650  (bit_and SSA_NAME@0 INTEGER_CST@1)
1651  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1652       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1653   @0))
1655 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1656 (simplify
1657  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1658  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1659       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1660   @0))
1662 /* x | C -> C if we know that x & ~C == 0.  */
1663 (simplify
1664  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1665  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1666       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1667   @1))
1668 #endif
1670 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1671 (simplify
1672  (bit_not (minus (bit_not @0) @1))
1673  (plus @0 @1))
1674 (simplify
1675  (bit_not (plus:c (bit_not @0) @1))
1676  (minus @0 @1))
1677 /* (~X - ~Y) -> Y - X.  */
1678 (simplify
1679  (minus (bit_not @0) (bit_not @1))
1680   (if (!TYPE_OVERFLOW_SANITIZED (type))
1681    (with { tree utype = unsigned_type_for (type); }
1682     (convert (minus (convert:utype @1) (convert:utype @0))))))
1684 /* ~(X - Y) -> ~X + Y.  */
1685 (simplify
1686  (bit_not (minus:s @0 @1))
1687  (plus (bit_not @0) @1))
1688 (simplify
1689  (bit_not (plus:s @0 INTEGER_CST@1))
1690  (if ((INTEGRAL_TYPE_P (type)
1691        && TYPE_UNSIGNED (type))
1692       || (!TYPE_OVERFLOW_SANITIZED (type)
1693           && may_negate_without_overflow_p (@1)))
1694   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1696 #if GIMPLE
1697 /* ~X + Y -> (Y - X) - 1.  */
1698 (simplify
1699  (plus:c (bit_not @0) @1)
1700   (if (ANY_INTEGRAL_TYPE_P (type)
1701        && TYPE_OVERFLOW_WRAPS (type)
1702        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1703        && !integer_all_onesp (@1))
1704    (plus (minus @1 @0) { build_minus_one_cst (type); })
1705    (if (INTEGRAL_TYPE_P (type)
1706         && TREE_CODE (@1) == INTEGER_CST
1707         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1708                                               SIGNED))
1709     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1710 #endif
1712 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1713 (simplify
1714  (bit_not (rshift:s @0 @1))
1715   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1716    (rshift (bit_not! @0) @1)
1717    /* For logical right shifts, this is possible only if @0 doesn't
1718       have MSB set and the logical right shift is changed into
1719       arithmetic shift.  */
1720    (if (INTEGRAL_TYPE_P (type)
1721         && !wi::neg_p (tree_nonzero_bits (@0)))
1722     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1723      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1725 /* x + (x & 1) -> (x + 1) & ~1 */
1726 (simplify
1727  (plus:c @0 (bit_and:s @0 integer_onep@1))
1728  (bit_and (plus @0 @1) (bit_not @1)))
1730 /* x & ~(x & y) -> x & ~y */
1731 /* x | ~(x | y) -> x | ~y  */
1732 (for bitop (bit_and bit_ior)
1733  (simplify
1734   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1735   (bitop @0 (bit_not @1))))
1737 /* (~x & y) | ~(x | y) -> ~x */
1738 (simplify
1739  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1740  @2)
1742 /* (x | y) ^ (x | ~y) -> ~x */
1743 (simplify
1744  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1745  (bit_not @0))
1747 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1748 (simplify
1749  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1750  (bit_not (bit_xor @0 @1)))
1752 /* (~x | y) ^ (x ^ y) -> x | ~y */
1753 (simplify
1754  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1755  (bit_ior @0 (bit_not @1)))
1757 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1758 (simplify
1759  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1760  (bit_not (bit_and @0 @1)))
1762 /* (x & y) ^ (x | y) -> x ^ y */
1763 (simplify
1764  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1765  (bit_xor @0 @1))
1767 /* (x ^ y) ^ (x | y) -> x & y */
1768 (simplify
1769  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1770  (bit_and @0 @1))
1772 /* (x & y) + (x ^ y) -> x | y */
1773 /* (x & y) | (x ^ y) -> x | y */
1774 /* (x & y) ^ (x ^ y) -> x | y */
1775 (for op (plus bit_ior bit_xor)
1776  (simplify
1777   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1778   (bit_ior @0 @1)))
1780 /* (x & y) + (x | y) -> x + y */
1781 (simplify
1782  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1783  (plus @0 @1))
1785 /* (x + y) - (x | y) -> x & y */
1786 (simplify
1787  (minus (plus @0 @1) (bit_ior @0 @1))
1788  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1789       && !TYPE_SATURATING (type))
1790   (bit_and @0 @1)))
1792 /* (x + y) - (x & y) -> x | y */
1793 (simplify
1794  (minus (plus @0 @1) (bit_and @0 @1))
1795  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1796       && !TYPE_SATURATING (type))
1797   (bit_ior @0 @1)))
1799 /* (x | y) - y -> (x & ~y) */
1800 (simplify
1801  (minus (bit_ior:cs @0 @1) @1)
1802  (bit_and @0 (bit_not @1)))
1804 /* (x | y) - (x ^ y) -> x & y */
1805 (simplify
1806  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1807  (bit_and @0 @1))
1809 /* (x | y) - (x & y) -> x ^ y */
1810 (simplify
1811  (minus (bit_ior @0 @1) (bit_and @0 @1))
1812  (bit_xor @0 @1))
1814 /* (x | y) & ~(x & y) -> x ^ y */
1815 (simplify
1816  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1817  (bit_xor @0 @1))
1819 /* (x | y) & (~x ^ y) -> x & y */
1820 (simplify
1821  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1822  (with { bool wascmp; }
1823   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1824        && (!wascmp || element_precision (type) == 1))
1825    (bit_and @0 @1))))
1827 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1828 (simplify
1829  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1830  (bit_not (bit_xor @0 @1)))
1832 /* (~x | y) ^ (x | ~y) -> x ^ y */
1833 (simplify
1834  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1835  (bit_xor @0 @1))
1837 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1838 (simplify
1839  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1840                               (nop_convert2? (bit_ior @0 @1))))
1841        integer_all_onesp)
1842  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1843       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1844       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1845       && !TYPE_SATURATING (TREE_TYPE (@2)))
1846  (bit_not (convert (bit_xor @0 @1)))))
1847 (simplify
1848  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1849                                integer_all_onesp))
1850        (nop_convert3? (bit_ior @0 @1)))
1851  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1852       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1853       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1854       && !TYPE_SATURATING (TREE_TYPE (@2)))
1855  (bit_not (convert (bit_xor @0 @1)))))
1856 (simplify
1857  (minus (nop_convert1? (bit_and @0 @1))
1858        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1859                                integer_onep)))
1860  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1861       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1862       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1863       && !TYPE_SATURATING (TREE_TYPE (@2)))
1864  (bit_not (convert (bit_xor @0 @1)))))
1866 /* ~x & ~y -> ~(x | y)
1867    ~x | ~y -> ~(x & y) */
1868 (for op (bit_and bit_ior)
1869      rop (bit_ior bit_and)
1870  (simplify
1871   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1872   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1873        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1874    (bit_not (rop (convert @0) (convert @1))))))
1876 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1877    with a constant, and the two constants have no bits in common,
1878    we should treat this as a BIT_IOR_EXPR since this may produce more
1879    simplifications.  */
1880 (for op (bit_xor plus)
1881  (simplify
1882   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1883       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1884   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1885        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1886        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1887    (bit_ior (convert @4) (convert @5)))))
1889 /* (X | Y) ^ X -> Y & ~ X*/
1890 (simplify
1891  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1892  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1893   (convert (bit_and @1 (bit_not @0)))))
1895 /* (~X | Y) ^ X -> ~(X & Y).  */
1896 (simplify
1897  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1898  (if (bitwise_equal_p (@0, @2))
1899   (convert (bit_not (bit_and @0 (convert @1))))))
1901 /* Convert ~X ^ ~Y to X ^ Y.  */
1902 (simplify
1903  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1904  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1905       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1906   (bit_xor (convert @0) (convert @1))))
1908 /* Convert ~X ^ C to X ^ ~C.  */
1909 (simplify
1910  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1911  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1912   (bit_xor (convert @0) (bit_not @1))))
1914 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1915 (for opo (bit_and bit_xor)
1916      opi (bit_xor bit_and)
1917  (simplify
1918   (opo:c (opi:cs @0 @1) @1)
1919   (bit_and (bit_not @0) @1)))
1921 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1922    operands are another bit-wise operation with a common input.  If so,
1923    distribute the bit operations to save an operation and possibly two if
1924    constants are involved.  For example, convert
1925      (A | B) & (A | C) into A | (B & C)
1926    Further simplification will occur if B and C are constants.  */
1927 (for op (bit_and bit_ior bit_xor)
1928      rop (bit_ior bit_and bit_and)
1929  (simplify
1930   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1931   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1932        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1933    (rop (convert @0) (op (convert @1) (convert @2))))))
1935 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1936 /* (X & Y) & Y -> X & Y
1937    (X | Y) | Y -> X | Y  */
1938 (for op (bit_and bit_ior)
1939  (simplify
1940   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1941   @2))
1942 /* (X ^ Y) ^ Y -> X  */
1943 (simplify
1944  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1945  (convert @0))
1947 /* (X & ~Y) & Y -> 0 */
1948 (simplify
1949  (bit_and:c (bit_and @0 @1) @2)
1950  (with { bool wascmp; }
1951   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1952        || bitwise_inverted_equal_p (@1, @2, wascmp))
1953    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1954 /* (X | ~Y) | Y -> -1 */
1955 (simplify
1956  (bit_ior:c (bit_ior @0 @1) @2)
1957  (with { bool wascmp; }
1958   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1959         || bitwise_inverted_equal_p (@1, @2, wascmp))
1960        && (!wascmp || element_precision (type) == 1))
1961    { build_all_ones_cst (TREE_TYPE (@0)); })))
1963 /* (X & Y) & (X & Z) -> (X & Y) & Z
1964    (X | Y) | (X | Z) -> (X | Y) | Z  */
1965 (for op (bit_and bit_ior)
1966  (simplify
1967   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1968   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1969        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1970    (if (single_use (@5) && single_use (@6))
1971     (op @3 (convert @2))
1972     (if (single_use (@3) && single_use (@4))
1973      (op (convert @1) @5))))))
1974 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1975 (simplify
1976  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1977  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1978       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1979   (bit_xor (convert @1) (convert @2))))
1981 /* Convert abs (abs (X)) into abs (X).
1982    also absu (absu (X)) into absu (X).  */
1983 (simplify
1984  (abs (abs@1 @0))
1985  @1)
1987 (simplify
1988  (absu (convert@2 (absu@1 @0)))
1989  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1990   @1))
1992 /* Convert abs[u] (-X) -> abs[u] (X).  */
1993 (simplify
1994  (abs (negate @0))
1995  (abs @0))
1997 (simplify
1998  (absu (negate @0))
1999  (absu @0))
2001 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
2002 (simplify
2003  (abs tree_expr_nonnegative_p@0)
2004  @0)
2006 (simplify
2007  (absu tree_expr_nonnegative_p@0)
2008  (convert @0))
2010 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
2011 (simplify
2012  (mult:c (nop_convert1?
2013           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
2014                     integer_onep))
2015          (nop_convert3? @0))
2016  (if (INTEGRAL_TYPE_P (type)
2017       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2018       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
2019   (if (TYPE_UNSIGNED (type))
2020    (absu @0)
2021    (abs @0)
2022   )
2026 /* A few cases of fold-const.cc negate_expr_p predicate.  */
2027 (match negate_expr_p
2028  INTEGER_CST
2029  (if ((INTEGRAL_TYPE_P (type)
2030        && TYPE_UNSIGNED (type))
2031       || (!TYPE_OVERFLOW_SANITIZED (type)
2032           && may_negate_without_overflow_p (t)))))
2033 (match negate_expr_p
2034  FIXED_CST)
2035 (match negate_expr_p
2036  (negate @0)
2037  (if (!TYPE_OVERFLOW_SANITIZED (type))))
2038 (match negate_expr_p
2039  REAL_CST
2040  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2041 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2042    ways.  */
2043 (match negate_expr_p
2044  VECTOR_CST
2045  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2046 (match negate_expr_p
2047  (minus @0 @1)
2048  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2049       || (FLOAT_TYPE_P (type)
2050           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2051           && !HONOR_SIGNED_ZEROS (type)))))
2053 /* (-A) * (-B) -> A * B  */
2054 (simplify
2055  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2056   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2057        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2058    (mult (convert @0) (convert (negate @1)))))
2060 /* -(A + B) -> (-B) - A.  */
2061 (simplify
2062  (negate (plus:c @0 negate_expr_p@1))
2063  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2064       && !HONOR_SIGNED_ZEROS (type))
2065   (minus (negate @1) @0)))
2067 /* -(A - B) -> B - A.  */
2068 (simplify
2069  (negate (minus @0 @1))
2070  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2071       || (FLOAT_TYPE_P (type)
2072           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2073           && !HONOR_SIGNED_ZEROS (type)))
2074   (minus @1 @0)))
2075 (simplify
2076  (negate (pointer_diff @0 @1))
2077  (if (TYPE_OVERFLOW_UNDEFINED (type))
2078   (pointer_diff @1 @0)))
2080 /* A - B -> A + (-B) if B is easily negatable.  */
2081 (simplify
2082  (minus @0 negate_expr_p@1)
2083  (if (!FIXED_POINT_TYPE_P (type))
2084  (plus @0 (negate @1))))
2086 /* 1 - a is a ^ 1 if a had a bool range. */
2087 /* This is only enabled for gimple as sometimes
2088    cfun is not set for the function which contains
2089    the SSA_NAME (e.g. while IPA passes are happening,
2090    fold might be called).  */
2091 (simplify
2092  (minus integer_onep@0 SSA_NAME@1)
2093   (if (INTEGRAL_TYPE_P (type)
2094        && ssa_name_has_boolean_range (@1))
2095    (bit_xor @1 @0)))
2097 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2098 (simplify
2099  (negate (mult:c@0 @1 negate_expr_p@2))
2100  (if (! TYPE_UNSIGNED (type)
2101       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2102       && single_use (@0))
2103   (mult @1 (negate @2))))
2105 (simplify
2106  (negate (rdiv@0 @1 negate_expr_p@2))
2107  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2108       && single_use (@0))
2109   (rdiv @1 (negate @2))))
2111 (simplify
2112  (negate (rdiv@0 negate_expr_p@1 @2))
2113  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2114       && single_use (@0))
2115   (rdiv (negate @1) @2)))
2117 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2118 (simplify
2119  (negate (convert? (rshift @0 INTEGER_CST@1)))
2120  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2121       && wi::to_wide (@1) == element_precision (type) - 1)
2122   (with { tree stype = TREE_TYPE (@0);
2123           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2124                                              : unsigned_type_for (stype); }
2125    (if (VECTOR_TYPE_P (type))
2126     (view_convert (rshift (view_convert:ntype @0) @1))
2127     (convert (rshift (convert:ntype @0) @1))))))
2129 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2130    when profitable.
2131    For bitwise binary operations apply operand conversions to the
2132    binary operation result instead of to the operands.  This allows
2133    to combine successive conversions and bitwise binary operations.
2134    We combine the above two cases by using a conditional convert.  */
2135 (for bitop (bit_and bit_ior bit_xor)
2136  (simplify
2137   (bitop (convert@2 @0) (convert?@3 @1))
2138   (if (((TREE_CODE (@1) == INTEGER_CST
2139          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2140          && (int_fits_type_p (@1, TREE_TYPE (@0))
2141              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2142         || types_match (@0, @1))
2143        && !POINTER_TYPE_P (TREE_TYPE (@0))
2144        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2145        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2146        /* ???  This transform conflicts with fold-const.cc doing
2147           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2148           constants (if x has signed type, the sign bit cannot be set
2149           in c).  This folds extension into the BIT_AND_EXPR.
2150           Restrict it to GIMPLE to avoid endless recursions.  */
2151        && (bitop != BIT_AND_EXPR || GIMPLE)
2152        && (/* That's a good idea if the conversion widens the operand, thus
2153               after hoisting the conversion the operation will be narrower.
2154               It is also a good if the conversion is a nop as moves the
2155               conversion to one side; allowing for combining of the conversions.  */
2156            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2157            /* The conversion check for being a nop can only be done at the gimple
2158               level as fold_binary has some re-association code which can conflict
2159               with this if there is a "constant" which is not a full INTEGER_CST.  */
2160            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2161            /* It's also a good idea if the conversion is to a non-integer
2162               mode.  */
2163            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2164            /* Or if the precision of TO is not the same as the precision
2165               of its mode.  */
2166            || !type_has_mode_precision_p (type)
2167            /* In GIMPLE, getting rid of 2 conversions for one new results
2168               in smaller IL.  */
2169            || (GIMPLE
2170                && TREE_CODE (@1) != INTEGER_CST
2171                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2172                && single_use (@2)
2173                && single_use (@3))))
2174    (convert (bitop @0 (convert @1)))))
2175  /* In GIMPLE, getting rid of 2 conversions for one new results
2176     in smaller IL.  */
2177  (simplify
2178   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2179   (if (GIMPLE
2180        && TREE_CODE (@1) != INTEGER_CST
2181        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2182        && types_match (type, @0)
2183        && !POINTER_TYPE_P (TREE_TYPE (@0))
2184        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2185    (bitop @0 (convert @1)))))
2187 (for bitop (bit_and bit_ior)
2188      rbitop (bit_ior bit_and)
2189   /* (x | y) & x -> x */
2190   /* (x & y) | x -> x */
2191  (simplify
2192   (bitop:c (rbitop:c @0 @1) @0)
2193   @0)
2194  /* (~x | y) & x -> x & y */
2195  /* (~x & y) | x -> x | y */
2196  (simplify
2197   (bitop:c (rbitop:c @2 @1) @0)
2198   (with { bool wascmp; }
2199    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2200         && (!wascmp || element_precision (type) == 1))
2201     (bitop @0 @1))))
2202   /* (x | y) & (x & z) -> (x & z) */
2203   /* (x & y) | (x | z) -> (x | z) */
2204  (simplify
2205   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2206   @3)
2207  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2208  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2209  (simplify
2210   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2211   (bitop @0 @3))
2212  /* x & ~(y | x) -> 0 */
2213  /* x | ~(y & x) -> -1 */
2214  (simplify
2215   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2216   (if (bitop == BIT_AND_EXPR)
2217    { build_zero_cst (type); }
2218    { build_minus_one_cst (type); })))
2220 /* ((x | y) & z) | x -> (z & y) | x
2221    ((x ^ y) & z) | x -> (z & y) | x  */
2222 (for op (bit_ior bit_xor)
2223  (simplify
2224   (bit_ior:c (nop_convert1?:s
2225                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2226   (if (bitwise_equal_p (@0, @3))
2227    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2229 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2230 (simplify
2231   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2232   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2234 /* Combine successive equal operations with constants.  */
2235 (for bitop (bit_and bit_ior bit_xor)
2236  (simplify
2237   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2238   (if (!CONSTANT_CLASS_P (@0))
2239    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2240       folded to a constant.  */
2241    (bitop @0 (bitop! @1 @2))
2242    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2243       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2244       the values involved are such that the operation can't be decided at
2245       compile time.  Try folding one of @0 or @1 with @2 to see whether
2246       that combination can be decided at compile time.
2248       Keep the existing form if both folds fail, to avoid endless
2249       oscillation.  */
2250    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2251     (if (cst1)
2252      (bitop @1 { cst1; })
2253      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2254       (if (cst2)
2255        (bitop @0 { cst2; }))))))))
2257 /* Try simple folding for X op !X, and X op X with the help
2258    of the truth_valued_p and logical_inverted_value predicates.  */
2259 (match truth_valued_p
2260  @0
2261  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2262 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2263  (match truth_valued_p
2264   (op @0 @1)))
2265 (match truth_valued_p
2266   (truth_not @0))
2268 (match (logical_inverted_value @0)
2269  (truth_not @0))
2270 (match (logical_inverted_value @0)
2271  (bit_not truth_valued_p@0))
2272 (match (logical_inverted_value @0)
2273  (eq @0 integer_zerop))
2274 (match (logical_inverted_value @0)
2275  (ne truth_valued_p@0 integer_truep))
2276 (match (logical_inverted_value @0)
2277  (bit_xor truth_valued_p@0 integer_truep))
2279 /* X & !X -> 0.  */
2280 (simplify
2281  (bit_and:c @0 (logical_inverted_value @0))
2282  { build_zero_cst (type); })
2283 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2284 (for op (bit_ior bit_xor)
2285  (simplify
2286   (op:c truth_valued_p@0 (logical_inverted_value @0))
2287   { constant_boolean_node (true, type); }))
2288 /* X ==/!= !X is false/true.  */
2289 (for op (eq ne)
2290  (simplify
2291   (op:c truth_valued_p@0 (logical_inverted_value @0))
2292   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2294 /* ~~x -> x */
2295 (simplify
2296   (bit_not (bit_not @0))
2297   @0)
2299 /* zero_one_valued_p will match when a value is known to be either
2300    0 or 1 including constants 0 or 1.
2301    Signed 1-bits includes -1 so they cannot match here. */
2302 (match zero_one_valued_p
2303  @0
2304  (if (INTEGRAL_TYPE_P (type)
2305       && (TYPE_UNSIGNED (type)
2306           || TYPE_PRECISION (type) > 1)
2307       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2308 (match zero_one_valued_p
2309  truth_valued_p@0
2310  (if (INTEGRAL_TYPE_P (type)
2311       && (TYPE_UNSIGNED (type)
2312           || TYPE_PRECISION (type) > 1))))
2314 /* (a&1) is always [0,1] too. This is useful again when
2315    the range is not known. */
2316 /* Note this can't be recursive due to VN handling of equivalents,
2317    VN and would cause an infinite recursion. */
2318 (match zero_one_valued_p
2319  (bit_and:c@0 @1 integer_onep)
2320  (if (INTEGRAL_TYPE_P (type))))
2322 /* A conversion from an zero_one_valued_p is still a [0,1].
2323    This is useful when the range of a variable is not known */
2324 /* Note this matches can't be recursive because of the way VN handles
2325    nop conversions being equivalent and then recursive between them. */
2326 (match zero_one_valued_p
2327  (convert@0 @1)
2328  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2329       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2330           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2331       && INTEGRAL_TYPE_P (type)
2332       && (TYPE_UNSIGNED (type)
2333           || TYPE_PRECISION (type) > 1)
2334       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2336 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2337 (simplify
2338  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2339  (if (INTEGRAL_TYPE_P (type))
2340   (bit_and @0 @1)))
2342 (for cmp (tcc_comparison)
2343      icmp (inverted_tcc_comparison)
2344  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2345  (simplify
2346   (bit_ior
2347    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2348    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2349     (if (INTEGRAL_TYPE_P (type)
2350          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2351          /* The scalar version has to be canonicalized after vectorization
2352             because it makes unconditional loads conditional ones, which
2353             means we lose vectorization because the loads may trap.  */
2354          && canonicalize_math_after_vectorization_p ())
2355      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2357  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2358     canonicalized further and we recognize the conditional form:
2359     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2360  (simplify
2361   (bit_ior
2362    (cond (cmp@0  @01 @02) @3 zerop)
2363    (cond (icmp@4 @01 @02) @5 zerop))
2364     (if (INTEGRAL_TYPE_P (type)
2365          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2366          /* The scalar version has to be canonicalized after vectorization
2367             because it makes unconditional loads conditional ones, which
2368             means we lose vectorization because the loads may trap.  */
2369          && canonicalize_math_after_vectorization_p ())
2370     (cond @0 @3 @5)))
2372  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2373     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2374  (simplify
2375   (bit_ior
2376    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2377    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2378     (if (integer_zerop (@5)
2379          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2380      (switch
2381       (if (integer_onep (@4))
2382        (bit_and (vec_cond @0 @2 @3) @4))
2383         (if (integer_minus_onep (@4))
2384          (vec_cond @0 @2 @3)))
2385     (if (integer_zerop (@4)
2386          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2387      (switch
2388       (if (integer_onep (@5))
2389        (bit_and (vec_cond @0 @3 @2) @5))
2390       (if (integer_minus_onep (@5))
2391        (vec_cond @0 @3 @2))))))
2393  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2394     into a < b ? d : c.  */
2395  (simplify
2396   (bit_ior
2397    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2398    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2399   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2400    (vec_cond @0 @2 @3))))
2402 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2403 (simplify
2404  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2405  (if (INTEGRAL_TYPE_P (type)
2406       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2408       /* Sign extending of the neg or a truncation of the neg
2409          is needed. */
2410       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2411           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2412   (mult (convert @0) @1)))
2414 /* Narrow integer multiplication by a zero_one_valued_p operand.
2415    Multiplication by [0,1] is guaranteed not to overflow except for
2416    1bit signed types.  */
2417 (simplify
2418  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2419  (if (INTEGRAL_TYPE_P (type)
2420       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2421       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))
2422       && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1))
2423   (mult (convert @1) (convert @2))))
2425 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2426    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2427    as some targets (such as x86's SSE) may return zero for larger C.  */
2428 (simplify
2429   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2430   (if (tree_fits_shwi_p (@1)
2431        && tree_to_shwi (@1) > 0
2432        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2433     (convert @0)))
2435 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2436    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2437    as some targets (such as x86's SSE) may return zero for larger C.  */
2438 (simplify
2439   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2440   (if (tree_fits_shwi_p (@1)
2441        && tree_to_shwi (@1) > 0
2442        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2443     (eq @0 @2)))
2445 /* Convert ~ (-A) to A - 1.  */
2446 (simplify
2447  (bit_not (convert? (negate @0)))
2448  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2449       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2450   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2452 /* Convert - (~A) to A + 1.  */
2453 (simplify
2454  (negate (nop_convert? (bit_not @0)))
2455  (plus (view_convert @0) { build_each_one_cst (type); }))
2457 /* (a & b) ^ (a == b) -> !(a | b) */
2458 /* (a & b) == (a ^ b) -> !(a | b) */
2459 (for first_op (bit_xor eq)
2460      second_op (eq bit_xor)
2461  (simplify
2462   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2463     (bit_not (bit_ior @0 @1))))
2465 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2466 (simplify
2467  (bit_not (convert? (minus @0 integer_each_onep)))
2468  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2469       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2470   (convert (negate @0))))
2471 (simplify
2472  (bit_not (convert? (plus @0 integer_all_onesp)))
2473  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2474       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2475   (convert (negate @0))))
2477 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2478 (simplify
2479  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2480  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2481   (convert (bit_xor @0 (bit_not @1)))))
2482 (simplify
2483  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2484  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2485   (convert (bit_xor @0 @1))))
2487 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2488 (simplify
2489  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2490  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2491   (bit_not (bit_xor (view_convert @0) @1))))
2493 /* ~(a ^ b) is a == b for truth valued a and b.  */
2494 (simplify
2495  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2496  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2497       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2498   (convert (eq @0 @1))))
2500 /* (~a) == b is a ^ b for truth valued a and b.  */
2501 (simplify
2502  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2503  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2504       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2505   (convert (bit_xor @0 @1))))
2507 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2508 (simplify
2509  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2510  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2512 /* Fold A - (A & B) into ~B & A.  */
2513 (simplify
2514  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2515  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2516       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2517   (convert (bit_and (bit_not @1) @0))))
2519 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2520 (if (!canonicalize_math_p ())
2521  (for cmp (tcc_comparison)
2522   (simplify
2523    (mult:c (convert (cmp@0 @1 @2)) @3)
2524    (if (INTEGRAL_TYPE_P (type)
2525         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2526      (cond @0 @3 { build_zero_cst (type); })))
2527 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2528   (simplify
2529    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2530    (if (INTEGRAL_TYPE_P (type)
2531         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2532      (cond @0 @3 { build_zero_cst (type); })))
2536 /* For integral types with undefined overflow and C != 0 fold
2537    x * C EQ/NE y * C into x EQ/NE y.  */
2538 (for cmp (eq ne)
2539  (simplify
2540   (cmp (mult:c @0 @1) (mult:c @2 @1))
2541   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2542        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2543        && tree_expr_nonzero_p (@1))
2544    (cmp @0 @2))))
2546 /* For integral types with wrapping overflow and C odd fold
2547    x * C EQ/NE y * C into x EQ/NE y.  */
2548 (for cmp (eq ne)
2549  (simplify
2550   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2551   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2552        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2553        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2554    (cmp @0 @2))))
2556 /* For integral types with undefined overflow and C != 0 fold
2557    x * C RELOP y * C into:
2559    x RELOP y for nonnegative C
2560    y RELOP x for negative C  */
2561 (for cmp (lt gt le ge)
2562  (simplify
2563   (cmp (mult:c @0 @1) (mult:c @2 @1))
2564   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2565        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2566    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2567     (cmp @0 @2)
2568    (if (TREE_CODE (@1) == INTEGER_CST
2569         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2570     (cmp @2 @0))))))
2572 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2573 (for cmp (le gt)
2574      icmp (gt le)
2575  (simplify
2576   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2577    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2578         && TYPE_UNSIGNED (TREE_TYPE (@0))
2579         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2580         && (wi::to_wide (@2)
2581             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2582     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2583      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2585 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2586 (for cmp (simple_comparison)
2587  (simplify
2588   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2589   (if (element_precision (@3) >= element_precision (@0)
2590        && types_match (@0, @1))
2591    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2592     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2593      (cmp @1 @0)
2594      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2595       (with
2596        {
2597         tree utype = unsigned_type_for (TREE_TYPE (@0));
2598        }
2599        (cmp (convert:utype @1) (convert:utype @0)))))
2600     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2601      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2602       (cmp @0 @1)
2603       (with
2604        {
2605         tree utype = unsigned_type_for (TREE_TYPE (@0));
2606        }
2607        (cmp (convert:utype @0) (convert:utype @1)))))))))
2609 /* X / C1 op C2 into a simple range test.  */
2610 (for cmp (simple_comparison)
2611  (simplify
2612   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2613   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2614        && integer_nonzerop (@1)
2615        && !TREE_OVERFLOW (@1)
2616        && !TREE_OVERFLOW (@2))
2617    (with { tree lo, hi; bool neg_overflow;
2618            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2619                                                    &neg_overflow); }
2620     (switch
2621      (if (code == LT_EXPR || code == GE_EXPR)
2622        (if (TREE_OVERFLOW (lo))
2623         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2624         (if (code == LT_EXPR)
2625          (lt @0 { lo; })
2626          (ge @0 { lo; }))))
2627      (if (code == LE_EXPR || code == GT_EXPR)
2628        (if (TREE_OVERFLOW (hi))
2629         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2630         (if (code == LE_EXPR)
2631          (le @0 { hi; })
2632          (gt @0 { hi; }))))
2633      (if (!lo && !hi)
2634       { build_int_cst (type, code == NE_EXPR); })
2635      (if (code == EQ_EXPR && !hi)
2636       (ge @0 { lo; }))
2637      (if (code == EQ_EXPR && !lo)
2638       (le @0 { hi; }))
2639      (if (code == NE_EXPR && !hi)
2640       (lt @0 { lo; }))
2641      (if (code == NE_EXPR && !lo)
2642       (gt @0 { hi; }))
2643      (if (GENERIC)
2644       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2645                            lo, hi); })
2646      (with
2647       {
2648         tree etype = range_check_type (TREE_TYPE (@0));
2649         if (etype)
2650           {
2651             hi = fold_convert (etype, hi);
2652             lo = fold_convert (etype, lo);
2653             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2654           }
2655       }
2656       (if (etype && hi && !TREE_OVERFLOW (hi))
2657        (if (code == EQ_EXPR)
2658         (le (minus (convert:etype @0) { lo; }) { hi; })
2659         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2661 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2662 (for op (lt le ge gt)
2663  (simplify
2664   (op (plus:c @0 @2) (plus:c @1 @2))
2665   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2666        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2667    (op @0 @1))))
2669 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2670    when C is an unsigned integer constant with only the MSB set, and X and
2671    Y have types of equal or lower integer conversion rank than C's.  */
2672 (for op (lt le ge gt)
2673  (simplify
2674   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2675   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2676        && TYPE_UNSIGNED (TREE_TYPE (@0))
2677        && wi::only_sign_bit_p (wi::to_wide (@0)))
2678    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2679     (op (convert:stype @1) (convert:stype @2))))))
2681 /* For equality and subtraction, this is also true with wrapping overflow.  */
2682 (for op (eq ne minus)
2683  (simplify
2684   (op (plus:c @0 @2) (plus:c @1 @2))
2685   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2686        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2687            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2688    (op @0 @1))))
2689 /* And similar for pointers.  */
2690 (for op (eq ne)
2691  (simplify
2692   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2693   (op @1 @2)))
2694 (simplify
2695  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2696  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2697   (convert (minus @1 @2))))
2699 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2700 (for op (lt le ge gt)
2701  (simplify
2702   (op (minus @0 @2) (minus @1 @2))
2703   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2704        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2705    (op @0 @1))))
2706 /* For equality and subtraction, this is also true with wrapping overflow.  */
2707 (for op (eq ne minus)
2708  (simplify
2709   (op (minus @0 @2) (minus @1 @2))
2710   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2711        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2712            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2713    (op @0 @1))))
2714 /* And for pointers...  */
2715 (for op (simple_comparison)
2716  (simplify
2717   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2718   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2719    (op @0 @1))))
2720 (simplify
2721  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2722  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2723       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2724   (pointer_diff @0 @1)))
2726 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2727 (for op (lt le ge gt)
2728  (simplify
2729   (op (minus @2 @0) (minus @2 @1))
2730   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2731        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2732    (op @1 @0))))
2733 /* For equality and subtraction, this is also true with wrapping overflow.  */
2734 (for op (eq ne minus)
2735  (simplify
2736   (op (minus @2 @0) (minus @2 @1))
2737   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2738        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2739            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2740    (op @1 @0))))
2741 /* And for pointers...  */
2742 (for op (simple_comparison)
2743  (simplify
2744   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2745   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2746    (op @1 @0))))
2747 (simplify
2748  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2749  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2750       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2751   (pointer_diff @1 @0)))
2753 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2754 (for op (lt le gt ge)
2755  (simplify
2756   (op:c (plus:c@2 @0 @1) @1)
2757   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2758        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2759        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2760        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2761    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2762 /* For equality, this is also true with wrapping overflow.  */
2763 (for op (eq ne)
2764  (simplify
2765   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2766   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2767        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2768            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2769        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2770        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2771        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2772    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2773  (simplify
2774   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2775   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2776        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2777        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2778    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2780 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2781 (for neeq (ne eq)
2782  (simplify
2783   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2784    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2785     (if (ptr_difference_const (@0, @2, &diff))
2786      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2787  (simplify
2788   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2789    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2790     (if (ptr_difference_const (@0, @2, &diff))
2791      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2793 /* X - Y < X is the same as Y > 0 when there is no overflow.
2794    For equality, this is also true with wrapping overflow.  */
2795 (for op (simple_comparison)
2796  (simplify
2797   (op:c @0 (minus@2 @0 @1))
2798   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2799        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2800            || ((op == EQ_EXPR || op == NE_EXPR)
2801                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2802        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2803    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2805 /* Transform:
2806    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2807    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2808 (for cmp (eq ne)
2809      ocmp (lt ge)
2810  (simplify
2811   (cmp (trunc_div @0 @1) integer_zerop)
2812   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2813        /* Complex ==/!= is allowed, but not </>=.  */
2814        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2815        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2816    (ocmp @0 @1))))
2818 /* X == C - X can never be true if C is odd.  */
2819 (for cmp (eq ne)
2820  (simplify
2821   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2822   (if (TREE_INT_CST_LOW (@1) & 1)
2823    { constant_boolean_node (cmp == NE_EXPR, type); })))
2826    U & N <= U  -> true
2827    U & N >  U  -> false
2828    U needs to be non-negative.
2830    U | N <  U  -> false
2831    U | N >= U  -> true
2832    U and N needs to be non-negative
2834    U | N <  U  -> true
2835    U | N >= U  -> false
2836    U needs to be non-negative and N needs to be a negative constant.
2837    */
2838 (for cmp   (lt      ge      le      gt     )
2839      bitop (bit_ior bit_ior bit_and bit_and)
2840  (simplify
2841   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2842   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2843    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2844     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2845     /* The sign is opposite now so the comparison is swapped around. */
2846     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2847      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2849 /* Arguments on which one can call get_nonzero_bits to get the bits
2850    possibly set.  */
2851 (match with_possible_nonzero_bits
2852  INTEGER_CST@0)
2853 (match with_possible_nonzero_bits
2854  SSA_NAME@0
2855  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2856 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2857 (match (with_possible_nonzero_bits2 @0)
2858  with_possible_nonzero_bits@0)
2859 (match (with_possible_nonzero_bits2 @0)
2860  (bit_and:c with_possible_nonzero_bits@0 @2))
2862 /* Same for bits that are known to be set, but we do not have
2863    an equivalent to get_nonzero_bits yet.  */
2864 (match (with_certain_nonzero_bits2 @0)
2865  INTEGER_CST@0)
2866 (match (with_certain_nonzero_bits2 @0)
2867  (bit_ior @1 INTEGER_CST@0))
2869 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2870 (for cmp (eq ne)
2871  (simplify
2872   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2873   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2874    { constant_boolean_node (cmp == NE_EXPR, type); })))
2876 /* ((X inner_op C0) outer_op C1)
2877    With X being a tree where range has reasoned certain bits to always be
2878    zero throughout its computed value range,
2879    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2880    where zero_mask has 1's for all bits that are sure to be 0 in
2881    and 0's otherwise.
2882    if (inner_op == '^') C0 &= ~C1;
2883    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2884    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2886 (for inner_op (bit_ior bit_xor)
2887      outer_op (bit_xor bit_ior)
2888 (simplify
2889  (outer_op
2890   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2891  (with
2892   {
2893     bool fail = false;
2894     wide_int zero_mask_not;
2895     wide_int C0;
2896     wide_int cst_emit;
2898     if (TREE_CODE (@2) == SSA_NAME)
2899       zero_mask_not = get_nonzero_bits (@2);
2900     else
2901       fail = true;
2903     if (inner_op == BIT_XOR_EXPR)
2904       {
2905         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2906         cst_emit = C0 | wi::to_wide (@1);
2907       }
2908     else
2909       {
2910         C0 = wi::to_wide (@0);
2911         cst_emit = C0 ^ wi::to_wide (@1);
2912       }
2913   }
2914   (if (!fail && (C0 & zero_mask_not) == 0)
2915    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2916    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2917     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2919 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2920 (simplify
2921   (pointer_plus (pointer_plus:s @0 @1) @3)
2922   (pointer_plus @0 (plus @1 @3)))
2923 #if GENERIC
2924 (simplify
2925   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2926   (convert:type (pointer_plus @0 (plus @1 @3))))
2927 #endif
2929 /* Pattern match
2930      tem1 = (long) ptr1;
2931      tem2 = (long) ptr2;
2932      tem3 = tem2 - tem1;
2933      tem4 = (unsigned long) tem3;
2934      tem5 = ptr1 + tem4;
2935    and produce
2936      tem5 = ptr2;  */
2937 (simplify
2938   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2939   /* Conditionally look through a sign-changing conversion.  */
2940   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2941        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2942             || (GENERIC && type == TREE_TYPE (@1))))
2943    @1))
2944 (simplify
2945   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2946   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2947    (convert @1)))
2949 /* Pattern match
2950      tem = (sizetype) ptr;
2951      tem = tem & algn;
2952      tem = -tem;
2953      ... = ptr p+ tem;
2954    and produce the simpler and easier to analyze with respect to alignment
2955      ... = ptr & ~algn;  */
2956 (simplify
2957   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2958   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2959    (bit_and @0 { algn; })))
2961 /* Try folding difference of addresses.  */
2962 (simplify
2963  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2964  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2965   (with { poly_int64 diff; }
2966    (if (ptr_difference_const (@0, @1, &diff))
2967     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2968 (simplify
2969  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2970  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2971   (with { poly_int64 diff; }
2972    (if (ptr_difference_const (@0, @1, &diff))
2973     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2974 (simplify
2975  (minus (convert ADDR_EXPR@0) (convert @1))
2976  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2977   (with { poly_int64 diff; }
2978    (if (ptr_difference_const (@0, @1, &diff))
2979     { build_int_cst_type (type, diff); }))))
2980 (simplify
2981  (minus (convert @0) (convert ADDR_EXPR@1))
2982  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2983   (with { poly_int64 diff; }
2984    (if (ptr_difference_const (@0, @1, &diff))
2985     { build_int_cst_type (type, diff); }))))
2986 (simplify
2987  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2988  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2989       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2990   (with { poly_int64 diff; }
2991    (if (ptr_difference_const (@0, @1, &diff))
2992     { build_int_cst_type (type, diff); }))))
2993 (simplify
2994  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2995  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2996       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2997   (with { poly_int64 diff; }
2998    (if (ptr_difference_const (@0, @1, &diff))
2999     { build_int_cst_type (type, diff); }))))
3001 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
3002 (simplify
3003  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
3004  (with { poly_int64 diff; }
3005    (if (ptr_difference_const (@0, @2, &diff))
3006     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
3007 /* (p + b) - &p->d -> offsetof (*p, d) + b */
3008 (simplify
3009  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
3010  (with { poly_int64 diff; }
3011    (if (ptr_difference_const (@0, @2, &diff))
3012     (plus { build_int_cst_type (type, diff); } (convert @1)))))
3013 (simplify
3014  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
3015  (with { poly_int64 diff; }
3016    (if (ptr_difference_const (@0, @1, &diff))
3017     (minus { build_int_cst_type (type, diff); } (convert @2)))))
3019 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
3020 (simplify
3021  (convert (pointer_diff @0 INTEGER_CST@1))
3022  (if (POINTER_TYPE_P (type))
3023   { build_fold_addr_expr_with_type
3024       (build2 (MEM_REF, char_type_node, @0,
3025                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
3026                type); }))
3028 /* If arg0 is derived from the address of an object or function, we may
3029    be able to fold this expression using the object or function's
3030    alignment.  */
3031 (simplify
3032  (bit_and (convert? @0) INTEGER_CST@1)
3033  (if (POINTER_TYPE_P (TREE_TYPE (@0))
3034       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3035   (with
3036    {
3037      unsigned int align;
3038      unsigned HOST_WIDE_INT bitpos;
3039      get_pointer_alignment_1 (@0, &align, &bitpos);
3040    }
3041    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3042     { wide_int_to_tree (type, (wi::to_wide (@1)
3043                                & (bitpos / BITS_PER_UNIT))); }))))
3045 (match min_value
3046  uniform_integer_cst_p
3047  (with {
3048    tree int_cst = uniform_integer_cst_p (t);
3049    tree inner_type = TREE_TYPE (int_cst);
3050   }
3051   (if ((INTEGRAL_TYPE_P (inner_type)
3052         || POINTER_TYPE_P (inner_type))
3053        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3055 (match max_value
3056  uniform_integer_cst_p
3057  (with {
3058    tree int_cst = uniform_integer_cst_p (t);
3059    tree itype = TREE_TYPE (int_cst);
3060   }
3061  (if ((INTEGRAL_TYPE_P (itype)
3062        || POINTER_TYPE_P (itype))
3063       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3065 /* Unsigned Saturation Add */
3066 /* SAT_ADD = usadd_left_part_1 | usadd_right_part_1, aka:
3067    SAT_ADD = (X + Y) | -((X + Y) < X)  */
3068 (match (usadd_left_part_1 @0 @1)
3069  (plus:c @0 @1)
3070  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3071       && types_match (type, @0, @1))))
3073 /* SAT_ADD = usadd_left_part_2 | usadd_right_part_2, aka:
3074    SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVERFLOW> != 0) */
3075 (match (usadd_left_part_2 @0 @1)
3076  (realpart (IFN_ADD_OVERFLOW:c @0 @1))
3077  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3078       && types_match (type, @0, @1))))
3080 /* SAT_ADD = usadd_left_part_1 | usadd_right_part_1, aka:
3081    SAT_ADD = (X + Y) | -((type)(X + Y) < X)  */
3082 (match (usadd_right_part_1 @0 @1)
3083  (negate (convert (lt (plus:c @0 @1) @0)))
3084  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3085       && types_match (type, @0, @1))))
3087 /* SAT_ADD = usadd_left_part_1 | usadd_right_part_1, aka:
3088    SAT_ADD = (X + Y) | -(X > (X + Y))  */
3089 (match (usadd_right_part_1 @0 @1)
3090  (negate (convert (gt @0 (plus:c @0 @1))))
3091  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3092       && types_match (type, @0, @1))))
3094 /* SAT_ADD = usadd_left_part_2 | usadd_right_part_2, aka:
3095    SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVERFLOW> != 0) */
3096 (match (usadd_right_part_2 @0 @1)
3097  (negate (convert (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop)))
3098  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3099       && types_match (type, @0, @1))))
3101 /* SAT_ADD = usadd_left_part_2 | usadd_right_part_2, aka:
3102    SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | -IMAGPART_EXPR <.ADD_OVERFLOW> */
3103 (match (usadd_right_part_2 @0 @1)
3104  (negate (imagpart (IFN_ADD_OVERFLOW:c @0 @1)))
3105  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3106       && types_match (type, @0, @1))))
3108 /* We cannot merge or overload usadd_left_part_1 and usadd_left_part_2
3109    because the sub part of left_part_2 cannot work with right_part_1.
3110    For example, left_part_2 pattern focus one .ADD_OVERFLOW but the
3111    right_part_1 has nothing to do with .ADD_OVERFLOW.  */
3113 /* Unsigned saturation add, case 1 (branchless):
3114    SAT_U_ADD = (X + Y) | - ((X + Y) < X) or
3115    SAT_U_ADD = (X + Y) | - (X > (X + Y)).  */
3116 (match (unsigned_integer_sat_add @0 @1)
3117  (bit_ior:c (usadd_left_part_1 @0 @1) (usadd_right_part_1 @0 @1)))
3119 /* Unsigned saturation add, case 2 (branchless with .ADD_OVERFLOW):
3120    SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | -IMAGPART_EXPR <.ADD_OVERFLOW> or
3121    SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVERFLOW> != 0) */
3122 (match (unsigned_integer_sat_add @0 @1)
3123  (bit_ior:c (usadd_left_part_2 @0 @1) (usadd_right_part_2 @0 @1)))
3125 /* Unsigned saturation add, case 3 (branch with ge):
3126    SAT_U_ADD = (X + Y) >= x ? (X + Y) : -1.  */
3127 (match (unsigned_integer_sat_add @0 @1)
3128  (cond^ (ge (usadd_left_part_1@2 @0 @1) @0) @2 integer_minus_onep))
3130 /* Unsigned saturation add, case 4 (branch with lt):
3131    SAT_U_ADD = (X + Y) < x ? -1 : (X + Y).  */
3132 (match (unsigned_integer_sat_add @0 @1)
3133  (cond^ (lt (usadd_left_part_1@2 @0 @1) @0) integer_minus_onep @2))
3135 /* Unsigned saturation add, case 5 (branch with eq .ADD_OVERFLOW):
3136    SAT_U_ADD = REALPART_EXPR <.ADD_OVERFLOW> == 0 ? .ADD_OVERFLOW : -1.  */
3137 (match (unsigned_integer_sat_add @0 @1)
3138  (cond^ (eq (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop)
3139   (usadd_left_part_2 @0 @1) integer_minus_onep))
3141 /* Unsigned saturation add, case 6 (branch with ne .ADD_OVERFLOW):
3142    SAT_U_ADD = REALPART_EXPR <.ADD_OVERFLOW> != 0 ? -1 : .ADD_OVERFLOW.  */
3143 (match (unsigned_integer_sat_add @0 @1)
3144  (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop)
3145   integer_minus_onep (usadd_left_part_2 @0 @1)))
3147 /* Unsigned saturation add, case 7 (branch with le):
3148    SAT_ADD = x <= (X + Y) ? (X + Y) : -1.  */
3149 (match (unsigned_integer_sat_add @0 @1)
3150  (cond^ (le @0 (usadd_left_part_1@2 @0 @1)) @2 integer_minus_onep))
3152 /* Unsigned saturation add, case 8 (branch with gt):
3153    SAT_ADD = x > (X + Y) ? -1 : (X + Y).  */
3154 (match (unsigned_integer_sat_add @0 @1)
3155  (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2))
3157 /* Unsigned saturation add, case 9 (one op is imm):
3158    SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1.  */
3159 (match (unsigned_integer_sat_add @0 @1)
3160  (plus (min @0 INTEGER_CST@2) INTEGER_CST@1)
3161  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3162       && types_match (type, @0, @1))
3163   (with
3164    {
3165     unsigned precision = TYPE_PRECISION (type);
3166     wide_int cst_1 = wi::to_wide (@1);
3167     wide_int cst_2 = wi::to_wide (@2);
3168     wide_int max = wi::mask (precision, false, precision);
3169     wide_int sum = wi::add (cst_1, cst_2);
3170    }
3171    (if (wi::eq_p (max, sum))))))
3173 /* Unsigned saturation add, case 10 (one op is imm):
3174    SAT_U_ADD = __builtin_add_overflow (X, 3, &ret) == 0 ? ret : -1.  */
3175 (match (unsigned_integer_sat_add @0 @1)
3176  (cond^ (ne (imagpart (IFN_ADD_OVERFLOW@2 @0 INTEGER_CST@1)) integer_zerop)
3177   integer_minus_onep (realpart @2))
3178   (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3179       && types_match (type, @0))))
3181 /* Unsigned saturation sub, case 1 (branch with gt):
3182    SAT_U_SUB = X > Y ? X - Y : 0  */
3183 (match (unsigned_integer_sat_sub @0 @1)
3184  (cond^ (gt @0 @1) (minus @0 @1) integer_zerop)
3185  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3186       && types_match (type, @0, @1))))
3188 /* Unsigned saturation sub, case 2 (branch with ge):
3189    SAT_U_SUB = X >= Y ? X - Y : 0.  */
3190 (match (unsigned_integer_sat_sub @0 @1)
3191  (cond^ (ge @0 @1) (convert? (minus (convert1? @0) (convert1? @1))) integer_zerop)
3192  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3193       && TYPE_UNSIGNED (TREE_TYPE (@0)) && types_match (@0, @1))))
3195 /* Unsigned saturation sub, case 3 (branchless with gt):
3196    SAT_U_SUB = (X - Y) * (X > Y).  */
3197 (match (unsigned_integer_sat_sub @0 @1)
3198  (mult:c (minus @0 @1) (convert (gt @0 @1)))
3199  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3200       && types_match (type, @0, @1))))
3202 /* Unsigned saturation sub, case 4 (branchless with ge):
3203    SAT_U_SUB = (X - Y) * (X >= Y).  */
3204 (match (unsigned_integer_sat_sub @0 @1)
3205  (mult:c (minus @0 @1) (convert (ge @0 @1)))
3206  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3207       && types_match (type, @0, @1))))
3209 /* Unsigned saturation sub, case 5 (branchless bit_and with .SUB_OVERFLOW).  */
3210 (match (unsigned_integer_sat_sub @0 @1)
3211  (bit_and:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1))
3212   (plus (imagpart @2) integer_minus_onep))
3213  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3214       && types_match (type, @0, @1))))
3216 /* Unsigned saturation sub, case 6 (branchless mult with .SUB_OVERFLOW).  */
3217 (match (unsigned_integer_sat_sub @0 @1)
3218  (mult:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1))
3219   (bit_xor (imagpart @2) integer_onep))
3220  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3221       && types_match (type, @0, @1))))
3223 /* Unsigned saturation sub, case 7 (branch eq with .SUB_OVERFLOW).  */
3224 (match (unsigned_integer_sat_sub @0 @1)
3225  (cond^ (eq (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3226   (realpart @2) integer_zerop)
3227  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3228       && types_match (type, @0, @1))))
3230 /* Unsigned saturation sub, case 8 (branch ne with .SUB_OVERFLOW).  */
3231 (match (unsigned_integer_sat_sub @0 @1)
3232  (cond^ (ne (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3233    integer_zerop (realpart @2))
3234  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3235       && types_match (type, @0, @1))))
3237 /* Unsigned saturation truncate, case 1, sizeof (WT) > sizeof (NT).
3238    SAT_U_TRUNC = (NT)x | (NT)(-(X > (WT)(NT)(-1))).  */
3239 (match (unsigned_integer_sat_trunc @0)
3240  (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1)))
3241    (convert @0))
3242  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3243       && TYPE_UNSIGNED (TREE_TYPE (@0)))
3244  (with
3245   {
3246    unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3247    unsigned otype_precision = TYPE_PRECISION (type);
3248    wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3249    wide_int int_cst = wi::to_wide (@1, itype_precision);
3250   }
3251   (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
3253 /* Unsigned saturation truncate, case 2, sizeof (WT) > sizeof (NT).
3254    SAT_U_TRUNC = (NT)(MIN_EXPR (X, 255)).  */
3255 /* If Op_0 def is MIN_EXPR and not single_use.  Aka below pattern:
3257      _18 = MIN_EXPR <left_8, 4294967295>; // op_0 def
3258      iftmp.0_11 = (unsigned int) _18;     // op_0
3259      stream.avail_out = iftmp.0_11;
3260      left_37 = left_8 - _18;              // op_0 use
3262    Transfer to .SAT_TRUNC will have MIN_EXPR still live.  Then the backend
3263    (for example x86/riscv) will have 2-3 more insns generation for .SAT_TRUNC
3264    besides the MIN_EXPR.  Thus,  keep the normal truncation as is should be
3265    the better choose.  */
3266 (match (unsigned_integer_sat_trunc @0)
3267  (convert (min@2 @0 INTEGER_CST@1))
3268  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3269       && TYPE_UNSIGNED (TREE_TYPE (@0)) && single_use (@2))
3270  (with
3271   {
3272    unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3273    unsigned otype_precision = TYPE_PRECISION (type);
3274    wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3275    wide_int int_cst = wi::to_wide (@1, itype_precision);
3276   }
3277   (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
3279 /* x >  y  &&  x != XXX_MIN  -->  x > y
3280    x >  y  &&  x == XXX_MIN  -->  false . */
3281 (for eqne (eq ne)
3282  (simplify
3283   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3284    (switch
3285     (if (eqne == EQ_EXPR)
3286      { constant_boolean_node (false, type); })
3287     (if (eqne == NE_EXPR)
3288      @2)
3289     )))
3291 /* x <  y  &&  x != XXX_MAX  -->  x < y
3292    x <  y  &&  x == XXX_MAX  -->  false.  */
3293 (for eqne (eq ne)
3294  (simplify
3295   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3296    (switch
3297     (if (eqne == EQ_EXPR)
3298      { constant_boolean_node (false, type); })
3299     (if (eqne == NE_EXPR)
3300      @2)
3301     )))
3303 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3304 (simplify
3305  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3306   @2)
3308 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3309 (simplify
3310  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3311   @2)
3313 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3314 (simplify
3315  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3316   @2)
3318 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3319 (simplify
3320  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3321   { constant_boolean_node (true, type); })
3323 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3324 (simplify
3325  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3326   @2)
3328 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3329 (simplify
3330  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3331   @2)
3333 /* x >=  y  ||  x != XXX_MAX   -->  true
3334    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3335 (for eqne (eq ne)
3336  (simplify
3337   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3338    (switch
3339     (if (eqne == EQ_EXPR)
3340      @2)
3341     (if (eqne == NE_EXPR)
3342      { constant_boolean_node (true, type); }))))
3344 /* y == XXX_MIN || x < y --> x <= y - 1 */
3345 (simplify
3346  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3347   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3348        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3349   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3351 /* y != XXX_MIN && x >= y --> x > y - 1 */
3352 (simplify
3353  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3354   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3355        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3356   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3358 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3359    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3360 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3361    Similarly for (X != Y).  */
3363 (for code1 (eq ne)
3364  (for code2 (eq ne lt gt le ge)
3365   (simplify
3366    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3367    (if ((TREE_CODE (@1) == INTEGER_CST
3368          && TREE_CODE (@2) == INTEGER_CST)
3369         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3370              || POINTER_TYPE_P (TREE_TYPE (@1)))
3371             && bitwise_equal_p (@1, @2)))
3372     (with
3373      {
3374       bool one_before = false;
3375       bool one_after = false;
3376       int cmp = 0;
3377       bool allbits = true;
3378       if (TREE_CODE (@1) == INTEGER_CST
3379           && TREE_CODE (@2) == INTEGER_CST)
3380         {
3381           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3382           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3383           auto t2 = wi::to_wide (@2);
3384           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3385           if (cmp < 0
3386               && t1 == t2 - 1)
3387             one_before = true;
3388           if (cmp > 0
3389               && t1 == t2 + 1)
3390             one_after = true;
3391         }
3392       bool val;
3393       switch (code2)
3394          {
3395         case EQ_EXPR: val = (cmp == 0); break;
3396         case NE_EXPR: val = (cmp != 0); break;
3397         case LT_EXPR: val = (cmp < 0); break;
3398         case GT_EXPR: val = (cmp > 0); break;
3399         case LE_EXPR: val = (cmp <= 0); break;
3400         case GE_EXPR: val = (cmp >= 0); break;
3401         default: gcc_unreachable ();
3402         }
3403      }
3404      (switch
3405       (if (code1 == EQ_EXPR && val) @3)
3406       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3407       (if (code1 == NE_EXPR && !val && allbits) @4)
3408       (if (code1 == NE_EXPR
3409            && code2 == GE_EXPR
3410            && cmp == 0
3411            && allbits)
3412        (gt @c0 (convert @1)))
3413       (if (code1 == NE_EXPR
3414            && code2 == LE_EXPR
3415            && cmp == 0
3416            && allbits)
3417        (lt @c0 (convert @1)))
3418       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3419       (if (code1 == NE_EXPR
3420            && code2 == GT_EXPR
3421            && one_after
3422            && allbits)
3423        (gt @c0 (convert @1)))
3424       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3425       (if (code1 == NE_EXPR
3426            && code2 == LT_EXPR
3427            && one_before
3428            && allbits)
3429        (lt @c0 (convert @1)))
3430      )
3431     )
3432    )
3433   )
3437 /* Convert (X OP1 CST1) && (X OP2 CST2).
3438    Convert (X OP1 Y) && (X OP2 Y).  */
3440 (for code1 (lt le gt ge)
3441  (for code2 (lt le gt ge)
3442   (simplify
3443   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3444   (if ((TREE_CODE (@1) == INTEGER_CST
3445         && TREE_CODE (@2) == INTEGER_CST)
3446        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3447             || POINTER_TYPE_P (TREE_TYPE (@1)))
3448            && operand_equal_p (@1, @2)))
3449    (with
3450     {
3451      int cmp = 0;
3452      if (TREE_CODE (@1) == INTEGER_CST
3453          && TREE_CODE (@2) == INTEGER_CST)
3454        cmp = tree_int_cst_compare (@1, @2);
3455     }
3456     (switch
3457      /* Choose the more restrictive of two < or <= comparisons.  */
3458      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3459           && (code2 == LT_EXPR || code2 == LE_EXPR))
3460       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3461        @3
3462        @4))
3463      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3464      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3465           && (code2 == GT_EXPR || code2 == GE_EXPR))
3466       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3467        @3
3468        @4))
3469      /* Check for singleton ranges.  */
3470      (if (cmp == 0
3471           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3472             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3473       (eq @0 @1))
3474      /* Check for disjoint ranges.  */
3475      (if (cmp <= 0
3476           && (code1 == LT_EXPR || code1 == LE_EXPR)
3477           && (code2 == GT_EXPR || code2 == GE_EXPR))
3478       { constant_boolean_node (false, type); })
3479      (if (cmp >= 0
3480           && (code1 == GT_EXPR || code1 == GE_EXPR)
3481           && (code2 == LT_EXPR || code2 == LE_EXPR))
3482       { constant_boolean_node (false, type); })
3483      ))))))
3485 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3486    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3487 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3488    Similarly for (X != Y).  */
3490 (for code1 (eq ne)
3491  (for code2 (eq ne lt gt le ge)
3492   (simplify
3493    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3494    (if ((TREE_CODE (@1) == INTEGER_CST
3495          && TREE_CODE (@2) == INTEGER_CST)
3496         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3497             || POINTER_TYPE_P (TREE_TYPE (@1)))
3498             && bitwise_equal_p (@1, @2)))
3499     (with
3500      {
3501       bool one_before = false;
3502       bool one_after = false;
3503       int cmp = 0;
3504       bool allbits = true;
3505       if (TREE_CODE (@1) == INTEGER_CST
3506           && TREE_CODE (@2) == INTEGER_CST)
3507         {
3508           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3509           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3510           auto t2 = wi::to_wide (@2);
3511           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3512           if (cmp < 0
3513               && t1 == t2 - 1)
3514             one_before = true;
3515           if (cmp > 0
3516               && t1 == t2 + 1)
3517             one_after = true;
3518         }
3519       bool val;
3520       switch (code2)
3521         {
3522         case EQ_EXPR: val = (cmp == 0); break;
3523         case NE_EXPR: val = (cmp != 0); break;
3524         case LT_EXPR: val = (cmp < 0); break;
3525         case GT_EXPR: val = (cmp > 0); break;
3526         case LE_EXPR: val = (cmp <= 0); break;
3527         case GE_EXPR: val = (cmp >= 0); break;
3528         default: gcc_unreachable ();
3529         }
3530      }
3531      (switch
3532       (if (code1 == EQ_EXPR && val) @4)
3533       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3534       (if (code1 == NE_EXPR && !val && allbits) @3)
3535       (if (code1 == EQ_EXPR
3536            && code2 == GT_EXPR
3537            && cmp == 0
3538            && allbits)
3539        (ge @c0 @2))
3540       (if (code1 == EQ_EXPR
3541            && code2 == LT_EXPR
3542            && cmp == 0
3543            && allbits)
3544        (le @c0 @2))
3545       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3546       (if (code1 == EQ_EXPR
3547            && code2 == GE_EXPR
3548            && one_before
3549            && allbits)
3550        (ge @c0 (convert @1)))
3551       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3552       (if (code1 == EQ_EXPR
3553            && code2 == LE_EXPR
3554            && one_after
3555            && allbits)
3556        (le @c0 (convert @1)))
3557      )
3558     )
3559    )
3560   )
3564 /* Convert (X OP1 CST1) || (X OP2 CST2).
3565    Convert (X OP1 Y)    || (X OP2 Y).  */
3567 (for code1 (lt le gt ge)
3568  (for code2 (lt le gt ge)
3569   (simplify
3570   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3571   (if ((TREE_CODE (@1) == INTEGER_CST
3572         && TREE_CODE (@2) == INTEGER_CST)
3573        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3574             || POINTER_TYPE_P (TREE_TYPE (@1)))
3575            && operand_equal_p (@1, @2)))
3576    (with
3577     {
3578      int cmp = 0;
3579      if (TREE_CODE (@1) == INTEGER_CST
3580          && TREE_CODE (@2) == INTEGER_CST)
3581        cmp = tree_int_cst_compare (@1, @2);
3582     }
3583     (switch
3584      /* Choose the more restrictive of two < or <= comparisons.  */
3585      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3586           && (code2 == LT_EXPR || code2 == LE_EXPR))
3587       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3588        @4
3589        @3))
3590      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3591      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3592           && (code2 == GT_EXPR || code2 == GE_EXPR))
3593       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3594        @4
3595        @3))
3596      /* Check for singleton ranges.  */
3597      (if (cmp == 0
3598           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3599               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3600       (ne @0 @2))
3601      /* Check for disjoint ranges.  */
3602      (if (cmp >= 0
3603           && (code1 == LT_EXPR || code1 == LE_EXPR)
3604           && (code2 == GT_EXPR || code2 == GE_EXPR))
3605       { constant_boolean_node (true, type); })
3606      (if (cmp <= 0
3607           && (code1 == GT_EXPR || code1 == GE_EXPR)
3608           && (code2 == LT_EXPR || code2 == LE_EXPR))
3609       { constant_boolean_node (true, type); })
3610      ))))))
3612 /* Optimize (a CMP b) ^ (a CMP b)  */
3613 /* Optimize (a CMP b) != (a CMP b)  */
3614 (for op (bit_xor ne)
3615  (for cmp1 (lt lt lt le le le)
3616       cmp2 (gt eq ne ge eq ne)
3617       rcmp (ne le gt ne lt ge)
3618   (simplify
3619    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3620    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3621     (rcmp @0 @1)))))
3623 /* Optimize (a CMP b) == (a CMP b)  */
3624 (for cmp1 (lt lt lt le le le)
3625      cmp2 (gt eq ne ge eq ne)
3626      rcmp (eq gt le eq ge lt)
3627  (simplify
3628   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3629   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3630     (rcmp @0 @1))))
3632 /* (type)([0,1]@a != 0) -> (type)a
3633    (type)([0,1]@a == 1) -> (type)a
3634    (type)([0,1]@a == 0) -> a ^ 1
3635    (type)([0,1]@a != 1) -> a ^ 1.  */
3636 (for eqne (eq ne)
3637  (simplify
3638   (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3639   (if ((integer_zerop (@1) || integer_onep (@1)))
3640    (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3641     (convert @0)
3642     /* Only do this if the types match as (type)(a == 0) is
3643        canonical form normally, while `a ^ 1` is canonical when
3644        there is no type change. */
3645     (if (types_match (type, TREE_TYPE (@0)))
3646      (bit_xor @0 { build_one_cst (type); } ))))))
3648 /* We can't reassociate at all for saturating types.  */
3649 (if (!TYPE_SATURATING (type))
3651  /* Contract negates.  */
3652  /* A + (-B) -> A - B */
3653  (simplify
3654   (plus:c @0 (convert? (negate @1)))
3655   /* Apply STRIP_NOPS on the negate.  */
3656   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3657        && !TYPE_OVERFLOW_SANITIZED (type))
3658    (with
3659     {
3660      tree t1 = type;
3661      if (INTEGRAL_TYPE_P (type)
3662          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3663        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3664     }
3665     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3666  /* A - (-B) -> A + B */
3667  (simplify
3668   (minus @0 (convert? (negate @1)))
3669   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3670        && !TYPE_OVERFLOW_SANITIZED (type))
3671    (with
3672     {
3673      tree t1 = type;
3674      if (INTEGRAL_TYPE_P (type)
3675          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3676        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3677     }
3678     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3679  /* -(T)(-A) -> (T)A
3680     Sign-extension is ok except for INT_MIN, which thankfully cannot
3681     happen without overflow.  */
3682  (simplify
3683   (negate (convert (negate @1)))
3684   (if (INTEGRAL_TYPE_P (type)
3685        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3686            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3687                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3688        && !TYPE_OVERFLOW_SANITIZED (type)
3689        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3690    (convert @1)))
3691  (simplify
3692   (negate (convert negate_expr_p@1))
3693   (if (SCALAR_FLOAT_TYPE_P (type)
3694        && ((DECIMAL_FLOAT_TYPE_P (type)
3695             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3696             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3697            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3698    (convert (negate @1))))
3699  (simplify
3700   (negate (nop_convert? (negate @1)))
3701   (if (!TYPE_OVERFLOW_SANITIZED (type)
3702        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3703    (view_convert @1)))
3705  /* We can't reassociate floating-point unless -fassociative-math
3706     or fixed-point plus or minus because of saturation to +-Inf.  */
3707  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3708       && !FIXED_POINT_TYPE_P (type))
3710   /* Match patterns that allow contracting a plus-minus pair
3711      irrespective of overflow issues.  */
3712   /* (A +- B) - A       ->  +- B */
3713   /* (A +- B) -+ B      ->  A */
3714   /* A - (A +- B)       -> -+ B */
3715   /* A +- (B -+ A)      ->  +- B */
3716   (simplify
3717    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3718    (view_convert @1))
3719   (simplify
3720    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3721    (if (!ANY_INTEGRAL_TYPE_P (type)
3722         || TYPE_OVERFLOW_WRAPS (type))
3723    (negate (view_convert @1))
3724    (view_convert (negate @1))))
3725   (simplify
3726    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3727    (view_convert @0))
3728   (simplify
3729    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3730     (if (!ANY_INTEGRAL_TYPE_P (type)
3731          || TYPE_OVERFLOW_WRAPS (type))
3732      (negate (view_convert @1))
3733      (view_convert (negate @1))))
3734   (simplify
3735    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3736    (view_convert @1))
3737   /* (A +- B) + (C - A)   -> C +- B */
3738   /* (A +  B) - (A - C)   -> B + C */
3739   /* More cases are handled with comparisons.  */
3740   (simplify
3741    (plus:c (plus:c @0 @1) (minus @2 @0))
3742    (plus @2 @1))
3743   (simplify
3744    (plus:c (minus @0 @1) (minus @2 @0))
3745    (minus @2 @1))
3746   (simplify
3747    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3748    (if (TYPE_OVERFLOW_UNDEFINED (type)
3749         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3750     (pointer_diff @2 @1)))
3751   (simplify
3752    (minus (plus:c @0 @1) (minus @0 @2))
3753    (plus @1 @2))
3755   /* (A +- CST1) +- CST2 -> A + CST3
3756      Use view_convert because it is safe for vectors and equivalent for
3757      scalars.  */
3758   (for outer_op (plus minus)
3759    (for inner_op (plus minus)
3760         neg_inner_op (minus plus)
3761     (simplify
3762      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3763                CONSTANT_CLASS_P@2)
3764      /* If one of the types wraps, use that one.  */
3765      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3766       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3767          forever if something doesn't simplify into a constant.  */
3768       (if (!CONSTANT_CLASS_P (@0))
3769        (if (outer_op == PLUS_EXPR)
3770         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3771         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3772       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3773            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3774        (if (outer_op == PLUS_EXPR)
3775         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3776         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3777        /* If the constant operation overflows we cannot do the transform
3778           directly as we would introduce undefined overflow, for example
3779           with (a - 1) + INT_MIN.  */
3780        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3781         (with { tree cst = const_binop (outer_op == inner_op
3782                                         ? PLUS_EXPR : MINUS_EXPR,
3783                                         type, @1, @2); }
3784          (if (cst)
3785           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3786            (inner_op @0 { cst; } )
3787            /* X+INT_MAX+1 is X-INT_MIN.  */
3788            (if (INTEGRAL_TYPE_P (type)
3789                 && wi::to_wide (cst) == wi::min_value (type))
3790             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3791             /* Last resort, use some unsigned type.  */
3792             (with { tree utype = unsigned_type_for (type); }
3793              (if (utype)
3794               (view_convert (inner_op
3795                              (view_convert:utype @0)
3796                              (view_convert:utype
3797                               { TREE_OVERFLOW (cst)
3798                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3800   /* (CST1 - A) +- CST2 -> CST3 - A  */
3801   (for outer_op (plus minus)
3802    (simplify
3803     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3804     /* If one of the types wraps, use that one.  */
3805     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3806      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3807         forever if something doesn't simplify into a constant.  */
3808      (if (!CONSTANT_CLASS_P (@0))
3809       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3810      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3811           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3812       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3813       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3814        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3815         (if (cst && !TREE_OVERFLOW (cst))
3816          (minus { cst; } @0))))))))
3818   /* CST1 - (CST2 - A) -> CST3 + A
3819      Use view_convert because it is safe for vectors and equivalent for
3820      scalars.  */
3821   (simplify
3822    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3823    /* If one of the types wraps, use that one.  */
3824    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3825     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3826       forever if something doesn't simplify into a constant.  */
3827     (if (!CONSTANT_CLASS_P (@0))
3828      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3829     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3830          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3831      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3832      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3833       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3834        (if (cst && !TREE_OVERFLOW (cst))
3835         (plus { cst; } @0)))))))
3837 /* ((T)(A)) + CST -> (T)(A + CST)  */
3838 #if GIMPLE
3839   (simplify
3840    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3841     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3842          && TREE_CODE (type) == INTEGER_TYPE
3843          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3844          && int_fits_type_p (@1, TREE_TYPE (@0)))
3845      /* Perform binary operation inside the cast if the constant fits
3846         and (A + CST)'s range does not overflow.  */
3847      (with
3848       {
3849         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3850                           max_ovf = wi::OVF_OVERFLOW;
3851         tree inner_type = TREE_TYPE (@0);
3853         wide_int w1
3854           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3855                             TYPE_SIGN (inner_type));
3857         int_range_max vr;
3858         if (get_global_range_query ()->range_of_expr (vr, @0)
3859             && !vr.varying_p () && !vr.undefined_p ())
3860           {
3861             wide_int wmin0 = vr.lower_bound ();
3862             wide_int wmax0 = vr.upper_bound ();
3863             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3864             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3865           }
3866       }
3867      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3868       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3869      )))
3870 #endif
3872 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3873 #if GIMPLE
3874   (for op (plus minus)
3875    (simplify
3876     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3877      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3878           && TREE_CODE (type) == INTEGER_TYPE
3879           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3880           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3881           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3882           && TYPE_OVERFLOW_WRAPS (type))
3883        (plus (convert @0) (op @2 (convert @1))))))
3884 #endif
3886 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3887    to a simple value.  */
3888   (for op (plus minus)
3889    (simplify
3890     (op (convert @0) (convert @1))
3891      (if (INTEGRAL_TYPE_P (type)
3892           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3893           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3894           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3895           && !TYPE_OVERFLOW_TRAPS (type)
3896           && !TYPE_OVERFLOW_SANITIZED (type))
3897       (convert (op! @0 @1)))))
3899   /* ~A + A -> -1 */
3900   (simplify
3901    (plus:c (convert? (bit_not @0)) (convert? @0))
3902    (if (!TYPE_OVERFLOW_TRAPS (type))
3903     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3905   /* ~A + 1 -> -A */
3906   (simplify
3907    (plus (convert? (bit_not @0)) integer_each_onep)
3908    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3909     (negate (convert @0))))
3911   /* -A - 1 -> ~A */
3912   (simplify
3913    (minus (convert? (negate @0)) integer_each_onep)
3914    (if (!TYPE_OVERFLOW_TRAPS (type)
3915         && TREE_CODE (type) != COMPLEX_TYPE
3916         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3917     (bit_not (convert @0))))
3919   /* -1 - A -> ~A */
3920   (simplify
3921    (minus integer_all_onesp @0)
3922    (if (TREE_CODE (type) != COMPLEX_TYPE)
3923     (bit_not @0)))
3925   /* (T)(P + A) - (T)P -> (T) A */
3926   (simplify
3927    (minus (convert (plus:c @@0 @1))
3928     (convert? @0))
3929    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3930         /* For integer types, if A has a smaller type
3931            than T the result depends on the possible
3932            overflow in P + A.
3933            E.g. T=size_t, A=(unsigned)429497295, P>0.
3934            However, if an overflow in P + A would cause
3935            undefined behavior, we can assume that there
3936            is no overflow.  */
3937         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3938             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3939     (convert @1)))
3940   (simplify
3941    (minus (convert (pointer_plus @@0 @1))
3942     (convert @0))
3943    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3944         /* For pointer types, if the conversion of A to the
3945            final type requires a sign- or zero-extension,
3946            then we have to punt - it is not defined which
3947            one is correct.  */
3948         || (POINTER_TYPE_P (TREE_TYPE (@0))
3949             && TREE_CODE (@1) == INTEGER_CST
3950             && tree_int_cst_sign_bit (@1) == 0))
3951     (convert @1)))
3952    (simplify
3953     (pointer_diff (pointer_plus @@0 @1) @0)
3954     /* The second argument of pointer_plus must be interpreted as signed, and
3955        thus sign-extended if necessary.  */
3956     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3957      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3958         second arg is unsigned even when we need to consider it as signed,
3959         we don't want to diagnose overflow here.  */
3960      (convert (view_convert:stype @1))))
3962   /* (T)P - (T)(P + A) -> -(T) A */
3963   (simplify
3964    (minus (convert? @0)
3965     (convert (plus:c @@0 @1)))
3966    (if (INTEGRAL_TYPE_P (type)
3967         && TYPE_OVERFLOW_UNDEFINED (type)
3968         /* For integer literals, using an intermediate unsigned type to avoid
3969            an overflow at run time is counter-productive because it introduces
3970            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3971            the result, which may be problematic in GENERIC for some front-ends:
3972              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3973            so we use the direct path for them.  */
3974         && TREE_CODE (@1) != INTEGER_CST
3975         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3976     (with { tree utype = unsigned_type_for (type); }
3977      (convert (negate (convert:utype @1))))
3978     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3979          /* For integer types, if A has a smaller type
3980             than T the result depends on the possible
3981             overflow in P + A.
3982             E.g. T=size_t, A=(unsigned)429497295, P>0.
3983             However, if an overflow in P + A would cause
3984             undefined behavior, we can assume that there
3985             is no overflow.  */
3986          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3987              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3988      (negate (convert @1)))))
3989   (simplify
3990    (minus (convert @0)
3991     (convert (pointer_plus @@0 @1)))
3992    (if (INTEGRAL_TYPE_P (type)
3993         && TYPE_OVERFLOW_UNDEFINED (type)
3994         /* See above the rationale for this condition.  */
3995         && TREE_CODE (@1) != INTEGER_CST
3996         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3997     (with { tree utype = unsigned_type_for (type); }
3998      (convert (negate (convert:utype @1))))
3999     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4000          /* For pointer types, if the conversion of A to the
4001             final type requires a sign- or zero-extension,
4002             then we have to punt - it is not defined which
4003             one is correct.  */
4004          || (POINTER_TYPE_P (TREE_TYPE (@0))
4005              && TREE_CODE (@1) == INTEGER_CST
4006              && tree_int_cst_sign_bit (@1) == 0))
4007      (negate (convert @1)))))
4008    (simplify
4009     (pointer_diff @0 (pointer_plus @@0 @1))
4010     /* The second argument of pointer_plus must be interpreted as signed, and
4011        thus sign-extended if necessary.  */
4012     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4013      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4014         second arg is unsigned even when we need to consider it as signed,
4015         we don't want to diagnose overflow here.  */
4016      (negate (convert (view_convert:stype @1)))))
4018   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
4019   (simplify
4020    (minus (convert (plus:c @@0 @1))
4021     (convert (plus:c @0 @2)))
4022    (if (INTEGRAL_TYPE_P (type)
4023         && TYPE_OVERFLOW_UNDEFINED (type)
4024         && element_precision (type) <= element_precision (TREE_TYPE (@1))
4025         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
4026     (with { tree utype = unsigned_type_for (type); }
4027      (convert (minus (convert:utype @1) (convert:utype @2))))
4028     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
4029           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
4030          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
4031              /* For integer types, if A has a smaller type
4032                 than T the result depends on the possible
4033                 overflow in P + A.
4034                 E.g. T=size_t, A=(unsigned)429497295, P>0.
4035                 However, if an overflow in P + A would cause
4036                 undefined behavior, we can assume that there
4037                 is no overflow.  */
4038              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
4039                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4040                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
4041                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
4042      (minus (convert @1) (convert @2)))))
4043   (simplify
4044    (minus (convert (pointer_plus @@0 @1))
4045     (convert (pointer_plus @0 @2)))
4046    (if (INTEGRAL_TYPE_P (type)
4047         && TYPE_OVERFLOW_UNDEFINED (type)
4048         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
4049     (with { tree utype = unsigned_type_for (type); }
4050      (convert (minus (convert:utype @1) (convert:utype @2))))
4051     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4052          /* For pointer types, if the conversion of A to the
4053             final type requires a sign- or zero-extension,
4054             then we have to punt - it is not defined which
4055             one is correct.  */
4056          || (POINTER_TYPE_P (TREE_TYPE (@0))
4057              && TREE_CODE (@1) == INTEGER_CST
4058              && tree_int_cst_sign_bit (@1) == 0
4059              && TREE_CODE (@2) == INTEGER_CST
4060              && tree_int_cst_sign_bit (@2) == 0))
4061      (minus (convert @1) (convert @2)))))
4062    (simplify
4063     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
4064      (pointer_diff @0 @1))
4065    (simplify
4066     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
4067     /* The second argument of pointer_plus must be interpreted as signed, and
4068        thus sign-extended if necessary.  */
4069     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4070      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4071         second arg is unsigned even when we need to consider it as signed,
4072         we don't want to diagnose overflow here.  */
4073      (minus (convert (view_convert:stype @1))
4074             (convert (view_convert:stype @2)))))))
4076 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
4077     Modeled after fold_plusminus_mult_expr.  */
4078 (if (!TYPE_SATURATING (type)
4079      && (!FLOAT_TYPE_P (type) || flag_associative_math))
4080  (for plusminus (plus minus)
4081   (simplify
4082    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
4083    (if (!ANY_INTEGRAL_TYPE_P (type)
4084         || TYPE_OVERFLOW_WRAPS (type)
4085         || (INTEGRAL_TYPE_P (type)
4086             && tree_expr_nonzero_p (@0)
4087             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
4088     (if (single_use (@3) || single_use (@4))
4089      /* If @1 +- @2 is constant require a hard single-use on either
4090         original operand (but not on both).  */
4091      (mult (plusminus @1 @2) @0)
4092      (mult! (plusminus @1 @2) @0)
4093   )))
4094   /* We cannot generate constant 1 for fract.  */
4095   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
4096    (simplify
4097     (plusminus @0 (mult:c@3 @0 @2))
4098     (if ((!ANY_INTEGRAL_TYPE_P (type)
4099           || TYPE_OVERFLOW_WRAPS (type)
4100           /* For @0 + @0*@2 this transformation would introduce UB
4101              (where there was none before) for @0 in [-1,0] and @2 max.
4102              For @0 - @0*@2 this transformation would introduce UB
4103              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
4104           || (INTEGRAL_TYPE_P (type)
4105               && ((tree_expr_nonzero_p (@0)
4106                    && expr_not_equal_to (@0,
4107                                 wi::minus_one (TYPE_PRECISION (type))))
4108                   || (plusminus == PLUS_EXPR
4109                       ? expr_not_equal_to (@2,
4110                             wi::max_value (TYPE_PRECISION (type), SIGNED))
4111                       /* Let's ignore the @0 -1 and @2 min case.  */
4112                       : (expr_not_equal_to (@2,
4113                             wi::min_value (TYPE_PRECISION (type), SIGNED))
4114                          && expr_not_equal_to (@2,
4115                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
4116                                 + 1))))))
4117          && single_use (@3))
4118      (mult (plusminus { build_one_cst (type); } @2) @0)))
4119    (simplify
4120     (plusminus (mult:c@3 @0 @2) @0)
4121     (if ((!ANY_INTEGRAL_TYPE_P (type)
4122           || TYPE_OVERFLOW_WRAPS (type)
4123           /* For @0*@2 + @0 this transformation would introduce UB
4124              (where there was none before) for @0 in [-1,0] and @2 max.
4125              For @0*@2 - @0 this transformation would introduce UB
4126              for @0 0 and @2 min.  */
4127           || (INTEGRAL_TYPE_P (type)
4128               && ((tree_expr_nonzero_p (@0)
4129                    && (plusminus == MINUS_EXPR
4130                        || expr_not_equal_to (@0,
4131                                 wi::minus_one (TYPE_PRECISION (type)))))
4132                   || expr_not_equal_to (@2,
4133                         (plusminus == PLUS_EXPR
4134                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
4135                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
4136          && single_use (@3))
4137      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
4139 #if GIMPLE
4140 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
4141    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
4142 (simplify
4143  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
4144   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4145        && tree_fits_uhwi_p (@1)
4146        && tree_to_uhwi (@1) < element_precision (type)
4147        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4148            || optab_handler (smul_optab,
4149                              TYPE_MODE (type)) != CODE_FOR_nothing))
4150    (with { tree t = type;
4151            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
4152            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
4153                                              element_precision (type));
4154            w += 1;
4155            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
4156                                         : t, w);
4157            cst = build_uniform_cst (t, cst); }
4158     (convert (mult (convert:t @0) { cst; })))))
4159 (simplify
4160  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
4161   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4162        && tree_fits_uhwi_p (@1)
4163        && tree_to_uhwi (@1) < element_precision (type)
4164        && tree_fits_uhwi_p (@2)
4165        && tree_to_uhwi (@2) < element_precision (type)
4166        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4167            || optab_handler (smul_optab,
4168                              TYPE_MODE (type)) != CODE_FOR_nothing))
4169    (with { tree t = type;
4170            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
4171            unsigned int prec = element_precision (type);
4172            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
4173            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
4174            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
4175                                         : t, w);
4176            cst = build_uniform_cst (t, cst); }
4177     (convert (mult (convert:t @0) { cst; })))))
4178 #endif
4180 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
4181    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
4182    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
4183 (for op (bit_ior bit_xor)
4184  (simplify
4185   (op (nop_convert?:s@6 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4186       (nop_convert?:s@7 (mult:s@3 (nop_convert? @5) INTEGER_CST@4)))
4187   (if (INTEGRAL_TYPE_P (type)
4188        && operand_equal_p (@1, @5, 0)
4189        && (tree_nonzero_bits (@6) & tree_nonzero_bits (@7)) == 0)
4190    (with { tree t = type;
4191            if (!TYPE_OVERFLOW_WRAPS (t))
4192              t = unsigned_type_for (t);
4193            wide_int c = wi::add (wi::to_wide (@2), wi::to_wide (@4)); }
4194     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4195  (simplify
4196   (op:c (nop_convert?:s@5 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4197         (lshift:s@3 @1 INTEGER_CST@4))
4198   (if (INTEGRAL_TYPE_P (type)
4199        && tree_int_cst_sgn (@4) > 0
4200        && (tree_nonzero_bits (@5) & tree_nonzero_bits (@3)) == 0)
4201    (with { tree t = type;
4202            if (!TYPE_OVERFLOW_WRAPS (t))
4203              t = unsigned_type_for (t);
4204            wide_int wone = wi::one (TYPE_PRECISION (type));
4205            wide_int c = wi::add (wi::to_wide (@2),
4206                                  wi::lshift (wone, wi::to_wide (@4))); }
4207     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4208  (simplify
4209   (op:c (nop_convert?:s@3 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4210         @1)
4211   (if (INTEGRAL_TYPE_P (type)
4212        && (tree_nonzero_bits (@3) & tree_nonzero_bits (@1)) == 0)
4213    (with { tree t = type;
4214            if (!TYPE_OVERFLOW_WRAPS (t))
4215              t = unsigned_type_for (t);
4216            wide_int c = wi::add (wi::to_wide (@2), 1); }
4217     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4218  (simplify
4219   (op (lshift:s@0 @1 INTEGER_CST@2)
4220       (lshift:s@3 @1 INTEGER_CST@4))
4221   (if (INTEGRAL_TYPE_P (type)
4222        && tree_int_cst_sgn (@2) > 0
4223        && tree_int_cst_sgn (@4) > 0
4224        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
4225    (with { tree t = type;
4226            if (!TYPE_OVERFLOW_WRAPS (t))
4227              t = unsigned_type_for (t);
4228            wide_int wone = wi::one (TYPE_PRECISION (t));
4229            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
4230                                  wi::lshift (wone, wi::to_wide (@4))); }
4231     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
4232  (simplify
4233   (op:c (lshift:s@0 @1 INTEGER_CST@2)
4234         @1)
4235   (if (INTEGRAL_TYPE_P (type)
4236        && tree_int_cst_sgn (@2) > 0
4237        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
4238    (with { tree t = type;
4239            if (!TYPE_OVERFLOW_WRAPS (t))
4240              t = unsigned_type_for (t);
4241            wide_int wone = wi::one (TYPE_PRECISION (t));
4242            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
4243     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
4245 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
4247 (for minmax (min max)
4248  (simplify
4249   (minmax @0 @0)
4250   @0)
4251 /* max(max(x,y),x) -> max(x,y)  */
4252  (simplify
4253   (minmax:c (minmax:c@2 @0 @1) @0)
4254   @2))
4255 /* For fmin() and fmax(), skip folding when both are sNaN.  */
4256 (for minmax (FMIN_ALL FMAX_ALL)
4257  (simplify
4258   (minmax @0 @0)
4259   (if (!tree_expr_maybe_signaling_nan_p (@0))
4260     @0)))
4261 /* min(max(x,y),y) -> y.  */
4262 (simplify
4263  (min:c (max:c @0 @1) @1)
4264  @1)
4265 /* max(min(x,y),y) -> y.  */
4266 (simplify
4267  (max:c (min:c @0 @1) @1)
4268  @1)
4269 /* max(a,-a) -> abs(a).  */
4270 (simplify
4271  (max:c @0 (negate @0))
4272  (if (TREE_CODE (type) != COMPLEX_TYPE
4273       && (! ANY_INTEGRAL_TYPE_P (type)
4274           || TYPE_OVERFLOW_UNDEFINED (type)))
4275   (abs @0)))
4276 /* min(a,-a) -> -abs(a).  */
4277 (simplify
4278  (min:c @0 (negate @0))
4279  (if (TREE_CODE (type) != COMPLEX_TYPE
4280       && (! ANY_INTEGRAL_TYPE_P (type)
4281           || TYPE_OVERFLOW_UNDEFINED (type)))
4282   (negate (abs @0))))
4283 (simplify
4284  (min @0 @1)
4285  (switch
4286   (if (INTEGRAL_TYPE_P (type)
4287        && TYPE_MIN_VALUE (type)
4288        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4289    @1)
4290   (if (INTEGRAL_TYPE_P (type)
4291        && TYPE_MAX_VALUE (type)
4292        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4293    @0)))
4294 (simplify
4295  (max @0 @1)
4296  (switch
4297   (if (INTEGRAL_TYPE_P (type)
4298        && TYPE_MAX_VALUE (type)
4299        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4300    @1)
4301   (if (INTEGRAL_TYPE_P (type)
4302        && TYPE_MIN_VALUE (type)
4303        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4304    @0)))
4306 /* max (a, a + CST) -> a + CST where CST is positive.  */
4307 /* max (a, a + CST) -> a where CST is negative.  */
4308 (simplify
4309  (max:c @0 (plus@2 @0 INTEGER_CST@1))
4310   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4311    (if (tree_int_cst_sgn (@1) > 0)
4312     @2
4313     @0)))
4315 /* min (a, a + CST) -> a where CST is positive.  */
4316 /* min (a, a + CST) -> a + CST where CST is negative. */
4317 (simplify
4318  (min:c @0 (plus@2 @0 INTEGER_CST@1))
4319   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4320    (if (tree_int_cst_sgn (@1) > 0)
4321     @0
4322     @2)))
4324 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4325    the addresses are known to be less, equal or greater.  */
4326 (for minmax (min max)
4327      cmp (lt gt)
4328  (simplify
4329   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4330   (with
4331    {
4332      poly_int64 off0, off1;
4333      tree base0, base1;
4334      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4335                                   off0, off1, GENERIC);
4336    }
4337    (if (equal == 1)
4338     (if (minmax == MIN_EXPR)
4339      (if (known_le (off0, off1))
4340       @2
4341       (if (known_gt (off0, off1))
4342        @3))
4343      (if (known_ge (off0, off1))
4344       @2
4345       (if (known_lt (off0, off1))
4346        @3)))))))
4348 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4349    and the outer convert demotes the expression back to x's type.  */
4350 (for minmax (min max)
4351  (simplify
4352   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4353   (if (INTEGRAL_TYPE_P (type)
4354        && types_match (@1, type) && int_fits_type_p (@2, type)
4355        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4356        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4357    (minmax @1 (convert @2)))))
4359 (for minmax (FMIN_ALL FMAX_ALL)
4360  /* If either argument is NaN and other one is not sNaN, return the other
4361     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4362  (simplify
4363   (minmax:c @0 REAL_CST@1)
4364    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4365        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4366        && !tree_expr_maybe_signaling_nan_p (@0))
4367    @0)))
4368 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4369    functions to return the numeric arg if the other one is NaN.
4370    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4371    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4372    worry about it either.  */
4373 (if (flag_finite_math_only)
4374  (simplify
4375   (FMIN_ALL @0 @1)
4376   (min @0 @1))
4377  (simplify
4378   (FMAX_ALL @0 @1)
4379   (max @0 @1)))
4380 /* min (-A, -B) -> -max (A, B)  */
4381 (for minmax (min max FMIN_ALL FMAX_ALL)
4382      maxmin (max min FMAX_ALL FMIN_ALL)
4383  (simplify
4384   (minmax (negate:s@2 @0) (negate:s@3 @1))
4385   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4386        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4387            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4388    (negate (maxmin @0 @1)))))
4389 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4390    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4391 (for minmax (min max)
4392  maxmin (max min)
4393  (simplify
4394   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4395   (bit_not (maxmin @0 @1)))
4396 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4397 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4398  (simplify
4399   (bit_not (minmax:cs (bit_not @0) @1))
4400   (maxmin @0 (bit_not @1))))
4402 /* MIN (X, Y) == X -> X <= Y  */
4403 /* MIN (X, Y) < X -> X > Y  */
4404 /* MIN (X, Y) >= X -> X <= Y  */
4405 (for minmax (min min min min max max max max)
4406      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4407      out    (le  gt  gt  le  ge  lt  lt  ge )
4408  (simplify
4409   (cmp:c (minmax:c @0 @1) @0)
4410   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4411    (out @0 @1))))
4412 /* MIN (X, 5) == 0 -> X == 0
4413    MIN (X, 5) == 7 -> false  */
4414 (for cmp (eq ne)
4415  (simplify
4416   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4417   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4418                  TYPE_SIGN (TREE_TYPE (@0))))
4419    { constant_boolean_node (cmp == NE_EXPR, type); }
4420    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4421                   TYPE_SIGN (TREE_TYPE (@0))))
4422     (cmp @0 @2)))))
4423 (for cmp (eq ne)
4424  (simplify
4425   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4426   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4427                  TYPE_SIGN (TREE_TYPE (@0))))
4428    { constant_boolean_node (cmp == NE_EXPR, type); }
4429    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4430                   TYPE_SIGN (TREE_TYPE (@0))))
4431     (cmp @0 @2)))))
4433 /* X <= MAX(X, Y) -> true
4434    X > MAX(X, Y) -> false 
4435    X >= MIN(X, Y) -> true
4436    X < MIN(X, Y) -> false */
4437 (for minmax (min     min     max     max     )
4438      cmp    (ge      lt      le      gt      )
4439  (simplify
4440   (cmp:c @0 (minmax:c @0 @1))
4441   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4443 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4444 (for minmax (min     min     max     max     min     min     max     max    )
4445      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4446      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4447  (simplify
4448   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4449   (comb (cmp @0 @2) (cmp @1 @2))))
4451 /* Undo fancy ways of writing max/min or other ?: expressions, like
4452    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4453    People normally use ?: and that is what we actually try to optimize.  */
4454 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4455 (simplify
4456  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4457  (if (INTEGRAL_TYPE_P (type)
4458       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4459   (cond (convert:boolean_type_node @2) @1 @0)))
4460 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4461 (simplify
4462  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4463  (if (INTEGRAL_TYPE_P (type)
4464       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4465   (cond (convert:boolean_type_node @2) @1 @0)))
4466 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4467 (simplify
4468  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4469  (if (INTEGRAL_TYPE_P (type)
4470       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4471   (cond (convert:boolean_type_node @2) @1 @0)))
4473 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4474 (simplify
4475  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4476  (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
4477   (max @2 @1)))
4479 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4480 (for op (bit_xor bit_ior plus)
4481  (simplify
4482   (cond (eq zero_one_valued_p@0
4483             integer_zerop)
4484         @1
4485         (op:c @2 @1))
4486   (if (INTEGRAL_TYPE_P (type)
4487        && TYPE_PRECISION (type) > 1
4488        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4489        (op (mult (convert:type @0) @2) @1))))
4491 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4492 (for op (bit_xor bit_ior plus)
4493  (simplify
4494   (cond (ne zero_one_valued_p@0
4495             integer_zerop)
4496        (op:c @2 @1)
4497         @1)
4498   (if (INTEGRAL_TYPE_P (type)
4499        && TYPE_PRECISION (type) > 1
4500        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4501        (op (mult (convert:type @0) @2) @1))))
4503 /* ?: Value replacement. */
4504 /* a == 0 ? b : b + a  -> b + a */
4505 (for op (plus bit_ior bit_xor)
4506  (simplify
4507   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4508    @2))
4509 /* a == 0 ? b : b - a  -> b - a */
4510 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4511 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4512 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4513  (simplify
4514   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4515    @2))
4517 /* a == 1 ? b : b / a  -> b / a */
4518 (for op (trunc_div ceil_div floor_div round_div exact_div)
4519  (simplify
4520   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4521    @2))
4523 /* a == 1 ? b : a * b -> a * b */
4524 (for op (mult)
4525  (simplify
4526   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4527    @2))
4529 /* a == -1 ? b : a & b -> a & b */
4530 (for op (bit_and)
4531  (simplify
4532   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4533    @2))
4535 /* a != 0 ? a / b : 0  -> a / b iff b is nonzero. */
4536 (for op (trunc_div ceil_div floor_div round_div exact_div)
4537  (simplify
4538   (cond (ne @0 integer_zerop) (op@2 @3 @1) integer_zerop )
4539    (if (bitwise_equal_p (@0, @3)
4540         && tree_expr_nonzero_p (@1))
4541     @2)))
4543 /* Note we prefer the != case here
4544    as (a != 0) * (a * b) will generate that version. */
4545 /* a != 0 ? a * b : 0 -> a * b */
4546 /* a != 0 ? a & b : 0 -> a & b */
4547 (for op (mult bit_and)
4548  (simplify
4549   (cond (ne @0 integer_zerop) (op:c@2 @1 @3) integer_zerop)
4550   (if (bitwise_equal_p (@0, @3))
4551    @2)))
4553 /* Simplifications of shift and rotates.  */
4555 (for rotate (lrotate rrotate)
4556  (simplify
4557   (rotate integer_all_onesp@0 @1)
4558   @0))
4560 /* Optimize -1 >> x for arithmetic right shifts.  */
4561 (simplify
4562  (rshift integer_all_onesp@0 @1)
4563  (if (!TYPE_UNSIGNED (type))
4564   @0))
4566 /* Optimize (x >> c) << c into x & (-1<<c).  */
4567 (simplify
4568  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4569  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4570   /* It doesn't matter if the right shift is arithmetic or logical.  */
4571   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4573 (simplify
4574  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4575  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4576       /* Allow intermediate conversion to integral type with whatever sign, as
4577          long as the low TYPE_PRECISION (type)
4578          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4579       && INTEGRAL_TYPE_P (type)
4580       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4581       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4582       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4583       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4584           || wi::geu_p (wi::to_wide (@1),
4585                         TYPE_PRECISION (type)
4586                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4587   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4589 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4590    unsigned x OR truncate into the precision(type) - c lowest bits
4591    of signed x (if they have mode precision or a precision of 1).  */
4592 (simplify
4593  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4594  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4595   (if (TYPE_UNSIGNED (type))
4596    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4597    (if (INTEGRAL_TYPE_P (type))
4598     (with {
4599       int width = element_precision (type) - tree_to_uhwi (@1);
4600       tree stype = NULL_TREE;
4601       if (width <= MAX_FIXED_MODE_SIZE)
4602         stype = build_nonstandard_integer_type (width, 0);
4603      }
4604      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4605       (convert (convert:stype @0))))))))
4607 /* Optimize x >> x into 0 */
4608 (simplify
4609  (rshift @0 @0)
4610   { build_zero_cst (type); })
4612 (for shiftrotate (lrotate rrotate lshift rshift)
4613  (simplify
4614   (shiftrotate @0 integer_zerop)
4615   (non_lvalue @0))
4616  (simplify
4617   (shiftrotate integer_zerop@0 @1)
4618   @0)
4619  /* Prefer vector1 << scalar to vector1 << vector2
4620     if vector2 is uniform.  */
4621  (for vec (VECTOR_CST CONSTRUCTOR)
4622   (simplify
4623    (shiftrotate @0 vec@1)
4624    (with { tree tem = uniform_vector_p (@1); }
4625     (if (tem)
4626      (shiftrotate @0 { tem; }))))))
4628 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4629    Y is 0.  Similarly for X >> Y.  */
4630 #if GIMPLE
4631 (for shift (lshift rshift)
4632  (simplify
4633   (shift @0 SSA_NAME@1)
4634    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4635     (with {
4636       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4637       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4638      }
4639      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4640       @0)))))
4641 #endif
4643 /* Rewrite an LROTATE_EXPR by a constant into an
4644    RROTATE_EXPR by a new constant.  */
4645 (simplify
4646  (lrotate @0 INTEGER_CST@1)
4647  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4648                             build_int_cst (TREE_TYPE (@1),
4649                                            element_precision (type)), @1); }))
4651 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4652 (for op (lrotate rrotate rshift lshift)
4653  (simplify
4654   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4655   (with { unsigned int prec = element_precision (type); }
4656    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4657         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4658         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4659         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4660     (with { unsigned int low = (tree_to_uhwi (@1)
4661                                 + tree_to_uhwi (@2)); }
4662      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4663         being well defined.  */
4664      (if (low >= prec)
4665       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4666        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4667        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4668         { build_zero_cst (type); }
4669         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4670       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4673 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4674 (simplify
4675  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4676   (if ((wi::to_wide (@1) & 1) != 0)
4677    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4678    { build_zero_cst (type); }))
4680 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4681    either to false if D is smaller (unsigned comparison) than C, or to
4682    x == log2 (D) - log2 (C).  Similarly for right shifts.
4683    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4684 (for cmp (ne eq)
4685      icmp (eq ne)
4686  (simplify
4687   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4688    (with { int c1 = wi::clz (wi::to_wide (@1));
4689            int c2 = wi::clz (wi::to_wide (@2)); }
4690     (if (c1 < c2)
4691      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4692      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4693  (simplify
4694   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4695    (if (tree_int_cst_sgn (@1) > 0)
4696     (with { int c1 = wi::clz (wi::to_wide (@1));
4697             int c2 = wi::clz (wi::to_wide (@2)); }
4698      (if (c1 > c2)
4699       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4700       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4701  /* `(1 >> X) != 0` -> `X == 0` */
4702  /* `(1 >> X) == 0` -> `X != 0` */
4703  (simplify
4704   (cmp (rshift integer_onep@1 @0) integer_zerop)
4705    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4706     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4708 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4709    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4710    if CST2 != 0.  */
4711 (for cmp (ne eq)
4712  (simplify
4713   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4714   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4715    (if (cand < 0
4716         || (!integer_zerop (@2)
4717             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4718     { constant_boolean_node (cmp == NE_EXPR, type); }
4719     (if (!integer_zerop (@2)
4720          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4721      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4723 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4724         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4725 (for cmp (ne eq)
4726  (simplify
4727   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4728   (if (tree_fits_shwi_p (@1)
4729        && tree_to_shwi (@1) > 0
4730        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4731     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4732       { constant_boolean_node (cmp == NE_EXPR, type); }
4733       (with { wide_int c1 = wi::to_wide (@1);
4734               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4735               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4736         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4737              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4738  (simplify
4739   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4740   (if (tree_fits_shwi_p (@1)
4741        && tree_to_shwi (@1) > 0
4742        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4743     (with { tree t0 = TREE_TYPE (@0);
4744             unsigned int prec = TYPE_PRECISION (t0);
4745             wide_int c1 = wi::to_wide (@1);
4746             wide_int c2 = wi::to_wide (@2);
4747             wide_int c3 = wi::to_wide (@3);
4748             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4749       (if ((c2 & c3) != c3)
4750         { constant_boolean_node (cmp == NE_EXPR, type); }
4751         (if (TYPE_UNSIGNED (t0))
4752           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4753             { constant_boolean_node (cmp == NE_EXPR, type); }
4754             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4755                  { wide_int_to_tree (t0, c3 << c1); }))
4756           (with { wide_int smask = wi::arshift (sb, c1); }
4757             (switch
4758               (if ((c2 & smask) == 0)
4759                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4760                      { wide_int_to_tree (t0, c3 << c1); }))
4761               (if ((c3 & smask) == 0)
4762                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4763                      { wide_int_to_tree (t0, c3 << c1); }))
4764               (if ((c2 & smask) != (c3 & smask))
4765                 { constant_boolean_node (cmp == NE_EXPR, type); })
4766               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4767                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4769 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4770         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4771    if the new mask might be further optimized.  */
4772 (for shift (lshift rshift)
4773  (simplify
4774   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4775            INTEGER_CST@2)
4776    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4777         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4778         && tree_fits_uhwi_p (@1)
4779         && tree_to_uhwi (@1) > 0
4780         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4781     (with
4782      {
4783        unsigned int shiftc = tree_to_uhwi (@1);
4784        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4785        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4786        tree shift_type = TREE_TYPE (@3);
4787        unsigned int prec;
4789        if (shift == LSHIFT_EXPR)
4790          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4791        else if (shift == RSHIFT_EXPR
4792                 && type_has_mode_precision_p (shift_type))
4793          {
4794            prec = TYPE_PRECISION (TREE_TYPE (@3));
4795            tree arg00 = @0;
4796            /* See if more bits can be proven as zero because of
4797               zero extension.  */
4798            if (@3 != @0
4799                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4800              {
4801                tree inner_type = TREE_TYPE (@0);
4802                if (type_has_mode_precision_p (inner_type)
4803                    && TYPE_PRECISION (inner_type) < prec)
4804                  {
4805                    prec = TYPE_PRECISION (inner_type);
4806                    /* See if we can shorten the right shift.  */
4807                    if (shiftc < prec)
4808                      shift_type = inner_type;
4809                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4810                       it into (X, 0) later on by making sure zerobits
4811                       is all ones.  */
4812                  }
4813              }
4814            zerobits = HOST_WIDE_INT_M1U;
4815            if (shiftc < prec)
4816              {
4817                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4818                zerobits <<= prec - shiftc;
4819              }
4820            /* For arithmetic shift if sign bit could be set, zerobits
4821               can contain actually sign bits, so no transformation is
4822               possible, unless MASK masks them all away.  In that
4823               case the shift needs to be converted into logical shift.  */
4824            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4825                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4826              {
4827                if ((mask & zerobits) == 0)
4828                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4829                else
4830                  zerobits = 0;
4831              }
4832          }
4833      }
4834      /* ((X << 16) & 0xff00) is (X, 0).  */
4835      (if ((mask & zerobits) == mask)
4836       { build_int_cst (type, 0); }
4837       (with { newmask = mask | zerobits; }
4838        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4839         (with
4840          {
4841            /* Only do the transformation if NEWMASK is some integer
4842               mode's mask.  */
4843            for (prec = BITS_PER_UNIT;
4844                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4845              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4846                break;
4847          }
4848          (if (prec < HOST_BITS_PER_WIDE_INT
4849               || newmask == HOST_WIDE_INT_M1U)
4850           (with
4851            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4852            (if (!tree_int_cst_equal (newmaskt, @2))
4853             (if (shift_type != TREE_TYPE (@3))
4854              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4855              (bit_and @4 { newmaskt; })))))))))))))
4857 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4858 (for cmp (ne eq)
4859        icmp (eq ne)
4860  (simplify
4861   (cmp
4862    (bit_and
4863     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4864   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4865    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4866                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4868 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4869    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4870 (for shift (lshift rshift)
4871  (for bit_op (bit_and bit_xor bit_ior)
4872   (simplify
4873    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4874    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4875     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4876      (if (mask)
4877       (bit_op (shift (convert @0) @1) { mask; })))))))
4879 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4880 (simplify
4881  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4882   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4883        && (element_precision (TREE_TYPE (@0))
4884            <= element_precision (TREE_TYPE (@1))
4885            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4886    (with
4887     { tree shift_type = TREE_TYPE (@0); }
4888      (convert (rshift (convert:shift_type @1) @2)))))
4890 /* ~(~X >>r Y) -> X >>r Y
4891    ~(~X <<r Y) -> X <<r Y */
4892 (for rotate (lrotate rrotate)
4893  (simplify
4894   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4895    (if ((element_precision (TREE_TYPE (@0))
4896          <= element_precision (TREE_TYPE (@1))
4897          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4898         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4899             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4900     (with
4901      { tree rotate_type = TREE_TYPE (@0); }
4902       (convert (rotate (convert:rotate_type @1) @2))))))
4904 (for cmp (eq ne)
4905  (for rotate (lrotate rrotate)
4906       invrot (rrotate lrotate)
4907   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4908   (simplify
4909    (cmp (rotate @1 @0) (rotate @2 @0))
4910    (cmp @1 @2))
4911   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4912   (simplify
4913    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4914    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4915   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4916   (simplify
4917    (cmp (rotate @0 @1) INTEGER_CST@2)
4918     (if (integer_zerop (@2) || integer_all_onesp (@2))
4919      (cmp @0 @2)))))
4921 /* Narrow a lshift by constant.  */
4922 (simplify
4923  (convert (lshift:s@0 @1 INTEGER_CST@2))
4924  (if (INTEGRAL_TYPE_P (type)
4925       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4926       && !integer_zerop (@2)
4927       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4928   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4929        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4930    (lshift (convert @1) @2)
4931    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4932     { build_zero_cst (type); }))))
4934 /* Simplifications of conversions.  */
4936 /* Basic strip-useless-type-conversions / strip_nops.  */
4937 (for cvt (convert view_convert float fix_trunc)
4938  (simplify
4939   (cvt @0)
4940   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4941        || (GENERIC && type == TREE_TYPE (@0)))
4942    @0)))
4944 /* Contract view-conversions.  */
4945 (simplify
4946   (view_convert (view_convert @0))
4947   (view_convert @0))
4949 /* For integral conversions with the same precision or pointer
4950    conversions use a NOP_EXPR instead.  */
4951 (simplify
4952   (view_convert @0)
4953   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4954        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4955        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4956    (convert @0)))
4958 /* Strip inner integral conversions that do not change precision or size, or
4959    zero-extend while keeping the same size (for bool-to-char).  */
4960 (simplify
4961   (view_convert (convert@0 @1))
4962   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4963        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4964        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4965        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4966            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4967                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4968    (view_convert @1)))
4970 /* Simplify a view-converted empty or single-element constructor.  */
4971 (simplify
4972   (view_convert CONSTRUCTOR@0)
4973   (with
4974    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4975                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4976    (switch
4977     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4978      { build_zero_cst (type); })
4979     (if (CONSTRUCTOR_NELTS (ctor) == 1
4980          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4981          && operand_equal_p (TYPE_SIZE (type),
4982                              TYPE_SIZE (TREE_TYPE
4983                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4984      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4986 /* Re-association barriers around constants and other re-association
4987    barriers can be removed.  */
4988 (simplify
4989  (paren CONSTANT_CLASS_P@0)
4990  @0)
4991 (simplify
4992  (paren (paren@1 @0))
4993  @1)
4995 /* Handle cases of two conversions in a row.  */
4996 (for ocvt (convert float fix_trunc)
4997  (for icvt (convert float)
4998   (simplify
4999    (ocvt (icvt@1 @0))
5000    (with
5001     {
5002       tree inside_type = TREE_TYPE (@0);
5003       tree inter_type = TREE_TYPE (@1);
5004       int inside_int = INTEGRAL_TYPE_P (inside_type);
5005       int inside_ptr = POINTER_TYPE_P (inside_type);
5006       int inside_float = FLOAT_TYPE_P (inside_type);
5007       int inside_vec = VECTOR_TYPE_P (inside_type);
5008       unsigned int inside_prec = element_precision (inside_type);
5009       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5010       int inter_int = INTEGRAL_TYPE_P (inter_type);
5011       int inter_ptr = POINTER_TYPE_P (inter_type);
5012       int inter_float = FLOAT_TYPE_P (inter_type);
5013       int inter_vec = VECTOR_TYPE_P (inter_type);
5014       unsigned int inter_prec = element_precision (inter_type);
5015       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5016       int final_int = INTEGRAL_TYPE_P (type);
5017       int final_ptr = POINTER_TYPE_P (type);
5018       int final_float = FLOAT_TYPE_P (type);
5019       int final_vec = VECTOR_TYPE_P (type);
5020       unsigned int final_prec = element_precision (type);
5021       int final_unsignedp = TYPE_UNSIGNED (type);
5022     }
5023    (switch
5024     /* In addition to the cases of two conversions in a row
5025        handled below, if we are converting something to its own
5026        type via an object of identical or wider precision, neither
5027        conversion is needed.  */
5028     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
5029           || (GENERIC
5030               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
5031          && (((inter_int || inter_ptr) && final_int)
5032              || (inter_float && final_float))
5033          && inter_prec >= final_prec)
5034      (ocvt @0))
5036     /* Likewise, if the intermediate and initial types are either both
5037        float or both integer, we don't need the middle conversion if the
5038        former is wider than the latter and doesn't change the signedness
5039        (for integers).  Avoid this if the final type is a pointer since
5040        then we sometimes need the middle conversion.  */
5041     (if (((inter_int && inside_int) || (inter_float && inside_float))
5042          && (final_int || final_float)
5043          && inter_prec >= inside_prec
5044          && (inter_float || inter_unsignedp == inside_unsignedp))
5045      (ocvt @0))
5047     /* If we have a sign-extension of a zero-extended value, we can
5048        replace that by a single zero-extension.  Likewise if the
5049        final conversion does not change precision we can drop the
5050        intermediate conversion.  Similarly truncation of a sign-extension
5051        can be replaced by a single sign-extension.  */
5052     (if (inside_int && inter_int && final_int
5053          && ((inside_prec < inter_prec && inter_prec < final_prec
5054               && inside_unsignedp && !inter_unsignedp)
5055              || final_prec == inter_prec
5056              || (inside_prec < inter_prec && inter_prec > final_prec
5057                  && !inside_unsignedp && inter_unsignedp)))
5058      (ocvt @0))
5060     /* Two conversions in a row are not needed unless:
5061         - some conversion is floating-point (overstrict for now), or
5062         - some conversion is a vector (overstrict for now), or
5063         - the intermediate type is narrower than both initial and
5064           final, or
5065         - the intermediate type and innermost type differ in signedness,
5066           and the outermost type is wider than the intermediate, or
5067         - the initial type is a pointer type and the precisions of the
5068           intermediate and final types differ, or
5069         - the final type is a pointer type and the precisions of the
5070           initial and intermediate types differ.  */
5071     (if (! inside_float && ! inter_float && ! final_float
5072          && ! inside_vec && ! inter_vec && ! final_vec
5073          && (inter_prec >= inside_prec || inter_prec >= final_prec)
5074          && ! (inside_int && inter_int
5075                && inter_unsignedp != inside_unsignedp
5076                && inter_prec < final_prec)
5077          && ((inter_unsignedp && inter_prec > inside_prec)
5078              == (final_unsignedp && final_prec > inter_prec))
5079          && ! (inside_ptr && inter_prec != final_prec)
5080          && ! (final_ptr && inside_prec != inter_prec))
5081      (ocvt @0))
5083    /* `(outer:M)(inter:N) a:O`
5084       can be converted to `(outer:M) a`
5085       if M <= O && N >= O. No matter what signedness of the casts,
5086       as the final is either a truncation from the original or just
5087       a sign change of the type. */
5088    (if (inside_int && inter_int && final_int
5089         && final_prec <= inside_prec
5090         && inter_prec >= inside_prec)
5091     (convert @0))
5093     /* A truncation to an unsigned type (a zero-extension) should be
5094        canonicalized as bitwise and of a mask.  */
5095     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
5096          && final_int && inter_int && inside_int
5097          && final_prec == inside_prec
5098          && final_prec > inter_prec
5099          && inter_unsignedp)
5100      (convert (bit_and @0 { wide_int_to_tree
5101                               (inside_type,
5102                                wi::mask (inter_prec, false,
5103                                          TYPE_PRECISION (inside_type))); })))
5105     /* If we are converting an integer to a floating-point that can
5106        represent it exactly and back to an integer, we can skip the
5107        floating-point conversion.  */
5108     (if (GIMPLE /* PR66211 */
5109          && inside_int && inter_float && final_int &&
5110          (unsigned) significand_size (TYPE_MODE (inter_type))
5111          >= inside_prec - !inside_unsignedp)
5112      (convert @0)))))))
5114 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
5115    float_type.  Only do the transformation if we do not need to preserve
5116    trapping behaviour, so require !flag_trapping_math. */
5117 #if GIMPLE
5118 (simplify
5119    (float (fix_trunc @0))
5120    (if (!flag_trapping_math
5121         && !HONOR_SIGNED_ZEROS (type)
5122         && types_match (type, TREE_TYPE (@0))
5123         && direct_internal_fn_supported_p (IFN_TRUNC, type,
5124                                           OPTIMIZE_FOR_BOTH))
5125       (IFN_TRUNC @0)))
5126 #endif
5128 /* If we have a narrowing conversion to an integral type that is fed by a
5129    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
5130    masks off bits outside the final type (and nothing else).  */
5131 (simplify
5132   (convert (bit_and @0 INTEGER_CST@1))
5133   (if (INTEGRAL_TYPE_P (type)
5134        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5135        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
5136        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
5137                                                     TYPE_PRECISION (type)), 0))
5138    (convert @0)))
5141 /* (X /[ex] A) * A -> X.  */
5142 (simplify
5143   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
5144   (convert @0))
5146 /* Simplify (A / B) * B + (A % B) -> A.  */
5147 (for div (trunc_div ceil_div floor_div round_div)
5148      mod (trunc_mod ceil_mod floor_mod round_mod)
5149   (simplify
5150    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
5151    @0))
5153 /* x / y * y == x -> x % y == 0.  */
5154 (simplify
5155   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
5156   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
5157     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5159 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
5160 (for op (plus minus)
5161  (simplify
5162   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
5163   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
5164        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
5165    (with
5166      {
5167        wi::overflow_type overflow;
5168        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
5169                                TYPE_SIGN (type), &overflow);
5170      }
5171      (if (types_match (type, TREE_TYPE (@2))
5172          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
5173       (op @0 { wide_int_to_tree (type, mul); })
5174       (with { tree utype = unsigned_type_for (type); }
5175        (convert (op (convert:utype @0)
5176                     (mult (convert:utype @1) (convert:utype @2))))))))))
5178 /* Canonicalization of binary operations.  */
5180 /* Convert X + -C into X - C.  */
5181 (simplify
5182  (plus @0 REAL_CST@1)
5183  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5184   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
5185    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
5186     (minus @0 { tem; })))))
5188 /* Convert x+x into x*2.  */
5189 (simplify
5190  (plus @0 @0)
5191  (if (SCALAR_FLOAT_TYPE_P (type))
5192   (mult @0 { build_real (type, dconst2); })
5193   (if (INTEGRAL_TYPE_P (type))
5194    (mult @0 { build_int_cst (type, 2); }))))
5196 /* 0 - X  ->  -X.  */
5197 (simplify
5198  (minus integer_zerop @1)
5199  (negate @1))
5200 (simplify
5201  (pointer_diff integer_zerop @1)
5202  (negate (convert @1)))
5204 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
5205    ARG0 is zero and X + ARG0 reduces to X, since that would mean
5206    (-ARG1 + ARG0) reduces to -ARG1.  */
5207 (simplify
5208  (minus real_zerop@0 @1)
5209  (if (fold_real_zero_addition_p (type, @1, @0, 0))
5210   (negate @1)))
5212 /* Transform x * -1 into -x.  */
5213 (simplify
5214  (mult @0 integer_minus_onep)
5215  (negate @0))
5217 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
5218    signed overflow for CST != 0 && CST != -1.  */
5219 (simplify
5220  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
5221  (if (TREE_CODE (@2) != INTEGER_CST
5222       && single_use (@3)
5223       && !integer_zerop (@1) && !integer_minus_onep (@1))
5224   (mult (mult @0 @2) @1)))
5226 /* True if we can easily extract the real and imaginary parts of a complex
5227    number.  */
5228 (match compositional_complex
5229  (convert? (complex @0 @1)))
5231 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
5232 (simplify
5233  (complex (realpart @0) (imagpart @0))
5234  @0)
5235 (simplify
5236  (realpart (complex @0 @1))
5237  @0)
5238 (simplify
5239  (imagpart (complex @0 @1))
5240  @1)
5242 /* Sometimes we only care about half of a complex expression.  */
5243 (simplify
5244  (realpart (convert?:s (conj:s @0)))
5245  (convert (realpart @0)))
5246 (simplify
5247  (imagpart (convert?:s (conj:s @0)))
5248  (convert (negate (imagpart @0))))
5249 (for part (realpart imagpart)
5250  (for op (plus minus)
5251   (simplify
5252    (part (convert?:s@2 (op:s @0 @1)))
5253    (convert (op (part @0) (part @1))))))
5254 (simplify
5255  (realpart (convert?:s (CEXPI:s @0)))
5256  (convert (COS @0)))
5257 (simplify
5258  (imagpart (convert?:s (CEXPI:s @0)))
5259  (convert (SIN @0)))
5261 /* conj(conj(x)) -> x  */
5262 (simplify
5263  (conj (convert? (conj @0)))
5264  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
5265   (convert @0)))
5267 /* conj({x,y}) -> {x,-y}  */
5268 (simplify
5269  (conj (convert?:s (complex:s @0 @1)))
5270  (with { tree itype = TREE_TYPE (type); }
5271   (complex (convert:itype @0) (negate (convert:itype @1)))))
5273 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
5274 (for bswap (BSWAP)
5275  (simplify
5276   (bswap (bswap @0))
5277   @0)
5278  (simplify
5279   (bswap (bit_not (bswap @0)))
5280   (bit_not @0))
5281  (for bitop (bit_xor bit_ior bit_and)
5282   (simplify
5283    (bswap (bitop:c (bswap @0) @1))
5284    (bitop @0 (bswap @1))))
5285  (for cmp (eq ne)
5286   (simplify
5287    (cmp (bswap@2 @0) (bswap @1))
5288    (with { tree ctype = TREE_TYPE (@2); }
5289     (cmp (convert:ctype @0) (convert:ctype @1))))
5290   (simplify
5291    (cmp (bswap @0) INTEGER_CST@1)
5292    (with { tree ctype = TREE_TYPE (@1); }
5293     (cmp (convert:ctype @0) (bswap! @1)))))
5294  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
5295  (simplify
5296   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5297            INTEGER_CST@3)
5298    (if (BITS_PER_UNIT == 8
5299         && tree_fits_uhwi_p (@2)
5300         && tree_fits_uhwi_p (@3))
5301     (with
5302      {
5303       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5304       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5305       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5306       unsigned HOST_WIDE_INT lo = bits & 7;
5307       unsigned HOST_WIDE_INT hi = bits - lo;
5308      }
5309      (if (bits < prec
5310           && mask < (256u>>lo)
5311           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5312       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5313        (if (ns == 0)
5314         (bit_and (convert @1) @3)
5315         (with
5316          {
5317           tree utype = unsigned_type_for (TREE_TYPE (@1));
5318           tree nst = build_int_cst (integer_type_node, ns);
5319          }
5320          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5321  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
5322  (simplify
5323   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5324    (if (BITS_PER_UNIT == 8
5325         && CHAR_TYPE_SIZE == 8
5326         && tree_fits_uhwi_p (@1))
5327     (with
5328      {
5329       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5330       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5331       /* If the bswap was extended before the original shift, this
5332          byte (shift) has the sign of the extension, not the sign of
5333          the original shift.  */
5334       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5335      }
5336      /* Special case: logical right shift of sign-extended bswap.
5337         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5338      (if (TYPE_PRECISION (type) > prec
5339           && !TYPE_UNSIGNED (TREE_TYPE (@2))
5340           && TYPE_UNSIGNED (type)
5341           && bits < prec && bits + 8 >= prec)
5342       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5343        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5344       (if (bits + 8 == prec)
5345        (if (TYPE_UNSIGNED (st))
5346         (convert (convert:unsigned_char_type_node @0))
5347         (convert (convert:signed_char_type_node @0)))
5348        (if (bits < prec && bits + 8 > prec)
5349         (with 
5350          {
5351           tree nst = build_int_cst (integer_type_node, bits & 7);
5352           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5353                                        : signed_char_type_node;
5354          }
5355          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5356  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5357  (simplify
5358   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5359    (if (BITS_PER_UNIT == 8
5360         && tree_fits_uhwi_p (@1)
5361         && tree_to_uhwi (@1) < 256)
5362     (with
5363      {
5364       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5365       tree utype = unsigned_type_for (TREE_TYPE (@0));
5366       tree nst = build_int_cst (integer_type_node, prec - 8);
5367      }
5368      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5371 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5373 /* Simplify constant conditions.
5374    Only optimize constant conditions when the selected branch
5375    has the same type as the COND_EXPR.  This avoids optimizing
5376    away "c ? x : throw", where the throw has a void type.
5377    Note that we cannot throw away the fold-const.cc variant nor
5378    this one as we depend on doing this transform before possibly
5379    A ? B : B -> B triggers and the fold-const.cc one can optimize
5380    0 ? A : B to B even if A has side-effects.  Something
5381    genmatch cannot handle.  */
5382 (simplify
5383  (cond INTEGER_CST@0 @1 @2)
5384  (if (integer_zerop (@0))
5385   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5386    @2)
5387   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5388    @1)))
5389 (simplify
5390  (vec_cond VECTOR_CST@0 @1 @2)
5391  (if (integer_all_onesp (@0))
5392   @1
5393   (if (integer_zerop (@0))
5394    @2)))
5396 /* Sink unary operations to branches, but only if we do fold both.  */
5397 (for op (negate bit_not abs absu)
5398  (simplify
5399   (op (vec_cond:s @0 @1 @2))
5400   (vec_cond @0 (op! @1) (op! @2))))
5402 /* Sink unary conversions to branches, but only if we do fold both
5403    and the target's truth type is the same as we already have.  */
5404 (simplify
5405  (convert (vec_cond:s @0 @1 @2))
5406  (if (VECTOR_TYPE_P (type)
5407       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5408   (vec_cond @0 (convert! @1) (convert! @2))))
5410 /* Likewise for view_convert of nop_conversions. */
5411 (simplify
5412  (view_convert (vec_cond:s @0 @1 @2))
5413  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5414       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5415                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5416       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5417   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5419 /* Sink binary operation to branches, but only if we can fold it.  */
5420 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5421          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5422          trunc_mod ceil_mod floor_mod round_mod min max)
5423 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5424  (simplify
5425   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5426   (if (TREE_CODE_CLASS (op) != tcc_comparison
5427        || types_match (type, TREE_TYPE (@1))
5428        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5429        || (optimize_vectors_before_lowering_p ()
5430            /* The following is optimistic on the side of non-support, we are
5431               missing the legacy vcond{,u,eq} cases.  Do this only when
5432               lowering will be able to fixup..  */
5433            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5434                                        TREE_TYPE (@0), ERROR_MARK)))
5435    (vec_cond @0 (op! @1 @3) (op! @2 @4))))
5437 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5438  (simplify
5439   (op (vec_cond:s @0 @1 @2) @3)
5440   (if (TREE_CODE_CLASS (op) != tcc_comparison
5441        || types_match (type, TREE_TYPE (@1))
5442        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5443        || (optimize_vectors_before_lowering_p ()
5444            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5445                                        TREE_TYPE (@0), ERROR_MARK)))
5446    (vec_cond @0 (op! @1 @3) (op! @2 @3))))
5447  (simplify
5448   (op @3 (vec_cond:s @0 @1 @2))
5449   (if (TREE_CODE_CLASS (op) != tcc_comparison
5450        || types_match (type, TREE_TYPE (@1))
5451        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5452        || (optimize_vectors_before_lowering_p ()
5453            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5454                                        TREE_TYPE (@0), ERROR_MARK)))
5455    (vec_cond @0 (op! @3 @1) (op! @3 @2)))))
5457 #if GIMPLE
5458 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5459  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5460            INTEGER_CST@1)
5461  (with {
5462          int ibit = tree_log2 (@0);
5463          int ibit2 = tree_log2 (@1);
5464        }
5465   (if (ibit == ibit2
5466       && ibit >= 0
5467       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5469 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5470  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5471           INTEGER_CST@1)
5472  (with {
5473          int ibit = tree_log2 (@0);
5474          int ibit2 = tree_log2 (@1);
5475        }
5476   (if (ibit == ibit2
5477       && ibit >= 0
5478       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5480 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5481  (bit_and:c
5482   (convert1?@4
5483    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5484   (convert2? @0))
5485  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5487 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5488  (bit_and:c
5489   (convert1?@4
5490    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5491   (convert2? @0))
5492  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5494 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5495  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5496             INTEGER_CST@1)
5497  (with {
5498          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5499                                               TYPE_PRECISION(type)));
5500          int ibit2 = tree_log2 (@1);
5501        }
5502   (if (ibit == ibit2
5503       && ibit >= 0
5504       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5506 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5507  (bit_and@4
5508   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5509   INTEGER_CST@1)
5510  (with {
5511          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5512                                               TYPE_PRECISION(type)));
5513          int ibit2 = tree_log2 (@1);
5514        }
5515   (if (ibit == ibit2
5516       && ibit >= 0
5517       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5519 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5520  (bit_and:c
5521   (convert1?@3
5522    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5523   (convert2? @0))
5524  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5526 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5527  (bit_and:c
5528   (convert1?@3
5529    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5530   (convert2? @0))
5531   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5533 #endif
5535 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5536    Currently disabled after pass lvec because ARM understands
5537    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5538 #if GIMPLE
5539 /* These can only be done in gimple as fold likes to convert:
5540    (CMP) & N into (CMP) ? N : 0
5541    and we try to match the same pattern again and again. */
5542 (simplify
5543  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5544  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5545   (vec_cond (bit_and @0 @3) @1 @2)))
5546 (simplify
5547  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5548  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5549   (vec_cond (bit_ior @0 @3) @1 @2)))
5550 (simplify
5551  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5552  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5553   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5554 (simplify
5555  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5556  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5557   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5559 /*  ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5560     (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5561     component type of the outer vec_cond is greater equal the inner one.  */
5562 (for cmp (simple_comparison)
5563  (simplify
5564   (vec_cond
5565     (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5566                                     integer_all_onesp
5567                                     integer_zerop))
5568           integer_zerop) @2 @3)
5569   (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5570        && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5571        && !TYPE_UNSIGNED (TREE_TYPE (@5))
5572        && VECTOR_TYPE_P (TREE_TYPE (@6))
5573        && VECTOR_TYPE_P (type)
5574        && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5575                            TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5576        && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5577    (with { tree vtype = TREE_TYPE (@6);}
5578      (view_convert:type
5579        (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5581 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5582 (simplify
5583  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5584  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5585   (vec_cond (bit_and @0 @1) @2 @3)))
5586 (simplify
5587  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5588  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5589   (vec_cond (bit_ior @0 @1) @2 @3)))
5590 (simplify
5591  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5592  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5593   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5594 (simplify
5595  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5596  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5597   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5598 #endif
5600 /* (a ? x : y) != (b ? x : y) --> (a^b) ? TRUE  : FALSE */
5601 /* (a ? x : y) == (b ? x : y) --> (a^b) ? FALSE : TRUE  */
5602 /* (a ? x : y) != (b ? y : x) --> (a^b) ? FALSE : TRUE  */
5603 /* (a ? x : y) == (b ? y : x) --> (a^b) ? TRUE  : FALSE */
5604 (for cnd (cond vec_cond)
5605  (for eqne (eq ne)
5606   (simplify
5607    (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2))
5608     (cnd (bit_xor @0 @3) { constant_boolean_node (eqne == NE_EXPR, type); }
5609      { constant_boolean_node (eqne != NE_EXPR, type); }))
5610   (simplify
5611    (eqne:c (cnd @0 @1 @2) (cnd @3 @2 @1))
5612     (cnd (bit_xor @0 @3) { constant_boolean_node (eqne != NE_EXPR, type); }
5613      { constant_boolean_node (eqne == NE_EXPR, type); }))))
5615 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5616    types are compatible.  */
5617 (simplify
5618  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5619  (if (VECTOR_BOOLEAN_TYPE_P (type)
5620       && types_match (type, TREE_TYPE (@0)))
5621   (if (integer_zerop (@1) && integer_all_onesp (@2))
5622    (bit_not @0)
5623    (if (integer_all_onesp (@1) && integer_zerop (@2))
5624     @0))))
5626 /* A few simplifications of "a ? CST1 : CST2". */
5627 /* NOTE: Only do this on gimple as the if-chain-to-switch
5628    optimization depends on the gimple to have if statements in it. */
5629 #if GIMPLE
5630 (simplify
5631  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5632  (switch
5633   (if (integer_zerop (@2))
5634    (switch
5635     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5636     (if (integer_onep (@1))
5637      (convert (convert:boolean_type_node @0)))
5638     /* a ? -1 : 0 -> -a.  */
5639     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5640      (if (TYPE_PRECISION (type) == 1)
5641       /* For signed 1-bit precision just cast bool to the type.  */
5642       (convert (convert:boolean_type_node @0))
5643       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5644        (with {
5645           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5646                                                       TYPE_UNSIGNED (type));
5647         }
5648         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5649        (negate (convert:type (convert:boolean_type_node @0))))))
5650     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5651     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5652      (with {
5653        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5654       }
5655       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5656   (if (integer_zerop (@1))
5657    (switch
5658     /* a ? 0 : 1 -> !a.  */
5659     (if (integer_onep (@2))
5660      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5661     /* a ? 0 : -1 -> -(!a).  */
5662     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5663      (if (TYPE_PRECISION (type) == 1)
5664       /* For signed 1-bit precision just cast bool to the type.  */
5665       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5666       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5667        (with {
5668           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5669                                                       TYPE_UNSIGNED (type));
5670         }
5671         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5672                                                 { boolean_true_node; })))))
5673        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5674                                       { boolean_true_node; }))))))
5675     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5676     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5677      (with {
5678        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5679       }
5680       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5681                                 { boolean_true_node; })) { shift; })))))))
5683 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5684    for unsigned types. */
5685 (simplify
5686  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5687  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5688       && bitwise_equal_p (@0, @2))
5689   (convert (eq @0 @1))
5693 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5694    for unsigned types. */
5695 (simplify
5696  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5697  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5698       && bitwise_equal_p (@0, @2))
5699   (convert (eq @0 @1))
5703 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5704    on the first bit of the CST.  */
5705 (simplify
5706  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5707  (if ((wi::to_wide (@1) & 1) != 0)
5708   @2
5709   { build_zero_cst (type); }))
5711 /* Optimize
5712    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5713    x_5 == cstN ? cst4 : cst3
5714    # op is == or != and N is 1 or 2
5715    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5716    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5717    of cst3 and cst4 is smaller.
5718    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5719 (for eqne (ne eq)
5720  (simplify
5721   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5722   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5723        && INTEGRAL_TYPE_P (type)
5724        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5725            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5726    (with {
5727      int_range_max r;
5728      get_range_query (cfun)->range_of_expr (r, @0);
5729      if (r.undefined_p ())
5730        r.set_varying (TREE_TYPE (@0));
5732      wide_int min = r.lower_bound ();
5733      wide_int max = r.upper_bound ();
5734     }
5735     (if (min + 1 == max
5736          && (wi::to_wide (@1) == min
5737              || wi::to_wide (@1) == max))
5738      (with {
5739        tree arg0 = @2, arg1 = @3;
5740        tree type1;
5741        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5742          std::swap (arg0, arg1);
5743        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5744          type1 = TREE_TYPE (@0);
5745        else
5746          type1 = type;
5747        auto prec = TYPE_PRECISION (type1);
5748        auto unsign = TYPE_UNSIGNED (type1);
5749        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5750         type1 = build_nonstandard_integer_type (prec, unsign);
5751        min = wide_int::from (min, prec,
5752                              TYPE_SIGN (TREE_TYPE (@0)));
5753        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5754                                     TYPE_SIGN (type));
5755        enum tree_code code;
5756        wi::overflow_type ovf;
5757        if (tree_int_cst_lt (arg0, arg1))
5758          {
5759            code = PLUS_EXPR;
5760            a -= min;
5761            if (!unsign)
5762              {
5763                /* lhs is known to be in range [min, min+1] and we want to add a
5764                   to it.  Check if that operation can overflow for those 2 values
5765                   and if yes, force unsigned type.  */
5766                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5767                if (ovf)
5768                  type1 = unsigned_type_for (type1);
5769              }
5770          }
5771        else
5772          {
5773            code = MINUS_EXPR;
5774            a += min;
5775            if (!unsign)
5776              {
5777                /* lhs is known to be in range [min, min+1] and we want to subtract
5778                   it from a.  Check if that operation can overflow for those 2
5779                   values and if yes, force unsigned type.  */
5780                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5781                if (ovf)
5782                 type1 = unsigned_type_for (type1);
5783              }
5784          }
5785        tree arg = wide_int_to_tree (type1, a);
5786       }
5787       (if (code == PLUS_EXPR)
5788        (convert (plus (convert:type1 @0) { arg; }))
5789        (convert (minus { arg; } (convert:type1 @0))))))))))
5790 #endif
5792 (simplify
5793  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5794  (if (INTEGRAL_TYPE_P (type)
5795       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5796   (cond @1 (convert @2) (convert @3))))
5798 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5799    be extended.  */
5800 /* This pattern implements two kinds simplification:
5802    Case 1)
5803    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5804      1) Conversions are type widening from smaller type.
5805      2) Const c1 equals to c2 after canonicalizing comparison.
5806      3) Comparison has tree code LT, LE, GT or GE.
5807    This specific pattern is needed when (cmp (convert x) c) may not
5808    be simplified by comparison patterns because of multiple uses of
5809    x.  It also makes sense here because simplifying across multiple
5810    referred var is always benefitial for complicated cases.
5812    Case 2)
5813    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5814 (for cmp (lt le gt ge eq ne)
5815  (simplify
5816   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5817   (with
5818    {
5819      tree from_type = TREE_TYPE (@1);
5820      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5821      enum tree_code code = ERROR_MARK;
5823      if (INTEGRAL_TYPE_P (from_type)
5824          && int_fits_type_p (@2, from_type)
5825          && (types_match (c1_type, from_type)
5826              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5827                  && (TYPE_UNSIGNED (from_type)
5828                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5829          && (types_match (c2_type, from_type)
5830              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5831                  && (TYPE_UNSIGNED (from_type)
5832                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5833        {
5834          if (cmp != EQ_EXPR)
5835            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5836          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5837          else if (int_fits_type_p (@3, from_type))
5838            code = EQ_EXPR;
5839        }
5840    }
5841    (if (code == MAX_EXPR)
5842     (convert (max @1 (convert @2)))
5843     (if (code == MIN_EXPR)
5844      (convert (min @1 (convert @2)))
5845      (if (code == EQ_EXPR)
5846       (convert (cond (eq @1 (convert @3))
5847                      (convert:from_type @3) (convert:from_type @2)))))))))
5849 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5851      1) OP is PLUS or MINUS.
5852      2) CMP is LT, LE, GT or GE.
5853      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5855    This pattern also handles special cases like:
5857      A) Operand x is a unsigned to signed type conversion and c1 is
5858         integer zero.  In this case,
5859           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5860           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5861      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5862         check equality for (c1+1) and (c1-1) by adjusting comparison
5863         code.
5865    TODO: Though signed type is handled by this pattern, it cannot be
5866    simplified at the moment because C standard requires additional
5867    type promotion.  In order to match&simplify it here, the IR needs
5868    to be cleaned up by other optimizers, i.e, VRP.  */
5869 (for op (plus minus)
5870  (for cmp (lt le gt ge)
5871   (simplify
5872    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5873    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5874     (if (types_match (from_type, to_type)
5875          /* Check if it is special case A).  */
5876          || (TYPE_UNSIGNED (from_type)
5877              && !TYPE_UNSIGNED (to_type)
5878              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5879              && integer_zerop (@1)
5880              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5881      (with
5882       {
5883         wi::overflow_type overflow = wi::OVF_NONE;
5884         enum tree_code code, cmp_code = cmp;
5885         wide_int real_c1;
5886         wide_int c1 = wi::to_wide (@1);
5887         wide_int c2 = wi::to_wide (@2);
5888         wide_int c3 = wi::to_wide (@3);
5889         signop sgn = TYPE_SIGN (from_type);
5891         /* Handle special case A), given x of unsigned type:
5892             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5893             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5894         if (!types_match (from_type, to_type))
5895           {
5896             if (cmp_code == LT_EXPR)
5897               cmp_code = GT_EXPR;
5898             if (cmp_code == GE_EXPR)
5899               cmp_code = LE_EXPR;
5900             c1 = wi::max_value (to_type);
5901           }
5902         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5903            compute (c3 op' c2) and check if it equals to c1 with op' being
5904            the inverted operator of op.  Make sure overflow doesn't happen
5905            if it is undefined.  */
5906         if (op == PLUS_EXPR)
5907           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5908         else
5909           real_c1 = wi::add (c3, c2, sgn, &overflow);
5911         code = cmp_code;
5912         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5913           {
5914             /* Check if c1 equals to real_c1.  Boundary condition is handled
5915                by adjusting comparison operation if necessary.  */
5916             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5917                 && !overflow)
5918               {
5919                 /* X <= Y - 1 equals to X < Y.  */
5920                 if (cmp_code == LE_EXPR)
5921                   code = LT_EXPR;
5922                 /* X > Y - 1 equals to X >= Y.  */
5923                 if (cmp_code == GT_EXPR)
5924                   code = GE_EXPR;
5925               }
5926             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5927                 && !overflow)
5928               {
5929                 /* X < Y + 1 equals to X <= Y.  */
5930                 if (cmp_code == LT_EXPR)
5931                   code = LE_EXPR;
5932                 /* X >= Y + 1 equals to X > Y.  */
5933                 if (cmp_code == GE_EXPR)
5934                   code = GT_EXPR;
5935               }
5936             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5937               {
5938                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5939                   code = MIN_EXPR;
5940                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5941                   code = MAX_EXPR;
5942               }
5943           }
5944       }
5945       (if (code == MAX_EXPR)
5946        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5947            { wide_int_to_tree (from_type, c2); })
5948        (if (code == MIN_EXPR)
5949         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5950             { wide_int_to_tree (from_type, c2); })))))))))
5952 #if GIMPLE
5953 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5954    in fold_cond_expr_with_comparison for GENERIC folding with
5955    some extra constraints.  */
5956 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5957  (simplify
5958   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5959         (convert3? @0) (convert4? @1))
5960   (if (!HONOR_SIGNED_ZEROS (type)
5961        && (/* Allow widening conversions of the compare operands as data.  */
5962            (INTEGRAL_TYPE_P (type)
5963             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5964             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5965             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5966             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5967            /* Or sign conversions for the comparison.  */
5968            || (types_match (type, TREE_TYPE (@0))
5969                && types_match (type, TREE_TYPE (@1)))))
5970    (switch
5971     (if (cmp == EQ_EXPR)
5972      (if (VECTOR_TYPE_P (type))
5973       (view_convert @c1)
5974       (convert @c1)))
5975     (if (cmp == NE_EXPR)
5976      (if (VECTOR_TYPE_P (type))
5977       (view_convert @c0)
5978       (convert @c0)))
5979     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5980      (if (!HONOR_NANS (type))
5981       (if (VECTOR_TYPE_P (type))
5982        (view_convert (min @c0 @c1))
5983        (convert (min @c0 @c1)))))
5984     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5985      (if (!HONOR_NANS (type))
5986       (if (VECTOR_TYPE_P (type))
5987        (view_convert (max @c0 @c1))
5988        (convert (max @c0 @c1)))))
5989     (if (cmp == UNEQ_EXPR)
5990      (if (!HONOR_NANS (type))
5991       (if (VECTOR_TYPE_P (type))
5992        (view_convert @c1)
5993        (convert @c1))))
5994     (if (cmp == LTGT_EXPR)
5995      (if (!HONOR_NANS (type))
5996       (if (VECTOR_TYPE_P (type))
5997        (view_convert @c0)
5998        (convert @c0))))))))
6000 /* This is for VEC_COND_EXPR
6001    Optimize A < B ? A : B to MIN (A, B)
6002             A > B ? A : B to MAX (A, B).  */
6003 (for cmp (lt le ungt unge gt ge unlt unle)
6004      minmax (min min min min max max max max)
6005      MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
6006  (simplify
6007   (vec_cond (cmp @0 @1) @0 @1)
6008    (if (VECTOR_INTEGER_TYPE_P (type)
6009        && target_supports_op_p (type, MINMAX, optab_vector))
6010     (minmax @0 @1))))
6012 (for cmp (lt le ungt unge gt ge unlt unle)
6013      minmax (max max max max min min min min)
6014      MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
6015  (simplify
6016   (vec_cond (cmp @0 @1) @1 @0)
6017    (if (VECTOR_INTEGER_TYPE_P (type)
6018        && target_supports_op_p (type, MINMAX, optab_vector))
6019     (minmax @0 @1))))
6021 /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
6022    and x < 0 ? 1 : 0 into (unsigned) x >> 31.  */
6023 (simplify
6024   (vec_cond (lt @0 integer_zerop) integer_all_onesp integer_zerop)
6025    (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6026        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6027        && tree_nop_conversion_p (type, TREE_TYPE (@0))
6028        && target_supports_op_p (TREE_TYPE (@0), RSHIFT_EXPR, optab_scalar))
6029     (with
6030       {
6031        unsigned int prec = element_precision (TREE_TYPE (@0));
6032       }
6033     (view_convert
6034       (rshift @0 { build_int_cst (integer_type_node, prec - 1);})))))
6036 (simplify
6037   (vec_cond (lt @0 integer_zerop) integer_onep integer_zerop)
6038    (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6039        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6040        && tree_nop_conversion_p (type, TREE_TYPE (@0)))
6041     (with
6042      {
6043        unsigned int prec = element_precision (TREE_TYPE (@0));
6044        tree utype = unsigned_type_for (TREE_TYPE (@0));
6045      }
6046      (if (target_supports_op_p (utype, RSHIFT_EXPR, optab_scalar))
6047       (view_convert
6048         (rshift (view_convert:utype @0)
6049                 { build_int_cst (integer_type_node, prec - 1);}))))))
6050 #endif
6052 (for cnd (cond vec_cond)
6053  /* (a != b) ? (a - b) : 0 -> (a - b) */
6054  (simplify
6055   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
6056   @2)
6057  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
6058  (simplify
6059   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
6060   @2)
6061  /* (a != b) ? (a & b) : a -> (a & b) */
6062  /* (a != b) ? (a | b) : a -> (a | b) */
6063  /* (a != b) ? min(a,b) : a -> min(a,b) */
6064  /* (a != b) ? max(a,b) : a -> max(a,b) */
6065  (for op (bit_and bit_ior min max)
6066   (simplify
6067    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
6068    @2))
6069  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
6070  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
6071  (for op (mult plus)
6072   (simplify
6073    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
6074    (if (ANY_INTEGRAL_TYPE_P (type))
6075     @2)))
6076  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
6077  (simplify
6078   (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
6079   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
6080    @2))
6083 /* These was part of minmax phiopt.  */
6084 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
6085    to minmax<min/max<a, b>, c> */
6086 (for minmax (min max)
6087  (for cmp (lt le gt ge ne)
6088   (simplify
6089    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
6090    (with
6091     {
6092       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
6093     }
6094     (if (code == MIN_EXPR)
6095      (minmax (min @1 @2) @4)
6096      (if (code == MAX_EXPR)
6097       (minmax (max @1 @2) @4)))))))
6099 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
6100 (for cmp    (gt  ge  lt  le)
6101      minmax (min min max max)
6102  (simplify
6103   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
6104    (with
6105     {
6106       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
6107     }
6108     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
6109          && code == MIN_EXPR
6110          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
6111      (min @2 @4)
6112      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
6113           && code == MAX_EXPR
6114           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
6115       (max @2 @4))))))
6117 #if GIMPLE
6118 /* These patterns should be after min/max detection as simplifications
6119    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
6120    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
6121    Even without those, reaching min/max/and/ior faster is better.  */
6122 (simplify
6123  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
6124  (switch
6125   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
6126   (if (integer_zerop (@2))
6127    (bit_and (convert @0) @1))
6128   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
6129   (if (integer_zerop (@1))
6130    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
6131   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
6132   (if (integer_onep (@1))
6133    (bit_ior (convert @0) @2))
6134   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
6135   (if (integer_onep (@2))
6136    (bit_ior (bit_xor (convert @0) @2) @1))
6139 #endif
6141 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
6142 (simplify
6143  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
6144  (if (!TYPE_SATURATING (type)
6145       && (TYPE_OVERFLOW_WRAPS (type)
6146           || !wi::only_sign_bit_p (wi::to_wide (@1)))
6147       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
6148   @3))
6150 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
6151 (simplify
6152  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
6153  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
6154   @3))
6156 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
6157 (for op (abs absu)
6158  (simplify
6159   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
6160   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
6161    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
6162     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6163      (convert (absu:utype @0)))
6164     @3))))
6166 /* X >  Positive ? X : ABS(X) -> ABS(X) */
6167 /* X >= Positive ? X : ABS(X) -> ABS(X) */
6168 /* X == Positive ? X : ABS(X) -> ABS(X) */
6169 (for cmp (eq gt ge)
6170  (simplify
6171   (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
6172   (if (INTEGRAL_TYPE_P (type))
6173    @3)))
6175 /* X == Positive ? Positive : ABS(X) -> ABS(X) */
6176 (simplify
6177  (cond (eq:c @0 tree_expr_nonnegative_p@1) @1 (abs@3 @0))
6178  (if (INTEGRAL_TYPE_P (type))
6179   @3))
6181 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
6182    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
6183 (simplify
6184  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
6185  (if (TYPE_UNSIGNED (type))
6186   (cond (ge @0 @1) (negate @0) @2)))
6188 (for cnd (cond vec_cond)
6189  /* A ? B : (A ? X : C) -> A ? B : C.  */
6190  (simplify
6191   (cnd @0 (cnd @0 @1 @2) @3)
6192   (cnd @0 @1 @3))
6193  (simplify
6194   (cnd @0 @1 (cnd @0 @2 @3))
6195   (cnd @0 @1 @3))
6196  /* A ? B : (!A ? C : X) -> A ? B : C.  */
6197  /* ???  This matches embedded conditions open-coded because genmatch
6198     would generate matching code for conditions in separate stmts only.
6199     The following is still important to merge then and else arm cases
6200     from if-conversion.  */
6201  (simplify
6202   (cnd @0 @1 (cnd @2 @3 @4))
6203   (if (inverse_conditions_p (@0, @2))
6204    (cnd @0 @1 @3)))
6205  (simplify
6206   (cnd @0 (cnd @1 @2 @3) @4)
6207   (if (inverse_conditions_p (@0, @1))
6208    (cnd @0 @3 @4)))
6210  /* A ? B : B -> B.  */
6211  (simplify
6212   (cnd @0 @1 @1)
6213   @1)
6215  /* !A ? B : C -> A ? C : B.  */
6216  (simplify
6217   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
6218   /* For CONDs, don't handle signed values here. */
6219   (if (cnd == VEC_COND_EXPR
6220        || TYPE_UNSIGNED (TREE_TYPE (@0)))
6221    (cnd @0 @2 @1))))
6223 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
6225    None of these transformations work for modes with signed
6226    zeros.  If A is +/-0, the first two transformations will
6227    change the sign of the result (from +0 to -0, or vice
6228    versa).  The last four will fix the sign of the result,
6229    even though the original expressions could be positive or
6230    negative, depending on the sign of A.
6232    Note that all these transformations are correct if A is
6233    NaN, since the two alternatives (A and -A) are also NaNs.  */
6235 (for cnd (cond vec_cond)
6236  /* A == 0 ? A : -A    same as -A */
6237  (for cmp (eq uneq)
6238   (simplify
6239    (cnd (cmp @0 zerop) @2 (negate@1 @2))
6240     (if (!HONOR_SIGNED_ZEROS (type)
6241          && bitwise_equal_p (@0, @2))
6242      @1))
6243   (simplify
6244    (cnd (cmp @0 zerop) zerop (negate@1 @2))
6245     (if (!HONOR_SIGNED_ZEROS (type)
6246          && bitwise_equal_p (@0, @2))
6247      @1))
6249  /* A != 0 ? A : -A    same as A */
6250  (for cmp (ne ltgt)
6251   (simplify
6252    (cnd (cmp @0 zerop) @1 (negate @1))
6253     (if (!HONOR_SIGNED_ZEROS (type)
6254          && bitwise_equal_p (@0, @1))
6255      @1))
6256   (simplify
6257    (cnd (cmp @0 zerop) @1 integer_zerop)
6258     (if (!HONOR_SIGNED_ZEROS (type)
6259          && bitwise_equal_p (@0, @1))
6260      @1))
6262  /* A >=/> 0 ? A : -A    same as abs (A) */
6263  (for cmp (ge gt)
6264   (simplify
6265    (cnd (cmp @0 zerop) @1 (negate @1))
6266     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
6267          && !TYPE_UNSIGNED (TREE_TYPE(@0))
6268          && bitwise_equal_p (@0, @1))
6269      (if (TYPE_UNSIGNED (type))
6270       (absu:type @0)
6271       (abs @0)))))
6272  /* A <=/< 0 ? A : -A    same as -abs (A) */
6273  (for cmp (le lt)
6274   (simplify
6275    (cnd (cmp @0 zerop) @1 (negate @1))
6276     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
6277          && !TYPE_UNSIGNED (TREE_TYPE(@0))
6278          && bitwise_equal_p (@0, @1))
6279      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6280            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6281           || TYPE_UNSIGNED (type))
6282       (with {
6283         tree utype = unsigned_type_for (TREE_TYPE(@0));
6284        }
6285        (convert (negate (absu:utype @0))))
6286        (negate (abs @0)))))
6289  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
6290  (for cmp (eq uneq)
6291   (simplify
6292    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
6293    (if (!HONOR_SIGNED_ZEROS (type))
6294     @3))
6295   (simplify
6296    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
6297    @3)
6299  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
6300  (for cmp (ne ltgt)
6301   (simplify
6302    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6303    (if (!HONOR_SIGNED_ZEROS (type))
6304     @0))
6305   (simplify
6306    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
6307    @0)
6309  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
6310  (for cmp (ge gt)
6311   (simplify
6312    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6313    (if (!HONOR_SIGNED_ZEROS (type)
6314         && !TYPE_UNSIGNED (type))
6315     (abs @0))))
6316  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
6317  (for cmp (le lt)
6318   (simplify
6319    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6320    (if (!HONOR_SIGNED_ZEROS (type)
6321         && !TYPE_UNSIGNED (type))
6322     (if (ANY_INTEGRAL_TYPE_P (type)
6323          && !TYPE_OVERFLOW_WRAPS (type))
6324      (with {
6325         tree utype = unsigned_type_for (type);
6326       }
6327       (convert (negate (absu:utype @0))))
6328       (negate (abs @0)))))
6332 /* -(type)!A -> (type)A - 1.  */
6333 (simplify
6334  (negate (convert?:s (logical_inverted_value:s @0)))
6335  (if (INTEGRAL_TYPE_P (type)
6336       && TREE_CODE (type) != BOOLEAN_TYPE
6337       && TYPE_PRECISION (type) > 1
6338       && TREE_CODE (@0) == SSA_NAME
6339       && ssa_name_has_boolean_range (@0))
6340   (plus (convert:type @0) { build_all_ones_cst (type); })))
6342 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
6343    return all -1 or all 0 results.  */
6344 /* ??? We could instead convert all instances of the vec_cond to negate,
6345    but that isn't necessarily a win on its own.  */
6346 (simplify
6347  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6348  (if (VECTOR_TYPE_P (type)
6349       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6350                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6351       && (TYPE_MODE (TREE_TYPE (type))
6352           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6353   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6355 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
6356 (simplify
6357  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6358  (if (VECTOR_TYPE_P (type)
6359       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6360                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6361       && (TYPE_MODE (TREE_TYPE (type))
6362           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6363   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6366 /* Simplifications of comparisons.  */
6368 /* See if we can reduce the magnitude of a constant involved in a
6369    comparison by changing the comparison code.  This is a canonicalization
6370    formerly done by maybe_canonicalize_comparison_1.  */
6371 (for cmp  (le gt)
6372      acmp (lt ge)
6373  (simplify
6374   (cmp @0 uniform_integer_cst_p@1)
6375   (with { tree cst = uniform_integer_cst_p (@1); }
6376    (if (tree_int_cst_sgn (cst) == -1)
6377      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6378                                    wide_int_to_tree (TREE_TYPE (cst),
6379                                                      wi::to_wide (cst)
6380                                                      + 1)); })))))
6381 (for cmp  (ge lt)
6382      acmp (gt le)
6383  (simplify
6384   (cmp @0 uniform_integer_cst_p@1)
6385   (with { tree cst = uniform_integer_cst_p (@1); }
6386    (if (tree_int_cst_sgn (cst) == 1)
6387     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6388                                   wide_int_to_tree (TREE_TYPE (cst),
6389                                   wi::to_wide (cst) - 1)); })))))
6391 /* We can simplify a logical negation of a comparison to the
6392    inverted comparison.  As we cannot compute an expression
6393    operator using invert_tree_comparison we have to simulate
6394    that with expression code iteration.  */
6395 (for cmp (tcc_comparison)
6396      icmp (inverted_tcc_comparison)
6397      ncmp (inverted_tcc_comparison_with_nans)
6398  /* Ideally we'd like to combine the following two patterns
6399     and handle some more cases by using
6400       (logical_inverted_value (cmp @0 @1))
6401     here but for that genmatch would need to "inline" that.
6402     For now implement what forward_propagate_comparison did.  */
6403  (simplify
6404   (bit_not (cmp @0 @1))
6405   (if (VECTOR_TYPE_P (type)
6406        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6407    /* Comparison inversion may be impossible for trapping math,
6408       invert_tree_comparison will tell us.  But we can't use
6409       a computed operator in the replacement tree thus we have
6410       to play the trick below.  */
6411    (with { enum tree_code ic = invert_tree_comparison
6412              (cmp, HONOR_NANS (@0)); }
6413     (if (ic == icmp)
6414      (icmp @0 @1)
6415      (if (ic == ncmp)
6416       (ncmp @0 @1))))))
6417  (simplify
6418   (bit_xor (cmp @0 @1) integer_truep)
6419   (with { enum tree_code ic = invert_tree_comparison
6420             (cmp, HONOR_NANS (@0)); }
6421    (if (ic == icmp)
6422     (icmp @0 @1)
6423     (if (ic == ncmp)
6424      (ncmp @0 @1)))))
6425  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
6426  (simplify
6427   (ne (cmp@2 @0 @1) integer_zerop)
6428   (if (types_match (type, TREE_TYPE (@2)))
6429    (cmp @0 @1)))
6430  (simplify
6431   (eq (cmp@2 @0 @1) integer_truep)
6432   (if (types_match (type, TREE_TYPE (@2)))
6433    (cmp @0 @1)))
6434  (simplify
6435   (ne (cmp@2 @0 @1) integer_truep)
6436   (if (types_match (type, TREE_TYPE (@2)))
6437    (with { enum tree_code ic = invert_tree_comparison
6438              (cmp, HONOR_NANS (@0)); }
6439     (if (ic == icmp)
6440      (icmp @0 @1)
6441      (if (ic == ncmp)
6442       (ncmp @0 @1))))))
6443  (simplify
6444   (eq (cmp@2 @0 @1) integer_zerop)
6445   (if (types_match (type, TREE_TYPE (@2)))
6446    (with { enum tree_code ic = invert_tree_comparison
6447              (cmp, HONOR_NANS (@0)); }
6448     (if (ic == icmp)
6449      (icmp @0 @1)
6450      (if (ic == ncmp)
6451       (ncmp @0 @1)))))))
6453 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6454    ??? The transformation is valid for the other operators if overflow
6455    is undefined for the type, but performing it here badly interacts
6456    with the transformation in fold_cond_expr_with_comparison which
6457    attempts to synthetize ABS_EXPR.  */
6458 (for cmp (eq ne)
6459  (for sub (minus pointer_diff)
6460   (simplify
6461    (cmp (sub@2 @0 @1) integer_zerop)
6462    (if (single_use (@2))
6463     (cmp @0 @1)))))
6465 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6466    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
6467 (for cmp (lt ge)
6468  (simplify
6469   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6470    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6471         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6472         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6473     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6474 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6475    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
6476 (simplify
6477  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6478   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6479        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6480        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6481    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6483 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6484    signed arithmetic case.  That form is created by the compiler
6485    often enough for folding it to be of value.  One example is in
6486    computing loop trip counts after Operator Strength Reduction.  */
6487 (for cmp (simple_comparison)
6488      scmp (swapped_simple_comparison)
6489  (simplify
6490   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6491   /* Handle unfolded multiplication by zero.  */
6492   (if (integer_zerop (@1))
6493    (cmp @1 @2)
6494    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6495         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6496         && single_use (@3))
6497     /* If @1 is negative we swap the sense of the comparison.  */
6498     (if (tree_int_cst_sgn (@1) < 0)
6499      (scmp @0 @2)
6500      (cmp @0 @2))))))
6502 /* For integral types with undefined overflow fold
6503    x * C1 == C2 into x == C2 / C1 or false.
6504    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6505    Z / 2^n Z.  */
6506 (for cmp (eq ne)
6507  (simplify
6508   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6509   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6510        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6511        && wi::to_wide (@1) != 0)
6512    (with { widest_int quot; }
6513     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6514                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6515      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6516      { constant_boolean_node (cmp == NE_EXPR, type); }))
6517    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6518         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6519         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6520     (cmp @0
6521      {
6522        tree itype = TREE_TYPE (@0);
6523        int p = TYPE_PRECISION (itype);
6524        wide_int m = wi::one (p + 1) << p;
6525        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6526        wide_int i = wide_int::from (wi::mod_inv (a, m),
6527                                     p, TYPE_SIGN (itype));
6528        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6529      })))))
6531 /* Simplify comparison of something with itself.  For IEEE
6532    floating-point, we can only do some of these simplifications.  */
6533 (for cmp (eq ge le)
6534  (simplify
6535   (cmp @0 @0)
6536   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6537        || ! tree_expr_maybe_nan_p (@0))
6538    { constant_boolean_node (true, type); }
6539    (if (cmp != EQ_EXPR
6540         /* With -ftrapping-math conversion to EQ loses an exception.  */
6541         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6542             || ! flag_trapping_math))
6543     (eq @0 @0)))))
6544 (for cmp (ne gt lt)
6545  (simplify
6546   (cmp @0 @0)
6547   (if (cmp != NE_EXPR
6548        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6549        || ! tree_expr_maybe_nan_p (@0))
6550    { constant_boolean_node (false, type); })))
6551 (for cmp (unle unge uneq)
6552  (simplify
6553   (cmp @0 @0)
6554   { constant_boolean_node (true, type); }))
6555 (for cmp (unlt ungt)
6556  (simplify
6557   (cmp @0 @0)
6558   (unordered @0 @0)))
6559 (simplify
6560  (ltgt @0 @0)
6561  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6562   { constant_boolean_node (false, type); }))
6564 /* x == ~x -> false */
6565 /* x != ~x -> true */
6566 (for cmp (eq ne)
6567  (simplify
6568   (cmp:c @0 (bit_not @0))
6569   { constant_boolean_node (cmp == NE_EXPR, type); }))
6571 /* Fold ~X op ~Y as Y op X.  */
6572 (for cmp (simple_comparison)
6573  (simplify
6574   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6575   (if (single_use (@2) && single_use (@3))
6576    (with { tree otype = TREE_TYPE (@4); }
6577     (cmp (convert:otype @1) (convert:otype @0))))))
6579 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6580 (for cmp (simple_comparison)
6581      scmp (swapped_simple_comparison)
6582  (simplify
6583   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6584   (if (single_use (@2)
6585        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6586    (with { tree otype = TREE_TYPE (@1); }
6587     (scmp (convert:otype @0) (bit_not @1))))))
6589 (for cmp (simple_comparison)
6590  (simplify
6591   (cmp @0 REAL_CST@1)
6592   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6593   (switch
6594    /* a CMP (-0) -> a CMP 0  */
6595    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6596     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6597    /* (-0) CMP b -> 0 CMP b.  */
6598    (if (TREE_CODE (@0) == REAL_CST
6599         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6600     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6601    /* x != NaN is always true, other ops are always false.  */
6602    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6603         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6604         && !tree_expr_signaling_nan_p (@1)
6605         && !tree_expr_maybe_signaling_nan_p (@0))
6606     { constant_boolean_node (cmp == NE_EXPR, type); })
6607    /* NaN != y is always true, other ops are always false.  */
6608    (if (TREE_CODE (@0) == REAL_CST
6609         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6610         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6611         && !tree_expr_signaling_nan_p (@0)
6612         && !tree_expr_signaling_nan_p (@1))
6613     { constant_boolean_node (cmp == NE_EXPR, type); })
6614    /* Fold comparisons against infinity.  */
6615    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6616         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6617     (with
6618      {
6619        REAL_VALUE_TYPE max;
6620        enum tree_code code = cmp;
6621        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6622        if (neg)
6623          code = swap_tree_comparison (code);
6624      }
6625      (switch
6626       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6627       (if (code == GT_EXPR
6628            && !(HONOR_NANS (@0) && flag_trapping_math))
6629        { constant_boolean_node (false, type); })
6630       (if (code == LE_EXPR)
6631        /* x <= +Inf is always true, if we don't care about NaNs.  */
6632        (if (! HONOR_NANS (@0))
6633         { constant_boolean_node (true, type); }
6634         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6635            an "invalid" exception.  */
6636         (if (!flag_trapping_math)
6637          (eq @0 @0))))
6638       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6639          for == this introduces an exception for x a NaN.  */
6640       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6641            || code == GE_EXPR)
6642        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6643         (if (neg)
6644          (lt @0 { build_real (TREE_TYPE (@0), max); })
6645          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6646       /* x < +Inf is always equal to x <= DBL_MAX.  */
6647       (if (code == LT_EXPR)
6648        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6649         (if (neg)
6650          (ge @0 { build_real (TREE_TYPE (@0), max); })
6651          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6652       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6653          an exception for x a NaN so use an unordered comparison.  */
6654       (if (code == NE_EXPR)
6655        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6656         (if (! HONOR_NANS (@0))
6657          (if (neg)
6658           (ge @0 { build_real (TREE_TYPE (@0), max); })
6659           (le @0 { build_real (TREE_TYPE (@0), max); }))
6660          (if (neg)
6661           (unge @0 { build_real (TREE_TYPE (@0), max); })
6662           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6664  /* If this is a comparison of a real constant with a PLUS_EXPR
6665     or a MINUS_EXPR of a real constant, we can convert it into a
6666     comparison with a revised real constant as long as no overflow
6667     occurs when unsafe_math_optimizations are enabled.  */
6668  (if (flag_unsafe_math_optimizations)
6669   (for op (plus minus)
6670    (simplify
6671     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6672     (with
6673      {
6674        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6675                                TREE_TYPE (@1), @2, @1);
6676      }
6677      (if (tem && !TREE_OVERFLOW (tem))
6678       (cmp @0 { tem; }))))))
6680  /* Likewise, we can simplify a comparison of a real constant with
6681     a MINUS_EXPR whose first operand is also a real constant, i.e.
6682     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6683     floating-point types only if -fassociative-math is set.  */
6684  (if (flag_associative_math)
6685   (simplify
6686    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6687    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6688     (if (tem && !TREE_OVERFLOW (tem))
6689      (cmp { tem; } @1)))))
6691  /* Fold comparisons against built-in math functions.  */
6692  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6693   (for sq (SQRT)
6694    (simplify
6695     (cmp (sq @0) REAL_CST@1)
6696     (switch
6697      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6698       (switch
6699        /* sqrt(x) < y is always false, if y is negative.  */
6700        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6701         { constant_boolean_node (false, type); })
6702        /* sqrt(x) > y is always true, if y is negative and we
6703           don't care about NaNs, i.e. negative values of x.  */
6704        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6705         { constant_boolean_node (true, type); })
6706        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6707        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6708      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6709       (switch
6710        /* sqrt(x) < 0 is always false.  */
6711        (if (cmp == LT_EXPR)
6712         { constant_boolean_node (false, type); })
6713        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6714        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6715         { constant_boolean_node (true, type); })
6716        /* sqrt(x) <= 0 -> x == 0.  */
6717        (if (cmp == LE_EXPR)
6718         (eq @0 @1))
6719        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6720           == or !=.  In the last case:
6722             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6724           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6725           the results for other x follow from natural arithmetic.  */
6726        (cmp @0 @1)))
6727      (if ((cmp == LT_EXPR
6728            || cmp == LE_EXPR
6729            || cmp == GT_EXPR
6730            || cmp == GE_EXPR)
6731           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6732           /* Give up for -frounding-math.  */
6733           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6734       (with
6735        {
6736          REAL_VALUE_TYPE c2;
6737          enum tree_code ncmp = cmp;
6738          const real_format *fmt
6739            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6740          real_arithmetic (&c2, MULT_EXPR,
6741                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6742          real_convert (&c2, fmt, &c2);
6743          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6744             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6745          if (!REAL_VALUE_ISINF (c2))
6746            {
6747              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6748                                         build_real (TREE_TYPE (@0), c2));
6749              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6750                ncmp = ERROR_MARK;
6751              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6752                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6753                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6754              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6755                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6756                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6757              else
6758                {
6759                  /* With rounding to even, sqrt of up to 3 different values
6760                     gives the same normal result, so in some cases c2 needs
6761                     to be adjusted.  */
6762                  REAL_VALUE_TYPE c2alt, tow;
6763                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6764                    tow = dconst0;
6765                  else
6766                    tow = dconstinf;
6767                  real_nextafter (&c2alt, fmt, &c2, &tow);
6768                  real_convert (&c2alt, fmt, &c2alt);
6769                  if (REAL_VALUE_ISINF (c2alt))
6770                    ncmp = ERROR_MARK;
6771                  else
6772                    {
6773                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6774                                            build_real (TREE_TYPE (@0), c2alt));
6775                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6776                        ncmp = ERROR_MARK;
6777                      else if (real_equal (&TREE_REAL_CST (c3),
6778                                           &TREE_REAL_CST (@1)))
6779                        c2 = c2alt;
6780                    }
6781                }
6782            }
6783        }
6784        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6785         (if (REAL_VALUE_ISINF (c2))
6786          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6787          (if (HONOR_INFINITIES (@0))
6788           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6789           { constant_boolean_node (false, type); })
6790          /* sqrt(x) > c is the same as x > c*c.  */
6791          (if (ncmp != ERROR_MARK)
6792           (if (ncmp == GE_EXPR)
6793            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6794            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6795         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6796         (if (REAL_VALUE_ISINF (c2))
6797          (switch
6798           /* sqrt(x) < y is always true, when y is a very large
6799              value and we don't care about NaNs or Infinities.  */
6800           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6801            { constant_boolean_node (true, type); })
6802           /* sqrt(x) < y is x != +Inf when y is very large and we
6803              don't care about NaNs.  */
6804           (if (! HONOR_NANS (@0))
6805            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6806           /* sqrt(x) < y is x >= 0 when y is very large and we
6807              don't care about Infinities.  */
6808           (if (! HONOR_INFINITIES (@0))
6809            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6810           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6811           (if (GENERIC)
6812            (truth_andif
6813             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6814             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6815          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6816          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6817           (if (ncmp == LT_EXPR)
6818            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6819            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6820           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6821           (if (ncmp != ERROR_MARK && GENERIC)
6822            (if (ncmp == LT_EXPR)
6823             (truth_andif
6824              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6825              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6826             (truth_andif
6827              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6828              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6829    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6830    (simplify
6831     (cmp (sq @0) (sq @1))
6832       (if (! HONOR_NANS (@0))
6833         (cmp @0 @1))))))
6835 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6836 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6837      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6838  (simplify
6839   (cmp (float@0 @1) (float @2))
6840    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6841         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6842     (with
6843      {
6844        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6845        tree type1 = TREE_TYPE (@1);
6846        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6847        tree type2 = TREE_TYPE (@2);
6848        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6849      }
6850      (if (fmt.can_represent_integral_type_p (type1)
6851           && fmt.can_represent_integral_type_p (type2))
6852       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6853        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6854        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6855             && type1_signed_p >= type2_signed_p)
6856         (icmp @1 (convert @2))
6857         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6858              && type1_signed_p <= type2_signed_p)
6859          (icmp (convert:type2 @1) @2)
6860          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6861               && type1_signed_p == type2_signed_p)
6862           (icmp @1 @2))))))))))
6864 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6865 (for cmp  (lt le eq ne ge gt)
6866      icmp (le le eq ne ge ge)
6867  (simplify
6868   (cmp (float @0) REAL_CST@1)
6869    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6870         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6871     (with
6872      {
6873        tree itype = TREE_TYPE (@0);
6874        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6875        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6876        /* Be careful to preserve any potential exceptions due to NaNs.
6877           qNaNs are ok in == or != context.  */
6878        bool exception_p = real_isnan (cst)
6879                           && flag_trapping_math
6880                           && ((cmp != EQ_EXPR && cmp != NE_EXPR)
6881                               || (cst->signalling
6882                                   && HONOR_SNANS (TREE_TYPE (@1))));
6883      }
6884      /* TODO: allow non-fitting itype and SNaNs when
6885         -fno-trapping-math.  */
6886      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6887       (with
6888        {
6889          signop isign = TYPE_SIGN (itype);
6890          REAL_VALUE_TYPE imin, imax;
6891          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6892          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6894          REAL_VALUE_TYPE icst;
6895          if (cmp == GT_EXPR || cmp == GE_EXPR)
6896            real_ceil (&icst, fmt, cst);
6897          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6898            real_floor (&icst, fmt, cst);
6899          else
6900            real_trunc (&icst, fmt, cst);
6902          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6904          bool overflow_p = false;
6905          wide_int icst_val
6906            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6907        }
6908        (switch
6909         /* Optimize cases when CST is outside of ITYPE's range.  */
6910         (if (real_compare (LT_EXPR, cst, &imin))
6911          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6912                                   type); })
6913         (if (real_compare (GT_EXPR, cst, &imax))
6914          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6915                                   type); })
6916         /* Remove cast if CST is an integer representable by ITYPE.  */
6917         (if (cst_int_p)
6918          (cmp @0 { gcc_assert (!overflow_p);
6919                    wide_int_to_tree (itype, icst_val); })
6920         )
6921         /* When CST is fractional, optimize
6922             (FTYPE) N == CST -> 0
6923             (FTYPE) N != CST -> 1.  */
6924         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6925          { constant_boolean_node (cmp == NE_EXPR, type); })
6926         /* Otherwise replace with sensible integer constant.  */
6927         (with
6928          {
6929            gcc_checking_assert (!overflow_p);
6930          }
6931          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6933 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6934 (for cmp (eq ne)
6935  (simplify
6936   (cmp (exact_div @0 @1) INTEGER_CST@2)
6937   (if (!integer_zerop (@1))
6938    (if (wi::to_wide (@2) == 0)
6939     (cmp @0 @2)
6940     (if (TREE_CODE (@1) == INTEGER_CST)
6941      (with
6942       {
6943         wi::overflow_type ovf;
6944         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6945                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6946       }
6947       (if (ovf)
6948        { constant_boolean_node (cmp == NE_EXPR, type); }
6949        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6950 (for cmp (lt le gt ge)
6951  (simplify
6952   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6953   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6954    (with
6955     {
6956       wi::overflow_type ovf;
6957       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6958                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6959     }
6960     (if (ovf)
6961      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6962                                         TYPE_SIGN (TREE_TYPE (@2)))
6963                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6964      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6966 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6968    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6969    For large C (more than min/B+2^size), this is also true, with the
6970    multiplication computed modulo 2^size.
6971    For intermediate C, this just tests the sign of A.  */
6972 (for cmp  (lt le gt ge)
6973      cmp2 (ge ge lt lt)
6974  (simplify
6975   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6976   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6977        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6978        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6979    (with
6980     {
6981       tree utype = TREE_TYPE (@2);
6982       wide_int denom = wi::to_wide (@1);
6983       wide_int right = wi::to_wide (@2);
6984       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6985       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6986       bool small = wi::leu_p (right, smax);
6987       bool large = wi::geu_p (right, smin);
6988     }
6989     (if (small || large)
6990      (cmp (convert:utype @0) (mult @2 (convert @1)))
6991      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6993 /* Unordered tests if either argument is a NaN.  */
6994 (simplify
6995  (bit_ior (unordered @0 @0) (unordered @1 @1))
6996  (if (types_match (@0, @1))
6997   (unordered @0 @1)))
6998 (simplify
6999  (bit_and (ordered @0 @0) (ordered @1 @1))
7000  (if (types_match (@0, @1))
7001   (ordered @0 @1)))
7002 (simplify
7003  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
7004  @2)
7005 (simplify
7006  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
7007  @2)
7009 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
7010    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
7012    Note that comparisons
7013      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
7014      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
7015    will be canonicalized to above so there's no need to
7016    consider them here.
7017  */
7019 (for cmp (le gt)
7020      eqcmp (eq ne)
7021  (simplify
7022   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
7023   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
7024    (with
7025     {
7026      tree ty = TREE_TYPE (@0);
7027      unsigned prec = TYPE_PRECISION (ty);
7028      wide_int mask = wi::to_wide (@2, prec);
7029      wide_int rhs = wi::to_wide (@3, prec);
7030      signop sgn = TYPE_SIGN (ty);
7031     }
7032     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
7033          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
7034       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
7035              { build_zero_cst (ty); }))))))
7037 /* -A CMP -B -> B CMP A.  */
7038 (for cmp (tcc_comparison)
7039      scmp (swapped_tcc_comparison)
7040  (simplify
7041   (cmp (negate @0) (negate @1))
7042   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
7043        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7044            && (cmp == EQ_EXPR
7045                || cmp == NE_EXPR
7046                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
7047    (scmp @0 @1)))
7048  (simplify
7049   (cmp (negate @0) CONSTANT_CLASS_P@1)
7050   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
7051        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7052            && (cmp == EQ_EXPR
7053                || cmp == NE_EXPR
7054                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
7055    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
7056     (if (tem && !TREE_OVERFLOW (tem))
7057      (scmp @0 { tem; }))))))
7059 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
7060 (for op (abs absu)
7061  (for eqne (eq ne)
7062   (simplify
7063    (eqne (op @0) zerop@1)
7064    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7066 /* From fold_sign_changed_comparison and fold_widened_comparison.
7067    FIXME: the lack of symmetry is disturbing.  */
7068 (for cmp (simple_comparison)
7069  (simplify
7070   (cmp (convert@0 @00) (convert?@1 @10))
7071   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7072        /* Disable this optimization if we're casting a function pointer
7073           type on targets that require function pointer canonicalization.  */
7074        && !(targetm.have_canonicalize_funcptr_for_compare ()
7075             && ((POINTER_TYPE_P (TREE_TYPE (@00))
7076                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
7077                 || (POINTER_TYPE_P (TREE_TYPE (@10))
7078                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
7079        && single_use (@0))
7080    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
7081         && (TREE_CODE (@10) == INTEGER_CST
7082             || @1 != @10)
7083         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
7084             || cmp == NE_EXPR
7085             || cmp == EQ_EXPR)
7086         && !POINTER_TYPE_P (TREE_TYPE (@00))
7087         /* (int)bool:32 != (int)uint is not the same as
7088            bool:32 != (bool:32)uint since boolean types only have two valid
7089            values independent of their precision.  */
7090         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
7091             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
7092     /* ???  The special-casing of INTEGER_CST conversion was in the original
7093        code and here to avoid a spurious overflow flag on the resulting
7094        constant which fold_convert produces.  */
7095     (if (TREE_CODE (@1) == INTEGER_CST)
7096      (cmp @00 { force_fit_type (TREE_TYPE (@00),
7097                                 wide_int::from (wi::to_wide (@1),
7098                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
7099                                                      TYPE_PRECISION (TREE_TYPE (@00))),
7100                                                 TYPE_SIGN (TREE_TYPE (@1))),
7101                                 0, TREE_OVERFLOW (@1)); })
7102      (cmp @00 (convert @1)))
7104     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
7105      /* If possible, express the comparison in the shorter mode.  */
7106      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
7107            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
7108            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
7109                && TYPE_UNSIGNED (TREE_TYPE (@00))))
7110           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
7111               || ((TYPE_PRECISION (TREE_TYPE (@00))
7112                    >= TYPE_PRECISION (TREE_TYPE (@10)))
7113                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
7114                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
7115               || (TREE_CODE (@1) == INTEGER_CST
7116                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7117                   && int_fits_type_p (@1, TREE_TYPE (@00)))))
7118       (cmp @00 (convert @10))
7119       (if (TREE_CODE (@1) == INTEGER_CST
7120            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7121            && !int_fits_type_p (@1, TREE_TYPE (@00)))
7122        (with
7123         {
7124           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
7125           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
7126           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @1));
7127           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @1, min));
7128         }
7129         (if (above || below)
7130          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
7131           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
7132           (if (cmp == LT_EXPR || cmp == LE_EXPR)
7133            { constant_boolean_node (above ? true : false, type); }
7134            (if (cmp == GT_EXPR || cmp == GE_EXPR)
7135             { constant_boolean_node (above ? false : true, type); })))))))))
7136    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
7137    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
7138         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7139             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
7140         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7141             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
7142     (with
7143      {
7144        tree type1 = TREE_TYPE (@10);
7145        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
7146          {
7147            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
7148            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
7149                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
7150              type1 = float_type_node;
7151            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
7152                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
7153              type1 = double_type_node;
7154          }
7155        tree newtype
7156          = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
7157             ? TREE_TYPE (@00) : type1);
7158      }
7159      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
7160           && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
7161       (cmp (convert:newtype @00) (convert:newtype @10))))))))
7164 (for cmp (eq ne)
7165  (simplify
7166   /* SSA names are canonicalized to 2nd place.  */
7167   (cmp addr@0 SSA_NAME@1)
7168   (with
7169    {
7170      poly_int64 off; tree base;
7171      tree addr = (TREE_CODE (@0) == SSA_NAME
7172                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7173    }
7174    /* A local variable can never be pointed to by
7175       the default SSA name of an incoming parameter.  */
7176    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
7177         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
7178         && (base = get_base_address (TREE_OPERAND (addr, 0)))
7179         && TREE_CODE (base) == VAR_DECL
7180         && auto_var_in_fn_p (base, current_function_decl))
7181     (if (cmp == NE_EXPR)
7182      { constant_boolean_node (true, type); }
7183      { constant_boolean_node (false, type); })
7184     /* If the address is based on @1 decide using the offset.  */
7185     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
7186          && TREE_CODE (base) == MEM_REF
7187          && TREE_OPERAND (base, 0) == @1)
7188      (with { off += mem_ref_offset (base).force_shwi (); }
7189       (if (known_ne (off, 0))
7190        { constant_boolean_node (cmp == NE_EXPR, type); }
7191        (if (known_eq (off, 0))
7192         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
7194 /* Equality compare simplifications from fold_binary  */
7195 (for cmp (eq ne)
7197  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7198     Similarly for NE_EXPR.  */
7199  (simplify
7200   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
7201   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
7202        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
7203    { constant_boolean_node (cmp == NE_EXPR, type); }))
7205  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
7206  (simplify
7207   (cmp (bit_xor @0 @1) integer_zerop)
7208   (cmp @0 @1))
7210  /* (X ^ Y) == Y becomes X == 0.
7211     Likewise (X ^ Y) == X becomes Y == 0.  */
7212  (simplify
7213   (cmp:c (bit_xor:c @0 @1) @0)
7214   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
7216  /* (X & Y) == X becomes (X & ~Y) == 0.  */
7217  (simplify
7218   (cmp:c (bit_and:c @0 @1) @0)
7219   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
7220  (simplify
7221   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
7222   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7223        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7224        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7225        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
7226        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
7227        && !wi::neg_p (wi::to_wide (@1)))
7228    (cmp (bit_and @0 (convert (bit_not @1)))
7229         { build_zero_cst (TREE_TYPE (@0)); })))
7231  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
7232  (simplify
7233   (cmp:c (bit_ior:c @0 @1) @1)
7234   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
7236  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
7237  (simplify
7238   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
7239   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
7240    (cmp @0 (bit_xor @1 (convert @2)))))
7242  (simplify
7243   (cmp (nop_convert? @0) integer_zerop)
7244   (if (tree_expr_nonzero_p (@0))
7245    { constant_boolean_node (cmp == NE_EXPR, type); }))
7247  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
7248  (simplify
7249   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
7250   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
7252 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
7253    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
7254    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
7255    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
7256 (for cmp (eq ne)
7257      ncmp (ge lt)
7258  (for sgncmp (ge lt)
7259   (simplify
7260    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
7261    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7262         && !TYPE_UNSIGNED (TREE_TYPE (@0))
7263         && types_match (@0, @1))
7264     (ncmp (bit_xor @0 @1) @2)))))
7265 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
7266    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
7267 (for cmp (eq ne)
7268      ncmp (lt ge)
7269  (simplify
7270   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
7271    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7272         && !TYPE_UNSIGNED (TREE_TYPE (@0))
7273         && types_match (@0, @1))
7274     (ncmp (bit_xor @0 @1) @2))))
7276 /* If we have (A & C) == C where C is a power of 2, convert this into
7277    (A & C) != 0.  Similarly for NE_EXPR.  */
7278 (for cmp (eq ne)
7279      icmp (ne eq)
7280  (simplify
7281   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
7282   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
7284 #if GIMPLE
7285 /* From fold_binary_op_with_conditional_arg handle the case of
7286    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
7287    compares simplify.  */
7288 (for cmp (simple_comparison)
7289  (simplify
7290   (cmp:c (cond @0 @1 @2) @3)
7291   /* Do not move possibly trapping operations into the conditional as this
7292      pessimizes code and causes gimplification issues when applied late.  */
7293   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
7294        || !operation_could_trap_p (cmp, true, false, @3))
7295    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
7296 #endif
7298 (for cmp (ge lt)
7299 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
7300 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
7301  (simplify
7302   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
7303    (if (INTEGRAL_TYPE_P (type)
7304         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7305         && !TYPE_UNSIGNED (TREE_TYPE (@0))
7306         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
7307     (with
7308      {
7309        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7310      }
7311     (if (cmp == LT_EXPR)
7312      (bit_xor (convert (rshift @0 {shifter;})) @1)
7313      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
7314 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
7315 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
7316  (simplify
7317   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
7318    (if (INTEGRAL_TYPE_P (type)
7319         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7320         && !TYPE_UNSIGNED (TREE_TYPE (@0))
7321         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
7322     (with
7323      {
7324        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7325      }
7326     (if (cmp == GE_EXPR)
7327      (bit_xor (convert (rshift @0 {shifter;})) @1)
7328      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
7330 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
7331    convert this into a shift followed by ANDing with D.  */
7332 (simplify
7333  (cond
7334   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
7335   INTEGER_CST@2 integer_zerop)
7336  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
7337   (with {
7338      int shift = (wi::exact_log2 (wi::to_wide (@2))
7339                   - wi::exact_log2 (wi::to_wide (@1)));
7340    }
7341    (if (shift > 0)
7342     (bit_and
7343      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
7344     (bit_and
7345      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
7346      @2)))))
7348 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7349    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7350 (for cmp (eq ne)
7351      ncmp (ge lt)
7352  (simplify
7353   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
7354   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7355        && type_has_mode_precision_p (TREE_TYPE (@0))
7356        && element_precision (@2) >= element_precision (@0)
7357        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
7358    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
7359     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7361 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7362    this into a right shift or sign extension followed by ANDing with C.  */
7363 (simplify
7364  (cond
7365   (lt @0 integer_zerop)
7366   INTEGER_CST@1 integer_zerop)
7367  (if (integer_pow2p (@1)
7368       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7369   (with {
7370     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7371    }
7372    (if (shift >= 0)
7373     (bit_and
7374      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7375      @1)
7376     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7377        sign extension followed by AND with C will achieve the effect.  */
7378     (bit_and (convert @0) @1)))))
7380 /* When the addresses are not directly of decls compare base and offset.
7381    This implements some remaining parts of fold_comparison address
7382    comparisons but still no complete part of it.  Still it is good
7383    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
7384 (for cmp (simple_comparison)
7385  (simplify
7386   (cmp (convert1?@2 addr@0) (convert2? addr@1))
7387   (with
7388    {
7389      poly_int64 off0, off1;
7390      tree base0, base1;
7391      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7392                                   off0, off1, GENERIC);
7393    }
7394    (if (equal == 1)
7395     (switch
7396      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7397       { constant_boolean_node (known_eq (off0, off1), type); })
7398      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7399       { constant_boolean_node (known_ne (off0, off1), type); })
7400      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7401       { constant_boolean_node (known_lt (off0, off1), type); })
7402      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7403       { constant_boolean_node (known_le (off0, off1), type); })
7404      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7405       { constant_boolean_node (known_ge (off0, off1), type); })
7406      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7407       { constant_boolean_node (known_gt (off0, off1), type); }))
7408     (if (equal == 0)
7409      (switch
7410       (if (cmp == EQ_EXPR)
7411        { constant_boolean_node (false, type); })
7412       (if (cmp == NE_EXPR)
7413        { constant_boolean_node (true, type); })))))))
7415 #if GIMPLE
7416 /* a?~t:t -> (-(a))^t */
7417 (simplify
7418  (cond @0 @1 @2)
7419  (with { bool wascmp; }
7420   (if (INTEGRAL_TYPE_P (type)
7421        && TYPE_UNSIGNED (TREE_TYPE (@0))
7422        && bitwise_inverted_equal_p (@1, @2, wascmp)
7423        && (!wascmp || TYPE_PRECISION (type) == 1))
7424    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7425         || TYPE_PRECISION (type) == 1)
7426     (bit_xor (convert:type @0) @2)
7427     (bit_xor (negate (convert:type @0)) @2)))))
7428 #endif
7430 /* Simplify pointer equality compares using PTA.  */
7431 (for neeq (ne eq)
7432  (simplify
7433   (neeq @0 @1)
7434   (if (POINTER_TYPE_P (TREE_TYPE (@0))
7435        && ptrs_compare_unequal (@0, @1))
7436    { constant_boolean_node (neeq != EQ_EXPR, type); })))
7438 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7439    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7440    Disable the transform if either operand is pointer to function.
7441    This broke pr22051-2.c for arm where function pointer
7442    canonicalizaion is not wanted.  */
7444 (for cmp (ne eq)
7445  (simplify
7446   (cmp (convert @0) INTEGER_CST@1)
7447   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7448          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7449          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7450          /* Don't perform this optimization in GENERIC if @0 has reference
7451             type when sanitizing.  See PR101210.  */
7452          && !(GENERIC
7453               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7454               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7455         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7456             && POINTER_TYPE_P (TREE_TYPE (@1))
7457             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7458        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7459    (cmp @0 (convert @1)))))
7461 /* Non-equality compare simplifications from fold_binary  */
7462 (for cmp (lt gt le ge)
7463  /* Comparisons with the highest or lowest possible integer of
7464     the specified precision will have known values.  */
7465  (simplify
7466   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7467   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7468         || POINTER_TYPE_P (TREE_TYPE (@1))
7469         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7470        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7471    (with
7472     {
7473       tree cst = uniform_integer_cst_p (@1);
7474       tree arg1_type = TREE_TYPE (cst);
7475       unsigned int prec = TYPE_PRECISION (arg1_type);
7476       wide_int max = wi::max_value (arg1_type);
7477       wide_int signed_max = wi::max_value (prec, SIGNED);
7478       wide_int min = wi::min_value (arg1_type);
7479     }
7480     (switch
7481      (if (wi::to_wide (cst) == max)
7482       (switch
7483        (if (cmp == GT_EXPR)
7484         { constant_boolean_node (false, type); })
7485        (if (cmp == GE_EXPR)
7486         (eq @2 @1))
7487        (if (cmp == LE_EXPR)
7488         { constant_boolean_node (true, type); })
7489        (if (cmp == LT_EXPR)
7490         (ne @2 @1))))
7491      (if (wi::to_wide (cst) == min)
7492       (switch
7493        (if (cmp == LT_EXPR)
7494         { constant_boolean_node (false, type); })
7495        (if (cmp == LE_EXPR)
7496         (eq @2 @1))
7497        (if (cmp == GE_EXPR)
7498         { constant_boolean_node (true, type); })
7499        (if (cmp == GT_EXPR)
7500         (ne @2 @1))))
7501      (if (wi::to_wide (cst) == max - 1)
7502       (switch
7503        (if (cmp == GT_EXPR)
7504         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7505                                     wide_int_to_tree (TREE_TYPE (cst),
7506                                                       wi::to_wide (cst)
7507                                                       + 1)); }))
7508        (if (cmp == LE_EXPR)
7509         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7510                                     wide_int_to_tree (TREE_TYPE (cst),
7511                                                       wi::to_wide (cst)
7512                                                       + 1)); }))))
7513      (if (wi::to_wide (cst) == min + 1)
7514       (switch
7515        (if (cmp == GE_EXPR)
7516         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7517                                     wide_int_to_tree (TREE_TYPE (cst),
7518                                                       wi::to_wide (cst)
7519                                                       - 1)); }))
7520        (if (cmp == LT_EXPR)
7521         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7522                                     wide_int_to_tree (TREE_TYPE (cst),
7523                                                       wi::to_wide (cst)
7524                                                       - 1)); }))))
7525      (if (wi::to_wide (cst) == signed_max
7526           && TYPE_UNSIGNED (arg1_type)
7527           && TYPE_MODE (arg1_type) != BLKmode
7528           /* We will flip the signedness of the comparison operator
7529              associated with the mode of @1, so the sign bit is
7530              specified by this mode.  Check that @1 is the signed
7531              max associated with this sign bit.  */
7532           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7533           /* signed_type does not work on pointer types.  */
7534           && INTEGRAL_TYPE_P (arg1_type))
7535       /* The following case also applies to X < signed_max+1
7536          and X >= signed_max+1 because previous transformations.  */
7537       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7538        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7539         (switch
7540          (if (cst == @1 && cmp == LE_EXPR)
7541           (ge (convert:st @0) { build_zero_cst (st); }))
7542          (if (cst == @1 && cmp == GT_EXPR)
7543           (lt (convert:st @0) { build_zero_cst (st); }))
7544          (if (cmp == LE_EXPR)
7545           (ge (view_convert:st @0) { build_zero_cst (st); }))
7546          (if (cmp == GT_EXPR)
7547           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7549 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7550 (simplify
7551  (lt:c @0 (convert (ne @0 integer_zerop)))
7552  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7553   { constant_boolean_node (false, type); }))
7555 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7556 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7557 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7558 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7559 (for outer (ne eq)
7560  (for inner (ne eq)
7561   (simplify
7562    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7563    (with {
7564      bool cst1 = integer_onep (@1);
7565      bool cst0 = integer_zerop (@1);
7566      bool innereq = inner == EQ_EXPR;
7567      bool outereq = outer == EQ_EXPR;
7568     }
7569     (switch
7570      (if (innereq ? cst0 : cst1)
7571       { constant_boolean_node (!outereq, type); })
7572      (if (innereq ? cst1 : cst0)
7573       (with {
7574         tree utype = unsigned_type_for (TREE_TYPE (@0));
7575         tree ucst1 = build_one_cst (utype);
7576        }
7577        (if (!outereq)
7578         (gt (convert:utype @0) { ucst1; })
7579         (le (convert:utype @0) { ucst1; })
7580        )
7581       )
7582      )
7583      (with {
7584        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7585       }
7586       (if (outereq)
7587        (eq @0 { value; })
7588        (ne @0 { value; })
7589       )
7590      )
7591     )
7592    )
7593   )
7597 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7598  /* If the second operand is NaN, the result is constant.  */
7599  (simplify
7600   (cmp @0 REAL_CST@1)
7601   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7602        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7603    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7604                             ? false : true, type); })))
7606 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7607 (simplify
7608   (unordered @0 @1)
7609   (switch
7610     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7611         { constant_boolean_node (true, type); })
7612     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7613         { constant_boolean_node (false, type); })))
7615 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7616 (simplify
7617   (ordered @0 @1)
7618   (switch
7619     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7620         { constant_boolean_node (false, type); })
7621     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7622         { constant_boolean_node (true, type); })))
7624 /* bool_var != 0 becomes bool_var.  */
7625 (simplify
7626  (ne @0 integer_zerop)
7627  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7628       && types_match (type, TREE_TYPE (@0)))
7629   (non_lvalue @0)))
7630 /* bool_var == 1 becomes bool_var.  */
7631 (simplify
7632  (eq @0 integer_onep)
7633  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7634       && types_match (type, TREE_TYPE (@0)))
7635   (non_lvalue @0)))
7636 /* Do not handle
7637    bool_var == 0 becomes !bool_var or
7638    bool_var != 1 becomes !bool_var
7639    here because that only is good in assignment context as long
7640    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7641    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7642    clearly less optimal and which we'll transform again in forwprop.  */
7644 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7645    where ~Y + 1 == pow2 and Z = ~Y.  */
7646 (for cst (VECTOR_CST INTEGER_CST)
7647  (for cmp (eq ne)
7648       icmp (le gt)
7649   (simplify
7650    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7651     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7652      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7653       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7654                          ? optab_vector : optab_default;
7655               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7656        (if (target_supports_op_p (utype, icmp, optab)
7657             || (optimize_vectors_before_lowering_p ()
7658                 && (!target_supports_op_p (type, cmp, optab)
7659                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7660         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7661          (icmp @0 { csts; })
7662          (icmp (view_convert:utype @0) { csts; })))))))))
7664 /* When one argument is a constant, overflow detection can be simplified.
7665    Currently restricted to single use so as not to interfere too much with
7666    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7667    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7668 (for cmp (lt le ge gt)
7669      out (gt gt le le)
7670  (simplify
7671   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7672   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7673        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7674        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7675        && wi::to_wide (@1) != 0
7676        && single_use (@2))
7677    (with {
7678      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7679      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7680     }
7681     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7682                                 wi::max_value (prec, sign)
7683                                 - wi::to_wide (@1)); })))))
7685 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7686    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7687    expects the long form, so we restrict the transformation for now.  */
7688 (for cmp (gt le)
7689  (simplify
7690   (cmp:c (minus@2 @0 @1) @0)
7691   (if (single_use (@2)
7692        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7693        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7694    (cmp @1 @0))))
7696 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7697 (for cmp (ge lt)
7698  (simplify
7699   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7700    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7701         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7702     (cmp @1 @0))))
7704 /* Testing for overflow is unnecessary if we already know the result.  */
7705 /* A - B > A  */
7706 (for cmp (gt le)
7707      out (ne eq)
7708  (simplify
7709   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7710   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7711        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7712    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7713 /* A + B < A  */
7714 (for cmp (lt ge)
7715      out (ne eq)
7716  (simplify
7717   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7718   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7719        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7720    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7722 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7723    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7724 (for cmp (lt ge)
7725      out (ne eq)
7726  (simplify
7727   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7728   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7729    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7730     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7732 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7733    is at least twice as wide as type of A and B, simplify to
7734    __builtin_mul_overflow (A, B, <unused>).  */
7735 (for cmp (eq ne)
7736  (simplify
7737   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7738        integer_zerop)
7739   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7740        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7741        && TYPE_UNSIGNED (TREE_TYPE (@0))
7742        && (TYPE_PRECISION (TREE_TYPE (@3))
7743            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7744        && tree_fits_uhwi_p (@2)
7745        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7746        && types_match (@0, @1)
7747        && type_has_mode_precision_p (TREE_TYPE (@0))
7748        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7749            != CODE_FOR_nothing))
7750    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7751     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7753 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7754 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7755  (simplify
7756   (ovf (convert@2 @0) @1)
7757   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7758        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7759        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7760        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7761    (ovf @0 @1)))
7762  (simplify
7763   (ovf @1 (convert@2 @0))
7764   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7765        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7766        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7767        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7768    (ovf @1 @0))))
7770 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7771    are unsigned to x > (umax / cst).  Similarly for signed type, but
7772    in that case it needs to be outside of a range.  */
7773 (simplify
7774  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7775   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7776        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7777        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7778        && int_fits_type_p (@1, TREE_TYPE (@0)))
7779    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7780     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7781     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7782      (if (integer_minus_onep (@1))
7783       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7784       (with
7785        {
7786          tree div = fold_convert (TREE_TYPE (@0), @1);
7787          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7788                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7789          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7790                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7791          tree etype = range_check_type (TREE_TYPE (@0));
7792          if (etype)
7793            {
7794              if (wi::neg_p (wi::to_wide (div)))
7795                std::swap (lo, hi);
7796              lo = fold_convert (etype, lo);
7797              hi = fold_convert (etype, hi);
7798              hi = int_const_binop (MINUS_EXPR, hi, lo);
7799            }
7800        }
7801        (if (etype)
7802         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7804 /* Simplification of math builtins.  These rules must all be optimizations
7805    as well as IL simplifications.  If there is a possibility that the new
7806    form could be a pessimization, the rule should go in the canonicalization
7807    section that follows this one.
7809    Rules can generally go in this section if they satisfy one of
7810    the following:
7812    - the rule describes an identity
7814    - the rule replaces calls with something as simple as addition or
7815      multiplication
7817    - the rule contains unary calls only and simplifies the surrounding
7818      arithmetic.  (The idea here is to exclude non-unary calls in which
7819      one operand is constant and in which the call is known to be cheap
7820      when the operand has that value.)  */
7822 (if (flag_unsafe_math_optimizations)
7823  /* Simplify x / sqrt(x) -> sqrt(x).  */
7824  (simplify
7825   (rdiv @0 (SQRT @0)) (SQRT @0))
7827  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7828  (simplify
7829   (mult (SQRT_ALL@1 @0) @1)
7830   (if (!tree_expr_maybe_signaling_nan_p (@0))
7831    @0))
7833  (for op (plus minus)
7834   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7835   (simplify
7836    (op (rdiv @0 @1)
7837        (rdiv @2 @1))
7838    (rdiv (op @0 @2) @1)))
7840  (for cmp (lt le gt ge)
7841       neg_cmp (gt ge lt le)
7842   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7843   (simplify
7844    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7845    (with
7846     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7847     (if (tem
7848          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7849               || (real_zerop (tem) && !real_zerop (@1))))
7850      (switch
7851       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7852        (cmp @0 { tem; }))
7853       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7854        (neg_cmp @0 { tem; })))))))
7856  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7857  (for root (SQRT CBRT)
7858   (simplify
7859    (mult (root:s @0) (root:s @1))
7860     (root (mult @0 @1))))
7862  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7863  (for exps (EXP EXP2 EXP10 POW10)
7864   (simplify
7865    (mult (exps:s @0) (exps:s @1))
7866     (exps (plus @0 @1))))
7868  /* Simplify a/root(b/c) into a*root(c/b).  */
7869  (for root (SQRT CBRT)
7870   (simplify
7871    (rdiv @0 (root:s (rdiv:s @1 @2)))
7872     (mult @0 (root (rdiv @2 @1)))))
7874  /* Simplify x/expN(y) into x*expN(-y).  */
7875  (for exps (EXP EXP2 EXP10 POW10)
7876   (simplify
7877    (rdiv @0 (exps:s @1))
7878     (mult @0 (exps (negate @1)))))
7880  (for logs (LOG LOG2 LOG10 LOG10)
7881       exps (EXP EXP2 EXP10 POW10)
7882   /* logN(expN(x)) -> x.  */
7883   (simplify
7884    (logs (exps @0))
7885    @0)
7886   /* expN(logN(x)) -> x.  */
7887   (simplify
7888    (exps (logs @0))
7889    @0))
7891  /* Optimize logN(func()) for various exponential functions.  We
7892     want to determine the value "x" and the power "exponent" in
7893     order to transform logN(x**exponent) into exponent*logN(x).  */
7894  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7895       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7896   (simplify
7897    (logs (exps @0))
7898    (if (SCALAR_FLOAT_TYPE_P (type))
7899     (with {
7900       tree x;
7901       switch (exps)
7902         {
7903         CASE_CFN_EXP:
7904           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7905           x = build_real_truncate (type, dconst_e ());
7906           break;
7907         CASE_CFN_EXP2:
7908           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7909           x = build_real (type, dconst2);
7910           break;
7911         CASE_CFN_EXP10:
7912         CASE_CFN_POW10:
7913           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7914           {
7915             REAL_VALUE_TYPE dconst10;
7916             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7917             x = build_real (type, dconst10);
7918           }
7919           break;
7920         default:
7921           gcc_unreachable ();
7922         }
7923       }
7924      (mult (logs { x; }) @0)))))
7926  (for logs (LOG LOG
7927             LOG2 LOG2
7928             LOG10 LOG10)
7929       exps (SQRT CBRT)
7930   (simplify
7931    (logs (exps @0))
7932    (if (SCALAR_FLOAT_TYPE_P (type))
7933     (with {
7934       tree x;
7935       switch (exps)
7936         {
7937         CASE_CFN_SQRT:
7938           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7939           x = build_real (type, dconsthalf);
7940           break;
7941         CASE_CFN_CBRT:
7942           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7943           x = build_real_truncate (type, dconst_third ());
7944           break;
7945         default:
7946           gcc_unreachable ();
7947         }
7948       }
7949      (mult { x; } (logs @0))))))
7951  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7952  (for logs (LOG LOG2 LOG10)
7953       pows (POW)
7954   (simplify
7955    (logs (pows @0 @1))
7956    (mult @1 (logs @0))))
7958  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7959     or if C is a positive power of 2,
7960     pow(C,x) -> exp2(log2(C)*x).  */
7961 #if GIMPLE
7962  (for pows (POW)
7963       exps (EXP)
7964       logs (LOG)
7965       exp2s (EXP2)
7966       log2s (LOG2)
7967   (simplify
7968    (pows REAL_CST@0 @1)
7969    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7970         && real_isfinite (TREE_REAL_CST_PTR (@0))
7971         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7972            the use_exp2 case until after vectorization.  It seems actually
7973            beneficial for all constants to postpone this until later,
7974            because exp(log(C)*x), while faster, will have worse precision
7975            and if x folds into a constant too, that is unnecessary
7976            pessimization.  */
7977         && canonicalize_math_after_vectorization_p ())
7978     (with {
7979        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7980        bool use_exp2 = false;
7981        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7982            && value->cl == rvc_normal)
7983          {
7984            REAL_VALUE_TYPE frac_rvt = *value;
7985            SET_REAL_EXP (&frac_rvt, 1);
7986            if (real_equal (&frac_rvt, &dconst1))
7987              use_exp2 = true;
7988          }
7989      }
7990      (if (!use_exp2)
7991       (if (optimize_pow_to_exp (@0, @1))
7992        (exps (mult (logs @0) @1)))
7993       (exp2s (mult (log2s @0) @1)))))))
7994 #endif
7996  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7997  (for pows (POW)
7998       exps (EXP EXP2 EXP10 POW10)
7999       logs (LOG LOG2 LOG10 LOG10)
8000   (simplify
8001    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
8002    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
8003         && real_isfinite (TREE_REAL_CST_PTR (@0)))
8004     (exps (plus (mult (logs @0) @1) @2)))))
8006  (for sqrts (SQRT)
8007       cbrts (CBRT)
8008       pows (POW)
8009       exps (EXP EXP2 EXP10 POW10)
8010   /* sqrt(expN(x)) -> expN(x*0.5).  */
8011   (simplify
8012    (sqrts (exps @0))
8013    (exps (mult @0 { build_real (type, dconsthalf); })))
8014   /* cbrt(expN(x)) -> expN(x/3).  */
8015   (simplify
8016    (cbrts (exps @0))
8017    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
8018   /* pow(expN(x), y) -> expN(x*y).  */
8019   (simplify
8020    (pows (exps @0) @1)
8021    (exps (mult @0 @1))))
8023  /* tan(atan(x)) -> x.  */
8024  (for tans (TAN)
8025       atans (ATAN)
8026   (simplify
8027    (tans (atans @0))
8028    @0)))
8030  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
8031  (for sins (SIN)
8032       atans (ATAN)
8033       sqrts (SQRT)
8034       copysigns (COPYSIGN)
8035   (simplify
8036    (sins (atans:s @0))
8037    (with
8038      {
8039       REAL_VALUE_TYPE r_cst;
8040       build_sinatan_real (&r_cst, type);
8041       tree t_cst = build_real (type, r_cst);
8042       tree t_one = build_one_cst (type);
8043      }
8044     (if (SCALAR_FLOAT_TYPE_P (type))
8045      (cond (lt (abs @0) { t_cst; })
8046       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
8047       (copysigns { t_one; } @0))))))
8049 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
8050  (for coss (COS)
8051       atans (ATAN)
8052       sqrts (SQRT)
8053       copysigns (COPYSIGN)
8054   (simplify
8055    (coss (atans:s @0))
8056    (with
8057      {
8058       REAL_VALUE_TYPE r_cst;
8059       build_sinatan_real (&r_cst, type);
8060       tree t_cst = build_real (type, r_cst);
8061       tree t_one = build_one_cst (type);
8062       tree t_zero = build_zero_cst (type);
8063      }
8064     (if (SCALAR_FLOAT_TYPE_P (type))
8065      (cond (lt (abs @0) { t_cst; })
8066       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
8067       (copysigns { t_zero; } @0))))))
8069  (if (!flag_errno_math)
8070   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
8071   (for sinhs (SINH)
8072        atanhs (ATANH)
8073        sqrts (SQRT)
8074    (simplify
8075     (sinhs (atanhs:s @0))
8076     (with { tree t_one = build_one_cst (type); }
8077     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
8079   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
8080   (for coshs (COSH)
8081        atanhs (ATANH)
8082        sqrts (SQRT)
8083    (simplify
8084     (coshs (atanhs:s @0))
8085     (with { tree t_one = build_one_cst (type); }
8086     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
8088 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
8089 (simplify
8090  (CABS (complex:C @0 real_zerop@1))
8091  (abs @0))
8093 /* trunc(trunc(x)) -> trunc(x), etc.  */
8094 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
8095  (simplify
8096   (fns (fns @0))
8097   (fns @0)))
8098 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
8099 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
8100  (simplify
8101   (fns integer_valued_real_p@0)
8102   @0))
8104 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
8105 (simplify
8106  (HYPOT:c @0 real_zerop@1)
8107  (abs @0))
8109 /* pow(1,x) -> 1.  */
8110 (simplify
8111  (POW real_onep@0 @1)
8112  @0)
8114 (simplify
8115  /* copysign(x,x) -> x.  */
8116  (COPYSIGN_ALL @0 @0)
8117  @0)
8119 (simplify
8120  /* copysign(x,-x) -> -x.  */
8121  (COPYSIGN_ALL @0 (negate@1 @0))
8122  @1)
8124 (simplify
8125  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
8126  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
8127  (abs @0))
8129 (simplify
8130  /* fabs (copysign(x, y)) -> fabs (x).  */
8131  (abs (COPYSIGN_ALL @0 @1))
8132  (abs @0))
8134 (for scale (LDEXP SCALBN SCALBLN)
8135  /* ldexp(0, x) -> 0.  */
8136  (simplify
8137   (scale real_zerop@0 @1)
8138   @0)
8139  /* ldexp(x, 0) -> x.  */
8140  (simplify
8141   (scale @0 integer_zerop@1)
8142   @0)
8143  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
8144  (simplify
8145   (scale REAL_CST@0 @1)
8146   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
8147    @0)))
8149 /* Canonicalization of sequences of math builtins.  These rules represent
8150    IL simplifications but are not necessarily optimizations.
8152    The sincos pass is responsible for picking "optimal" implementations
8153    of math builtins, which may be more complicated and can sometimes go
8154    the other way, e.g. converting pow into a sequence of sqrts.
8155    We only want to do these canonicalizations before the pass has run.  */
8157 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
8158  /* Simplify tan(x) * cos(x) -> sin(x). */
8159  (simplify
8160   (mult:c (TAN:s @0) (COS:s @0))
8161    (SIN @0))
8163  /* Simplify x * pow(x,c) -> pow(x,c+1). */
8164  (simplify
8165   (mult:c @0 (POW:s @0 REAL_CST@1))
8166   (if (!TREE_OVERFLOW (@1))
8167    (POW @0 (plus @1 { build_one_cst (type); }))))
8169  /* Simplify sin(x) / cos(x) -> tan(x). */
8170  (simplify
8171   (rdiv (SIN:s @0) (COS:s @0))
8172    (TAN @0))
8174  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
8175  (simplify
8176   (rdiv (SINH:s @0) (COSH:s @0))
8177    (TANH @0))
8179  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
8180  (simplify
8181    (rdiv (TANH:s @0) (SINH:s @0))
8182    (rdiv {build_one_cst (type);} (COSH @0)))
8184  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
8185  (simplify
8186   (rdiv (COS:s @0) (SIN:s @0))
8187    (rdiv { build_one_cst (type); } (TAN @0)))
8189  /* Simplify sin(x) / tan(x) -> cos(x). */
8190  (simplify
8191   (rdiv (SIN:s @0) (TAN:s @0))
8192   (if (! HONOR_NANS (@0)
8193        && ! HONOR_INFINITIES (@0))
8194    (COS @0)))
8196  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
8197  (simplify
8198   (rdiv (TAN:s @0) (SIN:s @0))
8199   (if (! HONOR_NANS (@0)
8200        && ! HONOR_INFINITIES (@0))
8201    (rdiv { build_one_cst (type); } (COS @0))))
8203  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
8204  (simplify
8205   (mult (POW:s @0 @1) (POW:s @0 @2))
8206    (POW @0 (plus @1 @2)))
8208  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
8209  (simplify
8210   (mult (POW:s @0 @1) (POW:s @2 @1))
8211    (POW (mult @0 @2) @1))
8213  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
8214  (simplify
8215   (mult (POWI:s @0 @1) (POWI:s @2 @1))
8216    (POWI (mult @0 @2) @1))
8218  /* Simplify pow(x,c) / x -> pow(x,c-1). */
8219  (simplify
8220   (rdiv (POW:s @0 REAL_CST@1) @0)
8221   (if (!TREE_OVERFLOW (@1))
8222    (POW @0 (minus @1 { build_one_cst (type); }))))
8224  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
8225  (simplify
8226   (rdiv @0 (POW:s @1 @2))
8227    (mult @0 (POW @1 (negate @2))))
8229  (for sqrts (SQRT)
8230       cbrts (CBRT)
8231       pows (POW)
8232   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
8233   (simplify
8234    (sqrts (sqrts @0))
8235    (pows @0 { build_real (type, dconst_quarter ()); }))
8236   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
8237   (simplify
8238    (sqrts (cbrts @0))
8239    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
8240   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
8241   (simplify
8242    (cbrts (sqrts @0))
8243    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
8244   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
8245   (simplify
8246    (cbrts (cbrts tree_expr_nonnegative_p@0))
8247    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
8248   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
8249   (simplify
8250    (sqrts (pows @0 @1))
8251    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
8252   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
8253   (simplify
8254    (cbrts (pows tree_expr_nonnegative_p@0 @1))
8255    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
8256   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
8257   (simplify
8258    (pows (sqrts @0) @1)
8259    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
8260   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
8261   (simplify
8262    (pows (cbrts tree_expr_nonnegative_p@0) @1)
8263    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
8264   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
8265   (simplify
8266    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
8267    (pows @0 (mult @1 @2))))
8269  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
8270  (simplify
8271   (CABS (complex @0 @0))
8272   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
8274  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
8275  (simplify
8276   (HYPOT @0 @0)
8277   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
8279  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
8280  (for cexps (CEXP)
8281       exps (EXP)
8282       cexpis (CEXPI)
8283   (simplify
8284    (cexps compositional_complex@0)
8285    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
8286     (complex
8287      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
8288      (mult @1 (imagpart @2)))))))
8290 (if (canonicalize_math_p ())
8291  /* floor(x) -> trunc(x) if x is nonnegative.  */
8292  (for floors (FLOOR_ALL)
8293       truncs (TRUNC_ALL)
8294   (simplify
8295    (floors tree_expr_nonnegative_p@0)
8296    (truncs @0))))
8298 (match double_value_p
8299  @0
8300  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
8301 (for froms (BUILT_IN_TRUNCL
8302             BUILT_IN_FLOORL
8303             BUILT_IN_CEILL
8304             BUILT_IN_ROUNDL
8305             BUILT_IN_NEARBYINTL
8306             BUILT_IN_RINTL)
8307      tos (BUILT_IN_TRUNC
8308           BUILT_IN_FLOOR
8309           BUILT_IN_CEIL
8310           BUILT_IN_ROUND
8311           BUILT_IN_NEARBYINT
8312           BUILT_IN_RINT)
8313  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
8314  (if (optimize && canonicalize_math_p ())
8315   (simplify
8316    (froms (convert double_value_p@0))
8317    (convert (tos @0)))))
8319 (match float_value_p
8320  @0
8321  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
8322 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
8323             BUILT_IN_FLOORL BUILT_IN_FLOOR
8324             BUILT_IN_CEILL BUILT_IN_CEIL
8325             BUILT_IN_ROUNDL BUILT_IN_ROUND
8326             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
8327             BUILT_IN_RINTL BUILT_IN_RINT)
8328      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
8329           BUILT_IN_FLOORF BUILT_IN_FLOORF
8330           BUILT_IN_CEILF BUILT_IN_CEILF
8331           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
8332           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
8333           BUILT_IN_RINTF BUILT_IN_RINTF)
8334  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
8335     if x is a float.  */
8336  (if (optimize && canonicalize_math_p ()
8337       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
8338   (simplify
8339    (froms (convert float_value_p@0))
8340    (convert (tos @0)))))
8342 #if GIMPLE
8343 (match float16_value_p
8344  @0
8345  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
8346 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
8347             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
8348             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
8349             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
8350             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
8351             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
8352             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
8353             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
8354      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
8355           IFN_FLOOR IFN_FLOOR IFN_FLOOR
8356           IFN_CEIL IFN_CEIL IFN_CEIL
8357           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
8358           IFN_ROUND IFN_ROUND IFN_ROUND
8359           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
8360           IFN_RINT IFN_RINT IFN_RINT
8361           IFN_SQRT IFN_SQRT IFN_SQRT)
8362  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
8363     if x is a _Float16.  */
8364  (simplify
8365    (convert (froms (convert float16_value_p@0)))
8366      (if (optimize
8367           && types_match (type, TREE_TYPE (@0))
8368           && direct_internal_fn_supported_p (as_internal_fn (tos),
8369                                              type, OPTIMIZE_FOR_BOTH))
8370        (tos @0))))
8372 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8373    x,y is float value, similar for _Float16/double.  */
8374 (for copysigns (COPYSIGN_ALL)
8375  (simplify
8376   (convert (copysigns (convert@2 @0) (convert @1)))
8377    (if (optimize
8378        && !HONOR_SNANS (@2)
8379        && types_match (type, TREE_TYPE (@0))
8380        && types_match (type, TREE_TYPE (@1))
8381        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8382        && direct_internal_fn_supported_p (IFN_COPYSIGN,
8383                                           type, OPTIMIZE_FOR_BOTH))
8384     (IFN_COPYSIGN @0 @1))))
8386 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8387      tos (IFN_FMA IFN_FMA IFN_FMA)
8388  (simplify
8389   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8390    (if (flag_unsafe_math_optimizations
8391        && optimize
8392        && FLOAT_TYPE_P (type)
8393        && FLOAT_TYPE_P (TREE_TYPE (@3))
8394        && types_match (type, TREE_TYPE (@0))
8395        && types_match (type, TREE_TYPE (@1))
8396        && types_match (type, TREE_TYPE (@2))
8397        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8398        && direct_internal_fn_supported_p (as_internal_fn (tos),
8399                                           type, OPTIMIZE_FOR_BOTH))
8400     (tos @0 @1 @2))))
8402 (for maxmin (max min)
8403  (simplify
8404   (convert (maxmin (convert@2 @0) (convert @1)))
8405    (if (optimize
8406        && FLOAT_TYPE_P (type)
8407        && FLOAT_TYPE_P (TREE_TYPE (@2))
8408        && types_match (type, TREE_TYPE (@0))
8409        && types_match (type, TREE_TYPE (@1))
8410        && element_precision (type) < element_precision (TREE_TYPE (@2)))
8411     (maxmin @0 @1))))
8412 #endif
8414 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8415      tos (XFLOOR XCEIL XROUND XRINT)
8416  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
8417  (if (optimize && canonicalize_math_p ())
8418   (simplify
8419    (froms (convert double_value_p@0))
8420    (tos @0))))
8422 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8423             XFLOOR XCEIL XROUND XRINT)
8424      tos (XFLOORF XCEILF XROUNDF XRINTF)
8425  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8426     if x is a float.  */
8427  (if (optimize && canonicalize_math_p ())
8428   (simplify
8429    (froms (convert float_value_p@0))
8430    (tos @0))))
8432 (if (canonicalize_math_p ())
8433  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
8434  (for floors (IFLOOR LFLOOR LLFLOOR)
8435   (simplify
8436    (floors tree_expr_nonnegative_p@0)
8437    (fix_trunc @0))))
8439 (if (canonicalize_math_p ())
8440  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
8441  (for fns (IFLOOR LFLOOR LLFLOOR
8442            ICEIL LCEIL LLCEIL
8443            IROUND LROUND LLROUND)
8444   (simplify
8445    (fns integer_valued_real_p@0)
8446    (fix_trunc @0)))
8447  (if (!flag_errno_math)
8448   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
8449   (for rints (IRINT LRINT LLRINT)
8450    (simplify
8451     (rints integer_valued_real_p@0)
8452     (fix_trunc @0)))))
8454 (if (canonicalize_math_p ())
8455  (for ifn (IFLOOR ICEIL IROUND IRINT)
8456       lfn (LFLOOR LCEIL LROUND LRINT)
8457       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8458   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8459      sizeof (int) == sizeof (long).  */
8460   (if (TYPE_PRECISION (integer_type_node)
8461        == TYPE_PRECISION (long_integer_type_node))
8462    (simplify
8463     (ifn @0)
8464     (lfn:long_integer_type_node @0)))
8465   /* Canonicalize llround (x) to lround (x) on LP64 targets where
8466      sizeof (long long) == sizeof (long).  */
8467   (if (TYPE_PRECISION (long_long_integer_type_node)
8468        == TYPE_PRECISION (long_integer_type_node))
8469    (simplify
8470     (llfn @0)
8471     (lfn:long_integer_type_node @0)))))
8473 /* cproj(x) -> x if we're ignoring infinities.  */
8474 (simplify
8475  (CPROJ @0)
8476  (if (!HONOR_INFINITIES (type))
8477    @0))
8479 /* If the real part is inf and the imag part is known to be
8480    nonnegative, return (inf + 0i).  */
8481 (simplify
8482  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8483  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8484   { build_complex_inf (type, false); }))
8486 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8487 (simplify
8488  (CPROJ (complex @0 REAL_CST@1))
8489  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8490   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8492 (for pows (POW)
8493      sqrts (SQRT)
8494      cbrts (CBRT)
8495  (simplify
8496   (pows @0 REAL_CST@1)
8497   (with {
8498     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8499     REAL_VALUE_TYPE tmp;
8500    }
8501    (switch
8502     /* pow(x,0) -> 1.  */
8503     (if (real_equal (value, &dconst0))
8504      { build_real (type, dconst1); })
8505     /* pow(x,1) -> x.  */
8506     (if (real_equal (value, &dconst1))
8507      @0)
8508     /* pow(x,-1) -> 1/x.  */
8509     (if (real_equal (value, &dconstm1))
8510      (rdiv { build_real (type, dconst1); } @0))
8511     /* pow(x,0.5) -> sqrt(x).  */
8512     (if (flag_unsafe_math_optimizations
8513          && canonicalize_math_p ()
8514          && real_equal (value, &dconsthalf))
8515      (sqrts @0))
8516     /* pow(x,1/3) -> cbrt(x).  */
8517     (if (flag_unsafe_math_optimizations
8518          && canonicalize_math_p ()
8519          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8520              real_equal (value, &tmp)))
8521      (cbrts @0))))))
8523 /* powi(1,x) -> 1.  */
8524 (simplify
8525  (POWI real_onep@0 @1)
8526  @0)
8528 (simplify
8529  (POWI @0 INTEGER_CST@1)
8530  (switch
8531   /* powi(x,0) -> 1.  */
8532   (if (wi::to_wide (@1) == 0)
8533    { build_real (type, dconst1); })
8534   /* powi(x,1) -> x.  */
8535   (if (wi::to_wide (@1) == 1)
8536    @0)
8537   /* powi(x,-1) -> 1/x.  */
8538   (if (wi::to_wide (@1) == -1)
8539    (rdiv { build_real (type, dconst1); } @0))))
8541 /* Narrowing of arithmetic and logical operations.
8543    These are conceptually similar to the transformations performed for
8544    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8545    term we want to move all that code out of the front-ends into here.  */
8547 /* Convert (outertype)((innertype0)a+(innertype1)b)
8548    into ((newtype)a+(newtype)b) where newtype
8549    is the widest mode from all of these.  */
8550 (for op (plus minus mult rdiv)
8551  (simplify
8552    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8553    /* If we have a narrowing conversion of an arithmetic operation where
8554       both operands are widening conversions from the same type as the outer
8555       narrowing conversion.  Then convert the innermost operands to a
8556       suitable unsigned type (to avoid introducing undefined behavior),
8557       perform the operation and convert the result to the desired type.  */
8558    (if (INTEGRAL_TYPE_P (type)
8559         && op != MULT_EXPR
8560         && op != RDIV_EXPR
8561         /* We check for type compatibility between @0 and @1 below,
8562            so there's no need to check that @2/@4 are integral types.  */
8563         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8564         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8565         /* The precision of the type of each operand must match the
8566            precision of the mode of each operand, similarly for the
8567            result.  */
8568         && type_has_mode_precision_p (TREE_TYPE (@1))
8569         && type_has_mode_precision_p (TREE_TYPE (@2))
8570         && type_has_mode_precision_p (type)
8571         /* The inner conversion must be a widening conversion.  */
8572         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8573         && types_match (@1, type)
8574         && (types_match (@1, @2)
8575             /* Or the second operand is const integer or converted const
8576                integer from valueize.  */
8577             || poly_int_tree_p (@4)))
8578      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8579        (op @1 (convert @2))
8580        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8581         (convert (op (convert:utype @1)
8582                      (convert:utype @2)))))
8583      (if (FLOAT_TYPE_P (type)
8584           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8585                == DECIMAL_FLOAT_TYPE_P (type))
8586       (with { tree arg0 = strip_float_extensions (@1);
8587               tree arg1 = strip_float_extensions (@2);
8588               tree itype = TREE_TYPE (@0);
8589               tree ty1 = TREE_TYPE (arg0);
8590               tree ty2 = TREE_TYPE (arg1);
8591               enum tree_code code = TREE_CODE (itype); }
8592         (if (FLOAT_TYPE_P (ty1)
8593              && FLOAT_TYPE_P (ty2))
8594          (with { tree newtype = type;
8595                  if (TYPE_MODE (ty1) == SDmode
8596                      || TYPE_MODE (ty2) == SDmode
8597                      || TYPE_MODE (type) == SDmode)
8598                    newtype = dfloat32_type_node;
8599                  if (TYPE_MODE (ty1) == DDmode
8600                      || TYPE_MODE (ty2) == DDmode
8601                      || TYPE_MODE (type) == DDmode)
8602                    newtype = dfloat64_type_node;
8603                  if (TYPE_MODE (ty1) == TDmode
8604                      || TYPE_MODE (ty2) == TDmode
8605                      || TYPE_MODE (type) == TDmode)
8606                    newtype = dfloat128_type_node; }
8607           (if ((newtype == dfloat32_type_node
8608                 || newtype == dfloat64_type_node
8609                 || newtype == dfloat128_type_node)
8610               && newtype == type
8611               && types_match (newtype, type))
8612             (op (convert:newtype @1) (convert:newtype @2))
8613             (with { if (element_precision (ty1) > element_precision (newtype))
8614                       newtype = ty1;
8615                     if (element_precision (ty2) > element_precision (newtype))
8616                       newtype = ty2; }
8617                /* Sometimes this transformation is safe (cannot
8618                   change results through affecting double rounding
8619                   cases) and sometimes it is not.  If NEWTYPE is
8620                   wider than TYPE, e.g. (float)((long double)double
8621                   + (long double)double) converted to
8622                   (float)(double + double), the transformation is
8623                   unsafe regardless of the details of the types
8624                   involved; double rounding can arise if the result
8625                   of NEWTYPE arithmetic is a NEWTYPE value half way
8626                   between two representable TYPE values but the
8627                   exact value is sufficiently different (in the
8628                   right direction) for this difference to be
8629                   visible in ITYPE arithmetic.  If NEWTYPE is the
8630                   same as TYPE, however, the transformation may be
8631                   safe depending on the types involved: it is safe
8632                   if the ITYPE has strictly more than twice as many
8633                   mantissa bits as TYPE, can represent infinities
8634                   and NaNs if the TYPE can, and has sufficient
8635                   exponent range for the product or ratio of two
8636                   values representable in the TYPE to be within the
8637                   range of normal values of ITYPE.  */
8638               (if (element_precision (newtype) < element_precision (itype)
8639                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8640                        || target_supports_op_p (newtype, op, optab_default))
8641                    && (flag_unsafe_math_optimizations
8642                        || (element_precision (newtype) == element_precision (type)
8643                            && real_can_shorten_arithmetic (element_mode (itype),
8644                                                            element_mode (type))
8645                            && !excess_precision_type (newtype)))
8646                    && !types_match (itype, newtype))
8647                  (convert:type (op (convert:newtype @1)
8648                                    (convert:newtype @2)))
8649          )))) )
8650    ))
8653 /* This is another case of narrowing, specifically when there's an outer
8654    BIT_AND_EXPR which masks off bits outside the type of the innermost
8655    operands.   Like the previous case we have to convert the operands
8656    to unsigned types to avoid introducing undefined behavior for the
8657    arithmetic operation.  */
8658 (for op (minus plus)
8659  (simplify
8660   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8661   (if (INTEGRAL_TYPE_P (type)
8662        /* We check for type compatibility between @0 and @1 below,
8663           so there's no need to check that @1/@3 are integral types.  */
8664        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8665        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8666        /* The precision of the type of each operand must match the
8667           precision of the mode of each operand, similarly for the
8668           result.  */
8669        && type_has_mode_precision_p (TREE_TYPE (@0))
8670        && type_has_mode_precision_p (TREE_TYPE (@1))
8671        && type_has_mode_precision_p (type)
8672        /* The inner conversion must be a widening conversion.  */
8673        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8674        && types_match (@0, @1)
8675        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8676            <= TYPE_PRECISION (TREE_TYPE (@0)))
8677        && (wi::to_wide (@4)
8678            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8679                        true, TYPE_PRECISION (type))) == 0)
8680    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8681     (with { tree ntype = TREE_TYPE (@0); }
8682      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8683     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8684      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8685                (convert:utype @4))))))))
8687 /* Transform (@0 < @1 and @0 < @2) to use min,
8688    (@0 > @1 and @0 > @2) to use max */
8689 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8690      op    (lt      le      gt      ge      lt      le      gt      ge     )
8691      ext   (min     min     max     max     max     max     min     min    )
8692  (simplify
8693   (logic (op:cs @0 @1) (op:cs @0 @2))
8694   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8695        && TREE_CODE (@0) != INTEGER_CST)
8696    (op @0 (ext @1 @2)))))
8698 /* Max<bool0, bool1> -> bool0 | bool1
8699    Min<bool0, bool1> -> bool0 & bool1 */
8700 (for op    (max     min)
8701      logic (bit_ior bit_and)
8702  (simplify
8703   (op zero_one_valued_p@0 zero_one_valued_p@1)
8704   (logic @0 @1)))
8706 /* signbit(x) != 0 ? -x : x -> abs(x)
8707    signbit(x) == 0 ? -x : x -> -abs(x) */
8708 (for sign (SIGNBIT)
8709  (for neeq (ne eq)
8710   (simplify
8711    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8712     (if (neeq == NE_EXPR)
8713      (abs @0)
8714      (negate (abs @0))))))
8716 (simplify
8717  /* signbit(x) -> 0 if x is nonnegative.  */
8718  (SIGNBIT tree_expr_nonnegative_p@0)
8719  { integer_zero_node; })
8721 (simplify
8722  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8723  (SIGNBIT @0)
8724  (if (!HONOR_SIGNED_ZEROS (@0))
8725   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8727 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8728 (for cmp (eq ne)
8729  (for op (plus minus)
8730       rop (minus plus)
8731   (simplify
8732    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8733    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8734         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8735         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8736         && !TYPE_SATURATING (TREE_TYPE (@0)))
8737     (with { tree res = int_const_binop (rop, @2, @1); }
8738      (if (TREE_OVERFLOW (res)
8739           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8740       { constant_boolean_node (cmp == NE_EXPR, type); }
8741       (if (single_use (@3))
8742        (cmp @0 { TREE_OVERFLOW (res)
8743                  ? drop_tree_overflow (res) : res; }))))))))
8744 (for cmp (lt le gt ge)
8745  (for op (plus minus)
8746       rop (minus plus)
8747   (simplify
8748    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8749    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8750         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8751     (with { tree res = int_const_binop (rop, @2, @1); }
8752      (if (TREE_OVERFLOW (res))
8753       {
8754         fold_overflow_warning (("assuming signed overflow does not occur "
8755                                 "when simplifying conditional to constant"),
8756                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8757         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8758         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8759         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8760                                   TYPE_SIGN (TREE_TYPE (@1)))
8761                         != (op == MINUS_EXPR);
8762         constant_boolean_node (less == ovf_high, type);
8763       }
8764       (if (single_use (@3))
8765        (with
8766         {
8767           fold_overflow_warning (("assuming signed overflow does not occur "
8768                                   "when changing X +- C1 cmp C2 to "
8769                                   "X cmp C2 -+ C1"),
8770                                  WARN_STRICT_OVERFLOW_COMPARISON);
8771         }
8772         (cmp @0 { res; })))))))))
8774 /* Canonicalizations of BIT_FIELD_REFs.  */
8776 (simplify
8777  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8778  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8780 (simplify
8781  (BIT_FIELD_REF (view_convert @0) @1 @2)
8782  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8783       || type_has_mode_precision_p (TREE_TYPE (@0)))
8784   (BIT_FIELD_REF @0 @1 @2)))
8786 (simplify
8787  (BIT_FIELD_REF @0 @1 integer_zerop)
8788  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8789   (view_convert @0)))
8791 (simplify
8792  (BIT_FIELD_REF @0 @1 @2)
8793  (switch
8794   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8795        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8796    (switch
8797     (if (integer_zerop (@2))
8798      (view_convert (realpart @0)))
8799     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8800      (view_convert (imagpart @0)))))
8801   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8802        && INTEGRAL_TYPE_P (type)
8803        /* On GIMPLE this should only apply to register arguments.  */
8804        && (! GIMPLE || is_gimple_reg (@0))
8805        /* A bit-field-ref that referenced the full argument can be stripped.  */
8806        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8807             && integer_zerop (@2))
8808            /* Low-parts can be reduced to integral conversions.
8809               ???  The following doesn't work for PDP endian.  */
8810            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8811                /* But only do this after vectorization.  */
8812                && canonicalize_math_after_vectorization_p ()
8813                /* Don't even think about BITS_BIG_ENDIAN.  */
8814                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8815                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8816                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8817                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8818                                             - TYPE_PRECISION (type))
8819                                          : 0)) == 0)))
8820    (convert @0))))
8822 /* Simplify vector extracts.  */
8824 (simplify
8825  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8826  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8827       && tree_fits_uhwi_p (TYPE_SIZE (type))
8828       && ((tree_to_uhwi (TYPE_SIZE (type))
8829            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8830           || (VECTOR_TYPE_P (type)
8831               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8832                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8833   (with
8834    {
8835      tree ctor = (TREE_CODE (@0) == SSA_NAME
8836                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8837      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8838      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8839      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8840      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8841    }
8842    (if (n != 0
8843         && (idx % width) == 0
8844         && (n % width) == 0
8845         && known_le ((idx + n) / width,
8846                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8847     (with
8848      {
8849        idx = idx / width;
8850        n = n / width;
8851        /* Constructor elements can be subvectors.  */
8852        poly_uint64 k = 1;
8853        if (CONSTRUCTOR_NELTS (ctor) != 0)
8854          {
8855            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8856            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8857              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8858          }
8859        unsigned HOST_WIDE_INT elt, count, const_k;
8860      }
8861      (switch
8862       /* We keep an exact subset of the constructor elements.  */
8863       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8864        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8865         { build_zero_cst (type); }
8866         (if (count == 1)
8867          (if (elt < CONSTRUCTOR_NELTS (ctor))
8868           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8869           { build_zero_cst (type); })
8870          /* We don't want to emit new CTORs unless the old one goes away.
8871             ???  Eventually allow this if the CTOR ends up constant or
8872             uniform.  */
8873          (if (single_use (@0))
8874           (with
8875             {
8876               vec<constructor_elt, va_gc> *vals;
8877               vec_alloc (vals, count);
8878               bool constant_p = true;
8879               tree res;
8880               for (unsigned i = 0;
8881                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8882                 {
8883                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8884                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8885                   if (!CONSTANT_CLASS_P (e))
8886                     constant_p = false;
8887                 }
8888               tree evtype = (types_match (TREE_TYPE (type),
8889                                           TREE_TYPE (TREE_TYPE (ctor)))
8890                              ? type
8891                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8892                                                   count * k));
8893               /* We used to build a CTOR in the non-constant case here
8894                  but that's not a GIMPLE value.  We'd have to expose this
8895                  operation somehow so the code generation can properly
8896                  split it out to a separate stmt.  */
8897               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8898                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8899             }
8900             (if (res)
8901              (view_convert { res; })))))))
8902       /* The bitfield references a single constructor element.  */
8903       (if (k.is_constant (&const_k)
8904            && idx + n <= (idx / const_k + 1) * const_k)
8905        (switch
8906         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8907          { build_zero_cst (type); })
8908         (if (n == const_k)
8909          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8910         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8911                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8913 /* Simplify a bit extraction from a bit insertion for the cases with
8914    the inserted element fully covering the extraction or the insertion
8915    not touching the extraction.  */
8916 (simplify
8917  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8918  (with
8919   {
8920     unsigned HOST_WIDE_INT isize;
8921     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8922       isize = TYPE_PRECISION (TREE_TYPE (@1));
8923     else
8924       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8925   }
8926   (switch
8927    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8928          || type_has_mode_precision_p (TREE_TYPE (@1)))
8929         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8930         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8931                       wi::to_wide (@ipos) + isize))
8932     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8933                                                  wi::to_wide (@rpos)
8934                                                  - wi::to_wide (@ipos)); }))
8935    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8936         && compare_tree_int (@rsize, isize) == 0)
8937     (convert @1))
8938    (if (wi::geu_p (wi::to_wide (@ipos),
8939                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8940         || wi::geu_p (wi::to_wide (@rpos),
8941                       wi::to_wide (@ipos) + isize))
8942     (BIT_FIELD_REF @0 @rsize @rpos)))))
8944 /* Simplify vector inserts of other vector extracts to a permute.  */
8945 (simplify
8946  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8947  (if (VECTOR_TYPE_P (type)
8948       && (VECTOR_MODE_P (TYPE_MODE (type))
8949           || optimize_vectors_before_lowering_p ())
8950       && types_match (@0, @1)
8951       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8952       && TYPE_VECTOR_SUBPARTS (type).is_constant ()
8953       && multiple_p (wi::to_poly_offset (@rpos),
8954                      wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
8955   (with
8956    {
8957      unsigned HOST_WIDE_INT elsz
8958        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8959      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8960      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8961      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8962      vec_perm_builder builder;
8963      builder.new_vector (nunits, nunits, 1);
8964      for (unsigned i = 0; i < nunits; ++i)
8965        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8966      vec_perm_indices sel (builder, 2, nunits);
8967    }
8968    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8969         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8970     (vec_perm @0 @1 { vec_perm_indices_to_tree
8971                         (build_vector_type (ssizetype, nunits), sel); })))))
8973 (if (canonicalize_math_after_vectorization_p ())
8974  (for fmas (FMA)
8975   (simplify
8976    (fmas:c (negate @0) @1 @2)
8977    (IFN_FNMA @0 @1 @2))
8978   (simplify
8979    (fmas @0 @1 (negate @2))
8980    (IFN_FMS @0 @1 @2))
8981   (simplify
8982    (fmas:c (negate @0) @1 (negate @2))
8983    (IFN_FNMS @0 @1 @2))
8984   (simplify
8985    (negate (fmas@3 @0 @1 @2))
8986    (if (single_use (@3))
8987     (IFN_FNMS @0 @1 @2))))
8989  (simplify
8990   (IFN_FMS:c (negate @0) @1 @2)
8991   (IFN_FNMS @0 @1 @2))
8992  (simplify
8993   (IFN_FMS @0 @1 (negate @2))
8994   (IFN_FMA @0 @1 @2))
8995  (simplify
8996   (IFN_FMS:c (negate @0) @1 (negate @2))
8997   (IFN_FNMA @0 @1 @2))
8998  (simplify
8999   (negate (IFN_FMS@3 @0 @1 @2))
9000    (if (single_use (@3))
9001     (IFN_FNMA @0 @1 @2)))
9003  (simplify
9004   (IFN_FNMA:c (negate @0) @1 @2)
9005   (IFN_FMA @0 @1 @2))
9006  (simplify
9007   (IFN_FNMA @0 @1 (negate @2))
9008   (IFN_FNMS @0 @1 @2))
9009  (simplify
9010   (IFN_FNMA:c (negate @0) @1 (negate @2))
9011   (IFN_FMS @0 @1 @2))
9012  (simplify
9013   (negate (IFN_FNMA@3 @0 @1 @2))
9014   (if (single_use (@3))
9015    (IFN_FMS @0 @1 @2)))
9017  (simplify
9018   (IFN_FNMS:c (negate @0) @1 @2)
9019   (IFN_FMS @0 @1 @2))
9020  (simplify
9021   (IFN_FNMS @0 @1 (negate @2))
9022   (IFN_FNMA @0 @1 @2))
9023  (simplify
9024   (IFN_FNMS:c (negate @0) @1 (negate @2))
9025   (IFN_FMA @0 @1 @2))
9026  (simplify
9027   (negate (IFN_FNMS@3 @0 @1 @2))
9028   (if (single_use (@3))
9029    (IFN_FMA @0 @1 @2))))
9031 /* CLZ simplifications.  */
9032 (for clz (CLZ)
9033  (for op (eq ne)
9034       cmp (lt ge)
9035   (simplify
9036    (op (clz:s@2 @0) INTEGER_CST@1)
9037    (if (integer_zerop (@1) && single_use (@2))
9038     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
9039     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
9040      (cmp (convert:stype @0) { build_zero_cst (stype); }))
9041     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
9042     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
9043      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
9044 (for op (eq ne)
9045      cmp (lt ge)
9046  (simplify
9047   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
9048   (if (integer_zerop (@1) && single_use (@2))
9049    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
9050    (with { tree type0 = TREE_TYPE (@0);
9051            tree stype = signed_type_for (TREE_TYPE (@0));
9052            /* Punt if clz(0) == 0.  */
9053            if (integer_zerop (@3))
9054              stype = NULL_TREE;
9055          }
9056     (if (stype)
9057      (cmp (convert:stype @0) { build_zero_cst (stype); })))
9058    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
9059    (with { bool ok = true;
9060            tree type0 = TREE_TYPE (@0);
9061            /* Punt if clz(0) == prec - 1.  */
9062            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
9063              ok = false;
9064          }
9065     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
9066      (op @0 { build_one_cst (type0); }))))))
9068 /* CTZ simplifications.  */
9069 (for ctz (CTZ)
9070  (for op (ge gt le lt)
9071       cmp (eq eq ne ne)
9072   (simplify
9073    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
9074    (op (ctz:s @0) INTEGER_CST@1)
9075     (with { bool ok = true;
9076             HOST_WIDE_INT val = 0;
9077             if (!tree_fits_shwi_p (@1))
9078               ok = false;
9079             else
9080               {
9081                 val = tree_to_shwi (@1);
9082                 /* Canonicalize to >= or <.  */
9083                 if (op == GT_EXPR || op == LE_EXPR)
9084                   {
9085                     if (val == HOST_WIDE_INT_MAX)
9086                       ok = false;
9087                     else
9088                       val++;
9089                   }
9090               }
9091             tree type0 = TREE_TYPE (@0);
9092             int prec = TYPE_PRECISION (type0);
9093           }
9094      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
9095       (if (val <= 0)
9096        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
9097        (if (val >= prec)
9098         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
9099         (cmp (bit_and @0 { wide_int_to_tree (type0,
9100                                              wi::mask (val, false, prec)); })
9101              { build_zero_cst (type0); })))))))
9102  (for op (eq ne)
9103   (simplify
9104    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
9105    (op (ctz:s @0) INTEGER_CST@1)
9106     (with { tree type0 = TREE_TYPE (@0);
9107             int prec = TYPE_PRECISION (type0);
9108           }
9109      (if (prec <= MAX_FIXED_MODE_SIZE)
9110       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
9111        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
9112        (op (bit_and @0 { wide_int_to_tree (type0,
9113                                            wi::mask (tree_to_uhwi (@1) + 1,
9114                                                      false, prec)); })
9115            { wide_int_to_tree (type0,
9116                                wi::shifted_mask (tree_to_uhwi (@1), 1,
9117                                                  false, prec)); })))))))
9118 (for op (ge gt le lt)
9119      cmp (eq eq ne ne)
9120  (simplify
9121   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
9122   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
9123    (with { bool ok = true;
9124            HOST_WIDE_INT val = 0;
9125            if (!tree_fits_shwi_p (@1))
9126              ok = false;
9127            else
9128              {
9129                val = tree_to_shwi (@1);
9130                /* Canonicalize to >= or <.  */
9131                if (op == GT_EXPR || op == LE_EXPR)
9132                  {
9133                    if (val == HOST_WIDE_INT_MAX)
9134                      ok = false;
9135                    else
9136                      val++;
9137                  }
9138              }
9139            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
9140            tree type0 = TREE_TYPE (@0);
9141            int prec = TYPE_PRECISION (type0);
9142            if (prec > MAX_FIXED_MODE_SIZE)
9143              ok = false;
9144           }
9145      (if (val <= 0)
9146       (if (ok && zero_val >= val)
9147        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
9148       (if (val >= prec)
9149        (if (ok && zero_val < val)
9150         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
9151        (if (ok && (zero_val < 0 || zero_val >= prec))
9152         (cmp (bit_and @0 { wide_int_to_tree (type0,
9153                                              wi::mask (val, false, prec)); })
9154              { build_zero_cst (type0); })))))))
9155 (for op (eq ne)
9156  (simplify
9157   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
9158   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
9159    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
9160            tree type0 = TREE_TYPE (@0);
9161            int prec = TYPE_PRECISION (type0);
9162          }
9163     (if (prec <= MAX_FIXED_MODE_SIZE)
9164      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
9165       (if (zero_val != wi::to_widest (@1))
9166        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
9167       (if (zero_val < 0 || zero_val >= prec)
9168        (op (bit_and @0 { wide_int_to_tree (type0,
9169                                            wi::mask (tree_to_uhwi (@1) + 1,
9170                                                      false, prec)); })
9171            { wide_int_to_tree (type0,
9172                                wi::shifted_mask (tree_to_uhwi (@1), 1,
9173                                                  false, prec)); })))))))
9175 #if GIMPLE
9176 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
9177 (simplify
9178   (CTZ (convert@1 @0))
9179   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9180        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9181        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9182    (with { combined_fn cfn = CFN_LAST;
9183            tree type0 = TREE_TYPE (@0);
9184            if (TREE_CODE (type0) == BITINT_TYPE)
9185              {
9186                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9187                  cfn = CFN_CTZ;
9188                else
9189                  type0
9190                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9191                                                      1);
9192              }
9193            type0 = unsigned_type_for (type0);
9194            if (cfn == CFN_LAST
9195                && direct_internal_fn_supported_p (IFN_CTZ, type0,
9196                                                   OPTIMIZE_FOR_BOTH))
9197              cfn = CFN_CTZ;
9198            if (cfn == CFN_LAST
9199                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9200                && !direct_internal_fn_supported_p (IFN_CTZ,
9201                                                    TREE_TYPE (@1),
9202                                                    OPTIMIZE_FOR_BOTH))
9203              {
9204                if (TYPE_PRECISION (type0)
9205                    == TYPE_PRECISION (unsigned_type_node))
9206                  cfn = CFN_BUILT_IN_CTZ;
9207                else if (TYPE_PRECISION (type0)
9208                         == TYPE_PRECISION (long_long_unsigned_type_node))
9209                  cfn = CFN_BUILT_IN_CTZLL;
9210              } }
9211     (if (cfn == CFN_CTZ)
9212      (IFN_CTZ (convert:type0 @0))
9213      (if (cfn == CFN_BUILT_IN_CTZ)
9214       (BUILT_IN_CTZ (convert:type0 @0))
9215       (if (cfn == CFN_BUILT_IN_CTZLL)
9216        (BUILT_IN_CTZLL (convert:type0 @0))))))))
9217 #endif
9219 /* POPCOUNT simplifications.  */
9220 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
9221 (simplify
9222   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
9223   (if (INTEGRAL_TYPE_P (type)
9224        && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
9225                         widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
9226            == 0))
9227    (with { tree utype = TREE_TYPE (@0);
9228            if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9229              utype = TREE_TYPE (@1); }
9230     (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
9232 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
9233 (for popcount (POPCOUNT)
9234   (for cmp (le eq ne gt)
9235        rep (eq eq ne ne)
9236     (simplify
9237       (cmp (popcount @0) integer_zerop)
9238       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
9240 /* popcount(bswap(x)) is popcount(x).  */
9241 (for popcount (POPCOUNT)
9242   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
9243               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
9244     (simplify
9245       (popcount (convert?@0 (bswap:s@1 @2)))
9246       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9247            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9248         (with { tree type0 = TREE_TYPE (@0);
9249                 tree type1 = TREE_TYPE (@1);
9250                 unsigned int prec0 = TYPE_PRECISION (type0);
9251                 unsigned int prec1 = TYPE_PRECISION (type1); }
9252           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
9253             (popcount (convert:type0 (convert:type1 @2)))))))))
9255 /* popcount(rotate(X Y)) is popcount(X).  */
9256 (for popcount (POPCOUNT)
9257   (for rot (lrotate rrotate)
9258     (simplify
9259       (popcount (convert?@0 (rot:s@1 @2 @3)))
9260       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9261            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
9262            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
9263         (with { tree type0 = TREE_TYPE (@0);
9264                 tree type1 = TREE_TYPE (@1);
9265                 unsigned int prec0 = TYPE_PRECISION (type0);
9266                 unsigned int prec1 = TYPE_PRECISION (type1); }
9267           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
9268             (popcount (convert:type0 @2))))))))
9270 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
9271 (simplify
9272   (bit_and (POPCOUNT @0) integer_onep)
9273   (PARITY @0))
9275 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
9276 (simplify
9277   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
9278   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
9280 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
9281 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
9282 (for popcount (POPCOUNT)
9283   (for log1 (bit_and bit_ior)
9284        log2 (bit_ior bit_and)
9285     (simplify
9286       (minus (plus:s (popcount:s @0) (popcount:s @1))
9287              (popcount:s (log1:cs @0 @1)))
9288       (popcount (log2 @0 @1)))
9289     (simplify
9290       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
9291               (popcount:s @1))
9292       (popcount (log2 @0 @1)))))
9294 #if GIMPLE
9295 /* popcount(zext(X)) == popcount(X).  */
9296 (simplify
9297   (POPCOUNT (convert@1 @0))
9298   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9299        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9300        && TYPE_UNSIGNED (TREE_TYPE (@0))
9301        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9302    (with { combined_fn cfn = CFN_LAST;
9303            tree type0 = TREE_TYPE (@0);
9304            if (TREE_CODE (type0) == BITINT_TYPE)
9305              {
9306                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9307                  cfn = CFN_POPCOUNT;
9308                else
9309                  type0
9310                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9311                                                      1);
9312              }
9313            if (cfn == CFN_LAST
9314                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
9315                                                   OPTIMIZE_FOR_BOTH))
9316              cfn = CFN_POPCOUNT;
9317            if (cfn == CFN_LAST
9318                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9319                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
9320                                                    TREE_TYPE (@1),
9321                                                    OPTIMIZE_FOR_BOTH))
9322              {
9323                if (TYPE_PRECISION (type0)
9324                    == TYPE_PRECISION (unsigned_type_node))
9325                  cfn = CFN_BUILT_IN_POPCOUNT;
9326                else if (TYPE_PRECISION (type0)
9327                         == TYPE_PRECISION (long_long_unsigned_type_node))
9328                  cfn = CFN_BUILT_IN_POPCOUNTLL;
9329              } }
9330     (if (cfn == CFN_POPCOUNT)
9331      (IFN_POPCOUNT (convert:type0 @0))
9332      (if (cfn == CFN_BUILT_IN_POPCOUNT)
9333       (BUILT_IN_POPCOUNT (convert:type0 @0))
9334       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
9335        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
9336 #endif
9338 /* PARITY simplifications.  */
9339 /* parity(~X) is parity(X).  */
9340 (simplify
9341   (PARITY (bit_not @0))
9342   (PARITY @0))
9344 /* parity(bswap(x)) is parity(x).  */
9345 (for parity (PARITY)
9346   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
9347               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
9348     (simplify
9349       (parity (convert?@0 (bswap:s@1 @2)))
9350       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9351            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9352            && TYPE_PRECISION (TREE_TYPE (@0))
9353               >= TYPE_PRECISION (TREE_TYPE (@1)))
9354         (with { tree type0 = TREE_TYPE (@0);
9355                 tree type1 = TREE_TYPE (@1); }
9356           (parity (convert:type0 (convert:type1 @2))))))))
9358 /* parity(rotate(X Y)) is parity(X).  */
9359 (for parity (PARITY)
9360   (for rot (lrotate rrotate)
9361     (simplify
9362       (parity (convert?@0 (rot:s@1 @2 @3)))
9363       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9364            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9365            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
9366            && TYPE_PRECISION (TREE_TYPE (@0))
9367               >= TYPE_PRECISION (TREE_TYPE (@1)))
9368         (with { tree type0 = TREE_TYPE (@0); }
9369           (parity (convert:type0 @2)))))))
9371 /* parity(X)^parity(Y) is parity(X^Y).  */
9372 (simplify
9373   (bit_xor (PARITY:s @0) (PARITY:s @1))
9374   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
9375    (PARITY (bit_xor @0 @1))
9376    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9377         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9378     (with { tree utype = TREE_TYPE (@0);
9379             if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9380               utype = TREE_TYPE (@1); }
9381      (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
9383 #if GIMPLE
9384 /* parity(zext(X)) == parity(X).  */
9385 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
9386 (simplify
9387   (PARITY (convert@1 @0))
9388   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9389        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9390        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
9391        && (TYPE_UNSIGNED (TREE_TYPE (@0))
9392            || ((TYPE_PRECISION (TREE_TYPE (@1))
9393                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
9394    (with { combined_fn cfn = CFN_LAST;
9395            tree type0 = TREE_TYPE (@0);
9396            if (TREE_CODE (type0) == BITINT_TYPE)
9397              {
9398                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9399                  cfn = CFN_PARITY;
9400                else
9401                  type0
9402                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9403                                                      1);
9404              }
9405            type0 = unsigned_type_for (type0);
9406            if (cfn == CFN_LAST
9407                && direct_internal_fn_supported_p (IFN_PARITY, type0,
9408                                                   OPTIMIZE_FOR_BOTH))
9409              cfn = CFN_PARITY;
9410            if (cfn == CFN_LAST
9411                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9412                && !direct_internal_fn_supported_p (IFN_PARITY,
9413                                                    TREE_TYPE (@1),
9414                                                    OPTIMIZE_FOR_BOTH))
9415              {
9416                if (TYPE_PRECISION (type0)
9417                    == TYPE_PRECISION (unsigned_type_node))
9418                  cfn = CFN_BUILT_IN_PARITY;
9419                else if (TYPE_PRECISION (type0)
9420                         == TYPE_PRECISION (long_long_unsigned_type_node))
9421                  cfn = CFN_BUILT_IN_PARITYLL;
9422              } }
9423     (if (cfn == CFN_PARITY)
9424      (IFN_PARITY (convert:type0 @0))
9425      (if (cfn == CFN_BUILT_IN_PARITY)
9426       (BUILT_IN_PARITY (convert:type0 @0))
9427       (if (cfn == CFN_BUILT_IN_PARITYLL)
9428        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9429 #endif
9431 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9432 (for func (POPCOUNT BSWAP FFS PARITY)
9433  (simplify
9434   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9435   @3))
9437 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9438    where CST is precision-1. */
9439 (for func (CLRSB)
9440  (simplify
9441   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9442   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9443    @4)))
9445 #if GIMPLE
9446 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9447 (for func (CLZ)
9448  (simplify
9449   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9450   (with { int val;
9451           internal_fn ifn = IFN_LAST;
9452           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9453             {
9454               if (tree_fits_shwi_p (@2))
9455                 {
9456                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9457                   if ((int) valw == valw)
9458                     {
9459                       val = valw;
9460                       ifn = IFN_CLZ;
9461                     }
9462                 }
9463             }
9464           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9465                                                    OPTIMIZE_FOR_BOTH)
9466                    && CLZ_DEFINED_VALUE_AT_ZERO
9467                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9468             ifn = IFN_CLZ;
9469         }
9470    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9471     (IFN_CLZ @3 @2)))))
9472 (simplify
9473  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9474   (with { int val;
9475           internal_fn ifn = IFN_LAST;
9476           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9477             ifn = IFN_CLZ;
9478           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9479                                                    OPTIMIZE_FOR_BOTH))
9480             ifn = IFN_CLZ;
9481         }
9482    (if (ifn == IFN_CLZ)
9483     (IFN_CLZ @3 @2))))
9485 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9486 (for func (CTZ)
9487  (simplify
9488   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9489   (with { int val;
9490           internal_fn ifn = IFN_LAST;
9491           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9492             {
9493               if (tree_fits_shwi_p (@2))
9494                 {
9495                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9496                   if ((int) valw == valw)
9497                     {
9498                       val = valw;
9499                       ifn = IFN_CTZ;
9500                     }
9501                 }
9502             }
9503           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9504                                                    OPTIMIZE_FOR_BOTH)
9505                    && CTZ_DEFINED_VALUE_AT_ZERO
9506                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9507             ifn = IFN_CTZ;
9508         }
9509    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9510     (IFN_CTZ @3 @2)))))
9511 (simplify
9512  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9513   (with { int val;
9514           internal_fn ifn = IFN_LAST;
9515           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9516             ifn = IFN_CTZ;
9517           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9518                                                    OPTIMIZE_FOR_BOTH))
9519             ifn = IFN_CTZ;
9520         }
9521    (if (ifn == IFN_CTZ)
9522     (IFN_CTZ @3 @2))))
9523 #endif
9525 /* Common POPCOUNT/PARITY simplifications.  */
9526 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9527 (for pfun (POPCOUNT PARITY)
9528   (simplify
9529     (pfun @0)
9530     (if (INTEGRAL_TYPE_P (type))
9531      (with { wide_int nz = tree_nonzero_bits (@0); }
9532        (switch
9533          (if (nz == 1)
9534            (convert @0))
9535          (if (wi::popcount (nz) == 1)
9536            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9537              (convert (rshift:utype (convert:utype @0)
9538                                     { build_int_cst (integer_type_node,
9539                                                      wi::ctz (nz)); })))))))))
9541 #if GIMPLE
9542 /* 64- and 32-bits branchless implementations of popcount are detected:
9544    int popcount64c (uint64_t x)
9545    {
9546      x -= (x >> 1) & 0x5555555555555555ULL;
9547      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9548      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9549      return (x * 0x0101010101010101ULL) >> 56;
9550    }
9552    int popcount32c (uint32_t x)
9553    {
9554      x -= (x >> 1) & 0x55555555;
9555      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9556      x = (x + (x >> 4)) & 0x0f0f0f0f;
9557      return (x * 0x01010101) >> 24;
9558    }  */
9559 (simplify
9560  (rshift
9561   (mult
9562    (bit_and
9563     (plus:c
9564      (rshift @8 INTEGER_CST@5)
9565       (plus:c@8
9566        (bit_and @6 INTEGER_CST@7)
9567         (bit_and
9568          (rshift
9569           (minus@6 @0
9570            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9571           INTEGER_CST@10)
9572          INTEGER_CST@9)))
9573     INTEGER_CST@3)
9574    INTEGER_CST@2)
9575   INTEGER_CST@1)
9576   /* Check constants and optab.  */
9577   (with { unsigned prec = TYPE_PRECISION (type);
9578           int shift = (64 - prec) & 63;
9579           unsigned HOST_WIDE_INT c1
9580             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9581           unsigned HOST_WIDE_INT c2
9582             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9583           unsigned HOST_WIDE_INT c3
9584             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9585           unsigned HOST_WIDE_INT c4
9586             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9587    }
9588    (if (prec >= 16
9589         && prec <= 64
9590         && pow2p_hwi (prec)
9591         && TYPE_UNSIGNED (type)
9592         && integer_onep (@4)
9593         && wi::to_widest (@10) == 2
9594         && wi::to_widest (@5) == 4
9595         && wi::to_widest (@1) == prec - 8
9596         && tree_to_uhwi (@2) == c1
9597         && tree_to_uhwi (@3) == c2
9598         && tree_to_uhwi (@9) == c3
9599         && tree_to_uhwi (@7) == c3
9600         && tree_to_uhwi (@11) == c4)
9601     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9602                                          OPTIMIZE_FOR_BOTH))
9603      (convert (IFN_POPCOUNT:type @0))
9604      /* Try to do popcount in two halves.  PREC must be at least
9605         five bits for this to work without extension before adding.  */
9606      (with {
9607        tree half_type = NULL_TREE;
9608        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9609        int half_prec = 8;
9610        if (m.exists ()
9611            && m.require () != TYPE_MODE (type))
9612          {
9613            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9614            half_type = build_nonstandard_integer_type (half_prec, 1);
9615          }
9616        gcc_assert (half_prec > 2);
9617       }
9618       (if (half_type != NULL_TREE
9619            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9620                                               OPTIMIZE_FOR_BOTH))
9621        (convert (plus
9622          (IFN_POPCOUNT:half_type (convert @0))
9623          (IFN_POPCOUNT:half_type (convert (rshift @0
9624             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9626 /* __builtin_ffs needs to deal on many targets with the possible zero
9627    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9628    should lead to better code.  */
9629 (simplify
9630  (FFS tree_expr_nonzero_p@0)
9631  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9632       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9633                                          OPTIMIZE_FOR_SPEED))
9634   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9635    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9636 #endif
9638 (for ffs (FFS)
9639  /* __builtin_ffs (X) == 0 -> X == 0.
9640     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9641  (for cmp (eq ne)
9642   (simplify
9643    (cmp (ffs@2 @0) INTEGER_CST@1)
9644     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9645      (switch
9646       (if (integer_zerop (@1))
9647        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9648       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9649        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9650       (if (single_use (@2))
9651        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9652                                             wi::mask (tree_to_uhwi (@1),
9653                                                       false, prec)); })
9654             { wide_int_to_tree (TREE_TYPE (@0),
9655                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9656                                                   false, prec)); }))))))
9658  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9659  (for cmp (gt le)
9660       cmp2 (ne eq)
9661       cmp3 (eq ne)
9662       bit_op (bit_and bit_ior)
9663   (simplify
9664    (cmp (ffs@2 @0) INTEGER_CST@1)
9665     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9666      (switch
9667       (if (integer_zerop (@1))
9668        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9669       (if (tree_int_cst_sgn (@1) < 0)
9670        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9671       (if (wi::to_widest (@1) >= prec)
9672        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9673       (if (wi::to_widest (@1) == prec - 1)
9674        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9675                                     wi::shifted_mask (prec - 1, 1,
9676                                                       false, prec)); }))
9677       (if (single_use (@2))
9678        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9679                (cmp3 (bit_and @0
9680                               { wide_int_to_tree (TREE_TYPE (@0),
9681                                                   wi::mask (tree_to_uhwi (@1),
9682                                                   false, prec)); })
9683                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9685 #if GIMPLE
9686 /* ffs(ext(X)) == ffs(X).  */
9687 (simplify
9688   (FFS (convert@1 @0))
9689   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9690        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9691        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9692    (with { combined_fn cfn = CFN_LAST;
9693            tree type0 = TREE_TYPE (@0);
9694            if (TREE_CODE (type0) == BITINT_TYPE)
9695              {
9696                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9697                  cfn = CFN_FFS;
9698                else
9699                  type0
9700                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9701                                                      0);
9702              }
9703            type0 = signed_type_for (type0);
9704            if (cfn == CFN_LAST
9705                && direct_internal_fn_supported_p (IFN_FFS, type0,
9706                                                   OPTIMIZE_FOR_BOTH))
9707              cfn = CFN_FFS;
9708            if (cfn == CFN_LAST
9709                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9710                && !direct_internal_fn_supported_p (IFN_FFS,
9711                                                    TREE_TYPE (@1),
9712                                                    OPTIMIZE_FOR_BOTH))
9713              {
9714                if (TYPE_PRECISION (type0)
9715                    == TYPE_PRECISION (integer_type_node))
9716                  cfn = CFN_BUILT_IN_FFS;
9717                else if (TYPE_PRECISION (type0)
9718                         == TYPE_PRECISION (long_long_integer_type_node))
9719                  cfn = CFN_BUILT_IN_FFSLL;
9720              } }
9721     (if (cfn == CFN_FFS)
9722      (IFN_FFS (convert:type0 @0))
9723      (if (cfn == CFN_BUILT_IN_FFS)
9724       (BUILT_IN_FFS (convert:type0 @0))
9725       (if (cfn == CFN_BUILT_IN_FFSLL)
9726        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9727 #endif
9729 #if GIMPLE
9731 /* Simplify:
9732      a = op a1
9733      r = cond ? a : b
9734      --> r = .COND_FN (cond, a, b)
9735 and,
9736     a = op a1
9737     r = cond ? b : a
9738     --> r = .COND_FN (~cond, b, a).  */
9740 (for uncond_op (UNCOND_UNARY)
9741      cond_op (COND_UNARY)
9742  (simplify
9743   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9744    (with { tree op_type = TREE_TYPE (@3); }
9745     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9746         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9747      (cond_op @0 (view_convert @1) @2))))
9748  (simplify
9749   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9750    (with { tree op_type = TREE_TYPE (@3); }
9751     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9752         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9753      (cond_op (bit_not @0) (view_convert @2) @1)))))
9755 (for uncond_op (UNCOND_UNARY)
9756      cond_op (COND_LEN_UNARY)
9757  (simplify
9758   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9759    (with { tree op_type = TREE_TYPE (@3); }
9760     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9761         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9762      (cond_op @0 (view_convert @1) @2 @4 @5))))
9763  (simplify
9764   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9765    (with { tree op_type = TREE_TYPE (@3); }
9766     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9767         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9768      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9770 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9771 (simplify
9772  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9773  (if (canonicalize_math_after_vectorization_p ()
9774       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9775       && is_truth_type_for (type, TREE_TYPE (@0)))
9776  (if (integer_all_onesp (@1) && integer_zerop (@2))
9777   (IFN_COND_NOT @0 @3 @3))
9778   (if (integer_all_onesp (@2) && integer_zerop (@1))
9779    (IFN_COND_NOT (bit_not @0) @3 @3))))
9781 /* Simplify:
9783      a = a1 op a2
9784      r = c ? a : b;
9786    to:
9788      r = c ? a1 op a2 : b;
9790    if the target can do it in one go.  This makes the operation conditional
9791    on c, so could drop potentially-trapping arithmetic, but that's a valid
9792    simplification if the result of the operation isn't needed.
9794    Avoid speculatively generating a stand-alone vector comparison
9795    on targets that might not support them.  Any target implementing
9796    conditional internal functions must support the same comparisons
9797    inside and outside a VEC_COND_EXPR.  */
9799 (for uncond_op (UNCOND_BINARY)
9800      cond_op (COND_BINARY)
9801  (simplify
9802   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9803   (with { tree op_type = TREE_TYPE (@4); }
9804    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9805         && is_truth_type_for (op_type, TREE_TYPE (@0))
9806         && single_use (@4))
9807     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9808  (simplify
9809   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9810   (with { tree op_type = TREE_TYPE (@4); }
9811    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9812         && is_truth_type_for (op_type, TREE_TYPE (@0))
9813         && single_use (@4))
9814     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9816 (for uncond_op (UNCOND_BINARY)
9817      cond_op (COND_LEN_BINARY)
9818  (simplify
9819   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9820   (with { tree op_type = TREE_TYPE (@4); }
9821    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9822         && is_truth_type_for (op_type, TREE_TYPE (@0))
9823         && single_use (@4))
9824     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9825  (simplify
9826   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9827   (with { tree op_type = TREE_TYPE (@4); }
9828    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9829         && is_truth_type_for (op_type, TREE_TYPE (@0))
9830         && single_use (@4))
9831     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9833 /* Same for ternary operations.  */
9834 (for uncond_op (UNCOND_TERNARY)
9835      cond_op (COND_TERNARY)
9836  (simplify
9837   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9838   (with { tree op_type = TREE_TYPE (@5); }
9839    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9840         && is_truth_type_for (op_type, TREE_TYPE (@0))
9841         && single_use (@5))
9842     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9843  (simplify
9844   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9845   (with { tree op_type = TREE_TYPE (@5); }
9846    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9847         && is_truth_type_for (op_type, TREE_TYPE (@0))
9848         && single_use (@5))
9849     (view_convert (cond_op (bit_not @0) @2 @3 @4
9850                   (view_convert:op_type @1)))))))
9852 (for uncond_op (UNCOND_TERNARY)
9853      cond_op (COND_LEN_TERNARY)
9854  (simplify
9855   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9856   (with { tree op_type = TREE_TYPE (@5); }
9857    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9858         && is_truth_type_for (op_type, TREE_TYPE (@0))
9859         && single_use (@5))
9860     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9861  (simplify
9862   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9863   (with { tree op_type = TREE_TYPE (@5); }
9864    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9865         && is_truth_type_for (op_type, TREE_TYPE (@0))
9866         && single_use (@5))
9867     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9868 #endif
9870 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9871    "else" value of an IFN_COND_*.  */
9872 (for cond_op (COND_BINARY)
9873  (simplify
9874   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9875   (with { tree op_type = TREE_TYPE (@3); }
9876    (if (element_precision (type) == element_precision (op_type))
9877     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9878  (simplify
9879   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9880   (with { tree op_type = TREE_TYPE (@5); }
9881    (if (inverse_conditions_p (@0, @2)
9882         && element_precision (type) == element_precision (op_type))
9883     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9885 /* Same for ternary operations.  */
9886 (for cond_op (COND_TERNARY)
9887  (simplify
9888   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9889   (with { tree op_type = TREE_TYPE (@4); }
9890    (if (element_precision (type) == element_precision (op_type))
9891     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9892  (simplify
9893   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9894   (with { tree op_type = TREE_TYPE (@6); }
9895    (if (inverse_conditions_p (@0, @2)
9896         && element_precision (type) == element_precision (op_type))
9897     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9899 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9900    "else" value of an IFN_COND_LEN_*.  */
9901 (for cond_len_op (COND_LEN_BINARY)
9902  (simplify
9903   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9904   (with { tree op_type = TREE_TYPE (@3); }
9905    (if (element_precision (type) == element_precision (op_type))
9906     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9907  (simplify
9908   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9909   (with { tree op_type = TREE_TYPE (@5); }
9910    (if (inverse_conditions_p (@0, @2)
9911         && element_precision (type) == element_precision (op_type))
9912     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9914 /* Same for ternary operations.  */
9915 (for cond_len_op (COND_LEN_TERNARY)
9916  (simplify
9917   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9918   (with { tree op_type = TREE_TYPE (@4); }
9919    (if (element_precision (type) == element_precision (op_type))
9920     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9921  (simplify
9922   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9923   (with { tree op_type = TREE_TYPE (@6); }
9924    (if (inverse_conditions_p (@0, @2)
9925         && element_precision (type) == element_precision (op_type))
9926     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9928 /* Detect simplication for a conditional reduction where
9930    a = mask1 ? b : 0
9931    c = mask2 ? d + a : d
9933    is turned into
9935    c = mask1 && mask2 ? d + b : d.  */
9936 (simplify
9937   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9938    (if (ANY_INTEGRAL_TYPE_P (type)
9939         || (FLOAT_TYPE_P (type)
9940             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9941    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9943 /* Detect simplication for a conditional length reduction where
9945    a = mask ? b : 0
9946    c = i < len + bias ? d + a : d
9948    is turned into
9950    c = mask && i < len + bias ? d + b : d.  */
9951 (simplify
9952   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9953    (if (ANY_INTEGRAL_TYPE_P (type)
9954         || (FLOAT_TYPE_P (type)
9955             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9956     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9958 /* Detect simplification for vector condition folding where
9960   c = mask1 ? (masked_op mask2 a b els) : els
9962   into
9964   c = masked_op (mask1 & mask2) a b els
9966   where the operation can be partially applied to one operand. */
9968 (for cond_op (COND_BINARY)
9969  (simplify
9970   (vec_cond @0
9971    (cond_op:s @1 @2 @3 @4) @4)
9972   (cond_op (bit_and @1 @0) @2 @3 @4)))
9974 /* And same for ternary expressions.  */
9976 (for cond_op (COND_TERNARY)
9977  (simplify
9978   (vec_cond @0
9979    (cond_op:s @1 @2 @3 @4 @5) @5)
9980   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9982 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9983    expressions like:
9985    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9986    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9988    If pointers are known not to wrap, B checks whether @1 bytes starting
9989    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9990    bytes.  A is more efficiently tested as:
9992    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9994    The equivalent expression for B is given by replacing @1 with @1 - 1:
9996    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9998    @0 and @2 can be swapped in both expressions without changing the result.
10000    The folds rely on sizetype's being unsigned (which is always true)
10001    and on its being the same width as the pointer (which we have to check).
10003    The fold replaces two pointer_plus expressions, two comparisons and
10004    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
10005    the best case it's a saving of two operations.  The A fold retains one
10006    of the original pointer_pluses, so is a win even if both pointer_pluses
10007    are used elsewhere.  The B fold is a wash if both pointer_pluses are
10008    used elsewhere, since all we end up doing is replacing a comparison with
10009    a pointer_plus.  We do still apply the fold under those circumstances
10010    though, in case applying it to other conditions eventually makes one of the
10011    pointer_pluses dead.  */
10012 (for ior (truth_orif truth_or bit_ior)
10013  (for cmp (le lt)
10014   (simplify
10015    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
10016         (cmp:cs (pointer_plus@4 @2 @1) @0))
10017    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
10018         && TYPE_OVERFLOW_WRAPS (sizetype)
10019         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
10020     /* Calculate the rhs constant.  */
10021     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
10022             offset_int rhs = off * 2; }
10023      /* Always fails for negative values.  */
10024      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
10025       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
10026          pick a canonical order.  This increases the chances of using the
10027          same pointer_plus in multiple checks.  */
10028       (with { bool swap_p = tree_swap_operands_p (@0, @2);
10029               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
10030        (if (cmp == LT_EXPR)
10031         (gt (convert:sizetype
10032              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
10033                                      { swap_p ? @0 : @2; }))
10034             { rhs_tree; })
10035         (gt (convert:sizetype
10036              (pointer_diff:ssizetype
10037               (pointer_plus { swap_p ? @2 : @0; }
10038                             { wide_int_to_tree (sizetype, off); })
10039               { swap_p ? @0 : @2; }))
10040             { rhs_tree; })))))))))
10042 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
10043    element of @1.  */
10044 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
10045  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
10046   (with { int i = single_nonzero_element (@1); }
10047    (if (i >= 0)
10048     (with { tree elt = vector_cst_elt (@1, i);
10049             tree elt_type = TREE_TYPE (elt);
10050             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
10051             tree size = bitsize_int (elt_bits);
10052             tree pos = bitsize_int (elt_bits * i); }
10053      (view_convert
10054       (bit_and:elt_type
10055        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
10056        { elt; })))))))
10058 /* Fold reduction of a single nonzero element constructor.  */
10059 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
10060   (simplify (reduc (CONSTRUCTOR@0))
10061     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
10062                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
10063             tree elt = ctor_single_nonzero_element (ctor); }
10064       (if (elt
10065            && !HONOR_SNANS (type)
10066            && !HONOR_SIGNED_ZEROS (type))
10067         { elt; }))))
10069 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
10070 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
10071             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
10072      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
10073   (simplify (reduc (op @0 VECTOR_CST@1))
10074     (op (reduc:type @0) (reduc:type @1))))
10076 /* Simplify vector floating point operations of alternating sub/add pairs
10077    into using an fneg of a wider element type followed by a normal add.
10078    under IEEE 754 the fneg of the wider type will negate every even entry
10079    and when doing an add we get a sub of the even and add of every odd
10080    elements.  */
10081 (for plusminus (plus minus)
10082      minusplus (minus plus)
10083  (simplify
10084   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
10085    (if (!VECTOR_INTEGER_TYPE_P (type)
10086         && !FLOAT_WORDS_BIG_ENDIAN
10087         /* plus is commutative, while minus is not, so :c can't be used.
10088            Do equality comparisons by hand and at the end pick the operands
10089            from the minus.  */
10090         && (operand_equal_p (@0, @2, 0)
10091             ? operand_equal_p (@1, @3, 0)
10092             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
10093    (with
10094     {
10095       /* Build a vector of integers from the tree mask.  */
10096       vec_perm_builder builder;
10097     }
10098     (if (tree_to_vec_perm_builder (&builder, @4))
10099      (with
10100       {
10101         /* Create a vec_perm_indices for the integer vector.  */
10102         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
10103         vec_perm_indices sel (builder, 2, nelts);
10104         machine_mode vec_mode = TYPE_MODE (type);
10105         machine_mode wide_mode;
10106         scalar_mode wide_elt_mode;
10107         poly_uint64 wide_nunits;
10108         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
10109       }
10110       (if (VECTOR_MODE_P (vec_mode)
10111            && sel.series_p (0, 2, 0, 2)
10112            && sel.series_p (1, 2, nelts + 1, 2)
10113            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
10114            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
10115            && related_vector_mode (vec_mode, wide_elt_mode,
10116                                    wide_nunits).exists (&wide_mode))
10117        (with
10118         {
10119           tree stype
10120             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
10121                                               TYPE_UNSIGNED (type));
10122           tree ntype = build_vector_type_for_mode (stype, wide_mode);
10124           /* The format has to be a non-extended ieee format.  */
10125           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
10126           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
10127         }
10128         (if (TYPE_MODE (stype) != BLKmode
10129              && VECTOR_TYPE_P (ntype)
10130              && fmt_old != NULL
10131              && fmt_new != NULL)
10132          (with
10133           {
10134             /* If the target doesn't support v1xx vectors, try using
10135                scalar mode xx instead.  */
10136             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
10137                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
10138               ntype = stype;
10139           }
10140           (if (fmt_new->signbit_rw
10141                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
10142                && fmt_new->signbit_rw == fmt_new->signbit_ro
10143                && targetm.can_change_mode_class (TYPE_MODE (ntype),
10144                                                  TYPE_MODE (type), ALL_REGS)
10145                && ((optimize_vectors_before_lowering_p ()
10146                     && VECTOR_TYPE_P (ntype))
10147                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
10148            (if (plusminus == PLUS_EXPR)
10149             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
10150             (minus @0 (view_convert:type
10151                         (negate (view_convert:ntype @1))))))))))))))))
10153 (simplify
10154  (vec_perm @0 @1 VECTOR_CST@2)
10155  (with
10156   {
10157     tree op0 = @0, op1 = @1, op2 = @2;
10158     machine_mode result_mode = TYPE_MODE (type);
10159     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
10161     /* Build a vector of integers from the tree mask.  */
10162     vec_perm_builder builder;
10163   }
10164   (if (tree_to_vec_perm_builder (&builder, op2))
10165    (with
10166     {
10167       /* Create a vec_perm_indices for the integer vector.  */
10168       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
10169       bool single_arg = (op0 == op1);
10170       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
10171     }
10172     (if (sel.series_p (0, 1, 0, 1))
10173      { op0; }
10174      (if (sel.series_p (0, 1, nelts, 1))
10175       { op1; }
10176       (with
10177        {
10178          if (!single_arg)
10179            {
10180              if (sel.all_from_input_p (0))
10181                op1 = op0;
10182              else if (sel.all_from_input_p (1))
10183                {
10184                  op0 = op1;
10185                  sel.rotate_inputs (1);
10186                }
10187              else if (known_ge (poly_uint64 (sel[0]), nelts))
10188                {
10189                  std::swap (op0, op1);
10190                  sel.rotate_inputs (1);
10191                }
10192            }
10193          gassign *def;
10194          tree cop0 = op0, cop1 = op1;
10195          if (TREE_CODE (op0) == SSA_NAME
10196              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
10197              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
10198            cop0 = gimple_assign_rhs1 (def);
10199          if (TREE_CODE (op1) == SSA_NAME
10200              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
10201              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
10202            cop1 = gimple_assign_rhs1 (def);
10203          tree t;
10204        }
10205        (if ((TREE_CODE (cop0) == VECTOR_CST
10206              || TREE_CODE (cop0) == CONSTRUCTOR)
10207             && (TREE_CODE (cop1) == VECTOR_CST
10208                 || TREE_CODE (cop1) == CONSTRUCTOR)
10209             && (t = fold_vec_perm (type, cop0, cop1, sel)))
10210         { t; }
10211         (with
10212          {
10213            bool changed = (op0 == op1 && !single_arg);
10214            tree ins = NULL_TREE;
10215            unsigned at = 0;
10217            /* See if the permutation is performing a single element
10218               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
10219               in that case.  But only if the vector mode is supported,
10220               otherwise this is invalid GIMPLE.  */
10221            if (op_mode != BLKmode
10222                && (TREE_CODE (cop0) == VECTOR_CST
10223                    || TREE_CODE (cop0) == CONSTRUCTOR
10224                    || TREE_CODE (cop1) == VECTOR_CST
10225                    || TREE_CODE (cop1) == CONSTRUCTOR))
10226              {
10227                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
10228                if (insert_first_p)
10229                  {
10230                    /* After canonicalizing the first elt to come from the
10231                       first vector we only can insert the first elt from
10232                       the first vector.  */
10233                    at = 0;
10234                    if ((ins = fold_read_from_vector (cop0, sel[0])))
10235                      op0 = op1;
10236                  }
10237                /* The above can fail for two-element vectors which always
10238                   appear to insert the first element, so try inserting
10239                   into the second lane as well.  For more than two
10240                   elements that's wasted time.  */
10241                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
10242                  {
10243                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
10244                    for (at = 0; at < encoded_nelts; ++at)
10245                      if (maybe_ne (sel[at], at))
10246                        break;
10247                    if (at < encoded_nelts
10248                        && (known_eq (at + 1, nelts)
10249                            || sel.series_p (at + 1, 1, at + 1, 1)))
10250                      {
10251                        if (known_lt (poly_uint64 (sel[at]), nelts))
10252                          ins = fold_read_from_vector (cop0, sel[at]);
10253                        else
10254                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
10255                      }
10256                  }
10257              }
10259            /* Generate a canonical form of the selector.  */
10260            if (!ins && sel.encoding () != builder)
10261              {
10262                /* Some targets are deficient and fail to expand a single
10263                   argument permutation while still allowing an equivalent
10264                   2-argument version.  */
10265                tree oldop2 = op2;
10266                if (sel.ninputs () == 2
10267                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
10268                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
10269                else
10270                  {
10271                    vec_perm_indices sel2 (builder, 2, nelts);
10272                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
10273                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
10274                    else
10275                      /* Not directly supported with either encoding,
10276                         so use the preferred form.  */
10277                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
10278                  }
10279                if (!operand_equal_p (op2, oldop2, 0))
10280                  changed = true;
10281              }
10282          }
10283          (if (ins)
10284           (bit_insert { op0; } { ins; }
10285            { bitsize_int (at * vector_element_bits (type)); })
10286           (if (changed)
10287            (vec_perm { op0; } { op1; } { op2; }))))))))))))
10289 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
10291 (match vec_same_elem_p
10292  (vec_duplicate @0))
10294 (match vec_same_elem_p
10295  CONSTRUCTOR@0
10296  (if (TREE_CODE (@0) == SSA_NAME
10297       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
10299 (match vec_same_elem_p
10300  @0
10301  (if (uniform_vector_p (@0))))
10304 (simplify
10305  (vec_perm vec_same_elem_p@0 @0 @1)
10306  (if (types_match (type, TREE_TYPE (@0)))
10307   @0
10308   (with
10309    {
10310      tree elem = uniform_vector_p (@0);
10311    }
10312    (if (elem)
10313     { build_vector_from_val (type, elem); }))))
10315 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
10316 (simplify
10317  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
10318  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
10319   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
10320 (simplify
10321  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
10322  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
10323   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
10326 /* Merge
10327      c = VEC_PERM_EXPR <a, b, VCST0>;
10328      d = VEC_PERM_EXPR <c, c, VCST1>;
10329    to
10330      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
10332 (simplify
10333  (vec_perm (view_convert?@0 (vec_perm@1 @2 @3 VECTOR_CST@4)) @0 VECTOR_CST@5)
10334  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10335   (with
10336    {
10337      machine_mode result_mode = TYPE_MODE (type);
10338      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@2));
10339      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10340      vec_perm_builder builder0;
10341      vec_perm_builder builder1;
10342      vec_perm_builder builder2 (nelts, nelts, 1);
10343    }
10344    (if (tree_to_vec_perm_builder (&builder0, @4)
10345         && tree_to_vec_perm_builder (&builder1, @5)
10346         && TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))
10347            == TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))))
10348     (with
10349      {
10350        vec_perm_indices sel0 (builder0, 2, nelts);
10351        vec_perm_indices sel1 (builder1, 1, nelts);
10353        for (int i = 0; i < nelts; i++)
10354          builder2.quick_push (sel0[sel1[i].to_constant ()]);
10356        vec_perm_indices sel2 (builder2, 2, nelts);
10358        tree op0 = NULL_TREE;
10359        /* If the new VEC_PERM_EXPR can't be handled but both
10360           original VEC_PERM_EXPRs can, punt.
10361           If one or both of the original VEC_PERM_EXPRs can't be
10362           handled and the new one can't be either, don't increase
10363           number of VEC_PERM_EXPRs that can't be handled.  */
10364        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10365            || (single_use (@0)
10366                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10367                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10368                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10369          op0 = vec_perm_indices_to_tree (TREE_TYPE (@5), sel2);
10370      }
10371      (if (op0)
10372       (view_convert (vec_perm @2 @3 { op0; }))))))))
10374 /* Merge
10375      c = VEC_PERM_EXPR <a, b, VCST0>;
10376      d = VEC_PERM_EXPR <x, c, VCST1>;
10377    to
10378      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
10379    when all elements from a or b are replaced by the later
10380    permutation.  */
10382 (simplify
10383  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
10384  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10385   (with
10386    {
10387      machine_mode result_mode = TYPE_MODE (type);
10388      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10389      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10390      vec_perm_builder builder0;
10391      vec_perm_builder builder1;
10392      vec_perm_builder builder2 (nelts, nelts, 2);
10393    }
10394    (if (tree_to_vec_perm_builder (&builder0, @3)
10395         && tree_to_vec_perm_builder (&builder1, @4))
10396     (with
10397      {
10398        vec_perm_indices sel0 (builder0, 2, nelts);
10399        vec_perm_indices sel1 (builder1, 2, nelts);
10400        bool use_1 = false, use_2 = false;
10402        for (int i = 0; i < nelts; i++)
10403          {
10404            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10405              builder2.quick_push (sel1[i]);
10406            else
10407              {
10408                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10409                                     .to_constant ()];
10410                if (known_lt (j, sel0.nelts_per_input ()))
10411                  use_1 = true;
10412                else
10413                  {
10414                    use_2 = true;
10415                    j -= sel0.nelts_per_input ();
10416                  }
10417                builder2.quick_push (j + sel1.nelts_per_input ());
10418              }
10419          }
10420      }
10421      (if (use_1 ^ use_2)
10422       (with
10423        {
10424          vec_perm_indices sel2 (builder2, 2, nelts);
10425          tree op0 = NULL_TREE;
10426          /* If the new VEC_PERM_EXPR can't be handled but both
10427             original VEC_PERM_EXPRs can, punt.
10428             If one or both of the original VEC_PERM_EXPRs can't be
10429             handled and the new one can't be either, don't increase
10430             number of VEC_PERM_EXPRs that can't be handled.  */
10431          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10432              || (single_use (@0)
10433                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10434                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10435                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10436            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10437        }
10438        (if (op0)
10439         (switch
10440          (if (use_1)
10441           (vec_perm @5 @1 { op0; }))
10442          (if (use_2)
10443           (vec_perm @5 @2 { op0; })))))))))))
10445 /* And the case with swapped outer permute sources.  */
10447 (simplify
10448  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10449  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10450   (with
10451    {
10452      machine_mode result_mode = TYPE_MODE (type);
10453      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10454      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10455      vec_perm_builder builder0;
10456      vec_perm_builder builder1;
10457      vec_perm_builder builder2 (nelts, nelts, 2);
10458    }
10459    (if (tree_to_vec_perm_builder (&builder0, @3)
10460         && tree_to_vec_perm_builder (&builder1, @4))
10461     (with
10462      {
10463        vec_perm_indices sel0 (builder0, 2, nelts);
10464        vec_perm_indices sel1 (builder1, 2, nelts);
10465        bool use_1 = false, use_2 = false;
10467        for (int i = 0; i < nelts; i++)
10468          {
10469            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10470              builder2.quick_push (sel1[i]);
10471            else
10472              {
10473                poly_uint64 j = sel0[sel1[i].to_constant ()];
10474                if (known_lt (j, sel0.nelts_per_input ()))
10475                  use_1 = true;
10476                else
10477                  {
10478                    use_2 = true;
10479                    j -= sel0.nelts_per_input ();
10480                  }
10481                builder2.quick_push (j);
10482              }
10483          }
10484      }
10485      (if (use_1 ^ use_2)
10486       (with
10487        {
10488          vec_perm_indices sel2 (builder2, 2, nelts);
10489          tree op0 = NULL_TREE;
10490          /* If the new VEC_PERM_EXPR can't be handled but both
10491             original VEC_PERM_EXPRs can, punt.
10492             If one or both of the original VEC_PERM_EXPRs can't be
10493             handled and the new one can't be either, don't increase
10494             number of VEC_PERM_EXPRs that can't be handled.  */
10495          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10496              || (single_use (@0)
10497                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10498                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10499                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10500            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10501        }
10502        (if (op0)
10503         (switch
10504          (if (use_1)
10505           (vec_perm @1 @5 { op0; }))
10506          (if (use_2)
10507           (vec_perm @2 @5 { op0; })))))))))))
10510 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10511    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10512    constant which when multiplied by a power of 2 contains a unique value
10513    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10514    to the number of trailing zeroes.  */
10515 (match (ctz_table_index @1 @2 @3)
10516   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10518 (match (cond_expr_convert_p @0 @2 @3 @6)
10519  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10520   (if (INTEGRAL_TYPE_P (type)
10521        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10522        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10523        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10524        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10525        && TYPE_PRECISION (TREE_TYPE (@0))
10526           == TYPE_PRECISION (TREE_TYPE (@2))
10527        && TYPE_PRECISION (TREE_TYPE (@0))
10528           == TYPE_PRECISION (TREE_TYPE (@3))
10529        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10530           signess when convert is truncation, but not ok for extension since
10531           it's sign_extend vs zero_extend.  */
10532        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10533            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10534                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10535        && single_use (@4)
10536        && single_use (@5))))
10538 (for bit_op (bit_and bit_ior bit_xor)
10539  (match (bitwise_induction_p @0 @2 @3)
10540   (bit_op:c
10541    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10542    @3)))
10544 (match (bitwise_induction_p @0 @2 @3)
10545  (bit_not
10546   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10548 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10549    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10550 (simplify
10551   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10552   (with { auto i = wi::neg (wi::to_wide (@2)); }
10553   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10554     (if (wi::popcount (i) == 1
10555          && (wi::to_wide (@1)) == (i - 1))
10556       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10557         (bit_and @0 @1)
10558       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10560 /* -x & 1 -> x & 1.  */
10561 (simplify 
10562  (bit_and (negate @0) integer_onep@1)
10563  (if (!TYPE_OVERFLOW_SANITIZED (type))
10564   (bit_and @0 @1)))
10566 /* `-a` is just `a` if the type is 1bit wide or when converting
10567    to a 1bit type; similar to the above transformation of `(-x)&1`.
10568    This is used mostly with the transformation of
10569    `a ? ~b : b` into `(-a)^b`.
10570    It also can show up with bitfields.  */
10571 (simplify
10572  (convert? (negate @0))
10573  (if (INTEGRAL_TYPE_P (type)
10574       && TYPE_PRECISION (type) == 1
10575       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10576   (convert @0)))
10578 /* Optimize
10579    c1 = VEC_PERM_EXPR (a, a, mask)
10580    c2 = VEC_PERM_EXPR (b, b, mask)
10581    c3 = c1 op c2
10582    -->
10583    c = a op b
10584    c3 = VEC_PERM_EXPR (c, c, mask)
10585    For all integer non-div operations.  */
10586 (for op (plus minus mult bit_and bit_ior bit_xor
10587          lshift rshift)
10588  (simplify
10589   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10590    (if (VECTOR_INTEGER_TYPE_P (type))
10591     (vec_perm (op@3 @0 @1) @3 @2))))
10593 /* Similar for float arithmetic when permutation constant covers
10594    all vector elements.  */
10595 (for op (plus minus mult)
10596  (simplify
10597   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10598    (if (VECTOR_FLOAT_TYPE_P (type)
10599         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10600     (with
10601      {
10602        tree perm_cst = @2;
10603        vec_perm_builder builder;
10604        bool full_perm_p = false;
10605        if (tree_to_vec_perm_builder (&builder, perm_cst))
10606          {
10607            unsigned HOST_WIDE_INT nelts;
10609            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10610            /* Create a vec_perm_indices for the VECTOR_CST.  */
10611            vec_perm_indices sel (builder, 1, nelts);
10613            /* Check if perm indices covers all vector elements.  */
10614            if (sel.encoding ().encoded_full_vector_p ())
10615              {
10616                auto_sbitmap seen (nelts);
10617                bitmap_clear (seen);
10619                unsigned HOST_WIDE_INT count = 0, i;
10621                for (i = 0; i < nelts; i++)
10622                  {
10623                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10624                     break;
10625                    count++;
10626                  }
10627                full_perm_p = count == nelts;
10628              }
10629          }
10630       }
10631       (if (full_perm_p)
10632         (vec_perm (op@3 @0 @1) @3 @2))))))