[LLVMbugs] [Bug 4357] New: Crappy code generated for simple loop

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Wed Jun 10 10:43:59 PDT 2009


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

           Summary: Crappy code generated for simple loop
           Product: libraries
           Version: trunk
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Common Code Generator Code
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: evan.cheng at apple.com
                CC: llvmbugs at cs.uiuc.edu


int hcf(int a, int b)
{
        while (a != 0)
        {
                if (a < b) b -= a;
                else a -= b;
        }
        return b;
}

David Majnemer has a piece of code that shows less than optimal code generation
for llvm.

This is what icc generates:

8048624 <hcf>:
 8048624:       8b 54 24 04             mov    0x4(%esp),%edx
 8048628:       8b 44 24 08             mov    0x8(%esp),%eax
 804862c:       85 d2                   test   %edx,%edx
 804862e:       74 0c                   je     804863c <hcf+0x18>
 8048630:       3b d0                   cmp    %eax,%edx
 8048632:       7d 04                   jge    8048638 <hcf+0x14>
 8048634:       2b c2                   sub    %edx,%eax
 8048636:       eb f8                   jmp    8048630 <hcf+0xc>
 8048638:       2b d0                   sub    %eax,%edx
 804863a:       75 f4                   jne    8048630 <hcf+0xc>
 804863c:       c3                      ret

This is what llvm-gcc generates with -fomit-frame-pointer:

_hcf:
        movl    8(%esp), %eax
        movl    4(%esp), %ecx
        jmp     LBB1_3
LBB1_1:
        cmpl    %eax, %ecx
        jl      LBB1_5
        subl    %eax, %ecx
LBB1_3:
        testl   %ecx, %ecx
        jne     LBB1_1
        ret
LBB1_5:
        subl    %ecx, %eax
        jmp     LBB1_3


clang does something even more horrible:

_hcf:
        pushl   %esi
        movl    12(%esp), %eax
        movl    8(%esp), %ecx
        jmp     LBB1_5
        .align  4,0x90
LBB1_1:
        xorl    %edx, %edx
        movl    %eax, %esi
        jmp     LBB1_3
        .align  4,0x90
LBB1_2:
        subl    %ecx, %esi
        addl    %ecx, %edx
LBB1_3:
        cmpl    %esi, %ecx
        jl      LBB1_2
        addl    %edx, %ecx
        subl    %eax, %ecx
        movl    %esi, %eax
LBB1_5:
        testl   %ecx, %ecx
        jne     LBB1_1
        popl    %esi
        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