public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "Explorer09 at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug target/114490] New: Optimization: x86 "shl" condition codes never reused Date: Tue, 26 Mar 2024 22:50:12 +0000 [thread overview] Message-ID: <bug-114490-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114490 Bug ID: 114490 Summary: Optimization: x86 "shl" condition codes never reused Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: Explorer09 at gmail dot com Target Milestone: --- This seems to happen with x86 target only. For the example code below, I can use the SF (sign flag) after a left shift (<<) operation, but somehow I cannot make GCC reuse the condition code of a "shl" instruction. It always generates an unnecessary "testl %edx, %edx" before the conditional jump. This is one case where the condition code of a bit shift can be useful. I know in the ARM target of GCC, the condition code is reused after the shift. ```c #include <stdbool.h> #include <stdint.h> bool my_isxdigit(unsigned char ch) { uint32_t mask1 = 0x7E00FFC0; // Prevent the compiler from transforming the constants. // Suppose we have to use them as written. __asm__ ("" : "+r" (mask1)); if (!((mask1 << (ch & 0x1F)) >> 31)) return false; uint32_t mask2 = 0x1A << 24; __asm__ ("" : "+r" (mask2)); if (!((mask2 << (ch >> 4)) >> 31)) return false; return true; } ``` x86-64 gcc with "-O3" option (Actually I tested in Compiler Explorer (godbolt.org)) ```x86asm my_isxdigit: movl %edi, %ecx movl $2113994688, %eax sall %cl, %eax movl %eax, %edx xorl %eax, %eax testl %edx, %edx jns .L1 shrb $4, %cl movl $436207616, %eax sall %cl, %eax shrl $31, %eax .L1: ret ``` Possible shorter code: ```x86asm my_isxdigit_2: movl %edi, %ecx xorl %eax, %eax movl $2113994688, %edx sall %cl, %edx jns .L2 shrb $4, %cl movl $436207616, %eax sall %cl, %eax shrl $31, %eax .L2: ret ```
next reply other threads:[~2024-03-26 22:50 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-03-26 22:50 Explorer09 at gmail dot com [this message] 2024-03-26 23:11 ` [Bug target/114490] " pinskia at gcc dot gnu.org 2024-03-26 23:26 ` pinskia at gcc dot gnu.org 2024-03-26 23:52 ` Explorer09 at gmail dot com 2024-03-27 11:28 ` Explorer09 at gmail dot com 2024-03-27 12:41 ` Explorer09 at gmail dot com 2024-03-27 19:08 ` pinskia at gcc dot gnu.org 2024-03-28 8:53 ` Explorer09 at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-114490-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).