[llvm-commits] CVS: llvm/lib/Transforms/Scalar/LoopUnroll.cpp
Chris Lattner
sabre at nondot.org
Sat May 5 11:50:14 PDT 2007
Changes in directory llvm/lib/Transforms/Scalar:
LoopUnroll.cpp updated: 1.43 -> 1.44
---
Log message:
Fix Transforms/LoopUnroll/2007-05-05-UnrollMiscomp.ll and PR1385: http://llvm.org/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.
---
Diffs of the changes: (+17 -18)
LoopUnroll.cpp | 35 +++++++++++++++++------------------
1 files changed, 17 insertions(+), 18 deletions(-)
Index: llvm/lib/Transforms/Scalar/LoopUnroll.cpp
diff -u llvm/lib/Transforms/Scalar/LoopUnroll.cpp:1.43 llvm/lib/Transforms/Scalar/LoopUnroll.cpp:1.44
--- llvm/lib/Transforms/Scalar/LoopUnroll.cpp:1.43 Sat May 5 13:36:36 2007
+++ llvm/lib/Transforms/Scalar/LoopUnroll.cpp Sat May 5 13:49:57 2007
@@ -203,7 +203,8 @@
// 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 @@
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 @@
// 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 @@
}
-
- // 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);
}
}
More information about the llvm-commits
mailing list