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