GCStrategy.h 5.5 KB

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