2010-12-22 00:54:34 +01:00
|
|
|
; RUN: opt < %s -basicaa -gvn -S | FileCheck %s
|
|
|
|
|
|
|
|
@a = external global i32 ; <i32*> [#uses=7]
|
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test1(
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
llvm-svn: 141177
2011-10-05 16:28:49 +02:00
|
|
|
define i32 @test1() nounwind {
|
2010-12-22 00:54:34 +01:00
|
|
|
entry:
|
2015-02-27 22:17:42 +01:00
|
|
|
%0 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
%1 = icmp eq i32 %0, 4
|
|
|
|
br i1 %1, label %bb, label %bb1
|
|
|
|
|
|
|
|
bb: ; preds = %entry
|
|
|
|
br label %bb8
|
|
|
|
|
|
|
|
bb1: ; preds = %entry
|
2015-02-27 22:17:42 +01:00
|
|
|
%2 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
%3 = icmp eq i32 %2, 5
|
|
|
|
br i1 %3, label %bb2, label %bb3
|
|
|
|
|
|
|
|
bb2: ; preds = %bb1
|
|
|
|
br label %bb8
|
|
|
|
|
|
|
|
bb3: ; preds = %bb1
|
2015-02-27 22:17:42 +01:00
|
|
|
%4 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
%5 = icmp eq i32 %4, 4
|
|
|
|
; CHECK: br i1 false, label %bb4, label %bb5
|
|
|
|
br i1 %5, label %bb4, label %bb5
|
|
|
|
|
|
|
|
bb4: ; preds = %bb3
|
2015-02-27 22:17:42 +01:00
|
|
|
%6 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
%7 = add i32 %6, 5
|
|
|
|
br label %bb8
|
|
|
|
|
|
|
|
bb5: ; preds = %bb3
|
2015-02-27 22:17:42 +01:00
|
|
|
%8 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
%9 = icmp eq i32 %8, 5
|
|
|
|
; CHECK: br i1 false, label %bb6, label %bb7
|
|
|
|
br i1 %9, label %bb6, label %bb7
|
|
|
|
|
|
|
|
bb6: ; preds = %bb5
|
2015-02-27 22:17:42 +01:00
|
|
|
%10 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
%11 = add i32 %10, 4
|
|
|
|
br label %bb8
|
|
|
|
|
|
|
|
bb7: ; preds = %bb5
|
2015-02-27 22:17:42 +01:00
|
|
|
%12 = load i32, i32* @a, align 4
|
2010-12-22 00:54:34 +01:00
|
|
|
br label %bb8
|
|
|
|
|
|
|
|
bb8: ; preds = %bb7, %bb6, %bb4, %bb2, %bb
|
|
|
|
%.0 = phi i32 [ %12, %bb7 ], [ %11, %bb6 ], [ %7, %bb4 ], [ 4, %bb2 ], [ 5, %bb ]
|
|
|
|
br label %return
|
|
|
|
|
|
|
|
return: ; preds = %bb8
|
|
|
|
ret i32 %.0
|
2011-10-05 16:17:01 +02:00
|
|
|
}
|
|
|
|
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
llvm-svn: 141177
2011-10-05 16:28:49 +02:00
|
|
|
declare void @foo(i1)
|
|
|
|
declare void @bar(i32)
|
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test3(
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
llvm-svn: 141177
2011-10-05 16:28:49 +02:00
|
|
|
define void @test3(i32 %x, i32 %y) {
|
|
|
|
%xz = icmp eq i32 %x, 0
|
|
|
|
%yz = icmp eq i32 %y, 0
|
|
|
|
%z = and i1 %xz, %yz
|
|
|
|
br i1 %z, label %both_zero, label %nope
|
|
|
|
both_zero:
|
|
|
|
call void @foo(i1 %xz)
|
|
|
|
; CHECK: call void @foo(i1 true)
|
|
|
|
call void @foo(i1 %yz)
|
|
|
|
; CHECK: call void @foo(i1 true)
|
|
|
|
call void @bar(i32 %x)
|
|
|
|
; CHECK: call void @bar(i32 0)
|
|
|
|
call void @bar(i32 %y)
|
|
|
|
; CHECK: call void @bar(i32 0)
|
|
|
|
ret void
|
|
|
|
nope:
|
|
|
|
call void @foo(i1 %z)
|
|
|
|
; CHECK: call void @foo(i1 false)
|
|
|
|
ret void
|
|
|
|
}
|
2011-10-07 10:29:06 +02:00
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test4(
|
2011-10-07 10:29:06 +02:00
|
|
|
define void @test4(i1 %b, i32 %x) {
|
|
|
|
br i1 %b, label %sw, label %case3
|
|
|
|
sw:
|
|
|
|
switch i32 %x, label %default [
|
|
|
|
i32 0, label %case0
|
|
|
|
i32 1, label %case1
|
|
|
|
i32 2, label %case0
|
|
|
|
i32 3, label %case3
|
|
|
|
i32 4, label %default
|
|
|
|
]
|
|
|
|
default:
|
|
|
|
; CHECK: default:
|
|
|
|
call void @bar(i32 %x)
|
|
|
|
; CHECK: call void @bar(i32 %x)
|
|
|
|
ret void
|
|
|
|
case0:
|
|
|
|
; CHECK: case0:
|
|
|
|
call void @bar(i32 %x)
|
|
|
|
; CHECK: call void @bar(i32 %x)
|
|
|
|
ret void
|
|
|
|
case1:
|
|
|
|
; CHECK: case1:
|
|
|
|
call void @bar(i32 %x)
|
|
|
|
; CHECK: call void @bar(i32 1)
|
|
|
|
ret void
|
|
|
|
case3:
|
|
|
|
; CHECK: case3:
|
|
|
|
call void @bar(i32 %x)
|
|
|
|
; CHECK: call void @bar(i32 %x)
|
|
|
|
ret void
|
|
|
|
}
|
2012-02-27 09:14:30 +01:00
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test5(
|
2012-02-27 09:14:30 +01:00
|
|
|
define i1 @test5(i32 %x, i32 %y) {
|
|
|
|
%cmp = icmp eq i32 %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
%cmp2 = icmp ne i32 %x, %y
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
%cmp3 = icmp eq i32 %x, %y
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test6(
|
2012-02-27 09:14:30 +01:00
|
|
|
define i1 @test6(i32 %x, i32 %y) {
|
|
|
|
%cmp2 = icmp ne i32 %x, %y
|
|
|
|
%cmp = icmp eq i32 %x, %y
|
|
|
|
%cmp3 = icmp eq i32 %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:29:48 +01:00
|
|
|
; CHECK-LABEL: @test6_fp(
|
|
|
|
define i1 @test6_fp(float %x, float %y) {
|
|
|
|
%cmp2 = fcmp une float %x, %y
|
|
|
|
%cmp = fcmp oeq float %x, %y
|
|
|
|
%cmp3 = fcmp oeq float %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test7(
|
2012-02-27 09:14:30 +01:00
|
|
|
define i1 @test7(i32 %x, i32 %y) {
|
|
|
|
%cmp = icmp sgt i32 %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
%cmp2 = icmp sle i32 %x, %y
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
%cmp3 = icmp sgt i32 %x, %y
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:29:48 +01:00
|
|
|
; CHECK-LABEL: @test7_fp(
|
|
|
|
define i1 @test7_fp(float %x, float %y) {
|
|
|
|
%cmp = fcmp ogt float %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
%cmp2 = fcmp ule float %x, %y
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
%cmp3 = fcmp ogt float %x, %y
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test8(
|
2012-02-27 09:14:30 +01:00
|
|
|
define i1 @test8(i32 %x, i32 %y) {
|
|
|
|
%cmp2 = icmp sle i32 %x, %y
|
|
|
|
%cmp = icmp sgt i32 %x, %y
|
|
|
|
%cmp3 = icmp sgt i32 %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
2012-02-29 12:12:03 +01:00
|
|
|
|
2015-01-12 20:29:48 +01:00
|
|
|
; CHECK-LABEL: @test8_fp(
|
|
|
|
define i1 @test8_fp(float %x, float %y) {
|
|
|
|
%cmp2 = fcmp ule float %x, %y
|
|
|
|
%cmp = fcmp ogt float %x, %y
|
|
|
|
%cmp3 = fcmp ogt float %x, %y
|
|
|
|
br i1 %cmp, label %same, label %different
|
|
|
|
|
|
|
|
same:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp2
|
|
|
|
|
|
|
|
different:
|
|
|
|
; CHECK: ret i1 false
|
|
|
|
ret i1 %cmp3
|
|
|
|
}
|
|
|
|
|
2012-02-29 12:12:03 +01:00
|
|
|
; PR1768
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test9(
|
2012-02-29 12:12:03 +01:00
|
|
|
define i32 @test9(i32 %i, i32 %j) {
|
|
|
|
%cmp = icmp eq i32 %i, %j
|
|
|
|
br i1 %cmp, label %cond_true, label %ret
|
|
|
|
|
|
|
|
cond_true:
|
|
|
|
%diff = sub i32 %i, %j
|
|
|
|
ret i32 %diff
|
|
|
|
; CHECK: ret i32 0
|
|
|
|
|
|
|
|
ret:
|
|
|
|
ret i32 5
|
|
|
|
; CHECK: ret i32 5
|
|
|
|
}
|
|
|
|
|
|
|
|
; PR1768
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test10(
|
2012-02-29 12:12:03 +01:00
|
|
|
define i32 @test10(i32 %j, i32 %i) {
|
|
|
|
%cmp = icmp eq i32 %i, %j
|
|
|
|
br i1 %cmp, label %cond_true, label %ret
|
|
|
|
|
|
|
|
cond_true:
|
|
|
|
%diff = sub i32 %i, %j
|
|
|
|
ret i32 %diff
|
|
|
|
; CHECK: ret i32 0
|
|
|
|
|
|
|
|
ret:
|
|
|
|
ret i32 5
|
|
|
|
; CHECK: ret i32 5
|
|
|
|
}
|
|
|
|
|
|
|
|
declare i32 @yogibar()
|
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test11(
|
2012-02-29 12:12:03 +01:00
|
|
|
define i32 @test11(i32 %x) {
|
|
|
|
%v0 = call i32 @yogibar()
|
|
|
|
%v1 = call i32 @yogibar()
|
|
|
|
%cmp = icmp eq i32 %v0, %v1
|
|
|
|
br i1 %cmp, label %cond_true, label %next
|
|
|
|
|
|
|
|
cond_true:
|
|
|
|
ret i32 %v1
|
|
|
|
; CHECK: ret i32 %v0
|
|
|
|
|
|
|
|
next:
|
|
|
|
%cmp2 = icmp eq i32 %x, %v0
|
|
|
|
br i1 %cmp2, label %cond_true2, label %next2
|
|
|
|
|
|
|
|
cond_true2:
|
|
|
|
ret i32 %v0
|
|
|
|
; CHECK: ret i32 %x
|
|
|
|
|
|
|
|
next2:
|
|
|
|
ret i32 0
|
|
|
|
}
|
2012-03-04 14:25:19 +01:00
|
|
|
|
2013-07-14 03:42:54 +02:00
|
|
|
; CHECK-LABEL: @test12(
|
2012-03-04 14:25:19 +01:00
|
|
|
define i32 @test12(i32 %x) {
|
|
|
|
%cmp = icmp eq i32 %x, 0
|
|
|
|
br i1 %cmp, label %cond_true, label %cond_false
|
|
|
|
|
|
|
|
cond_true:
|
|
|
|
br label %ret
|
|
|
|
|
|
|
|
cond_false:
|
|
|
|
br label %ret
|
|
|
|
|
|
|
|
ret:
|
|
|
|
%res = phi i32 [ %x, %cond_true ], [ %x, %cond_false ]
|
|
|
|
; CHECK: %res = phi i32 [ 0, %cond_true ], [ %x, %cond_false ]
|
|
|
|
ret i32 %res
|
|
|
|
}
|
2018-09-10 14:23:05 +02:00
|
|
|
|
|
|
|
; On the path from entry->if->end we know that ptr1==ptr2, so we can determine
|
|
|
|
; that gep2 does not alias ptr1 on that path (as it would require that
|
|
|
|
; ptr2==ptr2+2), so we can perform PRE of the load.
|
|
|
|
; CHECK-LABEL: @test13
|
|
|
|
define i32 @test13(i32* %ptr1, i32* %ptr2) {
|
|
|
|
; CHECK-LABEL: entry:
|
|
|
|
entry:
|
|
|
|
%gep1 = getelementptr i32, i32* %ptr2, i32 1
|
|
|
|
%gep2 = getelementptr i32, i32* %ptr2, i32 2
|
|
|
|
%cmp = icmp eq i32* %ptr1, %ptr2
|
|
|
|
br i1 %cmp, label %if, label %end
|
|
|
|
|
|
|
|
; CHECK: [[CRIT_EDGE:.*]]:
|
|
|
|
; CHECK: %[[PRE:.*]] = load i32, i32* %gep2, align 4
|
|
|
|
|
|
|
|
; CHECK-LABEL: if:
|
|
|
|
if:
|
|
|
|
%val1 = load i32, i32* %gep2, align 4
|
|
|
|
br label %end
|
|
|
|
|
|
|
|
; CHECK-LABEL: end:
|
|
|
|
; CHECK: %val2 = phi i32 [ %val1, %if ], [ %[[PRE]], %[[CRIT_EDGE]] ]
|
|
|
|
; CHECK-NOT: load
|
|
|
|
end:
|
|
|
|
%phi1 = phi i32* [ %ptr1, %if ], [ %gep1, %entry ]
|
|
|
|
%phi2 = phi i32 [ %val1, %if ], [ 0, %entry ]
|
|
|
|
store i32 0, i32* %phi1, align 4
|
|
|
|
%val2 = load i32, i32* %gep2, align 4
|
|
|
|
%ret = add i32 %phi2, %val2
|
|
|
|
ret i32 %ret
|
|
|
|
}
|
|
|
|
|
|
|
|
; CHECK-LABEL: @test14
|
|
|
|
define void @test14(i32* %ptr1, i32* noalias %ptr2) {
|
|
|
|
entry:
|
|
|
|
%gep1 = getelementptr inbounds i32, i32* %ptr1, i32 1
|
|
|
|
%gep2 = getelementptr inbounds i32, i32* %ptr1, i32 2
|
|
|
|
br label %loop
|
|
|
|
|
|
|
|
; CHECK-LABEL: loop:
|
|
|
|
loop:
|
|
|
|
%phi1 = phi i32* [ %gep3, %loop.end ], [ %gep1, %entry ]
|
|
|
|
br i1 undef, label %if1, label %then
|
|
|
|
|
|
|
|
; CHECK: [[CRIT_EDGE:.*]]:
|
|
|
|
; CHECK: %[[PRE:.*]] = load i32, i32* %gep2, align 4
|
|
|
|
|
|
|
|
; CHECK-LABEL: if1:
|
|
|
|
; CHECK: %val2 = phi i32 [ %[[PRE]], %[[CRIT_EDGE]] ], [ %val3, %loop.end ]
|
|
|
|
; CHECK-NOT: load
|
|
|
|
if1:
|
|
|
|
%val2 = load i32, i32* %gep2, align 4
|
|
|
|
store i32 %val2, i32* %gep2, align 4
|
|
|
|
store i32 0, i32* %phi1, align 4
|
|
|
|
br label %then
|
|
|
|
|
|
|
|
; CHECK-LABEL: then:
|
|
|
|
then:
|
|
|
|
%cmp = icmp eq i32* %gep2, %ptr2
|
|
|
|
br i1 %cmp, label %loop.end, label %if2
|
|
|
|
|
|
|
|
if2:
|
|
|
|
br label %loop.end
|
|
|
|
|
|
|
|
loop.end:
|
|
|
|
%phi3 = phi i32* [ %gep2, %then ], [ %ptr1, %if2 ]
|
|
|
|
%val3 = load i32, i32* %gep2, align 4
|
|
|
|
store i32 %val3, i32* %phi3, align 4
|
|
|
|
%gep3 = getelementptr inbounds i32, i32* %ptr1, i32 1
|
|
|
|
br i1 undef, label %loop, label %if1
|
|
|
|
}
|