LLVM  mainline
IRBuilder.cpp
Go to the documentation of this file.
00001 //===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the IRBuilder class, which is used as a convenient way
00011 // to create LLVM instructions with a consistent and simplified interface.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/IR/Function.h"
00016 #include "llvm/IR/GlobalVariable.h"
00017 #include "llvm/IR/IRBuilder.h"
00018 #include "llvm/IR/Intrinsics.h"
00019 #include "llvm/IR/LLVMContext.h"
00020 using namespace llvm;
00021 
00022 /// CreateGlobalString - Make a new global variable with an initializer that
00023 /// has array of i8 type filled in with the nul terminated string value
00024 /// specified.  If Name is specified, it is the name of the global variable
00025 /// created.
00026 GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
00027                                                   const Twine &Name) {
00028   Constant *StrConstant = ConstantDataArray::getString(Context, Str);
00029   Module &M = *BB->getParent()->getParent();
00030   GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
00031                                           true, GlobalValue::PrivateLinkage,
00032                                           StrConstant);
00033   GV->setName(Name);
00034   GV->setUnnamedAddr(true);
00035   return GV;
00036 }
00037 
00038 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
00039   assert(BB && BB->getParent() && "No current function!");
00040   return BB->getParent()->getReturnType();
00041 }
00042 
00043 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
00044   PointerType *PT = cast<PointerType>(Ptr->getType());
00045   if (PT->getElementType()->isIntegerTy(8))
00046     return Ptr;
00047   
00048   // Otherwise, we need to insert a bitcast.
00049   PT = getInt8PtrTy(PT->getAddressSpace());
00050   BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
00051   BB->getInstList().insert(InsertPt, BCI);
00052   SetInstDebugLocation(BCI);
00053   return BCI;
00054 }
00055 
00056 static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
00057                                   IRBuilderBase *Builder,
00058                                   const Twine& Name="") {
00059   CallInst *CI = CallInst::Create(Callee, Ops, Name);
00060   Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
00061   Builder->SetInstDebugLocation(CI);
00062   return CI;  
00063 }
00064 
00065 CallInst *IRBuilderBase::
00066 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
00067              bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
00068              MDNode *NoAliasTag) {
00069   Ptr = getCastedInt8PtrValue(Ptr);
00070   Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
00071   Type *Tys[] = { Ptr->getType(), Size->getType() };
00072   Module *M = BB->getParent()->getParent();
00073   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
00074   
00075   CallInst *CI = createCallHelper(TheFn, Ops, this);
00076   
00077   // Set the TBAA info if present.
00078   if (TBAATag)
00079     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
00080 
00081   if (ScopeTag)
00082     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
00083  
00084   if (NoAliasTag)
00085     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
00086  
00087   return CI;
00088 }
00089 
00090 CallInst *IRBuilderBase::
00091 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
00092              bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
00093              MDNode *ScopeTag, MDNode *NoAliasTag) {
00094   Dst = getCastedInt8PtrValue(Dst);
00095   Src = getCastedInt8PtrValue(Src);
00096 
00097   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
00098   Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
00099   Module *M = BB->getParent()->getParent();
00100   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
00101   
00102   CallInst *CI = createCallHelper(TheFn, Ops, this);
00103   
00104   // Set the TBAA info if present.
00105   if (TBAATag)
00106     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
00107 
00108   // Set the TBAA Struct info if present.
00109   if (TBAAStructTag)
00110     CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
00111  
00112   if (ScopeTag)
00113     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
00114  
00115   if (NoAliasTag)
00116     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
00117  
00118   return CI;  
00119 }
00120 
00121 CallInst *IRBuilderBase::
00122 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
00123               bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
00124               MDNode *NoAliasTag) {
00125   Dst = getCastedInt8PtrValue(Dst);
00126   Src = getCastedInt8PtrValue(Src);
00127   
00128   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
00129   Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
00130   Module *M = BB->getParent()->getParent();
00131   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
00132   
00133   CallInst *CI = createCallHelper(TheFn, Ops, this);
00134   
00135   // Set the TBAA info if present.
00136   if (TBAATag)
00137     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
00138  
00139   if (ScopeTag)
00140     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
00141  
00142   if (NoAliasTag)
00143     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
00144  
00145   return CI;  
00146 }
00147 
00148 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
00149   assert(isa<PointerType>(Ptr->getType()) &&
00150          "lifetime.start only applies to pointers.");
00151   Ptr = getCastedInt8PtrValue(Ptr);
00152   if (!Size)
00153     Size = getInt64(-1);
00154   else
00155     assert(Size->getType() == getInt64Ty() &&
00156            "lifetime.start requires the size to be an i64");
00157   Value *Ops[] = { Size, Ptr };
00158   Module *M = BB->getParent()->getParent();
00159   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
00160   return createCallHelper(TheFn, Ops, this);
00161 }
00162 
00163 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
00164   assert(isa<PointerType>(Ptr->getType()) &&
00165          "lifetime.end only applies to pointers.");
00166   Ptr = getCastedInt8PtrValue(Ptr);
00167   if (!Size)
00168     Size = getInt64(-1);
00169   else
00170     assert(Size->getType() == getInt64Ty() &&
00171            "lifetime.end requires the size to be an i64");
00172   Value *Ops[] = { Size, Ptr };
00173   Module *M = BB->getParent()->getParent();
00174   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
00175   return createCallHelper(TheFn, Ops, this);
00176 }
00177 
00178 CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
00179   assert(Cond->getType() == getInt1Ty() &&
00180          "an assumption condition must be of type i1");
00181 
00182   Value *Ops[] = { Cond };
00183   Module *M = BB->getParent()->getParent();
00184   Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
00185   return createCallHelper(FnAssume, Ops, this);
00186 }
00187 
00188 /// Create a call to a Masked Load intrinsic.
00189 /// Ptr      - the base pointer for the load
00190 /// Align    - alignment of the source location
00191 /// Mask     - an vector of booleans which indicates what vector lanes should
00192 ///            be accessed in memory
00193 /// PassThru - a pass-through value that is used to fill the masked-off lanes
00194 ///            of the result
00195 /// Name     - name of the result variable
00196 CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
00197                                           Value *Mask, Value *PassThru,
00198                                           const Twine &Name) {
00199   assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type");
00200   // DataTy is the overloaded type
00201   Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
00202   assert(DataTy->isVectorTy() && "Ptr should point to a vector");
00203   if (!PassThru)
00204     PassThru = UndefValue::get(DataTy);
00205   Value *Ops[] = { Ptr, getInt32(Align), Mask,  PassThru};
00206   return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
00207 }
00208 
00209 /// Create a call to a Masked Store intrinsic.
00210 /// Val   - the data to be stored,
00211 /// Ptr   - the base pointer for the store
00212 /// Align - alignment of the destination location
00213 /// Mask  - an vector of booleans which indicates what vector lanes should
00214 ///         be accessed in memory
00215 CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
00216                                            unsigned Align, Value *Mask) {
00217   Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
00218   // Type of the data to be stored - the only one overloaded type
00219   return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
00220 }
00221 
00222 /// Create a call to a Masked intrinsic, with given intrinsic Id,
00223 /// an array of operands - Ops, and one overloaded type - DataTy
00224 CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id,
00225                                                ArrayRef<Value *> Ops,
00226                                                Type *DataTy,
00227                                                const Twine &Name) {
00228   Module *M = BB->getParent()->getParent();
00229   Type *OverloadedTypes[] = { DataTy };
00230   Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes);
00231   return createCallHelper(TheFn, Ops, this, Name);
00232 }
00233 
00234 CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee,
00235                                             ArrayRef<Value *> CallArgs,
00236                                             ArrayRef<Value *> DeoptArgs,
00237                                             ArrayRef<Value *> GCArgs,
00238                                             const Twine &Name) {
00239  // Extract out the type of the callee.
00240  PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
00241  assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
00242         "actual callee must be a callable value");
00243 
00244  
00245  Module *M = BB->getParent()->getParent();
00246  // Fill in the one generic type'd argument (the function is also vararg)
00247  Type *ArgTypes[] = { FuncPtrType };
00248  Function *FnStatepoint =
00249    Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
00250                              ArgTypes);
00251 
00252  std::vector<llvm::Value *> args;
00253  args.push_back(ActualCallee);
00254  args.push_back(getInt32(CallArgs.size()));
00255  args.push_back(getInt32(0 /*unused*/));
00256  args.insert(args.end(), CallArgs.begin(), CallArgs.end());
00257  args.push_back(getInt32(DeoptArgs.size()));
00258  args.insert(args.end(), DeoptArgs.begin(), DeoptArgs.end());
00259  args.insert(args.end(), GCArgs.begin(), GCArgs.end());
00260 
00261  return createCallHelper(FnStatepoint, args, this, Name);
00262 }
00263 
00264 CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee,
00265                                             ArrayRef<Use> CallArgs,
00266                                             ArrayRef<Value *> DeoptArgs,
00267                                             ArrayRef<Value *> GCArgs,
00268                                             const Twine &Name) {
00269   std::vector<Value *> VCallArgs;
00270   for (auto &U : CallArgs)
00271     VCallArgs.push_back(U.get());
00272   return CreateGCStatepoint(ActualCallee, VCallArgs, DeoptArgs, GCArgs, Name);
00273 }
00274 
00275 CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
00276                                        Type *ResultType,
00277                                        const Twine &Name) {
00278  Intrinsic::ID ID = Intrinsic::experimental_gc_result;
00279  Module *M = BB->getParent()->getParent();
00280  Type *Types[] = {ResultType};
00281  Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
00282 
00283  Value *Args[] = {Statepoint};
00284  return createCallHelper(FnGCResult, Args, this, Name);
00285 }
00286 
00287 CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
00288                                          int BaseOffset,
00289                                          int DerivedOffset,
00290                                          Type *ResultType,
00291                                          const Twine &Name) {
00292  Module *M = BB->getParent()->getParent();
00293  Type *Types[] = {ResultType};
00294  Value *FnGCRelocate =
00295    Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
00296 
00297  Value *Args[] = {Statepoint,
00298                   getInt32(BaseOffset),
00299                   getInt32(DerivedOffset)};
00300  return createCallHelper(FnGCRelocate, Args, this, Name);
00301 }