blob: 456fa799e8e1ae119438fb3514d71ae6d02db034 [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
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000014#include "llvm/ADT/STLExtras.h"
Gordon Henriksenbcef14d2008-08-17 12:56:54 +000015#include "llvm/CodeGen/GCMetadata.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000016#include "llvm/CodeGen/GCStrategy.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"
Torok Edwinccb29cd2009-07-11 13:10:19 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner565449d2009-08-23 03:13:20 +000022#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000023#include <algorithm>
24#include <cassert>
25#include <memory>
26#include <string>
27
Gordon Henriksen613afce2007-09-27 22:18:46 +000028using namespace llvm;
29
30namespace {
Chris Lattner565449d2009-08-23 03:13:20 +000031
Philip Reames36319532015-01-16 23:16:12 +000032class Printer : public FunctionPass {
33 static char ID;
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000034
Philip Reames36319532015-01-16 23:16:12 +000035 raw_ostream &OS;
Craig Topper4584cd52014-03-07 09:26:03 +000036
Philip Reames36319532015-01-16 23:16:12 +000037public:
38 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
Craig Topper4584cd52014-03-07 09:26:03 +000039
Mehdi Amini117296c2016-10-01 02:56:57 +000040 StringRef getPassName() const override;
Philip Reames36319532015-01-16 23:16:12 +000041 void getAnalysisUsage(AnalysisUsage &AU) const override;
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +000042
Philip Reames36319532015-01-16 23:16:12 +000043 bool runOnFunction(Function &F) override;
44 bool doFinalization(Module &M) override;
45};
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000046
47} // end anonymous namespace
Gordon Henriksen613afce2007-09-27 22:18:46 +000048
Owen Andersona57b97e2010-07-21 22:09:45 +000049INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
Owen Andersondf7a4f22010-10-07 22:25:06 +000050 "Create Garbage Collector Module Metadata", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000051
Gordon Henriksen613afce2007-09-27 22:18:46 +000052// -----------------------------------------------------------------------------
53
Gordon Henriksend930f912008-08-17 18:44:35 +000054GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
Philip Reames36319532015-01-16 23:16:12 +000055 : F(F), S(S), FrameSize(~0LL) {}
Gordon Henriksen613afce2007-09-27 22:18:46 +000056
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000057GCFunctionInfo::~GCFunctionInfo() = default;
Gordon Henriksen613afce2007-09-27 22:18:46 +000058
59// -----------------------------------------------------------------------------
60
Gordon Henriksend930f912008-08-17 18:44:35 +000061char GCModuleInfo::ID = 0;
Gordon Henriksen613afce2007-09-27 22:18:46 +000062
Philip Reames36319532015-01-16 23:16:12 +000063GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000064 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
65}
Gordon Henriksen613afce2007-09-27 22:18:46 +000066
Gordon Henriksend930f912008-08-17 18:44:35 +000067GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
Gordon Henriksene431adb2008-08-17 16:18:50 +000068 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
Gordon Henriksend930f912008-08-17 18:44:35 +000069 assert(F.hasGC());
Philip Reames36319532015-01-16 23:16:12 +000070
Gordon Henriksend930f912008-08-17 18:44:35 +000071 finfo_map_type::iterator I = FInfoMap.find(&F);
72 if (I != FInfoMap.end())
Gordon Henriksen7843c162007-12-11 00:30:17 +000073 return *I->second;
Philip Reames36319532015-01-16 23:16:12 +000074
Philip Reames56a03932015-01-26 18:26:35 +000075 GCStrategy *S = getGCStrategy(F.getGC());
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000076 Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S));
Philip Reames1e308972014-12-11 01:47:23 +000077 GCFunctionInfo *GFI = Functions.back().get();
Gordon Henriksend930f912008-08-17 18:44:35 +000078 FInfoMap[&F] = GFI;
79 return *GFI;
Gordon Henriksen613afce2007-09-27 22:18:46 +000080}
81
Gordon Henriksend930f912008-08-17 18:44:35 +000082void GCModuleInfo::clear() {
Philip Reames1e308972014-12-11 01:47:23 +000083 Functions.clear();
Gordon Henriksend930f912008-08-17 18:44:35 +000084 FInfoMap.clear();
Philip Reames56a03932015-01-26 18:26:35 +000085 GCStrategyList.clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +000086}
87
88// -----------------------------------------------------------------------------
89
90char Printer::ID = 0;
91
Chris Lattner565449d2009-08-23 03:13:20 +000092FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
Gordon Henriksen613afce2007-09-27 22:18:46 +000093 return new Printer(OS);
94}
95
Mehdi Amini117296c2016-10-01 02:56:57 +000096StringRef Printer::getPassName() const {
Gordon Henriksen613afce2007-09-27 22:18:46 +000097 return "Print Garbage Collector Information";
98}
99
100void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000101 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen613afce2007-09-27 22:18:46 +0000102 AU.setPreservesAll();
Gordon Henriksend930f912008-08-17 18:44:35 +0000103 AU.addRequired<GCModuleInfo>();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000104}
105
106static const char *DescKind(GC::PointKind Kind) {
107 switch (Kind) {
Philip Reames36319532015-01-16 23:16:12 +0000108 case GC::PreCall:
109 return "pre-call";
110 case GC::PostCall:
111 return "post-call";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000112 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000113 llvm_unreachable("Invalid point kind");
Gordon Henriksen613afce2007-09-27 22:18:46 +0000114}
115
Gordon Henriksen7843c162007-12-11 00:30:17 +0000116bool Printer::runOnFunction(Function &F) {
Philip Reames36319532015-01-16 23:16:12 +0000117 if (F.hasGC())
118 return false;
119
Chris Lattner1065f492010-03-14 07:27:07 +0000120 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
Philip Reames36319532015-01-16 23:16:12 +0000121
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000122 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
Chris Lattner1065f492010-03-14 07:27:07 +0000123 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000124 RE = FD->roots_end();
125 RI != RE; ++RI)
Chris Lattner1065f492010-03-14 07:27:07 +0000126 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
Philip Reames36319532015-01-16 23:16:12 +0000127
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000128 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
Philip Reames36319532015-01-16 23:16:12 +0000129 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
130 ++PI) {
131
132 OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind)
133 << ", live = {";
134
Chris Lattner1065f492010-03-14 07:27:07 +0000135 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
Philip Reames36319532015-01-16 23:16:12 +0000136 RE = FD->live_end(PI);
137 ;) {
Chris Lattner1065f492010-03-14 07:27:07 +0000138 OS << " " << RI->Num;
139 if (++RI == RE)
140 break;
141 OS << ",";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000142 }
Philip Reames36319532015-01-16 23:16:12 +0000143
Chris Lattner1065f492010-03-14 07:27:07 +0000144 OS << " }\n";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000145 }
Philip Reames36319532015-01-16 23:16:12 +0000146
Gordon Henriksen613afce2007-09-27 22:18:46 +0000147 return false;
148}
149
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000150bool Printer::doFinalization(Module &M) {
Duncan Sands5a913d62009-01-28 13:14:17 +0000151 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000152 assert(GMI && "Printer didn't require GCModuleInfo?!");
Gordon Henriksend930f912008-08-17 18:44:35 +0000153 GMI->clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000154 return false;
155}
Philip Reames56a03932015-01-26 18:26:35 +0000156
Philip Reames56a03932015-01-26 18:26:35 +0000157GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
158 // TODO: Arguably, just doing a linear search would be faster for small N
159 auto NMI = GCStrategyMap.find(Name);
160 if (NMI != GCStrategyMap.end())
161 return NMI->getValue();
162
163 for (auto& Entry : GCRegistry::entries()) {
164 if (Name == Entry.getName()) {
165 std::unique_ptr<GCStrategy> S = Entry.instantiate();
166 S->Name = Name;
167 GCStrategyMap[Name] = S.get();
168 GCStrategyList.push_back(std::move(S));
169 return GCStrategyList.back().get();
170 }
171 }
172
Philip Reames20c24f12015-04-26 22:00:34 +0000173 if (GCRegistry::begin() == GCRegistry::end()) {
174 // In normal operation, the registry should not be empty. There should
175 // be the builtin GCs if nothing else. The most likely scenario here is
176 // that we got here without running the initializers used by the Registry
177 // itself and it's registration mechanism.
178 const std::string error = ("unsupported GC: " + Name).str() +
179 " (did you remember to link and initialize the CodeGen library?)";
180 report_fatal_error(error);
181 } else
182 report_fatal_error(std::string("unsupported GC: ") + Name);
Philip Reames56a03932015-01-26 18:26:35 +0000183}