blob: 1c80556dfef57943142f6cfe8c59b3af1649a4c8 [file] [log] [blame]
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
Gordon Henriksenfc328222007-09-27 22:18:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Henriksenfc328222007-09-27 22:18:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksen5eca0752008-08-17 18:44:35 +000010// This file implements the GCFunctionInfo class and GCModuleInfo pass.
Gordon Henriksenfc328222007-09-27 22:18:46 +000011//
12//===----------------------------------------------------------------------===//
13
Eugene Zelenko643c0a42017-06-07 23:53:32 +000014#include "llvm/ADT/STLExtras.h"
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000015#include "llvm/CodeGen/GCMetadata.h"
Chandler Carruth02d62882015-02-13 09:09:03 +000016#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000017#include "llvm/CodeGen/Passes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
Chris Lattneraba9bcb2010-03-14 07:27:07 +000019#include "llvm/MC/MCSymbol.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/Pass.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000022#include "llvm/Support/raw_ostream.h"
Eugene Zelenko643c0a42017-06-07 23:53:32 +000023#include <algorithm>
24#include <cassert>
25#include <memory>
26#include <string>
27
Gordon Henriksenfc328222007-09-27 22:18:46 +000028using namespace llvm;
29
30namespace {
Chris Lattnercf143a42009-08-23 03:13:20 +000031
Philip Reames99941272015-01-16 23:16:12 +000032class Printer : public FunctionPass {
33 static char ID;
Eugene Zelenko643c0a42017-06-07 23:53:32 +000034
Philip Reames99941272015-01-16 23:16:12 +000035 raw_ostream &OS;
Craig Topper9f998de2014-03-07 09:26:03 +000036
Philip Reames99941272015-01-16 23:16:12 +000037public:
38 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
Craig Topper9f998de2014-03-07 09:26:03 +000039
Mehdi Amini67f335d2016-10-01 02:56:57 +000040 StringRef getPassName() const override;
Philip Reames99941272015-01-16 23:16:12 +000041 void getAnalysisUsage(AnalysisUsage &AU) const override;
Benjamin Kramer06333732013-02-19 16:51:44 +000042
Philip Reames99941272015-01-16 23:16:12 +000043 bool runOnFunction(Function &F) override;
44 bool doFinalization(Module &M) override;
45};
Eugene Zelenko643c0a42017-06-07 23:53:32 +000046
47} // end anonymous namespace
Gordon Henriksenfc328222007-09-27 22:18:46 +000048
Owen Andersond13db2c2010-07-21 22:09:45 +000049INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
Owen Andersonce665bd2010-10-07 22:25:06 +000050 "Create Garbage Collector Module Metadata", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000051
Gordon Henriksenfc328222007-09-27 22:18:46 +000052// -----------------------------------------------------------------------------
53
Gordon Henriksen5eca0752008-08-17 18:44:35 +000054GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
Philip Reames99941272015-01-16 23:16:12 +000055 : F(F), S(S), FrameSize(~0LL) {}
Gordon Henriksenfc328222007-09-27 22:18:46 +000056
Eugene Zelenko643c0a42017-06-07 23:53:32 +000057GCFunctionInfo::~GCFunctionInfo() = default;
Gordon Henriksenfc328222007-09-27 22:18:46 +000058
59// -----------------------------------------------------------------------------
60
Gordon Henriksen5eca0752008-08-17 18:44:35 +000061char GCModuleInfo::ID = 0;
Gordon Henriksenfc328222007-09-27 22:18:46 +000062
Philip Reames99941272015-01-16 23:16:12 +000063GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000064 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
65}
Gordon Henriksenfc328222007-09-27 22:18:46 +000066
Gordon Henriksen5eca0752008-08-17 18:44:35 +000067GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
Gordon Henriksen418b6e82008-08-17 16:18:50 +000068 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
Gordon Henriksen5eca0752008-08-17 18:44:35 +000069 assert(F.hasGC());
Philip Reames99941272015-01-16 23:16:12 +000070
Gordon Henriksen5eca0752008-08-17 18:44:35 +000071 finfo_map_type::iterator I = FInfoMap.find(&F);
72 if (I != FInfoMap.end())
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000073 return *I->second;
Philip Reames99941272015-01-16 23:16:12 +000074
Philip Reames2bdb2382015-01-26 18:26:35 +000075 GCStrategy *S = getGCStrategy(F.getGC());
Eugene Zelenko643c0a42017-06-07 23:53:32 +000076 Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S));
Philip Reames5e62b842014-12-11 01:47:23 +000077 GCFunctionInfo *GFI = Functions.back().get();
Gordon Henriksen5eca0752008-08-17 18:44:35 +000078 FInfoMap[&F] = GFI;
79 return *GFI;
Gordon Henriksenfc328222007-09-27 22:18:46 +000080}
81
Gordon Henriksen5eca0752008-08-17 18:44:35 +000082void GCModuleInfo::clear() {
Philip Reames5e62b842014-12-11 01:47:23 +000083 Functions.clear();
Gordon Henriksen5eca0752008-08-17 18:44:35 +000084 FInfoMap.clear();
Philip Reames2bdb2382015-01-26 18:26:35 +000085 GCStrategyList.clear();
Gordon Henriksenfc328222007-09-27 22:18:46 +000086}
87
88// -----------------------------------------------------------------------------
89
90char Printer::ID = 0;
91
Chris Lattnercf143a42009-08-23 03:13:20 +000092FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
Gordon Henriksenfc328222007-09-27 22:18:46 +000093 return new Printer(OS);
94}
95
Mehdi Amini67f335d2016-10-01 02:56:57 +000096StringRef Printer::getPassName() const {
Gordon Henriksenfc328222007-09-27 22:18:46 +000097 return "Print Garbage Collector Information";
98}
99
100void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000101 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksenfc328222007-09-27 22:18:46 +0000102 AU.setPreservesAll();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000103 AU.addRequired<GCModuleInfo>();
Gordon Henriksenfc328222007-09-27 22:18:46 +0000104}
105
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000106bool Printer::runOnFunction(Function &F) {
Philip Reames99941272015-01-16 23:16:12 +0000107 if (F.hasGC())
108 return false;
109
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000110 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
Philip Reames99941272015-01-16 23:16:12 +0000111
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000112 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000113 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
Philip Reames99941272015-01-16 23:16:12 +0000114 RE = FD->roots_end();
115 RI != RE; ++RI)
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000116 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
Philip Reames99941272015-01-16 23:16:12 +0000117
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000118 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
Philip Reames99941272015-01-16 23:16:12 +0000119 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
120 ++PI) {
121
Philip Reames46063052018-11-12 22:03:53 +0000122 OS << "\t" << PI->Label->getName() << ": " << "post-call"
Philip Reames99941272015-01-16 23:16:12 +0000123 << ", live = {";
124
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000125 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
Philip Reames99941272015-01-16 23:16:12 +0000126 RE = FD->live_end(PI);
127 ;) {
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000128 OS << " " << RI->Num;
129 if (++RI == RE)
130 break;
131 OS << ",";
Gordon Henriksenfc328222007-09-27 22:18:46 +0000132 }
Philip Reames99941272015-01-16 23:16:12 +0000133
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000134 OS << " }\n";
Gordon Henriksenfc328222007-09-27 22:18:46 +0000135 }
Philip Reames99941272015-01-16 23:16:12 +0000136
Gordon Henriksenfc328222007-09-27 22:18:46 +0000137 return false;
138}
139
Benjamin Kramer06333732013-02-19 16:51:44 +0000140bool Printer::doFinalization(Module &M) {
Duncan Sands1465d612009-01-28 13:14:17 +0000141 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
Benjamin Kramer06333732013-02-19 16:51:44 +0000142 assert(GMI && "Printer didn't require GCModuleInfo?!");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000143 GMI->clear();
Gordon Henriksenfc328222007-09-27 22:18:46 +0000144 return false;
145}
Philip Reames2bdb2382015-01-26 18:26:35 +0000146
Philip Reames2bdb2382015-01-26 18:26:35 +0000147GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
148 // TODO: Arguably, just doing a linear search would be faster for small N
149 auto NMI = GCStrategyMap.find(Name);
150 if (NMI != GCStrategyMap.end())
151 return NMI->getValue();
Fangrui Songaf7b1832018-07-30 19:41:25 +0000152
Philip Reames2bdb2382015-01-26 18:26:35 +0000153 for (auto& Entry : GCRegistry::entries()) {
154 if (Name == Entry.getName()) {
155 std::unique_ptr<GCStrategy> S = Entry.instantiate();
156 S->Name = Name;
157 GCStrategyMap[Name] = S.get();
158 GCStrategyList.push_back(std::move(S));
159 return GCStrategyList.back().get();
160 }
161 }
162
Philip Reamesc4ab95d2015-04-26 22:00:34 +0000163 if (GCRegistry::begin() == GCRegistry::end()) {
Fangrui Songaf7b1832018-07-30 19:41:25 +0000164 // In normal operation, the registry should not be empty. There should
Philip Reamesc4ab95d2015-04-26 22:00:34 +0000165 // be the builtin GCs if nothing else. The most likely scenario here is
Fangrui Songaf7b1832018-07-30 19:41:25 +0000166 // that we got here without running the initializers used by the Registry
Philip Reamesc4ab95d2015-04-26 22:00:34 +0000167 // itself and it's registration mechanism.
Fangrui Songaf7b1832018-07-30 19:41:25 +0000168 const std::string error = ("unsupported GC: " + Name).str() +
Philip Reamesc4ab95d2015-04-26 22:00:34 +0000169 " (did you remember to link and initialize the CodeGen library?)";
170 report_fatal_error(error);
171 } else
172 report_fatal_error(std::string("unsupported GC: ") + Name);
Philip Reames2bdb2382015-01-26 18:26:35 +0000173}