comparison lib/IR/IRBuilder.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===// 1 //===- IRBuilder.cpp - Builder for LLVM Instrs ----------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file implements the IRBuilder class, which is used as a convenient way 9 // This file implements the IRBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface. 10 // to create LLVM instructions with a consistent and simplified interface.
12 // 11 //
13 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
14 13
15 #include "llvm/IR/IRBuilder.h" 14 #include "llvm/IR/IRBuilder.h"
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"
16 #include "llvm/IR/Function.h" 20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/IR/GlobalVariable.h" 22 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/IntrinsicInst.h" 23 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Intrinsics.h" 24 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Operator.h"
21 #include "llvm/IR/Statepoint.h" 27 #include "llvm/IR/Statepoint.h"
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
22 using namespace llvm; 36 using namespace llvm;
23 37
24 /// CreateGlobalString - Make a new global variable with an initializer that 38 /// CreateGlobalString - Make a new global variable with an initializer that
25 /// has array of i8 type filled in with the nul terminated string value 39 /// has array of i8 type filled in with the nul terminated string value
26 /// specified. If Name is specified, it is the name of the global variable 40 /// specified. If Name is specified, it is the name of the global variable
28 GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str, 42 GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
29 const Twine &Name, 43 const Twine &Name,
30 unsigned AddressSpace) { 44 unsigned AddressSpace) {
31 Constant *StrConstant = ConstantDataArray::getString(Context, Str); 45 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
32 Module &M = *BB->getParent()->getParent(); 46 Module &M = *BB->getParent()->getParent();
33 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(), 47 auto *GV = new GlobalVariable(M, StrConstant->getType(), true,
34 true, GlobalValue::PrivateLinkage, 48 GlobalValue::PrivateLinkage, StrConstant, Name,
35 StrConstant, Name, nullptr, 49 nullptr, GlobalVariable::NotThreadLocal,
36 GlobalVariable::NotThreadLocal, 50 AddressSpace);
37 AddressSpace);
38 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); 51 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
52 GV->setAlignment(1);
39 return GV; 53 return GV;
40 } 54 }
41 55
42 Type *IRBuilderBase::getCurrentFunctionReturnType() const { 56 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
43 assert(BB && BB->getParent() && "No current function!"); 57 assert(BB && BB->getParent() && "No current function!");
44 return BB->getParent()->getReturnType(); 58 return BB->getParent()->getReturnType();
45 } 59 }
46 60
47 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { 61 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
48 PointerType *PT = cast<PointerType>(Ptr->getType()); 62 auto *PT = cast<PointerType>(Ptr->getType());
49 if (PT->getElementType()->isIntegerTy(8)) 63 if (PT->getElementType()->isIntegerTy(8))
50 return Ptr; 64 return Ptr;
51 65
52 // Otherwise, we need to insert a bitcast. 66 // Otherwise, we need to insert a bitcast.
53 PT = getInt8PtrTy(PT->getAddressSpace()); 67 PT = getInt8PtrTy(PT->getAddressSpace());
54 BitCastInst *BCI = new BitCastInst(Ptr, PT, ""); 68 BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
55 BB->getInstList().insert(InsertPt, BCI); 69 BB->getInstList().insert(InsertPt, BCI);
56 SetInstDebugLocation(BCI); 70 SetInstDebugLocation(BCI);
57 return BCI; 71 return BCI;
58 } 72 }
59 73
60 static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops, 74 static CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
61 IRBuilderBase *Builder, 75 IRBuilderBase *Builder,
62 const Twine& Name="") { 76 const Twine &Name = "",
77 Instruction *FMFSource = nullptr) {
63 CallInst *CI = CallInst::Create(Callee, Ops, Name); 78 CallInst *CI = CallInst::Create(Callee, Ops, Name);
79 if (FMFSource)
80 CI->copyFastMathFlags(FMFSource);
64 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI); 81 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
65 Builder->SetInstDebugLocation(CI); 82 Builder->SetInstDebugLocation(CI);
66 return CI; 83 return CI;
67 } 84 }
68 85
69 static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest, 86 static InvokeInst *createInvokeHelper(Function *Invokee, BasicBlock *NormalDest,
70 BasicBlock *UnwindDest, 87 BasicBlock *UnwindDest,
71 ArrayRef<Value *> Ops, 88 ArrayRef<Value *> Ops,
72 IRBuilderBase *Builder, 89 IRBuilderBase *Builder,
73 const Twine &Name = "") { 90 const Twine &Name = "") {
74 InvokeInst *II = 91 InvokeInst *II =
85 MDNode *NoAliasTag) { 102 MDNode *NoAliasTag) {
86 Ptr = getCastedInt8PtrValue(Ptr); 103 Ptr = getCastedInt8PtrValue(Ptr);
87 Value *Ops[] = {Ptr, Val, Size, getInt1(isVolatile)}; 104 Value *Ops[] = {Ptr, Val, Size, getInt1(isVolatile)};
88 Type *Tys[] = { Ptr->getType(), Size->getType() }; 105 Type *Tys[] = { Ptr->getType(), Size->getType() };
89 Module *M = BB->getParent()->getParent(); 106 Module *M = BB->getParent()->getParent();
90 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); 107 Function *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
91 108
92 CallInst *CI = createCallHelper(TheFn, Ops, this); 109 CallInst *CI = createCallHelper(TheFn, Ops, this);
93 110
94 if (Align > 0) 111 if (Align > 0)
95 cast<MemSetInst>(CI)->setDestAlignment(Align); 112 cast<MemSetInst>(CI)->setDestAlignment(Align);
96 113
98 if (TBAATag) 115 if (TBAATag)
99 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); 116 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
100 117
101 if (ScopeTag) 118 if (ScopeTag)
102 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); 119 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
103 120
121 if (NoAliasTag)
122 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
123
124 return CI;
125 }
126
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();
137 Function *TheFn = Intrinsic::getDeclaration(
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
104 if (NoAliasTag) 151 if (NoAliasTag)
105 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); 152 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
106 153
107 return CI; 154 return CI;
108 } 155 }
117 Src = getCastedInt8PtrValue(Src); 164 Src = getCastedInt8PtrValue(Src);
118 165
119 Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)}; 166 Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)};
120 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; 167 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
121 Module *M = BB->getParent()->getParent(); 168 Module *M = BB->getParent()->getParent();
122 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); 169 Function *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
123 170
124 CallInst *CI = createCallHelper(TheFn, Ops, this); 171 CallInst *CI = createCallHelper(TheFn, Ops, this);
125 172
126 auto* MCI = cast<MemCpyInst>(CI); 173 auto* MCI = cast<MemCpyInst>(CI);
127 if (DstAlign > 0) 174 if (DstAlign > 0)
128 MCI->setDestAlignment(DstAlign); 175 MCI->setDestAlignment(DstAlign);
134 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); 181 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
135 182
136 // Set the TBAA Struct info if present. 183 // Set the TBAA Struct info if present.
137 if (TBAAStructTag) 184 if (TBAAStructTag)
138 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); 185 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
139 186
140 if (ScopeTag) 187 if (ScopeTag)
141 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); 188 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
142 189
143 if (NoAliasTag) 190 if (NoAliasTag)
144 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); 191 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
145 192
146 return CI; 193 return CI;
147 } 194 }
148 195
149 CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy( 196 CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy(
150 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, 197 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
151 uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag, 198 uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag,
158 Src = getCastedInt8PtrValue(Src); 205 Src = getCastedInt8PtrValue(Src);
159 206
160 Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)}; 207 Value *Ops[] = {Dst, Src, Size, getInt32(ElementSize)};
161 Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()}; 208 Type *Tys[] = {Dst->getType(), Src->getType(), Size->getType()};
162 Module *M = BB->getParent()->getParent(); 209 Module *M = BB->getParent()->getParent();
163 Value *TheFn = Intrinsic::getDeclaration( 210 Function *TheFn = Intrinsic::getDeclaration(
164 M, Intrinsic::memcpy_element_unordered_atomic, Tys); 211 M, Intrinsic::memcpy_element_unordered_atomic, Tys);
165 212
166 CallInst *CI = createCallHelper(TheFn, Ops, this); 213 CallInst *CI = createCallHelper(TheFn, Ops, this);
167 214
168 // Set the alignment of the pointer args. 215 // Set the alignment of the pointer args.
197 Src = getCastedInt8PtrValue(Src); 244 Src = getCastedInt8PtrValue(Src);
198 245
199 Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)}; 246 Value *Ops[] = {Dst, Src, Size, getInt1(isVolatile)};
200 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; 247 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
201 Module *M = BB->getParent()->getParent(); 248 Module *M = BB->getParent()->getParent();
202 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys); 249 Function *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
203 250
204 CallInst *CI = createCallHelper(TheFn, Ops, this); 251 CallInst *CI = createCallHelper(TheFn, Ops, this);
205 252
206 auto *MMI = cast<MemMoveInst>(CI); 253 auto *MMI = cast<MemMoveInst>(CI);
207 if (DstAlign > 0) 254 if (DstAlign > 0)
208 MMI->setDestAlignment(DstAlign); 255 MMI->setDestAlignment(DstAlign);
210 MMI->setSourceAlignment(SrcAlign); 257 MMI->setSourceAlignment(SrcAlign);
211 258
212 // Set the TBAA info if present. 259 // Set the TBAA info if present.
213 if (TBAATag) 260 if (TBAATag)
214 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); 261 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
215 262
216 if (ScopeTag) 263 if (ScopeTag)
217 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); 264 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
218 265
219 if (NoAliasTag) 266 if (NoAliasTag)
220 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); 267 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
221 268
222 return CI; 269 return CI;
270 }
271
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();
286 Function *TheFn = Intrinsic::getDeclaration(
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;
223 } 310 }
224 311
225 static CallInst *getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID, 312 static CallInst *getReductionIntrinsic(IRBuilderBase *Builder, Intrinsic::ID ID,
226 Value *Src) { 313 Value *Src) {
227 Module *M = Builder->GetInsertBlock()->getParent()->getParent(); 314 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
228 Value *Ops[] = {Src}; 315 Value *Ops[] = {Src};
229 Type *Tys[] = { Src->getType()->getVectorElementType(), Src->getType() }; 316 Type *Tys[] = { Src->getType() };
230 auto Decl = Intrinsic::getDeclaration(M, ID, Tys); 317 auto Decl = Intrinsic::getDeclaration(M, ID, Tys);
231 return createCallHelper(Decl, Ops, Builder); 318 return createCallHelper(Decl, Ops, Builder);
232 } 319 }
233 320
234 CallInst *IRBuilderBase::CreateFAddReduce(Value *Acc, Value *Src) { 321 CallInst *IRBuilderBase::CreateFAddReduce(Value *Acc, Value *Src) {
235 Module *M = GetInsertBlock()->getParent()->getParent(); 322 Module *M = GetInsertBlock()->getParent()->getParent();
236 Value *Ops[] = {Acc, Src}; 323 Value *Ops[] = {Acc, Src};
237 Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(), 324 Type *Tys[] = {Acc->getType(), Src->getType()};
238 Src->getType()};
239 auto Decl = Intrinsic::getDeclaration( 325 auto Decl = Intrinsic::getDeclaration(
240 M, Intrinsic::experimental_vector_reduce_fadd, Tys); 326 M, Intrinsic::experimental_vector_reduce_v2_fadd, Tys);
241 return createCallHelper(Decl, Ops, this); 327 return createCallHelper(Decl, Ops, this);
242 } 328 }
243 329
244 CallInst *IRBuilderBase::CreateFMulReduce(Value *Acc, Value *Src) { 330 CallInst *IRBuilderBase::CreateFMulReduce(Value *Acc, Value *Src) {
245 Module *M = GetInsertBlock()->getParent()->getParent(); 331 Module *M = GetInsertBlock()->getParent()->getParent();
246 Value *Ops[] = {Acc, Src}; 332 Value *Ops[] = {Acc, Src};
247 Type *Tys[] = {Src->getType()->getVectorElementType(), Acc->getType(), 333 Type *Tys[] = {Acc->getType(), Src->getType()};
248 Src->getType()};
249 auto Decl = Intrinsic::getDeclaration( 334 auto Decl = Intrinsic::getDeclaration(
250 M, Intrinsic::experimental_vector_reduce_fmul, Tys); 335 M, Intrinsic::experimental_vector_reduce_v2_fmul, Tys);
251 return createCallHelper(Decl, Ops, this); 336 return createCallHelper(Decl, Ops, this);
252 } 337 }
253 338
254 CallInst *IRBuilderBase::CreateAddReduce(Value *Src) { 339 CallInst *IRBuilderBase::CreateAddReduce(Value *Src) {
255 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add, 340 return getReductionIntrinsic(this, Intrinsic::experimental_vector_reduce_add,
319 else 404 else
320 assert(Size->getType() == getInt64Ty() && 405 assert(Size->getType() == getInt64Ty() &&
321 "lifetime.start requires the size to be an i64"); 406 "lifetime.start requires the size to be an i64");
322 Value *Ops[] = { Size, Ptr }; 407 Value *Ops[] = { Size, Ptr };
323 Module *M = BB->getParent()->getParent(); 408 Module *M = BB->getParent()->getParent();
324 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start, 409 Function *TheFn =
325 { Ptr->getType() }); 410 Intrinsic::getDeclaration(M, Intrinsic::lifetime_start, {Ptr->getType()});
326 return createCallHelper(TheFn, Ops, this); 411 return createCallHelper(TheFn, Ops, this);
327 } 412 }
328 413
329 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { 414 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
330 assert(isa<PointerType>(Ptr->getType()) && 415 assert(isa<PointerType>(Ptr->getType()) &&
335 else 420 else
336 assert(Size->getType() == getInt64Ty() && 421 assert(Size->getType() == getInt64Ty() &&
337 "lifetime.end requires the size to be an i64"); 422 "lifetime.end requires the size to be an i64");
338 Value *Ops[] = { Size, Ptr }; 423 Value *Ops[] = { Size, Ptr };
339 Module *M = BB->getParent()->getParent(); 424 Module *M = BB->getParent()->getParent();
340 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end, 425 Function *TheFn =
341 { Ptr->getType() }); 426 Intrinsic::getDeclaration(M, Intrinsic::lifetime_end, {Ptr->getType()});
342 return createCallHelper(TheFn, Ops, this); 427 return createCallHelper(TheFn, Ops, this);
343 } 428 }
344 429
345 CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) { 430 CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) {
346 431
355 440
356 Value *Ops[] = {Size, Ptr}; 441 Value *Ops[] = {Size, Ptr};
357 // Fill in the single overloaded type: memory object type. 442 // Fill in the single overloaded type: memory object type.
358 Type *ObjectPtr[1] = {Ptr->getType()}; 443 Type *ObjectPtr[1] = {Ptr->getType()};
359 Module *M = BB->getParent()->getParent(); 444 Module *M = BB->getParent()->getParent();
360 Value *TheFn = 445 Function *TheFn =
361 Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr); 446 Intrinsic::getDeclaration(M, Intrinsic::invariant_start, ObjectPtr);
362 return createCallHelper(TheFn, Ops, this); 447 return createCallHelper(TheFn, Ops, this);
363 } 448 }
364 449
365 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) { 450 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
366 assert(Cond->getType() == getInt1Ty() && 451 assert(Cond->getType() == getInt1Ty() &&
367 "an assumption condition must be of type i1"); 452 "an assumption condition must be of type i1");
368 453
369 Value *Ops[] = { Cond }; 454 Value *Ops[] = { Cond };
370 Module *M = BB->getParent()->getParent(); 455 Module *M = BB->getParent()->getParent();
371 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume); 456 Function *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
372 return createCallHelper(FnAssume, Ops, this); 457 return createCallHelper(FnAssume, Ops, this);
373 } 458 }
374 459
375 /// \brief Create a call to a Masked Load intrinsic. 460 /// Create a call to a Masked Load intrinsic.
376 /// \p Ptr - base pointer for the load 461 /// \p Ptr - base pointer for the load
377 /// \p Align - alignment of the source location 462 /// \p Align - alignment of the source location
378 /// \p Mask - vector of booleans which indicates what vector lanes should 463 /// \p Mask - vector of booleans which indicates what vector lanes should
379 /// be accessed in memory 464 /// be accessed in memory
380 /// \p PassThru - pass-through value that is used to fill the masked-off lanes 465 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
381 /// of the result 466 /// of the result
382 /// \p Name - name of the result variable 467 /// \p Name - name of the result variable
383 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align, 468 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
384 Value *Mask, Value *PassThru, 469 Value *Mask, Value *PassThru,
385 const Twine &Name) { 470 const Twine &Name) {
386 PointerType *PtrTy = cast<PointerType>(Ptr->getType()); 471 auto *PtrTy = cast<PointerType>(Ptr->getType());
387 Type *DataTy = PtrTy->getElementType(); 472 Type *DataTy = PtrTy->getElementType();
388 assert(DataTy->isVectorTy() && "Ptr should point to a vector"); 473 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
389 assert(Mask && "Mask should not be all-ones (null)"); 474 assert(Mask && "Mask should not be all-ones (null)");
390 if (!PassThru) 475 if (!PassThru)
391 PassThru = UndefValue::get(DataTy); 476 PassThru = UndefValue::get(DataTy);
393 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru}; 478 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
394 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, 479 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops,
395 OverloadedTypes, Name); 480 OverloadedTypes, Name);
396 } 481 }
397 482
398 /// \brief Create a call to a Masked Store intrinsic. 483 /// Create a call to a Masked Store intrinsic.
399 /// \p Val - data to be stored, 484 /// \p Val - data to be stored,
400 /// \p Ptr - base pointer for the store 485 /// \p Ptr - base pointer for the store
401 /// \p Align - alignment of the destination location 486 /// \p Align - alignment of the destination location
402 /// \p Mask - vector of booleans which indicates what vector lanes should 487 /// \p Mask - vector of booleans which indicates what vector lanes should
403 /// be accessed in memory 488 /// be accessed in memory
404 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr, 489 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
405 unsigned Align, Value *Mask) { 490 unsigned Align, Value *Mask) {
406 PointerType *PtrTy = cast<PointerType>(Ptr->getType()); 491 auto *PtrTy = cast<PointerType>(Ptr->getType());
407 Type *DataTy = PtrTy->getElementType(); 492 Type *DataTy = PtrTy->getElementType();
408 assert(DataTy->isVectorTy() && "Ptr should point to a vector"); 493 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
409 assert(Mask && "Mask should not be all-ones (null)"); 494 assert(Mask && "Mask should not be all-ones (null)");
410 Type *OverloadedTypes[] = { DataTy, PtrTy }; 495 Type *OverloadedTypes[] = { DataTy, PtrTy };
411 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask }; 496 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
418 CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id, 503 CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
419 ArrayRef<Value *> Ops, 504 ArrayRef<Value *> Ops,
420 ArrayRef<Type *> OverloadedTypes, 505 ArrayRef<Type *> OverloadedTypes,
421 const Twine &Name) { 506 const Twine &Name) {
422 Module *M = BB->getParent()->getParent(); 507 Module *M = BB->getParent()->getParent();
423 Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes); 508 Function *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
424 return createCallHelper(TheFn, Ops, this, Name); 509 return createCallHelper(TheFn, Ops, this, Name);
425 } 510 }
426 511
427 /// \brief Create a call to a Masked Gather intrinsic. 512 /// Create a call to a Masked Gather intrinsic.
428 /// \p Ptrs - vector of pointers for loading 513 /// \p Ptrs - vector of pointers for loading
429 /// \p Align - alignment for one element 514 /// \p Align - alignment for one element
430 /// \p Mask - vector of booleans which indicates what vector lanes should 515 /// \p Mask - vector of booleans which indicates what vector lanes should
431 /// be accessed in memory 516 /// be accessed in memory
432 /// \p PassThru - pass-through value that is used to fill the masked-off lanes 517 /// \p PassThru - pass-through value that is used to fill the masked-off lanes
454 // arguments are derived from this type. 539 // arguments are derived from this type.
455 return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes, 540 return CreateMaskedIntrinsic(Intrinsic::masked_gather, Ops, OverloadedTypes,
456 Name); 541 Name);
457 } 542 }
458 543
459 /// \brief Create a call to a Masked Scatter intrinsic. 544 /// Create a call to a Masked Scatter intrinsic.
460 /// \p Data - data to be stored, 545 /// \p Data - data to be stored,
461 /// \p Ptrs - the vector of pointers, where the \p Data elements should be 546 /// \p Ptrs - the vector of pointers, where the \p Data elements should be
462 /// stored 547 /// stored
463 /// \p Align - alignment for one element 548 /// \p Align - alignment for one element
464 /// \p Mask - vector of booleans which indicates what vector lanes should 549 /// \p Mask - vector of booleans which indicates what vector lanes should
515 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, 600 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
516 Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs, 601 Value *ActualCallee, uint32_t Flags, ArrayRef<T0> CallArgs,
517 ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, 602 ArrayRef<T1> TransitionArgs, ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs,
518 const Twine &Name) { 603 const Twine &Name) {
519 // Extract out the type of the callee. 604 // Extract out the type of the callee.
520 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType()); 605 auto *FuncPtrType = cast<PointerType>(ActualCallee->getType());
521 assert(isa<FunctionType>(FuncPtrType->getElementType()) && 606 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
522 "actual callee must be a callable value"); 607 "actual callee must be a callable value");
523 608
524 Module *M = Builder->GetInsertBlock()->getParent()->getParent(); 609 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
525 // Fill in the one generic type'd argument (the function is also vararg) 610 // Fill in the one generic type'd argument (the function is also vararg)
526 Type *ArgTypes[] = { FuncPtrType }; 611 Type *ArgTypes[] = { FuncPtrType };
527 Function *FnStatepoint = 612 Function *FnStatepoint =
528 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint, 613 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
529 ArgTypes); 614 ArgTypes);
530 615
531 std::vector<llvm::Value *> Args = 616 std::vector<Value *> Args =
532 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags, 617 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualCallee, Flags,
533 CallArgs, TransitionArgs, DeoptArgs, GCArgs); 618 CallArgs, TransitionArgs, DeoptArgs, GCArgs);
534 return createCallHelper(FnStatepoint, Args, Builder, Name); 619 return createCallHelper(FnStatepoint, Args, Builder, Name);
535 } 620 }
536 621
566 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes, 651 IRBuilderBase *Builder, uint64_t ID, uint32_t NumPatchBytes,
567 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest, 652 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
568 uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs, 653 uint32_t Flags, ArrayRef<T0> InvokeArgs, ArrayRef<T1> TransitionArgs,
569 ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) { 654 ArrayRef<T2> DeoptArgs, ArrayRef<T3> GCArgs, const Twine &Name) {
570 // Extract out the type of the callee. 655 // Extract out the type of the callee.
571 PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType()); 656 auto *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
572 assert(isa<FunctionType>(FuncPtrType->getElementType()) && 657 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
573 "actual callee must be a callable value"); 658 "actual callee must be a callable value");
574 659
575 Module *M = Builder->GetInsertBlock()->getParent()->getParent(); 660 Module *M = Builder->GetInsertBlock()->getParent()->getParent();
576 // Fill in the one generic type'd argument (the function is also vararg) 661 // Fill in the one generic type'd argument (the function is also vararg)
577 Function *FnStatepoint = Intrinsic::getDeclaration( 662 Function *FnStatepoint = Intrinsic::getDeclaration(
578 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType}); 663 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
579 664
580 std::vector<llvm::Value *> Args = 665 std::vector<Value *> Args =
581 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags, 666 getStatepointArgs(*Builder, ID, NumPatchBytes, ActualInvokee, Flags,
582 InvokeArgs, TransitionArgs, DeoptArgs, GCArgs); 667 InvokeArgs, TransitionArgs, DeoptArgs, GCArgs);
583 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder, 668 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, Builder,
584 Name); 669 Name);
585 } 670 }
619 Type *ResultType, 704 Type *ResultType,
620 const Twine &Name) { 705 const Twine &Name) {
621 Intrinsic::ID ID = Intrinsic::experimental_gc_result; 706 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
622 Module *M = BB->getParent()->getParent(); 707 Module *M = BB->getParent()->getParent();
623 Type *Types[] = {ResultType}; 708 Type *Types[] = {ResultType};
624 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types); 709 Function *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
625 710
626 Value *Args[] = {Statepoint}; 711 Value *Args[] = {Statepoint};
627 return createCallHelper(FnGCResult, Args, this, Name); 712 return createCallHelper(FnGCResult, Args, this, Name);
628 } 713 }
629 714
632 int DerivedOffset, 717 int DerivedOffset,
633 Type *ResultType, 718 Type *ResultType,
634 const Twine &Name) { 719 const Twine &Name) {
635 Module *M = BB->getParent()->getParent(); 720 Module *M = BB->getParent()->getParent();
636 Type *Types[] = {ResultType}; 721 Type *Types[] = {ResultType};
637 Value *FnGCRelocate = 722 Function *FnGCRelocate =
638 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types); 723 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
639 724
640 Value *Args[] = {Statepoint, 725 Value *Args[] = {Statepoint,
641 getInt32(BaseOffset), 726 getInt32(BaseOffset),
642 getInt32(DerivedOffset)}; 727 getInt32(DerivedOffset)};
643 return createCallHelper(FnGCRelocate, Args, this, Name); 728 return createCallHelper(FnGCRelocate, Args, this, Name);
644 } 729 }
645 730
646 CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID, 731 CallInst *IRBuilderBase::CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
647 Value *LHS, Value *RHS, 732 Instruction *FMFSource,
733 const Twine &Name) {
734 Module *M = BB->getModule();
735 Function *Fn = Intrinsic::getDeclaration(M, ID, {V->getType()});
736 return createCallHelper(Fn, {V}, this, Name, FMFSource);
737 }
738
739 CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS,
740 Value *RHS,
741 Instruction *FMFSource,
648 const Twine &Name) { 742 const Twine &Name) {
649 Module *M = BB->getParent()->getParent(); 743 Module *M = BB->getModule();
650 Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() }); 744 Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() });
651 return createCallHelper(Fn, { LHS, RHS }, this, Name); 745 return createCallHelper(Fn, {LHS, RHS}, this, Name, FMFSource);
652 } 746 }
747
748 CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID,
749 ArrayRef<Type *> Types,
750 ArrayRef<Value *> Args,
751 Instruction *FMFSource,
752 const Twine &Name) {
753 Module *M = BB->getModule();
754 Function *Fn = Intrinsic::getDeclaration(M, ID, Types);
755 return createCallHelper(Fn, Args, this, Name, FMFSource);
756 }