0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===-- GCMetadata.h - Garbage collector metadata ---------------*- C++ -*-===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file declares the GCFunctionInfo and GCModuleInfo classes, which are
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // used as a communication channel from the target code generator to the target
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 // garbage collectors. This interface allows code generators and garbage
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 // collectors to be developed independently.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 // The GCFunctionInfo class logs the data necessary to build a type accurate
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 // stack map. The code generator outputs:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 // - Safe points as specified by the GCStrategy's NeededSafePoints.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 // - Stack offsets for GC roots, as specified by calls to llvm.gcroot
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 // As a refinement, liveness analysis calculates the set of live roots at each
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 // safe point. Liveness analysis is not presently performed by the code
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 // generator, so all roots are assumed live.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 // GCModuleInfo simply collects GCFunctionInfo instances for each Function as
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 // they are compiled. This accretion is necessary for collectors which must emit
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 // a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 // outlives the MachineFunction from which it is derived and must not refer to
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 // any code generator data structures.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 #ifndef LLVM_CODEGEN_GCMETADATA_H
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 #define LLVM_CODEGEN_GCMETADATA_H
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 #include "llvm/ADT/DenseMap.h"
|
83
|
37 #include "llvm/ADT/SmallVector.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 #include "llvm/ADT/StringMap.h"
|
83
|
39 #include "llvm/CodeGen/GCStrategy.h"
|
77
|
40 #include "llvm/IR/DebugLoc.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 #include "llvm/Pass.h"
|
77
|
42 #include <memory>
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 namespace llvm {
|
83
|
45 class AsmPrinter;
|
|
46 class Constant;
|
|
47 class MCSymbol;
|
|
48
|
|
49 /// GCPoint - Metadata for a collector-safe point in machine code.
|
|
50 ///
|
|
51 struct GCPoint {
|
|
52 GC::PointKind Kind; ///< The kind of the safe point.
|
|
53 MCSymbol *Label; ///< A label.
|
|
54 DebugLoc Loc;
|
|
55
|
|
56 GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
|
|
57 : Kind(K), Label(L), Loc(DL) {}
|
|
58 };
|
|
59
|
|
60 /// GCRoot - Metadata for a pointer to an object managed by the garbage
|
|
61 /// collector.
|
|
62 struct GCRoot {
|
|
63 int Num; ///< Usually a frame index.
|
|
64 int StackOffset; ///< Offset from the stack pointer.
|
|
65 const Constant *Metadata; ///< Metadata straight from the call
|
|
66 ///< to llvm.gcroot.
|
|
67
|
|
68 GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
|
|
69 };
|
|
70
|
|
71 /// Garbage collection metadata for a single function. Currently, this
|
|
72 /// information only applies to GCStrategies which use GCRoot.
|
|
73 class GCFunctionInfo {
|
|
74 public:
|
|
75 typedef std::vector<GCPoint>::iterator iterator;
|
|
76 typedef std::vector<GCRoot>::iterator roots_iterator;
|
|
77 typedef std::vector<GCRoot>::const_iterator live_iterator;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78
|
83
|
79 private:
|
|
80 const Function &F;
|
|
81 GCStrategy &S;
|
|
82 uint64_t FrameSize;
|
|
83 std::vector<GCRoot> Roots;
|
|
84 std::vector<GCPoint> SafePoints;
|
|
85
|
|
86 // FIXME: Liveness. A 2D BitVector, perhaps?
|
|
87 //
|
|
88 // BitVector Liveness;
|
|
89 //
|
|
90 // bool islive(int point, int root) =
|
|
91 // Liveness[point * SafePoints.size() + root]
|
|
92 //
|
|
93 // The bit vector is the more compact representation where >3.2% of roots
|
|
94 // are live per safe point (1.5% on 64-bit hosts).
|
|
95
|
|
96 public:
|
|
97 GCFunctionInfo(const Function &F, GCStrategy &S);
|
|
98 ~GCFunctionInfo();
|
|
99
|
|
100 /// getFunction - Return the function to which this metadata applies.
|
|
101 ///
|
|
102 const Function &getFunction() const { return F; }
|
|
103
|
|
104 /// getStrategy - Return the GC strategy for the function.
|
|
105 ///
|
|
106 GCStrategy &getStrategy() { return S; }
|
|
107
|
|
108 /// addStackRoot - Registers a root that lives on the stack. Num is the
|
|
109 /// stack object ID for the alloca (if the code generator is
|
|
110 // using MachineFrameInfo).
|
|
111 void addStackRoot(int Num, const Constant *Metadata) {
|
|
112 Roots.push_back(GCRoot(Num, Metadata));
|
|
113 }
|
|
114
|
|
115 /// removeStackRoot - Removes a root.
|
|
116 roots_iterator removeStackRoot(roots_iterator position) {
|
|
117 return Roots.erase(position);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119
|
83
|
120 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
|
|
121 /// label just prior to the safe point (if the code generator is using
|
|
122 /// MachineModuleInfo).
|
|
123 void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) {
|
|
124 SafePoints.push_back(GCPoint(Kind, Label, DL));
|
|
125 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
126
|
83
|
127 /// getFrameSize/setFrameSize - Records the function's frame size.
|
|
128 ///
|
|
129 uint64_t getFrameSize() const { return FrameSize; }
|
|
130 void setFrameSize(uint64_t S) { FrameSize = S; }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131
|
83
|
132 /// begin/end - Iterators for safe points.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 ///
|
83
|
134 iterator begin() { return SafePoints.begin(); }
|
|
135 iterator end() { return SafePoints.end(); }
|
|
136 size_t size() const { return SafePoints.size(); }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
137
|
83
|
138 /// roots_begin/roots_end - Iterators for all roots in the function.
|
|
139 ///
|
|
140 roots_iterator roots_begin() { return Roots.begin(); }
|
|
141 roots_iterator roots_end() { return Roots.end(); }
|
|
142 size_t roots_size() const { return Roots.size(); }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143
|
83
|
144 /// live_begin/live_end - Iterators for live roots at a given safe point.
|
|
145 ///
|
|
146 live_iterator live_begin(const iterator &p) { return roots_begin(); }
|
|
147 live_iterator live_end(const iterator &p) { return roots_end(); }
|
|
148 size_t live_size(const iterator &p) const { return roots_size(); }
|
|
149 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150
|
83
|
151 /// An analysis pass which caches information about the entire Module.
|
|
152 /// Records both the function level information used by GCRoots and a
|
|
153 /// cache of the 'active' gc strategy objects for the current Module.
|
|
154 class GCModuleInfo : public ImmutablePass {
|
|
155 /// An owning list of all GCStrategies which have been created
|
|
156 SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
|
|
157 /// A helper map to speedup lookups into the above list
|
|
158 StringMap<GCStrategy*> GCStrategyMap;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159
|
83
|
160 public:
|
|
161 /// Lookup the GCStrategy object associated with the given gc name.
|
|
162 /// Objects are owned internally; No caller should attempt to delete the
|
|
163 /// returned objects.
|
|
164 GCStrategy *getGCStrategy(const StringRef Name);
|
|
165
|
|
166 /// List of per function info objects. In theory, Each of these
|
|
167 /// may be associated with a different GC.
|
|
168 typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169
|
83
|
170 FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
|
|
171 FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172
|
83
|
173 private:
|
|
174 /// Owning list of all GCFunctionInfos associated with this Module
|
|
175 FuncInfoVec Functions;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176
|
83
|
177 /// Non-owning map to bypass linear search when finding the GCFunctionInfo
|
|
178 /// associated with a particular Function.
|
|
179 typedef DenseMap<const Function *, GCFunctionInfo *> finfo_map_type;
|
|
180 finfo_map_type FInfoMap;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181
|
83
|
182 public:
|
|
183 typedef SmallVector<std::unique_ptr<GCStrategy>,1>::const_iterator iterator;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184
|
83
|
185 static char ID;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186
|
83
|
187 GCModuleInfo();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188
|
83
|
189 /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
|
|
190 /// call it in doFinalization().
|
|
191 ///
|
|
192 void clear();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193
|
83
|
194 /// begin/end - Iterators for used strategies.
|
|
195 ///
|
|
196 iterator begin() const { return GCStrategyList.begin(); }
|
|
197 iterator end() const { return GCStrategyList.end(); }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198
|
83
|
199 /// get - Look up function metadata. This is currently assumed
|
|
200 /// have the side effect of initializing the associated GCStrategy. That
|
|
201 /// will soon change.
|
|
202 GCFunctionInfo &getFunctionInfo(const Function &F);
|
|
203 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206 #endif
|