blob: 21bb5d000bc766c40817f697fae9ce54b1718165 [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
Chris Lattner99a53f62002-07-24 17:12:05 +000022#include "llvm/Transforms/IPO.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"
25#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000030#include "llvm/Support/raw_ostream.h"
Rafael Espindola3d7fc252013-10-21 17:14:55 +000031#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000032#include "llvm/Transforms/Utils/ModuleUtils.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000033#include <fstream>
34#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chandler Carruth964daaa2014-04-22 02:55:47 +000037#define DEBUG_TYPE "internalize"
38
Duncan Sands582c53d2009-01-05 21:24:45 +000039STATISTIC(NumAliases , "Number of aliases internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000040STATISTIC(NumFunctions, "Number of functions internalized");
41STATISTIC(NumGlobals , "Number of global vars internalized");
42
Dan Gohmand78c4002008-05-13 00:00:25 +000043// APIFile - A file which contains a list of symbols that should not be marked
44// external.
45static cl::opt<std::string>
46APIFile("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.
50static cl::list<std::string>
51APIList("internalize-public-api-list", cl::value_desc("list"),
52 cl::desc("A list of symbol names to preserve"),
53 cl::CommaSeparated);
54
Chris Lattnerb28b6802002-07-23 18:06:35 +000055namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000056 class InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000057 std::set<std::string> ExternalNames;
58 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000059 static char ID; // Pass identification, replacement for typeid
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +000060 explicit InternalizePass();
61 explicit InternalizePass(ArrayRef<const char *> ExportList);
Chris Lattner8cdc7732006-01-03 19:13:17 +000062 void LoadFile(const char *Filename);
Peter Collingbourne9b0fe612015-07-16 17:42:21 +000063 bool maybeInternalize(GlobalValue &GV,
64 const std::set<const Comdat *> &ExternalComdats);
65 void checkComdatVisibility(GlobalValue &GV,
66 std::set<const Comdat *> &ExternalComdats);
Craig Topper3e4c6972014-03-05 09:10:37 +000067 bool runOnModule(Module &M) override;
Nuno Lopes5093ab42008-09-30 22:04:30 +000068
Craig Topper3e4c6972014-03-05 09:10:37 +000069 void getAnalysisUsage(AnalysisUsage &AU) const override {
Nuno Lopes5093ab42008-09-30 22:04:30 +000070 AU.setPreservesCFG();
Chandler Carruth6378cf52013-11-26 04:19:30 +000071 AU.addPreserved<CallGraphWrapperPass>();
Nuno Lopes5093ab42008-09-30 22:04:30 +000072 }
Chris Lattner47cc3662002-07-30 19:48:44 +000073 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000074} // end anonymous namespace
75
Dan Gohmand78c4002008-05-13 00:00:25 +000076char InternalizePass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000077INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersondf7a4f22010-10-07 22:25:06 +000078 "Internalize Global Symbols", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000079
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +000080InternalizePass::InternalizePass() : ModulePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000081 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patelf2763e22008-05-14 20:01:01 +000082 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner8cdc7732006-01-03 19:13:17 +000083 LoadFile(APIFile.c_str());
Rafael Espindolab832d492013-09-04 18:53:21 +000084 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner8cdc7732006-01-03 19:13:17 +000085}
86
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +000087InternalizePass::InternalizePass(ArrayRef<const char *> ExportList)
88 : ModulePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000089 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +000090 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
91 itr != ExportList.end(); itr++) {
Devang Patel839d9262006-07-20 17:48:05 +000092 ExternalNames.insert(*itr);
93 }
94}
95
Chris Lattner8cdc7732006-01-03 19:13:17 +000096void InternalizePass::LoadFile(const char *Filename) {
97 // Load the APIFile...
98 std::ifstream In(Filename);
99 if (!In.good()) {
Chris Lattner317dbbc2009-08-23 07:05:07 +0000100 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelf2763e22008-05-14 20:01:01 +0000101 << "'! Continuing as if it's empty.\n";
102 return; // Just continue as if the file were empty
Chris Lattner8cdc7732006-01-03 19:13:17 +0000103 }
104 while (In) {
105 std::string Symbol;
106 In >> Symbol;
107 if (!Symbol.empty())
108 ExternalNames.insert(Symbol);
109 }
110}
111
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000112static bool isExternallyVisible(const GlobalValue &GV,
113 const std::set<std::string> &ExternalNames) {
Rafael Espindola49a6c152013-09-04 18:37:36 +0000114 // Function must be defined here
115 if (GV.isDeclaration())
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000116 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000117
118 // Available externally is really just a "declaration with a body".
119 if (GV.hasAvailableExternallyLinkage())
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000120 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000121
Yunzhong Gao9163e8b2013-12-03 18:05:14 +0000122 // Assume that dllexported symbols are referenced elsewhere
Nico Rieck7157bb72014-01-14 15:22:47 +0000123 if (GV.hasDLLExportStorageClass())
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000124 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000125
126 // Marked to keep external?
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000127 if (!GV.hasLocalLinkage() && ExternalNames.count(GV.getName()))
128 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000129
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000130 return false;
131}
132
133// Internalize GV if it is possible to do so, i.e. it is not externally visible
134// and is not a member of an externally visible comdat.
135bool InternalizePass::maybeInternalize(
136 GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) {
137 if (Comdat *C = GV.getComdat()) {
138 if (ExternalComdats.count(C))
139 return false;
140
141 // If a comdat is not externally visible we can drop it.
142 if (auto GO = dyn_cast<GlobalObject>(&GV))
143 GO->setComdat(nullptr);
144
145 if (GV.hasLocalLinkage())
146 return false;
147 } else {
148 if (GV.hasLocalLinkage())
149 return false;
150
151 if (isExternallyVisible(GV, ExternalNames))
152 return false;
153 }
154
155 GV.setVisibility(GlobalValue::DefaultVisibility);
156 GV.setLinkage(GlobalValue::InternalLinkage);
Rafael Espindola282a4702013-10-31 20:51:58 +0000157 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000158}
159
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000160// If GV is part of a comdat and is externally visible, keep track of its
161// comdat so that we don't internalize any of its members.
162void InternalizePass::checkComdatVisibility(
163 GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
164 Comdat *C = GV.getComdat();
165 if (!C)
166 return;
167
168 if (isExternallyVisible(GV, ExternalNames))
169 ExternalComdats.insert(C);
170}
171
Chris Lattner8cdc7732006-01-03 19:13:17 +0000172bool InternalizePass::runOnModule(Module &M) {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000173 CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +0000174 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
175 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000176
Rafael Espindola17600e22013-07-25 03:23:25 +0000177 SmallPtrSet<GlobalValue *, 8> Used;
178 collectUsedGlobalVariables(M, Used, false);
179
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000180 // Collect comdat visiblity information for the module.
181 std::set<const Comdat *> ExternalComdats;
182 if (!M.getComdatSymbolTable().empty()) {
183 for (Function &F : M)
184 checkComdatVisibility(F, ExternalComdats);
185 for (GlobalVariable &GV : M.globals())
186 checkComdatVisibility(GV, ExternalComdats);
187 for (GlobalAlias &GA : M.aliases())
188 checkComdatVisibility(GA, ExternalComdats);
189 }
190
Rafael Espindola17600e22013-07-25 03:23:25 +0000191 // We must assume that globals in llvm.used have a reference that not even
192 // the linker can see, so we don't internalize them.
193 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
194 // linker can drop those symbols. If this pass is running as part of LTO,
195 // one might think that it could just drop llvm.compiler.used. The problem
196 // is that even in LTO llvm doesn't see every reference. For example,
197 // we don't see references from function local inline assembly. To be
198 // conservative, we internalize symbols in llvm.compiler.used, but we
199 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
Craig Topper46276792014-08-24 23:23:06 +0000200 for (GlobalValue *V : Used) {
Rafael Espindola17600e22013-07-25 03:23:25 +0000201 ExternalNames.insert(V->getName());
202 }
203
Devang Patelf2763e22008-05-14 20:01:01 +0000204 // Mark all functions not in the api as internal.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000205 for (Function &I : M) {
206 if (!maybeInternalize(I, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000207 continue;
Bill Wendling2865be72013-08-30 21:07:33 +0000208
Rafael Espindola49a6c152013-09-04 18:37:36 +0000209 if (ExternalNode)
210 // Remove a callgraph edge from the external node to this function.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000211 ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]);
Rafael Espindola49a6c152013-09-04 18:37:36 +0000212
Rafael Espindola49a6c152013-09-04 18:37:36 +0000213 ++NumFunctions;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000214 DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
Rafael Espindola49a6c152013-09-04 18:37:36 +0000215 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000216
Chris Lattner8cdc7732006-01-03 19:13:17 +0000217 // Never internalize the llvm.used symbol. It is used to implement
218 // attribute((used)).
Chris Lattner58f9bb22009-07-20 06:14:25 +0000219 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner8cdc7732006-01-03 19:13:17 +0000220 ExternalNames.insert("llvm.used");
Chris Lattner58f9bb22009-07-20 06:14:25 +0000221 ExternalNames.insert("llvm.compiler.used");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000222
Jim Laskeyc56315c2007-01-26 21:22:28 +0000223 // Never internalize anchors used by the machine module info, else the info
224 // won't find them. (see MachineModuleInfo.)
Chris Lattner34404e32007-06-06 20:51:41 +0000225 ExternalNames.insert("llvm.global_ctors");
226 ExternalNames.insert("llvm.global_dtors");
Tanya Lattner30f65fe2007-10-03 17:05:40 +0000227 ExternalNames.insert("llvm.global.annotations");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000228
Bill Wendling585583c2012-04-13 01:06:27 +0000229 // Never internalize symbols code-gen inserts.
Bill Wendlinge1eaecd2013-08-12 20:09:37 +0000230 // FIXME: We should probably add this (and the __stack_chk_guard) via some
231 // type of call-back in CodeGen.
232 ExternalNames.insert("__stack_chk_fail");
Bill Wendling585583c2012-04-13 01:06:27 +0000233 ExternalNames.insert("__stack_chk_guard");
234
Devang Patelf2763e22008-05-14 20:01:01 +0000235 // Mark all global variables with initializers that are not in the api as
236 // internal as well.
Chris Lattner8cdc7732006-01-03 19:13:17 +0000237 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Rafael Espindola49a6c152013-09-04 18:37:36 +0000238 I != E; ++I) {
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000239 if (!maybeInternalize(*I, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000240 continue;
241
Rafael Espindola49a6c152013-09-04 18:37:36 +0000242 ++NumGlobals;
243 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
244 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000245
Duncan Sands582c53d2009-01-05 21:24:45 +0000246 // Mark all aliases that are not in the api as internal as well.
247 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
Rafael Espindola49a6c152013-09-04 18:37:36 +0000248 I != E; ++I) {
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000249 if (!maybeInternalize(*I, ExternalComdats))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000250 continue;
251
Rafael Espindola49a6c152013-09-04 18:37:36 +0000252 ++NumAliases;
253 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
254 }
Duncan Sands582c53d2009-01-05 21:24:45 +0000255
Peter Collingbourne9b0fe612015-07-16 17:42:21 +0000256 // We do not keep track of whether this pass changed the module because
257 // it adds unnecessary complexity:
258 // 1) This pass will generally be near the start of the pass pipeline, so
259 // there will be no analyses to invalidate.
260 // 2) This pass will most likely end up changing the module and it isn't worth
261 // worrying about optimizing the case where the module is unchanged.
262 return true;
Chris Lattner8cdc7732006-01-03 19:13:17 +0000263}
264
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000265ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
Devang Patel839d9262006-07-20 17:48:05 +0000266
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000267ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) {
268 return new InternalizePass(ExportList);
Devang Patel839d9262006-07-20 17:48:05 +0000269}