Set unnamed_addr on strings created through the IRBuilder.
[oota-llvm.git] / lib / VMCore / IRBuilder.cpp
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"
17 #include "llvm/Function.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/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.
26 Value *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   GV->setUnnamedAddr(true);
34   return GV;
35 }
36
37 const Type *IRBuilderBase::getCurrentFunctionReturnType() const {
38   assert(BB && BB->getParent() && "No current function!");
39   return BB->getParent()->getReturnType();
40 }
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
55 static CallInst *createCallHelper(Value *Callee, Value *const* Ops,
56                                   unsigned NumOps, IRBuilderBase *Builder) {
57   CallInst *CI = CallInst::Create(Callee, Ops, Ops + NumOps, "");
58   Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
59   Builder->SetInstDebugLocation(CI);
60   return CI;  
61 }
62
63
64 CallInst *IRBuilderBase::
65 CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
66              bool isVolatile, MDNode *TBAATag) {
67   Ptr = getCastedInt8PtrValue(Ptr);
68   Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
69   const Type *Tys[] = { Ptr->getType(), Size->getType() };
70   Module *M = BB->getParent()->getParent();
71   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys, 2);
72   
73   CallInst *CI = createCallHelper(TheFn, Ops, 5, this);
74   
75   // Set the TBAA info if present.
76   if (TBAATag)
77     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
78   
79   return CI;
80 }
81
82 CallInst *IRBuilderBase::
83 CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
84              bool isVolatile, MDNode *TBAATag) {
85   Dst = getCastedInt8PtrValue(Dst);
86   Src = getCastedInt8PtrValue(Src);
87
88   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
89   const Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
90   Module *M = BB->getParent()->getParent();
91   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys, 3);
92   
93   CallInst *CI = createCallHelper(TheFn, Ops, 5, this);
94   
95   // Set the TBAA info if present.
96   if (TBAATag)
97     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
98   
99   return CI;  
100 }
101
102 CallInst *IRBuilderBase::
103 CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
104               bool isVolatile, MDNode *TBAATag) {
105   Dst = getCastedInt8PtrValue(Dst);
106   Src = getCastedInt8PtrValue(Src);
107   
108   Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
109   const Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
110   Module *M = BB->getParent()->getParent();
111   Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys, 3);
112   
113   CallInst *CI = createCallHelper(TheFn, Ops, 5, this);
114   
115   // Set the TBAA info if present.
116   if (TBAATag)
117     CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
118   
119   return CI;  
120 }