Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Function.h" |
| 16 | #include "llvm/IR/GlobalVariable.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
| 18 | #include "llvm/IR/Intrinsics.h" |
| 19 | #include "llvm/IR/LLVMContext.h" |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Statepoint.h" |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | /// CreateGlobalString - Make a new global variable with an initializer that |
Dan Gohman | 97c5902 | 2010-02-10 20:04:19 +0000 | [diff] [blame] | 24 | /// has array of i8 type filled in with the nul terminated string value |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 25 | /// specified. If Name is specified, it is the name of the global variable |
| 26 | /// created. |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 27 | GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str, |
Tobias Grosser | cdb8914 | 2015-06-19 02:12:07 +0000 | [diff] [blame] | 28 | const Twine &Name, |
| 29 | unsigned AddressSpace) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 30 | Constant *StrConstant = ConstantDataArray::getString(Context, Str); |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 31 | Module &M = *BB->getParent()->getParent(); |
| 32 | GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(), |
Benjamin Kramer | f1fd6e3 | 2011-12-22 14:22:14 +0000 | [diff] [blame] | 33 | true, GlobalValue::PrivateLinkage, |
Tobias Grosser | cdb8914 | 2015-06-19 02:12:07 +0000 | [diff] [blame] | 34 | StrConstant, Name, nullptr, |
| 35 | GlobalVariable::NotThreadLocal, |
| 36 | AddressSpace); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 37 | GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 38 | return GV; |
| 39 | } |
Chris Lattner | 7ef1cac | 2009-12-28 21:45:40 +0000 | [diff] [blame] | 40 | |
Chris Lattner | b1907b2 | 2011-07-12 04:14:22 +0000 | [diff] [blame] | 41 | Type *IRBuilderBase::getCurrentFunctionReturnType() const { |
Chris Lattner | 49f9f76 | 2009-12-28 21:50:56 +0000 | [diff] [blame] | 42 | assert(BB && BB->getParent() && "No current function!"); |
| 43 | return BB->getParent()->getReturnType(); |
| 44 | } |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 45 | |
| 46 | Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 47 | PointerType *PT = cast<PointerType>(Ptr->getType()); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 48 | if (PT->getElementType()->isIntegerTy(8)) |
| 49 | return Ptr; |
| 50 | |
| 51 | // Otherwise, we need to insert a bitcast. |
| 52 | PT = getInt8PtrTy(PT->getAddressSpace()); |
| 53 | BitCastInst *BCI = new BitCastInst(Ptr, PT, ""); |
| 54 | BB->getInstList().insert(InsertPt, BCI); |
| 55 | SetInstDebugLocation(BCI); |
| 56 | return BCI; |
| 57 | } |
| 58 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 59 | static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops, |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 60 | IRBuilderBase *Builder, |
| 61 | const Twine& Name="") { |
| 62 | CallInst *CI = CallInst::Create(Callee, Ops, Name); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 63 | Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI); |
| 64 | Builder->SetInstDebugLocation(CI); |
| 65 | return CI; |
| 66 | } |
| 67 | |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 68 | static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest, |
| 69 | BasicBlock *UnwindDest, |
| 70 | ArrayRef<Value *> Ops, |
| 71 | IRBuilderBase *Builder, |
| 72 | const Twine &Name = "") { |
| 73 | InvokeInst *II = |
| 74 | InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name); |
| 75 | Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(), |
| 76 | II); |
| 77 | Builder->SetInstDebugLocation(II); |
| 78 | return II; |
| 79 | } |
| 80 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 81 | CallInst *IRBuilderBase:: |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 82 | CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align, |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 83 | bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, |
| 84 | MDNode *NoAliasTag) { |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 85 | Ptr = getCastedInt8PtrValue(Ptr); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 86 | Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) }; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 87 | Type *Tys[] = { Ptr->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 88 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 89 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 90 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 91 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 92 | |
| 93 | // Set the TBAA info if present. |
| 94 | if (TBAATag) |
| 95 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 96 | |
| 97 | if (ScopeTag) |
| 98 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 99 | |
| 100 | if (NoAliasTag) |
| 101 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 102 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 103 | return CI; |
| 104 | } |
| 105 | |
| 106 | CallInst *IRBuilderBase:: |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 107 | CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 108 | bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag, |
| 109 | MDNode *ScopeTag, MDNode *NoAliasTag) { |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 110 | Dst = getCastedInt8PtrValue(Dst); |
| 111 | Src = getCastedInt8PtrValue(Src); |
| 112 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 113 | Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) }; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 114 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 115 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 116 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 117 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 118 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 119 | |
| 120 | // Set the TBAA info if present. |
| 121 | if (TBAATag) |
| 122 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Dan Gohman | 099727f | 2012-09-26 22:17:14 +0000 | [diff] [blame] | 123 | |
| 124 | // Set the TBAA Struct info if present. |
| 125 | if (TBAAStructTag) |
| 126 | CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 127 | |
| 128 | if (ScopeTag) |
| 129 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 130 | |
| 131 | if (NoAliasTag) |
| 132 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 133 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 134 | return CI; |
| 135 | } |
| 136 | |
| 137 | CallInst *IRBuilderBase:: |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 138 | CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align, |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 139 | bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, |
| 140 | MDNode *NoAliasTag) { |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 141 | Dst = getCastedInt8PtrValue(Dst); |
| 142 | Src = getCastedInt8PtrValue(Src); |
| 143 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 144 | Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) }; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 145 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 146 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 147 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 148 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 149 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 150 | |
| 151 | // Set the TBAA info if present. |
| 152 | if (TBAATag) |
| 153 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 154 | |
| 155 | if (ScopeTag) |
| 156 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 157 | |
| 158 | if (NoAliasTag) |
| 159 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 160 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 161 | return CI; |
| 162 | } |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 163 | |
| 164 | CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) { |
| 165 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 166 | "lifetime.start only applies to pointers."); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 167 | Ptr = getCastedInt8PtrValue(Ptr); |
| 168 | if (!Size) |
| 169 | Size = getInt64(-1); |
| 170 | else |
| 171 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 172 | "lifetime.start requires the size to be an i64"); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 173 | Value *Ops[] = { Size, Ptr }; |
| 174 | Module *M = BB->getParent()->getParent(); |
| 175 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 176 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { |
| 180 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 181 | "lifetime.end only applies to pointers."); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 182 | Ptr = getCastedInt8PtrValue(Ptr); |
| 183 | if (!Size) |
| 184 | Size = getInt64(-1); |
| 185 | else |
| 186 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 187 | "lifetime.end requires the size to be an i64"); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 188 | Value *Ops[] = { Size, Ptr }; |
| 189 | Module *M = BB->getParent()->getParent(); |
| 190 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 191 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 192 | } |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 193 | |
Anna Thomas | 58d1192 | 2016-07-22 20:57:23 +0000 | [diff] [blame] | 194 | CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) { |
| 195 | |
| 196 | assert(isa<PointerType>(Ptr->getType()) && |
| 197 | "invariant.start only applies to pointers."); |
| 198 | Ptr = getCastedInt8PtrValue(Ptr); |
| 199 | if (!Size) |
| 200 | Size = getInt64(-1); |
| 201 | else |
| 202 | assert(Size->getType() == getInt64Ty() && |
| 203 | "invariant.start requires the size to be an i64"); |
| 204 | |
| 205 | Value *Ops[] = {Size, Ptr}; |
| 206 | // Fill in the single overloaded type: memory object type. |
| 207 | Type *ObjectPtr[1] = {Ptr->getType()}; |
| 208 | Module *M = BB->getParent()->getParent(); |
| 209 | Value *TheFn = |
| 210 | Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr); |
| 211 | return createCallHelper(TheFn, Ops, this); |
| 212 | } |
| 213 | |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 214 | CallInst *IRBuilderBase::CreateAssumption(Value *Cond) { |
| 215 | assert(Cond->getType() == getInt1Ty() && |
| 216 | "an assumption condition must be of type i1"); |
| 217 | |
| 218 | Value *Ops[] = { Cond }; |
| 219 | Module *M = BB->getParent()->getParent(); |
| 220 | Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume); |
| 221 | return createCallHelper(FnAssume, Ops, this); |
| 222 | } |
| 223 | |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 224 | /// \brief Create a call to a Masked Load intrinsic. |
| 225 | /// \p Ptr - base pointer for the load |
| 226 | /// \p Align - alignment of the source location |
| 227 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 228 | /// be accessed in memory |
| 229 | /// \p PassThru - pass-through value that is used to fill the masked-off lanes |
| 230 | /// of the result |
| 231 | /// \p Name - name of the result variable |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 232 | CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align, |
| 233 | Value *Mask, Value *PassThru, |
| 234 | const Twine &Name) { |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 235 | PointerType *PtrTy = cast<PointerType>(Ptr->getType()); |
| 236 | Type *DataTy = PtrTy->getElementType(); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 237 | assert(DataTy->isVectorTy() && "Ptr should point to a vector"); |
| 238 | if (!PassThru) |
| 239 | PassThru = UndefValue::get(DataTy); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 240 | Type *OverloadedTypes[] = { DataTy, PtrTy }; |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 241 | Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru}; |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 242 | return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, |
| 243 | OverloadedTypes, Name); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 246 | /// \brief Create a call to a Masked Store intrinsic. |
| 247 | /// \p Val - data to be stored, |
| 248 | /// \p Ptr - base pointer for the store |
| 249 | /// \p Align - alignment of the destination location |
| 250 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 251 | /// be accessed in memory |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 252 | CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr, |
| 253 | unsigned Align, Value *Mask) { |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 254 | PointerType *PtrTy = cast<PointerType>(Ptr->getType()); |
| 255 | Type *DataTy = PtrTy->getElementType(); |
| 256 | assert(DataTy->isVectorTy() && "Ptr should point to a vector"); |
| 257 | Type *OverloadedTypes[] = { DataTy, PtrTy }; |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 258 | Value *Ops[] = { Val, Ptr, getInt32(Align), Mask }; |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 259 | return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, OverloadedTypes); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /// Create a call to a Masked intrinsic, with given intrinsic Id, |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 263 | /// an array of operands - Ops, and an array of overloaded types - |
| 264 | /// OverloadedTypes. |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 265 | CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id, |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 266 | ArrayRef<Value *> Ops, |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 267 | ArrayRef<Type *> OverloadedTypes, |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 268 | const Twine &Name) { |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 269 | Module *M = BB->getParent()->getParent(); |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 270 | Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 271 | return createCallHelper(TheFn, Ops, this, Name); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 272 | } |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 273 | |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 274 | /// \brief Create a call to a Masked Gather intrinsic. |
| 275 | /// \p Ptrs - vector of pointers for loading |
| 276 | /// \p Align - alignment for one element |
| 277 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 278 | /// be accessed in memory |
| 279 | /// \p PassThru - pass-through value that is used to fill the masked-off lanes |
| 280 | /// of the result |
| 281 | /// \p Name - name of the result variable |
| 282 | CallInst *IRBuilderBase::CreateMaskedGather(Value *Ptrs, unsigned Align, |
| 283 | Value *Mask, Value *PassThru, |
| 284 | const Twine& Name) { |
| 285 | auto PtrsTy = cast<VectorType>(Ptrs->getType()); |
| 286 | auto PtrTy = cast<PointerType>(PtrsTy->getElementType()); |
| 287 | unsigned NumElts = PtrsTy->getVectorNumElements(); |
| 288 | Type *DataTy = VectorType::get(PtrTy->getElementType(), NumElts); |
| 289 | |
| 290 | if (!Mask) |
| 291 | Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context), |
| 292 | NumElts)); |
| 293 | |
| 294 | Value * Ops[] = {Ptrs, getInt32(Align), Mask, UndefValue::get(DataTy)}; |
| 295 | |
| 296 | // We specify only one type when we create this intrinsic. Types of other |
| 297 | // arguments are derived from this type. |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 298 | return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, { DataTy }, Name); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /// \brief Create a call to a Masked Scatter intrinsic. |
| 302 | /// \p Data - data to be stored, |
| 303 | /// \p Ptrs - the vector of pointers, where the \p Data elements should be |
| 304 | /// stored |
| 305 | /// \p Align - alignment for one element |
| 306 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 307 | /// be accessed in memory |
| 308 | CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs, |
| 309 | unsigned Align, Value *Mask) { |
| 310 | auto PtrsTy = cast<VectorType>(Ptrs->getType()); |
| 311 | auto DataTy = cast<VectorType>(Data->getType()); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 312 | unsigned NumElts = PtrsTy->getVectorNumElements(); |
| 313 | |
Tim Northover | 5a1a56c | 2016-02-17 21:16:59 +0000 | [diff] [blame] | 314 | #ifndef NDEBUG |
| 315 | auto PtrTy = cast<PointerType>(PtrsTy->getElementType()); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 316 | assert(NumElts == DataTy->getVectorNumElements() && |
Tim Northover | 5a1a56c | 2016-02-17 21:16:59 +0000 | [diff] [blame] | 317 | PtrTy->getElementType() == DataTy->getElementType() && |
| 318 | "Incompatible pointer and data types"); |
| 319 | #endif |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 320 | |
| 321 | if (!Mask) |
| 322 | Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context), |
| 323 | NumElts)); |
| 324 | Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask}; |
| 325 | |
| 326 | // We specify only one type when we create this intrinsic. Types of other |
| 327 | // arguments are derived from this type. |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 328 | return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, { DataTy }); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 331 | template <typename T0, typename T1, typename T2, typename T3> |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 332 | static std::vector<Value *> |
| 333 | getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 334 | Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, |
| 335 | ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, |
| 336 | ArrayRef<T3> GCArgs) { |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 337 | std::vector<Value *> Args; |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 338 | Args.push_back(B.getInt64(ID)); |
| 339 | Args.push_back(B.getInt32(NumPatchBytes)); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 340 | Args.push_back(ActualCallee); |
| 341 | Args.push_back(B.getInt32(CallArgs.size())); |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 342 | Args.push_back(B.getInt32(Flags)); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 343 | Args.insert(Args.end(), CallArgs.begin(), CallArgs.end()); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 344 | Args.push_back(B.getInt32(TransitionArgs.size())); |
| 345 | Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end()); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 346 | Args.push_back(B.getInt32(DeoptArgs.size())); |
| 347 | Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end()); |
| 348 | Args.insert(Args.end(), GCArgs.begin(), GCArgs.end()); |
| 349 | |
| 350 | return Args; |
| 351 | } |
| 352 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 353 | template <typename T0, typename T1, typename T2, typename T3> |
| 354 | static CallInst *CreateGCStatepointCallCommon( |
| 355 | IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 356 | Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 357 | ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, |
| 358 | const Twine &Name) { |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 359 | // Extract out the type of the callee. |
| 360 | PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType()); |
| 361 | assert(isa<FunctionType>(FuncPtrType->getElementType()) && |
| 362 | "actual callee must be a callable value"); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 363 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 364 | Module *M = Builder->GetInsertBlock()->getParent()->getParent(); |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 365 | // Fill in the one generic type'd argument (the function is also vararg) |
| 366 | Type *ArgTypes[] = { FuncPtrType }; |
| 367 | Function *FnStatepoint = |
| 368 | Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint, |
| 369 | ArgTypes); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 370 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 371 | std::vector<llvm::Value *> Args = |
| 372 | getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags, |
| 373 | CallArgs, TransitionArgs, DeoptArgs, GCArgs); |
| 374 | return createCallHelper(FnStatepoint, Args, Builder, Name); |
| 375 | } |
| 376 | |
| 377 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
| 378 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, |
| 379 | ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs, |
| 380 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
| 381 | return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 382 | this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None), |
| 383 | CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 387 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags, |
| 388 | ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 389 | ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) { |
| 390 | return CreateGCStatepointCallCommon<Use, Use, Use, Value *>( |
| 391 | this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs, |
| 392 | DeoptArgs, GCArgs, Name); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 395 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
| 396 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, |
| 397 | ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs, |
| 398 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 399 | return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 400 | this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None), |
| 401 | CallArgs, None, DeoptArgs, GCArgs, Name); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | template <typename T0, typename T1, typename T2, typename T3> |
| 405 | static InvokeInst *CreateGCStatepointInvokeCommon( |
| 406 | IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, |
| 407 | Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 408 | uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 409 | ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) { |
| 410 | // Extract out the type of the callee. |
| 411 | PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType()); |
| 412 | assert(isa<FunctionType>(FuncPtrType->getElementType()) && |
| 413 | "actual callee must be a callable value"); |
| 414 | |
| 415 | Module *M = Builder->GetInsertBlock()->getParent()->getParent(); |
| 416 | // Fill in the one generic type'd argument (the function is also vararg) |
| 417 | Function *FnStatepoint = Intrinsic::getDeclaration( |
| 418 | M, Intrinsic::experimental_gc_statepoint, {FuncPtrType}); |
| 419 | |
| 420 | std::vector<llvm::Value *> Args = |
| 421 | getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags, |
| 422 | InvokeArgs, TransitionArgs, DeoptArgs, GCArgs); |
| 423 | return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder, |
| 424 | Name); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 428 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
| 429 | BasicBlock *NormalDest, BasicBlock *UnwindDest, |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 430 | ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs, |
| 431 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 432 | return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>( |
| 433 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 434 | uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 435 | DeoptArgs, GCArgs, Name); |
| 436 | } |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 437 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 438 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
| 439 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 440 | BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 441 | ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs, |
| 442 | ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) { |
| 443 | return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>( |
| 444 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags, |
| 445 | InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 449 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
| 450 | BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, |
| 451 | ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 452 | return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>( |
| 453 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 454 | uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs, |
| 455 | Name); |
Ramkumar Ramachandra | 3408f3e | 2015-02-26 00:35:56 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 458 | CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint, |
| 459 | Type *ResultType, |
| 460 | const Twine &Name) { |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 461 | Intrinsic::ID ID = Intrinsic::experimental_gc_result; |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 462 | Module *M = BB->getParent()->getParent(); |
| 463 | Type *Types[] = {ResultType}; |
| 464 | Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types); |
| 465 | |
| 466 | Value *Args[] = {Statepoint}; |
| 467 | return createCallHelper(FnGCResult, Args, this, Name); |
| 468 | } |
| 469 | |
| 470 | CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint, |
| 471 | int BaseOffset, |
| 472 | int DerivedOffset, |
| 473 | Type *ResultType, |
| 474 | const Twine &Name) { |
| 475 | Module *M = BB->getParent()->getParent(); |
| 476 | Type *Types[] = {ResultType}; |
| 477 | Value *FnGCRelocate = |
| 478 | Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types); |
| 479 | |
| 480 | Value *Args[] = {Statepoint, |
| 481 | getInt32(BaseOffset), |
| 482 | getInt32(DerivedOffset)}; |
| 483 | return createCallHelper(FnGCRelocate, Args, this, Name); |
| 484 | } |