blob: 5ad8ad81eed21a437800edc2ac26fc7888d54fd6 [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"
Daniel Dunbar005975c2009-07-25 00:23:56 +000024#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/ADT/Statistic.h"
26#include <fstream>
27#include <set>
28using namespace llvm;
29
Duncan Sands414d6582009-01-05 21:24:45 +000030STATISTIC(NumAliases , "Number of aliases internalized");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031STATISTIC(NumFunctions, "Number of functions internalized");
32STATISTIC(NumGlobals , "Number of global vars internalized");
33
Dan Gohman089efff2008-05-13 00:00:25 +000034// APIFile - A file which contains a list of symbols that should not be marked
35// external.
36static cl::opt<std::string>
37APIFile("internalize-public-api-file", cl::value_desc("filename"),
38 cl::desc("A file containing list of symbol names to preserve"));
39
40// APIList - A list of symbols that should not be marked internal.
41static cl::list<std::string>
42APIList("internalize-public-api-list", cl::value_desc("list"),
43 cl::desc("A list of symbol names to preserve"),
44 cl::CommaSeparated);
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
48 std::set<std::string> ExternalNames;
Devang Patel23c4b312008-05-14 20:01:01 +000049 /// If no api symbols were specified and a main function is defined,
50 /// assume the main function is the only API
51 bool AllButMain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 public:
53 static char ID; // Pass identification, replacement for typeid
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +000054 explicit InternalizePass(bool AllButMain = true);
Dan Gohman34c280e2007-08-01 15:32:29 +000055 explicit InternalizePass(const std::vector <const char *>& exportList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 void LoadFile(const char *Filename);
57 virtual bool runOnModule(Module &M);
Nuno Lopes5de97242008-09-30 22:04:30 +000058
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesCFG();
Duncan Sands349bf2e2008-10-03 07:36:09 +000061 AU.addPreserved<CallGraph>();
Nuno Lopes5de97242008-09-30 22:04:30 +000062 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064} // end anonymous namespace
65
Dan Gohman089efff2008-05-13 00:00:25 +000066char InternalizePass::ID = 0;
67static RegisterPass<InternalizePass>
68X("internalize", "Internalize Global Symbols");
69
Devang Patel23c4b312008-05-14 20:01:01 +000070InternalizePass::InternalizePass(bool AllButMain)
Dan Gohman26f8c272008-09-04 17:05:41 +000071 : ModulePass(&ID), AllButMain(AllButMain){
Devang Patel23c4b312008-05-14 20:01:01 +000072 if (!APIFile.empty()) // If a filename is specified, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 LoadFile(APIFile.c_str());
Devang Patel23c4b312008-05-14 20:01:01 +000074 if (!APIList.empty()) // If a list is specified, use it as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 ExternalNames.insert(APIList.begin(), APIList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076}
77
Duncan Sands4eef18f2009-01-05 20:38:27 +000078InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Dan Gohman26f8c272008-09-04 17:05:41 +000079 : ModulePass(&ID), AllButMain(false){
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 for(std::vector<const char *>::const_iterator itr = exportList.begin();
81 itr != exportList.end(); itr++) {
82 ExternalNames.insert(*itr);
83 }
84}
85
86void InternalizePass::LoadFile(const char *Filename) {
87 // Load the APIFile...
88 std::ifstream In(Filename);
89 if (!In.good()) {
Duncan Sands4eef18f2009-01-05 20:38:27 +000090 cerr << "WARNING: Internalize couldn't load file '" << Filename
Devang Patel23c4b312008-05-14 20:01:01 +000091 << "'! Continuing as if it's empty.\n";
92 return; // Just continue as if the file were empty
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 }
94 while (In) {
95 std::string Symbol;
96 In >> Symbol;
97 if (!Symbol.empty())
98 ExternalNames.insert(Symbol);
99 }
100}
101
102bool InternalizePass::runOnModule(Module &M) {
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000103 CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
Duncan Sands349bf2e2008-10-03 07:36:09 +0000104 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
Owen Andersone1f1f822009-07-16 18:04:31 +0000105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 if (ExternalNames.empty()) {
Devang Patel23c4b312008-05-14 20:01:01 +0000107 // Return if we're not in 'all but main' mode and have no external api
108 if (!AllButMain)
Duncan Sands4eef18f2009-01-05 20:38:27 +0000109 return false;
Devang Patel23c4b312008-05-14 20:01:01 +0000110 // If no list or file of symbols was specified, check to see if there is a
111 // "main" symbol defined in the module. If so, use it, otherwise do not
112 // internalize the module, it must be a library or something.
113 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 Function *MainFunc = M.getFunction("main");
115 if (MainFunc == 0 || MainFunc->isDeclaration())
116 return false; // No main found, must be a library...
Duncan Sands4eef18f2009-01-05 20:38:27 +0000117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // Preserve main, internalize all else.
119 ExternalNames.insert(MainFunc->getName());
120 }
Duncan Sands4eef18f2009-01-05 20:38:27 +0000121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 bool Changed = false;
Duncan Sands4eef18f2009-01-05 20:38:27 +0000123
Devang Patel23c4b312008-05-14 20:01:01 +0000124 // Mark all functions not in the api as internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000125 // FIXME: maybe use private linkage?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
127 if (!I->isDeclaration() && // Function must be defined here
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000128 !I->hasLocalLinkage() && // Can't already have internal linkage
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 !ExternalNames.count(I->getName())) {// Not marked to keep external?
130 I->setLinkage(GlobalValue::InternalLinkage);
Duncan Sands349bf2e2008-10-03 07:36:09 +0000131 // Remove a callgraph edge from the external node to this function.
132 if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 Changed = true;
134 ++NumFunctions;
Daniel Dunbar005975c2009-07-25 00:23:56 +0000135 DEBUG(errs() << "Internalizing func " << I->getName() << "\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 }
Duncan Sands4eef18f2009-01-05 20:38:27 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 // Never internalize the llvm.used symbol. It is used to implement
139 // attribute((used)).
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000140 // FIXME: Shouldn't this just filter on llvm.metadata section??
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 ExternalNames.insert("llvm.used");
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000142 ExternalNames.insert("llvm.compiler.used");
Duncan Sands4eef18f2009-01-05 20:38:27 +0000143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 // Never internalize anchors used by the machine module info, else the info
145 // won't find them. (see MachineModuleInfo.)
146 ExternalNames.insert("llvm.dbg.compile_units");
147 ExternalNames.insert("llvm.dbg.global_variables");
148 ExternalNames.insert("llvm.dbg.subprograms");
149 ExternalNames.insert("llvm.global_ctors");
150 ExternalNames.insert("llvm.global_dtors");
Chris Lattner7ba70ae2007-10-03 03:59:15 +0000151 ExternalNames.insert("llvm.noinline");
Tanya Lattneraa42b9d2007-10-03 17:05:40 +0000152 ExternalNames.insert("llvm.global.annotations");
Duncan Sands4eef18f2009-01-05 20:38:27 +0000153
Devang Patel23c4b312008-05-14 20:01:01 +0000154 // Mark all global variables with initializers that are not in the api as
155 // internal as well.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000156 // FIXME: maybe use private linkage?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
158 I != E; ++I)
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000159 if (!I->isDeclaration() && !I->hasLocalLinkage() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 !ExternalNames.count(I->getName())) {
161 I->setLinkage(GlobalValue::InternalLinkage);
162 Changed = true;
163 ++NumGlobals;
Daniel Dunbar005975c2009-07-25 00:23:56 +0000164 DEBUG(errs() << "Internalized gvar " << I->getName() << "\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 }
Duncan Sands4eef18f2009-01-05 20:38:27 +0000166
Duncan Sands414d6582009-01-05 21:24:45 +0000167 // Mark all aliases that are not in the api as internal as well.
168 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
169 I != E; ++I)
170 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
171 !ExternalNames.count(I->getName())) {
172 I->setLinkage(GlobalValue::InternalLinkage);
173 Changed = true;
174 ++NumAliases;
Daniel Dunbar005975c2009-07-25 00:23:56 +0000175 DEBUG(errs() << "Internalized alias " << I->getName() << "\n");
Duncan Sands414d6582009-01-05 21:24:45 +0000176 }
177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 return Changed;
179}
180
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +0000181ModulePass *llvm::createInternalizePass(bool AllButMain) {
182 return new InternalizePass(AllButMain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183}
184
185ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
186 return new InternalizePass(el);
187}