llvm.org GIT mirror llvm / 9ec60d7
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352827 91177308-0d34-0410-b5e6-96231b3b80d8 James Y Knight 5 months ago
69 changed file(s) with 901 addition(s) and 935 deletion(s). Raw diff Collapse all Expand all
34903490 Look up the specified function in the ``Module`` SymbolTable_. If it does not
34913491 exist, return ``null``.
34923492
3493 * ``Function *getOrInsertFunction(const std::string &Name, const FunctionType
3494 *T)``
3495
3496 Look up the specified function in the ``Module`` SymbolTable_. If it does not
3497 exist, add an external declaration for the function and return it.
3493 * ``FunctionCallee getOrInsertFunction(const std::string &Name,
3494 const FunctionType *T)``
3495
3496 Look up the specified function in the ``Module`` SymbolTable_. If
3497 it does not exist, add an external declaration for the function and
3498 return it. Note that the function signature already present may not
3499 match the requested signature. Thus, in order to enable the common
3500 usage of passing the result directly to EmitCall, the return type is
3501 a struct of ``{FunctionType *T, Constant *FunctionPtr}``, rather
3502 than simply the ``Function*`` with potentially an unexpected
3503 signature.
34983504
34993505 * ``std::string getTypeName(const Type *Ty)``
35003506
7171 Tys);
7272
7373 //declare i32 @getchar()
74 getchar_func = cast(module->
75 getOrInsertFunction("getchar", IntegerType::getInt32Ty(C)));
74 getchar_func =
75 module->getOrInsertFunction("getchar", IntegerType::getInt32Ty(C));
7676
7777 //declare i32 @putchar(i32)
78 putchar_func = cast(module->
79 getOrInsertFunction("putchar", IntegerType::getInt32Ty(C),
80 IntegerType::getInt32Ty(C)));
78 putchar_func = module->getOrInsertFunction(
79 "putchar", IntegerType::getInt32Ty(C), IntegerType::getInt32Ty(C));
8180
8281 //Function header
8382
8483 //define void @brainf()
85 brainf_func = cast(module->
86 getOrInsertFunction("brainf", Type::getVoidTy(C)));
84 brainf_func = module->getOrInsertFunction("brainf", Type::getVoidTy(C));
8785
8886 builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
8987
152150 "aberrormsg");
153151
154152 //declare i32 @puts(i8 *)
155 Function *puts_func = cast(module->
156 getOrInsertFunction("puts", IntegerType::getInt32Ty(C),
157 PointerType::getUnqual(IntegerType::getInt8Ty(C))));
153 FunctionCallee puts_func = module->getOrInsertFunction(
154 "puts", IntegerType::getInt32Ty(C),
155 PointerType::getUnqual(IntegerType::getInt8Ty(C)));
158156
159157 //brainf.aberror:
160158 aberrorbb = BasicBlock::Create(C, label, brainf_func);
7777 CompileFlags comflag;
7878 std::istream *in;
7979 Module *module;
80 Function *brainf_func;
81 Function *getchar_func;
82 Function *putchar_func;
80 FunctionCallee brainf_func;
81 FunctionCallee getchar_func;
82 FunctionCallee putchar_func;
8383 Value *ptr_arr;
8484 Value *ptr_arrmax;
8585 BasicBlock *endbb;
7171 //Add main function so can be fully compiled
7272 void addMainFunction(Module *mod) {
7373 //define i32 @main(i32 %argc, i8 **%argv)
74 Function *main_func = cast(mod->
75 getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
76 IntegerType::getInt32Ty(mod->getContext()),
77 PointerType::getUnqual(PointerType::getUnqual(
78 IntegerType::getInt8Ty(mod->getContext())))));
74 FunctionType *main_func_fty = FunctionType::get(
75 Type::getInt32Ty(mod->getContext()),
76 {Type::getInt32Ty(mod->getContext()),
77 Type::getInt8Ty(mod->getContext())->getPointerTo()->getPointerTo()});
78 Function *main_func =
79 Function::create(main_func_fty, Function::ExternalLinkage, "main", mod);
80
7981 {
8082 Function::arg_iterator args = main_func->arg_begin();
8183 Value *arg_0 = &*args++;
5050 static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
5151 // Create the fib function and insert it into module M. This function is said
5252 // to return an int and take an int parameter.
53 FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
54 {Type::getInt32Ty(Context)}, false);
5355 Function *FibF =
54 cast(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
55 Type::getInt32Ty(Context)));
56 Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
5657
5758 // Add a basic block to the function.
5859 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
6868 // Create the add1 function entry and insert this entry into module M. The
6969 // function will have a return type of "int" and take an argument of "int".
7070 Function *Add1F =
71 cast(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
72 Type::getInt32Ty(Context)));
71 Function::Create(FunctionType::get(Type::getInt32Ty(Context),
72 {Type::getInt32Ty(Context)}, false),
73 Function::ExternalLinkage, "add1", M);
7374
7475 // Add a basic block to the function. As before, it automatically inserts
7576 // because of the last argument.
9899 // Now we're going to create function `foo', which returns an int and takes no
99100 // arguments.
100101 Function *FooF =
101 cast(M->getOrInsertFunction("foo", Type::getInt32Ty(Context)));
102 Function::Create(FunctionType::get(Type::getInt32Ty(Context), {}, false),
103 Function::ExternalLinkage, "foo", M);
102104
103105 // Add a basic block to the FooF function.
104106 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
4848 static Function* createAdd1(Module *M) {
4949 // Create the add1 function entry and insert this entry into module M. The
5050 // function will have a return type of "int" and take an argument of "int".
51 // The '0' terminates the list of argument types.
5251 Function *Add1F =
53 cast(M->getOrInsertFunction("add1",
54 Type::getInt32Ty(M->getContext()),
55 Type::getInt32Ty(M->getContext())));
52 Function::Create(FunctionType::get(Type::getInt32Ty(Context),
53 {Type::getInt32Ty(Context)}, false),
54 Function::ExternalLinkage, "add1", M);
5655
5756 // Add a basic block to the function. As before, it automatically inserts
5857 // because of the last argument.
7978 static Function *CreateFibFunction(Module *M) {
8079 // Create the fib function and insert it into module M. This function is said
8180 // to return an int and take an int parameter.
82 Function *FibF =
83 cast(M->getOrInsertFunction("fib",
84 Type::getInt32Ty(M->getContext()),
85 Type::getInt32Ty(M->getContext())));
81 FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
82 {Type::getInt32Ty(Context)}, false);
83 Function *FibF =
84 Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
8685
8786 // Add a basic block to the function.
8887 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
2929 public:
3030 explicit IntrinsicLowering(const DataLayout &DL) : DL(DL), Warned(false) {}
3131
32 /// Add all of the prototypes that might be needed by an intrinsic lowering
33 /// implementation to be inserted into the module specified.
34 void AddPrototypes(Module &M);
35
3632 /// Replace a call to the specified intrinsic function.
3733 /// If an intrinsic function must be implemented by the code generator
3834 /// (such as va_start), this function should print a message and abort.
155155 unsigned Type::getFunctionNumParams() const {
156156 return cast(this)->getNumParams();
157157 }
158
159 /// A handy container for a FunctionType+Callee-pointer pair, which can be
160 /// passed around as a single entity. This assists in replacing the use of
161 /// PointerType::getElementType() to access the function's type, since that's
162 /// slated for removal as part of the [opaque pointer types] project.
163 class FunctionCallee {
164 public:
165 // Allow implicit conversion from types which have a getFunctionType member
166 // (e.g. Function and InlineAsm).
167 template
168 FunctionCallee(T *Fn)
169 : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
170
171 FunctionCallee(FunctionType *FnTy, Value *Callee)
172 : FnTy(FnTy), Callee(Callee) {
173 assert((FnTy == nullptr) == (Callee == nullptr));
174 }
175
176 FunctionCallee(std::nullptr_t) {}
177
178 FunctionCallee() = default;
179
180 FunctionType *getFunctionType() { return FnTy; }
181
182 Value *getCallee() { return Callee; }
183
184 explicit operator bool() { return Callee; }
185
186 private:
187 FunctionType *FnTy = nullptr;
188 Value *Callee = nullptr;
189 };
158190
159191 /// Common super class of ArrayType, StructType and VectorType.
160192 class CompositeType : public Type {
904904 Name);
905905 }
906906
907 InvokeInst *CreateInvoke(Function *Callee, BasicBlock *NormalDest,
907 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
908908 BasicBlock *UnwindDest, ArrayRef Args,
909909 ArrayRef OpBundles,
910910 const Twine &Name = "") {
911 return CreateInvoke(Callee->getFunctionType(), Callee, NormalDest,
912 UnwindDest, Args, OpBundles, Name);
913 }
914
915 InvokeInst *CreateInvoke(Function *Callee, BasicBlock *NormalDest,
911 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
912 NormalDest, UnwindDest, Args, OpBundles, Name);
913 }
914
915 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
916916 BasicBlock *UnwindDest,
917917 ArrayRef Args = None,
918918 const Twine &Name = "") {
919 return CreateInvoke(Callee->getFunctionType(), Callee, NormalDest,
920 UnwindDest, Args, Name);
919 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
920 NormalDest, UnwindDest, Args, Name);
921921 }
922922
923923 // Deprecated [opaque pointer types]
19871987 return Insert(CI, Name);
19881988 }
19891989
1990 CallInst *CreateCall(Function *Callee, ArrayRef Args = None,
1990 CallInst *CreateCall(FunctionCallee Callee, ArrayRef Args = None,
19911991 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1992 return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag);
1993 }
1994
1995 CallInst *CreateCall(Function *Callee, ArrayRef Args,
1992 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
1993 FPMathTag);
1994 }
1995
1996 CallInst *CreateCall(FunctionCallee Callee, ArrayRef Args,
19961997 ArrayRef OpBundles,
19971998 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1998 return CreateCall(Callee->getFunctionType(), Callee, Args, OpBundles, Name,
1999 FPMathTag);
1999 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2000 OpBundles, Name, FPMathTag);
20002001 }
20012002
20022003 // Deprecated [opaque pointer types]
12311231 Fn);
12321232 }
12331233
1234 /// Sets the function called, including updating the function type.
1235 void setCalledFunction(FunctionCallee Fn) {
1236 setCalledFunction(Fn.getFunctionType(), Fn.getCallee());
1237 }
1238
12341239 /// Sets the function called, including updating to the specified function
12351240 /// type.
12361241 void setCalledFunction(FunctionType *FTy, Value *Fn) {
15421542 CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd);
15431543 }
15441544
1545 static CallInst *Create(Function *Func, const Twine &NameStr = "",
1545 static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
15461546 Instruction *InsertBefore = nullptr) {
1547 return Create(Func->getFunctionType(), Func, NameStr, InsertBefore);
1548 }
1549
1550 static CallInst *Create(Function *Func, ArrayRef Args,
1547 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1548 InsertBefore);
1549 }
1550
1551 static CallInst *Create(FunctionCallee Func, ArrayRef Args,
1552 ArrayRef Bundles = None,
15511553 const Twine &NameStr = "",
15521554 Instruction *InsertBefore = nullptr) {
1553 return Create(Func->getFunctionType(), Func, Args, NameStr, InsertBefore);
1554 }
1555
1556 static CallInst *Create(Function *Func, const Twine &NameStr,
1555 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1556 NameStr, InsertBefore);
1557 }
1558
1559 static CallInst *Create(FunctionCallee Func, ArrayRef Args,
1560 const Twine &NameStr,
1561 Instruction *InsertBefore = nullptr) {
1562 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1563 InsertBefore);
1564 }
1565
1566 static CallInst *Create(FunctionCallee Func, const Twine &NameStr,
15571567 BasicBlock *InsertAtEnd) {
1558 return Create(Func->getFunctionType(), Func, NameStr, InsertAtEnd);
1559 }
1560
1561 static CallInst *Create(Function *Func, ArrayRef Args,
1568 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1569 InsertAtEnd);
1570 }
1571
1572 static CallInst *Create(FunctionCallee Func, ArrayRef Args,
15621573 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1563 return Create(Func->getFunctionType(), Func, Args, NameStr, InsertAtEnd);
1574 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1575 InsertAtEnd);
1576 }
1577
1578 static CallInst *Create(FunctionCallee Func, ArrayRef Args,
1579 ArrayRef Bundles,
1580 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1581 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1582 NameStr, InsertAtEnd);
15641583 }
15651584
15661585 // Deprecated [opaque pointer types]
37033722 NameStr, InsertAtEnd);
37043723 }
37053724
3706 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
3725 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
37073726 BasicBlock *IfException, ArrayRef Args,
37083727 const Twine &NameStr,
37093728 Instruction *InsertBefore = nullptr) {
3710 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3711 None, NameStr, InsertBefore);
3712 }
3713
3714 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
3729 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3730 IfException, Args, None, NameStr, InsertBefore);
3731 }
3732
3733 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
37153734 BasicBlock *IfException, ArrayRef Args,
37163735 ArrayRef Bundles = None,
37173736 const Twine &NameStr = "",
37183737 Instruction *InsertBefore = nullptr) {
3719 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3720 Bundles, NameStr, InsertBefore);
3721 }
3722
3723 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
3738 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3739 IfException, Args, Bundles, NameStr, InsertBefore);
3740 }
3741
3742 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
37243743 BasicBlock *IfException, ArrayRef Args,
37253744 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3726 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3727 NameStr, InsertAtEnd);
3728 }
3729
3730 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
3745 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3746 IfException, Args, NameStr, InsertAtEnd);
3747 }
3748
3749 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
37313750 BasicBlock *IfException, ArrayRef Args,
37323751 ArrayRef Bundles,
37333752 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3734 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3735 Bundles, NameStr, InsertAtEnd);
3753 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3754 IfException, Args, Bundles, NameStr, InsertAtEnd);
37363755 }
37373756
37383757 // Deprecated [opaque pointer types]
331331 /// Look up the specified function in the module symbol table. Four
332332 /// possibilities:
333333 /// 1. If it does not exist, add a prototype for the function and return it.
334 /// 2. If it exists, and has a local linkage, the existing function is
335 /// renamed and a new one is inserted.
336 /// 3. Otherwise, if the existing function has the correct prototype, return
334 /// 2. Otherwise, if the existing function has the correct prototype, return
337335 /// the existing function.
338 /// 4. Finally, the function exists but has the wrong prototype: return the
336 /// 3. Finally, the function exists but has the wrong prototype: return the
339337 /// function with a constantexpr cast to the right prototype.
340 Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
341 AttributeList AttributeList);
342
343 Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
338 ///
339 /// In all cases, the returned value is a FunctionCallee wrapper around the
340 /// 'FunctionType *T' passed in, as well as a 'Value*' either of the Function or
341 /// the bitcast to the function.
342 FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T,
343 AttributeList AttributeList);
344
345 FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T);
344346
345347 /// Look up the specified function in the module symbol table. If it does not
346348 /// exist, add a prototype for the function and return it. This function
348350 /// or a ConstantExpr BitCast of that type if the named function has a
349351 /// different type. This version of the method takes a list of
350352 /// function arguments, which makes it easier for clients to use.
351 template
352 Constant *getOrInsertFunction(StringRef Name,
353 AttributeList AttributeList,
354 Type *RetTy, ArgsTy... Args)
355 {
353 template
354 FunctionCallee getOrInsertFunction(StringRef Name,
355 AttributeList AttributeList, Type *RetTy,
356 ArgsTy... Args) {
356357 SmallVector ArgTys{Args...};
357358 return getOrInsertFunction(Name,
358359 FunctionType::get(RetTy, ArgTys, false),
360361 }
361362
362363 /// Same as above, but without the attributes.
363 template
364 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
364 template
365 FunctionCallee getOrInsertFunction(StringRef Name, Type *RetTy,
366 ArgsTy... Args) {
365367 return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
366368 }
367369
368370 // Avoid an incorrect ordering that'd otherwise compile incorrectly.
369371 template
370 Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList,
371 FunctionType *Invalid, ArgsTy... Args) = delete;
372 FunctionCallee
373 getOrInsertFunction(StringRef Name, AttributeList AttributeList,
374 FunctionType *Invalid, ArgsTy... Args) = delete;
372375
373376 /// Look up the specified function in the module symbol table. If it does not
374377 /// exist, return null.
2020 template class ArrayRef;
2121 class Module;
2222 class Function;
23 class FunctionCallee;
2324 class GlobalValue;
2425 class GlobalVariable;
2526 class Constant;
3839 void appendToGlobalDtors(Module &M, Function *F, int Priority,
3940 Constant *Data = nullptr);
4041
41 // Validate the result of Module::getOrInsertFunction called for an interface
42 // function of given sanitizer. If the instrumented module defines a function
43 // with the same name, their prototypes must match, otherwise
44 // getOrInsertFunction returns a bitcast.
45 Function *checkSanitizerInterfaceFunction(Constant *FuncOrBitcast);
46
47 Function *declareSanitizerInitFunction(Module &M, StringRef InitName,
48 ArrayRef InitArgTypes);
42 FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
43 ArrayRef InitArgTypes);
4944
5045 /// Creates sanitizer constructor function, and calls sanitizer's init
5146 /// function from it.
5247 /// \return Returns pair of pointers to constructor, and init functions
5348 /// respectively.
54 std::pair *> createSanitizerCtorAndInitFunctions(
49 std::pairCallee> createSanitizerCtorAndInitFunctions(
5550 Module &M, StringRef CtorName, StringRef InitName,
5651 ArrayRef InitArgTypes, ArrayRef InitArgs,
5752 StringRef VersionCheckName = StringRef());
6358 ///
6459 /// \return Returns pair of pointers to constructor, and init functions
6560 /// respectively.
66 std::pair *> getOrCreateSanitizerCtorAndInitFunctions(
61 std::pairCallee> getOrCreateSanitizerCtorAndInitFunctions(
6762 Module &M, StringRef CtorName, StringRef InitName,
6863 ArrayRef InitArgTypes, ArrayRef InitArgs,
69 function_ref *)> FunctionsCreatedCallback,
64 function_refCallee)> FunctionsCreatedCallback,
7065 StringRef VersionCheckName = StringRef());
7166
7267 // Creates and returns a sanitizer init function without argument if it doesn't
17531753 for (Value *Arg : Args)
17541754 ArgTys.push_back(Arg->getType());
17551755 FunctionType *FnType = FunctionType::get(ResultTy, ArgTys, false);
1756 Constant *LibcallFn =
1756 FunctionCallee LibcallFn =
17571757 M->getOrInsertFunction(TLI->getLibcallName(RTLibType), FnType, Attr);
17581758 CallInst *Call = Builder.CreateCall(LibcallFn, Args);
17591759 Call->setAttributes(Attr);
4444
4545 class DwarfEHPrepare : public FunctionPass {
4646 // RewindFunction - _Unwind_Resume or the target equivalent.
47 Constant *RewindFunction = nullptr;
47 FunctionCallee RewindFunction = nullptr;
4848
4949 DominatorTree *DT = nullptr;
5050 const TargetLowering *TLI = nullptr;
2121 #include "llvm/Support/ErrorHandling.h"
2222 #include "llvm/Support/raw_ostream.h"
2323 using namespace llvm;
24
25 template
26 static void EnsureFunctionExists(Module &M, const char *Name,
27 ArgIt ArgBegin, ArgIt ArgEnd,
28 Type *RetTy) {
29 // Insert a correctly-typed definition now.
30 std::vector ParamTys;
31 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32 ParamTys.push_back(I->getType());
33 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
34 }
35
36 static void EnsureFPIntrinsicsExist(Module &M, Function &Fn,
37 const char *FName,
38 const char *DName, const char *LDName) {
39 // Insert definitions for all the floating point types.
40 switch((int)Fn.arg_begin()->getType()->getTypeID()) {
41 case Type::FloatTyID:
42 EnsureFunctionExists(M, FName, Fn.arg_begin(), Fn.arg_end(),
43 Type::getFloatTy(M.getContext()));
44 break;
45 case Type::DoubleTyID:
46 EnsureFunctionExists(M, DName, Fn.arg_begin(), Fn.arg_end(),
47 Type::getDoubleTy(M.getContext()));
48 break;
49 case Type::X86_FP80TyID:
50 case Type::FP128TyID:
51 case Type::PPC_FP128TyID:
52 EnsureFunctionExists(M, LDName, Fn.arg_begin(), Fn.arg_end(),
53 Fn.arg_begin()->getType());
54 break;
55 }
56 }
5724
5825 /// This function is used when we want to lower an intrinsic call to a call of
5926 /// an external function. This handles hard cases such as when there was already
7037 std::vector ParamTys;
7138 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
7239 ParamTys.push_back((*I)->getType());
73 Constant* FCache = M->getOrInsertFunction(NewFn,
74 FunctionType::get(RetTy, ParamTys, false));
40 FunctionCallee FCache =
41 M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
7542
7643 IRBuilder<> Builder(CI->getParent(), CI->getIterator());
7744 SmallVector Args(ArgBegin, ArgEnd);
8956 # undef setjmp
9057 # define setjmp_undefined_for_msvc
9158 #endif
92
93 void IntrinsicLowering::AddPrototypes(Module &M) {
94 LLVMContext &Context = M.getContext();
95 for (auto &F : M)
96 if (F.isDeclaration() && !F.use_empty())
97 switch (F.getIntrinsicID()) {
98 default: break;
99 case Intrinsic::setjmp:
100 EnsureFunctionExists(M, "setjmp", F.arg_begin(), F.arg_end(),
101 Type::getInt32Ty(M.getContext()));
102 break;
103 case Intrinsic::longjmp:
104 EnsureFunctionExists(M, "longjmp", F.arg_begin(), F.arg_end(),
105 Type::getVoidTy(M.getContext()));
106 break;
107 case Intrinsic::siglongjmp:
108 EnsureFunctionExists(M, "abort", F.arg_end(), F.arg_end(),
109 Type::getVoidTy(M.getContext()));
110 break;
111 case Intrinsic::memcpy:
112 M.getOrInsertFunction("memcpy",
113 Type::getInt8PtrTy(Context),
114 Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context),
116 DL.getIntPtrType(Context));
117 break;
118 case Intrinsic::memmove:
119 M.getOrInsertFunction("memmove",
120 Type::getInt8PtrTy(Context),
121 Type::getInt8PtrTy(Context),
122 Type::getInt8PtrTy(Context),
123 DL.getIntPtrType(Context));
124 break;
125 case Intrinsic::memset:
126 M.getOrInsertFunction("memset",
127 Type::getInt8PtrTy(Context),
128 Type::getInt8PtrTy(Context),
129 Type::getInt32Ty(M.getContext()),
130 DL.getIntPtrType(Context));
131 break;
132 case Intrinsic::sqrt:
133 EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl");
134 break;
135 case Intrinsic::sin:
136 EnsureFPIntrinsicsExist(M, F, "sinf", "sin", "sinl");
137 break;
138 case Intrinsic::cos:
139 EnsureFPIntrinsicsExist(M, F, "cosf", "cos", "cosl");
140 break;
141 case Intrinsic::pow:
142 EnsureFPIntrinsicsExist(M, F, "powf", "pow", "powl");
143 break;
144 case Intrinsic::log:
145 EnsureFPIntrinsicsExist(M, F, "logf", "log", "logl");
146 break;
147 case Intrinsic::log2:
148 EnsureFPIntrinsicsExist(M, F, "log2f", "log2", "log2l");
149 break;
150 case Intrinsic::log10:
151 EnsureFPIntrinsicsExist(M, F, "log10f", "log10", "log10l");
152 break;
153 case Intrinsic::exp:
154 EnsureFPIntrinsicsExist(M, F, "expf", "exp", "expl");
155 break;
156 case Intrinsic::exp2:
157 EnsureFPIntrinsicsExist(M, F, "exp2f", "exp2", "exp2l");
158 break;
159 }
160 }
16159
16260 /// Emit the code to lower bswap of V before the specified instruction IP.
16361 static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
269269 /// Create an empty function with the given name.
270270 static Function *createDummyFunction(StringRef Name, Module &M) {
271271 auto &Context = M.getContext();
272 Function *F = cast(M.getOrInsertFunction(
273 Name, FunctionType::get(Type::getVoidTy(Context), false)));
272 Function *F =
273 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
274 Function::ExternalLinkage, Name, M);
274275 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
275276 new UnreachableInst(Context, BB);
276277 return F;
11031103
11041104 // Create the function using an IR-level function.
11051105 LLVMContext &C = M.getContext();
1106 Function *F = dyn_cast(
1107 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
1108 assert(F && "Function was null!");
1106 Function *F =
1107 Function::Create(FunctionType::get(Type::getVoidTy(C), false),
1108 Function::ExternalLinkage, NameStream.str(), M);
11091109
11101110 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
11111111 // which gives us better results when we outline from linkonceodr functions.
6363 // If we haven't already looked up this function, check to see if the
6464 // program already contains a function with this name.
6565 Module *M = F.getParent();
66 Constant* FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
67
68 if (Function* Fn = dyn_cast(FCache)) {
66 FunctionCallee FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
67
68 if (Function *Fn = dyn_cast(FCache.getCallee())) {
6969 Fn->setLinkage(F.getLinkage());
7070 if (setNonLazyBind && !Fn->isWeakForLinker()) {
7171 // If we have Native ARC, set nonlazybind attribute for these APIs for
473473 /* Unreachable */ true, Weights);
474474 IRBuilder<> IRBFail(CheckTerm);
475475 // FIXME: respect -fsanitize-trap / -ftrap-function here?
476 Constant *StackChkFail = F.getParent()->getOrInsertFunction(
477 "__stack_chk_fail", IRB.getVoidTy());
476 FunctionCallee StackChkFail =
477 F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
478478 IRBFail.CreateCall(StackChkFail, {});
479479 }
480480
781781 if (DISubprogram *SP = F.getSubprogram())
782782 IRB.SetCurrentDebugLocation(DebugLoc::get(SP->getScopeLine(), 0, SP));
783783 if (SafeStackUsePointerAddress) {
784 Value *Fn = F.getParent()->getOrInsertFunction(
784 FunctionCallee Fn = F.getParent()->getOrInsertFunction(
785785 "__safestack_pointer_address", StackPtrTy->getPointerTo(0));
786786 UnsafeStackPtr = IRB.CreateCall(Fn);
787787 } else {
3838 Type *doubleUnderDataTy;
3939 Type *doubleUnderJBufTy;
4040 Type *FunctionContextTy;
41 Constant *RegisterFn;
42 Constant *UnregisterFn;
43 Constant *BuiltinSetupDispatchFn;
44 Constant *FrameAddrFn;
45 Constant *StackAddrFn;
46 Constant *StackRestoreFn;
47 Constant *LSDAAddrFn;
48 Constant *CallSiteFn;
49 Constant *FuncCtxFn;
41 FunctionCallee RegisterFn;
42 FunctionCallee UnregisterFn;
43 Function *BuiltinSetupDispatchFn;
44 Function *FrameAddrFn;
45 Function *StackAddrFn;
46 Function *StackRestoreFn;
47 Function *LSDAAddrFn;
48 Function *CallSiteFn;
49 Function *FuncCtxFn;
5050 AllocaInst *FuncCtx;
5151
5252 public:
498498 IRBuilder<> B(FailBB);
499499 B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
500500 if (Trip.isOSOpenBSD()) {
501 Constant *StackChkFail =
502 M->getOrInsertFunction("__stack_smash_handler",
503 Type::getVoidTy(Context),
504 Type::getInt8PtrTy(Context));
501 FunctionCallee StackChkFail = M->getOrInsertFunction(
502 "__stack_smash_handler", Type::getVoidTy(Context),
503 Type::getInt8PtrTy(Context));
505504
506505 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
507506 } else {
508 Constant *StackChkFail =
507 FunctionCallee StackChkFail =
509508 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
510509
511510 B.CreateCall(StackChkFail, {});
15861586 // thread's unsafe stack pointer.
15871587 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
15881588 Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
1589 Value *Fn = M->getOrInsertFunction("__safestack_pointer_address",
1590 StackPtrTy->getPointerTo(0));
1589 FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
1590 StackPtrTy->getPointerTo(0));
15911591 return IRB.CreateCall(Fn);
15921592 }
15931593
110110 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
111111 Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic
112112 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
113 Function *CallPersonalityF = nullptr; // _Unwind_CallPersonality() wrapper
113 FunctionCallee CallPersonalityF =
114 nullptr; // _Unwind_CallPersonality() wrapper
114115
115116 bool prepareEHPads(Function &F);
116117 bool prepareThrows(Function &F);
251252 Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception);
252253
253254 // _Unwind_CallPersonality() wrapper function, which calls the personality
254 CallPersonalityF = cast(M.getOrInsertFunction(
255 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()));
256 CallPersonalityF->setDoesNotThrow();
255 CallPersonalityF = M.getOrInsertFunction(
256 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
257 if (Function *F = dyn_cast(CallPersonalityF.getCallee()))
258 F->setDoesNotThrow();
257259
258260 unsigned Index = 0;
259261 for (auto *BB : CatchPads) {
10171017 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef Tys) {
10181018 // There can never be multiple globals with the same name of different types,
10191019 // because intrinsics must be a specific type.
1020 return
1021 cast(M->getOrInsertFunction(getName(id, Tys),
1022 getType(M->getContext(), id, Tys)));
1020 return cast(
1021 M->getOrInsertFunction(getName(id, Tys),
1022 getType(M->getContext(), id, Tys))
1023 .getCallee());
10231024 }
10241025
10251026 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
516516 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
517517 Module *M = BB->getParent()->getParent();
518518 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
519 Value *MallocFunc = MallocF;
519 FunctionCallee MallocFunc = MallocF;
520520 if (!MallocFunc)
521521 // prototype malloc as "void *malloc(size_t)"
522522 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
540540 }
541541 }
542542 MCall->setTailCall();
543 if (Function *F = dyn_cast(MallocFunc)) {
543 if (Function *F = dyn_cast(MallocFunc.getCallee())) {
544544 MCall->setCallingConv(F->getCallingConv());
545545 if (!F->returnDoesNotAlias())
546546 F->setReturnDoesNotAlias();
613613 Type *VoidTy = Type::getVoidTy(M->getContext());
614614 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
615615 // prototype free as "void free(void*)"
616 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
616 FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
617617 CallInst *Result = nullptr;
618618 Value *PtrCast = Source;
619619 if (InsertBefore) {
626626 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
627627 }
628628 Result->setTailCall();
629 if (Function *F = dyn_cast(FreeFunc))
629 if (Function *F = dyn_cast(FreeFunc.getCallee()))
630630 Result->setCallingConv(F->getCallingConv());
631631
632632 return Result;
139139 // it. This is nice because it allows most passes to get away with not handling
140140 // the symbol table directly for this common task.
141141 //
142 Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
143 AttributeList AttributeList) {
142 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
143 AttributeList AttributeList) {
144144 // See if we have a definition for the specified function already.
145145 GlobalValue *F = getNamedValue(Name);
146146 if (!F) {
150150 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
151151 New->setAttributes(AttributeList);
152152 FunctionList.push_back(New);
153 return New; // Return the new prototype.
153 return {Ty, New}; // Return the new prototype.
154154 }
155155
156156 // If the function exists but has the wrong type, return a bitcast to the
157157 // right type.
158158 auto *PTy = PointerType::get(Ty, F->getAddressSpace());
159159 if (F->getType() != PTy)
160 return ConstantExpr::getBitCast(F, PTy);
160 return {Ty, ConstantExpr::getBitCast(F, PTy)};
161161
162162 // Otherwise, we just found the existing function or a prototype.
163 return F;
164 }
165
166 Constant *Module::getOrInsertFunction(StringRef Name,
167 FunctionType *Ty) {
163 return {Ty, F};
164 }
165
166 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
168167 return getOrInsertFunction(Name, Ty, AttributeList());
169168 }
170169
1174711747 Type::getInt8PtrTy(M.getContext()));
1174811748
1174911749 // MSVC CRT has a function to validate security cookie.
11750 auto *SecurityCheckCookie = cast(
11751 M.getOrInsertFunction("__security_check_cookie",
11752 Type::getVoidTy(M.getContext()),
11753 Type::getInt8PtrTy(M.getContext())));
11754 SecurityCheckCookie->setCallingConv(CallingConv::Win64);
11755 SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
11750 FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
11751 "__security_check_cookie", Type::getVoidTy(M.getContext()),
11752 Type::getInt8PtrTy(M.getContext()));
11753 if (Function *F = dyn_cast(SecurityCheckCookie.getCallee())) {
11754 F->setCallingConv(CallingConv::Win64);
11755 F->addAttribute(1, Attribute::AttrKind::InReg);
11756 }
1175611757 return;
1175711758 }
1175811759 TargetLowering::insertSSPDeclarations(M);
7171
7272 // Return a pointer (pointer expr) to the function if function defintion with
7373 // "FuncName" exists. It may create a new function prototype in pre-link mode.
74 Constant *getFunction(Module *M, const FuncInfo& fInfo);
74 FunctionCallee getFunction(Module *M, const FuncInfo &fInfo);
7575
7676 // Replace a normal function with its native version.
7777 bool replaceWithNative(CallInst *CI, const FuncInfo &FInfo);
138138 // Insert an Alloc instruction.
139139 AllocaInst* insertAlloca(CallInst * UI, IRBuilder<> &B, const char *prefix);
140140 // Get a scalar native builtin signle argument FP function
141 Constant* getNativeFunction(Module* M, const FuncInfo &FInfo);
141 FunctionCallee getNativeFunction(Module *M, const FuncInfo &FInfo);
142142
143143 protected:
144144 CallInst *CI;
215215 false, false)
216216
217217 template
218 static CallInst *CreateCallEx(IRB &B, Value *Callee, Value *Arg,
218 static CallInst *CreateCallEx(IRB &B, FunctionCallee Callee, Value *Arg,
219219 const Twine &Name = "") {
220220 CallInst *R = B.CreateCall(Callee, Arg, Name);
221 if (Function* F = dyn_cast(Callee))
221 if (Function *F = dyn_cast(Callee.getCallee()))
222222 R->setCallingConv(F->getCallingConv());
223223 return R;
224224 }
225225
226226 template
227 static CallInst *CreateCallEx2(IRB &B, Value *Callee, Value *Arg1, Value *Arg2,
228 const Twine &Name = "") {
227 static CallInst *CreateCallEx2(IRB &B, FunctionCallee Callee, Value *Arg1,
228 Value *Arg2, const Twine &Name = "") {
229229 CallInst *R = B.CreateCall(Callee, {Arg1, Arg2}, Name);
230 if (Function* F = dyn_cast(Callee))
230 if (Function *F = dyn_cast(Callee.getCallee()))
231231 R->setCallingConv(F->getCallingConv());
232232 return R;
233233 }
470470 return (AMDGPULibFunc::EType)FInfo.getLeads()[0].ArgType;
471471 }
472472
473 Constant *AMDGPULibCalls::getFunction(Module *M, const FuncInfo& fInfo) {
473 FunctionCallee AMDGPULibCalls::getFunction(Module *M, const FuncInfo &fInfo) {
474474 // If we are doing PreLinkOpt, the function is external. So it is safe to
475475 // use getOrInsertFunction() at this stage.
476476
517517
518518 nf.setPrefix(AMDGPULibFunc::NATIVE);
519519 nf.setId(AMDGPULibFunc::EI_SIN);
520 Constant *sinExpr = getFunction(M, nf);
520 FunctionCallee sinExpr = getFunction(M, nf);
521521
522522 nf.setPrefix(AMDGPULibFunc::NATIVE);
523523 nf.setId(AMDGPULibFunc::EI_COS);
524 Constant *cosExpr = getFunction(M, nf);
524 FunctionCallee cosExpr = getFunction(M, nf);
525525 if (sinExpr && cosExpr) {
526526 Value *sinval = CallInst::Create(sinExpr, opr0, "splitsin", aCI);
527527 Value *cosval = CallInst::Create(cosExpr, opr0, "splitcos", aCI);
553553 return sincosUseNative(aCI, FInfo);
554554
555555 FInfo.setPrefix(AMDGPULibFunc::NATIVE);
556 Constant *F = getFunction(aCI->getModule(), FInfo);
556 FunctionCallee F = getFunction(aCI->getModule(), FInfo);
557557 if (!F)
558558 return false;
559559
611611 auto *FTy = FunctionType::get(Callee->getReturnType(),
612612 ArrayRef(ArgTys), false);
613613 AMDGPULibFunc NewLibFunc(Name, FTy);
614 auto *F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc);
614 FunctionCallee F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc);
615615 if (!F)
616616 return false;
617617
793793
794794 AMDGPULibFunc nf = FInfo;
795795 nf.setPrefix(AMDGPULibFunc::NATIVE);
796 if (Constant *FPExpr = getFunction(M, nf)) {
796 if (FunctionCallee FPExpr = getFunction(M, nf)) {
797797 LLVM_DEBUG(dbgs() << "AMDIC: " << *CI << " ---> ");
798798
799799 CI->setCalledFunction(FPExpr);
932932 if (CF && (CF->isExactlyValue(0.5) || CF->isExactlyValue(-0.5))) {
933933 // pow[r](x, [-]0.5) = sqrt(x)
934934 bool issqrt = CF->isExactlyValue(0.5);
935 if (Constant *FPExpr = getFunction(M,
936 AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
937 : AMDGPULibFunc::EI_RSQRT, FInfo))) {
935 if (FunctionCallee FPExpr =
936 getFunction(M, AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
937 : AMDGPULibFunc::EI_RSQRT,
938 FInfo))) {
938939 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
939940 << FInfo.getName().c_str() << "(" << *opr0 << ")\n");
940941 Value *nval = CreateCallEx(B,FPExpr, opr0, issqrt ? "__pow2sqrt"
10011002
10021003 // powr ---> exp2(y * log2(x))
10031004 // pown/pow ---> powr(fabs(x), y) | (x & ((int)y << 31))
1004 Constant *ExpExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2,
1005 FInfo));
1005 FunctionCallee ExpExpr =
1006 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2, FInfo));
10061007 if (!ExpExpr)
10071008 return false;
10081009
10881089
10891090 Value *nval;
10901091 if (needabs) {
1091 Constant *AbsExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS,
1092 FInfo));
1092 FunctionCallee AbsExpr =
1093 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS, FInfo));
10931094 if (!AbsExpr)
10941095 return false;
10951096 nval = CreateCallEx(B, AbsExpr, opr0, "__fabs");
10971098 nval = cnval ? cnval : opr0;
10981099 }
10991100 if (needlog) {
1100 Constant *LogExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2,
1101 FInfo));
1101 FunctionCallee LogExpr =
1102 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2, FInfo));
11021103 if (!LogExpr)
11031104 return false;
11041105 nval = CreateCallEx(B,LogExpr, nval, "__log2");
11571158 std::vector ParamsTys;
11581159 ParamsTys.push_back(opr0->getType());
11591160 Module *M = CI->getModule();
1160 if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT,
1161 FInfo))) {
1161 if (FunctionCallee FPExpr =
1162 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
11621163 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> sqrt(" << *opr0 << ")\n");
11631164 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2sqrt");
11641165 replaceCall(nval);
11661167 }
11671168 } else if (ci_opr1 == 3) { // rootn(x, 3) = cbrt(x)
11681169 Module *M = CI->getModule();
1169 if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT,
1170 FInfo))) {
1170 if (FunctionCallee FPExpr =
1171 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT, FInfo))) {
11711172 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> cbrt(" << *opr0 << ")\n");
11721173 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2cbrt");
11731174 replaceCall(nval);
11841185 std::vector ParamsTys;
11851186 ParamsTys.push_back(opr0->getType());
11861187 Module *M = CI->getModule();
1187 if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT,
1188 FInfo))) {
1188 if (FunctionCallee FPExpr =
1189 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT, FInfo))) {
11891190 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> rsqrt(" << *opr0
11901191 << ")\n");
11911192 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2rsqrt");
12411242 }
12421243
12431244 // Get a scalar native builtin signle argument FP function
1244 Constant* AMDGPULibCalls::getNativeFunction(Module* M, const FuncInfo& FInfo) {
1245 FunctionCallee AMDGPULibCalls::getNativeFunction(Module *M,
1246 const FuncInfo &FInfo) {
12451247 if (getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()))
12461248 return nullptr;
12471249 FuncInfo nf = FInfo;
12541256 const FuncInfo &FInfo) {
12551257 if (getArgType(FInfo) == AMDGPULibFunc::F32 && (getVecSize(FInfo) == 1) &&
12561258 (FInfo.getPrefix() != AMDGPULibFunc::NATIVE)) {
1257 if (Constant *FPExpr = getNativeFunction(
1258 CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
1259 if (FunctionCallee FPExpr = getNativeFunction(
1260 CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
12591261 Value *opr0 = CI->getArgOperand(0);
12601262 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
12611263 << "sqrt(" << *opr0 << ")\n");
13321334 // function.
13331335 AMDGPULibFunc nf(AMDGPULibFunc::EI_SINCOS, fInfo);
13341336 nf.getLeads()[0].PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AMDGPUAS::FLAT_ADDRESS);
1335 Function *Fsincos = dyn_cast_or_null(getFunction(M, nf));
1337 FunctionCallee Fsincos = getFunction(M, nf);
13361338 if (!Fsincos) return false;
13371339
13381340 BasicBlock::iterator ItOld = B.GetInsertPoint();
13401342 B.SetInsertPoint(UI);
13411343
13421344 Value *P = Alloc;
1343 Type *PTy = Fsincos->getFunctionType()->getParamType(1);
1345 Type *PTy = Fsincos.getFunctionType()->getParamType(1);
13441346 // The allocaInst allocates the memory in private address space. This need
13451347 // to be bitcasted to point to the address space of cos pointer type.
13461348 // In OpenCL 2.0 this is generic, while in 1.2 that is private.
959959 return nullptr;
960960 }
961961
962 Function *AMDGPULibFunc::getOrInsertFunction(Module *M,
963 const AMDGPULibFunc &fInfo) {
962 FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
963 const AMDGPULibFunc &fInfo) {
964964 std::string const FuncName = fInfo.mangle();
965965 Function *F = dyn_cast_or_null(
966966 M->getValueSymbolTable().lookup(FuncName));
986986 }
987987 }
988988
989 Constant *C = nullptr;
989 FunctionCallee C;
990990 if (hasPtr) {
991991 // Do not set extra attributes for functions with pointer arguments.
992992 C = M->getOrInsertFunction(FuncName, FuncTy);
10001000 C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
10011001 }
10021002
1003 return cast(C);
1003 return C;
10041004 }
10051005
10061006 bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
392392 }
393393 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
394394
395 static Function *getOrInsertFunction(llvm::Module *M,
396 const AMDGPULibFunc &fInfo);
395 static FunctionCallee getOrInsertFunction(llvm::Module *M,
396 const AMDGPULibFunc &fInfo);
397397 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
398398
399399 private:
22522252 Type *Int32PtrTy = Type::getInt32PtrTy(Ctx);
22532253 Type *VoidTy = Type::getVoidTy(Ctx);
22542254 Module *M = Func->getParent();
2255 Constant *CF = M->getOrInsertFunction(HexagonVolatileMemcpyName, VoidTy,
2256 Int32PtrTy, Int32PtrTy, Int32Ty);
2257 Function *Fn = cast(CF);
2258 Fn->setLinkage(Function::ExternalLinkage);
2255 FunctionCallee Fn = M->getOrInsertFunction(
2256 HexagonVolatileMemcpyName, VoidTy, Int32PtrTy, Int32PtrTy, Int32Ty);
22592257
22602258 const SCEV *OneS = SE->getConstant(Int32Ty, 1);
22612259 const SCEV *BECount32 = SE->getTruncateOrZeroExtend(BECount, Int32Ty);
413413 Attribute::ReadNone);
414414 A = A.addAttribute(C, AttributeList::FunctionIndex,
415415 Attribute::NoInline);
416 Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T));
416 FunctionCallee F = (M->getOrInsertFunction(Name, A, MyVoid, T));
417417 CallInst::Create(F, Params, "", &I);
418418 } else if (const CallInst *CI = dyn_cast(&I)) {
419419 FunctionType *FT = CI->getFunctionType();
108108 FunctionType::get(Type::getVoidTy(C), AtExitFuncArgs,
109109 /*isVarArg=*/false);
110110
111 Type *AtExitArgs[] = {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar};
112 FunctionType *AtExitTy = FunctionType::get(Type::getInt32Ty(C), AtExitArgs,
113 /*isVarArg=*/false);
114 Constant *AtExit = M.getOrInsertFunction("__cxa_atexit", AtExitTy);
111 FunctionCallee AtExit = M.getOrInsertFunction(
112 "__cxa_atexit",
113 FunctionType::get(Type::getInt32Ty(C),
114 {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar},
115 /*isVarArg=*/false));
115116
116117 // Declare __dso_local.
117118 Constant *DsoHandle = M.getNamedValue("__dso_handle");
22782278 Type::getInt8PtrTy(M.getContext()));
22792279
22802280 // MSVC CRT has a function to validate security cookie.
2281 auto *SecurityCheckCookie = cast(
2282 M.getOrInsertFunction("__security_check_cookie",
2283 Type::getVoidTy(M.getContext()),
2284 Type::getInt8PtrTy(M.getContext())));
2285 SecurityCheckCookie->setCallingConv(CallingConv::X86_FastCall);
2286 SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
2281 FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
2282 "__security_check_cookie", Type::getVoidTy(M.getContext()),
2283 Type::getInt8PtrTy(M.getContext()));
2284 if (Function *F = dyn_cast(SecurityCheckCookie.getCallee())) {
2285 F->setCallingConv(CallingConv::X86_FastCall);
2286 F->addAttribute(1, Attribute::AttrKind::InReg);
2287 }
22872288 return;
22882289 }
22892290 // glibc, bionic, and Fuchsia have a special slot for the stack guard.
8585 StructType *EHLinkRegistrationTy = nullptr;
8686 StructType *CXXEHRegistrationTy = nullptr;
8787 StructType *SEHRegistrationTy = nullptr;
88 Constant *SetJmp3 = nullptr;
89 Constant *CxxLongjmpUnwind = nullptr;
88 FunctionCallee SetJmp3 = nullptr;
89 FunctionCallee CxxLongjmpUnwind = nullptr;
9090
9191 // Per-function state
9292 EHPersonality Personality = EHPersonality::Unknown;
9393 Function *PersonalityFn = nullptr;
9494 bool UseStackGuard = false;
9595 int ParentBaseState;
96 Constant *SehLongjmpUnwind = nullptr;
96 FunctionCallee SehLongjmpUnwind = nullptr;
9797 Constant *Cookie = nullptr;
9898
9999 /// The stack allocation containing all EH data, including the link in the
302302 CxxLongjmpUnwind = TheModule->getOrInsertFunction(
303303 "__CxxLongjmpUnwind",
304304 FunctionType::get(VoidTy, Int8PtrType, /*isVarArg=*/false));
305 cast(CxxLongjmpUnwind->stripPointerCasts())
305 cast(CxxLongjmpUnwind.getCallee()->stripPointerCasts())
306306 ->setCallingConv(CallingConv::X86_StdCall);
307307 } else if (Personality == EHPersonality::MSVC_X86SEH) {
308308 // If _except_handler4 is in use, some additional guard checks and prologue
355355 UseStackGuard ? "_seh_longjmp_unwind4" : "_seh_longjmp_unwind",
356356 FunctionType::get(Type::getVoidTy(TheModule->getContext()), Int8PtrType,
357357 /*isVarArg=*/false));
358 cast(SehLongjmpUnwind->stripPointerCasts())
358 cast(SehLongjmpUnwind.getCallee()->stripPointerCasts())
359359 ->setCallingConv(CallingConv::X86_StdCall);
360360 } else {
361361 llvm_unreachable("unexpected personality function");
470470
471471 SmallVector OptionalArgs;
472472 if (Personality == EHPersonality::MSVC_CXX) {
473 OptionalArgs.push_back(CxxLongjmpUnwind);
473 OptionalArgs.push_back(CxxLongjmpUnwind.getCallee());
474474 OptionalArgs.push_back(State);
475475 OptionalArgs.push_back(emitEHLSDA(Builder, &F));
476476 } else if (Personality == EHPersonality::MSVC_X86SEH) {
477 OptionalArgs.push_back(SehLongjmpUnwind);
477 OptionalArgs.push_back(SehLongjmpUnwind.getCallee());
478478 OptionalArgs.push_back(State);
479479 if (UseStackGuard)
480480 OptionalArgs.push_back(Cookie);
765765 if (!CS)
766766 continue;
767767 if (CS.getCalledValue()->stripPointerCasts() !=
768 SetJmp3->stripPointerCasts())
768 SetJmp3.getCallee()->stripPointerCasts())
769769 continue;
770770
771771 SetJmp3CallSites.push_back(CS);
104104 }
105105
106106 LLVMContext &Ctx = M.getContext();
107 Constant *C = M.getOrInsertFunction(
107 FunctionCallee C = M.getOrInsertFunction(
108108 "__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
109109 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
110 Function *F = dyn_cast(C);
110 Function *F = dyn_cast(C.getCallee());
111111 // Take over the existing function. The frontend emits a weak stub so that the
112112 // linker knows about the symbol; this pass replaces the function body.
113113 F->deleteBody();
131131
132132 BasicBlock *TrapBB = BasicBlock::Create(Ctx, "fail", F);
133133 IRBuilder<> IRBFail(TrapBB);
134 Constant *CFICheckFailFn = M.getOrInsertFunction(
135 "__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx),
136 Type::getInt8PtrTy(Ctx));
134 FunctionCallee CFICheckFailFn =
135 M.getOrInsertFunction("__cfi_check_fail", Type::getVoidTy(Ctx),
136 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
137137 IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr});
138138 IRBFail.CreateBr(ExitBB);
139139
14931493 if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) {
14941494 // The type of the function in the declaration is irrelevant because every
14951495 // call site will cast it to the correct type.
1496 auto *SingleImpl = M.getOrInsertFunction(
1497 Res.SingleImplName, Type::getVoidTy(M.getContext()));
1496 Constant *SingleImpl =
1497 cast(M.getOrInsertFunction(Res.SingleImplName,
1498 Type::getVoidTy(M.getContext()))
1499 .getCallee());
14981500
14991501 // This is the import phase so we should not be exporting anything.
15001502 bool IsExported = false;
15361538 }
15371539
15381540 if (Res.TheKind == WholeProgramDevirtResolution::BranchFunnel) {
1539 auto *JT = M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
1540 Type::getVoidTy(M.getContext()));
1541 // The type of the function is irrelevant, because it's bitcast at calls
1542 // anyhow.
1543 Constant *JT = cast(
1544 M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
1545 Type::getVoidTy(M.getContext()))
1546 .getCallee());
15411547 bool IsExported = false;
15421548 applyICallBranchFunnel(SlotInfo, JT, IsExported);
15431549 assert(!IsExported);
714714 Type *IntptrTy;
715715 ShadowMapping Mapping;
716716 DominatorTree *DT;
717 Function *AsanHandleNoReturnFunc;
718 Function *AsanPtrCmpFunction, *AsanPtrSubFunction;
717 FunctionCallee AsanHandleNoReturnFunc;
718 FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
719719 Constant *AsanShadowGlobal;
720720
721721 // These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize).
722 Function *AsanErrorCallback[2][2][kNumberOfAccessSizes];
723 Function *AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
722 FunctionCallee AsanErrorCallback[2][2][kNumberOfAccessSizes];
723 FunctionCallee AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
724724
725725 // These arrays is indexed by AccessIsWrite and Experiment.
726 Function *AsanErrorCallbackSized[2][2];
727 Function *AsanMemoryAccessCallbackSized[2][2];
728
729 Function *AsanMemmove, *AsanMemcpy, *AsanMemset;
726 FunctionCallee AsanErrorCallbackSized[2][2];
727 FunctionCallee AsanMemoryAccessCallbackSized[2][2];
728
729 FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
730730 InlineAsm *EmptyAsm;
731731 Value *LocalDynamicShadow = nullptr;
732732 GlobalsMetadata GlobalsMD;
808808 LLVMContext *C;
809809 Triple TargetTriple;
810810 ShadowMapping Mapping;
811 Function *AsanPoisonGlobals;
812 Function *AsanUnpoisonGlobals;
813 Function *AsanRegisterGlobals;
814 Function *AsanUnregisterGlobals;
815 Function *AsanRegisterImageGlobals;
816 Function *AsanUnregisterImageGlobals;
817 Function *AsanRegisterElfGlobals;
818 Function *AsanUnregisterElfGlobals;
811 FunctionCallee AsanPoisonGlobals;
812 FunctionCallee AsanUnpoisonGlobals;
813 FunctionCallee AsanRegisterGlobals;
814 FunctionCallee AsanUnregisterGlobals;
815 FunctionCallee AsanRegisterImageGlobals;
816 FunctionCallee AsanUnregisterImageGlobals;
817 FunctionCallee AsanRegisterElfGlobals;
818 FunctionCallee AsanUnregisterElfGlobals;
819819
820820 Function *AsanCtorFunction = nullptr;
821821 Function *AsanDtorFunction = nullptr;
844844 SmallVector RetVec;
845845 unsigned StackAlignment;
846846
847 Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
848 *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
849 Function *AsanSetShadowFunc[0x100] = {};
850 Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
851 Function *AsanAllocaPoisonFunc, *AsanAllocasUnpoisonFunc;
847 FunctionCallee AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
848 AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
849 FunctionCallee AsanSetShadowFunc[0x100] = {};
850 FunctionCallee AsanPoisonStackMemoryFunc, AsanUnpoisonStackMemoryFunc;
851 FunctionCallee AsanAllocaPoisonFunc, AsanAllocasUnpoisonFunc;
852852
853853 // Stores a place and arguments of poisoning/unpoisoning call for alloca.
854854 struct AllocaPoisonCall {
13321332 void AddressSanitizer::instrumentPointerComparisonOrSubtraction(
13331333 Instruction *I) {
13341334 IRBuilder<> IRB(I);
1335 Function *F = isa(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
1335 FunctionCallee F = isa(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
13361336 Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
13371337 for (Value *&i : Param) {
13381338 if (i->getType()->isPointerTy())
17941794 IRBuilder<> IRB(*C);
17951795
17961796 // Declare our poisoning and unpoisoning functions.
1797 AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1798 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy));
1799 AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
1800 AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1801 kAsanUnpoisonGlobalsName, IRB.getVoidTy()));
1802 AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
1797 AsanPoisonGlobals =
1798 M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy);
1799 AsanUnpoisonGlobals =
1800 M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy());
18031801
18041802 // Declare functions that register/unregister globals.
1805 AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1806 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy));
1807 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
1808 AsanUnregisterGlobals = checkSanitizerInterfaceFunction(
1809 M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
1810 IntptrTy, IntptrTy));
1811 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
1803 AsanRegisterGlobals = M.getOrInsertFunction(
1804 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
1805 AsanUnregisterGlobals = M.getOrInsertFunction(
1806 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
18121807
18131808 // Declare the functions that find globals in a shared object and then invoke
18141809 // the (un)register function on them.
1815 AsanRegisterImageGlobals =
1816 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1817 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
1818 AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
1819
1820 AsanUnregisterImageGlobals =
1821 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1822 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
1823 AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
1824
1825 AsanRegisterElfGlobals = checkSanitizerInterfaceFunction(
1810 AsanRegisterImageGlobals = M.getOrInsertFunction(
1811 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
1812 AsanUnregisterImageGlobals = M.getOrInsertFunction(
1813 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
1814
1815 AsanRegisterElfGlobals =
18261816 M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(),
1827 IntptrTy, IntptrTy, IntptrTy));
1828 AsanRegisterElfGlobals->setLinkage(Function::ExternalLinkage);
1829
1830 AsanUnregisterElfGlobals = checkSanitizerInterfaceFunction(
1817 IntptrTy, IntptrTy, IntptrTy);
1818 AsanUnregisterElfGlobals =
18311819 M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(),
1832 IntptrTy, IntptrTy, IntptrTy));
1833 AsanUnregisterElfGlobals->setLinkage(Function::ExternalLinkage);
1820 IntptrTy, IntptrTy, IntptrTy);
18341821 }
18351822
18361823 // Put the metadata and the instrumented global in the same group. This ensures
23442331 Args2.push_back(ExpType);
23452332 Args1.push_back(ExpType);
23462333 }
2347 AsanErrorCallbackSized[AccessIsWrite][Exp] =
2348 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2349 kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr,
2350 FunctionType::get(IRB.getVoidTy(), Args2, false)));
2351
2352 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
2353 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2354 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
2355 FunctionType::get(IRB.getVoidTy(), Args2, false)));
2334 AsanErrorCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
2335 kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr,
2336 FunctionType::get(IRB.getVoidTy(), Args2, false));
2337
2338 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
2339 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
2340 FunctionType::get(IRB.getVoidTy(), Args2, false));
23562341
23572342 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
23582343 AccessSizeIndex++) {
23592344 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
23602345 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2361 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2346 M.getOrInsertFunction(
23622347 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
2363 FunctionType::get(IRB.getVoidTy(), Args1, false)));
2348 FunctionType::get(IRB.getVoidTy(), Args1, false));
23642349
23652350 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2366 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2351 M.getOrInsertFunction(
23672352 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
2368 FunctionType::get(IRB.getVoidTy(), Args1, false)));
2353 FunctionType::get(IRB.getVoidTy(), Args1, false));
23692354 }
23702355 }
23712356 }
23722357
23732358 const std::string MemIntrinCallbackPrefix =
23742359 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
2375 AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2376 MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
2377 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
2378 AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2379 MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
2380 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
2381 AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2382 MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
2383 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
2384
2385 AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction(
2386 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy()));
2387
2388 AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2389 kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy));
2390 AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2391 kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy));
2360 AsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
2361 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2362 IRB.getInt8PtrTy(), IntptrTy);
2363 AsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
2364 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2365 IRB.getInt8PtrTy(), IntptrTy);
2366 AsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
2367 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2368 IRB.getInt32Ty(), IntptrTy);
2369
2370 AsanHandleNoReturnFunc =
2371 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy());
2372
2373 AsanPtrCmpFunction =
2374 M.getOrInsertFunction(kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy);
2375 AsanPtrSubFunction =
2376 M.getOrInsertFunction(kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy);
23922377 // We insert an empty inline asm after __asan_report* to avoid callback merge.
23932378 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
23942379 StringRef(""), StringRef(""),
24262411 // We cannot just ignore these methods, because they may call other
24272412 // instrumented functions.
24282413 if (F.getName().find(" load]") != std::string::npos) {
2429 Function *AsanInitFunction =
2414 FunctionCallee AsanInitFunction =
24302415 declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {});
24312416 IRBuilder<> IRB(&F.front(), F.front().begin());
24322417 IRB.CreateCall(AsanInitFunction, {});
26412626 IRBuilder<> IRB(*C);
26422627 for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
26432628 std::string Suffix = itostr(i);
2644 AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
2645 M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
2646 IntptrTy));
2647 AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
2629 AsanStackMallocFunc[i] = M.getOrInsertFunction(
2630 kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy);
2631 AsanStackFreeFunc[i] =
26482632 M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
2649 IRB.getVoidTy(), IntptrTy, IntptrTy));
2633 IRB.getVoidTy(), IntptrTy, IntptrTy);
26502634 }
26512635 if (ASan.UseAfterScope) {
2652 AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
2653 M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
2654 IntptrTy, IntptrTy));
2655 AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
2656 M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
2657 IntptrTy, IntptrTy));
2636 AsanPoisonStackMemoryFunc = M.getOrInsertFunction(
2637 kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2638 AsanUnpoisonStackMemoryFunc = M.getOrInsertFunction(
2639 kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
26582640 }
26592641
26602642 for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
26622644 Name << kAsanSetShadowPrefix;
26632645 Name << std::setw(2) << std::setfill('0') << std::hex << Val;
26642646 AsanSetShadowFunc[Val] =
2665 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2666 Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy));
2667 }
2668
2669 AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2670 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
2671 AsanAllocasUnpoisonFunc =
2672 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2673 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
2647 M.getOrInsertFunction(Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy);
2648 }
2649
2650 AsanAllocaPoisonFunc = M.getOrInsertFunction(
2651 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
2652 AsanAllocasUnpoisonFunc = M.getOrInsertFunction(
2653 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
26742654 }
26752655
26762656 void FunctionStackPoisoner::copyToShadowInline(ArrayRef ShadowMask,
340340 FunctionType *DFSanSetLabelFnTy;
341341 FunctionType *DFSanNonzeroLabelFnTy;
342342 FunctionType *DFSanVarargWrapperFnTy;
343 Constant *DFSanUnionFn;
344 Constant *DFSanCheckedUnionFn;
345 Constant *DFSanUnionLoadFn;
346 Constant *DFSanUnimplementedFn;
347 Constant *DFSanSetLabelFn;
348 Constant *DFSanNonzeroLabelFn;
349 Constant *DFSanVarargWrapperFn;
343 FunctionCallee DFSanUnionFn;
344 FunctionCallee DFSanCheckedUnionFn;
345 FunctionCallee DFSanUnionLoadFn;
346 FunctionCallee DFSanUnimplementedFn;
347 FunctionCallee DFSanSetLabelFn;
348 FunctionCallee DFSanNonzeroLabelFn;
349 FunctionCallee DFSanVarargWrapperFn;
350350 MDNode *ColdCallWeights;
351351 DFSanABIList ABIList;
352352 DenseMap UnwrappedFnMap;
676676 Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
677677 StringRef FName) {
678678 FunctionType *FTT = getTrampolineFunctionType(FT);
679 Constant *C = Mod->getOrInsertFunction(FName, FTT);
680 Function *F = dyn_cast(C);
679 FunctionCallee C = Mod->getOrInsertFunction(FName, FTT);
680 Function *F = dyn_cast(C.getCallee());
681681 if (F && F->isDeclaration()) {
682682 F->setLinkage(GlobalValue::LinkOnceODRLinkage);
683683 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
702702 &*std::prev(F->arg_end()), RI);
703703 }
704704
705 return C;
705 return cast(C.getCallee());
706706 }
707707
708708 bool DataFlowSanitizer::runOnModule(Module &M) {
724724 ExternalShadowMask =
725725 Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy);
726726
727 DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
728 if (Function *F = dyn_cast(DFSanUnionFn)) {
729 F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
730 F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
731 F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
732 F->addParamAttr(0, Attribute::ZExt);
733 F->addParamAttr(1, Attribute::ZExt);
734 }
735 DFSanCheckedUnionFn = Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy);
736 if (Function *F = dyn_cast(DFSanCheckedUnionFn)) {
737 F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
738 F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
739 F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
740 F->addParamAttr(0, Attribute::ZExt);
741 F->addParamAttr(1, Attribute::ZExt);
742 }
743 DFSanUnionLoadFn =
744 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
745 if (Function *F = dyn_cast(DFSanUnionLoadFn)) {
746 F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
747 F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
748 F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
727 {
728 AttributeList AL;
729 AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
730 Attribute::NoUnwind);
731 AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
732 Attribute::ReadNone);
733 AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex,
734 Attribute::ZExt);
735 AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
736 AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
737 DFSanUnionFn =
738 Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy, AL);
739 }
740
741 {
742 AttributeList AL;
743 AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
744 Attribute::NoUnwind);
745 AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
746 Attribute::ReadNone);
747 AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex,
748 Attribute::ZExt);
749 AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
750 AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
751 DFSanCheckedUnionFn =
752 Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy, AL);
753 }
754 {
755 AttributeList AL;
756 AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
757 Attribute::NoUnwind);
758 AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex,
759 Attribute::ReadOnly);
760 AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex,
761 Attribute::ZExt);
762 DFSanUnionLoadFn =
763 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy, AL);
749764 }
750765 DFSanUnimplementedFn =
751766 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
752 DFSanSetLabelFn =
753 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
754 if (Function *F = dyn_cast(DFSanSetLabelFn)) {
755 F->addParamAttr(0, Attribute::ZExt);
767 {
768 AttributeList AL;
769 AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
770 DFSanSetLabelFn =
771 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy, AL);
756772 }
757773 DFSanNonzeroLabelFn =
758774 Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
763779 SmallPtrSet FnsWithNativeABI;
764780 for (Function &i : M) {
765781 if (!i.isIntrinsic() &&
766 &i != DFSanUnionFn &&
767 &i != DFSanCheckedUnionFn &&
768 &i != DFSanUnionLoadFn &&
769 &i != DFSanUnimplementedFn &&
770 &i != DFSanSetLabelFn &&
771 &i != DFSanNonzeroLabelFn &&
772 &i != DFSanVarargWrapperFn)
782 &i != DFSanUnionFn.getCallee()->stripPointerCasts() &&
783 &i != DFSanCheckedUnionFn.getCallee()->stripPointerCasts() &&
784 &i != DFSanUnionLoadFn.getCallee()->stripPointerCasts() &&
785 &i != DFSanUnimplementedFn.getCallee()->stripPointerCasts() &&
786 &i != DFSanSetLabelFn.getCallee()->stripPointerCasts() &&
787 &i != DFSanNonzeroLabelFn.getCallee()->stripPointerCasts() &&
788 &i != DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
773789 FnsToInstrument.push_back(&i);
774790 }
775791
15111527
15121528 // Calls to this function are synthesized in wrappers, and we shouldn't
15131529 // instrument them.
1514 if (F == DFSF.DFS.DFSanVarargWrapperFn)
1530 if (F == DFSF.DFS.DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
15151531 return;
15161532
15171533 IRBuilder<> IRB(CS.getInstruction());
15441560 TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT);
15451561 std::string CustomFName = "__dfsw_";
15461562 CustomFName += F->getName();
1547 Constant *CustomF = DFSF.DFS.Mod->getOrInsertFunction(
1563 FunctionCallee CustomF = DFSF.DFS.Mod->getOrInsertFunction(
15481564 CustomFName, CustomFn.TransformedType);
1549 if (Function *CustomFn = dyn_cast(CustomF)) {
1565 if (Function *CustomFn = dyn_cast(CustomF.getCallee())) {
15501566 CustomFn->copyAttributesFrom(F);
15511567
15521568 // Custom functions returning non-void will write to the return label.
201201 // Our slowpath involves callouts to the runtime library.
202202 // Access sizes are powers of two: 1, 2, 4, 8, 16.
203203 static const size_t NumberOfAccessSizes = 5;
204 Function *EsanAlignedLoad[NumberOfAccessSizes];
205 Function *EsanAlignedStore[NumberOfAccessSizes];
206 Function *EsanUnalignedLoad[NumberOfAccessSizes];
207 Function *EsanUnalignedStore[NumberOfAccessSizes];
204 FunctionCallee EsanAlignedLoad[NumberOfAccessSizes];
205 FunctionCallee EsanAlignedStore[NumberOfAccessSizes];
206 FunctionCallee EsanUnalignedLoad[NumberOfAccessSizes];
207 FunctionCallee EsanUnalignedStore[NumberOfAccessSizes];
208208 // For irregular sizes of any alignment:
209 Function *EsanUnalignedLoadN, *EsanUnalignedStoreN;
210 Function *MemmoveFn, *MemcpyFn, *MemsetFn;
209 FunctionCallee EsanUnalignedLoadN, EsanUnalignedStoreN;
210 FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
211211 Function *EsanCtorFunction;
212212 Function *EsanDtorFunction;
213213 // Remember the counter variable for each struct type to avoid
248248 // We'll inline the most common (i.e., aligned and frequent sizes)
249249 // load + store instrumentation: these callouts are for the slowpath.
250250 SmallString<32> AlignedLoadName("__esan_aligned_load" + ByteSizeStr);
251 EsanAlignedLoad[Idx] =
252 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
253 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
251 EsanAlignedLoad[Idx] = M.getOrInsertFunction(
252 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy());
254253 SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr);
255 EsanAlignedStore[Idx] =
256 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
257 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
254 EsanAlignedStore[Idx] = M.getOrInsertFunction(
255 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy());
258256 SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr);
259 EsanUnalignedLoad[Idx] =
260 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
261 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
257 EsanUnalignedLoad[Idx] = M.getOrInsertFunction(
258 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy());
262259 SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr);
263 EsanUnalignedStore[Idx] =
264 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
265 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
266 }
267 EsanUnalignedLoadN = checkSanitizerInterfaceFunction(
268 M.getOrInsertFunction("__esan_unaligned_loadN", IRB.getVoidTy(),
269 IRB.getInt8PtrTy(), IntptrTy));
270 EsanUnalignedStoreN = checkSanitizerInterfaceFunction(
271 M.getOrInsertFunction("__esan_unaligned_storeN", IRB.getVoidTy(),
272 IRB.getInt8PtrTy(), IntptrTy));
273 MemmoveFn = checkSanitizerInterfaceFunction(
260 EsanUnalignedStore[Idx] = M.getOrInsertFunction(
261 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy());
262 }
263 EsanUnalignedLoadN = M.getOrInsertFunction(
264 "__esan_unaligned_loadN", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy);
265 EsanUnalignedStoreN = M.getOrInsertFunction(
266 "__esan_unaligned_storeN", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy);
267 MemmoveFn =
274268 M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
275 IRB.getInt8PtrTy(), IntptrTy));
276 MemcpyFn = checkSanitizerInterfaceFunction(
269 IRB.getInt8PtrTy(), IntptrTy);
270 MemcpyFn =
277271 M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
278 IRB.getInt8PtrTy(), IntptrTy));
279 MemsetFn = checkSanitizerInterfaceFunction(
272 IRB.getInt8PtrTy(), IntptrTy);
273 MemsetFn =
280274 M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
281 IRB.getInt32Ty(), IntptrTy));
275 IRB.getInt32Ty(), IntptrTy);
282276 }
283277
284278 bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
509503 EsanModuleDtorName, &M);
510504 ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
511505 IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
512 Function *EsanExit = checkSanitizerInterfaceFunction(
513 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
514 Int8PtrTy));
515 EsanExit->setLinkage(Function::ExternalLinkage);
506 FunctionCallee EsanExit =
507 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(), Int8PtrTy);
516508 IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
517509 appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
518510 }
668660
669661 Type *OrigTy = cast(Addr->getType())->getElementType();
670662 const uint32_t TypeSizeBytes = DL.getTypeStoreSizeInBits(OrigTy) / 8;
671 Value *OnAccessFunc = nullptr;
663 FunctionCallee OnAccessFunc = nullptr;
672664
673665 // Convert 0 to the default alignment.
674666 if (Alignment == 0)
101101 std::vector &Regexes);
102102
103103 // Get pointers to the functions in the runtime library.
104 Constant *getStartFileFunc();
105 Constant *getEmitFunctionFunc();
106 Constant *getEmitArcsFunc();
107 Constant *getSummaryInfoFunc();
108 Constant *getEndFileFunc();
104 FunctionCallee getStartFileFunc();
105 FunctionCallee getEmitFunctionFunc();
106 FunctionCallee getEmitArcsFunc();
107 FunctionCallee getSummaryInfoFunc();
108 FunctionCallee getEndFileFunc();
109109
110110 // Add the function to write out all our counters to the global destructor
111111 // list.
646646 for (auto I : ForkAndExecs) {
647647 IRBuilder<> Builder(I);
648648 FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false);
649 Constant *GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy);
649 FunctionCallee GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy);
650650 Builder.CreateCall(GCOVFlush);
651651 I->getParent()->splitBasicBlock(I);
652652 }
862862
863863 // Initialize the environment and register the local writeout and flush
864864 // functions.
865 Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
865 FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
866866 Builder.CreateCall(GCOVInit, {WriteoutF, FlushF});
867867 Builder.CreateRetVoid();
868868
872872 return Result;
873873 }
874874
875 Constant *GCOVProfiler::getStartFileFunc() {
875 FunctionCallee GCOVProfiler::getStartFileFunc() {
876876 Type *Args[] = {
877877 Type::getInt8PtrTy(*Ctx), // const char *orig_filename
878878 Type::getInt8PtrTy(*Ctx), // const char version[4]
879879 Type::getInt32Ty(*Ctx), // uint32_t checksum
880880 };
881881 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
882 auto *Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy);
883 if (Function *FunRes = dyn_cast(Res))
884 if (auto AK = TLI->getExtAttrForI32Param(false))
885 FunRes->addParamAttr(2, AK);
882 AttributeList AL;
883 if (auto AK = TLI->getExtAttrForI32Param(false))
884 AL = AL.addParamAttribute(*Ctx, 2, AK);
885 FunctionCallee Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy, AL);
886886 return Res;
887
888 }
889
890 Constant *GCOVProfiler::getEmitFunctionFunc() {
887 }
888
889 FunctionCallee GCOVProfiler::getEmitFunctionFunc() {
891890 Type *Args[] = {
892891 Type::getInt32Ty(*Ctx), // uint32_t ident
893892 Type::getInt8PtrTy(*Ctx), // const char *function_name
896895 Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
897896 };
898897 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
899 auto *Res = M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
900 if (Function *FunRes = dyn_cast(Res))
901 if (auto AK = TLI->getExtAttrForI32Param(false)) {
902 FunRes->addParamAttr(0, AK);
903 FunRes->addParamAttr(2, AK);
904 FunRes->addParamAttr(3, AK);
905 FunRes->addParamAttr(4, AK);
906 }
907 return Res;
908 }
909
910 Constant *GCOVProfiler::getEmitArcsFunc() {
898 AttributeList AL;
899 if (auto AK = TLI->getExtAttrForI32Param(false)) {
900 AL = AL.addParamAttribute(*Ctx, 0, AK);
901 AL = AL.addParamAttribute(*Ctx, 2, AK);
902 AL = AL.addParamAttribute(*Ctx, 3, AK);
903 AL = AL.addParamAttribute(*Ctx, 4, AK);
904 }
905 return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
906 }
907
908 FunctionCallee GCOVProfiler::getEmitArcsFunc() {
911909 Type *Args[] = {
912910 Type::getInt32Ty(*Ctx), // uint32_t num_counters
913911 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
914912 };
915913 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
916 auto *Res = M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
917 if (Function *FunRes = dyn_cast(Res))
918 if (auto AK = TLI->getExtAttrForI32Param(false))
919 FunRes->addParamAttr(0, AK);
920 return Res;
921 }
922
923 Constant *GCOVProfiler::getSummaryInfoFunc() {
914 AttributeList AL;
915 if (auto AK = TLI->getExtAttrForI32Param(false))
916 AL = AL.addParamAttribute(*Ctx, 0, AK);
917 return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy, AL);
918 }
919
920 FunctionCallee GCOVProfiler::getSummaryInfoFunc() {
924921 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
925922 return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
926923 }
927924
928 Constant *GCOVProfiler::getEndFileFunc() {
925 FunctionCallee GCOVProfiler::getEndFileFunc() {
929926 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
930927 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
931928 }
945942 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
946943 IRBuilder<> Builder(BB);
947944
948 Constant *StartFile = getStartFileFunc();
949 Constant *EmitFunction = getEmitFunctionFunc();
950 Constant *EmitArcs = getEmitArcsFunc();
951 Constant *SummaryInfo = getSummaryInfoFunc();
952 Constant *EndFile = getEndFileFunc();
945 FunctionCallee StartFile = getStartFileFunc();
946 FunctionCallee EmitFunction = getEmitFunctionFunc();
947 FunctionCallee EmitArcs = getEmitArcsFunc();
948 FunctionCallee SummaryInfo = getSummaryInfoFunc();
949 FunctionCallee EndFile = getEndFileFunc();
953950
954951 NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu");
955952 if (!CUNodes) {
220220 LLVMContext *C;
221221 std::string CurModuleUniqueId;
222222 Triple TargetTriple;
223 Function *HWAsanMemmove, *HWAsanMemcpy, *HWAsanMemset;
223 FunctionCallee HWAsanMemmove, HWAsanMemcpy, HWAsanMemset;
224224
225225 // Frame description is a way to pass names/sizes of local variables
226226 // to the run-time w/o adding extra executable code in every function.
269269
270270 Function *HwasanCtorFunction;
271271
272 Function *HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
273 Function *HwasanMemoryAccessCallbackSized[2];
274
275 Function *HwasanTagMemoryFunc;
276 Function *HwasanGenerateTagFunc;
277 Function *HwasanThreadEnterFunc;
272 FunctionCallee HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
273 FunctionCallee HwasanMemoryAccessCallbackSized[2];
274
275 FunctionCallee HwasanTagMemoryFunc;
276 FunctionCallee HwasanGenerateTagFunc;
277 FunctionCallee HwasanThreadEnterFunc;
278278
279279 Constant *ShadowGlobal;
280280
368368 const std::string TypeStr = AccessIsWrite ? "store" : "load";
369369 const std::string EndingStr = Recover ? "_noabort" : "";
370370
371 HwasanMemoryAccessCallbackSized[AccessIsWrite] =
372 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
373 ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
374 FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false)));
371 HwasanMemoryAccessCallbackSized[AccessIsWrite] = M.getOrInsertFunction(
372 ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
373 FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false));
375374
376375 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
377376 AccessSizeIndex++) {
378377 HwasanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] =
379 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
378 M.getOrInsertFunction(
380379 ClMemoryAccessCallbackPrefix + TypeStr +
381380 itostr(1ULL << AccessSizeIndex) + EndingStr,
382 FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false)));
381 FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false));
383382 }
384383 }
385384
386 HwasanTagMemoryFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
387 "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy));
388 HwasanGenerateTagFunc = checkSanitizerInterfaceFunction(
389 M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty));
385 HwasanTagMemoryFunc = M.getOrInsertFunction(
386 "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy);
387 HwasanGenerateTagFunc =
388 M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty);
390389
391390 ShadowGlobal = M.getOrInsertGlobal("__hwasan_shadow",
392391 ArrayType::get(IRB.getInt8Ty(), 0));
393392
394393 const std::string MemIntrinCallbackPrefix =
395394 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
396 HWAsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
397 MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
398 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
399 HWAsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
400 MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
401 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
402 HWAsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
403 MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
404 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
405
406 HwasanThreadEnterFunc = checkSanitizerInterfaceFunction(
407 M.getOrInsertFunction("__hwasan_thread_enter", IRB.getVoidTy()));
395 HWAsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
396 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
397 IRB.getInt8PtrTy(), IntptrTy);
398 HWAsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
399 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
400 IRB.getInt8PtrTy(), IntptrTy);
401 HWAsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
402 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
403 IRB.getInt32Ty(), IntptrTy);
404
405 HwasanThreadEnterFunc =
406 M.getOrInsertFunction("__hwasan_thread_enter", IRB.getVoidTy());
408407 }
409408
410409 Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {
507507 return true;
508508 }
509509
510 static Constant *getOrInsertValueProfilingCall(Module &M,
511 const TargetLibraryInfo &TLI,
512 bool IsRange = false) {
510 static FunctionCallee
511 getOrInsertValueProfilingCall(Module &M, const TargetLibraryInfo &TLI,
512 bool IsRange = false) {
513513 LLVMContext &Ctx = M.getContext();
514514 auto *ReturnTy = Type::getVoidTy(M.getContext());
515515
516 Constant *Res;
516 AttributeList AL;
517 if (auto AK = TLI.getExtAttrForI32Param(false))
518 AL = AL.addParamAttribute(M.getContext(), 2, AK);
519
517520 if (!IsRange) {
518521 Type *ParamTypes[] = {
519522 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
521524 };
522525 auto *ValueProfilingCallTy =
523526 FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
524 Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(),
525 ValueProfilingCallTy);
527 return M.getOrInsertFunction(getInstrProfValueProfFuncName(),
528 ValueProfilingCallTy, AL);
526529 } else {
527530 Type *RangeParamTypes[] = {
528531 #define VALUE_RANGE_PROF 1
532535 };
533536 auto *ValueRangeProfilingCallTy =
534537 FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false);
535 Res = M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(),
536 ValueRangeProfilingCallTy);
537 }
538
539 if (Function *FunRes = dyn_cast(Res)) {
540 if (auto AK = TLI.getExtAttrForI32Param(false))
541 FunRes->addParamAttr(2, AK);
542 }
543 return Res;
538 return M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(),
539 ValueRangeProfilingCallTy, AL);
540 }
544541 }
545542
546543 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
535535 bool CallbacksInitialized = false;
536536
537537 /// The run-time callback to print a warning.
538 Value *WarningFn;
538 FunctionCallee WarningFn;
539539
540540 // These arrays are indexed by log2(AccessSize).
541 Value *MaybeWarningFn[kNumberOfAccessSizes];
542 Value *MaybeStoreOriginFn[kNumberOfAccessSizes];
541 FunctionCallee MaybeWarningFn[kNumberOfAccessSizes];
542 FunctionCallee MaybeStoreOriginFn[kNumberOfAccessSizes];
543543
544544 /// Run-time helper that generates a new origin value for a stack
545545 /// allocation.
546 Value *MsanSetAllocaOrigin4Fn;
546 FunctionCallee MsanSetAllocaOrigin4Fn;
547547
548548 /// Run-time helper that poisons stack on function entry.
549 Value *MsanPoisonStackFn;
549 FunctionCallee MsanPoisonStackFn;
550550
551551 /// Run-time helper that records a store (or any event) of an
552552 /// uninitialized value and returns an updated origin id encoding this info.
553 Value *MsanChainOriginFn;
553 FunctionCallee MsanChainOriginFn;
554554
555555 /// MSan runtime replacements for memmove, memcpy and memset.
556 Value *MemmoveFn, *MemcpyFn, *MemsetFn;
556 FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
557557
558558 /// KMSAN callback for task-local function argument shadow.
559 Value *MsanGetContextStateFn;
559 FunctionCallee MsanGetContextStateFn;
560560
561561 /// Functions for poisoning/unpoisoning local variables
562 Value *MsanPoisonAllocaFn, *MsanUnpoisonAllocaFn;
562 FunctionCallee MsanPoisonAllocaFn, MsanUnpoisonAllocaFn;
563563
564564 /// Each of the MsanMetadataPtrXxx functions returns a pair of shadow/origin
565565 /// pointers.
566 Value *MsanMetadataPtrForLoadN, *MsanMetadataPtrForStoreN;
567 Value *MsanMetadataPtrForLoad_1_8[4];
568 Value *MsanMetadataPtrForStore_1_8[4];
569 Value *MsanInstrumentAsmStoreFn;
566 FunctionCallee MsanMetadataPtrForLoadN, MsanMetadataPtrForStoreN;
567 FunctionCallee MsanMetadataPtrForLoad_1_8[4];
568 FunctionCallee MsanMetadataPtrForStore_1_8[4];
569 FunctionCallee MsanInstrumentAsmStoreFn;
570570
571571 /// Helper to choose between different MsanMetadataPtrXxx().
572 Value *getKmsanShadowOriginAccessFn(bool isStore, int size);
572 FunctionCallee getKmsanShadowOriginAccessFn(bool isStore, int size);
573573
574574 /// Memory map parameters used in application-to-shadow calculation.
575575 const MemoryMapParams *MapParams;
822822 CallbacksInitialized = true;
823823 }
824824
825 Value *MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore, int size) {
826 Value **Fns =
825 FunctionCallee MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore,
826 int size) {
827 FunctionCallee *Fns =
827828 isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8;
828829 switch (size) {
829830 case 1:
923924 /*InitArgs=*/{},
924925 // This callback is invoked when the functions are created the first
925926 // time. Hook them into the global ctors list in that case:
926 [&](Function *Ctor, Function *) {
927 [&](Function *Ctor, FunctionCallee) {
927928 if (!ClWithComdat) {
928929 appendToGlobalCtors(M, Ctor, 0);
929930 return;
11221123 DL.getTypeSizeInBits(ConvertedShadow->getType());
11231124 unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
11241125 if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
1125 Value *Fn = MS.MaybeStoreOriginFn[SizeIndex];
1126 FunctionCallee Fn = MS.MaybeStoreOriginFn[SizeIndex];
11261127 Value *ConvertedShadow2 = IRB.CreateZExt(
11271128 ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
11281129 IRB.CreateCall(Fn, {ConvertedShadow2,
12041205 unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
12051206 unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
12061207 if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
1207 Value *Fn = MS.MaybeWarningFn[SizeIndex];
1208 FunctionCallee Fn = MS.MaybeWarningFn[SizeIndex];
12081209 Value *ConvertedShadow2 =
12091210 IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
12101211 IRB.CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
14111412 const DataLayout &DL = F.getParent()->getDataLayout();
14121413 int Size = DL.getTypeStoreSize(ShadowTy);
14131414
1414 Value *Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
1415 FunctionCallee Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
14151416 Value *AddrCast =
14161417 IRB.CreatePointerCast(Addr, PointerType::get(IRB.getInt8Ty(), 0));
14171418 if (Getter) {
221221 std::string getSectionName(const std::string &Section) const;
222222 std::string getSectionStart(const std::string &Section) const;
223223 std::string getSectionEnd(const std::string &Section) const;
224 Function *SanCovTracePCIndir;
225 Function *SanCovTracePC, *SanCovTracePCGuard;
226 Function *SanCovTraceCmpFunction[4];
227 Function *SanCovTraceConstCmpFunction[4];
228 Function *SanCovTraceDivFunction[2];
229 Function *SanCovTraceGepFunction;
230 Function *SanCovTraceSwitchFunction;
224 FunctionCallee SanCovTracePCIndir;
225 FunctionCallee SanCovTracePC, SanCovTracePCGuard;
226 FunctionCallee SanCovTraceCmpFunction[4];
227 FunctionCallee SanCovTraceConstCmpFunction[4];
228 FunctionCallee SanCovTraceDivFunction[2];
229 FunctionCallee SanCovTraceGepFunction;
230 FunctionCallee SanCovTraceSwitchFunction;
231231 GlobalVariable *SanCovLowestStack;
232232 InlineAsm *EmptyAsm;
233233 Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
327327 Int16Ty = IRB.getInt16Ty();
328328 Int8Ty = IRB.getInt8Ty();
329329
330 SanCovTracePCIndir = checkSanitizerInterfaceFunction(
331 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
330 SanCovTracePCIndir =
331 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy);
332 // Make sure smaller parameters are zero-extended to i64 as required by the
333 // x86_64 ABI.
334 AttributeList SanCovTraceCmpZeroExtAL;
335 if (TargetTriple.getArch() == Triple::x86_64) {
336 SanCovTraceCmpZeroExtAL =
337 SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 0, Attribute::ZExt);
338 SanCovTraceCmpZeroExtAL =
339 SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 1, Attribute::ZExt);
340 }
341
332342 SanCovTraceCmpFunction[0] =
333 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
334 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
335 SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
336 M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
337 IRB.getInt16Ty()));
338 SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
339 M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
340 IRB.getInt32Ty()));
343 M.getOrInsertFunction(SanCovTraceCmp1, SanCovTraceCmpZeroExtAL, VoidTy,
344 IRB.getInt8Ty(), IRB.getInt8Ty());
345 SanCovTraceCmpFunction[1] =
346 M.getOrInsertFunction(SanCovTraceCmp2, SanCovTraceCmpZeroExtAL, VoidTy,
347 IRB.getInt16Ty(), IRB.getInt16Ty());
348 SanCovTraceCmpFunction[2] =
349 M.getOrInsertFunction(SanCovTraceCmp4, SanCovTraceCmpZeroExtAL, VoidTy,
350 IRB.getInt32Ty(), IRB.getInt32Ty());
341351 SanCovTraceCmpFunction[3] =
342 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
343 SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
344
345 SanCovTraceConstCmpFunction[0] =
346 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
347 SanCovTraceConstCmp1, VoidTy, Int8Ty, Int8Ty));
348 SanCovTraceConstCmpFunction[1] =
349 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
350 SanCovTraceConstCmp2, VoidTy, Int16Ty, Int16Ty));
351 SanCovTraceConstCmpFunction[2] =
352 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
353 SanCovTraceConstCmp4, VoidTy, Int32Ty, Int32Ty));
352 M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty);
353
354 SanCovTraceConstCmpFunction[0] = M.getOrInsertFunction(
355 SanCovTraceConstCmp1, SanCovTraceCmpZeroExtAL, VoidTy, Int8Ty, Int8Ty);
356 SanCovTraceConstCmpFunction[1] = M.getOrInsertFunction(
357 SanCovTraceConstCmp2, SanCovTraceCmpZeroExtAL, VoidTy, Int16Ty, Int16Ty);
358 SanCovTraceConstCmpFunction[2] = M.getOrInsertFunction(
359 SanCovTraceConstCmp4, SanCovTraceCmpZeroExtAL, VoidTy, Int32Ty, Int32Ty);
354360 SanCovTraceConstCmpFunction[3] =
355 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
356 SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty));
357
358 SanCovTraceDivFunction[0] =
359 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
360 SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
361 M.getOrInsertFunction(SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty);
362
363 {
364 AttributeList AL;
365 if (TargetTriple.getArch() == Triple::x86_64)
366 AL = AL.addParamAttribute(*C, 0, Attribute::ZExt);
367 SanCovTraceDivFunction[0] =
368 M.getOrInsertFunction(SanCovTraceDiv4, AL, VoidTy, IRB.getInt32Ty());
369 }
361370 SanCovTraceDivFunction[1] =
362 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
363 SanCovTraceDiv8, VoidTy, Int64Ty));
371 M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty);
364372 SanCovTraceGepFunction =
365 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
366 SanCovTraceGep, VoidTy, IntptrTy));
373 M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy);
367374 SanCovTraceSwitchFunction =
368 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
369 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
375 M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy);
370376
371377 Constant *SanCovLowestStackConstant =
372378 M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
376382 if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
377383 SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
378384
379 // Make sure smaller parameters are zero-extended to i64 as required by the
380 // x86_64 ABI.
381 if (TargetTriple.getArch() == Triple::x86_64) {
382 for (int i = 0; i < 3; i++) {
383 SanCovTraceCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
384 SanCovTraceCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
385 SanCovTraceConstCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
386 SanCovTraceConstCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
387 }
388 SanCovTraceDivFunction[0]->addParamAttr(0, Attribute::ZExt);
389 }
390
391
392385 // We insert an empty inline asm after cov callbacks to avoid callback merge.
393386 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
394387 StringRef(""), StringRef(""),
395388 /*hasSideEffects=*/true);
396389
397 SanCovTracePC = checkSanitizerInterfaceFunction(
398 M.getOrInsertFunction(SanCovTracePCName, VoidTy));
399 SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
400 SanCovTracePCGuardName, VoidTy, Int32PtrTy));
390 SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy);
391 SanCovTracePCGuard =
392 M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy);
401393
402394 for (auto &F : M)
403395 runOnFunction(F);
412404 SanCovCountersSectionName);
413405 if (Ctor && Options.PCTable) {
414406 auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
415 Function *InitFunction = declareSanitizerInitFunction(
407 FunctionCallee InitFunction = declareSanitizerInitFunction(
416408 M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
417409 IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
418410 IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
109109 Type *IntptrTy;
110110 IntegerType *OrdTy;
111111 // Callbacks to run-time library are computed in doInitialization.
112 Function *TsanFuncEntry;
113 Function *TsanFuncExit;
114 Function *TsanIgnoreBegin;
115 Function *TsanIgnoreEnd;
112 FunctionCallee TsanFuncEntry;
113 FunctionCallee TsanFuncExit;
114 FunctionCallee TsanIgnoreBegin;
115 FunctionCallee TsanIgnoreEnd;
116116 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
117117 static const size_t kNumberOfAccessSizes = 5;
118 Function *TsanRead[kNumberOfAccessSizes];
119 Function *TsanWrite[kNumberOfAccessSizes];
120 Function *TsanUnalignedRead[kNumberOfAccessSizes];
121 Function *TsanUnalignedWrite[kNumberOfAccessSizes];
122 Function *TsanAtomicLoad[kNumberOfAccessSizes];
123 Function *TsanAtomicStore[kNumberOfAccessSizes];
124 Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes];
125 Function *TsanAtomicCAS[kNumberOfAccessSizes];
126 Function *TsanAtomicThreadFence;
127 Function *TsanAtomicSignalFence;
128 Function *TsanVptrUpdate;
129 Function *TsanVptrLoad;
130 Function *MemmoveFn, *MemcpyFn, *MemsetFn;
118 FunctionCallee TsanRead[kNumberOfAccessSizes];
119 FunctionCallee TsanWrite[kNumberOfAccessSizes];
120 FunctionCallee TsanUnalignedRead[kNumberOfAccessSizes];
121 FunctionCallee TsanUnalignedWrite[kNumberOfAccessSizes];
122 FunctionCallee TsanAtomicLoad[kNumberOfAccessSizes];
123 FunctionCallee TsanAtomicStore[kNumberOfAccessSizes];
124 FunctionCallee TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1]
125 [kNumberOfAccessSizes];
126 FunctionCallee TsanAtomicCAS[kNumberOfAccessSizes];
127 FunctionCallee TsanAtomicThreadFence;
128 FunctionCallee TsanAtomicSignalFence;
129 FunctionCallee TsanVptrUpdate;
130 FunctionCallee TsanVptrLoad;
131 FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
131132 Function *TsanCtorFunction;
132133 };
133134
187188 Attr = Attr.addAttribute(M.getContext(), AttributeList::FunctionIndex,
188189 Attribute::NoUnwind);
189190 // Initialize the callbacks.
190 TsanFuncEntry = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
191 "__tsan_func_entry", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
192 TsanFuncExit = checkSanitizerInterfaceFunction(
193 M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy()));
194 TsanIgnoreBegin = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
195 "__tsan_ignore_thread_begin", Attr, IRB.getVoidTy()));
196 TsanIgnoreEnd = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
197 "__tsan_ignore_thread_end", Attr, IRB.getVoidTy()));
191 TsanFuncEntry = M.getOrInsertFunction("__tsan_func_entry", Attr,
192 IRB.getVoidTy(), IRB.getInt8PtrTy());
193 TsanFuncExit =
194 M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy());
195 TsanIgnoreBegin = M.getOrInsertFunction("__tsan_ignore_thread_begin", Attr,
196 IRB.getVoidTy());
197 TsanIgnoreEnd =
198 M.getOrInsertFunction("__tsan_ignore_thread_end", Attr, IRB.getVoidTy());
198199 OrdTy = IRB.getInt32Ty();
199200 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
200201 const unsigned ByteSize = 1U << i;
202203 std::string ByteSizeStr = utostr(ByteSize);
203204 std::string BitSizeStr = utostr(BitSize);
204205 SmallString<32> ReadName("__tsan_read" + ByteSizeStr);
205 TsanRead[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
206 ReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
206 TsanRead[i] = M.getOrInsertFunction(ReadName, Attr, IRB.getVoidTy(),
207 IRB.getInt8PtrTy());
207208
208209 SmallString<32> WriteName("__tsan_write" + ByteSizeStr);
209 TsanWrite[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
210 WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
210 TsanWrite[i] = M.getOrInsertFunction(WriteName, Attr, IRB.getVoidTy(),
211 IRB.getInt8PtrTy());
211212
212213 SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr);
213 TsanUnalignedRead[i] =
214 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
215 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
214 TsanUnalignedRead[i] = M.getOrInsertFunction(
215 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
216216
217217 SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr);
218 TsanUnalignedWrite[i] =
219 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
220 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
218 TsanUnalignedWrite[i] = M.getOrInsertFunction(
219 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
221220
222221 Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
223222 Type *PtrTy = Ty->getPointerTo();
224223 SmallString<32> AtomicLoadName("__tsan_atomic" + BitSizeStr + "_load");
225 TsanAtomicLoad[i] = checkSanitizerInterfaceFunction(
226 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy));
224 TsanAtomicLoad[i] =
225 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy);
227226
228227 SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store");
229 TsanAtomicStore[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
230 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy));
228 TsanAtomicStore[i] = M.getOrInsertFunction(
229 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy);
231230
232231 for (int op = AtomicRMWInst::FIRST_BINOP;
233232 op <= AtomicRMWInst::LAST_BINOP; ++op) {
250249 else
251250 continue;
252251 SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
253 TsanAtomicRMW[op][i] = checkSanitizerInterfaceFunction(
254 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy));
252 TsanAtomicRMW[op][i] =
253 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy);
255254 }
256255
257256 SmallString<32> AtomicCASName("__tsan_atomic" + BitSizeStr +
258257 "_compare_exchange_val");
259 TsanAtomicCAS[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
260 AtomicCASName, Attr, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy));
261 }
262 TsanVptrUpdate = checkSanitizerInterfaceFunction(
258 TsanAtomicCAS[i] = M.getOrInsertFunction(AtomicCASName, Attr, Ty, PtrTy, Ty,
259 Ty, OrdTy, OrdTy);
260 }
261 TsanVptrUpdate =
263262 M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(),
264 IRB.getInt8PtrTy(), IRB.getInt8PtrTy()));
265 TsanVptrLoad = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
266 "__tsan_vptr_read", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
267 TsanAtomicThreadFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
268 "__tsan_atomic_thread_fence", Attr, IRB.getVoidTy(), OrdTy));
269 TsanAtomicSignalFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
270 "__tsan_atomic_signal_fence", Attr, IRB.getVoidTy(), OrdTy));
271
272 MemmoveFn = checkSanitizerInterfaceFunction(
273 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
274 IRB.getInt8PtrTy(), IntptrTy));
275 MemcpyFn = checkSanitizerInterfaceFunction(
276 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
277 IRB.getInt8PtrTy(), IntptrTy));
278 MemsetFn = checkSanitizerInterfaceFunction(
279 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
280 IRB.getInt32Ty(), IntptrTy));
263 IRB.getInt8PtrTy(), IRB.getInt8PtrTy());
264 TsanVptrLoad = M.getOrInsertFunction("__tsan_vptr_read", Attr,
265 IRB.getVoidTy(), IRB.getInt8PtrTy());
266 TsanAtomicThreadFence = M.getOrInsertFunction("__tsan_atomic_thread_fence",
267 Attr, IRB.getVoidTy(), OrdTy);
268 TsanAtomicSignalFence = M.getOrInsertFunction("__tsan_atomic_signal_fence",
269 Attr, IRB.getVoidTy(), OrdTy);
270
271 MemmoveFn =
272 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(),
273 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
274 MemcpyFn =
275 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(),
276 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
277 MemsetFn =
278 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(),
279 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy);
281280 }
282281
283282 ThreadSanitizer::ThreadSanitizer(Module &M) {
289288 /*InitArgs=*/{},
290289 // This callback is invoked when the functions are created the first
291290 // time. Hook them into the global ctors list in that case:
292 [&](Function *Ctor, Function *) { appendToGlobalCtors(M, Ctor, 0); });
291 [&](Function *Ctor, FunctionCallee) {
292 appendToGlobalCtors(M, Ctor, 0);
293 });
293294 }
294295
295296 static bool isVtableAccess(Instruction *I) {
557558 : cast(I)->getAlignment();
558559 Type *OrigTy = cast(Addr->getType())->getElementType();
559560 const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
560 Value *OnAccessFunc = nullptr;
561 FunctionCallee OnAccessFunc = nullptr;
561562 if (Alignment == 0 || Alignment >= 8 || (Alignment % (TypeSize / 8)) == 0)
562563 OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
563564 else
657658 int Idx = getMemoryAccessFuncIndex(Addr, DL);
658659 if (Idx < 0)
659660 return false;
660 Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
661 FunctionCallee F = TsanAtomicRMW[RMWI->getOperation()][Idx];
661662 if (!F)
662663 return false;
663664 const unsigned ByteSize = 1U << Idx;
704705 I->eraseFromParent();
705706 } else if (FenceInst *FI = dyn_cast(I)) {
706707 Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
707 Function *F = FI->getSyncScopeID() == SyncScope::SingleThread ?
708 TsanAtomicSignalFence : TsanAtomicThreadFence;
708 FunctionCallee F = FI->getSyncScopeID() == SyncScope::SingleThread
709 ? TsanAtomicSignalFence
710 : TsanAtomicThreadFence;
709711 CallInst *C = CallInst::Create(F, Args);
710712 ReplaceInstWithInst(I, C);
711713 }
929929
930930 Module *M = TheStore->getModule();
931931 StringRef FuncName = "memset_pattern16";
932 Value *MSP =
933 M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
934 Int8PtrTy, Int8PtrTy, IntPtr);
932 FunctionCallee MSP = M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
933 Int8PtrTy, Int8PtrTy, IntPtr);
935934 inferLibFuncAttributes(M, FuncName, *TLI);
936935
937936 // Otherwise we should form a memset_pattern16. PatternValue is known to be
14791479 // calls to @llvm.experimental.deoptimize with different argument types in
14801480 // the same module. This is fine -- we assume the frontend knew what it
14811481 // was doing when generating this kind of IR.
1482 CallTarget =
1483 F->getParent()->getOrInsertFunction("__llvm_deoptimize", FTy);
1482 CallTarget = F->getParent()
1483 ->getOrInsertFunction("__llvm_deoptimize", FTy)
1484 .getCallee();
14841485
14851486 IsDeoptimize = true;
14861487 }
18991900
19001901 Module *M = CS.getInstruction()->getModule();
19011902 // Use a dummy vararg function to actually hold the values live
1902 Function *Func = cast(M->getOrInsertFunction(
1903 "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true)));
1903 FunctionCallee Func = M->getOrInsertFunction(
1904 "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true));
19041905 if (CS.isCall()) {
19051906 // For call safepoints insert dummy calls right after safepoint
19061907 Holders.push_back(CallInst::Create(Func, Values, "",
796796 Module *M = B.GetInsertBlock()->getModule();
797797 StringRef StrlenName = TLI->getName(LibFunc_strlen);
798798 LLVMContext &Context = B.GetInsertBlock()->getContext();
799 Constant *StrLen = M->getOrInsertFunction(StrlenName, DL.getIntPtrType(Context),
800 B.getInt8PtrTy());
799 FunctionCallee StrLen = M->getOrInsertFunction(
800 StrlenName, DL.getIntPtrType(Context), B.getInt8PtrTy());
801801 inferLibFuncAttributes(M, StrlenName, *TLI);
802802 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
803 if (const Function *F = dyn_cast(StrLen->stripPointerCasts()))
803 if (const Function *F =
804 dyn_cast(StrLen.getCallee()->stripPointerCasts()))
804805 CI->setCallingConv(F->getCallingConv());
805806
806807 return CI;
815816 StringRef StrChrName = TLI->getName(LibFunc_strchr);
816817 Type *I8Ptr = B.getInt8PtrTy();
817818 Type *I32Ty = B.getInt32Ty();
818 Constant *StrChr =
819 FunctionCallee StrChr =
819820 M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
820821 inferLibFuncAttributes(M, StrChrName, *TLI);
821822 CallInst *CI = B.CreateCall(
822823 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
823 if (const Function *F = dyn_cast(StrChr->stripPointerCasts()))
824 if (const Function *F =
825 dyn_cast(StrChr.getCallee()->stripPointerCasts()))
824826 CI->setCallingConv(F->getCallingConv());
825827 return CI;
826828 }
833835 Module *M = B.GetInsertBlock()->getModule();
834836 StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
835837 LLVMContext &Context = B.GetInsertBlock()->getContext();
836 Value *StrNCmp = M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(),
837 B.getInt8PtrTy(), B.getInt8PtrTy(),
838 DL.getIntPtrType(Context));
838 FunctionCallee StrNCmp =
839 M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
840 B.getInt8PtrTy(), DL.getIntPtrType(Context));
839841 inferLibFuncAttributes(M, StrNCmpName, *TLI);
840842 CallInst *CI = B.CreateCall(
841843 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
842844
843 if (const Function *F = dyn_cast(StrNCmp->stripPointerCasts()))
845 if (const Function *F =
846 dyn_cast(StrNCmp.getCallee()->stripPointerCasts()))
844847 CI->setCallingConv(F->getCallingConv());
845848
846849 return CI;
853856
854857 Module *M = B.GetInsertBlock()->getModule();
855858 Type *I8Ptr = B.getInt8PtrTy();
856 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
859 FunctionCallee StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
857860 inferLibFuncAttributes(M, Name, *TLI);
858861 CallInst *CI =
859862 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
860 if (const Function *F = dyn_cast(StrCpy->stripPointerCasts()))
863 if (const Function *F =
864 dyn_cast(StrCpy.getCallee()->stripPointerCasts()))
861865 CI->setCallingConv(F->getCallingConv());
862866 return CI;
863867 }
869873
870874 Module *M = B.GetInsertBlock()->getModule();
871875 Type *I8Ptr = B.getInt8PtrTy();
872 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
873 Len->getType());
876 FunctionCallee StrNCpy =
877 M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
874878 inferLibFuncAttributes(M, Name, *TLI);
875879 CallInst *CI = B.CreateCall(
876880 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
877 if (const Function *F = dyn_cast(StrNCpy->stripPointerCasts()))
881 if (const Function *F =
882 dyn_cast(StrNCpy.getCallee()->stripPointerCasts()))
878883 CI->setCallingConv(F->getCallingConv());
879884 return CI;
880885 }
890895 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
891896 Attribute::NoUnwind);
892897 LLVMContext &Context = B.GetInsertBlock()->getContext();
893 Value *MemCpy = M->getOrInsertFunction(
898 FunctionCallee MemCpy = M->getOrInsertFunction(
894899 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
895900 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
896901 DL.getIntPtrType(Context));
897902 Dst = castToCStr(Dst, B);
898903 Src = castToCStr(Src, B);
899904 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
900 if (const Function *F = dyn_cast(MemCpy->stripPointerCasts()))
905 if (const Function *F =
906 dyn_cast(MemCpy.getCallee()->stripPointerCasts()))
901907 CI->setCallingConv(F->getCallingConv());
902908 return CI;
903909 }
910916 Module *M = B.GetInsertBlock()->getModule();
911917 StringRef MemChrName = TLI->getName(LibFunc_memchr);
912918 LLVMContext &Context = B.GetInsertBlock()->getContext();
913 Value *MemChr = M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(),
914 B.getInt8PtrTy(), B.getInt32Ty(),
915 DL.getIntPtrType(Context));
919 FunctionCallee MemChr =
920 M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), B.getInt8PtrTy(),
921 B.getInt32Ty(), DL.getIntPtrType(Context));
916922 inferLibFuncAttributes(M, MemChrName, *TLI);
917923 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
918924
919 if (const Function *F = dyn_cast(MemChr->stripPointerCasts()))
925 if (const Function *F =
926 dyn_cast(MemChr.getCallee()->stripPointerCasts()))
920927 CI->setCallingConv(F->getCallingConv());
921928
922929 return CI;
930937 Module *M = B.GetInsertBlock()->getModule();
931938 StringRef MemCmpName = TLI->getName(LibFunc_memcmp);
932939 LLVMContext &Context = B.GetInsertBlock()->getContext();
933 Value *MemCmp = M->getOrInsertFunction(MemCmpName, B.getInt32Ty(),
934 B.getInt8PtrTy(), B.getInt8PtrTy(),
935 DL.getIntPtrType(Context));
940 FunctionCallee MemCmp =
941 M->getOrInsertFunction(MemCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
942 B.getInt8PtrTy(), DL.getIntPtrType(Context));
936943 inferLibFuncAttributes(M, MemCmpName, *TLI);
937944 CallInst *CI = B.CreateCall(
938945 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName);
939946
940 if (const Function *F = dyn_cast(MemCmp->stripPointerCasts()))
947 if (const Function *F =
948 dyn_cast(MemCmp.getCallee()->stripPointerCasts()))
941949 CI->setCallingConv(F->getCallingConv());
942950
943951 return CI;
964972 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
965973
966974 Module *M = B.GetInsertBlock()->getModule();
967 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
968 Op->getType());
975 FunctionCallee Callee =
976 M->getOrInsertFunction(Name, Op->getType(), Op->getType());
969977 CallInst *CI = B.CreateCall(Callee, Op, Name);
970978
971979 // The incoming attribute set may have come from a speculatable intrinsic, but
974982 CI->setAttributes(Attrs.removeAttribute(B.getContext(),
975983 AttributeList::FunctionIndex,
976984 Attribute::Speculatable));
977 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
985 if (const Function *F =
986 dyn_cast(Callee.getCallee()->stripPointerCasts()))
978987 CI->setCallingConv(F->getCallingConv());
979988
980989 return CI;
10071016 appendTypeSuffix(Op1, Name, NameBuffer);
10081017
10091018 Module *M = B.GetInsertBlock()->getModule();
1010 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
1011 Op2->getType());
1019 FunctionCallee Callee = M->getOrInsertFunction(
1020 Name, Op1->getType(), Op1->getType(), Op2->getType());
10121021 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
10131022 CI->setAttributes(Attrs);
1014 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
1023 if (const Function *F =
1024 dyn_cast(Callee.getCallee()->stripPointerCasts()))
10151025 CI->setCallingConv(F->getCallingConv());
10161026
10171027 return CI;
10241034
10251035 Module *M = B.GetInsertBlock()->getModule();
10261036 StringRef PutCharName = TLI->getName(LibFunc_putchar);
1027 Value *PutChar = M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
1037 FunctionCallee PutChar =
1038 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
10281039 inferLibFuncAttributes(M, PutCharName, *TLI);
10291040 CallInst *CI = B.CreateCall(PutChar,
10301041 B.CreateIntCast(Char,
10331044 "chari"),
10341045 PutCharName);
10351046
1036 if (const Function *F = dyn_cast(PutChar->stripPointerCasts()))
1047 if (const Function *F =
1048 dyn_cast(PutChar.getCallee()->stripPointerCasts()))
10371049 CI->setCallingConv(F->getCallingConv());
10381050 return CI;
10391051 }
10451057
10461058 Module *M = B.GetInsertBlock()->getModule();
10471059 StringRef PutsName = TLI->getName(LibFunc_puts);
1048 Value *PutS =
1060 FunctionCallee PutS =
10491061 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
10501062 inferLibFuncAttributes(M, PutsName, *TLI);
10511063 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
1052 if (const Function *F = dyn_cast(PutS->stripPointerCasts()))
1064 if (const Function *F =
1065 dyn_cast(PutS.getCallee()->stripPointerCasts()))
10531066 CI->setCallingConv(F->getCallingConv());
10541067 return CI;
10551068 }
10611074
10621075 Module *M = B.GetInsertBlock()->getModule();
10631076 StringRef FPutcName = TLI->getName(LibFunc_fputc);
1064 Constant *F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), B.getInt32Ty(),
1065 File->getType());
1077 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
1078 B.getInt32Ty(), File->getType());
10661079 if (File->getType()->isPointerTy())
10671080 inferLibFuncAttributes(M, FPutcName, *TLI);
10681081 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
10691082 "chari");
10701083 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
10711084
1072 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1085 if (const Function *Fn =
1086 dyn_cast(F.getCallee()->stripPointerCasts()))
10731087 CI->setCallingConv(Fn->getCallingConv());
10741088 return CI;
10751089 }
10811095
10821096 Module *M = B.GetInsertBlock()->getModule();
10831097 StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
1084 Constant *F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1085 B.getInt32Ty(), File->getType());
1098 FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1099 B.getInt32Ty(), File->getType());
10861100 if (File->getType()->isPointerTy())
10871101 inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
10881102 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
10891103 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
10901104
1091 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1105 if (const Function *Fn =
1106 dyn_cast(F.getCallee()->stripPointerCasts()))
10921107 CI->setCallingConv(Fn->getCallingConv());
10931108 return CI;
10941109 }
11001115
11011116 Module *M = B.GetInsertBlock()->getModule();
11021117 StringRef FPutsName = TLI->getName(LibFunc_fputs);
1103 Constant *F = M->getOrInsertFunction(
1104 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
1118 FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
1119 B.getInt8PtrTy(), File->getType());
11051120 if (File->getType()->isPointerTy())
11061121 inferLibFuncAttributes(M, FPutsName, *TLI);
11071122 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
11081123
1109 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1124 if (const Function *Fn =
1125 dyn_cast(F.getCallee()->stripPointerCasts()))
11101126 CI->setCallingConv(Fn->getCallingConv());
11111127 return CI;
11121128 }
11181134
11191135 Module *M = B.GetInsertBlock()->getModule();
11201136 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
1121 Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1122 B.getInt8PtrTy(), File->getType());
1137 FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1138 B.getInt8PtrTy(), File->getType());
11231139 if (File->getType()->isPointerTy())
11241140 inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
11251141 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
11261142
1127 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1143 if (const Function *Fn =
1144 dyn_cast(F.getCallee()->stripPointerCasts()))
11281145 CI->setCallingConv(Fn->getCallingConv());
11291146 return CI;
11301147 }
11371154 Module *M = B.GetInsertBlock()->getModule();
11381155 LLVMContext &Context = B.GetInsertBlock()->getContext();
11391156 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1140 Constant *F = M->getOrInsertFunction(
1157 FunctionCallee F = M->getOrInsertFunction(
11411158 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
11421159 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
11431160
11471164 B.CreateCall(F, {castToCStr(Ptr, B), Size,
11481165 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
11491166
1150 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1167 if (const Function *Fn =
1168 dyn_cast(F.getCallee()->stripPointerCasts()))
11511169 CI->setCallingConv(Fn->getCallingConv());
11521170 return CI;
11531171 }
11601178 Module *M = B.GetInsertBlock()->getModule();
11611179 StringRef MallocName = TLI->getName(LibFunc_malloc);
11621180 LLVMContext &Context = B.GetInsertBlock()->getContext();
1163 Value *Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1164 DL.getIntPtrType(Context));
1181 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1182 DL.getIntPtrType(Context));
11651183 inferLibFuncAttributes(M, MallocName, *TLI);
11661184 CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
11671185
1168 if (const Function *F = dyn_cast(Malloc->stripPointerCasts()))
1186 if (const Function *F =
1187 dyn_cast(Malloc.getCallee()->stripPointerCasts()))
11691188 CI->setCallingConv(F->getCallingConv());
11701189
11711190 return CI;
11801199 StringRef CallocName = TLI.getName(LibFunc_calloc);
11811200 const DataLayout &DL = M->getDataLayout();
11821201 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1183 Value *Calloc = M->getOrInsertFunction(CallocName, Attrs, B.getInt8PtrTy(),
1184 PtrType, PtrType);
1202 FunctionCallee Calloc = M->getOrInsertFunction(
1203 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
11851204 inferLibFuncAttributes(M, CallocName, TLI);
11861205 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
11871206
1188 if (const auto *F = dyn_cast(Calloc->stripPointerCasts()))
1207 if (const auto *F =
1208 dyn_cast(Calloc.getCallee()->stripPointerCasts()))
11891209 CI->setCallingConv(F->getCallingConv());
11901210
11911211 return CI;
12001220 Module *M = B.GetInsertBlock()->getModule();
12011221 LLVMContext &Context = B.GetInsertBlock()->getContext();
12021222 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
1203 Constant *F = M->getOrInsertFunction(
1223 FunctionCallee F = M->getOrInsertFunction(
12041224 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
12051225 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
12061226
12081228 inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
12091229 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
12101230
1211 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1231 if (const Function *Fn =
1232 dyn_cast(F.getCallee()->stripPointerCasts()))
12121233 CI->setCallingConv(Fn->getCallingConv());
12131234 return CI;
12141235 }
12201241
12211242 Module *M = B.GetInsertBlock()->getModule();
12221243 StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
1223 Constant *F =
1224 M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), File->getType());
1244 FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(),
1245 File->getType());
12251246 if (File->getType()->isPointerTy())
12261247 inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
12271248 CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
12281249
1229 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1250 if (const Function *Fn =
1251 dyn_cast(F.getCallee()->stripPointerCasts()))
12301252 CI->setCallingConv(Fn->getCallingConv());
12311253 return CI;
12321254 }
12381260
12391261 Module *M = B.GetInsertBlock()->getModule();
12401262 StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
1241 Constant *F =
1263 FunctionCallee F =
12421264 M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
12431265 B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
12441266 inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
12451267 CallInst *CI =
12461268 B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
12471269
1248 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1270 if (const Function *Fn =
1271 dyn_cast(F.getCallee()->stripPointerCasts()))
12491272 CI->setCallingConv(Fn->getCallingConv());
12501273 return CI;
12511274 }
12591282 Module *M = B.GetInsertBlock()->getModule();
12601283 LLVMContext &Context = B.GetInsertBlock()->getContext();
12611284 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
1262 Constant *F = M->getOrInsertFunction(
1285 FunctionCallee F = M->getOrInsertFunction(
12631286 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
12641287 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
12651288
12671290 inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
12681291 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
12691292
1270 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
1293 if (const Function *Fn =
1294 dyn_cast(F.getCallee()->stripPointerCasts()))
12711295 CI->setCallingConv(Fn->getCallingConv());
12721296 return CI;
12731297 }
2929 Func == "__mcount" ||
3030 Func == "_mcount" ||
3131 Func == "__cyg_profile_func_enter_bare") {
32 Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
32 FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
3333 CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
3434 Call->setDebugLoc(DL);
3535 return;
3838 if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") {
3939 Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)};
4040
41 Constant *Fn = M.getOrInsertFunction(
41 FunctionCallee Fn = M.getOrInsertFunction(
4242 Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false));
4343
4444 Instruction *RetAddr = CallInst::Create(
1717 #include "llvm/IR/Module.h"
1818 using namespace llvm;
1919
20 static Constant *getDefaultPersonalityFn(Module *M) {
20 static FunctionCallee getDefaultPersonalityFn(Module *M) {
2121 LLVMContext &C = M->getContext();
2222 Triple T(M->getTargetTriple());
2323 EHPersonality Pers = getDefaultEHPersonality(T);
6767 BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
6868 Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
6969 if (!F.hasPersonalityFn()) {
70 Constant *PersFn = getDefaultPersonalityFn(F.getParent());
71 F.setPersonalityFn(PersFn);
70 FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
71 F.setPersonalityFn(cast(PersFn.getCallee()));
7272 }
7373
7474 if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
125125 appendToUsedList(M, "llvm.compiler.used", Values);
126126 }
127127
128 Function *llvm::checkSanitizerInterfaceFunction(Constant *FuncOrBitcast) {
129 if (isa(FuncOrBitcast))
130 return cast(FuncOrBitcast);
131 FuncOrBitcast->print(errs());
132 errs() << '\n';
133 std::string Err;
134 raw_string_ostream Stream(Err);
135 Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
136 report_fatal_error(Err);
137 }
138
139 Function *llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
140 ArrayRef InitArgTypes) {
128 FunctionCallee
129 llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
130 ArrayRef InitArgTypes) {
141131 assert(!InitName.empty() && "Expected init function name");
142 Function *F = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
132 return M.getOrInsertFunction(
143133 InitName,
144134 FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
145 AttributeList()));
146 F->setLinkage(Function::ExternalLinkage);
147 return F;
148 }
149
150 std::pair llvm::createSanitizerCtorAndInitFunctions(
135 AttributeList());
136 }
137
138 std::pair llvm::createSanitizerCtorAndInitFunctions(
151139 Module &M, StringRef CtorName, StringRef InitName,
152140 ArrayRef InitArgTypes, ArrayRef InitArgs,
153141 StringRef VersionCheckName) {
154142 assert(!InitName.empty() && "Expected init function name");
155143 assert(InitArgs.size() == InitArgTypes.size() &&
156144 "Sanitizer's init function expects different number of arguments");
157 Function *InitFunction =
145 FunctionCallee InitFunction =
158146 declareSanitizerInitFunction(M, InitName, InitArgTypes);
159147 Function *Ctor = Function::Create(
160148 FunctionType::get(Type::getVoidTy(M.getContext()), false),
163151 IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
164152 IRB.CreateCall(InitFunction, InitArgs);
165153 if (!VersionCheckName.empty()) {
166 Function *VersionCheckFunction =
167 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
168 VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
169 AttributeList()));
154 FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
155 VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
156 AttributeList());
170157 IRB.CreateCall(VersionCheckFunction, {});
171158 }
172159 return std::make_pair(Ctor, InitFunction);
173160 }
174161
175 std::pair *>
162 std::pairCallee>
176163 llvm::getOrCreateSanitizerCtorAndInitFunctions(
177164 Module &M, StringRef CtorName, StringRef InitName,
178165 ArrayRef InitArgTypes, ArrayRef InitArgs,
179 function_ref *)> FunctionsCreatedCallback,
166 function_refCallee)> FunctionsCreatedCallback,
180167 StringRef VersionCheckName) {
181168 assert(!CtorName.empty() && "Expected ctor function name");