blob: 7a61d61e7d02c1556770b7c18c49ba48d93d242e [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"
Gordon Henriksen613afce2007-09-27 22:18:46 +000015#include "llvm/CodeGen/MachineFrameInfo.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"
Philip Reames2b453952015-01-16 20:07:33 +000018#include "llvm/IR/GCStrategy.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 Reames2b453952015-01-16 20:07:33 +000069 GCStrategy *S = F.getGCStrategy();
70 if (!S) {
71 std::string error = std::string("unsupported GC: ") + F.getGC();
72 report_fatal_error(error);
73 }
74 // Save the fact this strategy is associated with this module. Note that
75 // these are non-owning references, the GCStrategy remains owned by the
Philip Reames36319532015-01-16 23:16:12 +000076 // Context.
Philip Reames2b453952015-01-16 20:07:33 +000077 StrategyList.push_back(S);
Philip Reames1e308972014-12-11 01:47:23 +000078 Functions.push_back(make_unique<GCFunctionInfo>(F, *S));
79 GCFunctionInfo *GFI = Functions.back().get();
Gordon Henriksend930f912008-08-17 18:44:35 +000080 FInfoMap[&F] = GFI;
81 return *GFI;
Gordon Henriksen613afce2007-09-27 22:18:46 +000082}
83
Gordon Henriksend930f912008-08-17 18:44:35 +000084void GCModuleInfo::clear() {
Philip Reames1e308972014-12-11 01:47:23 +000085 Functions.clear();
Gordon Henriksend930f912008-08-17 18:44:35 +000086 FInfoMap.clear();
Gordon Henriksend930f912008-08-17 18:44:35 +000087 StrategyList.clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +000088}
89
90// -----------------------------------------------------------------------------
91
92char Printer::ID = 0;
93
Chris Lattner565449d2009-08-23 03:13:20 +000094FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
Gordon Henriksen613afce2007-09-27 22:18:46 +000095 return new Printer(OS);
96}
97
Gordon Henriksen613afce2007-09-27 22:18:46 +000098const char *Printer::getPassName() const {
99 return "Print Garbage Collector Information";
100}
101
102void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000103 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen613afce2007-09-27 22:18:46 +0000104 AU.setPreservesAll();
Gordon Henriksend930f912008-08-17 18:44:35 +0000105 AU.addRequired<GCModuleInfo>();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000106}
107
108static const char *DescKind(GC::PointKind Kind) {
109 switch (Kind) {
Philip Reames36319532015-01-16 23:16:12 +0000110 case GC::Loop:
111 return "loop";
112 case GC::Return:
113 return "return";
114 case GC::PreCall:
115 return "pre-call";
116 case GC::PostCall:
117 return "post-call";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000118 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000119 llvm_unreachable("Invalid point kind");
Gordon Henriksen613afce2007-09-27 22:18:46 +0000120}
121
Gordon Henriksen7843c162007-12-11 00:30:17 +0000122bool Printer::runOnFunction(Function &F) {
Philip Reames36319532015-01-16 23:16:12 +0000123 if (F.hasGC())
124 return false;
125
Chris Lattner1065f492010-03-14 07:27:07 +0000126 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
Philip Reames36319532015-01-16 23:16:12 +0000127
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000128 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
Chris Lattner1065f492010-03-14 07:27:07 +0000129 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000130 RE = FD->roots_end();
131 RI != RE; ++RI)
Chris Lattner1065f492010-03-14 07:27:07 +0000132 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
Philip Reames36319532015-01-16 23:16:12 +0000133
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000134 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
Philip Reames36319532015-01-16 23:16:12 +0000135 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
136 ++PI) {
137
138 OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind)
139 << ", live = {";
140
Chris Lattner1065f492010-03-14 07:27:07 +0000141 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
Philip Reames36319532015-01-16 23:16:12 +0000142 RE = FD->live_end(PI);
143 ;) {
Chris Lattner1065f492010-03-14 07:27:07 +0000144 OS << " " << RI->Num;
145 if (++RI == RE)
146 break;
147 OS << ",";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000148 }
Philip Reames36319532015-01-16 23:16:12 +0000149
Chris Lattner1065f492010-03-14 07:27:07 +0000150 OS << " }\n";
Gordon Henriksen613afce2007-09-27 22:18:46 +0000151 }
Philip Reames36319532015-01-16 23:16:12 +0000152
Gordon Henriksen613afce2007-09-27 22:18:46 +0000153 return false;
154}
155
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000156bool Printer::doFinalization(Module &M) {
Duncan Sands5a913d62009-01-28 13:14:17 +0000157 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
Benjamin Kramerb3aa2b82013-02-19 16:51:44 +0000158 assert(GMI && "Printer didn't require GCModuleInfo?!");
Gordon Henriksend930f912008-08-17 18:44:35 +0000159 GMI->clear();
Gordon Henriksen613afce2007-09-27 22:18:46 +0000160 return false;
161}