aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2002-08-20 15:35:35 +0000
committerChris Lattner <sabre@nondot.org>2002-08-20 15:35:35 +0000
commitf02c46834a2494f1318d9fbccc1fc7c7299360ee (patch)
tree3b161025102bfdf2e0aa36b8c9a5138f2832c3fd
parentdce942798563f7077ef37ed97799858a5cf5f609 (diff)
- implemented instcombine of phi (X, X, X) -> X
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3397 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 088a758c7f..71c2cc6e3f 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -594,10 +594,21 @@ Instruction *InstCombiner::visitCastInst(CastInst &CI) {
//
Instruction *InstCombiner::visitPHINode(PHINode &PN) {
// If the PHI node only has one incoming value, eliminate the PHI node...
+ if (PN.getNumIncomingValues() == 0)
+ return ReplaceInstUsesWith(PN, Constant::getNullValue(PN.getType()));
if (PN.getNumIncomingValues() == 1)
return ReplaceInstUsesWith(PN, PN.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 = PN.getIncomingValue(0);
+ for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i)
+ if (PN.getIncomingValue(i) != InVal)
+ return 0; // Not the same, bail out.
- return 0;
+ // All of the incoming values are the same, replace the PHI node now.
+ return ReplaceInstUsesWith(PN, InVal);
}