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   CallInst *CI = CallInst::Create(Callee, Ops, "");
00058   Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
00059   Builder->SetInstDebugLocation(CI);
00060   return CI;  
00061 }
00062 
00063 CallInst *IRBuilderBase::
00064 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
00065              bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
00066              MDNode *NoAliasTag) {
00067   Ptr = getCastedInt8PtrValue(Ptr);
00068   Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
00069   Type *Tys[] = { Ptr->getType(), Size->getType() };
00070   Module *M = BB->getParent()->getParent();
00071   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
00072   
00073   CallInst *CI = createCallHelper(TheFn, Ops, this);
00074   
00075   // Set the TBAA info if present.
00076   if (TBAATag)
00077     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
00078 
00079   if (ScopeTag)
00080     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
00081  
00082   if (NoAliasTag)
00083     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
00084  
00085   return CI;
00086 }
00087 
00088 CallInst *IRBuilderBase::
00089 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
00090              bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
00091              MDNode *ScopeTag, MDNode *NoAliasTag) {
00092   Dst = getCastedInt8PtrValue(Dst);
00093   Src = getCastedInt8PtrValue(Src);
00094 
00095   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
00096   Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
00097   Module *M = BB->getParent()->getParent();
00098   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
00099   
00100   CallInst *CI = createCallHelper(TheFn, Ops, this);
00101   
00102   // Set the TBAA info if present.
00103   if (TBAATag)
00104     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
00105 
00106   // Set the TBAA Struct info if present.
00107   if (TBAAStructTag)
00108     CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
00109  
00110   if (ScopeTag)
00111     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
00112  
00113   if (NoAliasTag)
00114     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
00115  
00116   return CI;  
00117 }
00118 
00119 CallInst *IRBuilderBase::
00120 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
00121               bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
00122               MDNode *NoAliasTag) {
00123   Dst = getCastedInt8PtrValue(Dst);
00124   Src = getCastedInt8PtrValue(Src);
00125   
00126   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
00127   Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
00128   Module *M = BB->getParent()->getParent();
00129   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
00130   
00131   CallInst *CI = createCallHelper(TheFn, Ops, this);
00132   
00133   // Set the TBAA info if present.
00134   if (TBAATag)
00135     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
00136  
00137   if (ScopeTag)
00138     CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
00139  
00140   if (NoAliasTag)
00141     CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
00142  
00143   return CI;  
00144 }
00145 
00146 CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
00147   assert(isa<PointerType>(Ptr->getType()) &&
00148          "lifetime.start only applies to pointers.");
00149   Ptr = getCastedInt8PtrValue(Ptr);
00150   if (!Size)
00151     Size = getInt64(-1);
00152   else
00153     assert(Size->getType() == getInt64Ty() &&
00154            "lifetime.start requires the size to be an i64");
00155   Value *Ops[] = { Size, Ptr };
00156   Module *M = BB->getParent()->getParent();
00157   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
00158   return createCallHelper(TheFn, Ops, this);
00159 }
00160 
00161 CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
00162   assert(isa<PointerType>(Ptr->getType()) &&
00163          "lifetime.end only applies to pointers.");
00164   Ptr = getCastedInt8PtrValue(Ptr);
00165   if (!Size)
00166     Size = getInt64(-1);
00167   else
00168     assert(Size->getType() == getInt64Ty() &&
00169            "lifetime.end requires the size to be an i64");
00170   Value *Ops[] = { Size, Ptr };
00171   Module *M = BB->getParent()->getParent();
00172   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
00173   return createCallHelper(TheFn, Ops, this);
00174 }