From f47d6b38c7a61d50db4566b02719de05492dcef1 Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Thu, 31 Jan 2019 20:35:56 +0000 Subject: [opaque pointer types] Add a FunctionCallee wrapper type, and use it. The FunctionCallee type is effectively a {FunctionType*,Value*} pair, and is a useful convenience to enable code to continue passing the result of getOrInsertFunction() through to EmitCall, even once pointer types lose their pointee-type. Then: - update the CallInst/InvokeInst instruction creation functions to take a Callee, - modify getOrInsertFunction to return FunctionCallee, and - update all callers appropriately. One area of particular note is the change to the sanitizer code. Previously, they had been casting the result of `getOrInsertFunction` to a `Function*` via `checkSanitizerInterfaceFunction`, and storing that. That would report an error if someone had already inserted a function declaraction with a mismatching signature. However, in general, LLVM allows for such mismatches, as `getOrInsertFunction` will automatically insert a bitcast if needed. As part of this cleanup, cause the sanitizer code to do the same. (It will call its functions using the expected signature, however they may have been declared.) Finally, in a small number of locations, callers of `getOrInsertFunction` actually were expecting/requiring that a brand new function was being created. In such cases, I've switched them to Function::Create instead. Differential Revision: https://reviews.llvm.org/D57315 llvm-svn: 352791 --- llvm/lib/CodeGen/IntrinsicLowering.cpp | 106 +-------------------------------- 1 file changed, 2 insertions(+), 104 deletions(-) (limited to 'llvm/lib/CodeGen/IntrinsicLowering.cpp') diff --git a/llvm/lib/CodeGen/IntrinsicLowering.cpp b/llvm/lib/CodeGen/IntrinsicLowering.cpp index aebc8fa510a..4a66083be35 100644 --- a/llvm/lib/CodeGen/IntrinsicLowering.cpp +++ b/llvm/lib/CodeGen/IntrinsicLowering.cpp @@ -23,39 +23,6 @@ #include "llvm/Support/raw_ostream.h" using namespace llvm; -template -static void EnsureFunctionExists(Module &M, const char *Name, - ArgIt ArgBegin, ArgIt ArgEnd, - Type *RetTy) { - // Insert a correctly-typed definition now. - std::vector ParamTys; - for (ArgIt I = ArgBegin; I != ArgEnd; ++I) - ParamTys.push_back(I->getType()); - M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); -} - -static void EnsureFPIntrinsicsExist(Module &M, Function &Fn, - const char *FName, - const char *DName, const char *LDName) { - // Insert definitions for all the floating point types. - switch((int)Fn.arg_begin()->getType()->getTypeID()) { - case Type::FloatTyID: - EnsureFunctionExists(M, FName, Fn.arg_begin(), Fn.arg_end(), - Type::getFloatTy(M.getContext())); - break; - case Type::DoubleTyID: - EnsureFunctionExists(M, DName, Fn.arg_begin(), Fn.arg_end(), - Type::getDoubleTy(M.getContext())); - break; - case Type::X86_FP80TyID: - case Type::FP128TyID: - case Type::PPC_FP128TyID: - EnsureFunctionExists(M, LDName, Fn.arg_begin(), Fn.arg_end(), - Fn.arg_begin()->getType()); - break; - } -} - /// This function is used when we want to lower an intrinsic call to a call of /// an external function. This handles hard cases such as when there was already /// a prototype for the external function, but that prototype doesn't match the @@ -71,8 +38,8 @@ static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, std::vector ParamTys; for (ArgIt I = ArgBegin; I != ArgEnd; ++I) ParamTys.push_back((*I)->getType()); - Constant* FCache = M->getOrInsertFunction(NewFn, - FunctionType::get(RetTy, ParamTys, false)); + FunctionCallee FCache = + M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false)); IRBuilder<> Builder(CI->getParent(), CI->getIterator()); SmallVector Args(ArgBegin, ArgEnd); @@ -91,75 +58,6 @@ static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, # define setjmp_undefined_for_msvc #endif -void IntrinsicLowering::AddPrototypes(Module &M) { - LLVMContext &Context = M.getContext(); - for (auto &F : M) - if (F.isDeclaration() && !F.use_empty()) - switch (F.getIntrinsicID()) { - default: break; - case Intrinsic::setjmp: - EnsureFunctionExists(M, "setjmp", F.arg_begin(), F.arg_end(), - Type::getInt32Ty(M.getContext())); - break; - case Intrinsic::longjmp: - EnsureFunctionExists(M, "longjmp", F.arg_begin(), F.arg_end(), - Type::getVoidTy(M.getContext())); - break; - case Intrinsic::siglongjmp: - EnsureFunctionExists(M, "abort", F.arg_end(), F.arg_end(), - Type::getVoidTy(M.getContext())); - break; - case Intrinsic::memcpy: - M.getOrInsertFunction("memcpy", - Type::getInt8PtrTy(Context), - Type::getInt8PtrTy(Context), - Type::getInt8PtrTy(Context), - DL.getIntPtrType(Context)); - break; - case Intrinsic::memmove: - M.getOrInsertFunction("memmove", - Type::getInt8PtrTy(Context), - Type::getInt8PtrTy(Context), - Type::getInt8PtrTy(Context), - DL.getIntPtrType(Context)); - break; - case Intrinsic::memset: - M.getOrInsertFunction("memset", - Type::getInt8PtrTy(Context), - Type::getInt8PtrTy(Context), - Type::getInt32Ty(M.getContext()), - DL.getIntPtrType(Context)); - break; - case Intrinsic::sqrt: - EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl"); - break; - case Intrinsic::sin: - EnsureFPIntrinsicsExist(M, F, "sinf", "sin", "sinl"); - break; - case Intrinsic::cos: - EnsureFPIntrinsicsExist(M, F, "cosf", "cos", "cosl"); - break; - case Intrinsic::pow: - EnsureFPIntrinsicsExist(M, F, "powf", "pow", "powl"); - break; - case Intrinsic::log: - EnsureFPIntrinsicsExist(M, F, "logf", "log", "logl"); - break; - case Intrinsic::log2: - EnsureFPIntrinsicsExist(M, F, "log2f", "log2", "log2l"); - break; - case Intrinsic::log10: - EnsureFPIntrinsicsExist(M, F, "log10f", "log10", "log10l"); - break; - case Intrinsic::exp: - EnsureFPIntrinsicsExist(M, F, "expf", "exp", "expl"); - break; - case Intrinsic::exp2: - EnsureFPIntrinsicsExist(M, F, "exp2f", "exp2", "exp2l"); - break; - } -} - /// Emit the code to lower bswap of V before the specified instruction IP. static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) { assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!"); -- cgit v1.2.3