blob: ab8383a45c4c2e2af7a9103bf81d0ac08b2e75cb [file] [log] [blame]
Gordon Henriksend930f912008-08-17 18:44:35 +00001//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
Gordon Henriksen613afce2007-09-27 22:18:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen613afce2007-09-27 22:18:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksend930f912008-08-17 18:44:35 +000010// This file implements the GCFunctionInfo class and GCModuleInfo pass.
Gordon Henriksen613afce2007-09-27 22:18:46 +000011//
12//===----------------------------------------------------------------------===//
13
Gordon Henriksenbcef14d2008-08-17 12:56:54 +000014#include "llvm/CodeGen/GCMetadata.h"
Gordon Henriksen613afce2007-09-27 22:18:46 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Gordon Henriksen7843c162007-12-11 00:30:17 +000016#include "llvm/CodeGen/Passes.h"
Philip Reames56a03932015-01-26 18:26:35 +000017#include "llvm/CodeGen/GCStrategy.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
Chris Lattner1065f492010-03-14 07:27:07 +000019#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Pass.h"
David Greene821e67e2010-01-04 21:35:15 +000021#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner565449d2009-08-23 03:13:20 +000023#include "llvm/Support/raw_ostream.h"
Gordon Henriksen613afce2007-09-27 22:18:46 +000024using namespace llvm;
25
26namespace {
Chris Lattner565449d2009-08-23 03:13:20 +000027
Philip Reames36319532015-01-16 23:16:12 +000028class Printer : public FunctionPass {
29 static char ID;
30 raw_ostream &OS;
Craig Topper4584cd52014-03-07 09:26:03 +000031
Philip Reames36319532015-01-16 23:16:12 +000032public:
33 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
Craig Topper4584cd52014-03-07 09:26:03 +000034
Philip Reames36319532015-01-16 23:16:12 +000035 const char *getPassName() const override;
36 void getAnalysisUsage(AnalysisUsage &AU) const override;
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +000037
Philip Reames36319532015-01-16 23:16:12 +000038 bool runOnFunction(Function &F) override;
39 bool doFinalization(Module &M) override;
40};
Gordon Henriksen613afce2007-09-27 22:18:46 +000041}
42
Owen Andersona57b97e2010-07-21 22:09:45 +000043INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
Owen Andersondf7a4f22010-10-07 22:25:06 +000044 "Create Garbage Collector Module Metadata", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000045
Gordon Henriksen613afce2007-09-27 22:18:46 +000046// -----------------------------------------------------------------------------
47
Gordon Henriksend930f912008-08-17 18:44:35 +000048GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
Philip Reames36319532015-01-16 23:16:12 +000049 : F(F), S(S), FrameSize(~0LL) {}
Gordon Henriksen613afce2007-09-27 22:18:46 +000050
Gordon Henriksend930f912008-08-17 18:44:35 +000051GCFunctionInfo::~GCFunctionInfo() {}
Gordon Henriksen613afce2007-09-27 22:18:46 +000052
53// -----------------------------------------------------------------------------
54
Gordon Henriksend930f912008-08-17 18:44:35 +000055char GCModuleInfo::ID = 0;
Gordon Henriksen613afce2007-09-27 22:18:46 +000056
Philip Reames36319532015-01-16 23:16:12 +000057GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000058 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
59}
Gordon Henriksen613afce2007-09-27 22:18:46 +000060
Gordon Henriksend930f912008-08-17 18:44:35 +000061GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
Gordon Henriksene431adb2008-08-17 16:18:50 +000062 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
Gordon Henriksend930f912008-08-17 18:44:35 +000063 assert(F.hasGC());
Philip Reames36319532015-01-16 23:16:12 +000064
Gordon Henriksend930f912008-08-17 18:44:35 +000065 finfo_map_type::iterator I = FInfoMap.find(&F);
66 if (I != FInfoMap.end())
Gordon Henriksen7843c162007-12-11 00:30:17 +000067 return *I->second;
Philip Reames36319532015-01-16 23:16:12 +000068
Philip Reames56a03932015-01-26 18:26:35 +000069 GCStrategy *S = getGCStrategy(F.getGC());
Philip Reames1e308972014-12-11 01:47:23 +000070 Functions.push_back(make_unique<GCFunctionInfo>(F, *S));
71 GCFunctionInfo *GFI = Functions.back().get();
Gordon Henriksend930f912008-08-17 18:44:35 +000072 FInfoMap[&F] = GFI;
73 return *GFI;
Gordon Henriksen613afce2007-09-27 22:18:46 +000074}
75
Gordon Henriksend930f912008-08-17 18:44:35 +000076void GCModuleInfo::clear() {
Philip Reames1e308972014-12-11 01:47:23 +000077 Functions.clear();
Gordon Henriksend930f912008-08-17 18:44:35 +000078 FInfoMap.clear();
Philip Reames56a03932015-01-26 18:26:35 +000079 GCStrategyList.clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +000080}
81
82// -----------------------------------------------------------------------------
83
84char Printer::ID = 0;
85
Chris Lattner565449d2009-08-23 03:13:20 +000086FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
Gordon Henriksen613afce2007-09-27 22:18:46 +000087 return new Printer(OS);
88}
89
Gordon Henriksen613afce2007-09-27 22:18:46 +000090const char *Printer::getPassName() const {
91 return "Print Garbage Collector Information";
92}
93
94void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen7843c162007-12-11 00:30:17 +000095 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen613afce2007-09-27 22:18:46 +000096 AU.setPreservesAll();
Gordon Henriksend930f912008-08-17 18:44:35 +000097 AU.addRequired<GCModuleInfo>();
Gordon Henriksen613afce2007-09-27 22:18:46 +000098}
99
100static const char *DescKind(GC::PointKind Kind) {
101 switch (Kind) {
Philip Reames36319532015-01-16 23:16:12 +0000102 case GC::Loop:
103 return "loop";
104 case GC::Return:
105 return "return";
106 case GC::PreCall:
107 return "pre-call";
108 case GC::PostCall:
109 return "post-call";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000110 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000111 llvm_unreachable("Invalid point kind");
Gordon Henriksen613afce2007-09-27 22:18:46 +0000112}
113
Gordon Henriksen7843c162007-12-11 00:30:17 +0000114bool Printer::runOnFunction(Function &F) {
Philip Reames36319532015-01-16 23:16:12 +0000115 if (F.hasGC())
116 return false;
117
Chris Lattner1065f492010-03-14 07:27:07 +0000118 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
Philip Reames36319532015-01-16 23:16:12 +0000119
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000120 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
Chris Lattner1065f492010-03-14 07:27:07 +0000121 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000122 RE = FD->roots_end();
123 RI != RE; ++RI)
Chris Lattner1065f492010-03-14 07:27:07 +0000124 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
Philip Reames36319532015-01-16 23:16:12 +0000125
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000126 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
Philip Reames36319532015-01-16 23:16:12 +0000127 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
128 ++PI) {
129
130 OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind)
131 << ", live = {";
132
Chris Lattner1065f492010-03-14 07:27:07 +0000133 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
Philip Reames36319532015-01-16 23:16:12 +0000134 RE = FD->live_end(PI);
135 ;) {
Chris Lattner1065f492010-03-14 07:27:07 +0000136 OS << " " << RI->Num;
137 if (++RI == RE)
138 break;
139 OS << ",";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000140 }
Philip Reames36319532015-01-16 23:16:12 +0000141
Chris Lattner1065f492010-03-14 07:27:07 +0000142 OS << " }\n";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000143 }
Philip Reames36319532015-01-16 23:16:12 +0000144
Gordon Henriksen613afce2007-09-27 22:18:46 +0000145 return false;
146}
147
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000148bool Printer::doFinalization(Module &M) {
Duncan Sands5a913d62009-01-28 13:14:17 +0000149 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000150 assert(GMI && "Printer didn't require GCModuleInfo?!");
Gordon Henriksend930f912008-08-17 18:44:35 +0000151 GMI->clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000152 return false;
153}
Philip Reames56a03932015-01-26 18:26:35 +0000154
155
156GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
157 // TODO: Arguably, just doing a linear search would be faster for small N
158 auto NMI = GCStrategyMap.find(Name);
159 if (NMI != GCStrategyMap.end())
160 return NMI->getValue();
161
162 for (auto& Entry : GCRegistry::entries()) {
163 if (Name == Entry.getName()) {
164 std::unique_ptr<GCStrategy> S = Entry.instantiate();
165 S->Name = Name;
166 GCStrategyMap[Name] = S.get();
167 GCStrategyList.push_back(std::move(S));
168 return GCStrategyList.back().get();
169 }
170 }
171
172 report_fatal_error(std::string("unsupported GC: ") + Name);
173}