Home | History | Annotate | Download | only in CodeGen
      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 
     36     const char *getPassName() const override;
     37     void getAnalysisUsage(AnalysisUsage &AU) const override;
     38 
     39     bool runOnFunction(Function &F) override;
     40     bool doFinalization(Module &M) override;
     41   };
     42 
     43 }
     44 
     45 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
     46                 "Create Garbage Collector Module Metadata", false, false)
     47 
     48 // -----------------------------------------------------------------------------
     49 
     50 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
     51   : F(F), S(S), FrameSize(~0LL) {}
     52 
     53 GCFunctionInfo::~GCFunctionInfo() {}
     54 
     55 // -----------------------------------------------------------------------------
     56 
     57 char GCModuleInfo::ID = 0;
     58 
     59 GCModuleInfo::GCModuleInfo()
     60     : ImmutablePass(ID) {
     61   initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
     62 }
     63 
     64 GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
     65                                               const std::string &Name) {
     66   strategy_map_type::iterator NMI = StrategyMap.find(Name);
     67   if (NMI != StrategyMap.end())
     68     return NMI->getValue();
     69 
     70   for (GCRegistry::iterator I = GCRegistry::begin(),
     71                             E = GCRegistry::end(); I != E; ++I) {
     72     if (Name == I->getName()) {
     73       std::unique_ptr<GCStrategy> S = I->instantiate();
     74       S->M = M;
     75       S->Name = Name;
     76       StrategyMap.GetOrCreateValue(Name).setValue(S.get());
     77       StrategyList.push_back(std::move(S));
     78       return StrategyList.back().get();
     79     }
     80   }
     81 
     82   dbgs() << "unsupported GC: " << Name << "\n";
     83   llvm_unreachable(nullptr);
     84 }
     85 
     86 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
     87   assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
     88   assert(F.hasGC());
     89 
     90   finfo_map_type::iterator I = FInfoMap.find(&F);
     91   if (I != FInfoMap.end())
     92     return *I->second;
     93 
     94   GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
     95   GCFunctionInfo *GFI = S->insertFunctionInfo(F);
     96   FInfoMap[&F] = GFI;
     97   return *GFI;
     98 }
     99 
    100 void GCModuleInfo::clear() {
    101   FInfoMap.clear();
    102   StrategyMap.clear();
    103   StrategyList.clear();
    104 }
    105 
    106 // -----------------------------------------------------------------------------
    107 
    108 char Printer::ID = 0;
    109 
    110 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
    111   return new Printer(OS);
    112 }
    113 
    114 
    115 const char *Printer::getPassName() const {
    116   return "Print Garbage Collector Information";
    117 }
    118 
    119 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
    120   FunctionPass::getAnalysisUsage(AU);
    121   AU.setPreservesAll();
    122   AU.addRequired<GCModuleInfo>();
    123 }
    124 
    125 static const char *DescKind(GC::PointKind Kind) {
    126   switch (Kind) {
    127     case GC::Loop:     return "loop";
    128     case GC::Return:   return "return";
    129     case GC::PreCall:  return "pre-call";
    130     case GC::PostCall: return "post-call";
    131   }
    132   llvm_unreachable("Invalid point kind");
    133 }
    134 
    135 bool Printer::runOnFunction(Function &F) {
    136   if (F.hasGC()) return false;
    137 
    138   GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
    139 
    140   OS << "GC roots for " << FD->getFunction().getName() << ":\n";
    141   for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
    142                                       RE = FD->roots_end(); RI != RE; ++RI)
    143     OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
    144 
    145   OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
    146   for (GCFunctionInfo::iterator PI = FD->begin(),
    147                                 PE = FD->end(); PI != PE; ++PI) {
    148 
    149     OS << "\t" << PI->Label->getName() << ": "
    150        << DescKind(PI->Kind) << ", live = {";
    151 
    152     for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
    153                                        RE = FD->live_end(PI);;) {
    154       OS << " " << RI->Num;
    155       if (++RI == RE)
    156         break;
    157       OS << ",";
    158     }
    159 
    160     OS << " }\n";
    161   }
    162 
    163   return false;
    164 }
    165 
    166 bool Printer::doFinalization(Module &M) {
    167   GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
    168   assert(GMI && "Printer didn't require GCModuleInfo?!");
    169   GMI->clear();
    170   return false;
    171 }
    172