GCMetadata.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifndef LLVM_CODEGEN_GCMETADATA_H
  33. #define LLVM_CODEGEN_GCMETADATA_H
  34. #include "llvm/ADT/DenseMap.h"
  35. #include "llvm/ADT/SmallVector.h"
  36. #include "llvm/ADT/StringMap.h"
  37. #include "llvm/CodeGen/GCStrategy.h"
  38. #include "llvm/IR/DebugLoc.h"
  39. #include "llvm/Pass.h"
  40. #include <memory>
  41. namespace llvm {
  42. class AsmPrinter;
  43. class Constant;
  44. class MCSymbol;
  45. /// GCPoint - Metadata for a collector-safe point in machine code.
  46. ///
  47. struct GCPoint {
  48. GC::PointKind Kind; ///< The kind of the safe point.
  49. MCSymbol *Label; ///< A label.
  50. DebugLoc Loc;
  51. GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
  52. : Kind(K), Label(L), Loc(DL) {}
  53. };
  54. /// GCRoot - Metadata for a pointer to an object managed by the garbage
  55. /// collector.
  56. struct GCRoot {
  57. int Num; ///< Usually a frame index.
  58. int StackOffset; ///< Offset from the stack pointer.
  59. const Constant *Metadata; ///< Metadata straight from the call
  60. ///< to llvm.gcroot.
  61. GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
  62. };
  63. /// Garbage collection metadata for a single function. Currently, this
  64. /// information only applies to GCStrategies which use GCRoot.
  65. class GCFunctionInfo {
  66. public:
  67. typedef std::vector<GCPoint>::iterator iterator;
  68. typedef std::vector<GCRoot>::iterator roots_iterator;
  69. typedef std::vector<GCRoot>::const_iterator live_iterator;
  70. private:
  71. const Function &F;
  72. GCStrategy &S;
  73. uint64_t FrameSize;
  74. std::vector<GCRoot> Roots;
  75. std::vector<GCPoint> SafePoints;
  76. // FIXME: Liveness. A 2D BitVector, perhaps?
  77. //
  78. // BitVector Liveness;
  79. //
  80. // bool islive(int point, int root) =
  81. // Liveness[point * SafePoints.size() + root]
  82. //
  83. // The bit vector is the more compact representation where >3.2% of roots
  84. // are live per safe point (1.5% on 64-bit hosts).
  85. public:
  86. GCFunctionInfo(const Function &F, GCStrategy &S);
  87. ~GCFunctionInfo();
  88. /// getFunction - Return the function to which this metadata applies.
  89. ///
  90. const Function &getFunction() const { return F; }
  91. /// getStrategy - Return the GC strategy for the function.
  92. ///
  93. GCStrategy &getStrategy() { return S; }
  94. /// addStackRoot - Registers a root that lives on the stack. Num is the
  95. /// stack object ID for the alloca (if the code generator is
  96. // using MachineFrameInfo).
  97. void addStackRoot(int Num, const Constant *Metadata) {
  98. Roots.push_back(GCRoot(Num, Metadata));
  99. }
  100. /// removeStackRoot - Removes a root.
  101. roots_iterator removeStackRoot(roots_iterator position) {
  102. return Roots.erase(position);
  103. }
  104. /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
  105. /// label just prior to the safe point (if the code generator is using
  106. /// MachineModuleInfo).
  107. void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) {
  108. SafePoints.emplace_back(Kind, Label, DL);
  109. }
  110. /// getFrameSize/setFrameSize - Records the function's frame size.
  111. ///
  112. uint64_t getFrameSize() const { return FrameSize; }
  113. void setFrameSize(uint64_t S) { FrameSize = S; }
  114. /// begin/end - Iterators for safe points.
  115. ///
  116. iterator begin() { return SafePoints.begin(); }
  117. iterator end() { return SafePoints.end(); }
  118. size_t size() const { return SafePoints.size(); }
  119. /// roots_begin/roots_end - Iterators for all roots in the function.
  120. ///
  121. roots_iterator roots_begin() { return Roots.begin(); }
  122. roots_iterator roots_end() { return Roots.end(); }
  123. size_t roots_size() const { return Roots.size(); }
  124. /// live_begin/live_end - Iterators for live roots at a given safe point.
  125. ///
  126. live_iterator live_begin(const iterator &p) { return roots_begin(); }
  127. live_iterator live_end(const iterator &p) { return roots_end(); }
  128. size_t live_size(const iterator &p) const { return roots_size(); }
  129. };
  130. /// An analysis pass which caches information about the entire Module.
  131. /// Records both the function level information used by GCRoots and a
  132. /// cache of the 'active' gc strategy objects for the current Module.
  133. class GCModuleInfo : public ImmutablePass {
  134. /// An owning list of all GCStrategies which have been created
  135. SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
  136. /// A helper map to speedup lookups into the above list
  137. StringMap<GCStrategy*> GCStrategyMap;
  138. public:
  139. /// Lookup the GCStrategy object associated with the given gc name.
  140. /// Objects are owned internally; No caller should attempt to delete the
  141. /// returned objects.
  142. GCStrategy *getGCStrategy(const StringRef Name);
  143. /// List of per function info objects. In theory, Each of these
  144. /// may be associated with a different GC.
  145. typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
  146. FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
  147. FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
  148. private:
  149. /// Owning list of all GCFunctionInfos associated with this Module
  150. FuncInfoVec Functions;
  151. /// Non-owning map to bypass linear search when finding the GCFunctionInfo
  152. /// associated with a particular Function.
  153. typedef DenseMap<const Function *, GCFunctionInfo *> finfo_map_type;
  154. finfo_map_type FInfoMap;
  155. public:
  156. typedef SmallVector<std::unique_ptr<GCStrategy>,1>::const_iterator iterator;
  157. static char ID;
  158. GCModuleInfo();
  159. /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
  160. /// call it in doFinalization().
  161. ///
  162. void clear();
  163. /// begin/end - Iterators for used strategies.
  164. ///
  165. iterator begin() const { return GCStrategyList.begin(); }
  166. iterator end() const { return GCStrategyList.end(); }
  167. /// get - Look up function metadata. This is currently assumed
  168. /// have the side effect of initializing the associated GCStrategy. That
  169. /// will soon change.
  170. GCFunctionInfo &getFunctionInfo(const Function &F);
  171. };
  172. }
  173. #endif