1 //===-- GCMetadata.h - Garbage collector metadata ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 // The GCFunctionInfo class logs the data necessary to build a type accurate
16 // stack map. The code generator outputs:
18 // - Safe points as specified by the GCStrategy's NeededSafePoints.
19 // - Stack offsets for GC roots, as specified by calls to llvm.gcroot
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.
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.
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_CODEGEN_GCMETADATA_H
34 #define LLVM_CODEGEN_GCMETADATA_H
36 #include "llvm/ADT/DenseMap.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringMap.h"
39 #include "llvm/CodeGen/GCStrategy.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/Pass.h"
50 /// GCPoint - Metadata for a collector-safe point in machine code.
53 GC::PointKind Kind; ///< The kind of the safe point.
54 MCSymbol *Label; ///< A label.
57 GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
58 : Kind(K), Label(L), Loc(std::move(DL)) {}
61 /// GCRoot - Metadata for a pointer to an object managed by the garbage
64 int Num; ///< Usually a frame index.
65 int StackOffset; ///< Offset from the stack pointer.
66 const Constant *Metadata; ///< Metadata straight from the call
69 GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
72 /// Garbage collection metadata for a single function. Currently, this
73 /// information only applies to GCStrategies which use GCRoot.
74 class GCFunctionInfo {
76 typedef std::vector<GCPoint>::iterator iterator;
77 typedef std::vector<GCRoot>::iterator roots_iterator;
78 typedef std::vector<GCRoot>::const_iterator live_iterator;
84 std::vector<GCRoot> Roots;
85 std::vector<GCPoint> SafePoints;
87 // FIXME: Liveness. A 2D BitVector, perhaps?
89 // BitVector Liveness;
91 // bool islive(int point, int root) =
92 // Liveness[point * SafePoints.size() + root]
94 // The bit vector is the more compact representation where >3.2% of roots
95 // are live per safe point (1.5% on 64-bit hosts).
98 GCFunctionInfo(const Function &F, GCStrategy &S);
101 /// getFunction - Return the function to which this metadata applies.
103 const Function &getFunction() const { return F; }
105 /// getStrategy - Return the GC strategy for the function.
107 GCStrategy &getStrategy() { return S; }
109 /// addStackRoot - Registers a root that lives on the stack. Num is the
110 /// stack object ID for the alloca (if the code generator is
111 // using MachineFrameInfo).
112 void addStackRoot(int Num, const Constant *Metadata) {
113 Roots.push_back(GCRoot(Num, Metadata));
116 /// removeStackRoot - Removes a root.
117 roots_iterator removeStackRoot(roots_iterator position) {
118 return Roots.erase(position);
121 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
122 /// label just prior to the safe point (if the code generator is using
123 /// MachineModuleInfo).
124 void addSafePoint(GC::PointKind Kind, MCSymbol *Label, const DebugLoc &DL) {
125 SafePoints.emplace_back(Kind, Label, DL);
128 /// getFrameSize/setFrameSize - Records the function's frame size.
130 uint64_t getFrameSize() const { return FrameSize; }
131 void setFrameSize(uint64_t S) { FrameSize = S; }
133 /// 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(); }
139 /// roots_begin/roots_end - Iterators for all roots in the function.
141 roots_iterator roots_begin() { return Roots.begin(); }
142 roots_iterator roots_end() { return Roots.end(); }
143 size_t roots_size() const { return Roots.size(); }
145 /// live_begin/live_end - Iterators for live roots at a given safe point.
147 live_iterator live_begin(const iterator &p) { return roots_begin(); }
148 live_iterator live_end(const iterator &p) { return roots_end(); }
149 size_t live_size(const iterator &p) const { return roots_size(); }
152 /// An analysis pass which caches information about the entire Module.
153 /// Records both the function level information used by GCRoots and a
154 /// cache of the 'active' gc strategy objects for the current Module.
155 class GCModuleInfo : public ImmutablePass {
156 /// An owning list of all GCStrategies which have been created
157 SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
158 /// A helper map to speedup lookups into the above list
159 StringMap<GCStrategy*> GCStrategyMap;
162 /// Lookup the GCStrategy object associated with the given gc name.
163 /// Objects are owned internally; No caller should attempt to delete the
164 /// returned objects.
165 GCStrategy *getGCStrategy(const StringRef Name);
167 /// List of per function info objects. In theory, Each of these
168 /// may be associated with a different GC.
169 typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
171 FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
172 FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
175 /// Owning list of all GCFunctionInfos associated with this Module
176 FuncInfoVec Functions;
178 /// Non-owning map to bypass linear search when finding the GCFunctionInfo
179 /// associated with a particular Function.
180 typedef DenseMap<const Function *, GCFunctionInfo *> finfo_map_type;
181 finfo_map_type FInfoMap;
184 typedef SmallVector<std::unique_ptr<GCStrategy>,1>::const_iterator iterator;
190 /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
191 /// call it in doFinalization().
195 /// begin/end - Iterators for used strategies.
197 iterator begin() const { return GCStrategyList.begin(); }
198 iterator end() const { return GCStrategyList.end(); }
200 /// get - Look up function metadata. This is currently assumed
201 /// have the side effect of initializing the associated GCStrategy. That
202 /// will soon change.
203 GCFunctionInfo &getFunctionInfo(const Function &F);