1
//===-- GCMetadata.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 GCFunctionInfo class and GCModuleInfo pass.
12
//===----------------------------------------------------------------------===//
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"
27
class Printer : public FunctionPass {
32
Printer() : FunctionPass(&ID), OS(errs()) {}
33
explicit Printer(raw_ostream &OS) : FunctionPass(&ID), OS(OS) {}
36
const char *getPassName() const;
37
void getAnalysisUsage(AnalysisUsage &AU) const;
39
bool runOnFunction(Function &F);
42
class Deleter : public FunctionPass {
48
const char *getPassName() const;
49
void getAnalysisUsage(AnalysisUsage &AU) const;
51
bool runOnFunction(Function &F);
52
bool doFinalization(Module &M);
57
static RegisterPass<GCModuleInfo>
58
X("collector-metadata", "Create Garbage Collector Module Metadata");
60
// -----------------------------------------------------------------------------
62
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
63
: F(F), S(S), FrameSize(~0LL) {}
65
GCFunctionInfo::~GCFunctionInfo() {}
67
// -----------------------------------------------------------------------------
69
char GCModuleInfo::ID = 0;
71
GCModuleInfo::GCModuleInfo()
72
: ImmutablePass(&ID) {}
74
GCModuleInfo::~GCModuleInfo() {
78
GCStrategy *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();
84
for (GCRegistry::iterator I = GCRegistry::begin(),
85
E = GCRegistry::end(); I != E; ++I) {
86
if (Name == I->getName()) {
87
GCStrategy *S = I->instantiate();
90
StrategyMap.GetOrCreateValue(Name).setValue(S);
91
StrategyList.push_back(S);
96
dbgs() << "unsupported GC: " << Name << "\n";
100
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
101
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
104
finfo_map_type::iterator I = FInfoMap.find(&F);
105
if (I != FInfoMap.end())
108
GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
109
GCFunctionInfo *GFI = S->insertFunctionInfo(F);
114
void GCModuleInfo::clear() {
118
for (iterator I = begin(), E = end(); I != E; ++I)
120
StrategyList.clear();
123
// -----------------------------------------------------------------------------
125
char Printer::ID = 0;
127
FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
128
return new Printer(OS);
132
const char *Printer::getPassName() const {
133
return "Print Garbage Collector Information";
136
void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
137
FunctionPass::getAnalysisUsage(AU);
138
AU.setPreservesAll();
139
AU.addRequired<GCModuleInfo>();
142
static const char *DescKind(GC::PointKind 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";
152
bool Printer::runOnFunction(Function &F) {
154
GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
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";
161
OS << "GC safe points for " << FD->getFunction().getNameStr() << ":\n";
162
for (GCFunctionInfo::iterator PI = FD->begin(),
163
PE = FD->end(); PI != PE; ++PI) {
165
OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
167
for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
168
RE = FD->live_end(PI);;) {
169
OS << " " << RI->Num;
182
// -----------------------------------------------------------------------------
184
char Deleter::ID = 0;
186
FunctionPass *llvm::createGCInfoDeleter() {
187
return new Deleter();
190
Deleter::Deleter() : FunctionPass(&ID) {}
192
const char *Deleter::getPassName() const {
193
return "Delete Garbage Collector Information";
196
void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
197
AU.setPreservesAll();
198
AU.addRequired<GCModuleInfo>();
201
bool Deleter::runOnFunction(Function &MF) {
205
bool Deleter::doFinalization(Module &M) {
206
GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
207
assert(GMI && "Deleter didn't require GCModuleInfo?!");