1 //===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
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
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,
33 GV->setUnnamedAddr(true);
37 Type *IRBuilderBase::getCurrentFunctionReturnType() const {
38 assert(BB && BB->getParent() && "No current function!");
39 return BB->getParent()->getReturnType();
42 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
43 PointerType *PT = cast<PointerType>(Ptr->getType());
44 if (PT->getElementType()->isIntegerTy(8))
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);
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);
63 CallInst *IRBuilderBase::
64 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
65 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
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);
73 CallInst *CI = createCallHelper(TheFn, Ops, this);
75 // Set the TBAA info if present.
77 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
80 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
83 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
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);
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);
100 CallInst *CI = createCallHelper(TheFn, Ops, this);
102 // Set the TBAA info if present.
104 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
106 // Set the TBAA Struct info if present.
108 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
111 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
114 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
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);
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);
131 CallInst *CI = createCallHelper(TheFn, Ops, this);
133 // Set the TBAA info if present.
135 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
138 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
141 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
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);
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);
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);
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);