2009-06-02 13:52:33 -04:00
|
|
|
//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
|
|
|
|
|
//
|
2019-08-20 16:50:12 -04: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
|
2009-06-02 13:52:33 -04:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This file implements the GCFunctionInfo class and GCModuleInfo pass.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
2021-07-29 16:15:26 -04:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-06-02 13:52:33 -04:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-04-08 14:41:23 -04:00
|
|
|
#include "llvm/IR/Function.h"
|
2024-07-27 19:34:35 -04:00
|
|
|
#include "llvm/IR/Module.h"
|
2020-01-17 15:45:01 -05:00
|
|
|
#include "llvm/InitializePasses.h"
|
2010-03-16 12:51:38 -04:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2013-04-08 14:41:23 -04:00
|
|
|
#include "llvm/Pass.h"
|
2009-10-14 13:57:32 -04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-10 09:44:06 -04:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2009-06-02 13:52:33 -04:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
2023-12-18 15:30:12 -05:00
|
|
|
bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
|
|
|
|
|
ModuleAnalysisManager::Invalidator &) {
|
|
|
|
|
for (const auto &F : M) {
|
|
|
|
|
if (F.isDeclaration() || !F.hasGC())
|
|
|
|
|
continue;
|
|
|
|
|
if (!StrategyMap.contains(F.getGC()))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-11-24 04:08:18 -05:00
|
|
|
|
2023-12-18 15:30:12 -05:00
|
|
|
AnalysisKey CollectorMetadataAnalysis::Key;
|
|
|
|
|
|
|
|
|
|
CollectorMetadataAnalysis::Result
|
|
|
|
|
CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
|
|
|
|
|
Result R;
|
|
|
|
|
auto &Map = R.StrategyMap;
|
|
|
|
|
for (auto &F : M) {
|
|
|
|
|
if (F.isDeclaration() || !F.hasGC())
|
|
|
|
|
continue;
|
|
|
|
|
if (auto GCName = F.getGC(); !Map.contains(GCName))
|
|
|
|
|
Map[GCName] = getGCStrategy(GCName);
|
|
|
|
|
}
|
|
|
|
|
return R;
|
|
|
|
|
}
|
2013-04-08 14:41:23 -04:00
|
|
|
|
2023-12-18 15:30:12 -05:00
|
|
|
AnalysisKey GCFunctionAnalysis::Key;
|
2017-06-10 09:44:06 -04:00
|
|
|
|
2023-12-18 15:30:12 -05:00
|
|
|
GCFunctionAnalysis::Result
|
|
|
|
|
GCFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
|
|
|
|
|
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
|
|
|
|
|
assert(F.hasGC() && "Function doesn't have GC!");
|
|
|
|
|
|
|
|
|
|
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
|
|
|
|
|
assert(
|
|
|
|
|
MAMProxy.cachedResultExists<CollectorMetadataAnalysis>(*F.getParent()) &&
|
|
|
|
|
"This pass need module analysis `collector-metadata`!");
|
|
|
|
|
auto &Map =
|
|
|
|
|
MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent())
|
|
|
|
|
->StrategyMap;
|
|
|
|
|
GCFunctionInfo Info(F, *Map[F.getGC()]);
|
|
|
|
|
return Info;
|
|
|
|
|
}
|
2009-06-02 13:52:33 -04:00
|
|
|
|
2010-09-17 11:48:55 -04:00
|
|
|
INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
|
2011-02-20 07:57:14 -05:00
|
|
|
"Create Garbage Collector Module Metadata", false, false)
|
2009-06-02 13:52:33 -04:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
|
2015-05-27 14:44:32 -04:00
|
|
|
: F(F), S(S), FrameSize(~0LL) {}
|
2009-06-02 13:52:33 -04:00
|
|
|
|
2017-06-10 09:44:06 -04:00
|
|
|
GCFunctionInfo::~GCFunctionInfo() = default;
|
2009-06-02 13:52:33 -04:00
|
|
|
|
2023-12-18 15:30:12 -05:00
|
|
|
bool GCFunctionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
|
|
|
|
|
FunctionAnalysisManager::Invalidator &) {
|
|
|
|
|
auto PAC = PA.getChecker<GCFunctionAnalysis>();
|
|
|
|
|
return !PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>();
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 13:52:33 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
char GCModuleInfo::ID = 0;
|
|
|
|
|
|
2015-05-27 14:44:32 -04:00
|
|
|
GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
|
2011-02-20 07:57:14 -05:00
|
|
|
initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
|
}
|
2009-06-02 13:52:33 -04:00
|
|
|
|
|
|
|
|
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
|
|
|
|
|
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
|
|
|
|
|
assert(F.hasGC());
|
2015-05-27 14:44:32 -04:00
|
|
|
|
2009-06-02 13:52:33 -04:00
|
|
|
finfo_map_type::iterator I = FInfoMap.find(&F);
|
|
|
|
|
if (I != FInfoMap.end())
|
|
|
|
|
return *I->second;
|
2015-05-27 14:44:32 -04:00
|
|
|
|
|
|
|
|
GCStrategy *S = getGCStrategy(F.getGC());
|
2019-10-23 13:51:42 -04:00
|
|
|
Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S));
|
2015-01-18 11:17:27 -05:00
|
|
|
GCFunctionInfo *GFI = Functions.back().get();
|
2009-06-02 13:52:33 -04:00
|
|
|
FInfoMap[&F] = GFI;
|
|
|
|
|
return *GFI;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GCModuleInfo::clear() {
|
2015-01-18 11:17:27 -05:00
|
|
|
Functions.clear();
|
2009-06-02 13:52:33 -04:00
|
|
|
FInfoMap.clear();
|
2015-05-27 14:44:32 -04:00
|
|
|
GCStrategyList.clear();
|
2009-06-02 13:52:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2015-05-27 14:44:32 -04: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-08-02 13:32:43 -04:00
|
|
|
|
2021-11-19 15:06:13 -05:00
|
|
|
std::unique_ptr<GCStrategy> S = llvm::getGCStrategy(Name);
|
|
|
|
|
S->Name = std::string(Name);
|
|
|
|
|
GCStrategyMap[Name] = S.get();
|
|
|
|
|
GCStrategyList.push_back(std::move(S));
|
|
|
|
|
return GCStrategyList.back().get();
|
2015-05-27 14:44:32 -04:00
|
|
|
}
|