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