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/CodeGen/GCMetadata.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
32 class Printer
: public FunctionPass
{
38 explicit Printer(raw_ostream
&OS
) : FunctionPass(ID
), OS(OS
) {}
40 StringRef
getPassName() const override
;
41 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
43 bool runOnFunction(Function
&F
) override
;
44 bool doFinalization(Module
&M
) override
;
47 } // end anonymous namespace
49 INITIALIZE_PASS(GCModuleInfo
, "collector-metadata",
50 "Create Garbage Collector Module Metadata", false, false)
52 // -----------------------------------------------------------------------------
54 GCFunctionInfo::GCFunctionInfo(const Function
&F
, GCStrategy
&S
)
55 : F(F
), S(S
), FrameSize(~0LL) {}
57 GCFunctionInfo::~GCFunctionInfo() = default;
59 // -----------------------------------------------------------------------------
61 char GCModuleInfo::ID
= 0;
63 GCModuleInfo::GCModuleInfo() : ImmutablePass(ID
) {
64 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
67 GCFunctionInfo
&GCModuleInfo::getFunctionInfo(const Function
&F
) {
68 assert(!F
.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
71 finfo_map_type::iterator I
= FInfoMap
.find(&F
);
72 if (I
!= FInfoMap
.end())
75 GCStrategy
*S
= getGCStrategy(F
.getGC());
76 Functions
.push_back(std::make_unique
<GCFunctionInfo
>(F
, *S
));
77 GCFunctionInfo
*GFI
= Functions
.back().get();
82 void GCModuleInfo::clear() {
85 GCStrategyList
.clear();
88 // -----------------------------------------------------------------------------
92 FunctionPass
*llvm::createGCInfoPrinter(raw_ostream
&OS
) {
93 return new Printer(OS
);
96 StringRef
Printer::getPassName() const {
97 return "Print Garbage Collector Information";
100 void Printer::getAnalysisUsage(AnalysisUsage
&AU
) const {
101 FunctionPass::getAnalysisUsage(AU
);
102 AU
.setPreservesAll();
103 AU
.addRequired
<GCModuleInfo
>();
106 bool Printer::runOnFunction(Function
&F
) {
110 GCFunctionInfo
*FD
= &getAnalysis
<GCModuleInfo
>().getFunctionInfo(F
);
112 OS
<< "GC roots for " << FD
->getFunction().getName() << ":\n";
113 for (GCFunctionInfo::roots_iterator RI
= FD
->roots_begin(),
114 RE
= FD
->roots_end();
116 OS
<< "\t" << RI
->Num
<< "\t" << RI
->StackOffset
<< "[sp]\n";
118 OS
<< "GC safe points for " << FD
->getFunction().getName() << ":\n";
119 for (GCFunctionInfo::iterator PI
= FD
->begin(), PE
= FD
->end(); PI
!= PE
;
122 OS
<< "\t" << PI
->Label
->getName() << ": " << "post-call"
125 ListSeparator
LS(",");
126 for (const GCRoot
&R
: make_range(FD
->live_begin(PI
), FD
->live_end(PI
)))
127 OS
<< LS
<< " " << R
.Num
;
135 bool Printer::doFinalization(Module
&M
) {
136 GCModuleInfo
*GMI
= getAnalysisIfAvailable
<GCModuleInfo
>();
137 assert(GMI
&& "Printer didn't require GCModuleInfo?!");
142 GCStrategy
*GCModuleInfo::getGCStrategy(const StringRef Name
) {
143 // TODO: Arguably, just doing a linear search would be faster for small N
144 auto NMI
= GCStrategyMap
.find(Name
);
145 if (NMI
!= GCStrategyMap
.end())
146 return NMI
->getValue();
148 std::unique_ptr
<GCStrategy
> S
= llvm::getGCStrategy(Name
);
149 S
->Name
= std::string(Name
);
150 GCStrategyMap
[Name
] = S
.get();
151 GCStrategyList
.push_back(std::move(S
));
152 return GCStrategyList
.back().get();