blob: c1fe01cebcd18fea2957cd82f9332e575530e12b [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 Lattner1631bcb2006-12-19 22:09:18 +000022#define DEBUG_TYPE "internalize"
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"
26#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"
Rafael Espindola3d7fc252013-10-21 17:14:55 +000032#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindola17600e22013-07-25 03:23:25 +000033#include "llvm/Transforms/Utils/ModuleUtils.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
Duncan Sands582c53d2009-01-05 21:24:45 +000038STATISTIC(NumAliases , "Number of aliases internalized");
Chris Lattner1631bcb2006-12-19 22:09:18 +000039STATISTIC(NumFunctions, "Number of functions internalized");
40STATISTIC(NumGlobals , "Number of global vars internalized");
41
Dan Gohmand78c4002008-05-13 00:00:25 +000042// APIFile - A file which contains a list of symbols that should not be marked
43// external.
44static cl::opt<std::string>
45APIFile("internalize-public-api-file", cl::value_desc("filename"),
46 cl::desc("A file containing list of symbol names to preserve"));
47
48// APIList - A list of symbols that should not be marked internal.
49static cl::list<std::string>
50APIList("internalize-public-api-list", cl::value_desc("list"),
51 cl::desc("A list of symbol names to preserve"),
52 cl::CommaSeparated);
53
Chris Lattnerb28b6802002-07-23 18:06:35 +000054namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000055 class InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000056 std::set<std::string> ExternalNames;
57 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000058 static char ID; // Pass identification, replacement for typeid
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +000059 explicit InternalizePass();
60 explicit InternalizePass(ArrayRef<const char *> ExportList);
Chris Lattner8cdc7732006-01-03 19:13:17 +000061 void LoadFile(const char *Filename);
Craig Topper3e4c6972014-03-05 09:10:37 +000062 bool runOnModule(Module &M) override;
Nuno Lopes5093ab42008-09-30 22:04:30 +000063
Craig Topper3e4c6972014-03-05 09:10:37 +000064 void getAnalysisUsage(AnalysisUsage &AU) const override {
Nuno Lopes5093ab42008-09-30 22:04:30 +000065 AU.setPreservesCFG();
Chandler Carruth6378cf52013-11-26 04:19:30 +000066 AU.addPreserved<CallGraphWrapperPass>();
Nuno Lopes5093ab42008-09-30 22:04:30 +000067 }
Chris Lattner47cc3662002-07-30 19:48:44 +000068 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000069} // end anonymous namespace
70
Dan Gohmand78c4002008-05-13 00:00:25 +000071char InternalizePass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000072INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersondf7a4f22010-10-07 22:25:06 +000073 "Internalize Global Symbols", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000074
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +000075InternalizePass::InternalizePass() : ModulePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000076 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patelf2763e22008-05-14 20:01:01 +000077 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner8cdc7732006-01-03 19:13:17 +000078 LoadFile(APIFile.c_str());
Rafael Espindolab832d492013-09-04 18:53:21 +000079 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner8cdc7732006-01-03 19:13:17 +000080}
81
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +000082InternalizePass::InternalizePass(ArrayRef<const char *> ExportList)
83 : ModulePass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000084 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +000085 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
86 itr != ExportList.end(); itr++) {
Devang Patel839d9262006-07-20 17:48:05 +000087 ExternalNames.insert(*itr);
88 }
89}
90
Chris Lattner8cdc7732006-01-03 19:13:17 +000091void InternalizePass::LoadFile(const char *Filename) {
92 // Load the APIFile...
93 std::ifstream In(Filename);
94 if (!In.good()) {
Chris Lattner317dbbc2009-08-23 07:05:07 +000095 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelf2763e22008-05-14 20:01:01 +000096 << "'! Continuing as if it's empty.\n";
97 return; // Just continue as if the file were empty
Chris Lattner8cdc7732006-01-03 19:13:17 +000098 }
99 while (In) {
100 std::string Symbol;
101 In >> Symbol;
102 if (!Symbol.empty())
103 ExternalNames.insert(Symbol);
104 }
105}
106
Rafael Espindola49a6c152013-09-04 18:37:36 +0000107static bool shouldInternalize(const GlobalValue &GV,
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000108 const std::set<std::string> &ExternalNames) {
Rafael Espindola49a6c152013-09-04 18:37:36 +0000109 // Function must be defined here
110 if (GV.isDeclaration())
111 return false;
112
113 // Available externally is really just a "declaration with a body".
114 if (GV.hasAvailableExternallyLinkage())
115 return false;
116
Yunzhong Gao9163e8b2013-12-03 18:05:14 +0000117 // Assume that dllexported symbols are referenced elsewhere
Nico Rieck7157bb72014-01-14 15:22:47 +0000118 if (GV.hasDLLExportStorageClass())
Yunzhong Gao9163e8b2013-12-03 18:05:14 +0000119 return false;
120
Rafael Espindola49a6c152013-09-04 18:37:36 +0000121 // Already has internal linkage
122 if (GV.hasLocalLinkage())
123 return false;
124
125 // Marked to keep external?
126 if (ExternalNames.count(GV.getName()))
127 return false;
128
Rafael Espindola282a4702013-10-31 20:51:58 +0000129 return true;
Rafael Espindola49a6c152013-09-04 18:37:36 +0000130}
131
Chris Lattner8cdc7732006-01-03 19:13:17 +0000132bool InternalizePass::runOnModule(Module &M) {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000133 CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>();
134 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : 0;
Duncan Sands3a813a52008-10-03 07:36:09 +0000135 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Chris Lattner8cdc7732006-01-03 19:13:17 +0000136 bool Changed = false;
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000137
Rafael Espindola17600e22013-07-25 03:23:25 +0000138 SmallPtrSet<GlobalValue *, 8> Used;
139 collectUsedGlobalVariables(M, Used, false);
140
141 // We must assume that globals in llvm.used have a reference that not even
142 // the linker can see, so we don't internalize them.
143 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
144 // linker can drop those symbols. If this pass is running as part of LTO,
145 // one might think that it could just drop llvm.compiler.used. The problem
146 // is that even in LTO llvm doesn't see every reference. For example,
147 // we don't see references from function local inline assembly. To be
148 // conservative, we internalize symbols in llvm.compiler.used, but we
149 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
150 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
151 I != E; ++I) {
152 GlobalValue *V = *I;
153 ExternalNames.insert(V->getName());
154 }
155
Devang Patelf2763e22008-05-14 20:01:01 +0000156 // Mark all functions not in the api as internal.
Rafael Espindola49a6c152013-09-04 18:37:36 +0000157 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000158 if (!shouldInternalize(*I, ExternalNames))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000159 continue;
Bill Wendling2865be72013-08-30 21:07:33 +0000160
Rafael Espindola49a6c152013-09-04 18:37:36 +0000161 I->setLinkage(GlobalValue::InternalLinkage);
Bill Wendling2865be72013-08-30 21:07:33 +0000162
Rafael Espindola49a6c152013-09-04 18:37:36 +0000163 if (ExternalNode)
164 // Remove a callgraph edge from the external node to this function.
165 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
166
167 Changed = true;
168 ++NumFunctions;
169 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
170 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000171
Chris Lattner8cdc7732006-01-03 19:13:17 +0000172 // Never internalize the llvm.used symbol. It is used to implement
173 // attribute((used)).
Chris Lattner58f9bb22009-07-20 06:14:25 +0000174 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner8cdc7732006-01-03 19:13:17 +0000175 ExternalNames.insert("llvm.used");
Chris Lattner58f9bb22009-07-20 06:14:25 +0000176 ExternalNames.insert("llvm.compiler.used");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000177
Jim Laskeyc56315c2007-01-26 21:22:28 +0000178 // Never internalize anchors used by the machine module info, else the info
179 // won't find them. (see MachineModuleInfo.)
Chris Lattner34404e32007-06-06 20:51:41 +0000180 ExternalNames.insert("llvm.global_ctors");
181 ExternalNames.insert("llvm.global_dtors");
Tanya Lattner30f65fe2007-10-03 17:05:40 +0000182 ExternalNames.insert("llvm.global.annotations");
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000183
Bill Wendling585583c2012-04-13 01:06:27 +0000184 // Never internalize symbols code-gen inserts.
Bill Wendlinge1eaecd2013-08-12 20:09:37 +0000185 // 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");
Bill Wendling585583c2012-04-13 01:06:27 +0000188 ExternalNames.insert("__stack_chk_guard");
189
Devang Patelf2763e22008-05-14 20:01:01 +0000190 // Mark all global variables with initializers that are not in the api as
191 // internal as well.
Chris Lattner8cdc7732006-01-03 19:13:17 +0000192 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Rafael Espindola49a6c152013-09-04 18:37:36 +0000193 I != E; ++I) {
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000194 if (!shouldInternalize(*I, ExternalNames))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000195 continue;
196
197 I->setLinkage(GlobalValue::InternalLinkage);
198 Changed = true;
199 ++NumGlobals;
200 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
201 }
Duncan Sandsd24b93f2009-01-05 20:38:27 +0000202
Duncan Sands582c53d2009-01-05 21:24:45 +0000203 // Mark all aliases that are not in the api as internal as well.
204 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
Rafael Espindola49a6c152013-09-04 18:37:36 +0000205 I != E; ++I) {
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000206 if (!shouldInternalize(*I, ExternalNames))
Rafael Espindola49a6c152013-09-04 18:37:36 +0000207 continue;
208
209 I->setLinkage(GlobalValue::InternalLinkage);
210 Changed = true;
211 ++NumAliases;
212 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
213 }
Duncan Sands582c53d2009-01-05 21:24:45 +0000214
Chris Lattner8cdc7732006-01-03 19:13:17 +0000215 return Changed;
216}
217
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000218ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
Devang Patel839d9262006-07-20 17:48:05 +0000219
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000220ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) {
221 return new InternalizePass(ExportList);
Devang Patel839d9262006-07-20 17:48:05 +0000222}