blob: 80085cd29de1e039f4755427c1cd588c0940206f [file] [log] [blame]
Gordon Henriksenfc328222007-09-27 22:18:46 +00001//===-- CollectorMetadata.cpp - Garbage collector metadata ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksenfc328222007-09-27 22:18:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CollectorMetadata and CollectorModuleMetadata
11// classes.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/CollectorMetadata.h"
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000016#include "llvm/CodeGen/Collector.h"
17#include "llvm/CodeGen/Collectors.h"
Gordon Henriksenfc328222007-09-27 22:18:46 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000019#include "llvm/Pass.h"
20#include "llvm/CodeGen/Passes.h"
Gordon Henriksenfc328222007-09-27 22:18:46 +000021#include "llvm/Function.h"
22#include "llvm/Support/Compiler.h"
23
24using namespace llvm;
25
26namespace {
27
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000028 class VISIBILITY_HIDDEN Printer : public FunctionPass {
Gordon Henriksenfc328222007-09-27 22:18:46 +000029 static char ID;
30 std::ostream &OS;
31
32 public:
Dan Gohmanded2b0d2007-12-14 15:41:34 +000033 explicit Printer(std::ostream &OS = *cerr);
Gordon Henriksenfc328222007-09-27 22:18:46 +000034
35 const char *getPassName() const;
36 void getAnalysisUsage(AnalysisUsage &AU) const;
37
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000038 bool runOnFunction(Function &F);
Gordon Henriksenfc328222007-09-27 22:18:46 +000039 };
40
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000041 class VISIBILITY_HIDDEN Deleter : public FunctionPass {
Gordon Henriksenfc328222007-09-27 22:18:46 +000042 static char ID;
43
44 public:
45 Deleter();
46
47 const char *getPassName() const;
48 void getAnalysisUsage(AnalysisUsage &AU) const;
49
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000050 bool runOnFunction(Function &F);
Gordon Henriksenfc328222007-09-27 22:18:46 +000051 bool doFinalization(Module &M);
52 };
53
54 RegisterPass<CollectorModuleMetadata>
55 X("collector-metadata", "Create Garbage Collector Module Metadata");
56
57}
58
59// -----------------------------------------------------------------------------
60
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000061CollectorMetadata::CollectorMetadata(const Function &F, Collector &C)
62 : F(F), C(C), FrameSize(~0LL) {}
Gordon Henriksenfc328222007-09-27 22:18:46 +000063
64CollectorMetadata::~CollectorMetadata() {}
65
66// -----------------------------------------------------------------------------
67
68char CollectorModuleMetadata::ID = 0;
69
70CollectorModuleMetadata::CollectorModuleMetadata()
71 : ImmutablePass((intptr_t)&ID) {}
72
73CollectorModuleMetadata::~CollectorModuleMetadata() {
74 clear();
75}
76
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000077Collector *CollectorModuleMetadata::
78getOrCreateCollector(const Module *M, const std::string &Name) {
79 const char *Start = Name.c_str();
80
81 collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size());
82 if (NMI != NameMap.end())
83 return NMI->getValue();
84
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();
89 C->M = M;
90 C->Name = Name;
91 NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C);
92 Collectors.push_back(C);
93 return C;
94 }
95 }
96
97 cerr << "unsupported collector: " << Name << "\n";
98 abort();
Gordon Henriksenfc328222007-09-27 22:18:46 +000099}
100
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000101CollectorMetadata &CollectorModuleMetadata::get(const Function &F) {
102 assert(F.hasCollector());
103 function_map_type::iterator I = Map.find(&F);
104 if (I != Map.end())
105 return *I->second;
106
107 Collector *C = getOrCreateCollector(F.getParent(), F.getCollector());
108 CollectorMetadata *MD = C->insertFunctionMetadata(F);
109 Map[&F] = MD;
110 return *MD;
Gordon Henriksenfc328222007-09-27 22:18:46 +0000111}
112
113void CollectorModuleMetadata::clear() {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000114 Map.clear();
115
116 // TODO: StringMap should provide a clear method.
117 while (!NameMap.empty())
118 NameMap.erase(NameMap.begin());
119
Gordon Henriksenfc328222007-09-27 22:18:46 +0000120 for (iterator I = begin(), E = end(); I != E; ++I)
121 delete *I;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000122 Collectors.clear();
Gordon Henriksenfc328222007-09-27 22:18:46 +0000123}
124
125// -----------------------------------------------------------------------------
126
127char Printer::ID = 0;
128
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000129FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
Gordon Henriksenfc328222007-09-27 22:18:46 +0000130 return new Printer(OS);
131}
132
133Printer::Printer(std::ostream &OS)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000134 : FunctionPass(intptr_t(&ID)), OS(OS) {}
Gordon Henriksenfc328222007-09-27 22:18:46 +0000135
136const char *Printer::getPassName() const {
137 return "Print Garbage Collector Information";
138}
139
140void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000141 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksenfc328222007-09-27 22:18:46 +0000142 AU.setPreservesAll();
143 AU.addRequired<CollectorModuleMetadata>();
144}
145
146static const char *DescKind(GC::PointKind Kind) {
147 switch (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";
153 }
154}
155
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000156bool Printer::runOnFunction(Function &F) {
157 if (F.hasCollector()) {
158 CollectorMetadata *FD = &getAnalysis<CollectorModuleMetadata>().get(F);
Gordon Henriksenfc328222007-09-27 22:18:46 +0000159
160 OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
161 for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
162 RE = FD->roots_end();
163 RI != RE; ++RI)
164 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
165
166 OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
167 for (CollectorMetadata::iterator PI = FD->begin(),
168 PE = FD->end(); PI != PE; ++PI) {
169
170 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
171
172 for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
173 RE = FD->live_end(PI);;) {
174 OS << " " << RI->Num;
175 if (++RI == RE)
176 break;
177 OS << ",";
178 }
179
180 OS << " }\n";
181 }
182 }
183
184 return false;
185}
186
187// -----------------------------------------------------------------------------
188
189char Deleter::ID = 0;
190
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000191FunctionPass *llvm::createCollectorMetadataDeleter() {
Gordon Henriksenfc328222007-09-27 22:18:46 +0000192 return new Deleter();
193}
194
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000195Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
Gordon Henriksenfc328222007-09-27 22:18:46 +0000196
197const char *Deleter::getPassName() const {
198 return "Delete Garbage Collector Information";
199}
200
201void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
202 AU.setPreservesAll();
203 AU.addRequired<CollectorModuleMetadata>();
204}
205
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000206bool Deleter::runOnFunction(Function &MF) {
Gordon Henriksenfc328222007-09-27 22:18:46 +0000207 return false;
208}
209
210bool Deleter::doFinalization(Module &M) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000211 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
212 assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");
213 CMM->clear();
Gordon Henriksenfc328222007-09-27 22:18:46 +0000214 return false;
215}