llvm.org GIT mirror llvm / 5be828a
Revert "[opaque pointer types] Add a FunctionCallee wrapper type, and use it." This reverts commit f47d6b38c7a61d50db4566b02719de05492dcef1 (r352791). Seems to run into compilation failures with GCC (but not clang, where I tested it). Reverting while I investigate. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352800 91177308-0d34-0410-b5e6-96231b3b80d8 James Y Knight 5 months ago
69 changed file(s) with 935 addition(s) and 902 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 * ``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.
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.
35043498
35053499 * ``std::string getTypeName(const Type *Ty)``
35063500
7171 Tys);
7272
7373 //declare i32 @getchar()
74 getchar_func =
75 module->getOrInsertFunction("getchar", IntegerType::getInt32Ty(C));
74 getchar_func = cast(module->
75 getOrInsertFunction("getchar", IntegerType::getInt32Ty(C)));
7676
7777 //declare i32 @putchar(i32)
78 putchar_func = module->getOrInsertFunction(
79 "putchar", IntegerType::getInt32Ty(C), IntegerType::getInt32Ty(C));
78 putchar_func = cast(module->
79 getOrInsertFunction("putchar", IntegerType::getInt32Ty(C),
80 IntegerType::getInt32Ty(C)));
8081
8182 //Function header
8283
8384 //define void @brainf()
84 brainf_func = module->getOrInsertFunction("brainf", Type::getVoidTy(C));
85 brainf_func = cast(module->
86 getOrInsertFunction("brainf", Type::getVoidTy(C)));
8587
8688 builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
8789
150152 "aberrormsg");
151153
152154 //declare i32 @puts(i8 *)
153 FunctionCallee puts_func = module->getOrInsertFunction(
154 "puts", IntegerType::getInt32Ty(C),
155 PointerType::getUnqual(IntegerType::getInt8Ty(C)));
155 Function *puts_func = cast(module->
156 getOrInsertFunction("puts", IntegerType::getInt32Ty(C),
157 PointerType::getUnqual(IntegerType::getInt8Ty(C))));
156158
157159 //brainf.aberror:
158160 aberrorbb = BasicBlock::Create(C, label, brainf_func);
7777 CompileFlags comflag;
7878 std::istream *in;
7979 Module *module;
80 FunctionCallee brainf_func;
81 FunctionCallee getchar_func;
82 FunctionCallee putchar_func;
80 Function *brainf_func;
81 Function *getchar_func;
82 Function *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 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
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())))));
8179 {
8280 Function::arg_iterator args = main_func->arg_begin();
8381 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);
5553 Function *FibF =
56 Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
54 cast(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
55 Type::getInt32Ty(Context)));
5756
5857 // Add a basic block to the function.
5958 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 Function::Create(FunctionType::get(Type::getInt32Ty(Context),
72 {Type::getInt32Ty(Context)}, false),
73 Function::ExternalLinkage, "add1", M);
71 cast(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
72 Type::getInt32Ty(Context)));
7473
7574 // Add a basic block to the function. As before, it automatically inserts
7675 // because of the last argument.
9998 // Now we're going to create function `foo', which returns an int and takes no
10099 // arguments.
101100 Function *FooF =
102 Function::Create(FunctionType::get(Type::getInt32Ty(Context), {}, false),
103 Function::ExternalLinkage, "foo", M);
101 cast(M->getOrInsertFunction("foo", Type::getInt32Ty(Context)));
104102
105103 // Add a basic block to the FooF function.
106104 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.
5152 Function *Add1F =
52 Function::Create(FunctionType::get(Type::getInt32Ty(Context),
53 {Type::getInt32Ty(Context)}, false),
54 Function::ExternalLinkage, "add1", M);
53 cast(M->getOrInsertFunction("add1",
54 Type::getInt32Ty(M->getContext()),
55 Type::getInt32Ty(M->getContext())));
5556
5657 // Add a basic block to the function. As before, it automatically inserts
5758 // because of the last argument.
7879 static Function *CreateFibFunction(Module *M) {
7980 // Create the fib function and insert it into module M. This function is said
8081 // to return an int and take an int parameter.
81 FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
82 {Type::getInt32Ty(Context)}, false);
83 Function *FibF =
84 Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
82 Function *FibF =
83 cast(M->getOrInsertFunction("fib",
84 Type::getInt32Ty(M->getContext()),
85 Type::getInt32Ty(M->getContext())));
8586
8687 // Add a basic block to the function.
8788 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
3236 /// Replace a call to the specified intrinsic function.
3337 /// If an intrinsic function must be implemented by the code generator
3438 /// (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 typename U = std::enable_if<(&T::getFunctionType != nullptr), void>>
169 FunctionCallee(T *Fn)
170 : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
171
172 FunctionCallee(FunctionType *FnTy, Value *Callee)
173 : FnTy(FnTy), Callee(Callee) {
174 assert((FnTy == nullptr) == (Callee == nullptr));
175 }
176
177 FunctionCallee(std::nullptr_t) {}
178
179 FunctionCallee() = default;
180
181 FunctionType *getFunctionType() { return FnTy; }
182
183 Value *getCallee() { return Callee; }
184
185 explicit operator bool() { return Callee; }
186
187 private:
188 FunctionType *FnTy = nullptr;
189 Value *Callee = nullptr;
190 };
191158
192159 /// Common super class of ArrayType, StructType and VectorType.
193160 class CompositeType : public Type {
904904 Name);
905905 }
906906
907 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
907 InvokeInst *CreateInvoke(Function *Callee, BasicBlock *NormalDest,
908908 BasicBlock *UnwindDest, ArrayRef Args,
909909 ArrayRef OpBundles,
910910 const Twine &Name = "") {
911 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
912 NormalDest, UnwindDest, Args, OpBundles, Name);
913 }
914
915 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
911 return CreateInvoke(Callee->getFunctionType(), Callee, NormalDest,
912 UnwindDest, Args, OpBundles, Name);
913 }
914
915 InvokeInst *CreateInvoke(Function *Callee, BasicBlock *NormalDest,
916916 BasicBlock *UnwindDest,
917917 ArrayRef Args = None,
918918 const Twine &Name = "") {
919 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
920 NormalDest, UnwindDest, Args, Name);
919 return CreateInvoke(Callee->getFunctionType(), Callee, NormalDest,
920 UnwindDest, Args, Name);
921921 }
922922
923923 // Deprecated [opaque pointer types]
19871987 return Insert(CI, Name);
19881988 }
19891989
1990 CallInst *CreateCall(FunctionCallee Callee, ArrayRef Args = None,
1990 CallInst *CreateCall(Function *Callee, ArrayRef Args = None,
19911991 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1992 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
1993 FPMathTag);
1994 }
1995
1996 CallInst *CreateCall(FunctionCallee Callee, ArrayRef Args,
1992 return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag);
1993 }
1994
1995 CallInst *CreateCall(Function *Callee, ArrayRef Args,
19971996 ArrayRef OpBundles,
19981997 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1999 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2000 OpBundles, Name, FPMathTag);
1998 return CreateCall(Callee->getFunctionType(), Callee, Args, OpBundles, Name,
1999 FPMathTag);
20012000 }
20022001
20032002 // 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
12391234 /// Sets the function called, including updating to the specified function
12401235 /// type.
12411236 void setCalledFunction(FunctionType *FTy, Value *Fn) {
15421542 CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd);
15431543 }
15441544
1545 static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
1545 static CallInst *Create(Function *Func, const Twine &NameStr = "",
15461546 Instruction *InsertBefore = nullptr) {
1547 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1548 InsertBefore);
1549 }
1550
1551 static CallInst *Create(FunctionCallee Func, ArrayRef Args,
1552 ArrayRef Bundles = None,
1547 return Create(Func->getFunctionType(), Func, NameStr, InsertBefore);
1548 }
1549
1550 static CallInst *Create(Function *Func, ArrayRef Args,
15531551 const Twine &NameStr = "",
15541552 Instruction *InsertBefore = nullptr) {
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,
1553 return Create(Func->getFunctionType(), Func, Args, NameStr, InsertBefore);
1554 }
1555
1556 static CallInst *Create(Function *Func, const Twine &NameStr,
15671557 BasicBlock *InsertAtEnd) {
1568 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1569 InsertAtEnd);
1570 }
1571
1572 static CallInst *Create(FunctionCallee Func, ArrayRef Args,
1558 return Create(Func->getFunctionType(), Func, NameStr, InsertAtEnd);
1559 }
1560
1561 static CallInst *Create(Function *Func, ArrayRef Args,
15731562 const Twine &NameStr, BasicBlock *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);
1563 return Create(Func->getFunctionType(), Func, Args, NameStr, InsertAtEnd);
15831564 }
15841565
15851566 // Deprecated [opaque pointer types]
37223703 NameStr, InsertAtEnd);
37233704 }
37243705
3725 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3706 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
37263707 BasicBlock *IfException, ArrayRef Args,
37273708 const Twine &NameStr,
37283709 Instruction *InsertBefore = nullptr) {
3729 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3730 IfException, Args, None, NameStr, InsertBefore);
3731 }
3732
3733 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3710 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3711 None, NameStr, InsertBefore);
3712 }
3713
3714 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
37343715 BasicBlock *IfException, ArrayRef Args,
37353716 ArrayRef Bundles = None,
37363717 const Twine &NameStr = "",
37373718 Instruction *InsertBefore = nullptr) {
3738 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3739 IfException, Args, Bundles, NameStr, InsertBefore);
3740 }
3741
3742 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3719 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3720 Bundles, NameStr, InsertBefore);
3721 }
3722
3723 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
37433724 BasicBlock *IfException, ArrayRef Args,
37443725 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3745 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3746 IfException, Args, NameStr, InsertAtEnd);
3747 }
3748
3749 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3726 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3727 NameStr, InsertAtEnd);
3728 }
3729
3730 static InvokeInst *Create(Function *Func, BasicBlock *IfNormal,
37503731 BasicBlock *IfException, ArrayRef Args,
37513732 ArrayRef Bundles,
37523733 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3753 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3754 IfException, Args, Bundles, NameStr, InsertAtEnd);
3734 return Create(Func->getFunctionType(), Func, IfNormal, IfException, Args,
3735 Bundles, NameStr, InsertAtEnd);
37553736 }
37563737
37573738 // 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. Otherwise, if the existing function has the correct prototype, return
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
335337 /// the existing function.
336 /// 3. Finally, the function exists but has the wrong prototype: return the
338 /// 4. Finally, the function exists but has the wrong prototype: return the
337339 /// function with a constantexpr cast to the right prototype.
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);
340 Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
341 AttributeList AttributeList);
342
343 Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
346344
347345 /// Look up the specified function in the module symbol table. If it does not
348346 /// exist, add a prototype for the function and return it. This function
350348 /// or a ConstantExpr BitCast of that type if the named function has a
351349 /// different type. This version of the method takes a list of
352350 /// function arguments, which makes it easier for clients to use.
353 template
354 FunctionCallee getOrInsertFunction(StringRef Name,
355 AttributeList AttributeList, Type *RetTy,
356 ArgsTy... Args) {
351 template
352 Constant *getOrInsertFunction(StringRef Name,
353 AttributeList AttributeList,
354 Type *RetTy, ArgsTy... Args)
355 {
357356 SmallVector ArgTys{Args...};
358357 return getOrInsertFunction(Name,
359358 FunctionType::get(RetTy, ArgTys, false),
361360 }
362361
363362 /// Same as above, but without the attributes.
364 template
365 FunctionCallee getOrInsertFunction(StringRef Name, Type *RetTy,
366 ArgsTy... Args) {
363 template
364 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
367365 return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
368366 }
369367
370368 // Avoid an incorrect ordering that'd otherwise compile incorrectly.
371369 template
372 FunctionCallee
373 getOrInsertFunction(StringRef Name, AttributeList AttributeList,
374 FunctionType *Invalid, ArgsTy... Args) = delete;
370 Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList,
371 FunctionType *Invalid, ArgsTy... Args) = delete;
375372
376373 /// Look up the specified function in the module symbol table. If it does not
377374 /// exist, return null.
2020 template class ArrayRef;
2121 class Module;
2222 class Function;
23 class FunctionCallee;
2423 class GlobalValue;
2524 class GlobalVariable;
2625 class Constant;
3938 void appendToGlobalDtors(Module &M, Function *F, int Priority,
4039 Constant *Data = nullptr);
4140
42 FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
43 ArrayRef InitArgTypes);
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);
4449
4550 /// Creates sanitizer constructor function, and calls sanitizer's init
4651 /// function from it.
4752 /// \return Returns pair of pointers to constructor, and init functions
4853 /// respectively.
49 std::pairCallee> createSanitizerCtorAndInitFunctions(
54 std::pair *> createSanitizerCtorAndInitFunctions(
5055 Module &M, StringRef CtorName, StringRef InitName,
5156 ArrayRef InitArgTypes, ArrayRef InitArgs,
5257 StringRef VersionCheckName = StringRef());
5863 ///
5964 /// \return Returns pair of pointers to constructor, and init functions
6065 /// respectively.
61 std::pairCallee> getOrCreateSanitizerCtorAndInitFunctions(
66 std::pair *> getOrCreateSanitizerCtorAndInitFunctions(
6267 Module &M, StringRef CtorName, StringRef InitName,
6368 ArrayRef InitArgTypes, ArrayRef InitArgs,
64 function_refCallee)> FunctionsCreatedCallback,
69 function_ref *)> FunctionsCreatedCallback,
6570 StringRef VersionCheckName = StringRef());
6671
6772 // 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 FunctionCallee LibcallFn =
1756 Constant *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 FunctionCallee RewindFunction = nullptr;
47 Constant *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 }
2457
2558 /// This function is used when we want to lower an intrinsic call to a call of
2659 /// an external function. This handles hard cases such as when there was already
3770 std::vector ParamTys;
3871 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
3972 ParamTys.push_back((*I)->getType());
40 FunctionCallee FCache =
41 M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
73 Constant* FCache = M->getOrInsertFunction(NewFn,
74 FunctionType::get(RetTy, ParamTys, false));
4275
4376 IRBuilder<> Builder(CI->getParent(), CI->getIterator());
4477 SmallVector Args(ArgBegin, ArgEnd);
5689 # undef setjmp
5790 # define setjmp_undefined_for_msvc
5891 #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 }
59161
60162 /// Emit the code to lower bswap of V before the specified instruction IP.
61163 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 =
273 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
274 Function::ExternalLinkage, Name, M);
272 Function *F = cast(M.getOrInsertFunction(
273 Name, FunctionType::get(Type::getVoidTy(Context), false)));
275274 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
276275 new UnreachableInst(Context, BB);
277276 return F;
11031103
11041104 // Create the function using an IR-level function.
11051105 LLVMContext &C = M.getContext();
1106 Function *F =
1107 Function::Create(FunctionType::get(Type::getVoidTy(C), false),
1108 Function::ExternalLinkage, NameStream.str(), M);
1106 Function *F = dyn_cast(
1107 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
1108 assert(F && "Function was null!");
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 FunctionCallee FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
67
68 if (Function *Fn = dyn_cast(FCache.getCallee())) {
66 Constant* FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
67
68 if (Function* Fn = dyn_cast(FCache)) {
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 FunctionCallee StackChkFail =
477 F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
476 Constant *StackChkFail = F.getParent()->getOrInsertFunction(
477 "__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 FunctionCallee Fn = F.getParent()->getOrInsertFunction(
784 Value *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 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;
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;
5050 AllocaInst *FuncCtx;
5151
5252 public:
498498 IRBuilder<> B(FailBB);
499499 B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
500500 if (Trip.isOSOpenBSD()) {
501 FunctionCallee StackChkFail = M->getOrInsertFunction(
502 "__stack_smash_handler", Type::getVoidTy(Context),
503 Type::getInt8PtrTy(Context));
501 Constant *StackChkFail =
502 M->getOrInsertFunction("__stack_smash_handler",
503 Type::getVoidTy(Context),
504 Type::getInt8PtrTy(Context));
504505
505506 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
506507 } else {
507 FunctionCallee StackChkFail =
508 Constant *StackChkFail =
508509 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
509510
510511 B.CreateCall(StackChkFail, {});
15861586 // thread's unsafe stack pointer.
15871587 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
15881588 Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
1589 FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
1590 StackPtrTy->getPointerTo(0));
1589 Value *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 FunctionCallee CallPersonalityF =
114 nullptr; // _Unwind_CallPersonality() wrapper
113 Function *CallPersonalityF = nullptr; // _Unwind_CallPersonality() wrapper
115114
116115 bool prepareEHPads(Function &F);
117116 bool prepareThrows(Function &F);
252251 Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception);
253252
254253 // _Unwind_CallPersonality() wrapper function, which calls the personality
255 CallPersonalityF = M.getOrInsertFunction(
256 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
257 if (Function *F = dyn_cast(CallPersonalityF.getCallee()))
258 F->setDoesNotThrow();
254 CallPersonalityF = cast(M.getOrInsertFunction(
255 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()));
256 CallPersonalityF->setDoesNotThrow();
259257
260258 unsigned Index = 0;
261259 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 cast(
1021 M->getOrInsertFunction(getName(id, Tys),
1022 getType(M->getContext(), id, Tys))
1023 .getCallee());
1020 return
1021 cast(M->getOrInsertFunction(getName(id, Tys),
1022 getType(M->getContext(), id, Tys)));
10241023 }
10251024
10261025 // 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 FunctionCallee MallocFunc = MallocF;
519 Value *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.getCallee())) {
543 if (Function *F = dyn_cast(MallocFunc)) {
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 FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
616 Value *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.getCallee()))
629 if (Function *F = dyn_cast(FreeFunc))
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 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
143 AttributeList AttributeList) {
142 Constant *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 {Ty, New}; // Return the new prototype.
153 return 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 {Ty, ConstantExpr::getBitCast(F, PTy)};
160 return ConstantExpr::getBitCast(F, PTy);
161161
162162 // Otherwise, we just found the existing function or a prototype.
163 return {Ty, F};
164 }
165
166 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
163 return F;
164 }
165
166 Constant *Module::getOrInsertFunction(StringRef Name,
167 FunctionType *Ty) {
167168 return getOrInsertFunction(Name, Ty, AttributeList());
168169 }
169170
1174711747 Type::getInt8PtrTy(M.getContext()));
1174811748
1174911749 // MSVC CRT has a function to validate security cookie.
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 }
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);
1175711756 return;
1175811757 }
1175911758 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 FunctionCallee getFunction(Module *M, const FuncInfo &fInfo);
74 Constant *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 FunctionCallee getNativeFunction(Module *M, const FuncInfo &FInfo);
141 Constant* getNativeFunction(Module* M, const FuncInfo &FInfo);
142142
143143 protected:
144144 CallInst *CI;
215215 false, false)
216216
217217 template
218 static CallInst *CreateCallEx(IRB &B, FunctionCallee Callee, Value *Arg,
218 static CallInst *CreateCallEx(IRB &B, Value *Callee, Value *Arg,
219219 const Twine &Name = "") {
220220 CallInst *R = B.CreateCall(Callee, Arg, Name);
221 if (Function *F = dyn_cast(Callee.getCallee()))
221 if (Function* F = dyn_cast(Callee))
222222 R->setCallingConv(F->getCallingConv());
223223 return R;
224224 }
225225
226226 template
227 static CallInst *CreateCallEx2(IRB &B, FunctionCallee Callee, Value *Arg1,
228 Value *Arg2, const Twine &Name = "") {
227 static CallInst *CreateCallEx2(IRB &B, Value *Callee, Value *Arg1, Value *Arg2,
228 const Twine &Name = "") {
229229 CallInst *R = B.CreateCall(Callee, {Arg1, Arg2}, Name);
230 if (Function *F = dyn_cast(Callee.getCallee()))
230 if (Function* F = dyn_cast(Callee))
231231 R->setCallingConv(F->getCallingConv());
232232 return R;
233233 }
470470 return (AMDGPULibFunc::EType)FInfo.getLeads()[0].ArgType;
471471 }
472472
473 FunctionCallee AMDGPULibCalls::getFunction(Module *M, const FuncInfo &fInfo) {
473 Constant *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 FunctionCallee sinExpr = getFunction(M, nf);
520 Constant *sinExpr = getFunction(M, nf);
521521
522522 nf.setPrefix(AMDGPULibFunc::NATIVE);
523523 nf.setId(AMDGPULibFunc::EI_COS);
524 FunctionCallee cosExpr = getFunction(M, nf);
524 Constant *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 FunctionCallee F = getFunction(aCI->getModule(), FInfo);
556 Constant *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 FunctionCallee F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc);
614 auto *F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc);
615615 if (!F)
616616 return false;
617617
793793
794794 AMDGPULibFunc nf = FInfo;
795795 nf.setPrefix(AMDGPULibFunc::NATIVE);
796 if (FunctionCallee FPExpr = getFunction(M, nf)) {
796 if (Constant *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 (FunctionCallee FPExpr =
936 getFunction(M, AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
937 : AMDGPULibFunc::EI_RSQRT,
938 FInfo))) {
935 if (Constant *FPExpr = getFunction(M,
936 AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
937 : AMDGPULibFunc::EI_RSQRT, FInfo))) {
939938 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
940939 << FInfo.getName().c_str() << "(" << *opr0 << ")\n");
941940 Value *nval = CreateCallEx(B,FPExpr, opr0, issqrt ? "__pow2sqrt"
10021001
10031002 // powr ---> exp2(y * log2(x))
10041003 // pown/pow ---> powr(fabs(x), y) | (x & ((int)y << 31))
1005 FunctionCallee ExpExpr =
1006 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2, FInfo));
1004 Constant *ExpExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2,
1005 FInfo));
10071006 if (!ExpExpr)
10081007 return false;
10091008
10891088
10901089 Value *nval;
10911090 if (needabs) {
1092 FunctionCallee AbsExpr =
1093 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS, FInfo));
1091 Constant *AbsExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS,
1092 FInfo));
10941093 if (!AbsExpr)
10951094 return false;
10961095 nval = CreateCallEx(B, AbsExpr, opr0, "__fabs");
10981097 nval = cnval ? cnval : opr0;
10991098 }
11001099 if (needlog) {
1101 FunctionCallee LogExpr =
1102 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2, FInfo));
1100 Constant *LogExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2,
1101 FInfo));
11031102 if (!LogExpr)
11041103 return false;
11051104 nval = CreateCallEx(B,LogExpr, nval, "__log2");
11581157 std::vector ParamsTys;
11591158 ParamsTys.push_back(opr0->getType());
11601159 Module *M = CI->getModule();
1161 if (FunctionCallee FPExpr =
1162 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
1160 if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT,
1161 FInfo))) {
11631162 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> sqrt(" << *opr0 << ")\n");
11641163 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2sqrt");
11651164 replaceCall(nval);
11671166 }
11681167 } else if (ci_opr1 == 3) { // rootn(x, 3) = cbrt(x)
11691168 Module *M = CI->getModule();
1170 if (FunctionCallee FPExpr =
1171 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT, FInfo))) {
1169 if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT,
1170 FInfo))) {
11721171 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> cbrt(" << *opr0 << ")\n");
11731172 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2cbrt");
11741173 replaceCall(nval);
11851184 std::vector ParamsTys;
11861185 ParamsTys.push_back(opr0->getType());
11871186 Module *M = CI->getModule();
1188 if (FunctionCallee FPExpr =
1189 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT, FInfo))) {
1187 if (Constant *FPExpr = getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT,
1188 FInfo))) {
11901189 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> rsqrt(" << *opr0
11911190 << ")\n");
11921191 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2rsqrt");
12421241 }
12431242
12441243 // Get a scalar native builtin signle argument FP function
1245 FunctionCallee AMDGPULibCalls::getNativeFunction(Module *M,
1246 const FuncInfo &FInfo) {
1244 Constant* AMDGPULibCalls::getNativeFunction(Module* M, const FuncInfo& FInfo) {
12471245 if (getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()))
12481246 return nullptr;
12491247 FuncInfo nf = FInfo;
12561254 const FuncInfo &FInfo) {
12571255 if (getArgType(FInfo) == AMDGPULibFunc::F32 && (getVecSize(FInfo) == 1) &&
12581256 (FInfo.getPrefix() != AMDGPULibFunc::NATIVE)) {
1259 if (FunctionCallee FPExpr = getNativeFunction(
1260 CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
1257 if (Constant *FPExpr = getNativeFunction(
1258 CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
12611259 Value *opr0 = CI->getArgOperand(0);
12621260 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
12631261 << "sqrt(" << *opr0 << ")\n");
13341332 // function.
13351333 AMDGPULibFunc nf(AMDGPULibFunc::EI_SINCOS, fInfo);
13361334 nf.getLeads()[0].PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AMDGPUAS::FLAT_ADDRESS);
1337 FunctionCallee Fsincos = getFunction(M, nf);
1335 Function *Fsincos = dyn_cast_or_null(getFunction(M, nf));
13381336 if (!Fsincos) return false;
13391337
13401338 BasicBlock::iterator ItOld = B.GetInsertPoint();
13421340 B.SetInsertPoint(UI);
13431341
13441342 Value *P = Alloc;
1345 Type *PTy = Fsincos.getFunctionType()->getParamType(1);
1343 Type *PTy = Fsincos->getFunctionType()->getParamType(1);
13461344 // The allocaInst allocates the memory in private address space. This need
13471345 // to be bitcasted to point to the address space of cos pointer type.
13481346 // In OpenCL 2.0 this is generic, while in 1.2 that is private.
959959 return nullptr;
960960 }
961961
962 FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
963 const AMDGPULibFunc &fInfo) {
962 Function *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 FunctionCallee C;
989 Constant *C = nullptr;
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 C;
1003 return cast(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 FunctionCallee getOrInsertFunction(llvm::Module *M,
396 const AMDGPULibFunc &fInfo);
395 static Function *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 FunctionCallee Fn = M->getOrInsertFunction(
2256 HexagonVolatileMemcpyName, VoidTy, Int32PtrTy, Int32PtrTy, Int32Ty);
2255 Constant *CF = M->getOrInsertFunction(HexagonVolatileMemcpyName, VoidTy,
2256 Int32PtrTy, Int32PtrTy, Int32Ty);
2257 Function *Fn = cast(CF);
2258 Fn->setLinkage(Function::ExternalLinkage);
22572259
22582260 const SCEV *OneS = SE->getConstant(Int32Ty, 1);
22592261 const SCEV *BECount32 = SE->getTruncateOrZeroExtend(BECount, Int32Ty);
413413 Attribute::ReadNone);
414414 A = A.addAttribute(C, AttributeList::FunctionIndex,
415415 Attribute::NoInline);
416 FunctionCallee F = (M->getOrInsertFunction(Name, A, MyVoid, T));
416 Value *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 FunctionCallee AtExit = M.getOrInsertFunction(
112 "__cxa_atexit",
113 FunctionType::get(Type::getInt32Ty(C),
114 {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar},
115 /*isVarArg=*/false));
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);
116115
117116 // Declare __dso_local.
118117 Constant *DsoHandle = M.getNamedValue("__dso_handle");
22782278 Type::getInt8PtrTy(M.getContext()));
22792279
22802280 // MSVC CRT has a function to validate security cookie.
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 }
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);
22882287 return;
22892288 }
22902289 // 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 FunctionCallee SetJmp3 = nullptr;
89 FunctionCallee CxxLongjmpUnwind = nullptr;
88 Constant *SetJmp3 = nullptr;
89 Constant *CxxLongjmpUnwind = nullptr;
9090
9191 // Per-function state
9292 EHPersonality Personality = EHPersonality::Unknown;
9393 Function *PersonalityFn = nullptr;
9494 bool UseStackGuard = false;
9595 int ParentBaseState;
96 FunctionCallee SehLongjmpUnwind = nullptr;
96 Constant *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.getCallee()->stripPointerCasts())
305 cast(CxxLongjmpUnwind->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.getCallee()->stripPointerCasts())
358 cast(SehLongjmpUnwind->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.getCallee());
473 OptionalArgs.push_back(CxxLongjmpUnwind);
474474 OptionalArgs.push_back(State);
475475 OptionalArgs.push_back(emitEHLSDA(Builder, &F));
476476 } else if (Personality == EHPersonality::MSVC_X86SEH) {
477 OptionalArgs.push_back(SehLongjmpUnwind.getCallee());
477 OptionalArgs.push_back(SehLongjmpUnwind);
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.getCallee()->stripPointerCasts())
768 SetJmp3->stripPointerCasts())
769769 continue;
770770
771771 SetJmp3CallSites.push_back(CS);
104104 }
105105
106106 LLVMContext &Ctx = M.getContext();
107 FunctionCallee C = M.getOrInsertFunction(
107 Constant *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.getCallee());
110 Function *F = dyn_cast(C);
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 FunctionCallee CFICheckFailFn =
135 M.getOrInsertFunction("__cfi_check_fail", Type::getVoidTy(Ctx),
136 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
134 Constant *CFICheckFailFn = M.getOrInsertFunction(
135 "__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx),
136 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 Constant *SingleImpl =
1497 cast(M.getOrInsertFunction(Res.SingleImplName,
1498 Type::getVoidTy(M.getContext()))
1499 .getCallee());
1496 auto *SingleImpl = M.getOrInsertFunction(
1497 Res.SingleImplName, Type::getVoidTy(M.getContext()));
15001498
15011499 // This is the import phase so we should not be exporting anything.
15021500 bool IsExported = false;
15381536 }
15391537
15401538 if (Res.TheKind == WholeProgramDevirtResolution::BranchFunnel) {
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());
1539 auto *JT = M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
1540 Type::getVoidTy(M.getContext()));
15471541 bool IsExported = false;
15481542 applyICallBranchFunnel(SlotInfo, JT, IsExported);
15491543 assert(!IsExported);
714714 Type *IntptrTy;
715715 ShadowMapping Mapping;
716716 DominatorTree *DT;
717 FunctionCallee AsanHandleNoReturnFunc;
718 FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
717 Function *AsanHandleNoReturnFunc;
718 Function *AsanPtrCmpFunction, *AsanPtrSubFunction;
719719 Constant *AsanShadowGlobal;
720720
721721 // These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize).
722 FunctionCallee AsanErrorCallback[2][2][kNumberOfAccessSizes];
723 FunctionCallee AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
722 Function *AsanErrorCallback[2][2][kNumberOfAccessSizes];
723 Function *AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
724724
725725 // These arrays is indexed by AccessIsWrite and Experiment.
726 FunctionCallee AsanErrorCallbackSized[2][2];
727 FunctionCallee AsanMemoryAccessCallbackSized[2][2];
728
729 FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
726 Function *AsanErrorCallbackSized[2][2];
727 Function *AsanMemoryAccessCallbackSized[2][2];
728
729 Function *AsanMemmove, *AsanMemcpy, *AsanMemset;
730730 InlineAsm *EmptyAsm;
731731 Value *LocalDynamicShadow = nullptr;
732732 GlobalsMetadata GlobalsMD;
808808 LLVMContext *C;
809809 Triple TargetTriple;
810810 ShadowMapping Mapping;
811 FunctionCallee AsanPoisonGlobals;
812 FunctionCallee AsanUnpoisonGlobals;
813 FunctionCallee AsanRegisterGlobals;
814 FunctionCallee AsanUnregisterGlobals;
815 FunctionCallee AsanRegisterImageGlobals;
816 FunctionCallee AsanUnregisterImageGlobals;
817 FunctionCallee AsanRegisterElfGlobals;
818 FunctionCallee AsanUnregisterElfGlobals;
811 Function *AsanPoisonGlobals;
812 Function *AsanUnpoisonGlobals;
813 Function *AsanRegisterGlobals;
814 Function *AsanUnregisterGlobals;
815 Function *AsanRegisterImageGlobals;
816 Function *AsanUnregisterImageGlobals;
817 Function *AsanRegisterElfGlobals;
818 Function *AsanUnregisterElfGlobals;
819819
820820 Function *AsanCtorFunction = nullptr;
821821 Function *AsanDtorFunction = nullptr;
844844 SmallVector RetVec;
845845 unsigned StackAlignment;
846846
847 FunctionCallee AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
848 AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
849 FunctionCallee AsanSetShadowFunc[0x100] = {};
850 FunctionCallee AsanPoisonStackMemoryFunc, AsanUnpoisonStackMemoryFunc;
851 FunctionCallee AsanAllocaPoisonFunc, AsanAllocasUnpoisonFunc;
847 Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
848 *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
849 Function *AsanSetShadowFunc[0x100] = {};
850 Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
851 Function *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 FunctionCallee F = isa(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
1335 Function *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 =
1798 M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy);
1799 AsanUnpoisonGlobals =
1800 M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy());
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);
18011803
18021804 // Declare functions that register/unregister globals.
1803 AsanRegisterGlobals = M.getOrInsertFunction(
1804 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
1805 AsanUnregisterGlobals = M.getOrInsertFunction(
1806 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
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);
18071812
18081813 // Declare the functions that find globals in a shared object and then invoke
18091814 // the (un)register function on them.
1810 AsanRegisterImageGlobals = M.getOrInsertFunction(
1811 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
1812 AsanUnregisterImageGlobals = M.getOrInsertFunction(
1813 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
1814
1815 AsanRegisterElfGlobals =
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(
18161826 M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(),
1817 IntptrTy, IntptrTy, IntptrTy);
1818 AsanUnregisterElfGlobals =
1827 IntptrTy, IntptrTy, IntptrTy));
1828 AsanRegisterElfGlobals->setLinkage(Function::ExternalLinkage);
1829
1830 AsanUnregisterElfGlobals = checkSanitizerInterfaceFunction(
18191831 M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(),
1820 IntptrTy, IntptrTy, IntptrTy);
1832 IntptrTy, IntptrTy, IntptrTy));
1833 AsanUnregisterElfGlobals->setLinkage(Function::ExternalLinkage);
18211834 }
18221835
18231836 // Put the metadata and the instrumented global in the same group. This ensures
23312344 Args2.push_back(ExpType);
23322345 Args1.push_back(ExpType);
23332346 }
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));
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)));
23412356
23422357 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
23432358 AccessSizeIndex++) {
23442359 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
23452360 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2346 M.getOrInsertFunction(
2361 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
23472362 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
2348 FunctionType::get(IRB.getVoidTy(), Args1, false));
2363 FunctionType::get(IRB.getVoidTy(), Args1, false)));
23492364
23502365 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2351 M.getOrInsertFunction(
2366 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
23522367 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
2353 FunctionType::get(IRB.getVoidTy(), Args1, false));
2368 FunctionType::get(IRB.getVoidTy(), Args1, false)));
23542369 }
23552370 }
23562371 }
23572372
23582373 const std::string MemIntrinCallbackPrefix =
23592374 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
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);
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));
23772392 // We insert an empty inline asm after __asan_report* to avoid callback merge.
23782393 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
23792394 StringRef(""), StringRef(""),
24112426 // We cannot just ignore these methods, because they may call other
24122427 // instrumented functions.
24132428 if (F.getName().find(" load]") != std::string::npos) {
2414 FunctionCallee AsanInitFunction =
2429 Function *AsanInitFunction =
24152430 declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {});
24162431 IRBuilder<> IRB(&F.front(), F.front().begin());
24172432 IRB.CreateCall(AsanInitFunction, {});
26262641 IRBuilder<> IRB(*C);
26272642 for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
26282643 std::string Suffix = itostr(i);
2629 AsanStackMallocFunc[i] = M.getOrInsertFunction(
2630 kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy);
2631 AsanStackFreeFunc[i] =
2644 AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
2645 M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
2646 IntptrTy));
2647 AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
26322648 M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
2633 IRB.getVoidTy(), IntptrTy, IntptrTy);
2649 IRB.getVoidTy(), IntptrTy, IntptrTy));
26342650 }
26352651 if (ASan.UseAfterScope) {
2636 AsanPoisonStackMemoryFunc = M.getOrInsertFunction(
2637 kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2638 AsanUnpoisonStackMemoryFunc = M.getOrInsertFunction(
2639 kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2652 AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
2653 M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
2654 IntptrTy, IntptrTy));
2655 AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
2656 M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
2657 IntptrTy, IntptrTy));
26402658 }
26412659
26422660 for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
26442662 Name << kAsanSetShadowPrefix;
26452663 Name << std::setw(2) << std::setfill('0') << std::hex << Val;
26462664 AsanSetShadowFunc[Val] =
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);
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));
26542674 }
26552675
26562676 void FunctionStackPoisoner::copyToShadowInline(ArrayRef ShadowMask,
340340 FunctionType *DFSanSetLabelFnTy;
341341 FunctionType *DFSanNonzeroLabelFnTy;
342342 FunctionType *DFSanVarargWrapperFnTy;
343 FunctionCallee DFSanUnionFn;
344 FunctionCallee DFSanCheckedUnionFn;
345 FunctionCallee DFSanUnionLoadFn;
346 FunctionCallee DFSanUnimplementedFn;
347 FunctionCallee DFSanSetLabelFn;
348 FunctionCallee DFSanNonzeroLabelFn;
349 FunctionCallee DFSanVarargWrapperFn;
343 Constant *DFSanUnionFn;
344 Constant *DFSanCheckedUnionFn;
345 Constant *DFSanUnionLoadFn;
346 Constant *DFSanUnimplementedFn;
347 Constant *DFSanSetLabelFn;
348 Constant *DFSanNonzeroLabelFn;
349 Constant *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 FunctionCallee C = Mod->getOrInsertFunction(FName, FTT);
680 Function *F = dyn_cast(C.getCallee());
679 Constant *C = Mod->getOrInsertFunction(FName, FTT);
680 Function *F = dyn_cast(C);
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 cast(C.getCallee());
705 return C;
706706 }
707707
708708 bool DataFlowSanitizer::runOnModule(Module &M) {
724724 ExternalShadowMask =
725725 Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy);
726726
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);
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);
764749 }
765750 DFSanUnimplementedFn =
766751 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
767 {
768 AttributeList AL;
769 AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
770 DFSanSetLabelFn =
771 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy, AL);
752 DFSanSetLabelFn =
753 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
754 if (Function *F = dyn_cast(DFSanSetLabelFn)) {
755 F->addParamAttr(0, Attribute::ZExt);
772756 }
773757 DFSanNonzeroLabelFn =
774758 Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
779763 SmallPtrSet FnsWithNativeABI;
780764 for (Function &i : M) {
781765 if (!i.isIntrinsic() &&
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())
766 &i != DFSanUnionFn &&
767 &i != DFSanCheckedUnionFn &&
768 &i != DFSanUnionLoadFn &&
769 &i != DFSanUnimplementedFn &&
770 &i != DFSanSetLabelFn &&
771 &i != DFSanNonzeroLabelFn &&
772 &i != DFSanVarargWrapperFn)
789773 FnsToInstrument.push_back(&i);
790774 }
791775
15271511
15281512 // Calls to this function are synthesized in wrappers, and we shouldn't
15291513 // instrument them.
1530 if (F == DFSF.DFS.DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
1514 if (F == DFSF.DFS.DFSanVarargWrapperFn)
15311515 return;
15321516
15331517 IRBuilder<> IRB(CS.getInstruction());
15601544 TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT);
15611545 std::string CustomFName = "__dfsw_";
15621546 CustomFName += F->getName();
1563 FunctionCallee CustomF = DFSF.DFS.Mod->getOrInsertFunction(
1547 Constant *CustomF = DFSF.DFS.Mod->getOrInsertFunction(
15641548 CustomFName, CustomFn.TransformedType);
1565 if (Function *CustomFn = dyn_cast(CustomF.getCallee())) {
1549 if (Function *CustomFn = dyn_cast(CustomF)) {
15661550 CustomFn->copyAttributesFrom(F);
15671551
15681552 // 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 FunctionCallee EsanAlignedLoad[NumberOfAccessSizes];
205 FunctionCallee EsanAlignedStore[NumberOfAccessSizes];
206 FunctionCallee EsanUnalignedLoad[NumberOfAccessSizes];
207 FunctionCallee EsanUnalignedStore[NumberOfAccessSizes];
204 Function *EsanAlignedLoad[NumberOfAccessSizes];
205 Function *EsanAlignedStore[NumberOfAccessSizes];
206 Function *EsanUnalignedLoad[NumberOfAccessSizes];
207 Function *EsanUnalignedStore[NumberOfAccessSizes];
208208 // For irregular sizes of any alignment:
209 FunctionCallee EsanUnalignedLoadN, EsanUnalignedStoreN;
210 FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
209 Function *EsanUnalignedLoadN, *EsanUnalignedStoreN;
210 Function *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] = M.getOrInsertFunction(
252 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy());
251 EsanAlignedLoad[Idx] =
252 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
253 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
253254 SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr);
254 EsanAlignedStore[Idx] = M.getOrInsertFunction(
255 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy());
255 EsanAlignedStore[Idx] =
256 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
257 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
256258 SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr);
257 EsanUnalignedLoad[Idx] = M.getOrInsertFunction(
258 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy());
259 EsanUnalignedLoad[Idx] =
260 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
261 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
259262 SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr);
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 =
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(
268274 M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
269 IRB.getInt8PtrTy(), IntptrTy);
270 MemcpyFn =
275 IRB.getInt8PtrTy(), IntptrTy));
276 MemcpyFn = checkSanitizerInterfaceFunction(
271277 M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
272 IRB.getInt8PtrTy(), IntptrTy);
273 MemsetFn =
278 IRB.getInt8PtrTy(), IntptrTy));
279 MemsetFn = checkSanitizerInterfaceFunction(
274280 M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
275 IRB.getInt32Ty(), IntptrTy);
281 IRB.getInt32Ty(), IntptrTy));
276282 }
277283
278284 bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
503509 EsanModuleDtorName, &M);
504510 ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
505511 IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
506 FunctionCallee EsanExit =
507 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(), Int8PtrTy);
512 Function *EsanExit = checkSanitizerInterfaceFunction(
513 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
514 Int8PtrTy));
515 EsanExit->setLinkage(Function::ExternalLinkage);
508516 IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
509517 appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
510518 }
660668
661669 Type *OrigTy = cast(Addr->getType())->getElementType();
662670 const uint32_t TypeSizeBytes = DL.getTypeStoreSizeInBits(OrigTy) / 8;
663 FunctionCallee OnAccessFunc = nullptr;
671 Value *OnAccessFunc = nullptr;
664672
665673 // Convert 0 to the default alignment.
666674 if (Alignment == 0)
101101 std::vector &Regexes);
102102
103103 // Get pointers to the functions in the runtime library.
104 FunctionCallee getStartFileFunc();
105 FunctionCallee getEmitFunctionFunc();
106 FunctionCallee getEmitArcsFunc();
107 FunctionCallee getSummaryInfoFunc();
108 FunctionCallee getEndFileFunc();
104 Constant *getStartFileFunc();
105 Constant *getEmitFunctionFunc();
106 Constant *getEmitArcsFunc();
107 Constant *getSummaryInfoFunc();
108 Constant *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 FunctionCallee GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy);
649 Constant *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 FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
865 Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
866866 Builder.CreateCall(GCOVInit, {WriteoutF, FlushF});
867867 Builder.CreateRetVoid();
868868
872872 return Result;
873873 }
874874
875 FunctionCallee GCOVProfiler::getStartFileFunc() {
875 Constant *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 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);
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);
886886 return Res;
887 }
888
889 FunctionCallee GCOVProfiler::getEmitFunctionFunc() {
887
888 }
889
890 Constant *GCOVProfiler::getEmitFunctionFunc() {
890891 Type *Args[] = {
891892 Type::getInt32Ty(*Ctx), // uint32_t ident
892893 Type::getInt8PtrTy(*Ctx), // const char *function_name
895896 Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
896897 };
897898 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
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() {
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() {
909911 Type *Args[] = {
910912 Type::getInt32Ty(*Ctx), // uint32_t num_counters
911913 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
912914 };
913915 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
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() {
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() {
921924 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
922925 return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
923926 }
924927
925 FunctionCallee GCOVProfiler::getEndFileFunc() {
928 Constant *GCOVProfiler::getEndFileFunc() {
926929 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
927930 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
928931 }
942945 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
943946 IRBuilder<> Builder(BB);
944947
945 FunctionCallee StartFile = getStartFileFunc();
946 FunctionCallee EmitFunction = getEmitFunctionFunc();
947 FunctionCallee EmitArcs = getEmitArcsFunc();
948 FunctionCallee SummaryInfo = getSummaryInfoFunc();
949 FunctionCallee EndFile = getEndFileFunc();
948 Constant *StartFile = getStartFileFunc();
949 Constant *EmitFunction = getEmitFunctionFunc();
950 Constant *EmitArcs = getEmitArcsFunc();
951 Constant *SummaryInfo = getSummaryInfoFunc();
952 Constant *EndFile = getEndFileFunc();
950953
951954 NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu");
952955 if (!CUNodes) {
220220 LLVMContext *C;
221221 std::string CurModuleUniqueId;
222222 Triple TargetTriple;
223 FunctionCallee HWAsanMemmove, HWAsanMemcpy, HWAsanMemset;
223 Function *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 FunctionCallee HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
273 FunctionCallee HwasanMemoryAccessCallbackSized[2];
274
275 FunctionCallee HwasanTagMemoryFunc;
276 FunctionCallee HwasanGenerateTagFunc;
277 FunctionCallee HwasanThreadEnterFunc;
272 Function *HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
273 Function *HwasanMemoryAccessCallbackSized[2];
274
275 Function *HwasanTagMemoryFunc;
276 Function *HwasanGenerateTagFunc;
277 Function *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] = M.getOrInsertFunction(
372 ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
373 FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false));
371 HwasanMemoryAccessCallbackSized[AccessIsWrite] =
372 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
373 ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
374 FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false)));
374375
375376 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
376377 AccessSizeIndex++) {
377378 HwasanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] =
378 M.getOrInsertFunction(
379 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
379380 ClMemoryAccessCallbackPrefix + TypeStr +
380381 itostr(1ULL << AccessSizeIndex) + EndingStr,
381 FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false));
382 FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false)));
382383 }
383384 }
384385
385 HwasanTagMemoryFunc = M.getOrInsertFunction(
386 "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy);
387 HwasanGenerateTagFunc =
388 M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty);
386 HwasanTagMemoryFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
387 "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy));
388 HwasanGenerateTagFunc = checkSanitizerInterfaceFunction(
389 M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty));
389390
390391 ShadowGlobal = M.getOrInsertGlobal("__hwasan_shadow",
391392 ArrayType::get(IRB.getInt8Ty(), 0));
392393
393394 const std::string MemIntrinCallbackPrefix =
394395 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
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());
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()));
407408 }
408409
409410 Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {
507507 return true;
508508 }
509509
510 static FunctionCallee
511 getOrInsertValueProfilingCall(Module &M, const TargetLibraryInfo &TLI,
512 bool IsRange = false) {
510 static Constant *getOrInsertValueProfilingCall(Module &M,
511 const TargetLibraryInfo &TLI,
512 bool IsRange = false) {
513513 LLVMContext &Ctx = M.getContext();
514514 auto *ReturnTy = Type::getVoidTy(M.getContext());
515515
516 AttributeList AL;
517 if (auto AK = TLI.getExtAttrForI32Param(false))
518 AL = AL.addParamAttribute(M.getContext(), 2, AK);
519
516 Constant *Res;
520517 if (!IsRange) {
521518 Type *ParamTypes[] = {
522519 #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
524521 };
525522 auto *ValueProfilingCallTy =
526523 FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
527 return M.getOrInsertFunction(getInstrProfValueProfFuncName(),
528 ValueProfilingCallTy, AL);
524 Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(),
525 ValueProfilingCallTy);
529526 } else {
530527 Type *RangeParamTypes[] = {
531528 #define VALUE_RANGE_PROF 1
535532 };
536533 auto *ValueRangeProfilingCallTy =
537534 FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false);
538 return M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(),
539 ValueRangeProfilingCallTy, AL);
540 }
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;
541544 }
542545
543546 void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
535535 bool CallbacksInitialized = false;
536536
537537 /// The run-time callback to print a warning.
538 FunctionCallee WarningFn;
538 Value *WarningFn;
539539
540540 // These arrays are indexed by log2(AccessSize).
541 FunctionCallee MaybeWarningFn[kNumberOfAccessSizes];
542 FunctionCallee MaybeStoreOriginFn[kNumberOfAccessSizes];
541 Value *MaybeWarningFn[kNumberOfAccessSizes];
542 Value *MaybeStoreOriginFn[kNumberOfAccessSizes];
543543
544544 /// Run-time helper that generates a new origin value for a stack
545545 /// allocation.
546 FunctionCallee MsanSetAllocaOrigin4Fn;
546 Value *MsanSetAllocaOrigin4Fn;
547547
548548 /// Run-time helper that poisons stack on function entry.
549 FunctionCallee MsanPoisonStackFn;
549 Value *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 FunctionCallee MsanChainOriginFn;
553 Value *MsanChainOriginFn;
554554
555555 /// MSan runtime replacements for memmove, memcpy and memset.
556 FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
556 Value *MemmoveFn, *MemcpyFn, *MemsetFn;
557557
558558 /// KMSAN callback for task-local function argument shadow.
559 FunctionCallee MsanGetContextStateFn;
559 Value *MsanGetContextStateFn;
560560
561561 /// Functions for poisoning/unpoisoning local variables
562 FunctionCallee MsanPoisonAllocaFn, MsanUnpoisonAllocaFn;
562 Value *MsanPoisonAllocaFn, *MsanUnpoisonAllocaFn;
563563
564564 /// Each of the MsanMetadataPtrXxx functions returns a pair of shadow/origin
565565 /// pointers.
566 FunctionCallee MsanMetadataPtrForLoadN, MsanMetadataPtrForStoreN;
567 FunctionCallee MsanMetadataPtrForLoad_1_8[4];
568 FunctionCallee MsanMetadataPtrForStore_1_8[4];
569 FunctionCallee MsanInstrumentAsmStoreFn;
566 Value *MsanMetadataPtrForLoadN, *MsanMetadataPtrForStoreN;
567 Value *MsanMetadataPtrForLoad_1_8[4];
568 Value *MsanMetadataPtrForStore_1_8[4];
569 Value *MsanInstrumentAsmStoreFn;
570570
571571 /// Helper to choose between different MsanMetadataPtrXxx().
572 FunctionCallee getKmsanShadowOriginAccessFn(bool isStore, int size);
572 Value *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 FunctionCallee MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore,
826 int size) {
827 FunctionCallee *Fns =
825 Value *MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore, int size) {
826 Value **Fns =
828827 isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8;
829828 switch (size) {
830829 case 1:
924923 /*InitArgs=*/{},
925924 // This callback is invoked when the functions are created the first
926925 // time. Hook them into the global ctors list in that case:
927 [&](Function *Ctor, FunctionCallee) {
926 [&](Function *Ctor, Function *) {
928927 if (!ClWithComdat) {
929928 appendToGlobalCtors(M, Ctor, 0);
930929 return;
11231122 DL.getTypeSizeInBits(ConvertedShadow->getType());
11241123 unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
11251124 if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
1126 FunctionCallee Fn = MS.MaybeStoreOriginFn[SizeIndex];
1125 Value *Fn = MS.MaybeStoreOriginFn[SizeIndex];
11271126 Value *ConvertedShadow2 = IRB.CreateZExt(
11281127 ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
11291128 IRB.CreateCall(Fn, {ConvertedShadow2,
12051204 unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
12061205 unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
12071206 if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
1208 FunctionCallee Fn = MS.MaybeWarningFn[SizeIndex];
1207 Value *Fn = MS.MaybeWarningFn[SizeIndex];
12091208 Value *ConvertedShadow2 =
12101209 IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
12111210 IRB.CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
14121411 const DataLayout &DL = F.getParent()->getDataLayout();
14131412 int Size = DL.getTypeStoreSize(ShadowTy);
14141413
1415 FunctionCallee Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
1414 Value *Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
14161415 Value *AddrCast =
14171416 IRB.CreatePointerCast(Addr, PointerType::get(IRB.getInt8Ty(), 0));
14181417 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 FunctionCallee SanCovTracePCIndir;
225 FunctionCallee SanCovTracePC, SanCovTracePCGuard;
226 FunctionCallee SanCovTraceCmpFunction[4];
227 FunctionCallee SanCovTraceConstCmpFunction[4];
228 FunctionCallee SanCovTraceDivFunction[2];
229 FunctionCallee SanCovTraceGepFunction;
230 FunctionCallee SanCovTraceSwitchFunction;
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;
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 =
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
330 SanCovTracePCIndir = checkSanitizerInterfaceFunction(
331 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
342332 SanCovTraceCmpFunction[0] =
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());
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()));
351341 SanCovTraceCmpFunction[3] =
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);
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));
360354 SanCovTraceConstCmpFunction[3] =
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 }
355 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
356 SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty));
357
358 SanCovTraceDivFunction[0] =
359 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
360 SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
370361 SanCovTraceDivFunction[1] =
371 M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty);
362 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
363 SanCovTraceDiv8, VoidTy, Int64Ty));
372364 SanCovTraceGepFunction =
373 M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy);
365 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
366 SanCovTraceGep, VoidTy, IntptrTy));
374367 SanCovTraceSwitchFunction =
375 M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy);
368 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
369 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
376370
377371 Constant *SanCovLowestStackConstant =
378372 M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
382376 if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
383377 SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
384378
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
385392 // We insert an empty inline asm after cov callbacks to avoid callback merge.
386393 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
387394 StringRef(""), StringRef(""),
388395 /*hasSideEffects=*/true);
389396
390 SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy);
391 SanCovTracePCGuard =
392 M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy);
397 SanCovTracePC = checkSanitizerInterfaceFunction(
398 M.getOrInsertFunction(SanCovTracePCName, VoidTy));
399 SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
400 SanCovTracePCGuardName, VoidTy, Int32PtrTy));
393401
394402 for (auto &F : M)
395403 runOnFunction(F);
404412 SanCovCountersSectionName);
405413 if (Ctor && Options.PCTable) {
406414 auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
407 FunctionCallee InitFunction = declareSanitizerInitFunction(
415 Function *InitFunction = declareSanitizerInitFunction(
408416 M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
409417 IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
410418 IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
109109 Type *IntptrTy;
110110 IntegerType *OrdTy;
111111 // Callbacks to run-time library are computed in doInitialization.
112 FunctionCallee TsanFuncEntry;
113 FunctionCallee TsanFuncExit;
114 FunctionCallee TsanIgnoreBegin;
115 FunctionCallee TsanIgnoreEnd;
112 Function *TsanFuncEntry;
113 Function *TsanFuncExit;
114 Function *TsanIgnoreBegin;
115 Function *TsanIgnoreEnd;
116116 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
117117 static const size_t kNumberOfAccessSizes = 5;
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;
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;
132131 Function *TsanCtorFunction;
133132 };
134133
188187 Attr = Attr.addAttribute(M.getContext(), AttributeList::FunctionIndex,
189188 Attribute::NoUnwind);
190189 // Initialize the callbacks.
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());
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()));
199198 OrdTy = IRB.getInt32Ty();
200199 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
201200 const unsigned ByteSize = 1U << i;
203202 std::string ByteSizeStr = utostr(ByteSize);
204203 std::string BitSizeStr = utostr(BitSize);
205204 SmallString<32> ReadName("__tsan_read" + ByteSizeStr);
206 TsanRead[i] = M.getOrInsertFunction(ReadName, Attr, IRB.getVoidTy(),
207 IRB.getInt8PtrTy());
205 TsanRead[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
206 ReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
208207
209208 SmallString<32> WriteName("__tsan_write" + ByteSizeStr);
210 TsanWrite[i] = M.getOrInsertFunction(WriteName, Attr, IRB.getVoidTy(),
211 IRB.getInt8PtrTy());
209 TsanWrite[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
210 WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
212211
213212 SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr);
214 TsanUnalignedRead[i] = M.getOrInsertFunction(
215 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
213 TsanUnalignedRead[i] =
214 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
215 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
216216
217217 SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr);
218 TsanUnalignedWrite[i] = M.getOrInsertFunction(
219 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
218 TsanUnalignedWrite[i] =
219 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
220 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
220221
221222 Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
222223 Type *PtrTy = Ty->getPointerTo();
223224 SmallString<32> AtomicLoadName("__tsan_atomic" + BitSizeStr + "_load");
224 TsanAtomicLoad[i] =
225 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy);
225 TsanAtomicLoad[i] = checkSanitizerInterfaceFunction(
226 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy));
226227
227228 SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store");
228 TsanAtomicStore[i] = M.getOrInsertFunction(
229 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy);
229 TsanAtomicStore[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
230 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy));
230231
231232 for (int op = AtomicRMWInst::FIRST_BINOP;
232233 op <= AtomicRMWInst::LAST_BINOP; ++op) {
249250 else
250251 continue;
251252 SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
252 TsanAtomicRMW[op][i] =
253 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy);
253 TsanAtomicRMW[op][i] = checkSanitizerInterfaceFunction(
254 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy));
254255 }
255256
256257 SmallString<32> AtomicCASName("__tsan_atomic" + BitSizeStr +
257258 "_compare_exchange_val");
258 TsanAtomicCAS[i] = M.getOrInsertFunction(AtomicCASName, Attr, Ty, PtrTy, Ty,
259 Ty, OrdTy, OrdTy);
260 }
261 TsanVptrUpdate =
259 TsanAtomicCAS[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
260 AtomicCASName, Attr, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy));
261 }
262 TsanVptrUpdate = checkSanitizerInterfaceFunction(
262263 M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(),
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);
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));
280281 }
281282
282283 ThreadSanitizer::ThreadSanitizer(Module &M) {
288289 /*InitArgs=*/{},
289290 // This callback is invoked when the functions are created the first
290291 // time. Hook them into the global ctors list in that case:
291 [&](Function *Ctor, FunctionCallee) {
292 appendToGlobalCtors(M, Ctor, 0);
293 });
292 [&](Function *Ctor, Function *) { appendToGlobalCtors(M, Ctor, 0); });
294293 }
295294
296295 static bool isVtableAccess(Instruction *I) {
558557 : cast(I)->getAlignment();
559558 Type *OrigTy = cast(Addr->getType())->getElementType();
560559 const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
561 FunctionCallee OnAccessFunc = nullptr;
560 Value *OnAccessFunc = nullptr;
562561 if (Alignment == 0 || Alignment >= 8 || (Alignment % (TypeSize / 8)) == 0)
563562 OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
564563 else
658657 int Idx = getMemoryAccessFuncIndex(Addr, DL);
659658 if (Idx < 0)
660659 return false;
661 FunctionCallee F = TsanAtomicRMW[RMWI->getOperation()][Idx];
660 Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
662661 if (!F)
663662 return false;
664663 const unsigned ByteSize = 1U << Idx;
705704 I->eraseFromParent();
706705 } else if (FenceInst *FI = dyn_cast(I)) {
707706 Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
708 FunctionCallee F = FI->getSyncScopeID() == SyncScope::SingleThread
709 ? TsanAtomicSignalFence
710 : TsanAtomicThreadFence;
707 Function *F = FI->getSyncScopeID() == SyncScope::SingleThread ?
708 TsanAtomicSignalFence : TsanAtomicThreadFence;
711709 CallInst *C = CallInst::Create(F, Args);
712710 ReplaceInstWithInst(I, C);
713711 }
929929
930930 Module *M = TheStore->getModule();
931931 StringRef FuncName = "memset_pattern16";
932 FunctionCallee MSP = M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
933 Int8PtrTy, Int8PtrTy, IntPtr);
932 Value *MSP =
933 M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
934 Int8PtrTy, Int8PtrTy, IntPtr);
934935 inferLibFuncAttributes(M, FuncName, *TLI);
935936
936937 // 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 = F->getParent()
1483 ->getOrInsertFunction("__llvm_deoptimize", FTy)
1484 .getCallee();
1482 CallTarget =
1483 F->getParent()->getOrInsertFunction("__llvm_deoptimize", FTy);
14851484
14861485 IsDeoptimize = true;
14871486 }
19001899
19011900 Module *M = CS.getInstruction()->getModule();
19021901 // Use a dummy vararg function to actually hold the values live
1903 FunctionCallee Func = M->getOrInsertFunction(
1904 "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true));
1902 Function *Func = cast(M->getOrInsertFunction(
1903 "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true)));
19051904 if (CS.isCall()) {
19061905 // For call safepoints insert dummy calls right after safepoint
19071906 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 FunctionCallee StrLen = M->getOrInsertFunction(
800 StrlenName, DL.getIntPtrType(Context), B.getInt8PtrTy());
799 Constant *StrLen = M->getOrInsertFunction(StrlenName, DL.getIntPtrType(Context),
800 B.getInt8PtrTy());
801801 inferLibFuncAttributes(M, StrlenName, *TLI);
802802 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
803 if (const Function *F =
804 dyn_cast(StrLen.getCallee()->stripPointerCasts()))
803 if (const Function *F = dyn_cast(StrLen->stripPointerCasts()))
805804 CI->setCallingConv(F->getCallingConv());
806805
807806 return CI;
816815 StringRef StrChrName = TLI->getName(LibFunc_strchr);
817816 Type *I8Ptr = B.getInt8PtrTy();
818817 Type *I32Ty = B.getInt32Ty();
819 FunctionCallee StrChr =
818 Constant *StrChr =
820819 M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
821820 inferLibFuncAttributes(M, StrChrName, *TLI);
822821 CallInst *CI = B.CreateCall(
823822 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
824 if (const Function *F =
825 dyn_cast(StrChr.getCallee()->stripPointerCasts()))
823 if (const Function *F = dyn_cast(StrChr->stripPointerCasts()))
826824 CI->setCallingConv(F->getCallingConv());
827825 return CI;
828826 }
835833 Module *M = B.GetInsertBlock()->getModule();
836834 StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
837835 LLVMContext &Context = B.GetInsertBlock()->getContext();
838 FunctionCallee StrNCmp =
839 M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
840 B.getInt8PtrTy(), DL.getIntPtrType(Context));
836 Value *StrNCmp = M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(),
837 B.getInt8PtrTy(), B.getInt8PtrTy(),
838 DL.getIntPtrType(Context));
841839 inferLibFuncAttributes(M, StrNCmpName, *TLI);
842840 CallInst *CI = B.CreateCall(
843841 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
844842
845 if (const Function *F =
846 dyn_cast(StrNCmp.getCallee()->stripPointerCasts()))
843 if (const Function *F = dyn_cast(StrNCmp->stripPointerCasts()))
847844 CI->setCallingConv(F->getCallingConv());
848845
849846 return CI;
856853
857854 Module *M = B.GetInsertBlock()->getModule();
858855 Type *I8Ptr = B.getInt8PtrTy();
859 FunctionCallee StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
856 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
860857 inferLibFuncAttributes(M, Name, *TLI);
861858 CallInst *CI =
862859 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
863 if (const Function *F =
864 dyn_cast(StrCpy.getCallee()->stripPointerCasts()))
860 if (const Function *F = dyn_cast(StrCpy->stripPointerCasts()))
865861 CI->setCallingConv(F->getCallingConv());
866862 return CI;
867863 }
873869
874870 Module *M = B.GetInsertBlock()->getModule();
875871 Type *I8Ptr = B.getInt8PtrTy();
876 FunctionCallee StrNCpy =
877 M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
872 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
873 Len->getType());
878874 inferLibFuncAttributes(M, Name, *TLI);
879875 CallInst *CI = B.CreateCall(
880876 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
881 if (const Function *F =
882 dyn_cast(StrNCpy.getCallee()->stripPointerCasts()))
877 if (const Function *F = dyn_cast(StrNCpy->stripPointerCasts()))
883878 CI->setCallingConv(F->getCallingConv());
884879 return CI;
885880 }
895890 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
896891 Attribute::NoUnwind);
897892 LLVMContext &Context = B.GetInsertBlock()->getContext();
898 FunctionCallee MemCpy = M->getOrInsertFunction(
893 Value *MemCpy = M->getOrInsertFunction(
899894 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
900895 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
901896 DL.getIntPtrType(Context));
902897 Dst = castToCStr(Dst, B);
903898 Src = castToCStr(Src, B);
904899 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
905 if (const Function *F =
906 dyn_cast(MemCpy.getCallee()->stripPointerCasts()))
900 if (const Function *F = dyn_cast(MemCpy->stripPointerCasts()))
907901 CI->setCallingConv(F->getCallingConv());
908902 return CI;
909903 }
916910 Module *M = B.GetInsertBlock()->getModule();
917911 StringRef MemChrName = TLI->getName(LibFunc_memchr);
918912 LLVMContext &Context = B.GetInsertBlock()->getContext();
919 FunctionCallee MemChr =
920 M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), B.getInt8PtrTy(),
921 B.getInt32Ty(), DL.getIntPtrType(Context));
913 Value *MemChr = M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(),
914 B.getInt8PtrTy(), B.getInt32Ty(),
915 DL.getIntPtrType(Context));
922916 inferLibFuncAttributes(M, MemChrName, *TLI);
923917 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
924918
925 if (const Function *F =
926 dyn_cast(MemChr.getCallee()->stripPointerCasts()))
919 if (const Function *F = dyn_cast(MemChr->stripPointerCasts()))
927920 CI->setCallingConv(F->getCallingConv());
928921
929922 return CI;
937930 Module *M = B.GetInsertBlock()->getModule();
938931 StringRef MemCmpName = TLI->getName(LibFunc_memcmp);
939932 LLVMContext &Context = B.GetInsertBlock()->getContext();
940 FunctionCallee MemCmp =
941 M->getOrInsertFunction(MemCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
942 B.getInt8PtrTy(), DL.getIntPtrType(Context));
933 Value *MemCmp = M->getOrInsertFunction(MemCmpName, B.getInt32Ty(),
934 B.getInt8PtrTy(), B.getInt8PtrTy(),
935 DL.getIntPtrType(Context));
943936 inferLibFuncAttributes(M, MemCmpName, *TLI);
944937 CallInst *CI = B.CreateCall(
945938 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName);
946939
947 if (const Function *F =
948 dyn_cast(MemCmp.getCallee()->stripPointerCasts()))
940 if (const Function *F = dyn_cast(MemCmp->stripPointerCasts()))
949941 CI->setCallingConv(F->getCallingConv());
950942
951943 return CI;
972964 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
973965
974966 Module *M = B.GetInsertBlock()->getModule();
975 FunctionCallee Callee =
976 M->getOrInsertFunction(Name, Op->getType(), Op->getType());
967 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
968 Op->getType());
977969 CallInst *CI = B.CreateCall(Callee, Op, Name);
978970
979971 // The incoming attribute set may have come from a speculatable intrinsic, but
982974 CI->setAttributes(Attrs.removeAttribute(B.getContext(),
983975 AttributeList::FunctionIndex,
984976 Attribute::Speculatable));
985 if (const Function *F =
986 dyn_cast(Callee.getCallee()->stripPointerCasts()))
977 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
987978 CI->setCallingConv(F->getCallingConv());
988979
989980 return CI;
10161007 appendTypeSuffix(Op1, Name, NameBuffer);
10171008
10181009 Module *M = B.GetInsertBlock()->getModule();
1019 FunctionCallee Callee = M->getOrInsertFunction(
1020 Name, Op1->getType(), Op1->getType(), Op2->getType());
1010 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
1011 Op2->getType());
10211012 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
10221013 CI->setAttributes(Attrs);
1023 if (const Function *F =
1024 dyn_cast(Callee.getCallee()->stripPointerCasts()))
1014 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
10251015 CI->setCallingConv(F->getCallingConv());
10261016
10271017 return CI;
10341024
10351025 Module *M = B.GetInsertBlock()->getModule();
10361026 StringRef PutCharName = TLI->getName(LibFunc_putchar);
1037 FunctionCallee PutChar =
1038 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
1027 Value *PutChar = M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
10391028 inferLibFuncAttributes(M, PutCharName, *TLI);
10401029 CallInst *CI = B.CreateCall(PutChar,
10411030 B.CreateIntCast(Char,
10441033 "chari"),
10451034 PutCharName);
10461035
1047 if (const Function *F =
1048 dyn_cast(PutChar.getCallee()->stripPointerCasts()))
1036 if (const Function *F = dyn_cast(PutChar->stripPointerCasts()))
10491037 CI->setCallingConv(F->getCallingConv());
10501038 return CI;
10511039 }
10571045
10581046 Module *M = B.GetInsertBlock()->getModule();
10591047 StringRef PutsName = TLI->getName(LibFunc_puts);
1060 FunctionCallee PutS =
1048 Value *PutS =
10611049 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
10621050 inferLibFuncAttributes(M, PutsName, *TLI);
10631051 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
1064 if (const Function *F =
1065 dyn_cast(PutS.getCallee()->stripPointerCasts()))
1052 if (const Function *F = dyn_cast(PutS->stripPointerCasts()))
10661053 CI->setCallingConv(F->getCallingConv());
10671054 return CI;
10681055 }
10741061
10751062 Module *M = B.GetInsertBlock()->getModule();
10761063 StringRef FPutcName = TLI->getName(LibFunc_fputc);
1077 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
1078 B.getInt32Ty(), File->getType());
1064 Constant *F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), B.getInt32Ty(),
1065 File->getType());
10791066 if (File->getType()->isPointerTy())
10801067 inferLibFuncAttributes(M, FPutcName, *TLI);
10811068 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
10821069 "chari");
10831070 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
10841071
1085 if (const Function *Fn =
1086 dyn_cast(F.getCallee()->stripPointerCasts()))
1072 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
10871073 CI->setCallingConv(Fn->getCallingConv());
10881074 return CI;
10891075 }
10951081
10961082 Module *M = B.GetInsertBlock()->getModule();
10971083 StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
1098 FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1099 B.getInt32Ty(), File->getType());
1084 Constant *F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1085 B.getInt32Ty(), File->getType());
11001086 if (File->getType()->isPointerTy())
11011087 inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
11021088 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
11031089 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
11041090
1105 if (const Function *Fn =
1106 dyn_cast(F.getCallee()->stripPointerCasts()))
1091 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11071092 CI->setCallingConv(Fn->getCallingConv());
11081093 return CI;
11091094 }
11151100
11161101 Module *M = B.GetInsertBlock()->getModule();
11171102 StringRef FPutsName = TLI->getName(LibFunc_fputs);
1118 FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
1119 B.getInt8PtrTy(), File->getType());
1103 Constant *F = M->getOrInsertFunction(
1104 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
11201105 if (File->getType()->isPointerTy())
11211106 inferLibFuncAttributes(M, FPutsName, *TLI);
11221107 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
11231108
1124 if (const Function *Fn =
1125 dyn_cast(F.getCallee()->stripPointerCasts()))
1109 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11261110 CI->setCallingConv(Fn->getCallingConv());
11271111 return CI;
11281112 }
11341118
11351119 Module *M = B.GetInsertBlock()->getModule();
11361120 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
1137 FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1138 B.getInt8PtrTy(), File->getType());
1121 Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1122 B.getInt8PtrTy(), File->getType());
11391123 if (File->getType()->isPointerTy())
11401124 inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
11411125 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
11421126
1143 if (const Function *Fn =
1144 dyn_cast(F.getCallee()->stripPointerCasts()))
1127 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11451128 CI->setCallingConv(Fn->getCallingConv());
11461129 return CI;
11471130 }
11541137 Module *M = B.GetInsertBlock()->getModule();
11551138 LLVMContext &Context = B.GetInsertBlock()->getContext();
11561139 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1157 FunctionCallee F = M->getOrInsertFunction(
1140 Constant *F = M->getOrInsertFunction(
11581141 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
11591142 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
11601143
11641147 B.CreateCall(F, {castToCStr(Ptr, B), Size,
11651148 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
11661149
1167 if (const Function *Fn =
1168 dyn_cast(F.getCallee()->stripPointerCasts()))
1150 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
11691151 CI->setCallingConv(Fn->getCallingConv());
11701152 return CI;
11711153 }
11781160 Module *M = B.GetInsertBlock()->getModule();
11791161 StringRef MallocName = TLI->getName(LibFunc_malloc);
11801162 LLVMContext &Context = B.GetInsertBlock()->getContext();
1181 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1182 DL.getIntPtrType(Context));
1163 Value *Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1164 DL.getIntPtrType(Context));
11831165 inferLibFuncAttributes(M, MallocName, *TLI);
11841166 CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
11851167
1186 if (const Function *F =
1187 dyn_cast(Malloc.getCallee()->stripPointerCasts()))
1168 if (const Function *F = dyn_cast(Malloc->stripPointerCasts()))
11881169 CI->setCallingConv(F->getCallingConv());
11891170
11901171 return CI;
11991180 StringRef CallocName = TLI.getName(LibFunc_calloc);
12001181 const DataLayout &DL = M->getDataLayout();
12011182 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1202 FunctionCallee Calloc = M->getOrInsertFunction(
1203 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
1183 Value *Calloc = M->getOrInsertFunction(CallocName, Attrs, B.getInt8PtrTy(),
1184 PtrType, PtrType);
12041185 inferLibFuncAttributes(M, CallocName, TLI);
12051186 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
12061187
1207 if (const auto *F =
1208 dyn_cast(Calloc.getCallee()->stripPointerCasts()))
1188 if (const auto *F = dyn_cast(Calloc->stripPointerCasts()))
12091189 CI->setCallingConv(F->getCallingConv());
12101190
12111191 return CI;
12201200 Module *M = B.GetInsertBlock()->getModule();
12211201 LLVMContext &Context = B.GetInsertBlock()->getContext();
12221202 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
1223 FunctionCallee F = M->getOrInsertFunction(
1203 Constant *F = M->getOrInsertFunction(
12241204 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
12251205 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
12261206
12281208 inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
12291209 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
12301210
1231 if (const Function *Fn =
1232 dyn_cast(F.getCallee()->stripPointerCasts()))
1211 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
12331212 CI->setCallingConv(Fn->getCallingConv());
12341213 return CI;
12351214 }
12411220
12421221 Module *M = B.GetInsertBlock()->getModule();
12431222 StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
1244 FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(),
1245 File->getType());
1223 Constant *F =
1224 M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), File->getType());
12461225 if (File->getType()->isPointerTy())
12471226 inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
12481227 CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
12491228
1250 if (const Function *Fn =
1251 dyn_cast(F.getCallee()->stripPointerCasts()))
1229 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
12521230 CI->setCallingConv(Fn->getCallingConv());
12531231 return CI;
12541232 }
12601238
12611239 Module *M = B.GetInsertBlock()->getModule();
12621240 StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
1263 FunctionCallee F =
1241 Constant *F =
12641242 M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
12651243 B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
12661244 inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
12671245 CallInst *CI =
12681246 B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
12691247
1270 if (const Function *Fn =
1271 dyn_cast(F.getCallee()->stripPointerCasts()))
1248 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
12721249 CI->setCallingConv(Fn->getCallingConv());
12731250 return CI;
12741251 }
12821259 Module *M = B.GetInsertBlock()->getModule();
12831260 LLVMContext &Context = B.GetInsertBlock()->getContext();
12841261 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
1285 FunctionCallee F = M->getOrInsertFunction(
1262 Constant *F = M->getOrInsertFunction(
12861263 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
12871264 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
12881265
12901267 inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
12911268 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
12921269
1293 if (const Function *Fn =
1294 dyn_cast(F.getCallee()->stripPointerCasts()))
1270 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
12951271 CI->setCallingConv(Fn->getCallingConv());
12961272 return CI;
12971273 }
2929 Func == "__mcount" ||
3030 Func == "_mcount" ||
3131 Func == "__cyg_profile_func_enter_bare") {
32 FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
32 Constant *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 FunctionCallee Fn = M.getOrInsertFunction(
41 Constant *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 FunctionCallee getDefaultPersonalityFn(Module *M) {
20 static Constant *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 FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
71 F.setPersonalityFn(cast(PersFn.getCallee()));
70 Constant *PersFn = getDefaultPersonalityFn(F.getParent());
71 F.setPersonalityFn(PersFn);
7272 }
7373
7474 if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
125125 appendToUsedList(M, "llvm.compiler.used", Values);
126126 }
127127
128 FunctionCallee
129 llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
130 ArrayRef InitArgTypes) {
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) {
131141 assert(!InitName.empty() && "Expected init function name");
132 return M.getOrInsertFunction(
142 Function *F = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
133143 InitName,
134144 FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
135 AttributeList());
136 }
137
138 std::pair llvm::createSanitizerCtorAndInitFunctions(
145 AttributeList()));
146 F->setLinkage(Function::ExternalLinkage);
147 return F;
148 }
149
150 std::pair llvm::createSanitizerCtorAndInitFunctions(
139151 Module &M, StringRef CtorName, StringRef InitName,
140152 ArrayRef InitArgTypes, ArrayRef InitArgs,
141153 StringRef VersionCheckName) {
142154 assert(!InitName.empty() && "Expected init function name");
143155 assert(InitArgs.size() == InitArgTypes.size() &&
144156 "Sanitizer's init function expects different number of arguments");
145 FunctionCallee InitFunction =
157 Function *InitFunction =
146158 declareSanitizerInitFunction(M, InitName, InitArgTypes);
147159 Function *Ctor = Function::Create(
148160 FunctionType::get(Type::getVoidTy(M.getContext()), false),
151163 IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
152164 IRB.CreateCall(InitFunction, InitArgs);
153165 if (!VersionCheckName.empty()) {
154 FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
155 VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
156 AttributeList());
166 Function *VersionCheckFunction =
167 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
168 VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
169 AttributeList()));
157170 IRB.CreateCall(VersionCheckFunction, {});
158171 }
159172 return std::make_pair(Ctor, InitFunction);
160173 }
161174
162 std::pairCallee>
175 std::pair *>
163176 llvm::getOrCreateSanitizerCtorAndInitFunctions(
164177 Module &M, StringRef CtorName, StringRef InitName,
165178 ArrayRef InitArgTypes, ArrayRef InitArgs,
166 function_refCallee)> FunctionsCreatedCallback,
179 function_ref *)> FunctionsCreatedCallback,
167180 StringRef VersionCheckName) {
168181 assert(!CtorName.empty() && "Expected ctor function name");
169182 <