blob: aa629cc0c6fba8b064e2aaef4816dea430990873 [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"
Duncan Sandsa2582da2008-10-03 07:36:09 +000017#include "llvm/Analysis/CallGraph.h"
Chris Lattner568ddab2002-07-24 17:12:05 +000018#include "llvm/Transforms/IPO.h"
Chris Lattnerdbb17352002-04-28 05:43:27 +000019#include "llvm/Pass.h"
20#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000023#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000025#include <fstream>
26#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Duncan Sands27a53002009-01-05 21:24:45 +000029STATISTIC(NumAliases , "Number of aliases internalized");
Chris Lattner86453c52006-12-19 22:09:18 +000030STATISTIC(NumFunctions, "Number of functions internalized");
31STATISTIC(NumGlobals , "Number of global vars internalized");
32
Dan Gohman844731a2008-05-13 00:00:25 +000033// APIFile - A file which contains a list of symbols that should not be marked
34// external.
35static cl::opt<std::string>
36APIFile("internalize-public-api-file", cl::value_desc("filename"),
37 cl::desc("A file containing list of symbol names to preserve"));
38
39// APIList - A list of symbols that should not be marked internal.
40static cl::list<std::string>
41APIList("internalize-public-api-list", cl::value_desc("list"),
42 cl::desc("A list of symbol names to preserve"),
43 cl::CommaSeparated);
44
Chris Lattnerf6293092002-07-23 18:06:35 +000045namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000046 class InternalizePass : public ModulePass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000047 std::set<std::string> ExternalNames;
48 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000049 static char ID; // Pass identification, replacement for typeid
Rafael Espindolae5551ed2012-10-26 18:47:48 +000050 explicit InternalizePass();
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000051 explicit InternalizePass(const std::vector <const char *>& exportList);
Chris Lattner4eb40df2006-01-03 19:13:17 +000052 void LoadFile(const char *Filename);
53 virtual bool runOnModule(Module &M);
Nuno Lopes0483d012008-09-30 22:04:30 +000054
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesCFG();
Duncan Sandsa2582da2008-10-03 07:36:09 +000057 AU.addPreserved<CallGraph>();
Nuno Lopes0483d012008-09-30 22:04:30 +000058 }
Chris Lattner55e41ba2002-07-30 19:48:44 +000059 };
Chris Lattnerf6293092002-07-23 18:06:35 +000060} // end anonymous namespace
61
Dan Gohman844731a2008-05-13 00:00:25 +000062char InternalizePass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000063INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersonce665bd2010-10-07 22:25:06 +000064 "Internalize Global Symbols", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000065
Rafael Espindolae5551ed2012-10-26 18:47:48 +000066InternalizePass::InternalizePass()
67 : ModulePass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000068 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patelef3682a2008-05-14 20:01:01 +000069 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner4eb40df2006-01-03 19:13:17 +000070 LoadFile(APIFile.c_str());
Devang Patelef3682a2008-05-14 20:01:01 +000071 if (!APIList.empty()) // If a list is specified, use it as well.
Chris Lattner4eb40df2006-01-03 19:13:17 +000072 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner4eb40df2006-01-03 19:13:17 +000073}
74
Duncan Sands2631ac32009-01-05 20:38:27 +000075InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Rafael Espindolae5551ed2012-10-26 18:47:48 +000076 : ModulePass(ID){
Owen Anderson081c34b2010-10-19 17:21:58 +000077 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
Devang Patel753d94a2006-07-20 17:48:05 +000078 for(std::vector<const char *>::const_iterator itr = exportList.begin();
Anton Korobeynikovbed29462007-04-16 18:10:23 +000079 itr != exportList.end(); itr++) {
Devang Patel753d94a2006-07-20 17:48:05 +000080 ExternalNames.insert(*itr);
81 }
82}
83
Chris Lattner4eb40df2006-01-03 19:13:17 +000084void InternalizePass::LoadFile(const char *Filename) {
85 // Load the APIFile...
86 std::ifstream In(Filename);
87 if (!In.good()) {
Chris Lattner4437ae22009-08-23 07:05:07 +000088 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelef3682a2008-05-14 20:01:01 +000089 << "'! Continuing as if it's empty.\n";
90 return; // Just continue as if the file were empty
Chris Lattner4eb40df2006-01-03 19:13:17 +000091 }
92 while (In) {
93 std::string Symbol;
94 In >> Symbol;
95 if (!Symbol.empty())
96 ExternalNames.insert(Symbol);
97 }
98}
99
100bool InternalizePass::runOnModule(Module &M) {
Duncan Sands1465d612009-01-28 13:14:17 +0000101 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000102 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Chris Lattner4eb40df2006-01-03 19:13:17 +0000103 bool Changed = false;
Duncan Sands2631ac32009-01-05 20:38:27 +0000104
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000105 // Never internalize functions which code-gen might insert.
Bill Wendlingab3a9192012-04-16 04:23:52 +0000106 // FIXME: We should probably add this (and the __stack_chk_guard) via some
107 // type of call-back in CodeGen.
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000108 ExternalNames.insert("__stack_chk_fail");
109
Devang Patelef3682a2008-05-14 20:01:01 +0000110 // Mark all functions not in the api as internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000111 // FIXME: maybe use private linkage?
Chris Lattner4eb40df2006-01-03 19:13:17 +0000112 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000113 if (!I->isDeclaration() && // Function must be defined here
Rafael Espindola1b5ec062011-03-06 23:41:34 +0000114 // Available externally is really just a "declaration with a body".
115 !I->hasAvailableExternallyLinkage() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000116 !I->hasLocalLinkage() && // Can't already have internal linkage
Chris Lattner4eb40df2006-01-03 19:13:17 +0000117 !ExternalNames.count(I->getName())) {// Not marked to keep external?
118 I->setLinkage(GlobalValue::InternalLinkage);
Duncan Sandsa2582da2008-10-03 07:36:09 +0000119 // Remove a callgraph edge from the external node to this function.
120 if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
Chris Lattner4eb40df2006-01-03 19:13:17 +0000121 Changed = true;
122 ++NumFunctions;
David Greenefd2ab9f2010-01-05 01:28:07 +0000123 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
Chris Lattner4eb40df2006-01-03 19:13:17 +0000124 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000125
Chris Lattner4eb40df2006-01-03 19:13:17 +0000126 // Never internalize the llvm.used symbol. It is used to implement
127 // attribute((used)).
Chris Lattner401e10c2009-07-20 06:14:25 +0000128 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner4eb40df2006-01-03 19:13:17 +0000129 ExternalNames.insert("llvm.used");
Chris Lattner401e10c2009-07-20 06:14:25 +0000130 ExternalNames.insert("llvm.compiler.used");
Duncan Sands2631ac32009-01-05 20:38:27 +0000131
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000132 // Never internalize anchors used by the machine module info, else the info
133 // won't find them. (see MachineModuleInfo.)
Chris Lattner4e2288b2007-06-06 20:51:41 +0000134 ExternalNames.insert("llvm.global_ctors");
135 ExternalNames.insert("llvm.global_dtors");
Tanya Lattner088b5912007-10-03 17:05:40 +0000136 ExternalNames.insert("llvm.global.annotations");
Duncan Sands2631ac32009-01-05 20:38:27 +0000137
Bill Wendlingaab3c0c2012-04-13 01:06:27 +0000138 // Never internalize symbols code-gen inserts.
139 ExternalNames.insert("__stack_chk_guard");
140
Devang Patelef3682a2008-05-14 20:01:01 +0000141 // Mark all global variables with initializers that are not in the api as
142 // internal as well.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000143 // FIXME: maybe use private linkage?
Chris Lattner4eb40df2006-01-03 19:13:17 +0000144 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
145 I != E; ++I)
Rafael Espindolabb46f522009-01-15 20:18:42 +0000146 if (!I->isDeclaration() && !I->hasLocalLinkage() &&
Chris Lattner8e9c48a2010-04-03 05:24:50 +0000147 // Available externally is really just a "declaration with a body".
148 !I->hasAvailableExternallyLinkage() &&
Chris Lattner4eb40df2006-01-03 19:13:17 +0000149 !ExternalNames.count(I->getName())) {
Chris Lattner4eb40df2006-01-03 19:13:17 +0000150 I->setLinkage(GlobalValue::InternalLinkage);
151 Changed = true;
152 ++NumGlobals;
David Greenefd2ab9f2010-01-05 01:28:07 +0000153 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
Chris Lattner4eb40df2006-01-03 19:13:17 +0000154 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000155
Duncan Sands27a53002009-01-05 21:24:45 +0000156 // Mark all aliases that are not in the api as internal as well.
157 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
158 I != E; ++I)
159 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
Chris Lattner8e9c48a2010-04-03 05:24:50 +0000160 // Available externally is really just a "declaration with a body".
161 !I->hasAvailableExternallyLinkage() &&
Duncan Sands27a53002009-01-05 21:24:45 +0000162 !ExternalNames.count(I->getName())) {
163 I->setLinkage(GlobalValue::InternalLinkage);
164 Changed = true;
165 ++NumAliases;
David Greenefd2ab9f2010-01-05 01:28:07 +0000166 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
Duncan Sands27a53002009-01-05 21:24:45 +0000167 }
168
Chris Lattner4eb40df2006-01-03 19:13:17 +0000169 return Changed;
170}
171
Rafael Espindolae5551ed2012-10-26 18:47:48 +0000172ModulePass *llvm::createInternalizePass() {
173 return new InternalizePass();
Chris Lattnerdbb17352002-04-28 05:43:27 +0000174}
Devang Patel753d94a2006-07-20 17:48:05 +0000175
Devang Pateld828a4b2006-07-20 18:03:39 +0000176ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
177 return new InternalizePass(el);
Devang Patel753d94a2006-07-20 17:48:05 +0000178}