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 in a regular compilation, but it gets
15 // extra information from the linker about what is safe.
17 // For example: Internalizing a function with external linkage. Only if we are
18 // told it is only used from within this module, it is safe to do it.
20 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "internalize"
23 #include "llvm/Transforms/IPO.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Transforms/Utils/GlobalStatus.h"
33 #include "llvm/Transforms/Utils/ModuleUtils.h"
38 STATISTIC(NumAliases , "Number of aliases internalized");
39 STATISTIC(NumFunctions, "Number of functions internalized");
40 STATISTIC(NumGlobals , "Number of global vars internalized");
42 // APIFile - A file which contains a list of symbols that should not be marked
44 static cl::opt<std::string>
45 APIFile("internalize-public-api-file", cl::value_desc("filename"),
46 cl::desc("A file containing list of symbol names to preserve"));
48 // APIList - A list of symbols that should not be marked internal.
49 static cl::list<std::string>
50 APIList("internalize-public-api-list", cl::value_desc("list"),
51 cl::desc("A list of symbol names to preserve"),
55 class InternalizePass : public ModulePass {
56 std::set<std::string> ExternalNames;
59 static char ID; // Pass identification, replacement for typeid
60 explicit InternalizePass(bool OnlyHidden = false);
61 explicit InternalizePass(ArrayRef<const char *> ExportList, bool OnlyHidden);
62 void LoadFile(const char *Filename);
63 virtual bool runOnModule(Module &M);
65 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addPreserved<CallGraphWrapperPass>();
70 } // end anonymous namespace
72 char InternalizePass::ID = 0;
73 INITIALIZE_PASS(InternalizePass, "internalize",
74 "Internalize Global Symbols", false, false)
76 InternalizePass::InternalizePass(bool OnlyHidden)
77 : ModulePass(ID), OnlyHidden(OnlyHidden) {
78 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
79 if (!APIFile.empty()) // If a filename is specified, use it.
80 LoadFile(APIFile.c_str());
81 ExternalNames.insert(APIList.begin(), APIList.end());
84 InternalizePass::InternalizePass(ArrayRef<const char *> ExportList,
86 : ModulePass(ID), OnlyHidden(OnlyHidden) {
87 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
88 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
89 itr != ExportList.end(); itr++) {
90 ExternalNames.insert(*itr);
94 void InternalizePass::LoadFile(const char *Filename) {
95 // Load the APIFile...
96 std::ifstream In(Filename);
98 errs() << "WARNING: Internalize couldn't load file '" << Filename
99 << "'! Continuing as if it's empty.\n";
100 return; // Just continue as if the file were empty
106 ExternalNames.insert(Symbol);
110 static bool shouldInternalize(const GlobalValue &GV,
111 const std::set<std::string> &ExternalNames,
113 if (OnlyHidden && !GV.hasHiddenVisibility())
116 // Function must be defined here
117 if (GV.isDeclaration())
120 // Available externally is really just a "declaration with a body".
121 if (GV.hasAvailableExternallyLinkage())
124 // Assume that dllexported symbols are referenced elsewhere
125 if (GV.hasDLLExportLinkage())
128 // Already has internal linkage
129 if (GV.hasLocalLinkage())
132 // Marked to keep external?
133 if (ExternalNames.count(GV.getName()))
139 bool InternalizePass::runOnModule(Module &M) {
140 CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>();
141 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : 0;
142 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
143 bool Changed = false;
145 SmallPtrSet<GlobalValue *, 8> Used;
146 collectUsedGlobalVariables(M, Used, false);
148 // We must assume that globals in llvm.used have a reference that not even
149 // the linker can see, so we don't internalize them.
150 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
151 // linker can drop those symbols. If this pass is running as part of LTO,
152 // one might think that it could just drop llvm.compiler.used. The problem
153 // is that even in LTO llvm doesn't see every reference. For example,
154 // we don't see references from function local inline assembly. To be
155 // conservative, we internalize symbols in llvm.compiler.used, but we
156 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
157 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
160 ExternalNames.insert(V->getName());
163 // Mark all functions not in the api as internal.
164 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
165 if (!shouldInternalize(*I, ExternalNames, OnlyHidden))
168 I->setLinkage(GlobalValue::InternalLinkage);
171 // Remove a callgraph edge from the external node to this function.
172 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
176 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
179 // Never internalize the llvm.used symbol. It is used to implement
180 // attribute((used)).
181 // FIXME: Shouldn't this just filter on llvm.metadata section??
182 ExternalNames.insert("llvm.used");
183 ExternalNames.insert("llvm.compiler.used");
185 // Never internalize anchors used by the machine module info, else the info
186 // won't find them. (see MachineModuleInfo.)
187 ExternalNames.insert("llvm.global_ctors");
188 ExternalNames.insert("llvm.global_dtors");
189 ExternalNames.insert("llvm.global.annotations");
191 // Never internalize symbols code-gen inserts.
192 // FIXME: We should probably add this (and the __stack_chk_guard) via some
193 // type of call-back in CodeGen.
194 ExternalNames.insert("__stack_chk_fail");
195 ExternalNames.insert("__stack_chk_guard");
197 // Mark all global variables with initializers that are not in the api as
199 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
201 if (!shouldInternalize(*I, ExternalNames, OnlyHidden))
204 I->setLinkage(GlobalValue::InternalLinkage);
207 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
210 // Mark all aliases that are not in the api as internal as well.
211 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
213 if (!shouldInternalize(*I, ExternalNames, OnlyHidden))
216 I->setLinkage(GlobalValue::InternalLinkage);
219 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
225 ModulePass *llvm::createInternalizePass(bool OnlyHidden) {
226 return new InternalizePass(OnlyHidden);
229 ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList,
231 return new InternalizePass(ExportList, OnlyHidden);