36#define DEBUG_TYPE "internalize"
38STATISTIC(NumAliases,
"Number of aliases internalized");
39STATISTIC(NumFunctions,
"Number of functions internalized");
40STATISTIC(NumGlobals,
"Number of global vars internalized");
41STATISTIC(NumIFuncs,
"Number of ifuncs internalized");
46class PreserveAPIList {
48 PreserveAPIList(
const IPOOptions &Opts) {
49 if (!Opts.internalize_public_api_file.empty())
50 LoadFile(Opts.internalize_public_api_file);
55 bool operator()(
const GlobalValue &GV) {
57 ExternalNames, [&](GlobPattern &GP) {
return GP.match(GV.
getName()); });
64 void addGlob(StringRef Pattern) {
67 errs() <<
"WARNING: when loading pattern: '"
68 <<
toString(GlobOrErr.takeError()) <<
"' ignoring";
71 ExternalNames.emplace_back(std::move(*GlobOrErr));
76 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
79 errs() <<
"WARNING: Internalize couldn't load file '" <<
Filename
80 <<
"'! Continuing as if it's empty.\n";
83 Buf = std::move(*BufOrErr);
84 for (line_iterator
I(*Buf,
true),
E;
I !=
E; ++
I)
88 std::shared_ptr<MemoryBuffer> Buf;
92bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
108 if (
G->isExternallyInitialized())
116 if (AlwaysPreserved.count(GV.
getName()))
119 return MustPreserveGV(GV);
122bool InternalizePass::maybeInternalize(
127 if (ComdatMap.
lookup(
C).External)
137 ComdatInfo &
Info = ComdatMap.
find(
C)->second;
139 GO->setComdat(
nullptr);
150 if (shouldPreserveGV(GV))
161void InternalizePass::checkComdat(
167 ComdatInfo &
Info = ComdatMap[
C];
169 if (shouldPreserveGV(GV))
170 Info.External =
true;
181 if (!M.getComdatSymbolTable().empty()) {
183 checkComdat(
F, ComdatMap);
185 checkComdat(GV, ComdatMap);
187 checkComdat(GA, ComdatMap);
200 AlwaysPreserved.insert(V->getName());
206 AlwaysPreserved.
insert(
"llvm.used");
207 AlwaysPreserved.insert(
"llvm.compiler.used");
211 AlwaysPreserved.insert(
"llvm.global_ctors");
212 AlwaysPreserved.insert(
"llvm.global_dtors");
213 AlwaysPreserved.insert(
"llvm.global.annotations");
218 AlwaysPreserved.insert(
"__stack_chk_fail");
219 if (M.getTargetTriple().isOSAIX())
220 AlwaysPreserved.insert(
"__ssp_canary_word");
222 AlwaysPreserved.insert(
"__stack_chk_guard");
225 if (M.getTargetTriple().isNVPTX())
226 AlwaysPreserved.insert(
"__llvm_rpc_client");
229 IsWasm = M.getTargetTriple().isOSBinFormatWasm();
231 if (!maybeInternalize(
I, ComdatMap))
241 for (
auto &GV : M.globals()) {
242 if (!maybeInternalize(GV, ComdatMap))
251 for (
auto &GA : M.aliases()) {
252 if (!maybeInternalize(GA, ComdatMap))
257 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
261 for (
auto &GI : M.ifuncs()) {
262 if (!maybeInternalize(GI, ComdatMap))
267 LLVM_DEBUG(
dbgs() <<
"Internalized ifunc " << GI.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 ...