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/MC/MCSymbol.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
28 class Printer : public FunctionPass {
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 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
58 "Create Garbage Collector Module Metadata", false, false)
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()
73 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
76 GCModuleInfo::~GCModuleInfo() {
80 GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
81 const std::string &Name) {
82 strategy_map_type::iterator NMI = StrategyMap.find(Name);
83 if (NMI != StrategyMap.end())
84 return NMI->getValue();
86 for (GCRegistry::iterator I = GCRegistry::begin(),
87 E = GCRegistry::end(); I != E; ++I) {
88 if (Name == I->getName()) {
89 GCStrategy *S = I->instantiate();
92 StrategyMap.GetOrCreateValue(Name).setValue(S);
93 StrategyList.push_back(S);
98 dbgs() << "unsupported GC: " << Name << "\n";
102 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
103 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
106 finfo_map_type::iterator I = FInfoMap.find(&F);
107 if (I != FInfoMap.end())
110 GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
111 GCFunctionInfo *GFI = S->insertFunctionInfo(F);
116 void GCModuleInfo::clear() {
120 for (iterator I = begin(), E = end(); I != E; ++I)
122 StrategyList.clear();
125 // -----------------------------------------------------------------------------
127 char Printer::ID = 0;
129 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
130 return new Printer(OS);
134 const char *Printer::getPassName() const {
135 return "Print Garbage Collector Information";
138 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
139 FunctionPass::getAnalysisUsage(AU);
140 AU.setPreservesAll();
141 AU.addRequired<GCModuleInfo>();
144 static const char *DescKind(GC::PointKind Kind) {
146 case GC::Loop: return "loop";
147 case GC::Return: return "return";
148 case GC::PreCall: return "pre-call";
149 case GC::PostCall: return "post-call";
151 llvm_unreachable("Invalid point kind");
154 bool Printer::runOnFunction(Function &F) {
155 if (F.hasGC()) return false;
157 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
159 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
160 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
161 RE = FD->roots_end(); RI != RE; ++RI)
162 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
164 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
165 for (GCFunctionInfo::iterator PI = FD->begin(),
166 PE = FD->end(); PI != PE; ++PI) {
168 OS << "\t" << PI->Label->getName() << ": "
169 << DescKind(PI->Kind) << ", live = {";
171 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
172 RE = FD->live_end(PI);;) {
173 OS << " " << RI->Num;
185 // -----------------------------------------------------------------------------
187 char Deleter::ID = 0;
189 FunctionPass *llvm::createGCInfoDeleter() {
190 return new Deleter();
193 Deleter::Deleter() : FunctionPass(ID) {}
195 const char *Deleter::getPassName() const {
196 return "Delete Garbage Collector Information";
199 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
200 AU.setPreservesAll();
201 AU.addRequired<GCModuleInfo>();
204 bool Deleter::runOnFunction(Function &MF) {
208 bool Deleter::doFinalization(Module &M) {
209 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
210 assert(GMI && "Deleter didn't require GCModuleInfo?!");