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/CodeGen/GCMetadata.h"
15 #include "llvm/CodeGen/GCStrategy.h"
16 #include "llvm/CodeGen/MachineFrameInfo.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/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
28 class Printer : public FunctionPass {
33 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
36 const char *getPassName() const override;
37 void getAnalysisUsage(AnalysisUsage &AU) const override;
39 bool runOnFunction(Function &F) override;
40 bool doFinalization(Module &M) override;
45 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
46 "Create Garbage Collector Module Metadata", false, false)
48 // -----------------------------------------------------------------------------
50 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
51 : F(F), S(S), FrameSize(~0LL) {}
53 GCFunctionInfo::~GCFunctionInfo() {}
55 // -----------------------------------------------------------------------------
57 char GCModuleInfo::ID = 0;
59 GCModuleInfo::GCModuleInfo()
61 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
64 GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
65 const std::string &Name) {
66 strategy_map_type::iterator NMI = StrategyMap.find(Name);
67 if (NMI != StrategyMap.end())
68 return NMI->getValue();
70 for (GCRegistry::iterator I = GCRegistry::begin(),
71 E = GCRegistry::end(); I != E; ++I) {
72 if (Name == I->getName()) {
73 std::unique_ptr<GCStrategy> S = I->instantiate();
75 StrategyMap[Name] = S.get();
76 StrategyList.push_back(std::move(S));
77 return StrategyList.back().get();
81 dbgs() << "unsupported GC: " << Name << "\n";
82 llvm_unreachable(nullptr);
85 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
86 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
89 finfo_map_type::iterator I = FInfoMap.find(&F);
90 if (I != FInfoMap.end())
93 GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
94 GCFunctionInfo *GFI = S->insertFunctionInfo(F);
99 void GCModuleInfo::clear() {
102 StrategyList.clear();
105 // -----------------------------------------------------------------------------
107 char Printer::ID = 0;
109 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
110 return new Printer(OS);
114 const char *Printer::getPassName() const {
115 return "Print Garbage Collector Information";
118 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
119 FunctionPass::getAnalysisUsage(AU);
120 AU.setPreservesAll();
121 AU.addRequired<GCModuleInfo>();
124 static const char *DescKind(GC::PointKind Kind) {
126 case GC::Loop: return "loop";
127 case GC::Return: return "return";
128 case GC::PreCall: return "pre-call";
129 case GC::PostCall: return "post-call";
131 llvm_unreachable("Invalid point kind");
134 bool Printer::runOnFunction(Function &F) {
135 if (F.hasGC()) return false;
137 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
139 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
140 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
141 RE = FD->roots_end(); RI != RE; ++RI)
142 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
144 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
145 for (GCFunctionInfo::iterator PI = FD->begin(),
146 PE = FD->end(); PI != PE; ++PI) {
148 OS << "\t" << PI->Label->getName() << ": "
149 << DescKind(PI->Kind) << ", live = {";
151 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
152 RE = FD->live_end(PI);;) {
153 OS << " " << RI->Num;
165 bool Printer::doFinalization(Module &M) {
166 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
167 assert(GMI && "Printer didn't require GCModuleInfo?!");