Teach ScalarEvolution how to compute a tripcount for a loop with
authorDan Gohman <gohman@apple.com>
Fri, 19 Feb 2010 18:12:07 +0000 (18:12 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 19 Feb 2010 18:12:07 +0000 (18:12 +0000)
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
test/Analysis/ScalarEvolution/trip-count10.ll [new file with mode: 0644]
test/Transforms/IndVarSimplify/2003-09-12-MultiplePred.ll
test/Transforms/IndVarSimplify/2003-12-10-RemoveInstrCrash.ll
test/Transforms/IndVarSimplify/2003-12-15-Crash.ll
test/Transforms/IndVarSimplify/2005-11-18-Crash.ll
test/Transforms/IndVarSimplify/2006-12-10-BitCast.ll
test/Transforms/IndVarSimplify/2009-05-24-useafterfree.ll
test/Transforms/IndVarSimplify/avoid-i0.ll
test/Transforms/IndVarSimplify/max-pointer.ll

index c2284a8ab9bd22a2ee429a6cf8d2f0241e44e759..a0700ba4218d42b80c683acac69915c27d5815e8 100644 (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));
 }
diff --git a/test/Analysis/ScalarEvolution/trip-count10.ll b/test/Analysis/ScalarEvolution/trip-count10.ll
new file mode 100644 (file)
index 0000000..0a992f9
--- /dev/null
@@ -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
+}
index 36d50065d370754ba8cbff8f5d35e18f4c7b3838..30d9ea6fb3ce2ee4c0148e0cfade25144bff264d 100644 (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
index 70ea11ebf04fcc7563e97a300848e497bda95af9..75363314750b94ce051eecc000b9e412afd43dd8 100644 (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
index 5aa2d90a42b9d0762b345b64e128000dc1f75cc3..662828c749d660588381d11ebece1c014dbfd1a2 100644 (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
index f9a3fe6233a558035a7d12e3cd53acdf8b72939a..7202c7ae9d8dbbbf9794f942ff8606dc68842f3c 100644 (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
index 79ac1f072de6aaa76b8b6a52185cfd7a774e1045..80c9ebf2f01779fd6698641c5a0c6e72acbe8829 100644 (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
index 9ad86913e22f91af040c41657e931dbf677a6c39..d73eee812b3074d9942848d1f68d419a84adffcd 100644 (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
index d110a8a7ba97d0f86c4a04d56cc5489e530620e6..59661fa2e88dd803e86ca370633a708ab3ecd9f0 100644 (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]
index 71bc720d5e9c84004c595976cdc895877e50bf87..f18f968f595d53eb6f1eeb843fc583d25db811b6 100644 (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