Chris Lattner | 17079fc | 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 | |
| 15 | #include "llvm/Support/IRBuilder.h" |
| 16 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 49f9f76 | 2009-12-28 21:50:56 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 18 | #include "llvm/Intrinsics.h" |
Chris Lattner | 7ef1cac | 2009-12-28 21:45:40 +0000 | [diff] [blame] | 19 | #include "llvm/LLVMContext.h" |
Chris Lattner | 17079fc | 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 | 97c5902 | 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 | 17079fc | 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 | 1116832 | 2011-04-12 00:29:07 +0000 | [diff] [blame] | 26 | Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) { |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 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); |
Nick Lewycky | 561f175 | 2011-04-07 00:14:29 +0000 | [diff] [blame] | 33 | GV->setUnnamedAddr(true); |
Chris Lattner | 17079fc | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 34 | return GV; |
| 35 | } |
Chris Lattner | 7ef1cac | 2009-12-28 21:45:40 +0000 | [diff] [blame] | 36 | |
Chris Lattner | b1907b2 | 2011-07-12 04:14:22 +0000 | [diff] [blame] | 37 | Type *IRBuilderBase::getCurrentFunctionReturnType() const { |
Chris Lattner | 49f9f76 | 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 | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 41 | |
| 42 | Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { |
| 43 | const 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); |
| 51 | SetInstDebugLocation(BCI); |
| 52 | return BCI; |
| 53 | } |
| 54 | |
Jay Foad | 5bd375a | 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 | 143a07c | 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 | 143a07c | 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 | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 68 | Type *Tys[] = { Ptr->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 69 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 70 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 71 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame^] | 72 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | 143a07c | 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, |
| 83 | bool isVolatile, MDNode *TBAATag) { |
| 84 | Dst = getCastedInt8PtrValue(Dst); |
| 85 | Src = getCastedInt8PtrValue(Src); |
| 86 | |
| 87 | Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) }; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 88 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 89 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 90 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 91 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame^] | 92 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | 143a07c | 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); |
| 97 | |
| 98 | return CI; |
| 99 | } |
| 100 | |
| 101 | CallInst *IRBuilderBase:: |
| 102 | CreateMemMove(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) }; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 108 | Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() }; |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 109 | Module *M = BB->getParent()->getParent(); |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 110 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 111 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame^] | 112 | CallInst *CI = createCallHelper(TheFn, Ops, this); |
Chris Lattner | 143a07c | 2010-12-26 22:49:25 +0000 | [diff] [blame] | 113 | |
| 114 | // Set the TBAA info if present. |
| 115 | if (TBAATag) |
| 116 | CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 117 | |
| 118 | return CI; |
| 119 | } |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 120 | |
| 121 | CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) { |
| 122 | assert(isa<PointerType>(Ptr->getType()) && |
| 123 | "lifetime.start only applies to pointers."); |
| 124 | Ptr = getCastedInt8PtrValue(Ptr); |
| 125 | if (!Size) |
| 126 | Size = getInt64(-1); |
| 127 | else |
| 128 | assert(Size->getType() == getInt64Ty() && |
| 129 | "lifetime.start requires the size to be an i64"); |
| 130 | Value *Ops[] = { Size, Ptr }; |
| 131 | Module *M = BB->getParent()->getParent(); |
| 132 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame^] | 133 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { |
| 137 | assert(isa<PointerType>(Ptr->getType()) && |
| 138 | "lifetime.end only applies to pointers."); |
| 139 | Ptr = getCastedInt8PtrValue(Ptr); |
| 140 | if (!Size) |
| 141 | Size = getInt64(-1); |
| 142 | else |
| 143 | assert(Size->getType() == getInt64Ty() && |
| 144 | "lifetime.end requires the size to be an i64"); |
| 145 | Value *Ops[] = { Size, Ptr }; |
| 146 | Module *M = BB->getParent()->getParent(); |
| 147 | Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame^] | 148 | return createCallHelper(TheFn, Ops, this); |
Nick Lewycky | babca9a | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 149 | } |