blob: 4bf5335f9d71f54e66a813ea671b163dff0ae6f7 [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::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
Philip Reames56a03932015-01-26 18:26:35 +0000155GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
156 // TODO: Arguably, just doing a linear search would be faster for small N
157 auto NMI = GCStrategyMap.find(Name);
158 if (NMI != GCStrategyMap.end())
159 return NMI->getValue();
Fangrui Songf78650a2018-07-30 19:41:25 +0000160
Philip Reames56a03932015-01-26 18:26:35 +0000161 for (auto& Entry : GCRegistry::entries()) {
162 if (Name == Entry.getName()) {
163 std::unique_ptr<GCStrategy> S = Entry.instantiate();
164 S->Name = Name;
165 GCStrategyMap[Name] = S.get();
166 GCStrategyList.push_back(std::move(S));
167 return GCStrategyList.back().get();
168 }
169 }
170
Philip Reames20c24f12015-04-26 22:00:34 +0000171 if (GCRegistry::begin() == GCRegistry::end()) {
Fangrui Songf78650a2018-07-30 19:41:25 +0000172 // In normal operation, the registry should not be empty. There should
Philip Reames20c24f12015-04-26 22:00:34 +0000173 // be the builtin GCs if nothing else. The most likely scenario here is
Fangrui Songf78650a2018-07-30 19:41:25 +0000174 // that we got here without running the initializers used by the Registry
Philip Reames20c24f12015-04-26 22:00:34 +0000175 // itself and it's registration mechanism.
Fangrui Songf78650a2018-07-30 19:41:25 +0000176 const std::string error = ("unsupported GC: " + Name).str() +
Philip Reames20c24f12015-04-26 22:00:34 +0000177 " (did you remember to link and initialize the CodeGen library?)";
178 report_fatal_error(error);
179 } else
180 report_fatal_error(std::string("unsupported GC: ") + Name);
Philip Reames56a03932015-01-26 18:26:35 +0000181}