Eugene Zelenko | 90d9920 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1 | //===- BuiltinGCs.cpp - Boilerplate for our built in GC types -------------===// |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 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 contains the boilerplate required to define our various built in |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 11 | // gc lowering strategies. |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Philip Reames | 2f9bd23 | 2018-11-10 16:08:10 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/BuiltinGCs.h" |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GCStrategy.h" |
Eugene Zelenko | 90d9920 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/Support/Casting.h" |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | /// An example GC which attempts to be compatibile with Erlang/OTP garbage |
| 25 | /// collector. |
| 26 | /// |
| 27 | /// The frametable emitter is in ErlangGCPrinter.cpp. |
| 28 | class ErlangGC : public GCStrategy { |
| 29 | public: |
| 30 | ErlangGC() { |
Philip Reames | 4606305 | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 31 | NeededSafePoints = true; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 32 | UsesMetadata = true; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 33 | } |
| 34 | }; |
| 35 | |
| 36 | /// An example GC which attempts to be compatible with Objective Caml 3.10.0 |
| 37 | /// |
| 38 | /// The frametable emitter is in OcamlGCPrinter.cpp. |
| 39 | class OcamlGC : public GCStrategy { |
| 40 | public: |
| 41 | OcamlGC() { |
Philip Reames | 4606305 | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 42 | NeededSafePoints = true; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 43 | UsesMetadata = true; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | /// A GC strategy for uncooperative targets. This implements lowering for the |
| 48 | /// llvm.gc* intrinsics for targets that do not natively support them (which |
| 49 | /// includes the C backend). Note that the code generated is not quite as |
| 50 | /// efficient as algorithms which generate stack maps to identify roots. |
| 51 | /// |
| 52 | /// In order to support this particular transformation, all stack roots are |
| 53 | /// coallocated in the stack. This allows a fully target-independent stack map |
| 54 | /// while introducing only minor runtime overhead. |
| 55 | class ShadowStackGC : public GCStrategy { |
| 56 | public: |
Philip Reames | 0165418 | 2018-11-12 02:34:54 +0000 | [diff] [blame] | 57 | ShadowStackGC() {} |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /// A GCStrategy which serves as an example for the usage of a statepoint based |
| 61 | /// lowering strategy. This GCStrategy is intended to suitable as a default |
| 62 | /// implementation usable with any collector which can consume the standard |
| 63 | /// stackmap format generated by statepoints, uses the default addrespace to |
| 64 | /// distinguish between gc managed and non-gc managed pointers, and has |
| 65 | /// reasonable relocation semantics. |
| 66 | class StatepointGC : public GCStrategy { |
| 67 | public: |
| 68 | StatepointGC() { |
| 69 | UseStatepoints = true; |
| 70 | // These options are all gc.root specific, we specify them so that the |
| 71 | // gc.root lowering code doesn't run. |
Philip Reames | 4606305 | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 72 | NeededSafePoints = false; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 73 | UsesMetadata = false; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 74 | } |
Eugene Zelenko | 90d9920 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 75 | |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 76 | Optional<bool> isGCManagedPointer(const Type *Ty) const override { |
| 77 | // Method is only valid on pointer typed values. |
| 78 | const PointerType *PT = cast<PointerType>(Ty); |
| 79 | // For the sake of this example GC, we arbitrarily pick addrspace(1) as our |
| 80 | // GC managed heap. We know that a pointer into this heap needs to be |
| 81 | // updated and that no other pointer does. Note that addrspace(1) is used |
| 82 | // only as an example, it has no special meaning, and is not reserved for |
| 83 | // GC usage. |
| 84 | return (1 == PT->getAddressSpace()); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | /// A GCStrategy for the CoreCLR Runtime. The strategy is similar to |
| 89 | /// Statepoint-example GC, but differs from it in certain aspects, such as: |
| 90 | /// 1) Base-pointers need not be explicitly tracked and reported for |
| 91 | /// interior pointers |
| 92 | /// 2) Uses a different format for encoding stack-maps |
| 93 | /// 3) Location of Safe-point polls: polls are only needed before loop-back |
| 94 | /// edges and before tail-calls (not needed at function-entry) |
| 95 | /// |
| 96 | /// The above differences in behavior are to be implemented in upcoming |
| 97 | /// checkins. |
| 98 | class CoreCLRGC : public GCStrategy { |
| 99 | public: |
| 100 | CoreCLRGC() { |
| 101 | UseStatepoints = true; |
| 102 | // These options are all gc.root specific, we specify them so that the |
| 103 | // gc.root lowering code doesn't run. |
Philip Reames | 4606305 | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 104 | NeededSafePoints = false; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 105 | UsesMetadata = false; |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 106 | } |
Eugene Zelenko | 90d9920 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 107 | |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 108 | Optional<bool> isGCManagedPointer(const Type *Ty) const override { |
| 109 | // Method is only valid on pointer typed values. |
| 110 | const PointerType *PT = cast<PointerType>(Ty); |
| 111 | // We pick addrspace(1) as our GC managed heap. |
| 112 | return (1 == PT->getAddressSpace()); |
| 113 | } |
| 114 | }; |
Eugene Zelenko | 90d9920 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 115 | |
| 116 | } // end anonymous namespace |
Philip Reames | 263517d | 2016-01-19 03:57:18 +0000 | [diff] [blame] | 117 | |
| 118 | // Register all the above so that they can be found at runtime. Note that |
| 119 | // these static initializers are important since the registration list is |
| 120 | // constructed from their storage. |
| 121 | static GCRegistry::Add<ErlangGC> A("erlang", |
| 122 | "erlang-compatible garbage collector"); |
| 123 | static GCRegistry::Add<OcamlGC> B("ocaml", "ocaml 3.10-compatible GC"); |
| 124 | static GCRegistry::Add<ShadowStackGC> |
| 125 | C("shadow-stack", "Very portable GC for uncooperative code generators"); |
| 126 | static GCRegistry::Add<StatepointGC> D("statepoint-example", |
| 127 | "an example strategy for statepoint"); |
| 128 | static GCRegistry::Add<CoreCLRGC> E("coreclr", "CoreCLR-compatible GC"); |
| 129 | |
Philip Reames | b92d1fe | 2018-11-09 23:56:21 +0000 | [diff] [blame] | 130 | // Provide hook to ensure the containing library is fully loaded. |
| 131 | void llvm::linkAllBuiltinGCs() {} |