[llvm-commits] [llvm] r41131 - /llvm/trunk/lib/Transforms/Scalar/GVN.cpp
Owen Anderson
resistor at mac.com
Thu Aug 16 15:51:56 PDT 2007
Author: resistor
Date: Thu Aug 16 17:51:56 2007
New Revision: 41131
URL: http://llvm.org/viewvc/llvm-project?rev=41131&view=rev
Log:
Factor out some code into a helper function.
Modified:
llvm/trunk/lib/Transforms/Scalar/GVN.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/GVN.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVN.cpp?rev=41131&r1=41130&r2=41131&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/GVN.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/GVN.cpp Thu Aug 16 17:51:56 2007
@@ -672,6 +672,7 @@
bool top_level = false);
void dump(DenseMap<BasicBlock*, Value*>& d);
bool iterateOnFunction(Function &F);
+ Value* CollapsePhi(PHINode* p);
};
char GVN::ID = 0;
@@ -723,6 +724,21 @@
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.
/// available values are in Phis.
@@ -757,52 +773,23 @@
}
// Attempt to collapse PHI nodes that are trivially redundant
- Value* v = PN->hasConstantValue();
+ Value* v = CollapsePhi(PN);
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;
+ MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
- PN->eraseFromParent();
+ MD.removeInstruction(PN);
+ PN->replaceAllUsesWith(v);
- Phis[BB] = inst;
+ for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
+ E = Phis.end(); I != E; ++I)
+ if (I->second == PN)
+ I->second = v;
- 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();
- PN->eraseFromParent();
+ Phis[BB] = v;
- Phis[BB] = v;
-
- return v;
- }
+ return v;
}
// Cache our phi construction results
@@ -960,24 +947,16 @@
// Collapse PHI nodes
if (PHINode* p = dyn_cast<PHINode>(I)) {
- Value* constVal = p->hasConstantValue();
+ Value* constVal = CollapsePhi(p);
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);
+ 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)) {
More information about the llvm-commits
mailing list