[LLVMbugs] [Bug 9814] New: optimizing integer power of 2 #2

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Apr 29 12:27:06 PDT 2011


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

           Summary: optimizing integer power of 2 #2
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: castet.matthieu at free.fr
                CC: llvmbugs at cs.uiuc.edu


Clang fails to optimize
int divu3(uint a, uint b)
{
        return a / ((1U<<b) / 4);
}

clang -S -Os -fomit-frame-pointer -mregparm=3 generate

divu30:                                 # @divu30
# BB#0:                                 # %entry
    movb    %dl, %cl
    movl    $1, %edx
    shll    %cl, %edx
    movl    %edx, %ecx
    sarl    $31, %ecx
    shrl    $30, %ecx
    addl    %edx, %ecx
    sarl    $2, %ecx
    xorl    %edx, %edx
    divl    %ecx, %eax
    ret

But because in C div by 0 is undefined in C ( C99
6.5.5p5), we have :
- (a / ((1U<<b) / 4)) and ((1U<<b) / 4) is not 0
- if ((1U<<b) / 4) is not 0, then it is equivalent to (1U << (b-2))
- so (a / ((1U<<b) / 4)) is equivalent to (a / (1U << (b-2)))
- so (a / ((1U<<b) / 4)) is equivalent to (a >> (b-2))

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