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