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());
79 errs() <<
"WARNING: Internalize couldn't load file '" << Filename
80 <<
"'! Continuing as if it's empty.\n";
89 bool InternalizePass::shouldPreserveGV(
const GlobalValue &GV) {
107 if (AlwaysPreserved.count(GV.
getName()))
110 return MustPreserveGV(GV);
113 bool InternalizePass::maybeInternalize(
116 if (ExternalComdats.
count(
C))
120 if (
auto GO = dyn_cast<GlobalObject>(&GV))
121 GO->setComdat(
nullptr);
129 if (shouldPreserveGV(GV))
140 void InternalizePass::checkComdatVisibility(
146 if (shouldPreserveGV(GV))
147 ExternalComdats.
insert(C);
151 bool Changed =
false;
161 checkComdatVisibility(
F, ExternalComdats);
163 checkComdatVisibility(GV, ExternalComdats);
165 checkComdatVisibility(GA, ExternalComdats);
178 AlwaysPreserved.insert(V->getName());
183 if (!maybeInternalize(
I, ExternalComdats))
192 LLVM_DEBUG(
dbgs() <<
"Internalizing func " << I.getName() <<
"\n");
198 AlwaysPreserved.insert(
"llvm.used");
199 AlwaysPreserved.insert(
"llvm.compiler.used");
203 AlwaysPreserved.insert(
"llvm.global_ctors");
204 AlwaysPreserved.insert(
"llvm.global_dtors");
205 AlwaysPreserved.insert(
"llvm.global.annotations");
210 AlwaysPreserved.insert(
"__stack_chk_fail");
211 AlwaysPreserved.insert(
"__stack_chk_guard");
215 for (
auto &GV : M.globals()) {
216 if (!maybeInternalize(GV, ExternalComdats))
225 for (
auto &GA : M.aliases()) {
226 if (!maybeInternalize(GA, ExternalComdats))
231 LLVM_DEBUG(
dbgs() <<
"Internalized alias " << GA.getName() <<
"\n");
249 class InternalizeLegacyPass :
public ModulePass {
251 std::function<bool(const GlobalValue &)> MustPreserveGV;
256 InternalizeLegacyPass() :
ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
259 :
ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
263 bool runOnModule(
Module &M)
override {
268 getAnalysisIfAvailable<CallGraphWrapperPass>();
282 "Internalize Global Symbols",
false,
false)
285 return new InternalizeLegacyPass();
290 return new InternalizeLegacyPass(std::move(MustPreserveGV));
void setVisibility(VisibilityTypes V)
bool hasDLLExportStorageClass() const
Represents either an error or a value T.
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)
Implements a dense probed hash-table based set.
bool hasAvailableExternallyLinkage() const
A forward iterator which reads text lines from a buffer.
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(const ValueT &V)
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).
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
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...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
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.