https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98865

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
        PR middle-end/98865
        * match.pd (a * (b >> (prec-1)) to ((signed)b >> (prec-1)) & a): New
        simplification.

--- gcc/match.pd.jj     2021-01-22 11:50:09.882909120 +0100
+++ gcc/match.pd        2021-01-28 15:20:20.536238614 +0100
@@ -793,6 +793,16 @@ (define_operator_list COND_TERNARY
        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
    (lshift @0 @2)))

+/* Fold (a * (b >> (prec-1))) with logical shift into
+   ((signed)b >> (prec-1)) & a.  */
+(simplify
+ (mult:c @0 (nop_convert? (rshift @1 INTEGER_CST@2)))
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
+       && TYPE_UNSIGNED (TREE_TYPE (@1))
+       && wi::to_widest (@2) + 1 == TYPE_PRECISION (TREE_TYPE (@1)))
+   (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
+    (bit_and (convert:type (rshift (convert:stype @1) @2)) @0))))
+
 /* Fold (1 << (C - x)) where C = precision(type) - 1
    into ((1 << C) >> x). */
 (simplify

(completely untested) does that.
It doesn't handle vector types, whether that is a good idea or not depends on
how do we deal with the match.pd simplifications after last veclower pass
issue.
And, given:
unsigned long long
foo (unsigned long long a, unsigned long long b)
{
  return (a >> 63) * b;
}

long long
bar (long long a, long long b)
{
  return -(a >> 63) * b;
}

long long
baz (long long a, long long b)
{
  long long c = a >> 63;
  long long d = -c;
  return d * b;
}
we optimize with it for and bar but not baz, apparently the -(a >> 63)
arithmetic to (a >> 63) logical shift is done only in GENERIC folding and not
later.

Reply via email to