LLVM  9.0.0svn
GCMetadata.h
Go to the documentation of this file.
1 //===- GCMetadata.h - Garbage collector metadata ----------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the GCFunctionInfo and GCModuleInfo classes, which are
11 // used as a communication channel from the target code generator to the target
12 // garbage collectors. This interface allows code generators and garbage
13 // collectors to be developed independently.
14 //
15 // The GCFunctionInfo class logs the data necessary to build a type accurate
16 // stack map. The code generator outputs:
17 //
18 // - Safe points as specified by the GCStrategy's NeededSafePoints.
19 // - Stack offsets for GC roots, as specified by calls to llvm.gcroot
20 //
21 // As a refinement, liveness analysis calculates the set of live roots at each
22 // safe point. Liveness analysis is not presently performed by the code
23 // generator, so all roots are assumed live.
24 //
25 // GCModuleInfo simply collects GCFunctionInfo instances for each Function as
26 // they are compiled. This accretion is necessary for collectors which must emit
27 // a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo
28 // outlives the MachineFunction from which it is derived and must not refer to
29 // any code generator data structures.
30 //
31 //===----------------------------------------------------------------------===//
32 
33 #ifndef LLVM_CODEGEN_GCMETADATA_H
34 #define LLVM_CODEGEN_GCMETADATA_H
35 
36 #include "llvm/ADT/DenseMap.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringMap.h"
39 #include "llvm/ADT/StringRef.h"
41 #include "llvm/IR/DebugLoc.h"
42 #include "llvm/Pass.h"
43 #include <algorithm>
44 #include <cstddef>
45 #include <cstdint>
46 #include <memory>
47 #include <vector>
48 
49 namespace llvm {
50 
51 class Constant;
52 class Function;
53 class MCSymbol;
54 
55 /// GCPoint - Metadata for a collector-safe point in machine code.
56 ///
57 struct 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.
67 struct 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.
79 public:
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 
84 private:
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 
101 public:
102  GCFunctionInfo(const Function &F, GCStrategy &S);
103  ~GCFunctionInfo();
104 
105  /// getFunction - Return the function to which this metadata applies.
106  const Function &getFunction() const { return F; }
107 
108  /// getStrategy - Return the GC strategy for the function.
109  GCStrategy &getStrategy() { return S; }
110 
111  /// addStackRoot - Registers a root that lives on the stack. Num is the
112  /// stack object ID for the alloca (if the code generator is
113  // using MachineFrameInfo).
114  void addStackRoot(int Num, const Constant *Metadata) {
115  Roots.push_back(GCRoot(Num, Metadata));
116  }
117 
118  /// removeStackRoot - Removes a root.
120  return Roots.erase(position);
121  }
122 
123  /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
124  /// label just prior to the safe point (if the code generator is using
125  /// MachineModuleInfo).
126  void addSafePoint(MCSymbol *Label, const DebugLoc &DL) {
127  SafePoints.emplace_back(Label, DL);
128  }
129 
130  /// getFrameSize/setFrameSize - Records the function's frame size.
131  uint64_t getFrameSize() const { return FrameSize; }
132  void setFrameSize(uint64_t S) { FrameSize = S; }
133 
134  /// begin/end - Iterators for safe points.
135  iterator begin() { return SafePoints.begin(); }
136  iterator end() { return SafePoints.end(); }
137  size_t size() const { return SafePoints.size(); }
138 
139  /// roots_begin/roots_end - Iterators for all roots in the function.
140  roots_iterator roots_begin() { return Roots.begin(); }
141  roots_iterator roots_end() { return Roots.end(); }
142  size_t roots_size() const { return Roots.size(); }
143 
144  /// live_begin/live_end - Iterators for live roots at a given safe point.
145  live_iterator live_begin(const iterator &p) { return roots_begin(); }
146  live_iterator live_end(const iterator &p) { return roots_end(); }
147  size_t live_size(const iterator &p) const { return roots_size(); }
148 };
149 
150 /// An analysis pass which caches information about the entire Module.
151 /// Records both the function level information used by GCRoots and a
152 /// cache of the 'active' gc strategy objects for the current Module.
153 class GCModuleInfo : public ImmutablePass {
154  /// An owning list of all GCStrategies which have been created
155  SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
156  /// A helper map to speedup lookups into the above list
157  StringMap<GCStrategy*> GCStrategyMap;
158 
159 public:
160  /// Lookup the GCStrategy object associated with the given gc name.
161  /// Objects are owned internally; No caller should attempt to delete the
162  /// returned objects.
163  GCStrategy *getGCStrategy(const StringRef Name);
164 
165  /// List of per function info objects. In theory, Each of these
166  /// may be associated with a different GC.
167  using FuncInfoVec = std::vector<std::unique_ptr<GCFunctionInfo>>;
168 
169  FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
170  FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
171 
172 private:
173  /// Owning list of all GCFunctionInfos associated with this Module
174  FuncInfoVec Functions;
175 
176  /// Non-owning map to bypass linear search when finding the GCFunctionInfo
177  /// associated with a particular Function.
179  finfo_map_type FInfoMap;
180 
181 public:
183 
184  static char ID;
185 
186  GCModuleInfo();
187 
188  /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
189  /// call it in doFinalization().
190  ///
191  void clear();
192 
193  /// begin/end - Iterators for used strategies.
194  ///
195  iterator begin() const { return GCStrategyList.begin(); }
196  iterator end() const { return GCStrategyList.end(); }
197 
198  /// get - Look up function metadata. This is currently assumed
199  /// have the side effect of initializing the associated GCStrategy. That
200  /// will soon change.
201  GCFunctionInfo &getFunctionInfo(const Function &F);
202 };
203 
204 } // end namespace llvm
205 
206 #endif // LLVM_CODEGEN_GCMETADATA_H
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:147
DebugLoc Loc
Definition: GCMetadata.h:59
void addStackRoot(int Num, const Constant *Metadata)
addStackRoot - Registers a root that lives on the stack.
Definition: GCMetadata.h:114
This class represents lattice values for constants.
Definition: AllocatorList.h:24
size_t size() const
Definition: GCMetadata.h:137
std::vector< std::unique_ptr< GCFunctionInfo > > FuncInfoVec
List of per function info objects.
Definition: GCMetadata.h:167
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
SmallVector< std::unique_ptr< GCStrategy >, 1 >::const_iterator iterator
Definition: GCMetadata.h:182
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:67
size_t roots_size() const
Definition: GCMetadata.h:142
A debug info location.
Definition: DebugLoc.h:34
F(f)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Definition: BitVector.h:938
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:153
GCPoint - Metadata for a collector-safe point in machine code.
Definition: GCMetadata.h:57
GCPoint(MCSymbol *L, DebugLoc DL)
Definition: GCMetadata.h:61
int Num
Usually a frame index.
Definition: GCMetadata.h:68
roots_iterator roots_end()
Definition: GCMetadata.h:141
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:106
This is an important base class in LLVM.
Definition: Constant.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:119
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:109
void addSafePoint(MCSymbol *Label, const DebugLoc &DL)
addSafePoint - Notes the existence of a safe point.
Definition: GCMetadata.h:126
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:169
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:140
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:81
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:256
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:145
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:170
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
MCSymbol * Label
A label.
Definition: GCMetadata.h:58
const Constant * Metadata
Metadata straight from the call to llvm.gcroot.
Definition: GCMetadata.h:70
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:212
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:135
static char ID
Definition: GCMetadata.h:184
iterator end() const
Definition: GCMetadata.h:196
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:80
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
GCStrategy describes a garbage collector algorithm&#39;s code generation requirements, and provides overridable hooks for those needs which cannot be abstractly described.
Definition: GCStrategy.h:67
iterator begin() const
begin/end - Iterators for used strategies.
Definition: GCMetadata.h:195
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function&#39;s frame size.
Definition: GCMetadata.h:131
#define N
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:132
GCRoot(int N, const Constant *MD)
Definition: GCMetadata.h:73
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:82
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:146
Garbage collection metadata for a single function.
Definition: GCMetadata.h:78
Root of the metadata hierarchy.
Definition: Metadata.h:58