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

           Summary: missed optimization in comparison
           Product: gcc
           Version: 4.5.4
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: bo...@gcc.gnu.org


Created attachment 22811
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22811
Preprocessed source to reproduce

The attached C test case shows missed optimization, where lenzero could be
optimized to generate the same code as lenzero2. This pattern is very common in
Ada, where the 'Length attribute for arrays expands to the same code as that
for len. As this is all simple integer code without any aliasing issues, I have
some hope that GCC can do better here.

Regards,
  -Geert 

cat lenzero.c && gcc -O3 -fomit-frame-pointer -save-temps -c lenzero.c && otool
-tv lenzero.o
int len(int f, int l) {
 return l < f ? 0 : l - f + 1;
}

int lenzero(int f, int l) {
 return len(f, l) == 0;
}

int lenzero2(int f, int l) {
 return l < f;
}

lenzero.o:
(__TEXT,__text) section
_len:
0000000000000000    xorl    %eax,%eax
0000000000000002    cmpl    %edi,%esi
0000000000000004    jl    0x0000000b
0000000000000006    subl    %edi,%esi
0000000000000008    leal    0x01(%rsi),%eax
000000000000000b    repz/ret
000000000000000d    nop
000000000000000e    nop
_lenzero:
0000000000000010    cmpl    %esi,%edi
0000000000000012    movl    $0x00000001,%eax
0000000000000017    jg    0x00000023
0000000000000019    subl    %edi,%esi
000000000000001b    xorl    %eax,%eax
000000000000001d    cmpl    $0xff,%esi
0000000000000020    sete    %al
0000000000000023    repz/ret
0000000000000025    nop
0000000000000026    nopw    %cs:0x00000000(%rax,%rax)
_lenzero2:
0000000000000030    xorl    %eax,%eax
0000000000000032    cmpl    %edi,%esi
0000000000000034    setl    %al
0000000000000037    ret

Reply via email to