From 82ada54da02a0b2a977fdeb6782c8e5ab9a9b9ea Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Fri, 8 Feb 2008 22:49:13 +0000 Subject: [PATCH] Fix PR 1995. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46898 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopIndexSplit.cpp | 38 +++++++-------- .../LoopIndexSplit/2008-02-08-Crash.ll | 48 +++++++++++++++++++ 2 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 test/Transforms/LoopIndexSplit/2008-02-08-Crash.ll diff --git a/lib/Transforms/Scalar/LoopIndexSplit.cpp b/lib/Transforms/Scalar/LoopIndexSplit.cpp index d31a589a78e..683a038824a 100644 --- a/lib/Transforms/Scalar/LoopIndexSplit.cpp +++ b/lib/Transforms/Scalar/LoopIndexSplit.cpp @@ -1392,6 +1392,11 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) { if (!Succ0->getSinglePredecessor() || !Succ1->getSinglePredecessor()) return false; + // If Exiting block includes loop variant instructions then this + // loop may not be split safely. + if (!safeExitingBlock(SD, ExitCondition->getParent())) + return false; + // After loop is cloned there are two loops. // // First loop, referred as ALoop, executes first part of loop's iteration @@ -1616,8 +1621,8 @@ void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, /// - ExitBB's single predecessor was Latch /// - Latch's second successor was Header /// Now -/// - ExitBB's single predecessor was Header -/// - Latch's one and only successor was Header +/// - ExitBB's single predecessor is Header +/// - Latch's one and only successor is Header /// /// Update ExitBB PHINodes' to reflect this change. void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, @@ -1632,27 +1637,18 @@ void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, Value *V = PN->getIncomingValueForBlock(Latch); if (PHINode *PHV = dyn_cast(V)) { - // PHV is in Latch. PHV has two uses, one use is in ExitBB PHINode - // (i.e. PN :)). - // The second use is in Header and it is new incoming value for PN. - PHINode *U1 = NULL; - PHINode *U2 = NULL; + // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use + // in Header which is new incoming value for PN. Value *NewV = NULL; for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end(); - UI != E; ++UI) { - if (!U1) - U1 = cast(*UI); - else if (!U2) - U2 = cast(*UI); - else - assert ( 0 && "Unexpected third use of this PHINode"); - } - assert (U1 && U2 && "Unable to find two uses"); - - if (U1->getParent() == Header) - NewV = U1; - else - NewV = U2; + UI != E; ++UI) + if (PHINode *U = dyn_cast(*UI)) + if (U->getParent() == Header) { + NewV = U; + break; + } + + assert (NewV && "Unable to find new incoming value for exit block PHI"); PN->addIncoming(NewV, Header); } else if (Instruction *PHI = dyn_cast(V)) { diff --git a/test/Transforms/LoopIndexSplit/2008-02-08-Crash.ll b/test/Transforms/LoopIndexSplit/2008-02-08-Crash.ll new file mode 100644 index 00000000000..4ad906719f8 --- /dev/null +++ b/test/Transforms/LoopIndexSplit/2008-02-08-Crash.ll @@ -0,0 +1,48 @@ +; RUN: llvm-as < %s | opt -loop-index-split -disable-output +; PR 1995 + +define void @add_blkdev_randomness(i32 %major) nounwind { +entry: + br label %bb + +bb: ; preds = %bb39, %entry + %A.0.reg2mem.0 = phi i32 [ undef, %entry ], [ %TEMP.0, %bb39 ] ; [#uses=1] + %D.0.reg2mem.0 = phi i32 [ undef, %entry ], [ %C.0.reg2mem.0, %bb39 ] ; [#uses=3] + %C.0.reg2mem.0 = phi i32 [ undef, %entry ], [ %tmp34, %bb39 ] ; [#uses=4] + %TEMP.1.reg2mem.0 = phi i32 [ undef, %entry ], [ %TEMP.0, %bb39 ] ; [#uses=1] + %i.0.reg2mem.0 = phi i32 [ 0, %entry ], [ %tmp38, %bb39 ] ; [#uses=3] + %B.0.reg2mem.0 = phi i32 [ undef, %entry ], [ %A.0.reg2mem.0, %bb39 ] ; [#uses=5] + %tmp1 = icmp slt i32 %i.0.reg2mem.0, 40 ; [#uses=1] + br i1 %tmp1, label %bb3, label %bb12 + +bb3: ; preds = %bb + %tmp6 = xor i32 %C.0.reg2mem.0, %D.0.reg2mem.0 ; [#uses=1] + %tmp8 = and i32 %B.0.reg2mem.0, %tmp6 ; [#uses=1] + %tmp10 = xor i32 %tmp8, %D.0.reg2mem.0 ; [#uses=1] + %tmp11 = add i32 %tmp10, 1518500249 ; [#uses=1] + br label %bb39 + +bb12: ; preds = %bb + %tmp14 = icmp slt i32 %i.0.reg2mem.0, 60 ; [#uses=1] + br i1 %tmp14, label %bb17, label %bb39 + +bb17: ; preds = %bb12 + %tmp20 = and i32 %B.0.reg2mem.0, %C.0.reg2mem.0 ; [#uses=1] + %tmp23 = xor i32 %B.0.reg2mem.0, %C.0.reg2mem.0 ; [#uses=1] + %tmp25 = and i32 %tmp23, %D.0.reg2mem.0 ; [#uses=1] + %tmp26 = add i32 %tmp20, -1894007588 ; [#uses=1] + %tmp27 = add i32 %tmp26, %tmp25 ; [#uses=1] + br label %bb39 + +bb39: ; preds = %bb12, %bb3, %bb17 + %TEMP.0 = phi i32 [ %tmp27, %bb17 ], [ %tmp11, %bb3 ], [ %TEMP.1.reg2mem.0, %bb12 ] ; [#uses=2] + %tmp31 = lshr i32 %B.0.reg2mem.0, 2 ; [#uses=1] + %tmp33 = shl i32 %B.0.reg2mem.0, 30 ; [#uses=1] + %tmp34 = or i32 %tmp31, %tmp33 ; [#uses=1] + %tmp38 = add i32 %i.0.reg2mem.0, 1 ; [#uses=2] + %tmp41 = icmp slt i32 %tmp38, 80 ; [#uses=1] + br i1 %tmp41, label %bb, label %return + +return: ; preds = %bb39 + ret void +} \ No newline at end of file