blob: 87fca8c861037fbf0e766ce9a24d3e2b79f7b4ef [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"
Pat Gavlincc0431d2015-05-08 18:07:42 +000020#include "llvm/IR/Statepoint.h"
Chris Lattner17079fc2009-12-28 21:28:46 +000021using namespace llvm;
22
23/// CreateGlobalString - Make a new global variable with an initializer that
Dan Gohman97c59022010-02-10 20:04:19 +000024/// has array of i8 type filled in with the nul terminated string value
Chris Lattner17079fc2009-12-28 21:28:46 +000025/// specified. If Name is specified, it is the name of the global variable
26/// created.
David Blaikieaa41cd52015-04-03 21:33:42 +000027GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
28 const Twine &Name) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +000029 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
Chris Lattner17079fc2009-12-28 21:28:46 +000030 Module &M = *BB->getParent()->getParent();
31 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
Benjamin Kramerf1fd6e32011-12-22 14:22:14 +000032 true, GlobalValue::PrivateLinkage,
Hans Wennborgcbe34b42012-06-23 11:37:03 +000033 StrConstant);
Chris Lattner17079fc2009-12-28 21:28:46 +000034 GV->setName(Name);
Nick Lewycky561f1752011-04-07 00:14:29 +000035 GV->setUnnamedAddr(true);
Chris Lattner17079fc2009-12-28 21:28:46 +000036 return GV;
37}
Chris Lattner7ef1cac2009-12-28 21:45:40 +000038
Chris Lattnerb1907b22011-07-12 04:14:22 +000039Type *IRBuilderBase::getCurrentFunctionReturnType() const {
Chris Lattner49f9f762009-12-28 21:50:56 +000040 assert(BB && BB->getParent() && "No current function!");
41 return BB->getParent()->getReturnType();
42}
Chris Lattner143a07c2010-12-26 22:49:25 +000043
44Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
Chris Lattner229907c2011-07-18 04:54:35 +000045 PointerType *PT = cast<PointerType>(Ptr->getType());
Chris Lattner143a07c2010-12-26 22:49:25 +000046 if (PT->getElementType()->isIntegerTy(8))
47 return Ptr;
48
49 // Otherwise, we need to insert a bitcast.
50 PT = getInt8PtrTy(PT->getAddressSpace());
51 BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
52 BB->getInstList().insert(InsertPt, BCI);
53 SetInstDebugLocation(BCI);
54 return BCI;
55}
56
Jay Foad5bd375a2011-07-15 08:37:34 +000057static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
Philip Reames4750dd12014-12-30 05:55:58 +000058 IRBuilderBase *Builder,
59 const Twine& Name="") {
60 CallInst *CI = CallInst::Create(Callee, Ops, Name);
Chris Lattner143a07c2010-12-26 22:49:25 +000061 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
62 Builder->SetInstDebugLocation(CI);
63 return CI;
64}
65
Sanjoy Dasabe1c682015-05-06 23:53:09 +000066static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest,
67 BasicBlock *UnwindDest,
68 ArrayRef<Value *> Ops,
69 IRBuilderBase *Builder,
70 const Twine &Name = "") {
71 InvokeInst *II =
72 InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name);
73 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),
74 II);
75 Builder->SetInstDebugLocation(II);
76 return II;
77}
78
Chris Lattner143a07c2010-12-26 22:49:25 +000079CallInst *IRBuilderBase::
80CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +000081 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
82 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +000083 Ptr = getCastedInt8PtrValue(Ptr);
84 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +000085 Type *Tys[] = { Ptr->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +000086 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +000087 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +000088
Jay Foad5bd375a2011-07-15 08:37:34 +000089 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +000090
91 // Set the TBAA info if present.
92 if (TBAATag)
93 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +000094
95 if (ScopeTag)
96 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
97
98 if (NoAliasTag)
99 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
100
Chris Lattner143a07c2010-12-26 22:49:25 +0000101 return CI;
102}
103
104CallInst *IRBuilderBase::
105CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000106 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
107 MDNode *ScopeTag, MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000108 Dst = getCastedInt8PtrValue(Dst);
109 Src = getCastedInt8PtrValue(Src);
110
111 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000112 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000113 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000114 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000115
Jay Foad5bd375a2011-07-15 08:37:34 +0000116 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000117
118 // Set the TBAA info if present.
119 if (TBAATag)
120 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Dan Gohman099727f2012-09-26 22:17:14 +0000121
122 // Set the TBAA Struct info if present.
123 if (TBAAStructTag)
124 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
Hal Finkel94146652014-07-24 14:25:39 +0000125
126 if (ScopeTag)
127 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
128
129 if (NoAliasTag)
130 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
131
Chris Lattner143a07c2010-12-26 22:49:25 +0000132 return CI;
133}
134
135CallInst *IRBuilderBase::
136CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000137 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
138 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000139 Dst = getCastedInt8PtrValue(Dst);
140 Src = getCastedInt8PtrValue(Src);
141
142 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000143 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000144 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000145 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000146
Jay Foad5bd375a2011-07-15 08:37:34 +0000147 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000148
149 // Set the TBAA info if present.
150 if (TBAATag)
151 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +0000152
153 if (ScopeTag)
154 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
155
156 if (NoAliasTag)
157 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
158
Chris Lattner143a07c2010-12-26 22:49:25 +0000159 return CI;
160}
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000161
162CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
163 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000164 "lifetime.start 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.start 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_start);
Jay Foad5bd375a2011-07-15 08:37:34 +0000174 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000175}
176
177CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
178 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000179 "lifetime.end only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000180 Ptr = getCastedInt8PtrValue(Ptr);
181 if (!Size)
182 Size = getInt64(-1);
183 else
184 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000185 "lifetime.end requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000186 Value *Ops[] = { Size, Ptr };
187 Module *M = BB->getParent()->getParent();
188 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
Jay Foad5bd375a2011-07-15 08:37:34 +0000189 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000190}
Hal Finkel6f814db2014-10-15 23:44:22 +0000191
192CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
193 assert(Cond->getType() == getInt1Ty() &&
194 "an assumption condition must be of type i1");
195
196 Value *Ops[] = { Cond };
197 Module *M = BB->getParent()->getParent();
198 Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
199 return createCallHelper(FnAssume, Ops, this);
200}
201
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000202/// Create a call to a Masked Load intrinsic.
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000203/// Ptr - the base pointer for the load
204/// Align - alignment of the source location
205/// Mask - an vector of booleans which indicates what vector lanes should
206/// be accessed in memory
207/// PassThru - a pass-through value that is used to fill the masked-off lanes
208/// of the result
209/// Name - name of the result variable
210CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
211 Value *Mask, Value *PassThru,
212 const Twine &Name) {
213 assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type");
214 // DataTy is the overloaded type
215 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
216 assert(DataTy->isVectorTy() && "Ptr should point to a vector");
217 if (!PassThru)
218 PassThru = UndefValue::get(DataTy);
219 Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
220 return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000221}
222
223/// Create a call to a Masked Store intrinsic.
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000224/// Val - the data to be stored,
225/// Ptr - the base pointer for the store
226/// Align - alignment of the destination location
227/// Mask - an vector of booleans which indicates what vector lanes should
228/// be accessed in memory
229CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
230 unsigned Align, Value *Mask) {
231 Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
232 // Type of the data to be stored - the only one overloaded type
233 return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000234}
235
236/// Create a call to a Masked intrinsic, with given intrinsic Id,
237/// an array of operands - Ops, and one overloaded type - DataTy
238CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
239 ArrayRef<Value *> Ops,
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000240 Type *DataTy,
241 const Twine &Name) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000242 Module *M = BB->getParent()->getParent();
243 Type *OverloadedTypes[] = { DataTy };
244 Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
Elena Demikhovsky84d19972014-12-30 14:28:14 +0000245 return createCallHelper(TheFn, Ops, this, Name);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000246}
Philip Reames4750dd12014-12-30 05:55:58 +0000247
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000248static std::vector<Value *> getStatepointArgs(IRBuilderBase &B,
249 Value *ActualCallee,
250 ArrayRef<Value *> CallArgs,
251 ArrayRef<Value *> DeoptArgs,
252 ArrayRef<Value *> GCArgs) {
253 std::vector<Value *> Args;
254 Args.push_back(ActualCallee);
255 Args.push_back(B.getInt32(CallArgs.size()));
Pat Gavlincc0431d2015-05-08 18:07:42 +0000256 Args.push_back(B.getInt32((unsigned)StatepointFlags::None));
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000257 Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
Pat Gavlincc0431d2015-05-08 18:07:42 +0000258 Args.push_back(B.getInt32(0 /* no transition args */));
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000259 Args.push_back(B.getInt32(DeoptArgs.size()));
260 Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
261 Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
262
263 return Args;
264}
265
266CallInst *IRBuilderBase::CreateGCStatepointCall(Value *ActualCallee,
267 ArrayRef<Value *> CallArgs,
268 ArrayRef<Value *> DeoptArgs,
269 ArrayRef<Value *> GCArgs,
270 const Twine &Name) {
Sanjoy Das63245b52015-05-06 02:36:34 +0000271 // Extract out the type of the callee.
272 PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
273 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
274 "actual callee must be a callable value");
Philip Reames4750dd12014-12-30 05:55:58 +0000275
Sanjoy Das63245b52015-05-06 02:36:34 +0000276 Module *M = BB->getParent()->getParent();
277 // Fill in the one generic type'd argument (the function is also vararg)
278 Type *ArgTypes[] = { FuncPtrType };
279 Function *FnStatepoint =
280 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
281 ArgTypes);
Philip Reames4750dd12014-12-30 05:55:58 +0000282
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000283 std::vector<llvm::Value *> Args =
284 getStatepointArgs(*this, ActualCallee, CallArgs, DeoptArgs, GCArgs);
285 return createCallHelper(FnStatepoint, Args, this, Name);
Philip Reames4750dd12014-12-30 05:55:58 +0000286}
287
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000288CallInst *IRBuilderBase::CreateGCStatepointCall(Value *ActualCallee,
289 ArrayRef<Use> CallArgs,
290 ArrayRef<Value *> DeoptArgs,
291 ArrayRef<Value *> GCArgs,
292 const Twine &Name) {
Ramkumar Ramachandra3408f3e2015-02-26 00:35:56 +0000293 std::vector<Value *> VCallArgs;
294 for (auto &U : CallArgs)
295 VCallArgs.push_back(U.get());
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000296 return CreateGCStatepointCall(ActualCallee, VCallArgs, DeoptArgs, GCArgs,
297 Name);
298}
299
300InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
301 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
302 ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
303 ArrayRef<Value *> GCArgs, const Twine &Name) {
304 // Extract out the type of the callee.
305 PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
306 assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
307 "actual callee must be a callable value");
308
309 Module *M = BB->getParent()->getParent();
310 // Fill in the one generic type'd argument (the function is also vararg)
311 Function *FnStatepoint = Intrinsic::getDeclaration(
312 M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
313
314 std::vector<llvm::Value *> Args =
315 getStatepointArgs(*this, ActualInvokee, InvokeArgs, DeoptArgs, GCArgs);
316 return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, this,
317 Name);
318}
319
320InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
321 Value *ActualInvokee, BasicBlock *NormalDest, BasicBlock *UnwindDest,
322 ArrayRef<Use> InvokeArgs, ArrayRef<Value *> DeoptArgs,
323 ArrayRef<Value *> GCArgs, const Twine &Name) {
324 std::vector<Value *> VCallArgs;
325 for (auto &U : InvokeArgs)
326 VCallArgs.push_back(U.get());
327 return CreateGCStatepointInvoke(ActualInvokee, NormalDest, UnwindDest,
328 VCallArgs, DeoptArgs, GCArgs, Name);
Ramkumar Ramachandra3408f3e2015-02-26 00:35:56 +0000329}
330
Philip Reames4750dd12014-12-30 05:55:58 +0000331CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
332 Type *ResultType,
333 const Twine &Name) {
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000334 Intrinsic::ID ID = Intrinsic::experimental_gc_result;
Philip Reames4750dd12014-12-30 05:55:58 +0000335 Module *M = BB->getParent()->getParent();
336 Type *Types[] = {ResultType};
337 Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
338
339 Value *Args[] = {Statepoint};
340 return createCallHelper(FnGCResult, Args, this, Name);
341}
342
343CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
344 int BaseOffset,
345 int DerivedOffset,
346 Type *ResultType,
347 const Twine &Name) {
348 Module *M = BB->getParent()->getParent();
349 Type *Types[] = {ResultType};
350 Value *FnGCRelocate =
351 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
352
353 Value *Args[] = {Statepoint,
354 getInt32(BaseOffset),
355 getInt32(DerivedOffset)};
356 return createCallHelper(FnGCRelocate, Args, this, Name);
357}