DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
+ typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
+ PhiMapType phiMap;
+
+
// This transformation requires dominator postdominator info
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
DenseMap<BasicBlock*, Value*> &Phis,
bool top_level = false);
void dump(DenseMap<BasicBlock*, Value*>& d);
+ bool iterateOnFunction(Function &F);
};
char GVN::ID = 0;
bool top_level) {
// If we have already computed this value, return the previously computed val.
- Value *&V = Phis[BB];
- if (V && ! top_level) return V;
+ DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
+ if (V != Phis.end() && !top_level) return V->second;
BasicBlock* singlePred = BB->getSinglePredecessor();
- if (singlePred)
- return V = GetValueForBlock(singlePred, orig, Phis);
-
+ if (singlePred) {
+ Value *ret = GetValueForBlock(singlePred, orig, Phis);
+ Phis[BB] = ret;
+ return ret;
+ }
// Otherwise, the idom is the loop, so we need to insert a PHI node. Do so
// now, then get values to fill in the incoming values for the PHI.
PHINode *PN = new PHINode(orig->getType(), orig->getName()+".rle",
BB->begin());
PN->reserveOperandSpace(std::distance(pred_begin(BB), pred_end(BB)));
- V = PN;
- bool all_same = true;
- Value* first = 0;
+ if (Phis.count(BB) == 0)
+ Phis.insert(std::make_pair(BB, PN));
// Fill in the incoming values for the block.
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Value* val = GetValueForBlock(*PI, orig, Phis);
- if (first == 0)
- first = val;
- else if (all_same && first != val)
- all_same = false;
PN->addIncoming(val, *PI);
}
- if (all_same) {
- MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
-
- MD.removeInstruction(PN);
- PN->replaceAllUsesWith(first);
-
- 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] = first;
-
- PN->eraseFromParent();
-
- Phis[BB] = first;
-
- return first;
+ Value* v = PN->hasConstantValue();
+ if (v) {
+ if (Instruction* inst = dyn_cast<Instruction>(v)) {
+ DominatorTree &DT = getAnalysis<DominatorTree>();
+ if (DT.dominates(inst, PN)) {
+ MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+
+ MD.removeInstruction(PN);
+ PN->replaceAllUsesWith(inst);
+
+ 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] = inst;
+
+ PN->eraseFromParent();
+
+ Phis[BB] = inst;
+
+ return inst;
+ }
+ } 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;
+ }
}
+ phiMap[orig->getPointerOperand()].insert(PN);
return PN;
}
MD.getNonLocalDependency(L, deps);
DenseMap<BasicBlock*, Value*> repl;
+
for (DenseMap<BasicBlock*, Value*>::iterator I = deps.begin(), E = deps.end();
I != E; ++I)
if (I->second == MemoryDependenceAnalysis::None) {
continue;
}else if (StoreInst* S = dyn_cast<StoreInst>(I->second)) {
if (S->getPointerOperand() == L->getPointerOperand())
- repl.insert(std::make_pair(I->first, S->getOperand(0)));
+ repl[I->first] = S->getOperand(0);
else
return false;
} else if (LoadInst* LD = dyn_cast<LoadInst>(I->second)) {
if (LD->getPointerOperand() == L->getPointerOperand())
- repl.insert(std::make_pair(I->first, LD));
+ repl[I->first] = LD;
else
return false;
} else {
return false;
}
+ SmallPtrSet<Instruction*, 4>& p = phiMap[L->getPointerOperand()];
+ for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
+ I != E; ++I) {
+ if ((*I)->getParent() == L->getParent()) {
+ MD.removeInstruction(L);
+ L->replaceAllUsesWith(*I);
+ toErase.push_back(L);
+ NumGVNLoad++;
+
+ return true;
+ } else {
+ repl.insert(std::make_pair((*I)->getParent(), *I));
+ }
+ }
+
SmallPtrSet<BasicBlock*, 4> visited;
Value* v = GetValueForBlock(L->getParent(), L, repl, true);
MD.removeInstruction(L);
L->replaceAllUsesWith(v);
toErase.push_back(L);
+ NumGVNLoad++;
return true;
}
// ... to a pointer that has been loaded from before...
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+ bool removedNonLocal = false;
Instruction* dep = MD.getDependency(L);
if (dep == MemoryDependenceAnalysis::NonLocal &&
- L->getParent() != &L->getParent()->getParent()->getEntryBlock())
- processNonLocalLoad(L, toErase);
+ L->getParent() != &L->getParent()->getParent()->getEntryBlock()) {
+ removedNonLocal = processNonLocalLoad(L, toErase);
+
+ if (!removedNonLocal)
+ last = L;
+
+ return removedNonLocal;
+ }
+
+
bool deletedLoad = false;
while (dep != MemoryDependenceAnalysis::None &&
return deletedLoad;
}
-/// buildsets_availout - When calculating availability, handle an instruction
+/// processInstruction - When calculating availability, handle an instruction
/// by inserting it into the appropriate sets
bool GVN::processInstruction(Instruction* I,
ValueNumberedSet& currAvail,
unsigned num = VN.lookup_or_add(I);
- if (currAvail.test(num)) {
+ if (PHINode* p = dyn_cast<PHINode>(I)) {
+ Value* constVal = p->hasConstantValue();
+
+ if (constVal) {
+ if (Instruction* inst = dyn_cast<Instruction>(constVal)) {
+ DominatorTree &DT = getAnalysis<DominatorTree>();
+ if (DT.dominates(inst, p)) {
+ for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
+ PI != PE; ++PI)
+ if (PI->second.count(p))
+ PI->second.erase(p);
+
+ p->replaceAllUsesWith(inst);
+ toErase.push_back(p);
+ }
+ } else {
+ p->replaceAllUsesWith(constVal);
+ toErase.push_back(p);
+ }
+ }
+ } else if (currAvail.test(num)) {
Value* repl = find_leader(currAvail, num);
VN.erase(I);
// GVN::runOnFunction - This is the main transformation entry point for a
// function.
//
-bool GVN::runOnFunction(Function &F) {
+bool GVN::runOnFunction(Function& F) {
+ bool changed = false;
+ bool shouldContinue = true;
+
+ while (shouldContinue) {
+ shouldContinue = iterateOnFunction(F);
+ changed |= shouldContinue;
+ }
+
+ return changed;
+}
+
+
+// GVN::iterateOnFunction - Executes one iteration of GVN
+bool GVN::iterateOnFunction(Function &F) {
// Clean out global sets from any previous functions
VN.clear();
availableOut.clear();
+ phiMap.clear();
bool changed_function = false;