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 Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) {
27 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
28 Module &M = *BB->getParent()->getParent();
29 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
30 true, GlobalValue::PrivateLinkage,
33 GV->setUnnamedAddr(true);
37 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
38 assert(BB && BB->getParent() && "No current function!");
39 return BB->getParent()->getReturnType();
42 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
43 PointerType *PT = cast<PointerType>(Ptr->getType());
44 if (PT->getElementType()->isIntegerTy(8))
47 // Otherwise, we need to insert a bitcast.
48 PT = getInt8PtrTy(PT->getAddressSpace());
49 BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
50 BB->getInstList().insert(InsertPt, BCI);
51 SetInstDebugLocation(BCI);
55 static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
56 IRBuilderBase *Builder,
57 const Twine& Name="") {
58 CallInst *CI = CallInst::Create(Callee, Ops, Name);
59 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
60 Builder->SetInstDebugLocation(CI);
64 CallInst *IRBuilderBase::
65 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
66 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
68 Ptr = getCastedInt8PtrValue(Ptr);
69 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
70 Type *Tys[] = { Ptr->getType(), Size->getType() };
71 Module *M = BB->getParent()->getParent();
72 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
74 CallInst *CI = createCallHelper(TheFn, Ops, this);
76 // Set the TBAA info if present.
78 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
81 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
84 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
89 CallInst *IRBuilderBase::
90 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
91 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
92 MDNode *ScopeTag, MDNode *NoAliasTag) {
93 Dst = getCastedInt8PtrValue(Dst);
94 Src = getCastedInt8PtrValue(Src);
96 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
97 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
98 Module *M = BB->getParent()->getParent();
99 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
101 CallInst *CI = createCallHelper(TheFn, Ops, this);
103 // Set the TBAA info if present.
105 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
107 // Set the TBAA Struct info if present.
109 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
112 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
115 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
120 CallInst *IRBuilderBase::
121 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
122 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
123 MDNode *NoAliasTag) {
124 Dst = getCastedInt8PtrValue(Dst);
125 Src = getCastedInt8PtrValue(Src);
127 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
128 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
129 Module *M = BB->getParent()->getParent();
130 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
132 CallInst *CI = createCallHelper(TheFn, Ops, this);
134 // Set the TBAA info if present.
136 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
139 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
142 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
147 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
148 assert(isa<PointerType>(Ptr->getType()) &&
149 "lifetime.start only applies to pointers.");
150 Ptr = getCastedInt8PtrValue(Ptr);
154 assert(Size->getType() == getInt64Ty() &&
155 "lifetime.start requires the size to be an i64");
156 Value *Ops[] = { Size, Ptr };
157 Module *M = BB->getParent()->getParent();
158 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
159 return createCallHelper(TheFn, Ops, this);
162 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
163 assert(isa<PointerType>(Ptr->getType()) &&
164 "lifetime.end only applies to pointers.");
165 Ptr = getCastedInt8PtrValue(Ptr);
169 assert(Size->getType() == getInt64Ty() &&
170 "lifetime.end requires the size to be an i64");
171 Value *Ops[] = { Size, Ptr };
172 Module *M = BB->getParent()->getParent();
173 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
174 return createCallHelper(TheFn, Ops, this);
177 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
178 assert(Cond->getType() == getInt1Ty() &&
179 "an assumption condition must be of type i1");
181 Value *Ops[] = { Cond };
182 Module *M = BB->getParent()->getParent();
183 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
184 return createCallHelper(FnAssume, Ops, this);
187 /// Create a call to a Masked Load intrinsic.
188 /// Ptr - the base pointer for the load
189 /// Align - alignment of the source location
190 /// Mask - an vector of booleans which indicates what vector lanes should
191 /// be accessed in memory
192 /// PassThru - a pass-through value that is used to fill the masked-off lanes
194 /// Name - name of the result variable
195 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
196 Value *Mask, Value *PassThru,
198 assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type");
199 // DataTy is the overloaded type
200 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
201 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
203 PassThru = UndefValue::get(DataTy);
204 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
205 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
208 /// Create a call to a Masked Store intrinsic.
209 /// Val - the data to be stored,
210 /// Ptr - the base pointer for the store
211 /// Align - alignment of the destination location
212 /// Mask - an vector of booleans which indicates what vector lanes should
213 /// be accessed in memory
214 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
215 unsigned Align, Value *Mask) {
216 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
217 // Type of the data to be stored - the only one overloaded type
218 return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
221 /// Create a call to a Masked intrinsic, with given intrinsic Id,
222 /// an array of operands - Ops, and one overloaded type - DataTy
223 CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
224 ArrayRef<Value *> Ops,
227 Module *M = BB->getParent()->getParent();
228 Type *OverloadedTypes[] = { DataTy };
229 Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
230 return createCallHelper(TheFn, Ops, this, Name);
233 CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee,
234 ArrayRef<Value*> CallArgs,
235 ArrayRef<Value*> DeoptArgs,
236 ArrayRef<Value*> GCArgs,
238 // Extract out the type of the callee.
239 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
240 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
241 "actual callee must be a callable value");
244 Module *M = BB->getParent()->getParent();
245 // Fill in the one generic type'd argument (the function is also vararg)
246 Type *ArgTypes[] = { FuncPtrType };
247 Function *FnStatepoint =
248 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
251 std::vector<llvm::Value *> args;
252 args.push_back(ActualCallee);
253 args.push_back(getInt32(CallArgs.size()));
254 args.push_back(getInt32(0 /*unused*/));
255 args.insert(args.end(), CallArgs.begin(), CallArgs.end());
256 args.push_back(getInt32(DeoptArgs.size()));
257 args.insert(args.end(), DeoptArgs.begin(), DeoptArgs.end());
258 args.insert(args.end(), GCArgs.begin(), GCArgs.end());
260 return createCallHelper(FnStatepoint, args, this, Name);
263 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
266 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
267 Module *M = BB->getParent()->getParent();
268 Type *Types[] = {ResultType};
269 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
271 Value *Args[] = {Statepoint};
272 return createCallHelper(FnGCResult, Args, this, Name);
275 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
280 Module *M = BB->getParent()->getParent();
281 Type *Types[] = {ResultType};
282 Value *FnGCRelocate =
283 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
285 Value *Args[] = {Statepoint,
286 getInt32(BaseOffset),
287 getInt32(DerivedOffset)};
288 return createCallHelper(FnGCRelocate, Args, this, Name);