[LLVMbugs] [Bug 478] NEW: [x86] Ugly quality for a simple function

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Thu Dec 9 19:53:08 PST 2004


           Summary: [x86] Ugly quality for a simple function
           Product: libraries
           Version: 1.0
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: X86 Backend
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: sabre at nondot.org

Consider this simple function:

int f(int a, int b){
    return a * a + 2 * a * b + b * b;

We currently generate this X86 code for it:

        subl $4, %esp
        movl %esi, (%esp)
        movl 8(%esp), %ecx
        movl 12(%esp), %eax
        movl %ecx, %edx
        imull %edx, %edx
        movl %eax, %esi
        imull %ecx, %esi
        addl %esi, %esi
        imull %eax, %eax
        addl %edx, %eax
        addl %esi, %eax
        movl (%esp), %esi
        addl $4, %esp

... uh, yuck.

GCC generates this:
        movl    4(%esp), %eax
        movl    8(%esp), %edx
        movl    %eax, %ecx
        imull   %eax, %ecx
        imull   %edx, %eax
        imull   %edx, %edx
        leal    (%ecx,%eax,2), %eax
        addl    %edx, %eax

Which is much nicer.

It would be even better if we reassociated it into something like:

int f(int a, int b) {
    return a * (a + 2 * b) + b * b;

... which would save a multiply (this is an instcombine thing).

The PPC backend is producing nice code:

        mullw r2, r3, r3
        mullw r3, r4, r3
        rlwinm r3, r3, 1, 0, 30
        mullw r4, r4, r4
        add r2, r4, r2
        add r3, r2, r3

The X86 backend should do so too! :)


------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.

More information about the llvm-bugs mailing list