Teach ScalarEvolution how to compute a tripcount for a loop with

true or false as its exit condition. These are usually eliminated by
SimplifyCFG, but the may be left around during a pass which wishes
to preserve the CFG.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96683 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2010-02-19 18:12:07 +00:00
parent 6b89aa57e6
commit 00cb5b7245
10 changed files with 101 additions and 12 deletions

View File

@ -3703,6 +3703,19 @@ ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
// Check for a constant condition. These are normally stripped out by
// SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
// preserve the CFG and is temporarily leaving constant conditions
// in place.
if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
if (L->contains(FBB) == !CI->getZExtValue())
// The backedge is always taken.
return getCouldNotCompute();
else
// The backedge is never taken.
return getIntegerSCEV(0, CI->getType());
}
// If it's not an integer or pointer comparison then compute it the hard way.
return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
}

View File

@ -0,0 +1,76 @@
; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s
; Trip counts with trivial exit conditions.
; CHECK: Determining loop execution counts for: @a
; CHECK: Loop %loop: Unpredictable backedge-taken count.
; CHECK: Loop %loop: Unpredictable max backedge-taken count.
; CHECK: Determining loop execution counts for: @b
; CHECK: Loop %loop: backedge-taken count is false
; CHECK: Loop %loop: max backedge-taken count is false
; CHECK: Determining loop execution counts for: @c
; CHECK: Loop %loop: backedge-taken count is false
; CHECK: Loop %loop: max backedge-taken count is false
; CHECK: Determining loop execution counts for: @d
; CHECK: Loop %loop: Unpredictable backedge-taken count.
; CHECK: Loop %loop: Unpredictable max backedge-taken count.
define void @a(i64 %n) nounwind {
entry:
%t0 = icmp sgt i64 %n, 0
br i1 %t0, label %loop, label %return
loop:
%i = phi i64 [ %i.next, %loop ], [ 0, %entry ]
%i.next = add nsw i64 %i, 1
%exitcond = icmp eq i64 %i.next, %n
br i1 false, label %return, label %loop
return:
ret void
}
define void @b(i64 %n) nounwind {
entry:
%t0 = icmp sgt i64 %n, 0
br i1 %t0, label %loop, label %return
loop:
%i = phi i64 [ %i.next, %loop ], [ 0, %entry ]
%i.next = add nsw i64 %i, 1
%exitcond = icmp eq i64 %i.next, %n
br i1 true, label %return, label %loop
return:
ret void
}
define void @c(i64 %n) nounwind {
entry:
%t0 = icmp sgt i64 %n, 0
br i1 %t0, label %loop, label %return
loop:
%i = phi i64 [ %i.next, %loop ], [ 0, %entry ]
%i.next = add nsw i64 %i, 1
%exitcond = icmp eq i64 %i.next, %n
br i1 false, label %loop, label %return
return:
ret void
}
define void @d(i64 %n) nounwind {
entry:
%t0 = icmp sgt i64 %n, 0
br i1 %t0, label %loop, label %return
loop:
%i = phi i64 [ %i.next, %loop ], [ 0, %entry ]
%i.next = add nsw i64 %i, 1
%exitcond = icmp eq i64 %i.next, %n
br i1 true, label %loop, label %return
return:
ret void
}

View File

@ -7,7 +7,7 @@ define i32 @test() {
LoopHead: ; preds = %LoopHead, %0, %0
%A = phi i32 [ 7, %0 ], [ 7, %0 ], [ %B, %LoopHead ] ; <i32> [#uses=1]
%B = add i32 %A, 1 ; <i32> [#uses=2]
br i1 false, label %LoopHead, label %Out
br i1 undef, label %LoopHead, label %Out
Out: ; preds = %LoopHead
ret i32 %B

View File

@ -10,7 +10,7 @@ no_exit: ; preds = %no_exit, %entry
%k.0.pn = phi i32 [ %inc.4, %no_exit ], [ 1, %entry ] ; <i32> [#uses=1]
%inc.3 = add i32 %j.0.pn, 1 ; <i32> [#uses=1]
%inc.4 = add i32 %k.0.pn, 1 ; <i32> [#uses=1]
br i1 false, label %no_exit, label %loopexit
br i1 undef, label %no_exit, label %loopexit
loopexit: ; preds = %no_exit, %entry
ret void

View File

@ -8,7 +8,7 @@ cond_continue.3: ; preds = %entry
loopexit.14: ; preds = %entry
%tmp.738 = sub i32 0, 0 ; <i32> [#uses=1]
br i1 false, label %no_exit.15.preheader, label %loopexit.15
br i1 undef, label %no_exit.15.preheader, label %loopexit.15
no_exit.15.preheader: ; preds = %loopexit.14
br label %no_exit.15
@ -16,7 +16,7 @@ no_exit.15.preheader: ; preds = %loopexit.14
no_exit.15: ; preds = %no_exit.15, %no_exit.15.preheader
%highC.0 = phi i32 [ %tmp.738, %no_exit.15.preheader ], [ %dec.0, %no_exit.15 ] ; <i32> [#uses=1]
%dec.0 = add i32 %highC.0, -1 ; <i32> [#uses=1]
br i1 false, label %no_exit.15, label %loopexit.15
br i1 undef, label %no_exit.15, label %loopexit.15
loopexit.15: ; preds = %no_exit.15, %loopexit.14
ret void

View File

@ -9,7 +9,7 @@ entry:
no_exit.0: ; preds = %no_exit.0, %entry
%p.0.0 = phi i32* [ getelementptr ([29 x [29 x [2 x i32]]]* @fixtab, i32 0, i32 0, i32 0, i32 0), %entry ], [ %inc.0, %no_exit.0 ] ; <i32*> [#uses=1]
%inc.0 = getelementptr i32* %p.0.0, i32 1 ; <i32*> [#uses=1]
br i1 false, label %no_exit.0, label %no_exit.1
br i1 undef, label %no_exit.0, label %no_exit.1
no_exit.1: ; preds = %no_exit.0
ret void

View File

@ -20,7 +20,7 @@ cond_next182.i: ; preds = %cond_next182.i, %cond_true52
%tmp194.i53 = bitcast i32 %decay.i.0 to float ; <float> [#uses=1]
%tmp195.i = fsub float %tmp194.i53, 8.000000e+00 ; <float> [#uses=1]
%tmp195.i.upgrd.1 = bitcast float %tmp195.i to i32 ; <i32> [#uses=1]
br i1 false, label %cond_next182.i, label %bb418.i.preheader
br i1 undef, label %cond_next182.i, label %bb418.i.preheader
bb418.i.preheader: ; preds = %cond_next182.i
ret void

View File

@ -12,7 +12,7 @@ bb.nph1.preheader: ; preds = %4
br label %bb.nph1
bb.nph1: ; preds = %.outer, %bb.nph1.preheader
br i1 false, label %bb.nph3.preheader, label %.outer
br i1 undef, label %bb.nph3.preheader, label %.outer
bb.nph3.preheader: ; preds = %bb.nph1
br label %bb.nph3
@ -31,7 +31,7 @@ bb.nph3: ; preds = %bb.nph3, %bb.nph3.preheader
br label %.outer
.outer: ; preds = %.outer.loopexit, %bb.nph1
br i1 false, label %bb.nph1, label %.outer._crit_edge.loopexit
br i1 undef, label %bb.nph1, label %.outer._crit_edge.loopexit
.outer._crit_edge.loopexit: ; preds = %.outer
br label %.outer._crit_edge

View File

@ -10,13 +10,13 @@ entry:
bb: ; preds = %bb6, %entry
%p_71_addr.0 = phi i8 [ %p_71, %entry ], [ %0, %bb6 ] ; <i8> [#uses=0]
br i1 false, label %bb4, label %bb1
br i1 undef, label %bb4, label %bb1
bb1: ; preds = %bb
ret i32 0
bb4: ; preds = %bb4, %bb
br i1 false, label %bb6, label %bb4
br i1 undef, label %bb6, label %bb4
bb6: ; preds = %bb4
%0 = and i8 0, 0 ; <i8> [#uses=1]

View File

@ -16,7 +16,7 @@ bb2: ; preds = %bb2, %entry
%str2Ptr_addr.1 = phi i8* [ %str2Ptr_addr.0, %entry ], [ %1, %bb2 ] ; <i8*> [#uses=1]
%1 = getelementptr i8* %str2Ptr_addr.1, i64 1 ; <i8*> [#uses=2]
%2 = icmp ult i8* %1, %inLastBytePtr ; <i1> [#uses=0]
br i1 false, label %bb2, label %return
br i1 undef, label %bb2, label %return
return: ; preds = %bb2
ret void
@ -32,7 +32,7 @@ bb2: ; preds = %bb2, %entry
%str2Ptr_addr.1 = phi i8* [ %str2Ptr_addr.0, %entry ], [ %1, %bb2 ] ; <i8*> [#uses=1]
%1 = getelementptr i8* %str2Ptr_addr.1, i64 1 ; <i8*> [#uses=2]
%2 = icmp slt i8* %1, %inLastBytePtr ; <i1> [#uses=0]
br i1 false, label %bb2, label %return
br i1 undef, label %bb2, label %return
return: ; preds = %bb2
ret void