Implement constant propogation of PHI instructions like this:
authorChris Lattner <sabre@nondot.org>
Tue, 7 May 2002 20:44:59 +0000 (20:44 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 7 May 2002 20:44:59 +0000 (20:44 +0000)
  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

index 0dae62865c9ddb0fed23d2517b686b62d5b8cf66..a366970fae821d321383d6c8f7deee438e632ca3 100644 (file)
@@ -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;