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"));
52 cl::desc(
"A list of symbol names to preserve"),
57 std::set<std::string> ExternalNames;
60 explicit InternalizePass();
62 void LoadFile(
const char *Filename);
63 bool runOnModule(
Module &M)
override;
74 "Internalize Global Symbols",
false,
false)
87 itr != ExportList.
end(); itr++) {
88 ExternalNames.insert(*itr);
92 void InternalizePass::LoadFile(
const char *Filename) {
94 std::ifstream
In(Filename);
96 errs() <<
"WARNING: Internalize couldn't load file '" << Filename
97 <<
"'! Continuing as if it's empty.\n";
104 ExternalNames.insert(Symbol);
109 const std::set<std::string> &ExternalNames) {
127 if (ExternalNames.count(GV.
getName()))
133 bool InternalizePass::runOnModule(
Module &M) {
136 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() :
nullptr;
137 bool Changed =
false;
152 ExternalNames.insert(V->getName());
160 I->setVisibility(GlobalValue::DefaultVisibility);
161 I->setLinkage(GlobalValue::InternalLinkage);
169 DEBUG(
dbgs() <<
"Internalizing func " << I->getName() <<
"\n");
175 ExternalNames.insert(
"llvm.used");
176 ExternalNames.insert(
"llvm.compiler.used");
180 ExternalNames.insert(
"llvm.global_ctors");
181 ExternalNames.insert(
"llvm.global_dtors");
182 ExternalNames.insert(
"llvm.global.annotations");
187 ExternalNames.insert(
"__stack_chk_fail");
188 ExternalNames.insert(
"__stack_chk_guard");
197 I->setVisibility(GlobalValue::DefaultVisibility);
198 I->setLinkage(GlobalValue::InternalLinkage);
201 DEBUG(
dbgs() <<
"Internalized gvar " << I->getName() <<
"\n");
210 I->setVisibility(GlobalValue::DefaultVisibility);
211 I->setLinkage(GlobalValue::InternalLinkage);
214 DEBUG(
dbgs() <<
"Internalized alias " << I->getName() <<
"\n");
223 return new InternalizePass(ExportList);
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.
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...
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
void initializeInternalizePassPass(PassRegistry &)
bool hasAvailableExternallyLinkage() const
A node in the call graph for a module.
StringRef getName() const
Return a constant reference to the value's name.
void removeOneAbstractEdgeTo(CallGraphNode *Callee)
Removes one edge associated with a null callsite from this node to the specified callee function...
bool hasDLLExportStorageClass() const
global_iterator global_begin()
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
INITIALIZE_PASS(InternalizePass,"internalize","Internalize Global Symbols", false, false) InternalizePass
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
The ModulePass which wraps up a CallGraph and the logic to build it.
alias_iterator alias_end()
Represent the analysis usage information of a pass.
global_iterator global_end()
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"))
Module.h This file contains the declarations for the Module class.
alias_iterator alias_begin()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
The basic data container for the call graph of a Module of IR.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
ModulePass * createInternalizePass(ArrayRef< const char * > ExportList)
createInternalizePass - This pass loops over all of the functions in the input module, internalizing all globals (functions and variables) it can.
bool hasLocalLinkage() const
const CallGraph & getCallGraph() const
The internal CallGraph around which the rest of this interface is wrapped.
static bool shouldInternalize(const GlobalValue &GV, const std::set< std::string > &ExternalNames)
GlobalVariable * collectUsedGlobalVariables(Module &M, SmallPtrSetImpl< GlobalValue * > &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...