2008-08-17 18:44:35 +00:00
|
|
|
//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
|
2007-09-27 22:18:46 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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-27 22:18:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-17 18:44:35 +00:00
|
|
|
// This file implements the GCFunctionInfo class and GCModuleInfo pass.
|
2007-09-27 22:18:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-17 12:56:54 +00:00
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
2021-02-17 23:58:43 -08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2007-12-11 00:30:17 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 13:15:01 -08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2010-03-14 07:27:07 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Pass.h"
|
2009-08-23 03:13:20 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-07 23:53:32 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2007-09-27 22:18:46 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2023-12-13 15:56:12 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalysisKey GCFunctionAnalysis::Key;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Create Garbage Collector Module Metadata", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2007-09-27 22:18:46 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
|
2015-01-16 23:16:12 +00:00
|
|
|
: F(F), S(S), FrameSize(~0LL) {}
|
2007-09-27 22:18:46 +00:00
|
|
|
|
2017-06-07 23:53:32 +00:00
|
|
|
GCFunctionInfo::~GCFunctionInfo() = default;
|
2007-09-27 22:18:46 +00:00
|
|
|
|
2023-12-13 15:56:12 +08:00
|
|
|
bool GCFunctionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
|
|
|
|
FunctionAnalysisManager::Invalidator &) {
|
|
|
|
auto PAC = PA.getChecker<GCFunctionAnalysis>();
|
|
|
|
return !PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>();
|
|
|
|
}
|
|
|
|
|
2007-09-27 22:18:46 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
char GCModuleInfo::ID = 0;
|
2007-09-27 22:18:46 +00:00
|
|
|
|
2015-01-16 23:16:12 +00:00
|
|
|
GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-09-27 22:18:46 +00:00
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
|
2008-08-17 16:18:50 +00:00
|
|
|
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
|
2008-08-17 18:44:35 +00:00
|
|
|
assert(F.hasGC());
|
2015-01-16 23:16:12 +00:00
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
finfo_map_type::iterator I = FInfoMap.find(&F);
|
|
|
|
if (I != FInfoMap.end())
|
2007-12-11 00:30:17 +00:00
|
|
|
return *I->second;
|
2015-01-16 23:16:12 +00:00
|
|
|
|
2015-01-26 18:26:35 +00:00
|
|
|
GCStrategy *S = getGCStrategy(F.getGC());
|
2019-08-15 15:54:37 +00:00
|
|
|
Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S));
|
2014-12-11 01:47:23 +00:00
|
|
|
GCFunctionInfo *GFI = Functions.back().get();
|
2008-08-17 18:44:35 +00:00
|
|
|
FInfoMap[&F] = GFI;
|
|
|
|
return *GFI;
|
2007-09-27 22:18:46 +00:00
|
|
|
}
|
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
void GCModuleInfo::clear() {
|
2014-12-11 01:47:23 +00:00
|
|
|
Functions.clear();
|
2008-08-17 18:44:35 +00:00
|
|
|
FInfoMap.clear();
|
2015-01-26 18:26:35 +00:00
|
|
|
GCStrategyList.clear();
|
2007-09-27 22:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2015-01-26 18:26:35 +00: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 19:41:25 +00:00
|
|
|
|
2021-08-02 12:00:56 +07: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-01-26 18:26:35 +00:00
|
|
|
}
|