blob: 45f13eaadcd2b03d4f7990a45524aa321a135b03 [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//
10// This pass loops over all of the functions in the input module, looking for a
Chris Lattner55e41ba2002-07-30 19:48:44 +000011// main function. If a main function is found, all other functions and all
12// global variables with initializers are 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;
Devang Patelef3682a2008-05-14 20:01:01 +000048 /// If no api symbols were specified and a main function is defined,
49 /// assume the main function is the only API
50 bool AllButMain;
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000051 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000052 static char ID; // Pass identification, replacement for typeid
Matthijs Kooijman4e789082008-06-24 09:14:10 +000053 explicit InternalizePass(bool AllButMain = true);
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000054 explicit InternalizePass(const std::vector <const char *>& exportList);
Chris Lattner4eb40df2006-01-03 19:13:17 +000055 void LoadFile(const char *Filename);
56 virtual bool runOnModule(Module &M);
Nuno Lopes0483d012008-09-30 22:04:30 +000057
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesCFG();
Duncan Sandsa2582da2008-10-03 07:36:09 +000060 AU.addPreserved<CallGraph>();
Nuno Lopes0483d012008-09-30 22:04:30 +000061 }
Chris Lattner55e41ba2002-07-30 19:48:44 +000062 };
Chris Lattnerf6293092002-07-23 18:06:35 +000063} // end anonymous namespace
64
Dan Gohman844731a2008-05-13 00:00:25 +000065char InternalizePass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000066INITIALIZE_PASS(InternalizePass, "internalize",
Owen Andersonce665bd2010-10-07 22:25:06 +000067 "Internalize Global Symbols", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000068
Devang Patelef3682a2008-05-14 20:01:01 +000069InternalizePass::InternalizePass(bool AllButMain)
Owen Anderson90c579d2010-08-06 18:33:48 +000070 : ModulePass(ID), AllButMain(AllButMain){
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
Duncan Sands2631ac32009-01-05 20:38:27 +000077InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Owen Anderson90c579d2010-08-06 18:33:48 +000078 : ModulePass(ID), AllButMain(false){
Devang Patel753d94a2006-07-20 17:48:05 +000079 for(std::vector<const char *>::const_iterator itr = exportList.begin();
Anton Korobeynikovbed29462007-04-16 18:10:23 +000080 itr != exportList.end(); itr++) {
Devang Patel753d94a2006-07-20 17:48:05 +000081 ExternalNames.insert(*itr);
82 }
83}
84
Chris Lattner4eb40df2006-01-03 19:13:17 +000085void InternalizePass::LoadFile(const char *Filename) {
86 // Load the APIFile...
87 std::ifstream In(Filename);
88 if (!In.good()) {
Chris Lattner4437ae22009-08-23 07:05:07 +000089 errs() << "WARNING: Internalize couldn't load file '" << Filename
Devang Patelef3682a2008-05-14 20:01:01 +000090 << "'! Continuing as if it's empty.\n";
91 return; // Just continue as if the file were empty
Chris Lattner4eb40df2006-01-03 19:13:17 +000092 }
93 while (In) {
94 std::string Symbol;
95 In >> Symbol;
96 if (!Symbol.empty())
97 ExternalNames.insert(Symbol);
98 }
99}
100
101bool InternalizePass::runOnModule(Module &M) {
Duncan Sands1465d612009-01-28 13:14:17 +0000102 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000103 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Owen Anderson001dbfe2009-07-16 18:04:31 +0000104
Chris Lattner4eb40df2006-01-03 19:13:17 +0000105 if (ExternalNames.empty()) {
Devang Patelef3682a2008-05-14 20:01:01 +0000106 // Return if we're not in 'all but main' mode and have no external api
107 if (!AllButMain)
Duncan Sands2631ac32009-01-05 20:38:27 +0000108 return false;
Devang Patelef3682a2008-05-14 20:01:01 +0000109 // If no list or file of symbols was specified, check to see if there is a
110 // "main" symbol defined in the module. If so, use it, otherwise do not
111 // internalize the module, it must be a library or something.
112 //
Reid Spencer688b0492007-02-05 21:19:13 +0000113 Function *MainFunc = M.getFunction("main");
Reid Spencer5cbf9852007-01-30 20:08:39 +0000114 if (MainFunc == 0 || MainFunc->isDeclaration())
Chris Lattner4eb40df2006-01-03 19:13:17 +0000115 return false; // No main found, must be a library...
Duncan Sands2631ac32009-01-05 20:38:27 +0000116
Chris Lattner4eb40df2006-01-03 19:13:17 +0000117 // Preserve main, internalize all else.
118 ExternalNames.insert(MainFunc->getName());
119 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000120
Chris Lattner4eb40df2006-01-03 19:13:17 +0000121 bool Changed = false;
Duncan Sands2631ac32009-01-05 20:38:27 +0000122
Devang Patelef3682a2008-05-14 20:01:01 +0000123 // Mark all functions not in the api as internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000124 // FIXME: maybe use private linkage?
Chris Lattner4eb40df2006-01-03 19:13:17 +0000125 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000126 if (!I->isDeclaration() && // Function must be defined here
Rafael Espindolabb46f522009-01-15 20:18:42 +0000127 !I->hasLocalLinkage() && // Can't already have internal linkage
Chris Lattner4eb40df2006-01-03 19:13:17 +0000128 !ExternalNames.count(I->getName())) {// Not marked to keep external?
129 I->setLinkage(GlobalValue::InternalLinkage);
Duncan Sandsa2582da2008-10-03 07:36:09 +0000130 // Remove a callgraph edge from the external node to this function.
131 if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
Chris Lattner4eb40df2006-01-03 19:13:17 +0000132 Changed = true;
133 ++NumFunctions;
David Greenefd2ab9f2010-01-05 01:28:07 +0000134 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
Chris Lattner4eb40df2006-01-03 19:13:17 +0000135 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000136
Chris Lattner4eb40df2006-01-03 19:13:17 +0000137 // Never internalize the llvm.used symbol. It is used to implement
138 // attribute((used)).
Chris Lattner401e10c2009-07-20 06:14:25 +0000139 // FIXME: Shouldn't this just filter on llvm.metadata section??
Chris Lattner4eb40df2006-01-03 19:13:17 +0000140 ExternalNames.insert("llvm.used");
Chris Lattner401e10c2009-07-20 06:14:25 +0000141 ExternalNames.insert("llvm.compiler.used");
Duncan Sands2631ac32009-01-05 20:38:27 +0000142
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000143 // Never internalize anchors used by the machine module info, else the info
144 // won't find them. (see MachineModuleInfo.)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000145 ExternalNames.insert("llvm.dbg.compile_units");
146 ExternalNames.insert("llvm.dbg.global_variables");
147 ExternalNames.insert("llvm.dbg.subprograms");
Chris Lattner4e2288b2007-06-06 20:51:41 +0000148 ExternalNames.insert("llvm.global_ctors");
149 ExternalNames.insert("llvm.global_dtors");
Chris Lattnerbd14f582007-10-03 03:59:15 +0000150 ExternalNames.insert("llvm.noinline");
Tanya Lattner088b5912007-10-03 17:05:40 +0000151 ExternalNames.insert("llvm.global.annotations");
Duncan Sands2631ac32009-01-05 20:38:27 +0000152
Devang Patelef3682a2008-05-14 20:01:01 +0000153 // Mark all global variables with initializers that are not in the api as
154 // internal as well.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000155 // FIXME: maybe use private linkage?
Chris Lattner4eb40df2006-01-03 19:13:17 +0000156 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
157 I != E; ++I)
Rafael Espindolabb46f522009-01-15 20:18:42 +0000158 if (!I->isDeclaration() && !I->hasLocalLinkage() &&
Chris Lattner8e9c48a2010-04-03 05:24:50 +0000159 // Available externally is really just a "declaration with a body".
160 !I->hasAvailableExternallyLinkage() &&
Chris Lattner4eb40df2006-01-03 19:13:17 +0000161 !ExternalNames.count(I->getName())) {
Chris Lattner4eb40df2006-01-03 19:13:17 +0000162 I->setLinkage(GlobalValue::InternalLinkage);
163 Changed = true;
164 ++NumGlobals;
David Greenefd2ab9f2010-01-05 01:28:07 +0000165 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
Chris Lattner4eb40df2006-01-03 19:13:17 +0000166 }
Duncan Sands2631ac32009-01-05 20:38:27 +0000167
Duncan Sands27a53002009-01-05 21:24:45 +0000168 // Mark all aliases that are not in the api as internal as well.
169 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
170 I != E; ++I)
171 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
Chris Lattner8e9c48a2010-04-03 05:24:50 +0000172 // Available externally is really just a "declaration with a body".
173 !I->hasAvailableExternallyLinkage() &&
Duncan Sands27a53002009-01-05 21:24:45 +0000174 !ExternalNames.count(I->getName())) {
175 I->setLinkage(GlobalValue::InternalLinkage);
176 Changed = true;
177 ++NumAliases;
David Greenefd2ab9f2010-01-05 01:28:07 +0000178 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
Duncan Sands27a53002009-01-05 21:24:45 +0000179 }
180
Chris Lattner4eb40df2006-01-03 19:13:17 +0000181 return Changed;
182}
183
Matthijs Kooijman4e789082008-06-24 09:14:10 +0000184ModulePass *llvm::createInternalizePass(bool AllButMain) {
185 return new InternalizePass(AllButMain);
Chris Lattnerdbb17352002-04-28 05:43:27 +0000186}
Devang Patel753d94a2006-07-20 17:48:05 +0000187
Devang Pateld828a4b2006-07-20 18:03:39 +0000188ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
189 return new InternalizePass(el);
Devang Patel753d94a2006-07-20 17:48:05 +0000190}