[llvm-commits] CVS: llvm/lib/VMCore/BasicBlock.cpp Instructions.cpp
Nate Begeman
natebegeman at mac.com
Thu Aug 4 16:24:31 PDT 2005
Changes in directory llvm/lib/VMCore:
BasicBlock.cpp updated: 1.64 -> 1.65
Instructions.cpp updated: 1.20 -> 1.21
---
Log message:
Fix a fixme in CondPropagate.cpp by moving a PhiNode optimization into
BasicBlock's removePredecessor routine. This requires shuffling around
the definition and implementation of hasContantValue from Utils.h,cpp into
Instructions.h,cpp
---
Diffs of the changes: (+39 -1)
BasicBlock.cpp | 10 +++++++++-
Instructions.cpp | 30 ++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 1 deletion(-)
Index: llvm/lib/VMCore/BasicBlock.cpp
diff -u llvm/lib/VMCore/BasicBlock.cpp:1.64 llvm/lib/VMCore/BasicBlock.cpp:1.65
--- llvm/lib/VMCore/BasicBlock.cpp:1.64 Sat Apr 23 16:38:35 2005
+++ llvm/lib/VMCore/BasicBlock.cpp Thu Aug 4 18:24:19 2005
@@ -189,8 +189,16 @@
// Okay, now we know that we need to remove predecessor #pred_idx from all
// PHI nodes. Iterate over each PHI node fixing them up
PHINode *PN;
- for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ++II)
+ for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ++II) {
PN->removeIncomingValue(Pred, false);
+ // If all incoming values to the Phi are the same, we can replace the Phi
+ // with that value.
+ if (Value *PNV = PN->hasConstantValue())
+ if (!isa<Instruction>(PNV)) {
+ PN->replaceAllUsesWith(PNV);
+ PN->eraseFromParent();
+ }
+ }
}
}
Index: llvm/lib/VMCore/Instructions.cpp
diff -u llvm/lib/VMCore/Instructions.cpp:1.20 llvm/lib/VMCore/Instructions.cpp:1.21
--- llvm/lib/VMCore/Instructions.cpp:1.20 Sat Jun 18 13:34:52 2005
+++ llvm/lib/VMCore/Instructions.cpp Thu Aug 4 18:24:19 2005
@@ -132,6 +132,36 @@
OperandList = NewOps;
}
+/// hasConstantValue - If the specified PHI node always merges together the same
+/// value, return the value, otherwise return null.
+///
+Value *PHINode::hasConstantValue() {
+ // If the PHI node only has one incoming value, eliminate the PHI node...
+ if (getNumIncomingValues() == 1)
+ return getIncomingValue(0);
+
+ // Otherwise if all of the incoming values are the same for the PHI, replace
+ // the PHI node with the incoming value.
+ //
+ Value *InVal = 0;
+ for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
+ if (getIncomingValue(i) != this && // Not the PHI node itself...
+ !isa<UndefValue>(getIncomingValue(i)))
+ if (InVal && getIncomingValue(i) != InVal)
+ return 0; // Not the same, bail out.
+ else
+ InVal = getIncomingValue(i);
+
+ // The only case that could cause InVal to be null is if we have a PHI node
+ // that only has entries for itself. In this case, there is no entry into the
+ // loop, so kill the PHI.
+ //
+ if (InVal == 0) InVal = UndefValue::get(getType());
+
+ // All of the incoming values are the same, return the value now.
+ return InVal;
+}
+
//===----------------------------------------------------------------------===//
// CallInst Implementation
More information about the llvm-commits
mailing list