From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 628A03858C5F; Tue, 21 Nov 2023 21:42:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 628A03858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700602979; bh=D7DgeePcTIXxg6FFbAM0LMx9fw3ce7sYov4vZUUaV10=; h=From:To:Subject:Date:From; b=gDF0lyivAsakhIVYU28/6hxasrv/WfAObDImQ/iu784iHFLVq5X6JnGIJVkG2mbbi Jr8uPPi+dCyjW/Ki7+wt/Ja2AhK7AHObUELqMsukrFAsQHcKnv4g457tnIsNOAHVvl VkLvOdQUMCXBaf+TcPKVqE2SzEkZ4KsX/GuylNR8= From: "goon.pri.low at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/112657] New: missed optimization: cmove not used with multiple returns Date: Tue, 21 Nov 2023 21:42:59 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: goon.pri.low at gmail dot com 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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=3D112657 Bug ID: 112657 Summary: missed optimization: cmove not used with multiple returns Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: goon.pri.low at gmail dot com Target Milestone: --- This function int unopt(int c) { if (c =3D=3D 14) return -9; else return c; } unopt: mov eax, edi cmp edi, 14 je .L4 ret .L4: mov eax, -9 ret Should probably be optimized to: int opt(int c) { if (c =3D=3D 14) c =3D -9; return c; } opt: cmp edi, 14 mov eax, -9 cmovne eax, edi ret This seems to only really happen when negative numbers are used, int positive(int c) { if (c =3D=3D 14) return 9; else return c; } positive: mov eax, edi cmp edi, 14 mov edx, 9 cmove eax, edx ret Though use of positive values still isn't completely optimized (possibly sa= me as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97968) Also it seems like if the order is reversed: int reverse(int c) { if (c !=3D 14) c =3D 9120; return c; } reverse: cmp edi, 14 mov edx, 14 mov eax, 9120 cmove eax, edx ret We could use %edi in the cmove and eliminate the 2nd instruction.=