public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals
@ 2023-04-19 12:39 i.nixman at autistici dot org
  2023-04-19 12:42 ` [Bug middle-end/109555] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: i.nixman at autistici dot org @ 2023-04-19 12:39 UTC (permalink / raw)
  To: gcc-bugs

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!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-19 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-19 12:39 [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals i.nixman at autistici dot org
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

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).