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 /// Ops - an array of operands.
189 CallInst *IRBuilderBase::CreateMaskedLoad(ArrayRef<Value *> Ops) {
190 // The only one overloaded type - the type of passthru value in this case
191 Type *DataTy = Ops[3]->getType();
192 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy);
195 /// Create a call to a Masked Store intrinsic.
196 /// Ops - an array of operands.
197 CallInst *IRBuilderBase::CreateMaskedStore(ArrayRef<Value *> Ops) {
198 // DataTy - type of the data to be stored - the only one overloaded type
199 Type *DataTy = Ops[0]->getType();
200 return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, DataTy);
203 /// Create a call to a Masked intrinsic, with given intrinsic Id,
204 /// an array of operands - Ops, and one overloaded type - DataTy
205 CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
206 ArrayRef<Value *> Ops,
208 Module *M = BB->getParent()->getParent();
209 Type *OverloadedTypes[] = { DataTy };
210 Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
211 return createCallHelper(TheFn, Ops, this);
214 CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee,
215 ArrayRef<Value*> CallArgs,
216 ArrayRef<Value*> DeoptArgs,
217 ArrayRef<Value*> GCArgs,
219 // Extract out the type of the callee.
220 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
221 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
222 "actual callee must be a callable value");
225 Module *M = BB->getParent()->getParent();
226 // Fill in the one generic type'd argument (the function is also vararg)
227 Type *ArgTypes[] = { FuncPtrType };
228 Function *FnStatepoint =
229 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
232 std::vector<llvm::Value *> args;
233 args.push_back(ActualCallee);
234 args.push_back(getInt32(CallArgs.size()));
235 args.push_back(getInt32(0 /*unused*/));
236 args.insert(args.end(), CallArgs.begin(), CallArgs.end());
237 args.push_back(getInt32(DeoptArgs.size()));
238 args.insert(args.end(), DeoptArgs.begin(), DeoptArgs.end());
239 args.insert(args.end(), GCArgs.begin(), GCArgs.end());
241 return createCallHelper(FnStatepoint, args, this, Name);
244 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
248 if (ResultType->isIntegerTy()) {
249 ID = Intrinsic::experimental_gc_result_int;
250 } else if (ResultType->isFloatingPointTy()) {
251 ID = Intrinsic::experimental_gc_result_float;
252 } else if (ResultType->isPointerTy()) {
253 ID = Intrinsic::experimental_gc_result_ptr;
255 llvm_unreachable("unimplemented result type for gc.result");
257 Module *M = BB->getParent()->getParent();
258 Type *Types[] = {ResultType};
259 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
261 Value *Args[] = {Statepoint};
262 return createCallHelper(FnGCResult, Args, this, Name);
265 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
270 Module *M = BB->getParent()->getParent();
271 Type *Types[] = {ResultType};
272 Value *FnGCRelocate =
273 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
275 Value *Args[] = {Statepoint,
276 getInt32(BaseOffset),
277 getInt32(DerivedOffset)};
278 return createCallHelper(FnGCRelocate, Args, this, Name);