From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5B1FE3844026; Thu, 28 Jan 2021 14:26:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B1FE3844026 From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/98865] Missed transform of (a >> 63) * b Date: Thu, 28 Jan 2021 14:26:10 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jan 2021 14:26:10 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98865 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #2 from Jakub Jelinek --- 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 =3D=3D TYPE_PRECISION (TREE_TYPE (@1))) + (with { tree stype =3D signed_type_for (TREE_TYPE (@1)); } + (bit_and (convert:type (rshift (convert:stype @1) @2)) @0)))) + /* Fold (1 << (C - x)) where C =3D 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 =3D a >> 63; long long d =3D -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 n= ot later.=