From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C0D60385840D; Sat, 18 Sep 2021 18:17:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0D60385840D From: "gabravier at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/102402] New: Seemingly suboptimal optimization of jmp/cmovcc for conditionally loading constants Date: Sat, 18 Sep 2021 18:17:08 +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: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gabravier 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: Sat, 18 Sep 2021 18:17:08 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102402 Bug ID: 102402 Summary: Seemingly suboptimal optimization of jmp/cmovcc for conditionally loading constants Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: gabravier at gmail dot com Target Milestone: --- #include struct MusicPlayerTrack { uint8_t flags; uint8_t modT; }; void ClearModM(struct MusicPlayerTrack *track, uint8_t modT) { if (track->modT =3D=3D 0) track->flags |=3D 3; else track->flags |=3D 12; } This is optimized weirdly by GCC. Leaving it as-is gives this AMD64 assembl= y: ClearModM: movzx edx, BYTE PTR [rdi] mov eax, edx or eax, 12 cmp BYTE PTR [rdi+1], 0 jne .L3 mov eax, edx or eax, 3 .L3: mov BYTE PTR [rdi], al ret Whereas changing the `if` to `if (modT =3D=3D 0)` gives this: ClearModM: movzx eax, BYTE PTR [rdi] mov edx, eax or eax, 12 or edx, 3 test sil, sil cmove eax, edx mov BYTE PTR [rdi], al ret It seems to me that this should be better than the first output, though of course this could be the other way considering how finicky cmovcc seems to = be, but it seems to me like at least one should be preferred above the other. Note that this also occurs on IA-32, so the issue seems unrelated to whether modT is in a register or in memory. Perhaps it's about whether it's a funct= ion argument ?=