From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A0F753858D3C; Thu, 5 May 2022 14:18:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0F753858D3C From: "redbeard0531 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/105496] New: Comparison optimizations result in unnecessary cmp instructions Date: Thu, 05 May 2022 14:18:53 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redbeard0531 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 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, 05 May 2022 14:18:53 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105496 Bug ID: 105496 Summary: Comparison optimizations result in unnecessary cmp instructions Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: redbeard0531 at gmail dot com Target Milestone: --- https://godbolt.org/z/1zdYsaqEj Consider these equivalent functions: int test1(int x) { if (x <=3D 10) return 123; if (x =3D=3D 11) return 456; return 789; } int test2(int x) { if (x < 11) return 123; if (x =3D=3D 11) return 456; return 789; } In test2 it is very clear that you can do a single cmp of x with 11 then use different flag bits to choose your case. In test1 it is less clear, but bec= ause x<=3D10 and x<11 are equivalent, you could always transform one to the othe= r. Clang seems to do this correctly and transforms test1 into test2 and only e= mits a single cmp instruction in each. For some reason, not only does gcc miss t= his optimization, it seems to go the other direction and transform test2 into test1, emitting 2 cmp instructions for both! test1(int): mov eax, 123 cmp edi, 10 jle .L1 cmp edi, 11 mov eax, 456 mov edx, 789 cmovne eax, edx .L1: ret test2(int): mov eax, 123 cmp edi, 10 jle .L6 cmp edi, 11 mov eax, 456 mov edx, 789 cmovne eax, edx .L6: ret Observed with at least -O2 and -O3. I initially observed this for code where each if generated an actual branch rather than a cmov, but when I reduced t= he example, the cmov was generated. I'm not sure if this should be a middle-end or target specific optimization, since ideally it would be smart on all targets that use comparison flags, e= ven if there are some targets that don't. Is there ever a down side to trying to make two adjacent comparisons compare the same number?=