2014-06-11 18:28:45 +00:00
|
|
|
; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s
|
2002-05-08 21:34:22 +00:00
|
|
|
|
2009-12-31 08:29:56 +00:00
|
|
|
define i32 @test1(i32 %arg) {
|
2014-06-11 18:28:45 +00:00
|
|
|
%tmp1 = sub i32 -12, %arg
|
|
|
|
%tmp2 = add i32 %tmp1, 12
|
|
|
|
ret i32 %tmp2
|
|
|
|
|
|
|
|
; CHECK-LABEL: @test1
|
2009-12-31 08:29:56 +00:00
|
|
|
; CHECK-NEXT: sub i32 0, %arg
|
|
|
|
; CHECK-NEXT: ret i32
|
2002-05-08 21:34:22 +00:00
|
|
|
}
|
2008-03-19 04:36:04 +00:00
|
|
|
|
2009-12-31 08:29:56 +00:00
|
|
|
define i32 @test2(i32 %reg109, i32 %reg1111) {
|
2014-06-11 18:28:45 +00:00
|
|
|
%reg115 = add i32 %reg109, -30
|
|
|
|
%reg116 = add i32 %reg115, %reg1111
|
|
|
|
%reg117 = add i32 %reg116, 30
|
|
|
|
ret i32 %reg117
|
|
|
|
|
|
|
|
; CHECK-LABEL: @test2
|
2014-11-19 23:21:20 +00:00
|
|
|
; CHECK-NEXT: %reg117 = add i32 %reg1111, %reg109
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-NEXT: ret i32 %reg117
|
2009-12-31 08:29:56 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 18:28:45 +00:00
|
|
|
@e = external global i32
|
|
|
|
@a = external global i32
|
|
|
|
@b = external global i32
|
|
|
|
@c = external global i32
|
|
|
|
@f = external global i32
|
2009-12-31 08:29:56 +00:00
|
|
|
|
|
|
|
define void @test3() {
|
2015-02-27 21:17:42 +00:00
|
|
|
%A = load i32, i32* @a
|
|
|
|
%B = load i32, i32* @b
|
|
|
|
%C = load i32, i32* @c
|
2014-06-11 18:28:45 +00:00
|
|
|
%t1 = add i32 %A, %B
|
|
|
|
%t2 = add i32 %t1, %C
|
|
|
|
%t3 = add i32 %C, %A
|
|
|
|
%t4 = add i32 %t3, %B
|
|
|
|
; e = (a+b)+c;
|
|
|
|
store i32 %t2, i32* @e
|
|
|
|
; f = (a+c)+b
|
|
|
|
store i32 %t4, i32* @f
|
|
|
|
ret void
|
|
|
|
|
|
|
|
; CHECK-LABEL: @test3
|
2009-12-31 08:29:56 +00:00
|
|
|
; CHECK: add i32
|
|
|
|
; CHECK: add i32
|
|
|
|
; CHECK-NOT: add i32
|
|
|
|
; CHECK: ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
define void @test4() {
|
2015-02-27 21:17:42 +00:00
|
|
|
%A = load i32, i32* @a
|
|
|
|
%B = load i32, i32* @b
|
|
|
|
%C = load i32, i32* @c
|
2014-06-11 18:28:45 +00:00
|
|
|
%t1 = add i32 %A, %B
|
|
|
|
%t2 = add i32 %t1, %C
|
|
|
|
%t3 = add i32 %C, %A
|
|
|
|
%t4 = add i32 %t3, %B
|
|
|
|
; e = c+(a+b)
|
|
|
|
store i32 %t2, i32* @e
|
|
|
|
; f = (c+a)+b
|
|
|
|
store i32 %t4, i32* @f
|
|
|
|
ret void
|
|
|
|
|
|
|
|
; CHECK-LABEL: @test4
|
2009-12-31 08:29:56 +00:00
|
|
|
; CHECK: add i32
|
|
|
|
; CHECK: add i32
|
|
|
|
; CHECK-NOT: add i32
|
|
|
|
; CHECK: ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
define void @test5() {
|
2015-02-27 21:17:42 +00:00
|
|
|
%A = load i32, i32* @a
|
|
|
|
%B = load i32, i32* @b
|
|
|
|
%C = load i32, i32* @c
|
2014-06-11 18:28:45 +00:00
|
|
|
%t1 = add i32 %B, %A
|
|
|
|
%t2 = add i32 %t1, %C
|
|
|
|
%t3 = add i32 %C, %A
|
|
|
|
%t4 = add i32 %t3, %B
|
|
|
|
; e = c+(b+a)
|
|
|
|
store i32 %t2, i32* @e
|
|
|
|
; f = (c+a)+b
|
|
|
|
store i32 %t4, i32* @f
|
|
|
|
ret void
|
|
|
|
|
|
|
|
; CHECK-LABEL: @test5
|
2009-12-31 08:29:56 +00:00
|
|
|
; CHECK: add i32
|
|
|
|
; CHECK: add i32
|
|
|
|
; CHECK-NOT: add i32
|
|
|
|
; CHECK: ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test6() {
|
2015-02-27 21:17:42 +00:00
|
|
|
%tmp.0 = load i32, i32* @a
|
|
|
|
%tmp.1 = load i32, i32* @b
|
2014-06-11 18:28:45 +00:00
|
|
|
; (a+b)
|
|
|
|
%tmp.2 = add i32 %tmp.0, %tmp.1
|
2015-02-27 21:17:42 +00:00
|
|
|
%tmp.4 = load i32, i32* @c
|
2014-06-11 18:28:45 +00:00
|
|
|
; (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-LABEL: @test6
|
2009-12-31 08:29:56 +00:00
|
|
|
; CHECK: ret i32 0
|
|
|
|
}
|
2009-12-31 08:32:22 +00:00
|
|
|
|
|
|
|
; This should be one add and two multiplies.
|
|
|
|
define i32 @test7(i32 %A, i32 %B, i32 %C) {
|
2014-06-11 18:28:45 +00:00
|
|
|
; 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-LABEL: @test7
|
2014-11-19 23:21:20 +00:00
|
|
|
; CHECK-NEXT: add i32 %C, %B
|
2009-12-31 08:32:22 +00:00
|
|
|
; CHECK-NEXT: mul i32
|
|
|
|
; CHECK-NEXT: mul i32
|
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
|
2014-06-11 18:28:45 +00:00
|
|
|
%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-LABEL: @test8
|
2014-11-19 23:21:20 +00:00
|
|
|
; CHECK-NEXT: %A = mul i32 %Y, %X
|
2009-12-31 08:32:22 +00:00
|
|
|
; CHECK-NEXT: %C = sub i32 %Z, %A
|
|
|
|
; CHECK-NEXT: ret i32 %C
|
|
|
|
}
|
2009-12-31 08:33:49 +00:00
|
|
|
|
|
|
|
; PR5458
|
|
|
|
define i32 @test9(i32 %X) {
|
|
|
|
%Y = mul i32 %X, 47
|
|
|
|
%Z = add i32 %Y, %Y
|
|
|
|
ret i32 %Z
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-LABEL: @test9
|
2009-12-31 19:24:52 +00:00
|
|
|
; 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
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-LABEL: @test10
|
2009-12-31 19:24:52 +00:00
|
|
|
; CHECK-NEXT: mul i32 %X, 3
|
|
|
|
; CHECK-NEXT: ret i32
|
2009-12-31 08:33:49 +00:00
|
|
|
}
|
2009-12-31 19:24:52 +00:00
|
|
|
|
|
|
|
define i32 @test11(i32 %W) {
|
|
|
|
%X = mul i32 %W, 127
|
|
|
|
%Y = add i32 %X ,%X
|
|
|
|
%Z = add i32 %Y, %X
|
|
|
|
ret i32 %Z
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-LABEL: @test11
|
2009-12-31 19:24:52 +00:00
|
|
|
; CHECK-NEXT: mul i32 %W, 381
|
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
|
|
|
|
2009-12-31 20:34:32 +00:00
|
|
|
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
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-LABEL: @test12
|
2009-12-31 20:34:32 +00:00
|
|
|
; CHECK-NEXT: mul i32 %X, -3
|
|
|
|
; CHECK-NEXT: add i32{{.*}}, 6
|
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
2009-12-31 19:24:52 +00:00
|
|
|
|
2010-01-01 00:50:00 +00:00
|
|
|
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
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-LABEL: @test13
|
2010-01-01 00:50:00 +00:00
|
|
|
; CHECK-NEXT: sub i32 %X3, %X2
|
|
|
|
; CHECK-NEXT: mul i32 {{.*}}, %X1
|
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
|
|
|
|
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
2010-01-01 01:13:15 +00:00
|
|
|
; 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
|
2014-06-11 18:28:45 +00:00
|
|
|
|
|
|
|
; CHECK-LABEL: @test14
|
2015-05-28 06:16:39 +00:00
|
|
|
; CHECK-NEXT: %[[SUB:.*]] = sub i32 %X1, %X2
|
|
|
|
; CHECK-NEXT: mul i32 %[[SUB]], 47
|
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
2010-01-01 01:13:15 +00:00
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
|
|
|
|
2010-02-06 01:16:25 +00:00
|
|
|
; 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
|
2014-06-11 18:28:45 +00:00
|
|
|
; CHECK-LABEL: @test15
|
2010-02-06 01:16:25 +00:00
|
|
|
; CHECK: and i1 %A, %B
|
|
|
|
}
|