]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/MDBuilder.cpp
Upgrade Unbound to 1.6.3. More to follow.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/Function.h"
18 #include "llvm/IR/Metadata.h"
19 using namespace llvm;
20
21 MDString *MDBuilder::createString(StringRef Str) {
22   return MDString::get(Context, Str);
23 }
24
25 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
26   return ConstantAsMetadata::get(C);
27 }
28
29 MDNode *MDBuilder::createFPMath(float Accuracy) {
30   if (Accuracy == 0.0)
31     return nullptr;
32   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
33   auto *Op =
34       createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
35   return MDNode::get(Context, Op);
36 }
37
38 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
39                                        uint32_t FalseWeight) {
40   return createBranchWeights({TrueWeight, FalseWeight});
41 }
42
43 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
44   assert(Weights.size() >= 1 && "Need at least one branch weights!");
45
46   SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
47   Vals[0] = createString("branch_weights");
48
49   Type *Int32Ty = Type::getInt32Ty(Context);
50   for (unsigned i = 0, e = Weights.size(); i != e; ++i)
51     Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
52
53   return MDNode::get(Context, Vals);
54 }
55
56 MDNode *MDBuilder::createUnpredictable() {
57   return MDNode::get(Context, None);
58 }
59
60 MDNode *MDBuilder::createFunctionEntryCount(
61     uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) {
62   Type *Int64Ty = Type::getInt64Ty(Context);
63   SmallVector<Metadata *, 8> Ops;
64   Ops.push_back(createString("function_entry_count"));
65   Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
66   if (Imports) {
67     SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
68     std::stable_sort(OrderID.begin(), OrderID.end(),
69       [] (GlobalValue::GUID A, GlobalValue::GUID B) {
70         return A < B;});
71     for (auto ID : OrderID)
72       Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
73   }
74   return MDNode::get(Context, Ops);
75 }
76
77 MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
78   return MDNode::get(Context,
79                      {createString("function_section_prefix"),
80                       createString(Prefix)});
81 }
82
83 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
84   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
85
86   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
87   return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
88 }
89
90 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
91   // If the range is everything then it is useless.
92   if (Hi == Lo)
93     return nullptr;
94
95   // Return the range [Lo, Hi).
96   return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
97 }
98
99 MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
100   SmallVector<Metadata *, 4> Ops;
101   for (Function *F : Callees)
102     Ops.push_back(createConstant(F));
103   return MDNode::get(Context, Ops);
104 }
105
106 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
107   // To ensure uniqueness the root node is self-referential.
108   auto Dummy = MDNode::getTemporary(Context, None);
109
110   SmallVector<Metadata *, 3> Args(1, Dummy.get());
111   if (Extra)
112     Args.push_back(Extra);
113   if (!Name.empty())
114     Args.push_back(createString(Name));
115   MDNode *Root = MDNode::get(Context, Args);
116
117   // At this point we have
118   //   !0 = metadata !{}            <- dummy
119   //   !1 = metadata !{metadata !0} <- root
120   // Replace the dummy operand with the root node itself and delete the dummy.
121   Root->replaceOperandWith(0, Root);
122
123   // We now have
124   //   !1 = metadata !{metadata !1} <- self-referential root
125   return Root;
126 }
127
128 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
129   return MDNode::get(Context, createString(Name));
130 }
131
132 /// \brief Return metadata for a non-root TBAA node with the given name,
133 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
134 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
135                                   bool isConstant) {
136   if (isConstant) {
137     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
138     return MDNode::get(Context,
139                        {createString(Name), Parent, createConstant(Flags)});
140   }
141   return MDNode::get(Context, {createString(Name), Parent});
142 }
143
144 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
145   return MDNode::get(Context, createString(Name));
146 }
147
148 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
149   return MDNode::get(Context, {createString(Name), Domain});
150 }
151
152 /// \brief Return metadata for a tbaa.struct node with the given
153 /// struct field descriptions.
154 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
155   SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
156   Type *Int64 = Type::getInt64Ty(Context);
157   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
158     Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
159     Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
160     Vals[i * 3 + 2] = Fields[i].Type;
161   }
162   return MDNode::get(Context, Vals);
163 }
164
165 /// \brief Return metadata for a TBAA struct node in the type DAG
166 /// with the given name, a list of pairs (offset, field type in the type DAG).
167 MDNode *MDBuilder::createTBAAStructTypeNode(
168     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
169   SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
170   Type *Int64 = Type::getInt64Ty(Context);
171   Ops[0] = createString(Name);
172   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
173     Ops[i * 2 + 1] = Fields[i].first;
174     Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
175   }
176   return MDNode::get(Context, Ops);
177 }
178
179 /// \brief Return metadata for a TBAA scalar type node with the
180 /// given name, an offset and a parent in the TBAA type DAG.
181 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
182                                             uint64_t Offset) {
183   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
184   return MDNode::get(Context,
185                      {createString(Name), Parent, createConstant(Off)});
186 }
187
188 /// \brief Return metadata for a TBAA tag node with the given
189 /// base type, access type and offset relative to the base type.
190 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
191                                            uint64_t Offset, bool IsConstant) {
192   IntegerType *Int64 = Type::getInt64Ty(Context);
193   ConstantInt *Off = ConstantInt::get(Int64, Offset);
194   if (IsConstant) {
195     return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
196                                  createConstant(ConstantInt::get(Int64, 1))});
197   }
198   return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
199 }
200
201 MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
202                                       Metadata *Id,
203                                       ArrayRef<TBAAStructField> Fields) {
204   SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
205   Type *Int64 = Type::getInt64Ty(Context);
206   Ops[0] = Parent;
207   Ops[1] = createConstant(ConstantInt::get(Int64, Size));
208   Ops[2] = Id;
209   for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
210     Ops[I * 3 + 3] = Fields[I].Type;
211     Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
212     Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
213   }
214   return MDNode::get(Context, Ops);
215 }
216
217 MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
218                                        uint64_t Offset, uint64_t Size,
219                                        bool IsImmutable) {
220   IntegerType *Int64 = Type::getInt64Ty(Context);
221   auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
222   auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
223   if (IsImmutable) {
224     auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
225     return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
226                                  ImmutabilityFlagNode});
227   }
228   return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
229 }
230
231 MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
232   SmallVector<Metadata *, 2> Vals(2);
233   Vals[0] = createString("loop_header_weight");
234   Vals[1] = createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight));
235   return MDNode::get(Context, Vals);
236 }