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

            Bug ID: 105992
           Summary: memcmp(p, q, 7) == 0 can be optimized better on x86
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pdimov at gmail dot com
  Target Milestone: ---

bool eq( char const* p )
{
    return __builtin_memcmp( p, "literal", 7 ) == 0;
}

generates

eq(char const*):
        cmp     DWORD PTR [rdi], 1702127980
        je      .L6
.L2:
        mov     eax, 1
        test    eax, eax
        sete    al
        ret
.L6:
        xor     eax, eax
        cmp     DWORD PTR [rdi+3], 1818325605
        jne     .L2
        test    eax, eax
        sete    al
        ret

(https://godbolt.org/z/68MKqGz9T)

but LLVM does

eq(char const*):                               # @eq(char const*)
        mov     eax, 1702127980
        xor     eax, dword ptr [rdi]
        mov     ecx, 1818325605
        xor     ecx, dword ptr [rdi + 3]
        or      ecx, eax
        sete    al
        ret

(https://godbolt.org/z/jxcb85Ysa)

There are similar bugs for ARM
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104611) and AVX512
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104610) but I haven't found one
for vanilla x86.

Recent changes to std::string::operator== make it use the above pattern:
https://godbolt.org/z/8KxqqG9cx

Reply via email to