43f3d431e97903ada1cd4c0ffb4bd19da9fa3ce5
[oota-llvm.git] / lib / CodeGen / CollectorMetadata.cpp
1 //===-- CollectorMetadata.cpp - Garbage collector metadata ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Gordon Henriksen and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the CollectorMetadata and CollectorModuleMetadata
11 // classes.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/CodeGen/CollectorMetadata.h"
16 #include "llvm/CodeGen/Collector.h"
17 #include "llvm/CodeGen/Collectors.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/Pass.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Support/Compiler.h"
23
24 using namespace llvm;
25
26 namespace {
27   
28   class VISIBILITY_HIDDEN Printer : public FunctionPass {
29     static char ID;
30     std::ostream &OS;
31     
32   public:
33     explicit Printer(std::ostream &OS = *cerr);
34     
35     const char *getPassName() const;
36     void getAnalysisUsage(AnalysisUsage &AU) const;
37     
38     bool runOnFunction(Function &F);
39   };
40   
41   class VISIBILITY_HIDDEN Deleter : public FunctionPass {
42     static char ID;
43     
44   public:
45     Deleter();
46     
47     const char *getPassName() const;
48     void getAnalysisUsage(AnalysisUsage &AU) const;
49     
50     bool runOnFunction(Function &F);
51     bool doFinalization(Module &M);
52   };
53   
54   RegisterPass<CollectorModuleMetadata>
55   X("collector-metadata", "Create Garbage Collector Module Metadata");
56   
57 }
58
59 // -----------------------------------------------------------------------------
60
61 CollectorMetadata::CollectorMetadata(const Function &F, Collector &C)
62   : F(F), C(C), FrameSize(~0LL) {}
63
64 CollectorMetadata::~CollectorMetadata() {}
65
66 // -----------------------------------------------------------------------------
67
68 char CollectorModuleMetadata::ID = 0;
69
70 CollectorModuleMetadata::CollectorModuleMetadata()
71   : ImmutablePass((intptr_t)&ID) {}
72
73 CollectorModuleMetadata::~CollectorModuleMetadata() {
74   clear();
75 }
76
77 Collector *CollectorModuleMetadata::
78 getOrCreateCollector(const Module *M, const std::string &Name) {
79   const char *Start = Name.c_str();
80   
81   collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size());
82   if (NMI != NameMap.end())
83     return NMI->getValue();
84   
85   for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
86                                    E = CollectorRegistry::end(); I != E; ++I) {
87     if (strcmp(Start, I->getName()) == 0) {
88       Collector *C = I->instantiate();
89       C->M = M;
90       C->Name = Name;
91       NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C);
92       Collectors.push_back(C);
93       return C;
94     }
95   }
96   
97   cerr << "unsupported collector: " << Name << "\n";
98   abort();
99 }
100
101 CollectorMetadata &CollectorModuleMetadata::get(const Function &F) {
102   assert(F.hasCollector());
103   function_map_type::iterator I = Map.find(&F);
104   if (I != Map.end())
105     return *I->second;
106     
107   Collector *C = getOrCreateCollector(F.getParent(), F.getCollector());
108   CollectorMetadata *MD = C->insertFunctionMetadata(F);
109   Map[&F] = MD;
110   return *MD;
111 }
112
113 void CollectorModuleMetadata::clear() {
114   Map.clear();
115   
116   // TODO: StringMap should provide a clear method.
117   while (!NameMap.empty())
118     NameMap.erase(NameMap.begin());
119   
120   for (iterator I = begin(), E = end(); I != E; ++I)
121     delete *I;
122   Collectors.clear();
123 }
124
125 // -----------------------------------------------------------------------------
126
127 char Printer::ID = 0;
128
129 FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
130   return new Printer(OS);
131 }
132
133 Printer::Printer(std::ostream &OS)
134   : FunctionPass(intptr_t(&ID)), OS(OS) {}
135
136 const char *Printer::getPassName() const {
137   return "Print Garbage Collector Information";
138 }
139
140 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
141   FunctionPass::getAnalysisUsage(AU);
142   AU.setPreservesAll();
143   AU.addRequired<CollectorModuleMetadata>();
144 }
145
146 static const char *DescKind(GC::PointKind Kind) {
147   switch (Kind) {
148     default: assert(0 && "Unknown GC point kind");
149     case GC::Loop:     return "loop";
150     case GC::Return:   return "return";
151     case GC::PreCall:  return "pre-call";
152     case GC::PostCall: return "post-call";
153   }
154 }
155
156 bool Printer::runOnFunction(Function &F) {
157   if (F.hasCollector()) {
158     CollectorMetadata *FD = &getAnalysis<CollectorModuleMetadata>().get(F);
159     
160     OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
161     for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
162                                            RE = FD->roots_end();
163                                            RI != RE; ++RI)
164       OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
165     
166     OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n";
167     for (CollectorMetadata::iterator PI = FD->begin(),
168                                      PE = FD->end(); PI != PE; ++PI) {
169       
170       OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
171       
172       for (CollectorMetadata::live_iterator RI = FD->live_begin(PI),
173                                             RE = FD->live_end(PI);;) {
174         OS << " " << RI->Num;
175         if (++RI == RE)
176           break;
177         OS << ",";
178       }
179       
180       OS << " }\n";
181     }
182   }
183   
184   return false;
185 }
186
187 // -----------------------------------------------------------------------------
188
189 char Deleter::ID = 0;
190
191 FunctionPass *llvm::createCollectorMetadataDeleter() {
192   return new Deleter();
193 }
194
195 Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
196
197 const char *Deleter::getPassName() const {
198   return "Delete Garbage Collector Information";
199 }
200
201 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
202   AU.setPreservesAll();
203   AU.addRequired<CollectorModuleMetadata>();
204 }
205
206 bool Deleter::runOnFunction(Function &MF) {
207   return false;
208 }
209
210 bool Deleter::doFinalization(Module &M) {
211   CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
212   assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");
213   CMM->clear();
214   return false;
215 }