make MachineFunction keep track of its ID and make
[oota-llvm.git] / include / llvm / CodeGen / GCMetadata.h
1 //===-- GCMetadata.h - Garbage collector metadata -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 // The GCFunctionInfo class logs the data necessary to build a type accurate
16 // stack map. The code generator outputs:
17 // 
18 //   - Safe points as specified by the GCStrategy's NeededSafePoints.
19 //   - Stack offsets for GC roots, as specified by calls to llvm.gcroot
20 //
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.
24 //
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.
30 //
31 //===----------------------------------------------------------------------===//
32
33 #ifndef LLVM_CODEGEN_GCMETADATA_H
34 #define LLVM_CODEGEN_GCMETADATA_H
35
36 #include "llvm/Pass.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/StringMap.h"
39
40 namespace llvm {
41   
42   class AsmPrinter;
43   class GCStrategy;
44   class Constant;
45   class MCAsmInfo;
46   
47   
48   namespace GC {
49     /// PointKind - The type of a collector-safe point.
50     /// 
51     enum PointKind {
52       Loop,    //< Instr is a loop (backwards branch).
53       Return,  //< Instr is a return instruction.
54       PreCall, //< Instr is a call instruction.
55       PostCall //< Instr is the return address of a call.
56     };
57   }
58   
59   /// GCPoint - Metadata for a collector-safe point in machine code.
60   /// 
61   struct GCPoint {
62     GC::PointKind Kind; //< The kind of the safe point.
63     unsigned Num;       //< Usually a label.
64     
65     GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {}
66   };
67   
68   /// GCRoot - Metadata for a pointer to an object managed by the garbage
69   /// collector.
70   struct GCRoot {
71     int Num;            //< Usually a frame index.
72     int StackOffset;    //< Offset from the stack pointer.
73     Constant *Metadata; //< Metadata straight from the call to llvm.gcroot.
74     
75     GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
76   };
77   
78   
79   /// GCFunctionInfo - Garbage collection metadata for a single function.
80   /// 
81   class GCFunctionInfo {
82   public:
83     typedef std::vector<GCPoint>::iterator iterator;
84     typedef std::vector<GCRoot>::iterator roots_iterator;
85     typedef std::vector<GCRoot>::const_iterator live_iterator;
86     
87   private:
88     const Function &F;
89     GCStrategy &S;
90     uint64_t FrameSize;
91     std::vector<GCRoot> Roots;
92     std::vector<GCPoint> SafePoints;
93     
94     // FIXME: Liveness. A 2D BitVector, perhaps?
95     // 
96     //   BitVector Liveness;
97     //   
98     //   bool islive(int point, int root) =
99     //     Liveness[point * SafePoints.size() + root]
100     // 
101     // The bit vector is the more compact representation where >3.2% of roots
102     // are live per safe point (1.5% on 64-bit hosts).
103     
104   public:
105     GCFunctionInfo(const Function &F, GCStrategy &S);
106     ~GCFunctionInfo();
107     
108     /// getFunction - Return the function to which this metadata applies.
109     /// 
110     const Function &getFunction() const { return F; }
111     
112     /// getStrategy - Return the GC strategy for the function.
113     /// 
114     GCStrategy &getStrategy() { return S; }
115     
116     /// addStackRoot - Registers a root that lives on the stack. Num is the
117     ///                stack object ID for the alloca (if the code generator is
118     //                 using  MachineFrameInfo).
119     void addStackRoot(int Num, Constant *Metadata) {
120       Roots.push_back(GCRoot(Num, Metadata));
121     }
122     
123     /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
124     /// label just prior to the safe point (if the code generator is using 
125     /// MachineModuleInfo).
126     void addSafePoint(GC::PointKind Kind, unsigned Num) {
127       SafePoints.push_back(GCPoint(Kind, Num));
128     }
129     
130     /// getFrameSize/setFrameSize - Records the function's frame size.
131     /// 
132     uint64_t getFrameSize() const { return FrameSize; }
133     void setFrameSize(uint64_t S) { FrameSize = S; }
134     
135     /// begin/end - Iterators for safe points.
136     /// 
137     iterator begin() { return SafePoints.begin(); }
138     iterator end()   { return SafePoints.end();   }
139     size_t size() const { return SafePoints.size(); }
140     
141     /// roots_begin/roots_end - Iterators for all roots in the function.
142     /// 
143     roots_iterator roots_begin() { return Roots.begin(); }
144     roots_iterator roots_end  () { return Roots.end();   }
145     size_t roots_size() const { return Roots.size(); }
146     
147     /// live_begin/live_end - Iterators for live roots at a given safe point.
148     /// 
149     live_iterator live_begin(const iterator &p) { return roots_begin(); }
150     live_iterator live_end  (const iterator &p) { return roots_end();   }
151     size_t live_size(const iterator &p) const { return roots_size(); }
152   };
153   
154   
155   /// GCModuleInfo - Garbage collection metadata for a whole module.
156   /// 
157   class GCModuleInfo : public ImmutablePass {
158     typedef StringMap<GCStrategy*> strategy_map_type;
159     typedef std::vector<GCStrategy*> list_type;
160     typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type;
161     
162     strategy_map_type StrategyMap;
163     list_type StrategyList;
164     finfo_map_type FInfoMap;
165     
166     GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name);
167     
168   public:
169     typedef list_type::const_iterator iterator;
170     
171     static char ID;
172     
173     GCModuleInfo();
174     ~GCModuleInfo();
175     
176     /// clear - Resets the pass. The metadata deleter pass calls this.
177     /// 
178     void clear();
179     
180     /// begin/end - Iterators for used strategies.
181     /// 
182     iterator begin() const { return StrategyList.begin(); }
183     iterator end()   const { return StrategyList.end();   }
184     
185     /// get - Look up function metadata.
186     /// 
187     GCFunctionInfo &getFunctionInfo(const Function &F);
188   };
189   
190 }
191
192 #endif