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/Pass.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Function.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
26 class Printer : public FunctionPass {
31 Printer() : FunctionPass(&ID), OS(errs()) {}
32 explicit Printer(raw_ostream &OS) : FunctionPass(&ID), OS(OS) {}
35 const char *getPassName() const;
36 void getAnalysisUsage(AnalysisUsage &AU) const;
38 bool runOnFunction(Function &F);
41 class Deleter : public FunctionPass {
47 const char *getPassName() const;
48 void getAnalysisUsage(AnalysisUsage &AU) const;
50 bool runOnFunction(Function &F);
51 bool doFinalization(Module &M);
56 static RegisterPass<GCModuleInfo>
57 X("collector-metadata", "Create Garbage Collector Module Metadata");
59 // -----------------------------------------------------------------------------
61 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
62 : F(F), S(S), FrameSize(~0LL) {}
64 GCFunctionInfo::~GCFunctionInfo() {}
66 // -----------------------------------------------------------------------------
68 char GCModuleInfo::ID = 0;
70 GCModuleInfo::GCModuleInfo()
71 : ImmutablePass(&ID) {}
73 GCModuleInfo::~GCModuleInfo() {
77 GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
78 const std::string &Name) {
79 strategy_map_type::iterator NMI = StrategyMap.find(Name);
80 if (NMI != StrategyMap.end())
81 return NMI->getValue();
83 for (GCRegistry::iterator I = GCRegistry::begin(),
84 E = GCRegistry::end(); I != E; ++I) {
85 if (Name == I->getName()) {
86 GCStrategy *S = I->instantiate();
89 StrategyMap.GetOrCreateValue(Name).setValue(S);
90 StrategyList.push_back(S);
95 errs() << "unsupported GC: " << Name << "\n";
99 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
100 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
103 finfo_map_type::iterator I = FInfoMap.find(&F);
104 if (I != FInfoMap.end())
107 GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
108 GCFunctionInfo *GFI = S->insertFunctionInfo(F);
113 void GCModuleInfo::clear() {
117 for (iterator I = begin(), E = end(); I != E; ++I)
119 StrategyList.clear();
122 // -----------------------------------------------------------------------------
124 char Printer::ID = 0;
126 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
127 return new Printer(OS);
131 const char *Printer::getPassName() const {
132 return "Print Garbage Collector Information";
135 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
136 FunctionPass::getAnalysisUsage(AU);
137 AU.setPreservesAll();
138 AU.addRequired<GCModuleInfo>();
141 static const char *DescKind(GC::PointKind Kind) {
143 default: llvm_unreachable("Unknown GC point kind");
144 case GC::Loop: return "loop";
145 case GC::Return: return "return";
146 case GC::PreCall: return "pre-call";
147 case GC::PostCall: return "post-call";
151 bool Printer::runOnFunction(Function &F) {
153 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
155 OS << "GC roots for " << FD->getFunction().getNameStr() << ":\n";
156 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
157 RE = FD->roots_end(); RI != RE; ++RI)
158 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
160 OS << "GC safe points for " << FD->getFunction().getNameStr() << ":\n";
161 for (GCFunctionInfo::iterator PI = FD->begin(),
162 PE = FD->end(); PI != PE; ++PI) {
164 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
166 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
167 RE = FD->live_end(PI);;) {
168 OS << " " << RI->Num;
181 // -----------------------------------------------------------------------------
183 char Deleter::ID = 0;
185 FunctionPass *llvm::createGCInfoDeleter() {
186 return new Deleter();
189 Deleter::Deleter() : FunctionPass(&ID) {}
191 const char *Deleter::getPassName() const {
192 return "Delete Garbage Collector Information";
195 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
196 AU.setPreservesAll();
197 AU.addRequired<GCModuleInfo>();
200 bool Deleter::runOnFunction(Function &MF) {
204 bool Deleter::doFinalization(Module &M) {
205 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
206 assert(GMI && "Deleter didn't require GCModuleInfo?!");