Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 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" |
Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | /// CreateGlobalString - Make a new global variable with an initializer that |
Dan Gohman | f3b11aa | 2010-02-10 20:04:19 +0000 | [diff] [blame] | 23 | /// has array of i8 type filled in with the nul terminated string value |
Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 24 | /// specified. If Name is specified, it is the name of the global variable |
| 25 | /// created. |
Nick Lewycky | cb4e4ea | 2011-04-12 00:29:07 +0000 | [diff] [blame] | 26 | Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) { |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 27 | Constant *StrConstant = ConstantDataArray::getString(Context, Str); |
Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 28 | Module &M = *BB->getParent()->getParent(); |
| 29 | GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(), |
Benjamin Kramer | b143ea3 | 2011-12-22 14:22:14 +0000 | [diff] [blame] | 30 | true, GlobalValue::PrivateLinkage, |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 31 | StrConstant); |
Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 32 | GV->setName(Name); |
Nick Lewycky | 84025ba | 2011-04-07 00:14:29 +0000 | [diff] [blame] | 33 | GV->setUnnamedAddr(true); |
Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 34 | return GV; |
| 35 | } |
Chris Lattner | 43469b6 | 2009-12-28 21:45:40 +0000 | [diff] [blame] | 36 | |
Chris Lattner | a17ce80 | 2011-07-12 04:14:22 +0000 | [diff] [blame] | 37 | Type *IRBuilderBase::getCurrentFunctionReturnType() const { |
Chris Lattner | 2ad3275 | 2009-12-28 21:50:56 +0000 | [diff] [blame] | 38 | assert(BB && BB->getParent() && "No current function!"); |
| 39 | return BB->getParent()->getReturnType(); |
| 40 | } |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 41 | |
| 42 | Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 43 | PointerType *PT = cast<PointerType>(Ptr->getType()); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 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); |
| 51 | SetInstDebugLocation(BCI); |
| 52 | return BCI; |
| 53 | } |
| 54 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 55 | static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops, |
| 56 | IRBuilderBase *Builder) { |
| 57 | CallInst *CI = CallInst::Create(Callee, Ops, ""); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 58 | Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI); |
| 59 | Builder->SetInstDebugLocation(CI); |
| 60 | return CI; |
| 61 | } |
| 62 | |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 63 | CallInst *IRBuilderBase:: |
| 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) }; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 68 | Type *Tys[] = { Ptr->getType(), Size->getType() }; |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 69 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 70 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 71 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 72 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 73 | |
| 74 | // Set the TBAA info if present. |
| 75 | if (TBAATag) |
| 76 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 77 | |
| 78 | return CI; |
| 79 | } |
| 80 | |
| 81 | CallInst *IRBuilderBase:: |
| 82 | CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, |
Dan Gohman | 8a63f99 | 2012-09-26 22:17:14 +0000 | [diff] [blame] | 83 | bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag) { |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 84 | Dst = getCastedInt8PtrValue(Dst); |
| 85 | Src = getCastedInt8PtrValue(Src); |
| 86 | |
| 87 | Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) }; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 88 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 89 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 90 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 91 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 92 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 93 | |
| 94 | // Set the TBAA info if present. |
| 95 | if (TBAATag) |
| 96 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Dan Gohman | 8a63f99 | 2012-09-26 22:17:14 +0000 | [diff] [blame] | 97 | |
| 98 | // Set the TBAA Struct info if present. |
| 99 | if (TBAAStructTag) |
| 100 | CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 101 | |
| 102 | return CI; |
| 103 | } |
| 104 | |
| 105 | CallInst *IRBuilderBase:: |
| 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) }; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 112 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 113 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 114 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 115 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 116 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | c0d5496 | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 117 | |
| 118 | // Set the TBAA info if present. |
| 119 | if (TBAATag) |
| 120 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 121 | |
| 122 | return CI; |
| 123 | } |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 124 | |
| 125 | CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) { |
| 126 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 127 | "lifetime.start only applies to pointers."); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 128 | Ptr = getCastedInt8PtrValue(Ptr); |
| 129 | if (!Size) |
| 130 | Size = getInt64(-1); |
| 131 | else |
| 132 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 133 | "lifetime.start requires the size to be an i64"); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 134 | Value *Ops[] = { Size, Ptr }; |
| 135 | Module *M = BB->getParent()->getParent(); |
| 136 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 137 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { |
| 141 | assert(isa<PointerType>(Ptr->getType()) && |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 142 | "lifetime.end only applies to pointers."); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 143 | Ptr = getCastedInt8PtrValue(Ptr); |
| 144 | if (!Size) |
| 145 | Size = getInt64(-1); |
| 146 | else |
| 147 | assert(Size->getType() == getInt64Ty() && |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 148 | "lifetime.end requires the size to be an i64"); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 149 | Value *Ops[] = { Size, Ptr }; |
| 150 | Module *M = BB->getParent()->getParent(); |
| 151 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 152 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 153 | } |