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/Pass.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/Function.h"
20#include "llvm/MC/MCSymbol.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26namespace {
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;
37    void getAnalysisUsage(AnalysisUsage &AU) const;
38
39    bool runOnFunction(Function &F);
40  };
41
42  class Deleter : public FunctionPass {
43    static char ID;
44
45  public:
46    Deleter();
47
48    const char *getPassName() const;
49    void getAnalysisUsage(AnalysisUsage &AU) const;
50
51    bool runOnFunction(Function &F);
52    bool doFinalization(Module &M);
53  };
54
55}
56
57INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
58                "Create Garbage Collector Module Metadata", false, false)
59
60// -----------------------------------------------------------------------------
61
62GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
63  : F(F), S(S), FrameSize(~0LL) {}
64
65GCFunctionInfo::~GCFunctionInfo() {}
66
67// -----------------------------------------------------------------------------
68
69char GCModuleInfo::ID = 0;
70
71GCModuleInfo::GCModuleInfo()
72    : ImmutablePass(ID) {
73  initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
74}
75
76GCModuleInfo::~GCModuleInfo() {
77  clear();
78}
79
80GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
81                                              const std::string &Name) {
82  strategy_map_type::iterator NMI = StrategyMap.find(Name);
83  if (NMI != StrategyMap.end())
84    return NMI->getValue();
85
86  for (GCRegistry::iterator I = GCRegistry::begin(),
87                            E = GCRegistry::end(); I != E; ++I) {
88    if (Name == I->getName()) {
89      GCStrategy *S = I->instantiate();
90      S->M = M;
91      S->Name = Name;
92      StrategyMap.GetOrCreateValue(Name).setValue(S);
93      StrategyList.push_back(S);
94      return S;
95    }
96  }
97
98  dbgs() << "unsupported GC: " << Name << "\n";
99  llvm_unreachable(0);
100}
101
102GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
103  assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
104  assert(F.hasGC());
105
106  finfo_map_type::iterator I = FInfoMap.find(&F);
107  if (I != FInfoMap.end())
108    return *I->second;
109
110  GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
111  GCFunctionInfo *GFI = S->insertFunctionInfo(F);
112  FInfoMap[&F] = GFI;
113  return *GFI;
114}
115
116void GCModuleInfo::clear() {
117  FInfoMap.clear();
118  StrategyMap.clear();
119
120  for (iterator I = begin(), E = end(); I != E; ++I)
121    delete *I;
122  StrategyList.clear();
123}
124
125// -----------------------------------------------------------------------------
126
127char Printer::ID = 0;
128
129FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
130  return new Printer(OS);
131}
132
133
134const char *Printer::getPassName() const {
135  return "Print Garbage Collector Information";
136}
137
138void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
139  FunctionPass::getAnalysisUsage(AU);
140  AU.setPreservesAll();
141  AU.addRequired<GCModuleInfo>();
142}
143
144static const char *DescKind(GC::PointKind Kind) {
145  switch (Kind) {
146    default: llvm_unreachable("Unknown GC point kind");
147    case GC::Loop:     return "loop";
148    case GC::Return:   return "return";
149    case GC::PreCall:  return "pre-call";
150    case GC::PostCall: return "post-call";
151  }
152}
153
154bool Printer::runOnFunction(Function &F) {
155  if (F.hasGC()) return false;
156
157  GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
158
159  OS << "GC roots for " << FD->getFunction().getNameStr() << ":\n";
160  for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
161                                      RE = FD->roots_end(); RI != RE; ++RI)
162    OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
163
164  OS << "GC safe points for " << FD->getFunction().getNameStr() << ":\n";
165  for (GCFunctionInfo::iterator PI = FD->begin(),
166                                PE = FD->end(); PI != PE; ++PI) {
167
168    OS << "\t" << PI->Label->getName() << ": "
169       << DescKind(PI->Kind) << ", live = {";
170
171    for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
172                                       RE = FD->live_end(PI);;) {
173      OS << " " << RI->Num;
174      if (++RI == RE)
175        break;
176      OS << ",";
177    }
178
179    OS << " }\n";
180  }
181
182  return false;
183}
184
185// -----------------------------------------------------------------------------
186
187char Deleter::ID = 0;
188
189FunctionPass *llvm::createGCInfoDeleter() {
190  return new Deleter();
191}
192
193Deleter::Deleter() : FunctionPass(ID) {}
194
195const char *Deleter::getPassName() const {
196  return "Delete Garbage Collector Information";
197}
198
199void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
200  AU.setPreservesAll();
201  AU.addRequired<GCModuleInfo>();
202}
203
204bool Deleter::runOnFunction(Function &MF) {
205  return false;
206}
207
208bool Deleter::doFinalization(Module &M) {
209  GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
210  assert(GMI && "Deleter didn't require GCModuleInfo?!");
211  GMI->clear();
212  return false;
213}
214