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