LLVM 22.0.0git
GCMetadata.h
Go to the documentation of this file.
1//===- GCMetadata.h - Garbage collector 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 declares the GCFunctionInfo and GCModuleInfo classes, which are
10// used as a communication channel from the target code generator to the target
11// garbage collectors. This interface allows code generators and garbage
12// collectors to be developed independently.
13//
14// The GCFunctionInfo class logs the data necessary to build a type accurate
15// stack map. The code generator outputs:
16//
17// - Safe points as specified by the GCStrategy's NeededSafePoints.
18// - Stack offsets for GC roots, as specified by calls to llvm.gcroot
19//
20// As a refinement, liveness analysis calculates the set of live roots at each
21// safe point. Liveness analysis is not presently performed by the code
22// generator, so all roots are assumed live.
23//
24// GCModuleInfo simply collects GCFunctionInfo instances for each Function as
25// they are compiled. This accretion is necessary for collectors which must emit
26// a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo
27// outlives the MachineFunction from which it is derived and must not refer to
28// any code generator data structures.
29//
30//===----------------------------------------------------------------------===//
31
32#ifndef LLVM_CODEGEN_GCMETADATA_H
33#define LLVM_CODEGEN_GCMETADATA_H
34
35#include "llvm/ADT/DenseMap.h"
36#include "llvm/ADT/MapVector.h"
38#include "llvm/ADT/StringMap.h"
39#include "llvm/ADT/StringRef.h"
40#include "llvm/IR/DebugLoc.h"
41#include "llvm/IR/GCStrategy.h"
42#include "llvm/IR/PassManager.h"
43#include "llvm/Pass.h"
45#include <algorithm>
46#include <cstddef>
47#include <cstdint>
48#include <memory>
49#include <vector>
50
51namespace llvm {
52
53class Constant;
54class Function;
55class MCSymbol;
56
57/// GCPoint - Metadata for a collector-safe point in machine code.
58///
59struct GCPoint {
60 MCSymbol *Label; ///< A label.
62
64 : Label(L), Loc(std::move(DL)) {}
65};
66
67/// GCRoot - Metadata for a pointer to an object managed by the garbage
68/// collector.
69struct GCRoot {
70 int Num; ///< Usually a frame index.
71 int StackOffset = -1; ///< Offset from the stack pointer.
72 const Constant *Metadata; ///< Metadata straight from the call
73 ///< to llvm.gcroot.
74
75 GCRoot(int N, const Constant *MD) : Num(N), Metadata(MD) {}
76};
77
78/// Garbage collection metadata for a single function. Currently, this
79/// information only applies to GCStrategies which use GCRoot.
81public:
82 using iterator = std::vector<GCPoint>::iterator;
83 using roots_iterator = std::vector<GCRoot>::iterator;
84 using live_iterator = std::vector<GCRoot>::const_iterator;
85
86private:
87 const Function &F;
88 GCStrategy &S;
89 uint64_t FrameSize;
90 std::vector<GCRoot> Roots;
91 std::vector<GCPoint> SafePoints;
92
93 // FIXME: Liveness. A 2D BitVector, perhaps?
94 //
95 // BitVector Liveness;
96 //
97 // bool islive(int point, int root) =
98 // Liveness[point * SafePoints.size() + root]
99 //
100 // The bit vector is the more compact representation where >3.2% of roots
101 // are live per safe point (1.5% on 64-bit hosts).
102
103public:
106
107 /// Handle invalidation explicitly.
110
111 /// getFunction - Return the function to which this metadata applies.
112 const Function &getFunction() const { return F; }
113
114 /// getStrategy - Return the GC strategy for the function.
115 GCStrategy &getStrategy() { return S; }
116
117 /// addStackRoot - Registers a root that lives on the stack. Num is the
118 /// stack object ID for the alloca (if the code generator is
119 // using MachineFrameInfo).
120 void addStackRoot(int Num, const Constant *Metadata) {
121 Roots.push_back(GCRoot(Num, Metadata));
122 }
123
124 /// removeStackRoot - Removes a root.
126 return Roots.erase(position);
127 }
128
129 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
130 /// label just prior to the safe point (if the code generator is using
131 /// MachineModuleInfo).
132 void addSafePoint(MCSymbol *Label, const DebugLoc &DL) {
133 SafePoints.emplace_back(Label, DL);
134 }
135
136 /// getFrameSize/setFrameSize - Records the function's frame size.
137 uint64_t getFrameSize() const { return FrameSize; }
138 void setFrameSize(uint64_t S) { FrameSize = S; }
139
140 /// begin/end - Iterators for safe points.
141 iterator begin() { return SafePoints.begin(); }
142 iterator end() { return SafePoints.end(); }
143 size_t size() const { return SafePoints.size(); }
144
145 /// roots_begin/roots_end - Iterators for all roots in the function.
146 roots_iterator roots_begin() { return Roots.begin(); }
147 roots_iterator roots_end() { return Roots.end(); }
148 size_t roots_size() const { return Roots.size(); }
149
150 /// live_begin/live_end - Iterators for live roots at a given safe point.
152 live_iterator live_end(const iterator &p) { return roots_end(); }
153 size_t live_size(const iterator &p) const { return roots_size(); }
154};
155
157 using MapT =
159 MapT Strategies;
160
161public:
162 GCStrategyMap() = default;
164
165 /// Handle invalidation explicitly.
166 LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA,
168
173
174 iterator begin() { return Strategies.begin(); }
175 const_iterator begin() const { return Strategies.begin(); }
176 iterator end() { return Strategies.end(); }
177 const_iterator end() const { return Strategies.end(); }
178
179 reverse_iterator rbegin() { return Strategies.rbegin(); }
180 const_reverse_iterator rbegin() const { return Strategies.rbegin(); }
181 reverse_iterator rend() { return Strategies.rend(); }
182 const_reverse_iterator rend() const { return Strategies.rend(); }
183
184 bool empty() const { return Strategies.empty(); }
185
186 const GCStrategy &operator[](StringRef GCName) const {
187 auto I = Strategies.find(GCName);
188 assert(I != Strategies.end() && "Required strategy doesn't exist!");
189 return *I->second;
190 }
191
192 std::pair<iterator, bool> try_emplace(StringRef GCName) {
193 return Strategies.try_emplace(GCName);
194 }
195
196 bool contains(StringRef GCName) const { return Strategies.contains(GCName); }
197};
198
199/// An analysis pass which caches information about the entire Module.
200/// Records a cache of the 'active' gc strategy objects for the current Module.
202 : public AnalysisInfoMixin<CollectorMetadataAnalysis> {
204 LLVM_ABI static AnalysisKey Key;
205
206public:
209};
210
211/// An analysis pass which caches information about the Function.
212/// Records the function level information used by GCRoots.
213/// This pass depends on `CollectorMetadataAnalysis`.
214class GCFunctionAnalysis : public AnalysisInfoMixin<GCFunctionAnalysis> {
216 LLVM_ABI static AnalysisKey Key;
217
218public:
221};
222
223/// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
224/// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
225/// directed by the GCStrategy. It also performs automatic root initialization
226/// and custom intrinsic lowering.
227///
228/// This pass requires `CollectorMetadataAnalysis`.
229class GCLoweringPass : public PassInfoMixin<GCLoweringPass> {
230public:
232};
233
234/// An analysis pass which caches information about the entire Module.
235/// Records both the function level information used by GCRoots and a
236/// cache of the 'active' gc strategy objects for the current Module.
238 /// An owning list of all GCStrategies which have been created
240 /// A helper map to speedup lookups into the above list
242
243public:
244 /// Lookup the GCStrategy object associated with the given gc name.
245 /// Objects are owned internally; No caller should attempt to delete the
246 /// returned objects.
248
249 /// List of per function info objects. In theory, Each of these
250 /// may be associated with a different GC.
251 using FuncInfoVec = std::vector<std::unique_ptr<GCFunctionInfo>>;
252
253 FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
254 FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
255
256private:
257 /// Owning list of all GCFunctionInfos associated with this Module
258 FuncInfoVec Functions;
259
260 /// Non-owning map to bypass linear search when finding the GCFunctionInfo
261 /// associated with a particular Function.
263 finfo_map_type FInfoMap;
264
265public:
267
268 LLVM_ABI static char ID;
269
271
272 /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
273 /// call it in doFinalization().
274 ///
275 LLVM_ABI void clear();
276
277 /// begin/end - Iterators for used strategies.
278 ///
279 iterator begin() const { return GCStrategyList.begin(); }
280 iterator end() const { return GCStrategyList.end(); }
281
282 /// get - Look up function metadata. This is currently assumed
283 /// have the side effect of initializing the associated GCStrategy. That
284 /// will soon change.
286};
287
288} // end namespace llvm
289
290#endif // LLVM_CODEGEN_GCMETADATA_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
std::string Name
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This file defines the SmallVector class.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:202
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &MAM)
Definition: GCMetadata.cpp:38
This is an important base class in LLVM.
Definition: Constant.h:43
A debug info location.
Definition: DebugLoc.h:124
An analysis pass which caches information about the Function.
Definition: GCMetadata.h:214
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &FAM)
Definition: GCMetadata.cpp:56
Garbage collection metadata for a single function.
Definition: GCMetadata.h:80
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:138
size_t roots_size() const
Definition: GCMetadata.h:148
void addStackRoot(int Num, const Constant *Metadata)
addStackRoot - Registers a root that lives on the stack.
Definition: GCMetadata.h:120
LLVM_ABI ~GCFunctionInfo()
void addSafePoint(MCSymbol *Label, const DebugLoc &DL)
addSafePoint - Notes the existence of a safe point.
Definition: GCMetadata.h:132
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:83
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation explicitly.
Definition: GCMetadata.cpp:81
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:153
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function's frame size.
Definition: GCMetadata.h:137
size_t size() const
Definition: GCMetadata.h:143
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:141
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:151
LLVM_ABI GCFunctionInfo(const Function &F, GCStrategy &S)
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:125
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:115
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:152
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:84
roots_iterator roots_end()
Definition: GCMetadata.h:147
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:82
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:112
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:146
LowerIntrinsics - This pass rewrites calls to the llvm.gcread or llvm.gcwrite intrinsics,...
Definition: GCMetadata.h:229
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:237
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:254
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:253
LLVM_ABI GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:95
iterator begin() const
begin/end - Iterators for used strategies.
Definition: GCMetadata.h:279
iterator end() const
Definition: GCMetadata.h:280
static LLVM_ABI char ID
Definition: GCMetadata.h:268
LLVM_ABI void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:110
LLVM_ABI GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:118
std::vector< std::unique_ptr< GCFunctionInfo > > FuncInfoVec
List of per function info objects.
Definition: GCMetadata.h:251
LLVM_ABI GCModuleInfo()
Definition: GCMetadata.cpp:91
std::pair< iterator, bool > try_emplace(StringRef GCName)
Definition: GCMetadata.h:192
reverse_iterator rbegin()
Definition: GCMetadata.h:179
GCStrategyMap(GCStrategyMap &&)=default
MapT::const_reverse_iterator const_reverse_iterator
Definition: GCMetadata.h:172
const_reverse_iterator rend() const
Definition: GCMetadata.h:182
const_iterator begin() const
Definition: GCMetadata.h:175
MapT::const_iterator const_iterator
Definition: GCMetadata.h:170
MapT::reverse_iterator reverse_iterator
Definition: GCMetadata.h:171
MapT::iterator iterator
Definition: GCMetadata.h:169
bool empty() const
Definition: GCMetadata.h:184
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &Inv)
Handle invalidation explicitly.
Definition: GCMetadata.cpp:24
GCStrategyMap()=default
bool contains(StringRef GCName) const
Definition: GCMetadata.h:196
iterator begin()
Definition: GCMetadata.h:174
const GCStrategy & operator[](StringRef GCName) const
Definition: GCMetadata.h:186
reverse_iterator rend()
Definition: GCMetadata.h:181
const_reverse_iterator rbegin() const
Definition: GCMetadata.h:180
const_iterator end() const
Definition: GCMetadata.h:177
GCStrategy describes a garbage collector algorithm's code generation requirements,...
Definition: GCStrategy.h:64
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:285
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
iterator end()
Definition: MapVector.h:67
reverse_iterator rend()
Definition: MapVector.h:72
iterator find(const KeyT &Key)
Definition: MapVector.h:141
bool contains(const KeyT &Key) const
Definition: MapVector.h:137
bool empty() const
Definition: MapVector.h:75
iterator begin()
Definition: MapVector.h:65
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition: MapVector.h:107
reverse_iterator rbegin()
Definition: MapVector.h:70
Root of the metadata hierarchy.
Definition: Metadata.h:63
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:34
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
GCPoint - Metadata for a collector-safe point in machine code.
Definition: GCMetadata.h:59
GCPoint(MCSymbol *L, DebugLoc DL)
Definition: GCMetadata.h:63
MCSymbol * Label
A label.
Definition: GCMetadata.h:60
DebugLoc Loc
Definition: GCMetadata.h:61
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:69
GCRoot(int N, const Constant *MD)
Definition: GCMetadata.h:75
const Constant * Metadata
Metadata straight from the call to llvm.gcroot.
Definition: GCMetadata.h:72
int Num
Usually a frame index.
Definition: GCMetadata.h:70
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70