GCMetadata.h 7.2 KB

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