2008-08-17 20:44:35 +02:00
|
|
|
//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
|
2007-09-28 00:18:46 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-09-28 00:18:46 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-17 20:44:35 +02:00
|
|
|
// This file implements the GCFunctionInfo class and GCModuleInfo pass.
|
2007-09-28 00:18:46 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-08 01:53:32 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-08-17 14:56:54 +02:00
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
2015-02-13 10:09:03 +01:00
|
|
|
#include "llvm/CodeGen/GCStrategy.h"
|
2007-12-11 01:30:17 +01:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
2010-03-14 08:27:07 +01:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Pass.h"
|
2009-07-11 15:10:19 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 05:13:20 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-08 01:53:32 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2007-09-28 00:18:46 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-08-23 05:13:20 +02:00
|
|
|
|
2015-01-17 00:16:12 +01:00
|
|
|
class Printer : public FunctionPass {
|
|
|
|
static char ID;
|
2017-06-08 01:53:32 +02:00
|
|
|
|
2015-01-17 00:16:12 +01:00
|
|
|
raw_ostream &OS;
|
2014-03-07 10:26:03 +01:00
|
|
|
|
2015-01-17 00:16:12 +01:00
|
|
|
public:
|
|
|
|
explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
|
2014-03-07 10:26:03 +01:00
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override;
|
2015-01-17 00:16:12 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2013-02-19 17:51:44 +01:00
|
|
|
|
2015-01-17 00:16:12 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
bool doFinalization(Module &M) override;
|
|
|
|
};
|
2017-06-08 01:53:32 +02:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2007-09-28 00:18:46 +02:00
|
|
|
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Create Garbage Collector Module Metadata", false, false)
|
2008-05-13 02:00:25 +02:00
|
|
|
|
2007-09-28 00:18:46 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-17 20:44:35 +02:00
|
|
|
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
|
2015-01-17 00:16:12 +01:00
|
|
|
: F(F), S(S), FrameSize(~0LL) {}
|
2007-09-28 00:18:46 +02:00
|
|
|
|
2017-06-08 01:53:32 +02:00
|
|
|
GCFunctionInfo::~GCFunctionInfo() = default;
|
2007-09-28 00:18:46 +02:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-17 20:44:35 +02:00
|
|
|
char GCModuleInfo::ID = 0;
|
2007-09-28 00:18:46 +02:00
|
|
|
|
2015-01-17 00:16:12 +01:00
|
|
|
GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-09-28 00:18:46 +02:00
|
|
|
|
2008-08-17 20:44:35 +02:00
|
|
|
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
|
2008-08-17 18:18:50 +02:00
|
|
|
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
|
2008-08-17 20:44:35 +02:00
|
|
|
assert(F.hasGC());
|
2015-01-17 00:16:12 +01:00
|
|
|
|
2008-08-17 20:44:35 +02:00
|
|
|
finfo_map_type::iterator I = FInfoMap.find(&F);
|
|
|
|
if (I != FInfoMap.end())
|
2007-12-11 01:30:17 +01:00
|
|
|
return *I->second;
|
2015-01-17 00:16:12 +01:00
|
|
|
|
2015-01-26 19:26:35 +01:00
|
|
|
GCStrategy *S = getGCStrategy(F.getGC());
|
2017-06-08 01:53:32 +02:00
|
|
|
Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S));
|
2014-12-11 02:47:23 +01:00
|
|
|
GCFunctionInfo *GFI = Functions.back().get();
|
2008-08-17 20:44:35 +02:00
|
|
|
FInfoMap[&F] = GFI;
|
|
|
|
return *GFI;
|
2007-09-28 00:18:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-17 20:44:35 +02:00
|
|
|
void GCModuleInfo::clear() {
|
2014-12-11 02:47:23 +01:00
|
|
|
Functions.clear();
|
2008-08-17 20:44:35 +02:00
|
|
|
FInfoMap.clear();
|
2015-01-26 19:26:35 +01:00
|
|
|
GCStrategyList.clear();
|
2007-09-28 00:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char Printer::ID = 0;
|
|
|
|
|
2009-08-23 05:13:20 +02:00
|
|
|
FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
|
2007-09-28 00:18:46 +02:00
|
|
|
return new Printer(OS);
|
|
|
|
}
|
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef Printer::getPassName() const {
|
2007-09-28 00:18:46 +02:00
|
|
|
return "Print Garbage Collector Information";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-12-11 01:30:17 +01:00
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
2007-09-28 00:18:46 +02:00
|
|
|
AU.setPreservesAll();
|
2008-08-17 20:44:35 +02:00
|
|
|
AU.addRequired<GCModuleInfo>();
|
2007-09-28 00:18:46 +02:00
|
|
|
}
|
|
|
|
|
2007-12-11 01:30:17 +01:00
|
|
|
bool Printer::runOnFunction(Function &F) {
|
2015-01-17 00:16:12 +01:00
|
|
|
if (F.hasGC())
|
|
|
|
return false;
|
|
|
|
|
2010-03-14 08:27:07 +01:00
|
|
|
GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
|
2015-01-17 00:16:12 +01:00
|
|
|
|
2011-11-15 17:27:03 +01:00
|
|
|
OS << "GC roots for " << FD->getFunction().getName() << ":\n";
|
2010-03-14 08:27:07 +01:00
|
|
|
for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
|
2015-01-17 00:16:12 +01:00
|
|
|
RE = FD->roots_end();
|
|
|
|
RI != RE; ++RI)
|
2010-03-14 08:27:07 +01:00
|
|
|
OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
|
2015-01-17 00:16:12 +01:00
|
|
|
|
2011-11-15 17:27:03 +01:00
|
|
|
OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
|
2015-01-17 00:16:12 +01:00
|
|
|
for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
|
|
|
|
++PI) {
|
|
|
|
|
2018-11-12 23:03:53 +01:00
|
|
|
OS << "\t" << PI->Label->getName() << ": " << "post-call"
|
2015-01-17 00:16:12 +01:00
|
|
|
<< ", live = {";
|
|
|
|
|
2010-03-14 08:27:07 +01:00
|
|
|
for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
|
2015-01-17 00:16:12 +01:00
|
|
|
RE = FD->live_end(PI);
|
|
|
|
;) {
|
2010-03-14 08:27:07 +01:00
|
|
|
OS << " " << RI->Num;
|
|
|
|
if (++RI == RE)
|
|
|
|
break;
|
|
|
|
OS << ",";
|
2007-09-28 00:18:46 +02:00
|
|
|
}
|
2015-01-17 00:16:12 +01:00
|
|
|
|
2010-03-14 08:27:07 +01:00
|
|
|
OS << " }\n";
|
2007-09-28 00:18:46 +02:00
|
|
|
}
|
2015-01-17 00:16:12 +01:00
|
|
|
|
2007-09-28 00:18:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-19 17:51:44 +01:00
|
|
|
bool Printer::doFinalization(Module &M) {
|
2009-01-28 14:14:17 +01:00
|
|
|
GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
|
2013-02-19 17:51:44 +01:00
|
|
|
assert(GMI && "Printer didn't require GCModuleInfo?!");
|
2008-08-17 20:44:35 +02:00
|
|
|
GMI->clear();
|
2007-09-28 00:18:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-26 19:26:35 +01:00
|
|
|
|
|
|
|
GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
|
|
|
|
// TODO: Arguably, just doing a linear search would be faster for small N
|
|
|
|
auto NMI = GCStrategyMap.find(Name);
|
|
|
|
if (NMI != GCStrategyMap.end())
|
|
|
|
return NMI->getValue();
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2015-01-26 19:26:35 +01:00
|
|
|
for (auto& Entry : GCRegistry::entries()) {
|
|
|
|
if (Name == Entry.getName()) {
|
|
|
|
std::unique_ptr<GCStrategy> S = Entry.instantiate();
|
|
|
|
S->Name = Name;
|
|
|
|
GCStrategyMap[Name] = S.get();
|
|
|
|
GCStrategyList.push_back(std::move(S));
|
|
|
|
return GCStrategyList.back().get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 00:00:34 +02:00
|
|
|
if (GCRegistry::begin() == GCRegistry::end()) {
|
2018-07-30 21:41:25 +02:00
|
|
|
// In normal operation, the registry should not be empty. There should
|
2015-04-27 00:00:34 +02:00
|
|
|
// be the builtin GCs if nothing else. The most likely scenario here is
|
2018-07-30 21:41:25 +02:00
|
|
|
// that we got here without running the initializers used by the Registry
|
2015-04-27 00:00:34 +02:00
|
|
|
// itself and it's registration mechanism.
|
2018-07-30 21:41:25 +02:00
|
|
|
const std::string error = ("unsupported GC: " + Name).str() +
|
2015-04-27 00:00:34 +02:00
|
|
|
" (did you remember to link and initialize the CodeGen library?)";
|
|
|
|
report_fatal_error(error);
|
|
|
|
} else
|
|
|
|
report_fatal_error(std::string("unsupported GC: ") + Name);
|
2015-01-26 19:26:35 +01:00
|
|
|
}
|