blob: 055172b4e0e98e4cb667388975b58bbe206f5d5b [file] [log] [blame]
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the GCFunctionInfo class and GCModuleInfo pass.
11//
12//===----------------------------------------------------------------------===//
13
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/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
25namespace {
26
27 class Printer : public FunctionPass {
28 static char ID;
29 raw_ostream &OS;
30
31 public:
32 Printer() : FunctionPass(&ID), OS(errs()) {}
33 explicit Printer(raw_ostream &OS) : FunctionPass(&ID), OS(OS) {}
34
35
36 const char *getPassName() const;
37 void getAnalysisUsage(AnalysisUsage &AU) const;
38
39 bool runOnFunction(Function &F);
40 };
41
42 class Deleter : public FunctionPass {
43 static char ID;
44
45 public:
46 Deleter();
47
48 const char *getPassName() const;
49 void getAnalysisUsage(AnalysisUsage &AU) const;
50
51 bool runOnFunction(Function &F);
52 bool doFinalization(Module &M);
53 };
54
55}
56
57static RegisterPass<GCModuleInfo>
58X("collector-metadata", "Create Garbage Collector Module Metadata");
59
60// -----------------------------------------------------------------------------
61
62GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
63 : F(F), S(S), FrameSize(~0LL) {}
64
65GCFunctionInfo::~GCFunctionInfo() {}
66
67// -----------------------------------------------------------------------------
68
69char GCModuleInfo::ID = 0;
70
71GCModuleInfo::GCModuleInfo()
72 : ImmutablePass(&ID) {}
73
74GCModuleInfo::~GCModuleInfo() {
75 clear();
76}
77
78GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
79 const std::string &Name) {
80 strategy_map_type::iterator NMI = StrategyMap.find(Name);
81 if (NMI != StrategyMap.end())
82 return NMI->getValue();
83
84 for (GCRegistry::iterator I = GCRegistry::begin(),
85 E = GCRegistry::end(); I != E; ++I) {
86 if (Name == I->getName()) {
87 GCStrategy *S = I->instantiate();
88 S->M = M;
89 S->Name = Name;
90 StrategyMap.GetOrCreateValue(Name).setValue(S);
91 StrategyList.push_back(S);
92 return S;
93 }
94 }
95
96 dbgs() << "unsupported GC: " << Name << "\n";
97 llvm_unreachable(0);
98}
99
100GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
101 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
102 assert(F.hasGC());
103
104 finfo_map_type::iterator I = FInfoMap.find(&F);
105 if (I != FInfoMap.end())
106 return *I->second;
107
108 GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
109 GCFunctionInfo *GFI = S->insertFunctionInfo(F);
110 FInfoMap[&F] = GFI;
111 return *GFI;
112}
113
114void GCModuleInfo::clear() {
115 FInfoMap.clear();
116 StrategyMap.clear();
117
118 for (iterator I = begin(), E = end(); I != E; ++I)
119 delete *I;
120 StrategyList.clear();
121}
122
123// -----------------------------------------------------------------------------
124
125char Printer::ID = 0;
126
127FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
128 return new Printer(OS);
129}
130
131
132const char *Printer::getPassName() const {
133 return "Print Garbage Collector Information";
134}
135
136void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
137 FunctionPass::getAnalysisUsage(AU);
138 AU.setPreservesAll();
139 AU.addRequired<GCModuleInfo>();
140}
141
142static const char *DescKind(GC::PointKind Kind) {
143 switch (Kind) {
144 default: llvm_unreachable("Unknown GC point kind");
145 case GC::Loop: return "loop";
146 case GC::Return: return "return";
147 case GC::PreCall: return "pre-call";
148 case GC::PostCall: return "post-call";
149 }
150}
151
152bool Printer::runOnFunction(Function &F) {
153 if (!F.hasGC()) {
154 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
155
156 OS << "GC roots for " << FD->getFunction().getNameStr() << ":\n";
157 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
158 RE = FD->roots_end(); RI != RE; ++RI)
159 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
160
161 OS << "GC safe points for " << FD->getFunction().getNameStr() << ":\n";
162 for (GCFunctionInfo::iterator PI = FD->begin(),
163 PE = FD->end(); PI != PE; ++PI) {
164
165 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
166
167 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
168 RE = FD->live_end(PI);;) {
169 OS << " " << RI->Num;
170 if (++RI == RE)
171 break;
172 OS << ",";
173 }
174
175 OS << " }\n";
176 }
177 }
178
179 return false;
180}
181
182// -----------------------------------------------------------------------------
183
184char Deleter::ID = 0;
185
186FunctionPass *llvm::createGCInfoDeleter() {
187 return new Deleter();
188}
189
190Deleter::Deleter() : FunctionPass(&ID) {}
191
192const char *Deleter::getPassName() const {
193 return "Delete Garbage Collector Information";
194}
195
196void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
197 AU.setPreservesAll();
198 AU.addRequired<GCModuleInfo>();
199}
200
201bool Deleter::runOnFunction(Function &MF) {
202 return false;
203}
204
205bool Deleter::doFinalization(Module &M) {
206 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
207 assert(GMI && "Deleter didn't require GCModuleInfo?!");
208 GMI->clear();
209 return false;
210}