CollectorMetadata abstractly describes stack maps for a function.
[oota-llvm.git] / include / llvm / CodeGen / CollectorMetadata.h
1 //===-- CollectorMetadata.h - Garbage collector metadata ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
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
20 // calls).
21 //
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
25 // live.
26 //
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.
32 //
33 //===----------------------------------------------------------------------===//
34
35 #ifndef LLVM_CODEGEN_COLLECTORMETADATA_H
36 #define LLVM_CODEGEN_COLLECTORMETADATA_H
37
38 #include "llvm/Pass.h"
39 #include "llvm/ADT/DenseMap.h"
40
41 namespace llvm {
42   
43   class Constant;
44   
45   
46   /// Creates a pass to print collector metadata.
47   /// 
48   Pass *createCollectorMetadataPrinter(std::ostream &OS);
49   
50   /// Creates a pass to destroy collector metadata.
51   /// 
52   Pass *createCollectorMetadataDeleter();
53   
54   
55   namespace GC {
56     /// PointKind - The type of a collector-safe point.
57     /// 
58     enum PointKind {
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.
63     };
64   }
65   
66   /// GCPoint - Metadata for a collector-safe point in machine code.
67   /// 
68   struct GCPoint {
69     GC::PointKind Kind; //< The kind of the safe point.
70     unsigned Num;       //< Usually a label.
71     
72     GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {}
73   };
74   
75   /// GCRoot - Metadata for a pointer to an object managed by the garbage
76   /// collector.
77   struct GCRoot {
78     int Num;            //< Usually a frame index.
79     int StackOffset;    //< Offset from the stack pointer.
80     Constant *Metadata; //< From the call to llvm.gcroot.
81     
82     GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
83   };
84   
85   
86   /// CollectorMetadata - Garbage collection metadata for a function.
87   /// 
88   class CollectorMetadata {
89   public:
90     typedef std::vector<GCPoint>::iterator iterator;
91     typedef std::vector<GCRoot>::iterator roots_iterator;
92     typedef std::vector<GCRoot>::const_iterator live_iterator;
93     
94   private:
95     const Function &F;
96     uint64_t FrameSize;
97     std::vector<GCRoot> Roots;
98     std::vector<GCPoint> SafePoints;
99     
100     // FIXME: Liveness. A 2D BitVector, perhaps?
101     // 
102     //   BitVector Liveness;
103     //   
104     //   bool islive(int point, int root) =
105     //     Liveness[point * SafePoints.size() + root]
106     // 
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).
109     
110     friend class CollectorModuleMetadata;
111     CollectorMetadata(const Function &F);
112     
113   public:
114     ~CollectorMetadata();
115     
116     const Function &getFunction() const { return F; }
117     
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));
123     }
124     
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));
130     }
131     
132     /// getFrameSize/setFrameSize - Records the function's frame size.
133     /// 
134     uint64_t getFrameSize() const { return FrameSize; }
135     void setFrameSize(uint64_t S) { FrameSize = S; }
136     
137     /// begin/end - Iterators for safe points.
138     /// 
139     iterator begin() { return SafePoints.begin(); }
140     iterator end()   { return SafePoints.end();   }
141     size_t size() const { return SafePoints.size(); }
142     
143     /// roots_begin/roots_end - Iterators for all roots in the function.
144     /// 
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(); }
148     
149     /// live_begin/live_end - Iterators for live roots at a given safe point.
150     /// 
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(); }
154   };
155   
156   
157   /// CollectorModuleMetadata - Garbage collection metadata for a whole module.
158   /// 
159   class CollectorModuleMetadata : public ImmutablePass {
160     typedef std::vector<CollectorMetadata*> list_type;
161     typedef DenseMap<const Function*,CollectorMetadata*> map_type;
162     
163     Module *Mod;
164     list_type Functions;
165     map_type Map;
166     
167   public:
168     typedef list_type::iterator iterator;
169     
170     static char ID;
171     
172     CollectorModuleMetadata();
173     ~CollectorModuleMetadata();
174     
175     /// clear - Used to delete module metadata. Collector invokes this as
176     /// necessary.
177     void clear();
178     
179     /// begin/end - Iterators for function metadata.
180     /// 
181     iterator begin() { return Functions.begin(); }
182     iterator end()   { return Functions.end();   }
183     
184     /// insert - Creates metadata for a function.
185     /// 
186     CollectorMetadata& insert(const Function *F);
187     
188     /// get - Looks up existing function metadata.
189     /// 
190     CollectorMetadata* get(const Function *F) const;
191   };
192   
193 }
194
195 #endif