LCOV - code coverage report
Current view: top level - lib/CodeGen - IntrinsicLowering.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 122 355 34.4 %
Date: 2018-06-17 00:07:59 Functions: 5 11 45.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the IntrinsicLowering class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/CodeGen/IntrinsicLowering.h"
      15             : #include "llvm/ADT/SmallVector.h"
      16             : #include "llvm/IR/CallSite.h"
      17             : #include "llvm/IR/Constants.h"
      18             : #include "llvm/IR/DataLayout.h"
      19             : #include "llvm/IR/DerivedTypes.h"
      20             : #include "llvm/IR/IRBuilder.h"
      21             : #include "llvm/IR/Module.h"
      22             : #include "llvm/IR/Type.h"
      23             : #include "llvm/Support/ErrorHandling.h"
      24             : #include "llvm/Support/raw_ostream.h"
      25             : using namespace llvm;
      26             : 
      27             : template <class ArgIt>
      28           0 : static void EnsureFunctionExists(Module &M, const char *Name,
      29             :                                  ArgIt ArgBegin, ArgIt ArgEnd,
      30             :                                  Type *RetTy) {
      31             :   // Insert a correctly-typed definition now.
      32             :   std::vector<Type *> ParamTys;
      33           0 :   for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
      34           0 :     ParamTys.push_back(I->getType());
      35           0 :   M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
      36           0 : }
      37             : 
      38           0 : static void EnsureFPIntrinsicsExist(Module &M, Function &Fn,
      39             :                                     const char *FName,
      40             :                                     const char *DName, const char *LDName) {
      41             :   // Insert definitions for all the floating point types.
      42           0 :   switch((int)Fn.arg_begin()->getType()->getTypeID()) {
      43           0 :   case Type::FloatTyID:
      44           0 :     EnsureFunctionExists(M, FName, Fn.arg_begin(), Fn.arg_end(),
      45             :                          Type::getFloatTy(M.getContext()));
      46           0 :     break;
      47           0 :   case Type::DoubleTyID:
      48           0 :     EnsureFunctionExists(M, DName, Fn.arg_begin(), Fn.arg_end(),
      49             :                          Type::getDoubleTy(M.getContext()));
      50           0 :     break;
      51             :   case Type::X86_FP80TyID:
      52             :   case Type::FP128TyID:
      53             :   case Type::PPC_FP128TyID:
      54           0 :     EnsureFunctionExists(M, LDName, Fn.arg_begin(), Fn.arg_end(),
      55             :                          Fn.arg_begin()->getType());
      56           0 :     break;
      57             :   }
      58           0 : }
      59             : 
      60             : /// This function is used when we want to lower an intrinsic call to a call of
      61             : /// an external function. This handles hard cases such as when there was already
      62             : /// a prototype for the external function, but that prototype doesn't match the
      63             : /// arguments we expect to pass in.
      64             : template <class ArgIt>
      65          14 : static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
      66             :                                  ArgIt ArgBegin, ArgIt ArgEnd,
      67             :                                  Type *RetTy) {
      68             :   // If we haven't already looked up this function, check to see if the
      69             :   // program already contains a function with this name.
      70          14 :   Module *M = CI->getModule();
      71             :   // Get or insert the definition now.
      72             :   std::vector<Type *> ParamTys;
      73          46 :   for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
      74          32 :     ParamTys.push_back((*I)->getType());
      75          28 :   Constant* FCache = M->getOrInsertFunction(NewFn,
      76             :                                   FunctionType::get(RetTy, ParamTys, false));
      77             : 
      78          14 :   IRBuilder<> Builder(CI->getParent(), CI->getIterator());
      79             :   SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
      80          14 :   CallInst *NewCI = Builder.CreateCall(FCache, Args);
      81          28 :   NewCI->setName(CI->getName());
      82          14 :   if (!CI->use_empty())
      83           0 :     CI->replaceAllUsesWith(NewCI);
      84          14 :   return NewCI;
      85             : }
      86             : 
      87             : // VisualStudio defines setjmp as _setjmp
      88             : #if defined(_MSC_VER) && defined(setjmp) && \
      89             :                          !defined(setjmp_undefined_for_msvc)
      90             : #  pragma push_macro("setjmp")
      91             : #  undef setjmp
      92             : #  define setjmp_undefined_for_msvc
      93             : #endif
      94             : 
      95           0 : void IntrinsicLowering::AddPrototypes(Module &M) {
      96           0 :   LLVMContext &Context = M.getContext();
      97           0 :   for (auto &F : M)
      98           0 :     if (F.isDeclaration() && !F.use_empty())
      99           0 :       switch (F.getIntrinsicID()) {
     100             :       default: break;
     101           0 :       case Intrinsic::setjmp:
     102           0 :         EnsureFunctionExists(M, "setjmp", F.arg_begin(), F.arg_end(),
     103           0 :                              Type::getInt32Ty(M.getContext()));
     104           0 :         break;
     105           0 :       case Intrinsic::longjmp:
     106           0 :         EnsureFunctionExists(M, "longjmp", F.arg_begin(), F.arg_end(),
     107             :                              Type::getVoidTy(M.getContext()));
     108           0 :         break;
     109           0 :       case Intrinsic::siglongjmp:
     110           0 :         EnsureFunctionExists(M, "abort", F.arg_end(), F.arg_end(),
     111             :                              Type::getVoidTy(M.getContext()));
     112           0 :         break;
     113           0 :       case Intrinsic::memcpy:
     114           0 :         M.getOrInsertFunction("memcpy",
     115           0 :           Type::getInt8PtrTy(Context),
     116             :                               Type::getInt8PtrTy(Context), 
     117             :                               Type::getInt8PtrTy(Context), 
     118           0 :                               DL.getIntPtrType(Context));
     119           0 :         break;
     120           0 :       case Intrinsic::memmove:
     121           0 :         M.getOrInsertFunction("memmove",
     122           0 :           Type::getInt8PtrTy(Context),
     123             :                               Type::getInt8PtrTy(Context), 
     124             :                               Type::getInt8PtrTy(Context), 
     125           0 :                               DL.getIntPtrType(Context));
     126           0 :         break;
     127           0 :       case Intrinsic::memset:
     128           0 :         M.getOrInsertFunction("memset",
     129           0 :           Type::getInt8PtrTy(Context),
     130             :                               Type::getInt8PtrTy(Context), 
     131             :                               Type::getInt32Ty(M.getContext()), 
     132           0 :                               DL.getIntPtrType(Context));
     133           0 :         break;
     134           0 :       case Intrinsic::sqrt:
     135           0 :         EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl");
     136           0 :         break;
     137           0 :       case Intrinsic::sin:
     138           0 :         EnsureFPIntrinsicsExist(M, F, "sinf", "sin", "sinl");
     139           0 :         break;
     140           0 :       case Intrinsic::cos:
     141           0 :         EnsureFPIntrinsicsExist(M, F, "cosf", "cos", "cosl");
     142           0 :         break;
     143           0 :       case Intrinsic::pow:
     144           0 :         EnsureFPIntrinsicsExist(M, F, "powf", "pow", "powl");
     145           0 :         break;
     146           0 :       case Intrinsic::log:
     147           0 :         EnsureFPIntrinsicsExist(M, F, "logf", "log", "logl");
     148           0 :         break;
     149           0 :       case Intrinsic::log2:
     150           0 :         EnsureFPIntrinsicsExist(M, F, "log2f", "log2", "log2l");
     151           0 :         break;
     152           0 :       case Intrinsic::log10:
     153           0 :         EnsureFPIntrinsicsExist(M, F, "log10f", "log10", "log10l");
     154           0 :         break;
     155           0 :       case Intrinsic::exp:
     156           0 :         EnsureFPIntrinsicsExist(M, F, "expf", "exp", "expl");
     157           0 :         break;
     158           0 :       case Intrinsic::exp2:
     159           0 :         EnsureFPIntrinsicsExist(M, F, "exp2f", "exp2", "exp2l");
     160           0 :         break;
     161             :       }
     162           0 : }
     163             : 
     164             : /// Emit the code to lower bswap of V before the specified instruction IP.
     165           6 : static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
     166             :   assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");
     167             : 
     168           6 :   unsigned BitSize = V->getType()->getScalarSizeInBits();
     169             : 
     170           6 :   IRBuilder<> Builder(IP);
     171             : 
     172           6 :   switch(BitSize) {
     173           0 :   default: llvm_unreachable("Unhandled type size of value to byteswap!");
     174             :   case 16: {
     175           4 :     Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
     176           2 :                                     "bswap.2");
     177           4 :     Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
     178           2 :                                      "bswap.1");
     179           2 :     V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
     180             :     break;
     181             :   }
     182             :   case 32: {
     183           4 :     Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
     184           2 :                                     "bswap.4");
     185           4 :     Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
     186           2 :                                     "bswap.3");
     187           4 :     Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
     188           2 :                                      "bswap.2");
     189           4 :     Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
     190           2 :                                      "bswap.1");
     191           2 :     Tmp3 = Builder.CreateAnd(Tmp3,
     192           2 :                          ConstantInt::get(V->getType(), 0xFF0000),
     193             :                              "bswap.and3");
     194           2 :     Tmp2 = Builder.CreateAnd(Tmp2,
     195           2 :                            ConstantInt::get(V->getType(), 0xFF00),
     196             :                              "bswap.and2");
     197           2 :     Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
     198           2 :     Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
     199           2 :     V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
     200             :     break;
     201             :   }
     202             :   case 64: {
     203           4 :     Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
     204           2 :                                     "bswap.8");
     205           4 :     Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
     206           2 :                                     "bswap.7");
     207           4 :     Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
     208           2 :                                     "bswap.6");
     209           4 :     Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
     210           2 :                                     "bswap.5");
     211           4 :     Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
     212           2 :                                      "bswap.4");
     213           2 :     Value* Tmp3 = Builder.CreateLShr(V, 
     214           2 :                                      ConstantInt::get(V->getType(), 24),
     215           2 :                                      "bswap.3");
     216           2 :     Value* Tmp2 = Builder.CreateLShr(V, 
     217           2 :                                      ConstantInt::get(V->getType(), 40),
     218           2 :                                      "bswap.2");
     219           2 :     Value* Tmp1 = Builder.CreateLShr(V, 
     220           2 :                                      ConstantInt::get(V->getType(), 56),
     221           2 :                                      "bswap.1");
     222           2 :     Tmp7 = Builder.CreateAnd(Tmp7,
     223           2 :                              ConstantInt::get(V->getType(),
     224             :                                               0xFF000000000000ULL),
     225             :                              "bswap.and7");
     226           2 :     Tmp6 = Builder.CreateAnd(Tmp6,
     227           2 :                              ConstantInt::get(V->getType(),
     228             :                                               0xFF0000000000ULL),
     229             :                              "bswap.and6");
     230           2 :     Tmp5 = Builder.CreateAnd(Tmp5,
     231           2 :                         ConstantInt::get(V->getType(),
     232             :                              0xFF00000000ULL),
     233             :                              "bswap.and5");
     234           2 :     Tmp4 = Builder.CreateAnd(Tmp4,
     235           2 :                         ConstantInt::get(V->getType(),
     236             :                              0xFF000000ULL),
     237             :                              "bswap.and4");
     238           2 :     Tmp3 = Builder.CreateAnd(Tmp3,
     239           2 :                              ConstantInt::get(V->getType(),
     240             :                              0xFF0000ULL),
     241             :                              "bswap.and3");
     242           2 :     Tmp2 = Builder.CreateAnd(Tmp2,
     243           2 :                              ConstantInt::get(V->getType(),
     244             :                              0xFF00ULL),
     245             :                              "bswap.and2");
     246           2 :     Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
     247           2 :     Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
     248           2 :     Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
     249           2 :     Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
     250           2 :     Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
     251           2 :     Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
     252           2 :     V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
     253             :     break;
     254             :   }
     255             :   }
     256           6 :   return V;
     257             : }
     258             : 
     259             : /// Emit the code to lower ctpop of V before the specified instruction IP.
     260           0 : static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
     261             :   assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
     262             : 
     263             :   static const uint64_t MaskValues[6] = {
     264             :     0x5555555555555555ULL, 0x3333333333333333ULL,
     265             :     0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
     266             :     0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
     267             :   };
     268             : 
     269           0 :   IRBuilder<> Builder(IP);
     270             : 
     271           0 :   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
     272           0 :   unsigned WordSize = (BitSize + 63) / 64;
     273           0 :   Value *Count = ConstantInt::get(V->getType(), 0);
     274             : 
     275           0 :   for (unsigned n = 0; n < WordSize; ++n) {
     276             :     Value *PartValue = V;
     277           0 :     for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize); 
     278           0 :          i <<= 1, ++ct) {
     279           0 :       Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
     280           0 :       Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
     281           0 :       Value *VShift = Builder.CreateLShr(PartValue,
     282           0 :                                         ConstantInt::get(V->getType(), i),
     283           0 :                                          "ctpop.sh");
     284           0 :       Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
     285           0 :       PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
     286             :     }
     287           0 :     Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
     288           0 :     if (BitSize > 64) {
     289           0 :       V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
     290             :                              "ctpop.part.sh");
     291           0 :       BitSize -= 64;
     292             :     }
     293             :   }
     294             : 
     295           0 :   return Count;
     296             : }
     297             : 
     298             : /// Emit the code to lower ctlz of V before the specified instruction IP.
     299           0 : static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
     300             : 
     301           0 :   IRBuilder<> Builder(IP);
     302             : 
     303           0 :   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
     304           0 :   for (unsigned i = 1; i < BitSize; i <<= 1) {
     305           0 :     Value *ShVal = ConstantInt::get(V->getType(), i);
     306           0 :     ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
     307           0 :     V = Builder.CreateOr(V, ShVal, "ctlz.step");
     308             :   }
     309             : 
     310           0 :   V = Builder.CreateNot(V);
     311           0 :   return LowerCTPOP(Context, V, IP);
     312             : }
     313             : 
     314          14 : static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
     315             :                                        const char *Dname,
     316             :                                        const char *LDname) {
     317             :   CallSite CS(CI);
     318          42 :   switch (CI->getArgOperand(0)->getType()->getTypeID()) {
     319           0 :   default: llvm_unreachable("Invalid type in intrinsic");
     320           7 :   case Type::FloatTyID:
     321          14 :     ReplaceCallWith(Fname, CI, CS.arg_begin(), CS.arg_end(),
     322             :                   Type::getFloatTy(CI->getContext()));
     323           7 :     break;
     324           7 :   case Type::DoubleTyID:
     325          14 :     ReplaceCallWith(Dname, CI, CS.arg_begin(), CS.arg_end(),
     326             :                   Type::getDoubleTy(CI->getContext()));
     327           7 :     break;
     328             :   case Type::X86_FP80TyID:
     329             :   case Type::FP128TyID:
     330             :   case Type::PPC_FP128TyID:
     331           0 :     ReplaceCallWith(LDname, CI, CS.arg_begin(), CS.arg_end(),
     332             :                   CI->getArgOperand(0)->getType());
     333           0 :     break;
     334             :   }
     335          14 : }
     336             : 
     337          20 : void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
     338          20 :   IRBuilder<> Builder(CI);
     339          20 :   LLVMContext &Context = CI->getContext();
     340             : 
     341             :   const Function *Callee = CI->getCalledFunction();
     342             :   assert(Callee && "Cannot lower an indirect call!");
     343             : 
     344             :   CallSite CS(CI);
     345          20 :   switch (Callee->getIntrinsicID()) {
     346             :   case Intrinsic::not_intrinsic:
     347           0 :     report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
     348           0 :                       Callee->getName() + "'!");
     349             :   default:
     350           0 :     report_fatal_error("Code generator does not support intrinsic function '"+
     351           0 :                       Callee->getName()+"'!");
     352             : 
     353           0 :   case Intrinsic::expect: {
     354             :     // Just replace __builtin_expect(exp, c) with EXP.
     355           0 :     Value *V = CI->getArgOperand(0);
     356           0 :     CI->replaceAllUsesWith(V);
     357           0 :     break;
     358             :   }
     359             : 
     360             :     // The setjmp/longjmp intrinsics should only exist in the code if it was
     361             :     // never optimized (ie, right out of the CFE), or if it has been hacked on
     362             :     // by the lowerinvoke pass.  In both cases, the right thing to do is to
     363             :     // convert the call to an explicit setjmp or longjmp call.
     364           0 :   case Intrinsic::setjmp: {
     365           0 :     Value *V = ReplaceCallWith("setjmp", CI, CS.arg_begin(), CS.arg_end(),
     366           0 :                                Type::getInt32Ty(Context));
     367           0 :     if (!CI->getType()->isVoidTy())
     368           0 :       CI->replaceAllUsesWith(V);
     369             :     break;
     370             :   }
     371           0 :   case Intrinsic::sigsetjmp:
     372           0 :      if (!CI->getType()->isVoidTy())
     373           0 :        CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
     374             :      break;
     375             : 
     376           0 :   case Intrinsic::longjmp: {
     377           0 :     ReplaceCallWith("longjmp", CI, CS.arg_begin(), CS.arg_end(),
     378             :                     Type::getVoidTy(Context));
     379           0 :     break;
     380             :   }
     381             : 
     382           0 :   case Intrinsic::siglongjmp: {
     383             :     // Insert the call to abort
     384           0 :     ReplaceCallWith("abort", CI, CS.arg_end(), CS.arg_end(), 
     385             :                     Type::getVoidTy(Context));
     386           0 :     break;
     387             :   }
     388           0 :   case Intrinsic::ctpop:
     389           0 :     CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
     390           0 :     break;
     391             : 
     392           6 :   case Intrinsic::bswap:
     393          12 :     CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
     394           6 :     break;
     395             :     
     396           0 :   case Intrinsic::ctlz:
     397           0 :     CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
     398           0 :     break;
     399             : 
     400           0 :   case Intrinsic::cttz: {
     401             :     // cttz(x) -> ctpop(~X & (X-1))
     402           0 :     Value *Src = CI->getArgOperand(0);
     403           0 :     Value *NotSrc = Builder.CreateNot(Src);
     404           0 :     NotSrc->setName(Src->getName() + ".not");
     405           0 :     Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
     406           0 :     SrcM1 = Builder.CreateSub(Src, SrcM1);
     407           0 :     Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
     408           0 :     CI->replaceAllUsesWith(Src);
     409           0 :     break;
     410             :   }
     411             : 
     412           0 :   case Intrinsic::stacksave:
     413             :   case Intrinsic::stackrestore: {
     414           0 :     if (!Warned)
     415           0 :       errs() << "WARNING: this target does not support the llvm.stack"
     416           0 :              << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
     417           0 :                "save" : "restore") << " intrinsic.\n";
     418           0 :     Warned = true;
     419           0 :     if (Callee->getIntrinsicID() == Intrinsic::stacksave)
     420           0 :       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
     421             :     break;
     422             :   }
     423             :     
     424           0 :   case Intrinsic::get_dynamic_area_offset:
     425           0 :     errs() << "WARNING: this target does not support the custom llvm.get."
     426             :               "dynamic.area.offset.  It is being lowered to a constant 0\n";
     427             :     // Just lower it to a constant 0 because for most targets
     428             :     // @llvm.get.dynamic.area.offset is lowered to zero.
     429           0 :     CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
     430           0 :     break;
     431           0 :   case Intrinsic::returnaddress:
     432             :   case Intrinsic::frameaddress:
     433           0 :     errs() << "WARNING: this target does not support the llvm."
     434           0 :            << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
     435           0 :              "return" : "frame") << "address intrinsic.\n";
     436           0 :     CI->replaceAllUsesWith(
     437           0 :         ConstantPointerNull::get(cast<PointerType>(CI->getType())));
     438           0 :     break;
     439           0 :   case Intrinsic::addressofreturnaddress:
     440           0 :     errs() << "WARNING: this target does not support the "
     441             :               "llvm.addressofreturnaddress intrinsic.\n";
     442           0 :     CI->replaceAllUsesWith(
     443           0 :         ConstantPointerNull::get(cast<PointerType>(CI->getType())));
     444           0 :     break;
     445             : 
     446             :   case Intrinsic::prefetch:
     447             :     break;    // Simply strip out prefetches on unsupported architectures
     448             : 
     449             :   case Intrinsic::pcmarker:
     450             :     break;    // Simply strip out pcmarker on unsupported architectures
     451           0 :   case Intrinsic::readcyclecounter: {
     452           0 :     errs() << "WARNING: this target does not support the llvm.readcyclecoun"
     453           0 :            << "ter intrinsic.  It is being lowered to a constant 0\n";
     454           0 :     CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
     455           0 :     break;
     456             :   }
     457             : 
     458             :   case Intrinsic::dbg_declare:
     459             :   case Intrinsic::dbg_label:
     460             :     break;    // Simply strip out debugging intrinsics
     461             : 
     462           0 :   case Intrinsic::eh_typeid_for:
     463             :     // Return something different to eh_selector.
     464           0 :     CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
     465           0 :     break;
     466             : 
     467           0 :   case Intrinsic::annotation:
     468             :   case Intrinsic::ptr_annotation:
     469             :     // Just drop the annotation, but forward the value
     470           0 :     CI->replaceAllUsesWith(CI->getOperand(0));
     471           0 :     break;
     472             : 
     473             :   case Intrinsic::assume:
     474             :   case Intrinsic::var_annotation:
     475             :     break;   // Strip out these intrinsics
     476             :  
     477           0 :   case Intrinsic::memcpy: {
     478           0 :     Type *IntPtr = DL.getIntPtrType(Context);
     479           0 :     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
     480           0 :                                         /* isSigned */ false);
     481             :     Value *Ops[3];
     482           0 :     Ops[0] = CI->getArgOperand(0);
     483           0 :     Ops[1] = CI->getArgOperand(1);
     484           0 :     Ops[2] = Size;
     485           0 :     ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
     486             :     break;
     487             :   }
     488           0 :   case Intrinsic::memmove: {
     489           0 :     Type *IntPtr = DL.getIntPtrType(Context);
     490           0 :     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
     491           0 :                                         /* isSigned */ false);
     492             :     Value *Ops[3];
     493           0 :     Ops[0] = CI->getArgOperand(0);
     494           0 :     Ops[1] = CI->getArgOperand(1);
     495           0 :     Ops[2] = Size;
     496           0 :     ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
     497             :     break;
     498             :   }
     499           0 :   case Intrinsic::memset: {
     500           0 :     Value *Op0 = CI->getArgOperand(0);
     501           0 :     Type *IntPtr = DL.getIntPtrType(Op0->getType());
     502           0 :     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
     503           0 :                                         /* isSigned */ false);
     504             :     Value *Ops[3];
     505           0 :     Ops[0] = Op0;
     506             :     // Extend the amount to i32.
     507           0 :     Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
     508           0 :                                    Type::getInt32Ty(Context),
     509             :                                    /* isSigned */ false);
     510           0 :     Ops[2] = Size;
     511           0 :     ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
     512             :     break;
     513             :   }
     514           0 :   case Intrinsic::sqrt: {
     515           0 :     ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
     516           0 :     break;
     517             :   }
     518           0 :   case Intrinsic::log: {
     519           0 :     ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
     520           0 :     break;
     521             :   }
     522           0 :   case Intrinsic::log2: {
     523           0 :     ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
     524           0 :     break;
     525             :   }
     526           0 :   case Intrinsic::log10: {
     527           0 :     ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
     528           0 :     break;
     529             :   }
     530           0 :   case Intrinsic::exp: {
     531           0 :     ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
     532           0 :     break;
     533             :   }
     534           0 :   case Intrinsic::exp2: {
     535           0 :     ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
     536           0 :     break;
     537             :   }
     538           0 :   case Intrinsic::pow: {
     539           0 :     ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
     540           0 :     break;
     541             :   }
     542           2 :   case Intrinsic::sin: {
     543           2 :     ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl");
     544           2 :     break;
     545             :   }
     546           2 :   case Intrinsic::cos: {
     547           2 :     ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl");
     548           2 :     break;
     549             :   }
     550           2 :   case Intrinsic::floor: {
     551           2 :     ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl");
     552           2 :     break;
     553             :   }
     554           2 :   case Intrinsic::ceil: {
     555           2 :     ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill");
     556           2 :     break;
     557             :   }
     558           2 :   case Intrinsic::trunc: {
     559           2 :     ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl");
     560           2 :     break;
     561             :   }
     562           2 :   case Intrinsic::round: {
     563           2 :     ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl");
     564           2 :     break;
     565             :   }
     566           2 :   case Intrinsic::copysign: {
     567           2 :     ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl");
     568           2 :     break;
     569             :   }
     570           0 :   case Intrinsic::flt_rounds:
     571             :      // Lower to "round to the nearest"
     572           0 :      if (!CI->getType()->isVoidTy())
     573           0 :        CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
     574             :      break;
     575           0 :   case Intrinsic::invariant_start:
     576             :   case Intrinsic::lifetime_start:
     577             :     // Discard region information.
     578           0 :     CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
     579           0 :     break;
     580             :   case Intrinsic::invariant_end:
     581             :   case Intrinsic::lifetime_end:
     582             :     // Discard region information.
     583             :     break;
     584             :   }
     585             : 
     586             :   assert(CI->use_empty() &&
     587             :          "Lowering should have eliminated any uses of the intrinsic call!");
     588          20 :   CI->eraseFromParent();
     589          20 : }
     590             : 
     591          49 : bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
     592             :   // Verify this is a simple bswap.
     593          98 :   if (CI->getNumArgOperands() != 1 ||
     594         147 :       CI->getType() != CI->getArgOperand(0)->getType() ||
     595             :       !CI->getType()->isIntegerTy())
     596             :     return false;
     597             : 
     598             :   IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
     599             :   if (!Ty)
     600             :     return false;
     601             : 
     602             :   // Okay, we can do this xform, do so now.
     603          49 :   Module *M = CI->getModule();
     604          98 :   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty);
     605             : 
     606          49 :   Value *Op = CI->getArgOperand(0);
     607          98 :   Op = CallInst::Create(Int, Op, CI->getName(), CI);
     608             : 
     609          49 :   CI->replaceAllUsesWith(Op);
     610          49 :   CI->eraseFromParent();
     611          49 :   return true;
     612             : }

Generated by: LCOV version 1.13