37 #define DEBUG_TYPE "internalize" 39 STATISTIC(NumAliases,
"Number of aliases internalized");
40 STATISTIC(NumFunctions,
"Number of functions internalized");
41 STATISTIC(NumGlobals,
"Number of global vars internalized");
47 cl::desc(
"A file containing list of symbol names to preserve"));
57 class PreserveAPIList {
66 return ExternalNames.count(GV.
getName());
75 std::ifstream
In(Filename.
data());
77 errs() <<
"WARNING: Internalize couldn't load file '" << Filename
78 <<
"'! Continuing as if it's empty.\n";
85 ExternalNames.
insert(Symbol);
91 bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
109 if (AlwaysPreserved.count(GV.
getName()))
112 return MustPreserveGV(GV);
115 bool InternalizePass::maybeInternalize(
116 GlobalValue &GV,
const std::set<const Comdat *> &ExternalComdats) {
118 if (ExternalComdats.count(
C))
122 if (
auto GO = dyn_cast<GlobalObject>(&GV))
123 GO->setComdat(
nullptr);
131 if (shouldPreserveGV(GV))
142 void InternalizePass::checkComdatVisibility(
143 GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
148 if (shouldPreserveGV(GV))
149 ExternalComdats.insert(C);
153 bool Changed =
false;
160 std::set<const Comdat *> ExternalComdats;
163 checkComdatVisibility(
F, ExternalComdats);
165 checkComdatVisibility(GV, ExternalComdats);
167 checkComdatVisibility(GA, ExternalComdats);
180 AlwaysPreserved.insert(V->getName());
185 if (!maybeInternalize(
I, ExternalComdats))
194 LLVM_DEBUG(
dbgs() <<
"Internalizing func " << I.getName() <<
"\n");
200 AlwaysPreserved.insert(
"llvm.used");
201 AlwaysPreserved.insert(
"llvm.compiler.used");
205 AlwaysPreserved.insert(
"llvm.global_ctors");
206 AlwaysPreserved.insert(
"llvm.global_dtors");
207 AlwaysPreserved.insert(
"llvm.global.annotations");
212 AlwaysPreserved.insert(
"__stack_chk_fail");
213 AlwaysPreserved.insert(
"__stack_chk_guard");
217 for (
auto &GV : M.globals()) {
218 if (!maybeInternalize(GV, ExternalComdats))
227 for (
auto &GA : M.aliases()) {
228 if (!maybeInternalize(GA, ExternalComdats))
233 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
251 class InternalizeLegacyPass :
public ModulePass {
253 std::function<bool(const GlobalValue &)> MustPreserveGV;
258 InternalizeLegacyPass() :
ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
261 :
ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
265 bool runOnModule(
Module &M)
override {
270 getAnalysisIfAvailable<CallGraphWrapperPass>();
284 "Internalize Global Symbols",
false,
false)
287 return new InternalizeLegacyPass();
292 return new InternalizeLegacyPass(std::move(MustPreserveGV));
void setVisibility(VisibilityTypes V)
bool hasDLLExportStorageClass() const
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool hasLocalLinkage() const
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 PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void initializeInternalizeLegacyPassPass(PassRegistry &)
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
bool hasAvailableExternallyLinkage() const
STATISTIC(NumFunctions, "Total number of functions")
A node in the call graph for a module.
INITIALIZE_PASS(InternalizeLegacyPass, "internalize", "Internalize Global Symbols", false, false) ModulePass *llvm
void removeOneAbstractEdgeTo(CallGraphNode *Callee)
Removes one edge associated with a null callsite from this node to the specified callee function...
const ComdatSymTabType & getComdatSymbolTable() const
Get the Module's symbol table for COMDATs (constant).
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
A set of analyses that are preserved following a run of a transformation pass.
The ModulePass which wraps up a CallGraph and the logic to build it.
Represent the analysis usage information of a pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
std::pair< typename base::iterator, bool > insert(StringRef Key)
const CallGraph & getCallGraph() const
The internal CallGraph around which the rest of this interface is wrapped.
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue *> &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
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"))
ModulePass * createInternalizePass(std::function< bool(const GlobalValue &)> MustPreserveGV)
createInternalizePass - This pass loops over all of the functions in the input module, internalizing all globals (functions and variables) it can.
Module.h This file contains the declarations for the Module class.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
void setLinkage(LinkageTypes LT)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const Comdat * getComdat() const
An analysis pass to compute the CallGraph for a Module.
The basic data container for the call graph of a Module of IR.
bool internalizeModule(Module &TheModule, CallGraph *CG=nullptr)
Run the internalizer on TheModule, returns true if any changes was made.
StringRef getName() const
Return a constant reference to the value's name.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
void preserve()
Mark an analysis as preserved.
Rename collisions when linking (static functions).
CallGraphNode * getExternalCallingNode() const
Returns the CallGraphNode which is used to represent undetermined calls into the callgraph.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringSet - A wrapper for StringMap that provides set-like functionality.
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.