1 //===-- CollectorMetadata.h - Garbage collector metadata ------------------===//
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 CollectorMetadata and CollectorModuleMetadata classes,
11 // which are used as a communication channel from the target code generator
12 // to the target garbage collectors. This interface allows code generators and
13 // garbage collectors to be developed independently.
15 // The CollectorMetadata class records the data necessary to build a type
16 // accurate stack map. Roots are specified in the LLVM IR using the llvm.gcroot
17 // intrinsic, which the code generator understands. The code generator records
18 // the stack offset for each GC root. Safe points are generated by the code
19 // generator according to the collector's declared needs (generally at function
22 // Safe points and roots are sufficient to build type-accurate stack maps. As a
23 // refinement, liveness analysis calculates the set of live roots at each safe
24 // point. Liveness analysis is not presently performed, so all roots are assumed
27 // CollectorModuleMetadata simply collects CollectorMetadata structures for each
28 // Function as it is compiled. This is necessary for collectors which must emit
29 // a stack map for the entire compilation unit. CollectorMetadata outlives the
30 // MachineFunction from which it is derived, so must not refer to any code
31 // generator data structures.
33 //===----------------------------------------------------------------------===//
35 #ifndef LLVM_CODEGEN_COLLECTORMETADATA_H
36 #define LLVM_CODEGEN_COLLECTORMETADATA_H
38 #include "llvm/Pass.h"
39 #include "llvm/ADT/DenseMap.h"
40 #include "llvm/ADT/StringMap.h"
51 /// PointKind - The type of a collector-safe point.
54 Loop, //< Instr is a loop (backwards branch).
55 Return, //< Instr is a return instruction.
56 PreCall, //< Instr is a call instruction.
57 PostCall //< Instr is the return address of a call.
61 /// GCPoint - Metadata for a collector-safe point in machine code.
64 GC::PointKind Kind; //< The kind of the safe point.
65 unsigned Num; //< Usually a label.
67 GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {}
70 /// GCRoot - Metadata for a pointer to an object managed by the garbage
73 int Num; //< Usually a frame index.
74 int StackOffset; //< Offset from the stack pointer.
75 Constant *Metadata; //< Metadata straight from the call to llvm.gcroot.
77 GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
81 /// CollectorMetadata - Garbage collection metadata for a function.
83 class CollectorMetadata {
85 typedef std::vector<GCPoint>::iterator iterator;
86 typedef std::vector<GCRoot>::iterator roots_iterator;
87 typedef std::vector<GCRoot>::const_iterator live_iterator;
93 std::vector<GCRoot> Roots;
94 std::vector<GCPoint> SafePoints;
96 // FIXME: Liveness. A 2D BitVector, perhaps?
98 // BitVector Liveness;
100 // bool islive(int point, int root) =
101 // Liveness[point * SafePoints.size() + root]
103 // The bit vector is the more compact representation where >3.2% of roots
104 // are live per safe point (1.5% on 64-bit hosts).
107 CollectorMetadata(const Function &F, Collector &C);
108 ~CollectorMetadata();
110 /// getFunction - Return the function to which this metadata applies.
112 const Function &getFunction() const { return F; }
114 /// getCollector - Return the collector for the function.
116 Collector &getCollector() { return C; }
118 /// addStackRoot - Registers a root that lives on the stack. Num is the
119 /// stack object ID for the alloca (if the code generator is using
120 /// MachineFrameInfo).
121 void addStackRoot(int Num, Constant *Metadata) {
122 Roots.push_back(GCRoot(Num, Metadata));
125 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
126 /// label just prior to the safe point (if the code generator is using
127 /// MachineModuleInfo).
128 void addSafePoint(GC::PointKind Kind, unsigned Num) {
129 SafePoints.push_back(GCPoint(Kind, Num));
132 /// getFrameSize/setFrameSize - Records the function's frame size.
134 uint64_t getFrameSize() const { return FrameSize; }
135 void setFrameSize(uint64_t S) { FrameSize = S; }
137 /// 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(); }
143 /// roots_begin/roots_end - Iterators for all roots in the function.
145 roots_iterator roots_begin() { return Roots.begin(); }
146 roots_iterator roots_end () { return Roots.end(); }
147 size_t roots_size() const { return Roots.size(); }
149 /// live_begin/live_end - Iterators for live roots at a given safe point.
151 live_iterator live_begin(const iterator &p) { return roots_begin(); }
152 live_iterator live_end (const iterator &p) { return roots_end(); }
153 size_t live_size(const iterator &p) const { return roots_size(); }
157 /// CollectorModuleMetadata - Garbage collection metadata for a whole module.
159 class CollectorModuleMetadata : public ImmutablePass {
160 typedef StringMap<Collector*> collector_map_type;
161 typedef std::vector<Collector*> list_type;
162 typedef DenseMap<const Function*,CollectorMetadata*> function_map_type;
164 collector_map_type NameMap;
165 list_type Collectors;
166 function_map_type Map;
168 Collector *getOrCreateCollector(const Module *M, const std::string &Name);
171 typedef list_type::const_iterator iterator;
175 CollectorModuleMetadata();
176 ~CollectorModuleMetadata();
178 /// clear - Used to delete module metadata. The metadata deleter pass calls
182 /// begin/end - Iterators for collectors.
184 iterator begin() const { return Collectors.begin(); }
185 iterator end() const { return Collectors.end(); }
187 /// get - Look up function metadata.
189 CollectorMetadata &get(const Function &F);