LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IRBuilder.cpp
Go to the documentation of this file.
1 //===---- 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 
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"
20 using namespace llvm;
21 
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
25 /// created.
27  Constant *StrConstant = ConstantDataArray::getString(Context, Str);
28  Module &M = *BB->getParent()->getParent();
29  GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
31  StrConstant);
32  GV->setName(Name);
33  GV->setUnnamedAddr(true);
34  return GV;
35 }
36 
38  assert(BB && BB->getParent() && "No current function!");
39  return BB->getParent()->getReturnType();
40 }
41 
42 Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
43  PointerType *PT = cast<PointerType>(Ptr->getType());
44  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);
52  return BCI;
53 }
54 
56  IRBuilderBase *Builder) {
57  CallInst *CI = CallInst::Create(Callee, Ops, "");
58  Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
59  Builder->SetInstDebugLocation(CI);
60  return CI;
61 }
62 
64 CreateMemSet(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) };
68  Type *Tys[] = { Ptr->getType(), Size->getType() };
69  Module *M = BB->getParent()->getParent();
71 
72  CallInst *CI = createCallHelper(TheFn, Ops, this);
73 
74  // Set the TBAA info if present.
75  if (TBAATag)
76  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
77 
78  return CI;
79 }
80 
82 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
83  bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag) {
84  Dst = getCastedInt8PtrValue(Dst);
85  Src = getCastedInt8PtrValue(Src);
86 
87  Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
88  Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
89  Module *M = BB->getParent()->getParent();
91 
92  CallInst *CI = createCallHelper(TheFn, Ops, this);
93 
94  // Set the TBAA info if present.
95  if (TBAATag)
96  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
97 
98  // Set the TBAA Struct info if present.
99  if (TBAAStructTag)
100  CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
101 
102  return CI;
103 }
104 
106 CreateMemMove(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) };
112  Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
113  Module *M = BB->getParent()->getParent();
115 
116  CallInst *CI = createCallHelper(TheFn, Ops, this);
117 
118  // Set the TBAA info if present.
119  if (TBAATag)
120  CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
121 
122  return CI;
123 }
124 
126  assert(isa<PointerType>(Ptr->getType()) &&
127  "lifetime.start only applies to pointers.");
128  Ptr = getCastedInt8PtrValue(Ptr);
129  if (!Size)
130  Size = getInt64(-1);
131  else
132  assert(Size->getType() == getInt64Ty() &&
133  "lifetime.start requires the size to be an i64");
134  Value *Ops[] = { Size, Ptr };
135  Module *M = BB->getParent()->getParent();
137  return createCallHelper(TheFn, Ops, this);
138 }
139 
141  assert(isa<PointerType>(Ptr->getType()) &&
142  "lifetime.end only applies to pointers.");
143  Ptr = getCastedInt8PtrValue(Ptr);
144  if (!Size)
145  Size = getInt64(-1);
146  else
147  assert(Size->getType() == getInt64Ty() &&
148  "lifetime.end requires the size to be an i64");
149  Value *Ops[] = { Size, Ptr };
150  Module *M = BB->getParent()->getParent();
152  return createCallHelper(TheFn, Ops, this);
153 }
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:50
IntegerType * getType() const
Definition: Constants.h:139
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
Definition: Constants.cpp:2357
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:78
static CallInst * createCallHelper(Value *Callee, ArrayRef< Value * > Ops, IRBuilderBase *Builder)
Definition: IRBuilder.cpp:55
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:42
Type * getReturnType() const
Definition: Function.cpp:179
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:116
MDNode - a tuple of other values.
Definition: Metadata.h:69
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:445
CallInst * CreateLifetimeEnd(Value *Ptr, ConstantInt *Size=0)
Create a lifetime.end intrinsic.
Definition: IRBuilder.cpp:140
LLVMContext & Context
Definition: IRBuilder.h:55
CallInst * CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=0)
Create and insert a memmove between the specified pointers.
Definition: IRBuilder.h:381
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:315
void setName(const Twine &Name)
Definition: Value.cpp:175
BasicBlock * BB
Definition: IRBuilder.h:53
This class represents a no-op cast from one type to another.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:683
Type * getElementType() const
Definition: DerivedTypes.h:319
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:251
Type * getCurrentFunctionReturnType() const
Get the return type of the current function that we're emitting into.
Definition: IRBuilder.cpp:37
LLVM Constant Representation.
Definition: Constant.h:41
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:214
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:281
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:335
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:589
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:111
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:85
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:276
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:77
bool isIntegerTy() const
Definition: Type.h:196
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=0)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:353
Module * getParent()
Definition: GlobalValue.h:286
LLVM Value Representation.
Definition: Value.h:66
CallInst * CreateLifetimeStart(Value *Ptr, ConstantInt *Size=0)
Create a lifetime.start intrinsic.
Definition: IRBuilder.cpp:125
Value * CreateGlobalString(StringRef Str, const Twine &Name="")
Make a new global variable with initializer type i8*.
Definition: IRBuilder.cpp:26
BasicBlock::iterator InsertPt
Definition: IRBuilder.h:54
CallInst * CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=0, MDNode *TBAAStructTag=0)
Create and insert a memcpy between the specified pointers.
Definition: IRBuilder.h:365
void SetInstDebugLocation(Instruction *I) const
If this builder has a current debug location, set it on the specified instruction.
Definition: IRBuilder.h:127