2007-09-27 22:18:46 +00:00
|
|
|
//===-- CollectorMetadata.cpp - Garbage collector metadata ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-27 22:18:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CollectorMetadata and CollectorModuleMetadata
|
|
|
|
// classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/CollectorMetadata.h"
|
2007-12-11 00:30:17 +00:00
|
|
|
#include "llvm/CodeGen/Collector.h"
|
|
|
|
#include "llvm/CodeGen/Collectors.h"
|
2007-09-27 22:18:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-11 00:30:17 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2007-09-27 22:18:46 +00:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
class VISIBILITY_HIDDEN Printer : public FunctionPass {
|
2007-09-27 22:18:46 +00:00
|
|
|
static char ID;
|
|
|
|
std::ostream &OS;
|
|
|
|
|
|
|
|
public:
|
2007-12-14 15:41:34 +00:00
|
|
|
explicit Printer(std::ostream &OS = *cerr);
|
2007-09-27 22:18:46 +00:00
|
|
|
|
|
|
|
const char *getPassName() const;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
bool runOnFunction(Function &F);
|
2007-09-27 22:18:46 +00:00
|
|
|
};
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
class VISIBILITY_HIDDEN Deleter : public FunctionPass {
|
2007-09-27 22:18:46 +00:00
|
|
|
static char ID;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Deleter();
|
|
|
|
|
|
|
|
const char *getPassName() const;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
bool runOnFunction(Function &F);
|
2007-09-27 22:18:46 +00:00
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterPass<CollectorModuleMetadata>
|
|
|
|
X("collector-metadata", "Create Garbage Collector Module Metadata");
|
|
|
|
|
2007-09-27 22:18:46 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
CollectorMetadata::CollectorMetadata(const Function &F, Collector &C)
|
|
|
|
: F(F), C(C), FrameSize(~0LL) {}
|
2007-09-27 22:18:46 +00:00
|
|
|
|
|
|
|
CollectorMetadata::~CollectorMetadata() {}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char CollectorModuleMetadata::ID = 0;
|
|
|
|
|
|
|
|
CollectorModuleMetadata::CollectorModuleMetadata()
|
|
|
|
: ImmutablePass((intptr_t)&ID) {}
|
|
|
|
|
|
|
|
CollectorModuleMetadata::~CollectorModuleMetadata() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
Collector *CollectorModuleMetadata::
|
|
|
|
getOrCreateCollector(const Module *M, const std::string &Name) {
|
|
|
|
const char *Start = Name.c_str();
|
|
|
|
|
|
|
|
collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size());
|
|
|
|
if (NMI != NameMap.end())
|
|
|
|
return NMI->getValue();
|
|
|
|
|
|
|
|
for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
|
|
|
|
E = CollectorRegistry::end(); I != E; ++I) {
|
|
|
|
if (strcmp(Start, I->getName()) == 0) {
|
|
|
|
Collector *C = I->instantiate();
|
|
|
|
C->M = M;
|
|
|
|
C->Name = Name;
|
|
|
|
NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C);
|
|
|
|
Collectors.push_back(C);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cerr << "unsupported collector: " << Name << "\n";
|
|
|
|
abort();
|
2007-09-27 22:18:46 +00:00
|
|
|
}
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
CollectorMetadata &CollectorModuleMetadata::get(const Function &F) {
|
|
|
|
assert(F.hasCollector());
|
|
|
|
function_map_type::iterator I = Map.find(&F);
|
|
|
|
if (I != Map.end())
|
|
|
|
return *I->second;
|
|
|
|
|
|
|
|
Collector *C = getOrCreateCollector(F.getParent(), F.getCollector());
|
|
|
|
CollectorMetadata *MD = C->insertFunctionMetadata(F);
|
|
|
|
Map[&F] = MD;
|
|
|
|
return *MD;
|
2007-09-27 22:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollectorModuleMetadata::clear() {
|
2007-12-11 00:30:17 +00:00
|
|
|
Map.clear();
|
2008-07-02 05:26:32 +00:00
|
|
|
NameMap.clear();
|
2007-12-11 00:30:17 +00:00
|
|
|
|
2007-09-27 22:18:46 +00:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
delete *I;
|
2007-12-11 00:30:17 +00:00
|
|
|
Collectors.clear();
|
2007-09-27 22:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char Printer::ID = 0;
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
|
2007-09-27 22:18:46 +00:00
|
|
|
return new Printer(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Printer::Printer(std::ostream &OS)
|
2007-12-11 00:30:17 +00:00
|
|
|
: FunctionPass(intptr_t(&ID)), OS(OS) {}
|
2007-09-27 22:18:46 +00:00
|
|
|
|
|
|
|
const char *Printer::getPassName() const {
|
|
|
|
return "Print Garbage Collector Information";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-12-11 00:30:17 +00:00
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
2007-09-27 22:18:46 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<CollectorModuleMetadata>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 00:30:17 +00:00
|
|
|
bool Printer::runOnFunction(Function &F) {
|
|
|
|
if (F.hasCollector()) {
|
|
|
|
CollectorMetadata *FD = &getAnalysis<CollectorModuleMetadata>().get(F);
|
2007-09-27 22:18:46 +00:00
|
|
|
|
|
|
|
OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
|
|
|
|
for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
|
|
|
|
RE = FD->roots_end();
|
|
|
|
RI != RE; ++RI)
|
|
|
|
OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
|
|
|
|
|
|
|
|
OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
|
|
|
|
for (CollectorMetadata::iterator PI = FD->begin(),
|
|
|
|
PE = FD->end(); PI != PE; ++PI) {
|
|
|
|
|
|
|
|
OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
|
|
|
|
|
|
|
|
for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
|
|
|
|
RE = FD->live_end(PI);;) {
|
|
|
|
OS << " " << RI->Num;
|
|
|
|
if (++RI == RE)
|
|
|
|
break;
|
|
|
|
OS << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << " }\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char Deleter::ID = 0;
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
FunctionPass *llvm::createCollectorMetadataDeleter() {
|
2007-09-27 22:18:46 +00:00
|
|
|
return new Deleter();
|
|
|
|
}
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
|
2007-09-27 22:18:46 +00:00
|
|
|
|
|
|
|
const char *Deleter::getPassName() const {
|
|
|
|
return "Delete Garbage Collector Information";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<CollectorModuleMetadata>();
|
|
|
|
}
|
|
|
|
|
2007-12-11 00:30:17 +00:00
|
|
|
bool Deleter::runOnFunction(Function &MF) {
|
2007-09-27 22:18:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Deleter::doFinalization(Module &M) {
|
2007-12-11 00:30:17 +00:00
|
|
|
CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
|
|
|
|
assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");
|
|
|
|
CMM->clear();
|
2007-09-27 22:18:46 +00:00
|
|
|
return false;
|
|
|
|
}
|