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