aboutsummaryrefslogtreecommitdiffstats
path: root/test/Transforms/Reassociate
diff options
context:
space:
mode:
authorShih-wei Liao <sliao@google.com>2010-02-10 11:10:31 -0800
committerShih-wei Liao <sliao@google.com>2010-02-10 11:10:31 -0800
commite264f62ca09a8f65c87a46d562a4d0f9ec5d457e (patch)
tree59e3d57ef656cef79afa708ae0a3daf25cd91fcf /test/Transforms/Reassociate
downloadexternal_llvm-e264f62ca09a8f65c87a46d562a4d0f9ec5d457e.zip
external_llvm-e264f62ca09a8f65c87a46d562a4d0f9ec5d457e.tar.gz
external_llvm-e264f62ca09a8f65c87a46d562a4d0f9ec5d457e.tar.bz2
Check in LLVM r95781.
Diffstat (limited to 'test/Transforms/Reassociate')
-rw-r--r--test/Transforms/Reassociate/2002-05-15-AgressiveSubMove.ll9
-rw-r--r--test/Transforms/Reassociate/2002-05-15-MissedTree.ll9
-rw-r--r--test/Transforms/Reassociate/2002-05-15-SubReassociate.ll12
-rw-r--r--test/Transforms/Reassociate/2002-05-15-SubReassociate2.ll13
-rw-r--r--test/Transforms/Reassociate/2002-07-09-DominanceProblem.ll10
-rw-r--r--test/Transforms/Reassociate/2003-08-12-InfiniteLoop.ll9
-rw-r--r--test/Transforms/Reassociate/2005-08-24-Crash.ll13
-rw-r--r--test/Transforms/Reassociate/2005-09-01-ArrayOutOfBounds.ll23
-rw-r--r--test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll8
-rw-r--r--test/Transforms/Reassociate/basictest.ll216
-rw-r--r--test/Transforms/Reassociate/crash.ll33
-rw-r--r--test/Transforms/Reassociate/dg.exp3
-rw-r--r--test/Transforms/Reassociate/inverses.ll34
-rw-r--r--test/Transforms/Reassociate/looptest.ll50
-rw-r--r--test/Transforms/Reassociate/mulfactor.ll14
-rw-r--r--test/Transforms/Reassociate/mulfactor2.ll15
-rw-r--r--test/Transforms/Reassociate/negation.ll21
-rw-r--r--test/Transforms/Reassociate/otherops.ll28
-rw-r--r--test/Transforms/Reassociate/shift-factor.ll12
-rw-r--r--test/Transforms/Reassociate/shifttest.ll12
-rw-r--r--test/Transforms/Reassociate/subtest.ll11
-rw-r--r--test/Transforms/Reassociate/subtest2.ll13
22 files changed, 568 insertions, 0 deletions
diff --git a/test/Transforms/Reassociate/2002-05-15-AgressiveSubMove.ll b/test/Transforms/Reassociate/2002-05-15-AgressiveSubMove.ll
new file mode 100644
index 0000000..5780990
--- /dev/null
+++ b/test/Transforms/Reassociate/2002-05-15-AgressiveSubMove.ll
@@ -0,0 +1,9 @@
+; RUN: opt < %s -reassociate -instcombine -constprop -dce -S | not grep add
+
+define i32 @test(i32 %A) {
+ %X = add i32 %A, 1 ; <i32> [#uses=1]
+ %Y = add i32 %A, 1 ; <i32> [#uses=1]
+ %r = sub i32 %X, %Y ; <i32> [#uses=1]
+ ret i32 %r
+}
+
diff --git a/test/Transforms/Reassociate/2002-05-15-MissedTree.ll b/test/Transforms/Reassociate/2002-05-15-MissedTree.ll
new file mode 100644
index 0000000..e8bccbd
--- /dev/null
+++ b/test/Transforms/Reassociate/2002-05-15-MissedTree.ll
@@ -0,0 +1,9 @@
+; RUN: opt < %s -reassociate -instcombine -constprop -die -S | not grep 5
+
+define i32 @test(i32 %A, i32 %B) {
+ %W = add i32 %B, -5 ; <i32> [#uses=1]
+ %Y = add i32 %A, 5 ; <i32> [#uses=1]
+ %Z = add i32 %W, %Y ; <i32> [#uses=1]
+ ret i32 %Z
+}
+
diff --git a/test/Transforms/Reassociate/2002-05-15-SubReassociate.ll b/test/Transforms/Reassociate/2002-05-15-SubReassociate.ll
new file mode 100644
index 0000000..c18af5e
--- /dev/null
+++ b/test/Transforms/Reassociate/2002-05-15-SubReassociate.ll
@@ -0,0 +1,12 @@
+; With sub reassociation, constant folding can eliminate all of the constants.
+;
+; RUN: opt < %s -reassociate -constprop -instcombine -dce -S | not grep add
+
+define i32 @test(i32 %A, i32 %B) {
+ %W = add i32 5, %B ; <i32> [#uses=1]
+ %X = add i32 -7, %A ; <i32> [#uses=1]
+ %Y = sub i32 %X, %W ; <i32> [#uses=1]
+ %Z = add i32 %Y, 12 ; <i32> [#uses=1]
+ ret i32 %Z
+}
+
diff --git a/test/Transforms/Reassociate/2002-05-15-SubReassociate2.ll b/test/Transforms/Reassociate/2002-05-15-SubReassociate2.ll
new file mode 100644
index 0000000..5848821
--- /dev/null
+++ b/test/Transforms/Reassociate/2002-05-15-SubReassociate2.ll
@@ -0,0 +1,13 @@
+; With sub reassociation, constant folding can eliminate the two 12 constants.
+;
+; RUN: opt < %s -reassociate -constprop -dce -S | not grep 12
+
+define i32 @test(i32 %A, i32 %B, i32 %C, i32 %D) {
+ %M = add i32 %A, 12 ; <i32> [#uses=1]
+ %N = add i32 %M, %B ; <i32> [#uses=1]
+ %O = add i32 %N, %C ; <i32> [#uses=1]
+ %P = sub i32 %D, %O ; <i32> [#uses=1]
+ %Q = add i32 %P, 12 ; <i32> [#uses=1]
+ ret i32 %Q
+}
+
diff --git a/test/Transforms/Reassociate/2002-07-09-DominanceProblem.ll b/test/Transforms/Reassociate/2002-07-09-DominanceProblem.ll
new file mode 100644
index 0000000..bbb08f9
--- /dev/null
+++ b/test/Transforms/Reassociate/2002-07-09-DominanceProblem.ll
@@ -0,0 +1,10 @@
+; The reassociate pass is not preserving dominance properties correctly
+;
+; RUN: opt < %s -reassociate
+
+define i32 @compute_dist(i32 %i, i32 %j) {
+ %reg119 = sub i32 %j, %i ; <i32> [#uses=1]
+ ret i32 %reg119
+}
+
+
diff --git a/test/Transforms/Reassociate/2003-08-12-InfiniteLoop.ll b/test/Transforms/Reassociate/2003-08-12-InfiniteLoop.ll
new file mode 100644
index 0000000..af7a821
--- /dev/null
+++ b/test/Transforms/Reassociate/2003-08-12-InfiniteLoop.ll
@@ -0,0 +1,9 @@
+; RUN: opt < %s -reassociate -disable-output
+
+define i32 @test(i32 %A.1, i32 %B.1, i32 %C.1, i32 %D.1) {
+ %tmp.16 = and i32 %A.1, %B.1 ; <i32> [#uses=1]
+ %tmp.18 = and i32 %tmp.16, %C.1 ; <i32> [#uses=1]
+ %tmp.20 = and i32 %tmp.18, %D.1 ; <i32> [#uses=1]
+ ret i32 %tmp.20
+}
+
diff --git a/test/Transforms/Reassociate/2005-08-24-Crash.ll b/test/Transforms/Reassociate/2005-08-24-Crash.ll
new file mode 100644
index 0000000..9864de4
--- /dev/null
+++ b/test/Transforms/Reassociate/2005-08-24-Crash.ll
@@ -0,0 +1,13 @@
+; RUN: opt < %s -reassociate -disable-output
+
+define void @test(i32 %a, i32 %b, i32 %c, i32 %d) {
+ %tmp.2 = xor i32 %a, %b ; <i32> [#uses=1]
+ %tmp.5 = xor i32 %c, %d ; <i32> [#uses=1]
+ %tmp.6 = xor i32 %tmp.2, %tmp.5 ; <i32> [#uses=1]
+ %tmp.9 = xor i32 %c, %a ; <i32> [#uses=1]
+ %tmp.12 = xor i32 %b, %d ; <i32> [#uses=1]
+ %tmp.13 = xor i32 %tmp.9, %tmp.12 ; <i32> [#uses=1]
+ %tmp.16 = xor i32 %tmp.6, %tmp.13 ; <i32> [#uses=0]
+ ret void
+}
+
diff --git a/test/Transforms/Reassociate/2005-09-01-ArrayOutOfBounds.ll b/test/Transforms/Reassociate/2005-09-01-ArrayOutOfBounds.ll
new file mode 100644
index 0000000..33e44d4
--- /dev/null
+++ b/test/Transforms/Reassociate/2005-09-01-ArrayOutOfBounds.ll
@@ -0,0 +1,23 @@
+; RUN: opt < %s -reassociate -instcombine -S |\
+; RUN: grep {ret i32 0}
+
+define i32 @f(i32 %a0, i32 %a1, i32 %a2, i32 %a3, i32 %a4) {
+ %tmp.2 = add i32 %a4, %a3 ; <i32> [#uses=1]
+ %tmp.4 = add i32 %tmp.2, %a2 ; <i32> [#uses=1]
+ %tmp.6 = add i32 %tmp.4, %a1 ; <i32> [#uses=1]
+ %tmp.8 = add i32 %tmp.6, %a0 ; <i32> [#uses=1]
+ %tmp.11 = add i32 %a3, %a2 ; <i32> [#uses=1]
+ %tmp.13 = add i32 %tmp.11, %a1 ; <i32> [#uses=1]
+ %tmp.15 = add i32 %tmp.13, %a0 ; <i32> [#uses=1]
+ %tmp.18 = add i32 %a2, %a1 ; <i32> [#uses=1]
+ %tmp.20 = add i32 %tmp.18, %a0 ; <i32> [#uses=1]
+ %tmp.23 = add i32 %a1, %a0 ; <i32> [#uses=1]
+ %tmp.26 = sub i32 %tmp.8, %tmp.15 ; <i32> [#uses=1]
+ %tmp.28 = add i32 %tmp.26, %tmp.20 ; <i32> [#uses=1]
+ %tmp.30 = sub i32 %tmp.28, %tmp.23 ; <i32> [#uses=1]
+ %tmp.32 = sub i32 %tmp.30, %a4 ; <i32> [#uses=1]
+ %tmp.34 = sub i32 %tmp.32, %a2 ; <i32> [#uses=2]
+ %T = mul i32 %tmp.34, %tmp.34 ; <i32> [#uses=1]
+ ret i32 %T
+}
+
diff --git a/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll b/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll
new file mode 100644
index 0000000..384cbc9
--- /dev/null
+++ b/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll
@@ -0,0 +1,8 @@
+; RUN: opt < %s -reassociate -disable-output
+
+define void @foo() {
+ %tmp162 = fsub <4 x float> zeroinitializer, zeroinitializer ; <<4 x float>> [#uses=1]
+ %tmp164 = fmul <4 x float> zeroinitializer, %tmp162 ; <<4 x float>> [#uses=0]
+ ret void
+}
+
diff --git a/test/Transforms/Reassociate/basictest.ll b/test/Transforms/Reassociate/basictest.ll
new file mode 100644
index 0000000..0864740
--- /dev/null
+++ b/test/Transforms/Reassociate/basictest.ll
@@ -0,0 +1,216 @@
+; With reassociation, constant folding can eliminate the 12 and -12 constants.
+;
+; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s
+
+define i32 @test1(i32 %arg) {
+ %tmp1 = sub i32 -12, %arg
+ %tmp2 = add i32 %tmp1, 12
+ ret i32 %tmp2
+; CHECK: @test1
+; CHECK-NEXT: sub i32 0, %arg
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test2(i32 %reg109, i32 %reg1111) {
+ %reg115 = add i32 %reg109, -30 ; <i32> [#uses=1]
+ %reg116 = add i32 %reg115, %reg1111 ; <i32> [#uses=1]
+ %reg117 = add i32 %reg116, 30 ; <i32> [#uses=1]
+ ret i32 %reg117
+; CHECK: @test2
+; CHECK-NEXT: add i32 %reg1111, %reg109
+; CHECK-NEXT: ret i32
+}
+
+@e = external global i32 ; <i32*> [#uses=3]
+@a = external global i32 ; <i32*> [#uses=3]
+@b = external global i32 ; <i32*> [#uses=3]
+@c = external global i32 ; <i32*> [#uses=3]
+@f = external global i32 ; <i32*> [#uses=3]
+
+define void @test3() {
+ %A = load i32* @a ; <i32> [#uses=2]
+ %B = load i32* @b ; <i32> [#uses=2]
+ %C = load i32* @c ; <i32> [#uses=2]
+ %t1 = add i32 %A, %B ; <i32> [#uses=1]
+ %t2 = add i32 %t1, %C ; <i32> [#uses=1]
+ %t3 = add i32 %C, %A ; <i32> [#uses=1]
+ %t4 = add i32 %t3, %B ; <i32> [#uses=1]
+ ; e = (a+b)+c;
+ store i32 %t2, i32* @e
+ ; f = (a+c)+b
+ store i32 %t4, i32* @f
+ ret void
+; CHECK: @test3
+; CHECK: add i32
+; CHECK: add i32
+; CHECK-NOT: add i32
+; CHECK: ret void
+}
+
+define void @test4() {
+ %A = load i32* @a ; <i32> [#uses=2]
+ %B = load i32* @b ; <i32> [#uses=2]
+ %C = load i32* @c ; <i32> [#uses=2]
+ %t1 = add i32 %A, %B ; <i32> [#uses=1]
+ %t2 = add i32 %t1, %C ; <i32> [#uses=1]
+ %t3 = add i32 %C, %A ; <i32> [#uses=1]
+ %t4 = add i32 %t3, %B ; <i32> [#uses=1]
+ ; e = c+(a+b)
+ store i32 %t2, i32* @e
+ ; f = (c+a)+b
+ store i32 %t4, i32* @f
+ ret void
+; CHECK: @test4
+; CHECK: add i32
+; CHECK: add i32
+; CHECK-NOT: add i32
+; CHECK: ret void
+}
+
+define void @test5() {
+ %A = load i32* @a ; <i32> [#uses=2]
+ %B = load i32* @b ; <i32> [#uses=2]
+ %C = load i32* @c ; <i32> [#uses=2]
+ %t1 = add i32 %B, %A ; <i32> [#uses=1]
+ %t2 = add i32 %t1, %C ; <i32> [#uses=1]
+ %t3 = add i32 %C, %A ; <i32> [#uses=1]
+ %t4 = add i32 %t3, %B ; <i32> [#uses=1]
+ ; e = c+(b+a)
+ store i32 %t2, i32* @e
+ ; f = (c+a)+b
+ store i32 %t4, i32* @f
+ ret void
+; CHECK: @test5
+; CHECK: add i32
+; CHECK: add i32
+; CHECK-NOT: add i32
+; CHECK: ret void
+}
+
+define i32 @test6() {
+ %tmp.0 = load i32* @a
+ %tmp.1 = load i32* @b
+ ; (a+b)
+ %tmp.2 = add i32 %tmp.0, %tmp.1
+ %tmp.4 = load i32* @c
+ ; (a+b)+c
+ %tmp.5 = add i32 %tmp.2, %tmp.4
+ ; (a+c)
+ %tmp.8 = add i32 %tmp.0, %tmp.4
+ ; (a+c)+b
+ %tmp.11 = add i32 %tmp.8, %tmp.1
+ ; X ^ X = 0
+ %RV = xor i32 %tmp.5, %tmp.11
+ ret i32 %RV
+; CHECK: @test6
+; CHECK: ret i32 0
+}
+
+; This should be one add and two multiplies.
+define i32 @test7(i32 %A, i32 %B, i32 %C) {
+ ; A*A*B + A*C*A
+ %aa = mul i32 %A, %A
+ %aab = mul i32 %aa, %B
+ %ac = mul i32 %A, %C
+ %aac = mul i32 %ac, %A
+ %r = add i32 %aab, %aac
+ ret i32 %r
+; CHECK: @test7
+; CHECK-NEXT: add i32 %C, %B
+; CHECK-NEXT: mul i32
+; CHECK-NEXT: mul i32
+; CHECK-NEXT: ret i32
+}
+
+
+define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
+ %A = sub i32 0, %X
+ %B = mul i32 %A, %Y
+ ; (-X)*Y + Z -> Z-X*Y
+ %C = add i32 %B, %Z
+ ret i32 %C
+; CHECK: @test8
+; CHECK-NEXT: %A = mul i32 %Y, %X
+; CHECK-NEXT: %C = sub i32 %Z, %A
+; CHECK-NEXT: ret i32 %C
+}
+
+
+; PR5458
+define i32 @test9(i32 %X) {
+ %Y = mul i32 %X, 47
+ %Z = add i32 %Y, %Y
+ ret i32 %Z
+; CHECK: @test9
+; CHECK-NEXT: mul i32 %X, 94
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test10(i32 %X) {
+ %Y = add i32 %X ,%X
+ %Z = add i32 %Y, %X
+ ret i32 %Z
+; CHECK: @test10
+; CHECK-NEXT: mul i32 %X, 3
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test11(i32 %W) {
+ %X = mul i32 %W, 127
+ %Y = add i32 %X ,%X
+ %Z = add i32 %Y, %X
+ ret i32 %Z
+; CHECK: @test11
+; CHECK-NEXT: mul i32 %W, 381
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test12(i32 %X) {
+ %A = sub i32 1, %X
+ %B = sub i32 2, %X
+ %C = sub i32 3, %X
+
+ %Y = add i32 %A ,%B
+ %Z = add i32 %Y, %C
+ ret i32 %Z
+; CHECK: @test12
+; CHECK-NEXT: mul i32 %X, -3
+; CHECK-NEXT: add i32{{.*}}, 6
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test13(i32 %X1, i32 %X2, i32 %X3) {
+ %A = sub i32 0, %X1
+ %B = mul i32 %A, %X2 ; -X1*X2
+ %C = mul i32 %X1, %X3 ; X1*X3
+ %D = add i32 %B, %C ; -X1*X2 + X1*X3 -> X1*(X3-X2)
+ ret i32 %D
+; CHECK: @test13
+; CHECK-NEXT: sub i32 %X3, %X2
+; CHECK-NEXT: mul i32 {{.*}}, %X1
+; CHECK-NEXT: ret i32
+}
+
+; PR5359
+define i32 @test14(i32 %X1, i32 %X2) {
+ %B = mul i32 %X1, 47 ; X1*47
+ %C = mul i32 %X2, -47 ; X2*-47
+ %D = add i32 %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2)
+ ret i32 %D
+; CHECK: @test14
+; CHECK-NEXT: sub i32 %X1, %X2
+; CHECK-NEXT: mul i32 {{.*}}, 47
+; CHECK-NEXT: ret i32
+}
+
+; Do not reassociate expressions of type i1
+define i32 @test15(i32 %X1, i32 %X2, i32 %X3) {
+ %A = icmp ne i32 %X1, 0
+ %B = icmp slt i32 %X2, %X3
+ %C = and i1 %A, %B
+ %D = select i1 %C, i32 %X1, i32 0
+ ret i32 %D
+; CHECK: @test15
+; CHECK: and i1 %A, %B
+}
+
diff --git a/test/Transforms/Reassociate/crash.ll b/test/Transforms/Reassociate/crash.ll
new file mode 100644
index 0000000..060018d
--- /dev/null
+++ b/test/Transforms/Reassociate/crash.ll
@@ -0,0 +1,33 @@
+; RUN: opt -reassociate -disable-output %s
+
+
+; rdar://7507855
+define fastcc i32 @test1() nounwind {
+entry:
+ %cond = select i1 undef, i32 1, i32 -1 ; <i32> [#uses=2]
+ br label %for.cond
+
+for.cond: ; preds = %for.body, %entry
+ %sub889 = sub i32 undef, undef ; <i32> [#uses=1]
+ %sub891 = sub i32 %sub889, %cond ; <i32> [#uses=0]
+ %add896 = sub i32 0, %cond ; <i32> [#uses=0]
+ ret i32 undef
+}
+
+; PR5981
+define i32 @test2() nounwind ssp {
+entry:
+ %0 = load i32* undef, align 4
+ %1 = mul nsw i32 undef, %0
+ %2 = mul nsw i32 undef, %0
+ %3 = add nsw i32 undef, %1
+ %4 = add nsw i32 %3, %2
+ %5 = add nsw i32 %4, 4
+ %6 = shl i32 %0, 3 ; <i32> [#uses=1]
+ %7 = add nsw i32 %5, %6
+ br label %bb4.i9
+
+bb4.i9: ; preds = %bb3.i7, %bb1.i25.i
+ %8 = add nsw i32 undef, %1
+ ret i32 0
+}
diff --git a/test/Transforms/Reassociate/dg.exp b/test/Transforms/Reassociate/dg.exp
new file mode 100644
index 0000000..f200589
--- /dev/null
+++ b/test/Transforms/Reassociate/dg.exp
@@ -0,0 +1,3 @@
+load_lib llvm.exp
+
+RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll,c,cpp}]]
diff --git a/test/Transforms/Reassociate/inverses.ll b/test/Transforms/Reassociate/inverses.ll
new file mode 100644
index 0000000..34abdc7
--- /dev/null
+++ b/test/Transforms/Reassociate/inverses.ll
@@ -0,0 +1,34 @@
+; RUN: opt < %s -reassociate -die -S | FileCheck %s
+
+define i32 @test1(i32 %a, i32 %b) {
+ %tmp.2 = and i32 %b, %a
+ %tmp.4 = xor i32 %a, -1
+ ; (A&B)&~A == 0
+ %tmp.5 = and i32 %tmp.2, %tmp.4
+ ret i32 %tmp.5
+; CHECK: @test1
+; CHECK: ret i32 0
+}
+
+define i32 @test2(i32 %a, i32 %b) {
+ %tmp.1 = and i32 %a, 1234
+ %tmp.2 = and i32 %b, %tmp.1
+ %tmp.4 = xor i32 %a, -1
+ ; A&~A == 0
+ %tmp.5 = and i32 %tmp.2, %tmp.4
+ ret i32 %tmp.5
+; CHECK: @test2
+; CHECK: ret i32 0
+}
+
+define i32 @test3(i32 %b, i32 %a) {
+ %tmp.1 = add i32 %a, 1234
+ %tmp.2 = add i32 %b, %tmp.1
+ %tmp.4 = sub i32 0, %a
+ ; (b+(a+1234))+-a -> b+1234
+ %tmp.5 = add i32 %tmp.2, %tmp.4
+ ret i32 %tmp.5
+; CHECK: @test3
+; CHECK: %tmp.5 = add i32 %b, 1234
+; CHECK: ret i32 %tmp.5
+}
diff --git a/test/Transforms/Reassociate/looptest.ll b/test/Transforms/Reassociate/looptest.ll
new file mode 100644
index 0000000..91723bc
--- /dev/null
+++ b/test/Transforms/Reassociate/looptest.ll
@@ -0,0 +1,50 @@
+; This testcase comes from this C fragment:
+;
+; void test(unsigned Num, int *Array) {
+; unsigned i, j, k;
+;
+; for (i = 0; i != Num; ++i)
+; for (j = 0; j != Num; ++j)
+; for (k = 0; k != Num; ++k)
+; printf("%d\n", i+k+j); /* Reassociate to (i+j)+k */
+;}
+;
+; In this case, we want to reassociate the specified expr so that i+j can be
+; hoisted out of the inner most loop.
+;
+; RUN: opt < %s -reassociate -S | grep 115 | not grep 117
+; END.
+@.LC0 = internal global [4 x i8] c"%d\0A\00" ; <[4 x i8]*> [#uses=1]
+
+declare i32 @printf(i8*, ...)
+
+define void @test(i32 %Num, i32* %Array) {
+bb0:
+ %cond221 = icmp eq i32 0, %Num ; <i1> [#uses=3]
+ br i1 %cond221, label %bb7, label %bb2
+bb2: ; preds = %bb6, %bb0
+ %reg115 = phi i32 [ %reg120, %bb6 ], [ 0, %bb0 ] ; <i32> [#uses=2]
+ br i1 %cond221, label %bb6, label %bb3
+bb3: ; preds = %bb5, %bb2
+ %reg116 = phi i32 [ %reg119, %bb5 ], [ 0, %bb2 ] ; <i32> [#uses=2]
+ br i1 %cond221, label %bb5, label %bb4
+bb4: ; preds = %bb4, %bb3
+ %reg117 = phi i32 [ %reg118, %bb4 ], [ 0, %bb3 ] ; <i32> [#uses=2]
+ %reg113 = add i32 %reg115, %reg117 ; <i32> [#uses=1]
+ %reg114 = add i32 %reg113, %reg116 ; <i32> [#uses=1]
+ %cast227 = getelementptr [4 x i8]* @.LC0, i64 0, i64 0 ; <i8*> [#uses=1]
+ call i32 (i8*, ...)* @printf( i8* %cast227, i32 %reg114 ) ; <i32>:0 [#uses=0]
+ %reg118 = add i32 %reg117, 1 ; <i32> [#uses=2]
+ %cond224 = icmp ne i32 %reg118, %Num ; <i1> [#uses=1]
+ br i1 %cond224, label %bb4, label %bb5
+bb5: ; preds = %bb4, %bb3
+ %reg119 = add i32 %reg116, 1 ; <i32> [#uses=2]
+ %cond225 = icmp ne i32 %reg119, %Num ; <i1> [#uses=1]
+ br i1 %cond225, label %bb3, label %bb6
+bb6: ; preds = %bb5, %bb2
+ %reg120 = add i32 %reg115, 1 ; <i32> [#uses=2]
+ %cond226 = icmp ne i32 %reg120, %Num ; <i1> [#uses=1]
+ br i1 %cond226, label %bb2, label %bb7
+bb7: ; preds = %bb6, %bb0
+ ret void
+}
diff --git a/test/Transforms/Reassociate/mulfactor.ll b/test/Transforms/Reassociate/mulfactor.ll
new file mode 100644
index 0000000..f279727
--- /dev/null
+++ b/test/Transforms/Reassociate/mulfactor.ll
@@ -0,0 +1,14 @@
+; RUN: opt < %s -reassociate -instcombine -S | grep mul | count 2
+
+; This should have exactly 2 multiplies when we're done.
+
+define i32 @f(i32 %a, i32 %b) {
+ %tmp.2 = mul i32 %a, %a ; <i32> [#uses=1]
+ %tmp.5 = shl i32 %a, 1 ; <i32> [#uses=1]
+ %tmp.6 = mul i32 %tmp.5, %b ; <i32> [#uses=1]
+ %tmp.10 = mul i32 %b, %b ; <i32> [#uses=1]
+ %tmp.7 = add i32 %tmp.6, %tmp.2 ; <i32> [#uses=1]
+ %tmp.11 = add i32 %tmp.7, %tmp.10 ; <i32> [#uses=1]
+ ret i32 %tmp.11
+}
+
diff --git a/test/Transforms/Reassociate/mulfactor2.ll b/test/Transforms/Reassociate/mulfactor2.ll
new file mode 100644
index 0000000..8116554
--- /dev/null
+++ b/test/Transforms/Reassociate/mulfactor2.ll
@@ -0,0 +1,15 @@
+; This should turn into one multiply and one add.
+
+; RUN: opt < %s -instcombine -reassociate -instcombine -S > %t
+; RUN: grep mul %t | count 1
+; RUN: grep add %t | count 1
+
+define i32 @main(i32 %t) {
+ %tmp.3 = mul i32 %t, 12 ; <i32> [#uses=1]
+ %tmp.4 = add i32 %tmp.3, 5 ; <i32> [#uses=1]
+ %tmp.6 = mul i32 %t, 6 ; <i32> [#uses=1]
+ %tmp.8 = mul i32 %tmp.4, 3 ; <i32> [#uses=1]
+ %tmp.9 = add i32 %tmp.8, %tmp.6 ; <i32> [#uses=1]
+ ret i32 %tmp.9
+}
+
diff --git a/test/Transforms/Reassociate/negation.ll b/test/Transforms/Reassociate/negation.ll
new file mode 100644
index 0000000..6a3dfd3
--- /dev/null
+++ b/test/Transforms/Reassociate/negation.ll
@@ -0,0 +1,21 @@
+; RUN: opt < %s -reassociate -instcombine -S | not grep sub
+
+; Test that we can turn things like X*-(Y*Z) -> X*-1*Y*Z.
+
+define i32 @test1(i32 %a, i32 %b, i32 %z) {
+ %c = sub i32 0, %z ; <i32> [#uses=1]
+ %d = mul i32 %a, %b ; <i32> [#uses=1]
+ %e = mul i32 %c, %d ; <i32> [#uses=1]
+ %f = mul i32 %e, 12345 ; <i32> [#uses=1]
+ %g = sub i32 0, %f ; <i32> [#uses=1]
+ ret i32 %g
+}
+
+define i32 @test2(i32 %a, i32 %b, i32 %z) {
+ %d = mul i32 %z, 40 ; <i32> [#uses=1]
+ %c = sub i32 0, %d ; <i32> [#uses=1]
+ %e = mul i32 %a, %c ; <i32> [#uses=1]
+ %f = sub i32 0, %e ; <i32> [#uses=1]
+ ret i32 %f
+}
+
diff --git a/test/Transforms/Reassociate/otherops.ll b/test/Transforms/Reassociate/otherops.ll
new file mode 100644
index 0000000..d68d008
--- /dev/null
+++ b/test/Transforms/Reassociate/otherops.ll
@@ -0,0 +1,28 @@
+; Reassociation should apply to Add, Mul, And, Or, & Xor
+;
+; RUN: opt < %s -reassociate -constprop -instcombine -die -S | not grep 12
+
+define i32 @test_mul(i32 %arg) {
+ %tmp1 = mul i32 12, %arg ; <i32> [#uses=1]
+ %tmp2 = mul i32 %tmp1, 12 ; <i32> [#uses=1]
+ ret i32 %tmp2
+}
+
+define i32 @test_and(i32 %arg) {
+ %tmp1 = and i32 14, %arg ; <i32> [#uses=1]
+ %tmp2 = and i32 %tmp1, 14 ; <i32> [#uses=1]
+ ret i32 %tmp2
+}
+
+define i32 @test_or(i32 %arg) {
+ %tmp1 = or i32 14, %arg ; <i32> [#uses=1]
+ %tmp2 = or i32 %tmp1, 14 ; <i32> [#uses=1]
+ ret i32 %tmp2
+}
+
+define i32 @test_xor(i32 %arg) {
+ %tmp1 = xor i32 12, %arg ; <i32> [#uses=1]
+ %tmp2 = xor i32 %tmp1, 12 ; <i32> [#uses=1]
+ ret i32 %tmp2
+}
+
diff --git a/test/Transforms/Reassociate/shift-factor.ll b/test/Transforms/Reassociate/shift-factor.ll
new file mode 100644
index 0000000..73af5e5
--- /dev/null
+++ b/test/Transforms/Reassociate/shift-factor.ll
@@ -0,0 +1,12 @@
+; There should be exactly one shift and one add left.
+; RUN: opt < %s -reassociate -instcombine -S > %t
+; RUN: grep shl %t | count 1
+; RUN: grep add %t | count 1
+
+define i32 @test(i32 %X, i32 %Y) {
+ %tmp.2 = shl i32 %X, 1 ; <i32> [#uses=1]
+ %tmp.6 = shl i32 %Y, 1 ; <i32> [#uses=1]
+ %tmp.4 = add i32 %tmp.6, %tmp.2 ; <i32> [#uses=1]
+ ret i32 %tmp.4
+}
+
diff --git a/test/Transforms/Reassociate/shifttest.ll b/test/Transforms/Reassociate/shifttest.ll
new file mode 100644
index 0000000..8b2cbc9
--- /dev/null
+++ b/test/Transforms/Reassociate/shifttest.ll
@@ -0,0 +1,12 @@
+; With shl->mul reassociation, we can see that this is (shl A, 9) * A
+;
+; RUN: opt < %s -reassociate -instcombine -S |\
+; RUN: grep {shl .*, 9}
+
+define i32 @test(i32 %A, i32 %B) {
+ %X = shl i32 %A, 5 ; <i32> [#uses=1]
+ %Y = shl i32 %A, 4 ; <i32> [#uses=1]
+ %Z = mul i32 %Y, %X ; <i32> [#uses=1]
+ ret i32 %Z
+}
+
diff --git a/test/Transforms/Reassociate/subtest.ll b/test/Transforms/Reassociate/subtest.ll
new file mode 100644
index 0000000..4c63d12
--- /dev/null
+++ b/test/Transforms/Reassociate/subtest.ll
@@ -0,0 +1,11 @@
+; With sub reassociation, constant folding can eliminate the 12 and -12 constants.
+;
+; RUN: opt < %s -reassociate -instcombine -S | not grep 12
+
+define i32 @test(i32 %A, i32 %B) {
+ %X = add i32 -12, %A ; <i32> [#uses=1]
+ %Y = sub i32 %X, %B ; <i32> [#uses=1]
+ %Z = add i32 %Y, 12 ; <i32> [#uses=1]
+ ret i32 %Z
+}
+
diff --git a/test/Transforms/Reassociate/subtest2.ll b/test/Transforms/Reassociate/subtest2.ll
new file mode 100644
index 0000000..0513c5f
--- /dev/null
+++ b/test/Transforms/Reassociate/subtest2.ll
@@ -0,0 +1,13 @@
+; With sub reassociation, constant folding can eliminate the uses of %a.
+;
+; RUN: opt < %s -reassociate -instcombine -S | grep %a | count 1
+; PR2047
+
+define i32 @test(i32 %a, i32 %b, i32 %c) nounwind {
+entry:
+ %tmp3 = sub i32 %a, %b ; <i32> [#uses=1]
+ %tmp5 = sub i32 %tmp3, %c ; <i32> [#uses=1]
+ %tmp7 = sub i32 %tmp5, %a ; <i32> [#uses=1]
+ ret i32 %tmp7
+}
+