diff options
Diffstat (limited to 'llvm/lib/VMCore')
-rw-r--r-- | llvm/lib/VMCore/BasicBlock.cpp | 10 | ||||
-rw-r--r-- | llvm/lib/VMCore/Instructions.cpp | 30 |
2 files changed, 39 insertions, 1 deletions
diff --git a/llvm/lib/VMCore/BasicBlock.cpp b/llvm/lib/VMCore/BasicBlock.cpp index 98d12d8f138..407080cf68b 100644 --- a/llvm/lib/VMCore/BasicBlock.cpp +++ b/llvm/lib/VMCore/BasicBlock.cpp @@ -189,8 +189,16 @@ void BasicBlock::removePredecessor(BasicBlock *Pred, // 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(); + } + } } } diff --git a/llvm/lib/VMCore/Instructions.cpp b/llvm/lib/VMCore/Instructions.cpp index 68b685e7fcb..e454d6b3a0d 100644 --- a/llvm/lib/VMCore/Instructions.cpp +++ b/llvm/lib/VMCore/Instructions.cpp @@ -132,6 +132,36 @@ void PHINode::resizeOperands(unsigned NumOps) { 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 |