From ee7bf41c06f3d760792487cbceef747a766e775a Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Mon, 19 May 2008 22:23:55 +0000 Subject: [PATCH] Do not erase induction variable increment if it is used outside the loop. llvm-svn: 51280 --- llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp | 24 +++++++++-- .../LoopIndexSplit/2008-05-19-IndVar.ll | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 llvm/test/Transforms/LoopIndexSplit/2008-05-19-IndVar.ll diff --git a/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp b/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp index 8b554444c526..5faec97a9711 100644 --- a/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp +++ b/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp @@ -596,11 +596,27 @@ bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) { if (isa(I) || I == LTerminator) continue; - if (I == IndVarIncrement) - I->replaceAllUsesWith(ExitValue); - else + if (I == IndVarIncrement) { + // Replace induction variable increment if it is not used outside + // the loop. + bool UsedOutsideLoop = false; + for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); + UI != E; ++UI) { + if (Instruction *Use = dyn_cast(UI)) + if (!L->contains(Use->getParent())) { + UsedOutsideLoop = true; + break; + } + } + if (!UsedOutsideLoop) { + I->replaceAllUsesWith(ExitValue); + I->eraseFromParent(); + } + } + else { I->replaceAllUsesWith(UndefValue::get(I->getType())); - I->eraseFromParent(); + I->eraseFromParent(); + } } LPM->deleteLoopFromQueue(L); diff --git a/llvm/test/Transforms/LoopIndexSplit/2008-05-19-IndVar.ll b/llvm/test/Transforms/LoopIndexSplit/2008-05-19-IndVar.ll new file mode 100644 index 000000000000..8aa2e52b3fe9 --- /dev/null +++ b/llvm/test/Transforms/LoopIndexSplit/2008-05-19-IndVar.ll @@ -0,0 +1,41 @@ +; RUN: llvm-as < %s | opt -loop-index-split | llvm-dis | grep tmp8 | count 3 +; count 3 = increment + phi + store +;PR2294 +@g_2 = external global i16 ; [#uses=4] +@g_5 = external global i32 ; [#uses=1] +@.str = external constant [4 x i8] ; <[4 x i8]*> [#uses=1] + +declare void @func_1() nounwind + +define i32 @main() nounwind { +entry: + %tmp101.i = load i16* @g_2, align 2 ; [#uses=1] + %tmp112.i = icmp sgt i16 %tmp101.i, 0 ; [#uses=1] + br i1 %tmp112.i, label %bb.preheader.i, label %func_1.exit +bb.preheader.i: ; preds = %entry + %g_2.promoted.i = load i16* @g_2 ; [#uses=1] + br label %bb.i +bb.i: ; preds = %bb6.i, %bb.preheader.i + %g_2.tmp.0.i = phi i16 [ %g_2.promoted.i, %bb.preheader.i ], [ %tmp8.i, %bb6.i ] ; [#uses=2] + %tmp2.i = icmp eq i16 %g_2.tmp.0.i, 0 ; [#uses=1] + br i1 %tmp2.i, label %bb4.i, label %bb6.i +bb4.i: ; preds = %bb.i + %tmp5.i = volatile load i32* @g_5, align 4 ; [#uses=0] + br label %bb6.i +bb6.i: ; preds = %bb4.i, %bb.i + %tmp8.i = add i16 %g_2.tmp.0.i, 1 ; [#uses=3] + %tmp11.i = icmp sgt i16 %tmp8.i, 0 ; [#uses=1] + br i1 %tmp11.i, label %bb.i, label %return.loopexit.i +return.loopexit.i: ; preds = %bb6.i + %tmp8.i.lcssa = phi i16 [ %tmp8.i, %bb6.i ] ; [#uses=1] + store i16 %tmp8.i.lcssa, i16* @g_2 + br label %func_1.exit +func_1.exit: ; preds = %return.loopexit.i, %entry + %tmp1 = load i16* @g_2, align 2 ; [#uses=1] + %tmp12 = sext i16 %tmp1 to i32 ; [#uses=1] + %tmp3 = tail call i32 (i8*, ...)* @printf( i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), i32 %tmp12 ) nounwind ; [#uses=0] + ret i32 0 +} + +declare i32 @printf(i8*, ...) nounwind +