From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 75A83388700A; Thu, 20 Jun 2024 06:57:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 75A83388700A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1718866679; bh=BJ0LyG72NUc6K/jUR6jHRyBXV3NUrlJo78AcVogKiew=; h=From:To:Subject:Date:In-Reply-To:References:From; b=yIwHk3ZFeQ15KdGJAi7CbvwCNcehmdrIOpF6mtEOinJMxT4FRPnWYlsgBdIWBBbWt zFk/fhYsb9HXw5VtHgzNlwKjamb8k7XzovhlXkZAlhhBHm9uN8/a6Ne5KwE8MbSLNI F9pWRl1LxwpNipUEvGJsGKSKHasgE2Xku7pG/nlw= From: "burnus at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/115551] [missed optimization] "c1 << (a + c2)" not optimized into "(c1 << c2) << a" Date: Thu, 20 Jun 2024 06:57:58 +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: 15.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: burnus 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: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D115551 --- Comment #2 from Tobias Burnus --- > Thus we need some range info to do this optimization. Good point. It seems as if for c1 << (c2 * a + c3), C requires a >=3D -c3/c2 (read as = float division; c2 =E2=89=A0 0) And the suggested optimization requires c2*a >=3D 0 and c3 >=3D 0 to fulfil= l C requirement of nonnegative shifts. Thus, this is fulfilled for any value of 'a' if c3 >=3D 0 and abs(c2) > c3. The optimization can also be done for any value of 'a', if the hardware supports c1 << (negative value) (as right shift, fillung with zeros) and popcount(c1) =3D=3D popcount(c1 << c3). The first condition is fulfilled in this example. I don't know about the second, but observed that Clang/LLVM optimizes the d= iff mask1-mask2 to 0 on ARM but not x86_64 (not checked why nor whether ARM han= dles negative shifts in a well-defined way or not).=