LLVM  7.0.0svn
IRBuilder.cpp
Go to the documentation of this file.
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/ADT/ArrayRef.h"
17 #include "llvm/ADT/None.h"
18 #include "llvm/IR/Constant.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/Statepoint.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/IR/Value.h"
31 #include "llvm/Support/Casting.h"
33 #include <cassert>
34 #include <cstdint>
35 #include <vector>
36 
37 using namespace llvm;
38 
39 /// CreateGlobalString - Make a new global variable with an initializer that
40 /// has array of i8 type filled in with the nul terminated string value
41 /// specified. If Name is specified, it is the name of the global variable
42 /// created.
44  const Twine &Name,
45  unsigned AddressSpace) {
46  Constant *StrConstant = ConstantDataArray::getString(Context, Str);
47  Module &M = *BB->getParent()->getParent();
48  auto *GV = new GlobalVariable(M, StrConstant->getType(), true,
49  GlobalValue::PrivateLinkage, StrConstant, Name,
51  AddressSpace);
53  return GV;
54 }
55 
57  assert(BB && BB->getParent() && "No current function!");
58  return BB->getParent()->getReturnType();
59 }
60 
61 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
62  auto *PT = cast<PointerType>(Ptr->getType());
63  if (PT->getElementType()->isIntegerTy(8))
64  return Ptr;
65 
66  // Otherwise, we need to insert a bitcast.
67  PT = getInt8PtrTy(PT->getAddressSpace());
68  BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
69  BB->getInstList().insert(InsertPt, BCI);
71  return BCI;
72 }
73 
75  IRBuilderBase *Builder,
76  const Twine &Name = "",
77  Instruction *FMFSource = nullptr) {
78  CallInst *CI = CallInst::Create(Callee, Ops, Name);
79  if (FMFSource)
80  CI->copyFastMathFlags(FMFSource);
81  Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
82  Builder->SetInstDebugLocation(CI);
83  return CI;
84 }
85 
86 static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest,
87  BasicBlock *UnwindDest,
89  IRBuilderBase *Builder,
90  const Twine &Name = "") {
91  InvokeInst *II =
92  InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name);
93  Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),
94  II);
95  Builder->SetInstDebugLocation(II);
96  return II;
97 }
98 
100 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
101  bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
102  MDNode *NoAliasTag) {
103  Ptr = getCastedInt8PtrValue(Ptr);
104  Value *Ops[] = {Ptr, Val, Size, getInt1(isVolatile)};
105  Type *Tys[] = { Ptr->getType(), Size->getType() };
106  Module *M = BB->getParent()->getParent();
107  Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
108 
109  CallInst *CI = createCallHelper(TheFn, Ops, this);
110 
111  if (Align > 0)
112  cast<MemSetInst>(CI)->setDestAlignment(Align);
113 
114  // Set the TBAA info if present.
115  if (TBAATag)
116  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
117 
118  if (ScopeTag)
120 
121  if (NoAliasTag)
122  CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
123 
124  return CI;
125 }
126 
128 CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
129  Value *Size, bool isVolatile, MDNode *TBAATag,
130  MDNode *TBAAStructTag, MDNode *ScopeTag, MDNode *NoAliasTag) {
131  assert((DstAlign == 0 || isPowerOf2_32(DstAlign)) && "Must be 0 or a power of 2");
132  assert((SrcAlign == 0 || isPowerOf2_32(SrcAlign)) && "Must be 0 or a power of 2");
133  Dst = getCastedInt8PtrValue(Dst);
134  Src = getCastedInt8PtrValue(Src);
135 
136  Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)};
137  Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
138  Module *M = BB->getParent()->getParent();
139  Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
140 
141  CallInst *CI = createCallHelper(TheFn, Ops, this);
142 
143  auto* MCI = cast<MemCpyInst>(CI);
144  if (DstAlign > 0)
145  MCI->setDestAlignment(DstAlign);
146  if (SrcAlign > 0)
147  MCI->setSourceAlignment(SrcAlign);
148 
149  // Set the TBAA info if present.
150  if (TBAATag)
151  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
152 
153  // Set the TBAA Struct info if present.
154  if (TBAAStructTag)
155  CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
156 
157  if (ScopeTag)
159 
160  if (NoAliasTag)
161  CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
162 
163  return CI;
164 }
165 
167  Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
168  uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag,
169  MDNode *ScopeTag, MDNode *NoAliasTag) {
170  assert(DstAlign >= ElementSize &&
171  "Pointer alignment must be at least element size");
172  assert(SrcAlign >= ElementSize &&
173  "Pointer alignment must be at least element size");
174  Dst = getCastedInt8PtrValue(Dst);
175  Src = getCastedInt8PtrValue(Src);
176 
177  Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)};
178  Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()};
179  Module *M = BB->getParent()->getParent();
181  M, Intrinsic::memcpy_element_unordered_atomic, Tys);
182 
183  CallInst *CI = createCallHelper(TheFn, Ops, this);
184 
185  // Set the alignment of the pointer args.
186  auto *AMCI = cast<AtomicMemCpyInst>(CI);
187  AMCI->setDestAlignment(DstAlign);
188  AMCI->setSourceAlignment(SrcAlign);
189 
190  // Set the TBAA info if present.
191  if (TBAATag)
192  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
193 
194  // Set the TBAA Struct info if present.
195  if (TBAAStructTag)
196  CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
197 
198  if (ScopeTag)
200 
201  if (NoAliasTag)
202  CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
203 
204  return CI;
205 }
206 
208 CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
209  Value *Size, bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
210  MDNode *NoAliasTag) {
211  assert((DstAlign == 0 || isPowerOf2_32(DstAlign)) && "Must be 0 or a power of 2");
212  assert((SrcAlign == 0 || isPowerOf2_32(SrcAlign)) && "Must be 0 or a power of 2");
213  Dst = getCastedInt8PtrValue(Dst);
214  Src = getCastedInt8PtrValue(Src);
215 
216  Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)};
217  Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
218  Module *M = BB->getParent()->getParent();
219  Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
220 
221  CallInst *CI = createCallHelper(TheFn, Ops, this);
222 
223  auto *MMI = cast<MemMoveInst>(CI);
224  if (DstAlign > 0)
225  MMI->setDestAlignment(DstAlign);
226  if (SrcAlign > 0)
227  MMI->setSourceAlignment(SrcAlign);
228 
229  // Set the TBAA info if present.
230  if (TBAATag)
231  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
232 
233  if (ScopeTag)
235 
236  if (NoAliasTag)
237  CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
238 
239  return CI;
240 }
241 
243  Value *Src) {
244  Module *M = Builder->GetInsertBlock()->getParent()->getParent();
245  Value *Ops[] = {Src};
246  Type *Tys[] = { Src->getType()->getVectorElementType(), Src->getType() };
247  auto Decl = Intrinsic::getDeclaration(M, ID, Tys);
248  return createCallHelper(Decl, Ops, Builder);
249 }
250 
253  Value *Ops[] = {Acc, Src};
254  Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(),
255  Src->getType()};
256  auto Decl = Intrinsic::getDeclaration(
257  M, Intrinsic::experimental_vector_reduce_fadd, Tys);
258  return createCallHelper(Decl, Ops, this);
259 }
260 
263  Value *Ops[] = {Acc, Src};
264  Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(),
265  Src->getType()};
266  auto Decl = Intrinsic::getDeclaration(
267  M, Intrinsic::experimental_vector_reduce_fmul, Tys);
268  return createCallHelper(Decl, Ops, this);
269 }
270 
272  return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add,
273  Src);
274 }
275 
277  return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_mul,
278  Src);
279 }
280 
282  return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_and,
283  Src);
284 }
285 
287  return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_or,
288  Src);
289 }
290 
292  return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_xor,
293  Src);
294 }
295 
297  auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smax
298  : Intrinsic::experimental_vector_reduce_umax;
299  return getReductionIntrinsic(this, ID, Src);
300 }
301 
303  auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smin
304  : Intrinsic::experimental_vector_reduce_umin;
305  return getReductionIntrinsic(this, ID, Src);
306 }
307 
309  auto Rdx = getReductionIntrinsic(
310  this, Intrinsic::experimental_vector_reduce_fmax, Src);
311  if (NoNaN) {
313  FMF.setNoNaNs();
314  Rdx->setFastMathFlags(FMF);
315  }
316  return Rdx;
317 }
318 
320  auto Rdx = getReductionIntrinsic(
321  this, Intrinsic::experimental_vector_reduce_fmin, Src);
322  if (NoNaN) {
324  FMF.setNoNaNs();
325  Rdx->setFastMathFlags(FMF);
326  }
327  return Rdx;
328 }
329 
331  assert(isa<PointerType>(Ptr->getType()) &&
332  "lifetime.start only applies to pointers.");
333  Ptr = getCastedInt8PtrValue(Ptr);
334  if (!Size)
335  Size = getInt64(-1);
336  else
337  assert(Size->getType() == getInt64Ty() &&
338  "lifetime.start requires the size to be an i64");
339  Value *Ops[] = { Size, Ptr };
340  Module *M = BB->getParent()->getParent();
341  Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start,
342  { Ptr->getType() });
343  return createCallHelper(TheFn, Ops, this);
344 }
345 
347  assert(isa<PointerType>(Ptr->getType()) &&
348  "lifetime.end only applies to pointers.");
349  Ptr = getCastedInt8PtrValue(Ptr);
350  if (!Size)
351  Size = getInt64(-1);
352  else
353  assert(Size->getType() == getInt64Ty() &&
354  "lifetime.end requires the size to be an i64");
355  Value *Ops[] = { Size, Ptr };
356  Module *M = BB->getParent()->getParent();
357  Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end,
358  { Ptr->getType() });
359  return createCallHelper(TheFn, Ops, this);
360 }
361 
363 
364  assert(isa<PointerType>(Ptr->getType()) &&
365  "invariant.start only applies to pointers.");
366  Ptr = getCastedInt8PtrValue(Ptr);
367  if (!Size)
368  Size = getInt64(-1);
369  else
370  assert(Size->getType() == getInt64Ty() &&
371  "invariant.start requires the size to be an i64");
372 
373  Value *Ops[] = {Size, Ptr};
374  // Fill in the single overloaded type: memory object type.
375  Type *ObjectPtr[1] = {Ptr->getType()};
376  Module *M = BB->getParent()->getParent();
377  Value *TheFn =
378  Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr);
379  return createCallHelper(TheFn, Ops, this);
380 }
381 
383  assert(Cond->getType() == getInt1Ty() &&
384  "an assumption condition must be of type i1");
385 
386  Value *Ops[] = { Cond };
387  Module *M = BB->getParent()->getParent();
388  Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
389  return createCallHelper(FnAssume, Ops, this);
390 }
391 
392 /// \brief Create a call to a Masked Load intrinsic.
393 /// \p Ptr - base pointer for the load
394 /// \p Align - alignment of the source location
395 /// \p Mask - vector of booleans which indicates what vector lanes should
396 /// be accessed in memory
397 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
398 /// of the result
399 /// \p Name - name of the result variable
401  Value *Mask, Value *PassThru,
402  const Twine &Name) {
403  auto *PtrTy = cast<PointerType>(Ptr->getType());
404  Type *DataTy = PtrTy->getElementType();
405  assert(DataTy->isVectorTy() && "Ptr should point to a vector");
406  assert(Mask && "Mask should not be all-ones (null)");
407  if (!PassThru)
408  PassThru = UndefValue::get(DataTy);
409  Type *OverloadedTypes[] = { DataTy, PtrTy };
410  Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
411  return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops,
412  OverloadedTypes, Name);
413 }
414 
415 /// \brief Create a call to a Masked Store intrinsic.
416 /// \p Val - data to be stored,
417 /// \p Ptr - base pointer for the store
418 /// \p Align - alignment of the destination location
419 /// \p Mask - vector of booleans which indicates what vector lanes should
420 /// be accessed in memory
422  unsigned Align, Value *Mask) {
423  auto *PtrTy = cast<PointerType>(Ptr->getType());
424  Type *DataTy = PtrTy->getElementType();
425  assert(DataTy->isVectorTy() && "Ptr should point to a vector");
426  assert(Mask && "Mask should not be all-ones (null)");
427  Type *OverloadedTypes[] = { DataTy, PtrTy };
428  Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
429  return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, OverloadedTypes);
430 }
431 
432 /// Create a call to a Masked intrinsic, with given intrinsic Id,
433 /// an array of operands - Ops, and an array of overloaded types -
434 /// OverloadedTypes.
435 CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
436  ArrayRef<Value *> Ops,
437  ArrayRef<Type *> OverloadedTypes,
438  const Twine &Name) {
439  Module *M = BB->getParent()->getParent();
440  Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
441  return createCallHelper(TheFn, Ops, this, Name);
442 }
443 
444 /// \brief Create a call to a Masked Gather intrinsic.
445 /// \p Ptrs - vector of pointers for loading
446 /// \p Align - alignment for one element
447 /// \p Mask - vector of booleans which indicates what vector lanes should
448 /// be accessed in memory
449 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
450 /// of the result
451 /// \p Name - name of the result variable
453  Value *Mask, Value *PassThru,
454  const Twine& Name) {
455  auto PtrsTy = cast<VectorType>(Ptrs->getType());
456  auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
457  unsigned NumElts = PtrsTy->getVectorNumElements();
458  Type *DataTy = VectorType::get(PtrTy->getElementType(), NumElts);
459 
460  if (!Mask)
462  NumElts));
463 
464  if (!PassThru)
465  PassThru = UndefValue::get(DataTy);
466 
467  Type *OverloadedTypes[] = {DataTy, PtrsTy};
468  Value * Ops[] = {Ptrs, getInt32(Align), Mask, PassThru};
469 
470  // We specify only one type when we create this intrinsic. Types of other
471  // arguments are derived from this type.
472  return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes,
473  Name);
474 }
475 
476 /// \brief Create a call to a Masked Scatter intrinsic.
477 /// \p Data - data to be stored,
478 /// \p Ptrs - the vector of pointers, where the \p Data elements should be
479 /// stored
480 /// \p Align - alignment for one element
481 /// \p Mask - vector of booleans which indicates what vector lanes should
482 /// be accessed in memory
484  unsigned Align, Value *Mask) {
485  auto PtrsTy = cast<VectorType>(Ptrs->getType());
486  auto DataTy = cast<VectorType>(Data->getType());
487  unsigned NumElts = PtrsTy->getVectorNumElements();
488 
489 #ifndef NDEBUG
490  auto PtrTy = cast<PointerType>(PtrsTy->getElementType());
491  assert(NumElts == DataTy->getVectorNumElements() &&
492  PtrTy->getElementType() == DataTy->getElementType() &&
493  "Incompatible pointer and data types");
494 #endif
495 
496  if (!Mask)
498  NumElts));
499 
500  Type *OverloadedTypes[] = {DataTy, PtrsTy};
501  Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask};
502 
503  // We specify only one type when we create this intrinsic. Types of other
504  // arguments are derived from this type.
505  return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, OverloadedTypes);
506 }
507 
508 template <typename T0, typename T1, typename T2, typename T3>
509 static std::vector<Value *>
510 getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
511  Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
512  ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs,
513  ArrayRef<T3> GCArgs) {
514  std::vector<Value *> Args;
515  Args.push_back(B.getInt64(ID));
516  Args.push_back(B.getInt32(NumPatchBytes));
517  Args.push_back(ActualCallee);
518  Args.push_back(B.getInt32(CallArgs.size()));
519  Args.push_back(B.getInt32(Flags));
520  Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
521  Args.push_back(B.getInt32(TransitionArgs.size()));
522  Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end());
523  Args.push_back(B.getInt32(DeoptArgs.size()));
524  Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
525  Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
526 
527  return Args;
528 }
529 
530 template <typename T0, typename T1, typename T2, typename T3>
532  IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
533  Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
534  ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
535  const Twine &Name) {
536  // Extract out the type of the callee.
537  auto *FuncPtrType = cast<PointerType>(ActualCallee->getType());
538  assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
539  "actual callee must be a callable value");
540 
541  Module *M = Builder->GetInsertBlock()->getParent()->getParent();
542  // Fill in the one generic type'd argument (the function is also vararg)
543  Type *ArgTypes[] = { FuncPtrType };
544  Function *FnStatepoint =
545  Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
546  ArgTypes);
547 
548  std::vector<Value *> Args =
549  getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
550  CallArgs, TransitionArgs, DeoptArgs, GCArgs);
551  return createCallHelper(FnStatepoint, Args, Builder, Name);
552 }
553 
555  uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
556  ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
557  ArrayRef<Value *> GCArgs, const Twine &Name) {
558  return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>(
559  this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
560  CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name);
561 }
562 
564  uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags,
565  ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs,
566  ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
567  return CreateGCStatepointCallCommon<Use, Use, Use, Value *>(
568  this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs,
569  DeoptArgs, GCArgs, Name);
570 }
571 
573  uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
574  ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
575  ArrayRef<Value *> GCArgs, const Twine &Name) {
576  return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>(
577  this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None),
578  CallArgs, None, DeoptArgs, GCArgs, Name);
579 }
580 
581 template <typename T0, typename T1, typename T2, typename T3>
583  IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
584  Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
585  uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
586  ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
587  // Extract out the type of the callee.
588  auto *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
589  assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
590  "actual callee must be a callable value");
591 
592  Module *M = Builder->GetInsertBlock()->getParent()->getParent();
593  // Fill in the one generic type'd argument (the function is also vararg)
594  Function *FnStatepoint = Intrinsic::getDeclaration(
595  M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
596 
597  std::vector<Value *> Args =
598  getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
599  InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
600  return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
601  Name);
602 }
603 
605  uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
606  BasicBlock *NormalDest, BasicBlock *UnwindDest,
607  ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
608  ArrayRef<Value *> GCArgs, const Twine &Name) {
609  return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>(
610  this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
611  uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/,
612  DeoptArgs, GCArgs, Name);
613 }
614 
616  uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
617  BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
618  ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
619  ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
620  return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>(
621  this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags,
622  InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name);
623 }
624 
626  uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
627  BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
628  ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
629  return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>(
630  this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest,
631  uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs,
632  Name);
633 }
634 
636  Type *ResultType,
637  const Twine &Name) {
638  Intrinsic::ID ID = Intrinsic::experimental_gc_result;
639  Module *M = BB->getParent()->getParent();
640  Type *Types[] = {ResultType};
641  Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
642 
643  Value *Args[] = {Statepoint};
644  return createCallHelper(FnGCResult, Args, this, Name);
645 }
646 
648  int BaseOffset,
649  int DerivedOffset,
650  Type *ResultType,
651  const Twine &Name) {
652  Module *M = BB->getParent()->getParent();
653  Type *Types[] = {ResultType};
654  Value *FnGCRelocate =
655  Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
656 
657  Value *Args[] = {Statepoint,
658  getInt32(BaseOffset),
659  getInt32(DerivedOffset)};
660  return createCallHelper(FnGCRelocate, Args, this, Name);
661 }
662 
664  Value *LHS, Value *RHS,
665  const Twine &Name) {
666  Module *M = BB->getModule();
667  Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() });
668  return createCallHelper(Fn, { LHS, RHS }, this, Name);
669 }
670 
673  Instruction *FMFSource,
674  const Twine &Name) {
675  assert(!Args.empty() && "Expected at least one argument to intrinsic");
676  Module *M = BB->getModule();
677  Function *Fn = Intrinsic::getDeclaration(M, ID, { Args.front()->getType() });
678  return createCallHelper(Fn, Args, this, Name, FMFSource);
679 }
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:152
Type * getVectorElementType() const
Definition: Type.h:368
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:89
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:172
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2479
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:173
CallInst * CreateInvariantStart(Value *Ptr, ConstantInt *Size=nullptr)
Create a call to invariant.start intrinsic.
Definition: IRBuilder.cpp:362
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
iterator begin() const
Definition: ArrayRef.h:137
FastMathFlags FMF
Definition: IRBuilder.h:98
CallInst * CreateGCRelocate(Instruction *Statepoint, int BaseOffset, int DerivedOffset, Type *ResultType, const Twine &Name="")
Create a call to the experimental.gc.relocate intrinsics to project the relocated value of one pointe...
Definition: IRBuilder.cpp:647
This class represents a function call, abstracting a target machine&#39;s calling convention.
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:57
Metadata node.
Definition: Metadata.h:862
static CallInst * Create(Value *Func, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:404
LLVMContext & Context
Definition: IRBuilder.h:95
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:116
static CallInst * CreateGCStatepointCallCommon(IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags, ArrayRef< T0 > CallArgs, ArrayRef< T1 > TransitionArgs, ArrayRef< T2 > DeoptArgs, ArrayRef< T3 > GCArgs, const Twine &Name)
Definition: IRBuilder.cpp:531
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:352
CallInst * CreateFAddReduce(Value *Acc, Value *Src)
Create a vector fadd reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:251
CallInst * CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memmove between the specified pointers.
Definition: IRBuilder.h:470
CallInst * CreateMaskedGather(Value *Ptrs, unsigned Align, Value *Mask=nullptr, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Gather intrinsic.
Definition: IRBuilder.cpp:452
BasicBlock * BB
Definition: IRBuilder.h:93
CallInst * CreateFPMinReduce(Value *Src, bool NoNaN=false)
Create a vector float min reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:319
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:121
void SetInstDebugLocation(Instruction *I) const
If this builder has a current debug location, set it on the specified instruction.
Definition: IRBuilder.h:158
This class represents a no-op cast from one type to another.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
amdgpu Simplify well known AMD library false Value * Callee
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:993
CallInst * CreateXorReduce(Value *Src)
Create a vector int XOR reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:291
CallInst * CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, ArrayRef< Value *> CallArgs, ArrayRef< Value *> DeoptArgs, ArrayRef< Value *> GCArgs, const Twine &Name="")
Create a call to the experimental.gc.statepoint intrinsic to start a new statepoint sequence...
Definition: IRBuilder.cpp:554
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:150
static CallInst * createCallHelper(Value *Callee, ArrayRef< Value *> Ops, IRBuilderBase *Builder, const Twine &Name="", Instruction *FMFSource=nullptr)
Definition: IRBuilder.cpp:74
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:421
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:282
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static InvokeInst * CreateGCStatepointInvokeCommon(IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, ArrayRef< T0 > InvokeArgs, ArrayRef< T1 > TransitionArgs, ArrayRef< T2 > DeoptArgs, ArrayRef< T3 > GCArgs, const Twine &Name)
Definition: IRBuilder.cpp:582
InvokeInst * CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> InvokeArgs, ArrayRef< Value *> DeoptArgs, ArrayRef< Value *> GCArgs, const Twine &Name="")
Create an invoke to the experimental.gc.statepoint intrinsic to start a new statepoint sequence...
Definition: IRBuilder.cpp:604
A specialization of it&#39;s base class for read-write access to a gc.statepoint.
Definition: Statepoint.h:319
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:312
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:312
Type * getCurrentFunctionReturnType() const
Get the return type of the current function that we&#39;re emitting into.
Definition: IRBuilder.cpp:56
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1382
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:385
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1222
static InvokeInst * Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value *> Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
CallInst * CreateLifetimeEnd(Value *Ptr, ConstantInt *Size=nullptr)
Create a lifetime.end intrinsic.
Definition: IRBuilder.cpp:346
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:317
CallInst * CreateAddReduce(Value *Src)
Create a vector int add reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:271
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
CallInst * CreateIntMaxReduce(Value *Src, bool IsSigned=false)
Create a vector integer max reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:296
GlobalVariable * CreateGlobalString(StringRef Str, const Twine &Name="", unsigned AddressSpace=0)
Make a new global variable with initializer type i8*.
Definition: IRBuilder.cpp:43
CallInst * CreateAssumption(Value *Cond)
Create an assume intrinsic call that allows the optimizer to assume that the provided condition will ...
Definition: IRBuilder.cpp:382
static CallInst * getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID, Value *Src)
Definition: IRBuilder.cpp:242
AddressSpace
Definition: NVPTXBaseInfo.h:22
iterator end() const
Definition: ArrayRef.h:138
CallInst * CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align, Value *Mask)
Create a call to Masked Store intrinsic.
Definition: IRBuilder.cpp:421
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:307
CallInst * CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memcpy between the specified pointers.
Definition: IRBuilder.h:422
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Value *> Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 or more operands assuming the intrinsic and all operands have th...
Definition: IRBuilder.cpp:671
static std::vector< Value * > getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags, ArrayRef< T0 > CallArgs, ArrayRef< T1 > TransitionArgs, ArrayRef< T2 > DeoptArgs, ArrayRef< T3 > GCArgs)
Definition: IRBuilder.cpp:510
CallInst * CreateFMulReduce(Value *Acc, Value *Src)
Create a vector fmul reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:261
CallInst * CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Load intrinsic.
Definition: IRBuilder.cpp:400
static InvokeInst * createInvokeHelper(Value *Invokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Ops, IRBuilderBase *Builder, const Twine &Name="")
Definition: IRBuilder.cpp:86
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:332
CallInst * CreateAndReduce(Value *Src)
Create a vector int AND reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:281
CallInst * CreateOrReduce(Value *Src)
Create a vector int OR reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:286
CallInst * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type. ...
Definition: IRBuilder.cpp:663
CallInst * CreateIntMinReduce(Value *Src, bool IsSigned=false)
Create a vector integer min reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:302
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:215
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
CallInst * CreateGCResult(Instruction *Statepoint, Type *ResultType, const Twine &Name="")
Create a call to the experimental.gc.result intrinsic to extract the result from a call wrapped in a ...
Definition: IRBuilder.cpp:635
CallInst * CreateMulReduce(Value *Src)
Create a vector int mul reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:276
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
CallInst * CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align, Value *Mask=nullptr)
Create a call to Masked Scatter intrinsic.
Definition: IRBuilder.cpp:483
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:73
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:593
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:122
Invoke instruction.
CallInst * CreateLifetimeStart(Value *Ptr, ConstantInt *Size=nullptr)
Create a lifetime.start intrinsic.
Definition: IRBuilder.cpp:330
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static bool isVolatile(Instruction *Inst)
CallInst * CreateElementUnorderedAtomicMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert an element unordered-atomic memcpy between the specified pointers.
Definition: IRBuilder.h:448
CallInst * CreateFPMaxReduce(Value *Src, bool NoNaN=false)
Create a vector float max reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:308
BasicBlock::iterator InsertPt
Definition: IRBuilder.h:94
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144