From 54753e279032efd7d7f9aba12dbbb5b7ba8a20ce Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 7 May 2002 20:44:59 +0000 Subject: [PATCH] Implement constant propogation of PHI instructions like this: X = phi(0, 0, 0) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2543 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 0dae62865c9..a366970fae8 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -5,7 +5,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ConstantHandling.h" -#include "llvm/Instruction.h" +#include "llvm/iPHINode.h" #include AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules", @@ -15,6 +15,22 @@ AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules", // If successful, the constant result is returned, if not, null is returned. // Constant *ConstantFoldInstruction(Instruction *I) { + if (PHINode *PN = dyn_cast(I)) { + if (PN->getNumIncomingValues() == 0) + return Constant::getNullValue(PN->getType()); + + Constant *Result = dyn_cast(PN->getIncomingValue(0)); + if (Result == 0) return 0; + + // Handle PHI nodes specially here... + for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) + if (PN->getIncomingValue(i) != Result) + return 0; // Not all the same incoming constants... + + // If we reach here, all incoming values are the same constant. + return Result; + } + Constant *Op0 = 0; Constant *Op1 = 0;