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);
56 static RegisterPass<CollectorModuleMetadata>
57 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() {
117 for (iterator I = begin(), E = end(); I != E; ++I)
122 // -----------------------------------------------------------------------------
124 char Printer::ID = 0;
126 FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
127 return new Printer(OS);
130 Printer::Printer(std::ostream &OS)
131 : FunctionPass(intptr_t(&ID)), OS(OS) {}
133 const char *Printer::getPassName() const {
134 return "Print Garbage Collector Information";
137 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
138 FunctionPass::getAnalysisUsage(AU);
139 AU.setPreservesAll();
140 AU.addRequired<CollectorModuleMetadata>();
143 static const char *DescKind(GC::PointKind Kind) {
145 default: assert(0 && "Unknown GC point kind");
146 case GC::Loop: return "loop";
147 case GC::Return: return "return";
148 case GC::PreCall: return "pre-call";
149 case GC::PostCall: return "post-call";
153 bool Printer::runOnFunction(Function &F) {
154 if (F.hasCollector()) {
155 CollectorMetadata *FD = &getAnalysis<CollectorModuleMetadata>().get(F);
157 OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
158 for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
159 RE = FD->roots_end();
161 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
163 OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
164 for (CollectorMetadata::iterator PI = FD->begin(),
165 PE = FD->end(); PI != PE; ++PI) {
167 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
169 for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
170 RE = FD->live_end(PI);;) {
171 OS << " " << RI->Num;
184 // -----------------------------------------------------------------------------
186 char Deleter::ID = 0;
188 FunctionPass *llvm::createCollectorMetadataDeleter() {
189 return new Deleter();
192 Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
194 const char *Deleter::getPassName() const {
195 return "Delete Garbage Collector Information";
198 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
199 AU.setPreservesAll();
200 AU.addRequired<CollectorModuleMetadata>();
203 bool Deleter::runOnFunction(Function &MF) {
207 bool Deleter::doFinalization(Module &M) {
208 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
209 assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");