1//===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the MDBuilder class, which is used as a convenient way to
10// create LLVM metadata with a consistent and simplified interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_MDBUILDER_H
15#define LLVM_IR_MDBUILDER_H
16
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/Support/DataTypes.h"
22#include <utility>
23
24namespace llvm {
25
26class APInt;
27template <typename T> class ArrayRef;
28class LLVMContext;
29class Constant;
30class ConstantAsMetadata;
31class Function;
32class MDNode;
33class MDString;
34class Metadata;
35
36class MDBuilder {
37 LLVMContext &Context;
38
39public:
40 MDBuilder(LLVMContext &context) : Context(context) {}
41
42 /// Return the given string as metadata.
43 MDString *createString(StringRef Str);
44
45 /// Return the given constant as metadata.
46 ConstantAsMetadata *createConstant(Constant *C);
47
48 //===------------------------------------------------------------------===//
49 // FPMath metadata.
50 //===------------------------------------------------------------------===//
51
52 /// Return metadata with the given settings. The special value 0.0
53 /// for the Accuracy parameter indicates the default (maximal precision)
54 /// setting.
55 MDNode *createFPMath(float Accuracy);
56
57 //===------------------------------------------------------------------===//
58 // Prof metadata.
59 //===------------------------------------------------------------------===//
60
61 /// Return metadata containing two branch weights.
62 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
63
64 /// Return metadata containing a number of branch weights.
65 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
66
67 /// Return metadata specifying that a branch or switch is unpredictable.
68 MDNode *createUnpredictable();
69
70 /// Return metadata containing the entry \p Count for a function, a boolean
71 /// \Synthetic indicating whether the counts were synthetized, and the
72 /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
73 /// enable the same inlines as the profiled optimized binary
74 MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
75 const DenseSet<GlobalValue::GUID> *Imports);
76
77 /// Return metadata containing the section prefix for a function.
78 MDNode *createFunctionSectionPrefix(StringRef Prefix);
79
80 /// Return metadata containing the pseudo probe descriptor for a function.
81 MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName);
82
83 /// Return metadata containing llvm statistics.
84 MDNode *
85 createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStatsVec);
86
87 //===------------------------------------------------------------------===//
88 // Range metadata.
89 //===------------------------------------------------------------------===//
90
91 /// Return metadata describing the range [Lo, Hi).
92 MDNode *createRange(const APInt &Lo, const APInt &Hi);
93
94 /// Return metadata describing the range [Lo, Hi).
95 MDNode *createRange(Constant *Lo, Constant *Hi);
96
97 //===------------------------------------------------------------------===//
98 // Callees metadata.
99 //===------------------------------------------------------------------===//
100
101 /// Return metadata indicating the possible callees of indirect
102 /// calls.
103 MDNode *createCallees(ArrayRef<Function *> Callees);
104
105 //===------------------------------------------------------------------===//
106 // Callback metadata.
107 //===------------------------------------------------------------------===//
108
109 /// Return metadata describing a callback (see llvm::AbstractCallSite).
110 MDNode *createCallbackEncoding(unsigned CalleeArgNo, ArrayRef<int> Arguments,
111 bool VarArgsArePassed);
112
113 /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
114 MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB);
115
116 /// Return metadata feeding to the CodeGen about how to generate a function
117 /// prologue for the "function" santizier.
118 MDNode *createRTTIPointerPrologue(Constant *PrologueSig, Constant *RTTI);
119
120 //===------------------------------------------------------------------===//
121 // PC sections metadata.
122 //===------------------------------------------------------------------===//
123
124 /// A pair of PC section name with auxilliary constant data.
125 using PCSection = std::pair<StringRef, SmallVector<Constant *>>;
126
127 /// Return metadata for PC sections.
128 MDNode *createPCSections(ArrayRef<PCSection> Sections);
129
130 //===------------------------------------------------------------------===//
131 // AA metadata.
132 //===------------------------------------------------------------------===//
133
134protected:
135 /// Return metadata appropriate for a AA root node (scope or TBAA).
136 /// Each returned node is distinct from all other metadata and will never
137 /// be identified (uniqued) with anything else.
138 MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
139 MDNode *Extra = nullptr);
140
141public:
142 /// Return metadata appropriate for a TBAA root node. Each returned
143 /// node is distinct from all other metadata and will never be identified
144 /// (uniqued) with anything else.
145 MDNode *createAnonymousTBAARoot() {
146 return createAnonymousAARoot();
147 }
148
149 /// Return metadata appropriate for an alias scope domain node.
150 /// Each returned node is distinct from all other metadata and will never
151 /// be identified (uniqued) with anything else.
152 MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
153 return createAnonymousAARoot(Name);
154 }
155
156 /// Return metadata appropriate for an alias scope root node.
157 /// Each returned node is distinct from all other metadata and will never
158 /// be identified (uniqued) with anything else.
159 MDNode *createAnonymousAliasScope(MDNode *Domain,
160 StringRef Name = StringRef()) {
161 return createAnonymousAARoot(Name, Extra: Domain);
162 }
163
164 /// Return metadata appropriate for a TBAA root node with the given
165 /// name. This may be identified (uniqued) with other roots with the same
166 /// name.
167 MDNode *createTBAARoot(StringRef Name);
168
169 /// Return metadata appropriate for an alias scope domain node with
170 /// the given name. This may be identified (uniqued) with other roots with
171 /// the same name.
172 MDNode *createAliasScopeDomain(StringRef Name);
173
174 /// Return metadata appropriate for an alias scope node with
175 /// the given name. This may be identified (uniqued) with other scopes with
176 /// the same name and domain.
177 MDNode *createAliasScope(StringRef Name, MDNode *Domain);
178
179 /// Return metadata for a non-root TBAA node with the given name,
180 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
181 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
182 bool isConstant = false);
183
184 struct TBAAStructField {
185 uint64_t Offset;
186 uint64_t Size;
187 MDNode *Type;
188 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
189 Offset(Offset), Size(Size), Type(Type) {}
190 };
191
192 /// Return metadata for a tbaa.struct node with the given
193 /// struct field descriptions.
194 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
195
196 /// Return metadata for a TBAA struct node in the type DAG
197 /// with the given name, a list of pairs (offset, field type in the type DAG).
198 MDNode *
199 createTBAAStructTypeNode(StringRef Name,
200 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
201
202 /// Return metadata for a TBAA scalar type node with the
203 /// given name, an offset and a parent in the TBAA type DAG.
204 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
205 uint64_t Offset = 0);
206
207 /// Return metadata for a TBAA tag node with the given
208 /// base type, access type and offset relative to the base type.
209 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
210 uint64_t Offset, bool IsConstant = false);
211
212 /// Return metadata for a TBAA type node in the TBAA type DAG with the
213 /// given parent type, size in bytes, type identifier and a list of fields.
214 MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
215 ArrayRef<TBAAStructField> Fields =
216 ArrayRef<TBAAStructField>());
217
218 /// Return metadata for a TBAA access tag with the given base type,
219 /// final access type, offset of the access relative to the base type, size of
220 /// the access and flag indicating whether the accessed object can be
221 /// considered immutable for the purposes of the TBAA analysis.
222 MDNode *createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
223 uint64_t Offset, uint64_t Size,
224 bool IsImmutable = false);
225
226 /// Return mutable version of the given mutable or immutable TBAA
227 /// access tag.
228 MDNode *createMutableTBAAAccessTag(MDNode *Tag);
229
230 /// Return metadata containing an irreducible loop header weight.
231 MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
232};
233
234} // end namespace llvm
235
236#endif
237

source code of llvm/include/llvm/IR/MDBuilder.h