public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/105992] New: memcmp(p, q, 7) == 0 can be optimized better on x86
@ 2022-06-15 18:01 pdimov at gmail dot com
  2022-06-16  7:43 ` [Bug target/105992] " crazylht at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: pdimov at gmail dot com @ 2022-06-15 18:01 UTC (permalink / raw)
  To: gcc-bugs

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

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

* [Bug target/105992] memcmp(p, q, 7) == 0 can be optimized better on x86
  2022-06-15 18:01 [Bug target/105992] New: memcmp(p, q, 7) == 0 can be optimized better on x86 pdimov at gmail dot com
@ 2022-06-16  7:43 ` crazylht at gmail dot com
  2022-06-16 15:08 ` hjl.tools at gmail dot com
  2022-06-16 15:37 ` hjl.tools at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: crazylht at gmail dot com @ 2022-06-16  7:43 UTC (permalink / raw)
  To: gcc-bugs

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

Hongtao.liu <crazylht at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |crazylht at gmail dot com,
                   |                            |hjl.tools at gmail dot com

--- Comment #1 from Hongtao.liu <crazylht at gmail dot com> ---
I'm wondering would targetm.overlap_op_by_pieces_p helps here.

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

* [Bug target/105992] memcmp(p, q, 7) == 0 can be optimized better on x86
  2022-06-15 18:01 [Bug target/105992] New: memcmp(p, q, 7) == 0 can be optimized better on x86 pdimov at gmail dot com
  2022-06-16  7:43 ` [Bug target/105992] " crazylht at gmail dot com
@ 2022-06-16 15:08 ` hjl.tools at gmail dot com
  2022-06-16 15:37 ` hjl.tools at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: hjl.tools at gmail dot com @ 2022-06-16 15:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from H.J. Lu <hjl.tools at gmail dot com> ---
(In reply to Hongtao.liu from comment #1)
> I'm wondering would targetm.overlap_op_by_pieces_p helps here.

It is related to PR 103798.

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

* [Bug target/105992] memcmp(p, q, 7) == 0 can be optimized better on x86
  2022-06-15 18:01 [Bug target/105992] New: memcmp(p, q, 7) == 0 can be optimized better on x86 pdimov at gmail dot com
  2022-06-16  7:43 ` [Bug target/105992] " crazylht at gmail dot com
  2022-06-16 15:08 ` hjl.tools at gmail dot com
@ 2022-06-16 15:37 ` hjl.tools at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: hjl.tools at gmail dot com @ 2022-06-16 15:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from H.J. Lu <hjl.tools at gmail dot com> ---
(In reply to Hongtao.liu from comment #1)
> I'm wondering would targetm.overlap_op_by_pieces_p helps here.

The code is generated by targetm.overlap_op_by_pieces_p.  It compares
4 byte at a time with branches.

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

end of thread, other threads:[~2022-06-16 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-15 18:01 [Bug target/105992] New: memcmp(p, q, 7) == 0 can be optimized better on x86 pdimov at gmail dot com
2022-06-16  7:43 ` [Bug target/105992] " crazylht at gmail dot com
2022-06-16 15:08 ` hjl.tools at gmail dot com
2022-06-16 15:37 ` hjl.tools at gmail dot com

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