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/MachineFrameInfo.h" 16 #include "llvm/CodeGen/Passes.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/GCStrategy.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 = F.getGCStrategy(); 70 if (!S) { 71 std::string error = std::string("unsupported GC: ") + F.getGC(); 72 report_fatal_error(error); 73 } 74 // Save the fact this strategy is associated with this module. Note that 75 // these are non-owning references, the GCStrategy remains owned by the 76 // Context. 77 StrategyList.push_back(S); 78 Functions.push_back(make_unique<GCFunctionInfo>(F, *S)); 79 GCFunctionInfo *GFI = Functions.back().get(); 80 FInfoMap[&F] = GFI; 81 return *GFI; 82 } 83 84 void GCModuleInfo::clear() { 85 Functions.clear(); 86 FInfoMap.clear(); 87 StrategyList.clear(); 88 } 89 90 // ----------------------------------------------------------------------------- 91 92 char Printer::ID = 0; 93 94 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) { 95 return new Printer(OS); 96 } 97 98 const char *Printer::getPassName() const { 99 return "Print Garbage Collector Information"; 100 } 101 102 void Printer::getAnalysisUsage(AnalysisUsage &AU) const { 103 FunctionPass::getAnalysisUsage(AU); 104 AU.setPreservesAll(); 105 AU.addRequired<GCModuleInfo>(); 106 } 107 108 static const char *DescKind(GC::PointKind Kind) { 109 switch (Kind) { 110 case GC::Loop: 111 return "loop"; 112 case GC::Return: 113 return "return"; 114 case GC::PreCall: 115 return "pre-call"; 116 case GC::PostCall: 117 return "post-call"; 118 } 119 llvm_unreachable("Invalid point kind"); 120 } 121 122 bool Printer::runOnFunction(Function &F) { 123 if (F.hasGC()) 124 return false; 125 126 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); 127 128 OS << "GC roots for " << FD->getFunction().getName() << ":\n"; 129 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), 130 RE = FD->roots_end(); 131 RI != RE; ++RI) 132 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; 133 134 OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; 135 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE; 136 ++PI) { 137 138 OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind) 139 << ", live = {"; 140 141 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), 142 RE = FD->live_end(PI); 143 ;) { 144 OS << " " << RI->Num; 145 if (++RI == RE) 146 break; 147 OS << ","; 148 } 149 150 OS << " }\n"; 151 } 152 153 return false; 154 } 155 156 bool Printer::doFinalization(Module &M) { 157 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>(); 158 assert(GMI && "Printer didn't require GCModuleInfo?!"); 159 GMI->clear(); 160 return false; 161 } 162