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