blob: dd29e7ae24fc307ca2df2fa5b63b842907fbfeb8 [file] [log] [blame]
Chris Lattner17079fc2009-12-28 21:28:46 +00001//===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#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"
Chris Lattner17079fc2009-12-28 21:28:46 +000020using namespace llvm;
21
22/// CreateGlobalString - Make a new global variable with an initializer that
Dan Gohman97c59022010-02-10 20:04:19 +000023/// has array of i8 type filled in with the nul terminated string value
Chris Lattner17079fc2009-12-28 21:28:46 +000024/// specified. If Name is specified, it is the name of the global variable
25/// created.
Nick Lewycky11168322011-04-12 00:29:07 +000026Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +000027 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
Chris Lattner17079fc2009-12-28 21:28:46 +000028 Module &M = *BB->getParent()->getParent();
29 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
Benjamin Kramerf1fd6e32011-12-22 14:22:14 +000030 true, GlobalValue::PrivateLinkage,
Hans Wennborgcbe34b42012-06-23 11:37:03 +000031 StrConstant);
Chris Lattner17079fc2009-12-28 21:28:46 +000032 GV->setName(Name);
Nick Lewycky561f1752011-04-07 00:14:29 +000033 GV->setUnnamedAddr(true);
Chris Lattner17079fc2009-12-28 21:28:46 +000034 return GV;
35}
Chris Lattner7ef1cac2009-12-28 21:45:40 +000036
Chris Lattnerb1907b22011-07-12 04:14:22 +000037Type *IRBuilderBase::getCurrentFunctionReturnType() const {
Chris Lattner49f9f762009-12-28 21:50:56 +000038 assert(BB && BB->getParent() && "No current function!");
39 return BB->getParent()->getReturnType();
40}
Chris Lattner143a07c2010-12-26 22:49:25 +000041
42Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
Chris Lattner229907c2011-07-18 04:54:35 +000043 PointerType *PT = cast<PointerType>(Ptr->getType());
Chris Lattner143a07c2010-12-26 22:49:25 +000044 if (PT->getElementType()->isIntegerTy(8))
45 return Ptr;
46
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);
52 return BCI;
53}
54
Jay Foad5bd375a2011-07-15 08:37:34 +000055static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
Philip Reames4750dd12014-12-30 05:55:58 +000056 IRBuilderBase *Builder,
57 const Twine& Name="") {
58 CallInst *CI = CallInst::Create(Callee, Ops, Name);
Chris Lattner143a07c2010-12-26 22:49:25 +000059 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
60 Builder->SetInstDebugLocation(CI);
61 return CI;
62}
63
Chris Lattner143a07c2010-12-26 22:49:25 +000064CallInst *IRBuilderBase::
65CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +000066 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
67 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +000068 Ptr = getCastedInt8PtrValue(Ptr);
69 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +000070 Type *Tys[] = { Ptr->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +000071 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +000072 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +000073
Jay Foad5bd375a2011-07-15 08:37:34 +000074 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +000075
76 // Set the TBAA info if present.
77 if (TBAATag)
78 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +000079
80 if (ScopeTag)
81 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
82
83 if (NoAliasTag)
84 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
85
Chris Lattner143a07c2010-12-26 22:49:25 +000086 return CI;
87}
88
89CallInst *IRBuilderBase::
90CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +000091 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
92 MDNode *ScopeTag, MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +000093 Dst = getCastedInt8PtrValue(Dst);
94 Src = getCastedInt8PtrValue(Src);
95
96 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +000097 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +000098 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +000099 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000100
Jay Foad5bd375a2011-07-15 08:37:34 +0000101 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000102
103 // Set the TBAA info if present.
104 if (TBAATag)
105 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Dan Gohman099727f2012-09-26 22:17:14 +0000106
107 // Set the TBAA Struct info if present.
108 if (TBAAStructTag)
109 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
Hal Finkel94146652014-07-24 14:25:39 +0000110
111 if (ScopeTag)
112 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
113
114 if (NoAliasTag)
115 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
116
Chris Lattner143a07c2010-12-26 22:49:25 +0000117 return CI;
118}
119
120CallInst *IRBuilderBase::
121CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000122 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
123 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000124 Dst = getCastedInt8PtrValue(Dst);
125 Src = getCastedInt8PtrValue(Src);
126
127 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000128 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000129 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000130 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000131
Jay Foad5bd375a2011-07-15 08:37:34 +0000132 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000133
134 // Set the TBAA info if present.
135 if (TBAATag)
136 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +0000137
138 if (ScopeTag)
139 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
140
141 if (NoAliasTag)
142 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
143
Chris Lattner143a07c2010-12-26 22:49:25 +0000144 return CI;
145}
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000146
147CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
148 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000149 "lifetime.start only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000150 Ptr = getCastedInt8PtrValue(Ptr);
151 if (!Size)
152 Size = getInt64(-1);
153 else
154 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000155 "lifetime.start requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000156 Value *Ops[] = { Size, Ptr };
157 Module *M = BB->getParent()->getParent();
158 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
Jay Foad5bd375a2011-07-15 08:37:34 +0000159 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000160}
161
162CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
163 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000164 "lifetime.end only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000165 Ptr = getCastedInt8PtrValue(Ptr);
166 if (!Size)
167 Size = getInt64(-1);
168 else
169 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000170 "lifetime.end requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000171 Value *Ops[] = { Size, Ptr };
172 Module *M = BB->getParent()->getParent();
173 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
Jay Foad5bd375a2011-07-15 08:37:34 +0000174 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000175}
Hal Finkel6f814db2014-10-15 23:44:22 +0000176
177CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
178 assert(Cond->getType() == getInt1Ty() &&
179 "an assumption condition must be of type i1");
180
181 Value *Ops[] = { Cond };
182 Module *M = BB->getParent()->getParent();
183 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
184 return createCallHelper(FnAssume, Ops, this);
185}
186
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000187/// Create a call to a Masked Load intrinsic.
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000188/// 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
193/// of the result
194/// Name - name of the result variable
195CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
196 Value *Mask, Value *PassThru,
197 const Twine &Name) {
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");
202 if (!PassThru)
203 PassThru = UndefValue::get(DataTy);
204 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
205 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000206}
207
208/// Create a call to a Masked Store intrinsic.
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000209/// 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
214CallInst *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());
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000219}
220
221/// Create a call to a Masked intrinsic, with given intrinsic Id,
222/// an array of operands - Ops, and one overloaded type - DataTy
223CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
224 ArrayRef<Value *> Ops,
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000225 Type *DataTy,
226 const Twine &Name) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000227 Module *M = BB->getParent()->getParent();
228 Type *OverloadedTypes[] = { DataTy };
229 Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000230 return createCallHelper(TheFn, Ops, this, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000231}
Philip Reames4750dd12014-12-30 05:55:58 +0000232
233CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee,
234 ArrayRef<Value*> CallArgs,
235 ArrayRef<Value*> DeoptArgs,
236 ArrayRef<Value*> GCArgs,
237 const Twine& Name) {
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");
242
243
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,
249 ArgTypes);
250
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());
259
260 return createCallHelper(FnStatepoint, args, this, Name);
261}
262
263CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
264 Type *ResultType,
265 const Twine &Name) {
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000266 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
Philip Reames4750dd12014-12-30 05:55:58 +0000267 Module *M = BB->getParent()->getParent();
268 Type *Types[] = {ResultType};
269 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
270
271 Value *Args[] = {Statepoint};
272 return createCallHelper(FnGCResult, Args, this, Name);
273}
274
275CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
276 int BaseOffset,
277 int DerivedOffset,
278 Type *ResultType,
279 const Twine &Name) {
280 Module *M = BB->getParent()->getParent();
281 Type *Types[] = {ResultType};
282 Value *FnGCRelocate =
283 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
284
285 Value *Args[] = {Statepoint,
286 getInt32(BaseOffset),
287 getInt32(DerivedOffset)};
288 return createCallHelper(FnGCRelocate, Args, this, Name);
289}