Enhance hasConstantValue to ignore undef values in phi nodes. This allows it
[oota-llvm.git] / lib / Transforms / Utils / Local.cpp
index df831c78a42f8e1311d4699e2588fb3865689051..b188884a4052333b6ba36229f0744ecc92460759 100644 (file)
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/MathExtras.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Constants.h"
 #include "llvm/Instructions.h"
 #include <cmath>
 using namespace llvm;
 
-#if defined(__POWERPC__) && defined(__APPLE_CC__)
-// FIXME: Currently it seems that isnan didn't make its way into the Apple
-// C++ headers, although it IS in the C headers (which confuses autoconf
-// in a big way). This is a quick fix to get things compiling, until one of
-// us has time to write a more complicated autoconf test.
-extern "C" int isnan (double d);
-namespace std { int isnan (double d) { return ::isnan (d); } }
-#endif
-
 //===----------------------------------------------------------------------===//
 //  Local constant propagation...
 //
@@ -301,8 +293,9 @@ Constant *llvm::ConstantFoldCall(Function *F,
         double Op1V = Op1->getValue(), Op2V = Op2->getValue();
 
         if (Name == "llvm.isunordered")
-          return ConstantBool::get(std::isnan(Op1V) | std::isnan(Op2V));
-        else if (Name == "pow") {
+          return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
+        else 
+        if (Name == "pow") {
           errno = 0;
           double V = pow(Op1V, Op2V);
           if (errno == 0)
@@ -360,7 +353,8 @@ Value *llvm::hasConstantValue(PHINode *PN) {
   //
   Value *InVal = 0;
   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
-    if (PN->getIncomingValue(i) != PN)  // Not the PHI node itself...
+    if (PN->getIncomingValue(i) != PN &&  // Not the PHI node itself...
+        !isa<UndefValue>(PN->getIncomingValue(i)))
       if (InVal && PN->getIncomingValue(i) != InVal)
         return 0;  // Not the same, bail out.
       else
@@ -370,7 +364,7 @@ Value *llvm::hasConstantValue(PHINode *PN) {
   // that only has entries for itself.  In this case, there is no entry into the
   // loop, so kill the PHI.
   //
-  if (InVal == 0) InVal = Constant::getNullValue(PN->getType());
+  if (InVal == 0) InVal = UndefValue::get(PN->getType());
 
   // All of the incoming values are the same, return the value now.
   return InVal;