1 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GCFunctionInfo class and GCModuleInfo pass.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/CodeGen/GCMetadata.h"
16 #include "llvm/CodeGen/GCStrategy.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/Function.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(llvm::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 static const char *DescKind(GC::PointKind Kind
) {
113 llvm_unreachable("Invalid point kind");
116 bool Printer::runOnFunction(Function
&F
) {
120 GCFunctionInfo
*FD
= &getAnalysis
<GCModuleInfo
>().getFunctionInfo(F
);
122 OS
<< "GC roots for " << FD
->getFunction().getName() << ":\n";
123 for (GCFunctionInfo::roots_iterator RI
= FD
->roots_begin(),
124 RE
= FD
->roots_end();
126 OS
<< "\t" << RI
->Num
<< "\t" << RI
->StackOffset
<< "[sp]\n";
128 OS
<< "GC safe points for " << FD
->getFunction().getName() << ":\n";
129 for (GCFunctionInfo::iterator PI
= FD
->begin(), PE
= FD
->end(); PI
!= PE
;
132 OS
<< "\t" << PI
->Label
->getName() << ": " << DescKind(PI
->Kind
)
135 for (GCFunctionInfo::live_iterator RI
= FD
->live_begin(PI
),
136 RE
= FD
->live_end(PI
);
138 OS
<< " " << RI
->Num
;
150 bool Printer::doFinalization(Module
&M
) {
151 GCModuleInfo
*GMI
= getAnalysisIfAvailable
<GCModuleInfo
>();
152 assert(GMI
&& "Printer didn't require GCModuleInfo?!");
157 GCStrategy
*GCModuleInfo::getGCStrategy(const StringRef Name
) {
158 // TODO: Arguably, just doing a linear search would be faster for small N
159 auto NMI
= GCStrategyMap
.find(Name
);
160 if (NMI
!= GCStrategyMap
.end())
161 return NMI
->getValue();
163 for (auto& Entry
: GCRegistry::entries()) {
164 if (Name
== Entry
.getName()) {
165 std::unique_ptr
<GCStrategy
> S
= Entry
.instantiate();
167 GCStrategyMap
[Name
] = S
.get();
168 GCStrategyList
.push_back(std::move(S
));
169 return GCStrategyList
.back().get();
173 if (GCRegistry::begin() == GCRegistry::end()) {
174 // In normal operation, the registry should not be empty. There should
175 // be the builtin GCs if nothing else. The most likely scenario here is
176 // that we got here without running the initializers used by the Registry
177 // itself and it's registration mechanism.
178 const std::string error
= ("unsupported GC: " + Name
).str() +
179 " (did you remember to link and initialize the CodeGen library?)";
180 report_fatal_error(error
);
182 report_fatal_error(std::string("unsupported GC: ") + Name
);