1 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the GCFunctionInfo class and GCModuleInfo pass. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/GCMetadata.h" 15 #include "llvm/CodeGen/GCStrategy.h" 16 #include "llvm/CodeGen/MachineFrameInfo.h" 17 #include "llvm/CodeGen/Passes.h" 18 #include "llvm/IR/Function.h" 19 #include "llvm/MC/MCSymbol.h" 20 #include "llvm/Pass.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/raw_ostream.h" 24 using namespace llvm; 25 26 namespace { 27 28 class Printer : public FunctionPass { 29 static char ID; 30 raw_ostream &OS; 31 32 public: 33 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {} 34 35 const char *getPassName() const override; 36 void getAnalysisUsage(AnalysisUsage &AU) const override; 37 38 bool runOnFunction(Function &F) override; 39 bool doFinalization(Module &M) override; 40 }; 41 } 42 43 INITIALIZE_PASS(GCModuleInfo, "collector-metadata", 44 "Create Garbage Collector Module Metadata", false, false) 45 46 // ----------------------------------------------------------------------------- 47 48 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) 49 : F(F), S(S), FrameSize(~0LL) {} 50 51 GCFunctionInfo::~GCFunctionInfo() {} 52 53 // ----------------------------------------------------------------------------- 54 55 char GCModuleInfo::ID = 0; 56 57 GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) { 58 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); 59 } 60 61 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { 62 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); 63 assert(F.hasGC()); 64 65 finfo_map_type::iterator I = FInfoMap.find(&F); 66 if (I != FInfoMap.end()) 67 return *I->second; 68 69 GCStrategy *S = getGCStrategy(F.getGC()); 70 Functions.push_back(make_unique<GCFunctionInfo>(F, *S)); 71 GCFunctionInfo *GFI = Functions.back().get(); 72 FInfoMap[&F] = GFI; 73 return *GFI; 74 } 75 76 void GCModuleInfo::clear() { 77 Functions.clear(); 78 FInfoMap.clear(); 79 GCStrategyList.clear(); 80 } 81 82 // ----------------------------------------------------------------------------- 83 84 char Printer::ID = 0; 85 86 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) { 87 return new Printer(OS); 88 } 89 90 const char *Printer::getPassName() const { 91 return "Print Garbage Collector Information"; 92 } 93 94 void Printer::getAnalysisUsage(AnalysisUsage &AU) const { 95 FunctionPass::getAnalysisUsage(AU); 96 AU.setPreservesAll(); 97 AU.addRequired<GCModuleInfo>(); 98 } 99 100 static const char *DescKind(GC::PointKind Kind) { 101 switch (Kind) { 102 case GC::Loop: 103 return "loop"; 104 case GC::Return: 105 return "return"; 106 case GC::PreCall: 107 return "pre-call"; 108 case GC::PostCall: 109 return "post-call"; 110 } 111 llvm_unreachable("Invalid point kind"); 112 } 113 114 bool Printer::runOnFunction(Function &F) { 115 if (F.hasGC()) 116 return false; 117 118 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); 119 120 OS << "GC roots for " << FD->getFunction().getName() << ":\n"; 121 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), 122 RE = FD->roots_end(); 123 RI != RE; ++RI) 124 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; 125 126 OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; 127 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE; 128 ++PI) { 129 130 OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind) 131 << ", live = {"; 132 133 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), 134 RE = FD->live_end(PI); 135 ;) { 136 OS << " " << RI->Num; 137 if (++RI == RE) 138 break; 139 OS << ","; 140 } 141 142 OS << " }\n"; 143 } 144 145 return false; 146 } 147 148 bool Printer::doFinalization(Module &M) { 149 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>(); 150 assert(GMI && "Printer didn't require GCModuleInfo?!"); 151 GMI->clear(); 152 return false; 153 } 154 155 156 GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) { 157 // TODO: Arguably, just doing a linear search would be faster for small N 158 auto NMI = GCStrategyMap.find(Name); 159 if (NMI != GCStrategyMap.end()) 160 return NMI->getValue(); 161 162 for (auto& Entry : GCRegistry::entries()) { 163 if (Name == Entry.getName()) { 164 std::unique_ptr<GCStrategy> S = Entry.instantiate(); 165 S->Name = Name; 166 GCStrategyMap[Name] = S.get(); 167 GCStrategyList.push_back(std::move(S)); 168 return GCStrategyList.back().get(); 169 } 170 } 171 172 report_fatal_error(std::string("unsupported GC: ") + Name); 173 } 174