From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BF686383303F; Thu, 17 Dec 2020 10:04:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF686383303F From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/98334] Failure to optimally optimize add loop to mul Date: Thu, 17 Dec 2020 10:04:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization 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, 17 Dec 2020 10:04:17 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98334 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #1 from Jakub Jelinek --- This boils down to: int bar (int x, int y) { return (int) (y - 1U) * x + x; } int baz (int x, int y) { return (y - 1) * x + x; } We do optimize the latter, but don't optimize the former into y * x. For non-wrapping operation in bar that would be an invalid optimization, as (int) (y - 1U) * x + x is well defined for y =3D=3D INT_MIN and x =3D=3D= -1: it is INT_MAX * -1 + -1, i.e. INT_MIN without overflow, but INT_MIN * -1 does overflow. But we perhaps could and should turn that into just (int) ((y - 1U) * x), i.e. unsigned multiplication. We shouldn't do this until very late though, because turning signed arithme= tics into unsigned may disable other optimizations as it has no UB. Or we could optimize this on RTL, combiner attempts: Trying 7, 8 -> 9: 7: {r90:SI=3Dr93:SI-0x1;clobber flags:CC;} REG_DEAD r93:SI REG_UNUSED flags:CC 8: {r91:SI=3Dr90:SI*r92:SI;clobber flags:CC;} REG_UNUSED flags:CC REG_DEAD r90:SI 9: r89:SI=3Dr91:SI+r92:SI REG_DEAD r92:SI REG_DEAD r91:SI Failed to match this instruction: (set (reg:SI 89) (plus:SI (mult:SI (plus:SI (reg:SI 93) (const_int -1 [0xffffffffffffffff])) (reg:SI 92)) (reg:SI 92))) so if we hack up simplify-rtx.c to optimize that to (mult:SI (reg:SI 93) (reg:SI 92)), it should work.=