1 //===-- CollectorMetadata.cpp - Garbage collector metadata ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Gordon Henriksen and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CollectorMetadata and CollectorModuleMetadata
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/CollectorMetadata.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/Function.h"
19 #include "llvm/Support/Compiler.h"
25 class VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
30 Printer(std::ostream &OS = *cerr);
32 const char *getPassName() const;
33 void getAnalysisUsage(AnalysisUsage &AU) const;
35 bool runOnMachineFunction(MachineFunction &MF);
38 class VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
44 const char *getPassName() const;
45 void getAnalysisUsage(AnalysisUsage &AU) const;
47 bool runOnMachineFunction(MachineFunction &MF);
48 bool doFinalization(Module &M);
51 RegisterPass<CollectorModuleMetadata>
52 X("collector-metadata", "Create Garbage Collector Module Metadata");
56 // -----------------------------------------------------------------------------
58 CollectorMetadata::CollectorMetadata(const Function &F)
59 : F(F), FrameSize(~0LL) {}
61 CollectorMetadata::~CollectorMetadata() {}
63 // -----------------------------------------------------------------------------
65 char CollectorModuleMetadata::ID = 0;
67 CollectorModuleMetadata::CollectorModuleMetadata()
68 : ImmutablePass((intptr_t)&ID) {}
70 CollectorModuleMetadata::~CollectorModuleMetadata() {
74 CollectorMetadata& CollectorModuleMetadata::insert(const Function *F) {
75 assert(Map.find(F) == Map.end() && "Function GC metadata already exists!");
76 CollectorMetadata *FMD = new CollectorMetadata(*F);
77 Functions.push_back(FMD);
82 CollectorMetadata* CollectorModuleMetadata::get(const Function *F) const {
83 map_type::iterator I = Map.find(F);
89 void CollectorModuleMetadata::clear() {
90 for (iterator I = begin(), E = end(); I != E; ++I)
97 // -----------------------------------------------------------------------------
101 Pass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
102 return new Printer(OS);
105 Printer::Printer(std::ostream &OS)
106 : MachineFunctionPass(intptr_t(&ID)), OS(OS) {}
108 const char *Printer::getPassName() const {
109 return "Print Garbage Collector Information";
112 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
113 MachineFunctionPass::getAnalysisUsage(AU);
114 AU.setPreservesAll();
115 AU.addRequired<CollectorModuleMetadata>();
118 static const char *DescKind(GC::PointKind Kind) {
120 default: assert(0 && "Unknown GC point kind");
121 case GC::Loop: return "loop";
122 case GC::Return: return "return";
123 case GC::PreCall: return "pre-call";
124 case GC::PostCall: return "post-call";
128 bool Printer::runOnMachineFunction(MachineFunction &MF) {
129 if (CollectorMetadata *FD =
130 getAnalysis<CollectorModuleMetadata>().get(MF.getFunction())) {
132 OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
133 for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
134 RE = FD->roots_end();
136 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
138 OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
139 for (CollectorMetadata::iterator PI = FD->begin(),
140 PE = FD->end(); PI != PE; ++PI) {
142 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
144 for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
145 RE = FD->live_end(PI);;) {
146 OS << " " << RI->Num;
159 // -----------------------------------------------------------------------------
161 char Deleter::ID = 0;
163 Pass *llvm::createCollectorMetadataDeleter() {
164 return new Deleter();
167 Deleter::Deleter() : MachineFunctionPass(intptr_t(&ID)) {}
169 const char *Deleter::getPassName() const {
170 return "Delete Garbage Collector Information";
173 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
174 AU.setPreservesAll();
175 AU.addRequired<CollectorModuleMetadata>();
178 bool Deleter::runOnMachineFunction(MachineFunction &MF) {
182 bool Deleter::doFinalization(Module &M) {
183 getAnalysis<CollectorModuleMetadata>().clear();