Newer
Older
Owen Anderson
committed
delete PREInstr;
DEBUG(verifyRemoved(PREInstr));
Owen Anderson
committed
continue;
}
Owen Anderson
committed
PREInstr->insertBefore(PREPred->getTerminator());
Chris Lattner
committed
PREInstr->setName(CurInst->getName() + ".pre");
Owen Anderson
committed
predMap[PREPred] = PREInstr;
Owen Anderson
committed
NumGVNPRE++;
Owen Anderson
committed
// Update the availability map to include the new instruction.
localAvail[PREPred]->table.insert(std::make_pair(ValNo, PREInstr));
Owen Anderson
committed
// Create a PHI to make the value available in this block.
Chris Lattner
committed
PHINode* Phi = PHINode::Create(CurInst->getType(),
CurInst->getName() + ".pre-phi",
Owen Anderson
committed
CurrentBlock->begin());
for (pred_iterator PI = pred_begin(CurrentBlock),
PE = pred_end(CurrentBlock); PI != PE; ++PI)
Owen Anderson
committed
Phi->addIncoming(predMap[*PI], *PI);
VN.add(Phi, ValNo);
localAvail[CurrentBlock]->table[ValNo] = Phi;
Chris Lattner
committed
CurInst->replaceAllUsesWith(Phi);
if (isa<PointerType>(Phi->getType()))
MD->invalidateCachedPointerInfo(Phi);
Chris Lattner
committed
VN.erase(CurInst);
DEBUG(errs() << "GVN PRE removed: " << *CurInst << '\n');
Chris Lattner
committed
MD->removeInstruction(CurInst);
CurInst->eraseFromParent();
Chris Lattner
committed
Changed = true;
Owen Anderson
committed
}
}
for (SmallVector<std::pair<TerminatorInst*, unsigned>, 4>::iterator
Anton Korobeynikov
committed
I = toSplit.begin(), E = toSplit.end(); I != E; ++I)
SplitCriticalEdge(I->first, I->second, this);
Anton Korobeynikov
committed
return Changed || toSplit.size();
Owen Anderson
committed
}
/// iterateOnFunction - Executes one iteration of GVN
for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
if (DI->getIDom())
localAvail[DI->getBlock()] =
new ValueNumberScope(localAvail[DI->getIDom()->getBlock()]);
else
localAvail[DI->getBlock()] = new ValueNumberScope(0);
}
Owen Anderson
committed
// Top-down walk of the dominator tree
#if 0
// Needed for value numbering with phi construction to work.
Owen Anderson
committed
ReversePostOrderTraversal<Function*> RPOT(&F);
for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(),
RE = RPOT.end(); RI != RE; ++RI)
Changed |= processBlock(*RI);
#else
for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
DE = df_end(DT->getRootNode()); DI != DE; ++DI)
Changed |= processBlock(DI->getBlock());
#endif
Owen Anderson
committed
}
void GVN::cleanupGlobalSets() {
VN.clear();
phiMap.clear();
for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
I = localAvail.begin(), E = localAvail.end(); I != E; ++I)
delete I->second;
localAvail.clear();
}
Bill Wendling
committed
/// verifyRemoved - Verify that the specified instruction does not occur in our
/// internal data structures.
void GVN::verifyRemoved(const Instruction *Inst) const {
VN.verifyRemoved(Inst);
// Walk through the PHI map to make sure the instruction isn't hiding in there
// somewhere.
for (PhiMapType::iterator
I = phiMap.begin(), E = phiMap.end(); I != E; ++I) {
assert(I->first != Inst && "Inst is still a key in PHI map!");
for (SmallPtrSet<Instruction*, 4>::iterator
II = I->second.begin(), IE = I->second.end(); II != IE; ++II) {
assert(*II != Inst && "Inst is still a value in PHI map!");
}
}
// Walk through the value number scope to make sure the instruction isn't
// ferreted away in it.
for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
I = localAvail.begin(), E = localAvail.end(); I != E; ++I) {
const ValueNumberScope *VNS = I->second;
while (VNS) {
for (DenseMap<uint32_t, Value*>::iterator
II = VNS->table.begin(), IE = VNS->table.end(); II != IE; ++II) {
assert(II->second != Inst && "Inst still in value numbering scope!");
}
VNS = VNS->parent;
}
}