1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass loops over all of the functions and variables in the input module.
11 // If the function or variable is not in the list of external names given to
12 // the pass it is marked as internal.
14 // This transformation would not be legal or profitable in a regular
15 // compilation, but it gets extra information from the linker about what is safe
18 // As an example of a normally illegal transformation: Internalizing a function
19 // with external linkage. Only if we are told it is only used from within this
20 // module, it is safe to do it.
22 // On the profitability side: It is always legal to internalize a linkonce_odr
23 // whose address is not used. Doing so normally would introduce code bloat, but
24 // if we are told by the linker that the only use of this would be for a
25 // DSO symbol table, it is profitable to hide it.
27 //===----------------------------------------------------------------------===//
29 #define DEBUG_TYPE "internalize"
30 #include "llvm/Transforms/IPO.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Analysis/CallGraph.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Transforms/Utils/GlobalStatus.h"
40 #include "llvm/Transforms/Utils/ModuleUtils.h"
45 STATISTIC(NumAliases , "Number of aliases internalized");
46 STATISTIC(NumFunctions, "Number of functions internalized");
47 STATISTIC(NumGlobals , "Number of global vars internalized");
49 // APIFile - A file which contains a list of symbols that should not be marked
51 static cl::opt<std::string>
52 APIFile("internalize-public-api-file", cl::value_desc("filename"),
53 cl::desc("A file containing list of symbol names to preserve"));
55 // APIList - A list of symbols that should not be marked internal.
56 static cl::list<std::string>
57 APIList("internalize-public-api-list", cl::value_desc("list"),
58 cl::desc("A list of symbol names to preserve"),
61 static cl::list<std::string>
62 DSOList("internalize-dso-list", cl::value_desc("list"),
63 cl::desc("A list of symbol names need for a dso symbol table"),
67 class InternalizePass : public ModulePass {
68 std::set<std::string> ExternalNames;
69 std::set<std::string> DSONames;
71 static char ID; // Pass identification, replacement for typeid
72 explicit InternalizePass();
73 explicit InternalizePass(ArrayRef<const char *> ExportList,
74 ArrayRef<const char *> DSOList);
75 void LoadFile(const char *Filename);
76 virtual bool runOnModule(Module &M);
78 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 AU.addPreserved<CallGraph>();
83 } // end anonymous namespace
85 char InternalizePass::ID = 0;
86 INITIALIZE_PASS(InternalizePass, "internalize",
87 "Internalize Global Symbols", false, false)
89 InternalizePass::InternalizePass()
91 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
92 if (!APIFile.empty()) // If a filename is specified, use it.
93 LoadFile(APIFile.c_str());
94 ExternalNames.insert(APIList.begin(), APIList.end());
95 DSONames.insert(DSOList.begin(), DSOList.end());
98 InternalizePass::InternalizePass(ArrayRef<const char *> ExportList,
99 ArrayRef<const char *> DSOList)
101 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
102 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
103 itr != ExportList.end(); itr++) {
104 ExternalNames.insert(*itr);
106 for(ArrayRef<const char *>::const_iterator itr = DSOList.begin();
107 itr != DSOList.end(); itr++) {
108 DSONames.insert(*itr);
112 void InternalizePass::LoadFile(const char *Filename) {
113 // Load the APIFile...
114 std::ifstream In(Filename);
116 errs() << "WARNING: Internalize couldn't load file '" << Filename
117 << "'! Continuing as if it's empty.\n";
118 return; // Just continue as if the file were empty
124 ExternalNames.insert(Symbol);
128 static bool shouldInternalize(const GlobalValue &GV,
129 const std::set<std::string> &ExternalNames,
130 const std::set<std::string> &DSONames) {
131 // Function must be defined here
132 if (GV.isDeclaration())
135 // Available externally is really just a "declaration with a body".
136 if (GV.hasAvailableExternallyLinkage())
139 // Already has internal linkage
140 if (GV.hasLocalLinkage())
143 // Marked to keep external?
144 if (ExternalNames.count(GV.getName()))
147 // Not needed for the symbol table?
148 if (!DSONames.count(GV.getName()))
151 // Not a linkonce. Someone can depend on it being on the symbol table.
152 if (!GV.hasLinkOnceLinkage())
155 // The address is not important, we can hide it.
156 if (GV.hasUnnamedAddr())
160 if (GlobalStatus::analyzeGlobal(&GV, GS))
163 return !GS.IsCompared;
166 bool InternalizePass::runOnModule(Module &M) {
167 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
168 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
169 bool Changed = false;
171 SmallPtrSet<GlobalValue *, 8> Used;
172 collectUsedGlobalVariables(M, Used, false);
174 // We must assume that globals in llvm.used have a reference that not even
175 // the linker can see, so we don't internalize them.
176 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
177 // linker can drop those symbols. If this pass is running as part of LTO,
178 // one might think that it could just drop llvm.compiler.used. The problem
179 // is that even in LTO llvm doesn't see every reference. For example,
180 // we don't see references from function local inline assembly. To be
181 // conservative, we internalize symbols in llvm.compiler.used, but we
182 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
183 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
186 ExternalNames.insert(V->getName());
189 // Mark all functions not in the api as internal.
190 // FIXME: maybe use private linkage?
191 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
192 if (!shouldInternalize(*I, ExternalNames, DSONames))
195 I->setLinkage(GlobalValue::InternalLinkage);
198 // Remove a callgraph edge from the external node to this function.
199 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
203 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
206 // Never internalize the llvm.used symbol. It is used to implement
207 // attribute((used)).
208 // FIXME: Shouldn't this just filter on llvm.metadata section??
209 ExternalNames.insert("llvm.used");
210 ExternalNames.insert("llvm.compiler.used");
212 // Never internalize anchors used by the machine module info, else the info
213 // won't find them. (see MachineModuleInfo.)
214 ExternalNames.insert("llvm.global_ctors");
215 ExternalNames.insert("llvm.global_dtors");
216 ExternalNames.insert("llvm.global.annotations");
218 // Never internalize symbols code-gen inserts.
219 // FIXME: We should probably add this (and the __stack_chk_guard) via some
220 // type of call-back in CodeGen.
221 ExternalNames.insert("__stack_chk_fail");
222 ExternalNames.insert("__stack_chk_guard");
224 // Mark all global variables with initializers that are not in the api as
226 // FIXME: maybe use private linkage?
227 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
229 if (!shouldInternalize(*I, ExternalNames, DSONames))
232 I->setLinkage(GlobalValue::InternalLinkage);
235 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
238 // Mark all aliases that are not in the api as internal as well.
239 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
241 if (!shouldInternalize(*I, ExternalNames, DSONames))
244 I->setLinkage(GlobalValue::InternalLinkage);
247 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
253 ModulePass *llvm::createInternalizePass() {
254 return new InternalizePass();
257 ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList,
258 ArrayRef<const char *> DSOList) {
259 return new InternalizePass(ExportList, DSOList);