LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GCMetadata.h
Go to the documentation of this file.
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/Pass.h"
39 #include "llvm/Support/DebugLoc.h"
40 
41 namespace 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.
64 
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
75  ///< to llvm.gcroot.
76 
77  GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
78  };
79 
80 
81  /// GCFunctionInfo - Garbage collection metadata for a single function.
82  ///
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  GCStrategy &S;
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  GCFunctionInfo(const Function &F, GCStrategy &S);
108  ~GCFunctionInfo();
109 
110  /// getFunction - Return the function to which this metadata applies.
111  ///
112  const Function &getFunction() const { return F; }
113 
114  /// getStrategy - Return the GC strategy for the function.
115  ///
116  GCStrategy &getStrategy() { return S; }
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
120  // using MachineFrameInfo).
121  void addStackRoot(int Num, const Constant *Metadata) {
122  Roots.push_back(GCRoot(Num, Metadata));
123  }
124 
125  /// removeStackRoot - Removes a root.
127  return Roots.erase(position);
128  }
129 
130  /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
131  /// label just prior to the safe point (if the code generator is using
132  /// MachineModuleInfo).
134  SafePoints.push_back(GCPoint(Kind, Label, DL));
135  }
136 
137  /// getFrameSize/setFrameSize - Records the function's frame size.
138  ///
139  uint64_t getFrameSize() const { return FrameSize; }
140  void setFrameSize(uint64_t S) { FrameSize = S; }
141 
142  /// begin/end - Iterators for safe points.
143  ///
144  iterator begin() { return SafePoints.begin(); }
145  iterator end() { return SafePoints.end(); }
146  size_t size() const { return SafePoints.size(); }
147 
148  /// roots_begin/roots_end - Iterators for all roots in the function.
149  ///
150  roots_iterator roots_begin() { return Roots.begin(); }
151  roots_iterator roots_end () { return Roots.end(); }
152  size_t roots_size() const { return Roots.size(); }
153 
154  /// live_begin/live_end - Iterators for live roots at a given safe point.
155  ///
157  live_iterator live_end (const iterator &p) { return roots_end(); }
158  size_t live_size(const iterator &p) const { return roots_size(); }
159  };
160 
161 
162  /// GCModuleInfo - Garbage collection metadata for a whole module.
163  ///
164  class GCModuleInfo : public ImmutablePass {
166  typedef std::vector<GCStrategy*> list_type;
168 
169  strategy_map_type StrategyMap;
170  list_type StrategyList;
171  finfo_map_type FInfoMap;
172 
173  GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name);
174 
175  public:
176  typedef list_type::const_iterator iterator;
177 
178  static char ID;
179 
180  GCModuleInfo();
181  ~GCModuleInfo();
182 
183  /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
184  /// call it in doFinalization().
185  ///
186  void clear();
187 
188  /// begin/end - Iterators for used strategies.
189  ///
190  iterator begin() const { return StrategyList.begin(); }
191  iterator end() const { return StrategyList.end(); }
192 
193  /// get - Look up function metadata.
194  ///
196  };
197 
198 }
199 
200 #endif
DebugLoc Loc
Definition: GCMetadata.h:63
size_t roots_size() const
Definition: GCMetadata.h:152
void addStackRoot(int Num, const Constant *Metadata)
Definition: GCMetadata.h:121
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
int StackOffset
Offset from the stack pointer.
Definition: GCMetadata.h:73
F(f)
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:85
GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
Definition: GCMetadata.h:65
iterator end() const
Definition: GCMetadata.h:191
GCFunctionInfo & getFunctionInfo(const Function &F)
Definition: GCMetadata.cpp:90
GC::PointKind Kind
The kind of the safe point.
Definition: GCMetadata.h:61
list_type::const_iterator iterator
Definition: GCMetadata.h:176
int Num
Usually a frame index.
Definition: GCMetadata.h:72
roots_iterator roots_end()
Definition: GCMetadata.h:151
GCFunctionInfo(const Function &F, GCStrategy &S)
LLVM Constant Representation.
Definition: Constant.h:41
Instr is the return address of a call.
Definition: GCMetadata.h:54
Instr is a loop (backwards branch).
Definition: GCMetadata.h:51
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:126
GCStrategy & getStrategy()
Definition: GCMetadata.h:116
uint64_t getFrameSize() const
Definition: GCMetadata.h:139
roots_iterator roots_begin()
Definition: GCMetadata.h:150
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:158
live_iterator live_begin(const iterator &p)
Definition: GCMetadata.h:156
Instr is a return instruction.
Definition: GCMetadata.h:52
MCSymbol * Label
A label.
Definition: GCMetadata.h:62
const Constant * Metadata
Definition: GCMetadata.h:74
static char ID
Definition: GCMetadata.h:178
iterator begin() const
Definition: GCMetadata.h:190
size_t size() const
Definition: GCMetadata.h:146
#define N
Instr is a call instruction.
Definition: GCMetadata.h:53
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:140
GCRoot(int N, const Constant *MD)
Definition: GCMetadata.h:77
const Function & getFunction() const
Definition: GCMetadata.h:112
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:157
void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL)
Definition: GCMetadata.h:133
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:86
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:87