blob: c01a5e1c8abdfd46be8143d56e5c0e412797cfc7 [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.
11// If the function or variable is not in the list of external names given to
12// the pass 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"
Chris Lattner99a53f62002-07-24 17:12:05 +000023#include "llvm/Transforms/IPO.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000024#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/ADT/Statistic.h"
Mehdi Aminic87d7d02016-02-10 23:24:31 +000026#include "llvm/ADT/StringSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000032#include "llvm/Support/raw_ostream.h"
Rafael Espindola3d7fc252013-10-21 17:14:55 +000033#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000034#include "llvm/Transforms/Utils/ModuleUtils.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000035#include <fstream>
36#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chandler Carruth964daaa2014-04-22 02:55:47 +000039#define DEBUG_TYPE "internalize"
40
Mehdi Amini24d34142016-04-13 05:25:08 +000041STATISTIC(NumAliases, "Number of aliases internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000042STATISTIC(NumFunctions, "Number of functions internalized");
Mehdi Amini24d34142016-04-13 05:25:08 +000043STATISTIC(NumGlobals, "Number of global vars internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000044
Dan Gohmand78c4002008-05-13 00:00:25 +000045// APIFile - A file which contains a list of symbols that should not be marked
46// external.
47static cl::opt<std::string>
Mehdi Amini24d34142016-04-13 05:25:08 +000048 APIFile("internalize-public-api-file", cl::value_desc("filename"),
49 cl::desc("A file containing list of symbol names to preserve"));
Dan Gohmand78c4002008-05-13 00:00:25 +000050
51// APIList - A list of symbols that should not be marked internal.
52static cl::list<std::string>
Mehdi Amini24d34142016-04-13 05:25:08 +000053 APIList("internalize-public-api-list", cl::value_desc("list"),
54 cl::desc("A list of symbol names to preserve"), cl::CommaSeparated);
Dan Gohmand78c4002008-05-13 00:00:25 +000055
Chris Lattnerb28b6802002-07-23 18:06:35 +000056namespace {
Mehdi Amini24d34142016-04-13 05:25:08 +000057
Mehdi Amini40787092016-04-13 04:20:32 +000058// Helper to load an API list to preserve from file and expose it as a functor
Mehdi Amini24d34142016-04-13 05:25:08 +000059// for internalization.
Mehdi Amini40787092016-04-13 04:20:32 +000060class PreserveAPIList {
61public:
62 PreserveAPIList() {
63 if (!APIFile.empty())
64 LoadFile(APIFile);
65 ExternalNames.insert(APIList.begin(), APIList.end());
66 }
67
68 bool operator()(const GlobalValue &GV) {
69 return ExternalNames.count(GV.getName());
70 }
71
72private:
73 // Contains the set of symbols loaded from file
Mehdi Amini24d34142016-04-13 05:25:08 +000074 StringSet<> ExternalNames;
Mehdi Aminic87d7d02016-02-10 23:24:31 +000075
Mehdi Amini24d34142016-04-13 05:25:08 +000076 void LoadFile(StringRef Filename) {
77 // Load the APIFile...
78 std::ifstream In(Filename.data());
79 if (!In.good()) {
80 errs() << "WARNING: Internalize couldn't load file '" << Filename
81 << "'! Continuing as if it's empty.\n";
82 return; // Just continue as if the file were empty
Mehdi Amini40787092016-04-13 04:20:32 +000083 }
Mehdi Amini24d34142016-04-13 05:25:08 +000084 while (In) {
85 std::string Symbol;
86 In >> Symbol;
87 if (!Symbol.empty())
88 ExternalNames.insert(Symbol);
89 }
90 }
Mehdi Amini40787092016-04-13 04:20:32 +000091};
Mehdi Amini40787092016-04-13 04:20:32 +000092
Mehdi Amini24d34142016-04-13 05:25:08 +000093// Internalization exposed as a pass
Mehdi Amini40787092016-04-13 04:20:32 +000094class InternalizePass : public ModulePass {
Mehdi Amini24d34142016-04-13 05:25:08 +000095 // Client supplied callback to control wheter a symbol must be preserved.
Mehdi Amini40787092016-04-13 04:20:32 +000096 std::function<bool(const GlobalValue &)> MustPreserveGV;
97
Mehdi Amini24d34142016-04-13 05:25:08 +000098public:
99 static char ID; // Pass identification, replacement for typeid
100
101 InternalizePass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
102
103 InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
104 : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
105 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
106 }
107
108 bool runOnModule(Module &M) override {
109 CallGraphWrapperPass *CGPass =
110 getAnalysisIfAvailable<CallGraphWrapperPass>();
111 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
112 return internalizeModule(M, MustPreserveGV, CG);
113 }
114
115 void getAnalysisUsage(AnalysisUsage &AU) const override {
116 AU.setPreservesCFG();
117 AU.addPreserved<CallGraphWrapperPass>();
118 }
119};
Mehdi Amini24d34142016-04-13 05:25:08 +0000120
121// Helper class to perform internalization.
122class Internalizer {
123 // Client supplied callback to control wheter a symbol must be preserved.
124 const std::function<bool(const GlobalValue &)> &MustPreserveGV;
125
Mehdi Amini40787092016-04-13 04:20:32 +0000126 // Set of symbols private to the compiler that this pass should not touch.
127 StringSet<> AlwaysPreserved;
128
129 // Return false if we're allowed to internalize this GV.
130 bool ShouldPreserveGV(const GlobalValue &GV) {
131 // Function must be defined here
132 if (GV.isDeclaration())
133 return true;
134
135 // Available externally is really just a "declaration with a body".
136 if (GV.hasAvailableExternallyLinkage())
137 return true;
138
139 // Assume that dllexported symbols are referenced elsewhere
140 if (GV.hasDLLExportStorageClass())
141 return true;
142
143 // Already local, has nothing to do.
144 if (GV.hasLocalLinkage())
145 return false;
146
147 // Check some special cases
148 if (AlwaysPreserved.count(GV.getName()))
149 return true;
150
151 return MustPreserveGV(GV);
152 }
153
Mehdi Amini24d34142016-04-13 05:25:08 +0000154 bool maybeInternalize(GlobalValue &GV,
155 const std::set<const Comdat *> &ExternalComdats);
156 void checkComdatVisibility(GlobalValue &GV,
157 std::set<const Comdat *> &ExternalComdats);
Mehdi Amini40787092016-04-13 04:20:32 +0000158
Mehdi Amini24d34142016-04-13 05:25:08 +0000159public:
160 Internalizer(const std::function<bool(const GlobalValue &)> &MustPreserveGV)
161 : MustPreserveGV(MustPreserveGV) {}
Nuno Lopes5093ab42008-09-30 22:04:30 +0000162
Mehdi Amini24d34142016-04-13 05:25:08 +0000163 /// Run the internalizer on \p TheModule, returns true if any changes was
164 /// made.
165 ///
166 /// If the CallGraph \p CG is supplied, it will be updated when
167 /// internalizing a function (by removing any edge from the "external node")
168 bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
169};
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000170
171// Internalize GV if it is possible to do so, i.e. it is not externally visible
172// and is not a member of an externally visible comdat.
Mehdi Amini24d34142016-04-13 05:25:08 +0000173bool Internalizer::maybeInternalize(
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000174 GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) {
175 if (Comdat *C = GV.getComdat()) {
176 if (ExternalComdats.count(C))
177 return false;
178
179 // If a comdat is not externally visible we can drop it.
180 if (auto GO = dyn_cast<GlobalObject>(&GV))
181 GO->setComdat(nullptr);
182
183 if (GV.hasLocalLinkage())
184 return false;
185 } else {
186 if (GV.hasLocalLinkage())
187 return false;
188
Mehdi Amini40787092016-04-13 04:20:32 +0000189 if (ShouldPreserveGV(GV))
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000190 return false;
191 }
192
193 GV.setVisibility(GlobalValue::DefaultVisibility);
194 GV.setLinkage(GlobalValue::InternalLinkage);
Rafael Espindola282a4702013-10-31 20:51:58 +0000195 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000196}
197
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000198// If GV is part of a comdat and is externally visible, keep track of its
199// comdat so that we don't internalize any of its members.
Mehdi Amini24d34142016-04-13 05:25:08 +0000200void Internalizer::checkComdatVisibility(
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000201 GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
202 Comdat *C = GV.getComdat();
203 if (!C)
204 return;
205
Mehdi Amini40787092016-04-13 04:20:32 +0000206 if (ShouldPreserveGV(GV))
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000207 ExternalComdats.insert(C);
208}
209
Mehdi Amini24d34142016-04-13 05:25:08 +0000210bool Internalizer::internalizeModule(Module &M, CallGraph *CG) {
Mehdi Amini59269a82016-04-13 05:25:16 +0000211 bool Changed = false;
Craig Topperf40110f2014-04-25 05:29:35 +0000212 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000213
Rafael Espindola17600e22013-07-25 03:23:25 +0000214 SmallPtrSet<GlobalValue *, 8> Used;
215 collectUsedGlobalVariables(M, Used, false);
216
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000217 // Collect comdat visiblity information for the module.
218 std::set<const Comdat *> ExternalComdats;
219 if (!M.getComdatSymbolTable().empty()) {
220 for (Function &F : M)
221 checkComdatVisibility(F, ExternalComdats);
222 for (GlobalVariable &GV : M.globals())
223 checkComdatVisibility(GV, ExternalComdats);
224 for (GlobalAlias &GA : M.aliases())
225 checkComdatVisibility(GA, ExternalComdats);
226 }
227
Rafael Espindola17600e22013-07-25 03:23:25 +0000228 // We must assume that globals in llvm.used have a reference that not even
229 // the linker can see, so we don't internalize them.
230 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
231 // linker can drop those symbols. If this pass is running as part of LTO,
232 // one might think that it could just drop llvm.compiler.used. The problem
233 // is that even in LTO llvm doesn't see every reference. For example,
234 // we don't see references from function local inline assembly. To be
235 // conservative, we internalize symbols in llvm.compiler.used, but we
236 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
Craig Topper46276792014-08-24 23:23:06 +0000237 for (GlobalValue *V : Used) {
Mehdi Amini40787092016-04-13 04:20:32 +0000238 AlwaysPreserved.insert(V->getName());
Rafael Espindola17600e22013-07-25 03:23:25 +0000239 }
240
Devang Patelf2763e22008-05-14 20:01:01 +0000241 // Mark all functions not in the api as internal.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000242 for (Function &I : M) {
243 if (!maybeInternalize(I, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000244 continue;
Mehdi Amini59269a82016-04-13 05:25:16 +0000245 Changed = true;
Bill Wendling2865be72013-08-30 21:07:33 +0000246
Rafael Espindola49a6c152013-09-04 18:37:36 +0000247 if (ExternalNode)
248 // Remove a callgraph edge from the external node to this function.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000249 ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]);
Rafael Espindola49a6c152013-09-04 18:37:36 +0000250
Rafael Espindola49a6c152013-09-04 18:37:36 +0000251 ++NumFunctions;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000252 DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000253 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000254
Chris Lattner8cdc7732006-01-03 19:13:17 +0000255 // Never internalize the llvm.used symbol. It is used to implement
256 // attribute((used)).
Chris Lattner58f9bb22009-07-20 06:14:25 +0000257 // FIXME: Shouldn't this just filter on llvm.metadata section??
Mehdi Amini40787092016-04-13 04:20:32 +0000258 AlwaysPreserved.insert("llvm.used");
259 AlwaysPreserved.insert("llvm.compiler.used");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000260
Jim Laskeyc56315c2007-01-26 21:22:28 +0000261 // Never internalize anchors used by the machine module info, else the info
262 // won't find them. (see MachineModuleInfo.)
Mehdi Amini40787092016-04-13 04:20:32 +0000263 AlwaysPreserved.insert("llvm.global_ctors");
264 AlwaysPreserved.insert("llvm.global_dtors");
265 AlwaysPreserved.insert("llvm.global.annotations");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000266
Bill Wendling585583c2012-04-13 01:06:27 +0000267 // Never internalize symbols code-gen inserts.
Bill Wendlinge1eaecd2013-08-12 20:09:37 +0000268 // FIXME: We should probably add this (and the __stack_chk_guard) via some
269 // type of call-back in CodeGen.
Mehdi Amini40787092016-04-13 04:20:32 +0000270 AlwaysPreserved.insert("__stack_chk_fail");
271 AlwaysPreserved.insert("__stack_chk_guard");
Bill Wendling585583c2012-04-13 01:06:27 +0000272
Devang Patelf2763e22008-05-14 20:01:01 +0000273 // Mark all global variables with initializers that are not in the api as
274 // internal as well.
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000275 for (auto &GV : M.globals()) {
276 if (!maybeInternalize(GV, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000277 continue;
Mehdi Amini59269a82016-04-13 05:25:16 +0000278 Changed = true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000279
Rafael Espindola49a6c152013-09-04 18:37:36 +0000280 ++NumGlobals;
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000281 DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000282 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000283
Duncan Sands582c53d2009-01-05 21:24:45 +0000284 // Mark all aliases that are not in the api as internal as well.
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000285 for (auto &GA : M.aliases()) {
286 if (!maybeInternalize(GA, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000287 continue;
Mehdi Amini59269a82016-04-13 05:25:16 +0000288 Changed = true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000289
Rafael Espindola49a6c152013-09-04 18:37:36 +0000290 ++NumAliases;
Mehdi Amini3949b9e2016-04-13 05:25:12 +0000291 DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000292 }
Duncan Sands582c53d2009-01-05 21:24:45 +0000293
Mehdi Amini59269a82016-04-13 05:25:16 +0000294 return Changed;
Chris Lattner8cdc7732006-01-03 19:13:17 +0000295}
296
Mehdi Amini10593832016-04-13 06:32:29 +0000297} // end anonymous namespace
298
299char InternalizePass::ID = 0;
300INITIALIZE_PASS(InternalizePass, "internalize", "Internalize Global Symbols",
301 false, false)
302
Mehdi Amini24d34142016-04-13 05:25:08 +0000303/// Public API below
304
305bool llvm::internalizeModule(
306 Module &TheModule,
307 const std::function<bool(const GlobalValue &)> &MustPreserveGV,
308 CallGraph *CG) {
309 return Internalizer(MustPreserveGV).internalizeModule(TheModule, CG);
Devang Patel839d9262006-07-20 17:48:05 +0000310}
Mehdi Aminic87d7d02016-02-10 23:24:31 +0000311
Mehdi Amini24d34142016-04-13 05:25:08 +0000312ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
313
Mehdi Amini40787092016-04-13 04:20:32 +0000314ModulePass *llvm::createInternalizePass(
315 std::function<bool(const GlobalValue &)> MustPreserveGV) {
316 return new InternalizePass(std::move(MustPreserveGV));
Mehdi Aminic87d7d02016-02-10 23:24:31 +0000317}