From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14005 invoked by alias); 23 Jan 2015 15:06:30 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 13941 invoked by uid 48); 23 Jan 2015 15:06:21 -0000 From: "rv at rasmusvillemoes dot dk" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 5.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: rv at rasmusvillemoes dot dk X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter cf_gcchost cf_gcctarget Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-01/txt/msg02581.txt.bz2 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.