blob: e61591870bc57d01e19da15c810ac773bc43aa69 [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 Espindola3d7fc252013-10-21 17:14:55 +000014// This transformation would not be legal or profitable in a regular
15// compilation, but it gets extra information from the linker about what is safe
16// or profitable.
17//
18// As an example of a normally illegal transformation: Internalizing a function
19// with external linkage. Only if we are told it is only used from within this
20// module, it is safe to do it.
21//
22// On the profitability side: It is always legal to internalize a linkonce_odr
23// whose address is not used. Doing so normally would introduce code bloat, but
24// if we are told by the linker that the only use of this would be for a
25// DSO symbol table, it is profitable to hide it.
26//
Chris Lattner1b94c002002-04-28 05:43:27 +000027//===----------------------------------------------------------------------===//
28
Chris Lattner1631bcb2006-12-19 22:09:18 +000029#define DEBUG_TYPE "internalize"
Chris Lattner99a53f62002-07-24 17:12:05 +000030#include "llvm/Transforms/IPO.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000031#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000038#include "llvm/Support/raw_ostream.h"
Rafael Espindola3d7fc252013-10-21 17:14:55 +000039#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000040#include "llvm/Transforms/Utils/ModuleUtils.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000041#include <fstream>
42#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000043using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000044
Duncan Sands582c53d2009-01-05 21:24:45 +000045STATISTIC(NumAliases , "Number of aliases internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000046STATISTIC(NumFunctions, "Number of functions internalized");
47STATISTIC(NumGlobals , "Number of global vars internalized");
48
Dan Gohmand78c4002008-05-13 00:00:25 +000049// APIFile - A file which contains a list of symbols that should not be marked
50// external.
51static cl::opt<std::string>
52APIFile("internalize-public-api-file", cl::value_desc("filename"),
53 cl::desc("A file containing list of symbol names to preserve"));
54
55// APIList - A list of symbols that should not be marked internal.
56static cl::list<std::string>
57APIList("internalize-public-api-list", cl::value_desc("list"),
58 cl::desc("A list of symbol names to preserve"),
59 cl::CommaSeparated);
60
Rafael Espindolacda29112013-10-03 18:29:09 +000061static cl::list<std::string>
62DSOList("internalize-dso-list", cl::value_desc("list"),
63 cl::desc("A list of symbol names need for a dso symbol table"),
64 cl::CommaSeparated);
65
Chris Lattnerb28b6802002-07-23 18:06:35 +000066namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000067 class InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000068 std::set<std::string> ExternalNames;
Rafael Espindolacda29112013-10-03 18:29:09 +000069 std::set<std::string> DSONames;
Chris Lattner44457bb2003-05-22 19:34:49 +000070 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000071 static char ID; // Pass identification, replacement for typeid
Rafael Espindola4253bd82012-10-26 18:47:48 +000072 explicit InternalizePass();
Rafael Espindolacda29112013-10-03 18:29:09 +000073 explicit InternalizePass(ArrayRef<const char *> ExportList,
74 ArrayRef<const char *> DSOList);
Chris Lattner8cdc7732006-01-03 19:13:17 +000075 void LoadFile(const char *Filename);
76 virtual bool runOnModule(Module &M);
Nuno Lopes5093ab42008-09-30 22:04:30 +000077
78 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesCFG();
Duncan Sands3a813a52008-10-03 07:36:09 +000080 AU.addPreserved<CallGraph>();
Nuno Lopes5093ab42008-09-30 22:04:30 +000081 }
Chris Lattner47cc3662002-07-30 19:48:44 +000082 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000083} // end anonymous namespace
84
Dan Gohmand78c4002008-05-13 00:00:25 +000085char InternalizePass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000086INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersondf7a4f22010-10-07 22:25:06 +000087 "Internalize Global Symbols", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000088
Rafael Espindola4253bd82012-10-26 18:47:48 +000089InternalizePass::InternalizePass()
90 : ModulePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000091 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patelf2763e22008-05-14 20:01:01 +000092 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner8cdc7732006-01-03 19:13:17 +000093 LoadFile(APIFile.c_str());
Rafael Espindolab832d492013-09-04 18:53:21 +000094 ExternalNames.insert(APIList.begin(), APIList.end());
Rafael Espindolacda29112013-10-03 18:29:09 +000095 DSONames.insert(DSOList.begin(), DSOList.end());
Chris Lattner8cdc7732006-01-03 19:13:17 +000096}
97
Rafael Espindolacda29112013-10-03 18:29:09 +000098InternalizePass::InternalizePass(ArrayRef<const char *> ExportList,
99 ArrayRef<const char *> DSOList)
Rafael Espindola4253bd82012-10-26 18:47:48 +0000100 : ModulePass(ID){
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000101 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000102 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
103 itr != ExportList.end(); itr++) {
Devang Patel839d9262006-07-20 17:48:05 +0000104 ExternalNames.insert(*itr);
105 }
Rafael Espindolacda29112013-10-03 18:29:09 +0000106 for(ArrayRef<const char *>::const_iterator itr = DSOList.begin();
107 itr != DSOList.end(); itr++) {
108 DSONames.insert(*itr);
109 }
Devang Patel839d9262006-07-20 17:48:05 +0000110}
111
Chris Lattner8cdc7732006-01-03 19:13:17 +0000112void InternalizePass::LoadFile(const char *Filename) {
113 // Load the APIFile...
114 std::ifstream In(Filename);
115 if (!In.good()) {
Chris Lattner317dbbc2009-08-23 07:05:07 +0000116 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelf2763e22008-05-14 20:01:01 +0000117 << "'! Continuing as if it's empty.\n";
118 return; // Just continue as if the file were empty
Chris Lattner8cdc7732006-01-03 19:13:17 +0000119 }
120 while (In) {
121 std::string Symbol;
122 In >> Symbol;
123 if (!Symbol.empty())
124 ExternalNames.insert(Symbol);
125 }
126}
127
Rafael Espindola49a6c152013-09-04 18:37:36 +0000128static bool shouldInternalize(const GlobalValue &GV,
Rafael Espindolacda29112013-10-03 18:29:09 +0000129 const std::set<std::string> &ExternalNames,
130 const std::set<std::string> &DSONames) {
Rafael Espindola49a6c152013-09-04 18:37:36 +0000131 // Function must be defined here
132 if (GV.isDeclaration())
133 return false;
134
135 // Available externally is really just a "declaration with a body".
136 if (GV.hasAvailableExternallyLinkage())
137 return false;
138
139 // Already has internal linkage
140 if (GV.hasLocalLinkage())
141 return false;
142
143 // Marked to keep external?
144 if (ExternalNames.count(GV.getName()))
145 return false;
146
Rafael Espindolacda29112013-10-03 18:29:09 +0000147 // Not needed for the symbol table?
148 if (!DSONames.count(GV.getName()))
149 return true;
150
151 // Not a linkonce. Someone can depend on it being on the symbol table.
152 if (!GV.hasLinkOnceLinkage())
153 return false;
154
155 // The address is not important, we can hide it.
156 if (GV.hasUnnamedAddr())
157 return true;
158
Rafael Espindola3d7fc252013-10-21 17:14:55 +0000159 GlobalStatus GS;
160 if (GlobalStatus::analyzeGlobal(&GV, GS))
161 return false;
162
163 return !GS.IsCompared;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000164}
165
Chris Lattner8cdc7732006-01-03 19:13:17 +0000166bool InternalizePass::runOnModule(Module &M) {
Duncan Sands5a913d62009-01-28 13:14:17 +0000167 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
Duncan Sands3a813a52008-10-03 07:36:09 +0000168 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Chris Lattner8cdc7732006-01-03 19:13:17 +0000169 bool Changed = false;
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000170
Rafael Espindola17600e22013-07-25 03:23:25 +0000171 SmallPtrSet<GlobalValue *, 8> Used;
172 collectUsedGlobalVariables(M, Used, false);
173
174 // We must assume that globals in llvm.used have a reference that not even
175 // the linker can see, so we don't internalize them.
176 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
177 // linker can drop those symbols. If this pass is running as part of LTO,
178 // one might think that it could just drop llvm.compiler.used. The problem
179 // is that even in LTO llvm doesn't see every reference. For example,
180 // we don't see references from function local inline assembly. To be
181 // conservative, we internalize symbols in llvm.compiler.used, but we
182 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
183 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
184 I != E; ++I) {
185 GlobalValue *V = *I;
186 ExternalNames.insert(V->getName());
187 }
188
Devang Patelf2763e22008-05-14 20:01:01 +0000189 // Mark all functions not in the api as internal.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000190 // FIXME: maybe use private linkage?
Rafael Espindola49a6c152013-09-04 18:37:36 +0000191 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolacda29112013-10-03 18:29:09 +0000192 if (!shouldInternalize(*I, ExternalNames, DSONames))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000193 continue;
Bill Wendling2865be72013-08-30 21:07:33 +0000194
Rafael Espindola49a6c152013-09-04 18:37:36 +0000195 I->setLinkage(GlobalValue::InternalLinkage);
Bill Wendling2865be72013-08-30 21:07:33 +0000196
Rafael Espindola49a6c152013-09-04 18:37:36 +0000197 if (ExternalNode)
198 // Remove a callgraph edge from the external node to this function.
199 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
200
201 Changed = true;
202 ++NumFunctions;
203 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
204 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000205
Chris Lattner8cdc7732006-01-03 19:13:17 +0000206 // Never internalize the llvm.used symbol. It is used to implement
207 // attribute((used)).
Chris Lattner58f9bb22009-07-20 06:14:25 +0000208 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner8cdc7732006-01-03 19:13:17 +0000209 ExternalNames.insert("llvm.used");
Chris Lattner58f9bb22009-07-20 06:14:25 +0000210 ExternalNames.insert("llvm.compiler.used");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000211
Jim Laskeyc56315c2007-01-26 21:22:28 +0000212 // Never internalize anchors used by the machine module info, else the info
213 // won't find them. (see MachineModuleInfo.)
Chris Lattner34404e32007-06-06 20:51:41 +0000214 ExternalNames.insert("llvm.global_ctors");
215 ExternalNames.insert("llvm.global_dtors");
Tanya Lattner30f65fe2007-10-03 17:05:40 +0000216 ExternalNames.insert("llvm.global.annotations");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000217
Bill Wendling585583c2012-04-13 01:06:27 +0000218 // Never internalize symbols code-gen inserts.
Bill Wendlinge1eaecd2013-08-12 20:09:37 +0000219 // FIXME: We should probably add this (and the __stack_chk_guard) via some
220 // type of call-back in CodeGen.
221 ExternalNames.insert("__stack_chk_fail");
Bill Wendling585583c2012-04-13 01:06:27 +0000222 ExternalNames.insert("__stack_chk_guard");
223
Devang Patelf2763e22008-05-14 20:01:01 +0000224 // Mark all global variables with initializers that are not in the api as
225 // internal as well.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000226 // FIXME: maybe use private linkage?
Chris Lattner8cdc7732006-01-03 19:13:17 +0000227 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Rafael Espindola49a6c152013-09-04 18:37:36 +0000228 I != E; ++I) {
Rafael Espindolacda29112013-10-03 18:29:09 +0000229 if (!shouldInternalize(*I, ExternalNames, DSONames))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000230 continue;
231
232 I->setLinkage(GlobalValue::InternalLinkage);
233 Changed = true;
234 ++NumGlobals;
235 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
236 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000237
Duncan Sands582c53d2009-01-05 21:24:45 +0000238 // Mark all aliases that are not in the api as internal as well.
239 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
Rafael Espindola49a6c152013-09-04 18:37:36 +0000240 I != E; ++I) {
Rafael Espindolacda29112013-10-03 18:29:09 +0000241 if (!shouldInternalize(*I, ExternalNames, DSONames))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000242 continue;
243
244 I->setLinkage(GlobalValue::InternalLinkage);
245 Changed = true;
246 ++NumAliases;
247 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
248 }
Duncan Sands582c53d2009-01-05 21:24:45 +0000249
Chris Lattner8cdc7732006-01-03 19:13:17 +0000250 return Changed;
251}
252
Rafael Espindola4253bd82012-10-26 18:47:48 +0000253ModulePass *llvm::createInternalizePass() {
254 return new InternalizePass();
Chris Lattner1b94c002002-04-28 05:43:27 +0000255}
Devang Patel839d9262006-07-20 17:48:05 +0000256
Rafael Espindolacda29112013-10-03 18:29:09 +0000257ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList,
258 ArrayRef<const char *> DSOList) {
259 return new InternalizePass(ExportList, DSOList);
Devang Patel839d9262006-07-20 17:48:05 +0000260}