Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 1 | //===- IRBuilder.cpp - Builder for LLVM Instrs ----------------------------===// |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 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 | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GlobalVariable.h" |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Intrinsics.h" |
| 26 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Operator.h" |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Statepoint.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Type.h" |
| 30 | #include "llvm/IR/Value.h" |
| 31 | #include "llvm/Support/Casting.h" |
| 32 | #include "llvm/Support/MathExtras.h" |
| 33 | #include <cassert> |
| 34 | #include <cstdint> |
| 35 | #include <vector> |
| 36 | |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
| 39 | /// CreateGlobalString - Make a new global variable with an initializer that |
Dan Gohman | 97c5902 | 2010-02-10 20:04:19 +0000 | [diff] [blame] | 40 | /// 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] | 41 | /// specified. If Name is specified, it is the name of the global variable |
| 42 | /// created. |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 43 | GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str, |
Tobias Grosser | cdb8914 | 2015-06-19 02:12:07 +0000 | [diff] [blame] | 44 | const Twine &Name, |
| 45 | unsigned AddressSpace) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 46 | Constant *StrConstant = ConstantDataArray::getString(Context, Str); |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 47 | Module &M = *BB->getParent()->getParent(); |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 48 | auto *GV = new GlobalVariable(M, StrConstant->getType(), true, |
| 49 | GlobalValue::PrivateLinkage, StrConstant, Name, |
| 50 | nullptr, GlobalVariable::NotThreadLocal, |
| 51 | AddressSpace); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 52 | GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 53 | return GV; |
| 54 | } |
Chris Lattner | 7ef1cac | 2009-12-28 21:45:40 +0000 | [diff] [blame] | 55 | |
Chris Lattner | b1907b2 | 2011-07-12 04:14:22 +0000 | [diff] [blame] | 56 | Type *IRBuilderBase::getCurrentFunctionReturnType() const { |
Chris Lattner | 49f9f76 | 2009-12-28 21:50:56 +0000 | [diff] [blame] | 57 | assert(BB && BB->getParent() && "No current function!"); |
| 58 | return BB->getParent()->getReturnType(); |
| 59 | } |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 60 | |
| 61 | Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 62 | auto *PT = cast<PointerType>(Ptr->getType()); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 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); |
| 70 | SetInstDebugLocation(BCI); |
| 71 | return BCI; |
| 72 | } |
| 73 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 74 | static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops, |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 75 | IRBuilderBase *Builder, |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 76 | const Twine &Name = "", |
| 77 | Instruction *FMFSource = nullptr) { |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 78 | CallInst *CI = CallInst::Create(Callee, Ops, Name); |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 79 | if (FMFSource) |
| 80 | CI->copyFastMathFlags(FMFSource); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 81 | Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI); |
| 82 | Builder->SetInstDebugLocation(CI); |
| 83 | return CI; |
| 84 | } |
| 85 | |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 86 | static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest, |
| 87 | BasicBlock *UnwindDest, |
| 88 | ArrayRef<Value *> Ops, |
| 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 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 99 | CallInst *IRBuilderBase:: |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 100 | CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align, |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 101 | bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, |
| 102 | MDNode *NoAliasTag) { |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 103 | Ptr = getCastedInt8PtrValue(Ptr); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 104 | Value *Ops[] = {Ptr, Val, Size, getInt1(isVolatile)}; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 105 | Type *Tys[] = { Ptr->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 106 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 107 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 108 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 109 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 110 | |
| 111 | if (Align > 0) |
| 112 | cast<MemSetInst>(CI)->setDestAlignment(Align); |
| 113 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 114 | // Set the TBAA info if present. |
| 115 | if (TBAATag) |
| 116 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 117 | |
| 118 | if (ScopeTag) |
| 119 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 120 | |
| 121 | if (NoAliasTag) |
| 122 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 124 | return CI; |
| 125 | } |
| 126 | |
| 127 | CallInst *IRBuilderBase:: |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 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"); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 133 | Dst = getCastedInt8PtrValue(Dst); |
| 134 | Src = getCastedInt8PtrValue(Src); |
| 135 | |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 136 | Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)}; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 137 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 138 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 139 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 140 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 141 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 142 | |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 143 | auto* MCI = cast<MemCpyInst>(CI); |
| 144 | if (DstAlign > 0) |
| 145 | MCI->setDestAlignment(DstAlign); |
| 146 | if (SrcAlign > 0) |
| 147 | MCI->setSourceAlignment(SrcAlign); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 149 | // Set the TBAA info if present. |
| 150 | if (TBAATag) |
| 151 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Dan Gohman | 099727f | 2012-09-26 22:17:14 +0000 | [diff] [blame] | 152 | |
| 153 | // Set the TBAA Struct info if present. |
| 154 | if (TBAAStructTag) |
| 155 | CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 156 | |
| 157 | if (ScopeTag) |
| 158 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 159 | |
| 160 | if (NoAliasTag) |
| 161 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 163 | return CI; |
| 164 | } |
| 165 | |
Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 166 | CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy( |
Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 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"); |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 174 | Dst = getCastedInt8PtrValue(Dst); |
| 175 | Src = getCastedInt8PtrValue(Src); |
| 176 | |
Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 177 | Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)}; |
| 178 | Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()}; |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 179 | Module *M = BB->getParent()->getParent(); |
Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 180 | Value *TheFn = Intrinsic::getDeclaration( |
| 181 | M, Intrinsic::memcpy_element_unordered_atomic, Tys); |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 182 | |
| 183 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
| 184 | |
Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 185 | // Set the alignment of the pointer args. |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 186 | auto *AMCI = cast<AtomicMemCpyInst>(CI); |
| 187 | AMCI->setDestAlignment(DstAlign); |
| 188 | AMCI->setSourceAlignment(SrcAlign); |
Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 189 | |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 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) |
| 199 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 200 | |
| 201 | if (NoAliasTag) |
| 202 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 203 | |
| 204 | return CI; |
| 205 | } |
| 206 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 207 | CallInst *IRBuilderBase:: |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 208 | CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, |
| 209 | Value *Size, bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 210 | MDNode *NoAliasTag) { |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 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"); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 213 | Dst = getCastedInt8PtrValue(Dst); |
| 214 | Src = getCastedInt8PtrValue(Src); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 215 | |
| 216 | Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)}; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 217 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 218 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 219 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 220 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 221 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 222 | |
| 223 | auto *MMI = cast<MemMoveInst>(CI); |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 224 | if (DstAlign > 0) |
| 225 | MMI->setDestAlignment(DstAlign); |
| 226 | if (SrcAlign > 0) |
| 227 | MMI->setSourceAlignment(SrcAlign); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 229 | // Set the TBAA info if present. |
| 230 | if (TBAATag) |
| 231 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 232 | |
| 233 | if (ScopeTag) |
| 234 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 235 | |
| 236 | if (NoAliasTag) |
| 237 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 238 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 239 | return CI; |
| 240 | } |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 241 | |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 242 | static CallInst *getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID, |
| 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 | |
| 251 | CallInst *IRBuilderBase::CreateFAddReduce(Value *Acc, Value *Src) { |
| 252 | Module *M = GetInsertBlock()->getParent()->getParent(); |
| 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 | |
| 261 | CallInst *IRBuilderBase::CreateFMulReduce(Value *Acc, Value *Src) { |
| 262 | Module *M = GetInsertBlock()->getParent()->getParent(); |
| 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 | |
| 271 | CallInst *IRBuilderBase::CreateAddReduce(Value *Src) { |
| 272 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add, |
| 273 | Src); |
| 274 | } |
| 275 | |
| 276 | CallInst *IRBuilderBase::CreateMulReduce(Value *Src) { |
| 277 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_mul, |
| 278 | Src); |
| 279 | } |
| 280 | |
| 281 | CallInst *IRBuilderBase::CreateAndReduce(Value *Src) { |
| 282 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_and, |
| 283 | Src); |
| 284 | } |
| 285 | |
| 286 | CallInst *IRBuilderBase::CreateOrReduce(Value *Src) { |
| 287 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_or, |
| 288 | Src); |
| 289 | } |
| 290 | |
| 291 | CallInst *IRBuilderBase::CreateXorReduce(Value *Src) { |
| 292 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_xor, |
| 293 | Src); |
| 294 | } |
| 295 | |
| 296 | CallInst *IRBuilderBase::CreateIntMaxReduce(Value *Src, bool IsSigned) { |
| 297 | auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smax |
| 298 | : Intrinsic::experimental_vector_reduce_umax; |
| 299 | return getReductionIntrinsic(this, ID, Src); |
| 300 | } |
| 301 | |
| 302 | CallInst *IRBuilderBase::CreateIntMinReduce(Value *Src, bool IsSigned) { |
| 303 | auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smin |
| 304 | : Intrinsic::experimental_vector_reduce_umin; |
| 305 | return getReductionIntrinsic(this, ID, Src); |
| 306 | } |
| 307 | |
| 308 | CallInst *IRBuilderBase::CreateFPMaxReduce(Value *Src, bool NoNaN) { |
| 309 | auto Rdx = getReductionIntrinsic( |
| 310 | this, Intrinsic::experimental_vector_reduce_fmax, Src); |
| 311 | if (NoNaN) { |
| 312 | FastMathFlags FMF; |
| 313 | FMF.setNoNaNs(); |
| 314 | Rdx->setFastMathFlags(FMF); |
| 315 | } |
| 316 | return Rdx; |
| 317 | } |
| 318 | |
| 319 | CallInst *IRBuilderBase::CreateFPMinReduce(Value *Src, bool NoNaN) { |
| 320 | auto Rdx = getReductionIntrinsic( |
| 321 | this, Intrinsic::experimental_vector_reduce_fmin, Src); |
| 322 | if (NoNaN) { |
| 323 | FastMathFlags FMF; |
| 324 | FMF.setNoNaNs(); |
| 325 | Rdx->setFastMathFlags(FMF); |
| 326 | } |
| 327 | return Rdx; |
| 328 | } |
| 329 | |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 330 | CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) { |
| 331 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 332 | "lifetime.start only applies to pointers."); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 333 | Ptr = getCastedInt8PtrValue(Ptr); |
| 334 | if (!Size) |
| 335 | Size = getInt64(-1); |
| 336 | else |
| 337 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 338 | "lifetime.start requires the size to be an i64"); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 339 | Value *Ops[] = { Size, Ptr }; |
| 340 | Module *M = BB->getParent()->getParent(); |
Matt Arsenault | f10061e | 2017-04-10 20:18:21 +0000 | [diff] [blame] | 341 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start, |
| 342 | { Ptr->getType() }); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 343 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { |
| 347 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 348 | "lifetime.end only applies to pointers."); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 349 | Ptr = getCastedInt8PtrValue(Ptr); |
| 350 | if (!Size) |
| 351 | Size = getInt64(-1); |
| 352 | else |
| 353 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 354 | "lifetime.end requires the size to be an i64"); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 355 | Value *Ops[] = { Size, Ptr }; |
| 356 | Module *M = BB->getParent()->getParent(); |
Matt Arsenault | f10061e | 2017-04-10 20:18:21 +0000 | [diff] [blame] | 357 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end, |
| 358 | { Ptr->getType() }); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 359 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 360 | } |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 361 | |
Anna Thomas | 58d1192 | 2016-07-22 20:57:23 +0000 | [diff] [blame] | 362 | CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) { |
| 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 | |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 382 | CallInst *IRBuilderBase::CreateAssumption(Value *Cond) { |
| 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 | |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 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 |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 400 | CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align, |
| 401 | Value *Mask, Value *PassThru, |
| 402 | const Twine &Name) { |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 403 | auto *PtrTy = cast<PointerType>(Ptr->getType()); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 404 | Type *DataTy = PtrTy->getElementType(); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 405 | assert(DataTy->isVectorTy() && "Ptr should point to a vector"); |
Ayal Zaks | e841b21 | 2017-07-31 13:21:42 +0000 | [diff] [blame] | 406 | assert(Mask && "Mask should not be all-ones (null)"); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 407 | if (!PassThru) |
| 408 | PassThru = UndefValue::get(DataTy); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 409 | Type *OverloadedTypes[] = { DataTy, PtrTy }; |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 410 | Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru}; |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 411 | return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, |
| 412 | OverloadedTypes, Name); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 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 |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 421 | CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr, |
| 422 | unsigned Align, Value *Mask) { |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 423 | auto *PtrTy = cast<PointerType>(Ptr->getType()); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 424 | Type *DataTy = PtrTy->getElementType(); |
| 425 | assert(DataTy->isVectorTy() && "Ptr should point to a vector"); |
Ayal Zaks | e841b21 | 2017-07-31 13:21:42 +0000 | [diff] [blame] | 426 | assert(Mask && "Mask should not be all-ones (null)"); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 427 | Type *OverloadedTypes[] = { DataTy, PtrTy }; |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 428 | Value *Ops[] = { Val, Ptr, getInt32(Align), Mask }; |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 429 | return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, OverloadedTypes); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /// Create a call to a Masked intrinsic, with given intrinsic Id, |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 433 | /// an array of operands - Ops, and an array of overloaded types - |
| 434 | /// OverloadedTypes. |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 435 | CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id, |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 436 | ArrayRef<Value *> Ops, |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 437 | ArrayRef<Type *> OverloadedTypes, |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 438 | const Twine &Name) { |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 439 | Module *M = BB->getParent()->getParent(); |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 440 | Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 441 | return createCallHelper(TheFn, Ops, this, Name); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 442 | } |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 443 | |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 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 |
| 452 | CallInst *IRBuilderBase::CreateMaskedGather(Value *Ptrs, unsigned Align, |
| 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) |
| 461 | Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context), |
| 462 | NumElts)); |
| 463 | |
Amara Emerson | 4d33c86 | 2017-05-19 10:40:18 +0000 | [diff] [blame] | 464 | if (!PassThru) |
| 465 | PassThru = UndefValue::get(DataTy); |
| 466 | |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 467 | Type *OverloadedTypes[] = {DataTy, PtrsTy}; |
Amara Emerson | 4d33c86 | 2017-05-19 10:40:18 +0000 | [diff] [blame] | 468 | Value * Ops[] = {Ptrs, getInt32(Align), Mask, PassThru}; |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 469 | |
| 470 | // We specify only one type when we create this intrinsic. Types of other |
| 471 | // arguments are derived from this type. |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 472 | return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes, |
| 473 | Name); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 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 |
| 483 | CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs, |
| 484 | unsigned Align, Value *Mask) { |
| 485 | auto PtrsTy = cast<VectorType>(Ptrs->getType()); |
| 486 | auto DataTy = cast<VectorType>(Data->getType()); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 487 | unsigned NumElts = PtrsTy->getVectorNumElements(); |
| 488 | |
Tim Northover | 5a1a56c | 2016-02-17 21:16:59 +0000 | [diff] [blame] | 489 | #ifndef NDEBUG |
| 490 | auto PtrTy = cast<PointerType>(PtrsTy->getElementType()); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 491 | assert(NumElts == DataTy->getVectorNumElements() && |
Tim Northover | 5a1a56c | 2016-02-17 21:16:59 +0000 | [diff] [blame] | 492 | PtrTy->getElementType() == DataTy->getElementType() && |
| 493 | "Incompatible pointer and data types"); |
| 494 | #endif |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 495 | |
| 496 | if (!Mask) |
| 497 | Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context), |
| 498 | NumElts)); |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 499 | |
| 500 | Type *OverloadedTypes[] = {DataTy, PtrsTy}; |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 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. |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 505 | return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, OverloadedTypes); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 508 | template <typename T0, typename T1, typename T2, typename T3> |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 509 | static std::vector<Value *> |
| 510 | getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 511 | Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, |
| 512 | ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, |
| 513 | ArrayRef<T3> GCArgs) { |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 514 | std::vector<Value *> Args; |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 515 | Args.push_back(B.getInt64(ID)); |
| 516 | Args.push_back(B.getInt32(NumPatchBytes)); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 517 | Args.push_back(ActualCallee); |
| 518 | Args.push_back(B.getInt32(CallArgs.size())); |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 519 | Args.push_back(B.getInt32(Flags)); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 520 | Args.insert(Args.end(), CallArgs.begin(), CallArgs.end()); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 521 | Args.push_back(B.getInt32(TransitionArgs.size())); |
| 522 | Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end()); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 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 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 530 | template <typename T0, typename T1, typename T2, typename T3> |
| 531 | static CallInst *CreateGCStatepointCallCommon( |
| 532 | IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 533 | Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 534 | ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, |
| 535 | const Twine &Name) { |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 536 | // Extract out the type of the callee. |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 537 | auto *FuncPtrType = cast<PointerType>(ActualCallee->getType()); |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 538 | assert(isa<FunctionType>(FuncPtrType->getElementType()) && |
| 539 | "actual callee must be a callable value"); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 540 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 541 | Module *M = Builder->GetInsertBlock()->getParent()->getParent(); |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 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); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 547 | |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 548 | std::vector<Value *> Args = |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 549 | getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags, |
| 550 | CallArgs, TransitionArgs, DeoptArgs, GCArgs); |
| 551 | return createCallHelper(FnStatepoint, Args, Builder, Name); |
| 552 | } |
| 553 | |
| 554 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
| 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 *>( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 559 | this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None), |
| 560 | CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 564 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags, |
| 565 | ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 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); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 572 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
| 573 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, |
| 574 | ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs, |
| 575 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 576 | return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 577 | this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None), |
| 578 | CallArgs, None, DeoptArgs, GCArgs, Name); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | template <typename T0, typename T1, typename T2, typename T3> |
| 582 | static InvokeInst *CreateGCStatepointInvokeCommon( |
| 583 | IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, |
| 584 | Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 585 | uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 586 | ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) { |
| 587 | // Extract out the type of the callee. |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 588 | auto *FuncPtrType = cast<PointerType>(ActualInvokee->getType()); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 597 | std::vector<Value *> Args = |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 598 | getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags, |
| 599 | InvokeArgs, TransitionArgs, DeoptArgs, GCArgs); |
| 600 | return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder, |
| 601 | Name); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 605 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
| 606 | BasicBlock *NormalDest, BasicBlock *UnwindDest, |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 607 | ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs, |
| 608 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 609 | return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>( |
| 610 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 611 | uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 612 | DeoptArgs, GCArgs, Name); |
| 613 | } |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 614 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 615 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
| 616 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 617 | BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 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); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 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) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 629 | return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>( |
| 630 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 631 | uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs, |
| 632 | Name); |
Ramkumar Ramachandra | 3408f3e | 2015-02-26 00:35:56 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 635 | CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint, |
| 636 | Type *ResultType, |
| 637 | const Twine &Name) { |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 638 | Intrinsic::ID ID = Intrinsic::experimental_gc_result; |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 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 | |
| 647 | CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint, |
| 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 | } |
Matt Arsenault | cdb468c | 2017-02-27 23:08:49 +0000 | [diff] [blame] | 662 | |
| 663 | CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID, |
| 664 | Value *LHS, Value *RHS, |
| 665 | const Twine &Name) { |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 666 | Module *M = BB->getModule(); |
| 667 | Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() }); |
Matt Arsenault | cdb468c | 2017-02-27 23:08:49 +0000 | [diff] [blame] | 668 | return createCallHelper(Fn, { LHS, RHS }, this, Name); |
| 669 | } |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 670 | |
| 671 | CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID, |
| 672 | ArrayRef<Value *> Args, |
| 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 | } |