blob: 89338c8b84982359502c77a3def0a1ac44d0d3fa [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 Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Function.h"
17#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#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);
Peter Collingbourne96efdd62016-06-14 21:01:22 +000037 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
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
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000137CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy(
138 Value *Dst, Value *Src, Value *Size, uint32_t ElementSize, MDNode *TBAATag,
139 MDNode *TBAAStructTag, MDNode *ScopeTag, MDNode *NoAliasTag) {
Anna Thomasb2a212c2017-06-06 16:45:25 +0000140 Dst = getCastedInt8PtrValue(Dst);
141 Src = getCastedInt8PtrValue(Src);
142
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000143 Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)};
144 Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()};
Anna Thomasb2a212c2017-06-06 16:45:25 +0000145 Module *M = BB->getParent()->getParent();
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000146 Value *TheFn = Intrinsic::getDeclaration(
147 M, Intrinsic::memcpy_element_unordered_atomic, Tys);
Anna Thomasb2a212c2017-06-06 16:45:25 +0000148
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 // Set the TBAA Struct info if present.
156 if (TBAAStructTag)
157 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
158
159 if (ScopeTag)
160 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
161
162 if (NoAliasTag)
163 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
164
165 return CI;
166}
167
Chris Lattner143a07c2010-12-26 22:49:25 +0000168CallInst *IRBuilderBase::
Pete Cooper67cf9a72015-11-19 05:56:52 +0000169CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000170 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
171 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000172 Dst = getCastedInt8PtrValue(Dst);
173 Src = getCastedInt8PtrValue(Src);
174
Pete Cooper67cf9a72015-11-19 05:56:52 +0000175 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000176 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000177 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000178 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000179
Jay Foad5bd375a2011-07-15 08:37:34 +0000180 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000181
182 // Set the TBAA info if present.
183 if (TBAATag)
184 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +0000185
186 if (ScopeTag)
187 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
188
189 if (NoAliasTag)
190 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
191
Chris Lattner143a07c2010-12-26 22:49:25 +0000192 return CI;
193}
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000194
Amara Emersoncf9daa32017-05-09 10:43:25 +0000195static CallInst *getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID,
196 Value *Src) {
197 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
198 Value *Ops[] = {Src};
199 Type *Tys[] = { Src->getType()->getVectorElementType(), Src->getType() };
200 auto Decl = Intrinsic::getDeclaration(M, ID, Tys);
201 return createCallHelper(Decl, Ops, Builder);
202}
203
204CallInst *IRBuilderBase::CreateFAddReduce(Value *Acc, Value *Src) {
205 Module *M = GetInsertBlock()->getParent()->getParent();
206 Value *Ops[] = {Acc, Src};
207 Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(),
208 Src->getType()};
209 auto Decl = Intrinsic::getDeclaration(
210 M, Intrinsic::experimental_vector_reduce_fadd, Tys);
211 return createCallHelper(Decl, Ops, this);
212}
213
214CallInst *IRBuilderBase::CreateFMulReduce(Value *Acc, Value *Src) {
215 Module *M = GetInsertBlock()->getParent()->getParent();
216 Value *Ops[] = {Acc, Src};
217 Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(),
218 Src->getType()};
219 auto Decl = Intrinsic::getDeclaration(
220 M, Intrinsic::experimental_vector_reduce_fmul, Tys);
221 return createCallHelper(Decl, Ops, this);
222}
223
224CallInst *IRBuilderBase::CreateAddReduce(Value *Src) {
225 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add,
226 Src);
227}
228
229CallInst *IRBuilderBase::CreateMulReduce(Value *Src) {
230 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_mul,
231 Src);
232}
233
234CallInst *IRBuilderBase::CreateAndReduce(Value *Src) {
235 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_and,
236 Src);
237}
238
239CallInst *IRBuilderBase::CreateOrReduce(Value *Src) {
240 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_or,
241 Src);
242}
243
244CallInst *IRBuilderBase::CreateXorReduce(Value *Src) {
245 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_xor,
246 Src);
247}
248
249CallInst *IRBuilderBase::CreateIntMaxReduce(Value *Src, bool IsSigned) {
250 auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smax
251 : Intrinsic::experimental_vector_reduce_umax;
252 return getReductionIntrinsic(this, ID, Src);
253}
254
255CallInst *IRBuilderBase::CreateIntMinReduce(Value *Src, bool IsSigned) {
256 auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smin
257 : Intrinsic::experimental_vector_reduce_umin;
258 return getReductionIntrinsic(this, ID, Src);
259}
260
261CallInst *IRBuilderBase::CreateFPMaxReduce(Value *Src, bool NoNaN) {
262 auto Rdx = getReductionIntrinsic(
263 this, Intrinsic::experimental_vector_reduce_fmax, Src);
264 if (NoNaN) {
265 FastMathFlags FMF;
266 FMF.setNoNaNs();
267 Rdx->setFastMathFlags(FMF);
268 }
269 return Rdx;
270}
271
272CallInst *IRBuilderBase::CreateFPMinReduce(Value *Src, bool NoNaN) {
273 auto Rdx = getReductionIntrinsic(
274 this, Intrinsic::experimental_vector_reduce_fmin, Src);
275 if (NoNaN) {
276 FastMathFlags FMF;
277 FMF.setNoNaNs();
278 Rdx->setFastMathFlags(FMF);
279 }
280 return Rdx;
281}
282
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000283CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
284 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000285 "lifetime.start only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000286 Ptr = getCastedInt8PtrValue(Ptr);
287 if (!Size)
288 Size = getInt64(-1);
289 else
290 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000291 "lifetime.start requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000292 Value *Ops[] = { Size, Ptr };
293 Module *M = BB->getParent()->getParent();
Matt Arsenaultf10061e2017-04-10 20:18:21 +0000294 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start,
295 { Ptr->getType() });
Jay Foad5bd375a2011-07-15 08:37:34 +0000296 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000297}
298
299CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
300 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000301 "lifetime.end only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000302 Ptr = getCastedInt8PtrValue(Ptr);
303 if (!Size)
304 Size = getInt64(-1);
305 else
306 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000307 "lifetime.end requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000308 Value *Ops[] = { Size, Ptr };
309 Module *M = BB->getParent()->getParent();
Matt Arsenaultf10061e2017-04-10 20:18:21 +0000310 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end,
311 { Ptr->getType() });
Jay Foad5bd375a2011-07-15 08:37:34 +0000312 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000313}
Hal Finkel6f814db2014-10-15 23:44:22 +0000314
Anna Thomas58d11922016-07-22 20:57:23 +0000315CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) {
316
317 assert(isa<PointerType>(Ptr->getType()) &&
318 "invariant.start only applies to pointers.");
319 Ptr = getCastedInt8PtrValue(Ptr);
320 if (!Size)
321 Size = getInt64(-1);
322 else
323 assert(Size->getType() == getInt64Ty() &&
324 "invariant.start requires the size to be an i64");
325
326 Value *Ops[] = {Size, Ptr};
327 // Fill in the single overloaded type: memory object type.
328 Type *ObjectPtr[1] = {Ptr->getType()};
329 Module *M = BB->getParent()->getParent();
330 Value *TheFn =
331 Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr);
332 return createCallHelper(TheFn, Ops, this);
333}
334
Hal Finkel6f814db2014-10-15 23:44:22 +0000335CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
336 assert(Cond->getType() == getInt1Ty() &&
337 "an assumption condition must be of type i1");
338
339 Value *Ops[] = { Cond };
340 Module *M = BB->getParent()->getParent();
341 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
342 return createCallHelper(FnAssume, Ops, this);
343}
344
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000345/// \brief Create a call to a Masked Load intrinsic.
346/// \p Ptr - base pointer for the load
347/// \p Align - alignment of the source location
348/// \p Mask - vector of booleans which indicates what vector lanes should
349/// be accessed in memory
350/// \p PassThru - pass-through value that is used to fill the masked-off lanes
351/// of the result
352/// \p Name - name of the result variable
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000353CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
354 Value *Mask, Value *PassThru,
355 const Twine &Name) {
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000356 PointerType *PtrTy = cast<PointerType>(Ptr->getType());
357 Type *DataTy = PtrTy->getElementType();
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000358 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
Ayal Zakse841b212017-07-31 13:21:42 +0000359 assert(Mask && "Mask should not be all-ones (null)");
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000360 if (!PassThru)
361 PassThru = UndefValue::get(DataTy);
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000362 Type *OverloadedTypes[] = { DataTy, PtrTy };
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000363 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000364 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops,
365 OverloadedTypes, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000366}
367
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000368/// \brief Create a call to a Masked Store intrinsic.
369/// \p Val - data to be stored,
370/// \p Ptr - base pointer for the store
371/// \p Align - alignment of the destination location
372/// \p Mask - vector of booleans which indicates what vector lanes should
373/// be accessed in memory
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000374CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
375 unsigned Align, Value *Mask) {
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000376 PointerType *PtrTy = cast<PointerType>(Ptr->getType());
377 Type *DataTy = PtrTy->getElementType();
378 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
Ayal Zakse841b212017-07-31 13:21:42 +0000379 assert(Mask && "Mask should not be all-ones (null)");
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000380 Type *OverloadedTypes[] = { DataTy, PtrTy };
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000381 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000382 return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, OverloadedTypes);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000383}
384
385/// Create a call to a Masked intrinsic, with given intrinsic Id,
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000386/// an array of operands - Ops, and an array of overloaded types -
387/// OverloadedTypes.
Pete Cooper9e1d3352015-05-20 17:16:39 +0000388CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000389 ArrayRef<Value *> Ops,
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000390 ArrayRef<Type *> OverloadedTypes,
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000391 const Twine &Name) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000392 Module *M = BB->getParent()->getParent();
Pete Cooper9e1d3352015-05-20 17:16:39 +0000393 Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000394 return createCallHelper(TheFn, Ops, this, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000395}
Philip Reames4750dd12014-12-30 05:55:58 +0000396
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000397/// \brief Create a call to a Masked Gather intrinsic.
398/// \p Ptrs - vector of pointers for loading
399/// \p Align - alignment for one element
400/// \p Mask - vector of booleans which indicates what vector lanes should
401/// be accessed in memory
402/// \p PassThru - pass-through value that is used to fill the masked-off lanes
403/// of the result
404/// \p Name - name of the result variable
405CallInst *IRBuilderBase::CreateMaskedGather(Value *Ptrs, unsigned Align,
406 Value *Mask, Value *PassThru,
407 const Twine& Name) {
408 auto PtrsTy = cast<VectorType>(Ptrs->getType());
409 auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
410 unsigned NumElts = PtrsTy->getVectorNumElements();
411 Type *DataTy = VectorType::get(PtrTy->getElementType(), NumElts);
412
413 if (!Mask)
414 Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
415 NumElts));
416
Amara Emerson4d33c862017-05-19 10:40:18 +0000417 if (!PassThru)
418 PassThru = UndefValue::get(DataTy);
419
Elad Cohenef5798a2017-05-03 12:28:54 +0000420 Type *OverloadedTypes[] = {DataTy, PtrsTy};
Amara Emerson4d33c862017-05-19 10:40:18 +0000421 Value * Ops[] = {Ptrs, getInt32(Align), Mask, PassThru};
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000422
423 // We specify only one type when we create this intrinsic. Types of other
424 // arguments are derived from this type.
Elad Cohenef5798a2017-05-03 12:28:54 +0000425 return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes,
426 Name);
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000427}
428
429/// \brief Create a call to a Masked Scatter intrinsic.
430/// \p Data - data to be stored,
431/// \p Ptrs - the vector of pointers, where the \p Data elements should be
432/// stored
433/// \p Align - alignment for one element
434/// \p Mask - vector of booleans which indicates what vector lanes should
435/// be accessed in memory
436CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs,
437 unsigned Align, Value *Mask) {
438 auto PtrsTy = cast<VectorType>(Ptrs->getType());
439 auto DataTy = cast<VectorType>(Data->getType());
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000440 unsigned NumElts = PtrsTy->getVectorNumElements();
441
Tim Northover5a1a56c2016-02-17 21:16:59 +0000442#ifndef NDEBUG
443 auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000444 assert(NumElts == DataTy->getVectorNumElements() &&
Tim Northover5a1a56c2016-02-17 21:16:59 +0000445 PtrTy->getElementType() == DataTy->getElementType() &&
446 "Incompatible pointer and data types");
447#endif
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000448
449 if (!Mask)
450 Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context),
451 NumElts));
Elad Cohenef5798a2017-05-03 12:28:54 +0000452
453 Type *OverloadedTypes[] = {DataTy, PtrsTy};
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000454 Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask};
455
456 // We specify only one type when we create this intrinsic. Types of other
457 // arguments are derived from this type.
Elad Cohenef5798a2017-05-03 12:28:54 +0000458 return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, OverloadedTypes);
Elena Demikhovsky88e76ca2016-02-17 19:23:04 +0000459}
460
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000461template <typename T0, typename T1, typename T2, typename T3>
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000462static std::vector<Value *>
463getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000464 Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
465 ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs,
466 ArrayRef<T3> GCArgs) {
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000467 std::vector<Value *> Args;
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000468 Args.push_back(B.getInt64(ID));
469 Args.push_back(B.getInt32(NumPatchBytes));
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000470 Args.push_back(ActualCallee);
471 Args.push_back(B.getInt32(CallArgs.size()));
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000472 Args.push_back(B.getInt32(Flags));
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000473 Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000474 Args.push_back(B.getInt32(TransitionArgs.size()));
475 Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end());
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000476 Args.push_back(B.getInt32(DeoptArgs.size()));
477 Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
478 Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
479
480 return Args;
481}
482
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000483template <typename T0, typename T1, typename T2, typename T3>
484static CallInst *CreateGCStatepointCallCommon(
485 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000486 Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000487 ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
488 const Twine &Name) {
Sanjoy Das63245b52015-05-06 02:36:34 +0000489 // Extract out the type of the callee.
490 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
491 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
492 "actual callee must be a callable value");
Philip Reames4750dd12014-12-30 05:55:58 +0000493
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000494 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
Sanjoy Das63245b52015-05-06 02:36:34 +0000495 // Fill in the one generic type'd argument (the function is also vararg)
496 Type *ArgTypes[] = { FuncPtrType };
497 Function *FnStatepoint =
498 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
499 ArgTypes);
Philip Reames4750dd12014-12-30 05:55:58 +0000500
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000501 std::vector<llvm::Value *> Args =
502 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
503 CallArgs, TransitionArgs, DeoptArgs, GCArgs);
504 return createCallHelper(FnStatepoint, Args, Builder, Name);
505}
506
507CallInst *IRBuilderBase::CreateGCStatepointCall(
508 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
509 ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
510 ArrayRef<Value *> GCArgs, const Twine &Name) {
511 return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>(
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000512 this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
513 CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name);
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000514}
515
516CallInst *IRBuilderBase::CreateGCStatepointCall(
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000517 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags,
518 ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000519 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
520 return CreateGCStatepointCallCommon<Use, Use, Use, Value *>(
521 this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs,
522 DeoptArgs, GCArgs, Name);
Philip Reames4750dd12014-12-30 05:55:58 +0000523}
524
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000525CallInst *IRBuilderBase::CreateGCStatepointCall(
526 uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
527 ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
528 ArrayRef<Value *> GCArgs, const Twine &Name) {
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000529 return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>(
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000530 this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
531 CallArgs, None, DeoptArgs, GCArgs, Name);
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000532}
533
534template <typename T0, typename T1, typename T2, typename T3>
535static InvokeInst *CreateGCStatepointInvokeCommon(
536 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
537 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000538 uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000539 ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
540 // Extract out the type of the callee.
541 PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
542 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
543 "actual callee must be a callable value");
544
545 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
546 // Fill in the one generic type'd argument (the function is also vararg)
547 Function *FnStatepoint = Intrinsic::getDeclaration(
548 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
549
550 std::vector<llvm::Value *> Args =
551 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
552 InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
553 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
554 Name);
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000555}
556
557InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000558 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
559 BasicBlock *NormalDest, BasicBlock *UnwindDest,
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000560 ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
561 ArrayRef<Value *> GCArgs, const Twine &Name) {
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000562 return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>(
563 this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000564 uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000565 DeoptArgs, GCArgs, Name);
566}
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000567
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000568InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
569 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000570 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000571 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
572 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
573 return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>(
574 this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags,
575 InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name);
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000576}
577
578InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000579 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
580 BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
581 ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
Sanjoy Dasaf6980c2015-10-07 19:52:12 +0000582 return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>(
583 this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
Sanjoy Das4fd3d402015-10-08 23:18:33 +0000584 uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs,
585 Name);
Ramkumar Ramachandra3408f3e2015-02-26 00:35:56 +0000586}
587
Philip Reames4750dd12014-12-30 05:55:58 +0000588CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
589 Type *ResultType,
590 const Twine &Name) {
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000591 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
Philip Reames4750dd12014-12-30 05:55:58 +0000592 Module *M = BB->getParent()->getParent();
593 Type *Types[] = {ResultType};
594 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
595
596 Value *Args[] = {Statepoint};
597 return createCallHelper(FnGCResult, Args, this, Name);
598}
599
600CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
601 int BaseOffset,
602 int DerivedOffset,
603 Type *ResultType,
604 const Twine &Name) {
605 Module *M = BB->getParent()->getParent();
606 Type *Types[] = {ResultType};
607 Value *FnGCRelocate =
608 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
609
610 Value *Args[] = {Statepoint,
611 getInt32(BaseOffset),
612 getInt32(DerivedOffset)};
613 return createCallHelper(FnGCRelocate, Args, this, Name);
614}
Matt Arsenaultcdb468c2017-02-27 23:08:49 +0000615
616CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID,
617 Value *LHS, Value *RHS,
618 const Twine &Name) {
619 Module *M = BB->getParent()->getParent();
620 Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() });
621 return createCallHelper(Fn, { LHS, RHS }, this, Name);
622}