Skip to content

Commit

Permalink
Record PRE predecessors with a SmallVector instead of a DenseMap, and
Browse files Browse the repository at this point in the history
avoid a second pred_iterator traversal.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175001 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Dan Gohman committed Feb 12, 2013
1 parent e1d6403 commit 5f3c4a3
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,7 @@ bool GVN::processBlock(BasicBlock *BB) {
/// control flow patterns and attempts to perform simple PRE at the join point.
bool GVN::performPRE(Function &F) {
bool Changed = false;
DenseMap<BasicBlock*, Value*> predMap;
SmallVector<std::pair<Value*, BasicBlock*>, 8> predMap;
for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
BasicBlock *CurrentBlock = *DI;
Expand Down Expand Up @@ -2452,14 +2452,15 @@ bool GVN::performPRE(Function &F) {

Value* predV = findLeader(P, ValNo);
if (predV == 0) {
predMap.push_back(std::make_pair(static_cast<Value *>(0), P));
PREPred = P;
++NumWithout;
} else if (predV == CurInst) {
/* CurInst dominates this predecessor. */
NumWithout = 2;
break;
} else {
predMap[P] = predV;
predMap.push_back(std::make_pair(predV, P));
++NumWith;
}
}
Expand Down Expand Up @@ -2514,21 +2515,21 @@ bool GVN::performPRE(Function &F) {
PREInstr->insertBefore(PREPred->getTerminator());
PREInstr->setName(CurInst->getName() + ".pre");
PREInstr->setDebugLoc(CurInst->getDebugLoc());
predMap[PREPred] = PREInstr;
VN.add(PREInstr, ValNo);
++NumGVNPRE;

// Update the availability map to include the new instruction.
addToLeaderTable(ValNo, PREInstr, PREPred);

// Create a PHI to make the value available in this block.
pred_iterator PB = pred_begin(CurrentBlock), PE = pred_end(CurrentBlock);
PHINode* Phi = PHINode::Create(CurInst->getType(), std::distance(PB, PE),
PHINode* Phi = PHINode::Create(CurInst->getType(), predMap.size(),
CurInst->getName() + ".pre-phi",
CurrentBlock->begin());
for (pred_iterator PI = PB; PI != PE; ++PI) {
BasicBlock *P = *PI;
Phi->addIncoming(predMap[P], P);
for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
if (Value *V = predMap[i].first)
Phi->addIncoming(V, predMap[i].second);
else
Phi->addIncoming(PREInstr, PREPred);
}

VN.add(Phi, ValNo);
Expand Down

0 comments on commit 5f3c4a3

Please sign in to comment.