public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "i.nixman at autistici dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals
Date: Wed, 19 Apr 2023 12:39:21 +0000	[thread overview]
Message-ID: <bug-109555-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109555

            Bug ID: 109555
           Summary: suboptimal code for comparing strings with string
                    literals
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i.nixman at autistici dot org
  Target Milestone: ---

hello!


the example:

#include <cstring>
int main(int argc, char **argv) {
    return std::memcmp(argv[0], "MCRYF", 5) == 0;
}


this code compiled with GCC (-std=c++11 -O2) will be translated as the
following assembler code (https://godbolt.org/z/zzjaGdWdT):

main:
        mov     rax, QWORD PTR [rsi]
        cmp     DWORD PTR [rax], 1498563405
        je      .L5
.L2:
        mov     eax, 1
.L3:
        xor     eax, 1
        ret
.L5:
        cmp     BYTE PTR [rax+4], 70
        jne     .L2
        xor     eax, eax
        jmp     .L3



but if the code is compiled using CLang, we get a more optimal branch-less
assembler code (https://godbolt.org/z/E9o1oMzra):

main:                                   # @main
        mov     rax, qword ptr [rsi]
        mov     ecx, 1498563405
        xor     ecx, dword ptr [rax]
        movzx   edx, byte ptr [rax + 4]
        xor     edx, 70
        xor     eax, eax
        or      edx, ecx
        sete    al
        ret



maybe it's not a difficult fix, and maybe someone will see the point to improve
this...


thanks!

             reply	other threads:[~2023-04-19 12:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-19 12:39 i.nixman at autistici dot org [this message]
2023-04-19 12:42 ` [Bug middle-end/109555] " pinskia at gcc dot gnu.org
2023-04-19 12:45 ` i.nixman at autistici dot org
2023-04-19 12:49 ` pinskia at gcc dot gnu.org
2023-04-19 15:58 ` [Bug middle-end/109555] memcmp with string literals cause branchy code maybe could use branchless? pinskia at gcc dot gnu.org

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-109555-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: link
Be 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).