38 #define DEBUG_TYPE "internalize" 40 STATISTIC(NumAliases,
"Number of aliases internalized");
41 STATISTIC(NumFunctions,
"Number of functions internalized");
42 STATISTIC(NumGlobals,
"Number of global vars internalized");
48 cl::desc(
"A file containing list of symbol names to preserve"));
58 class PreserveAPIList {
67 return ExternalNames.count(GV.
getName());
76 std::ifstream
In(Filename.
data());
78 errs() <<
"WARNING: Internalize couldn't load file '" << Filename
79 <<
"'! Continuing as if it's empty.\n";
86 ExternalNames.
insert(Symbol);
92 bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
110 if (AlwaysPreserved.count(GV.
getName()))
113 return MustPreserveGV(GV);
116 bool InternalizePass::maybeInternalize(
117 GlobalValue &GV,
const std::set<const Comdat *> &ExternalComdats) {
119 if (ExternalComdats.count(
C))
123 if (
auto GO = dyn_cast<GlobalObject>(&GV))
124 GO->setComdat(
nullptr);
132 if (shouldPreserveGV(GV))
143 void InternalizePass::checkComdatVisibility(
144 GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
149 if (shouldPreserveGV(GV))
150 ExternalComdats.insert(C);
154 bool Changed =
false;
161 std::set<const Comdat *> ExternalComdats;
164 checkComdatVisibility(
F, ExternalComdats);
166 checkComdatVisibility(GV, ExternalComdats);
168 checkComdatVisibility(GA, ExternalComdats);
181 AlwaysPreserved.insert(V->getName());
186 if (!maybeInternalize(
I, ExternalComdats))
195 LLVM_DEBUG(
dbgs() <<
"Internalizing func " << I.getName() <<
"\n");
201 AlwaysPreserved.insert(
"llvm.used");
202 AlwaysPreserved.insert(
"llvm.compiler.used");
206 AlwaysPreserved.insert(
"llvm.global_ctors");
207 AlwaysPreserved.insert(
"llvm.global_dtors");
208 AlwaysPreserved.insert(
"llvm.global.annotations");
213 AlwaysPreserved.insert(
"__stack_chk_fail");
214 AlwaysPreserved.insert(
"__stack_chk_guard");
218 for (
auto &GV : M.globals()) {
219 if (!maybeInternalize(GV, ExternalComdats))
228 for (
auto &GA : M.aliases()) {
229 if (!maybeInternalize(GA, ExternalComdats))
234 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
252 class InternalizeLegacyPass :
public ModulePass {
254 std::function<bool(const GlobalValue &)> MustPreserveGV;
259 InternalizeLegacyPass() :
ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
262 :
ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
266 bool runOnModule(
Module &M)
override {
271 getAnalysisIfAvailable<CallGraphWrapperPass>();
285 "Internalize Global Symbols",
false,
false)
288 return new InternalizeLegacyPass();
293 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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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...
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.