summaryrefslogtreecommitdiff
path: root/lib/Transforms
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-05-05 18:49:57 +0000
committerChris Lattner <sabre@nondot.org>2007-05-05 18:49:57 +0000
commitcaccc996e2b77503526ab8769444dd5542960a79 (patch)
treec845af40d680eeda81ae488af3fc389303dcd00b /lib/Transforms
parent823572d44d99817e3bb795f6cb9615ca2a7558b6 (diff)
downloadllvm-caccc996e2b77503526ab8769444dd5542960a79.tar.gz
llvm-caccc996e2b77503526ab8769444dd5542960a79.tar.bz2
llvm-caccc996e2b77503526ab8769444dd5542960a79.tar.xz
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
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/Scalar/LoopUnroll.cpp35
1 files changed, 17 insertions, 18 deletions
diff --git a/lib/Transforms/Scalar/LoopUnroll.cpp b/lib/Transforms/Scalar/LoopUnroll.cpp
index bdf8f4d9b7..89ca6d9158 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<const Value*, Value*> LastValueMap;
+ typedef DenseMap<const Value*, Value*> ValueMapTy;
+ ValueMapTy LastValueMap;
std::vector<PHINode*> OrigPHINode;
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
PHINode *PN = cast<PHINode>(I);
@@ -231,7 +232,7 @@ bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(),
E = LoopBlocks.end(); BB != E; ++BB) {
- DenseMap<const Value*, Value*> 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<const Value*, Value*>::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<PHINode*, 8> Users;
for (Value::use_iterator UI = LatchBlock->use_begin(),
UE = LatchBlock->use_end(); UI != UE; ++UI)
if (PHINode* phi = dyn_cast<PHINode>(*UI))
Users.insert(phi);
-
+
+ BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]);
for (SmallPtrSet<PHINode*,8>::iterator SI = Users.begin(), SE = Users.end();
SI != SE; ++SI) {
PHINode *PN = *SI;
- Value* InVal = PN->getIncomingValueForBlock(LatchBlock);
- if (isa<Instruction>(InVal))
- InVal = LastValueMap[InVal];
- PN->removeIncomingValue(LatchBlock, false);
- if (InVal)
- PN->addIncoming(InVal, cast<BasicBlock>(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<Instruction>(InVal)) {
+ if (L->contains(InValI->getParent()))
+ InVal = LastValueMap[InVal];
}
+ PN->addIncoming(InVal, LastIterationBB);
}
}