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.
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 
41 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
42   assert(BB && BB->getParent() && "No current function!");
43   return BB->getParent()->getReturnType();
44 }
45 
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 
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 
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::
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::
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::
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 
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 
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 
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 /// \brief Create a call to a Masked Load intrinsic.
205 /// \p Ptr      - base pointer for the load
206 /// \p Align    - alignment of the source location
207 /// \p Mask     - vector of booleans which indicates what vector lanes should
208 ///               be accessed in memory
209 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
210 ///               of the result
211 /// \p Name     - name of the result variable
212 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
213                                           Value *Mask, Value *PassThru,
214                                           const Twine &Name) {
215   // DataTy is the overloaded type
216   Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
217   assert(DataTy->isVectorTy() && "Ptr should point to a vector");
218   if (!PassThru)
219     PassThru = UndefValue::get(DataTy);
220   Value *Ops[] = { Ptr, getInt32(Align), Mask,  PassThru};
221   return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
222 }
223 
224 /// \brief Create a call to a Masked Store intrinsic.
225 /// \p Val   - data to be stored,
226 /// \p Ptr   - base pointer for the store
227 /// \p Align - alignment of the destination location
228 /// \p Mask  - vector of booleans which indicates what vector lanes should
229 ///            be accessed in memory
230 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
231                                            unsigned Align, Value *Mask) {
232   Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
233   // Type of the data to be stored - the only one overloaded type
234   return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
235 }
236 
237 /// Create a call to a Masked intrinsic, with given intrinsic Id,
238 /// an array of operands - Ops, and one overloaded type - DataTy
239 CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
240                                                ArrayRef<Value *> Ops,
241                                                Type *DataTy,
242                                                const Twine &Name) {
243   Module *M = BB->getParent()->getParent();
244   Type *OverloadedTypes[] = { DataTy };
245   Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
246   return createCallHelper(TheFn, Ops, this, Name);
247 }
248 
249 /// \brief Create a call to a Masked Gather intrinsic.
250 /// \p Ptrs     - vector of pointers for loading
251 /// \p Align    - alignment for one element
252 /// \p Mask     - vector of booleans which indicates what vector lanes should
253 ///               be accessed in memory
254 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
255 ///               of the result
256 /// \p Name     - name of the result variable
257 CallInst *IRBuilderBase::CreateMaskedGather(Value *Ptrs, unsigned Align,
258                                             Value *Mask,  Value *PassThru,
259                                             const Twine& Name) {
260   auto PtrsTy = cast<VectorType>(Ptrs->getType());
261   auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
262   unsigned NumElts = PtrsTy->getVectorNumElements();
263   Type *DataTy = VectorType::get(PtrTy->getElementType(), NumElts);
264 
265   if (!Mask)
266     Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
267                                      NumElts));
268 
269   Value * Ops[] = {Ptrs, getInt32(Align), Mask, UndefValue::get(DataTy)};
270 
271   // We specify only one type when we create this intrinsic. Types of other
272   // arguments are derived from this type.
273   return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, DataTy, Name);
274 }
275 
276 /// \brief Create a call to a Masked Scatter intrinsic.
277 /// \p Data  - data to be stored,
278 /// \p Ptrs  - the vector of pointers, where the \p Data elements should be
279 ///            stored
280 /// \p Align - alignment for one element
281 /// \p Mask  - vector of booleans which indicates what vector lanes should
282 ///            be accessed in memory
283 CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs,
284                                              unsigned Align, Value *Mask) {
285   auto PtrsTy = cast<VectorType>(Ptrs->getType());
286   auto DataTy = cast<VectorType>(Data->getType());
287   unsigned NumElts = PtrsTy->getVectorNumElements();
288 
289 #ifndef NDEBUG
290   auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
291   assert(NumElts == DataTy->getVectorNumElements() &&
292          PtrTy->getElementType() == DataTy->getElementType() &&
293          "Incompatible pointer and data types");
294 #endif
295 
296   if (!Mask)
297     Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
298                                      NumElts));
299   Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask};
300 
301   // We specify only one type when we create this intrinsic. Types of other
302   // arguments are derived from this type.
303   return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, DataTy);
304 }
305 
306 template <typename T0, typename T1, typename T2, typename T3>
307 static std::vector<Value *>
308 getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
309                   Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
310                   ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs,
311                   ArrayRef<T3> GCArgs) {
312   std::vector<Value *> Args;
313   Args.push_back(B.getInt64(ID));
314   Args.push_back(B.getInt32(NumPatchBytes));
315   Args.push_back(ActualCallee);
316   Args.push_back(B.getInt32(CallArgs.size()));
317   Args.push_back(B.getInt32(Flags));
318   Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
319   Args.push_back(B.getInt32(TransitionArgs.size()));
320   Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end());
321   Args.push_back(B.getInt32(DeoptArgs.size()));
322   Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
323   Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
324 
325   return Args;
326 }
327 
328 template <typename T0, typename T1, typename T2, typename T3>
329 static CallInst *CreateGCStatepointCallCommon(
330     IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
331     Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
332     ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
333     const Twine &Name) {
334   // Extract out the type of the callee.
335   PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
336   assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
337          "actual callee must be a callable value");
338 
339   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
340   // Fill in the one generic type'd argument (the function is also vararg)
341   Type *ArgTypes[] = { FuncPtrType };
342   Function *FnStatepoint =
343     Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
344                               ArgTypes);
345 
346   std::vector<llvm::Value *> Args =
347       getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
348                         CallArgs, TransitionArgs, DeoptArgs, GCArgs);
349   return createCallHelper(FnStatepoint, Args, Builder, Name);
350 }
351 
352 CallInst *IRBuilderBase::CreateGCStatepointCall(
353     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
354     ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
355     ArrayRef<Value *> GCArgs, const Twine &Name) {
356   return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>(
357       this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
358       CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name);
359 }
360 
361 CallInst *IRBuilderBase::CreateGCStatepointCall(
362     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags,
363     ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs,
364     ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
365   return CreateGCStatepointCallCommon<Use, Use, Use, Value *>(
366       this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs,
367       DeoptArgs, GCArgs, Name);
368 }
369 
370 CallInst *IRBuilderBase::CreateGCStatepointCall(
371     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
372     ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
373     ArrayRef<Value *> GCArgs, const Twine &Name) {
374   return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>(
375       this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
376       CallArgs, None, DeoptArgs, GCArgs, Name);
377 }
378 
379 template <typename T0, typename T1, typename T2, typename T3>
380 static InvokeInst *CreateGCStatepointInvokeCommon(
381     IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
382     Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
383     uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
384     ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
385   // Extract out the type of the callee.
386   PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
387   assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
388          "actual callee must be a callable value");
389 
390   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
391   // Fill in the one generic type'd argument (the function is also vararg)
392   Function *FnStatepoint = Intrinsic::getDeclaration(
393       M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
394 
395   std::vector<llvm::Value *> Args =
396       getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
397                         InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
398   return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
399                             Name);
400 }
401 
402 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
403     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
404     BasicBlock *NormalDest, BasicBlock *UnwindDest,
405     ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
406     ArrayRef<Value *> GCArgs, const Twine &Name) {
407   return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>(
408       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
409       uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/,
410       DeoptArgs, GCArgs, Name);
411 }
412 
413 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
414     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
415     BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
416     ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
417     ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
418   return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>(
419       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags,
420       InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name);
421 }
422 
423 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
424     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
425     BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
426     ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
427   return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>(
428       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
429       uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs,
430       Name);
431 }
432 
433 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
434                                        Type *ResultType,
435                                        const Twine &Name) {
436  Intrinsic::ID ID = Intrinsic::experimental_gc_result;
437  Module *M = BB->getParent()->getParent();
438  Type *Types[] = {ResultType};
439  Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
440 
441  Value *Args[] = {Statepoint};
442  return createCallHelper(FnGCResult, Args, this, Name);
443 }
444 
445 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
446                                          int BaseOffset,
447                                          int DerivedOffset,
448                                          Type *ResultType,
449                                          const Twine &Name) {
450  Module *M = BB->getParent()->getParent();
451  Type *Types[] = {ResultType};
452  Value *FnGCRelocate =
453    Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
454 
455  Value *Args[] = {Statepoint,
456                   getInt32(BaseOffset),
457                   getInt32(DerivedOffset)};
458  return createCallHelper(FnGCRelocate, Args, this, Name);
459 }
460