1 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the GCFunctionInfo class and GCModuleInfo pass.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/CodeGen/GCMetadata.h"
15 #include "llvm/CodeGen/GCStrategy.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
31 class Printer
: public FunctionPass
{
37 explicit Printer(raw_ostream
&OS
) : FunctionPass(ID
), OS(OS
) {}
39 StringRef
getPassName() const override
;
40 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
42 bool runOnFunction(Function
&F
) override
;
43 bool doFinalization(Module
&M
) override
;
46 } // end anonymous namespace
48 INITIALIZE_PASS(GCModuleInfo
, "collector-metadata",
49 "Create Garbage Collector Module Metadata", false, false)
51 // -----------------------------------------------------------------------------
53 GCFunctionInfo::GCFunctionInfo(const Function
&F
, GCStrategy
&S
)
54 : F(F
), S(S
), FrameSize(~0LL) {}
56 GCFunctionInfo::~GCFunctionInfo() = default;
58 // -----------------------------------------------------------------------------
60 char GCModuleInfo::ID
= 0;
62 GCModuleInfo::GCModuleInfo() : ImmutablePass(ID
) {
63 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
66 GCFunctionInfo
&GCModuleInfo::getFunctionInfo(const Function
&F
) {
67 assert(!F
.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
70 finfo_map_type::iterator I
= FInfoMap
.find(&F
);
71 if (I
!= FInfoMap
.end())
74 GCStrategy
*S
= getGCStrategy(F
.getGC());
75 Functions
.push_back(llvm::make_unique
<GCFunctionInfo
>(F
, *S
));
76 GCFunctionInfo
*GFI
= Functions
.back().get();
81 void GCModuleInfo::clear() {
84 GCStrategyList
.clear();
87 // -----------------------------------------------------------------------------
91 FunctionPass
*llvm::createGCInfoPrinter(raw_ostream
&OS
) {
92 return new Printer(OS
);
95 StringRef
Printer::getPassName() const {
96 return "Print Garbage Collector Information";
99 void Printer::getAnalysisUsage(AnalysisUsage
&AU
) const {
100 FunctionPass::getAnalysisUsage(AU
);
101 AU
.setPreservesAll();
102 AU
.addRequired
<GCModuleInfo
>();
105 bool Printer::runOnFunction(Function
&F
) {
109 GCFunctionInfo
*FD
= &getAnalysis
<GCModuleInfo
>().getFunctionInfo(F
);
111 OS
<< "GC roots for " << FD
->getFunction().getName() << ":\n";
112 for (GCFunctionInfo::roots_iterator RI
= FD
->roots_begin(),
113 RE
= FD
->roots_end();
115 OS
<< "\t" << RI
->Num
<< "\t" << RI
->StackOffset
<< "[sp]\n";
117 OS
<< "GC safe points for " << FD
->getFunction().getName() << ":\n";
118 for (GCFunctionInfo::iterator PI
= FD
->begin(), PE
= FD
->end(); PI
!= PE
;
121 OS
<< "\t" << PI
->Label
->getName() << ": " << "post-call"
124 for (GCFunctionInfo::live_iterator RI
= FD
->live_begin(PI
),
125 RE
= FD
->live_end(PI
);
127 OS
<< " " << RI
->Num
;
139 bool Printer::doFinalization(Module
&M
) {
140 GCModuleInfo
*GMI
= getAnalysisIfAvailable
<GCModuleInfo
>();
141 assert(GMI
&& "Printer didn't require GCModuleInfo?!");
146 GCStrategy
*GCModuleInfo::getGCStrategy(const StringRef Name
) {
147 // TODO: Arguably, just doing a linear search would be faster for small N
148 auto NMI
= GCStrategyMap
.find(Name
);
149 if (NMI
!= GCStrategyMap
.end())
150 return NMI
->getValue();
152 for (auto& Entry
: GCRegistry::entries()) {
153 if (Name
== Entry
.getName()) {
154 std::unique_ptr
<GCStrategy
> S
= Entry
.instantiate();
156 GCStrategyMap
[Name
] = S
.get();
157 GCStrategyList
.push_back(std::move(S
));
158 return GCStrategyList
.back().get();
162 if (GCRegistry::begin() == GCRegistry::end()) {
163 // In normal operation, the registry should not be empty. There should
164 // be the builtin GCs if nothing else. The most likely scenario here is
165 // that we got here without running the initializers used by the Registry
166 // itself and it's registration mechanism.
167 const std::string error
= ("unsupported GC: " + Name
).str() +
168 " (did you remember to link and initialize the CodeGen library?)";
169 report_fatal_error(error
);
171 report_fatal_error(std::string("unsupported GC: ") + Name
);