blob: 0b5c6f0662cf27a45d881bba7c56b72ea6176317 [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
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000015#include "llvm/CodeGen/GCMetadata.h"
16#include "llvm/CodeGen/GCStrategy.h"
17#include "llvm/CodeGen/GCs.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
Gordon Henriksenfc328222007-09-27 22:18:46 +000054}
55
Dan Gohman844731a2008-05-13 00:00:25 +000056static RegisterPass<CollectorModuleMetadata>
57X("collector-metadata", "Create Garbage Collector Module Metadata");
58
Gordon Henriksenfc328222007-09-27 22:18:46 +000059// -----------------------------------------------------------------------------
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) {
Gordon Henriksen418b6e82008-08-17 16:18:50 +0000102 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000103 assert(F.hasCollector());
Gordon Henriksen418b6e82008-08-17 16:18:50 +0000104
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000105 function_map_type::iterator I = Map.find(&F);
106 if (I != Map.end())
107 return *I->second;
108
109 Collector *C = getOrCreateCollector(F.getParent(), F.getCollector());
110 CollectorMetadata *MD = C->insertFunctionMetadata(F);
111 Map[&F] = MD;
112 return *MD;
Gordon Henriksenfc328222007-09-27 22:18:46 +0000113}
114
115void CollectorModuleMetadata::clear() {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000116 Map.clear();
Chris Lattnerfce6e542008-07-02 05:26:32 +0000117 NameMap.clear();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000118
Gordon Henriksenfc328222007-09-27 22:18:46 +0000119 for (iterator I = begin(), E = end(); I != E; ++I)
120 delete *I;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000121 Collectors.clear();
Gordon Henriksenfc328222007-09-27 22:18:46 +0000122}
123
124// -----------------------------------------------------------------------------
125
126char Printer::ID = 0;
127
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000128FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
Gordon Henriksenfc328222007-09-27 22:18:46 +0000129 return new Printer(OS);
130}
131
132Printer::Printer(std::ostream &OS)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000133 : FunctionPass(intptr_t(&ID)), OS(OS) {}
Gordon Henriksenfc328222007-09-27 22:18:46 +0000134
135const char *Printer::getPassName() const {
136 return "Print Garbage Collector Information";
137}
138
139void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000140 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksenfc328222007-09-27 22:18:46 +0000141 AU.setPreservesAll();
142 AU.addRequired<CollectorModuleMetadata>();
143}
144
145static const char *DescKind(GC::PointKind Kind) {
146 switch (Kind) {
147 default: assert(0 && "Unknown GC point kind");
148 case GC::Loop: return "loop";
149 case GC::Return: return "return";
150 case GC::PreCall: return "pre-call";
151 case GC::PostCall: return "post-call";
152 }
153}
154
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000155bool Printer::runOnFunction(Function &F) {
156 if (F.hasCollector()) {
157 CollectorMetadata *FD = &getAnalysis<CollectorModuleMetadata>().get(F);
Gordon Henriksenfc328222007-09-27 22:18:46 +0000158
159 OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
160 for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
161 RE = FD->roots_end();
162 RI != RE; ++RI)
163 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
164
165 OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
166 for (CollectorMetadata::iterator PI = FD->begin(),
167 PE = FD->end(); PI != PE; ++PI) {
168
169 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
170
171 for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
172 RE = FD->live_end(PI);;) {
173 OS << " " << RI->Num;
174 if (++RI == RE)
175 break;
176 OS << ",";
177 }
178
179 OS << " }\n";
180 }
181 }
182
183 return false;
184}
185
186// -----------------------------------------------------------------------------
187
188char Deleter::ID = 0;
189
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000190FunctionPass *llvm::createCollectorMetadataDeleter() {
Gordon Henriksenfc328222007-09-27 22:18:46 +0000191 return new Deleter();
192}
193
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000194Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
Gordon Henriksenfc328222007-09-27 22:18:46 +0000195
196const char *Deleter::getPassName() const {
197 return "Delete Garbage Collector Information";
198}
199
200void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
201 AU.setPreservesAll();
202 AU.addRequired<CollectorModuleMetadata>();
203}
204
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000205bool Deleter::runOnFunction(Function &MF) {
Gordon Henriksenfc328222007-09-27 22:18:46 +0000206 return false;
207}
208
209bool Deleter::doFinalization(Module &M) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000210 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
211 assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");
212 CMM->clear();
Gordon Henriksenfc328222007-09-27 22:18:46 +0000213 return false;
214}