diff options
-rw-r--r-- | lib/Transforms/Scalar/LoopIdiomRecognize.cpp | 6 | ||||
-rw-r--r-- | test/Transforms/LoopIdiom/basic.ll | 18 |
2 files changed, 24 insertions, 0 deletions
diff --git a/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/lib/Transforms/Scalar/LoopIdiomRecognize.cpp index 8b022ba..57a502b 100644 --- a/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -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; diff --git a/test/Transforms/LoopIdiom/basic.ll b/test/Transforms/LoopIdiom/basic.ll index 3fa6eb9..e8e0f38 100644 --- a/test/Transforms/LoopIdiom/basic.ll +++ b/test/Transforms/LoopIdiom/basic.ll @@ -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 +} |