LLVM API Documentation
00001 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the GCFunctionInfo class and GCModuleInfo pass. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/GCMetadata.h" 00015 #include "llvm/CodeGen/GCStrategy.h" 00016 #include "llvm/CodeGen/MachineFrameInfo.h" 00017 #include "llvm/Pass.h" 00018 #include "llvm/CodeGen/Passes.h" 00019 #include "llvm/Function.h" 00020 #include "llvm/Support/Compiler.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 00023 using namespace llvm; 00024 00025 namespace { 00026 00027 class VISIBILITY_HIDDEN Printer : public FunctionPass { 00028 static char ID; 00029 std::ostream &OS; 00030 00031 public: 00032 explicit Printer(std::ostream &OS = *cerr); 00033 00034 const char *getPassName() const; 00035 void getAnalysisUsage(AnalysisUsage &AU) const; 00036 00037 bool runOnFunction(Function &F); 00038 }; 00039 00040 class VISIBILITY_HIDDEN Deleter : public FunctionPass { 00041 static char ID; 00042 00043 public: 00044 Deleter(); 00045 00046 const char *getPassName() const; 00047 void getAnalysisUsage(AnalysisUsage &AU) const; 00048 00049 bool runOnFunction(Function &F); 00050 bool doFinalization(Module &M); 00051 }; 00052 00053 } 00054 00055 static RegisterPass<GCModuleInfo> 00056 X("collector-metadata", "Create Garbage Collector Module Metadata"); 00057 00058 // ----------------------------------------------------------------------------- 00059 00060 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) 00061 : F(F), S(S), FrameSize(~0LL) {} 00062 00063 GCFunctionInfo::~GCFunctionInfo() {} 00064 00065 // ----------------------------------------------------------------------------- 00066 00067 char GCModuleInfo::ID = 0; 00068 00069 GCModuleInfo::GCModuleInfo() 00070 : ImmutablePass(&ID) {} 00071 00072 GCModuleInfo::~GCModuleInfo() { 00073 clear(); 00074 } 00075 00076 GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M, 00077 const std::string &Name) { 00078 strategy_map_type::iterator NMI = StrategyMap.find(Name); 00079 if (NMI != StrategyMap.end()) 00080 return NMI->getValue(); 00081 00082 for (GCRegistry::iterator I = GCRegistry::begin(), 00083 E = GCRegistry::end(); I != E; ++I) { 00084 if (Name == I->getName()) { 00085 GCStrategy *S = I->instantiate(); 00086 S->M = M; 00087 S->Name = Name; 00088 StrategyMap.GetOrCreateValue(Name).setValue(S); 00089 StrategyList.push_back(S); 00090 return S; 00091 } 00092 } 00093 00094 cerr << "unsupported GC: " << Name << "\n"; 00095 llvm_unreachable(0); 00096 } 00097 00098 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { 00099 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); 00100 assert(F.hasGC()); 00101 00102 finfo_map_type::iterator I = FInfoMap.find(&F); 00103 if (I != FInfoMap.end()) 00104 return *I->second; 00105 00106 GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC()); 00107 GCFunctionInfo *GFI = S->insertFunctionInfo(F); 00108 FInfoMap[&F] = GFI; 00109 return *GFI; 00110 } 00111 00112 void GCModuleInfo::clear() { 00113 FInfoMap.clear(); 00114 StrategyMap.clear(); 00115 00116 for (iterator I = begin(), E = end(); I != E; ++I) 00117 delete *I; 00118 StrategyList.clear(); 00119 } 00120 00121 // ----------------------------------------------------------------------------- 00122 00123 char Printer::ID = 0; 00124 00125 FunctionPass *llvm::createGCInfoPrinter(std::ostream &OS) { 00126 return new Printer(OS); 00127 } 00128 00129 Printer::Printer(std::ostream &OS) 00130 : FunctionPass(&ID), OS(OS) {} 00131 00132 const char *Printer::getPassName() const { 00133 return "Print Garbage Collector Information"; 00134 } 00135 00136 void Printer::getAnalysisUsage(AnalysisUsage &AU) const { 00137 FunctionPass::getAnalysisUsage(AU); 00138 AU.setPreservesAll(); 00139 AU.addRequired<GCModuleInfo>(); 00140 } 00141 00142 static const char *DescKind(GC::PointKind Kind) { 00143 switch (Kind) { 00144 default: llvm_unreachable("Unknown GC point kind"); 00145 case GC::Loop: return "loop"; 00146 case GC::Return: return "return"; 00147 case GC::PreCall: return "pre-call"; 00148 case GC::PostCall: return "post-call"; 00149 } 00150 } 00151 00152 bool Printer::runOnFunction(Function &F) { 00153 if (!F.hasGC()) { 00154 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); 00155 00156 OS << "GC roots for " << FD->getFunction().getNameStr() << ":\n"; 00157 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), 00158 RE = FD->roots_end(); RI != RE; ++RI) 00159 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; 00160 00161 OS << "GC safe points for " << FD->getFunction().getNameStr() << ":\n"; 00162 for (GCFunctionInfo::iterator PI = FD->begin(), 00163 PE = FD->end(); PI != PE; ++PI) { 00164 00165 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {"; 00166 00167 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), 00168 RE = FD->live_end(PI);;) { 00169 OS << " " << RI->Num; 00170 if (++RI == RE) 00171 break; 00172 OS << ","; 00173 } 00174 00175 OS << " }\n"; 00176 } 00177 } 00178 00179 return false; 00180 } 00181 00182 // ----------------------------------------------------------------------------- 00183 00184 char Deleter::ID = 0; 00185 00186 FunctionPass *llvm::createGCInfoDeleter() { 00187 return new Deleter(); 00188 } 00189 00190 Deleter::Deleter() : FunctionPass(&ID) {} 00191 00192 const char *Deleter::getPassName() const { 00193 return "Delete Garbage Collector Information"; 00194 } 00195 00196 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const { 00197 AU.setPreservesAll(); 00198 AU.addRequired<GCModuleInfo>(); 00199 } 00200 00201 bool Deleter::runOnFunction(Function &MF) { 00202 return false; 00203 } 00204 00205 bool Deleter::doFinalization(Module &M) { 00206 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>(); 00207 assert(GMI && "Deleter didn't require GCModuleInfo?!"); 00208 GMI->clear(); 00209 return false; 00210 }