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 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "internalize"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/Utils/ModuleUtils.h"
31 STATISTIC(NumAliases , "Number of aliases internalized");
32 STATISTIC(NumFunctions, "Number of functions internalized");
33 STATISTIC(NumGlobals , "Number of global vars internalized");
35 // APIFile - A file which contains a list of symbols that should not be marked
37 static cl::opt<std::string>
38 APIFile("internalize-public-api-file", cl::value_desc("filename"),
39 cl::desc("A file containing list of symbol names to preserve"));
41 // APIList - A list of symbols that should not be marked internal.
42 static cl::list<std::string>
43 APIList("internalize-public-api-list", cl::value_desc("list"),
44 cl::desc("A list of symbol names to preserve"),
48 class InternalizePass : public ModulePass {
49 std::set<std::string> ExternalNames;
51 static char ID; // Pass identification, replacement for typeid
52 explicit InternalizePass();
53 explicit InternalizePass(ArrayRef<const char *> exportList);
54 void LoadFile(const char *Filename);
55 virtual bool runOnModule(Module &M);
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.addPreserved<CallGraph>();
62 } // end anonymous namespace
64 char InternalizePass::ID = 0;
65 INITIALIZE_PASS(InternalizePass, "internalize",
66 "Internalize Global Symbols", false, false)
68 InternalizePass::InternalizePass()
70 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
71 if (!APIFile.empty()) // If a filename is specified, use it.
72 LoadFile(APIFile.c_str());
73 if (!APIList.empty()) // If a list is specified, use it as well.
74 ExternalNames.insert(APIList.begin(), APIList.end());
77 InternalizePass::InternalizePass(ArrayRef<const char *> exportList)
79 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
80 for(ArrayRef<const char *>::const_iterator itr = exportList.begin();
81 itr != exportList.end(); itr++) {
82 ExternalNames.insert(*itr);
86 void InternalizePass::LoadFile(const char *Filename) {
87 // Load the APIFile...
88 std::ifstream In(Filename);
90 errs() << "WARNING: Internalize couldn't load file '" << Filename
91 << "'! Continuing as if it's empty.\n";
92 return; // Just continue as if the file were empty
98 ExternalNames.insert(Symbol);
102 bool InternalizePass::runOnModule(Module &M) {
103 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
104 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
105 bool Changed = false;
107 SmallPtrSet<GlobalValue *, 8> Used;
108 collectUsedGlobalVariables(M, Used, false);
110 // We must assume that globals in llvm.used have a reference that not even
111 // the linker can see, so we don't internalize them.
112 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
113 // linker can drop those symbols. If this pass is running as part of LTO,
114 // one might think that it could just drop llvm.compiler.used. The problem
115 // is that even in LTO llvm doesn't see every reference. For example,
116 // we don't see references from function local inline assembly. To be
117 // conservative, we internalize symbols in llvm.compiler.used, but we
118 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
119 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
122 ExternalNames.insert(V->getName());
125 // Mark all functions not in the api as internal.
126 // FIXME: maybe use private linkage?
127 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
128 if (!I->isDeclaration() && // Function must be defined here
129 // Available externally is really just a "declaration with a body".
130 !I->hasAvailableExternallyLinkage() &&
131 !I->hasLocalLinkage() && // Can't already have internal linkage
132 !ExternalNames.count(I->getName())) {// Not marked to keep external?
133 I->setLinkage(GlobalValue::InternalLinkage);
136 // Remove a callgraph edge from the external node to this function.
137 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
141 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
144 // Never internalize the llvm.used symbol. It is used to implement
145 // attribute((used)).
146 // FIXME: Shouldn't this just filter on llvm.metadata section??
147 ExternalNames.insert("llvm.used");
148 ExternalNames.insert("llvm.compiler.used");
150 // Never internalize anchors used by the machine module info, else the info
151 // won't find them. (see MachineModuleInfo.)
152 ExternalNames.insert("llvm.global_ctors");
153 ExternalNames.insert("llvm.global_dtors");
154 ExternalNames.insert("llvm.global.annotations");
156 // Never internalize symbols code-gen inserts.
157 // FIXME: We should probably add this (and the __stack_chk_guard) via some
158 // type of call-back in CodeGen.
159 ExternalNames.insert("__stack_chk_fail");
160 ExternalNames.insert("__stack_chk_guard");
162 // Mark all global variables with initializers that are not in the api as
164 // FIXME: maybe use private linkage?
165 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
167 if (!I->isDeclaration() && !I->hasLocalLinkage() &&
168 // Available externally is really just a "declaration with a body".
169 !I->hasAvailableExternallyLinkage() &&
170 !ExternalNames.count(I->getName())) {
171 I->setLinkage(GlobalValue::InternalLinkage);
174 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
177 // Mark all aliases that are not in the api as internal as well.
178 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
180 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
181 // Available externally is really just a "declaration with a body".
182 !I->hasAvailableExternallyLinkage() &&
183 !ExternalNames.count(I->getName())) {
184 I->setLinkage(GlobalValue::InternalLinkage);
187 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
193 ModulePass *llvm::createInternalizePass() {
194 return new InternalizePass();
197 ModulePass *llvm::createInternalizePass(ArrayRef<const char *> el) {
198 return new InternalizePass(el);