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