Masked Load / Store Intrinsics - the CodeGen part.
[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   CallInst *CI = CallInst::Create(Callee, Ops, "");
58   Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
59   Builder->SetInstDebugLocation(CI);
60   return CI;  
61 }
62
63 CallInst *IRBuilderBase::
64 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
65              bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
66              MDNode *NoAliasTag) {
67   Ptr = getCastedInt8PtrValue(Ptr);
68   Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
69   Type *Tys[] = { Ptr->getType(), Size->getType() };
70   Module *M = BB->getParent()->getParent();
71   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
72   
73   CallInst *CI = createCallHelper(TheFn, Ops, this);
74   
75   // Set the TBAA info if present.
76   if (TBAATag)
77     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
78
79   if (ScopeTag)
80     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
81  
82   if (NoAliasTag)
83     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
84  
85   return CI;
86 }
87
88 CallInst *IRBuilderBase::
89 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
90              bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
91              MDNode *ScopeTag, MDNode *NoAliasTag) {
92   Dst = getCastedInt8PtrValue(Dst);
93   Src = getCastedInt8PtrValue(Src);
94
95   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
96   Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
97   Module *M = BB->getParent()->getParent();
98   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
99   
100   CallInst *CI = createCallHelper(TheFn, Ops, this);
101   
102   // Set the TBAA info if present.
103   if (TBAATag)
104     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
105
106   // Set the TBAA Struct info if present.
107   if (TBAAStructTag)
108     CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
109  
110   if (ScopeTag)
111     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
112  
113   if (NoAliasTag)
114     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
115  
116   return CI;  
117 }
118
119 CallInst *IRBuilderBase::
120 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
121               bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
122               MDNode *NoAliasTag) {
123   Dst = getCastedInt8PtrValue(Dst);
124   Src = getCastedInt8PtrValue(Src);
125   
126   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
127   Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
128   Module *M = BB->getParent()->getParent();
129   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
130   
131   CallInst *CI = createCallHelper(TheFn, Ops, this);
132   
133   // Set the TBAA info if present.
134   if (TBAATag)
135     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
136  
137   if (ScopeTag)
138     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
139  
140   if (NoAliasTag)
141     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
142  
143   return CI;  
144 }
145
146 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
147   assert(isa<PointerType>(Ptr->getType()) &&
148          "lifetime.start only applies to pointers.");
149   Ptr = getCastedInt8PtrValue(Ptr);
150   if (!Size)
151     Size = getInt64(-1);
152   else
153     assert(Size->getType() == getInt64Ty() &&
154            "lifetime.start requires the size to be an i64");
155   Value *Ops[] = { Size, Ptr };
156   Module *M = BB->getParent()->getParent();
157   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
158   return createCallHelper(TheFn, Ops, this);
159 }
160
161 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
162   assert(isa<PointerType>(Ptr->getType()) &&
163          "lifetime.end only applies to pointers.");
164   Ptr = getCastedInt8PtrValue(Ptr);
165   if (!Size)
166     Size = getInt64(-1);
167   else
168     assert(Size->getType() == getInt64Ty() &&
169            "lifetime.end requires the size to be an i64");
170   Value *Ops[] = { Size, Ptr };
171   Module *M = BB->getParent()->getParent();
172   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
173   return createCallHelper(TheFn, Ops, this);
174 }
175
176 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
177   assert(Cond->getType() == getInt1Ty() &&
178          "an assumption condition must be of type i1");
179
180   Value *Ops[] = { Cond };
181   Module *M = BB->getParent()->getParent();
182   Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
183   return createCallHelper(FnAssume, Ops, this);
184 }
185
186 /// Create a call to a Masked Load intrinsic.
187 /// Ops - an array of operands.
188 CallInst *IRBuilderBase::CreateMaskedLoad(ArrayRef<Value *> Ops) {
189   // The only one overloaded type - the type of passthru value in this case
190   Type *DataTy = Ops[1]->getType();
191   return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy);
192 }
193
194 /// Create a call to a Masked Store intrinsic.
195 /// Ops - an array of operands.
196 CallInst *IRBuilderBase::CreateMaskedStore(ArrayRef<Value *> Ops) {
197   // DataTy - type of the data to be stored - the only one overloaded type
198   Type *DataTy = Ops[1]->getType();
199   return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, DataTy);
200 }
201
202 /// Create a call to a Masked intrinsic, with given intrinsic Id,
203 /// an array of operands - Ops, and one overloaded type - DataTy
204 CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
205                                                ArrayRef<Value *> Ops,
206                                                Type *DataTy) {
207   Module *M = BB->getParent()->getParent();
208   Type *OverloadedTypes[] = { DataTy };
209   Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
210   return createCallHelper(TheFn, Ops, this);
211 }