36#define DEBUG_TYPE "internalize"
38STATISTIC(NumAliases,
"Number of aliases internalized");
39STATISTIC(NumFunctions,
"Number of functions internalized");
40STATISTIC(NumGlobals,
"Number of global vars internalized");
45class PreserveAPIList {
47 PreserveAPIList(
const IPOOptions &Opts) {
48 if (!Opts.internalize_public_api_file.empty())
49 LoadFile(Opts.internalize_public_api_file);
54 bool operator()(
const GlobalValue &GV) {
56 ExternalNames, [&](GlobPattern &GP) {
return GP.match(GV.
getName()); });
63 void addGlob(StringRef Pattern) {
66 errs() <<
"WARNING: when loading pattern: '"
67 <<
toString(GlobOrErr.takeError()) <<
"' ignoring";
70 ExternalNames.emplace_back(std::move(*GlobOrErr));
75 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
78 errs() <<
"WARNING: Internalize couldn't load file '" <<
Filename
79 <<
"'! Continuing as if it's empty.\n";
82 Buf = std::move(*BufOrErr);
83 for (line_iterator
I(*Buf,
true),
E;
I !=
E; ++
I)
87 std::shared_ptr<MemoryBuffer> Buf;
91bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
107 if (
G->isExternallyInitialized())
115 if (AlwaysPreserved.count(GV.
getName()))
118 return MustPreserveGV(GV);
121bool InternalizePass::maybeInternalize(
126 if (ComdatMap.
lookup(
C).External)
136 ComdatInfo &
Info = ComdatMap.
find(
C)->second;
138 GO->setComdat(
nullptr);
149 if (shouldPreserveGV(GV))
160void InternalizePass::checkComdat(
166 ComdatInfo &
Info = ComdatMap[
C];
168 if (shouldPreserveGV(GV))
169 Info.External =
true;
180 if (!M.getComdatSymbolTable().empty()) {
182 checkComdat(
F, ComdatMap);
184 checkComdat(GV, ComdatMap);
186 checkComdat(GA, ComdatMap);
199 AlwaysPreserved.insert(V->getName());
205 AlwaysPreserved.
insert(
"llvm.used");
206 AlwaysPreserved.insert(
"llvm.compiler.used");
210 AlwaysPreserved.insert(
"llvm.global_ctors");
211 AlwaysPreserved.insert(
"llvm.global_dtors");
212 AlwaysPreserved.insert(
"llvm.global.annotations");
217 AlwaysPreserved.insert(
"__stack_chk_fail");
218 if (M.getTargetTriple().isOSAIX())
219 AlwaysPreserved.insert(
"__ssp_canary_word");
221 AlwaysPreserved.insert(
"__stack_chk_guard");
224 if (M.getTargetTriple().isNVPTX())
225 AlwaysPreserved.insert(
"__llvm_rpc_client");
228 IsWasm = M.getTargetTriple().isOSBinFormatWasm();
230 if (!maybeInternalize(
I, ComdatMap))
240 for (
auto &GV : M.globals()) {
241 if (!maybeInternalize(GV, ComdatMap))
250 for (
auto &GA : M.aliases()) {
251 if (!maybeInternalize(GA, ComdatMap))
256 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
Module.h This file contains the declarations for the Module class.
static constexpr StringLiteral Filename
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
StringSet - A set-like wrapper for the StringMap.
Represents analyses that only rely on functions' control flow.
@ NoDeduplicate
No deduplication is performed.
iterator find(const_arg_type_t< KeyT > Val)
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static LLVM_ABI Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={}, bool SlashAgnostic=false)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
bool hasLocalLinkage() const
LLVM_ABI const Comdat * getComdat() const
void setLinkage(LinkageTypes LT)
bool hasDLLExportStorageClass() const
@ DefaultVisibility
The GV is visible.
void setVisibility(VisibilityTypes V)
bool hasAvailableExternallyLinkage() const
@ InternalLinkage
Rename collisions when linking (static functions).
LLVM_ABI bool internalizeModule(Module &TheModule)
Run the internalizer on TheModule, returns true if any changes was made.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This is an optimization pass for GlobalISel generic memory operations.
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV)
Helper function to internalize functions and variables in a Module.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
const IPOOptions & getIPOOptions(const LLVMContext &C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...