blob: c8116a453d2dc6567309e6dafece6d0d3a7aab87 [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"
Chandler Carruth71f308a2015-02-13 09:09:03 +000015#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksen613afce2007-09-27 22:18:46 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Gordon Henriksen7843c162007-12-11 00:30:17 +000017#include "llvm/CodeGen/Passes.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::PreCall:
103 return "pre-call";
104 case GC::PostCall:
105 return "post-call";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000106 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000107 llvm_unreachable("Invalid point kind");
Gordon Henriksen613afce2007-09-27 22:18:46 +0000108}
109
Gordon Henriksen7843c162007-12-11 00:30:17 +0000110bool Printer::runOnFunction(Function &F) {
Philip Reames36319532015-01-16 23:16:12 +0000111 if (F.hasGC())
112 return false;
113
Chris Lattner1065f492010-03-14 07:27:07 +0000114 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
Philip Reames36319532015-01-16 23:16:12 +0000115
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000116 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
Chris Lattner1065f492010-03-14 07:27:07 +0000117 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000118 RE = FD->roots_end();
119 RI != RE; ++RI)
Chris Lattner1065f492010-03-14 07:27:07 +0000120 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
Philip Reames36319532015-01-16 23:16:12 +0000121
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000122 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
Philip Reames36319532015-01-16 23:16:12 +0000123 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
124 ++PI) {
125
126 OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind)
127 << ", live = {";
128
Chris Lattner1065f492010-03-14 07:27:07 +0000129 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
Philip Reames36319532015-01-16 23:16:12 +0000130 RE = FD->live_end(PI);
131 ;) {
Chris Lattner1065f492010-03-14 07:27:07 +0000132 OS << " " << RI->Num;
133 if (++RI == RE)
134 break;
135 OS << ",";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000136 }
Philip Reames36319532015-01-16 23:16:12 +0000137
Chris Lattner1065f492010-03-14 07:27:07 +0000138 OS << " }\n";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000139 }
Philip Reames36319532015-01-16 23:16:12 +0000140
Gordon Henriksen613afce2007-09-27 22:18:46 +0000141 return false;
142}
143
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000144bool Printer::doFinalization(Module &M) {
Duncan Sands5a913d62009-01-28 13:14:17 +0000145 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000146 assert(GMI && "Printer didn't require GCModuleInfo?!");
Gordon Henriksend930f912008-08-17 18:44:35 +0000147 GMI->clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000148 return false;
149}
Philip Reames56a03932015-01-26 18:26:35 +0000150
Philip Reames56a03932015-01-26 18:26:35 +0000151GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
152 // TODO: Arguably, just doing a linear search would be faster for small N
153 auto NMI = GCStrategyMap.find(Name);
154 if (NMI != GCStrategyMap.end())
155 return NMI->getValue();
156
157 for (auto& Entry : GCRegistry::entries()) {
158 if (Name == Entry.getName()) {
159 std::unique_ptr<GCStrategy> S = Entry.instantiate();
160 S->Name = Name;
161 GCStrategyMap[Name] = S.get();
162 GCStrategyList.push_back(std::move(S));
163 return GCStrategyList.back().get();
164 }
165 }
166
Philip Reames20c24f12015-04-26 22:00:34 +0000167 if (GCRegistry::begin() == GCRegistry::end()) {
168 // In normal operation, the registry should not be empty. There should
169 // be the builtin GCs if nothing else. The most likely scenario here is
170 // that we got here without running the initializers used by the Registry
171 // itself and it's registration mechanism.
172 const std::string error = ("unsupported GC: " + Name).str() +
173 " (did you remember to link and initialize the CodeGen library?)";
174 report_fatal_error(error);
175 } else
176 report_fatal_error(std::string("unsupported GC: ") + Name);
Philip Reames56a03932015-01-26 18:26:35 +0000177}