From: Chris Lattner Date: Sat, 5 May 2007 18:49:57 +0000 (+0000) Subject: Fix Transforms/LoopUnroll/2007-05-05-UnrollMiscomp.ll and PR1385. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=caccc996e2b77503526ab8769444dd5542960a79;p=oota-llvm.git Fix Transforms/LoopUnroll/2007-05-05-UnrollMiscomp.ll and PR1385. If we have a LCSSA, only modify the input value if the inval was defined by an instruction in the loop. If defined by something before the loop, it is still valid. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36784 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/LoopUnroll.cpp b/lib/Transforms/Scalar/LoopUnroll.cpp index bdf8f4d9b7c..89ca6d91587 100644 --- a/lib/Transforms/Scalar/LoopUnroll.cpp +++ b/lib/Transforms/Scalar/LoopUnroll.cpp @@ -203,7 +203,8 @@ bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { // For the first iteration of the loop, we should use the precloned values for // PHI nodes. Insert associations now. - DenseMap LastValueMap; + typedef DenseMap ValueMapTy; + ValueMapTy LastValueMap; std::vector OrigPHINode; for (BasicBlock::iterator I = Header->begin(); isa(I); ++I) { PHINode *PN = cast(I); @@ -231,7 +232,7 @@ bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { for (std::vector::iterator BB = LoopBlocks.begin(), E = LoopBlocks.end(); BB != E; ++BB) { - DenseMap ValueMap; + ValueMapTy ValueMap; BasicBlock *New = CloneBasicBlock(*BB, ValueMap, SuffixBuffer); Header->getParent()->getBasicBlockList().push_back(New); @@ -250,8 +251,8 @@ bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { // Update our running map of newest clones LastValueMap[*BB] = New; - for (DenseMap::iterator VI = ValueMap.begin(), - VE = ValueMap.end(); VI != VE; ++VI) + for (ValueMapTy::iterator VI = ValueMap.begin(), VE = ValueMap.end(); + VI != VE; ++VI) LastValueMap[VI->first] = VI->second; L->addBasicBlockToLoop(New, *LI); @@ -291,30 +292,28 @@ bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { } - - // Update PHI nodes that reference the final latch block + // The latch block exits the loop. If there are any PHI nodes in the + // successor blocks, update them to use the appropriate values computed as the + // last iteration of the loop. if (TripCount > 1) { SmallPtrSet Users; for (Value::use_iterator UI = LatchBlock->use_begin(), UE = LatchBlock->use_end(); UI != UE; ++UI) if (PHINode* phi = dyn_cast(*UI)) Users.insert(phi); - + + BasicBlock *LastIterationBB = cast(LastValueMap[LatchBlock]); for (SmallPtrSet::iterator SI = Users.begin(), SE = Users.end(); SI != SE; ++SI) { PHINode *PN = *SI; - Value* InVal = PN->getIncomingValueForBlock(LatchBlock); - if (isa(InVal)) - InVal = LastValueMap[InVal]; - PN->removeIncomingValue(LatchBlock, false); - if (InVal) - PN->addIncoming(InVal, cast(LastValueMap[LatchBlock])); - if (PN->getNumIncomingValues() == 0) { - // Remove this phi node. - // If anyone is using this PHI, make them use a dummy value instead... - PN->replaceAllUsesWith(UndefValue::get(PN->getType())); - PN->eraseFromParent(); + Value *InVal = PN->removeIncomingValue(LatchBlock, false); + // If this value was defined in the loop, take the value defined by the + // last iteration of the loop. + if (Instruction *InValI = dyn_cast(InVal)) { + if (L->contains(InValI->getParent())) + InVal = LastValueMap[InVal]; } + PN->addIncoming(InVal, LastIterationBB); } }