diff options
author | Chris Lattner <sabre@nondot.org> | 2002-05-07 20:44:59 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2002-05-07 20:44:59 +0000 |
commit | 54753e279032efd7d7f9aba12dbbb5b7ba8a20ce (patch) | |
tree | 245e2294ef7b94ac66e6dbbc0e91241779f74307 | |
parent | 0651a9d207470bbc2deb33da8a66de3217bdfd56 (diff) |
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
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 0dae62865c..a366970fae 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 <cmath> 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<PHINode>(I)) { + if (PN->getNumIncomingValues() == 0) + return Constant::getNullValue(PN->getType()); + + Constant *Result = dyn_cast<Constant>(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; |