blob: 217577e090e6abe9ea2fd5457def433444f2b4bd [file] [log] [blame]
Chris Lattner17079fc2009-12-28 21:28:46 +00001//===---- 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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#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"
Pat Gavlincc0431d2015-05-08 18:07:42 +000020#include "llvm/IR/Statepoint.h"
Chris Lattner17079fc2009-12-28 21:28:46 +000021using namespace llvm;
22
23/// CreateGlobalString - Make a new global variable with an initializer that
Dan Gohman97c59022010-02-10 20:04:19 +000024/// has array of i8 type filled in with the nul terminated string value
Chris Lattner17079fc2009-12-28 21:28:46 +000025/// specified. If Name is specified, it is the name of the global variable
26/// created.
David Blaikieaa41cd52015-04-03 21:33:42 +000027GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
Tobias Grossercdb89142015-06-19 02:12:07 +000028 const Twine &Name,
29 unsigned AddressSpace) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +000030 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
Chris Lattner17079fc2009-12-28 21:28:46 +000031 Module &M = *BB->getParent()->getParent();
32 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
Benjamin Kramerf1fd6e32011-12-22 14:22:14 +000033 true, GlobalValue::PrivateLinkage,
Tobias Grossercdb89142015-06-19 02:12:07 +000034 StrConstant, Name, nullptr,
35 GlobalVariable::NotThreadLocal,
36 AddressSpace);
Nick Lewycky561f1752011-04-07 00:14:29 +000037 GV->setUnnamedAddr(true);
Chris Lattner17079fc2009-12-28 21:28:46 +000038 return GV;
39}
Chris Lattner7ef1cac2009-12-28 21:45:40 +000040
Chris Lattnerb1907b22011-07-12 04:14:22 +000041Type *IRBuilderBase::getCurrentFunctionReturnType() const {
Chris Lattner49f9f762009-12-28 21:50:56 +000042 assert(BB && BB->getParent() && "No current function!");
43 return BB->getParent()->getReturnType();
44}
Chris Lattner143a07c2010-12-26 22:49:25 +000045
46Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
Chris Lattner229907c2011-07-18 04:54:35 +000047 PointerType *PT = cast<PointerType>(Ptr->getType());
Chris Lattner143a07c2010-12-26 22:49:25 +000048 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
Jay Foad5bd375a2011-07-15 08:37:34 +000059static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
Philip Reames4750dd12014-12-30 05:55:58 +000060 IRBuilderBase *Builder,
61 const Twine& Name="") {
62 CallInst *CI = CallInst::Create(Callee, Ops, Name);
Chris Lattner143a07c2010-12-26 22:49:25 +000063 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
64 Builder->SetInstDebugLocation(CI);
65 return CI;
66}
67
Sanjoy Dasabe1c682015-05-06 23:53:09 +000068static 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
Chris Lattner143a07c2010-12-26 22:49:25 +000081CallInst *IRBuilderBase::
Pete Cooper67cf9a72015-11-19 05:56:52 +000082CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +000083 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
84 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +000085 Ptr = getCastedInt8PtrValue(Ptr);
Pete Cooper67cf9a72015-11-19 05:56:52 +000086 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +000087 Type *Tys[] = { Ptr->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +000088 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +000089 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +000090
Jay Foad5bd375a2011-07-15 08:37:34 +000091 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +000092
93 // Set the TBAA info if present.
94 if (TBAATag)
95 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +000096
97 if (ScopeTag)
98 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
99
100 if (NoAliasTag)
101 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
102
Chris Lattner143a07c2010-12-26 22:49:25 +0000103 return CI;
104}
105
106CallInst *IRBuilderBase::
Pete Cooper67cf9a72015-11-19 05:56:52 +0000107CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000108 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
109 MDNode *ScopeTag, MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000110 Dst = getCastedInt8PtrValue(Dst);
111 Src = getCastedInt8PtrValue(Src);
112
Pete Cooper67cf9a72015-11-19 05:56:52 +0000113 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000114 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000115 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000116 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000117
Jay Foad5bd375a2011-07-15 08:37:34 +0000118 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000119
120 // Set the TBAA info if present.
121 if (TBAATag)
122 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Dan Gohman099727f2012-09-26 22:17:14 +0000123
124 // Set the TBAA Struct info if present.
125 if (TBAAStructTag)
126 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
Hal Finkel94146652014-07-24 14:25:39 +0000127
128 if (ScopeTag)
129 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
130
131 if (NoAliasTag)
132 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
133
Chris Lattner143a07c2010-12-26 22:49:25 +0000134 return CI;
135}
136
137CallInst *IRBuilderBase::
Pete Cooper67cf9a72015-11-19 05:56:52 +0000138CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000139 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
140 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000141 Dst = getCastedInt8PtrValue(Dst);
142 Src = getCastedInt8PtrValue(Src);
143
Pete Cooper67cf9a72015-11-19 05:56:52 +0000144 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000145 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000146 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000147 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000148
Jay Foad5bd375a2011-07-15 08:37:34 +0000149 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000150
151 // Set the TBAA info if present.
152 if (TBAATag)
153 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +0000154
155 if (ScopeTag)
156 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
157
158 if (NoAliasTag)
159 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
160
Chris Lattner143a07c2010-12-26 22:49:25 +0000161 return CI;
162}
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000163
164CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
165 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000166 "lifetime.start only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000167 Ptr = getCastedInt8PtrValue(Ptr);
168 if (!Size)
169 Size = getInt64(-1);
170 else
171 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000172 "lifetime.start requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000173 Value *Ops[] = { Size, Ptr };
174 Module *M = BB->getParent()->getParent();
175 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
Jay Foad5bd375a2011-07-15 08:37:34 +0000176 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000177}
178
179CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
180 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000181 "lifetime.end only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000182 Ptr = getCastedInt8PtrValue(Ptr);
183 if (!Size)
184 Size = getInt64(-1);
185 else
186 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000187 "lifetime.end requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000188 Value *Ops[] = { Size, Ptr };
189 Module *M = BB->getParent()->getParent();
190 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
Jay Foad5bd375a2011-07-15 08:37:34 +0000191 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000192}
Hal Finkel6f814db2014-10-15 23:44:22 +0000193
194CallInst *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
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000204/// \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
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000212CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
213 Value *Mask, Value *PassThru,
214 const Twine &Name) {
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000215 // 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);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000222}
223
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000224/// \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
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000230CallInst *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());
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000235}
236
237/// Create a call to a Masked intrinsic, with given intrinsic Id,
238/// an array of operands - Ops, and one overloaded type - DataTy
Pete Cooper9e1d3352015-05-20 17:16:39 +0000239CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000240 ArrayRef<Value *> Ops,
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000241 Type *DataTy,
242 const Twine &Name) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000243 Module *M = BB->getParent()->getParent();
244 Type *OverloadedTypes[] = { DataTy };
Pete Cooper9e1d3352015-05-20 17:16:39 +0000245 Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000246 return createCallHelper(TheFn, Ops, this, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000247}
Philip Reames4750dd12014-12-30 05:55:58 +0000248
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000249/// \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
257CallInst *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
283CallInst *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
288 auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
289 unsigned NumElts = PtrsTy->getVectorNumElements();
290
291 assert(NumElts == DataTy->getVectorNumElements() &&
292 PtrTy->getElementType() == DataTy->getElementType() &&
293 "Incompatible pointer and data types");
294
295 if (!Mask)
296 Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
297 NumElts));
298 Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask};
299
300 // We specify only one type when we create this intrinsic. Types of other
301 // arguments are derived from this type.
302 return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, DataTy);
303}
304
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000305template <typename T0, typename T1, typename T2, typename T3>
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000306static std::vector<Value *>
307getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000308 Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
309 ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs,
310 ArrayRef<T3> GCArgs) {
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000311 std::vector<Value *> Args;
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000312 Args.push_back(B.getInt64(ID));
313 Args.push_back(B.getInt32(NumPatchBytes));
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000314 Args.push_back(ActualCallee);
315 Args.push_back(B.getInt32(CallArgs.size()));
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000316 Args.push_back(B.getInt32(Flags));
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000317 Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000318 Args.push_back(B.getInt32(TransitionArgs.size()));
319 Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end());
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000320 Args.push_back(B.getInt32(DeoptArgs.size()));
321 Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
322 Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
323
324 return Args;
325}
326
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000327template <typename T0, typename T1, typename T2, typename T3>
328static CallInst *CreateGCStatepointCallCommon(
329 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000330 Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000331 ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
332 const Twine &Name) {
Sanjoy Das63245b52015-05-06 02:36:34 +0000333 // Extract out the type of the callee.
334 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
335 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
336 "actual callee must be a callable value");
Philip Reames4750dd12014-12-30 05:55:58 +0000337
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000338 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
Sanjoy Das63245b52015-05-06 02:36:34 +0000339 // Fill in the one generic type'd argument (the function is also vararg)
340 Type *ArgTypes[] = { FuncPtrType };
341 Function *FnStatepoint =
342 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
343 ArgTypes);
Philip Reames4750dd12014-12-30 05:55:58 +0000344
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000345 std::vector<llvm::Value *> Args =
346 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
347 CallArgs, TransitionArgs, DeoptArgs, GCArgs);
348 return createCallHelper(FnStatepoint, Args, Builder, Name);
349}
350
351CallInst *IRBuilderBase::CreateGCStatepointCall(
352 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
353 ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
354 ArrayRef<Value *> GCArgs, const Twine &Name) {
355 return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>(
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000356 this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
357 CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name);
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000358}
359
360CallInst *IRBuilderBase::CreateGCStatepointCall(
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000361 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags,
362 ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000363 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
364 return CreateGCStatepointCallCommon<Use, Use, Use, Value *>(
365 this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs,
366 DeoptArgs, GCArgs, Name);
Philip Reames4750dd12014-12-30 05:55:58 +0000367}
368
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000369CallInst *IRBuilderBase::CreateGCStatepointCall(
370 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
371 ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
372 ArrayRef<Value *> GCArgs, const Twine &Name) {
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000373 return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>(
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000374 this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
375 CallArgs, None, DeoptArgs, GCArgs, Name);
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000376}
377
378template <typename T0, typename T1, typename T2, typename T3>
379static InvokeInst *CreateGCStatepointInvokeCommon(
380 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
381 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000382 uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000383 ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
384 // Extract out the type of the callee.
385 PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
386 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
387 "actual callee must be a callable value");
388
389 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
390 // Fill in the one generic type'd argument (the function is also vararg)
391 Function *FnStatepoint = Intrinsic::getDeclaration(
392 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
393
394 std::vector<llvm::Value *> Args =
395 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
396 InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
397 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
398 Name);
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000399}
400
401InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000402 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
403 BasicBlock *NormalDest, BasicBlock *UnwindDest,
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000404 ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
405 ArrayRef<Value *> GCArgs, const Twine &Name) {
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000406 return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>(
407 this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000408 uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000409 DeoptArgs, GCArgs, Name);
410}
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000411
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000412InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
413 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000414 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000415 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
416 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
417 return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>(
418 this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags,
419 InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name);
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000420}
421
422InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000423 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
424 BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
425 ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000426 return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>(
427 this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000428 uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs,
429 Name);
Ramkumar Ramachandra3408f3e2015-02-26 00:35:56 +0000430}
431
Philip Reames4750dd12014-12-30 05:55:58 +0000432CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
433 Type *ResultType,
434 const Twine &Name) {
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000435 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
Philip Reames4750dd12014-12-30 05:55:58 +0000436 Module *M = BB->getParent()->getParent();
437 Type *Types[] = {ResultType};
438 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
439
440 Value *Args[] = {Statepoint};
441 return createCallHelper(FnGCResult, Args, this, Name);
442}
443
444CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
445 int BaseOffset,
446 int DerivedOffset,
447 Type *ResultType,
448 const Twine &Name) {
449 Module *M = BB->getParent()->getParent();
450 Type *Types[] = {ResultType};
451 Value *FnGCRelocate =
452 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
453
454 Value *Args[] = {Statepoint,
455 getInt32(BaseOffset),
456 getInt32(DerivedOffset)};
457 return createCallHelper(FnGCRelocate, Args, this, Name);
458}