If a loop iterates exactly once (has backedge count = 0) then don't
authorChris Lattner <sabre@nondot.org>
Sun, 2 Jan 2011 20:24:21 +0000 (20:24 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 2 Jan 2011 20:24:21 +0000 (20:24 +0000)
mess with it.  We'd rather peel/unroll it than convert all of its
stores into memsets.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122711 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LoopIdiomRecognize.cpp
test/Transforms/LoopIdiom/basic.ll

index 8b022bab83a7280fb2a6eef5d12dd3a79aad9911..57a502b1d25fbb03972cc09940d8fa7aa0d75432 100644 (file)
@@ -159,6 +159,12 @@ bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
   const SCEV *BECount = SE->getBackedgeTakenCount(L);
   if (isa<SCEVCouldNotCompute>(BECount)) return false;
   
+  // If this loop executes exactly one time, then it should be peeled, not
+  // optimized by this pass.
+  if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
+    if (BECst->getValue()->getValue() == 0)
+      return false;
+  
   // We require target data for now.
   TD = getAnalysisIfAvailable<TargetData>();
   if (TD == 0) return false;
index 3fa6eb900b4aea75d66cb67529f27e0c32c325e1..e8e0f38004c878d25689162871c5d64bee978b81 100644 (file)
@@ -188,4 +188,22 @@ for.end:                                          ; preds = %for.body, %entry
 ; CHECK-NOT: store
 }
 
+; This is a loop should not be transformed, it only executes one iteration.
+define void @test8(i64* %Ptr, i64 %Size) nounwind ssp {
+bb.nph:                                           ; preds = %entry
+  br label %for.body
+
+for.body:                                         ; preds = %bb.nph, %for.body
+  %indvar = phi i64 [ 0, %bb.nph ], [ %indvar.next, %for.body ]
+  %PI = getelementptr i64* %Ptr, i64 %indvar
+  store i64 0, i64 *%PI
+  %indvar.next = add i64 %indvar, 1
+  %exitcond = icmp eq i64 %indvar.next, 1
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:                                          ; preds = %for.body, %entry
+  ret void
+; CHECK: @test8
+; CHECK: store i64 0, i64* %PI
+}