adjust to constant folding api changes.
[oota-llvm.git] / lib / Analysis / ConstantFolding.cpp
index 7e802ba7dd600835c6ea88c5010dd3a15aacbe47..ef70ece7765bee26d7ca00f7d2376c79a4d9518c 100644 (file)
@@ -35,13 +35,13 @@ llvm::canConstantFoldCallTo(Function *F) {
   const std::string &Name = F->getName();
 
   switch (F->getIntrinsicID()) {
-  case Intrinsic::isunordered_f32:
-  case Intrinsic::isunordered_f64:
   case Intrinsic::sqrt_f32:
   case Intrinsic::sqrt_f64:
   case Intrinsic::bswap_i16:
   case Intrinsic::bswap_i32:
   case Intrinsic::bswap_i64:
+  case Intrinsic::powi_f32:
+  case Intrinsic::powi_f64:
   // FIXME: these should be constant folded as well
   //case Intrinsic::ctpop_i8:
   //case Intrinsic::ctpop_i16:
@@ -85,23 +85,24 @@ llvm::canConstantFoldCallTo(Function *F) {
   }
 }
 
-Constant *
-llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) {
+static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, 
+                                const Type *Ty) {
   errno = 0;
   V = NativeFP(V);
   if (errno == 0)
     return ConstantFP::get(Ty, V);
+  errno = 0;
   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) {
+llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) {
   const std::string &Name = F->getName();
   const Type *Ty = F->getReturnType();
 
-  if (Operands.size() == 1) {
+  if (NumOperands == 1) {
     if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
       double V = Op->getValue();
       switch (Name[0])
@@ -163,24 +164,21 @@ llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
         default:
           break;
       }
-    } else if (ConstantUInt *Op = dyn_cast<ConstantUInt>(Operands[0])) {
-      uint64_t V = Op->getValue();
+    } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
+      uint64_t V = Op->getZExtValue();
       if (Name == "llvm.bswap.i16")
-        return ConstantUInt::get(Ty, ByteSwap_16(V));
+        return ConstantInt::get(Ty, ByteSwap_16(V));
       else if (Name == "llvm.bswap.i32")
-        return ConstantUInt::get(Ty, ByteSwap_32(V));
+        return ConstantInt::get(Ty, ByteSwap_32(V));
       else if (Name == "llvm.bswap.i64")
-        return ConstantUInt::get(Ty, ByteSwap_64(V));
+        return ConstantInt::get(Ty, ByteSwap_64(V));
     }
-  } else if (Operands.size() == 2) {
+  } else if (NumOperands == 2) {
     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
       double Op1V = Op1->getValue();
       if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
         double Op2V = Op2->getValue();
 
-        if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64")
-          return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
-        else
         if (Name == "pow") {
           errno = 0;
           double V = pow(Op1V, Op2V);
@@ -191,8 +189,17 @@ llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
           double V = fmod(Op1V, Op2V);
           if (errno == 0)
             return ConstantFP::get(Ty, V);
-        } else if (Name == "atan2")
+        } else if (Name == "atan2") {
           return ConstantFP::get(Ty, atan2(Op1V,Op2V));
+        }
+      } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
+        if (Name == "llvm.powi.f32") {
+          return ConstantFP::get(Ty, std::pow((float)Op1V,
+                                              (int)Op2C->getZExtValue()));
+        } else if (Name == "llvm.powi.f64") {
+          return ConstantFP::get(Ty, std::pow((double)Op1V,
+                                              (int)Op2C->getZExtValue()));
+        }
       }
     }
   }