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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the IRBuilder class, which is used as a convenient way |
| 10 | // to create LLVM instructions with a consistent and simplified interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
| 16 | #include "llvm/ADT/None.h" |
| 17 | #include "llvm/IR/Constant.h" |
| 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 21 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GlobalVariable.h" |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Intrinsics.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Operator.h" |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Statepoint.h" |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Type.h" |
| 29 | #include "llvm/IR/Value.h" |
| 30 | #include "llvm/Support/Casting.h" |
| 31 | #include "llvm/Support/MathExtras.h" |
| 32 | #include <cassert> |
| 33 | #include <cstdint> |
| 34 | #include <vector> |
| 35 | |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
| 38 | /// CreateGlobalString - Make a new global variable with an initializer that |
Dan Gohman | 97c5902 | 2010-02-10 20:04:19 +0000 | [diff] [blame] | 39 | /// 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] | 40 | /// specified. If Name is specified, it is the name of the global variable |
| 41 | /// created. |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 42 | GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str, |
Tobias Grosser | cdb8914 | 2015-06-19 02:12:07 +0000 | [diff] [blame] | 43 | const Twine &Name, |
| 44 | unsigned AddressSpace) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 45 | Constant *StrConstant = ConstantDataArray::getString(Context, Str); |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 46 | Module &M = *BB->getParent()->getParent(); |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 47 | auto *GV = new GlobalVariable(M, StrConstant->getType(), true, |
| 48 | GlobalValue::PrivateLinkage, StrConstant, Name, |
| 49 | nullptr, GlobalVariable::NotThreadLocal, |
| 50 | AddressSpace); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 51 | GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
David Green | e6918ca | 2018-09-06 08:42:17 +0000 | [diff] [blame] | 52 | GV->setAlignment(1); |
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; |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 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 | |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 74 | static CallInst *createCallHelper(Function *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); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 83 | return CI; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
James Y Knight | d9e85a0 | 2019-02-01 20:43:34 +0000 | [diff] [blame] | 86 | static InvokeInst *createInvokeHelper(Function *Invokee, BasicBlock *NormalDest, |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 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(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 107 | Function *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +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); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 120 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 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 | |
Daniel Neilson | 936d50a | 2018-05-30 20:02:56 +0000 | [diff] [blame] | 127 | CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemSet( |
| 128 | Value *Ptr, Value *Val, Value *Size, unsigned Align, uint32_t ElementSize, |
| 129 | MDNode *TBAATag, MDNode *ScopeTag, MDNode *NoAliasTag) { |
| 130 | assert(Align >= ElementSize && |
| 131 | "Pointer alignment must be at least element size."); |
| 132 | |
| 133 | Ptr = getCastedInt8PtrValue(Ptr); |
| 134 | Value *Ops[] = {Ptr, Val, Size, getInt32(ElementSize)}; |
| 135 | Type *Tys[] = {Ptr->getType(), Size->getType()}; |
| 136 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 137 | Function *TheFn = Intrinsic::getDeclaration( |
Daniel Neilson | 936d50a | 2018-05-30 20:02:56 +0000 | [diff] [blame] | 138 | M, Intrinsic::memset_element_unordered_atomic, Tys); |
| 139 | |
| 140 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
| 141 | |
| 142 | cast<AtomicMemSetInst>(CI)->setDestAlignment(Align); |
| 143 | |
| 144 | // Set the TBAA info if present. |
| 145 | if (TBAATag) |
| 146 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 147 | |
| 148 | if (ScopeTag) |
| 149 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 150 | |
| 151 | if (NoAliasTag) |
| 152 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 153 | |
| 154 | return CI; |
| 155 | } |
| 156 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 157 | CallInst *IRBuilderBase:: |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 158 | CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, |
| 159 | Value *Size, bool isVolatile, MDNode *TBAATag, |
| 160 | MDNode *TBAAStructTag, MDNode *ScopeTag, MDNode *NoAliasTag) { |
| 161 | assert((DstAlign == 0 || isPowerOf2_32(DstAlign)) && "Must be 0 or a power of 2"); |
| 162 | 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] | 163 | Dst = getCastedInt8PtrValue(Dst); |
| 164 | Src = getCastedInt8PtrValue(Src); |
| 165 | |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 166 | Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)}; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 167 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 168 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 169 | Function *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 170 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 171 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 172 | |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 173 | auto* MCI = cast<MemCpyInst>(CI); |
| 174 | if (DstAlign > 0) |
| 175 | MCI->setDestAlignment(DstAlign); |
| 176 | if (SrcAlign > 0) |
| 177 | MCI->setSourceAlignment(SrcAlign); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 179 | // Set the TBAA info if present. |
| 180 | if (TBAATag) |
| 181 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Dan Gohman | 099727f | 2012-09-26 22:17:14 +0000 | [diff] [blame] | 182 | |
| 183 | // Set the TBAA Struct info if present. |
| 184 | if (TBAAStructTag) |
| 185 | CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 186 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 187 | if (ScopeTag) |
| 188 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 189 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 190 | if (NoAliasTag) |
| 191 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 192 | |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 193 | return CI; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 196 | CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy( |
Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 197 | Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, |
| 198 | uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag, |
| 199 | MDNode *ScopeTag, MDNode *NoAliasTag) { |
| 200 | assert(DstAlign >= ElementSize && |
| 201 | "Pointer alignment must be at least element size"); |
| 202 | assert(SrcAlign >= ElementSize && |
| 203 | "Pointer alignment must be at least element size"); |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 204 | Dst = getCastedInt8PtrValue(Dst); |
| 205 | Src = getCastedInt8PtrValue(Src); |
| 206 | |
Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 207 | Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)}; |
| 208 | Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()}; |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 209 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 210 | Function *TheFn = Intrinsic::getDeclaration( |
Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 211 | M, Intrinsic::memcpy_element_unordered_atomic, Tys); |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 212 | |
| 213 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
| 214 | |
Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 215 | // Set the alignment of the pointer args. |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 216 | auto *AMCI = cast<AtomicMemCpyInst>(CI); |
| 217 | AMCI->setDestAlignment(DstAlign); |
| 218 | AMCI->setSourceAlignment(SrcAlign); |
Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 219 | |
Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 220 | // Set the TBAA info if present. |
| 221 | if (TBAATag) |
| 222 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 223 | |
| 224 | // Set the TBAA Struct info if present. |
| 225 | if (TBAAStructTag) |
| 226 | CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); |
| 227 | |
| 228 | if (ScopeTag) |
| 229 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 230 | |
| 231 | if (NoAliasTag) |
| 232 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 233 | |
| 234 | return CI; |
| 235 | } |
| 236 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 237 | CallInst *IRBuilderBase:: |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 238 | CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, |
| 239 | Value *Size, bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 240 | MDNode *NoAliasTag) { |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 241 | assert((DstAlign == 0 || isPowerOf2_32(DstAlign)) && "Must be 0 or a power of 2"); |
| 242 | 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] | 243 | Dst = getCastedInt8PtrValue(Dst); |
| 244 | Src = getCastedInt8PtrValue(Src); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 245 | |
| 246 | Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)}; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 247 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 248 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 249 | Function *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 250 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 251 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 252 | |
| 253 | auto *MMI = cast<MemMoveInst>(CI); |
Daniel Neilson | 551a4d6 | 2018-01-27 17:59:10 +0000 | [diff] [blame] | 254 | if (DstAlign > 0) |
| 255 | MMI->setDestAlignment(DstAlign); |
| 256 | if (SrcAlign > 0) |
| 257 | MMI->setSourceAlignment(SrcAlign); |
Daniel Neilson | 1e68724 | 2018-01-19 17:13:12 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 259 | // Set the TBAA info if present. |
| 260 | if (TBAATag) |
| 261 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 262 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 263 | if (ScopeTag) |
| 264 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 265 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 266 | if (NoAliasTag) |
| 267 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 268 | |
| 269 | return CI; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 270 | } |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 271 | |
Daniel Neilson | 936d50a | 2018-05-30 20:02:56 +0000 | [diff] [blame] | 272 | CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove( |
| 273 | Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, |
| 274 | uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag, |
| 275 | MDNode *ScopeTag, MDNode *NoAliasTag) { |
| 276 | assert(DstAlign >= ElementSize && |
| 277 | "Pointer alignment must be at least element size"); |
| 278 | assert(SrcAlign >= ElementSize && |
| 279 | "Pointer alignment must be at least element size"); |
| 280 | Dst = getCastedInt8PtrValue(Dst); |
| 281 | Src = getCastedInt8PtrValue(Src); |
| 282 | |
| 283 | Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)}; |
| 284 | Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()}; |
| 285 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 286 | Function *TheFn = Intrinsic::getDeclaration( |
Daniel Neilson | 936d50a | 2018-05-30 20:02:56 +0000 | [diff] [blame] | 287 | M, Intrinsic::memmove_element_unordered_atomic, Tys); |
| 288 | |
| 289 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
| 290 | |
| 291 | // Set the alignment of the pointer args. |
| 292 | CI->addParamAttr(0, Attribute::getWithAlignment(CI->getContext(), DstAlign)); |
| 293 | CI->addParamAttr(1, Attribute::getWithAlignment(CI->getContext(), SrcAlign)); |
| 294 | |
| 295 | // Set the TBAA info if present. |
| 296 | if (TBAATag) |
| 297 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 298 | |
| 299 | // Set the TBAA Struct info if present. |
| 300 | if (TBAAStructTag) |
| 301 | CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); |
| 302 | |
| 303 | if (ScopeTag) |
| 304 | CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); |
| 305 | |
| 306 | if (NoAliasTag) |
| 307 | CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); |
| 308 | |
| 309 | return CI; |
| 310 | } |
| 311 | |
Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 312 | static CallInst *getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID, |
| 313 | Value *Src) { |
| 314 | Module *M = Builder->GetInsertBlock()->getParent()->getParent(); |
| 315 | Value *Ops[] = {Src}; |
| 316 | Type *Tys[] = { Src->getType()->getVectorElementType(), Src->getType() }; |
| 317 | auto Decl = Intrinsic::getDeclaration(M, ID, Tys); |
| 318 | return createCallHelper(Decl, Ops, Builder); |
| 319 | } |
| 320 | |
| 321 | CallInst *IRBuilderBase::CreateFAddReduce(Value *Acc, Value *Src) { |
| 322 | Module *M = GetInsertBlock()->getParent()->getParent(); |
| 323 | Value *Ops[] = {Acc, Src}; |
| 324 | Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(), |
| 325 | Src->getType()}; |
| 326 | auto Decl = Intrinsic::getDeclaration( |
| 327 | M, Intrinsic::experimental_vector_reduce_fadd, Tys); |
| 328 | return createCallHelper(Decl, Ops, this); |
| 329 | } |
| 330 | |
| 331 | CallInst *IRBuilderBase::CreateFMulReduce(Value *Acc, Value *Src) { |
| 332 | Module *M = GetInsertBlock()->getParent()->getParent(); |
| 333 | Value *Ops[] = {Acc, Src}; |
| 334 | Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(), |
| 335 | Src->getType()}; |
| 336 | auto Decl = Intrinsic::getDeclaration( |
| 337 | M, Intrinsic::experimental_vector_reduce_fmul, Tys); |
| 338 | return createCallHelper(Decl, Ops, this); |
| 339 | } |
| 340 | |
| 341 | CallInst *IRBuilderBase::CreateAddReduce(Value *Src) { |
| 342 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add, |
| 343 | Src); |
| 344 | } |
| 345 | |
| 346 | CallInst *IRBuilderBase::CreateMulReduce(Value *Src) { |
| 347 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_mul, |
| 348 | Src); |
| 349 | } |
| 350 | |
| 351 | CallInst *IRBuilderBase::CreateAndReduce(Value *Src) { |
| 352 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_and, |
| 353 | Src); |
| 354 | } |
| 355 | |
| 356 | CallInst *IRBuilderBase::CreateOrReduce(Value *Src) { |
| 357 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_or, |
| 358 | Src); |
| 359 | } |
| 360 | |
| 361 | CallInst *IRBuilderBase::CreateXorReduce(Value *Src) { |
| 362 | return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_xor, |
| 363 | Src); |
| 364 | } |
| 365 | |
| 366 | CallInst *IRBuilderBase::CreateIntMaxReduce(Value *Src, bool IsSigned) { |
| 367 | auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smax |
| 368 | : Intrinsic::experimental_vector_reduce_umax; |
| 369 | return getReductionIntrinsic(this, ID, Src); |
| 370 | } |
| 371 | |
| 372 | CallInst *IRBuilderBase::CreateIntMinReduce(Value *Src, bool IsSigned) { |
| 373 | auto ID = IsSigned ? Intrinsic::experimental_vector_reduce_smin |
| 374 | : Intrinsic::experimental_vector_reduce_umin; |
| 375 | return getReductionIntrinsic(this, ID, Src); |
| 376 | } |
| 377 | |
| 378 | CallInst *IRBuilderBase::CreateFPMaxReduce(Value *Src, bool NoNaN) { |
| 379 | auto Rdx = getReductionIntrinsic( |
| 380 | this, Intrinsic::experimental_vector_reduce_fmax, Src); |
| 381 | if (NoNaN) { |
| 382 | FastMathFlags FMF; |
| 383 | FMF.setNoNaNs(); |
| 384 | Rdx->setFastMathFlags(FMF); |
| 385 | } |
| 386 | return Rdx; |
| 387 | } |
| 388 | |
| 389 | CallInst *IRBuilderBase::CreateFPMinReduce(Value *Src, bool NoNaN) { |
| 390 | auto Rdx = getReductionIntrinsic( |
| 391 | this, Intrinsic::experimental_vector_reduce_fmin, Src); |
| 392 | if (NoNaN) { |
| 393 | FastMathFlags FMF; |
| 394 | FMF.setNoNaNs(); |
| 395 | Rdx->setFastMathFlags(FMF); |
| 396 | } |
| 397 | return Rdx; |
| 398 | } |
| 399 | |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 400 | CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) { |
| 401 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 402 | "lifetime.start only applies to pointers."); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 403 | Ptr = getCastedInt8PtrValue(Ptr); |
| 404 | if (!Size) |
| 405 | Size = getInt64(-1); |
| 406 | else |
| 407 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 408 | "lifetime.start requires the size to be an i64"); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 409 | Value *Ops[] = { Size, Ptr }; |
| 410 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 411 | Function *TheFn = |
| 412 | Intrinsic::getDeclaration(M, Intrinsic::lifetime_start, {Ptr->getType()}); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 413 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { |
| 417 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 418 | "lifetime.end only applies to pointers."); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 419 | Ptr = getCastedInt8PtrValue(Ptr); |
| 420 | if (!Size) |
| 421 | Size = getInt64(-1); |
| 422 | else |
| 423 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 424 | "lifetime.end requires the size to be an i64"); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 425 | Value *Ops[] = { Size, Ptr }; |
| 426 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 427 | Function *TheFn = |
| 428 | Intrinsic::getDeclaration(M, Intrinsic::lifetime_end, {Ptr->getType()}); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 429 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 430 | } |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 431 | |
Anna Thomas | 58d1192 | 2016-07-22 20:57:23 +0000 | [diff] [blame] | 432 | CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) { |
| 433 | |
| 434 | assert(isa<PointerType>(Ptr->getType()) && |
| 435 | "invariant.start only applies to pointers."); |
| 436 | Ptr = getCastedInt8PtrValue(Ptr); |
| 437 | if (!Size) |
| 438 | Size = getInt64(-1); |
| 439 | else |
| 440 | assert(Size->getType() == getInt64Ty() && |
| 441 | "invariant.start requires the size to be an i64"); |
| 442 | |
| 443 | Value *Ops[] = {Size, Ptr}; |
| 444 | // Fill in the single overloaded type: memory object type. |
| 445 | Type *ObjectPtr[1] = {Ptr->getType()}; |
| 446 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 447 | Function *TheFn = |
Anna Thomas | 58d1192 | 2016-07-22 20:57:23 +0000 | [diff] [blame] | 448 | Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr); |
| 449 | return createCallHelper(TheFn, Ops, this); |
| 450 | } |
| 451 | |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 452 | CallInst *IRBuilderBase::CreateAssumption(Value *Cond) { |
| 453 | assert(Cond->getType() == getInt1Ty() && |
| 454 | "an assumption condition must be of type i1"); |
| 455 | |
| 456 | Value *Ops[] = { Cond }; |
| 457 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 458 | Function *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume); |
Hal Finkel | 6f814db | 2014-10-15 23:44:22 +0000 | [diff] [blame] | 459 | return createCallHelper(FnAssume, Ops, this); |
| 460 | } |
| 461 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 462 | /// Create a call to a Masked Load intrinsic. |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 463 | /// \p Ptr - base pointer for the load |
| 464 | /// \p Align - alignment of the source location |
| 465 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 466 | /// be accessed in memory |
| 467 | /// \p PassThru - pass-through value that is used to fill the masked-off lanes |
| 468 | /// of the result |
| 469 | /// \p Name - name of the result variable |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 470 | CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align, |
| 471 | Value *Mask, Value *PassThru, |
| 472 | const Twine &Name) { |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 473 | auto *PtrTy = cast<PointerType>(Ptr->getType()); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 474 | Type *DataTy = PtrTy->getElementType(); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 475 | assert(DataTy->isVectorTy() && "Ptr should point to a vector"); |
Ayal Zaks | e841b21 | 2017-07-31 13:21:42 +0000 | [diff] [blame] | 476 | assert(Mask && "Mask should not be all-ones (null)"); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 477 | if (!PassThru) |
| 478 | PassThru = UndefValue::get(DataTy); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 479 | Type *OverloadedTypes[] = { DataTy, PtrTy }; |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 480 | Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru}; |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 481 | return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, |
| 482 | OverloadedTypes, Name); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 485 | /// Create a call to a Masked Store intrinsic. |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 486 | /// \p Val - data to be stored, |
| 487 | /// \p Ptr - base pointer for the store |
| 488 | /// \p Align - alignment of the destination location |
| 489 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 490 | /// be accessed in memory |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 491 | CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr, |
| 492 | unsigned Align, Value *Mask) { |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 493 | auto *PtrTy = cast<PointerType>(Ptr->getType()); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 494 | Type *DataTy = PtrTy->getElementType(); |
| 495 | assert(DataTy->isVectorTy() && "Ptr should point to a vector"); |
Ayal Zaks | e841b21 | 2017-07-31 13:21:42 +0000 | [diff] [blame] | 496 | assert(Mask && "Mask should not be all-ones (null)"); |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 497 | Type *OverloadedTypes[] = { DataTy, PtrTy }; |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 498 | Value *Ops[] = { Val, Ptr, getInt32(Align), Mask }; |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 499 | return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, OverloadedTypes); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /// Create a call to a Masked intrinsic, with given intrinsic Id, |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 503 | /// an array of operands - Ops, and an array of overloaded types - |
| 504 | /// OverloadedTypes. |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 505 | CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id, |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 506 | ArrayRef<Value *> Ops, |
Artur Pilipenko | 7ad95ec | 2016-06-28 18:27:25 +0000 | [diff] [blame] | 507 | ArrayRef<Type *> OverloadedTypes, |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 508 | const Twine &Name) { |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 509 | Module *M = BB->getParent()->getParent(); |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 510 | Function *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes); |
Elena Demikhovsky | 84d1997 | 2014-12-30 14:28:14 +0000 | [diff] [blame] | 511 | return createCallHelper(TheFn, Ops, this, Name); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 512 | } |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 513 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 514 | /// Create a call to a Masked Gather intrinsic. |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 515 | /// \p Ptrs - vector of pointers for loading |
| 516 | /// \p Align - alignment for one element |
| 517 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 518 | /// be accessed in memory |
| 519 | /// \p PassThru - pass-through value that is used to fill the masked-off lanes |
| 520 | /// of the result |
| 521 | /// \p Name - name of the result variable |
| 522 | CallInst *IRBuilderBase::CreateMaskedGather(Value *Ptrs, unsigned Align, |
| 523 | Value *Mask, Value *PassThru, |
| 524 | const Twine& Name) { |
| 525 | auto PtrsTy = cast<VectorType>(Ptrs->getType()); |
| 526 | auto PtrTy = cast<PointerType>(PtrsTy->getElementType()); |
| 527 | unsigned NumElts = PtrsTy->getVectorNumElements(); |
| 528 | Type *DataTy = VectorType::get(PtrTy->getElementType(), NumElts); |
| 529 | |
| 530 | if (!Mask) |
| 531 | Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context), |
| 532 | NumElts)); |
| 533 | |
Amara Emerson | 4d33c86 | 2017-05-19 10:40:18 +0000 | [diff] [blame] | 534 | if (!PassThru) |
| 535 | PassThru = UndefValue::get(DataTy); |
| 536 | |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 537 | Type *OverloadedTypes[] = {DataTy, PtrsTy}; |
Amara Emerson | 4d33c86 | 2017-05-19 10:40:18 +0000 | [diff] [blame] | 538 | Value * Ops[] = {Ptrs, getInt32(Align), Mask, PassThru}; |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 539 | |
| 540 | // We specify only one type when we create this intrinsic. Types of other |
| 541 | // arguments are derived from this type. |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 542 | return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes, |
| 543 | Name); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 546 | /// Create a call to a Masked Scatter intrinsic. |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 547 | /// \p Data - data to be stored, |
| 548 | /// \p Ptrs - the vector of pointers, where the \p Data elements should be |
| 549 | /// stored |
| 550 | /// \p Align - alignment for one element |
| 551 | /// \p Mask - vector of booleans which indicates what vector lanes should |
| 552 | /// be accessed in memory |
| 553 | CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs, |
| 554 | unsigned Align, Value *Mask) { |
| 555 | auto PtrsTy = cast<VectorType>(Ptrs->getType()); |
| 556 | auto DataTy = cast<VectorType>(Data->getType()); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 557 | unsigned NumElts = PtrsTy->getVectorNumElements(); |
| 558 | |
Tim Northover | 5a1a56c | 2016-02-17 21:16:59 +0000 | [diff] [blame] | 559 | #ifndef NDEBUG |
| 560 | auto PtrTy = cast<PointerType>(PtrsTy->getElementType()); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 561 | assert(NumElts == DataTy->getVectorNumElements() && |
Tim Northover | 5a1a56c | 2016-02-17 21:16:59 +0000 | [diff] [blame] | 562 | PtrTy->getElementType() == DataTy->getElementType() && |
| 563 | "Incompatible pointer and data types"); |
| 564 | #endif |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 565 | |
| 566 | if (!Mask) |
| 567 | Mask = Constant::getAllOnesValue(VectorType::get(Type::getInt1Ty(Context), |
| 568 | NumElts)); |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 569 | |
| 570 | Type *OverloadedTypes[] = {DataTy, PtrsTy}; |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 571 | Value * Ops[] = {Data, Ptrs, getInt32(Align), Mask}; |
| 572 | |
| 573 | // We specify only one type when we create this intrinsic. Types of other |
| 574 | // arguments are derived from this type. |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 575 | return CreateMaskedIntrinsic(Intrinsic::masked_scatter, Ops, OverloadedTypes); |
Elena Demikhovsky | 88e76ca | 2016-02-17 19:23:04 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 578 | template <typename T0, typename T1, typename T2, typename T3> |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 579 | static std::vector<Value *> |
| 580 | getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 581 | Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, |
| 582 | ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, |
| 583 | ArrayRef<T3> GCArgs) { |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 584 | std::vector<Value *> Args; |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 585 | Args.push_back(B.getInt64(ID)); |
| 586 | Args.push_back(B.getInt32(NumPatchBytes)); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 587 | Args.push_back(ActualCallee); |
| 588 | Args.push_back(B.getInt32(CallArgs.size())); |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 589 | Args.push_back(B.getInt32(Flags)); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 590 | Args.insert(Args.end(), CallArgs.begin(), CallArgs.end()); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 591 | Args.push_back(B.getInt32(TransitionArgs.size())); |
| 592 | Args.insert(Args.end(), TransitionArgs.begin(), TransitionArgs.end()); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 593 | Args.push_back(B.getInt32(DeoptArgs.size())); |
| 594 | Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end()); |
| 595 | Args.insert(Args.end(), GCArgs.begin(), GCArgs.end()); |
| 596 | |
| 597 | return Args; |
| 598 | } |
| 599 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 600 | template <typename T0, typename T1, typename T2, typename T3> |
| 601 | static CallInst *CreateGCStatepointCallCommon( |
| 602 | IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 603 | Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 604 | ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, |
| 605 | const Twine &Name) { |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 606 | // Extract out the type of the callee. |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 607 | auto *FuncPtrType = cast<PointerType>(ActualCallee->getType()); |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 608 | assert(isa<FunctionType>(FuncPtrType->getElementType()) && |
| 609 | "actual callee must be a callable value"); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 610 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 611 | Module *M = Builder->GetInsertBlock()->getParent()->getParent(); |
Sanjoy Das | 63245b5 | 2015-05-06 02:36:34 +0000 | [diff] [blame] | 612 | // Fill in the one generic type'd argument (the function is also vararg) |
| 613 | Type *ArgTypes[] = { FuncPtrType }; |
| 614 | Function *FnStatepoint = |
| 615 | Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint, |
| 616 | ArgTypes); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 617 | |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 618 | std::vector<Value *> Args = |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 619 | getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags, |
| 620 | CallArgs, TransitionArgs, DeoptArgs, GCArgs); |
| 621 | return createCallHelper(FnStatepoint, Args, Builder, Name); |
| 622 | } |
| 623 | |
| 624 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
| 625 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, |
| 626 | ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs, |
| 627 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
| 628 | return CreateGCStatepointCallCommon<Value *, Value *, Value *, Value *>( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 629 | this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None), |
| 630 | CallArgs, None /* No Transition Args */, DeoptArgs, GCArgs, Name); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 634 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, uint32_t Flags, |
| 635 | ArrayRef<Use> CallArgs, ArrayRef<Use> TransitionArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 636 | ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) { |
| 637 | return CreateGCStatepointCallCommon<Use, Use, Use, Value *>( |
| 638 | this, ID, NumPatchBytes, ActualCallee, Flags, CallArgs, TransitionArgs, |
| 639 | DeoptArgs, GCArgs, Name); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 642 | CallInst *IRBuilderBase::CreateGCStatepointCall( |
| 643 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee, |
| 644 | ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs, |
| 645 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 646 | return CreateGCStatepointCallCommon<Use, Value *, Value *, Value *>( |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 647 | this, ID, NumPatchBytes, ActualCallee, uint32_t(StatepointFlags::None), |
| 648 | CallArgs, None, DeoptArgs, GCArgs, Name); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | template <typename T0, typename T1, typename T2, typename T3> |
| 652 | static InvokeInst *CreateGCStatepointInvokeCommon( |
| 653 | IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, |
| 654 | Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 655 | uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 656 | ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) { |
| 657 | // Extract out the type of the callee. |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 658 | auto *FuncPtrType = cast<PointerType>(ActualInvokee->getType()); |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 659 | assert(isa<FunctionType>(FuncPtrType->getElementType()) && |
| 660 | "actual callee must be a callable value"); |
| 661 | |
| 662 | Module *M = Builder->GetInsertBlock()->getParent()->getParent(); |
| 663 | // Fill in the one generic type'd argument (the function is also vararg) |
| 664 | Function *FnStatepoint = Intrinsic::getDeclaration( |
| 665 | M, Intrinsic::experimental_gc_statepoint, {FuncPtrType}); |
| 666 | |
Eugene Zelenko | 7fb5d41 | 2018-03-30 00:47:31 +0000 | [diff] [blame] | 667 | std::vector<Value *> Args = |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 668 | getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags, |
| 669 | InvokeArgs, TransitionArgs, DeoptArgs, GCArgs); |
| 670 | return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder, |
| 671 | Name); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 675 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
| 676 | BasicBlock *NormalDest, BasicBlock *UnwindDest, |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 677 | ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs, |
| 678 | ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 679 | return CreateGCStatepointInvokeCommon<Value *, Value *, Value *, Value *>( |
| 680 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 681 | uint32_t(StatepointFlags::None), InvokeArgs, None /* No Transition Args*/, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 682 | DeoptArgs, GCArgs, Name); |
| 683 | } |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 684 | |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 685 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
| 686 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 687 | BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 688 | ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs, |
| 689 | ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) { |
| 690 | return CreateGCStatepointInvokeCommon<Use, Use, Use, Value *>( |
| 691 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, Flags, |
| 692 | InvokeArgs, TransitionArgs, DeoptArgs, GCArgs, Name); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | InvokeInst *IRBuilderBase::CreateGCStatepointInvoke( |
Sanjoy Das | a1d39ba | 2015-05-12 23:52:24 +0000 | [diff] [blame] | 696 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
| 697 | BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, |
| 698 | ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) { |
Sanjoy Das | af6980c | 2015-10-07 19:52:12 +0000 | [diff] [blame] | 699 | return CreateGCStatepointInvokeCommon<Use, Value *, Value *, Value *>( |
| 700 | this, ID, NumPatchBytes, ActualInvokee, NormalDest, UnwindDest, |
Sanjoy Das | 4fd3d40 | 2015-10-08 23:18:33 +0000 | [diff] [blame] | 701 | uint32_t(StatepointFlags::None), InvokeArgs, None, DeoptArgs, GCArgs, |
| 702 | Name); |
Ramkumar Ramachandra | 3408f3e | 2015-02-26 00:35:56 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 705 | CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint, |
| 706 | Type *ResultType, |
| 707 | const Twine &Name) { |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 708 | Intrinsic::ID ID = Intrinsic::experimental_gc_result; |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 709 | Module *M = BB->getParent()->getParent(); |
| 710 | Type *Types[] = {ResultType}; |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 711 | Function *FnGCResult = Intrinsic::getDeclaration(M, ID, Types); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 712 | |
| 713 | Value *Args[] = {Statepoint}; |
| 714 | return createCallHelper(FnGCResult, Args, this, Name); |
| 715 | } |
| 716 | |
| 717 | CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint, |
| 718 | int BaseOffset, |
| 719 | int DerivedOffset, |
| 720 | Type *ResultType, |
| 721 | const Twine &Name) { |
| 722 | Module *M = BB->getParent()->getParent(); |
| 723 | Type *Types[] = {ResultType}; |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 724 | Function *FnGCRelocate = |
| 725 | Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types); |
Philip Reames | 4750dd1 | 2014-12-30 05:55:58 +0000 | [diff] [blame] | 726 | |
| 727 | Value *Args[] = {Statepoint, |
| 728 | getInt32(BaseOffset), |
| 729 | getInt32(DerivedOffset)}; |
| 730 | return createCallHelper(FnGCRelocate, Args, this, Name); |
| 731 | } |
Matt Arsenault | cdb468c | 2017-02-27 23:08:49 +0000 | [diff] [blame] | 732 | |
Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 733 | CallInst *IRBuilderBase::CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, |
| 734 | Instruction *FMFSource, |
| 735 | const Twine &Name) { |
| 736 | Module *M = BB->getModule(); |
| 737 | Function *Fn = Intrinsic::getDeclaration(M, ID, {V->getType()}); |
| 738 | return createCallHelper(Fn, {V}, this, Name, FMFSource); |
| 739 | } |
| 740 | |
| 741 | CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, |
| 742 | Value *RHS, |
| 743 | Instruction *FMFSource, |
Matt Arsenault | cdb468c | 2017-02-27 23:08:49 +0000 | [diff] [blame] | 744 | const Twine &Name) { |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 745 | Module *M = BB->getModule(); |
| 746 | Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() }); |
Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 747 | return createCallHelper(Fn, {LHS, RHS}, this, Name, FMFSource); |
Matt Arsenault | cdb468c | 2017-02-27 23:08:49 +0000 | [diff] [blame] | 748 | } |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 749 | |
| 750 | CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID, |
Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 751 | ArrayRef<Type *> Types, |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 752 | ArrayRef<Value *> Args, |
| 753 | Instruction *FMFSource, |
| 754 | const Twine &Name) { |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 755 | Module *M = BB->getModule(); |
Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 756 | Function *Fn = Intrinsic::getDeclaration(M, ID, Types); |
Sanjay Patel | d32104e | 2018-02-23 21:16:12 +0000 | [diff] [blame] | 757 | return createCallHelper(Fn, Args, this, Name, FMFSource); |
| 758 | } |