19aa0b031f36dd90c14a17b95fc23e9aa9c1c496
[oota-llvm.git] / include / llvm / CodeGen / GCMetadata.h
1 //===-- GCMetadata.h - Garbage collector metadata ---------------*- C++ -*-===//
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/ADT/DenseMap.h"
37 #include "llvm/ADT/StringMap.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/GCStrategy.h"
40 #include "llvm/Pass.h"
41 #include <memory>
42
43 namespace llvm {
44 class AsmPrinter;
45 class Constant;
46 class MCSymbol;
47
48 /// GCPoint - Metadata for a collector-safe point in machine code.
49 ///
50 struct GCPoint {
51   GC::PointKind Kind; ///< The kind of the safe point.
52   MCSymbol *Label;    ///< A label.
53   DebugLoc Loc;
54
55   GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
56       : Kind(K), Label(L), Loc(DL) {}
57 };
58
59 /// GCRoot - Metadata for a pointer to an object managed by the garbage
60 /// collector.
61 struct GCRoot {
62   int Num;                  ///< Usually a frame index.
63   int StackOffset;          ///< Offset from the stack pointer.
64   const Constant *Metadata; ///< Metadata straight from the call
65                             ///< to llvm.gcroot.
66
67   GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
68 };
69
70 /// Garbage collection metadata for a single function.  Currently, this
71 /// information only applies to GCStrategies which use GCRoot.
72 class GCFunctionInfo {
73 public:
74   typedef std::vector<GCPoint>::iterator iterator;
75   typedef std::vector<GCRoot>::iterator roots_iterator;
76   typedef std::vector<GCRoot>::const_iterator live_iterator;
77
78 private:
79   const Function &F;
80   GCStrategy &S;
81   uint64_t FrameSize;
82   std::vector<GCRoot> Roots;
83   std::vector<GCPoint> SafePoints;
84
85   // FIXME: Liveness. A 2D BitVector, perhaps?
86   //
87   //   BitVector Liveness;
88   //
89   //   bool islive(int point, int root) =
90   //     Liveness[point * SafePoints.size() + root]
91   //
92   // The bit vector is the more compact representation where >3.2% of roots
93   // are live per safe point (1.5% on 64-bit hosts).
94
95 public:
96   GCFunctionInfo(const Function &F, GCStrategy &S);
97   ~GCFunctionInfo();
98
99   /// getFunction - Return the function to which this metadata applies.
100   ///
101   const Function &getFunction() const { return F; }
102
103   /// getStrategy - Return the GC strategy for the function.
104   ///
105   GCStrategy &getStrategy() { return S; }
106
107   /// addStackRoot - Registers a root that lives on the stack. Num is the
108   ///                stack object ID for the alloca (if the code generator is
109   //                 using  MachineFrameInfo).
110   void addStackRoot(int Num, const Constant *Metadata) {
111     Roots.push_back(GCRoot(Num, Metadata));
112   }
113
114   /// removeStackRoot - Removes a root.
115   roots_iterator removeStackRoot(roots_iterator position) {
116     return Roots.erase(position);
117   }
118
119   /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
120   /// label just prior to the safe point (if the code generator is using
121   /// MachineModuleInfo).
122   void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) {
123     SafePoints.push_back(GCPoint(Kind, Label, DL));
124   }
125
126   /// getFrameSize/setFrameSize - Records the function's frame size.
127   ///
128   uint64_t getFrameSize() const { return FrameSize; }
129   void setFrameSize(uint64_t S) { FrameSize = S; }
130
131   /// begin/end - Iterators for safe points.
132   ///
133   iterator begin() { return SafePoints.begin(); }
134   iterator end() { return SafePoints.end(); }
135   size_t size() const { return SafePoints.size(); }
136
137   /// roots_begin/roots_end - Iterators for all roots in the function.
138   ///
139   roots_iterator roots_begin() { return Roots.begin(); }
140   roots_iterator roots_end() { return Roots.end(); }
141   size_t roots_size() const { return Roots.size(); }
142
143   /// live_begin/live_end - Iterators for live roots at a given safe point.
144   ///
145   live_iterator live_begin(const iterator &p) { return roots_begin(); }
146   live_iterator live_end(const iterator &p) { return roots_end(); }
147   size_t live_size(const iterator &p) const { return roots_size(); }
148 };
149
150 /// An analysis pass which caches information about the entire Module.
151 /// Records both the function level information used by GCRoots and a
152 /// cache of the 'active' gc strategy objects for the current Module.
153 class GCModuleInfo : public ImmutablePass {
154   /// A list of GCStrategies which are active in this Module.  These are
155   /// not owning pointers.
156   std::vector<GCStrategy *> StrategyList;
157
158 public:
159   /// List of per function info objects.  In theory, Each of these
160   /// may be associated with a different GC.
161   typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
162
163   FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
164   FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
165
166 private:
167   /// Owning list of all GCFunctionInfos associated with this Module
168   FuncInfoVec Functions;
169
170   /// Non-owning map to bypass linear search when finding the GCFunctionInfo
171   /// associated with a particular Function.
172   typedef DenseMap<const Function *, GCFunctionInfo *> finfo_map_type;
173   finfo_map_type FInfoMap;
174
175 public:
176   typedef std::vector<GCStrategy *>::const_iterator iterator;
177
178   static char ID;
179
180   GCModuleInfo();
181
182   /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
183   /// call it in doFinalization().
184   ///
185   void clear();
186
187   /// begin/end - Iterators for used strategies.
188   ///
189   iterator begin() const { return StrategyList.begin(); }
190   iterator end() const { return StrategyList.end(); }
191
192   /// get - Look up function metadata.  This is currently assumed
193   /// have the side effect of initializing the associated GCStrategy.  That
194   /// will soon change.
195   GCFunctionInfo &getFunctionInfo(const Function &F);
196 };
197 }
198
199 #endif