blob: 595dea470bc32c949d4b5e60907980c6be779e24 [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
15#include "llvm/Support/IRBuilder.h"
16#include "llvm/GlobalVariable.h"
Chris Lattner49f9f762009-12-28 21:50:56 +000017#include "llvm/Function.h"
Chris Lattner143a07c2010-12-26 22:49:25 +000018#include "llvm/Intrinsics.h"
Chris Lattner7ef1cac2009-12-28 21:45:40 +000019#include "llvm/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.
26Value *IRBuilderBase::CreateGlobalString(const char *Str, const Twine &Name) {
27 Constant *StrConstant = ConstantArray::get(Context, Str, true);
28 Module &M = *BB->getParent()->getParent();
29 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
30 true, GlobalValue::InternalLinkage,
31 StrConstant, "", 0, false);
32 GV->setName(Name);
33 return GV;
34}
Chris Lattner7ef1cac2009-12-28 21:45:40 +000035
Chris Lattner49f9f762009-12-28 21:50:56 +000036const Type *IRBuilderBase::getCurrentFunctionReturnType() const {
37 assert(BB && BB->getParent() && "No current function!");
38 return BB->getParent()->getReturnType();
39}
Chris Lattner143a07c2010-12-26 22:49:25 +000040
41Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
42 const PointerType *PT = cast<PointerType>(Ptr->getType());
43 if (PT->getElementType()->isIntegerTy(8))
44 return Ptr;
45
46 // Otherwise, we need to insert a bitcast.
47 PT = getInt8PtrTy(PT->getAddressSpace());
48 BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
49 BB->getInstList().insert(InsertPt, BCI);
50 SetInstDebugLocation(BCI);
51 return BCI;
52}
53
54static CallInst *createCallHelper(Value *Callee, Value *const* Ops,
55 unsigned NumOps, IRBuilderBase *Builder) {
56 CallInst *CI = CallInst::Create(Callee, Ops, Ops + NumOps, "");
57 Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
58 Builder->SetInstDebugLocation(CI);
59 return CI;
60}
61
62
63CallInst *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) };
68 const Type *Tys[] = { Ptr->getType(), Size->getType() };
69 Module *M = BB->getParent()->getParent();
70 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys, 2);
71
72 CallInst *CI = createCallHelper(TheFn, Ops, 5, 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
81CallInst *IRBuilderBase::
82CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
83 bool isVolatile, MDNode *TBAATag) {
84 Dst = getCastedInt8PtrValue(Dst);
85 Src = getCastedInt8PtrValue(Src);
86
87 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
88 const Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
89 Module *M = BB->getParent()->getParent();
90 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys, 3);
91
92 CallInst *CI = createCallHelper(TheFn, Ops, 5, this);
93
94 // Set the TBAA info if present.
95 if (TBAATag)
96 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
97
98 return CI;
99}
100
101CallInst *IRBuilderBase::
102CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
103 bool isVolatile, MDNode *TBAATag) {
104 Dst = getCastedInt8PtrValue(Dst);
105 Src = getCastedInt8PtrValue(Src);
106
107 Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
108 const Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
109 Module *M = BB->getParent()->getParent();
110 Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys, 3);
111
112 CallInst *CI = createCallHelper(TheFn, Ops, 5, this);
113
114 // Set the TBAA info if present.
115 if (TBAATag)
116 CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
117
118 return CI;
119}