0f958a833867adf9ca2664b1c3be75319731552a
[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 collectors. 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 #include "llvm/ADT/StringMap.h"
41
42 namespace llvm {
43   
44   class AsmPrinter;
45   class Collector;
46   class Constant;
47   class TargetAsmInfo;
48   
49   
50   namespace GC {
51     /// PointKind - The type of a collector-safe point.
52     /// 
53     enum PointKind {
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.
58     };
59   }
60   
61   /// GCPoint - Metadata for a collector-safe point in machine code.
62   /// 
63   struct GCPoint {
64     GC::PointKind Kind; //< The kind of the safe point.
65     unsigned Num;       //< Usually a label.
66     
67     GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {}
68   };
69   
70   /// GCRoot - Metadata for a pointer to an object managed by the garbage
71   /// collector.
72   struct GCRoot {
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.
76     
77     GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
78   };
79   
80   
81   /// CollectorMetadata - Garbage collection metadata for a function.
82   /// 
83   class CollectorMetadata {
84   public:
85     typedef std::vector<GCPoint>::iterator iterator;
86     typedef std::vector<GCRoot>::iterator roots_iterator;
87     typedef std::vector<GCRoot>::const_iterator live_iterator;
88     
89   private:
90     const Function &F;
91     Collector &C;
92     uint64_t FrameSize;
93     std::vector<GCRoot> Roots;
94     std::vector<GCPoint> SafePoints;
95     
96     // FIXME: Liveness. A 2D BitVector, perhaps?
97     // 
98     //   BitVector Liveness;
99     //   
100     //   bool islive(int point, int root) =
101     //     Liveness[point * SafePoints.size() + root]
102     // 
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).
105     
106   public:
107     CollectorMetadata(const Function &F, Collector &C);
108     ~CollectorMetadata();
109     
110     /// getFunction - Return the function to which this metadata applies.
111     /// 
112     const Function &getFunction() const { return F; }
113     
114     /// getCollector - Return the collector for the function.
115     /// 
116     Collector &getCollector() { return C; }
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 StringMap<Collector*> collector_map_type;
161     typedef std::vector<Collector*> list_type;
162     typedef DenseMap<const Function*,CollectorMetadata*> function_map_type;
163     
164     collector_map_type NameMap;
165     list_type Collectors;
166     function_map_type Map;
167     
168     Collector *getOrCreateCollector(const Module *M, const std::string &Name);
169     
170   public:
171     typedef list_type::const_iterator iterator;
172     
173     static char ID;
174     
175     CollectorModuleMetadata();
176     ~CollectorModuleMetadata();
177     
178     /// clear - Used to delete module metadata. The metadata deleter pass calls
179     /// this.
180     void clear();
181     
182     /// begin/end - Iterators for collectors.
183     /// 
184     iterator begin() const { return Collectors.begin(); }
185     iterator end()   const { return Collectors.end();   }
186     
187     /// get - Look up function metadata.
188     /// 
189     CollectorMetadata &get(const Function &F);
190   };
191   
192 }
193
194 #endif