blob: 435e54f0ea2a6de83571d4eb8290e2f9ef4af41b [file] [log] [blame]
Chris Lattnera53cfd12009-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 Carruth0b8c9a82013-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 Lattnera53cfd12009-12-28 21:28:46 +000020using namespace llvm;
21
22/// CreateGlobalString - Make a new global variable with an initializer that
Dan Gohmanf3b11aa2010-02-10 20:04:19 +000023/// has array of i8 type filled in with the nul terminated string value
Chris Lattnera53cfd12009-12-28 21:28:46 +000024/// specified. If Name is specified, it is the name of the global variable
25/// created.
Nick Lewyckycb4e4ea2011-04-12 00:29:07 +000026Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) {
Chris Lattner18c7f802012-02-05 02:29:43 +000027 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
Chris Lattnera53cfd12009-12-28 21:28:46 +000028 Module &M = *BB->getParent()->getParent();
29 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
Benjamin Kramerb143ea32011-12-22 14:22:14 +000030 true, GlobalValue::PrivateLinkage,
Hans Wennborgce718ff2012-06-23 11:37:03 +000031 StrConstant);
Chris Lattnera53cfd12009-12-28 21:28:46 +000032 GV->setName(Name);
Nick Lewycky84025ba2011-04-07 00:14:29 +000033 GV->setUnnamedAddr(true);
Chris Lattnera53cfd12009-12-28 21:28:46 +000034 return GV;
35}
Chris Lattner43469b62009-12-28 21:45:40 +000036
Chris Lattnera17ce802011-07-12 04:14:22 +000037Type *IRBuilderBase::getCurrentFunctionReturnType() const {
Chris Lattner2ad32752009-12-28 21:50:56 +000038 assert(BB && BB->getParent() && "No current function!");
39 return BB->getParent()->getReturnType();
40}
Chris Lattnerc0d54962010-12-26 22:49:25 +000041
42Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000043 PointerType *PT = cast<PointerType>(Ptr->getType());
Chris Lattnerc0d54962010-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 Foada3efbb12011-07-15 08:37:34 +000055static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
56 IRBuilderBase *Builder) {
57 CallInst *CI = CallInst::Create(Callee, Ops, "");
Chris Lattnerc0d54962010-12-26 22:49:25 +000058 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
59 Builder->SetInstDebugLocation(CI);
60 return CI;
61}
62
Chris Lattnerc0d54962010-12-26 22:49:25 +000063CallInst *IRBuilderBase::
64CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
65 bool isVolatile, MDNode *TBAATag) {
66 Ptr = getCastedInt8PtrValue(Ptr);
67 Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foad5fdd6c82011-07-12 14:06:48 +000068 Type *Tys[] = { Ptr->getType(), Size->getType() };
Chris Lattnerc0d54962010-12-26 22:49:25 +000069 Module *M = BB->getParent()->getParent();
Benjamin Kramereb9a85f2011-07-14 17:45:39 +000070 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
Chris Lattnerc0d54962010-12-26 22:49:25 +000071
Jay Foada3efbb12011-07-15 08:37:34 +000072 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattnerc0d54962010-12-26 22:49:25 +000073
74 // Set the TBAA info if present.
75 if (TBAATag)
76 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
77
78 return CI;
79}
80
81CallInst *IRBuilderBase::
82CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
Dan Gohman8a63f992012-09-26 22:17:14 +000083 bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag) {
Chris Lattnerc0d54962010-12-26 22:49:25 +000084 Dst = getCastedInt8PtrValue(Dst);
85 Src = getCastedInt8PtrValue(Src);
86
87 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foad5fdd6c82011-07-12 14:06:48 +000088 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattnerc0d54962010-12-26 22:49:25 +000089 Module *M = BB->getParent()->getParent();
Benjamin Kramereb9a85f2011-07-14 17:45:39 +000090 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
Chris Lattnerc0d54962010-12-26 22:49:25 +000091
Jay Foada3efbb12011-07-15 08:37:34 +000092 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattnerc0d54962010-12-26 22:49:25 +000093
94 // Set the TBAA info if present.
95 if (TBAATag)
96 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
Dan Gohman8a63f992012-09-26 22:17:14 +000097
98 // Set the TBAA Struct info if present.
99 if (TBAAStructTag)
100 CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
Chris Lattnerc0d54962010-12-26 22:49:25 +0000101
102 return CI;
103}
104
105CallInst *IRBuilderBase::
106CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
107 bool isVolatile, MDNode *TBAATag) {
108 Dst = getCastedInt8PtrValue(Dst);
109 Src = getCastedInt8PtrValue(Src);
110
111 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
Jay Foad5fdd6c82011-07-12 14:06:48 +0000112 Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
Chris Lattnerc0d54962010-12-26 22:49:25 +0000113 Module *M = BB->getParent()->getParent();
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000114 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
Chris Lattnerc0d54962010-12-26 22:49:25 +0000115
Jay Foada3efbb12011-07-15 08:37:34 +0000116 CallInst *CI = createCallHelper(TheFn, Ops, this);
Chris Lattnerc0d54962010-12-26 22:49:25 +0000117
118 // Set the TBAA info if present.
119 if (TBAATag)
120 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
121
122 return CI;
123}
Nick Lewycky0cf51562011-05-21 23:14:36 +0000124
125CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
126 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendling56cb2292012-07-19 00:11:40 +0000127 "lifetime.start only applies to pointers.");
Nick Lewycky0cf51562011-05-21 23:14:36 +0000128 Ptr = getCastedInt8PtrValue(Ptr);
129 if (!Size)
130 Size = getInt64(-1);
131 else
132 assert(Size->getType() == getInt64Ty() &&
Bill Wendling56cb2292012-07-19 00:11:40 +0000133 "lifetime.start requires the size to be an i64");
Nick Lewycky0cf51562011-05-21 23:14:36 +0000134 Value *Ops[] = { Size, Ptr };
135 Module *M = BB->getParent()->getParent();
136 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
Jay Foada3efbb12011-07-15 08:37:34 +0000137 return createCallHelper(TheFn, Ops, this);
Nick Lewycky0cf51562011-05-21 23:14:36 +0000138}
139
140CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
141 assert(isa<PointerType>(Ptr->getType()) &&
Bill Wendling56cb2292012-07-19 00:11:40 +0000142 "lifetime.end only applies to pointers.");
Nick Lewycky0cf51562011-05-21 23:14:36 +0000143 Ptr = getCastedInt8PtrValue(Ptr);
144 if (!Size)
145 Size = getInt64(-1);
146 else
147 assert(Size->getType() == getInt64Ty() &&
Bill Wendling56cb2292012-07-19 00:11:40 +0000148 "lifetime.end requires the size to be an i64");
Nick Lewycky0cf51562011-05-21 23:14:36 +0000149 Value *Ops[] = { Size, Ptr };
150 Module *M = BB->getParent()->getParent();
151 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
Jay Foada3efbb12011-07-15 08:37:34 +0000152 return createCallHelper(TheFn, Ops, this);
Nick Lewycky0cf51562011-05-21 23:14:36 +0000153}