[LLVMbugs] [Bug 9802] New: Missed optimization: unnecessary branching (int version)

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Apr 27 19:30:42 PDT 2011


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

           Summary: Missed optimization: unnecessary branching (int
                    version)
           Product: libraries
           Version: trunk
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: jmuizelaar at mozilla.com
                CC: llvmbugs at cs.uiuc.edu


This is basically the integer version of bug 9661

llvm generates worse code for clamp() than clamp2().

int clamp(int a)
{
        if (a > 5) {
                return 5;
        } else if (a < 0) {
                return 0;
        } else {
                return a;
        }
}

int clamp2(int a)
{
        if (a > 5) {
                a = 5;
        }
        if (a < 0) {
                return 0;
        } else {
                return a;
        }
}


_clamp:                                 ## @clamp
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $5, %eax
        cmpl    $5, %edi
        jg      LBB0_2
        testl   %edi, %edi
        movl    $0, %eax
        cmovnsl %edi, %eax
LBB0_2:                                 ## %return
        popq    %rbp
        ret

_clamp2:                                ## @clamp2
        pushq   %rbp
        movq    %rsp, %rbp
        cmpl    $5, %edi
        movl    $5, %ecx
        cmovlel %edi, %ecx
        testl   %ecx, %ecx
        movl    $0, %eax
        cmovnsl %ecx, %eax
        popq    %rbp
        ret

gcc generates something similar to the clamp2 version in both cases.

-- 
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