blob: 9c53550eaa9d4827503c439a5b3d81553474fefc [file] [log] [blame]
Gordon Henriksend930f912008-08-17 18:44:35 +00001//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
Gordon Henriksen613afce2007-09-27 22:18:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Gordon Henriksen613afce2007-09-27 22:18:46 +00006//
7//===----------------------------------------------------------------------===//
8//
Gordon Henriksend930f912008-08-17 18:44:35 +00009// This file implements the GCFunctionInfo class and GCModuleInfo pass.
Gordon Henriksen613afce2007-09-27 22:18:46 +000010//
11//===----------------------------------------------------------------------===//
12
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000013#include "llvm/ADT/STLExtras.h"
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 Henriksen7843c162007-12-11 00:30:17 +000016#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Function.h"
Chris Lattner1065f492010-03-14 07:27:07 +000018#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Pass.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner565449d2009-08-23 03:13:20 +000021#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000022#include <algorithm>
23#include <cassert>
24#include <memory>
25#include <string>
26
Gordon Henriksen613afce2007-09-27 22:18:46 +000027using namespace llvm;
28
29namespace {
Chris Lattner565449d2009-08-23 03:13:20 +000030
Philip Reames36319532015-01-16 23:16:12 +000031class Printer : public FunctionPass {
32 static char ID;
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000033
Philip Reames36319532015-01-16 23:16:12 +000034 raw_ostream &OS;
Craig Topper4584cd52014-03-07 09:26:03 +000035
Philip Reames36319532015-01-16 23:16:12 +000036public:
37 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
Craig Topper4584cd52014-03-07 09:26:03 +000038
Mehdi Amini117296c2016-10-01 02:56:57 +000039 StringRef getPassName() const override;
Philip Reames36319532015-01-16 23:16:12 +000040 void getAnalysisUsage(AnalysisUsage &AU) const override;
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +000041
Philip Reames36319532015-01-16 23:16:12 +000042 bool runOnFunction(Function &F) override;
43 bool doFinalization(Module &M) override;
44};
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000045
46} // end anonymous namespace
Gordon Henriksen613afce2007-09-27 22:18:46 +000047
Owen Andersona57b97e2010-07-21 22:09:45 +000048INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
Owen Andersondf7a4f22010-10-07 22:25:06 +000049 "Create Garbage Collector Module Metadata", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000050
Gordon Henriksen613afce2007-09-27 22:18:46 +000051// -----------------------------------------------------------------------------
52
Gordon Henriksend930f912008-08-17 18:44:35 +000053GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
Philip Reames36319532015-01-16 23:16:12 +000054 : F(F), S(S), FrameSize(~0LL) {}
Gordon Henriksen613afce2007-09-27 22:18:46 +000055
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000056GCFunctionInfo::~GCFunctionInfo() = default;
Gordon Henriksen613afce2007-09-27 22:18:46 +000057
58// -----------------------------------------------------------------------------
59
Gordon Henriksend930f912008-08-17 18:44:35 +000060char GCModuleInfo::ID = 0;
Gordon Henriksen613afce2007-09-27 22:18:46 +000061
Philip Reames36319532015-01-16 23:16:12 +000062GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000063 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
64}
Gordon Henriksen613afce2007-09-27 22:18:46 +000065
Gordon Henriksend930f912008-08-17 18:44:35 +000066GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
Gordon Henriksene431adb2008-08-17 16:18:50 +000067 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
Gordon Henriksend930f912008-08-17 18:44:35 +000068 assert(F.hasGC());
Philip Reames36319532015-01-16 23:16:12 +000069
Gordon Henriksend930f912008-08-17 18:44:35 +000070 finfo_map_type::iterator I = FInfoMap.find(&F);
71 if (I != FInfoMap.end())
Gordon Henriksen7843c162007-12-11 00:30:17 +000072 return *I->second;
Philip Reames36319532015-01-16 23:16:12 +000073
Philip Reames56a03932015-01-26 18:26:35 +000074 GCStrategy *S = getGCStrategy(F.getGC());
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000075 Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S));
Philip Reames1e308972014-12-11 01:47:23 +000076 GCFunctionInfo *GFI = Functions.back().get();
Gordon Henriksend930f912008-08-17 18:44:35 +000077 FInfoMap[&F] = GFI;
78 return *GFI;
Gordon Henriksen613afce2007-09-27 22:18:46 +000079}
80
Gordon Henriksend930f912008-08-17 18:44:35 +000081void GCModuleInfo::clear() {
Philip Reames1e308972014-12-11 01:47:23 +000082 Functions.clear();
Gordon Henriksend930f912008-08-17 18:44:35 +000083 FInfoMap.clear();
Philip Reames56a03932015-01-26 18:26:35 +000084 GCStrategyList.clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +000085}
86
87// -----------------------------------------------------------------------------
88
89char Printer::ID = 0;
90
Chris Lattner565449d2009-08-23 03:13:20 +000091FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
Gordon Henriksen613afce2007-09-27 22:18:46 +000092 return new Printer(OS);
93}
94
Mehdi Amini117296c2016-10-01 02:56:57 +000095StringRef Printer::getPassName() const {
Gordon Henriksen613afce2007-09-27 22:18:46 +000096 return "Print Garbage Collector Information";
97}
98
99void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000100 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen613afce2007-09-27 22:18:46 +0000101 AU.setPreservesAll();
Gordon Henriksend930f912008-08-17 18:44:35 +0000102 AU.addRequired<GCModuleInfo>();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000103}
104
Gordon Henriksen7843c162007-12-11 00:30:17 +0000105bool Printer::runOnFunction(Function &F) {
Philip Reames36319532015-01-16 23:16:12 +0000106 if (F.hasGC())
107 return false;
108
Chris Lattner1065f492010-03-14 07:27:07 +0000109 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
Philip Reames36319532015-01-16 23:16:12 +0000110
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000111 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
Chris Lattner1065f492010-03-14 07:27:07 +0000112 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000113 RE = FD->roots_end();
114 RI != RE; ++RI)
Chris Lattner1065f492010-03-14 07:27:07 +0000115 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
Philip Reames36319532015-01-16 23:16:12 +0000116
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000117 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
Philip Reames36319532015-01-16 23:16:12 +0000118 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
119 ++PI) {
120
Philip Reamese44a55d2018-11-12 22:03:53 +0000121 OS << "\t" << PI->Label->getName() << ": " << "post-call"
Philip Reames36319532015-01-16 23:16:12 +0000122 << ", live = {";
123
Chris Lattner1065f492010-03-14 07:27:07 +0000124 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
Philip Reames36319532015-01-16 23:16:12 +0000125 RE = FD->live_end(PI);
126 ;) {
Chris Lattner1065f492010-03-14 07:27:07 +0000127 OS << " " << RI->Num;
128 if (++RI == RE)
129 break;
130 OS << ",";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000131 }
Philip Reames36319532015-01-16 23:16:12 +0000132
Chris Lattner1065f492010-03-14 07:27:07 +0000133 OS << " }\n";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000134 }
Philip Reames36319532015-01-16 23:16:12 +0000135
Gordon Henriksen613afce2007-09-27 22:18:46 +0000136 return false;
137}
138
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000139bool Printer::doFinalization(Module &M) {
Duncan Sands5a913d62009-01-28 13:14:17 +0000140 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000141 assert(GMI && "Printer didn't require GCModuleInfo?!");
Gordon Henriksend930f912008-08-17 18:44:35 +0000142 GMI->clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000143 return false;
144}
Philip Reames56a03932015-01-26 18:26:35 +0000145
Philip Reames56a03932015-01-26 18:26:35 +0000146GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
147 // TODO: Arguably, just doing a linear search would be faster for small N
148 auto NMI = GCStrategyMap.find(Name);
149 if (NMI != GCStrategyMap.end())
150 return NMI->getValue();
Fangrui Songf78650a2018-07-30 19:41:25 +0000151
Philip Reames56a03932015-01-26 18:26:35 +0000152 for (auto& Entry : GCRegistry::entries()) {
153 if (Name == Entry.getName()) {
154 std::unique_ptr<GCStrategy> S = Entry.instantiate();
155 S->Name = Name;
156 GCStrategyMap[Name] = S.get();
157 GCStrategyList.push_back(std::move(S));
158 return GCStrategyList.back().get();
159 }
160 }
161
Philip Reames20c24f12015-04-26 22:00:34 +0000162 if (GCRegistry::begin() == GCRegistry::end()) {
Fangrui Songf78650a2018-07-30 19:41:25 +0000163 // In normal operation, the registry should not be empty. There should
Philip Reames20c24f12015-04-26 22:00:34 +0000164 // be the builtin GCs if nothing else. The most likely scenario here is
Fangrui Songf78650a2018-07-30 19:41:25 +0000165 // that we got here without running the initializers used by the Registry
Philip Reames20c24f12015-04-26 22:00:34 +0000166 // itself and it's registration mechanism.
Fangrui Songf78650a2018-07-30 19:41:25 +0000167 const std::string error = ("unsupported GC: " + Name).str() +
Philip Reames20c24f12015-04-26 22:00:34 +0000168 " (did you remember to link and initialize the CodeGen library?)";
169 report_fatal_error(error);
170 } else
171 report_fatal_error(std::string("unsupported GC: ") + Name);
Philip Reames56a03932015-01-26 18:26:35 +0000172}