// ValueTable Class
//===----------------------------------------------------------------------===//
// ValueTable Class
//===----------------------------------------------------------------------===//
+static DominatorTree* DT;
+static AliasAnalysis* AA;
+static MemoryDependenceAnalysis* MD;
+
/// This class holds the mapping between values and value numbers. It is used
/// as an efficient mechanism to determine the expression-wise equivalence of
/// two values.
/// This class holds the mapping between values and value numbers. It is used
/// as an efficient mechanism to determine the expression-wise equivalence of
/// two values.
private:
DenseMap<Value*, uint32_t> valueNumbering;
DenseMap<Expression, uint32_t> expressionNumbering;
private:
DenseMap<Value*, uint32_t> valueNumbering;
DenseMap<Expression, uint32_t> expressionNumbering;
- AliasAnalysis* AA;
- MemoryDependenceAnalysis* MD;
uint32_t nextValueNumber;
uint32_t nextValueNumber;
void clear();
void erase(Value* v);
unsigned size();
void clear();
void erase(Value* v);
unsigned size();
- void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
- void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
}
Value* GVN::CollapsePhi(PHINode* p) {
}
Value* GVN::CollapsePhi(PHINode* p) {
- DominatorTree &DT = getAnalysis<DominatorTree>();
Value* constVal = p->hasConstantValue();
if (!constVal) return 0;
Value* constVal = p->hasConstantValue();
if (!constVal) return 0;
if (!inst)
return constVal;
if (!inst)
return constVal;
- if (DT.dominates(inst, p))
+ if (DT->dominates(inst, p))
if (isSafeReplacement(p, inst))
return inst;
return 0;
if (isSafeReplacement(p, inst))
return inst;
return 0;
PN->addIncoming(val, *PI);
}
PN->addIncoming(val, *PI);
}
- AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
- AA.copyValue(orig, PN);
+ AA->copyValue(orig, PN);
// Attempt to collapse PHI nodes that are trivially redundant
Value* v = CollapsePhi(PN);
// Attempt to collapse PHI nodes that are trivially redundant
Value* v = CollapsePhi(PN);
phiMap[orig->getPointerOperand()].insert(PN);
return PN;
}
phiMap[orig->getPointerOperand()].insert(PN);
return PN;
}
-
- MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
- MD.removeInstruction(PN);
+ MD->removeInstruction(PN);
PN->replaceAllUsesWith(v);
for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
PN->replaceAllUsesWith(v);
for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
/// non-local by performing PHI construction.
bool GVN::processNonLocalLoad(LoadInst* L,
SmallVectorImpl<Instruction*> &toErase) {
/// non-local by performing PHI construction.
bool GVN::processNonLocalLoad(LoadInst* L,
SmallVectorImpl<Instruction*> &toErase) {
- MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
-
// Find the non-local dependencies of the load
DenseMap<BasicBlock*, Value*> deps;
// Find the non-local dependencies of the load
DenseMap<BasicBlock*, Value*> deps;
- MD.getNonLocalDependency(L, deps);
+ MD->getNonLocalDependency(L, deps);
DenseMap<BasicBlock*, Value*> repl;
DenseMap<BasicBlock*, Value*> repl;
for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
I != E; ++I) {
if ((*I)->getParent() == L->getParent()) {
for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
I != E; ++I) {
if ((*I)->getParent() == L->getParent()) {
- MD.removeInstruction(L);
+ MD->removeInstruction(L);
L->replaceAllUsesWith(*I);
toErase.push_back(L);
NumGVNLoad++;
L->replaceAllUsesWith(*I);
toErase.push_back(L);
NumGVNLoad++;
SmallPtrSet<BasicBlock*, 4> visited;
Value* v = GetValueForBlock(L->getParent(), L, repl, true);
SmallPtrSet<BasicBlock*, 4> visited;
Value* v = GetValueForBlock(L->getParent(), L, repl, true);
- MD.removeInstruction(L);
+ MD->removeInstruction(L);
L->replaceAllUsesWith(v);
toErase.push_back(L);
NumGVNLoad++;
L->replaceAllUsesWith(v);
toErase.push_back(L);
NumGVNLoad++;
LoadInst*& last = lastLoad[pointer];
// ... to a pointer that has been loaded from before...
LoadInst*& last = lastLoad[pointer];
// ... to a pointer that has been loaded from before...
- MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
bool removedNonLocal = false;
bool removedNonLocal = false;
- Instruction* dep = MD.getDependency(L);
+ Instruction* dep = MD->getDependency(L);
if (dep == MemoryDependenceAnalysis::NonLocal &&
L->getParent() != &L->getParent()->getParent()->getEntryBlock()) {
removedNonLocal = processNonLocalLoad(L, toErase);
if (dep == MemoryDependenceAnalysis::NonLocal &&
L->getParent() != &L->getParent()->getParent()->getEntryBlock()) {
removedNonLocal = processNonLocalLoad(L, toErase);
if (StoreInst* S = dyn_cast<StoreInst>(dep)) {
if (S->getPointerOperand() == pointer) {
// Remove it!
if (StoreInst* S = dyn_cast<StoreInst>(dep)) {
if (S->getPointerOperand() == pointer) {
// Remove it!
- MD.removeInstruction(L);
+ MD->removeInstruction(L);
L->replaceAllUsesWith(S->getOperand(0));
toErase.push_back(L);
L->replaceAllUsesWith(S->getOperand(0));
toErase.push_back(L);
break;
} else if (dep == last) {
// Remove it!
break;
} else if (dep == last) {
// Remove it!
- MD.removeInstruction(L);
+ MD->removeInstruction(L);
L->replaceAllUsesWith(last);
toErase.push_back(L);
L->replaceAllUsesWith(last);
toErase.push_back(L);
- dep = MD.getDependency(L, dep);
+ dep = MD->getDependency(L, dep);
// If this load depends directly on an allocation, there isn't
// anything stored there; therefore, we can optimize this load
// to undef.
// If this load depends directly on an allocation, there isn't
// anything stored there; therefore, we can optimize this load
// to undef.
- MD.removeInstruction(L);
+ MD->removeInstruction(L);
L->replaceAllUsesWith(UndefValue::get(L->getType()));
toErase.push_back(L);
L->replaceAllUsesWith(UndefValue::get(L->getType()));
toErase.push_back(L);
Value* repl = find_leader(currAvail, num);
// Remove it!
Value* repl = find_leader(currAvail, num);
// Remove it!
- MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
- MD.removeInstruction(I);
+ MD->removeInstruction(I);
VN.erase(I);
I->replaceAllUsesWith(repl);
VN.erase(I);
I->replaceAllUsesWith(repl);
// function.
//
bool GVN::runOnFunction(Function& F) {
// function.
//
bool GVN::runOnFunction(Function& F) {
- VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
- VN.setMemDep(&getAnalysis<MemoryDependenceAnalysis>());
+ DT = &getAnalysis<DominatorTree>();
+ AA = &getAnalysis<AliasAnalysis>();
+ MD = &getAnalysis<MemoryDependenceAnalysis>();
bool changed = false;
bool shouldContinue = true;
bool changed = false;
bool shouldContinue = true;
bool changed_function = false;
bool changed_function = false;
- DominatorTree &DT = getAnalysis<DominatorTree>();
-
SmallVector<Instruction*, 8> toErase;
DenseMap<Value*, LoadInst*> lastSeenLoad;
DenseMap<DomTreeNode*, size_t> numChildrenVisited;
// Top-down walk of the dominator tree
SmallVector<Instruction*, 8> toErase;
DenseMap<Value*, LoadInst*> lastSeenLoad;
DenseMap<DomTreeNode*, size_t> numChildrenVisited;
// Top-down walk of the dominator tree
- for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
- E = df_end(DT.getRootNode()); DI != E; ++DI) {
+ for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
+ E = df_end(DT->getRootNode()); DI != E; ++DI) {
// Get the set to update for this block
ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
// Get the set to update for this block
ValueNumberedSet& currAvail = availableOut[DI->getBlock()];