[LLVMbugs] [Bug 2191] New: missed optimization: b &= ~a

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Fri Apr 4 07:53:55 PDT 2008


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

           Summary: missed optimization: b &= ~a
           Product: new-bugs
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: edwintorok at gmail.com
                CC: llvmbugs at cs.uiuc.edu


Using llvm-gcc SVN r49213 on the simple testcase below generates code with
extra mov instructions.

unsigned sum(unsigned a, unsigned b)
{
        do {
                a ^= b;
                b &= ~a;
        } while (b <<= 1);
        return a;
}

This is the assembly generated, note the extra movl, and testl after the and:

sum:
        .align  16
.LBB1_1:        # bb
        xorl    %esi, %edi
        movl    %edi, %eax
        xorl    $2147483647, %eax
        andl    %esi, %eax
        addl    %eax, %eax
        testl   %eax, %eax
        movl    %eax, %esi
        jne     .LBB1_1 # bb
.LBB1_2:        # bb12
        movl    %edi, %eax
        ret
        .size   sum, .-sum

This should be a more optimized version (maybe xor could be changed to a not,
not sure):
sum:
    .align    16
.LBB1_1:    # bb
    xorl    %esi, %edi
    movl    %edi, %eax
    xorl    $2147483647, %eax
    andl    %eax, %esi
    addl    %esi, %esi
    jne    .LBB1_1    # bb
.LBB1_2:    # bb12
    movl    %edi, %eax
    ret
    .size    sum, .-sum

FWIW, gcc doesn't generate the optimized version either, here is what I get
with gcc-4.3 -O3:
sum:
        .align  16
.LBB1_1:        # bb
        xorl    %esi, %edi
        movl    %edi, %eax
        xorl    $2147483647, %eax
        andl    %esi, %eax
        addl    %eax, %eax
        testl   %eax, %eax
        movl    %eax, %esi
        jne     .LBB1_1 # bb
.LBB1_2:        # bb12
        movl    %edi, %


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