When a function takes a variable number of pointer arguments, with a zero
[oota-llvm.git] / lib / CodeGen / IntrinsicLowering.cpp
index a4ec31d4196dccce493cc2d995434b9a642dc611..a570ef54c4582a1e26f088d52cc1f6d64746efcc 100644 (file)
@@ -1,21 +1,24 @@
 //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // This file implements the default intrinsic lowering implementation.
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/IntrinsicLowering.h"
+#include "llvm/CodeGen/IntrinsicLowering.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
-#include "llvm/iOther.h"
+#include "llvm/Instructions.h"
+#include "llvm/Type.h"
+#include <iostream>
+
 using namespace llvm;
 
 template <class ArgIt>
@@ -85,37 +88,107 @@ void DefaultIntrinsicLowering::AddPrototypes(Module &M) {
       switch (I->getIntrinsicID()) {
       default: break;
       case Intrinsic::setjmp:
-        EnsureFunctionExists(M, "setjmp", I->abegin(), I->aend(), Type::IntTy);
+        EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
+                             Type::IntTy);
         break;
       case Intrinsic::longjmp:
-        EnsureFunctionExists(M, "longjmp", I->abegin(), I->aend(),Type::VoidTy);
+        EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
+                             Type::VoidTy);
         break;
       case Intrinsic::siglongjmp:
-        EnsureFunctionExists(M, "abort", I->aend(), I->aend(), Type::VoidTy);
+        EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
+                             Type::VoidTy);
         break;
       case Intrinsic::memcpy:
-        EnsureFunctionExists(M, "memcpy", I->abegin(), --I->aend(),
-                             I->abegin()->getType());
+        EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
+                             I->arg_begin()->getType());
         break;
       case Intrinsic::memmove:
-        EnsureFunctionExists(M, "memmove", I->abegin(), --I->aend(),
-                             I->abegin()->getType());
+        EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
+                             I->arg_begin()->getType());
         break;
       case Intrinsic::memset:
-        EnsureFunctionExists(M, "memset", I->abegin(), --I->aend(),
-                             I->abegin()->getType());
+        M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
+                              PointerType::get(Type::SByteTy),
+                              Type::IntTy, (--(--I->arg_end()))->getType(),
+                              (Type *)0);
+        break;
+      case Intrinsic::isunordered:
+        EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
+                             Type::BoolTy);
         break;
-      case Intrinsic::isnan:
-        EnsureFunctionExists(M, "isnan", I->abegin(), I->aend(), Type::BoolTy);
+      case Intrinsic::sqrt:
+        if(I->arg_begin()->getType() == Type::FloatTy)
+          EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
+                               Type::FloatTy);
+        else
+          EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
+                               Type::DoubleTy);
         break;
       }
+}
+
+/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
+/// instruction.
+static Value *LowerCTPOP(Value *V, Instruction *IP) {
+  assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
+
+  static const uint64_t MaskValues[6] = {
+    0x5555555555555555ULL, 0x3333333333333333ULL,
+    0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
+    0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
+  };
+
+  const Type *DestTy = V->getType();
+
+  // Force to unsigned so that the shift rights are logical.
+  if (DestTy->isSigned())
+    V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
+
+  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
+  for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
+    Value *MaskCst =
+      ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy,
+                                              MaskValues[ct]), V->getType());
+    Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
+    Value *VShift = new ShiftInst(Instruction::Shr, V,
+                      ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
+    Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
+    V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
+  }
+
+  if (V->getType() != DestTy)
+    V = new CastInst(V, DestTy, V->getName(), IP);
+  return V;
+}
+
+/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
+/// instruction.
+static Value *LowerCTLZ(Value *V, Instruction *IP) {
+  const Type *DestTy = V->getType();
+
+  // Force to unsigned so that the shift rights are logical.
+  if (DestTy->isSigned())
+    V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
 
+  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
+  for (unsigned i = 1; i != BitSize; i <<= 1) {
+    Value *ShVal = ConstantInt::get(Type::UByteTy, i);
+    ShVal = new ShiftInst(Instruction::Shr, V, ShVal, "ctlz.sh", IP);
+    V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
+  }
+
+  if (V->getType() != DestTy)
+    V = new CastInst(V, DestTy, V->getName(), IP);
+
+  V = BinaryOperator::createNot(V, "", IP);
+  return LowerCTPOP(V, IP);
 }
 
 void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
   Function *Callee = CI->getCalledFunction();
   assert(Callee && "Cannot lower an indirect call!");
-  
+
   switch (Callee->getIntrinsicID()) {
   case Intrinsic::not_intrinsic:
     std::cerr << "Cannot lower a call to a non-intrinsic function '"
@@ -138,7 +211,7 @@ void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
       CI->replaceAllUsesWith(V);
     break;
   }
-  case Intrinsic::sigsetjmp: 
+  case Intrinsic::sigsetjmp:
      if (CI->getType() != Type::VoidTy)
        CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
      break;
@@ -157,16 +230,39 @@ void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
                     AbortFCache);
     break;
   }
+  case Intrinsic::ctpop:
+    CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
+    break;
+
+  case Intrinsic::ctlz:
+    CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
+    break;
+  case Intrinsic::cttz: {
+    // cttz(x) -> ctpop(~X & (X-1))
+    Value *Src = CI->getOperand(1);
+    Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
+    Value *SrcM1  = ConstantInt::get(Src->getType(), 1);
+    SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
+    Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
+    CI->replaceAllUsesWith(Src);
+    break;
+  }
 
   case Intrinsic::returnaddress:
   case Intrinsic::frameaddress:
     std::cerr << "WARNING: this target does not support the llvm."
-              << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? 
+              << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
                   "return" : "frame") << "address intrinsic.\n";
     CI->replaceAllUsesWith(ConstantPointerNull::get(
                                             cast<PointerType>(CI->getType())));
     break;
 
+  case Intrinsic::prefetch:
+    break;    // Simply strip out prefetches on unsupported architectures
+
+  case Intrinsic::pcmarker:
+    break;    // Simply strip out pcmarker on unsupported architectures
+
   case Intrinsic::dbg_stoppoint:
   case Intrinsic::dbg_region_start:
   case Intrinsic::dbg_region_end:
@@ -200,23 +296,31 @@ void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
                     (*(CI->op_begin()+1))->getType(), MemsetFCache);
     break;
   }
-  case Intrinsic::isnan: {
-    // FIXME: This should force the argument to be a double.  There may be
-    // multiple isnans for different FP arguments.
-    static Function *isnanFCache = 0;
-    ReplaceCallWith("isnan", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::BoolTy, isnanFCache);
+  case Intrinsic::isunordered: {
+    Value *L = CI->getOperand(1);
+    Value *R = CI->getOperand(2);
+
+    Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
+    Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
+    CI->replaceAllUsesWith(
+      BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
+                             "isunordered", CI));
     break;
   }
-  case Intrinsic::isunordered: {
-    static Function *isunorderedFCache = 0;
-    ReplaceCallWith("isunordered", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::BoolTy, isunorderedFCache);
+  case Intrinsic::sqrt: {
+    static Function *sqrtFCache = 0;
+    static Function *sqrtfFCache = 0;
+    if(CI->getType() == Type::FloatTy)
+      ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
+                      Type::FloatTy, sqrtfFCache);
+    else
+      ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
+                      Type::DoubleTy, sqrtFCache);
     break;
   }
   }
-  
+
   assert(CI->use_empty() &&
          "Lowering should have eliminated any uses of the intrinsic call!");
-  CI->getParent()->getInstList().erase(CI);
+  CI->eraseFromParent();
 }