36#define DEBUG_TYPE "internalize"
38STATISTIC(NumAliases,
"Number of aliases internalized");
39STATISTIC(NumFunctions,
"Number of functions internalized");
40STATISTIC(NumGlobals,
"Number of global vars internalized");
46 cl::desc(
"A file containing list of symbol names to preserve"));
56class PreserveAPIList {
65 bool operator()(
const GlobalValue &GV) {
67 ExternalNames, [&](GlobPattern &GP) {
return GP.
match(GV.
getName()); });
74 void addGlob(StringRef Pattern) {
77 errs() <<
"WARNING: when loading pattern: '"
78 <<
toString(GlobOrErr.takeError()) <<
"' ignoring";
81 ExternalNames.emplace_back(std::move(*GlobOrErr));
84 void LoadFile(StringRef Filename) {
86 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
89 errs() <<
"WARNING: Internalize couldn't load file '" <<
Filename
90 <<
"'! Continuing as if it's empty.\n";
93 Buf = std::move(*BufOrErr);
94 for (line_iterator
I(*Buf,
true),
E;
I !=
E; ++
I)
98 std::shared_ptr<MemoryBuffer> Buf;
102bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
118 if (
G->isExternallyInitialized())
126 if (AlwaysPreserved.count(GV.
getName()))
129 return MustPreserveGV(GV);
132bool InternalizePass::maybeInternalize(
137 if (ComdatMap.
lookup(
C).External)
147 ComdatInfo &
Info = ComdatMap.
find(
C)->second;
149 GO->setComdat(
nullptr);
160 if (shouldPreserveGV(GV))
171void InternalizePass::checkComdat(
177 ComdatInfo &
Info = ComdatMap[
C];
179 if (shouldPreserveGV(GV))
180 Info.External =
true;
191 if (!M.getComdatSymbolTable().empty()) {
193 checkComdat(
F, ComdatMap);
195 checkComdat(GV, ComdatMap);
197 checkComdat(GA, ComdatMap);
210 AlwaysPreserved.insert(V->getName());
216 AlwaysPreserved.
insert(
"llvm.used");
217 AlwaysPreserved.insert(
"llvm.compiler.used");
221 AlwaysPreserved.insert(
"llvm.global_ctors");
222 AlwaysPreserved.insert(
"llvm.global_dtors");
223 AlwaysPreserved.insert(
"llvm.global.annotations");
228 AlwaysPreserved.insert(
"__stack_chk_fail");
229 if (M.getTargetTriple().isOSAIX())
230 AlwaysPreserved.insert(
"__ssp_canary_word");
232 AlwaysPreserved.insert(
"__stack_chk_guard");
235 if (M.getTargetTriple().isNVPTX())
236 AlwaysPreserved.insert(
"__llvm_rpc_client");
239 IsWasm = M.getTargetTriple().isOSBinFormatWasm();
241 if (!maybeInternalize(
I, ComdatMap))
251 for (
auto &GV : M.globals()) {
252 if (!maybeInternalize(GV, ComdatMap))
261 for (
auto &GA : M.aliases()) {
262 if (!maybeInternalize(GA, ComdatMap))
267 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
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 cl::list< std::string > APIList("internalize-public-api-list", cl::value_desc("list"), cl::desc("A list of symbol names to preserve"), cl::CommaSeparated)
static cl::opt< std::string > APIFile("internalize-public-api-file", cl::value_desc("filename"), cl::desc("A file containing list of symbol names to preserve"))
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.
@ NoDeduplicate
No deduplication is performed.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
LLVM_ABI bool match(StringRef S) const
static LLVM_ABI Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})
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 InternalizePass()
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 none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
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 ...