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