blob: 7448e50bd82417e409b3788709044bb1f807b73f [file] [log] [blame]
Chris Lattner17079fc2009-12-28 21:28:46 +00001//===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#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"
Chris Lattner17079fc2009-12-28 21:28:46 +000020using namespace llvm;
21
22/// CreateGlobalString - Make a new global variable with an initializer that
Dan Gohman97c59022010-02-10 20:04:19 +000023/// has array of i8 type filled in with the nul terminated string value
Chris Lattner17079fc2009-12-28 21:28:46 +000024/// specified. If Name is specified, it is the name of the global variable
25/// created.
Nick Lewycky11168322011-04-12 00:29:07 +000026Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +000027 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
Chris Lattner17079fc2009-12-28 21:28:46 +000028 Module &M = *BB->getParent()->getParent();
29 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
Benjamin Kramerf1fd6e32011-12-22 14:22:14 +000030 true, GlobalValue::PrivateLinkage,
Hans Wennborgcbe34b42012-06-23 11:37:03 +000031 StrConstant);
Chris Lattner17079fc2009-12-28 21:28:46 +000032 GV->setName(Name);
Nick Lewycky561f1752011-04-07 00:14:29 +000033 GV->setUnnamedAddr(true);
Chris Lattner17079fc2009-12-28 21:28:46 +000034 return GV;
35}
Chris Lattner7ef1cac2009-12-28 21:45:40 +000036
Chris Lattnerb1907b22011-07-12 04:14:22 +000037Type *IRBuilderBase::getCurrentFunctionReturnType() const {
Chris Lattner49f9f762009-12-28 21:50:56 +000038 assert(BB && BB->getParent() && "No current function!");
39 return BB->getParent()->getReturnType();
40}
Chris Lattner143a07c2010-12-26 22:49:25 +000041
42Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
Chris Lattner229907c2011-07-18 04:54:35 +000043 PointerType *PT = cast<PointerType>(Ptr->getType());
Chris Lattner143a07c2010-12-26 22:49:25 +000044 if (PT->getElementType()->isIntegerTy(8))
45 return Ptr;
46
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);
52 return BCI;
53}
54
Jay Foad5bd375a2011-07-15 08:37:34 +000055static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
56 IRBuilderBase *Builder) {
57 CallInst *CI = CallInst::Create(Callee, Ops, "");
Chris Lattner143a07c2010-12-26 22:49:25 +000058 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
59 Builder->SetInstDebugLocation(CI);
60 return CI;
61}
62
Chris Lattner143a07c2010-12-26 22:49:25 +000063CallInst *IRBuilderBase::
64CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +000065 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
66 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +000067 Ptr = getCastedInt8PtrValue(Ptr);
68 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +000069 Type *Tys[] = { Ptr->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +000070 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +000071 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +000072
Jay Foad5bd375a2011-07-15 08:37:34 +000073 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +000074
75 // Set the TBAA info if present.
76 if (TBAATag)
77 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +000078
79 if (ScopeTag)
80 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
81
82 if (NoAliasTag)
83 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
84
Chris Lattner143a07c2010-12-26 22:49:25 +000085 return CI;
86}
87
88CallInst *IRBuilderBase::
89CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +000090 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
91 MDNode *ScopeTag, MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +000092 Dst = getCastedInt8PtrValue(Dst);
93 Src = getCastedInt8PtrValue(Src);
94
95 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +000096 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +000097 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +000098 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +000099
Jay Foad5bd375a2011-07-15 08:37:34 +0000100 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000101
102 // Set the TBAA info if present.
103 if (TBAATag)
104 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Dan Gohman099727f2012-09-26 22:17:14 +0000105
106 // Set the TBAA Struct info if present.
107 if (TBAAStructTag)
108 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
Hal Finkel94146652014-07-24 14:25:39 +0000109
110 if (ScopeTag)
111 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
112
113 if (NoAliasTag)
114 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
115
Chris Lattner143a07c2010-12-26 22:49:25 +0000116 return CI;
117}
118
119CallInst *IRBuilderBase::
120CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
Hal Finkel94146652014-07-24 14:25:39 +0000121 bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
122 MDNode *NoAliasTag) {
Chris Lattner143a07c2010-12-26 22:49:25 +0000123 Dst = getCastedInt8PtrValue(Dst);
124 Src = getCastedInt8PtrValue(Src);
125
126 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foadb804a2b2011-07-12 14:06:48 +0000127 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattner143a07c2010-12-26 22:49:25 +0000128 Module *M = BB->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000129 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
Chris Lattner143a07c2010-12-26 22:49:25 +0000130
Jay Foad5bd375a2011-07-15 08:37:34 +0000131 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattner143a07c2010-12-26 22:49:25 +0000132
133 // Set the TBAA info if present.
134 if (TBAATag)
135 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Hal Finkel94146652014-07-24 14:25:39 +0000136
137 if (ScopeTag)
138 CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
139
140 if (NoAliasTag)
141 CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
142
Chris Lattner143a07c2010-12-26 22:49:25 +0000143 return CI;
144}
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000145
146CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
147 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000148 "lifetime.start only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000149 Ptr = getCastedInt8PtrValue(Ptr);
150 if (!Size)
151 Size = getInt64(-1);
152 else
153 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000154 "lifetime.start requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000155 Value *Ops[] = { Size, Ptr };
156 Module *M = BB->getParent()->getParent();
157 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
Jay Foad5bd375a2011-07-15 08:37:34 +0000158 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000159}
160
161CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
162 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000163 "lifetime.end only applies to pointers.");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000164 Ptr = getCastedInt8PtrValue(Ptr);
165 if (!Size)
166 Size = getInt64(-1);
167 else
168 assert(Size->getType() == getInt64Ty() &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000169 "lifetime.end requires the size to be an i64");
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000170 Value *Ops[] = { Size, Ptr };
171 Module *M = BB->getParent()->getParent();
172 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
Jay Foad5bd375a2011-07-15 08:37:34 +0000173 return createCallHelper(TheFn, Ops, this);
Nick Lewyckybabca9a2011-05-21 23:14:36 +0000174}