1 //===-- CollectorMetadata.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 CollectorMetadata and CollectorModuleMetadata
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/CollectorMetadata.h"
16 #include "llvm/CodeGen/Collector.h"
17 #include "llvm/CodeGen/Collectors.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/Pass.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Support/Compiler.h"
28 class VISIBILITY_HIDDEN Printer : public FunctionPass {
33 explicit Printer(std::ostream &OS = *cerr);
35 const char *getPassName() const;
36 void getAnalysisUsage(AnalysisUsage &AU) const;
38 bool runOnFunction(Function &F);
41 class VISIBILITY_HIDDEN Deleter : public FunctionPass {
47 const char *getPassName() const;
48 void getAnalysisUsage(AnalysisUsage &AU) const;
50 bool runOnFunction(Function &F);
51 bool doFinalization(Module &M);
54 RegisterPass<CollectorModuleMetadata>
55 X("collector-metadata", "Create Garbage Collector Module Metadata");
59 // -----------------------------------------------------------------------------
61 CollectorMetadata::CollectorMetadata(const Function &F, Collector &C)
62 : F(F), C(C), FrameSize(~0LL) {}
64 CollectorMetadata::~CollectorMetadata() {}
66 // -----------------------------------------------------------------------------
68 char CollectorModuleMetadata::ID = 0;
70 CollectorModuleMetadata::CollectorModuleMetadata()
71 : ImmutablePass((intptr_t)&ID) {}
73 CollectorModuleMetadata::~CollectorModuleMetadata() {
77 Collector *CollectorModuleMetadata::
78 getOrCreateCollector(const Module *M, const std::string &Name) {
79 const char *Start = Name.c_str();
81 collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size());
82 if (NMI != NameMap.end())
83 return NMI->getValue();
85 for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
86 E = CollectorRegistry::end(); I != E; ++I) {
87 if (strcmp(Start, I->getName()) == 0) {
88 Collector *C = I->instantiate();
91 NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C);
92 Collectors.push_back(C);
97 cerr << "unsupported collector: " << Name << "\n";
101 CollectorMetadata &CollectorModuleMetadata::get(const Function &F) {
102 assert(F.hasCollector());
103 function_map_type::iterator I = Map.find(&F);
107 Collector *C = getOrCreateCollector(F.getParent(), F.getCollector());
108 CollectorMetadata *MD = C->insertFunctionMetadata(F);
113 void CollectorModuleMetadata::clear() {
116 // TODO: StringMap should provide a clear method.
117 while (!NameMap.empty())
118 NameMap.erase(NameMap.begin());
120 for (iterator I = begin(), E = end(); I != E; ++I)
125 // -----------------------------------------------------------------------------
127 char Printer::ID = 0;
129 FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
130 return new Printer(OS);
133 Printer::Printer(std::ostream &OS)
134 : FunctionPass(intptr_t(&ID)), OS(OS) {}
136 const char *Printer::getPassName() const {
137 return "Print Garbage Collector Information";
140 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
141 FunctionPass::getAnalysisUsage(AU);
142 AU.setPreservesAll();
143 AU.addRequired<CollectorModuleMetadata>();
146 static const char *DescKind(GC::PointKind Kind) {
148 default: assert(0 && "Unknown GC point kind");
149 case GC::Loop: return "loop";
150 case GC::Return: return "return";
151 case GC::PreCall: return "pre-call";
152 case GC::PostCall: return "post-call";
156 bool Printer::runOnFunction(Function &F) {
157 if (F.hasCollector()) {
158 CollectorMetadata *FD = &getAnalysis<CollectorModuleMetadata>().get(F);
160 OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
161 for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
162 RE = FD->roots_end();
164 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
166 OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
167 for (CollectorMetadata::iterator PI = FD->begin(),
168 PE = FD->end(); PI != PE; ++PI) {
170 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
172 for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
173 RE = FD->live_end(PI);;) {
174 OS << " " << RI->Num;
187 // -----------------------------------------------------------------------------
189 char Deleter::ID = 0;
191 FunctionPass *llvm::createCollectorMetadataDeleter() {
192 return new Deleter();
195 Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
197 const char *Deleter::getPassName() const {
198 return "Delete Garbage Collector Information";
201 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
202 AU.setPreservesAll();
203 AU.addRequired<CollectorModuleMetadata>();
206 bool Deleter::runOnFunction(Function &MF) {
210 bool Deleter::doFinalization(Module &M) {
211 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
212 assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");