Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.6 KiB

  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/StringMap.h"
  36. #include "llvm/Pass.h"
  37. #include "llvm/Support/DebugLoc.h"
  38. namespace llvm {
  39. class AsmPrinter;
  40. class GCStrategy;
  41. class Constant;
  42. class MCSymbol;
  43. namespace GC {
  44. /// PointKind - The type of a collector-safe point.
  45. ///
  46. enum PointKind {
  47. Loop, ///< Instr is a loop (backwards branch).
  48. Return, ///< Instr is a return instruction.
  49. PreCall, ///< Instr is a call instruction.
  50. PostCall ///< Instr is the return address of a call.
  51. };
  52. }
  53. /// GCPoint - Metadata for a collector-safe point in machine code.
  54. ///
  55. struct GCPoint {
  56. GC::PointKind Kind; ///< The kind of the safe point.
  57. MCSymbol *Label; ///< A label.
  58. DebugLoc Loc;
  59. GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
  60. : Kind(K), Label(L), Loc(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; ///< 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), StackOffset(-1), Metadata(MD) {}
  70. };
  71. /// GCFunctionInfo - Garbage collection metadata for a single function.
  72. ///
  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;
  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. ///
  98. const Function &getFunction() const { return F; }
  99. /// getStrategy - Return the GC strategy for the function.
  100. ///
  101. GCStrategy &getStrategy() { return S; }
  102. /// addStackRoot - Registers a root that lives on the stack. Num is the
  103. /// stack object ID for the alloca (if the code generator is
  104. // using MachineFrameInfo).
  105. void addStackRoot(int Num, const Constant *Metadata) {
  106. Roots.push_back(GCRoot(Num, Metadata));
  107. }
  108. /// removeStackRoot - Removes a root.
  109. roots_iterator removeStackRoot(roots_iterator position) {
  110. return Roots.erase(position);
  111. }
  112. /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
  113. /// label just prior to the safe point (if the code generator is using
  114. /// MachineModuleInfo).
  115. void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) {
  116. SafePoints.push_back(GCPoint(Kind, Label, DL));
  117. }
  118. /// getFrameSize/setFrameSize - Records the function's frame size.
  119. ///
  120. uint64_t getFrameSize() const { return FrameSize; }
  121. void setFrameSize(uint64_t S) { FrameSize = S; }
  122. /// begin/end - Iterators for safe points.
  123. ///
  124. iterator begin() { return SafePoints.begin(); }
  125. iterator end() { return SafePoints.end(); }
  126. size_t size() const { return SafePoints.size(); }
  127. /// roots_begin/roots_end - Iterators for all roots in the function.
  128. ///
  129. roots_iterator roots_begin() { return Roots.begin(); }
  130. roots_iterator roots_end () { return Roots.end(); }
  131. size_t roots_size() const { return Roots.size(); }
  132. /// live_begin/live_end - Iterators for live roots at a given safe point.
  133. ///
  134. live_iterator live_begin(const iterator &p) { return roots_begin(); }
  135. live_iterator live_end (const iterator &p) { return roots_end(); }
  136. size_t live_size(const iterator &p) const { return roots_size(); }
  137. };
  138. /// GCModuleInfo - Garbage collection metadata for a whole module.
  139. ///
  140. class GCModuleInfo : public ImmutablePass {
  141. typedef StringMap<GCStrategy*> strategy_map_type;
  142. typedef std::vector<GCStrategy*> list_type;
  143. typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type;
  144. strategy_map_type StrategyMap;
  145. list_type StrategyList;
  146. finfo_map_type FInfoMap;
  147. GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name);
  148. public:
  149. typedef list_type::const_iterator iterator;
  150. static char ID;
  151. GCModuleInfo();
  152. ~GCModuleInfo();
  153. /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
  154. /// call it in doFinalization().
  155. ///
  156. void clear();
  157. /// begin/end - Iterators for used strategies.
  158. ///
  159. iterator begin() const { return StrategyList.begin(); }
  160. iterator end() const { return StrategyList.end(); }
  161. /// get - Look up function metadata.
  162. ///
  163. GCFunctionInfo &getFunctionInfo(const Function &F);
  164. };
  165. }
  166. #endif