From 00cb5b724547a821516292fe5cded7c6a5358bda Mon Sep 17 00:00:00 2001
From: Dan Gohman <gohman@apple.com>
Date: Fri, 19 Feb 2010 18:12:07 +0000
Subject: [PATCH] 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
---
 lib/Analysis/ScalarEvolution.cpp              | 13 ++++
 test/Analysis/ScalarEvolution/trip-count10.ll | 76 +++++++++++++++++++
 .../IndVarSimplify/2003-09-12-MultiplePred.ll |  2 +-
 .../2003-12-10-RemoveInstrCrash.ll            |  2 +-
 .../IndVarSimplify/2003-12-15-Crash.ll        |  4 +-
 .../IndVarSimplify/2005-11-18-Crash.ll        |  2 +-
 .../IndVarSimplify/2006-12-10-BitCast.ll      |  2 +-
 .../IndVarSimplify/2009-05-24-useafterfree.ll |  4 +-
 test/Transforms/IndVarSimplify/avoid-i0.ll    |  4 +-
 test/Transforms/IndVarSimplify/max-pointer.ll |  4 +-
 10 files changed, 101 insertions(+), 12 deletions(-)
 create mode 100644 test/Analysis/ScalarEvolution/trip-count10.ll

diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp
index c2284a8ab9b..a0700ba4218 100644
--- a/lib/Analysis/ScalarEvolution.cpp
+++ b/lib/Analysis/ScalarEvolution.cpp
@@ -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));
 }
diff --git a/test/Analysis/ScalarEvolution/trip-count10.ll b/test/Analysis/ScalarEvolution/trip-count10.ll
new file mode 100644
index 00000000000..0a992f952a8
--- /dev/null
+++ b/test/Analysis/ScalarEvolution/trip-count10.ll
@@ -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
+}
diff --git a/test/Transforms/IndVarSimplify/2003-09-12-MultiplePred.ll b/test/Transforms/IndVarSimplify/2003-09-12-MultiplePred.ll
index 36d50065d37..30d9ea6fb3c 100644
--- a/test/Transforms/IndVarSimplify/2003-09-12-MultiplePred.ll
+++ b/test/Transforms/IndVarSimplify/2003-09-12-MultiplePred.ll
@@ -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
diff --git a/test/Transforms/IndVarSimplify/2003-12-10-RemoveInstrCrash.ll b/test/Transforms/IndVarSimplify/2003-12-10-RemoveInstrCrash.ll
index 70ea11ebf04..75363314750 100644
--- a/test/Transforms/IndVarSimplify/2003-12-10-RemoveInstrCrash.ll
+++ b/test/Transforms/IndVarSimplify/2003-12-10-RemoveInstrCrash.ll
@@ -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
diff --git a/test/Transforms/IndVarSimplify/2003-12-15-Crash.ll b/test/Transforms/IndVarSimplify/2003-12-15-Crash.ll
index 5aa2d90a42b..662828c749d 100644
--- a/test/Transforms/IndVarSimplify/2003-12-15-Crash.ll
+++ b/test/Transforms/IndVarSimplify/2003-12-15-Crash.ll
@@ -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
diff --git a/test/Transforms/IndVarSimplify/2005-11-18-Crash.ll b/test/Transforms/IndVarSimplify/2005-11-18-Crash.ll
index f9a3fe6233a..7202c7ae9d8 100644
--- a/test/Transforms/IndVarSimplify/2005-11-18-Crash.ll
+++ b/test/Transforms/IndVarSimplify/2005-11-18-Crash.ll
@@ -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
diff --git a/test/Transforms/IndVarSimplify/2006-12-10-BitCast.ll b/test/Transforms/IndVarSimplify/2006-12-10-BitCast.ll
index 79ac1f072de..80c9ebf2f01 100644
--- a/test/Transforms/IndVarSimplify/2006-12-10-BitCast.ll
+++ b/test/Transforms/IndVarSimplify/2006-12-10-BitCast.ll
@@ -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
diff --git a/test/Transforms/IndVarSimplify/2009-05-24-useafterfree.ll b/test/Transforms/IndVarSimplify/2009-05-24-useafterfree.ll
index 9ad86913e22..d73eee812b3 100644
--- a/test/Transforms/IndVarSimplify/2009-05-24-useafterfree.ll
+++ b/test/Transforms/IndVarSimplify/2009-05-24-useafterfree.ll
@@ -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
diff --git a/test/Transforms/IndVarSimplify/avoid-i0.ll b/test/Transforms/IndVarSimplify/avoid-i0.ll
index d110a8a7ba9..59661fa2e88 100644
--- a/test/Transforms/IndVarSimplify/avoid-i0.ll
+++ b/test/Transforms/IndVarSimplify/avoid-i0.ll
@@ -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]
diff --git a/test/Transforms/IndVarSimplify/max-pointer.ll b/test/Transforms/IndVarSimplify/max-pointer.ll
index 71bc720d5e9..f18f968f595 100644
--- a/test/Transforms/IndVarSimplify/max-pointer.ll
+++ b/test/Transforms/IndVarSimplify/max-pointer.ll
@@ -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