2008-08-18 02:44:35 +08:00
|
|
|
//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
|
2007-09-28 06:18:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-28 06:18:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-18 02:44:35 +08:00
|
|
|
// This file implements the GCFunctionInfo class and GCModuleInfo pass.
|
2007-09-28 06:18:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-17 20:56:54 +08:00
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
|
|
|
#include "llvm/CodeGen/GCStrategy.h"
|
2007-09-28 06:18:46 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-11 08:30:17 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2007-09-28 06:18:46 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
class VISIBILITY_HIDDEN Printer : public FunctionPass {
|
2007-09-28 06:18:46 +08:00
|
|
|
static char ID;
|
|
|
|
std::ostream &OS;
|
|
|
|
|
|
|
|
public:
|
2007-12-14 23:41:34 +08:00
|
|
|
explicit Printer(std::ostream &OS = *cerr);
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
const char *getPassName() const;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
bool runOnFunction(Function &F);
|
2007-09-28 06:18:46 +08:00
|
|
|
};
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
class VISIBILITY_HIDDEN Deleter : public FunctionPass {
|
2007-09-28 06:18:46 +08:00
|
|
|
static char ID;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Deleter();
|
|
|
|
|
|
|
|
const char *getPassName() const;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
bool runOnFunction(Function &F);
|
2007-09-28 06:18:46 +08:00
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
static RegisterPass<GCModuleInfo>
|
2008-05-13 08:00:25 +08:00
|
|
|
X("collector-metadata", "Create Garbage Collector Module Metadata");
|
|
|
|
|
2007-09-28 06:18:46 +08:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
|
|
|
|
: F(F), S(S), FrameSize(~0LL) {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCFunctionInfo::~GCFunctionInfo() {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
char GCModuleInfo::ID = 0;
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCModuleInfo::GCModuleInfo()
|
2008-09-05 01:05:41 +08:00
|
|
|
: ImmutablePass(&ID) {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCModuleInfo::~GCModuleInfo() {
|
2007-09-28 06:18:46 +08:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
|
|
|
|
const std::string &Name) {
|
2007-12-11 08:30:17 +08:00
|
|
|
const char *Start = Name.c_str();
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
strategy_map_type::iterator NMI =
|
|
|
|
StrategyMap.find(Start, Start + Name.size());
|
|
|
|
if (NMI != StrategyMap.end())
|
2007-12-11 08:30:17 +08:00
|
|
|
return NMI->getValue();
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
for (GCRegistry::iterator I = GCRegistry::begin(),
|
|
|
|
E = GCRegistry::end(); I != E; ++I) {
|
2007-12-11 08:30:17 +08:00
|
|
|
if (strcmp(Start, I->getName()) == 0) {
|
2008-08-18 02:44:35 +08:00
|
|
|
GCStrategy *S = I->instantiate();
|
|
|
|
S->M = M;
|
|
|
|
S->Name = Name;
|
|
|
|
StrategyMap.GetOrCreateValue(Start, Start + Name.size()).setValue(S);
|
|
|
|
StrategyList.push_back(S);
|
|
|
|
return S;
|
2007-12-11 08:30:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
cerr << "unsupported GC: " << Name << "\n";
|
2007-12-11 08:30:17 +08:00
|
|
|
abort();
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
|
2008-08-18 00:18:50 +08:00
|
|
|
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
|
2008-08-18 02:44:35 +08:00
|
|
|
assert(F.hasGC());
|
2008-08-18 00:18:50 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
finfo_map_type::iterator I = FInfoMap.find(&F);
|
|
|
|
if (I != FInfoMap.end())
|
2007-12-11 08:30:17 +08:00
|
|
|
return *I->second;
|
2008-08-18 02:44:35 +08:00
|
|
|
|
|
|
|
GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
|
|
|
|
GCFunctionInfo *GFI = S->insertFunctionInfo(F);
|
|
|
|
FInfoMap[&F] = GFI;
|
|
|
|
return *GFI;
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
void GCModuleInfo::clear() {
|
|
|
|
FInfoMap.clear();
|
|
|
|
StrategyMap.clear();
|
2007-12-11 08:30:17 +08:00
|
|
|
|
2007-09-28 06:18:46 +08:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
delete *I;
|
2008-08-18 02:44:35 +08:00
|
|
|
StrategyList.clear();
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char Printer::ID = 0;
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
FunctionPass *llvm::createGCInfoPrinter(std::ostream &OS) {
|
2007-09-28 06:18:46 +08:00
|
|
|
return new Printer(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Printer::Printer(std::ostream &OS)
|
2009-02-18 13:09:16 +08:00
|
|
|
: FunctionPass(&ID), OS(OS) {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
const char *Printer::getPassName() const {
|
|
|
|
return "Print Garbage Collector Information";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-12-11 08:30:17 +08:00
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
2007-09-28 06:18:46 +08:00
|
|
|
AU.setPreservesAll();
|
2008-08-18 02:44:35 +08:00
|
|
|
AU.addRequired<GCModuleInfo>();
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *DescKind(GC::PointKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown GC point kind");
|
|
|
|
case GC::Loop: return "loop";
|
|
|
|
case GC::Return: return "return";
|
|
|
|
case GC::PreCall: return "pre-call";
|
|
|
|
case GC::PostCall: return "post-call";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
bool Printer::runOnFunction(Function &F) {
|
2008-08-18 02:44:35 +08:00
|
|
|
if (!F.hasGC()) {
|
|
|
|
GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
|
2008-08-18 02:44:35 +08:00
|
|
|
for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
|
|
|
|
RE = FD->roots_end(); RI != RE; ++RI)
|
2007-09-28 06:18:46 +08:00
|
|
|
OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
|
|
|
|
|
|
|
|
OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
|
2008-08-18 02:44:35 +08:00
|
|
|
for (GCFunctionInfo::iterator PI = FD->begin(),
|
|
|
|
PE = FD->end(); PI != PE; ++PI) {
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
|
|
|
|
RE = FD->live_end(PI);;) {
|
2007-09-28 06:18:46 +08:00
|
|
|
OS << " " << RI->Num;
|
|
|
|
if (++RI == RE)
|
|
|
|
break;
|
|
|
|
OS << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << " }\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char Deleter::ID = 0;
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
FunctionPass *llvm::createGCInfoDeleter() {
|
2007-09-28 06:18:46 +08:00
|
|
|
return new Deleter();
|
|
|
|
}
|
|
|
|
|
2009-02-18 13:09:16 +08:00
|
|
|
Deleter::Deleter() : FunctionPass(&ID) {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
const char *Deleter::getPassName() const {
|
|
|
|
return "Delete Garbage Collector Information";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2008-08-18 02:44:35 +08:00
|
|
|
AU.addRequired<GCModuleInfo>();
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
bool Deleter::runOnFunction(Function &MF) {
|
2007-09-28 06:18:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Deleter::doFinalization(Module &M) {
|
2009-01-28 21:14:17 +08:00
|
|
|
GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
|
2008-08-18 02:44:35 +08:00
|
|
|
assert(GMI && "Deleter didn't require GCModuleInfo?!");
|
|
|
|
GMI->clear();
|
2007-09-28 06:18:46 +08:00
|
|
|
return false;
|
|
|
|
}
|