1 //===-- CollectorMetadata.h - Garbage collector metadata ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Gordon Henriksen and is distributed under
6 // the University of Illinois Open Source 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 collector. 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"
46 /// Creates a pass to print collector metadata.
48 Pass *createCollectorMetadataPrinter(std::ostream &OS);
50 /// Creates a pass to destroy collector metadata.
52 Pass *createCollectorMetadataDeleter();
56 /// PointKind - The type of a collector-safe point.
59 Loop, //< Instr is a loop (backwards branch).
60 Return, //< Instr is a return instruction.
61 PreCall, //< Instr is a call instruction.
62 PostCall //< Instr is the return address of a call.
66 /// GCPoint - Metadata for a collector-safe point in machine code.
69 GC::PointKind Kind; //< The kind of the safe point.
70 unsigned Num; //< Usually a label.
72 GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {}
75 /// GCRoot - Metadata for a pointer to an object managed by the garbage
78 int Num; //< Usually a frame index.
79 int StackOffset; //< Offset from the stack pointer.
80 Constant *Metadata; //< From the call to llvm.gcroot.
82 GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
86 /// CollectorMetadata - Garbage collection metadata for a function.
88 class CollectorMetadata {
90 typedef std::vector<GCPoint>::iterator iterator;
91 typedef std::vector<GCRoot>::iterator roots_iterator;
92 typedef std::vector<GCRoot>::const_iterator live_iterator;
97 std::vector<GCRoot> Roots;
98 std::vector<GCPoint> SafePoints;
100 // FIXME: Liveness. A 2D BitVector, perhaps?
102 // BitVector Liveness;
104 // bool islive(int point, int root) =
105 // Liveness[point * SafePoints.size() + root]
107 // The bit vector is the more compact representation where >3.2% of roots
108 // are live per safe point (1.5% on 64-bit hosts).
110 friend class CollectorModuleMetadata;
111 CollectorMetadata(const Function &F);
114 ~CollectorMetadata();
116 const Function &getFunction() const { return F; }
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 std::vector<CollectorMetadata*> list_type;
161 typedef DenseMap<const Function*,CollectorMetadata*> map_type;
168 typedef list_type::iterator iterator;
172 CollectorModuleMetadata();
173 ~CollectorModuleMetadata();
175 /// clear - Used to delete module metadata. Collector invokes this as
179 /// begin/end - Iterators for function metadata.
181 iterator begin() { return Functions.begin(); }
182 iterator end() { return Functions.end(); }
184 /// insert - Creates metadata for a function.
186 CollectorMetadata& insert(const Function *F);
188 /// get - Looks up existing function metadata.
190 CollectorMetadata* get(const Function *F) const;