LLVM API Documentation

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