blob: 462daab02642f8f7ba232ade280c6e81d2e79884 [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"
Reid Spencer9133fe22007-02-05 23:32:05 +000022#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
24#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
Chris Lattner86453c52006-12-19 22:09:18 +000029STATISTIC(NumFunctions, "Number of functions internalized");
30STATISTIC(NumGlobals , "Number of global vars internalized");
31
Dan Gohman844731a2008-05-13 00:00:25 +000032// APIFile - A file which contains a list of symbols that should not be marked
33// external.
34static cl::opt<std::string>
35APIFile("internalize-public-api-file", cl::value_desc("filename"),
36 cl::desc("A file containing list of symbol names to preserve"));
37
38// APIList - A list of symbols that should not be marked internal.
39static cl::list<std::string>
40APIList("internalize-public-api-list", cl::value_desc("list"),
41 cl::desc("A list of symbol names to preserve"),
42 cl::CommaSeparated);
43
Chris Lattnerf6293092002-07-23 18:06:35 +000044namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000045 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000046 std::set<std::string> ExternalNames;
Devang Patelef3682a2008-05-14 20:01:01 +000047 /// If no api symbols were specified and a main function is defined,
48 /// assume the main function is the only API
49 bool AllButMain;
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000050 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000051 static char ID; // Pass identification, replacement for typeid
Matthijs Kooijman4e789082008-06-24 09:14:10 +000052 explicit InternalizePass(bool AllButMain = true);
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000053 explicit InternalizePass(const std::vector <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;
65static RegisterPass<InternalizePass>
66X("internalize", "Internalize Global Symbols");
67
Devang Patelef3682a2008-05-14 20:01:01 +000068InternalizePass::InternalizePass(bool AllButMain)
Dan Gohmanae73dc12008-09-04 17:05:41 +000069 : ModulePass(&ID), AllButMain(AllButMain){
Devang Patelef3682a2008-05-14 20:01:01 +000070 if (!APIFile.empty()) // If a filename is specified, use it.
Chris Lattner4eb40df2006-01-03 19:13:17 +000071 LoadFile(APIFile.c_str());
Devang Patelef3682a2008-05-14 20:01:01 +000072 if (!APIList.empty()) // If a list is specified, use it as well.
Chris Lattner4eb40df2006-01-03 19:13:17 +000073 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner4eb40df2006-01-03 19:13:17 +000074}
75
Devang Patel56eac5f2006-09-13 01:02:26 +000076InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Dan Gohmanae73dc12008-09-04 17:05:41 +000077 : ModulePass(&ID), AllButMain(false){
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()) {
Devang Patelef3682a2008-05-14 20:01:01 +000088 cerr << "WARNING: Internalize couldn't load file '" << Filename
89 << "'! 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 Sandsa2582da2008-10-03 07:36:09 +0000101 CallGraph *CG = getAnalysisToUpdate<CallGraph>();
102 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
103
Chris Lattner4eb40df2006-01-03 19:13:17 +0000104 if (ExternalNames.empty()) {
Devang Patelef3682a2008-05-14 20:01:01 +0000105 // Return if we're not in 'all but main' mode and have no external api
106 if (!AllButMain)
107 return false;
108 // If no list or file of symbols was specified, check to see if there is a
109 // "main" symbol defined in the module. If so, use it, otherwise do not
110 // internalize the module, it must be a library or something.
111 //
Reid Spencer688b0492007-02-05 21:19:13 +0000112 Function *MainFunc = M.getFunction("main");
Reid Spencer5cbf9852007-01-30 20:08:39 +0000113 if (MainFunc == 0 || MainFunc->isDeclaration())
Chris Lattner4eb40df2006-01-03 19:13:17 +0000114 return false; // No main found, must be a library...
115
116 // Preserve main, internalize all else.
117 ExternalNames.insert(MainFunc->getName());
118 }
119
120 bool Changed = false;
121
Devang Patelef3682a2008-05-14 20:01:01 +0000122 // Mark all functions not in the api as internal.
Chris Lattner4eb40df2006-01-03 19:13:17 +0000123 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000124 if (!I->isDeclaration() && // Function must be defined here
Chris Lattner4eb40df2006-01-03 19:13:17 +0000125 !I->hasInternalLinkage() && // Can't already have internal linkage
126 !ExternalNames.count(I->getName())) {// Not marked to keep external?
127 I->setLinkage(GlobalValue::InternalLinkage);
Duncan Sandsa2582da2008-10-03 07:36:09 +0000128 // Remove a callgraph edge from the external node to this function.
129 if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
Chris Lattner4eb40df2006-01-03 19:13:17 +0000130 Changed = true;
131 ++NumFunctions;
Bill Wendling0a81aac2006-11-26 10:02:32 +0000132 DOUT << "Internalizing func " << I->getName() << "\n";
Chris Lattner4eb40df2006-01-03 19:13:17 +0000133 }
134
135 // Never internalize the llvm.used symbol. It is used to implement
136 // attribute((used)).
137 ExternalNames.insert("llvm.used");
Chris Lattneree9e14c2006-01-19 00:40:39 +0000138
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000139 // Never internalize anchors used by the machine module info, else the info
140 // won't find them. (see MachineModuleInfo.)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000141 ExternalNames.insert("llvm.dbg.compile_units");
142 ExternalNames.insert("llvm.dbg.global_variables");
143 ExternalNames.insert("llvm.dbg.subprograms");
Chris Lattner4e2288b2007-06-06 20:51:41 +0000144 ExternalNames.insert("llvm.global_ctors");
145 ExternalNames.insert("llvm.global_dtors");
Chris Lattnerbd14f582007-10-03 03:59:15 +0000146 ExternalNames.insert("llvm.noinline");
Tanya Lattner088b5912007-10-03 17:05:40 +0000147 ExternalNames.insert("llvm.global.annotations");
Chris Lattner4eb40df2006-01-03 19:13:17 +0000148
Devang Patelef3682a2008-05-14 20:01:01 +0000149 // Mark all global variables with initializers that are not in the api as
150 // internal as well.
Chris Lattner4eb40df2006-01-03 19:13:17 +0000151 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
152 I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000153 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
Chris Lattner4eb40df2006-01-03 19:13:17 +0000154 !ExternalNames.count(I->getName())) {
Chris Lattner4eb40df2006-01-03 19:13:17 +0000155 I->setLinkage(GlobalValue::InternalLinkage);
156 Changed = true;
157 ++NumGlobals;
Bill Wendling0a81aac2006-11-26 10:02:32 +0000158 DOUT << "Internalized gvar " << I->getName() << "\n";
Chris Lattner4eb40df2006-01-03 19:13:17 +0000159 }
160
161 return Changed;
162}
163
Matthijs Kooijman4e789082008-06-24 09:14:10 +0000164ModulePass *llvm::createInternalizePass(bool AllButMain) {
165 return new InternalizePass(AllButMain);
Chris Lattnerdbb17352002-04-28 05:43:27 +0000166}
Devang Patel753d94a2006-07-20 17:48:05 +0000167
Devang Pateld828a4b2006-07-20 18:03:39 +0000168ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
169 return new InternalizePass(el);
Devang Patel753d94a2006-07-20 17:48:05 +0000170}