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