[LLVMbugs] [Bug 12393] New: Missed optimization opportunity when using the ?: operator.

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Mar 28 22:34:46 PDT 2012


http://llvm.org/bugs/show_bug.cgi?id=12393

             Bug #: 12393
           Summary: Missed optimization opportunity when using the ?:
                    operator.
           Product: new-bugs
           Version: 3.0
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: brenthwalker at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified


The following seemingly identical functions, get compiled to quite
different machine code (one optimized, and the other not).  The first is
correctly optimized (the computation of var y is nicely moved into the else
branch of the "if" statement), which the second one is not (the full
computation of var y is always done).  The output was produced using the demo
page on llvm's web site (optimization level LTO).

A simple fix would be for you to lower ?: into an "if" in the front end and
since the "if" statement case is optimized you get the optimization for free.

=================================================
int f1(int x) {
  int y = 5*x*x*x+2*x*x+3*x+1;

  if (x > 0)
    return 0;
  else
     return y;
}

int f2(int x) {
  int y = 5*x*x*x+2*x*x+3*x+1;

  return x > 0 ? 0 : y;
}

=================================================

f1:                                     # @f1
.Ltmp0:
    .cfi_startproc
# BB#0:
    movl    4(%esp), %ecx
    testl    %ecx, %ecx
    jle    .LBB0_2
# BB#1:
    xorl    %eax, %eax
    ret
.LBB0_2:
    leal    2(%ecx,%ecx,4), %eax
    imull    %ecx, %eax
    addl    $3, %eax
    imull    %ecx, %eax
    incl    %eax
    ret
.Ltmp1:
    .size    f1, .Ltmp1-f1
.Ltmp2:
    .cfi_endproc
.Leh_func_end0:

    .globl    f2
    .align    16, 0x90
    .type    f2, at function
f2:                                     # @f2
.Ltmp3:
    .cfi_startproc
# BB#0:
    movl    4(%esp), %eax
    leal    2(%eax,%eax,4), %ecx
    imull    %eax, %ecx
    addl    $3, %ecx
    imull    %eax, %ecx
    incl    %ecx
    testl    %eax, %eax
    movl    $0, %eax
    cmovlel    %ecx, %eax
    ret

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list