LLVM  4.0.0
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/SmallVector.h"
38 #include "llvm/ADT/StringMap.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/Pass.h"
42 #include <memory>
43 #include <utility>
44 
45 namespace llvm {
46 class AsmPrinter;
47 class Constant;
48 class MCSymbol;
49 
50 /// GCPoint - Metadata for a collector-safe point in machine code.
51 ///
52 struct GCPoint {
53  GC::PointKind Kind; ///< The kind of the safe point.
54  MCSymbol *Label; ///< A label.
56 
58  : Kind(K), Label(L), Loc(std::move(DL)) {}
59 };
60 
61 /// GCRoot - Metadata for a pointer to an object managed by the garbage
62 /// collector.
63 struct GCRoot {
64  int Num; ///< Usually a frame index.
65  int StackOffset; ///< Offset from the stack pointer.
66  const Constant *Metadata; ///< Metadata straight from the call
67  ///< to llvm.gcroot.
68 
69  GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
70 };
71 
72 /// Garbage collection metadata for a single function. Currently, this
73 /// information only applies to GCStrategies which use GCRoot.
75 public:
76  typedef std::vector<GCPoint>::iterator iterator;
77  typedef std::vector<GCRoot>::iterator roots_iterator;
78  typedef std::vector<GCRoot>::const_iterator live_iterator;
79 
80 private:
81  const Function &F;
82  GCStrategy &S;
83  uint64_t FrameSize;
84  std::vector<GCRoot> Roots;
85  std::vector<GCPoint> SafePoints;
86 
87  // FIXME: Liveness. A 2D BitVector, perhaps?
88  //
89  // BitVector Liveness;
90  //
91  // bool islive(int point, int root) =
92  // Liveness[point * SafePoints.size() + root]
93  //
94  // The bit vector is the more compact representation where >3.2% of roots
95  // are live per safe point (1.5% on 64-bit hosts).
96 
97 public:
98  GCFunctionInfo(const Function &F, GCStrategy &S);
100 
101  /// getFunction - Return the function to which this metadata applies.
102  ///
103  const Function &getFunction() const { return F; }
104 
105  /// getStrategy - Return the GC strategy for the function.
106  ///
107  GCStrategy &getStrategy() { return S; }
108 
109  /// addStackRoot - Registers a root that lives on the stack. Num is the
110  /// stack object ID for the alloca (if the code generator is
111  // using MachineFrameInfo).
112  void addStackRoot(int Num, const Constant *Metadata) {
113  Roots.push_back(GCRoot(Num, Metadata));
114  }
115 
116  /// removeStackRoot - Removes a root.
118  return Roots.erase(position);
119  }
120 
121  /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
122  /// label just prior to the safe point (if the code generator is using
123  /// MachineModuleInfo).
124  void addSafePoint(GC::PointKind Kind, MCSymbol *Label, const DebugLoc &DL) {
125  SafePoints.emplace_back(Kind, Label, DL);
126  }
127 
128  /// getFrameSize/setFrameSize - Records the function's frame size.
129  ///
130  uint64_t getFrameSize() const { return FrameSize; }
131  void setFrameSize(uint64_t S) { FrameSize = S; }
132 
133  /// begin/end - Iterators for safe points.
134  ///
135  iterator begin() { return SafePoints.begin(); }
136  iterator end() { return SafePoints.end(); }
137  size_t size() const { return SafePoints.size(); }
138 
139  /// roots_begin/roots_end - Iterators for all roots in the function.
140  ///
141  roots_iterator roots_begin() { return Roots.begin(); }
142  roots_iterator roots_end() { return Roots.end(); }
143  size_t roots_size() const { return Roots.size(); }
144 
145  /// live_begin/live_end - Iterators for live roots at a given safe point.
146  ///
148  live_iterator live_end(const iterator &p) { return roots_end(); }
149  size_t live_size(const iterator &p) const { return roots_size(); }
150 };
151 
152 /// An analysis pass which caches information about the entire Module.
153 /// Records both the function level information used by GCRoots and a
154 /// cache of the 'active' gc strategy objects for the current Module.
155 class GCModuleInfo : public ImmutablePass {
156  /// An owning list of all GCStrategies which have been created
157  SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
158  /// A helper map to speedup lookups into the above list
159  StringMap<GCStrategy*> GCStrategyMap;
160 
161 public:
162  /// Lookup the GCStrategy object associated with the given gc name.
163  /// Objects are owned internally; No caller should attempt to delete the
164  /// returned objects.
166 
167  /// List of per function info objects. In theory, Each of these
168  /// may be associated with a different GC.
169  typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
170 
171  FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
172  FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
173 
174 private:
175  /// Owning list of all GCFunctionInfos associated with this Module
176  FuncInfoVec Functions;
177 
178  /// Non-owning map to bypass linear search when finding the GCFunctionInfo
179  /// associated with a particular Function.
180  typedef DenseMap<const Function *, GCFunctionInfo *> finfo_map_type;
181  finfo_map_type FInfoMap;
182 
183 public:
185 
186  static char ID;
187 
188  GCModuleInfo();
189 
190  /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
191  /// call it in doFinalization().
192  ///
193  void clear();
194 
195  /// begin/end - Iterators for used strategies.
196  ///
197  iterator begin() const { return GCStrategyList.begin(); }
198  iterator end() const { return GCStrategyList.end(); }
199 
200  /// get - Look up function metadata. This is currently assumed
201  /// have the side effect of initializing the associated GCStrategy. That
202  /// will soon change.
204 };
205 }
206 
207 #endif
MachineLoop * L
DebugLoc Loc
Definition: GCMetadata.h:55
size_t roots_size() const
Definition: GCMetadata.h:143
void addStackRoot(int Num, const Constant *Metadata)
addStackRoot - Registers a root that lives on the stack.
Definition: GCMetadata.h:112
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:63
A debug info location.
Definition: DebugLoc.h:34
int StackOffset
Offset from the stack pointer.
Definition: GCMetadata.h:65
PointKind
PointKind - Used to indicate whether the address of the call instruction or the address after the cal...
Definition: GCStrategy.h:67
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:76
GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
Definition: GCMetadata.h:57
std::vector< std::unique_ptr< GCFunctionInfo > > FuncInfoVec
List of per function info objects.
Definition: GCMetadata.h:169
iterator end() const
Definition: GCMetadata.h:198
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:61
void addSafePoint(GC::PointKind Kind, MCSymbol *Label, const DebugLoc &DL)
addSafePoint - Notes the existence of a safe point.
Definition: GCMetadata.h:124
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:155
GC::PointKind Kind
The kind of the safe point.
Definition: GCMetadata.h:53
GCPoint - Metadata for a collector-safe point in machine code.
Definition: GCMetadata.h:52
#define F(x, y, z)
Definition: MD5.cpp:51
int Num
Usually a frame index.
Definition: GCMetadata.h:64
roots_iterator roots_end()
Definition: GCMetadata.h:142
GCFunctionInfo(const Function &F, GCStrategy &S)
This is an important base class in LLVM.
Definition: Constant.h:42
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:117
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:107
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function's frame size.
Definition: GCMetadata.h:130
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:171
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:141
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:149
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:147
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:172
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:151
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:223
MCSymbol * Label
A label.
Definition: GCMetadata.h:54
const Constant * Metadata
Metadata straight from the call to llvm.gcroot.
Definition: GCMetadata.h:66
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:135
static char ID
Definition: GCMetadata.h:186
iterator begin() const
begin/end - Iterators for used strategies.
Definition: GCMetadata.h:197
GCStrategy describes a garbage collector algorithm's code generation requirements, and provides overridable hooks for those needs which cannot be abstractly described.
Definition: GCStrategy.h:78
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:76
size_t size() const
Definition: GCMetadata.h:137
#define N
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:131
const unsigned Kind
GCRoot(int N, const Constant *MD)
Definition: GCMetadata.h:69
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:103
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:148
Garbage collection metadata for a single function.
Definition: GCMetadata.h:74
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:77
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:78
Root of the metadata hierarchy.
Definition: Metadata.h:55
SmallVector< std::unique_ptr< GCStrategy >, 1 >::const_iterator iterator
Definition: GCMetadata.h:184