When factoring multiply expressions across adds, factor both
authorChris Lattner <sabre@nondot.org>
Fri, 1 Jan 2010 01:13:15 +0000 (01:13 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 1 Jan 2010 01:13:15 +0000 (01:13 +0000)
commit9506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15
tree1c030c28e100afacf6936be49286623afbd78c5b
parent75954e0bbd83d7f9f95ac82b46c92cb9eec25870
When factoring multiply expressions across adds, factor both
positive and negative forms of constants together.  This
allows us to compile:

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

into:

_foo:                                                       ## @foo
subl %esi, %edi
leal (%rdi,%rdi,2), %eax
ret

instead of (where the 3 and -3 were not factored):

_foo:
        imull   $-3, 8(%esp), %ecx
        imull   $3, 4(%esp), %eax
        addl    %ecx, %eax
        ret

this started out as:
    movl    12(%ebp), %ecx
    imull   $3, 8(%ebp), %eax
    subl    %ecx, %eax
    subl    %ecx, %eax
    subl    %ecx, %eax
    ret

This comes from PR5359.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92381 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/Scalar/Reassociate.cpp
test/Transforms/Reassociate/basictest.ll