Add IRBuilder routines for gc.statepoints, gc.results, and gc.relocates
[oota-llvm.git] / lib / IR / IRBuilder.cpp
1 //===---- 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
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"
20 using namespace llvm;
21
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
25 /// created.
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,
31                                           StrConstant);
32   GV->setName(Name);
33   GV->setUnnamedAddr(true);
34   return GV;
35 }
36
37 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
38   assert(BB && BB->getParent() && "No current function!");
39   return BB->getParent()->getReturnType();
40 }
41
42 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
43   PointerType *PT = cast<PointerType>(Ptr->getType());
44   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
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);
61   return CI;  
62 }
63
64 CallInst *IRBuilderBase::
65 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
66              bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
67              MDNode *NoAliasTag) {
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);
73   
74   CallInst *CI = createCallHelper(TheFn, Ops, this);
75   
76   // Set the TBAA info if present.
77   if (TBAATag)
78     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
79
80   if (ScopeTag)
81     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
82  
83   if (NoAliasTag)
84     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
85  
86   return CI;
87 }
88
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);
95
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);
100   
101   CallInst *CI = createCallHelper(TheFn, Ops, this);
102   
103   // Set the TBAA info if present.
104   if (TBAATag)
105     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
106
107   // Set the TBAA Struct info if present.
108   if (TBAAStructTag)
109     CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
110  
111   if (ScopeTag)
112     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
113  
114   if (NoAliasTag)
115     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
116  
117   return CI;  
118 }
119
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);
126   
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);
131   
132   CallInst *CI = createCallHelper(TheFn, Ops, this);
133   
134   // Set the TBAA info if present.
135   if (TBAATag)
136     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
137  
138   if (ScopeTag)
139     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
140  
141   if (NoAliasTag)
142     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
143  
144   return CI;  
145 }
146
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);
151   if (!Size)
152     Size = getInt64(-1);
153   else
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);
160 }
161
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);
166   if (!Size)
167     Size = getInt64(-1);
168   else
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);
175 }
176
177 CallInst *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
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);
193 }
194
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);
201 }
202
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,
207                                                Type *DataTy) {
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);
212 }
213
214 CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee,
215                                             ArrayRef<Value*> CallArgs,
216                                             ArrayRef<Value*> DeoptArgs,
217                                             ArrayRef<Value*> GCArgs,
218                                             const Twine& Name) {
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");
223
224  
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,
230                              ArgTypes);
231
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());
240
241  return createCallHelper(FnStatepoint, args, this, Name);
242 }
243
244 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
245                                        Type *ResultType,
246                                        const Twine &Name) {
247  Intrinsic::ID ID;
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;
254  } else {
255    llvm_unreachable("unimplemented result type for gc.result");
256  }
257  Module *M = BB->getParent()->getParent();
258  Type *Types[] = {ResultType};
259  Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
260
261  Value *Args[] = {Statepoint};
262  return createCallHelper(FnGCResult, Args, this, Name);
263 }
264
265 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
266                                          int BaseOffset,
267                                          int DerivedOffset,
268                                          Type *ResultType,
269                                          const Twine &Name) {
270  Module *M = BB->getParent()->getParent();
271  Type *Types[] = {ResultType};
272  Value *FnGCRelocate =
273    Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
274
275  Value *Args[] = {Statepoint,
276                   getInt32(BaseOffset),
277                   getInt32(DerivedOffset)};
278  return createCallHelper(FnGCRelocate, Args, this, Name);
279 }