[LLVMbugs] [Bug 5359] New: Common subexpression and loop with integer incs

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Sat Oct 31 10:34:51 PDT 2009


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

           Summary: Common subexpression and loop with integer incs
           Product: new-bugs
           Version: 2.6
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Keywords: code-quality
          Severity: normal
          Priority: P2
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: bearophile at mailas.com
                CC: llvmbugs at cs.uiuc.edu


_sabre_ on IRC has said that it's OK to file a single bug report for this that
I have found.
Two reduced test cases written in C:

int foo(int x, int y) {
    return (x-y) + (x-y) + (x-y);
}

int spam(int n) {
    int sum = 0;
    int i, j, k;
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            for (k = 0; k < n; k++)
                sum++;
    return sum;
}


Below you can see the asm produced by gcc, llvm-gcc and ldc. I have shown them
all because the results differ. To me it seems the asm produced by llvm can be
improved.

I don't know why for the foo function the asm produced by ldc is better than
the asm produced by gcc-llvm. _sabre_ on IRC has answered "probably subtlety of
how lowering is happening".


C code compiled with GCC -O3 -S:
(GCC V.4.3.3-dw2-tdm-1)

_foo:
    pushl   %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %eax
    subl    12(%ebp), %eax
    popl    %ebp
    leal    (%eax,%eax,2), %eax
    ret


_spam:
    pushl   %ebp
    xorl    %eax, %eax
    movl    %esp, %ebp
    xorl    %edx, %edx
    pushl   %ebx
    movl    8(%ebp), %ebx
    testl   %ebx, %ebx
    jle L14
    movl    %ebx, %ecx
    imull   %ebx, %ecx
    .p2align 4,,7
L17:
    addl    $1, %edx
    addl    %ecx, %eax
    cmpl    %edx, %ebx
    jg  L17
L14:
    popl    %ebx
    popl    %ebp
    ret

----------------------------

C code compiled with LLVM-GCC -O3 -S:
(LLVM-GCC 2.6)

_foo:
    pushl   %ebp
    movl    %esp, %ebp
    movl    12(%ebp), %ecx
    imull   $3, 8(%ebp), %eax
    subl    %ecx, %eax
    subl    %ecx, %eax
    subl    %ecx, %eax
    popl    %ebp
    ret


_spam:
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %esi
    movl    8(%ebp), %ecx
    testl   %ecx, %ecx
    jg  LBB5_2
    xorl    %eax, %eax
    jmp LBB5_8
LBB5_2:
    xorl    %edx, %edx
    movl    %edx, %eax
    jmp LBB5_6
    .align  16
LBB5_3:
    incl    %esi
    cmpl    %ecx, %esi
    jne LBB5_3
    incl    %edx
    cmpl    %ecx, %edx
    je  LBB5_7
    imull   %ecx, %esi
    addl    %esi, %eax
LBB5_6:
    xorl    %esi, %esi
    jmp LBB5_3
LBB5_7:
    imull   %ecx, %esi
    addl    %esi, %eax
LBB5_8:
    popl    %esi
    popl    %ebp
    ret

----------------------------

The same code can be compiled by the D compiler too:

D code compiled with LDC, -O3 -release -output-s:
(LDC based on DMD v1.045 and llvm 2.6, Thu Sep 10 23:50:27 2009)

_D4test3fooFiiZi:
    movl    4(%esp), %ecx
    subl    %eax, %ecx
    leal    (%ecx,%ecx,2), %eax
    ret $4


_D4test4spamFiZi:
    pushl   %esi
    testl   %eax, %eax
    jle .LBB2_7
    xorl    %edx, %edx
    movl    %edx, %ecx
.LBB2_2:
    xorl    %esi, %esi
    .align  16
.LBB2_3:
    incl    %esi
    cmpl    %eax, %esi
    jne .LBB2_3
    incl    %edx
    cmpl    %eax, %edx
    jne .LBB2_8
    imull   %eax, %esi
    addl    %esi, %ecx
.LBB2_6:
    movl    %ecx, %eax
    popl    %esi
    ret
.LBB2_7:
    xorl    %ecx, %ecx
    jmp .LBB2_6
.LBB2_8:
    imull   %eax, %esi
    addl    %esi, %ecx
    jmp .LBB2_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