1 //===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the IRBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Statepoint.h"
21 using namespace llvm;
22
23 /// CreateGlobalString - Make a new global variable with an initializer that
24 /// has array of i8 type filled in with the nul terminated string value
25 /// specified. If Name is specified, it is the name of the global variable
26 /// created.
CreateGlobalString(StringRef Str,const Twine & Name,unsigned AddressSpace)27 GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
28 const Twine &Name,
29 unsigned AddressSpace) {
30 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
31 Module &M = *BB->getParent()->getParent();
32 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
33 true, GlobalValue::PrivateLinkage,
34 StrConstant, Name, nullptr,
35 GlobalVariable::NotThreadLocal,
36 AddressSpace);
37 GV->setUnnamedAddr(true);
38 return GV;
39 }
40
getCurrentFunctionReturnType() const41 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
42 assert(BB && BB->getParent() && "No current function!");
43 return BB->getParent()->getReturnType();
44 }
45
getCastedInt8PtrValue(Value * Ptr)46 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
47 PointerType *PT = cast<PointerType>(Ptr->getType());
48 if (PT->getElementType()->isIntegerTy(8))
49 return Ptr;
50
51 // Otherwise, we need to insert a bitcast.
52 PT = getInt8PtrTy(PT->getAddressSpace());
53 BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
54 BB->getInstList().insert(InsertPt, BCI);
55 SetInstDebugLocation(BCI);
56 return BCI;
57 }
58
createCallHelper(Value * Callee,ArrayRef<Value * > Ops,IRBuilderBase * Builder,const Twine & Name="")59 static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
60 IRBuilderBase *Builder,
61 const Twine& Name="") {
62 CallInst *CI = CallInst::Create(Callee, Ops, Name);
63 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
64 Builder->SetInstDebugLocation(CI);
65 return CI;
66 }
67
createInvokeHelper(Value * Invokee,BasicBlock * NormalDest,BasicBlock * UnwindDest,ArrayRef<Value * > Ops,IRBuilderBase * Builder,const Twine & Name="")68 static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest,
69 BasicBlock *UnwindDest,
70 ArrayRef<Value *> Ops,
71 IRBuilderBase *Builder,
72 const Twine &Name = "") {
73 InvokeInst *II =
74 InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name);
75 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),
76 II);
77 Builder->SetInstDebugLocation(II);
78 return II;
79 }
80
81 CallInst *IRBuilderBase::
CreateMemSet(Value * Ptr,Value * Val,Value * Size,unsigned Align,bool isVolatile,MDNode * TBAATag,MDNode * ScopeTag,MDNode * NoAliasTag)82 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
83 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
84 MDNode *NoAliasTag) {
85 Ptr = getCastedInt8PtrValue(Ptr);
86 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
87 Type *Tys[] = { Ptr->getType(), Size->getType() };
88 Module *M = BB->getParent()->getParent();
89 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
90
91 CallInst *CI = createCallHelper(TheFn, Ops, this);
92
93 // Set the TBAA info if present.
94 if (TBAATag)
95 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
96
97 if (ScopeTag)
98 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
99
100 if (NoAliasTag)
101 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
102
103 return CI;
104 }
105
106 CallInst *IRBuilderBase::
CreateMemCpy(Value * Dst,Value * Src,Value * Size,unsigned Align,bool isVolatile,MDNode * TBAATag,MDNode * TBAAStructTag,MDNode * ScopeTag,MDNode * NoAliasTag)107 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
108 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
109 MDNode *ScopeTag, MDNode *NoAliasTag) {
110 Dst = getCastedInt8PtrValue(Dst);
111 Src = getCastedInt8PtrValue(Src);
112
113 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
114 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
115 Module *M = BB->getParent()->getParent();
116 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
117
118 CallInst *CI = createCallHelper(TheFn, Ops, this);
119
120 // Set the TBAA info if present.
121 if (TBAATag)
122 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
123
124 // Set the TBAA Struct info if present.
125 if (TBAAStructTag)
126 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
127
128 if (ScopeTag)
129 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
130
131 if (NoAliasTag)
132 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
133
134 return CI;
135 }
136
137 CallInst *IRBuilderBase::
CreateMemMove(Value * Dst,Value * Src,Value * Size,unsigned Align,bool isVolatile,MDNode * TBAATag,MDNode * ScopeTag,MDNode * NoAliasTag)138 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
139 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
140 MDNode *NoAliasTag) {
141 Dst = getCastedInt8PtrValue(Dst);
142 Src = getCastedInt8PtrValue(Src);
143
144 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
145 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
146 Module *M = BB->getParent()->getParent();
147 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
148
149 CallInst *CI = createCallHelper(TheFn, Ops, this);
150
151 // Set the TBAA info if present.
152 if (TBAATag)
153 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
154
155 if (ScopeTag)
156 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
157
158 if (NoAliasTag)
159 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
160
161 return CI;
162 }
163
CreateLifetimeStart(Value * Ptr,ConstantInt * Size)164 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
165 assert(isa<PointerType>(Ptr->getType()) &&
166 "lifetime.start only applies to pointers.");
167 Ptr = getCastedInt8PtrValue(Ptr);
168 if (!Size)
169 Size = getInt64(-1);
170 else
171 assert(Size->getType() == getInt64Ty() &&
172 "lifetime.start requires the size to be an i64");
173 Value *Ops[] = { Size, Ptr };
174 Module *M = BB->getParent()->getParent();
175 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
176 return createCallHelper(TheFn, Ops, this);
177 }
178
CreateLifetimeEnd(Value * Ptr,ConstantInt * Size)179 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
180 assert(isa<PointerType>(Ptr->getType()) &&
181 "lifetime.end only applies to pointers.");
182 Ptr = getCastedInt8PtrValue(Ptr);
183 if (!Size)
184 Size = getInt64(-1);
185 else
186 assert(Size->getType() == getInt64Ty() &&
187 "lifetime.end requires the size to be an i64");
188 Value *Ops[] = { Size, Ptr };
189 Module *M = BB->getParent()->getParent();
190 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
191 return createCallHelper(TheFn, Ops, this);
192 }
193
CreateAssumption(Value * Cond)194 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
195 assert(Cond->getType() == getInt1Ty() &&
196 "an assumption condition must be of type i1");
197
198 Value *Ops[] = { Cond };
199 Module *M = BB->getParent()->getParent();
200 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
201 return createCallHelper(FnAssume, Ops, this);
202 }
203
204 /// Create a call to a Masked Load intrinsic.
205 /// Ptr - the base pointer for the load
206 /// Align - alignment of the source location
207 /// Mask - an vector of booleans which indicates what vector lanes should
208 /// be accessed in memory
209 /// PassThru - a pass-through value that is used to fill the masked-off lanes
210 /// of the result
211 /// Name - name of the result variable
CreateMaskedLoad(Value * Ptr,unsigned Align,Value * Mask,Value * PassThru,const Twine & Name)212 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
213 Value *Mask, Value *PassThru,
214 const Twine &Name) {
215 assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type");
216 // DataTy is the overloaded type
217 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
218 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
219 if (!PassThru)
220 PassThru = UndefValue::get(DataTy);
221 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
222 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
223 }
224
225 /// Create a call to a Masked Store intrinsic.
226 /// Val - the data to be stored,
227 /// Ptr - the base pointer for the store
228 /// Align - alignment of the destination location
229 /// Mask - an vector of booleans which indicates what vector lanes should
230 /// be accessed in memory
CreateMaskedStore(Value * Val,Value * Ptr,unsigned Align,Value * Mask)231 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
232 unsigned Align, Value *Mask) {
233 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
234 // Type of the data to be stored - the only one overloaded type
235 return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
236 }
237
238 /// Create a call to a Masked intrinsic, with given intrinsic Id,
239 /// an array of operands - Ops, and one overloaded type - DataTy
CreateMaskedIntrinsic(Intrinsic::ID Id,ArrayRef<Value * > Ops,Type * DataTy,const Twine & Name)240 CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
241 ArrayRef<Value *> Ops,
242 Type *DataTy,
243 const Twine &Name) {
244 Module *M = BB->getParent()->getParent();
245 Type *OverloadedTypes[] = { DataTy };
246 Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
247 return createCallHelper(TheFn, Ops, this, Name);
248 }
249
250 static std::vector<Value *>
getStatepointArgs(IRBuilderBase & B,uint64_t ID,uint32_t NumPatchBytes,Value * ActualCallee,ArrayRef<Value * > CallArgs,ArrayRef<Value * > DeoptArgs,ArrayRef<Value * > GCArgs)251 getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
252 Value *ActualCallee, ArrayRef<Value *> CallArgs,
253 ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs) {
254 std::vector<Value *> Args;
255 Args.push_back(B.getInt64(ID));
256 Args.push_back(B.getInt32(NumPatchBytes));
257 Args.push_back(ActualCallee);
258 Args.push_back(B.getInt32(CallArgs.size()));
259 Args.push_back(B.getInt32((unsigned)StatepointFlags::None));
260 Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
261 Args.push_back(B.getInt32(0 /* no transition args */));
262 Args.push_back(B.getInt32(DeoptArgs.size()));
263 Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
264 Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
265
266 return Args;
267 }
268
CreateGCStatepointCall(uint64_t ID,uint32_t NumPatchBytes,Value * ActualCallee,ArrayRef<Value * > CallArgs,ArrayRef<Value * > DeoptArgs,ArrayRef<Value * > GCArgs,const Twine & Name)269 CallInst *IRBuilderBase::CreateGCStatepointCall(
270 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
271 ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
272 ArrayRef<Value *> GCArgs, const Twine &Name) {
273 // Extract out the type of the callee.
274 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
275 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
276 "actual callee must be a callable value");
277
278 Module *M = BB->getParent()->getParent();
279 // Fill in the one generic type'd argument (the function is also vararg)
280 Type *ArgTypes[] = { FuncPtrType };
281 Function *FnStatepoint =
282 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
283 ArgTypes);
284
285 std::vector<llvm::Value *> Args = getStatepointArgs(
286 *this, ID, NumPatchBytes, ActualCallee, CallArgs, DeoptArgs, GCArgs);
287 return createCallHelper(FnStatepoint, Args, this, Name);
288 }
289
CreateGCStatepointCall(uint64_t ID,uint32_t NumPatchBytes,Value * ActualCallee,ArrayRef<Use> CallArgs,ArrayRef<Value * > DeoptArgs,ArrayRef<Value * > GCArgs,const Twine & Name)290 CallInst *IRBuilderBase::CreateGCStatepointCall(
291 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
292 ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
293 ArrayRef<Value *> GCArgs, const Twine &Name) {
294 std::vector<Value *> VCallArgs;
295 for (auto &U : CallArgs)
296 VCallArgs.push_back(U.get());
297 return CreateGCStatepointCall(ID, NumPatchBytes, ActualCallee, VCallArgs,
298 DeoptArgs, GCArgs, Name);
299 }
300
CreateGCStatepointInvoke(uint64_t ID,uint32_t NumPatchBytes,Value * ActualInvokee,BasicBlock * NormalDest,BasicBlock * UnwindDest,ArrayRef<Value * > InvokeArgs,ArrayRef<Value * > DeoptArgs,ArrayRef<Value * > GCArgs,const Twine & Name)301 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
302 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
303 BasicBlock *NormalDest, BasicBlock *UnwindDest,
304 ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
305 ArrayRef<Value *> GCArgs, const Twine &Name) {
306 // Extract out the type of the callee.
307 PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
308 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
309 "actual callee must be a callable value");
310
311 Module *M = BB->getParent()->getParent();
312 // Fill in the one generic type'd argument (the function is also vararg)
313 Function *FnStatepoint = Intrinsic::getDeclaration(
314 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
315
316 std::vector<llvm::Value *> Args = getStatepointArgs(
317 *this, ID, NumPatchBytes, ActualInvokee, InvokeArgs, DeoptArgs, GCArgs);
318 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, this,
319 Name);
320 }
321
CreateGCStatepointInvoke(uint64_t ID,uint32_t NumPatchBytes,Value * ActualInvokee,BasicBlock * NormalDest,BasicBlock * UnwindDest,ArrayRef<Use> InvokeArgs,ArrayRef<Value * > DeoptArgs,ArrayRef<Value * > GCArgs,const Twine & Name)322 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
323 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
324 BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
325 ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
326 std::vector<Value *> VCallArgs;
327 for (auto &U : InvokeArgs)
328 VCallArgs.push_back(U.get());
329 return CreateGCStatepointInvoke(ID, NumPatchBytes, ActualInvokee, NormalDest,
330 UnwindDest, VCallArgs, DeoptArgs, GCArgs,
331 Name);
332 }
333
CreateGCResult(Instruction * Statepoint,Type * ResultType,const Twine & Name)334 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
335 Type *ResultType,
336 const Twine &Name) {
337 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
338 Module *M = BB->getParent()->getParent();
339 Type *Types[] = {ResultType};
340 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
341
342 Value *Args[] = {Statepoint};
343 return createCallHelper(FnGCResult, Args, this, Name);
344 }
345
CreateGCRelocate(Instruction * Statepoint,int BaseOffset,int DerivedOffset,Type * ResultType,const Twine & Name)346 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
347 int BaseOffset,
348 int DerivedOffset,
349 Type *ResultType,
350 const Twine &Name) {
351 Module *M = BB->getParent()->getParent();
352 Type *Types[] = {ResultType};
353 Value *FnGCRelocate =
354 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
355
356 Value *Args[] = {Statepoint,
357 getInt32(BaseOffset),
358 getInt32(DerivedOffset)};
359 return createCallHelper(FnGCRelocate, Args, this, Name);
360 }
361