blob: 26db1465bb26eed91e0eeff76764366908448a4f [file] [log] [blame]
Chris Lattner1b94c002002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1b94c002002-04-28 05:43:27 +00009//
Rafael Espindola4253bd82012-10-26 18:47:48 +000010// This pass loops over all of the functions and variables in the input module.
Justin Bogner4563a062016-04-26 20:15:52 +000011// If the function or variable does not need to be preserved according to the
12// client supplied callback, it is marked as internal.
Chris Lattner1b94c002002-04-28 05:43:27 +000013//
Rafael Espindola282a4702013-10-31 20:51:58 +000014// This transformation would not be legal in a regular compilation, but it gets
15// extra information from the linker about what is safe.
Rafael Espindola3d7fc252013-10-21 17:14:55 +000016//
Rafael Espindola282a4702013-10-31 20:51:58 +000017// 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.
Rafael Espindola3d7fc252013-10-21 17:14:55 +000019//
Chris Lattner1b94c002002-04-28 05:43:27 +000020//===----------------------------------------------------------------------===//
21
Mehdi Amini24d34142016-04-13 05:25:08 +000022#include "llvm/Transforms/IPO/Internalize.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000023#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
Mehdi Aminic87d7d02016-02-10 23:24:31 +000025#include "llvm/ADT/StringSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000031#include "llvm/Support/raw_ostream.h"
Justin Bogner4563a062016-04-26 20:15:52 +000032#include "llvm/Transforms/IPO.h"
Rafael Espindola3d7fc252013-10-21 17:14:55 +000033#include "llvm/Transforms/Utils/GlobalStatus.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000034#include <fstream>
35#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Chandler Carruth964daaa2014-04-22 02:55:47 +000038#define DEBUG_TYPE "internalize"
39
Mehdi Amini24d34142016-04-13 05:25:08 +000040STATISTIC(NumAliases, "Number of aliases internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000041STATISTIC(NumFunctions, "Number of functions internalized");
Mehdi Amini24d34142016-04-13 05:25:08 +000042STATISTIC(NumGlobals, "Number of global vars internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000043
Dan Gohmand78c4002008-05-13 00:00:25 +000044// APIFile - A file which contains a list of symbols that should not be marked
45// external.
46static cl::opt<std::string>
Mehdi Amini24d34142016-04-13 05:25:08 +000047 APIFile("internalize-public-api-file", cl::value_desc("filename"),
48 cl::desc("A file containing list of symbol names to preserve"));
Dan Gohmand78c4002008-05-13 00:00:25 +000049
50// APIList - A list of symbols that should not be marked internal.
51static cl::list<std::string>
Mehdi Amini24d34142016-04-13 05:25:08 +000052 APIList("internalize-public-api-list", cl::value_desc("list"),
53 cl::desc("A list of symbol names to preserve"), cl::CommaSeparated);
Dan Gohmand78c4002008-05-13 00:00:25 +000054
Chris Lattnerb28b6802002-07-23 18:06:35 +000055namespace {
Mehdi Amini40787092016-04-13 04:20:32 +000056// Helper to load an API list to preserve from file and expose it as a functor
Mehdi Amini24d34142016-04-13 05:25:08 +000057// for internalization.
Mehdi Amini40787092016-04-13 04:20:32 +000058class PreserveAPIList {
59public:
60 PreserveAPIList() {
61 if (!APIFile.empty())
62 LoadFile(APIFile);
63 ExternalNames.insert(APIList.begin(), APIList.end());
64 }
65
66 bool operator()(const GlobalValue &GV) {
67 return ExternalNames.count(GV.getName());
68 }
69
70private:
71 // Contains the set of symbols loaded from file
Mehdi Amini24d34142016-04-13 05:25:08 +000072 StringSet<> ExternalNames;
Mehdi Aminic87d7d02016-02-10 23:24:31 +000073
Mehdi Amini24d34142016-04-13 05:25:08 +000074 void LoadFile(StringRef Filename) {
75 // Load the APIFile...
76 std::ifstream In(Filename.data());
77 if (!In.good()) {
78 errs() << "WARNING: Internalize couldn't load file '" << Filename
79 << "'! Continuing as if it's empty.\n";
80 return; // Just continue as if the file were empty
Mehdi Amini40787092016-04-13 04:20:32 +000081 }
Mehdi Amini24d34142016-04-13 05:25:08 +000082 while (In) {
83 std::string Symbol;
84 In >> Symbol;
85 if (!Symbol.empty())
86 ExternalNames.insert(Symbol);
87 }
88 }
Mehdi Amini40787092016-04-13 04:20:32 +000089};
Justin Bogner4563a062016-04-26 20:15:52 +000090} // end anonymous namespace
Mehdi Amini40787092016-04-13 04:20:32 +000091
Justin Bogner4563a062016-04-26 20:15:52 +000092bool InternalizePass::shouldPreserveGV(const GlobalValue &GV) {
93 // Function must be defined here
94 if (GV.isDeclaration())
95 return true;
Mehdi Amini40787092016-04-13 04:20:32 +000096
Justin Bogner4563a062016-04-26 20:15:52 +000097 // Available externally is really just a "declaration with a body".
98 if (GV.hasAvailableExternallyLinkage())
99 return true;
Mehdi Amini24d34142016-04-13 05:25:08 +0000100
Justin Bogner4563a062016-04-26 20:15:52 +0000101 // Assume that dllexported symbols are referenced elsewhere
102 if (GV.hasDLLExportStorageClass())
103 return true;
Mehdi Amini24d34142016-04-13 05:25:08 +0000104
Justin Bogner4563a062016-04-26 20:15:52 +0000105 // Already local, has nothing to do.
106 if (GV.hasLocalLinkage())
107 return false;
Mehdi Amini24d34142016-04-13 05:25:08 +0000108
Justin Bogner4563a062016-04-26 20:15:52 +0000109 // Check some special cases
110 if (AlwaysPreserved.count(GV.getName()))
111 return true;
Andrew Kayloraa641a52016-04-22 22:06:11 +0000112
Justin Bogner4563a062016-04-26 20:15:52 +0000113 return MustPreserveGV(GV);
114}
Mehdi Amini24d34142016-04-13 05:25:08 +0000115
Justin Bogner4563a062016-04-26 20:15:52 +0000116bool InternalizePass::maybeInternalize(
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000117 GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) {
118 if (Comdat *C = GV.getComdat()) {
119 if (ExternalComdats.count(C))
120 return false;
121
122 // If a comdat is not externally visible we can drop it.
123 if (auto GO = dyn_cast<GlobalObject>(&GV))
124 GO->setComdat(nullptr);
125
126 if (GV.hasLocalLinkage())
127 return false;
128 } else {
129 if (GV.hasLocalLinkage())
130 return false;
131
Justin Bogner4563a062016-04-26 20:15:52 +0000132 if (shouldPreserveGV(GV))
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000133 return false;
134 }
135
136 GV.setVisibility(GlobalValue::DefaultVisibility);
137 GV.setLinkage(GlobalValue::InternalLinkage);
Rafael Espindola282a4702013-10-31 20:51:58 +0000138 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000139}
140
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000141// If GV is part of a comdat and is externally visible, keep track of its
142// comdat so that we don't internalize any of its members.
Justin Bogner4563a062016-04-26 20:15:52 +0000143void InternalizePass::checkComdatVisibility(
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000144 GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
145 Comdat *C = GV.getComdat();
146 if (!C)
147 return;
148
Justin Bogner4563a062016-04-26 20:15:52 +0000149 if (shouldPreserveGV(GV))
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000150 ExternalComdats.insert(C);
151}
152
Justin Bogner4563a062016-04-26 20:15:52 +0000153bool InternalizePass::internalizeModule(Module &M, CallGraph *CG) {
Mehdi Amini59269a82016-04-13 05:25:16 +0000154 bool Changed = false;
Craig Topperf40110f2014-04-25 05:29:35 +0000155 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000156
Rafael Espindola17600e22013-07-25 03:23:25 +0000157 SmallPtrSet<GlobalValue *, 8> Used;
158 collectUsedGlobalVariables(M, Used, false);
159
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000160 // Collect comdat visiblity information for the module.
161 std::set<const Comdat *> ExternalComdats;
162 if (!M.getComdatSymbolTable().empty()) {
163 for (Function &F : M)
164 checkComdatVisibility(F, ExternalComdats);
165 for (GlobalVariable &GV : M.globals())
166 checkComdatVisibility(GV, ExternalComdats);
167 for (GlobalAlias &GA : M.aliases())
168 checkComdatVisibility(GA, ExternalComdats);
169 }
170
Rafael Espindola17600e22013-07-25 03:23:25 +0000171 // We must assume that globals in llvm.used have a reference that not even
172 // the linker can see, so we don't internalize them.
173 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
174 // linker can drop those symbols. If this pass is running as part of LTO,
175 // one might think that it could just drop llvm.compiler.used. The problem
176 // is that even in LTO llvm doesn't see every reference. For example,
177 // we don't see references from function local inline assembly. To be
178 // conservative, we internalize symbols in llvm.compiler.used, but we
179 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
Craig Topper46276792014-08-24 23:23:06 +0000180 for (GlobalValue *V : Used) {
Mehdi Amini40787092016-04-13 04:20:32 +0000181 AlwaysPreserved.insert(V->getName());
Rafael Espindola17600e22013-07-25 03:23:25 +0000182 }
183
Devang Patelf2763e22008-05-14 20:01:01 +0000184 // Mark all functions not in the api as internal.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000185 for (Function &I : M) {
186 if (!maybeInternalize(I, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000187 continue;
Mehdi Amini59269a82016-04-13 05:25:16 +0000188 Changed = true;
Bill Wendling2865be72013-08-30 21:07:33 +0000189
Rafael Espindola49a6c152013-09-04 18:37:36 +0000190 if (ExternalNode)
191 // Remove a callgraph edge from the external node to this function.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000192 ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]);
Rafael Espindola49a6c152013-09-04 18:37:36 +0000193
Rafael Espindola49a6c152013-09-04 18:37:36 +0000194 ++NumFunctions;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000195 DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000196 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000197
Chris Lattner8cdc7732006-01-03 19:13:17 +0000198 // Never internalize the llvm.used symbol. It is used to implement
199 // attribute((used)).
Chris Lattner58f9bb22009-07-20 06:14:25 +0000200 // FIXME: Shouldn't this just filter on llvm.metadata section??
Mehdi Amini40787092016-04-13 04:20:32 +0000201 AlwaysPreserved.insert("llvm.used");
202 AlwaysPreserved.insert("llvm.compiler.used");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000203
Jim Laskeyc56315c2007-01-26 21:22:28 +0000204 // Never internalize anchors used by the machine module info, else the info
205 // won't find them. (see MachineModuleInfo.)
Mehdi Amini40787092016-04-13 04:20:32 +0000206 AlwaysPreserved.insert("llvm.global_ctors");
207 AlwaysPreserved.insert("llvm.global_dtors");
208 AlwaysPreserved.insert("llvm.global.annotations");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000209
Bill Wendling585583c2012-04-13 01:06:27 +0000210 // Never internalize symbols code-gen inserts.
Bill Wendlinge1eaecd2013-08-12 20:09:37 +0000211 // FIXME: We should probably add this (and the __stack_chk_guard) via some
212 // type of call-back in CodeGen.
Mehdi Amini40787092016-04-13 04:20:32 +0000213 AlwaysPreserved.insert("__stack_chk_fail");
214 AlwaysPreserved.insert("__stack_chk_guard");
Bill Wendling585583c2012-04-13 01:06:27 +0000215
Devang Patelf2763e22008-05-14 20:01:01 +0000216 // Mark all global variables with initializers that are not in the api as
217 // internal as well.
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000218 for (auto &GV : M.globals()) {
219 if (!maybeInternalize(GV, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000220 continue;
Mehdi Amini59269a82016-04-13 05:25:16 +0000221 Changed = true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000222
Rafael Espindola49a6c152013-09-04 18:37:36 +0000223 ++NumGlobals;
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000224 DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000225 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000226
Duncan Sands582c53d2009-01-05 21:24:45 +0000227 // Mark all aliases that are not in the api as internal as well.
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000228 for (auto &GA : M.aliases()) {
229 if (!maybeInternalize(GA, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000230 continue;
Mehdi Amini59269a82016-04-13 05:25:16 +0000231 Changed = true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000232
Rafael Espindola49a6c152013-09-04 18:37:36 +0000233 ++NumAliases;
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000234 DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000235 }
Duncan Sands582c53d2009-01-05 21:24:45 +0000236
Mehdi Amini59269a82016-04-13 05:25:16 +0000237 return Changed;
Chris Lattner8cdc7732006-01-03 19:13:17 +0000238}
239
Justin Bogner4563a062016-04-26 20:15:52 +0000240InternalizePass::InternalizePass() : MustPreserveGV(PreserveAPIList()) {}
Mehdi Amini10593832016-04-13 06:32:29 +0000241
Sean Silvafd03ac62016-08-09 00:28:38 +0000242PreservedAnalyses InternalizePass::run(Module &M, ModuleAnalysisManager &AM) {
Justin Bogner4563a062016-04-26 20:15:52 +0000243 if (!internalizeModule(M, AM.getCachedResult<CallGraphAnalysis>(M)))
244 return PreservedAnalyses::all();
Mehdi Amini10593832016-04-13 06:32:29 +0000245
Justin Bogner4563a062016-04-26 20:15:52 +0000246 PreservedAnalyses PA;
247 PA.preserve<CallGraphAnalysis>();
248 return PA;
Devang Patel839d9262006-07-20 17:48:05 +0000249}
Mehdi Aminic87d7d02016-02-10 23:24:31 +0000250
Justin Bogner4563a062016-04-26 20:15:52 +0000251namespace {
252class InternalizeLegacyPass : public ModulePass {
253 // Client supplied callback to control wheter a symbol must be preserved.
254 std::function<bool(const GlobalValue &)> MustPreserveGV;
255
256public:
257 static char ID; // Pass identification, replacement for typeid
258
259 InternalizeLegacyPass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
260
261 InternalizeLegacyPass(std::function<bool(const GlobalValue &)> MustPreserveGV)
262 : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
263 initializeInternalizeLegacyPassPass(*PassRegistry::getPassRegistry());
264 }
265
266 bool runOnModule(Module &M) override {
267 if (skipModule(M))
268 return false;
269
270 CallGraphWrapperPass *CGPass =
271 getAnalysisIfAvailable<CallGraphWrapperPass>();
272 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
273 return internalizeModule(M, MustPreserveGV, CG);
274 }
275
276 void getAnalysisUsage(AnalysisUsage &AU) const override {
277 AU.setPreservesCFG();
278 AU.addPreserved<CallGraphWrapperPass>();
279 }
280};
281}
282
283char InternalizeLegacyPass::ID = 0;
284INITIALIZE_PASS(InternalizeLegacyPass, "internalize",
285 "Internalize Global Symbols", false, false)
286
287ModulePass *llvm::createInternalizePass() {
288 return new InternalizeLegacyPass();
289}
Mehdi Amini24d34142016-04-13 05:25:08 +0000290
Mehdi Amini40787092016-04-13 04:20:32 +0000291ModulePass *llvm::createInternalizePass(
292 std::function<bool(const GlobalValue &)> MustPreserveGV) {
Justin Bogner4563a062016-04-26 20:15:52 +0000293 return new InternalizeLegacyPass(std::move(MustPreserveGV));
Mehdi Aminic87d7d02016-02-10 23:24:31 +0000294}