public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/64753] New: Redundant cmp instruction on x86_64
@ 2015-01-23 15:06 rv at rasmusvillemoes dot dk
  2015-01-28 12:58 ` [Bug target/64753] " rv at rasmusvillemoes dot dk
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: rv at rasmusvillemoes dot dk @ 2015-01-23 15:06 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 64753
           Summary: Redundant cmp instruction on x86_64
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rv at rasmusvillemoes dot dk
              Host: x86_64
            Target: x86_64

The linux kernel's library strncmp is this:

int strncmp(const char *cs, const char *ct, size_t count)
{
        unsigned char c1, c2;

        while (count) {
                c1 = *cs++;
                c2 = *ct++;
                if (c1 != c2)
                        return c1 < c2 ? -1 : 1;
                if (!c1)
                        break;
                count--;
        }
        return 0;
}

Compiling with gcc -O2 -S I get this:

strncmp:
.LFB0:
        .cfi_startproc
        testq   %rdx, %rdx
        je      .L10
        movzbl  (%rdi), %ecx
        movzbl  (%rsi), %r8d
#        cmpb    %r8b, %cl
#        jne     .L3
        testb   %cl, %cl
        je      .L10
        subq    $1, %rdx
        xorl    %eax, %eax
        jmp     .L4
        .p2align 4,,10
        .p2align 3
.L6:
        movzbl  1(%rdi,%rax), %ecx
        movzbl  1(%rsi,%rax), %r8d
#        cmpb    %r8b, %cl
#        jne     .L3
        addq    $1, %rax
        testb   %cl, %cl
        je      .L10
.L4:
        cmpq    %rdx, %rax
        jne     .L6
.L10:
        xorl    %eax, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L3:
        cmpb    %r8b, %cl
        sbbl    %eax, %eax
        orl     $1, %eax
        ret
        .cfi_endproc

At the two places marked # we do a cmp and a conditional jump to .L3, where for
good measure the same cmp is done again... there's no other path to .L3, so it
would seem that simply omitting that extra cmp should be ok. 

This is with gcc-5.0 (GCC) 5.0.0 20150112 (experimental), but I see the same
with gcc (Debian 4.7.2-5) 4.7.2.


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

end of thread, other threads:[~2024-03-17  7:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-23 15:06 [Bug target/64753] New: Redundant cmp instruction on x86_64 rv at rasmusvillemoes dot dk
2015-01-28 12:58 ` [Bug target/64753] " rv at rasmusvillemoes dot dk
2015-01-28 13:31 ` ubizjak at gmail dot com
2024-03-17  7:39 ` 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).