blob: b6d89aafd0f0a37b433fcaddf874200c212c06ea [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass loops over all of the functions in the input module, looking for a
11// main function. If a main function is found, all other functions and all
12// global variables with initializers are marked as internal.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "internalize"
Duncan Sands349bf2e2008-10-03 07:36:09 +000017#include "llvm/Analysis/CallGraph.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Transforms/IPO.h"
19#include "llvm/Pass.h"
20#include "llvm/Module.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/ADT/Statistic.h"
25#include <fstream>
26#include <set>
27using namespace llvm;
28
Duncan Sands414d6582009-01-05 21:24:45 +000029STATISTIC(NumAliases , "Number of aliases internalized");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030STATISTIC(NumFunctions, "Number of functions internalized");
31STATISTIC(NumGlobals , "Number of global vars internalized");
32
Dan Gohman089efff2008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
47 std::set<std::string> ExternalNames;
Devang Patel23c4b312008-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 public:
52 static char ID; // Pass identification, replacement for typeid
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +000053 explicit InternalizePass(bool AllButMain = true);
Dan Gohman34c280e2007-08-01 15:32:29 +000054 explicit InternalizePass(const std::vector <const char *>& exportList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 void LoadFile(const char *Filename);
56 virtual bool runOnModule(Module &M);
Nuno Lopes5de97242008-09-30 22:04:30 +000057
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesCFG();
Duncan Sands349bf2e2008-10-03 07:36:09 +000060 AU.addPreserved<CallGraph>();
Nuno Lopes5de97242008-09-30 22:04:30 +000061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063} // end anonymous namespace
64
Dan Gohman089efff2008-05-13 00:00:25 +000065char InternalizePass::ID = 0;
66static RegisterPass<InternalizePass>
67X("internalize", "Internalize Global Symbols");
68
Devang Patel23c4b312008-05-14 20:01:01 +000069InternalizePass::InternalizePass(bool AllButMain)
Dan Gohman26f8c272008-09-04 17:05:41 +000070 : ModulePass(&ID), AllButMain(AllButMain){
Devang Patel23c4b312008-05-14 20:01:01 +000071 if (!APIFile.empty()) // If a filename is specified, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 LoadFile(APIFile.c_str());
Devang Patel23c4b312008-05-14 20:01:01 +000073 if (!APIList.empty()) // If a list is specified, use it as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 ExternalNames.insert(APIList.begin(), APIList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
Duncan Sands4eef18f2009-01-05 20:38:27 +000077InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Dan Gohman26f8c272008-09-04 17:05:41 +000078 : ModulePass(&ID), AllButMain(false){
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 for(std::vector<const char *>::const_iterator itr = exportList.begin();
80 itr != exportList.end(); itr++) {
81 ExternalNames.insert(*itr);
82 }
83}
84
85void InternalizePass::LoadFile(const char *Filename) {
86 // Load the APIFile...
87 std::ifstream In(Filename);
88 if (!In.good()) {
Duncan Sands4eef18f2009-01-05 20:38:27 +000089 cerr << "WARNING: Internalize couldn't load file '" << Filename
Devang Patel23c4b312008-05-14 20:01:01 +000090 << "'! Continuing as if it's empty.\n";
91 return; // Just continue as if the file were empty
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Sands349bf2e2008-10-03 07:36:09 +0000102 CallGraph *CG = getAnalysisToUpdate<CallGraph>();
103 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 if (ExternalNames.empty()) {
Devang Patel23c4b312008-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 Sands4eef18f2009-01-05 20:38:27 +0000108 return false;
Devang Patel23c4b312008-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 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 Function *MainFunc = M.getFunction("main");
114 if (MainFunc == 0 || MainFunc->isDeclaration())
115 return false; // No main found, must be a library...
Duncan Sands4eef18f2009-01-05 20:38:27 +0000116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 // Preserve main, internalize all else.
118 ExternalNames.insert(MainFunc->getName());
119 }
Duncan Sands4eef18f2009-01-05 20:38:27 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 bool Changed = false;
Duncan Sands4eef18f2009-01-05 20:38:27 +0000122
Devang Patel23c4b312008-05-14 20:01:01 +0000123 // Mark all functions not in the api as internal.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
125 if (!I->isDeclaration() && // Function must be defined here
126 !I->hasInternalLinkage() && // Can't already have internal linkage
127 !ExternalNames.count(I->getName())) {// Not marked to keep external?
128 I->setLinkage(GlobalValue::InternalLinkage);
Duncan Sands349bf2e2008-10-03 07:36:09 +0000129 // Remove a callgraph edge from the external node to this function.
130 if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 Changed = true;
132 ++NumFunctions;
133 DOUT << "Internalizing func " << I->getName() << "\n";
134 }
Duncan Sands4eef18f2009-01-05 20:38:27 +0000135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 // Never internalize the llvm.used symbol. It is used to implement
137 // attribute((used)).
138 ExternalNames.insert("llvm.used");
Duncan Sands4eef18f2009-01-05 20:38:27 +0000139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 // Never internalize anchors used by the machine module info, else the info
141 // won't find them. (see MachineModuleInfo.)
142 ExternalNames.insert("llvm.dbg.compile_units");
143 ExternalNames.insert("llvm.dbg.global_variables");
144 ExternalNames.insert("llvm.dbg.subprograms");
145 ExternalNames.insert("llvm.global_ctors");
146 ExternalNames.insert("llvm.global_dtors");
Chris Lattner7ba70ae2007-10-03 03:59:15 +0000147 ExternalNames.insert("llvm.noinline");
Tanya Lattneraa42b9d2007-10-03 17:05:40 +0000148 ExternalNames.insert("llvm.global.annotations");
Duncan Sands4eef18f2009-01-05 20:38:27 +0000149
Devang Patel23c4b312008-05-14 20:01:01 +0000150 // Mark all global variables with initializers that are not in the api as
151 // internal as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
153 I != E; ++I)
154 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
155 !ExternalNames.count(I->getName())) {
156 I->setLinkage(GlobalValue::InternalLinkage);
157 Changed = true;
158 ++NumGlobals;
159 DOUT << "Internalized gvar " << I->getName() << "\n";
160 }
Duncan Sands4eef18f2009-01-05 20:38:27 +0000161
Duncan Sands414d6582009-01-05 21:24:45 +0000162 // Mark all aliases that are not in the api as internal as well.
163 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
164 I != E; ++I)
165 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
166 !ExternalNames.count(I->getName())) {
167 I->setLinkage(GlobalValue::InternalLinkage);
168 Changed = true;
169 ++NumAliases;
170 DOUT << "Internalized alias " << I->getName() << "\n";
171 }
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 return Changed;
174}
175
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +0000176ModulePass *llvm::createInternalizePass(bool AllButMain) {
177 return new InternalizePass(AllButMain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178}
179
180ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
181 return new InternalizePass(el);
182}