]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | //===-- Internalize.cpp - Mark functions internal -------------------------===// |
2 | // | |
3 | // The LLVM Compiler Infrastructure | |
4 | // | |
5 | // This file is distributed under the University of Illinois Open Source | |
6 | // License. See LICENSE.TXT for details. | |
7 | // | |
8 | //===----------------------------------------------------------------------===// | |
9 | // | |
970d7e83 LB |
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. | |
223e47cc | 13 | // |
1a4d82fc JJ |
14 | // This transformation would not be legal in a regular compilation, but it gets |
15 | // extra information from the linker about what is safe. | |
16 | // | |
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. | |
19 | // | |
223e47cc LB |
20 | //===----------------------------------------------------------------------===// |
21 | ||
223e47cc | 22 | #include "llvm/Transforms/IPO.h" |
1a4d82fc | 23 | #include "llvm/ADT/SmallPtrSet.h" |
970d7e83 LB |
24 | #include "llvm/ADT/Statistic.h" |
25 | #include "llvm/Analysis/CallGraph.h" | |
26 | #include "llvm/IR/Module.h" | |
223e47cc | 27 | #include "llvm/Pass.h" |
223e47cc LB |
28 | #include "llvm/Support/CommandLine.h" |
29 | #include "llvm/Support/Debug.h" | |
30 | #include "llvm/Support/raw_ostream.h" | |
1a4d82fc JJ |
31 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
32 | #include "llvm/Transforms/Utils/ModuleUtils.h" | |
223e47cc LB |
33 | #include <fstream> |
34 | #include <set> | |
35 | using namespace llvm; | |
36 | ||
1a4d82fc JJ |
37 | #define DEBUG_TYPE "internalize" |
38 | ||
223e47cc LB |
39 | STATISTIC(NumAliases , "Number of aliases internalized"); |
40 | STATISTIC(NumFunctions, "Number of functions internalized"); | |
41 | STATISTIC(NumGlobals , "Number of global vars internalized"); | |
42 | ||
43 | // APIFile - A file which contains a list of symbols that should not be marked | |
44 | // external. | |
45 | static cl::opt<std::string> | |
46 | APIFile("internalize-public-api-file", cl::value_desc("filename"), | |
47 | cl::desc("A file containing list of symbol names to preserve")); | |
48 | ||
49 | // APIList - A list of symbols that should not be marked internal. | |
50 | static cl::list<std::string> | |
51 | APIList("internalize-public-api-list", cl::value_desc("list"), | |
52 | cl::desc("A list of symbol names to preserve"), | |
53 | cl::CommaSeparated); | |
54 | ||
55 | namespace { | |
56 | class InternalizePass : public ModulePass { | |
57 | std::set<std::string> ExternalNames; | |
223e47cc LB |
58 | public: |
59 | static char ID; // Pass identification, replacement for typeid | |
970d7e83 | 60 | explicit InternalizePass(); |
1a4d82fc | 61 | explicit InternalizePass(ArrayRef<const char *> ExportList); |
223e47cc | 62 | void LoadFile(const char *Filename); |
1a4d82fc | 63 | bool runOnModule(Module &M) override; |
223e47cc | 64 | |
1a4d82fc | 65 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
223e47cc | 66 | AU.setPreservesCFG(); |
1a4d82fc | 67 | AU.addPreserved<CallGraphWrapperPass>(); |
223e47cc LB |
68 | } |
69 | }; | |
70 | } // end anonymous namespace | |
71 | ||
72 | char InternalizePass::ID = 0; | |
73 | INITIALIZE_PASS(InternalizePass, "internalize", | |
74 | "Internalize Global Symbols", false, false) | |
75 | ||
1a4d82fc | 76 | InternalizePass::InternalizePass() : ModulePass(ID) { |
223e47cc LB |
77 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
78 | if (!APIFile.empty()) // If a filename is specified, use it. | |
79 | LoadFile(APIFile.c_str()); | |
1a4d82fc | 80 | ExternalNames.insert(APIList.begin(), APIList.end()); |
223e47cc LB |
81 | } |
82 | ||
1a4d82fc JJ |
83 | InternalizePass::InternalizePass(ArrayRef<const char *> ExportList) |
84 | : ModulePass(ID) { | |
223e47cc | 85 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
1a4d82fc JJ |
86 | for(ArrayRef<const char *>::const_iterator itr = ExportList.begin(); |
87 | itr != ExportList.end(); itr++) { | |
223e47cc LB |
88 | ExternalNames.insert(*itr); |
89 | } | |
90 | } | |
91 | ||
92 | void InternalizePass::LoadFile(const char *Filename) { | |
93 | // Load the APIFile... | |
94 | std::ifstream In(Filename); | |
95 | if (!In.good()) { | |
96 | errs() << "WARNING: Internalize couldn't load file '" << Filename | |
97 | << "'! Continuing as if it's empty.\n"; | |
98 | return; // Just continue as if the file were empty | |
99 | } | |
100 | while (In) { | |
101 | std::string Symbol; | |
102 | In >> Symbol; | |
103 | if (!Symbol.empty()) | |
104 | ExternalNames.insert(Symbol); | |
105 | } | |
106 | } | |
107 | ||
1a4d82fc JJ |
108 | static bool shouldInternalize(const GlobalValue &GV, |
109 | const std::set<std::string> &ExternalNames) { | |
110 | // Function must be defined here | |
111 | if (GV.isDeclaration()) | |
112 | return false; | |
113 | ||
114 | // Available externally is really just a "declaration with a body". | |
115 | if (GV.hasAvailableExternallyLinkage()) | |
116 | return false; | |
970d7e83 | 117 | |
1a4d82fc JJ |
118 | // Assume that dllexported symbols are referenced elsewhere |
119 | if (GV.hasDLLExportStorageClass()) | |
120 | return false; | |
121 | ||
122 | // Already has internal linkage | |
123 | if (GV.hasLocalLinkage()) | |
124 | return false; | |
125 | ||
126 | // Marked to keep external? | |
127 | if (ExternalNames.count(GV.getName())) | |
128 | return false; | |
129 | ||
130 | return true; | |
970d7e83 LB |
131 | } |
132 | ||
223e47cc | 133 | bool InternalizePass::runOnModule(Module &M) { |
1a4d82fc JJ |
134 | CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>(); |
135 | CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr; | |
136 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr; | |
223e47cc LB |
137 | bool Changed = false; |
138 | ||
1a4d82fc JJ |
139 | SmallPtrSet<GlobalValue *, 8> Used; |
140 | collectUsedGlobalVariables(M, Used, false); | |
141 | ||
142 | // We must assume that globals in llvm.used have a reference that not even | |
143 | // the linker can see, so we don't internalize them. | |
144 | // For llvm.compiler.used the situation is a bit fuzzy. The assembler and | |
145 | // linker can drop those symbols. If this pass is running as part of LTO, | |
146 | // one might think that it could just drop llvm.compiler.used. The problem | |
147 | // is that even in LTO llvm doesn't see every reference. For example, | |
148 | // we don't see references from function local inline assembly. To be | |
149 | // conservative, we internalize symbols in llvm.compiler.used, but we | |
150 | // keep llvm.compiler.used so that the symbol is not deleted by llvm. | |
151 | for (GlobalValue *V : Used) { | |
152 | ExternalNames.insert(V->getName()); | |
153 | } | |
223e47cc LB |
154 | |
155 | // Mark all functions not in the api as internal. | |
1a4d82fc JJ |
156 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
157 | if (!shouldInternalize(*I, ExternalNames)) | |
158 | continue; | |
159 | ||
160 | I->setVisibility(GlobalValue::DefaultVisibility); | |
161 | I->setLinkage(GlobalValue::InternalLinkage); | |
162 | ||
163 | if (ExternalNode) | |
223e47cc | 164 | // Remove a callgraph edge from the external node to this function. |
1a4d82fc JJ |
165 | ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); |
166 | ||
167 | Changed = true; | |
168 | ++NumFunctions; | |
169 | DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n"); | |
170 | } | |
223e47cc LB |
171 | |
172 | // Never internalize the llvm.used symbol. It is used to implement | |
173 | // attribute((used)). | |
174 | // FIXME: Shouldn't this just filter on llvm.metadata section?? | |
175 | ExternalNames.insert("llvm.used"); | |
176 | ExternalNames.insert("llvm.compiler.used"); | |
177 | ||
178 | // Never internalize anchors used by the machine module info, else the info | |
179 | // won't find them. (see MachineModuleInfo.) | |
180 | ExternalNames.insert("llvm.global_ctors"); | |
181 | ExternalNames.insert("llvm.global_dtors"); | |
182 | ExternalNames.insert("llvm.global.annotations"); | |
183 | ||
184 | // Never internalize symbols code-gen inserts. | |
1a4d82fc JJ |
185 | // FIXME: We should probably add this (and the __stack_chk_guard) via some |
186 | // type of call-back in CodeGen. | |
187 | ExternalNames.insert("__stack_chk_fail"); | |
223e47cc LB |
188 | ExternalNames.insert("__stack_chk_guard"); |
189 | ||
190 | // Mark all global variables with initializers that are not in the api as | |
191 | // internal as well. | |
223e47cc | 192 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
1a4d82fc JJ |
193 | I != E; ++I) { |
194 | if (!shouldInternalize(*I, ExternalNames)) | |
195 | continue; | |
196 | ||
197 | I->setVisibility(GlobalValue::DefaultVisibility); | |
198 | I->setLinkage(GlobalValue::InternalLinkage); | |
199 | Changed = true; | |
200 | ++NumGlobals; | |
201 | DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n"); | |
202 | } | |
223e47cc LB |
203 | |
204 | // Mark all aliases that are not in the api as internal as well. | |
205 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); | |
1a4d82fc JJ |
206 | I != E; ++I) { |
207 | if (!shouldInternalize(*I, ExternalNames)) | |
208 | continue; | |
209 | ||
210 | I->setVisibility(GlobalValue::DefaultVisibility); | |
211 | I->setLinkage(GlobalValue::InternalLinkage); | |
212 | Changed = true; | |
213 | ++NumAliases; | |
214 | DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n"); | |
215 | } | |
223e47cc LB |
216 | |
217 | return Changed; | |
218 | } | |
219 | ||
1a4d82fc | 220 | ModulePass *llvm::createInternalizePass() { return new InternalizePass(); } |
223e47cc | 221 | |
1a4d82fc JJ |
222 | ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) { |
223 | return new InternalizePass(ExportList); | |
223e47cc | 224 | } |