LLVM  13.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/SmallVector.h"
37 #include "llvm/ADT/StringMap.h"
38 #include "llvm/ADT/StringRef.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/Pass.h"
42 #include <algorithm>
43 #include <cstddef>
44 #include <cstdint>
45 #include <memory>
46 #include <vector>
47 
48 namespace llvm {
49 
50 class Constant;
51 class Function;
52 class MCSymbol;
53 
54 /// GCPoint - Metadata for a collector-safe point in machine code.
55 ///
56 struct GCPoint {
57  MCSymbol *Label; ///< A label.
59 
61  : Label(L), Loc(std::move(DL)) {}
62 };
63 
64 /// GCRoot - Metadata for a pointer to an object managed by the garbage
65 /// collector.
66 struct GCRoot {
67  int Num; ///< Usually a frame index.
68  int StackOffset = -1; ///< Offset from the stack pointer.
69  const Constant *Metadata; ///< Metadata straight from the call
70  ///< to llvm.gcroot.
71 
72  GCRoot(int N, const Constant *MD) : Num(N), Metadata(MD) {}
73 };
74 
75 /// Garbage collection metadata for a single function. Currently, this
76 /// information only applies to GCStrategies which use GCRoot.
78 public:
79  using iterator = std::vector<GCPoint>::iterator;
80  using roots_iterator = std::vector<GCRoot>::iterator;
81  using live_iterator = std::vector<GCRoot>::const_iterator;
82 
83 private:
84  const Function &F;
85  GCStrategy &S;
86  uint64_t FrameSize;
87  std::vector<GCRoot> Roots;
88  std::vector<GCPoint> SafePoints;
89 
90  // FIXME: Liveness. A 2D BitVector, perhaps?
91  //
92  // BitVector Liveness;
93  //
94  // bool islive(int point, int root) =
95  // Liveness[point * SafePoints.size() + root]
96  //
97  // The bit vector is the more compact representation where >3.2% of roots
98  // are live per safe point (1.5% on 64-bit hosts).
99 
100 public:
101  GCFunctionInfo(const Function &F, GCStrategy &S);
102  ~GCFunctionInfo();
103 
104  /// getFunction - Return the function to which this metadata applies.
105  const Function &getFunction() const { return F; }
106 
107  /// getStrategy - Return the GC strategy for the function.
108  GCStrategy &getStrategy() { return S; }
109 
110  /// addStackRoot - Registers a root that lives on the stack. Num is the
111  /// stack object ID for the alloca (if the code generator is
112  // using MachineFrameInfo).
113  void addStackRoot(int Num, const Constant *Metadata) {
114  Roots.push_back(GCRoot(Num, Metadata));
115  }
116 
117  /// removeStackRoot - Removes a root.
119  return Roots.erase(position);
120  }
121 
122  /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
123  /// label just prior to the safe point (if the code generator is using
124  /// MachineModuleInfo).
125  void addSafePoint(MCSymbol *Label, const DebugLoc &DL) {
126  SafePoints.emplace_back(Label, DL);
127  }
128 
129  /// getFrameSize/setFrameSize - Records the function's frame size.
130  uint64_t getFrameSize() const { return FrameSize; }
131  void setFrameSize(uint64_t S) { FrameSize = S; }
132 
133  /// begin/end - Iterators for safe points.
134  iterator begin() { return SafePoints.begin(); }
135  iterator end() { return SafePoints.end(); }
136  size_t size() const { return SafePoints.size(); }
137 
138  /// roots_begin/roots_end - Iterators for all roots in the function.
139  roots_iterator roots_begin() { return Roots.begin(); }
140  roots_iterator roots_end() { return Roots.end(); }
141  size_t roots_size() const { return Roots.size(); }
142 
143  /// live_begin/live_end - Iterators for live roots at a given safe point.
145  live_iterator live_end(const iterator &p) { return roots_end(); }
146  size_t live_size(const iterator &p) const { return roots_size(); }
147 };
148 
149 /// An analysis pass which caches information about the entire Module.
150 /// Records both the function level information used by GCRoots and a
151 /// cache of the 'active' gc strategy objects for the current Module.
152 class GCModuleInfo : public ImmutablePass {
153  /// An owning list of all GCStrategies which have been created
154  SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
155  /// A helper map to speedup lookups into the above list
156  StringMap<GCStrategy*> GCStrategyMap;
157 
158 public:
159  /// Lookup the GCStrategy object associated with the given gc name.
160  /// Objects are owned internally; No caller should attempt to delete the
161  /// returned objects.
163 
164  /// List of per function info objects. In theory, Each of these
165  /// may be associated with a different GC.
166  using FuncInfoVec = std::vector<std::unique_ptr<GCFunctionInfo>>;
167 
168  FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
169  FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
170 
171 private:
172  /// Owning list of all GCFunctionInfos associated with this Module
173  FuncInfoVec Functions;
174 
175  /// Non-owning map to bypass linear search when finding the GCFunctionInfo
176  /// associated with a particular Function.
177  using finfo_map_type = DenseMap<const Function *, GCFunctionInfo *>;
178  finfo_map_type FInfoMap;
179 
180 public:
182 
183  static char ID;
184 
185  GCModuleInfo();
186 
187  /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
188  /// call it in doFinalization().
189  ///
190  void clear();
191 
192  /// begin/end - Iterators for used strategies.
193  ///
194  iterator begin() const { return GCStrategyList.begin(); }
195  iterator end() const { return GCStrategyList.end(); }
196 
197  /// get - Look up function metadata. This is currently assumed
198  /// have the side effect of initializing the associated GCStrategy. That
199  /// will soon change.
201 };
202 
203 } // end namespace llvm
204 
205 #endif // LLVM_CODEGEN_GCMETADATA_H
llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::GCFunctionInfo::live_begin
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:144
llvm::GCModuleInfo::begin
iterator begin() const
begin/end - Iterators for used strategies.
Definition: GCMetadata.h:194
llvm::GCStrategy
GCStrategy describes a garbage collector algorithm's code generation requirements,...
Definition: GCStrategy.h:66
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
llvm::Function
Definition: Function.h:61
StringRef.h
Pass.h
llvm::GCFunctionInfo::addSafePoint
void addSafePoint(MCSymbol *Label, const DebugLoc &DL)
addSafePoint - Notes the existence of a safe point.
Definition: GCMetadata.h:125
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::GCRoot
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:66
llvm::GCFunctionInfo
Garbage collection metadata for a single function.
Definition: GCMetadata.h:77
llvm::GCFunctionInfo::iterator
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:79
DenseMap.h
llvm::GCFunctionInfo::roots_begin
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:139
llvm::GCRoot::Metadata
const Constant * Metadata
Metadata straight from the call to llvm.gcroot.
Definition: GCMetadata.h:69
llvm::GCFunctionInfo::getStrategy
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:108
llvm::GCFunctionInfo::~GCFunctionInfo
~GCFunctionInfo()
p
the resulting code requires compare and branches when and if * p
Definition: README.txt:396
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::GCModuleInfo::iterator
SmallVector< std::unique_ptr< GCStrategy >, 1 >::const_iterator iterator
Definition: GCMetadata.h:181
llvm::GCFunctionInfo::live_iterator
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:81
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:69
llvm::GCPoint::GCPoint
GCPoint(MCSymbol *L, DebugLoc DL)
Definition: GCMetadata.h:60
llvm::GCModuleInfo::FuncInfoVec
std::vector< std::unique_ptr< GCFunctionInfo > > FuncInfoVec
List of per function info objects.
Definition: GCMetadata.h:166
llvm::GCFunctionInfo::begin
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:134
llvm::GCFunctionInfo::setFrameSize
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:131
llvm::GCModuleInfo::getGCStrategy
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:143
llvm::GCModuleInfo::clear
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:83
llvm::GCModuleInfo::ID
static char ID
Definition: GCMetadata.h:183
DebugLoc.h
llvm::GCFunctionInfo::getFrameSize
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function's frame size.
Definition: GCMetadata.h:130
StringMap.h
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:58
llvm::GCFunctionInfo::live_end
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:145
llvm::GCRoot::Num
int Num
Usually a frame index.
Definition: GCMetadata.h:67
llvm::GCFunctionInfo::live_size
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:146
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::GCPoint::Label
MCSymbol * Label
A label.
Definition: GCMetadata.h:57
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::GCModuleInfo::getFunctionInfo
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:68
llvm::DenseMap< const Function *, GCFunctionInfo * >
llvm::GCModuleInfo::funcinfo_end
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:169
llvm::move
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:1556
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::GCFunctionInfo::size
size_t size() const
Definition: GCMetadata.h:136
const_iterator
llvm::StackOffset
StackOffset is a class to represent an offset with 2 dimensions, named fixed and scalable,...
Definition: TypeSize.h:130
llvm::GCFunctionInfo::end
iterator end()
Definition: GCMetadata.h:135
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::GCModuleInfo::funcinfo_begin
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:168
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:941
llvm::GCFunctionInfo::addStackRoot
void addStackRoot(int Num, const Constant *Metadata)
addStackRoot - Registers a root that lives on the stack.
Definition: GCMetadata.h:113
llvm::GCPoint::Loc
DebugLoc Loc
Definition: GCMetadata.h:58
llvm::GCFunctionInfo::roots_size
size_t roots_size() const
Definition: GCMetadata.h:141
llvm::GCModuleInfo::GCModuleInfo
GCModuleInfo()
Definition: GCMetadata.cpp:64
SmallVector.h
N
#define N
llvm::GCPoint
GCPoint - Metadata for a collector-safe point in machine code.
Definition: GCMetadata.h:56
llvm::GCFunctionInfo::GCFunctionInfo
GCFunctionInfo(const Function &F, GCStrategy &S)
llvm::GCModuleInfo::end
iterator end() const
Definition: GCMetadata.h:195
llvm::GCFunctionInfo::removeStackRoot
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:118
llvm::GCRoot::GCRoot
GCRoot(int N, const Constant *MD)
Definition: GCMetadata.h:72
llvm::GCFunctionInfo::roots_iterator
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:80
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::GCFunctionInfo::roots_end
roots_iterator roots_end()
Definition: GCMetadata.h:140
llvm::GCModuleInfo
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:152
llvm::GCFunctionInfo::getFunction
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:105
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:165
GCStrategy.h