summaryrefslogtreecommitdiffstats
path: root/llvm/lib/CodeGen
diff options
context:
space:
mode:
authorJames Y Knight <jyknight@google.com>2019-01-31 20:35:56 +0000
committerJames Y Knight <jyknight@google.com>2019-01-31 20:35:56 +0000
commitf47d6b38c7a61d50db4566b02719de05492dcef1 (patch)
treef5c99869bcceba2f8b973cdbcadf7d3db52660b9 /llvm/lib/CodeGen
parente1b332efba12ce1d5a482084a5182e0527e9c88d (diff)
downloadbcm5719-llvm-f47d6b38c7a61d50db4566b02719de05492dcef1.tar.gz
bcm5719-llvm-f47d6b38c7a61d50db4566b02719de05492dcef1.zip
[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
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r--llvm/lib/CodeGen/AtomicExpandPass.cpp2
-rw-r--r--llvm/lib/CodeGen/DwarfEHPrepare.cpp2
-rw-r--r--llvm/lib/CodeGen/IntrinsicLowering.cpp106
-rw-r--r--llvm/lib/CodeGen/MIRParser/MIRParser.cpp5
-rw-r--r--llvm/lib/CodeGen/MachineOutliner.cpp6
-rw-r--r--llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp4
-rw-r--r--llvm/lib/CodeGen/SafeStack.cpp6
-rw-r--r--llvm/lib/CodeGen/SjLjEHPrepare.cpp18
-rw-r--r--llvm/lib/CodeGen/StackProtector.cpp9
-rw-r--r--llvm/lib/CodeGen/TargetLoweringBase.cpp4
-rw-r--r--llvm/lib/CodeGen/WasmEHPrepare.cpp10
11 files changed, 36 insertions, 136 deletions
diff --git a/llvm/lib/CodeGen/AtomicExpandPass.cpp b/llvm/lib/CodeGen/AtomicExpandPass.cpp
index 2d915945392..6e8d68ebfa1 100644
--- a/llvm/lib/CodeGen/AtomicExpandPass.cpp
+++ b/llvm/lib/CodeGen/AtomicExpandPass.cpp
@@ -1754,7 +1754,7 @@ bool AtomicExpand::expandAtomicOpToLibcall(
for (Value *Arg : Args)
ArgTys.push_back(Arg->getType());
FunctionType *FnType = FunctionType::get(ResultTy, ArgTys, false);
- Constant *LibcallFn =
+ FunctionCallee LibcallFn =
M->getOrInsertFunction(TLI->getLibcallName(RTLibType), FnType, Attr);
CallInst *Call = Builder.CreateCall(LibcallFn, Args);
Call->setAttributes(Attr);
diff --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index 896b2cb38cf..6b36a93c62c 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -45,7 +45,7 @@ namespace {
class DwarfEHPrepare : public FunctionPass {
// RewindFunction - _Unwind_Resume or the target equivalent.
- Constant *RewindFunction = nullptr;
+ FunctionCallee RewindFunction = nullptr;
DominatorTree *DT = nullptr;
const TargetLowering *TLI = nullptr;
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 <class ArgIt>
-static void EnsureFunctionExists(Module &M, const char *Name,
- ArgIt ArgBegin, ArgIt ArgEnd,
- Type *RetTy) {
- // Insert a correctly-typed definition now.
- std::vector<Type *> 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<Type *> 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<Value *, 8> 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!");
diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
index b7e2957ded0..2fc53d78290 100644
--- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
@@ -270,8 +270,9 @@ bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
/// Create an empty function with the given name.
static Function *createDummyFunction(StringRef Name, Module &M) {
auto &Context = M.getContext();
- Function *F = cast<Function>(M.getOrInsertFunction(
- Name, FunctionType::get(Type::getVoidTy(Context), false)));
+ Function *F =
+ Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
+ Function::ExternalLinkage, Name, M);
BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
new UnreachableInst(Context, BB);
return F;
diff --git a/llvm/lib/CodeGen/MachineOutliner.cpp b/llvm/lib/CodeGen/MachineOutliner.cpp
index 1a0190c2fbf..8c0626092bb 100644
--- a/llvm/lib/CodeGen/MachineOutliner.cpp
+++ b/llvm/lib/CodeGen/MachineOutliner.cpp
@@ -1104,9 +1104,9 @@ MachineOutliner::createOutlinedFunction(Module &M, OutlinedFunction &OF,
// Create the function using an IR-level function.
LLVMContext &C = M.getContext();
- Function *F = dyn_cast<Function>(
- M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
- assert(F && "Function was null!");
+ Function *F =
+ Function::Create(FunctionType::get(Type::getVoidTy(C), false),
+ Function::ExternalLinkage, NameStream.str(), M);
// NOTE: If this is linkonceodr, then we can take advantage of linker deduping
// which gives us better results when we outline from linkonceodr functions.
diff --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
index 915b2c56d49..182c3924a70 100644
--- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
+++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
@@ -64,9 +64,9 @@ static bool lowerObjCCall(Function &F, const char *NewFn,
// If we haven't already looked up this function, check to see if the
// program already contains a function with this name.
Module *M = F.getParent();
- Constant* FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
+ FunctionCallee FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
- if (Function* Fn = dyn_cast<Function>(FCache)) {
+ if (Function *Fn = dyn_cast<Function>(FCache.getCallee())) {
Fn->setLinkage(F.getLinkage());
if (setNonLazyBind && !Fn->isWeakForLinker()) {
// If we have Native ARC, set nonlazybind attribute for these APIs for
diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp
index cf99cb842d8..2b80c63e570 100644
--- a/llvm/lib/CodeGen/SafeStack.cpp
+++ b/llvm/lib/CodeGen/SafeStack.cpp
@@ -474,8 +474,8 @@ void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
/* Unreachable */ true, Weights);
IRBuilder<> IRBFail(CheckTerm);
// FIXME: respect -fsanitize-trap / -ftrap-function here?
- Constant *StackChkFail = F.getParent()->getOrInsertFunction(
- "__stack_chk_fail", IRB.getVoidTy());
+ FunctionCallee StackChkFail =
+ F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
IRBFail.CreateCall(StackChkFail, {});
}
@@ -782,7 +782,7 @@ bool SafeStack::run() {
if (DISubprogram *SP = F.getSubprogram())
IRB.SetCurrentDebugLocation(DebugLoc::get(SP->getScopeLine(), 0, SP));
if (SafeStackUsePointerAddress) {
- Value *Fn = F.getParent()->getOrInsertFunction(
+ FunctionCallee Fn = F.getParent()->getOrInsertFunction(
"__safestack_pointer_address", StackPtrTy->getPointerTo(0));
UnsafeStackPtr = IRB.CreateCall(Fn);
} else {
diff --git a/llvm/lib/CodeGen/SjLjEHPrepare.cpp b/llvm/lib/CodeGen/SjLjEHPrepare.cpp
index 99ed78a508f..94a1ac55fd2 100644
--- a/llvm/lib/CodeGen/SjLjEHPrepare.cpp
+++ b/llvm/lib/CodeGen/SjLjEHPrepare.cpp
@@ -39,15 +39,15 @@ class SjLjEHPrepare : public FunctionPass {
Type *doubleUnderDataTy;
Type *doubleUnderJBufTy;
Type *FunctionContextTy;
- Constant *RegisterFn;
- Constant *UnregisterFn;
- Constant *BuiltinSetupDispatchFn;
- Constant *FrameAddrFn;
- Constant *StackAddrFn;
- Constant *StackRestoreFn;
- Constant *LSDAAddrFn;
- Constant *CallSiteFn;
- Constant *FuncCtxFn;
+ FunctionCallee RegisterFn;
+ FunctionCallee UnregisterFn;
+ Function *BuiltinSetupDispatchFn;
+ Function *FrameAddrFn;
+ Function *StackAddrFn;
+ Function *StackRestoreFn;
+ Function *LSDAAddrFn;
+ Function *CallSiteFn;
+ Function *FuncCtxFn;
AllocaInst *FuncCtx;
public:
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index 78f13e78585..e7be79e8ecb 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -499,14 +499,13 @@ BasicBlock *StackProtector::CreateFailBB() {
IRBuilder<> B(FailBB);
B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
if (Trip.isOSOpenBSD()) {
- Constant *StackChkFail =
- M->getOrInsertFunction("__stack_smash_handler",
- Type::getVoidTy(Context),
- Type::getInt8PtrTy(Context));
+ FunctionCallee StackChkFail = M->getOrInsertFunction(
+ "__stack_smash_handler", Type::getVoidTy(Context),
+ Type::getInt8PtrTy(Context));
B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
} else {
- Constant *StackChkFail =
+ FunctionCallee StackChkFail =
M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
B.CreateCall(StackChkFail, {});
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 1fb67bfce10..01b628442c9 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -1587,8 +1587,8 @@ Value *TargetLoweringBase::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
// thread's unsafe stack pointer.
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
- Value *Fn = M->getOrInsertFunction("__safestack_pointer_address",
- StackPtrTy->getPointerTo(0));
+ FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
+ StackPtrTy->getPointerTo(0));
return IRB.CreateCall(Fn);
}
diff --git a/llvm/lib/CodeGen/WasmEHPrepare.cpp b/llvm/lib/CodeGen/WasmEHPrepare.cpp
index 271f3d4568d..02516fad387 100644
--- a/llvm/lib/CodeGen/WasmEHPrepare.cpp
+++ b/llvm/lib/CodeGen/WasmEHPrepare.cpp
@@ -111,7 +111,8 @@ class WasmEHPrepare : public FunctionPass {
Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic
Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
- Function *CallPersonalityF = nullptr; // _Unwind_CallPersonality() wrapper
+ FunctionCallee CallPersonalityF =
+ nullptr; // _Unwind_CallPersonality() wrapper
bool prepareEHPads(Function &F);
bool prepareThrows(Function &F);
@@ -252,9 +253,10 @@ bool WasmEHPrepare::prepareEHPads(Function &F) {
Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception);
// _Unwind_CallPersonality() wrapper function, which calls the personality
- CallPersonalityF = cast<Function>(M.getOrInsertFunction(
- "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()));
- CallPersonalityF->setDoesNotThrow();
+ CallPersonalityF = M.getOrInsertFunction(
+ "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
+ if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
+ F->setDoesNotThrow();
unsigned Index = 0;
for (auto *BB : CatchPads) {
OpenPOWER on IntegriCloud