1 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the GCFunctionInfo class and GCModuleInfo pass. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/GCMetadata.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/CodeGen/GCStrategy.h" 17 #include "llvm/CodeGen/Passes.h" 18 #include "llvm/IR/Function.h" 19 #include "llvm/InitializePasses.h" 20 #include "llvm/MC/MCSymbol.h" 21 #include "llvm/Pass.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 #include <cassert> 26 #include <memory> 27 #include <string> 28 29 using namespace llvm; 30 31 namespace { 32 33 class Printer : public FunctionPass { 34 static char ID; 35 36 raw_ostream &OS; 37 38 public: 39 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {} 40 41 StringRef getPassName() const override; 42 void getAnalysisUsage(AnalysisUsage &AU) const override; 43 44 bool runOnFunction(Function &F) override; 45 bool doFinalization(Module &M) override; 46 }; 47 48 } // end anonymous namespace 49 50 INITIALIZE_PASS(GCModuleInfo, "collector-metadata", 51 "Create Garbage Collector Module Metadata", false, false) 52 53 // ----------------------------------------------------------------------------- 54 55 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) 56 : F(F), S(S), FrameSize(~0LL) {} 57 58 GCFunctionInfo::~GCFunctionInfo() = default; 59 60 // ----------------------------------------------------------------------------- 61 62 char GCModuleInfo::ID = 0; 63 64 GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) { 65 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); 66 } 67 68 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { 69 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); 70 assert(F.hasGC()); 71 72 finfo_map_type::iterator I = FInfoMap.find(&F); 73 if (I != FInfoMap.end()) 74 return *I->second; 75 76 GCStrategy *S = getGCStrategy(F.getGC()); 77 Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S)); 78 GCFunctionInfo *GFI = Functions.back().get(); 79 FInfoMap[&F] = GFI; 80 return *GFI; 81 } 82 83 void GCModuleInfo::clear() { 84 Functions.clear(); 85 FInfoMap.clear(); 86 GCStrategyList.clear(); 87 } 88 89 // ----------------------------------------------------------------------------- 90 91 char Printer::ID = 0; 92 93 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) { 94 return new Printer(OS); 95 } 96 97 StringRef Printer::getPassName() const { 98 return "Print Garbage Collector Information"; 99 } 100 101 void Printer::getAnalysisUsage(AnalysisUsage &AU) const { 102 FunctionPass::getAnalysisUsage(AU); 103 AU.setPreservesAll(); 104 AU.addRequired<GCModuleInfo>(); 105 } 106 107 bool Printer::runOnFunction(Function &F) { 108 if (F.hasGC()) 109 return false; 110 111 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); 112 113 OS << "GC roots for " << FD->getFunction().getName() << ":\n"; 114 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), 115 RE = FD->roots_end(); 116 RI != RE; ++RI) 117 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; 118 119 OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; 120 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE; 121 ++PI) { 122 123 OS << "\t" << PI->Label->getName() << ": " << "post-call" 124 << ", live = {"; 125 126 ListSeparator LS(","); 127 for (const GCRoot &R : make_range(FD->live_begin(PI), FD->live_end(PI))) 128 OS << LS << " " << R.Num; 129 130 OS << " }\n"; 131 } 132 133 return false; 134 } 135 136 bool Printer::doFinalization(Module &M) { 137 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>(); 138 assert(GMI && "Printer didn't require GCModuleInfo?!"); 139 GMI->clear(); 140 return false; 141 } 142 143 GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) { 144 // TODO: Arguably, just doing a linear search would be faster for small N 145 auto NMI = GCStrategyMap.find(Name); 146 if (NMI != GCStrategyMap.end()) 147 return NMI->getValue(); 148 149 for (auto& Entry : GCRegistry::entries()) { 150 if (Name == Entry.getName()) { 151 std::unique_ptr<GCStrategy> S = Entry.instantiate(); 152 S->Name = std::string(Name); 153 GCStrategyMap[Name] = S.get(); 154 GCStrategyList.push_back(std::move(S)); 155 return GCStrategyList.back().get(); 156 } 157 } 158 159 if (GCRegistry::begin() == GCRegistry::end()) { 160 // In normal operation, the registry should not be empty. There should 161 // be the builtin GCs if nothing else. The most likely scenario here is 162 // that we got here without running the initializers used by the Registry 163 // itself and it's registration mechanism. 164 const std::string error = ("unsupported GC: " + Name).str() + 165 " (did you remember to link and initialize the CodeGen library?)"; 166 report_fatal_error(error); 167 } else 168 report_fatal_error(std::string("unsupported GC: ") + Name); 169 } 170