2008-08-18 02:44:35 +08:00
|
|
|
//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
|
2007-09-28 06:18:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-28 06:18:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-18 02:44:35 +08:00
|
|
|
// This file implements the GCFunctionInfo class and GCModuleInfo pass.
|
2007-09-28 06:18:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-17 20:56:54 +08:00
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
|
|
|
#include "llvm/CodeGen/GCStrategy.h"
|
2007-09-28 06:18:46 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-11 08:30:17 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2010-03-14 15:27:07 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2010-01-05 05:35:15 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 11:13:20 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-09-28 06:18:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-10-25 14:33:48 +08:00
|
|
|
class Printer : public FunctionPass {
|
2007-09-28 06:18:46 +08:00
|
|
|
static char ID;
|
2009-08-23 11:13:20 +08:00
|
|
|
raw_ostream &OS;
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
public:
|
2010-08-07 02:33:48 +08:00
|
|
|
explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
|
2009-08-23 11:13:20 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
|
|
|
|
const char *getPassName() const override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
bool doFinalization(Module &M) override;
|
2007-09-28 06:18:46 +08:00
|
|
|
};
|
2013-02-20 00:51:44 +08:00
|
|
|
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Create Garbage Collector Module Metadata", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2007-09-28 06:18:46 +08:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
|
|
|
|
: F(F), S(S), FrameSize(~0LL) {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCFunctionInfo::~GCFunctionInfo() {}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
char GCModuleInfo::ID = 0;
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCModuleInfo::GCModuleInfo()
|
2010-10-20 01:21:58 +08:00
|
|
|
: ImmutablePass(ID) {
|
|
|
|
initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCStrategy *GCModuleInfo::getOrCreateStrategy(const Module *M,
|
|
|
|
const std::string &Name) {
|
2009-07-24 02:17:34 +08:00
|
|
|
strategy_map_type::iterator NMI = StrategyMap.find(Name);
|
2008-08-18 02:44:35 +08:00
|
|
|
if (NMI != StrategyMap.end())
|
2007-12-11 08:30:17 +08:00
|
|
|
return NMI->getValue();
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
for (GCRegistry::iterator I = GCRegistry::begin(),
|
|
|
|
E = GCRegistry::end(); I != E; ++I) {
|
2009-07-24 02:17:34 +08:00
|
|
|
if (Name == I->getName()) {
|
2014-04-15 13:53:26 +08:00
|
|
|
std::unique_ptr<GCStrategy> S = I->instantiate();
|
2008-08-18 02:44:35 +08:00
|
|
|
S->Name = Name;
|
2014-11-19 13:49:42 +08:00
|
|
|
StrategyMap[Name] = S.get();
|
2014-04-15 13:34:49 +08:00
|
|
|
StrategyList.push_back(std::move(S));
|
|
|
|
return StrategyList.back().get();
|
2007-12-11 08:30:17 +08:00
|
|
|
}
|
|
|
|
}
|
2009-07-11 21:10:19 +08:00
|
|
|
|
2010-01-05 05:35:15 +08:00
|
|
|
dbgs() << "unsupported GC: " << Name << "\n";
|
2014-04-14 08:51:57 +08:00
|
|
|
llvm_unreachable(nullptr);
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
|
2008-08-18 00:18:50 +08:00
|
|
|
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
|
2008-08-18 02:44:35 +08:00
|
|
|
assert(F.hasGC());
|
2008-08-18 00:18:50 +08:00
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
finfo_map_type::iterator I = FInfoMap.find(&F);
|
|
|
|
if (I != FInfoMap.end())
|
2007-12-11 08:30:17 +08:00
|
|
|
return *I->second;
|
2008-08-18 02:44:35 +08:00
|
|
|
|
|
|
|
GCStrategy *S = getOrCreateStrategy(F.getParent(), F.getGC());
|
2014-12-11 09:47:23 +08:00
|
|
|
Functions.push_back(make_unique<GCFunctionInfo>(F, *S));
|
|
|
|
GCFunctionInfo *GFI = Functions.back().get();
|
2008-08-18 02:44:35 +08:00
|
|
|
FInfoMap[&F] = GFI;
|
|
|
|
return *GFI;
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
void GCModuleInfo::clear() {
|
2014-12-11 09:47:23 +08:00
|
|
|
Functions.clear();
|
2008-08-18 02:44:35 +08:00
|
|
|
FInfoMap.clear();
|
|
|
|
StrategyMap.clear();
|
|
|
|
StrategyList.clear();
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char Printer::ID = 0;
|
|
|
|
|
2009-08-23 11:13:20 +08:00
|
|
|
FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
|
2007-09-28 06:18:46 +08:00
|
|
|
return new Printer(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *Printer::getPassName() const {
|
|
|
|
return "Print Garbage Collector Information";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-12-11 08:30:17 +08:00
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
2007-09-28 06:18:46 +08:00
|
|
|
AU.setPreservesAll();
|
2008-08-18 02:44:35 +08:00
|
|
|
AU.addRequired<GCModuleInfo>();
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *DescKind(GC::PointKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case GC::Loop: return "loop";
|
|
|
|
case GC::Return: return "return";
|
|
|
|
case GC::PreCall: return "pre-call";
|
|
|
|
case GC::PostCall: return "post-call";
|
|
|
|
}
|
2012-01-11 02:08:01 +08:00
|
|
|
llvm_unreachable("Invalid point kind");
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2007-12-11 08:30:17 +08:00
|
|
|
bool Printer::runOnFunction(Function &F) {
|
2010-03-14 15:27:07 +08:00
|
|
|
if (F.hasGC()) return false;
|
|
|
|
|
|
|
|
GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
|
|
|
|
|
2011-11-16 00:27:03 +08:00
|
|
|
OS << "GC roots for " << FD->getFunction().getName() << ":\n";
|
2010-03-14 15:27:07 +08:00
|
|
|
for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
|
|
|
|
RE = FD->roots_end(); RI != RE; ++RI)
|
|
|
|
OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
|
|
|
|
|
2011-11-16 00:27:03 +08:00
|
|
|
OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
|
2010-03-14 15:27:07 +08:00
|
|
|
for (GCFunctionInfo::iterator PI = FD->begin(),
|
|
|
|
PE = FD->end(); PI != PE; ++PI) {
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2010-03-14 15:27:07 +08:00
|
|
|
OS << "\t" << PI->Label->getName() << ": "
|
|
|
|
<< DescKind(PI->Kind) << ", live = {";
|
2007-09-28 06:18:46 +08:00
|
|
|
|
2010-03-14 15:27:07 +08:00
|
|
|
for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
|
|
|
|
RE = FD->live_end(PI);;) {
|
|
|
|
OS << " " << RI->Num;
|
|
|
|
if (++RI == RE)
|
|
|
|
break;
|
|
|
|
OS << ",";
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
2010-03-14 15:27:07 +08:00
|
|
|
|
|
|
|
OS << " }\n";
|
2007-09-28 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-20 00:51:44 +08:00
|
|
|
bool Printer::doFinalization(Module &M) {
|
2009-01-28 21:14:17 +08:00
|
|
|
GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
|
2013-02-20 00:51:44 +08:00
|
|
|
assert(GMI && "Printer didn't require GCModuleInfo?!");
|
2008-08-18 02:44:35 +08:00
|
|
|
GMI->clear();
|
2007-09-28 06:18:46 +08:00
|
|
|
return false;
|
|
|
|
}
|