blob: cd20a1d937305c0f2e019712f3e15be0a862f4e7 [file] [log] [blame]
Chris Lattnerdbb17352002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerdbb17352002-04-28 05:43:27 +00009//
Rafael Espindolae5551ed2012-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 Lattnerdbb17352002-04-28 05:43:27 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner86453c52006-12-19 22:09:18 +000016#define DEBUG_TYPE "internalize"
Chris Lattner568ddab2002-07-24 17:12:05 +000017#include "llvm/Transforms/IPO.h"
Rafael Espindola4ef7eaf2013-07-25 03:23:25 +000018#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/Statistic.h"
20#include "llvm/Analysis/CallGraph.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000025#include "llvm/Support/raw_ostream.h"
Rafael Espindola4ef7eaf2013-07-25 03:23:25 +000026#include "llvm/Transforms/Utils/ModuleUtils.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000027#include <fstream>
28#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Duncan Sands27a53002009-01-05 21:24:45 +000031STATISTIC(NumAliases , "Number of aliases internalized");
Chris Lattner86453c52006-12-19 22:09:18 +000032STATISTIC(NumFunctions, "Number of functions internalized");
33STATISTIC(NumGlobals , "Number of global vars internalized");
34
Dan Gohman844731a2008-05-13 00:00:25 +000035// APIFile - A file which contains a list of symbols that should not be marked
36// external.
37static cl::opt<std::string>
38APIFile("internalize-public-api-file", cl::value_desc("filename"),
39 cl::desc("A file containing list of symbol names to preserve"));
40
41// APIList - A list of symbols that should not be marked internal.
42static cl::list<std::string>
43APIList("internalize-public-api-list", cl::value_desc("list"),
44 cl::desc("A list of symbol names to preserve"),
45 cl::CommaSeparated);
46
Chris Lattnerf6293092002-07-23 18:06:35 +000047namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000048 class InternalizePass : public ModulePass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000049 std::set<std::string> ExternalNames;
50 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000051 static char ID; // Pass identification, replacement for typeid
Rafael Espindolae5551ed2012-10-26 18:47:48 +000052 explicit InternalizePass();
Rafael Espindola0439f3e2012-12-11 16:36:02 +000053 explicit InternalizePass(ArrayRef<const char *> exportList);
Chris Lattner4eb40df2006-01-03 19:13:17 +000054 void LoadFile(const char *Filename);
55 virtual bool runOnModule(Module &M);
Nuno Lopes0483d012008-09-30 22:04:30 +000056
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesCFG();
Duncan Sandsa2582da2008-10-03 07:36:09 +000059 AU.addPreserved<CallGraph>();
Nuno Lopes0483d012008-09-30 22:04:30 +000060 }
Chris Lattner55e41ba2002-07-30 19:48:44 +000061 };
Chris Lattnerf6293092002-07-23 18:06:35 +000062} // end anonymous namespace
63
Dan Gohman844731a2008-05-13 00:00:25 +000064char InternalizePass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000065INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersonce665bd2010-10-07 22:25:06 +000066 "Internalize Global Symbols", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000067
Rafael Espindolae5551ed2012-10-26 18:47:48 +000068InternalizePass::InternalizePass()
69 : ModulePass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000070 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patelef3682a2008-05-14 20:01:01 +000071 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner4eb40df2006-01-03 19:13:17 +000072 LoadFile(APIFile.c_str());
Devang Patelef3682a2008-05-14 20:01:01 +000073 if (!APIList.empty()) // If a list is specified, use it as well.
Chris Lattner4eb40df2006-01-03 19:13:17 +000074 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner4eb40df2006-01-03 19:13:17 +000075}
76
Rafael Espindola0439f3e2012-12-11 16:36:02 +000077InternalizePass::InternalizePass(ArrayRef<const char *> exportList)
Rafael Espindolae5551ed2012-10-26 18:47:48 +000078 : ModulePass(ID){
Owen Anderson081c34b2010-10-19 17:21:58 +000079 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Rafael Espindola0439f3e2012-12-11 16:36:02 +000080 for(ArrayRef<const char *>::const_iterator itr = exportList.begin();
Anton Korobeynikovbed29462007-04-16 18:10:23 +000081 itr != exportList.end(); itr++) {
Devang Patel753d94a2006-07-20 17:48:05 +000082 ExternalNames.insert(*itr);
83 }
84}
85
Chris Lattner4eb40df2006-01-03 19:13:17 +000086void InternalizePass::LoadFile(const char *Filename) {
87 // Load the APIFile...
88 std::ifstream In(Filename);
89 if (!In.good()) {
Chris Lattner4437ae22009-08-23 07:05:07 +000090 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelef3682a2008-05-14 20:01:01 +000091 << "'! Continuing as if it's empty.\n";
92 return; // Just continue as if the file were empty
Chris Lattner4eb40df2006-01-03 19:13:17 +000093 }
94 while (In) {
95 std::string Symbol;
96 In >> Symbol;
97 if (!Symbol.empty())
98 ExternalNames.insert(Symbol);
99 }
100}
101
Rafael Espindola0fb77162013-09-04 18:37:36 +0000102static bool shouldInternalize(const GlobalValue &GV,
103 const std::set<std::string> &ExternalNames) {
104 // Function must be defined here
105 if (GV.isDeclaration())
106 return false;
107
108 // Available externally is really just a "declaration with a body".
109 if (GV.hasAvailableExternallyLinkage())
110 return false;
111
112 // Already has internal linkage
113 if (GV.hasLocalLinkage())
114 return false;
115
116 // Marked to keep external?
117 if (ExternalNames.count(GV.getName()))
118 return false;
119
120 return true;
121}
122
Chris Lattner4eb40df2006-01-03 19:13:17 +0000123bool InternalizePass::runOnModule(Module &M) {
Duncan Sands1465d612009-01-28 13:14:17 +0000124 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000125 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Chris Lattner4eb40df2006-01-03 19:13:17 +0000126 bool Changed = false;
Duncan Sands2631ac32009-01-05 20:38:27 +0000127
Rafael Espindola4ef7eaf2013-07-25 03:23:25 +0000128 SmallPtrSet<GlobalValue *, 8> Used;
129 collectUsedGlobalVariables(M, Used, false);
130
131 // We must assume that globals in llvm.used have a reference that not even
132 // the linker can see, so we don't internalize them.
133 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
134 // linker can drop those symbols. If this pass is running as part of LTO,
135 // one might think that it could just drop llvm.compiler.used. The problem
136 // is that even in LTO llvm doesn't see every reference. For example,
137 // we don't see references from function local inline assembly. To be
138 // conservative, we internalize symbols in llvm.compiler.used, but we
139 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
140 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
141 I != E; ++I) {
142 GlobalValue *V = *I;
143 ExternalNames.insert(V->getName());
144 }
145
Devang Patelef3682a2008-05-14 20:01:01 +0000146 // Mark all functions not in the api as internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000147 // FIXME: maybe use private linkage?
Rafael Espindola0fb77162013-09-04 18:37:36 +0000148 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
149 if (!shouldInternalize(*I, ExternalNames))
150 continue;
Bill Wendling86d49562013-08-30 21:07:33 +0000151
Rafael Espindola0fb77162013-09-04 18:37:36 +0000152 I->setLinkage(GlobalValue::InternalLinkage);
Bill Wendling86d49562013-08-30 21:07:33 +0000153
Rafael Espindola0fb77162013-09-04 18:37:36 +0000154 if (ExternalNode)
155 // Remove a callgraph edge from the external node to this function.
156 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
157
158 Changed = true;
159 ++NumFunctions;
160 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
161 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000162
Chris Lattner4eb40df2006-01-03 19:13:17 +0000163 // Never internalize the llvm.used symbol. It is used to implement
164 // attribute((used)).
Chris Lattner401e10c2009-07-20 06:14:25 +0000165 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner4eb40df2006-01-03 19:13:17 +0000166 ExternalNames.insert("llvm.used");
Chris Lattner401e10c2009-07-20 06:14:25 +0000167 ExternalNames.insert("llvm.compiler.used");
Duncan Sands2631ac32009-01-05 20:38:27 +0000168
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000169 // Never internalize anchors used by the machine module info, else the info
170 // won't find them. (see MachineModuleInfo.)
Chris Lattner4e2288b2007-06-06 20:51:41 +0000171 ExternalNames.insert("llvm.global_ctors");
172 ExternalNames.insert("llvm.global_dtors");
Tanya Lattner088b5912007-10-03 17:05:40 +0000173 ExternalNames.insert("llvm.global.annotations");
Duncan Sands2631ac32009-01-05 20:38:27 +0000174
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000175 // Never internalize symbols code-gen inserts.
Bill Wendlingd275ff52013-08-12 20:09:37 +0000176 // FIXME: We should probably add this (and the __stack_chk_guard) via some
177 // type of call-back in CodeGen.
178 ExternalNames.insert("__stack_chk_fail");
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000179 ExternalNames.insert("__stack_chk_guard");
180
Devang Patelef3682a2008-05-14 20:01:01 +0000181 // Mark all global variables with initializers that are not in the api as
182 // internal as well.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000183 // FIXME: maybe use private linkage?
Chris Lattner4eb40df2006-01-03 19:13:17 +0000184 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Rafael Espindola0fb77162013-09-04 18:37:36 +0000185 I != E; ++I) {
186 if (!shouldInternalize(*I, ExternalNames))
187 continue;
188
189 I->setLinkage(GlobalValue::InternalLinkage);
190 Changed = true;
191 ++NumGlobals;
192 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
193 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000194
Duncan Sands27a53002009-01-05 21:24:45 +0000195 // Mark all aliases that are not in the api as internal as well.
196 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
Rafael Espindola0fb77162013-09-04 18:37:36 +0000197 I != E; ++I) {
198 if (!shouldInternalize(*I, ExternalNames))
199 continue;
200
201 I->setLinkage(GlobalValue::InternalLinkage);
202 Changed = true;
203 ++NumAliases;
204 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
205 }
Duncan Sands27a53002009-01-05 21:24:45 +0000206
Chris Lattner4eb40df2006-01-03 19:13:17 +0000207 return Changed;
208}
209
Rafael Espindolae5551ed2012-10-26 18:47:48 +0000210ModulePass *llvm::createInternalizePass() {
211 return new InternalizePass();
Chris Lattnerdbb17352002-04-28 05:43:27 +0000212}
Devang Patel753d94a2006-07-20 17:48:05 +0000213
Rafael Espindola0439f3e2012-12-11 16:36:02 +0000214ModulePass *llvm::createInternalizePass(ArrayRef<const char *> el) {
Devang Pateld828a4b2006-07-20 18:03:39 +0000215 return new InternalizePass(el);
Devang Patel753d94a2006-07-20 17:48:05 +0000216}