1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 10:32:48 +02:00

[opaque pointer types] Add a FunctionCallee wrapper type, and use it.

Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.

Original Message:
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: 352827
This commit is contained in:
James Y Knight 2019-02-01 02:28:03 +00:00
parent c147a4be5b
commit 846be29e5e
69 changed files with 854 additions and 888 deletions

View File

@ -3491,11 +3491,17 @@ Important Public Members of the ``Module`` class
Look up the specified function in the ``Module`` SymbolTable_. If it does not Look up the specified function in the ``Module`` SymbolTable_. If it does not
exist, return ``null``. exist, return ``null``.
* ``Function *getOrInsertFunction(const std::string &Name, const FunctionType * ``FunctionCallee getOrInsertFunction(const std::string &Name,
*T)`` const FunctionType *T)``
Look up the specified function in the ``Module`` SymbolTable_. If it does not Look up the specified function in the ``Module`` SymbolTable_. If
exist, add an external declaration for the function and return it. it does not exist, add an external declaration for the function and
return it. Note that the function signature already present may not
match the requested signature. Thus, in order to enable the common
usage of passing the result directly to EmitCall, the return type is
a struct of ``{FunctionType *T, Constant *FunctionPtr}``, rather
than simply the ``Function*`` with potentially an unexpected
signature.
* ``std::string getTypeName(const Type *Ty)`` * ``std::string getTypeName(const Type *Ty)``

View File

@ -72,19 +72,17 @@ void BrainF::header(LLVMContext& C) {
Tys); Tys);
//declare i32 @getchar() //declare i32 @getchar()
getchar_func = cast<Function>(module-> getchar_func =
getOrInsertFunction("getchar", IntegerType::getInt32Ty(C))); module->getOrInsertFunction("getchar", IntegerType::getInt32Ty(C));
//declare i32 @putchar(i32) //declare i32 @putchar(i32)
putchar_func = cast<Function>(module-> putchar_func = module->getOrInsertFunction(
getOrInsertFunction("putchar", IntegerType::getInt32Ty(C), "putchar", IntegerType::getInt32Ty(C), IntegerType::getInt32Ty(C));
IntegerType::getInt32Ty(C)));
//Function header //Function header
//define void @brainf() //define void @brainf()
brainf_func = cast<Function>(module-> brainf_func = module->getOrInsertFunction("brainf", Type::getVoidTy(C));
getOrInsertFunction("brainf", Type::getVoidTy(C)));
builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func)); builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
@ -153,9 +151,9 @@ void BrainF::header(LLVMContext& C) {
"aberrormsg"); "aberrormsg");
//declare i32 @puts(i8 *) //declare i32 @puts(i8 *)
Function *puts_func = cast<Function>(module-> FunctionCallee puts_func = module->getOrInsertFunction(
getOrInsertFunction("puts", IntegerType::getInt32Ty(C), "puts", IntegerType::getInt32Ty(C),
PointerType::getUnqual(IntegerType::getInt8Ty(C)))); PointerType::getUnqual(IntegerType::getInt8Ty(C)));
//brainf.aberror: //brainf.aberror:
aberrorbb = BasicBlock::Create(C, label, brainf_func); aberrorbb = BasicBlock::Create(C, label, brainf_func);

View File

@ -78,9 +78,9 @@ class BrainF {
CompileFlags comflag; CompileFlags comflag;
std::istream *in; std::istream *in;
Module *module; Module *module;
Function *brainf_func; FunctionCallee brainf_func;
Function *getchar_func; FunctionCallee getchar_func;
Function *putchar_func; FunctionCallee putchar_func;
Value *ptr_arr; Value *ptr_arr;
Value *ptr_arrmax; Value *ptr_arrmax;
BasicBlock *endbb; BasicBlock *endbb;

View File

@ -72,11 +72,13 @@ JIT("jit", cl::desc("Run program Just-In-Time"));
//Add main function so can be fully compiled //Add main function so can be fully compiled
void addMainFunction(Module *mod) { void addMainFunction(Module *mod) {
//define i32 @main(i32 %argc, i8 **%argv) //define i32 @main(i32 %argc, i8 **%argv)
Function *main_func = cast<Function>(mod-> FunctionType *main_func_fty = FunctionType::get(
getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()), Type::getInt32Ty(mod->getContext()),
IntegerType::getInt32Ty(mod->getContext()), {Type::getInt32Ty(mod->getContext()),
PointerType::getUnqual(PointerType::getUnqual( Type::getInt8Ty(mod->getContext())->getPointerTo()->getPointerTo()});
IntegerType::getInt8Ty(mod->getContext()))))); Function *main_func =
Function::create(main_func_fty, Function::ExternalLinkage, "main", mod);
{ {
Function::arg_iterator args = main_func->arg_begin(); Function::arg_iterator args = main_func->arg_begin();
Value *arg_0 = &*args++; Value *arg_0 = &*args++;

View File

@ -51,9 +51,10 @@ using namespace llvm;
static Function *CreateFibFunction(Module *M, LLVMContext &Context) { static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
// Create the fib function and insert it into module M. This function is said // Create the fib function and insert it into module M. This function is said
// to return an int and take an int parameter. // to return an int and take an int parameter.
FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
{Type::getInt32Ty(Context)}, false);
Function *FibF = Function *FibF =
cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context), Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
Type::getInt32Ty(Context)));
// Add a basic block to the function. // Add a basic block to the function.
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF); BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);

View File

@ -69,8 +69,9 @@ int main() {
// Create the add1 function entry and insert this entry into module M. The // Create the add1 function entry and insert this entry into module M. The
// function will have a return type of "int" and take an argument of "int". // function will have a return type of "int" and take an argument of "int".
Function *Add1F = Function *Add1F =
cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context), Function::Create(FunctionType::get(Type::getInt32Ty(Context),
Type::getInt32Ty(Context))); {Type::getInt32Ty(Context)}, false),
Function::ExternalLinkage, "add1", M);
// Add a basic block to the function. As before, it automatically inserts // Add a basic block to the function. As before, it automatically inserts
// because of the last argument. // because of the last argument.
@ -99,7 +100,8 @@ int main() {
// Now we're going to create function `foo', which returns an int and takes no // Now we're going to create function `foo', which returns an int and takes no
// arguments. // arguments.
Function *FooF = Function *FooF =
cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context))); Function::Create(FunctionType::get(Type::getInt32Ty(Context), {}, false),
Function::ExternalLinkage, "foo", M);
// Add a basic block to the FooF function. // Add a basic block to the FooF function.
BB = BasicBlock::Create(Context, "EntryBlock", FooF); BB = BasicBlock::Create(Context, "EntryBlock", FooF);

View File

@ -49,11 +49,10 @@ using namespace llvm;
static Function* createAdd1(Module *M) { static Function* createAdd1(Module *M) {
// Create the add1 function entry and insert this entry into module M. The // Create the add1 function entry and insert this entry into module M. The
// function will have a return type of "int" and take an argument of "int". // function will have a return type of "int" and take an argument of "int".
// The '0' terminates the list of argument types.
Function *Add1F = Function *Add1F =
cast<Function>(M->getOrInsertFunction("add1", Function::Create(FunctionType::get(Type::getInt32Ty(Context),
Type::getInt32Ty(M->getContext()), {Type::getInt32Ty(Context)}, false),
Type::getInt32Ty(M->getContext()))); Function::ExternalLinkage, "add1", M);
// Add a basic block to the function. As before, it automatically inserts // Add a basic block to the function. As before, it automatically inserts
// because of the last argument. // because of the last argument.
@ -80,10 +79,10 @@ static Function* createAdd1(Module *M) {
static Function *CreateFibFunction(Module *M) { static Function *CreateFibFunction(Module *M) {
// Create the fib function and insert it into module M. This function is said // Create the fib function and insert it into module M. This function is said
// to return an int and take an int parameter. // to return an int and take an int parameter.
Function *FibF = FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
cast<Function>(M->getOrInsertFunction("fib", {Type::getInt32Ty(Context)}, false);
Type::getInt32Ty(M->getContext()), Function *FibF =
Type::getInt32Ty(M->getContext()))); Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
// Add a basic block to the function. // Add a basic block to the function.
BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF); BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);

View File

@ -30,10 +30,6 @@ class IntrinsicLowering {
public: public:
explicit IntrinsicLowering(const DataLayout &DL) : DL(DL), Warned(false) {} explicit IntrinsicLowering(const DataLayout &DL) : DL(DL), Warned(false) {}
/// Add all of the prototypes that might be needed by an intrinsic lowering
/// implementation to be inserted into the module specified.
void AddPrototypes(Module &M);
/// Replace a call to the specified intrinsic function. /// Replace a call to the specified intrinsic function.
/// If an intrinsic function must be implemented by the code generator /// If an intrinsic function must be implemented by the code generator
/// (such as va_start), this function should print a message and abort. /// (such as va_start), this function should print a message and abort.

View File

@ -157,6 +157,38 @@ unsigned Type::getFunctionNumParams() const {
return cast<FunctionType>(this)->getNumParams(); return cast<FunctionType>(this)->getNumParams();
} }
/// A handy container for a FunctionType+Callee-pointer pair, which can be
/// passed around as a single entity. This assists in replacing the use of
/// PointerType::getElementType() to access the function's type, since that's
/// slated for removal as part of the [opaque pointer types] project.
class FunctionCallee {
public:
// Allow implicit conversion from types which have a getFunctionType member
// (e.g. Function and InlineAsm).
template <typename T, typename U = decltype(&T::getFunctionType)>
FunctionCallee(T *Fn)
: FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
FunctionCallee(FunctionType *FnTy, Value *Callee)
: FnTy(FnTy), Callee(Callee) {
assert((FnTy == nullptr) == (Callee == nullptr));
}
FunctionCallee(std::nullptr_t) {}
FunctionCallee() = default;
FunctionType *getFunctionType() { return FnTy; }
Value *getCallee() { return Callee; }
explicit operator bool() { return Callee; }
private:
FunctionType *FnTy = nullptr;
Value *Callee = nullptr;
};
/// Common super class of ArrayType, StructType and VectorType. /// Common super class of ArrayType, StructType and VectorType.
class CompositeType : public Type { class CompositeType : public Type {
protected: protected:

View File

@ -905,20 +905,20 @@ public:
Name); Name);
} }
InvokeInst *CreateInvoke(Function *Callee, BasicBlock *NormalDest, InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
BasicBlock *UnwindDest, ArrayRef<Value *> Args, BasicBlock *UnwindDest, ArrayRef<Value *> Args,
ArrayRef<OperandBundleDef> OpBundles, ArrayRef<OperandBundleDef> OpBundles,
const Twine &Name = "") { const Twine &Name = "") {
return CreateInvoke(Callee->getFunctionType(), Callee, NormalDest, return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
UnwindDest, Args, OpBundles, Name); NormalDest, UnwindDest, Args, OpBundles, Name);
} }
InvokeInst *CreateInvoke(Function *Callee, BasicBlock *NormalDest, InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
BasicBlock *UnwindDest, BasicBlock *UnwindDest,
ArrayRef<Value *> Args = None, ArrayRef<Value *> Args = None,
const Twine &Name = "") { const Twine &Name = "") {
return CreateInvoke(Callee->getFunctionType(), Callee, NormalDest, return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
UnwindDest, Args, Name); NormalDest, UnwindDest, Args, Name);
} }
// Deprecated [opaque pointer types] // Deprecated [opaque pointer types]
@ -1988,16 +1988,17 @@ public:
return Insert(CI, Name); return Insert(CI, Name);
} }
CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args = None, CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,
const Twine &Name = "", MDNode *FPMathTag = nullptr) { const Twine &Name = "", MDNode *FPMathTag = nullptr) {
return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag); return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
FPMathTag);
} }
CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args, CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
ArrayRef<OperandBundleDef> OpBundles, ArrayRef<OperandBundleDef> OpBundles,
const Twine &Name = "", MDNode *FPMathTag = nullptr) { const Twine &Name = "", MDNode *FPMathTag = nullptr) {
return CreateCall(Callee->getFunctionType(), Callee, Args, OpBundles, Name, return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
FPMathTag); OpBundles, Name, FPMathTag);
} }
// Deprecated [opaque pointer types] // Deprecated [opaque pointer types]

View File

@ -1232,6 +1232,11 @@ public:
Fn); Fn);
} }
/// Sets the function called, including updating the function type.
void setCalledFunction(FunctionCallee Fn) {
setCalledFunction(Fn.getFunctionType(), Fn.getCallee());
}
/// Sets the function called, including updating to the specified function /// Sets the function called, including updating to the specified function
/// type. /// type.
void setCalledFunction(FunctionType *FTy, Value *Fn) { void setCalledFunction(FunctionType *FTy, Value *Fn) {

View File

@ -1543,25 +1543,44 @@ public:
CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd);
} }
static CallInst *Create(Function *Func, const Twine &NameStr = "", static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
Instruction *InsertBefore = nullptr) { Instruction *InsertBefore = nullptr) {
return Create(Func->getFunctionType(), Func, NameStr, InsertBefore); return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
InsertBefore);
} }
static CallInst *Create(Function *Func, ArrayRef<Value *> Args, static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
ArrayRef<OperandBundleDef> Bundles = None,
const Twine &NameStr = "", const Twine &NameStr = "",
Instruction *InsertBefore = nullptr) { Instruction *InsertBefore = nullptr) {
return Create(Func->getFunctionType(), Func, Args, NameStr, InsertBefore); return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
NameStr, InsertBefore);
} }
static CallInst *Create(Function *Func, const Twine &NameStr, static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
const Twine &NameStr,
Instruction *InsertBefore = nullptr) {
return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
InsertBefore);
}
static CallInst *Create(FunctionCallee Func, const Twine &NameStr,
BasicBlock *InsertAtEnd) { BasicBlock *InsertAtEnd) {
return Create(Func->getFunctionType(), Func, NameStr, InsertAtEnd); return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
InsertAtEnd);
} }
static CallInst *Create(Function *Func, ArrayRef<Value *> Args, static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
const Twine &NameStr, BasicBlock *InsertAtEnd) { const Twine &NameStr, BasicBlock *InsertAtEnd) {
return Create(Func->getFunctionType(), Func, Args, NameStr, InsertAtEnd); return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
InsertAtEnd);
}
static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
ArrayRef<OperandBundleDef> Bundles,
const Twine &NameStr, BasicBlock *InsertAtEnd) {
return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
NameStr, InsertAtEnd);
} }
// Deprecated [opaque pointer types] // Deprecated [opaque pointer types]
@ -3704,36 +3723,36 @@ public:
NameStr, InsertAtEnd); NameStr, InsertAtEnd);
} }
static InvokeInst *Create(Function *Func, BasicBlock *IfNormal, static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
BasicBlock *IfException, ArrayRef<Value *> Args, BasicBlock *IfException, ArrayRef<Value *> Args,
const Twine &NameStr, const Twine &NameStr,
Instruction *InsertBefore = nullptr) { Instruction *InsertBefore = nullptr) {
return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args, return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
None, NameStr, InsertBefore); IfException, Args, None, NameStr, InsertBefore);
} }
static InvokeInst *Create(Function *Func, BasicBlock *IfNormal, static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
BasicBlock *IfException, ArrayRef<Value *> Args, BasicBlock *IfException, ArrayRef<Value *> Args,
ArrayRef<OperandBundleDef> Bundles = None, ArrayRef<OperandBundleDef> Bundles = None,
const Twine &NameStr = "", const Twine &NameStr = "",
Instruction *InsertBefore = nullptr) { Instruction *InsertBefore = nullptr) {
return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args, return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
Bundles, NameStr, InsertBefore); IfException, Args, Bundles, NameStr, InsertBefore);
} }
static InvokeInst *Create(Function *Func, BasicBlock *IfNormal, static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
BasicBlock *IfException, ArrayRef<Value *> Args, BasicBlock *IfException, ArrayRef<Value *> Args,
const Twine &NameStr, BasicBlock *InsertAtEnd) { const Twine &NameStr, BasicBlock *InsertAtEnd) {
return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args, return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
NameStr, InsertAtEnd); IfException, Args, NameStr, InsertAtEnd);
} }
static InvokeInst *Create(Function *Func, BasicBlock *IfNormal, static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
BasicBlock *IfException, ArrayRef<Value *> Args, BasicBlock *IfException, ArrayRef<Value *> Args,
ArrayRef<OperandBundleDef> Bundles, ArrayRef<OperandBundleDef> Bundles,
const Twine &NameStr, BasicBlock *InsertAtEnd) { const Twine &NameStr, BasicBlock *InsertAtEnd) {
return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args, return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
Bundles, NameStr, InsertAtEnd); IfException, Args, Bundles, NameStr, InsertAtEnd);
} }
// Deprecated [opaque pointer types] // Deprecated [opaque pointer types]

View File

@ -332,16 +332,18 @@ public:
/// Look up the specified function in the module symbol table. Four /// Look up the specified function in the module symbol table. Four
/// possibilities: /// possibilities:
/// 1. If it does not exist, add a prototype for the function and return it. /// 1. If it does not exist, add a prototype for the function and return it.
/// 2. If it exists, and has a local linkage, the existing function is /// 2. Otherwise, if the existing function has the correct prototype, return
/// renamed and a new one is inserted.
/// 3. Otherwise, if the existing function has the correct prototype, return
/// the existing function. /// the existing function.
/// 4. Finally, the function exists but has the wrong prototype: return the /// 3. Finally, the function exists but has the wrong prototype: return the
/// function with a constantexpr cast to the right prototype. /// function with a constantexpr cast to the right prototype.
Constant *getOrInsertFunction(StringRef Name, FunctionType *T, ///
AttributeList AttributeList); /// In all cases, the returned value is a FunctionCallee wrapper around the
/// 'FunctionType *T' passed in, as well as a 'Value*' either of the Function or
/// the bitcast to the function.
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T,
AttributeList AttributeList);
Constant *getOrInsertFunction(StringRef Name, FunctionType *T); FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T);
/// Look up the specified function in the module symbol table. If it does not /// Look up the specified function in the module symbol table. If it does not
/// exist, add a prototype for the function and return it. This function /// exist, add a prototype for the function and return it. This function
@ -349,11 +351,10 @@ public:
/// or a ConstantExpr BitCast of that type if the named function has a /// or a ConstantExpr BitCast of that type if the named function has a
/// different type. This version of the method takes a list of /// different type. This version of the method takes a list of
/// function arguments, which makes it easier for clients to use. /// function arguments, which makes it easier for clients to use.
template<typename... ArgsTy> template <typename... ArgsTy>
Constant *getOrInsertFunction(StringRef Name, FunctionCallee getOrInsertFunction(StringRef Name,
AttributeList AttributeList, AttributeList AttributeList, Type *RetTy,
Type *RetTy, ArgsTy... Args) ArgsTy... Args) {
{
SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...}; SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...};
return getOrInsertFunction(Name, return getOrInsertFunction(Name,
FunctionType::get(RetTy, ArgTys, false), FunctionType::get(RetTy, ArgTys, false),
@ -361,15 +362,17 @@ public:
} }
/// Same as above, but without the attributes. /// Same as above, but without the attributes.
template<typename... ArgsTy> template <typename... ArgsTy>
Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) { FunctionCallee getOrInsertFunction(StringRef Name, Type *RetTy,
ArgsTy... Args) {
return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...); return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
} }
// Avoid an incorrect ordering that'd otherwise compile incorrectly. // Avoid an incorrect ordering that'd otherwise compile incorrectly.
template <typename... ArgsTy> template <typename... ArgsTy>
Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList, FunctionCallee
FunctionType *Invalid, ArgsTy... Args) = delete; getOrInsertFunction(StringRef Name, AttributeList AttributeList,
FunctionType *Invalid, ArgsTy... Args) = delete;
/// Look up the specified function in the module symbol table. If it does not /// Look up the specified function in the module symbol table. If it does not
/// exist, return null. /// exist, return null.

View File

@ -21,6 +21,7 @@ namespace llvm {
template <typename T> class ArrayRef; template <typename T> class ArrayRef;
class Module; class Module;
class Function; class Function;
class FunctionCallee;
class GlobalValue; class GlobalValue;
class GlobalVariable; class GlobalVariable;
class Constant; class Constant;
@ -39,20 +40,14 @@ void appendToGlobalCtors(Module &M, Function *F, int Priority,
void appendToGlobalDtors(Module &M, Function *F, int Priority, void appendToGlobalDtors(Module &M, Function *F, int Priority,
Constant *Data = nullptr); Constant *Data = nullptr);
// Validate the result of Module::getOrInsertFunction called for an interface FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
// function of given sanitizer. If the instrumented module defines a function ArrayRef<Type *> InitArgTypes);
// with the same name, their prototypes must match, otherwise
// getOrInsertFunction returns a bitcast.
Function *checkSanitizerInterfaceFunction(Constant *FuncOrBitcast);
Function *declareSanitizerInitFunction(Module &M, StringRef InitName,
ArrayRef<Type *> InitArgTypes);
/// Creates sanitizer constructor function, and calls sanitizer's init /// Creates sanitizer constructor function, and calls sanitizer's init
/// function from it. /// function from it.
/// \return Returns pair of pointers to constructor, and init functions /// \return Returns pair of pointers to constructor, and init functions
/// respectively. /// respectively.
std::pair<Function *, Function *> createSanitizerCtorAndInitFunctions( std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
Module &M, StringRef CtorName, StringRef InitName, Module &M, StringRef CtorName, StringRef InitName,
ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs, ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
StringRef VersionCheckName = StringRef()); StringRef VersionCheckName = StringRef());
@ -64,10 +59,10 @@ std::pair<Function *, Function *> createSanitizerCtorAndInitFunctions(
/// ///
/// \return Returns pair of pointers to constructor, and init functions /// \return Returns pair of pointers to constructor, and init functions
/// respectively. /// respectively.
std::pair<Function *, Function *> getOrCreateSanitizerCtorAndInitFunctions( std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
Module &M, StringRef CtorName, StringRef InitName, Module &M, StringRef CtorName, StringRef InitName,
ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs, ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
function_ref<void(Function *, Function *)> FunctionsCreatedCallback, function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
StringRef VersionCheckName = StringRef()); StringRef VersionCheckName = StringRef());
// Creates and returns a sanitizer init function without argument if it doesn't // Creates and returns a sanitizer init function without argument if it doesn't

View File

@ -1754,7 +1754,7 @@ bool AtomicExpand::expandAtomicOpToLibcall(
for (Value *Arg : Args) for (Value *Arg : Args)
ArgTys.push_back(Arg->getType()); ArgTys.push_back(Arg->getType());
FunctionType *FnType = FunctionType::get(ResultTy, ArgTys, false); FunctionType *FnType = FunctionType::get(ResultTy, ArgTys, false);
Constant *LibcallFn = FunctionCallee LibcallFn =
M->getOrInsertFunction(TLI->getLibcallName(RTLibType), FnType, Attr); M->getOrInsertFunction(TLI->getLibcallName(RTLibType), FnType, Attr);
CallInst *Call = Builder.CreateCall(LibcallFn, Args); CallInst *Call = Builder.CreateCall(LibcallFn, Args);
Call->setAttributes(Attr); Call->setAttributes(Attr);

View File

@ -45,7 +45,7 @@ namespace {
class DwarfEHPrepare : public FunctionPass { class DwarfEHPrepare : public FunctionPass {
// RewindFunction - _Unwind_Resume or the target equivalent. // RewindFunction - _Unwind_Resume or the target equivalent.
Constant *RewindFunction = nullptr; FunctionCallee RewindFunction = nullptr;
DominatorTree *DT = nullptr; DominatorTree *DT = nullptr;
const TargetLowering *TLI = nullptr; const TargetLowering *TLI = nullptr;

View File

@ -23,39 +23,6 @@
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
using namespace llvm; 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 /// 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 /// 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 /// 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; std::vector<Type *> ParamTys;
for (ArgIt I = ArgBegin; I != ArgEnd; ++I) for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
ParamTys.push_back((*I)->getType()); ParamTys.push_back((*I)->getType());
Constant* FCache = M->getOrInsertFunction(NewFn, FunctionCallee FCache =
FunctionType::get(RetTy, ParamTys, false)); M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
IRBuilder<> Builder(CI->getParent(), CI->getIterator()); IRBuilder<> Builder(CI->getParent(), CI->getIterator());
SmallVector<Value *, 8> Args(ArgBegin, ArgEnd); SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
@ -91,75 +58,6 @@ static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
# define setjmp_undefined_for_msvc # define setjmp_undefined_for_msvc
#endif #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. /// Emit the code to lower bswap of V before the specified instruction IP.
static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) { static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!"); assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");

View File

@ -270,8 +270,9 @@ bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
/// Create an empty function with the given name. /// Create an empty function with the given name.
static Function *createDummyFunction(StringRef Name, Module &M) { static Function *createDummyFunction(StringRef Name, Module &M) {
auto &Context = M.getContext(); auto &Context = M.getContext();
Function *F = cast<Function>(M.getOrInsertFunction( Function *F =
Name, FunctionType::get(Type::getVoidTy(Context), false))); Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
Function::ExternalLinkage, Name, M);
BasicBlock *BB = BasicBlock::Create(Context, "entry", F); BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
new UnreachableInst(Context, BB); new UnreachableInst(Context, BB);
return F; return F;

View File

@ -1104,9 +1104,9 @@ MachineOutliner::createOutlinedFunction(Module &M, OutlinedFunction &OF,
// Create the function using an IR-level function. // Create the function using an IR-level function.
LLVMContext &C = M.getContext(); LLVMContext &C = M.getContext();
Function *F = dyn_cast<Function>( Function *F =
M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C))); Function::Create(FunctionType::get(Type::getVoidTy(C), false),
assert(F && "Function was null!"); Function::ExternalLinkage, NameStream.str(), M);
// NOTE: If this is linkonceodr, then we can take advantage of linker deduping // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
// which gives us better results when we outline from linkonceodr functions. // which gives us better results when we outline from linkonceodr functions.

View File

@ -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 // If we haven't already looked up this function, check to see if the
// program already contains a function with this name. // program already contains a function with this name.
Module *M = F.getParent(); 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()); Fn->setLinkage(F.getLinkage());
if (setNonLazyBind && !Fn->isWeakForLinker()) { if (setNonLazyBind && !Fn->isWeakForLinker()) {
// If we have Native ARC, set nonlazybind attribute for these APIs for // If we have Native ARC, set nonlazybind attribute for these APIs for

View File

@ -474,8 +474,8 @@ void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
/* Unreachable */ true, Weights); /* Unreachable */ true, Weights);
IRBuilder<> IRBFail(CheckTerm); IRBuilder<> IRBFail(CheckTerm);
// FIXME: respect -fsanitize-trap / -ftrap-function here? // FIXME: respect -fsanitize-trap / -ftrap-function here?
Constant *StackChkFail = F.getParent()->getOrInsertFunction( FunctionCallee StackChkFail =
"__stack_chk_fail", IRB.getVoidTy()); F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
IRBFail.CreateCall(StackChkFail, {}); IRBFail.CreateCall(StackChkFail, {});
} }
@ -782,7 +782,7 @@ bool SafeStack::run() {
if (DISubprogram *SP = F.getSubprogram()) if (DISubprogram *SP = F.getSubprogram())
IRB.SetCurrentDebugLocation(DebugLoc::get(SP->getScopeLine(), 0, SP)); IRB.SetCurrentDebugLocation(DebugLoc::get(SP->getScopeLine(), 0, SP));
if (SafeStackUsePointerAddress) { if (SafeStackUsePointerAddress) {
Value *Fn = F.getParent()->getOrInsertFunction( FunctionCallee Fn = F.getParent()->getOrInsertFunction(
"__safestack_pointer_address", StackPtrTy->getPointerTo(0)); "__safestack_pointer_address", StackPtrTy->getPointerTo(0));
UnsafeStackPtr = IRB.CreateCall(Fn); UnsafeStackPtr = IRB.CreateCall(Fn);
} else { } else {

View File

@ -39,15 +39,15 @@ class SjLjEHPrepare : public FunctionPass {
Type *doubleUnderDataTy; Type *doubleUnderDataTy;
Type *doubleUnderJBufTy; Type *doubleUnderJBufTy;
Type *FunctionContextTy; Type *FunctionContextTy;
Constant *RegisterFn; FunctionCallee RegisterFn;
Constant *UnregisterFn; FunctionCallee UnregisterFn;
Constant *BuiltinSetupDispatchFn; Function *BuiltinSetupDispatchFn;
Constant *FrameAddrFn; Function *FrameAddrFn;
Constant *StackAddrFn; Function *StackAddrFn;
Constant *StackRestoreFn; Function *StackRestoreFn;
Constant *LSDAAddrFn; Function *LSDAAddrFn;
Constant *CallSiteFn; Function *CallSiteFn;
Constant *FuncCtxFn; Function *FuncCtxFn;
AllocaInst *FuncCtx; AllocaInst *FuncCtx;
public: public:

View File

@ -499,14 +499,13 @@ BasicBlock *StackProtector::CreateFailBB() {
IRBuilder<> B(FailBB); IRBuilder<> B(FailBB);
B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram())); B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
if (Trip.isOSOpenBSD()) { if (Trip.isOSOpenBSD()) {
Constant *StackChkFail = FunctionCallee StackChkFail = M->getOrInsertFunction(
M->getOrInsertFunction("__stack_smash_handler", "__stack_smash_handler", Type::getVoidTy(Context),
Type::getVoidTy(Context), Type::getInt8PtrTy(Context));
Type::getInt8PtrTy(Context));
B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH")); B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
} else { } else {
Constant *StackChkFail = FunctionCallee StackChkFail =
M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context)); M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
B.CreateCall(StackChkFail, {}); B.CreateCall(StackChkFail, {});

View File

@ -1587,8 +1587,8 @@ Value *TargetLoweringBase::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
// thread's unsafe stack pointer. // thread's unsafe stack pointer.
Module *M = IRB.GetInsertBlock()->getParent()->getParent(); Module *M = IRB.GetInsertBlock()->getParent()->getParent();
Type *StackPtrTy = Type::getInt8PtrTy(M->getContext()); Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
Value *Fn = M->getOrInsertFunction("__safestack_pointer_address", FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
StackPtrTy->getPointerTo(0)); StackPtrTy->getPointerTo(0));
return IRB.CreateCall(Fn); return IRB.CreateCall(Fn);
} }

View File

@ -111,7 +111,8 @@ class WasmEHPrepare : public FunctionPass {
Function *GetExnF = nullptr; // wasm.get.exception() intrinsic Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic
Function *GetSelectorF = nullptr; // wasm.get.ehselector() 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 prepareEHPads(Function &F);
bool prepareThrows(Function &F); bool prepareThrows(Function &F);
@ -252,9 +253,10 @@ bool WasmEHPrepare::prepareEHPads(Function &F) {
Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception); Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception);
// _Unwind_CallPersonality() wrapper function, which calls the personality // _Unwind_CallPersonality() wrapper function, which calls the personality
CallPersonalityF = cast<Function>(M.getOrInsertFunction( CallPersonalityF = M.getOrInsertFunction(
"_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy())); "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
CallPersonalityF->setDoesNotThrow(); if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
F->setDoesNotThrow();
unsigned Index = 0; unsigned Index = 0;
for (auto *BB : CatchPads) { for (auto *BB : CatchPads) {

View File

@ -1018,9 +1018,10 @@ bool Intrinsic::isLeaf(ID id) {
Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
// There can never be multiple globals with the same name of different types, // There can never be multiple globals with the same name of different types,
// because intrinsics must be a specific type. // because intrinsics must be a specific type.
return return cast<Function>(
cast<Function>(M->getOrInsertFunction(getName(id, Tys), M->getOrInsertFunction(getName(id, Tys),
getType(M->getContext(), id, Tys))); getType(M->getContext(), id, Tys))
.getCallee());
} }
// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.

View File

@ -517,7 +517,7 @@ static Instruction *createMalloc(Instruction *InsertBefore,
BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
Module *M = BB->getParent()->getParent(); Module *M = BB->getParent()->getParent();
Type *BPTy = Type::getInt8PtrTy(BB->getContext()); Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Value *MallocFunc = MallocF; FunctionCallee MallocFunc = MallocF;
if (!MallocFunc) if (!MallocFunc)
// prototype malloc as "void *malloc(size_t)" // prototype malloc as "void *malloc(size_t)"
MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
@ -541,7 +541,7 @@ static Instruction *createMalloc(Instruction *InsertBefore,
} }
} }
MCall->setTailCall(); MCall->setTailCall();
if (Function *F = dyn_cast<Function>(MallocFunc)) { if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) {
MCall->setCallingConv(F->getCallingConv()); MCall->setCallingConv(F->getCallingConv());
if (!F->returnDoesNotAlias()) if (!F->returnDoesNotAlias())
F->setReturnDoesNotAlias(); F->setReturnDoesNotAlias();
@ -614,7 +614,7 @@ static Instruction *createFree(Value *Source,
Type *VoidTy = Type::getVoidTy(M->getContext()); Type *VoidTy = Type::getVoidTy(M->getContext());
Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
// prototype free as "void free(void*)" // prototype free as "void free(void*)"
Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
CallInst *Result = nullptr; CallInst *Result = nullptr;
Value *PtrCast = Source; Value *PtrCast = Source;
if (InsertBefore) { if (InsertBefore) {
@ -627,7 +627,7 @@ static Instruction *createFree(Value *Source,
Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
} }
Result->setTailCall(); Result->setTailCall();
if (Function *F = dyn_cast<Function>(FreeFunc)) if (Function *F = dyn_cast<Function>(FreeFunc.getCallee()))
Result->setCallingConv(F->getCallingConv()); Result->setCallingConv(F->getCallingConv());
return Result; return Result;

View File

@ -140,8 +140,8 @@ void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
// it. This is nice because it allows most passes to get away with not handling // it. This is nice because it allows most passes to get away with not handling
// the symbol table directly for this common task. // the symbol table directly for this common task.
// //
Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty, FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
AttributeList AttributeList) { AttributeList AttributeList) {
// See if we have a definition for the specified function already. // See if we have a definition for the specified function already.
GlobalValue *F = getNamedValue(Name); GlobalValue *F = getNamedValue(Name);
if (!F) { if (!F) {
@ -151,21 +151,20 @@ Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
if (!New->isIntrinsic()) // Intrinsics get attrs set on construction if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
New->setAttributes(AttributeList); New->setAttributes(AttributeList);
FunctionList.push_back(New); FunctionList.push_back(New);
return New; // Return the new prototype. return {Ty, New}; // Return the new prototype.
} }
// If the function exists but has the wrong type, return a bitcast to the // If the function exists but has the wrong type, return a bitcast to the
// right type. // right type.
auto *PTy = PointerType::get(Ty, F->getAddressSpace()); auto *PTy = PointerType::get(Ty, F->getAddressSpace());
if (F->getType() != PTy) if (F->getType() != PTy)
return ConstantExpr::getBitCast(F, PTy); return {Ty, ConstantExpr::getBitCast(F, PTy)};
// Otherwise, we just found the existing function or a prototype. // Otherwise, we just found the existing function or a prototype.
return F; return {Ty, F};
} }
Constant *Module::getOrInsertFunction(StringRef Name, FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
FunctionType *Ty) {
return getOrInsertFunction(Name, Ty, AttributeList()); return getOrInsertFunction(Name, Ty, AttributeList());
} }

View File

@ -11748,12 +11748,13 @@ void AArch64TargetLowering::insertSSPDeclarations(Module &M) const {
Type::getInt8PtrTy(M.getContext())); Type::getInt8PtrTy(M.getContext()));
// MSVC CRT has a function to validate security cookie. // MSVC CRT has a function to validate security cookie.
auto *SecurityCheckCookie = cast<Function>( FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
M.getOrInsertFunction("__security_check_cookie", "__security_check_cookie", Type::getVoidTy(M.getContext()),
Type::getVoidTy(M.getContext()), Type::getInt8PtrTy(M.getContext()));
Type::getInt8PtrTy(M.getContext()))); if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
SecurityCheckCookie->setCallingConv(CallingConv::Win64); F->setCallingConv(CallingConv::Win64);
SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg); F->addAttribute(1, Attribute::AttrKind::InReg);
}
return; return;
} }
TargetLowering::insertSSPDeclarations(M); TargetLowering::insertSSPDeclarations(M);

View File

@ -72,7 +72,7 @@ private:
// Return a pointer (pointer expr) to the function if function defintion with // Return a pointer (pointer expr) to the function if function defintion with
// "FuncName" exists. It may create a new function prototype in pre-link mode. // "FuncName" exists. It may create a new function prototype in pre-link mode.
Constant *getFunction(Module *M, const FuncInfo& fInfo); FunctionCallee getFunction(Module *M, const FuncInfo &fInfo);
// Replace a normal function with its native version. // Replace a normal function with its native version.
bool replaceWithNative(CallInst *CI, const FuncInfo &FInfo); bool replaceWithNative(CallInst *CI, const FuncInfo &FInfo);
@ -139,7 +139,7 @@ private:
// Insert an Alloc instruction. // Insert an Alloc instruction.
AllocaInst* insertAlloca(CallInst * UI, IRBuilder<> &B, const char *prefix); AllocaInst* insertAlloca(CallInst * UI, IRBuilder<> &B, const char *prefix);
// Get a scalar native builtin signle argument FP function // Get a scalar native builtin signle argument FP function
Constant* getNativeFunction(Module* M, const FuncInfo &FInfo); FunctionCallee getNativeFunction(Module *M, const FuncInfo &FInfo);
protected: protected:
CallInst *CI; CallInst *CI;
@ -216,19 +216,19 @@ INITIALIZE_PASS(AMDGPUUseNativeCalls, "amdgpu-usenative",
false, false) false, false)
template <typename IRB> template <typename IRB>
static CallInst *CreateCallEx(IRB &B, Value *Callee, Value *Arg, static CallInst *CreateCallEx(IRB &B, FunctionCallee Callee, Value *Arg,
const Twine &Name = "") { const Twine &Name = "") {
CallInst *R = B.CreateCall(Callee, Arg, Name); CallInst *R = B.CreateCall(Callee, Arg, Name);
if (Function* F = dyn_cast<Function>(Callee)) if (Function *F = dyn_cast<Function>(Callee.getCallee()))
R->setCallingConv(F->getCallingConv()); R->setCallingConv(F->getCallingConv());
return R; return R;
} }
template <typename IRB> template <typename IRB>
static CallInst *CreateCallEx2(IRB &B, Value *Callee, Value *Arg1, Value *Arg2, static CallInst *CreateCallEx2(IRB &B, FunctionCallee Callee, Value *Arg1,
const Twine &Name = "") { Value *Arg2, const Twine &Name = "") {
CallInst *R = B.CreateCall(Callee, {Arg1, Arg2}, Name); CallInst *R = B.CreateCall(Callee, {Arg1, Arg2}, Name);
if (Function* F = dyn_cast<Function>(Callee)) if (Function *F = dyn_cast<Function>(Callee.getCallee()))
R->setCallingConv(F->getCallingConv()); R->setCallingConv(F->getCallingConv());
return R; return R;
} }
@ -471,7 +471,7 @@ static inline AMDGPULibFunc::EType getArgType(const AMDGPULibFunc& FInfo) {
return (AMDGPULibFunc::EType)FInfo.getLeads()[0].ArgType; return (AMDGPULibFunc::EType)FInfo.getLeads()[0].ArgType;
} }
Constant *AMDGPULibCalls::getFunction(Module *M, const FuncInfo& fInfo) { FunctionCallee AMDGPULibCalls::getFunction(Module *M, const FuncInfo &fInfo) {
// If we are doing PreLinkOpt, the function is external. So it is safe to // If we are doing PreLinkOpt, the function is external. So it is safe to
// use getOrInsertFunction() at this stage. // use getOrInsertFunction() at this stage.
@ -518,11 +518,11 @@ bool AMDGPULibCalls::sincosUseNative(CallInst *aCI, const FuncInfo &FInfo) {
nf.setPrefix(AMDGPULibFunc::NATIVE); nf.setPrefix(AMDGPULibFunc::NATIVE);
nf.setId(AMDGPULibFunc::EI_SIN); nf.setId(AMDGPULibFunc::EI_SIN);
Constant *sinExpr = getFunction(M, nf); FunctionCallee sinExpr = getFunction(M, nf);
nf.setPrefix(AMDGPULibFunc::NATIVE); nf.setPrefix(AMDGPULibFunc::NATIVE);
nf.setId(AMDGPULibFunc::EI_COS); nf.setId(AMDGPULibFunc::EI_COS);
Constant *cosExpr = getFunction(M, nf); FunctionCallee cosExpr = getFunction(M, nf);
if (sinExpr && cosExpr) { if (sinExpr && cosExpr) {
Value *sinval = CallInst::Create(sinExpr, opr0, "splitsin", aCI); Value *sinval = CallInst::Create(sinExpr, opr0, "splitsin", aCI);
Value *cosval = CallInst::Create(cosExpr, opr0, "splitcos", aCI); Value *cosval = CallInst::Create(cosExpr, opr0, "splitcos", aCI);
@ -554,7 +554,7 @@ bool AMDGPULibCalls::useNative(CallInst *aCI) {
return sincosUseNative(aCI, FInfo); return sincosUseNative(aCI, FInfo);
FInfo.setPrefix(AMDGPULibFunc::NATIVE); FInfo.setPrefix(AMDGPULibFunc::NATIVE);
Constant *F = getFunction(aCI->getModule(), FInfo); FunctionCallee F = getFunction(aCI->getModule(), FInfo);
if (!F) if (!F)
return false; return false;
@ -612,7 +612,7 @@ bool AMDGPULibCalls::fold_read_write_pipe(CallInst *CI, IRBuilder<> &B,
auto *FTy = FunctionType::get(Callee->getReturnType(), auto *FTy = FunctionType::get(Callee->getReturnType(),
ArrayRef<Type *>(ArgTys), false); ArrayRef<Type *>(ArgTys), false);
AMDGPULibFunc NewLibFunc(Name, FTy); AMDGPULibFunc NewLibFunc(Name, FTy);
auto *F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc); FunctionCallee F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc);
if (!F) if (!F)
return false; return false;
@ -794,7 +794,7 @@ bool AMDGPULibCalls::replaceWithNative(CallInst *CI, const FuncInfo &FInfo) {
AMDGPULibFunc nf = FInfo; AMDGPULibFunc nf = FInfo;
nf.setPrefix(AMDGPULibFunc::NATIVE); nf.setPrefix(AMDGPULibFunc::NATIVE);
if (Constant *FPExpr = getFunction(M, nf)) { if (FunctionCallee FPExpr = getFunction(M, nf)) {
LLVM_DEBUG(dbgs() << "AMDIC: " << *CI << " ---> "); LLVM_DEBUG(dbgs() << "AMDIC: " << *CI << " ---> ");
CI->setCalledFunction(FPExpr); CI->setCalledFunction(FPExpr);
@ -933,9 +933,10 @@ bool AMDGPULibCalls::fold_pow(CallInst *CI, IRBuilder<> &B,
if (CF && (CF->isExactlyValue(0.5) || CF->isExactlyValue(-0.5))) { if (CF && (CF->isExactlyValue(0.5) || CF->isExactlyValue(-0.5))) {
// pow[r](x, [-]0.5) = sqrt(x) // pow[r](x, [-]0.5) = sqrt(x)
bool issqrt = CF->isExactlyValue(0.5); bool issqrt = CF->isExactlyValue(0.5);
if (Constant *FPExpr = getFunction(M, if (FunctionCallee FPExpr =
AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT getFunction(M, AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
: AMDGPULibFunc::EI_RSQRT, FInfo))) { : AMDGPULibFunc::EI_RSQRT,
FInfo))) {
LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
<< FInfo.getName().c_str() << "(" << *opr0 << ")\n"); << FInfo.getName().c_str() << "(" << *opr0 << ")\n");
Value *nval = CreateCallEx(B,FPExpr, opr0, issqrt ? "__pow2sqrt" Value *nval = CreateCallEx(B,FPExpr, opr0, issqrt ? "__pow2sqrt"
@ -1002,8 +1003,8 @@ bool AMDGPULibCalls::fold_pow(CallInst *CI, IRBuilder<> &B,
// powr ---> exp2(y * log2(x)) // powr ---> exp2(y * log2(x))
// pown/pow ---> powr(fabs(x), y) | (x & ((int)y << 31)) // pown/pow ---> powr(fabs(x), y) | (x & ((int)y << 31))
Constant *ExpExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2, FunctionCallee ExpExpr =
FInfo)); getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2, FInfo));
if (!ExpExpr) if (!ExpExpr)
return false; return false;
@ -1089,8 +1090,8 @@ bool AMDGPULibCalls::fold_pow(CallInst *CI, IRBuilder<> &B,
Value *nval; Value *nval;
if (needabs) { if (needabs) {
Constant *AbsExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS, FunctionCallee AbsExpr =
FInfo)); getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS, FInfo));
if (!AbsExpr) if (!AbsExpr)
return false; return false;
nval = CreateCallEx(B, AbsExpr, opr0, "__fabs"); nval = CreateCallEx(B, AbsExpr, opr0, "__fabs");
@ -1098,8 +1099,8 @@ bool AMDGPULibCalls::fold_pow(CallInst *CI, IRBuilder<> &B,
nval = cnval ? cnval : opr0; nval = cnval ? cnval : opr0;
} }
if (needlog) { if (needlog) {
Constant *LogExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2, FunctionCallee LogExpr =
FInfo)); getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2, FInfo));
if (!LogExpr) if (!LogExpr)
return false; return false;
nval = CreateCallEx(B,LogExpr, nval, "__log2"); nval = CreateCallEx(B,LogExpr, nval, "__log2");
@ -1158,8 +1159,8 @@ bool AMDGPULibCalls::fold_rootn(CallInst *CI, IRBuilder<> &B,
std::vector<const Type*> ParamsTys; std::vector<const Type*> ParamsTys;
ParamsTys.push_back(opr0->getType()); ParamsTys.push_back(opr0->getType());
Module *M = CI->getModule(); Module *M = CI->getModule();
if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, if (FunctionCallee FPExpr =
FInfo))) { getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> sqrt(" << *opr0 << ")\n"); LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> sqrt(" << *opr0 << ")\n");
Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2sqrt"); Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2sqrt");
replaceCall(nval); replaceCall(nval);
@ -1167,8 +1168,8 @@ bool AMDGPULibCalls::fold_rootn(CallInst *CI, IRBuilder<> &B,
} }
} else if (ci_opr1 == 3) { // rootn(x, 3) = cbrt(x) } else if (ci_opr1 == 3) { // rootn(x, 3) = cbrt(x)
Module *M = CI->getModule(); Module *M = CI->getModule();
if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT, if (FunctionCallee FPExpr =
FInfo))) { getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT, FInfo))) {
LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> cbrt(" << *opr0 << ")\n"); LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> cbrt(" << *opr0 << ")\n");
Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2cbrt"); Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2cbrt");
replaceCall(nval); replaceCall(nval);
@ -1185,8 +1186,8 @@ bool AMDGPULibCalls::fold_rootn(CallInst *CI, IRBuilder<> &B,
std::vector<const Type*> ParamsTys; std::vector<const Type*> ParamsTys;
ParamsTys.push_back(opr0->getType()); ParamsTys.push_back(opr0->getType());
Module *M = CI->getModule(); Module *M = CI->getModule();
if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT, if (FunctionCallee FPExpr =
FInfo))) { getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT, FInfo))) {
LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> rsqrt(" << *opr0 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> rsqrt(" << *opr0
<< ")\n"); << ")\n");
Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2rsqrt"); Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2rsqrt");
@ -1242,7 +1243,8 @@ bool AMDGPULibCalls::fold_fma_mad(CallInst *CI, IRBuilder<> &B,
} }
// Get a scalar native builtin signle argument FP function // Get a scalar native builtin signle argument FP function
Constant* AMDGPULibCalls::getNativeFunction(Module* M, const FuncInfo& FInfo) { FunctionCallee AMDGPULibCalls::getNativeFunction(Module *M,
const FuncInfo &FInfo) {
if (getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId())) if (getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()))
return nullptr; return nullptr;
FuncInfo nf = FInfo; FuncInfo nf = FInfo;
@ -1255,8 +1257,8 @@ bool AMDGPULibCalls::fold_sqrt(CallInst *CI, IRBuilder<> &B,
const FuncInfo &FInfo) { const FuncInfo &FInfo) {
if (getArgType(FInfo) == AMDGPULibFunc::F32 && (getVecSize(FInfo) == 1) && if (getArgType(FInfo) == AMDGPULibFunc::F32 && (getVecSize(FInfo) == 1) &&
(FInfo.getPrefix() != AMDGPULibFunc::NATIVE)) { (FInfo.getPrefix() != AMDGPULibFunc::NATIVE)) {
if (Constant *FPExpr = getNativeFunction( if (FunctionCallee FPExpr = getNativeFunction(
CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) { CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
Value *opr0 = CI->getArgOperand(0); Value *opr0 = CI->getArgOperand(0);
LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
<< "sqrt(" << *opr0 << ")\n"); << "sqrt(" << *opr0 << ")\n");
@ -1333,7 +1335,7 @@ bool AMDGPULibCalls::fold_sincos(CallInst *CI, IRBuilder<> &B,
// function. // function.
AMDGPULibFunc nf(AMDGPULibFunc::EI_SINCOS, fInfo); AMDGPULibFunc nf(AMDGPULibFunc::EI_SINCOS, fInfo);
nf.getLeads()[0].PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AMDGPUAS::FLAT_ADDRESS); nf.getLeads()[0].PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AMDGPUAS::FLAT_ADDRESS);
Function *Fsincos = dyn_cast_or_null<Function>(getFunction(M, nf)); FunctionCallee Fsincos = getFunction(M, nf);
if (!Fsincos) return false; if (!Fsincos) return false;
BasicBlock::iterator ItOld = B.GetInsertPoint(); BasicBlock::iterator ItOld = B.GetInsertPoint();
@ -1341,7 +1343,7 @@ bool AMDGPULibCalls::fold_sincos(CallInst *CI, IRBuilder<> &B,
B.SetInsertPoint(UI); B.SetInsertPoint(UI);
Value *P = Alloc; Value *P = Alloc;
Type *PTy = Fsincos->getFunctionType()->getParamType(1); Type *PTy = Fsincos.getFunctionType()->getParamType(1);
// The allocaInst allocates the memory in private address space. This need // The allocaInst allocates the memory in private address space. This need
// to be bitcasted to point to the address space of cos pointer type. // to be bitcasted to point to the address space of cos pointer type.
// In OpenCL 2.0 this is generic, while in 1.2 that is private. // In OpenCL 2.0 this is generic, while in 1.2 that is private.

View File

@ -960,8 +960,8 @@ Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
return nullptr; return nullptr;
} }
Function *AMDGPULibFunc::getOrInsertFunction(Module *M, FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
const AMDGPULibFunc &fInfo) { const AMDGPULibFunc &fInfo) {
std::string const FuncName = fInfo.mangle(); std::string const FuncName = fInfo.mangle();
Function *F = dyn_cast_or_null<Function>( Function *F = dyn_cast_or_null<Function>(
M->getValueSymbolTable().lookup(FuncName)); M->getValueSymbolTable().lookup(FuncName));
@ -987,7 +987,7 @@ Function *AMDGPULibFunc::getOrInsertFunction(Module *M,
} }
} }
Constant *C = nullptr; FunctionCallee C;
if (hasPtr) { if (hasPtr) {
// Do not set extra attributes for functions with pointer arguments. // Do not set extra attributes for functions with pointer arguments.
C = M->getOrInsertFunction(FuncName, FuncTy); C = M->getOrInsertFunction(FuncName, FuncTy);
@ -1001,7 +1001,7 @@ Function *AMDGPULibFunc::getOrInsertFunction(Module *M,
C = M->getOrInsertFunction(FuncName, FuncTy, Attr); C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
} }
return cast<Function>(C); return C;
} }
bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) { bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {

View File

@ -393,8 +393,8 @@ public:
} }
static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo); static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
static Function *getOrInsertFunction(llvm::Module *M, static FunctionCallee getOrInsertFunction(llvm::Module *M,
const AMDGPULibFunc &fInfo); const AMDGPULibFunc &fInfo);
static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr); static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
private: private:

View File

@ -2253,10 +2253,8 @@ CleanupAndExit:
Type *Int32PtrTy = Type::getInt32PtrTy(Ctx); Type *Int32PtrTy = Type::getInt32PtrTy(Ctx);
Type *VoidTy = Type::getVoidTy(Ctx); Type *VoidTy = Type::getVoidTy(Ctx);
Module *M = Func->getParent(); Module *M = Func->getParent();
Constant *CF = M->getOrInsertFunction(HexagonVolatileMemcpyName, VoidTy, FunctionCallee Fn = M->getOrInsertFunction(
Int32PtrTy, Int32PtrTy, Int32Ty); HexagonVolatileMemcpyName, VoidTy, Int32PtrTy, Int32PtrTy, Int32Ty);
Function *Fn = cast<Function>(CF);
Fn->setLinkage(Function::ExternalLinkage);
const SCEV *OneS = SE->getConstant(Int32Ty, 1); const SCEV *OneS = SE->getConstant(Int32Ty, 1);
const SCEV *BECount32 = SE->getTruncateOrZeroExtend(BECount, Int32Ty); const SCEV *BECount32 = SE->getTruncateOrZeroExtend(BECount, Int32Ty);

View File

@ -414,7 +414,7 @@ static bool fixupFPReturnAndCall(Function &F, Module *M,
Attribute::ReadNone); Attribute::ReadNone);
A = A.addAttribute(C, AttributeList::FunctionIndex, A = A.addAttribute(C, AttributeList::FunctionIndex,
Attribute::NoInline); Attribute::NoInline);
Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T)); FunctionCallee F = (M->getOrInsertFunction(Name, A, MyVoid, T));
CallInst::Create(F, Params, "", &I); CallInst::Create(F, Params, "", &I);
} else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
FunctionType *FT = CI->getFunctionType(); FunctionType *FT = CI->getFunctionType();

View File

@ -109,10 +109,11 @@ bool LowerGlobalDtors::runOnModule(Module &M) {
FunctionType::get(Type::getVoidTy(C), AtExitFuncArgs, FunctionType::get(Type::getVoidTy(C), AtExitFuncArgs,
/*isVarArg=*/false); /*isVarArg=*/false);
Type *AtExitArgs[] = {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar}; FunctionCallee AtExit = M.getOrInsertFunction(
FunctionType *AtExitTy = FunctionType::get(Type::getInt32Ty(C), AtExitArgs, "__cxa_atexit",
/*isVarArg=*/false); FunctionType::get(Type::getInt32Ty(C),
Constant *AtExit = M.getOrInsertFunction("__cxa_atexit", AtExitTy); {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar},
/*isVarArg=*/false));
// Declare __dso_local. // Declare __dso_local.
Constant *DsoHandle = M.getNamedValue("__dso_handle"); Constant *DsoHandle = M.getNamedValue("__dso_handle");

View File

@ -2279,12 +2279,13 @@ void X86TargetLowering::insertSSPDeclarations(Module &M) const {
Type::getInt8PtrTy(M.getContext())); Type::getInt8PtrTy(M.getContext()));
// MSVC CRT has a function to validate security cookie. // MSVC CRT has a function to validate security cookie.
auto *SecurityCheckCookie = cast<Function>( FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
M.getOrInsertFunction("__security_check_cookie", "__security_check_cookie", Type::getVoidTy(M.getContext()),
Type::getVoidTy(M.getContext()), Type::getInt8PtrTy(M.getContext()));
Type::getInt8PtrTy(M.getContext()))); if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
SecurityCheckCookie->setCallingConv(CallingConv::X86_FastCall); F->setCallingConv(CallingConv::X86_FastCall);
SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg); F->addAttribute(1, Attribute::AttrKind::InReg);
}
return; return;
} }
// glibc, bionic, and Fuchsia have a special slot for the stack guard. // glibc, bionic, and Fuchsia have a special slot for the stack guard.

View File

@ -86,15 +86,15 @@ private:
StructType *EHLinkRegistrationTy = nullptr; StructType *EHLinkRegistrationTy = nullptr;
StructType *CXXEHRegistrationTy = nullptr; StructType *CXXEHRegistrationTy = nullptr;
StructType *SEHRegistrationTy = nullptr; StructType *SEHRegistrationTy = nullptr;
Constant *SetJmp3 = nullptr; FunctionCallee SetJmp3 = nullptr;
Constant *CxxLongjmpUnwind = nullptr; FunctionCallee CxxLongjmpUnwind = nullptr;
// Per-function state // Per-function state
EHPersonality Personality = EHPersonality::Unknown; EHPersonality Personality = EHPersonality::Unknown;
Function *PersonalityFn = nullptr; Function *PersonalityFn = nullptr;
bool UseStackGuard = false; bool UseStackGuard = false;
int ParentBaseState; int ParentBaseState;
Constant *SehLongjmpUnwind = nullptr; FunctionCallee SehLongjmpUnwind = nullptr;
Constant *Cookie = nullptr; Constant *Cookie = nullptr;
/// The stack allocation containing all EH data, including the link in the /// The stack allocation containing all EH data, including the link in the
@ -303,7 +303,7 @@ void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
CxxLongjmpUnwind = TheModule->getOrInsertFunction( CxxLongjmpUnwind = TheModule->getOrInsertFunction(
"__CxxLongjmpUnwind", "__CxxLongjmpUnwind",
FunctionType::get(VoidTy, Int8PtrType, /*isVarArg=*/false)); FunctionType::get(VoidTy, Int8PtrType, /*isVarArg=*/false));
cast<Function>(CxxLongjmpUnwind->stripPointerCasts()) cast<Function>(CxxLongjmpUnwind.getCallee()->stripPointerCasts())
->setCallingConv(CallingConv::X86_StdCall); ->setCallingConv(CallingConv::X86_StdCall);
} else if (Personality == EHPersonality::MSVC_X86SEH) { } else if (Personality == EHPersonality::MSVC_X86SEH) {
// If _except_handler4 is in use, some additional guard checks and prologue // If _except_handler4 is in use, some additional guard checks and prologue
@ -356,7 +356,7 @@ void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
UseStackGuard ? "_seh_longjmp_unwind4" : "_seh_longjmp_unwind", UseStackGuard ? "_seh_longjmp_unwind4" : "_seh_longjmp_unwind",
FunctionType::get(Type::getVoidTy(TheModule->getContext()), Int8PtrType, FunctionType::get(Type::getVoidTy(TheModule->getContext()), Int8PtrType,
/*isVarArg=*/false)); /*isVarArg=*/false));
cast<Function>(SehLongjmpUnwind->stripPointerCasts()) cast<Function>(SehLongjmpUnwind.getCallee()->stripPointerCasts())
->setCallingConv(CallingConv::X86_StdCall); ->setCallingConv(CallingConv::X86_StdCall);
} else { } else {
llvm_unreachable("unexpected personality function"); llvm_unreachable("unexpected personality function");
@ -471,11 +471,11 @@ void WinEHStatePass::rewriteSetJmpCallSite(IRBuilder<> &Builder, Function &F,
SmallVector<Value *, 3> OptionalArgs; SmallVector<Value *, 3> OptionalArgs;
if (Personality == EHPersonality::MSVC_CXX) { if (Personality == EHPersonality::MSVC_CXX) {
OptionalArgs.push_back(CxxLongjmpUnwind); OptionalArgs.push_back(CxxLongjmpUnwind.getCallee());
OptionalArgs.push_back(State); OptionalArgs.push_back(State);
OptionalArgs.push_back(emitEHLSDA(Builder, &F)); OptionalArgs.push_back(emitEHLSDA(Builder, &F));
} else if (Personality == EHPersonality::MSVC_X86SEH) { } else if (Personality == EHPersonality::MSVC_X86SEH) {
OptionalArgs.push_back(SehLongjmpUnwind); OptionalArgs.push_back(SehLongjmpUnwind.getCallee());
OptionalArgs.push_back(State); OptionalArgs.push_back(State);
if (UseStackGuard) if (UseStackGuard)
OptionalArgs.push_back(Cookie); OptionalArgs.push_back(Cookie);
@ -766,7 +766,7 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
if (!CS) if (!CS)
continue; continue;
if (CS.getCalledValue()->stripPointerCasts() != if (CS.getCalledValue()->stripPointerCasts() !=
SetJmp3->stripPointerCasts()) SetJmp3.getCallee()->stripPointerCasts())
continue; continue;
SetJmp3CallSites.push_back(CS); SetJmp3CallSites.push_back(CS);

View File

@ -105,10 +105,10 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
} }
LLVMContext &Ctx = M.getContext(); LLVMContext &Ctx = M.getContext();
Constant *C = M.getOrInsertFunction( FunctionCallee C = M.getOrInsertFunction(
"__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx), "__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx)); Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
Function *F = dyn_cast<Function>(C); Function *F = dyn_cast<Function>(C.getCallee());
// Take over the existing function. The frontend emits a weak stub so that the // Take over the existing function. The frontend emits a weak stub so that the
// linker knows about the symbol; this pass replaces the function body. // linker knows about the symbol; this pass replaces the function body.
F->deleteBody(); F->deleteBody();
@ -132,9 +132,9 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
BasicBlock *TrapBB = BasicBlock::Create(Ctx, "fail", F); BasicBlock *TrapBB = BasicBlock::Create(Ctx, "fail", F);
IRBuilder<> IRBFail(TrapBB); IRBuilder<> IRBFail(TrapBB);
Constant *CFICheckFailFn = M.getOrInsertFunction( FunctionCallee CFICheckFailFn =
"__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), M.getOrInsertFunction("__cfi_check_fail", Type::getVoidTy(Ctx),
Type::getInt8PtrTy(Ctx)); Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr}); IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr});
IRBFail.CreateBr(ExitBB); IRBFail.CreateBr(ExitBB);

View File

@ -1494,8 +1494,10 @@ void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) {
if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) { if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) {
// The type of the function in the declaration is irrelevant because every // The type of the function in the declaration is irrelevant because every
// call site will cast it to the correct type. // call site will cast it to the correct type.
auto *SingleImpl = M.getOrInsertFunction( Constant *SingleImpl =
Res.SingleImplName, Type::getVoidTy(M.getContext())); cast<Constant>(M.getOrInsertFunction(Res.SingleImplName,
Type::getVoidTy(M.getContext()))
.getCallee());
// This is the import phase so we should not be exporting anything. // This is the import phase so we should not be exporting anything.
bool IsExported = false; bool IsExported = false;
@ -1537,8 +1539,12 @@ void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) {
} }
if (Res.TheKind == WholeProgramDevirtResolution::BranchFunnel) { if (Res.TheKind == WholeProgramDevirtResolution::BranchFunnel) {
auto *JT = M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"), // The type of the function is irrelevant, because it's bitcast at calls
Type::getVoidTy(M.getContext())); // anyhow.
Constant *JT = cast<Constant>(
M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
Type::getVoidTy(M.getContext()))
.getCallee());
bool IsExported = false; bool IsExported = false;
applyICallBranchFunnel(SlotInfo, JT, IsExported); applyICallBranchFunnel(SlotInfo, JT, IsExported);
assert(!IsExported); assert(!IsExported);

View File

@ -715,19 +715,19 @@ private:
Type *IntptrTy; Type *IntptrTy;
ShadowMapping Mapping; ShadowMapping Mapping;
DominatorTree *DT; DominatorTree *DT;
Function *AsanHandleNoReturnFunc; FunctionCallee AsanHandleNoReturnFunc;
Function *AsanPtrCmpFunction, *AsanPtrSubFunction; FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
Constant *AsanShadowGlobal; Constant *AsanShadowGlobal;
// These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize). // These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize).
Function *AsanErrorCallback[2][2][kNumberOfAccessSizes]; FunctionCallee AsanErrorCallback[2][2][kNumberOfAccessSizes];
Function *AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes]; FunctionCallee AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
// These arrays is indexed by AccessIsWrite and Experiment. // These arrays is indexed by AccessIsWrite and Experiment.
Function *AsanErrorCallbackSized[2][2]; FunctionCallee AsanErrorCallbackSized[2][2];
Function *AsanMemoryAccessCallbackSized[2][2]; FunctionCallee AsanMemoryAccessCallbackSized[2][2];
Function *AsanMemmove, *AsanMemcpy, *AsanMemset; FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
InlineAsm *EmptyAsm; InlineAsm *EmptyAsm;
Value *LocalDynamicShadow = nullptr; Value *LocalDynamicShadow = nullptr;
GlobalsMetadata GlobalsMD; GlobalsMetadata GlobalsMD;
@ -809,14 +809,14 @@ private:
LLVMContext *C; LLVMContext *C;
Triple TargetTriple; Triple TargetTriple;
ShadowMapping Mapping; ShadowMapping Mapping;
Function *AsanPoisonGlobals; FunctionCallee AsanPoisonGlobals;
Function *AsanUnpoisonGlobals; FunctionCallee AsanUnpoisonGlobals;
Function *AsanRegisterGlobals; FunctionCallee AsanRegisterGlobals;
Function *AsanUnregisterGlobals; FunctionCallee AsanUnregisterGlobals;
Function *AsanRegisterImageGlobals; FunctionCallee AsanRegisterImageGlobals;
Function *AsanUnregisterImageGlobals; FunctionCallee AsanUnregisterImageGlobals;
Function *AsanRegisterElfGlobals; FunctionCallee AsanRegisterElfGlobals;
Function *AsanUnregisterElfGlobals; FunctionCallee AsanUnregisterElfGlobals;
Function *AsanCtorFunction = nullptr; Function *AsanCtorFunction = nullptr;
Function *AsanDtorFunction = nullptr; Function *AsanDtorFunction = nullptr;
@ -845,11 +845,11 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
SmallVector<Instruction *, 8> RetVec; SmallVector<Instruction *, 8> RetVec;
unsigned StackAlignment; unsigned StackAlignment;
Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1], FunctionCallee AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
*AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1]; AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
Function *AsanSetShadowFunc[0x100] = {}; FunctionCallee AsanSetShadowFunc[0x100] = {};
Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc; FunctionCallee AsanPoisonStackMemoryFunc, AsanUnpoisonStackMemoryFunc;
Function *AsanAllocaPoisonFunc, *AsanAllocasUnpoisonFunc; FunctionCallee AsanAllocaPoisonFunc, AsanAllocasUnpoisonFunc;
// Stores a place and arguments of poisoning/unpoisoning call for alloca. // Stores a place and arguments of poisoning/unpoisoning call for alloca.
struct AllocaPoisonCall { struct AllocaPoisonCall {
@ -1333,7 +1333,7 @@ bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
void AddressSanitizer::instrumentPointerComparisonOrSubtraction( void AddressSanitizer::instrumentPointerComparisonOrSubtraction(
Instruction *I) { Instruction *I) {
IRBuilder<> IRB(I); IRBuilder<> IRB(I);
Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction; FunctionCallee F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
Value *Param[2] = {I->getOperand(0), I->getOperand(1)}; Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
for (Value *&i : Param) { for (Value *&i : Param) {
if (i->getType()->isPointerTy()) if (i->getType()->isPointerTy())
@ -1795,43 +1795,30 @@ void AddressSanitizerModule::initializeCallbacks(Module &M) {
IRBuilder<> IRB(*C); IRBuilder<> IRB(*C);
// Declare our poisoning and unpoisoning functions. // Declare our poisoning and unpoisoning functions.
AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanPoisonGlobals =
kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy)); M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy);
AsanPoisonGlobals->setLinkage(Function::ExternalLinkage); AsanUnpoisonGlobals =
AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy());
kAsanUnpoisonGlobalsName, IRB.getVoidTy()));
AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
// Declare functions that register/unregister globals. // Declare functions that register/unregister globals.
AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanRegisterGlobals = M.getOrInsertFunction(
kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy)); kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
AsanRegisterGlobals->setLinkage(Function::ExternalLinkage); AsanUnregisterGlobals = M.getOrInsertFunction(
AsanUnregisterGlobals = checkSanitizerInterfaceFunction( kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
IntptrTy, IntptrTy));
AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
// Declare the functions that find globals in a shared object and then invoke // Declare the functions that find globals in a shared object and then invoke
// the (un)register function on them. // the (un)register function on them.
AsanRegisterImageGlobals = AsanRegisterImageGlobals = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy)); AsanUnregisterImageGlobals = M.getOrInsertFunction(
AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage); kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
AsanUnregisterImageGlobals = AsanRegisterElfGlobals =
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
AsanRegisterElfGlobals = checkSanitizerInterfaceFunction(
M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(), M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(),
IntptrTy, IntptrTy, IntptrTy)); IntptrTy, IntptrTy, IntptrTy);
AsanRegisterElfGlobals->setLinkage(Function::ExternalLinkage); AsanUnregisterElfGlobals =
AsanUnregisterElfGlobals = checkSanitizerInterfaceFunction(
M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(), M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(),
IntptrTy, IntptrTy, IntptrTy)); IntptrTy, IntptrTy, IntptrTy);
AsanUnregisterElfGlobals->setLinkage(Function::ExternalLinkage);
} }
// Put the metadata and the instrumented global in the same group. This ensures // Put the metadata and the instrumented global in the same group. This ensures
@ -2345,51 +2332,49 @@ void AddressSanitizer::initializeCallbacks(Module &M) {
Args2.push_back(ExpType); Args2.push_back(ExpType);
Args1.push_back(ExpType); Args1.push_back(ExpType);
} }
AsanErrorCallbackSized[AccessIsWrite][Exp] = AsanErrorCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr,
kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr, FunctionType::get(IRB.getVoidTy(), Args2, false));
FunctionType::get(IRB.getVoidTy(), Args2, false)));
AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] = AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr, FunctionType::get(IRB.getVoidTy(), Args2, false));
FunctionType::get(IRB.getVoidTy(), Args2, false)));
for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
AccessSizeIndex++) { AccessSizeIndex++) {
const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex); const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] = AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(
kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr, kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
FunctionType::get(IRB.getVoidTy(), Args1, false))); FunctionType::get(IRB.getVoidTy(), Args1, false));
AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] = AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(
ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr, ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
FunctionType::get(IRB.getVoidTy(), Args1, false))); FunctionType::get(IRB.getVoidTy(), Args1, false));
} }
} }
} }
const std::string MemIntrinCallbackPrefix = const std::string MemIntrinCallbackPrefix =
CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix; CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IntptrTy);
AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IntptrTy);
AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy)); IRB.getInt32Ty(), IntptrTy);
AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction( AsanHandleNoReturnFunc =
M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy())); M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy());
AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanPtrCmpFunction =
kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy)); M.getOrInsertFunction(kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy);
AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanPtrSubFunction =
kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy)); M.getOrInsertFunction(kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy);
// We insert an empty inline asm after __asan_report* to avoid callback merge. // We insert an empty inline asm after __asan_report* to avoid callback merge.
EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
StringRef(""), StringRef(""), StringRef(""), StringRef(""),
@ -2427,7 +2412,7 @@ bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
// We cannot just ignore these methods, because they may call other // We cannot just ignore these methods, because they may call other
// instrumented functions. // instrumented functions.
if (F.getName().find(" load]") != std::string::npos) { if (F.getName().find(" load]") != std::string::npos) {
Function *AsanInitFunction = FunctionCallee AsanInitFunction =
declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {}); declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {});
IRBuilder<> IRB(&F.front(), F.front().begin()); IRBuilder<> IRB(&F.front(), F.front().begin());
IRB.CreateCall(AsanInitFunction, {}); IRB.CreateCall(AsanInitFunction, {});
@ -2642,20 +2627,17 @@ void FunctionStackPoisoner::initializeCallbacks(Module &M) {
IRBuilder<> IRB(*C); IRBuilder<> IRB(*C);
for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) { for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
std::string Suffix = itostr(i); std::string Suffix = itostr(i);
AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction( AsanStackMallocFunc[i] = M.getOrInsertFunction(
M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy, kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy);
IntptrTy)); AsanStackFreeFunc[i] =
AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix, M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
IRB.getVoidTy(), IntptrTy, IntptrTy)); IRB.getVoidTy(), IntptrTy, IntptrTy);
} }
if (ASan.UseAfterScope) { if (ASan.UseAfterScope) {
AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction( AsanPoisonStackMemoryFunc = M.getOrInsertFunction(
M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(), kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
IntptrTy, IntptrTy)); AsanUnpoisonStackMemoryFunc = M.getOrInsertFunction(
AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction( kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
IntptrTy, IntptrTy));
} }
for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) { for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
@ -2663,15 +2645,13 @@ void FunctionStackPoisoner::initializeCallbacks(Module &M) {
Name << kAsanSetShadowPrefix; Name << kAsanSetShadowPrefix;
Name << std::setw(2) << std::setfill('0') << std::hex << Val; Name << std::setw(2) << std::setfill('0') << std::hex << Val;
AsanSetShadowFunc[Val] = AsanSetShadowFunc[Val] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy);
Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy));
} }
AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction( AsanAllocaPoisonFunc = M.getOrInsertFunction(
kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy)); kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
AsanAllocasUnpoisonFunc = AsanAllocasUnpoisonFunc = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
} }
void FunctionStackPoisoner::copyToShadowInline(ArrayRef<uint8_t> ShadowMask, void FunctionStackPoisoner::copyToShadowInline(ArrayRef<uint8_t> ShadowMask,

View File

@ -341,13 +341,13 @@ class DataFlowSanitizer : public ModulePass {
FunctionType *DFSanSetLabelFnTy; FunctionType *DFSanSetLabelFnTy;
FunctionType *DFSanNonzeroLabelFnTy; FunctionType *DFSanNonzeroLabelFnTy;
FunctionType *DFSanVarargWrapperFnTy; FunctionType *DFSanVarargWrapperFnTy;
Constant *DFSanUnionFn; FunctionCallee DFSanUnionFn;
Constant *DFSanCheckedUnionFn; FunctionCallee DFSanCheckedUnionFn;
Constant *DFSanUnionLoadFn; FunctionCallee DFSanUnionLoadFn;
Constant *DFSanUnimplementedFn; FunctionCallee DFSanUnimplementedFn;
Constant *DFSanSetLabelFn; FunctionCallee DFSanSetLabelFn;
Constant *DFSanNonzeroLabelFn; FunctionCallee DFSanNonzeroLabelFn;
Constant *DFSanVarargWrapperFn; FunctionCallee DFSanVarargWrapperFn;
MDNode *ColdCallWeights; MDNode *ColdCallWeights;
DFSanABIList ABIList; DFSanABIList ABIList;
DenseMap<Value *, Function *> UnwrappedFnMap; DenseMap<Value *, Function *> UnwrappedFnMap;
@ -677,8 +677,8 @@ DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT, Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
StringRef FName) { StringRef FName) {
FunctionType *FTT = getTrampolineFunctionType(FT); FunctionType *FTT = getTrampolineFunctionType(FT);
Constant *C = Mod->getOrInsertFunction(FName, FTT); FunctionCallee C = Mod->getOrInsertFunction(FName, FTT);
Function *F = dyn_cast<Function>(C); Function *F = dyn_cast<Function>(C.getCallee());
if (F && F->isDeclaration()) { if (F && F->isDeclaration()) {
F->setLinkage(GlobalValue::LinkOnceODRLinkage); F->setLinkage(GlobalValue::LinkOnceODRLinkage);
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F); BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
@ -703,7 +703,7 @@ Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
&*std::prev(F->arg_end()), RI); &*std::prev(F->arg_end()), RI);
} }
return C; return cast<Constant>(C.getCallee());
} }
bool DataFlowSanitizer::runOnModule(Module &M) { bool DataFlowSanitizer::runOnModule(Module &M) {
@ -725,35 +725,51 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
ExternalShadowMask = ExternalShadowMask =
Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy); Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy);
DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy); {
if (Function *F = dyn_cast<Function>(DFSanUnionFn)) { AttributeList AL;
F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); Attribute::NoUnwind);
F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
F->addParamAttr(0, Attribute::ZExt); Attribute::ReadNone);
F->addParamAttr(1, Attribute::ZExt); AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex,
Attribute::ZExt);
AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
DFSanUnionFn =
Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy, AL);
} }
DFSanCheckedUnionFn = Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy);
if (Function *F = dyn_cast<Function>(DFSanCheckedUnionFn)) { {
F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); AttributeList AL;
F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); Attribute::NoUnwind);
F->addParamAttr(0, Attribute::ZExt); AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
F->addParamAttr(1, Attribute::ZExt); Attribute::ReadNone);
AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex,
Attribute::ZExt);
AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
DFSanCheckedUnionFn =
Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy, AL);
} }
DFSanUnionLoadFn = {
Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy); AttributeList AL;
if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) { AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); Attribute::NoUnwind);
F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly); AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); Attribute::ReadOnly);
AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex,
Attribute::ZExt);
DFSanUnionLoadFn =
Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy, AL);
} }
DFSanUnimplementedFn = DFSanUnimplementedFn =
Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy); Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
DFSanSetLabelFn = {
Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy); AttributeList AL;
if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) { AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
F->addParamAttr(0, Attribute::ZExt); DFSanSetLabelFn =
Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy, AL);
} }
DFSanNonzeroLabelFn = DFSanNonzeroLabelFn =
Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy); Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
@ -764,13 +780,13 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
SmallPtrSet<Function *, 2> FnsWithNativeABI; SmallPtrSet<Function *, 2> FnsWithNativeABI;
for (Function &i : M) { for (Function &i : M) {
if (!i.isIntrinsic() && if (!i.isIntrinsic() &&
&i != DFSanUnionFn && &i != DFSanUnionFn.getCallee()->stripPointerCasts() &&
&i != DFSanCheckedUnionFn && &i != DFSanCheckedUnionFn.getCallee()->stripPointerCasts() &&
&i != DFSanUnionLoadFn && &i != DFSanUnionLoadFn.getCallee()->stripPointerCasts() &&
&i != DFSanUnimplementedFn && &i != DFSanUnimplementedFn.getCallee()->stripPointerCasts() &&
&i != DFSanSetLabelFn && &i != DFSanSetLabelFn.getCallee()->stripPointerCasts() &&
&i != DFSanNonzeroLabelFn && &i != DFSanNonzeroLabelFn.getCallee()->stripPointerCasts() &&
&i != DFSanVarargWrapperFn) &i != DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
FnsToInstrument.push_back(&i); FnsToInstrument.push_back(&i);
} }
@ -1512,7 +1528,7 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
// Calls to this function are synthesized in wrappers, and we shouldn't // Calls to this function are synthesized in wrappers, and we shouldn't
// instrument them. // instrument them.
if (F == DFSF.DFS.DFSanVarargWrapperFn) if (F == DFSF.DFS.DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
return; return;
IRBuilder<> IRB(CS.getInstruction()); IRBuilder<> IRB(CS.getInstruction());
@ -1545,9 +1561,9 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT); TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT);
std::string CustomFName = "__dfsw_"; std::string CustomFName = "__dfsw_";
CustomFName += F->getName(); CustomFName += F->getName();
Constant *CustomF = DFSF.DFS.Mod->getOrInsertFunction( FunctionCallee CustomF = DFSF.DFS.Mod->getOrInsertFunction(
CustomFName, CustomFn.TransformedType); CustomFName, CustomFn.TransformedType);
if (Function *CustomFn = dyn_cast<Function>(CustomF)) { if (Function *CustomFn = dyn_cast<Function>(CustomF.getCallee())) {
CustomFn->copyAttributesFrom(F); CustomFn->copyAttributesFrom(F);
// Custom functions returning non-void will write to the return label. // Custom functions returning non-void will write to the return label.

View File

@ -202,13 +202,13 @@ private:
// Our slowpath involves callouts to the runtime library. // Our slowpath involves callouts to the runtime library.
// Access sizes are powers of two: 1, 2, 4, 8, 16. // Access sizes are powers of two: 1, 2, 4, 8, 16.
static const size_t NumberOfAccessSizes = 5; static const size_t NumberOfAccessSizes = 5;
Function *EsanAlignedLoad[NumberOfAccessSizes]; FunctionCallee EsanAlignedLoad[NumberOfAccessSizes];
Function *EsanAlignedStore[NumberOfAccessSizes]; FunctionCallee EsanAlignedStore[NumberOfAccessSizes];
Function *EsanUnalignedLoad[NumberOfAccessSizes]; FunctionCallee EsanUnalignedLoad[NumberOfAccessSizes];
Function *EsanUnalignedStore[NumberOfAccessSizes]; FunctionCallee EsanUnalignedStore[NumberOfAccessSizes];
// For irregular sizes of any alignment: // For irregular sizes of any alignment:
Function *EsanUnalignedLoadN, *EsanUnalignedStoreN; FunctionCallee EsanUnalignedLoadN, EsanUnalignedStoreN;
Function *MemmoveFn, *MemcpyFn, *MemsetFn; FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
Function *EsanCtorFunction; Function *EsanCtorFunction;
Function *EsanDtorFunction; Function *EsanDtorFunction;
// Remember the counter variable for each struct type to avoid // Remember the counter variable for each struct type to avoid
@ -249,37 +249,31 @@ void EfficiencySanitizer::initializeCallbacks(Module &M) {
// We'll inline the most common (i.e., aligned and frequent sizes) // We'll inline the most common (i.e., aligned and frequent sizes)
// load + store instrumentation: these callouts are for the slowpath. // load + store instrumentation: these callouts are for the slowpath.
SmallString<32> AlignedLoadName("__esan_aligned_load" + ByteSizeStr); SmallString<32> AlignedLoadName("__esan_aligned_load" + ByteSizeStr);
EsanAlignedLoad[Idx] = EsanAlignedLoad[Idx] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy());
AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr); SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr);
EsanAlignedStore[Idx] = EsanAlignedStore[Idx] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy());
AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr); SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr);
EsanUnalignedLoad[Idx] = EsanUnalignedLoad[Idx] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy());
UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr); SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr);
EsanUnalignedStore[Idx] = EsanUnalignedStore[Idx] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy());
UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
} }
EsanUnalignedLoadN = checkSanitizerInterfaceFunction( EsanUnalignedLoadN = M.getOrInsertFunction(
M.getOrInsertFunction("__esan_unaligned_loadN", IRB.getVoidTy(), "__esan_unaligned_loadN", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy);
IRB.getInt8PtrTy(), IntptrTy)); EsanUnalignedStoreN = M.getOrInsertFunction(
EsanUnalignedStoreN = checkSanitizerInterfaceFunction( "__esan_unaligned_storeN", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy);
M.getOrInsertFunction("__esan_unaligned_storeN", IRB.getVoidTy(), MemmoveFn =
IRB.getInt8PtrTy(), IntptrTy));
MemmoveFn = checkSanitizerInterfaceFunction(
M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IntptrTy);
MemcpyFn = checkSanitizerInterfaceFunction( MemcpyFn =
M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IntptrTy);
MemsetFn = checkSanitizerInterfaceFunction( MemsetFn =
M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt32Ty(), IntptrTy)); IRB.getInt32Ty(), IntptrTy);
} }
bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) { bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
@ -510,10 +504,8 @@ void EfficiencySanitizer::createDestructor(Module &M, Constant *ToolInfoArg) {
EsanModuleDtorName, &M); EsanModuleDtorName, &M);
ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction)); ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator()); IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
Function *EsanExit = checkSanitizerInterfaceFunction( FunctionCallee EsanExit =
M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(), M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(), Int8PtrTy);
Int8PtrTy));
EsanExit->setLinkage(Function::ExternalLinkage);
IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg}); IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority); appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
} }
@ -669,7 +661,7 @@ bool EfficiencySanitizer::instrumentLoadOrStore(Instruction *I,
Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType(); Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType();
const uint32_t TypeSizeBytes = DL.getTypeStoreSizeInBits(OrigTy) / 8; const uint32_t TypeSizeBytes = DL.getTypeStoreSizeInBits(OrigTy) / 8;
Value *OnAccessFunc = nullptr; FunctionCallee OnAccessFunc = nullptr;
// Convert 0 to the default alignment. // Convert 0 to the default alignment.
if (Alignment == 0) if (Alignment == 0)

View File

@ -102,11 +102,11 @@ private:
std::vector<Regex> &Regexes); std::vector<Regex> &Regexes);
// Get pointers to the functions in the runtime library. // Get pointers to the functions in the runtime library.
Constant *getStartFileFunc(); FunctionCallee getStartFileFunc();
Constant *getEmitFunctionFunc(); FunctionCallee getEmitFunctionFunc();
Constant *getEmitArcsFunc(); FunctionCallee getEmitArcsFunc();
Constant *getSummaryInfoFunc(); FunctionCallee getSummaryInfoFunc();
Constant *getEndFileFunc(); FunctionCallee getEndFileFunc();
// Add the function to write out all our counters to the global destructor // Add the function to write out all our counters to the global destructor
// list. // list.
@ -647,7 +647,7 @@ void GCOVProfiler::AddFlushBeforeForkAndExec() {
for (auto I : ForkAndExecs) { for (auto I : ForkAndExecs) {
IRBuilder<> Builder(I); IRBuilder<> Builder(I);
FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false); FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false);
Constant *GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy); FunctionCallee GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy);
Builder.CreateCall(GCOVFlush); Builder.CreateCall(GCOVFlush);
I->getParent()->splitBasicBlock(I); I->getParent()->splitBasicBlock(I);
} }
@ -863,7 +863,7 @@ bool GCOVProfiler::emitProfileArcs() {
// Initialize the environment and register the local writeout and flush // Initialize the environment and register the local writeout and flush
// functions. // functions.
Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy); FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
Builder.CreateCall(GCOVInit, {WriteoutF, FlushF}); Builder.CreateCall(GCOVInit, {WriteoutF, FlushF});
Builder.CreateRetVoid(); Builder.CreateRetVoid();
@ -873,22 +873,21 @@ bool GCOVProfiler::emitProfileArcs() {
return Result; return Result;
} }
Constant *GCOVProfiler::getStartFileFunc() { FunctionCallee GCOVProfiler::getStartFileFunc() {
Type *Args[] = { Type *Args[] = {
Type::getInt8PtrTy(*Ctx), // const char *orig_filename Type::getInt8PtrTy(*Ctx), // const char *orig_filename
Type::getInt8PtrTy(*Ctx), // const char version[4] Type::getInt8PtrTy(*Ctx), // const char version[4]
Type::getInt32Ty(*Ctx), // uint32_t checksum Type::getInt32Ty(*Ctx), // uint32_t checksum
}; };
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
auto *Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy); AttributeList AL;
if (Function *FunRes = dyn_cast<Function>(Res)) if (auto AK = TLI->getExtAttrForI32Param(false))
if (auto AK = TLI->getExtAttrForI32Param(false)) AL = AL.addParamAttribute(*Ctx, 2, AK);
FunRes->addParamAttr(2, AK); FunctionCallee Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy, AL);
return Res; return Res;
} }
Constant *GCOVProfiler::getEmitFunctionFunc() { FunctionCallee GCOVProfiler::getEmitFunctionFunc() {
Type *Args[] = { Type *Args[] = {
Type::getInt32Ty(*Ctx), // uint32_t ident Type::getInt32Ty(*Ctx), // uint32_t ident
Type::getInt8PtrTy(*Ctx), // const char *function_name Type::getInt8PtrTy(*Ctx), // const char *function_name
@ -897,36 +896,34 @@ Constant *GCOVProfiler::getEmitFunctionFunc() {
Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
}; };
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
auto *Res = M->getOrInsertFunction("llvm_gcda_emit_function", FTy); AttributeList AL;
if (Function *FunRes = dyn_cast<Function>(Res)) if (auto AK = TLI->getExtAttrForI32Param(false)) {
if (auto AK = TLI->getExtAttrForI32Param(false)) { AL = AL.addParamAttribute(*Ctx, 0, AK);
FunRes->addParamAttr(0, AK); AL = AL.addParamAttribute(*Ctx, 2, AK);
FunRes->addParamAttr(2, AK); AL = AL.addParamAttribute(*Ctx, 3, AK);
FunRes->addParamAttr(3, AK); AL = AL.addParamAttribute(*Ctx, 4, AK);
FunRes->addParamAttr(4, AK); }
} return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
return Res;
} }
Constant *GCOVProfiler::getEmitArcsFunc() { FunctionCallee GCOVProfiler::getEmitArcsFunc() {
Type *Args[] = { Type *Args[] = {
Type::getInt32Ty(*Ctx), // uint32_t num_counters Type::getInt32Ty(*Ctx), // uint32_t num_counters
Type::getInt64PtrTy(*Ctx), // uint64_t *counters Type::getInt64PtrTy(*Ctx), // uint64_t *counters
}; };
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
auto *Res = M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy); AttributeList AL;
if (Function *FunRes = dyn_cast<Function>(Res)) if (auto AK = TLI->getExtAttrForI32Param(false))
if (auto AK = TLI->getExtAttrForI32Param(false)) AL = AL.addParamAttribute(*Ctx, 0, AK);
FunRes->addParamAttr(0, AK); return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy, AL);
return Res;
} }
Constant *GCOVProfiler::getSummaryInfoFunc() { FunctionCallee GCOVProfiler::getSummaryInfoFunc() {
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
return M->getOrInsertFunction("llvm_gcda_summary_info", FTy); return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
} }
Constant *GCOVProfiler::getEndFileFunc() { FunctionCallee GCOVProfiler::getEndFileFunc() {
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
return M->getOrInsertFunction("llvm_gcda_end_file", FTy); return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
} }
@ -946,11 +943,11 @@ Function *GCOVProfiler::insertCounterWriteout(
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF); BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
IRBuilder<> Builder(BB); IRBuilder<> Builder(BB);
Constant *StartFile = getStartFileFunc(); FunctionCallee StartFile = getStartFileFunc();
Constant *EmitFunction = getEmitFunctionFunc(); FunctionCallee EmitFunction = getEmitFunctionFunc();
Constant *EmitArcs = getEmitArcsFunc(); FunctionCallee EmitArcs = getEmitArcsFunc();
Constant *SummaryInfo = getSummaryInfoFunc(); FunctionCallee SummaryInfo = getSummaryInfoFunc();
Constant *EndFile = getEndFileFunc(); FunctionCallee EndFile = getEndFileFunc();
NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu"); NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu");
if (!CUNodes) { if (!CUNodes) {

View File

@ -221,7 +221,7 @@ private:
LLVMContext *C; LLVMContext *C;
std::string CurModuleUniqueId; std::string CurModuleUniqueId;
Triple TargetTriple; Triple TargetTriple;
Function *HWAsanMemmove, *HWAsanMemcpy, *HWAsanMemset; FunctionCallee HWAsanMemmove, HWAsanMemcpy, HWAsanMemset;
// Frame description is a way to pass names/sizes of local variables // Frame description is a way to pass names/sizes of local variables
// to the run-time w/o adding extra executable code in every function. // to the run-time w/o adding extra executable code in every function.
@ -270,12 +270,12 @@ private:
Function *HwasanCtorFunction; Function *HwasanCtorFunction;
Function *HwasanMemoryAccessCallback[2][kNumberOfAccessSizes]; FunctionCallee HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
Function *HwasanMemoryAccessCallbackSized[2]; FunctionCallee HwasanMemoryAccessCallbackSized[2];
Function *HwasanTagMemoryFunc; FunctionCallee HwasanTagMemoryFunc;
Function *HwasanGenerateTagFunc; FunctionCallee HwasanGenerateTagFunc;
Function *HwasanThreadEnterFunc; FunctionCallee HwasanThreadEnterFunc;
Constant *ShadowGlobal; Constant *ShadowGlobal;
@ -369,43 +369,42 @@ void HWAddressSanitizer::initializeCallbacks(Module &M) {
const std::string TypeStr = AccessIsWrite ? "store" : "load"; const std::string TypeStr = AccessIsWrite ? "store" : "load";
const std::string EndingStr = Recover ? "_noabort" : ""; const std::string EndingStr = Recover ? "_noabort" : "";
HwasanMemoryAccessCallbackSized[AccessIsWrite] = HwasanMemoryAccessCallbackSized[AccessIsWrite] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr, FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false));
FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false)));
for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
AccessSizeIndex++) { AccessSizeIndex++) {
HwasanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] = HwasanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(
ClMemoryAccessCallbackPrefix + TypeStr + ClMemoryAccessCallbackPrefix + TypeStr +
itostr(1ULL << AccessSizeIndex) + EndingStr, itostr(1ULL << AccessSizeIndex) + EndingStr,
FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false))); FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false));
} }
} }
HwasanTagMemoryFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction( HwasanTagMemoryFunc = M.getOrInsertFunction(
"__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy)); "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy);
HwasanGenerateTagFunc = checkSanitizerInterfaceFunction( HwasanGenerateTagFunc =
M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty)); M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty);
ShadowGlobal = M.getOrInsertGlobal("__hwasan_shadow", ShadowGlobal = M.getOrInsertGlobal("__hwasan_shadow",
ArrayType::get(IRB.getInt8Ty(), 0)); ArrayType::get(IRB.getInt8Ty(), 0));
const std::string MemIntrinCallbackPrefix = const std::string MemIntrinCallbackPrefix =
CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix; CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
HWAsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction( HWAsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IntptrTy);
HWAsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction( HWAsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IntptrTy);
HWAsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction( HWAsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy)); IRB.getInt32Ty(), IntptrTy);
HwasanThreadEnterFunc = checkSanitizerInterfaceFunction( HwasanThreadEnterFunc =
M.getOrInsertFunction("__hwasan_thread_enter", IRB.getVoidTy())); M.getOrInsertFunction("__hwasan_thread_enter", IRB.getVoidTy());
} }
Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) { Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {

View File

@ -508,13 +508,16 @@ bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) {
return true; return true;
} }
static Constant *getOrInsertValueProfilingCall(Module &M, static FunctionCallee
const TargetLibraryInfo &TLI, getOrInsertValueProfilingCall(Module &M, const TargetLibraryInfo &TLI,
bool IsRange = false) { bool IsRange = false) {
LLVMContext &Ctx = M.getContext(); LLVMContext &Ctx = M.getContext();
auto *ReturnTy = Type::getVoidTy(M.getContext()); auto *ReturnTy = Type::getVoidTy(M.getContext());
Constant *Res; AttributeList AL;
if (auto AK = TLI.getExtAttrForI32Param(false))
AL = AL.addParamAttribute(M.getContext(), 2, AK);
if (!IsRange) { if (!IsRange) {
Type *ParamTypes[] = { Type *ParamTypes[] = {
#define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
@ -522,8 +525,8 @@ static Constant *getOrInsertValueProfilingCall(Module &M,
}; };
auto *ValueProfilingCallTy = auto *ValueProfilingCallTy =
FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(), return M.getOrInsertFunction(getInstrProfValueProfFuncName(),
ValueProfilingCallTy); ValueProfilingCallTy, AL);
} else { } else {
Type *RangeParamTypes[] = { Type *RangeParamTypes[] = {
#define VALUE_RANGE_PROF 1 #define VALUE_RANGE_PROF 1
@ -533,15 +536,9 @@ static Constant *getOrInsertValueProfilingCall(Module &M,
}; };
auto *ValueRangeProfilingCallTy = auto *ValueRangeProfilingCallTy =
FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false); FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false);
Res = M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(), return M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(),
ValueRangeProfilingCallTy); ValueRangeProfilingCallTy, AL);
} }
if (Function *FunRes = dyn_cast<Function>(Res)) {
if (auto AK = TLI.getExtAttrForI32Param(false))
FunRes->addParamAttr(2, AK);
}
return Res;
} }
void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {

View File

@ -536,41 +536,41 @@ private:
bool CallbacksInitialized = false; bool CallbacksInitialized = false;
/// The run-time callback to print a warning. /// The run-time callback to print a warning.
Value *WarningFn; FunctionCallee WarningFn;
// These arrays are indexed by log2(AccessSize). // These arrays are indexed by log2(AccessSize).
Value *MaybeWarningFn[kNumberOfAccessSizes]; FunctionCallee MaybeWarningFn[kNumberOfAccessSizes];
Value *MaybeStoreOriginFn[kNumberOfAccessSizes]; FunctionCallee MaybeStoreOriginFn[kNumberOfAccessSizes];
/// Run-time helper that generates a new origin value for a stack /// Run-time helper that generates a new origin value for a stack
/// allocation. /// allocation.
Value *MsanSetAllocaOrigin4Fn; FunctionCallee MsanSetAllocaOrigin4Fn;
/// Run-time helper that poisons stack on function entry. /// Run-time helper that poisons stack on function entry.
Value *MsanPoisonStackFn; FunctionCallee MsanPoisonStackFn;
/// Run-time helper that records a store (or any event) of an /// Run-time helper that records a store (or any event) of an
/// uninitialized value and returns an updated origin id encoding this info. /// uninitialized value and returns an updated origin id encoding this info.
Value *MsanChainOriginFn; FunctionCallee MsanChainOriginFn;
/// MSan runtime replacements for memmove, memcpy and memset. /// MSan runtime replacements for memmove, memcpy and memset.
Value *MemmoveFn, *MemcpyFn, *MemsetFn; FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
/// KMSAN callback for task-local function argument shadow. /// KMSAN callback for task-local function argument shadow.
Value *MsanGetContextStateFn; FunctionCallee MsanGetContextStateFn;
/// Functions for poisoning/unpoisoning local variables /// Functions for poisoning/unpoisoning local variables
Value *MsanPoisonAllocaFn, *MsanUnpoisonAllocaFn; FunctionCallee MsanPoisonAllocaFn, MsanUnpoisonAllocaFn;
/// Each of the MsanMetadataPtrXxx functions returns a pair of shadow/origin /// Each of the MsanMetadataPtrXxx functions returns a pair of shadow/origin
/// pointers. /// pointers.
Value *MsanMetadataPtrForLoadN, *MsanMetadataPtrForStoreN; FunctionCallee MsanMetadataPtrForLoadN, MsanMetadataPtrForStoreN;
Value *MsanMetadataPtrForLoad_1_8[4]; FunctionCallee MsanMetadataPtrForLoad_1_8[4];
Value *MsanMetadataPtrForStore_1_8[4]; FunctionCallee MsanMetadataPtrForStore_1_8[4];
Value *MsanInstrumentAsmStoreFn; FunctionCallee MsanInstrumentAsmStoreFn;
/// Helper to choose between different MsanMetadataPtrXxx(). /// Helper to choose between different MsanMetadataPtrXxx().
Value *getKmsanShadowOriginAccessFn(bool isStore, int size); FunctionCallee getKmsanShadowOriginAccessFn(bool isStore, int size);
/// Memory map parameters used in application-to-shadow calculation. /// Memory map parameters used in application-to-shadow calculation.
const MemoryMapParams *MapParams; const MemoryMapParams *MapParams;
@ -823,8 +823,9 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
CallbacksInitialized = true; CallbacksInitialized = true;
} }
Value *MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore, int size) { FunctionCallee MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore,
Value **Fns = int size) {
FunctionCallee *Fns =
isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8; isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8;
switch (size) { switch (size) {
case 1: case 1:
@ -924,7 +925,7 @@ void MemorySanitizer::initializeModule(Module &M) {
/*InitArgs=*/{}, /*InitArgs=*/{},
// This callback is invoked when the functions are created the first // This callback is invoked when the functions are created the first
// time. Hook them into the global ctors list in that case: // time. Hook them into the global ctors list in that case:
[&](Function *Ctor, Function *) { [&](Function *Ctor, FunctionCallee) {
if (!ClWithComdat) { if (!ClWithComdat) {
appendToGlobalCtors(M, Ctor, 0); appendToGlobalCtors(M, Ctor, 0);
return; return;
@ -1123,7 +1124,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
DL.getTypeSizeInBits(ConvertedShadow->getType()); DL.getTypeSizeInBits(ConvertedShadow->getType());
unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits); unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) { if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
Value *Fn = MS.MaybeStoreOriginFn[SizeIndex]; FunctionCallee Fn = MS.MaybeStoreOriginFn[SizeIndex];
Value *ConvertedShadow2 = IRB.CreateZExt( Value *ConvertedShadow2 = IRB.CreateZExt(
ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex))); ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
IRB.CreateCall(Fn, {ConvertedShadow2, IRB.CreateCall(Fn, {ConvertedShadow2,
@ -1205,7 +1206,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType()); unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits); unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) { if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
Value *Fn = MS.MaybeWarningFn[SizeIndex]; FunctionCallee Fn = MS.MaybeWarningFn[SizeIndex];
Value *ConvertedShadow2 = Value *ConvertedShadow2 =
IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex))); IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
IRB.CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin IRB.CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
@ -1412,7 +1413,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
const DataLayout &DL = F.getParent()->getDataLayout(); const DataLayout &DL = F.getParent()->getDataLayout();
int Size = DL.getTypeStoreSize(ShadowTy); int Size = DL.getTypeStoreSize(ShadowTy);
Value *Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size); FunctionCallee Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
Value *AddrCast = Value *AddrCast =
IRB.CreatePointerCast(Addr, PointerType::get(IRB.getInt8Ty(), 0)); IRB.CreatePointerCast(Addr, PointerType::get(IRB.getInt8Ty(), 0));
if (Getter) { if (Getter) {

View File

@ -222,13 +222,13 @@ private:
std::string getSectionName(const std::string &Section) const; std::string getSectionName(const std::string &Section) const;
std::string getSectionStart(const std::string &Section) const; std::string getSectionStart(const std::string &Section) const;
std::string getSectionEnd(const std::string &Section) const; std::string getSectionEnd(const std::string &Section) const;
Function *SanCovTracePCIndir; FunctionCallee SanCovTracePCIndir;
Function *SanCovTracePC, *SanCovTracePCGuard; FunctionCallee SanCovTracePC, SanCovTracePCGuard;
Function *SanCovTraceCmpFunction[4]; FunctionCallee SanCovTraceCmpFunction[4];
Function *SanCovTraceConstCmpFunction[4]; FunctionCallee SanCovTraceConstCmpFunction[4];
Function *SanCovTraceDivFunction[2]; FunctionCallee SanCovTraceDivFunction[2];
Function *SanCovTraceGepFunction; FunctionCallee SanCovTraceGepFunction;
Function *SanCovTraceSwitchFunction; FunctionCallee SanCovTraceSwitchFunction;
GlobalVariable *SanCovLowestStack; GlobalVariable *SanCovLowestStack;
InlineAsm *EmptyAsm; InlineAsm *EmptyAsm;
Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy, Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
@ -328,46 +328,52 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
Int16Ty = IRB.getInt16Ty(); Int16Ty = IRB.getInt16Ty();
Int8Ty = IRB.getInt8Ty(); Int8Ty = IRB.getInt8Ty();
SanCovTracePCIndir = checkSanitizerInterfaceFunction( SanCovTracePCIndir =
M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy)); M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy);
// Make sure smaller parameters are zero-extended to i64 as required by the
// x86_64 ABI.
AttributeList SanCovTraceCmpZeroExtAL;
if (TargetTriple.getArch() == Triple::x86_64) {
SanCovTraceCmpZeroExtAL =
SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 0, Attribute::ZExt);
SanCovTraceCmpZeroExtAL =
SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 1, Attribute::ZExt);
}
SanCovTraceCmpFunction[0] = SanCovTraceCmpFunction[0] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTraceCmp1, SanCovTraceCmpZeroExtAL, VoidTy,
SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty())); IRB.getInt8Ty(), IRB.getInt8Ty());
SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction( SanCovTraceCmpFunction[1] =
M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(), M.getOrInsertFunction(SanCovTraceCmp2, SanCovTraceCmpZeroExtAL, VoidTy,
IRB.getInt16Ty())); IRB.getInt16Ty(), IRB.getInt16Ty());
SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction( SanCovTraceCmpFunction[2] =
M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(), M.getOrInsertFunction(SanCovTraceCmp4, SanCovTraceCmpZeroExtAL, VoidTy,
IRB.getInt32Ty())); IRB.getInt32Ty(), IRB.getInt32Ty());
SanCovTraceCmpFunction[3] = SanCovTraceCmpFunction[3] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty);
SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
SanCovTraceConstCmpFunction[0] = SanCovTraceConstCmpFunction[0] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( SanCovTraceConstCmp1, SanCovTraceCmpZeroExtAL, VoidTy, Int8Ty, Int8Ty);
SanCovTraceConstCmp1, VoidTy, Int8Ty, Int8Ty)); SanCovTraceConstCmpFunction[1] = M.getOrInsertFunction(
SanCovTraceConstCmpFunction[1] = SanCovTraceConstCmp2, SanCovTraceCmpZeroExtAL, VoidTy, Int16Ty, Int16Ty);
checkSanitizerInterfaceFunction(M.getOrInsertFunction( SanCovTraceConstCmpFunction[2] = M.getOrInsertFunction(
SanCovTraceConstCmp2, VoidTy, Int16Ty, Int16Ty)); SanCovTraceConstCmp4, SanCovTraceCmpZeroExtAL, VoidTy, Int32Ty, Int32Ty);
SanCovTraceConstCmpFunction[2] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
SanCovTraceConstCmp4, VoidTy, Int32Ty, Int32Ty));
SanCovTraceConstCmpFunction[3] = SanCovTraceConstCmpFunction[3] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty);
SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty));
SanCovTraceDivFunction[0] = {
checkSanitizerInterfaceFunction(M.getOrInsertFunction( AttributeList AL;
SanCovTraceDiv4, VoidTy, IRB.getInt32Ty())); if (TargetTriple.getArch() == Triple::x86_64)
AL = AL.addParamAttribute(*C, 0, Attribute::ZExt);
SanCovTraceDivFunction[0] =
M.getOrInsertFunction(SanCovTraceDiv4, AL, VoidTy, IRB.getInt32Ty());
}
SanCovTraceDivFunction[1] = SanCovTraceDivFunction[1] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty);
SanCovTraceDiv8, VoidTy, Int64Ty));
SanCovTraceGepFunction = SanCovTraceGepFunction =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy);
SanCovTraceGep, VoidTy, IntptrTy));
SanCovTraceSwitchFunction = SanCovTraceSwitchFunction =
checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy);
SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
Constant *SanCovLowestStackConstant = Constant *SanCovLowestStackConstant =
M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy); M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
@ -377,28 +383,14 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
if (Options.StackDepth && !SanCovLowestStack->isDeclaration()) if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy)); SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
// Make sure smaller parameters are zero-extended to i64 as required by the
// x86_64 ABI.
if (TargetTriple.getArch() == Triple::x86_64) {
for (int i = 0; i < 3; i++) {
SanCovTraceCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
SanCovTraceCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
SanCovTraceConstCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
SanCovTraceConstCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
}
SanCovTraceDivFunction[0]->addParamAttr(0, Attribute::ZExt);
}
// We insert an empty inline asm after cov callbacks to avoid callback merge. // We insert an empty inline asm after cov callbacks to avoid callback merge.
EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
StringRef(""), StringRef(""), StringRef(""), StringRef(""),
/*hasSideEffects=*/true); /*hasSideEffects=*/true);
SanCovTracePC = checkSanitizerInterfaceFunction( SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy);
M.getOrInsertFunction(SanCovTracePCName, VoidTy)); SanCovTracePCGuard =
SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction( M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy);
SanCovTracePCGuardName, VoidTy, Int32PtrTy));
for (auto &F : M) for (auto &F : M)
runOnFunction(F); runOnFunction(F);
@ -413,7 +405,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
SanCovCountersSectionName); SanCovCountersSectionName);
if (Ctor && Options.PCTable) { if (Ctor && Options.PCTable) {
auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy); auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
Function *InitFunction = declareSanitizerInitFunction( FunctionCallee InitFunction = declareSanitizerInitFunction(
M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy}); M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator()); IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second}); IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});

View File

@ -110,25 +110,26 @@ private:
Type *IntptrTy; Type *IntptrTy;
IntegerType *OrdTy; IntegerType *OrdTy;
// Callbacks to run-time library are computed in doInitialization. // Callbacks to run-time library are computed in doInitialization.
Function *TsanFuncEntry; FunctionCallee TsanFuncEntry;
Function *TsanFuncExit; FunctionCallee TsanFuncExit;
Function *TsanIgnoreBegin; FunctionCallee TsanIgnoreBegin;
Function *TsanIgnoreEnd; FunctionCallee TsanIgnoreEnd;
// Accesses sizes are powers of two: 1, 2, 4, 8, 16. // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
static const size_t kNumberOfAccessSizes = 5; static const size_t kNumberOfAccessSizes = 5;
Function *TsanRead[kNumberOfAccessSizes]; FunctionCallee TsanRead[kNumberOfAccessSizes];
Function *TsanWrite[kNumberOfAccessSizes]; FunctionCallee TsanWrite[kNumberOfAccessSizes];
Function *TsanUnalignedRead[kNumberOfAccessSizes]; FunctionCallee TsanUnalignedRead[kNumberOfAccessSizes];
Function *TsanUnalignedWrite[kNumberOfAccessSizes]; FunctionCallee TsanUnalignedWrite[kNumberOfAccessSizes];
Function *TsanAtomicLoad[kNumberOfAccessSizes]; FunctionCallee TsanAtomicLoad[kNumberOfAccessSizes];
Function *TsanAtomicStore[kNumberOfAccessSizes]; FunctionCallee TsanAtomicStore[kNumberOfAccessSizes];
Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes]; FunctionCallee TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1]
Function *TsanAtomicCAS[kNumberOfAccessSizes]; [kNumberOfAccessSizes];
Function *TsanAtomicThreadFence; FunctionCallee TsanAtomicCAS[kNumberOfAccessSizes];
Function *TsanAtomicSignalFence; FunctionCallee TsanAtomicThreadFence;
Function *TsanVptrUpdate; FunctionCallee TsanAtomicSignalFence;
Function *TsanVptrLoad; FunctionCallee TsanVptrUpdate;
Function *MemmoveFn, *MemcpyFn, *MemsetFn; FunctionCallee TsanVptrLoad;
FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
Function *TsanCtorFunction; Function *TsanCtorFunction;
}; };
@ -188,14 +189,14 @@ void ThreadSanitizer::initializeCallbacks(Module &M) {
Attr = Attr.addAttribute(M.getContext(), AttributeList::FunctionIndex, Attr = Attr.addAttribute(M.getContext(), AttributeList::FunctionIndex,
Attribute::NoUnwind); Attribute::NoUnwind);
// Initialize the callbacks. // Initialize the callbacks.
TsanFuncEntry = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanFuncEntry = M.getOrInsertFunction("__tsan_func_entry", Attr,
"__tsan_func_entry", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy())); IRB.getVoidTy(), IRB.getInt8PtrTy());
TsanFuncExit = checkSanitizerInterfaceFunction( TsanFuncExit =
M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy())); M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy());
TsanIgnoreBegin = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanIgnoreBegin = M.getOrInsertFunction("__tsan_ignore_thread_begin", Attr,
"__tsan_ignore_thread_begin", Attr, IRB.getVoidTy())); IRB.getVoidTy());
TsanIgnoreEnd = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanIgnoreEnd =
"__tsan_ignore_thread_end", Attr, IRB.getVoidTy())); M.getOrInsertFunction("__tsan_ignore_thread_end", Attr, IRB.getVoidTy());
OrdTy = IRB.getInt32Ty(); OrdTy = IRB.getInt32Ty();
for (size_t i = 0; i < kNumberOfAccessSizes; ++i) { for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
const unsigned ByteSize = 1U << i; const unsigned ByteSize = 1U << i;
@ -203,32 +204,30 @@ void ThreadSanitizer::initializeCallbacks(Module &M) {
std::string ByteSizeStr = utostr(ByteSize); std::string ByteSizeStr = utostr(ByteSize);
std::string BitSizeStr = utostr(BitSize); std::string BitSizeStr = utostr(BitSize);
SmallString<32> ReadName("__tsan_read" + ByteSizeStr); SmallString<32> ReadName("__tsan_read" + ByteSizeStr);
TsanRead[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanRead[i] = M.getOrInsertFunction(ReadName, Attr, IRB.getVoidTy(),
ReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy())); IRB.getInt8PtrTy());
SmallString<32> WriteName("__tsan_write" + ByteSizeStr); SmallString<32> WriteName("__tsan_write" + ByteSizeStr);
TsanWrite[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanWrite[i] = M.getOrInsertFunction(WriteName, Attr, IRB.getVoidTy(),
WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy())); IRB.getInt8PtrTy());
SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr); SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr);
TsanUnalignedRead[i] = TsanUnalignedRead[i] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr); SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr);
TsanUnalignedWrite[i] = TsanUnalignedWrite[i] = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
Type *Ty = Type::getIntNTy(M.getContext(), BitSize); Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
Type *PtrTy = Ty->getPointerTo(); Type *PtrTy = Ty->getPointerTo();
SmallString<32> AtomicLoadName("__tsan_atomic" + BitSizeStr + "_load"); SmallString<32> AtomicLoadName("__tsan_atomic" + BitSizeStr + "_load");
TsanAtomicLoad[i] = checkSanitizerInterfaceFunction( TsanAtomicLoad[i] =
M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy)); M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy);
SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store"); SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store");
TsanAtomicStore[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanAtomicStore[i] = M.getOrInsertFunction(
AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy)); AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy);
for (int op = AtomicRMWInst::FIRST_BINOP; for (int op = AtomicRMWInst::FIRST_BINOP;
op <= AtomicRMWInst::LAST_BINOP; ++op) { op <= AtomicRMWInst::LAST_BINOP; ++op) {
@ -251,34 +250,34 @@ void ThreadSanitizer::initializeCallbacks(Module &M) {
else else
continue; continue;
SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart); SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
TsanAtomicRMW[op][i] = checkSanitizerInterfaceFunction( TsanAtomicRMW[op][i] =
M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy)); M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy);
} }
SmallString<32> AtomicCASName("__tsan_atomic" + BitSizeStr + SmallString<32> AtomicCASName("__tsan_atomic" + BitSizeStr +
"_compare_exchange_val"); "_compare_exchange_val");
TsanAtomicCAS[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanAtomicCAS[i] = M.getOrInsertFunction(AtomicCASName, Attr, Ty, PtrTy, Ty,
AtomicCASName, Attr, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy)); Ty, OrdTy, OrdTy);
} }
TsanVptrUpdate = checkSanitizerInterfaceFunction( TsanVptrUpdate =
M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(), M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy())); IRB.getInt8PtrTy(), IRB.getInt8PtrTy());
TsanVptrLoad = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanVptrLoad = M.getOrInsertFunction("__tsan_vptr_read", Attr,
"__tsan_vptr_read", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy())); IRB.getVoidTy(), IRB.getInt8PtrTy());
TsanAtomicThreadFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanAtomicThreadFence = M.getOrInsertFunction("__tsan_atomic_thread_fence",
"__tsan_atomic_thread_fence", Attr, IRB.getVoidTy(), OrdTy)); Attr, IRB.getVoidTy(), OrdTy);
TsanAtomicSignalFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction( TsanAtomicSignalFence = M.getOrInsertFunction("__tsan_atomic_signal_fence",
"__tsan_atomic_signal_fence", Attr, IRB.getVoidTy(), OrdTy)); Attr, IRB.getVoidTy(), OrdTy);
MemmoveFn = checkSanitizerInterfaceFunction( MemmoveFn =
M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
MemcpyFn = checkSanitizerInterfaceFunction( MemcpyFn =
M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IntptrTy)); IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
MemsetFn = checkSanitizerInterfaceFunction( MemsetFn =
M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(),
IRB.getInt32Ty(), IntptrTy)); IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy);
} }
ThreadSanitizer::ThreadSanitizer(Module &M) { ThreadSanitizer::ThreadSanitizer(Module &M) {
@ -290,7 +289,9 @@ ThreadSanitizer::ThreadSanitizer(Module &M) {
/*InitArgs=*/{}, /*InitArgs=*/{},
// This callback is invoked when the functions are created the first // This callback is invoked when the functions are created the first
// time. Hook them into the global ctors list in that case: // time. Hook them into the global ctors list in that case:
[&](Function *Ctor, Function *) { appendToGlobalCtors(M, Ctor, 0); }); [&](Function *Ctor, FunctionCallee) {
appendToGlobalCtors(M, Ctor, 0);
});
} }
static bool isVtableAccess(Instruction *I) { static bool isVtableAccess(Instruction *I) {
@ -558,7 +559,7 @@ bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I,
: cast<LoadInst>(I)->getAlignment(); : cast<LoadInst>(I)->getAlignment();
Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType(); Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType();
const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy); const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
Value *OnAccessFunc = nullptr; FunctionCallee OnAccessFunc = nullptr;
if (Alignment == 0 || Alignment >= 8 || (Alignment % (TypeSize / 8)) == 0) if (Alignment == 0 || Alignment >= 8 || (Alignment % (TypeSize / 8)) == 0)
OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx]; OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
else else
@ -658,7 +659,7 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
int Idx = getMemoryAccessFuncIndex(Addr, DL); int Idx = getMemoryAccessFuncIndex(Addr, DL);
if (Idx < 0) if (Idx < 0)
return false; return false;
Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx]; FunctionCallee F = TsanAtomicRMW[RMWI->getOperation()][Idx];
if (!F) if (!F)
return false; return false;
const unsigned ByteSize = 1U << Idx; const unsigned ByteSize = 1U << Idx;
@ -705,8 +706,9 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
I->eraseFromParent(); I->eraseFromParent();
} else if (FenceInst *FI = dyn_cast<FenceInst>(I)) { } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
Value *Args[] = {createOrdering(&IRB, FI->getOrdering())}; Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
Function *F = FI->getSyncScopeID() == SyncScope::SingleThread ? FunctionCallee F = FI->getSyncScopeID() == SyncScope::SingleThread
TsanAtomicSignalFence : TsanAtomicThreadFence; ? TsanAtomicSignalFence
: TsanAtomicThreadFence;
CallInst *C = CallInst::Create(F, Args); CallInst *C = CallInst::Create(F, Args);
ReplaceInstWithInst(I, C); ReplaceInstWithInst(I, C);
} }

View File

@ -930,9 +930,8 @@ bool LoopIdiomRecognize::processLoopStridedStore(
Module *M = TheStore->getModule(); Module *M = TheStore->getModule();
StringRef FuncName = "memset_pattern16"; StringRef FuncName = "memset_pattern16";
Value *MSP = FunctionCallee MSP = M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
M->getOrInsertFunction(FuncName, Builder.getVoidTy(), Int8PtrTy, Int8PtrTy, IntPtr);
Int8PtrTy, Int8PtrTy, IntPtr);
inferLibFuncAttributes(M, FuncName, *TLI); inferLibFuncAttributes(M, FuncName, *TLI);
// Otherwise we should form a memset_pattern16. PatternValue is known to be // Otherwise we should form a memset_pattern16. PatternValue is known to be

View File

@ -1480,8 +1480,9 @@ makeStatepointExplicitImpl(const CallSite CS, /* to replace */
// calls to @llvm.experimental.deoptimize with different argument types in // calls to @llvm.experimental.deoptimize with different argument types in
// the same module. This is fine -- we assume the frontend knew what it // the same module. This is fine -- we assume the frontend knew what it
// was doing when generating this kind of IR. // was doing when generating this kind of IR.
CallTarget = CallTarget = F->getParent()
F->getParent()->getOrInsertFunction("__llvm_deoptimize", FTy); ->getOrInsertFunction("__llvm_deoptimize", FTy)
.getCallee();
IsDeoptimize = true; IsDeoptimize = true;
} }
@ -1900,8 +1901,8 @@ static void insertUseHolderAfter(CallSite &CS, const ArrayRef<Value *> Values,
Module *M = CS.getInstruction()->getModule(); Module *M = CS.getInstruction()->getModule();
// Use a dummy vararg function to actually hold the values live // Use a dummy vararg function to actually hold the values live
Function *Func = cast<Function>(M->getOrInsertFunction( FunctionCallee Func = M->getOrInsertFunction(
"__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true))); "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true));
if (CS.isCall()) { if (CS.isCall()) {
// For call safepoints insert dummy calls right after safepoint // For call safepoints insert dummy calls right after safepoint
Holders.push_back(CallInst::Create(Func, Values, "", Holders.push_back(CallInst::Create(Func, Values, "",

View File

@ -797,11 +797,12 @@ Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef StrlenName = TLI->getName(LibFunc_strlen); StringRef StrlenName = TLI->getName(LibFunc_strlen);
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
Constant *StrLen = M->getOrInsertFunction(StrlenName, DL.getIntPtrType(Context), FunctionCallee StrLen = M->getOrInsertFunction(
B.getInt8PtrTy()); StrlenName, DL.getIntPtrType(Context), B.getInt8PtrTy());
inferLibFuncAttributes(M, StrlenName, *TLI); inferLibFuncAttributes(M, StrlenName, *TLI);
CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName); CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(StrLen.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -816,12 +817,13 @@ Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
StringRef StrChrName = TLI->getName(LibFunc_strchr); StringRef StrChrName = TLI->getName(LibFunc_strchr);
Type *I8Ptr = B.getInt8PtrTy(); Type *I8Ptr = B.getInt8PtrTy();
Type *I32Ty = B.getInt32Ty(); Type *I32Ty = B.getInt32Ty();
Constant *StrChr = FunctionCallee StrChr =
M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty); M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
inferLibFuncAttributes(M, StrChrName, *TLI); inferLibFuncAttributes(M, StrChrName, *TLI);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName); StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(StrChr.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
@ -834,14 +836,15 @@ Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef StrNCmpName = TLI->getName(LibFunc_strncmp); StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
Value *StrNCmp = M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), FunctionCallee StrNCmp =
B.getInt8PtrTy(), B.getInt8PtrTy(), M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
DL.getIntPtrType(Context)); B.getInt8PtrTy(), DL.getIntPtrType(Context));
inferLibFuncAttributes(M, StrNCmpName, *TLI); inferLibFuncAttributes(M, StrNCmpName, *TLI);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName); StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(StrNCmp.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -854,11 +857,12 @@ Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
Type *I8Ptr = B.getInt8PtrTy(); Type *I8Ptr = B.getInt8PtrTy();
Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr); FunctionCallee StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
inferLibFuncAttributes(M, Name, *TLI); inferLibFuncAttributes(M, Name, *TLI);
CallInst *CI = CallInst *CI =
B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name); B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(StrCpy.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
@ -870,12 +874,13 @@ Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
Type *I8Ptr = B.getInt8PtrTy(); Type *I8Ptr = B.getInt8PtrTy();
Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, FunctionCallee StrNCpy =
Len->getType()); M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
inferLibFuncAttributes(M, Name, *TLI); inferLibFuncAttributes(M, Name, *TLI);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name); StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(StrNCpy.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
@ -891,14 +896,15 @@ Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
Attribute::NoUnwind); Attribute::NoUnwind);
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
Value *MemCpy = M->getOrInsertFunction( FunctionCallee MemCpy = M->getOrInsertFunction(
"__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
DL.getIntPtrType(Context)); DL.getIntPtrType(Context));
Dst = castToCStr(Dst, B); Dst = castToCStr(Dst, B);
Src = castToCStr(Src, B); Src = castToCStr(Src, B);
CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
@ -911,13 +917,14 @@ Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef MemChrName = TLI->getName(LibFunc_memchr); StringRef MemChrName = TLI->getName(LibFunc_memchr);
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
Value *MemChr = M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), FunctionCallee MemChr =
B.getInt8PtrTy(), B.getInt32Ty(), M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), B.getInt8PtrTy(),
DL.getIntPtrType(Context)); B.getInt32Ty(), DL.getIntPtrType(Context));
inferLibFuncAttributes(M, MemChrName, *TLI); inferLibFuncAttributes(M, MemChrName, *TLI);
CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName); CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(MemChr.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -931,14 +938,15 @@ Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef MemCmpName = TLI->getName(LibFunc_memcmp); StringRef MemCmpName = TLI->getName(LibFunc_memcmp);
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
Value *MemCmp = M->getOrInsertFunction(MemCmpName, B.getInt32Ty(), FunctionCallee MemCmp =
B.getInt8PtrTy(), B.getInt8PtrTy(), M->getOrInsertFunction(MemCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
DL.getIntPtrType(Context)); B.getInt8PtrTy(), DL.getIntPtrType(Context));
inferLibFuncAttributes(M, MemCmpName, *TLI); inferLibFuncAttributes(M, MemCmpName, *TLI);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName); MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName);
if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(MemCmp.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -965,8 +973,8 @@ static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall"); assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
Value *Callee = M->getOrInsertFunction(Name, Op->getType(), FunctionCallee Callee =
Op->getType()); M->getOrInsertFunction(Name, Op->getType(), Op->getType());
CallInst *CI = B.CreateCall(Callee, Op, Name); CallInst *CI = B.CreateCall(Callee, Op, Name);
// The incoming attribute set may have come from a speculatable intrinsic, but // The incoming attribute set may have come from a speculatable intrinsic, but
@ -975,7 +983,8 @@ static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
CI->setAttributes(Attrs.removeAttribute(B.getContext(), CI->setAttributes(Attrs.removeAttribute(B.getContext(),
AttributeList::FunctionIndex, AttributeList::FunctionIndex,
Attribute::Speculatable)); Attribute::Speculatable));
if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -1008,11 +1017,12 @@ Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
appendTypeSuffix(Op1, Name, NameBuffer); appendTypeSuffix(Op1, Name, NameBuffer);
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(), FunctionCallee Callee = M->getOrInsertFunction(
Op2->getType()); Name, Op1->getType(), Op1->getType(), Op2->getType());
CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name); CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
CI->setAttributes(Attrs); CI->setAttributes(Attrs);
if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -1025,7 +1035,8 @@ Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef PutCharName = TLI->getName(LibFunc_putchar); StringRef PutCharName = TLI->getName(LibFunc_putchar);
Value *PutChar = M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty()); FunctionCallee PutChar =
M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
inferLibFuncAttributes(M, PutCharName, *TLI); inferLibFuncAttributes(M, PutCharName, *TLI);
CallInst *CI = B.CreateCall(PutChar, CallInst *CI = B.CreateCall(PutChar,
B.CreateIntCast(Char, B.CreateIntCast(Char,
@ -1034,7 +1045,8 @@ Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
"chari"), "chari"),
PutCharName); PutCharName);
if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
@ -1046,11 +1058,12 @@ Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef PutsName = TLI->getName(LibFunc_puts); StringRef PutsName = TLI->getName(LibFunc_puts);
Value *PutS = FunctionCallee PutS =
M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy()); M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
inferLibFuncAttributes(M, PutsName, *TLI); inferLibFuncAttributes(M, PutsName, *TLI);
CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
@ -1062,15 +1075,16 @@ Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef FPutcName = TLI->getName(LibFunc_fputc); StringRef FPutcName = TLI->getName(LibFunc_fputc);
Constant *F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), B.getInt32Ty(), FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
File->getType()); B.getInt32Ty(), File->getType());
if (File->getType()->isPointerTy()) if (File->getType()->isPointerTy())
inferLibFuncAttributes(M, FPutcName, *TLI); inferLibFuncAttributes(M, FPutcName, *TLI);
Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
"chari"); "chari");
CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1082,14 +1096,15 @@ Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked); StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
Constant *F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(), FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
B.getInt32Ty(), File->getType()); B.getInt32Ty(), File->getType());
if (File->getType()->isPointerTy()) if (File->getType()->isPointerTy())
inferLibFuncAttributes(M, FPutcUnlockedName, *TLI); inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari"); Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName); CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1101,13 +1116,14 @@ Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef FPutsName = TLI->getName(LibFunc_fputs); StringRef FPutsName = TLI->getName(LibFunc_fputs);
Constant *F = M->getOrInsertFunction( FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType()); B.getInt8PtrTy(), File->getType());
if (File->getType()->isPointerTy()) if (File->getType()->isPointerTy())
inferLibFuncAttributes(M, FPutsName, *TLI); inferLibFuncAttributes(M, FPutsName, *TLI);
CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName); CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1119,13 +1135,14 @@ Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked); StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(), FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
B.getInt8PtrTy(), File->getType()); B.getInt8PtrTy(), File->getType());
if (File->getType()->isPointerTy()) if (File->getType()->isPointerTy())
inferLibFuncAttributes(M, FPutsUnlockedName, *TLI); inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName); CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1138,7 +1155,7 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
StringRef FWriteName = TLI->getName(LibFunc_fwrite); StringRef FWriteName = TLI->getName(LibFunc_fwrite);
Constant *F = M->getOrInsertFunction( FunctionCallee F = M->getOrInsertFunction(
FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
@ -1148,7 +1165,8 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
B.CreateCall(F, {castToCStr(Ptr, B), Size, B.CreateCall(F, {castToCStr(Ptr, B), Size,
ConstantInt::get(DL.getIntPtrType(Context), 1), File}); ConstantInt::get(DL.getIntPtrType(Context), 1), File});
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1161,12 +1179,13 @@ Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef MallocName = TLI->getName(LibFunc_malloc); StringRef MallocName = TLI->getName(LibFunc_malloc);
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
Value *Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(), FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
DL.getIntPtrType(Context)); DL.getIntPtrType(Context));
inferLibFuncAttributes(M, MallocName, *TLI); inferLibFuncAttributes(M, MallocName, *TLI);
CallInst *CI = B.CreateCall(Malloc, Num, MallocName); CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts())) if (const Function *F =
dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -1181,12 +1200,13 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
StringRef CallocName = TLI.getName(LibFunc_calloc); StringRef CallocName = TLI.getName(LibFunc_calloc);
const DataLayout &DL = M->getDataLayout(); const DataLayout &DL = M->getDataLayout();
IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
Value *Calloc = M->getOrInsertFunction(CallocName, Attrs, B.getInt8PtrTy(), FunctionCallee Calloc = M->getOrInsertFunction(
PtrType, PtrType); CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
inferLibFuncAttributes(M, CallocName, TLI); inferLibFuncAttributes(M, CallocName, TLI);
CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts())) if (const auto *F =
dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
@ -1201,7 +1221,7 @@ Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked); StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
Constant *F = M->getOrInsertFunction( FunctionCallee F = M->getOrInsertFunction(
FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
@ -1209,7 +1229,8 @@ Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
inferLibFuncAttributes(M, FWriteUnlockedName, *TLI); inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1221,13 +1242,14 @@ Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked); StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
Constant *F = FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(),
M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), File->getType()); File->getType());
if (File->getType()->isPointerTy()) if (File->getType()->isPointerTy())
inferLibFuncAttributes(M, FGetCUnlockedName, *TLI); inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName); CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1239,14 +1261,15 @@ Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked); StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
Constant *F = FunctionCallee F =
M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(), M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
B.getInt8PtrTy(), B.getInt32Ty(), File->getType()); B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
inferLibFuncAttributes(M, FGetSUnlockedName, *TLI); inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
CallInst *CI = CallInst *CI =
B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName); B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
@ -1260,7 +1283,7 @@ Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
Module *M = B.GetInsertBlock()->getModule(); Module *M = B.GetInsertBlock()->getModule();
LLVMContext &Context = B.GetInsertBlock()->getContext(); LLVMContext &Context = B.GetInsertBlock()->getContext();
StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked); StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
Constant *F = M->getOrInsertFunction( FunctionCallee F = M->getOrInsertFunction(
FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
@ -1268,7 +1291,8 @@ Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
inferLibFuncAttributes(M, FReadUnlockedName, *TLI); inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn =
dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }

View File

@ -30,7 +30,7 @@ static void insertCall(Function &CurFn, StringRef Func,
Func == "__mcount" || Func == "__mcount" ||
Func == "_mcount" || Func == "_mcount" ||
Func == "__cyg_profile_func_enter_bare") { Func == "__cyg_profile_func_enter_bare") {
Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C)); FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
CallInst *Call = CallInst::Create(Fn, "", InsertionPt); CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
Call->setDebugLoc(DL); Call->setDebugLoc(DL);
return; return;
@ -39,7 +39,7 @@ static void insertCall(Function &CurFn, StringRef Func,
if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") { if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") {
Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)}; Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)};
Constant *Fn = M.getOrInsertFunction( FunctionCallee Fn = M.getOrInsertFunction(
Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false)); Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false));
Instruction *RetAddr = CallInst::Create( Instruction *RetAddr = CallInst::Create(

View File

@ -18,7 +18,7 @@
#include "llvm/IR/Module.h" #include "llvm/IR/Module.h"
using namespace llvm; using namespace llvm;
static Constant *getDefaultPersonalityFn(Module *M) { static FunctionCallee getDefaultPersonalityFn(Module *M) {
LLVMContext &C = M->getContext(); LLVMContext &C = M->getContext();
Triple T(M->getTargetTriple()); Triple T(M->getTargetTriple());
EHPersonality Pers = getDefaultEHPersonality(T); EHPersonality Pers = getDefaultEHPersonality(T);
@ -68,8 +68,8 @@ IRBuilder<> *EscapeEnumerator::Next() {
BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F); BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C)); Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
if (!F.hasPersonalityFn()) { if (!F.hasPersonalityFn()) {
Constant *PersFn = getDefaultPersonalityFn(F.getParent()); FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
F.setPersonalityFn(PersFn); F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
} }
if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) { if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {

View File

@ -126,36 +126,24 @@ void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
appendToUsedList(M, "llvm.compiler.used", Values); appendToUsedList(M, "llvm.compiler.used", Values);
} }
Function *llvm::checkSanitizerInterfaceFunction(Constant *FuncOrBitcast) { FunctionCallee
if (isa<Function>(FuncOrBitcast)) llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
return cast<Function>(FuncOrBitcast); ArrayRef<Type *> InitArgTypes) {
FuncOrBitcast->print(errs());
errs() << '\n';
std::string Err;
raw_string_ostream Stream(Err);
Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
report_fatal_error(Err);
}
Function *llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
ArrayRef<Type *> InitArgTypes) {
assert(!InitName.empty() && "Expected init function name"); assert(!InitName.empty() && "Expected init function name");
Function *F = checkSanitizerInterfaceFunction(M.getOrInsertFunction( return M.getOrInsertFunction(
InitName, InitName,
FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false), FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
AttributeList())); AttributeList());
F->setLinkage(Function::ExternalLinkage);
return F;
} }
std::pair<Function *, Function *> llvm::createSanitizerCtorAndInitFunctions( std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
Module &M, StringRef CtorName, StringRef InitName, Module &M, StringRef CtorName, StringRef InitName,
ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs, ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
StringRef VersionCheckName) { StringRef VersionCheckName) {
assert(!InitName.empty() && "Expected init function name"); assert(!InitName.empty() && "Expected init function name");
assert(InitArgs.size() == InitArgTypes.size() && assert(InitArgs.size() == InitArgTypes.size() &&
"Sanitizer's init function expects different number of arguments"); "Sanitizer's init function expects different number of arguments");
Function *InitFunction = FunctionCallee InitFunction =
declareSanitizerInitFunction(M, InitName, InitArgTypes); declareSanitizerInitFunction(M, InitName, InitArgTypes);
Function *Ctor = Function::Create( Function *Ctor = Function::Create(
FunctionType::get(Type::getVoidTy(M.getContext()), false), FunctionType::get(Type::getVoidTy(M.getContext()), false),
@ -164,20 +152,19 @@ std::pair<Function *, Function *> llvm::createSanitizerCtorAndInitFunctions(
IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB)); IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
IRB.CreateCall(InitFunction, InitArgs); IRB.CreateCall(InitFunction, InitArgs);
if (!VersionCheckName.empty()) { if (!VersionCheckName.empty()) {
Function *VersionCheckFunction = FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
checkSanitizerInterfaceFunction(M.getOrInsertFunction( VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false), AttributeList());
AttributeList()));
IRB.CreateCall(VersionCheckFunction, {}); IRB.CreateCall(VersionCheckFunction, {});
} }
return std::make_pair(Ctor, InitFunction); return std::make_pair(Ctor, InitFunction);
} }
std::pair<Function *, Function *> std::pair<Function *, FunctionCallee>
llvm::getOrCreateSanitizerCtorAndInitFunctions( llvm::getOrCreateSanitizerCtorAndInitFunctions(
Module &M, StringRef CtorName, StringRef InitName, Module &M, StringRef CtorName, StringRef InitName,
ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs, ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
function_ref<void(Function *, Function *)> FunctionsCreatedCallback, function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
StringRef VersionCheckName) { StringRef VersionCheckName) {
assert(!CtorName.empty() && "Expected ctor function name"); assert(!CtorName.empty() && "Expected ctor function name");
@ -188,7 +175,8 @@ llvm::getOrCreateSanitizerCtorAndInitFunctions(
Ctor->getReturnType() == Type::getVoidTy(M.getContext())) Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)}; return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
Function *Ctor, *InitFunction; Function *Ctor;
FunctionCallee InitFunction;
std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions( std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName); M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
FunctionsCreatedCallback(Ctor, InitFunction); FunctionsCreatedCallback(Ctor, InitFunction);
@ -207,9 +195,10 @@ Function *llvm::getOrCreateInitFunction(Module &M, StringRef Name) {
} }
return F; return F;
} }
Function *F = checkSanitizerInterfaceFunction(M.getOrInsertFunction( Function *F =
Name, AttributeList(), Type::getVoidTy(M.getContext()))); cast<Function>(M.getOrInsertFunction(Name, AttributeList(),
F->setLinkage(Function::ExternalLinkage); Type::getVoidTy(M.getContext()))
.getCallee());
appendToGlobalCtors(M, F, 0); appendToGlobalCtors(M, F, 0);

View File

@ -488,8 +488,10 @@ void PredicateInfo::buildPredicateInfo() {
// tricky (FIXME). // tricky (FIXME).
static Function *getCopyDeclaration(Module *M, Type *Ty) { static Function *getCopyDeclaration(Module *M, Type *Ty) {
std::string Name = "llvm.ssa.copy." + utostr((uintptr_t) Ty); std::string Name = "llvm.ssa.copy." + utostr((uintptr_t) Ty);
return cast<Function>(M->getOrInsertFunction( return cast<Function>(
Name, getType(M->getContext(), Intrinsic::ssa_copy, Ty))); M->getOrInsertFunction(Name,
getType(M->getContext(), Intrinsic::ssa_copy, Ty))
.getCallee());
} }
// Given the renaming stack, make all the operands currently on the stack real // Given the renaming stack, make all the operands currently on the stack real

View File

@ -56,8 +56,8 @@ void SanitizerStatReport::create(IRBuilder<> &B, SanitizerStatKind SK) {
FunctionType *StatReportTy = FunctionType *StatReportTy =
FunctionType::get(B.getVoidTy(), Int8PtrTy, false); FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
Constant *StatReport = M->getOrInsertFunction( FunctionCallee StatReport =
"__sanitizer_stat_report", StatReportTy); M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
auto InitAddr = ConstantExpr::getGetElementPtr( auto InitAddr = ConstantExpr::getGetElementPtr(
EmptyModuleStatsTy, ModuleStatsGV, EmptyModuleStatsTy, ModuleStatsGV,
@ -97,8 +97,8 @@ void SanitizerStatReport::finish() {
IRBuilder<> B(BB); IRBuilder<> B(BB);
FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false); FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
Constant *StatInit = M->getOrInsertFunction( FunctionCallee StatInit =
"__sanitizer_stat_init", StatInitTy); M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy)); B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
B.CreateRetVoid(); B.CreateRetVoid();

View File

@ -1503,9 +1503,8 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {
One = ConstantExpr::getFPExtend(One, Op->getType()); One = ConstantExpr::getFPExtend(One, Op->getType());
Module *M = CI->getModule(); Module *M = CI->getModule();
Value *NewCallee = FunctionCallee NewCallee = M->getOrInsertFunction(
M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(), TLI->getName(LdExp), Op->getType(), Op->getType(), B.getInt32Ty());
Op->getType(), B.getInt32Ty());
CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg}); CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg});
if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
@ -1727,8 +1726,8 @@ static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
} }
Module *M = OrigCallee->getParent(); Module *M = OrigCallee->getParent();
Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(), FunctionCallee Callee =
ResTy, ArgTy); M->getOrInsertFunction(Name, OrigCallee->getAttributes(), ResTy, ArgTy);
if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) { if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
// If the argument is an instruction, it must dominate all uses so put our // If the argument is an instruction, it must dominate all uses so put our
@ -2025,7 +2024,7 @@ Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
// arguments. // arguments.
if (TLI->has(LibFunc_iprintf) && !callHasFloatingPointArgument(CI)) { if (TLI->has(LibFunc_iprintf) && !callHasFloatingPointArgument(CI)) {
Module *M = B.GetInsertBlock()->getParent()->getParent(); Module *M = B.GetInsertBlock()->getParent()->getParent();
Constant *IPrintFFn = FunctionCallee IPrintFFn =
M->getOrInsertFunction("iprintf", FT, Callee->getAttributes()); M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
CallInst *New = cast<CallInst>(CI->clone()); CallInst *New = cast<CallInst>(CI->clone());
New->setCalledFunction(IPrintFFn); New->setCalledFunction(IPrintFFn);
@ -2104,7 +2103,7 @@ Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {
// point arguments. // point arguments.
if (TLI->has(LibFunc_siprintf) && !callHasFloatingPointArgument(CI)) { if (TLI->has(LibFunc_siprintf) && !callHasFloatingPointArgument(CI)) {
Module *M = B.GetInsertBlock()->getParent()->getParent(); Module *M = B.GetInsertBlock()->getParent()->getParent();
Constant *SIPrintFFn = FunctionCallee SIPrintFFn =
M->getOrInsertFunction("siprintf", FT, Callee->getAttributes()); M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
CallInst *New = cast<CallInst>(CI->clone()); CallInst *New = cast<CallInst>(CI->clone());
New->setCalledFunction(SIPrintFFn); New->setCalledFunction(SIPrintFFn);
@ -2261,7 +2260,7 @@ Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {
// floating point arguments. // floating point arguments.
if (TLI->has(LibFunc_fiprintf) && !callHasFloatingPointArgument(CI)) { if (TLI->has(LibFunc_fiprintf) && !callHasFloatingPointArgument(CI)) {
Module *M = B.GetInsertBlock()->getParent()->getParent(); Module *M = B.GetInsertBlock()->getParent()->getParent();
Constant *FIPrintFFn = FunctionCallee FIPrintFFn =
M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes()); M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
CallInst *New = cast<CallInst>(CI->clone()); CallInst *New = cast<CallInst>(CI->clone());
New->setCalledFunction(FIPrintFFn); New->setCalledFunction(FIPrintFFn);

View File

@ -826,13 +826,14 @@ CleanupAndPrepareModules(BugDriver &BD, std::unique_ptr<Module> Test,
// Add the resolver to the Safe module. // Add the resolver to the Safe module.
// Prototype: void *getPointerToNamedFunction(const char* Name) // Prototype: void *getPointerToNamedFunction(const char* Name)
Constant *resolverFunc = Safe->getOrInsertFunction( FunctionCallee resolverFunc = Safe->getOrInsertFunction(
"getPointerToNamedFunction", Type::getInt8PtrTy(Safe->getContext()), "getPointerToNamedFunction", Type::getInt8PtrTy(Safe->getContext()),
Type::getInt8PtrTy(Safe->getContext())); Type::getInt8PtrTy(Safe->getContext()));
// Use the function we just added to get addresses of functions we need. // Use the function we just added to get addresses of functions we need.
for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && if (F->isDeclaration() && !F->use_empty() &&
&*F != resolverFunc.getCallee() &&
!F->isIntrinsic() /* ignore intrinsics */) { !F->isIntrinsic() /* ignore intrinsics */) {
Function *TestFn = Test->getFunction(F->getName()); Function *TestFn = Test->getFunction(F->getName());

View File

@ -595,8 +595,8 @@ int main(int argc, char **argv, char * const *envp) {
if (!RemoteMCJIT) { if (!RemoteMCJIT) {
// If the program doesn't explicitly call exit, we will need the Exit // If the program doesn't explicitly call exit, we will need the Exit
// function later on to make an explicit call, so get the function now. // function later on to make an explicit call, so get the function now.
Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), FunctionCallee Exit = Mod->getOrInsertFunction(
Type::getInt32Ty(Context)); "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context));
// Run static constructors. // Run static constructors.
if (!ForceInterpreter) { if (!ForceInterpreter) {
@ -620,19 +620,21 @@ int main(int argc, char **argv, char * const *envp) {
// If the program didn't call exit explicitly, we should call it now. // If the program didn't call exit explicitly, we should call it now.
// This ensures that any atexit handlers get called correctly. // This ensures that any atexit handlers get called correctly.
if (Function *ExitF = dyn_cast<Function>(Exit)) { if (Function *ExitF =
std::vector<GenericValue> Args; dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) {
GenericValue ResultGV; if (ExitF->getFunctionType() == Exit.getFunctionType()) {
ResultGV.IntVal = APInt(32, Result); std::vector<GenericValue> Args;
Args.push_back(ResultGV); GenericValue ResultGV;
EE->runFunction(ExitF, Args); ResultGV.IntVal = APInt(32, Result);
WithColor::error(errs(), argv[0]) << "exit(" << Result << ") returned!\n"; Args.push_back(ResultGV);
abort(); EE->runFunction(ExitF, Args);
} else { WithColor::error(errs(), argv[0])
WithColor::error(errs(), argv[0]) << "exit(" << Result << ") returned!\n";
<< "exit defined with wrong prototype!\n"; abort();
abort(); }
} }
WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
abort();
} else { } else {
// else == "if (RemoteMCJIT)" // else == "if (RemoteMCJIT)"

View File

@ -166,7 +166,7 @@ TEST_F(AliasAnalysisTest, getModRefInfo) {
// Setup function. // Setup function.
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(C), std::vector<Type *>(), false); FunctionType::get(Type::getVoidTy(C), std::vector<Type *>(), false);
auto *F = cast<Function>(M.getOrInsertFunction("f", FTy)); auto *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
auto *BB = BasicBlock::Create(C, "entry", F); auto *BB = BasicBlock::Create(C, "entry", F);
auto IntType = Type::getInt32Ty(C); auto IntType = Type::getInt32Ty(C);
auto PtrType = Type::getInt32PtrTy(C); auto PtrType = Type::getInt32PtrTy(C);

View File

@ -78,7 +78,7 @@ TEST_F(DivergenceAnalysisTest, DAInitialState) {
IntegerType *IntTy = IntegerType::getInt32Ty(Context); IntegerType *IntTy = IntegerType::getInt32Ty(Context);
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), {IntTy}, false); FunctionType::get(Type::getVoidTy(Context), {IntTy}, false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *BB = BasicBlock::Create(Context, "entry", F); BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
ReturnInst::Create(Context, nullptr, BB); ReturnInst::Create(Context, nullptr, BB);

View File

@ -25,7 +25,7 @@ TEST(OrderedInstructionsTest, DominanceTest) {
IRBuilder<> B(Ctx); IRBuilder<> B(Ctx);
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Ctx), {B.getInt8PtrTy()}, false); FunctionType::get(Type::getVoidTy(Ctx), {B.getInt8PtrTy()}, false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
// Create the function as follow and check for dominance relation. // Create the function as follow and check for dominance relation.
// //

View File

@ -26,7 +26,8 @@ TEST(PhiValuesTest, SimplePhi) {
Type *I32PtrTy = Type::getInt32PtrTy(C); Type *I32PtrTy = Type::getInt32PtrTy(C);
// Create a function with phis that do not have other phis as incoming values // Create a function with phis that do not have other phis as incoming values
Function *F = cast<Function>(M.getOrInsertFunction("f", FunctionType::get(VoidTy, false))); Function *F = Function::Create(FunctionType::get(VoidTy, false),
Function::ExternalLinkage, "f", M);
BasicBlock *Entry = BasicBlock::Create(C, "entry", F); BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
BasicBlock *If = BasicBlock::Create(C, "if", F); BasicBlock *If = BasicBlock::Create(C, "if", F);
@ -92,7 +93,8 @@ TEST(PhiValuesTest, DependentPhi) {
Type *I32PtrTy = Type::getInt32PtrTy(C); Type *I32PtrTy = Type::getInt32PtrTy(C);
// Create a function with a phi that has another phi as an incoming value // Create a function with a phi that has another phi as an incoming value
Function *F = cast<Function>(M.getOrInsertFunction("f", FunctionType::get(VoidTy, false))); Function *F = Function::Create(FunctionType::get(VoidTy, false),
Function::ExternalLinkage, "f", M);
BasicBlock *Entry = BasicBlock::Create(C, "entry", F); BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
BasicBlock *If1 = BasicBlock::Create(C, "if1", F); BasicBlock *If1 = BasicBlock::Create(C, "if1", F);

View File

@ -63,7 +63,7 @@ protected:
TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) { TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
std::vector<Type *>(), false); std::vector<Type *>(), false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *BB = BasicBlock::Create(Context, "entry", F); BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
ReturnInst::Create(Context, nullptr, BB); ReturnInst::Create(Context, nullptr, BB);
@ -112,7 +112,7 @@ TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
TEST_F(ScalarEvolutionsTest, SimplifiedPHI) { TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
std::vector<Type *>(), false); std::vector<Type *>(), false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F); BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F); BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F); BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
@ -146,7 +146,7 @@ TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) {
auto *I32PtrTy = Type::getInt32PtrTy(Context); auto *I32PtrTy = Type::getInt32PtrTy(Context);
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false); FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F); BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F); BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F); BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
@ -329,7 +329,7 @@ TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
TEST_F(ScalarEvolutionsTest, CompareSCEVComplexity) { TEST_F(ScalarEvolutionsTest, CompareSCEVComplexity) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false); FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F); BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F); BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F);
BranchInst::Create(LoopBB, EntryBB); BranchInst::Create(LoopBB, EntryBB);
@ -399,7 +399,7 @@ TEST_F(ScalarEvolutionsTest, CompareValueComplexity) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), {IntPtrTy, IntPtrTy}, false); FunctionType::get(Type::getVoidTy(Context), {IntPtrTy, IntPtrTy}, false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F); BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
Value *X = &*F->arg_begin(); Value *X = &*F->arg_begin();
@ -435,7 +435,7 @@ TEST_F(ScalarEvolutionsTest, SCEVAddExpr) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), ArgTys, false); FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
Argument *A1 = &*F->arg_begin(); Argument *A1 = &*F->arg_begin();
Argument *A2 = &*(std::next(F->arg_begin())); Argument *A2 = &*(std::next(F->arg_begin()));
@ -669,7 +669,7 @@ TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExpr) {
// ret void // ret void
// } // }
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false);
Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F); BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
BasicBlock *CondBB = BasicBlock::Create(Context, "for.cond", F); BasicBlock *CondBB = BasicBlock::Create(Context, "for.cond", F);
@ -748,7 +748,7 @@ TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExprNonIntegral) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false); FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
Argument *Arg = &*F->arg_begin(); Argument *Arg = &*F->arg_begin();
@ -821,7 +821,7 @@ TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetLoop) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false); FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
BasicBlock *Top = BasicBlock::Create(Context, "top", F); BasicBlock *Top = BasicBlock::Create(Context, "top", F);
BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F); BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
@ -919,7 +919,7 @@ TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetValue) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false); FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
Argument *Arg = &*F->arg_begin(); Argument *Arg = &*F->arg_begin();
@ -979,7 +979,8 @@ TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstants) {
// ix. // ix.
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false); FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
Function *F = cast<Function>(M.getOrInsertFunction("addrecphitest", FTy)); Function *F =
Function::Create(FTy, Function::ExternalLinkage, "addrecphitest", M);
/* /*
Create IR: Create IR:
@ -1035,7 +1036,8 @@ TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstantAccum) {
SmallVector<Type *, 1> Types; SmallVector<Type *, 1> Types;
Types.push_back(Int32Ty); Types.push_back(Int32Ty);
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
Function *F = cast<Function>(M.getOrInsertFunction("addrecphitest", FTy)); Function *F =
Function::Create(FTy, Function::ExternalLinkage, "addrecphitest", M);
/* /*
Create IR: Create IR:
@ -1089,7 +1091,7 @@ TEST_F(ScalarEvolutionsTest, SCEVFoldSumOfTruncs) {
SmallVector<Type *, 1> Types; SmallVector<Type *, 1> Types;
Types.push_back(ArgTy); Types.push_back(ArgTy);
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
Function *F = cast<Function>(M.getOrInsertFunction("f", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
BasicBlock *BB = BasicBlock::Create(Context, "entry", F); BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
ReturnInst::Create(Context, nullptr, BB); ReturnInst::Create(Context, nullptr, BB);
@ -1145,7 +1147,7 @@ TEST_F(ScalarEvolutionsTest, SCEVExpanderIsSafeToExpandAt) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false); FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
BasicBlock *Top = BasicBlock::Create(Context, "top", F); BasicBlock *Top = BasicBlock::Create(Context, "top", F);
BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F); BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
@ -1206,7 +1208,7 @@ TEST_F(ScalarEvolutionsTest, SCEVExpanderNUW) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false); FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false);
Function *F = cast<Function>(M.getOrInsertFunction("func", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
Argument *Arg = &*F->arg_begin(); Argument *Arg = &*F->arg_begin();
ConstantInt *C = ConstantInt::get(Context, APInt(64, -1)); ConstantInt *C = ConstantInt::get(Context, APInt(64, -1));
@ -1258,7 +1260,7 @@ TEST_F(ScalarEvolutionsTest, SCEVExpanderNSW) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false); FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false);
Function *F = cast<Function>(M.getOrInsertFunction("func", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
Argument *Arg = &*F->arg_begin(); Argument *Arg = &*F->arg_begin();
ConstantInt *C = ConstantInt::get(Context, APInt(64, -1)); ConstantInt *C = ConstantInt::get(Context, APInt(64, -1));
@ -1308,7 +1310,7 @@ TEST_F(ScalarEvolutionsTest, SCEVCacheNUW) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false); FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false);
Function *F = cast<Function>(M.getOrInsertFunction("func", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
Argument *Arg = &*F->arg_begin(); Argument *Arg = &*F->arg_begin();
ConstantInt *C = ConstantInt::get(Context, APInt(64, -1)); ConstantInt *C = ConstantInt::get(Context, APInt(64, -1));
@ -1359,7 +1361,7 @@ TEST_F(ScalarEvolutionsTest, SCEVCacheNSW) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false); FunctionType::get(Type::getVoidTy(Context), { T_int64 }, false);
Function *F = cast<Function>(M.getOrInsertFunction("func", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
Argument *Arg = &*F->arg_begin(); Argument *Arg = &*F->arg_begin();
ConstantInt *C = ConstantInt::get(Context, APInt(64, -1)); ConstantInt *C = ConstantInt::get(Context, APInt(64, -1));
@ -1409,7 +1411,7 @@ TEST_F(ScalarEvolutionsTest, SCEVComputeExpressionSize) {
FunctionType *FTy = FunctionType *FTy =
FunctionType::get(Type::getVoidTy(Context), { T_int64, T_int64 }, false); FunctionType::get(Type::getVoidTy(Context), { T_int64, T_int64 }, false);
Function *F = cast<Function>(M.getOrInsertFunction("func", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
Argument *A = &*F->arg_begin(); Argument *A = &*F->arg_begin();
Argument *B = &*std::next(F->arg_begin()); Argument *B = &*std::next(F->arg_begin());
ConstantInt *C = ConstantInt::get(Context, APInt(64, 1)); ConstantInt *C = ConstantInt::get(Context, APInt(64, 1));

View File

@ -33,7 +33,7 @@ protected:
static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) { static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) {
auto &C = M->getContext(); auto &C = M->getContext();
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {}); FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {});
auto *F = cast<Function>(M->getOrInsertFunction(Name, FTy)); auto *F = Function::Create(FTy, Function::ExternalLinkage, Name, M);
auto *BB = BasicBlock::Create(C, "entry", F); auto *BB = BasicBlock::Create(C, "entry", F);
auto *IntType = Type::getInt32Ty(C); auto *IntType = Type::getInt32Ty(C);
auto *PtrType = Type::getInt32PtrTy(C); auto *PtrType = Type::getInt32PtrTy(C);

View File

@ -67,7 +67,7 @@ TEST_F(TargetLibraryInfoTest, InvalidProto) {
for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) { for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) {
LibFunc LF = (LibFunc)FI; LibFunc LF = (LibFunc)FI;
auto *F = cast<Function>( auto *F = cast<Function>(
M->getOrInsertFunction(TLI.getName(LF), InvalidFTy)); M->getOrInsertFunction(TLI.getName(LF), InvalidFTy).getCallee());
EXPECT_FALSE(isLibFunc(F, LF)); EXPECT_FALSE(isLibFunc(F, LF));
} }
} }

View File

@ -23,7 +23,7 @@ CFGHolder::CFGHolder(StringRef ModuleName, StringRef FunctionName)
: Context(llvm::make_unique<LLVMContext>()), : Context(llvm::make_unique<LLVMContext>()),
M(llvm::make_unique<Module>(ModuleName, *Context)) { M(llvm::make_unique<Module>(ModuleName, *Context)) {
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Context), {}, false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Context), {}, false);
F = cast<Function>(M->getOrInsertFunction(FunctionName, FTy)); F = Function::Create(FTy, Function::ExternalLinkage, FunctionName, M.get());
} }
CFGHolder::~CFGHolder() = default; CFGHolder::~CFGHolder() = default;

View File

@ -117,8 +117,9 @@ protected:
32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, ""); 32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, "");
} }
Function *getFunction(StringRef Name) { Function *getFunction(StringRef Name) {
return cast<Function>(M.getOrInsertFunction( return Function::Create(
Name, FunctionType::get(Type::getVoidTy(Context), None, false))); FunctionType::get(Type::getVoidTy(Context), None, false),
Function::ExternalLinkage, Name, M);
} }
}; };
typedef MetadataTest MDStringTest; typedef MetadataTest MDStringTest;

View File

@ -26,7 +26,7 @@ TEST(VerifierTest, Branch_i1) {
LLVMContext C; LLVMContext C;
Module M("M", C); Module M("M", C);
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
BasicBlock *Entry = BasicBlock::Create(C, "entry", F); BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
BasicBlock *Exit = BasicBlock::Create(C, "exit", F); BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
ReturnInst::Create(C, Exit); ReturnInst::Create(C, Exit);
@ -49,7 +49,7 @@ TEST(VerifierTest, InvalidRetAttribute) {
LLVMContext C; LLVMContext C;
Module M("M", C); Module M("M", C);
FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
AttributeList AS = F->getAttributes(); AttributeList AS = F->getAttributes();
F->setAttributes( F->setAttributes(
AS.addAttribute(C, AttributeList::ReturnIndex, Attribute::UWTable)); AS.addAttribute(C, AttributeList::ReturnIndex, Attribute::UWTable));
@ -67,9 +67,9 @@ TEST(VerifierTest, CrossModuleRef) {
Module M2("M2", C); Module M2("M2", C);
Module M3("M3", C); Module M3("M3", C);
FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
Function *F1 = cast<Function>(M1.getOrInsertFunction("foo1", FTy)); Function *F1 = Function::Create(FTy, Function::ExternalLinkage, "foo1", M1);
Function *F2 = cast<Function>(M2.getOrInsertFunction("foo2", FTy)); Function *F2 = Function::Create(FTy, Function::ExternalLinkage, "foo2", M2);
Function *F3 = cast<Function>(M3.getOrInsertFunction("foo3", FTy)); Function *F3 = Function::Create(FTy, Function::ExternalLinkage, "foo3", M3);
BasicBlock *Entry1 = BasicBlock::Create(C, "entry", F1); BasicBlock *Entry1 = BasicBlock::Create(C, "entry", F1);
BasicBlock *Entry3 = BasicBlock::Create(C, "entry", F3); BasicBlock *Entry3 = BasicBlock::Create(C, "entry", F3);
@ -173,8 +173,8 @@ TEST(VerifierTest, DetectInvalidDebugInfo) {
new GlobalVariable(M, Type::getInt8Ty(C), false, new GlobalVariable(M, Type::getInt8Ty(C), false,
GlobalValue::ExternalLinkage, nullptr, "g"); GlobalValue::ExternalLinkage, nullptr, "g");
auto *F = cast<Function>(M.getOrInsertFunction( auto *F = Function::Create(FunctionType::get(Type::getVoidTy(C), false),
"f", FunctionType::get(Type::getVoidTy(C), false))); Function::ExternalLinkage, "f", M);
IRBuilder<> Builder(BasicBlock::Create(C, "", F)); IRBuilder<> Builder(BasicBlock::Create(C, "", F));
Builder.CreateUnreachable(); Builder.CreateUnreachable();
F->setSubprogram(DIB.createFunction( F->setSubprogram(DIB.createFunction(