From a472c4a2800122429c435a5b5bbdfb12e5496891 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Mon, 12 May 2008 20:15:55 +0000 Subject: [PATCH] Go back to passing the analyses around as parameters. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50995 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/GVN.cpp | 54 +++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 3a05c33e9f6..66f5df55aa3 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -42,10 +42,6 @@ STATISTIC(NumGVNLoad, "Number of loads deleted"); // 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. @@ -133,6 +129,9 @@ namespace { private: DenseMap valueNumbering; DenseMap expressionNumbering; + AliasAnalysis* AA; + MemoryDependenceAnalysis* MD; + DominatorTree* DT; uint32_t nextValueNumber; @@ -156,6 +155,9 @@ namespace { void clear(); void erase(Value* v); unsigned size(); + void setAliasAnalysis(AliasAnalysis* A) { AA = A; } + void setMemDep(MemoryDependenceAnalysis* M) { MD = M; } + void setDomTree(DominatorTree* D) { DT = D; } }; } @@ -734,6 +736,7 @@ void GVN::dump(DenseMap& d) { } Value* GVN::CollapsePhi(PHINode* p) { + DominatorTree &DT = getAnalysis(); Value* constVal = p->hasConstantValue(); if (!constVal) return 0; @@ -742,7 +745,7 @@ Value* GVN::CollapsePhi(PHINode* p) { if (!inst) return constVal; - if (DT->dominates(inst, p)) + if (DT.dominates(inst, p)) if (isSafeReplacement(p, inst)) return inst; return 0; @@ -793,7 +796,8 @@ Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig, PN->addIncoming(val, *PI); } - AA->copyValue(orig, PN); + AliasAnalysis& AA = getAnalysis(); + AA.copyValue(orig, PN); // Attempt to collapse PHI nodes that are trivially redundant Value* v = CollapsePhi(PN); @@ -802,8 +806,10 @@ Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig, phiMap[orig->getPointerOperand()].insert(PN); return PN; } + + MemoryDependenceAnalysis& MD = getAnalysis(); - MD->removeInstruction(PN); + MD.removeInstruction(PN); PN->replaceAllUsesWith(v); for (DenseMap::iterator I = Phis.begin(), @@ -821,9 +827,11 @@ Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig, /// non-local by performing PHI construction. bool GVN::processNonLocalLoad(LoadInst* L, SmallVectorImpl &toErase) { + MemoryDependenceAnalysis& MD = getAnalysis(); + // Find the non-local dependencies of the load DenseMap deps; - MD->getNonLocalDependency(L, deps); + MD.getNonLocalDependency(L, deps); DenseMap repl; @@ -854,7 +862,7 @@ bool GVN::processNonLocalLoad(LoadInst* L, for (SmallPtrSet::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++; @@ -868,7 +876,7 @@ bool GVN::processNonLocalLoad(LoadInst* L, SmallPtrSet visited; Value* v = GetValueForBlock(L->getParent(), L, repl, true); - MD->removeInstruction(L); + MD.removeInstruction(L); L->replaceAllUsesWith(v); toErase.push_back(L); NumGVNLoad++; @@ -889,8 +897,9 @@ bool GVN::processLoad(LoadInst *L, DenseMap &lastLoad, LoadInst*& last = lastLoad[pointer]; // ... to a pointer that has been loaded from before... + MemoryDependenceAnalysis& MD = getAnalysis(); 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); @@ -913,7 +922,7 @@ bool GVN::processLoad(LoadInst *L, DenseMap &lastLoad, if (StoreInst* S = dyn_cast(dep)) { if (S->getPointerOperand() == pointer) { // Remove it! - MD->removeInstruction(L); + MD.removeInstruction(L); L->replaceAllUsesWith(S->getOperand(0)); toErase.push_back(L); @@ -930,7 +939,7 @@ bool GVN::processLoad(LoadInst *L, DenseMap &lastLoad, break; } else if (dep == last) { // Remove it! - MD->removeInstruction(L); + MD.removeInstruction(L); L->replaceAllUsesWith(last); toErase.push_back(L); @@ -939,7 +948,7 @@ bool GVN::processLoad(LoadInst *L, DenseMap &lastLoad, break; } else { - dep = MD->getDependency(L, dep); + dep = MD.getDependency(L, dep); } } @@ -961,7 +970,7 @@ bool GVN::processLoad(LoadInst *L, DenseMap &lastLoad, // 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); @@ -1009,7 +1018,8 @@ bool GVN::processInstruction(Instruction *I, ValueNumberedSet &currAvail, Value* repl = find_leader(currAvail, num); // Remove it! - MD->removeInstruction(I); + MemoryDependenceAnalysis& MD = getAnalysis(); + MD.removeInstruction(I); VN.erase(I); I->replaceAllUsesWith(repl); @@ -1027,9 +1037,9 @@ bool GVN::processInstruction(Instruction *I, ValueNumberedSet &currAvail, // function. // bool GVN::runOnFunction(Function& F) { - DT = &getAnalysis(); - AA = &getAnalysis(); - MD = &getAnalysis(); + VN.setAliasAnalysis(&getAnalysis()); + VN.setMemDep(&getAnalysis()); + VN.setDomTree(&getAnalysis()); bool changed = false; bool shouldContinue = true; @@ -1052,13 +1062,15 @@ bool GVN::iterateOnFunction(Function &F) { bool changed_function = false; + DominatorTree &DT = getAnalysis(); + SmallVector toErase; DenseMap lastSeenLoad; DenseMap numChildrenVisited; // Top-down walk of the dominator tree - for (df_iterator DI = df_begin(DT->getRootNode()), - E = df_end(DT->getRootNode()); DI != E; ++DI) { + for (df_iterator 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()];