OSDN Git Service

Sort the remaining #include lines in include/... and lib/....
[android-x86/external-llvm.git] / 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/IRBuilder.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalVariable.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   if (!PassThru)
385     PassThru = UndefValue::get(DataTy);
386
387   Type *OverloadedTypes[] = {DataTy, PtrsTy};
388   Value * Ops[] = {Ptrs, getInt32(Align), Mask, PassThru};
389
390   // We specify only one type when we create this intrinsic. Types of other
391   // arguments are derived from this type.
392   return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes,
393                                Name);
394 }
395
396 /// \brief Create a call to a Masked Scatter intrinsic.
397 /// \p Data  - data to be stored,
398 /// \p Ptrs  - the vector of pointers, where the \p Data elements should be
399 ///            stored
400 /// \p Align - alignment for one element
401 /// \p Mask  - vector of booleans which indicates what vector lanes should
402 ///            be accessed in memory
403 CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs,
404                                              unsigned Align, Value *Mask) {
405   auto PtrsTy = cast<VectorType>(Ptrs->getType());
406   auto DataTy = cast<VectorType>(Data->getType());
407   unsigned NumElts = PtrsTy->getVectorNumElements();
408
409 #ifndef NDEBUG
410   auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
411   assert(NumElts == DataTy->getVectorNumElements() &&
412          PtrTy->getElementType() == DataTy->getElementType() &&
413          "Incompatible pointer and data types");
414 #endif
415
416   if (!Mask)
417     Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
418                                      NumElts));
419
420   Type *OverloadedTypes[] = {DataTy, PtrsTy};
421   Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask};
422
423   // We specify only one type when we create this intrinsic. Types of other
424   // arguments are derived from this type.
425   return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, OverloadedTypes);
426 }
427
428 template <typename T0, typename T1, typename T2, typename T3>
429 static std::vector<Value *>
430 getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
431                   Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
432                   ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs,
433                   ArrayRef<T3> GCArgs) {
434   std::vector<Value *> Args;
435   Args.push_back(B.getInt64(ID));
436   Args.push_back(B.getInt32(NumPatchBytes));
437   Args.push_back(ActualCallee);
438   Args.push_back(B.getInt32(CallArgs.size()));
439   Args.push_back(B.getInt32(Flags));
440   Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
441   Args.push_back(B.getInt32(TransitionArgs.size()));
442   Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end());
443   Args.push_back(B.getInt32(DeoptArgs.size()));
444   Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
445   Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
446
447   return Args;
448 }
449
450 template <typename T0, typename T1, typename T2, typename T3>
451 static CallInst *CreateGCStatepointCallCommon(
452     IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
453     Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
454     ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
455     const Twine &Name) {
456   // Extract out the type of the callee.
457   PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
458   assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
459          "actual callee must be a callable value");
460
461   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
462   // Fill in the one generic type'd argument (the function is also vararg)
463   Type *ArgTypes[] = { FuncPtrType };
464   Function *FnStatepoint =
465     Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
466                               ArgTypes);
467
468   std::vector<llvm::Value *> Args =
469       getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
470                         CallArgs, TransitionArgs, DeoptArgs, GCArgs);
471   return createCallHelper(FnStatepoint, Args, Builder, Name);
472 }
473
474 CallInst *IRBuilderBase::CreateGCStatepointCall(
475     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
476     ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
477     ArrayRef<Value *> GCArgs, const Twine &Name) {
478   return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>(
479       this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
480       CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name);
481 }
482
483 CallInst *IRBuilderBase::CreateGCStatepointCall(
484     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags,
485     ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs,
486     ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
487   return CreateGCStatepointCallCommon<Use, Use, Use, Value *>(
488       this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs,
489       DeoptArgs, GCArgs, Name);
490 }
491
492 CallInst *IRBuilderBase::CreateGCStatepointCall(
493     uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
494     ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
495     ArrayRef<Value *> GCArgs, const Twine &Name) {
496   return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>(
497       this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
498       CallArgs, None, DeoptArgs, GCArgs, Name);
499 }
500
501 template <typename T0, typename T1, typename T2, typename T3>
502 static InvokeInst *CreateGCStatepointInvokeCommon(
503     IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
504     Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
505     uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
506     ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
507   // Extract out the type of the callee.
508   PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
509   assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
510          "actual callee must be a callable value");
511
512   Module *M = Builder->GetInsertBlock()->getParent()->getParent();
513   // Fill in the one generic type'd argument (the function is also vararg)
514   Function *FnStatepoint = Intrinsic::getDeclaration(
515       M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
516
517   std::vector<llvm::Value *> Args =
518       getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
519                         InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
520   return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
521                             Name);
522 }
523
524 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
525     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
526     BasicBlock *NormalDest, BasicBlock *UnwindDest,
527     ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
528     ArrayRef<Value *> GCArgs, const Twine &Name) {
529   return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>(
530       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
531       uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/,
532       DeoptArgs, GCArgs, Name);
533 }
534
535 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
536     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
537     BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
538     ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
539     ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
540   return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>(
541       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags,
542       InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name);
543 }
544
545 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
546     uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
547     BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
548     ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
549   return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>(
550       this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
551       uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs,
552       Name);
553 }
554
555 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
556                                        Type *ResultType,
557                                        const Twine &Name) {
558  Intrinsic::ID ID = Intrinsic::experimental_gc_result;
559  Module *M = BB->getParent()->getParent();
560  Type *Types[] = {ResultType};
561  Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
562
563  Value *Args[] = {Statepoint};
564  return createCallHelper(FnGCResult, Args, this, Name);
565 }
566
567 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
568                                          int BaseOffset,
569                                          int DerivedOffset,
570                                          Type *ResultType,
571                                          const Twine &Name) {
572  Module *M = BB->getParent()->getParent();
573  Type *Types[] = {ResultType};
574  Value *FnGCRelocate =
575    Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
576
577  Value *Args[] = {Statepoint,
578                   getInt32(BaseOffset),
579                   getInt32(DerivedOffset)};
580  return createCallHelper(FnGCRelocate, Args, this, Name);
581 }
582
583 CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID,
584                                                Value *LHS, Value *RHS,
585                                                const Twine &Name) {
586   Module *M = BB->getParent()->getParent();
587   Function *Fn =  Intrinsic::getDeclaration(M, ID, { LHS->getType() });
588   return createCallHelper(Fn, { LHS, RHS }, this, Name);
589 }