37#define DEBUG_TYPE "internalize"
39STATISTIC(NumAliases,
"Number of aliases internalized");
40STATISTIC(NumFunctions,
"Number of functions internalized");
41STATISTIC(NumGlobals,
"Number of global vars internalized");
47 cl::desc(
"A file containing list of symbol names to preserve"));
57class PreserveAPIList {
78 errs() <<
"WARNING: when loading pattern: '"
79 <<
toString(GlobOrErr.takeError()) <<
"' ignoring";
90 errs() <<
"WARNING: Internalize couldn't load file '" <<
Filename
91 <<
"'! Continuing as if it's empty.\n";
94 Buf = std::move(*BufOrErr);
99 std::shared_ptr<MemoryBuffer> Buf;
103bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
118 if (
const auto *
G = dyn_cast<GlobalVariable>(&GV))
119 if (
G->isExternallyInitialized())
130 return MustPreserveGV(GV);
133bool InternalizePass::maybeInternalize(
139 if (ComdatMap.
lookup(
C).External)
142 if (
auto *GO = dyn_cast<GlobalObject>(&GV)) {
149 ComdatInfo &
Info = ComdatMap.
find(
C)->second;
151 GO->setComdat(
nullptr);
162 if (shouldPreserveGV(GV))
173void InternalizePass::checkComdat(
181 if (shouldPreserveGV(GV))
182 Info.External =
true;
186 bool Changed =
false;
193 if (!M.getComdatSymbolTable().empty()) {
195 checkComdat(
F, ComdatMap);
197 checkComdat(GV, ComdatMap);
199 checkComdat(GA, ComdatMap);
212 AlwaysPreserved.
insert(V->getName());
218 AlwaysPreserved.
insert(
"llvm.used");
219 AlwaysPreserved.
insert(
"llvm.compiler.used");
223 AlwaysPreserved.
insert(
"llvm.global_ctors");
224 AlwaysPreserved.
insert(
"llvm.global_dtors");
225 AlwaysPreserved.
insert(
"llvm.global.annotations");
230 AlwaysPreserved.
insert(
"__stack_chk_fail");
232 AlwaysPreserved.
insert(
"__ssp_canary_word");
234 AlwaysPreserved.
insert(
"__stack_chk_guard");
239 if (!maybeInternalize(
I, ComdatMap))
249 for (
auto &GV : M.globals()) {
250 if (!maybeInternalize(GV, ComdatMap))
259 for (
auto &GA : M.aliases()) {
260 if (!maybeInternalize(GA, ComdatMap))
265 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
Analysis containing CSE Info
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
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"))
Module.h This file contains the declarations for the Module class.
This file defines the SmallString class.
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.
A container for analyses that lazily runs them and caches their results.
@ 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 > try_emplace(KeyT &&Key, Ts &&... Args)
Represents either an error or a value T.
This class implements a glob pattern matcher similar to the one found in bash, but with some key diff...
bool match(StringRef S) const
static Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
bool hasLocalLinkage() const
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).
bool internalizeModule(Module &TheModule)
Run the internalizer on TheModule, returns true if any changes was made.
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.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
StringRef - Represent a constant reference to a string, i.e.
std::pair< typename Base::iterator, bool > insert(StringRef key)
Triple - Helper class for working with autoconf configuration names.
bool isOSBinFormatWasm() const
Tests whether the OS uses the Wasm binary format.
bool isOSAIX() const
Tests whether the OS is AIX.
StringRef getName() const
Return a constant reference to the value's name.
A forward iterator which reads text lines from a buffer.
@ C
The default llvm calling convention, compatible with C.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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 ...