Factor out some code into a helper function.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41131 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2007-08-16 22:51:56 +00:00
parent fe5b4393d4
commit 1defe2df5d

View File

@ -672,6 +672,7 @@ namespace {
bool top_level = false); bool top_level = false);
void dump(DenseMap<BasicBlock*, Value*>& d); void dump(DenseMap<BasicBlock*, Value*>& d);
bool iterateOnFunction(Function &F); bool iterateOnFunction(Function &F);
Value* CollapsePhi(PHINode* p);
}; };
char GVN::ID = 0; char GVN::ID = 0;
@ -723,6 +724,21 @@ void GVN::dump(DenseMap<BasicBlock*, Value*>& d) {
printf("}\n"); printf("}\n");
} }
Value* GVN::CollapsePhi(PHINode* p) {
DominatorTree &DT = getAnalysis<DominatorTree>();
Value* constVal = p->hasConstantValue();
if (constVal) {
if (Instruction* inst = dyn_cast<Instruction>(constVal)) {
if (DT.dominates(inst, p))
return inst;
} else {
return constVal;
}
}
return 0;
}
/// GetValueForBlock - Get the value to use within the specified basic block. /// GetValueForBlock - Get the value to use within the specified basic block.
/// available values are in Phis. /// available values are in Phis.
@ -757,52 +773,23 @@ Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig,
} }
// Attempt to collapse PHI nodes that are trivially redundant // Attempt to collapse PHI nodes that are trivially redundant
Value* v = PN->hasConstantValue(); Value* v = CollapsePhi(PN);
if (v) { if (v) {
if (Instruction* inst = dyn_cast<Instruction>(v)) { MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
DominatorTree &DT = getAnalysis<DominatorTree>();
if (DT.dominates(inst, PN)) {
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
MD.removeInstruction(PN); MD.removeInstruction(PN);
PN->replaceAllUsesWith(inst); PN->replaceAllUsesWith(v);
SmallVector<BasicBlock*, 4> toRemove; for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(), E = Phis.end(); I != E; ++I)
E = Phis.end(); I != E; ++I) if (I->second == PN)
if (I->second == PN) I->second = v;
toRemove.push_back(I->first);
for (SmallVector<BasicBlock*, 4>::iterator I = toRemove.begin(),
E= toRemove.end(); I != E; ++I)
Phis[*I] = inst;
PN->eraseFromParent(); PN->eraseFromParent();
Phis[BB] = inst; Phis[BB] = v;
return inst; return v;
}
} else {
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
MD.removeInstruction(PN);
PN->replaceAllUsesWith(v);
SmallVector<BasicBlock*, 4> toRemove;
for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
E = Phis.end(); I != E; ++I)
if (I->second == PN)
toRemove.push_back(I->first);
for (SmallVector<BasicBlock*, 4>::iterator I = toRemove.begin(),
E= toRemove.end(); I != E; ++I)
Phis[*I] = v;
PN->eraseFromParent();
Phis[BB] = v;
return v;
}
} }
// Cache our phi construction results // Cache our phi construction results
@ -960,24 +947,16 @@ bool GVN::processInstruction(Instruction* I,
// Collapse PHI nodes // Collapse PHI nodes
if (PHINode* p = dyn_cast<PHINode>(I)) { if (PHINode* p = dyn_cast<PHINode>(I)) {
Value* constVal = p->hasConstantValue(); Value* constVal = CollapsePhi(p);
if (constVal) { if (constVal) {
if (Instruction* inst = dyn_cast<Instruction>(constVal)) { for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
DominatorTree &DT = getAnalysis<DominatorTree>(); PI != PE; ++PI)
if (DT.dominates(inst, p)) { if (PI->second.count(p))
for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end(); PI->second.erase(p);
PI != PE; ++PI)
if (PI->second.count(p))
PI->second.erase(p);
p->replaceAllUsesWith(inst); p->replaceAllUsesWith(constVal);
toErase.push_back(p); toErase.push_back(p);
}
} else {
p->replaceAllUsesWith(constVal);
toErase.push_back(p);
}
} }
// Perform value-number based elimination // Perform value-number based elimination
} else if (currAvail.test(num)) { } else if (currAvail.test(num)) {