Add scoped-noalias metadata
[oota-llvm.git] / lib / IR / MDBuilder.cpp
1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
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 defines the MDBuilder class, which is used as a convenient way to
11 // create LLVM metadata with a consistent and simplified interface.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/IR/MDBuilder.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Metadata.h"
18 using namespace llvm;
19
20 MDString *MDBuilder::createString(StringRef Str) {
21   return MDString::get(Context, Str);
22 }
23
24 MDNode *MDBuilder::createFPMath(float Accuracy) {
25   if (Accuracy == 0.0)
26     return nullptr;
27   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
28   Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
29   return MDNode::get(Context, Op);
30 }
31
32 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
33                                        uint32_t FalseWeight) {
34   uint32_t Weights[] = {TrueWeight, FalseWeight};
35   return createBranchWeights(Weights);
36 }
37
38 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
39   assert(Weights.size() >= 2 && "Need at least two branch weights!");
40
41   SmallVector<Value *, 4> Vals(Weights.size() + 1);
42   Vals[0] = createString("branch_weights");
43
44   Type *Int32Ty = Type::getInt32Ty(Context);
45   for (unsigned i = 0, e = Weights.size(); i != e; ++i)
46     Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]);
47
48   return MDNode::get(Context, Vals);
49 }
50
51 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
52   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
53   // If the range is everything then it is useless.
54   if (Hi == Lo)
55     return nullptr;
56
57   // Return the range [Lo, Hi).
58   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
59   Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)};
60   return MDNode::get(Context, Range);
61 }
62
63 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name) {
64   // To ensure uniqueness the root node is self-referential.
65   MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
66
67   SmallVector<Value *, 2> Args(1, Dummy);
68   if (!Name.empty())
69     Args.push_back(createString(Name));
70   MDNode *Root = MDNode::get(Context, Args);
71
72   // At this point we have
73   //   !0 = metadata !{}            <- dummy
74   //   !1 = metadata !{metadata !0} <- root
75   // Replace the dummy operand with the root node itself and delete the dummy.
76   Root->replaceOperandWith(0, Root);
77   MDNode::deleteTemporary(Dummy);
78   // We now have
79   //   !1 = metadata !{metadata !1} <- self-referential root
80   return Root;
81 }
82
83 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
84   return MDNode::get(Context, createString(Name));
85 }
86
87 /// \brief Return metadata for a non-root TBAA node with the given name,
88 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
89 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
90                                   bool isConstant) {
91   if (isConstant) {
92     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
93     Value *Ops[3] = {createString(Name), Parent, Flags};
94     return MDNode::get(Context, Ops);
95   } else {
96     Value *Ops[2] = {createString(Name), Parent};
97     return MDNode::get(Context, Ops);
98   }
99 }
100
101 MDNode *MDBuilder::createAliasScopeRoot(StringRef Name) {
102   return MDNode::get(Context, createString(Name));
103 }
104
105 MDNode *MDBuilder::createAliasScopeNode(StringRef Name, MDNode *Parent) {
106   Value *Ops[2] = { createString(Name), Parent };
107   return MDNode::get(Context, Ops);
108 }
109
110 /// \brief Return metadata for a tbaa.struct node with the given
111 /// struct field descriptions.
112 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
113   SmallVector<Value *, 4> Vals(Fields.size() * 3);
114   Type *Int64 = Type::getInt64Ty(Context);
115   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
116     Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
117     Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
118     Vals[i * 3 + 2] = Fields[i].TBAA;
119   }
120   return MDNode::get(Context, Vals);
121 }
122
123 /// \brief Return metadata for a TBAA struct node in the type DAG
124 /// with the given name, a list of pairs (offset, field type in the type DAG).
125 MDNode *MDBuilder::createTBAAStructTypeNode(
126     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
127   SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
128   Type *Int64 = Type::getInt64Ty(Context);
129   Ops[0] = createString(Name);
130   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
131     Ops[i * 2 + 1] = Fields[i].first;
132     Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
133   }
134   return MDNode::get(Context, Ops);
135 }
136
137 /// \brief Return metadata for a TBAA scalar type node with the
138 /// given name, an offset and a parent in the TBAA type DAG.
139 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
140                                             uint64_t Offset) {
141   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
142   Value *Ops[3] = {createString(Name), Parent, Off};
143   return MDNode::get(Context, Ops);
144 }
145
146 /// \brief Return metadata for a TBAA tag node with the given
147 /// base type, access type and offset relative to the base type.
148 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
149                                            uint64_t Offset) {
150   Type *Int64 = Type::getInt64Ty(Context);
151   Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
152   return MDNode::get(Context, Ops);
153 }