blob: 4a30e1443f8360e5afc5a98d600feac6955bebf3 [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"
Nuno Lopes5de97242008-09-30 22:04:30 +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
29STATISTIC(NumFunctions, "Number of functions internalized");
30STATISTIC(NumGlobals , "Number of global vars internalized");
31
Dan Gohman089efff2008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
46 std::set<std::string> ExternalNames;
Devang Patel23c4b312008-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 public:
51 static char ID; // Pass identification, replacement for typeid
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +000052 explicit InternalizePass(bool AllButMain = true);
Dan Gohman34c280e2007-08-01 15:32:29 +000053 explicit InternalizePass(const std::vector <const char *>& exportList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 void LoadFile(const char *Filename);
55 virtual bool runOnModule(Module &M);
Nuno Lopes5de97242008-09-30 22:04:30 +000056
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesCFG();
59 AU.addPreserved<CallGraph>();
60 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062} // end anonymous namespace
63
Dan Gohman089efff2008-05-13 00:00:25 +000064char InternalizePass::ID = 0;
65static RegisterPass<InternalizePass>
66X("internalize", "Internalize Global Symbols");
67
Devang Patel23c4b312008-05-14 20:01:01 +000068InternalizePass::InternalizePass(bool AllButMain)
Dan Gohman26f8c272008-09-04 17:05:41 +000069 : ModulePass(&ID), AllButMain(AllButMain){
Devang Patel23c4b312008-05-14 20:01:01 +000070 if (!APIFile.empty()) // If a filename is specified, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 LoadFile(APIFile.c_str());
Devang Patel23c4b312008-05-14 20:01:01 +000072 if (!APIList.empty()) // If a list is specified, use it as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 ExternalNames.insert(APIList.begin(), APIList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074}
75
76InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Dan Gohman26f8c272008-09-04 17:05:41 +000077 : ModulePass(&ID), AllButMain(false){
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 for(std::vector<const char *>::const_iterator itr = exportList.begin();
79 itr != exportList.end(); itr++) {
80 ExternalNames.insert(*itr);
81 }
82}
83
84void InternalizePass::LoadFile(const char *Filename) {
85 // Load the APIFile...
86 std::ifstream In(Filename);
87 if (!In.good()) {
Devang Patel23c4b312008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 if (ExternalNames.empty()) {
Devang Patel23c4b312008-05-14 20:01:01 +0000102 // Return if we're not in 'all but main' mode and have no external api
103 if (!AllButMain)
104 return false;
105 // If no list or file of symbols was specified, check to see if there is a
106 // "main" symbol defined in the module. If so, use it, otherwise do not
107 // internalize the module, it must be a library or something.
108 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 Function *MainFunc = M.getFunction("main");
110 if (MainFunc == 0 || MainFunc->isDeclaration())
111 return false; // No main found, must be a library...
112
113 // Preserve main, internalize all else.
114 ExternalNames.insert(MainFunc->getName());
115 }
116
117 bool Changed = false;
118
Devang Patel23c4b312008-05-14 20:01:01 +0000119 // Mark all functions not in the api as internal.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
121 if (!I->isDeclaration() && // Function must be defined here
122 !I->hasInternalLinkage() && // Can't already have internal linkage
123 !ExternalNames.count(I->getName())) {// Not marked to keep external?
124 I->setLinkage(GlobalValue::InternalLinkage);
125 Changed = true;
126 ++NumFunctions;
127 DOUT << "Internalizing func " << I->getName() << "\n";
128 }
129
130 // Never internalize the llvm.used symbol. It is used to implement
131 // attribute((used)).
132 ExternalNames.insert("llvm.used");
133
134 // Never internalize anchors used by the machine module info, else the info
135 // won't find them. (see MachineModuleInfo.)
136 ExternalNames.insert("llvm.dbg.compile_units");
137 ExternalNames.insert("llvm.dbg.global_variables");
138 ExternalNames.insert("llvm.dbg.subprograms");
139 ExternalNames.insert("llvm.global_ctors");
140 ExternalNames.insert("llvm.global_dtors");
Chris Lattner7ba70ae2007-10-03 03:59:15 +0000141 ExternalNames.insert("llvm.noinline");
Tanya Lattneraa42b9d2007-10-03 17:05:40 +0000142 ExternalNames.insert("llvm.global.annotations");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
Devang Patel23c4b312008-05-14 20:01:01 +0000144 // Mark all global variables with initializers that are not in the api as
145 // internal as well.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
147 I != E; ++I)
148 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
149 !ExternalNames.count(I->getName())) {
150 I->setLinkage(GlobalValue::InternalLinkage);
151 Changed = true;
152 ++NumGlobals;
153 DOUT << "Internalized gvar " << I->getName() << "\n";
154 }
155
156 return Changed;
157}
158
Matthijs Kooijman99feb6a2008-06-24 09:14:10 +0000159ModulePass *llvm::createInternalizePass(bool AllButMain) {
160 return new InternalizePass(AllButMain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161}
162
163ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
164 return new InternalizePass(el);
165}