GCMetadata.h 7.4 KB

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