From 1defe2df5d563e519ae5dd6c19d9519f0abddbfc Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Thu, 16 Aug 2007 22:51:56 +0000 Subject: [PATCH] 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 --- lib/Transforms/Scalar/GVN.cpp | 89 +++++++++++++---------------------- 1 file changed, 34 insertions(+), 55 deletions(-) diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 0b0825544d7..75edd8eaf9e 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -672,6 +672,7 @@ namespace { bool top_level = false); void dump(DenseMap& d); bool iterateOnFunction(Function &F); + Value* CollapsePhi(PHINode* p); }; char GVN::ID = 0; @@ -723,6 +724,21 @@ void GVN::dump(DenseMap& d) { printf("}\n"); } +Value* GVN::CollapsePhi(PHINode* p) { + DominatorTree &DT = getAnalysis(); + Value* constVal = p->hasConstantValue(); + + if (constVal) { + if (Instruction* inst = dyn_cast(constVal)) { + if (DT.dominates(inst, p)) + return inst; + } else { + return constVal; + } + } + + return 0; +} /// GetValueForBlock - Get the value to use within the specified basic block. /// 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 - Value* v = PN->hasConstantValue(); + Value* v = CollapsePhi(PN); if (v) { - if (Instruction* inst = dyn_cast(v)) { - DominatorTree &DT = getAnalysis(); - if (DT.dominates(inst, PN)) { - MemoryDependenceAnalysis& MD = getAnalysis(); - - MD.removeInstruction(PN); - PN->replaceAllUsesWith(inst); + MemoryDependenceAnalysis& MD = getAnalysis(); - SmallVector toRemove; - for (DenseMap::iterator I = Phis.begin(), - E = Phis.end(); I != E; ++I) - if (I->second == PN) - toRemove.push_back(I->first); - for (SmallVector::iterator I = toRemove.begin(), - E= toRemove.end(); I != E; ++I) - Phis[*I] = inst; + MD.removeInstruction(PN); + PN->replaceAllUsesWith(v); - PN->eraseFromParent(); + for (DenseMap::iterator I = Phis.begin(), + E = Phis.end(); I != E; ++I) + if (I->second == PN) + I->second = v; - Phis[BB] = inst; + PN->eraseFromParent(); - return inst; - } - } else { - MemoryDependenceAnalysis& MD = getAnalysis(); - - MD.removeInstruction(PN); - PN->replaceAllUsesWith(v); - - SmallVector toRemove; - for (DenseMap::iterator I = Phis.begin(), - E = Phis.end(); I != E; ++I) - if (I->second == PN) - toRemove.push_back(I->first); - for (SmallVector::iterator I = toRemove.begin(), - E= toRemove.end(); I != E; ++I) - Phis[*I] = v; + Phis[BB] = v; - PN->eraseFromParent(); - - Phis[BB] = v; - - return v; - } + return v; } // Cache our phi construction results @@ -960,24 +947,16 @@ bool GVN::processInstruction(Instruction* I, // Collapse PHI nodes if (PHINode* p = dyn_cast(I)) { - Value* constVal = p->hasConstantValue(); + Value* constVal = CollapsePhi(p); if (constVal) { - if (Instruction* inst = dyn_cast(constVal)) { - DominatorTree &DT = getAnalysis(); - 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); + 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); - } + p->replaceAllUsesWith(constVal); + toErase.push_back(p); } // Perform value-number based elimination } else if (currAvail.test(num)) { -- 2.34.1