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