public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/54829] New: bad optimization: sub followed by cmp w/ zero (x86 & ARM)
@ 2012-10-05 23:16 daniel.santos at pobox dot com
  2012-10-05 23:40 ` [Bug target/54829] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: daniel.santos at pobox dot com @ 2012-10-05 23:16 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54829

             Bug #: 54829
           Summary: bad optimization: sub followed by cmp w/ zero (x86 &
                    ARM)
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: daniel.santos@pobox.com


I originally posted this under bug #3507 but have since discovered that it is
target-specific and is a separate issue than bug #3507.

extern print_gt(void);
extern print_lt(void);
extern print_eq(void);

void cmp_and_branch(long a, long b)
{
    long diff = a - b;

    if (diff > 0) {
        print_gt();
    } else if (diff < 0) {
        print_lt();
    } else {
        print_eq();
    }
}

Here, result of the subtraction is directly used in the branch code and nowhere
else.  However, gcc -O2 -S still generates this output:

cmp_and_branch:
.LFB0:
    .cfi_startproc
    subq    %rsi, %rdi
    cmpq    $0, %rdi
    jg    .L5
    jne    .L6
    jmp    print_eq
    .p2align 4,,10
    .p2align 3
.L5:
    jmp    print_gt
    .p2align 4,,10
    .p2align 3
.L6:
    jmp    print_lt
    .cfi_endproc

Notice that we're using subq followed by cmpq instead of just cmpq %rsi, %rdi. 
In another case, where there is a loop and one of the values compared against
remains the same, an additional mov instruction is required to prevent the
unchanging value's register from being destroyed, so it actually generates two
extra instructions in that situation.

When built on ARM, we get something similar:
cmp_and_branch:
    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 0, uses_anonymous_args = 0
    @ link register save eliminated.
    rsb    r1, r1, r0
    cmp    r1, #0
    bgt    .L5
    bne    .L6
    b    print_eq
.L5:
    b    print_gt
.L6:
    b    print_lt

Note here that we do rsb followed by cmp with zero again.  However, on PPC
(apinski from freenode compiled this for me), the result is actually correct:

subf. 9,4,3
bgt 0,.L5
bne 0,.L6
print_eq

Finally, on MIPS (also from apinski):
dsubu $4,$4,$5
bgtz $4,$L5
nop


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

end of thread, other threads:[~2015-02-15  6:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-05 23:16 [Bug target/54829] New: bad optimization: sub followed by cmp w/ zero (x86 & ARM) daniel.santos at pobox dot com
2012-10-05 23:40 ` [Bug target/54829] " pinskia at gcc dot gnu.org
2012-10-06 13:31 ` [Bug target/54829] bad optimization: sub followed by cmp w/ zero (ARM) hjl.tools at gmail dot com
2012-10-06 15:56 ` [Bug target/54829] bad optimization: sub followed by cmp w/ zero (x86 & ARM) daniel.santos at pobox dot com
2012-10-06 15:57 ` daniel.santos at pobox dot com
2012-10-13 16:05 ` rearnsha at gcc dot gnu.org
2012-10-13 16:18 ` rearnsha at gcc dot gnu.org
2012-11-15 21:56 ` daniel.santos at pobox dot com
2013-08-05 21:04 ` rearnsha at gcc dot gnu.org
2015-02-15  6:17 ` daniel.santos at pobox 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).