blob: 47fe5fe85ad3b31969119e76273a4b03effe046d [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"
17#include "llvm/Transforms/IPO.h"
18#include "llvm/Pass.h"
19#include "llvm/Module.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/ADT/Statistic.h"
24#include <fstream>
25#include <set>
26using namespace llvm;
27
28STATISTIC(NumFunctions, "Number of functions internalized");
29STATISTIC(NumGlobals , "Number of global vars internalized");
30
Dan Gohman089efff2008-05-13 00:00:25 +000031// APIFile - A file which contains a list of symbols that should not be marked
32// external.
33static cl::opt<std::string>
34APIFile("internalize-public-api-file", cl::value_desc("filename"),
35 cl::desc("A file containing list of symbol names to preserve"));
36
37// APIList - A list of symbols that should not be marked internal.
38static cl::list<std::string>
39APIList("internalize-public-api-list", cl::value_desc("list"),
40 cl::desc("A list of symbol names to preserve"),
41 cl::CommaSeparated);
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
45 std::set<std::string> ExternalNames;
Devang Patel23c4b312008-05-14 20:01:01 +000046 /// If no api symbols were specified and a main function is defined,
47 /// assume the main function is the only API
48 bool AllButMain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 public:
50 static char ID; // Pass identification, replacement for typeid
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +000051 explicit InternalizePass(bool AllButMain = true);
Dan Gohman34c280e2007-08-01 15:32:29 +000052 explicit InternalizePass(const std::vector <const char *>& exportList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 void LoadFile(const char *Filename);
54 virtual bool runOnModule(Module &M);
Nuno Lopes5de97242008-09-30 22:04:30 +000055
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesCFG();
Nuno Lopes5de97242008-09-30 22:04:30 +000058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060} // end anonymous namespace
61
Dan Gohman089efff2008-05-13 00:00:25 +000062char InternalizePass::ID = 0;
63static RegisterPass<InternalizePass>
64X("internalize", "Internalize Global Symbols");
65
Devang Patel23c4b312008-05-14 20:01:01 +000066InternalizePass::InternalizePass(bool AllButMain)
Dan Gohman26f8c272008-09-04 17:05:41 +000067 : ModulePass(&ID), AllButMain(AllButMain){
Devang Patel23c4b312008-05-14 20:01:01 +000068 if (!APIFile.empty()) // If a filename is specified, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 LoadFile(APIFile.c_str());
Devang Patel23c4b312008-05-14 20:01:01 +000070 if (!APIList.empty()) // If a list is specified, use it as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 ExternalNames.insert(APIList.begin(), APIList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072}
73
74InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Dan Gohman26f8c272008-09-04 17:05:41 +000075 : ModulePass(&ID), AllButMain(false){
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 for(std::vector<const char *>::const_iterator itr = exportList.begin();
77 itr != exportList.end(); itr++) {
78 ExternalNames.insert(*itr);
79 }
80}
81
82void InternalizePass::LoadFile(const char *Filename) {
83 // Load the APIFile...
84 std::ifstream In(Filename);
85 if (!In.good()) {
Devang Patel23c4b312008-05-14 20:01:01 +000086 cerr << "WARNING: Internalize couldn't load file '" << Filename
87 << "'! Continuing as if it's empty.\n";
88 return; // Just continue as if the file were empty
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 }
90 while (In) {
91 std::string Symbol;
92 In >> Symbol;
93 if (!Symbol.empty())
94 ExternalNames.insert(Symbol);
95 }
96}
97
98bool InternalizePass::runOnModule(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 if (ExternalNames.empty()) {
Devang Patel23c4b312008-05-14 20:01:01 +0000100 // Return if we're not in 'all but main' mode and have no external api
101 if (!AllButMain)
102 return false;
103 // If no list or file of symbols was specified, check to see if there is a
104 // "main" symbol defined in the module. If so, use it, otherwise do not
105 // internalize the module, it must be a library or something.
106 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 Function *MainFunc = M.getFunction("main");
108 if (MainFunc == 0 || MainFunc->isDeclaration())
109 return false; // No main found, must be a library...
110
111 // Preserve main, internalize all else.
112 ExternalNames.insert(MainFunc->getName());
113 }
114
115 bool Changed = false;
116
Devang Patel23c4b312008-05-14 20:01:01 +0000117 // Mark all functions not in the api as internal.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
119 if (!I->isDeclaration() && // Function must be defined here
120 !I->hasInternalLinkage() && // Can't already have internal linkage
121 !ExternalNames.count(I->getName())) {// Not marked to keep external?
122 I->setLinkage(GlobalValue::InternalLinkage);
123 Changed = true;
124 ++NumFunctions;
125 DOUT << "Internalizing func " << I->getName() << "\n";
126 }
127
128 // Never internalize the llvm.used symbol. It is used to implement
129 // attribute((used)).
130 ExternalNames.insert("llvm.used");
131
132 // Never internalize anchors used by the machine module info, else the info
133 // won't find them. (see MachineModuleInfo.)
134 ExternalNames.insert("llvm.dbg.compile_units");
135 ExternalNames.insert("llvm.dbg.global_variables");
136 ExternalNames.insert("llvm.dbg.subprograms");
137 ExternalNames.insert("llvm.global_ctors");
138 ExternalNames.insert("llvm.global_dtors");
Chris Lattner7ba70ae2007-10-03 03:59:15 +0000139 ExternalNames.insert("llvm.noinline");
Tanya Lattneraa42b9d2007-10-03 17:05:40 +0000140 ExternalNames.insert("llvm.global.annotations");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
Devang Patel23c4b312008-05-14 20:01:01 +0000142 // Mark all global variables with initializers that are not in the api as
143 // internal as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
145 I != E; ++I)
146 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
147 !ExternalNames.count(I->getName())) {
148 I->setLinkage(GlobalValue::InternalLinkage);
149 Changed = true;
150 ++NumGlobals;
151 DOUT << "Internalized gvar " << I->getName() << "\n";
152 }
153
154 return Changed;
155}
156
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +0000157ModulePass *llvm::createInternalizePass(bool AllButMain) {
158 return new InternalizePass(AllButMain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159}
160
161ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
162 return new InternalizePass(el);
163}