GCStrategy.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===- llvm/CodeGen/GCStrategy.h - Garbage collection -----------*- 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. // GCStrategy coordinates code generation algorithms and implements some itself
  10. // in order to generate code compatible with a target code generator as
  11. // specified in a function's 'gc' attribute. Algorithms are enabled by setting
  12. // flags in a subclass's constructor, and some virtual methods can be
  13. // overridden.
  14. //
  15. // GCStrategy is relevant for implementations using either gc.root or
  16. // gc.statepoint based lowering strategies, but is currently focused mostly on
  17. // options for gc.root. This will change over time.
  18. //
  19. // When requested by a subclass of GCStrategy, the gc.root implementation will
  20. // populate GCModuleInfo and GCFunctionInfo with that about each Function in
  21. // the Module that opts in to garbage collection. Specifically:
  22. //
  23. // - Safe points
  24. // Garbage collection is generally only possible at certain points in code.
  25. // GCStrategy can request that the collector insert such points:
  26. //
  27. // - At and after any call to a subroutine
  28. // - Before returning from the current function
  29. // - Before backwards branches (loops)
  30. //
  31. // - Roots
  32. // When a reference to a GC-allocated object exists on the stack, it must be
  33. // stored in an alloca registered with llvm.gcoot.
  34. //
  35. // This information can used to emit the metadata tables which are required by
  36. // the target garbage collector runtime.
  37. //
  38. // When used with gc.statepoint, information about safepoint and roots can be
  39. // found in the binary StackMap section after code generation. Safepoint
  40. // placement is currently the responsibility of the frontend, though late
  41. // insertion support is planned. gc.statepoint does not currently support
  42. // custom stack map formats; such can be generated by parsing the standard
  43. // stack map section if desired.
  44. //
  45. // The read and write barrier support can be used with either implementation.
  46. //
  47. //===----------------------------------------------------------------------===//
  48. #ifndef LLVM_IR_GCSTRATEGY_H
  49. #define LLVM_IR_GCSTRATEGY_H
  50. #include "llvm/ADT/None.h"
  51. #include "llvm/ADT/Optional.h"
  52. #include "llvm/Support/Registry.h"
  53. #include <string>
  54. namespace llvm {
  55. class Type;
  56. /// GCStrategy describes a garbage collector algorithm's code generation
  57. /// requirements, and provides overridable hooks for those needs which cannot
  58. /// be abstractly described. GCStrategy objects must be looked up through
  59. /// the Function. The objects themselves are owned by the Context and must
  60. /// be immutable.
  61. class GCStrategy {
  62. private:
  63. friend class GCModuleInfo;
  64. std::string Name;
  65. protected:
  66. bool UseStatepoints = false; /// Uses gc.statepoints as opposed to gc.roots,
  67. /// if set, none of the other options can be
  68. /// anything but their default values.
  69. bool NeededSafePoints = false; ///< if set, calls are inferred to be safepoints
  70. bool UsesMetadata = false; ///< If set, backend must emit metadata tables.
  71. public:
  72. GCStrategy();
  73. virtual ~GCStrategy() = default;
  74. /// Return the name of the GC strategy. This is the value of the collector
  75. /// name string specified on functions which use this strategy.
  76. const std::string &getName() const { return Name; }
  77. /// Returns true if this strategy is expecting the use of gc.statepoints,
  78. /// and false otherwise.
  79. bool useStatepoints() const { return UseStatepoints; }
  80. /** @name Statepoint Specific Properties */
  81. ///@{
  82. /// If the type specified can be reliably distinguished, returns true for
  83. /// pointers to GC managed locations and false for pointers to non-GC
  84. /// managed locations. Note a GCStrategy can always return 'None' (i.e. an
  85. /// empty optional indicating it can't reliably distinguish.
  86. virtual Optional<bool> isGCManagedPointer(const Type *Ty) const {
  87. return None;
  88. }
  89. ///@}
  90. /** @name GCRoot Specific Properties
  91. * These properties and overrides only apply to collector strategies using
  92. * GCRoot.
  93. */
  94. ///@{
  95. /// True if safe points need to be inferred on call sites
  96. bool needsSafePoints() const { return NeededSafePoints; }
  97. /// If set, appropriate metadata tables must be emitted by the back-end
  98. /// (assembler, JIT, or otherwise). For statepoint, this method is
  99. /// currently unsupported. The stackmap information can be found in the
  100. /// StackMap section as described in the documentation.
  101. bool usesMetadata() const { return UsesMetadata; }
  102. ///@}
  103. };
  104. /// Subclasses of GCStrategy are made available for use during compilation by
  105. /// adding them to the global GCRegistry. This can done either within the
  106. /// LLVM source tree or via a loadable plugin. An example registeration
  107. /// would be:
  108. /// static GCRegistry::Add<CustomGC> X("custom-name",
  109. /// "my custom supper fancy gc strategy");
  110. ///
  111. /// Note that to use a custom GCMetadataPrinter w/gc.roots, you must also
  112. /// register your GCMetadataPrinter subclass with the
  113. /// GCMetadataPrinterRegistery as well.
  114. using GCRegistry = Registry<GCStrategy>;
  115. } // end namespace llvm
  116. #endif // LLVM_IR_GCSTRATEGY_H