Enhance hasConstantValue to ignore undef values in phi nodes. This allows it
[oota-llvm.git] / lib / Transforms / Utils / Local.cpp
index 821d1e13ebc5ba4d5c413e29e7f9b794f83293c9..b188884a4052333b6ba36229f0744ecc92460759 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/MathExtras.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Constants.h"
 #include "llvm/Instructions.h"
+#include "llvm/Intrinsics.h"
+#include <cerrno>
+#include <cmath>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -57,6 +61,18 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I) {
     
     // If we reach here, all incoming values are the same constant.
     return Result;
+  } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
+    if (Function *F = CI->getCalledFunction())
+      if (canConstantFoldCallTo(F)) {
+        std::vector<Constant*> Args;
+        for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
+          if (Constant *Op = dyn_cast<Constant>(CI->getOperand(i)))
+            Args.push_back(Op);
+          else
+            return 0;
+        return ConstantFoldCall(F, Args);
+      }
+    return 0;
   }
 
   Constant *Op0 = 0, *Op1 = 0;
@@ -79,6 +95,10 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I) {
   default: return 0;
   case Instruction::Cast:
     return ConstantExpr::getCast(Op0, I->getType());
+  case Instruction::Select:
+    if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(2)))
+      return ConstantExpr::getSelect(Op0, Op1, Op2);
+    return 0;
   case Instruction::GetElementPtr:
     std::vector<Constant*> IdxList;
     IdxList.reserve(I->getNumOperands()-1);
@@ -212,6 +232,86 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
   return false;
 }
 
+/// canConstantFoldCallTo - Return true if its even possible to fold a call to
+/// the specified function.
+bool llvm::canConstantFoldCallTo(Function *F) {
+  const std::string &Name = F->getName();
+
+  switch (F->getIntrinsicID()) {
+  case Intrinsic::isunordered: return true;
+  default: break;
+  }
+
+  return Name == "sin" || Name == "cos" || Name == "tan" || Name == "sqrt" ||
+         Name == "log" || Name == "log10" || Name == "exp" || Name == "pow" ||
+         Name == "acos" || Name == "asin" || Name == "atan" || Name == "fmod";
+}
+
+static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
+                                const Type *Ty) {
+  errno = 0;
+  V = NativeFP(V);
+  if (errno == 0)
+    return ConstantFP::get(Ty, V);
+  return 0;
+}
+
+/// ConstantFoldCall - Attempt to constant fold a call to the specified function
+/// with the specified arguments, returning null if unsuccessful.
+Constant *llvm::ConstantFoldCall(Function *F,
+                                 const std::vector<Constant*> &Operands) {
+  const std::string &Name = F->getName();
+  const Type *Ty = F->getReturnType();
+
+  if (Operands.size() == 1) {
+    if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
+      double V = Op->getValue();
+      if (Name == "sin")
+        return ConstantFP::get(Ty, sin(V));
+      else if (Name == "cos")
+        return ConstantFP::get(Ty, cos(V));
+      else if (Name == "tan")
+        return ConstantFP::get(Ty, tan(V));
+      else if (Name == "sqrt" && V >= 0)
+        return ConstantFP::get(Ty, sqrt(V));
+      else if (Name == "exp")
+        return ConstantFP::get(Ty, exp(V));
+      else if (Name == "log" && V > 0)
+        return ConstantFP::get(Ty, log(V));
+      else if (Name == "log10")
+        return ConstantFoldFP(log10, V, Ty);
+      else if (Name == "acos")
+        return ConstantFoldFP(acos, V, Ty);
+      else if (Name == "asin")
+        return ConstantFoldFP(asin, V, Ty);
+      else if (Name == "atan")
+        return ConstantFP::get(Ty, atan(V));
+    }
+  } else if (Operands.size() == 2) {
+    if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0]))
+      if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
+        double Op1V = Op1->getValue(), Op2V = Op2->getValue();
+
+        if (Name == "llvm.isunordered")
+          return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
+        else 
+        if (Name == "pow") {
+          errno = 0;
+          double V = pow(Op1V, Op2V);
+          if (errno == 0)
+            return ConstantFP::get(Ty, V);
+        } else if (Name == "fmod") {
+          errno = 0;
+          double V = fmod(Op1V, Op2V);
+          if (errno == 0)
+            return ConstantFP::get(Ty, V);
+        }
+      }
+  }
+  return 0;
+}
+
+
 
 
 //===----------------------------------------------------------------------===//
@@ -253,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
@@ -263,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;