[Bug tree-optimization/51938] New: missed optimization: 2 comparisons

marc.glisse at normalesup dot org gcc-bugzilla@gcc.gnu.org
Sat Jan 21 22:54:00 GMT 2012


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

             Bug #: 51938
           Summary: missed optimization: 2 comparisons
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: marc.glisse@normalesup.org


Hello,

this is possibly related to bug 28685, or to the bugs about the tracking of
flag register values.

void f();
enum Sign { NEG=-1, ZERO, POS };
enum Sign sign(double x){
    if(x>0) return POS;
    if(x<0) return NEG;
    return ZERO;
}
void g(double x){
    if(sign(x)==NEG) f();
}


Ideally, the compiler would realize that g is equivalent to:
if(x<0) f();

but on x86_64 I get with 4.6 (optimization -O3):

    xorpd    %xmm1, %xmm1
    ucomisd    %xmm1, %xmm0
    ja    .L8
    ucomisd    %xmm0, %xmm1
    jbe    .L8
    xorl    %eax, %eax
    jmp    f
.L8:
    rep
    ret

And with 4.7:

    xorpd    %xmm1, %xmm1
    ucomisd    %xmm1, %xmm0
    jbe    .L12
.L9:
    rep
    ret
.L12:
    ucomisd    %xmm0, %xmm1
    jbe    .L9
    xorl    %eax, %eax
    jmp    f

Other compilers: clang 2.9 does the optimization (it has only one ucomisd and
j*), but neither Intel 11.1 nor Oracle 5.12.

In my application, this optimization has a 5% impact on the total runtime.



More information about the Gcc-bugs mailing list