Correctly extract the ValueType from a VTSDNode.
[oota-llvm.git] / lib / CodeGen / IntrinsicLowering.cpp
index e6ba069ac7149c49eca613d844b6af97e7da8746..bd3d74d27ac82203590d07e42b49abea3c9e5733 100644 (file)
@@ -20,6 +20,7 @@
 #include "llvm/Support/Streams.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/STLExtras.h"
 using namespace llvm;
 
 template <class ArgIt>
@@ -53,8 +54,8 @@ static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
                                     FunctionType::get(RetTy, ParamTys, false));
   }
 
-  SmallVector<Value*, 8> Operands(ArgBegin, ArgEnd);
-  CallInst *NewCI = new CallInst(FCache, &Operands[0], Operands.size(),
+  SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
+  CallInst *NewCI = new CallInst(FCache, Args.begin(), Args.end(),
                                  CI->getName(), CI);
   if (!CI->use_empty())
     CI->replaceAllUsesWith(NewCI);
@@ -98,14 +99,65 @@ void IntrinsicLowering::AddPrototypes(Module &M) {
                               PointerType::get(Type::Int8Ty), Type::Int32Ty, 
                               TD.getIntPtrType(), (Type *)0);
         break;
-      case Intrinsic::sqrt_f32:
-      case Intrinsic::sqrt_f64:
-        if(I->arg_begin()->getType() == Type::FloatTy)
+      case Intrinsic::sqrt:
+        switch((int)I->arg_begin()->getType()->getTypeID()) {
+        case Type::FloatTyID:
           EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
                                Type::FloatTy);
-        else
+        case Type::DoubleTyID:
           EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
                                Type::DoubleTy);
+        case Type::X86_FP80TyID:
+        case Type::FP128TyID:
+        case Type::PPC_FP128TyID:
+          EnsureFunctionExists(M, "sqrtl", I->arg_begin(), I->arg_end(),
+                               I->arg_begin()->getType());
+        }
+        break;
+      case Intrinsic::sin:
+        switch((int)I->arg_begin()->getType()->getTypeID()) {
+        case Type::FloatTyID:
+          EnsureFunctionExists(M, "sinf", I->arg_begin(), I->arg_end(),
+                               Type::FloatTy);
+        case Type::DoubleTyID:
+          EnsureFunctionExists(M, "sin", I->arg_begin(), I->arg_end(),
+                               Type::DoubleTy);
+        case Type::X86_FP80TyID:
+        case Type::FP128TyID:
+        case Type::PPC_FP128TyID:
+          EnsureFunctionExists(M, "sinl", I->arg_begin(), I->arg_end(),
+                               I->arg_begin()->getType());
+        }
+        break;
+      case Intrinsic::cos:
+        switch((int)I->arg_begin()->getType()->getTypeID()) {
+        case Type::FloatTyID:
+          EnsureFunctionExists(M, "cosf", I->arg_begin(), I->arg_end(),
+                               Type::FloatTy);
+        case Type::DoubleTyID:
+          EnsureFunctionExists(M, "cos", I->arg_begin(), I->arg_end(),
+                               Type::DoubleTy);
+        case Type::X86_FP80TyID:
+        case Type::FP128TyID:
+        case Type::PPC_FP128TyID:
+          EnsureFunctionExists(M, "cosl", I->arg_begin(), I->arg_end(),
+                               I->arg_begin()->getType());
+        }
+        break;
+      case Intrinsic::pow:
+        switch((int)I->arg_begin()->getType()->getTypeID()) {
+        case Type::FloatTyID:
+          EnsureFunctionExists(M, "powf", I->arg_begin(), I->arg_end(),
+                               Type::FloatTy);
+        case Type::DoubleTyID:
+          EnsureFunctionExists(M, "pow", I->arg_begin(), I->arg_end(),
+                               Type::DoubleTy);
+        case Type::X86_FP80TyID:
+        case Type::FP128TyID:
+        case Type::PPC_FP128TyID:
+          EnsureFunctionExists(M, "powl", I->arg_begin(), I->arg_end(),
+                               I->arg_begin()->getType());
+        }
         break;
       }
 }
@@ -144,7 +196,7 @@ static Value *LowerBSWAP(Value *V, Instruction *IP) {
                                      "bswap.and2", IP);
     Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
     Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
-    V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
+    V = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.i32", IP);
     break;
   }
   case 64: {
@@ -231,7 +283,7 @@ static Value *LowerCTPOP(Value *V, Instruction *IP) {
     }
   }
 
-  return CastInst::createIntegerCast(Count, Type::Int32Ty, false, "ctpop", IP);
+  return Count;
 }
 
 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
@@ -421,7 +473,7 @@ static Instruction *LowerPartSelect(CallInst *CI) {
     CI->getOperand(2),
     CI->getOperand(3)
   };
-  return new CallInst(F, Args, sizeof(Args)/sizeof(Args[0]), CI->getName(), CI);
+  return new CallInst(F, Args, array_endof(Args), CI->getName(), CI);
 }
 
 /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes 
@@ -587,7 +639,7 @@ static Instruction *LowerPartSet(CallInst *CI) {
     CI->getOperand(3),
     CI->getOperand(4)
   };
-  return new CallInst(F, Args, sizeof(Args)/sizeof(Args[0]), CI->getName(), CI);
+  return new CallInst(F, Args, array_endof(Args), CI->getName(), CI);
 }
 
 
@@ -706,11 +758,23 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
   case Intrinsic::dbg_region_end:
   case Intrinsic::dbg_func_start:
   case Intrinsic::dbg_declare:
+    break;    // Simply strip out debugging intrinsics
+
   case Intrinsic::eh_exception:
-  case Intrinsic::eh_selector:
-  case Intrinsic::eh_filter:
-    break;    // Simply strip out debugging and eh intrinsics
+  case Intrinsic::eh_selector_i32:
+  case Intrinsic::eh_selector_i64:
+    CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
+    break;
+
+  case Intrinsic::eh_typeid_for_i32:
+  case Intrinsic::eh_typeid_for_i64:
+    // Return something different to eh_selector.
+    CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
+    break;
 
+  case Intrinsic::var_annotation:
+    break;   // Strip out annotate intrinsic
+    
   case Intrinsic::memcpy_i32:
   case Intrinsic::memcpy_i64: {
     static Constant *MemcpyFCache = 0;
@@ -769,16 +833,27 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
                     MemsetFCache);
     break;
   }
-  case Intrinsic::sqrt_f32: {
+  case Intrinsic::sqrt: {
     static Constant *sqrtfFCache = 0;
-    ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, sqrtfFCache);
-    break;
-  }
-  case Intrinsic::sqrt_f64: {
     static Constant *sqrtFCache = 0;
-    ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
+    static Constant *sqrtLDCache = 0;
+    switch (CI->getOperand(1)->getType()->getTypeID()) {
+    default: assert(0 && "Invalid type in sqrt"); abort();
+    case Type::FloatTyID:
+      ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
+                    Type::FloatTy, sqrtfFCache);
+      break;
+    case Type::DoubleTyID:
+      ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
                     Type::DoubleTy, sqrtFCache);
+      break;
+    case Type::X86_FP80TyID:
+    case Type::FP128TyID:
+    case Type::PPC_FP128TyID:
+      ReplaceCallWith("sqrtl", CI, CI->op_begin()+1, CI->op_end(),
+                    CI->getOperand(1)->getType(), sqrtLDCache);
+      break;
+    }
     break;
   }
   }