]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/IRBuilder.cpp
Merge ^/head r318560 through r318657.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / IRBuilder.cpp
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(GlobalValue::UnnamedAddr::Global);
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 static CallInst *getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID,
165                                     Value *Src) {
166   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
167   Value *Ops[] = {Src};
168   Type *Tys[] = { Src->getType()->getVectorElementType(), Src->getType() };
169   auto Decl = Intrinsic::getDeclaration(M, ID, Tys);
170   return createCallHelper(Decl, Ops, Builder);
171 }
172
173 CallInst *IRBuilderBase::CreateFAddReduce(Value *Acc, Value *Src) {
174   Module *M = GetInsertBlock()->getParent()->getParent();
175   Value *Ops[] = {Acc, Src};
176   Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(),
177                  Src->getType()};
178   auto Decl = Intrinsic::getDeclaration(
179       M, Intrinsic::experimental_vector_reduce_fadd, Tys);
180   return createCallHelper(Decl, Ops, this);
181 }
182
183 CallInst *IRBuilderBase::CreateFMulReduce(Value *Acc, Value *Src) {
184   Module *M = GetInsertBlock()->getParent()->getParent();
185   Value *Ops[] = {Acc, Src};
186   Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(),
187                  Src->getType()};
188   auto Decl = Intrinsic::getDeclaration(
189       M, Intrinsic::experimental_vector_reduce_fmul, Tys);
190   return createCallHelper(Decl, Ops, this);
191 }
192
193 CallInst *IRBuilderBase::CreateAddReduce(Value *Src) {
194   return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add,
195                                Src);
196 }
197
198 CallInst *IRBuilderBase::CreateMulReduce(Value *Src) {
199   return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_mul,
200                                Src);
201 }
202
203 CallInst *IRBuilderBase::CreateAndReduce(Value *Src) {
204   return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_and,
205                                Src);
206 }
207
208 CallInst *IRBuilderBase::CreateOrReduce(Value *Src) {
209   return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_or,
210                                Src);
211 }
212
213 CallInst *IRBuilderBase::CreateXorReduce(Value *Src) {
214   return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_xor,
215                                Src);
216 }
217
218 CallInst *IRBuilderBase::CreateIntMaxReduce(Value *Src, bool IsSigned) {
219   auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smax
220                      : Intrinsic::experimental_vector_reduce_umax;
221   return getReductionIntrinsic(this, ID, Src);
222 }
223
224 CallInst *IRBuilderBase::CreateIntMinReduce(Value *Src, bool IsSigned) {
225   auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smin
226                      : Intrinsic::experimental_vector_reduce_umin;
227   return getReductionIntrinsic(this, ID, Src);
228 }
229
230 CallInst *IRBuilderBase::CreateFPMaxReduce(Value *Src, bool NoNaN) {
231   auto Rdx = getReductionIntrinsic(
232       this, Intrinsic::experimental_vector_reduce_fmax, Src);
233   if (NoNaN) {
234     FastMathFlags FMF;
235     FMF.setNoNaNs();
236     Rdx->setFastMathFlags(FMF);
237   }
238   return Rdx;
239 }
240
241 CallInst *IRBuilderBase::CreateFPMinReduce(Value *Src, bool NoNaN) {
242   auto Rdx = getReductionIntrinsic(
243       this, Intrinsic::experimental_vector_reduce_fmin, Src);
244   if (NoNaN) {
245     FastMathFlags FMF;
246     FMF.setNoNaNs();
247     Rdx->setFastMathFlags(FMF);
248   }
249   return Rdx;
250 }
251
252 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
253   assert(isa<PointerType>(Ptr->getType()) &&
254          "lifetime.start only applies to pointers.");
255   Ptr = getCastedInt8PtrValue(Ptr);
256   if (!Size)
257     Size = getInt64(-1);
258   else
259     assert(Size->getType() == getInt64Ty() &&
260            "lifetime.start requires the size to be an i64");
261   Value *Ops[] = { Size, Ptr };
262   Module *M = BB->getParent()->getParent();
263   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start,
264                                            { Ptr->getType() });
265   return createCallHelper(TheFn, Ops, this);
266 }
267
268 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
269   assert(isa<PointerType>(Ptr->getType()) &&
270          "lifetime.end only applies to pointers.");
271   Ptr = getCastedInt8PtrValue(Ptr);
272   if (!Size)
273     Size = getInt64(-1);
274   else
275     assert(Size->getType() == getInt64Ty() &&
276            "lifetime.end requires the size to be an i64");
277   Value *Ops[] = { Size, Ptr };
278   Module *M = BB->getParent()->getParent();
279   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end,
280                                            { Ptr->getType() });
281   return createCallHelper(TheFn, Ops, this);
282 }
283
284 CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) {
285
286   assert(isa<PointerType>(Ptr->getType()) &&
287          "invariant.start only applies to pointers.");
288   Ptr = getCastedInt8PtrValue(Ptr);
289   if (!Size)
290     Size = getInt64(-1);
291   else
292     assert(Size->getType() == getInt64Ty() &&
293            "invariant.start requires the size to be an i64");
294
295   Value *Ops[] = {Size, Ptr};
296   // Fill in the single overloaded type: memory object type.
297   Type *ObjectPtr[1] = {Ptr->getType()};
298   Module *M = BB->getParent()->getParent();
299   Value *TheFn =
300       Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr);
301   return createCallHelper(TheFn, Ops, this);
302 }
303
304 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
305   assert(Cond->getType() == getInt1Ty() &&
306          "an assumption condition must be of type i1");
307
308   Value *Ops[] = { Cond };
309   Module *M = BB->getParent()->getParent();
310   Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
311   return createCallHelper(FnAssume, Ops, this);
312 }
313
314 /// \brief Create a call to a Masked Load intrinsic.
315 /// \p Ptr      - base pointer for the load
316 /// \p Align    - alignment of the source location
317 /// \p Mask     - vector of booleans which indicates what vector lanes should
318 ///               be accessed in memory
319 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
320 ///               of the result
321 /// \p Name     - name of the result variable
322 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
323                                           Value *Mask, Value *PassThru,
324                                           const Twine &Name) {
325   PointerType *PtrTy = cast<PointerType>(Ptr->getType());
326   Type *DataTy = PtrTy->getElementType();
327   assert(DataTy->isVectorTy() && "Ptr should point to a vector");
328   if (!PassThru)
329     PassThru = UndefValue::get(DataTy);
330   Type *OverloadedTypes[] = { DataTy, PtrTy };
331   Value *Ops[] = { Ptr, getInt32(Align), Mask,  PassThru};
332   return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops,
333                                OverloadedTypes, Name);
334 }
335
336 /// \brief Create a call to a Masked Store intrinsic.
337 /// \p Val   - data to be stored,
338 /// \p Ptr   - base pointer for the store
339 /// \p Align - alignment of the destination location
340 /// \p Mask  - vector of booleans which indicates what vector lanes should
341 ///            be accessed in memory
342 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
343                                            unsigned Align, Value *Mask) {
344   PointerType *PtrTy = cast<PointerType>(Ptr->getType());
345   Type *DataTy = PtrTy->getElementType();
346   assert(DataTy->isVectorTy() && "Ptr should point to a vector");
347   Type *OverloadedTypes[] = { DataTy, PtrTy };
348   Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
349   return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, OverloadedTypes);
350 }
351
352 /// Create a call to a Masked intrinsic, with given intrinsic Id,
353 /// an array of operands - Ops, and an array of overloaded types -
354 /// OverloadedTypes.
355 CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
356                                                ArrayRef<Value *> Ops,
357                                                ArrayRef<Type *> OverloadedTypes,
358                                                const Twine &Name) {
359   Module *M = BB->getParent()->getParent();
360   Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
361   return createCallHelper(TheFn, Ops, this, Name);
362 }
363
364 /// \brief Create a call to a Masked Gather intrinsic.
365 /// \p Ptrs     - vector of pointers for loading
366 /// \p Align    - alignment for one element
367 /// \p Mask     - vector of booleans which indicates what vector lanes should
368 ///               be accessed in memory
369 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
370 ///               of the result
371 /// \p Name     - name of the result variable
372 CallInst *IRBuilderBase::CreateMaskedGather(Value *Ptrs, unsigned Align,
373                                             Value *Mask,  Value *PassThru,
374                                             const Twine& Name) {
375   auto PtrsTy = cast<VectorType>(Ptrs->getType());
376   auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
377   unsigned NumElts = PtrsTy->getVectorNumElements();
378   Type *DataTy = VectorType::get(PtrTy->getElementType(), NumElts);
379
380   if (!Mask)
381     Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
382                                      NumElts));
383
384   Type *OverloadedTypes[] = {DataTy, PtrsTy};
385   Value * Ops[] = {Ptrs, getInt32(Align), Mask, UndefValue::get(DataTy)};
386
387   // We specify only one type when we create this intrinsic. Types of other
388   // arguments are derived from this type.
389   return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes,
390                                Name);
391 }
392
393 /// \brief Create a call to a Masked Scatter intrinsic.
394 /// \p Data  - data to be stored,
395 /// \p Ptrs  - the vector of pointers, where the \p Data elements should be
396 ///            stored
397 /// \p Align - alignment for one element
398 /// \p Mask  - vector of booleans which indicates what vector lanes should
399 ///            be accessed in memory
400 CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs,
401                                              unsigned Align, Value *Mask) {
402   auto PtrsTy = cast<VectorType>(Ptrs->getType());
403   auto DataTy = cast<VectorType>(Data->getType());
404   unsigned NumElts = PtrsTy->getVectorNumElements();
405
406 #ifndef NDEBUG
407   auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
408   assert(NumElts == DataTy->getVectorNumElements() &&
409          PtrTy->getElementType() == DataTy->getElementType() &&
410          "Incompatible pointer and data types");
411 #endif
412
413   if (!Mask)
414     Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
415                                      NumElts));
416
417   Type *OverloadedTypes[] = {DataTy, PtrsTy};
418   Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask};
419
420   // We specify only one type when we create this intrinsic. Types of other
421   // arguments are derived from this type.
422   return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, OverloadedTypes);
423 }
424
425 template <typename T0, typename T1, typename T2, typename T3>
426 static std::vector<Value *>
427 getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
428                   Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
429                   ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs,
430                   ArrayRef<T3> GCArgs) {
431   std::vector<Value *> Args;
432   Args.push_back(B.getInt64(ID));
433   Args.push_back(B.getInt32(NumPatchBytes));
434   Args.push_back(ActualCallee);
435   Args.push_back(B.getInt32(CallArgs.size()));
436   Args.push_back(B.getInt32(Flags));
437   Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
438   Args.push_back(B.getInt32(TransitionArgs.size()));
439   Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end());
440   Args.push_back(B.getInt32(DeoptArgs.size()));
441   Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
442   Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
443
444   return Args;
445 }
446
447 template <typename T0, typename T1, typename T2, typename T3>
448 static CallInst *CreateGCStatepointCallCommon(
449     IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
450     Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
451     ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
452     const Twine &Name) {
453   // Extract out the type of the callee.
454   PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
455   assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
456          "actual callee must be a callable value");
457
458   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
459   // Fill in the one generic type'd argument (the function is also vararg)
460   Type *ArgTypes[] = { FuncPtrType };
461   Function *FnStatepoint =
462     Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
463                               ArgTypes);
464
465   std::vector<llvm::Value *> Args =
466       getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
467                         CallArgs, TransitionArgs, DeoptArgs, GCArgs);
468   return createCallHelper(FnStatepoint, Args, Builder, Name);
469 }
470
471 CallInst *IRBuilderBase::CreateGCStatepointCall(
472     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
473     ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
474     ArrayRef<Value *> GCArgs, const Twine &Name) {
475   return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>(
476       this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
477       CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name);
478 }
479
480 CallInst *IRBuilderBase::CreateGCStatepointCall(
481     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags,
482     ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs,
483     ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
484   return CreateGCStatepointCallCommon<Use, Use, Use, Value *>(
485       this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs,
486       DeoptArgs, GCArgs, Name);
487 }
488
489 CallInst *IRBuilderBase::CreateGCStatepointCall(
490     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
491     ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
492     ArrayRef<Value *> GCArgs, const Twine &Name) {
493   return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>(
494       this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
495       CallArgs, None, DeoptArgs, GCArgs, Name);
496 }
497
498 template <typename T0, typename T1, typename T2, typename T3>
499 static InvokeInst *CreateGCStatepointInvokeCommon(
500     IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
501     Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
502     uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
503     ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
504   // Extract out the type of the callee.
505   PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
506   assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
507          "actual callee must be a callable value");
508
509   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
510   // Fill in the one generic type'd argument (the function is also vararg)
511   Function *FnStatepoint = Intrinsic::getDeclaration(
512       M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
513
514   std::vector<llvm::Value *> Args =
515       getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
516                         InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
517   return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
518                             Name);
519 }
520
521 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
522     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
523     BasicBlock *NormalDest, BasicBlock *UnwindDest,
524     ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
525     ArrayRef<Value *> GCArgs, const Twine &Name) {
526   return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>(
527       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
528       uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/,
529       DeoptArgs, GCArgs, Name);
530 }
531
532 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
533     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
534     BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
535     ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
536     ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
537   return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>(
538       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags,
539       InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name);
540 }
541
542 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
543     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
544     BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
545     ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
546   return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>(
547       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
548       uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs,
549       Name);
550 }
551
552 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
553                                        Type *ResultType,
554                                        const Twine &Name) {
555  Intrinsic::ID ID = Intrinsic::experimental_gc_result;
556  Module *M = BB->getParent()->getParent();
557  Type *Types[] = {ResultType};
558  Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
559
560  Value *Args[] = {Statepoint};
561  return createCallHelper(FnGCResult, Args, this, Name);
562 }
563
564 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
565                                          int BaseOffset,
566                                          int DerivedOffset,
567                                          Type *ResultType,
568                                          const Twine &Name) {
569  Module *M = BB->getParent()->getParent();
570  Type *Types[] = {ResultType};
571  Value *FnGCRelocate =
572    Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
573
574  Value *Args[] = {Statepoint,
575                   getInt32(BaseOffset),
576                   getInt32(DerivedOffset)};
577  return createCallHelper(FnGCRelocate, Args, this, Name);
578 }
579
580 CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID,
581                                                Value *LHS, Value *RHS,
582                                                const Twine &Name) {
583   Module *M = BB->getParent()->getParent();
584   Function *Fn =  Intrinsic::getDeclaration(M, ID, { LHS->getType() });
585   return createCallHelper(Fn, { LHS, RHS }, this, Name);
586 }