blob: abac555d6602552104cc2be931324208adc9c4da [file] [log] [blame]
Eugene Zelenkofa912a72017-02-27 22:45:06 +00001//===- BuiltinGCs.cpp - Boilerplate for our built in GC types -------------===//
Philip Reames31955002016-01-19 03:57:18 +00002//
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
Philip Reames31955002016-01-19 03:57:18 +000015#include "llvm/CodeGen/GCStrategy.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/GCs.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000017#include "llvm/IR/DerivedTypes.h"
18#include "llvm/Support/Casting.h"
Philip Reames31955002016-01-19 03:57:18 +000019
20using namespace llvm;
21
22namespace {
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.
28class ErlangGC : public GCStrategy {
29public:
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.
41class OcamlGC : public GCStrategy {
42public:
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.
57class ShadowStackGC : public GCStrategy {
58public:
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.
71class StatepointGC : public GCStrategy {
72public:
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 }
Eugene Zelenkofa912a72017-02-27 22:45:06 +000082
Philip Reames31955002016-01-19 03:57:18 +000083 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.
105class CoreCLRGC : public GCStrategy {
106public:
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 }
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000116
Philip Reames31955002016-01-19 03:57:18 +0000117 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};
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000124
125} // end anonymous namespace
Philip Reames31955002016-01-19 03:57:18 +0000126
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.
130static GCRegistry::Add<ErlangGC> A("erlang",
131 "erlang-compatible garbage collector");
132static GCRegistry::Add<OcamlGC> B("ocaml", "ocaml 3.10-compatible GC");
133static GCRegistry::Add<ShadowStackGC>
134 C("shadow-stack", "Very portable GC for uncooperative code generators");
135static GCRegistry::Add<StatepointGC> D("statepoint-example",
136 "an example strategy for statepoint");
137static GCRegistry::Add<CoreCLRGC> E("coreclr", "CoreCLR-compatible GC");
138
139// Provide hooks to ensure the containing library is fully loaded.
140void llvm::linkErlangGC() {}
141void llvm::linkOcamlGC() {}
142void llvm::linkShadowStackGC() {}
143void llvm::linkStatepointExampleGC() {}
144void llvm::linkCoreCLRGC() {}