blob: 64e2cedfb13ac69abfeca3ad08dabde94f93d876 [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//
Rafael Espindola7e667c52013-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 Espindola713cab02013-10-21 17:14:55 +000016//
Rafael Espindola7e667c52013-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 Espindola713cab02013-10-21 17:14:55 +000019//
Chris Lattnerdbb17352002-04-28 05:43:27 +000020//===----------------------------------------------------------------------===//
21
Chris Lattner86453c52006-12-19 22:09:18 +000022#define DEBUG_TYPE "internalize"
Chris Lattner568ddab2002-07-24 17:12:05 +000023#include "llvm/Transforms/IPO.h"
Rafael Espindola4ef7eaf2013-07-25 03:23:25 +000024#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/Analysis/CallGraph.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000028#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000031#include "llvm/Support/raw_ostream.h"
Rafael Espindola713cab02013-10-21 17:14:55 +000032#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindola4ef7eaf2013-07-25 03:23:25 +000033#include "llvm/Transforms/Utils/ModuleUtils.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000034#include <fstream>
35#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Duncan Sands27a53002009-01-05 21:24:45 +000038STATISTIC(NumAliases , "Number of aliases internalized");
Chris Lattner86453c52006-12-19 22:09:18 +000039STATISTIC(NumFunctions, "Number of functions internalized");
40STATISTIC(NumGlobals , "Number of global vars internalized");
41
Dan Gohman844731a2008-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 Lattnerf6293092002-07-23 18:06:35 +000054namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000055 class InternalizePass : public ModulePass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000056 std::set<std::string> ExternalNames;
57 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000058 static char ID; // Pass identification, replacement for typeid
Rafael Espindolae5551ed2012-10-26 18:47:48 +000059 explicit InternalizePass();
Rafael Espindola7e667c52013-10-31 20:51:58 +000060 explicit InternalizePass(ArrayRef<const char *> ExportList);
Chris Lattner4eb40df2006-01-03 19:13:17 +000061 void LoadFile(const char *Filename);
62 virtual bool runOnModule(Module &M);
Nuno Lopes0483d012008-09-30 22:04:30 +000063
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesCFG();
Duncan Sandsa2582da2008-10-03 07:36:09 +000066 AU.addPreserved<CallGraph>();
Nuno Lopes0483d012008-09-30 22:04:30 +000067 }
Chris Lattner55e41ba2002-07-30 19:48:44 +000068 };
Chris Lattnerf6293092002-07-23 18:06:35 +000069} // end anonymous namespace
70
Dan Gohman844731a2008-05-13 00:00:25 +000071char InternalizePass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000072INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersonce665bd2010-10-07 22:25:06 +000073 "Internalize Global Symbols", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000074
Rafael Espindolae5551ed2012-10-26 18:47:48 +000075InternalizePass::InternalizePass()
76 : ModulePass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000077 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patelef3682a2008-05-14 20:01:01 +000078 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner4eb40df2006-01-03 19:13:17 +000079 LoadFile(APIFile.c_str());
Rafael Espindola1d7df342013-09-04 18:53:21 +000080 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner4eb40df2006-01-03 19:13:17 +000081}
82
Rafael Espindola7e667c52013-10-31 20:51:58 +000083InternalizePass::InternalizePass(ArrayRef<const char *> ExportList)
Rafael Espindolae5551ed2012-10-26 18:47:48 +000084 : ModulePass(ID){
Owen Anderson081c34b2010-10-19 17:21:58 +000085 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Rafael Espindola775079c2013-09-04 20:08:46 +000086 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
87 itr != ExportList.end(); itr++) {
Devang Patel753d94a2006-07-20 17:48:05 +000088 ExternalNames.insert(*itr);
89 }
90}
91
Chris Lattner4eb40df2006-01-03 19:13:17 +000092void InternalizePass::LoadFile(const char *Filename) {
93 // Load the APIFile...
94 std::ifstream In(Filename);
95 if (!In.good()) {
Chris Lattner4437ae22009-08-23 07:05:07 +000096 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelef3682a2008-05-14 20:01:01 +000097 << "'! Continuing as if it's empty.\n";
98 return; // Just continue as if the file were empty
Chris Lattner4eb40df2006-01-03 19:13:17 +000099 }
100 while (In) {
101 std::string Symbol;
102 In >> Symbol;
103 if (!Symbol.empty())
104 ExternalNames.insert(Symbol);
105 }
106}
107
Rafael Espindola0fb77162013-09-04 18:37:36 +0000108static bool shouldInternalize(const GlobalValue &GV,
Rafael Espindola7e667c52013-10-31 20:51:58 +0000109 const std::set<std::string> &ExternalNames) {
Rafael Espindola0fb77162013-09-04 18:37:36 +0000110 // Function must be defined here
111 if (GV.isDeclaration())
112 return false;
113
114 // Available externally is really just a "declaration with a body".
115 if (GV.hasAvailableExternallyLinkage())
116 return false;
117
118 // Already has internal linkage
119 if (GV.hasLocalLinkage())
120 return false;
121
122 // Marked to keep external?
123 if (ExternalNames.count(GV.getName()))
124 return false;
125
Rafael Espindola7e667c52013-10-31 20:51:58 +0000126 return true;
Rafael Espindola0fb77162013-09-04 18:37:36 +0000127}
128
Chris Lattner4eb40df2006-01-03 19:13:17 +0000129bool InternalizePass::runOnModule(Module &M) {
Duncan Sands1465d612009-01-28 13:14:17 +0000130 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000131 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Chris Lattner4eb40df2006-01-03 19:13:17 +0000132 bool Changed = false;
Duncan Sands2631ac32009-01-05 20:38:27 +0000133
Rafael Espindola4ef7eaf2013-07-25 03:23:25 +0000134 SmallPtrSet<GlobalValue *, 8> Used;
135 collectUsedGlobalVariables(M, Used, false);
136
137 // We must assume that globals in llvm.used have a reference that not even
138 // the linker can see, so we don't internalize them.
139 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
140 // linker can drop those symbols. If this pass is running as part of LTO,
141 // one might think that it could just drop llvm.compiler.used. The problem
142 // is that even in LTO llvm doesn't see every reference. For example,
143 // we don't see references from function local inline assembly. To be
144 // conservative, we internalize symbols in llvm.compiler.used, but we
145 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
146 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end();
147 I != E; ++I) {
148 GlobalValue *V = *I;
149 ExternalNames.insert(V->getName());
150 }
151
Devang Patelef3682a2008-05-14 20:01:01 +0000152 // Mark all functions not in the api as internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000153 // FIXME: maybe use private linkage?
Rafael Espindola0fb77162013-09-04 18:37:36 +0000154 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindola7e667c52013-10-31 20:51:58 +0000155 if (!shouldInternalize(*I, ExternalNames))
Rafael Espindola0fb77162013-09-04 18:37:36 +0000156 continue;
Bill Wendling86d49562013-08-30 21:07:33 +0000157
Rafael Espindola0fb77162013-09-04 18:37:36 +0000158 I->setLinkage(GlobalValue::InternalLinkage);
Bill Wendling86d49562013-08-30 21:07:33 +0000159
Rafael Espindola0fb77162013-09-04 18:37:36 +0000160 if (ExternalNode)
161 // Remove a callgraph edge from the external node to this function.
162 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
163
164 Changed = true;
165 ++NumFunctions;
166 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
167 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000168
Chris Lattner4eb40df2006-01-03 19:13:17 +0000169 // Never internalize the llvm.used symbol. It is used to implement
170 // attribute((used)).
Chris Lattner401e10c2009-07-20 06:14:25 +0000171 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner4eb40df2006-01-03 19:13:17 +0000172 ExternalNames.insert("llvm.used");
Chris Lattner401e10c2009-07-20 06:14:25 +0000173 ExternalNames.insert("llvm.compiler.used");
Duncan Sands2631ac32009-01-05 20:38:27 +0000174
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000175 // Never internalize anchors used by the machine module info, else the info
176 // won't find them. (see MachineModuleInfo.)
Chris Lattner4e2288b2007-06-06 20:51:41 +0000177 ExternalNames.insert("llvm.global_ctors");
178 ExternalNames.insert("llvm.global_dtors");
Tanya Lattner088b5912007-10-03 17:05:40 +0000179 ExternalNames.insert("llvm.global.annotations");
Duncan Sands2631ac32009-01-05 20:38:27 +0000180
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000181 // Never internalize symbols code-gen inserts.
Bill Wendlingd275ff52013-08-12 20:09:37 +0000182 // FIXME: We should probably add this (and the __stack_chk_guard) via some
183 // type of call-back in CodeGen.
184 ExternalNames.insert("__stack_chk_fail");
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000185 ExternalNames.insert("__stack_chk_guard");
186
Devang Patelef3682a2008-05-14 20:01:01 +0000187 // Mark all global variables with initializers that are not in the api as
188 // internal as well.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000189 // FIXME: maybe use private linkage?
Chris Lattner4eb40df2006-01-03 19:13:17 +0000190 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Rafael Espindola0fb77162013-09-04 18:37:36 +0000191 I != E; ++I) {
Rafael Espindola7e667c52013-10-31 20:51:58 +0000192 if (!shouldInternalize(*I, ExternalNames))
Rafael Espindola0fb77162013-09-04 18:37:36 +0000193 continue;
194
195 I->setLinkage(GlobalValue::InternalLinkage);
196 Changed = true;
197 ++NumGlobals;
198 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
199 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000200
Duncan Sands27a53002009-01-05 21:24:45 +0000201 // Mark all aliases that are not in the api as internal as well.
202 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
Rafael Espindola0fb77162013-09-04 18:37:36 +0000203 I != E; ++I) {
Rafael Espindola7e667c52013-10-31 20:51:58 +0000204 if (!shouldInternalize(*I, ExternalNames))
Rafael Espindola0fb77162013-09-04 18:37:36 +0000205 continue;
206
207 I->setLinkage(GlobalValue::InternalLinkage);
208 Changed = true;
209 ++NumAliases;
210 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
211 }
Duncan Sands27a53002009-01-05 21:24:45 +0000212
Chris Lattner4eb40df2006-01-03 19:13:17 +0000213 return Changed;
214}
215
Rafael Espindolae5551ed2012-10-26 18:47:48 +0000216ModulePass *llvm::createInternalizePass() {
217 return new InternalizePass();
Chris Lattnerdbb17352002-04-28 05:43:27 +0000218}
Devang Patel753d94a2006-07-20 17:48:05 +0000219
Rafael Espindola7e667c52013-10-31 20:51:58 +0000220ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) {
221 return new InternalizePass(ExportList);
Devang Patel753d94a2006-07-20 17:48:05 +0000222}