LLVM 20.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"
37#include "llvm/ADT/StringMap.h"
38#include "llvm/ADT/StringRef.h"
39#include "llvm/IR/DebugLoc.h"
40#include "llvm/IR/GCStrategy.h"
41#include "llvm/IR/PassManager.h"
42#include "llvm/Pass.h"
43#include <algorithm>
44#include <cstddef>
45#include <cstdint>
46#include <memory>
47#include <vector>
48
49namespace llvm {
50
51class Constant;
52class Function;
53class MCSymbol;
54
55/// GCPoint - Metadata for a collector-safe point in machine code.
56///
57struct GCPoint {
58 MCSymbol *Label; ///< A label.
60
62 : Label(L), Loc(std::move(DL)) {}
63};
64
65/// GCRoot - Metadata for a pointer to an object managed by the garbage
66/// collector.
67struct GCRoot {
68 int Num; ///< Usually a frame index.
69 int StackOffset = -1; ///< Offset from the stack pointer.
70 const Constant *Metadata; ///< Metadata straight from the call
71 ///< to llvm.gcroot.
72
73 GCRoot(int N, const Constant *MD) : Num(N), Metadata(MD) {}
74};
75
76/// Garbage collection metadata for a single function. Currently, this
77/// information only applies to GCStrategies which use GCRoot.
79public:
80 using iterator = std::vector<GCPoint>::iterator;
81 using roots_iterator = std::vector<GCRoot>::iterator;
82 using live_iterator = std::vector<GCRoot>::const_iterator;
83
84private:
85 const Function &F;
86 GCStrategy &S;
87 uint64_t FrameSize;
88 std::vector<GCRoot> Roots;
89 std::vector<GCPoint> SafePoints;
90
91 // FIXME: Liveness. A 2D BitVector, perhaps?
92 //
93 // BitVector Liveness;
94 //
95 // bool islive(int point, int root) =
96 // Liveness[point * SafePoints.size() + root]
97 //
98 // The bit vector is the more compact representation where >3.2% of roots
99 // are live per safe point (1.5% on 64-bit hosts).
100
101public:
104
105 /// Handle invalidation explicitly.
106 bool invalidate(Function &F, const PreservedAnalyses &PA,
108
109 /// getFunction - Return the function to which this metadata applies.
110 const Function &getFunction() const { return F; }
111
112 /// getStrategy - Return the GC strategy for the function.
113 GCStrategy &getStrategy() { return S; }
114
115 /// addStackRoot - Registers a root that lives on the stack. Num is the
116 /// stack object ID for the alloca (if the code generator is
117 // using MachineFrameInfo).
118 void addStackRoot(int Num, const Constant *Metadata) {
119 Roots.push_back(GCRoot(Num, Metadata));
120 }
121
122 /// removeStackRoot - Removes a root.
124 return Roots.erase(position);
125 }
126
127 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
128 /// label just prior to the safe point (if the code generator is using
129 /// MachineModuleInfo).
130 void addSafePoint(MCSymbol *Label, const DebugLoc &DL) {
131 SafePoints.emplace_back(Label, DL);
132 }
133
134 /// getFrameSize/setFrameSize - Records the function's frame size.
135 uint64_t getFrameSize() const { return FrameSize; }
136 void setFrameSize(uint64_t S) { FrameSize = S; }
137
138 /// begin/end - Iterators for safe points.
139 iterator begin() { return SafePoints.begin(); }
140 iterator end() { return SafePoints.end(); }
141 size_t size() const { return SafePoints.size(); }
142
143 /// roots_begin/roots_end - Iterators for all roots in the function.
144 roots_iterator roots_begin() { return Roots.begin(); }
145 roots_iterator roots_end() { return Roots.end(); }
146 size_t roots_size() const { return Roots.size(); }
147
148 /// live_begin/live_end - Iterators for live roots at a given safe point.
150 live_iterator live_end(const iterator &p) { return roots_end(); }
151 size_t live_size(const iterator &p) const { return roots_size(); }
152};
153
156
157 GCStrategyMap() = default;
159
160 /// Handle invalidation explicitly.
163};
164
165/// An analysis pass which caches information about the entire Module.
166/// Records a cache of the 'active' gc strategy objects for the current Module.
168 : public AnalysisInfoMixin<CollectorMetadataAnalysis> {
170 static AnalysisKey Key;
171
172public:
175};
176
177/// An analysis pass which caches information about the Function.
178/// Records the function level information used by GCRoots.
179/// This pass depends on `CollectorMetadataAnalysis`.
180class GCFunctionAnalysis : public AnalysisInfoMixin<GCFunctionAnalysis> {
182 static AnalysisKey Key;
183
184public:
187};
188
189/// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
190/// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
191/// directed by the GCStrategy. It also performs automatic root initialization
192/// and custom intrinsic lowering.
193///
194/// This pass requires `CollectorMetadataAnalysis`.
195class GCLoweringPass : public PassInfoMixin<GCLoweringPass> {
196public:
198};
199
200/// An analysis pass which caches information about the entire Module.
201/// Records both the function level information used by GCRoots and a
202/// cache of the 'active' gc strategy objects for the current Module.
204 /// An owning list of all GCStrategies which have been created
206 /// A helper map to speedup lookups into the above list
208
209public:
210 /// Lookup the GCStrategy object associated with the given gc name.
211 /// Objects are owned internally; No caller should attempt to delete the
212 /// returned objects.
214
215 /// List of per function info objects. In theory, Each of these
216 /// may be associated with a different GC.
217 using FuncInfoVec = std::vector<std::unique_ptr<GCFunctionInfo>>;
218
219 FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
220 FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
221
222private:
223 /// Owning list of all GCFunctionInfos associated with this Module
224 FuncInfoVec Functions;
225
226 /// Non-owning map to bypass linear search when finding the GCFunctionInfo
227 /// associated with a particular Function.
229 finfo_map_type FInfoMap;
230
231public:
233
234 static char ID;
235
236 GCModuleInfo();
237
238 /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
239 /// call it in doFinalization().
240 ///
241 void clear();
242
243 /// begin/end - Iterators for used strategies.
244 ///
245 iterator begin() const { return GCStrategyList.begin(); }
246 iterator end() const { return GCStrategyList.end(); }
247
248 /// get - Look up function metadata. This is currently assumed
249 /// have the side effect of initializing the associated GCStrategy. That
250 /// will soon change.
252};
253
254} // end namespace llvm
255
256#endif // LLVM_CODEGEN_GCMETADATA_H
This file defines the StringMap class.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This file defines the SmallVector class.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:168
Result run(Module &M, ModuleAnalysisManager &MAM)
Definition: GCMetadata.cpp:38
This is an important base class in LLVM.
Definition: Constant.h:42
A debug info location.
Definition: DebugLoc.h:33
An analysis pass which caches information about the Function.
Definition: GCMetadata.h:180
Result run(Function &F, FunctionAnalysisManager &FAM)
Definition: GCMetadata.cpp:53
Garbage collection metadata for a single function.
Definition: GCMetadata.h:78
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:136
size_t roots_size() const
Definition: GCMetadata.h:146
void addStackRoot(int Num, const Constant *Metadata)
addStackRoot - Registers a root that lives on the stack.
Definition: GCMetadata.h:118
void addSafePoint(MCSymbol *Label, const DebugLoc &DL)
addSafePoint - Notes the existence of a safe point.
Definition: GCMetadata.h:130
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:81
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation explicitly.
Definition: GCMetadata.cpp:78
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:151
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function's frame size.
Definition: GCMetadata.h:135
size_t size() const
Definition: GCMetadata.h:141
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:139
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:149
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:123
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:113
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:150
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:82
roots_iterator roots_end()
Definition: GCMetadata.h:145
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:80
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:110
GCFunctionInfo(const Function &F, GCStrategy &S)
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:144
LowerIntrinsics - This pass rewrites calls to the llvm.gcread or llvm.gcwrite intrinsics,...
Definition: GCMetadata.h:195
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:203
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:220
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:219
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:92
iterator begin() const
begin/end - Iterators for used strategies.
Definition: GCMetadata.h:245
iterator end() const
Definition: GCMetadata.h:246
static char ID
Definition: GCMetadata.h:234
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:107
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:115
std::vector< std::unique_ptr< GCFunctionInfo > > FuncInfoVec
List of per function info objects.
Definition: GCMetadata.h:217
GCStrategy describes a garbage collector algorithm's code generation requirements,...
Definition: GCStrategy.h:63
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:281
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:33
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
GCPoint - Metadata for a collector-safe point in machine code.
Definition: GCMetadata.h:57
GCPoint(MCSymbol *L, DebugLoc DL)
Definition: GCMetadata.h:61
MCSymbol * Label
A label.
Definition: GCMetadata.h:58
DebugLoc Loc
Definition: GCMetadata.h:59
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:67
GCRoot(int N, const Constant *MD)
Definition: GCMetadata.h:73
const Constant * Metadata
Metadata straight from the call to llvm.gcroot.
Definition: GCMetadata.h:70
int Num
Usually a frame index.
Definition: GCMetadata.h:68
GCStrategyMap(GCStrategyMap &&)=default
bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &Inv)
Handle invalidation explicitly.
Definition: GCMetadata.cpp:24
GCStrategyMap()=default
StringMap< std::unique_ptr< GCStrategy > > StrategyMap
Definition: GCMetadata.h:155
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69