LLVM 23.0.0git
MDBuilder.h
Go to the documentation of this file.
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"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/GlobalValue.h"
23#include <utility>
24
25namespace llvm {
26
27class APInt;
28template <typename T> class ArrayRef;
29class LLVMContext;
30class Constant;
32class Function;
33class MDNode;
34class MDString;
35class Metadata;
36
37class MDBuilder {
38 LLVMContext &Context;
39
40public:
41 /// The weight for a branch taken with high probability.
42 ///
43 /// This is the weight used for Likely branches, for example, as used by
44 /// __builtin_expect* or when profile indicates a branch is taken with very
45 /// high probability.
46 static constexpr uint32_t kLikelyBranchWeight = (1U << 20) - 1;
47
48 /// The weight for a branch taken with low probability.
49 ///
50 /// This is the weight used for unlikely branches, for example, as used by
51 /// __builtin_expect* or when profile indicates a branch is taken with very
52 /// low probability.
53 static constexpr uint32_t kUnlikelyBranchWeight = 1;
54
55 MDBuilder(LLVMContext &context) : Context(context) {}
56
57 /// Return the given string as metadata.
59
60 /// Return the given constant as metadata.
62
63 //===------------------------------------------------------------------===//
64 // FPMath metadata.
65 //===------------------------------------------------------------------===//
66
67 /// Return metadata with the given settings. The special value 0.0
68 /// for the Accuracy parameter indicates the default (maximal precision)
69 /// setting.
70 LLVM_ABI MDNode *createFPMath(float Accuracy);
71
72 //===------------------------------------------------------------------===//
73 // Prof metadata.
74 //===------------------------------------------------------------------===//
75
76 /// Return metadata containing two branch weights.
77 /// @param TrueWeight the weight of the true branch
78 /// @param FalseWeight the weight of the false branch
79 /// @param Do these weights come from __builtin_expect*
81 uint32_t FalseWeight,
82 bool IsExpected = false);
83
84 /// Return metadata containing two branch weights, with significant bias
85 /// towards `true` destination.
87
88 /// Return metadata containing two branch weights, with significant bias
89 /// towards `false` destination.
91
92 /// Return metadata containing a number of branch weights.
93 /// @param Weights the weights of all the branches
94 /// @param Do these weights come from __builtin_expect*
96 bool IsExpected = false);
97
98 /// Return metadata specifying that a branch or switch is unpredictable.
100
101 /// Return metadata containing the entry \p Count for a function, a boolean
102 /// \Synthetic indicating whether the counts were synthetized, and the
103 /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
104 /// enable the same inlines as the profiled optimized binary
107 const DenseSet<GlobalValue::GUID> *Imports);
108
109 /// Return metadata containing the section prefix for a global object.
111
112 /// Return metadata containing the pseudo probe descriptor for a function.
114 StringRef FName);
115
116 /// Return metadata containing llvm statistics.
118 createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStatsVec);
119
120 //===------------------------------------------------------------------===//
121 // Range metadata.
122 //===------------------------------------------------------------------===//
123
124 /// Return metadata describing the range [Lo, Hi).
125 LLVM_ABI MDNode *createRange(const APInt &Lo, const APInt &Hi);
126
127 /// Return metadata describing the range [Lo, Hi).
129
130 //===------------------------------------------------------------------===//
131 // Callees metadata.
132 //===------------------------------------------------------------------===//
133
134 /// Return metadata indicating the possible callees of indirect
135 /// calls.
137
138 //===------------------------------------------------------------------===//
139 // Callback metadata.
140 //===------------------------------------------------------------------===//
141
142 /// Return metadata describing a callback (see llvm::AbstractCallSite).
143 LLVM_ABI MDNode *createCallbackEncoding(unsigned CalleeArgNo,
145 bool VarArgsArePassed);
146
147 /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
148 LLVM_ABI MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks,
149 MDNode *NewCB);
150
151 /// Return metadata feeding to the CodeGen about how to generate a function
152 /// prologue for the "function" santizier.
154 Constant *RTTI);
155
156 //===------------------------------------------------------------------===//
157 // PC sections metadata.
158 //===------------------------------------------------------------------===//
159
160 /// A pair of PC section name with auxilliary constant data.
161 using PCSection = std::pair<StringRef, SmallVector<Constant *>>;
162
163 /// Return metadata for PC sections.
165
166 //===------------------------------------------------------------------===//
167 // AA metadata.
168 //===------------------------------------------------------------------===//
169
170protected:
171 /// Return metadata appropriate for a AA root node (scope or TBAA).
172 /// Each returned node is distinct from all other metadata and will never
173 /// be identified (uniqued) with anything else.
175 MDNode *Extra = nullptr);
176
177public:
178 /// Return metadata appropriate for a TBAA root node. Each returned
179 /// node is distinct from all other metadata and will never be identified
180 /// (uniqued) with anything else.
184
185 /// Return metadata appropriate for an alias scope domain node.
186 /// Each returned node is distinct from all other metadata and will never
187 /// be identified (uniqued) with anything else.
191
192 /// Return metadata appropriate for an alias scope root node.
193 /// Each returned node is distinct from all other metadata and will never
194 /// be identified (uniqued) with anything else.
199
200 /// Return metadata appropriate for a TBAA root node with the given
201 /// name. This may be identified (uniqued) with other roots with the same
202 /// name.
204
205 /// Return metadata appropriate for an alias scope domain node with
206 /// the given name. This may be identified (uniqued) with other roots with
207 /// the same name.
209
210 /// Return metadata appropriate for an alias scope node with
211 /// the given name. This may be identified (uniqued) with other scopes with
212 /// the same name and domain.
214
215 /// Return metadata for a non-root TBAA node with the given name,
216 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
218 bool isConstant = false);
219
227
228 /// Return metadata for a tbaa.struct node with the given
229 /// struct field descriptions.
231
232 /// Return metadata for a TBAA struct node in the type DAG
233 /// with the given name, a list of pairs (offset, field type in the type DAG).
236 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
237
238 /// Return metadata for a TBAA scalar type node with the
239 /// given name, an offset and a parent in the TBAA type DAG.
241 uint64_t Offset = 0);
242
243 /// Return metadata for a TBAA tag node with the given
244 /// base type, access type and offset relative to the base type.
247 bool IsConstant = false);
248
249 /// Return metadata for a TBAA type node in the TBAA type DAG with the
250 /// given parent type, size in bytes, type identifier and a list of fields.
252 MDNode *Parent, uint64_t Size, Metadata *Id,
254
255 /// Return metadata for a TBAA access tag with the given base type,
256 /// final access type, offset of the access relative to the base type, size of
257 /// the access and flag indicating whether the accessed object can be
258 /// considered immutable for the purposes of the TBAA analysis.
261 bool IsImmutable = false);
262
263 /// Return mutable version of the given mutable or immutable TBAA
264 /// access tag.
266
267 /// Return metadata containing an irreducible loop header weight.
269};
270
271} // end namespace llvm
272
273#endif
static bool isConstant(const MachineInstr &MI)
AMDGPU Lower Kernel Arguments
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseSet and SmallDenseSet classes.
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file defines the SmallVector class.
Class for arbitrary precision integers.
Definition APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is an important base class in LLVM.
Definition Constant.h:43
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI MDNode * createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, uint64_t Size, bool IsImmutable=false)
Return metadata for a TBAA access tag with the given base type, final access type,...
LLVM_ABI MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
LLVM_ABI MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
LLVM_ABI MDNode * createFunctionEntryCount(uint64_t Count, bool Synthetic, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, a boolean \Synthetic indicating whether th...
Definition MDBuilder.cpp:72
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
Definition MDBuilder.h:195
LLVM_ABI MDNode * createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName)
Return metadata containing the pseudo probe descriptor for a function.
LLVM_ABI ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition MDBuilder.cpp:25
LLVM_ABI MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition MDBuilder.cpp:29
LLVM_ABI MDNode * createPCSections(ArrayRef< PCSection > Sections)
Return metadata for PC sections.
LLVM_ABI MDNode * createTBAANode(StringRef Name, MDNode *Parent, bool isConstant=false)
Return metadata for a non-root TBAA node with the given name, parent in the TBAA tree,...
LLVM_ABI MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
static constexpr uint32_t kLikelyBranchWeight
The weight for a branch taken with high probability.
Definition MDBuilder.h:46
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition MDBuilder.cpp:38
LLVM_ABI MDString * createString(StringRef Str)
Return the given string as metadata.
Definition MDBuilder.cpp:21
MDNode * createAnonymousTBAARoot()
Return metadata appropriate for a TBAA root node.
Definition MDBuilder.h:181
LLVM_ABI MDNode * createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset=0)
Return metadata for a TBAA scalar type node with the given name, an offset and a parent in the TBAA t...
LLVM_ABI MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
LLVM_ABI MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition MDBuilder.cpp:70
LLVM_ABI MDNode * createTBAAStructTypeNode(StringRef Name, ArrayRef< std::pair< MDNode *, uint64_t > > Fields)
Return metadata for a TBAA struct node in the type DAG with the given name, a list of pairs (offset,...
LLVM_ABI MDNode * createCallees(ArrayRef< Function * > Callees)
Return metadata indicating the possible callees of indirect calls.
LLVM_ABI MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
LLVM_ABI MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition MDBuilder.cpp:96
LLVM_ABI MDNode * createLikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards true destination.
Definition MDBuilder.cpp:43
LLVM_ABI MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
MDBuilder(LLVMContext &context)
Definition MDBuilder.h:55
LLVM_ABI MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
static constexpr uint32_t kUnlikelyBranchWeight
The weight for a branch taken with low probability.
Definition MDBuilder.h:53
LLVM_ABI MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
std::pair< StringRef, SmallVector< Constant * > > PCSection
A pair of PC section name with auxilliary constant data.
Definition MDBuilder.h:161
LLVM_ABI MDNode * createGlobalObjectSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a global object.
Definition MDBuilder.cpp:91
LLVM_ABI MDNode * createLLVMStats(ArrayRef< std::pair< StringRef, uint64_t > > LLVMStatsVec)
Return metadata containing llvm statistics.
LLVM_ABI MDNode * createRTTIPointerPrologue(Constant *PrologueSig, Constant *RTTI)
Return metadata feeding to the CodeGen about how to generate a function prologue for the "function" s...
MDNode * createAnonymousAliasScopeDomain(StringRef Name=StringRef())
Return metadata appropriate for an alias scope domain node.
Definition MDBuilder.h:188
LLVM_ABI MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition MDBuilder.cpp:48
LLVM_ABI MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
LLVM_ABI MDNode * createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, bool IsConstant=false)
Return metadata for a TBAA tag node with the given base type, access type and offset relative to the ...
LLVM_ABI MDNode * createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id, ArrayRef< TBAAStructField > Fields=ArrayRef< TBAAStructField >())
Return metadata for a TBAA type node in the TBAA type DAG with the given parent type,...
Metadata node.
Definition Metadata.h:1080
A single uniqued string.
Definition Metadata.h:722
Root of the metadata hierarchy.
Definition Metadata.h:64
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type)
Definition MDBuilder.h:224