1 //===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/LLVMContext.h"
22 /// CreateGlobalString - Make a new global variable with an initializer that
23 /// has array of i8 type filled in with the nul terminated string value
24 /// specified. If Name is specified, it is the name of the global variable
26 GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
28 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
29 Module &M = *BB->getParent()->getParent();
30 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
31 true, GlobalValue::PrivateLinkage,
34 GV->setUnnamedAddr(true);
38 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
39 assert(BB && BB->getParent() && "No current function!");
40 return BB->getParent()->getReturnType();
43 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
44 PointerType *PT = cast<PointerType>(Ptr->getType());
45 if (PT->getElementType()->isIntegerTy(8))
48 // Otherwise, we need to insert a bitcast.
49 PT = getInt8PtrTy(PT->getAddressSpace());
50 BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
51 BB->getInstList().insert(InsertPt, BCI);
52 SetInstDebugLocation(BCI);
56 static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
57 IRBuilderBase *Builder,
58 const Twine& Name="") {
59 CallInst *CI = CallInst::Create(Callee, Ops, Name);
60 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
61 Builder->SetInstDebugLocation(CI);
65 static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest,
66 BasicBlock *UnwindDest,
67 ArrayRef<Value *> Ops,
68 IRBuilderBase *Builder,
69 const Twine &Name = "") {
71 InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name);
72 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),
74 Builder->SetInstDebugLocation(II);
78 CallInst *IRBuilderBase::
79 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
80 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
82 Ptr = getCastedInt8PtrValue(Ptr);
83 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
84 Type *Tys[] = { Ptr->getType(), Size->getType() };
85 Module *M = BB->getParent()->getParent();
86 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
88 CallInst *CI = createCallHelper(TheFn, Ops, this);
90 // Set the TBAA info if present.
92 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
95 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
98 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
103 CallInst *IRBuilderBase::
104 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
105 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
106 MDNode *ScopeTag, MDNode *NoAliasTag) {
107 Dst = getCastedInt8PtrValue(Dst);
108 Src = getCastedInt8PtrValue(Src);
110 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
111 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
112 Module *M = BB->getParent()->getParent();
113 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
115 CallInst *CI = createCallHelper(TheFn, Ops, this);
117 // Set the TBAA info if present.
119 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
121 // Set the TBAA Struct info if present.
123 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
126 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
129 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
134 CallInst *IRBuilderBase::
135 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
136 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
137 MDNode *NoAliasTag) {
138 Dst = getCastedInt8PtrValue(Dst);
139 Src = getCastedInt8PtrValue(Src);
141 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
142 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
143 Module *M = BB->getParent()->getParent();
144 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
146 CallInst *CI = createCallHelper(TheFn, Ops, this);
148 // Set the TBAA info if present.
150 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
153 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
156 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
161 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
162 assert(isa<PointerType>(Ptr->getType()) &&
163 "lifetime.start only applies to pointers.");
164 Ptr = getCastedInt8PtrValue(Ptr);
168 assert(Size->getType() == getInt64Ty() &&
169 "lifetime.start requires the size to be an i64");
170 Value *Ops[] = { Size, Ptr };
171 Module *M = BB->getParent()->getParent();
172 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
173 return createCallHelper(TheFn, Ops, this);
176 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
177 assert(isa<PointerType>(Ptr->getType()) &&
178 "lifetime.end only applies to pointers.");
179 Ptr = getCastedInt8PtrValue(Ptr);
183 assert(Size->getType() == getInt64Ty() &&
184 "lifetime.end requires the size to be an i64");
185 Value *Ops[] = { Size, Ptr };
186 Module *M = BB->getParent()->getParent();
187 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
188 return createCallHelper(TheFn, Ops, this);
191 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
192 assert(Cond->getType() == getInt1Ty() &&
193 "an assumption condition must be of type i1");
195 Value *Ops[] = { Cond };
196 Module *M = BB->getParent()->getParent();
197 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
198 return createCallHelper(FnAssume, Ops, this);
201 /// Create a call to a Masked Load intrinsic.
202 /// Ptr - the base pointer for the load
203 /// Align - alignment of the source location
204 /// Mask - an vector of booleans which indicates what vector lanes should
205 /// be accessed in memory
206 /// PassThru - a pass-through value that is used to fill the masked-off lanes
208 /// Name - name of the result variable
209 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
210 Value *Mask, Value *PassThru,
212 assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type");
213 // DataTy is the overloaded type
214 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
215 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
217 PassThru = UndefValue::get(DataTy);
218 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
219 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
222 /// Create a call to a Masked Store intrinsic.
223 /// Val - the data to be stored,
224 /// Ptr - the base pointer for the store
225 /// Align - alignment of the destination location
226 /// Mask - an vector of booleans which indicates what vector lanes should
227 /// be accessed in memory
228 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
229 unsigned Align, Value *Mask) {
230 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
231 // Type of the data to be stored - the only one overloaded type
232 return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
235 /// Create a call to a Masked intrinsic, with given intrinsic Id,
236 /// an array of operands - Ops, and one overloaded type - DataTy
237 CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
238 ArrayRef<Value *> Ops,
241 Module *M = BB->getParent()->getParent();
242 Type *OverloadedTypes[] = { DataTy };
243 Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
244 return createCallHelper(TheFn, Ops, this, Name);
247 static std::vector<Value *> getStatepointArgs(IRBuilderBase &B,
249 ArrayRef<Value *> CallArgs,
250 ArrayRef<Value *> DeoptArgs,
251 ArrayRef<Value *> GCArgs) {
252 std::vector<Value *> Args;
253 Args.push_back(ActualCallee);
254 Args.push_back(B.getInt32(CallArgs.size()));
255 Args.push_back(B.getInt32(0)); // unused
256 Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
257 Args.push_back(B.getInt32(DeoptArgs.size()));
258 Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
259 Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
264 CallInst *IRBuilderBase::CreateGCStatepointCall(Value *ActualCallee,
265 ArrayRef<Value *> CallArgs,
266 ArrayRef<Value *> DeoptArgs,
267 ArrayRef<Value *> GCArgs,
269 // Extract out the type of the callee.
270 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
271 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
272 "actual callee must be a callable value");
274 Module *M = BB->getParent()->getParent();
275 // Fill in the one generic type'd argument (the function is also vararg)
276 Type *ArgTypes[] = { FuncPtrType };
277 Function *FnStatepoint =
278 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
281 std::vector<llvm::Value *> Args =
282 getStatepointArgs(*this, ActualCallee, CallArgs, DeoptArgs, GCArgs);
283 return createCallHelper(FnStatepoint, Args, this, Name);
286 CallInst *IRBuilderBase::CreateGCStatepointCall(Value *ActualCallee,
287 ArrayRef<Use> CallArgs,
288 ArrayRef<Value *> DeoptArgs,
289 ArrayRef<Value *> GCArgs,
291 std::vector<Value *> VCallArgs;
292 for (auto &U : CallArgs)
293 VCallArgs.push_back(U.get());
294 return CreateGCStatepointCall(ActualCallee, VCallArgs, DeoptArgs, GCArgs,
298 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
299 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
300 ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
301 ArrayRef<Value *> GCArgs, const Twine &Name) {
302 // Extract out the type of the callee.
303 PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
304 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
305 "actual callee must be a callable value");
307 Module *M = BB->getParent()->getParent();
308 // Fill in the one generic type'd argument (the function is also vararg)
309 Function *FnStatepoint = Intrinsic::getDeclaration(
310 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
312 std::vector<llvm::Value *> Args =
313 getStatepointArgs(*this, ActualInvokee, InvokeArgs, DeoptArgs, GCArgs);
314 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, this,
318 InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
319 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
320 ArrayRef<Use> InvokeArgs, ArrayRef<Value *> DeoptArgs,
321 ArrayRef<Value *> GCArgs, const Twine &Name) {
322 std::vector<Value *> VCallArgs;
323 for (auto &U : InvokeArgs)
324 VCallArgs.push_back(U.get());
325 return CreateGCStatepointInvoke(ActualInvokee, NormalDest, UnwindDest,
326 VCallArgs, DeoptArgs, GCArgs, Name);
329 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
332 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
333 Module *M = BB->getParent()->getParent();
334 Type *Types[] = {ResultType};
335 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
337 Value *Args[] = {Statepoint};
338 return createCallHelper(FnGCResult, Args, this, Name);
341 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
346 Module *M = BB->getParent()->getParent();
347 Type *Types[] = {ResultType};
348 Value *FnGCRelocate =
349 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
351 Value *Args[] = {Statepoint,
352 getInt32(BaseOffset),
353 getInt32(DerivedOffset)};
354 return createCallHelper(FnGCRelocate, Args, this, Name);