LLVM 24.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 and the
102 /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
103 /// enable the same inlines as the profiled optimized binary
106 const DenseSet<GlobalValue::GUID> *Imports);
107
108 /// Return metadata containing the section prefix for a global object.
110
111 /// Return metadata containing the pseudo probe descriptor for a function.
113 StringRef FName);
114
115 /// Return metadata containing llvm statistics.
117 createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStatsVec);
118
119 //===------------------------------------------------------------------===//
120 // Range metadata.
121 //===------------------------------------------------------------------===//
122
123 /// Return metadata describing the range [Lo, Hi).
124 LLVM_ABI MDNode *createRange(const APInt &Lo, const APInt &Hi);
125
126 /// Return metadata describing the range [Lo, Hi).
128
129 //===------------------------------------------------------------------===//
130 // Callees metadata.
131 //===------------------------------------------------------------------===//
132
133 /// Return metadata indicating the possible callees of indirect
134 /// calls.
136
137 //===------------------------------------------------------------------===//
138 // Callback metadata.
139 //===------------------------------------------------------------------===//
140
141 /// Return metadata describing a callback (see llvm::AbstractCallSite).
142 LLVM_ABI MDNode *createCallbackEncoding(unsigned CalleeArgNo,
144 bool VarArgsArePassed);
145
146 /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
147 LLVM_ABI MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks,
148 MDNode *NewCB);
149
150 /// Return metadata feeding to the CodeGen about how to generate a function
151 /// prologue for the "function" santizier.
153 Constant *RTTI);
154
155 //===------------------------------------------------------------------===//
156 // PC sections metadata.
157 //===------------------------------------------------------------------===//
158
159 /// A pair of PC section name with auxilliary constant data.
160 using PCSection = std::pair<StringRef, SmallVector<Constant *>>;
161
162 /// Return metadata for PC sections.
164
165 //===------------------------------------------------------------------===//
166 // AA metadata.
167 //===------------------------------------------------------------------===//
168
169protected:
170 /// Return metadata appropriate for a AA root node (scope or TBAA).
171 /// Each returned node is distinct from all other metadata and will never
172 /// be identified (uniqued) with anything else.
174 MDNode *Extra = nullptr);
175
176public:
177 /// Return metadata appropriate for a TBAA root node. Each returned
178 /// node is distinct from all other metadata and will never be identified
179 /// (uniqued) with anything else.
183
184 /// Return metadata appropriate for an alias scope domain node, described by
185 /// \p Description, whose scopes are disjoint if \p DisjointScopes is set.
186 /// Each returned node is distinct from all other metadata and will never
187 /// be identified (uniqued) with anything else.
190 bool DisjointScopes = false);
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, whose scopes are disjoint if \p DisjointScopes is set.
207 /// This may be identified (uniqued) with other roots with the same name.
209 bool DisjointScopes = false);
210
211 /// Return metadata appropriate for an alias scope node with
212 /// the given name. This may be identified (uniqued) with other scopes with
213 /// the same name and domain.
215
216 /// Return metadata for a non-root TBAA node with the given name,
217 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
219 bool isConstant = false);
220
228
229 /// Return metadata for a tbaa.struct node with the given
230 /// struct field descriptions.
232
233 /// Return metadata for a TBAA struct node in the type DAG
234 /// with the given name, a list of pairs (offset, field type in the type DAG).
237 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
238
239 /// Return metadata for a TBAA scalar type node with the
240 /// given name, an offset and a parent in the TBAA type DAG.
242 uint64_t Offset = 0);
243
244 /// Return metadata for a TBAA tag node with the given
245 /// base type, access type and offset relative to the base type.
248 bool IsConstant = false);
249
250 /// Return metadata for a TBAA type node in the TBAA type DAG with the
251 /// given parent type, size in bytes, type identifier and a list of fields.
253 MDNode *Parent, uint64_t Size, Metadata *Id,
255
256 /// Return metadata for a TBAA access tag with the given base type,
257 /// final access type, offset of the access relative to the base type, size of
258 /// the access and flag indicating whether the accessed object can be
259 /// considered immutable for the purposes of the TBAA analysis.
262 bool IsImmutable = false);
263
264 /// Return mutable version of the given mutable or immutable TBAA
265 /// access tag.
267
268 /// Return metadata containing an irreducible loop header weight.
270};
271
272} // end namespace llvm
273
274#endif
uint64_t uint64_t
static bool isConstant(const MachineInstr &MI)
AMDGPU Lower Kernel Arguments
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
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
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:281
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
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 * createAliasScopeDomain(StringRef Name, bool DisjointScopes=false)
Return metadata appropriate for an alias scope domain node with the given name, whose scopes are disj...
LLVM_ABI MDNode * createFunctionEntryCount(uint64_t Count, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function and the GUIDs stored in Imports that need t...
Definition MDBuilder.cpp:72
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).
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:180
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 * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition MDBuilder.cpp:92
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 * createAnonymousAliasScopeDomain(StringRef Description=StringRef(), bool DisjointScopes=false)
Return metadata appropriate for an alias scope domain node, described by Description,...
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:160
LLVM_ABI MDNode * createGlobalObjectSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a global object.
Definition MDBuilder.cpp:87
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...
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:1081
A single uniqued string.
Definition Metadata.h:733
Root of the metadata hierarchy.
Definition Metadata.h:64
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type)
Definition MDBuilder.h:225