public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rv at rasmusvillemoes dot dk" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/64753] New: Redundant cmp instruction on x86_64
Date: Fri, 23 Jan 2015 15:06:00 -0000	[thread overview]
Message-ID: <bug-64753-4@http.gcc.gnu.org/bugzilla/> (raw)

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.


             reply	other threads:[~2015-01-23 15:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 15:06 rv at rasmusvillemoes dot dk [this message]
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

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