It turns out most of the thumb2 instructions are not allowed to touch SP. The semanti...
[llvm/avr.git] / lib / CodeGen / GCMetadata.cpp
blobcc8f82fbe531ce5934a3f129a547b0904228089a
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.
12 //===----------------------------------------------------------------------===//
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"
23 using namespace llvm;
25 namespace {
27 class VISIBILITY_HIDDEN Printer : public FunctionPass {
28 static char ID;
29 std::ostream &OS;
31 public:
32 explicit Printer(std::ostream &OS = *cerr);
34 const char *getPassName() const;
35 void getAnalysisUsage(AnalysisUsage &AU) const;
37 bool runOnFunction(Function &F);
40 class VISIBILITY_HIDDEN Deleter : public FunctionPass {
41 static char ID;
43 public:
44 Deleter();
46 const char *getPassName() const;
47 void getAnalysisUsage(AnalysisUsage &AU) const;
49 bool runOnFunction(Function &F);
50 bool doFinalization(Module &M);
55 static RegisterPass<GCModuleInfo>
56 X("collector-metadata", "Create Garbage Collector Module Metadata");
58 // -----------------------------------------------------------------------------
60 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
61 : F(F), S(S), FrameSize(~0LL) {}
63 GCFunctionInfo::~GCFunctionInfo() {}
65 // -----------------------------------------------------------------------------
67 char GCModuleInfo::ID = 0;
69 GCModuleInfo::GCModuleInfo()
70 : ImmutablePass(&ID) {}
72 GCModuleInfo::~GCModuleInfo() {
73 clear();
76 GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
77 const std::string &Name) {
78 strategy_map_type::iterator NMI = StrategyMap.find(Name);
79 if (NMI != StrategyMap.end())
80 return NMI->getValue();
82 for (GCRegistry::iterator I = GCRegistry::begin(),
83 E = GCRegistry::end(); I != E; ++I) {
84 if (Name == I->getName()) {
85 GCStrategy *S = I->instantiate();
86 S->M = M;
87 S->Name = Name;
88 StrategyMap.GetOrCreateValue(Name).setValue(S);
89 StrategyList.push_back(S);
90 return S;
94 cerr << "unsupported GC: " << Name << "\n";
95 llvm_unreachable(0);
98 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
99 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
100 assert(F.hasGC());
102 finfo_map_type::iterator I = FInfoMap.find(&F);
103 if (I != FInfoMap.end())
104 return *I->second;
106 GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
107 GCFunctionInfo *GFI = S->insertFunctionInfo(F);
108 FInfoMap[&F] = GFI;
109 return *GFI;
112 void GCModuleInfo::clear() {
113 FInfoMap.clear();
114 StrategyMap.clear();
116 for (iterator I = begin(), E = end(); I != E; ++I)
117 delete *I;
118 StrategyList.clear();
121 // -----------------------------------------------------------------------------
123 char Printer::ID = 0;
125 FunctionPass *llvm::createGCInfoPrinter(std::ostream &OS) {
126 return new Printer(OS);
129 Printer::Printer(std::ostream &OS)
130 : FunctionPass(&ID), OS(OS) {}
132 const char *Printer::getPassName() const {
133 return "Print Garbage Collector Information";
136 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
137 FunctionPass::getAnalysisUsage(AU);
138 AU.setPreservesAll();
139 AU.addRequired<GCModuleInfo>();
142 static const char *DescKind(GC::PointKind Kind) {
143 switch (Kind) {
144 default: llvm_unreachable("Unknown GC point kind");
145 case GC::Loop: return "loop";
146 case GC::Return: return "return";
147 case GC::PreCall: return "pre-call";
148 case GC::PostCall: return "post-call";
152 bool Printer::runOnFunction(Function &F) {
153 if (!F.hasGC()) {
154 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
156 OS << "GC roots for " << FD->getFunction().getNameStr() << ":\n";
157 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
158 RE = FD->roots_end(); RI != RE; ++RI)
159 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
161 OS << "GC safe points for " << FD->getFunction().getNameStr() << ":\n";
162 for (GCFunctionInfo::iterator PI = FD->begin(),
163 PE = FD->end(); PI != PE; ++PI) {
165 OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {";
167 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
168 RE = FD->live_end(PI);;) {
169 OS << " " << RI->Num;
170 if (++RI == RE)
171 break;
172 OS << ",";
175 OS << " }\n";
179 return false;
182 // -----------------------------------------------------------------------------
184 char Deleter::ID = 0;
186 FunctionPass *llvm::createGCInfoDeleter() {
187 return new Deleter();
190 Deleter::Deleter() : FunctionPass(&ID) {}
192 const char *Deleter::getPassName() const {
193 return "Delete Garbage Collector Information";
196 void Deleter::getAnalysisUsage(AnalysisUsage &AU) const {
197 AU.setPreservesAll();
198 AU.addRequired<GCModuleInfo>();
201 bool Deleter::runOnFunction(Function &MF) {
202 return false;
205 bool Deleter::doFinalization(Module &M) {
206 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
207 assert(GMI && "Deleter didn't require GCModuleInfo?!");
208 GMI->clear();
209 return false;