blob: 453b4945e7306618758ea4bf5689c948c0872d84 [file] [log] [blame]
Chris Lattner1b94c002002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1b94c002002-04-28 05:43:27 +00009//
10// This pass loops over all of the functions in the input module, looking for a
Chris Lattner47cc3662002-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 Lattner1b94c002002-04-28 05:43:27 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner1631bcb2006-12-19 22:09:18 +000016#define DEBUG_TYPE "internalize"
Chris Lattner99a53f62002-07-24 17:12:05 +000017#include "llvm/Transforms/IPO.h"
Chris Lattner1b94c002002-04-28 05:43:27 +000018#include "llvm/Pass.h"
19#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Reid Spencer557ab152007-02-05 23:32:05 +000021#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
23#include "llvm/ADT/Statistic.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000024#include <fstream>
25#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner1631bcb2006-12-19 22:09:18 +000028STATISTIC(NumFunctions, "Number of functions internalized");
29STATISTIC(NumGlobals , "Number of global vars internalized");
30
Dan Gohmand78c4002008-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
Chris Lattnerb28b6802002-07-23 18:06:35 +000043namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000044 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000045 std::set<std::string> ExternalNames;
Chris Lattner45517ba2005-10-18 06:29:22 +000046 bool DontInternalize;
Chris Lattner44457bb2003-05-22 19:34:49 +000047 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000048 static char ID; // Pass identification, replacement for typeid
Dan Gohman34d442f2007-08-01 15:32:29 +000049 explicit InternalizePass(bool InternalizeEverything = true);
50 explicit InternalizePass(const std::vector <const char *>& exportList);
Chris Lattner8cdc7732006-01-03 19:13:17 +000051 void LoadFile(const char *Filename);
52 virtual bool runOnModule(Module &M);
Chris Lattner47cc3662002-07-30 19:48:44 +000053 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000054} // end anonymous namespace
55
Dan Gohmand78c4002008-05-13 00:00:25 +000056char InternalizePass::ID = 0;
57static RegisterPass<InternalizePass>
58X("internalize", "Internalize Global Symbols");
59
Chris Lattner8cdc7732006-01-03 19:13:17 +000060InternalizePass::InternalizePass(bool InternalizeEverything)
Devang Patel09f162c2007-05-01 21:15:47 +000061 : ModulePass((intptr_t)&ID), DontInternalize(false){
Chris Lattner8cdc7732006-01-03 19:13:17 +000062 if (!APIFile.empty()) // If a filename is specified, use it
63 LoadFile(APIFile.c_str());
64 else if (!APIList.empty()) // Else, if a list is specified, use it.
65 ExternalNames.insert(APIList.begin(), APIList.end());
66 else if (!InternalizeEverything)
67 // Finally, if we're allowed to, internalize all but main.
68 DontInternalize = true;
69}
70
Devang Patelfab49722006-09-13 01:02:26 +000071InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
Devang Patel09f162c2007-05-01 21:15:47 +000072 : ModulePass((intptr_t)&ID), DontInternalize(false){
Devang Patel839d9262006-07-20 17:48:05 +000073 for(std::vector<const char *>::const_iterator itr = exportList.begin();
Anton Korobeynikovfb801512007-04-16 18:10:23 +000074 itr != exportList.end(); itr++) {
Devang Patel839d9262006-07-20 17:48:05 +000075 ExternalNames.insert(*itr);
76 }
77}
78
Chris Lattner8cdc7732006-01-03 19:13:17 +000079void InternalizePass::LoadFile(const char *Filename) {
80 // Load the APIFile...
81 std::ifstream In(Filename);
82 if (!In.good()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000083 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +000084 return; // Do not internalize anything...
85 }
86 while (In) {
87 std::string Symbol;
88 In >> Symbol;
89 if (!Symbol.empty())
90 ExternalNames.insert(Symbol);
91 }
92}
93
94bool InternalizePass::runOnModule(Module &M) {
95 if (DontInternalize) return false;
96
97 // If no list or file of symbols was specified, check to see if there is a
98 // "main" symbol defined in the module. If so, use it, otherwise do not
99 // internalize the module, it must be a library or something.
100 //
101 if (ExternalNames.empty()) {
Reid Spencer1241d6d2007-02-05 21:19:13 +0000102 Function *MainFunc = M.getFunction("main");
Reid Spencer5301e7c2007-01-30 20:08:39 +0000103 if (MainFunc == 0 || MainFunc->isDeclaration())
Chris Lattner8cdc7732006-01-03 19:13:17 +0000104 return false; // No main found, must be a library...
105
106 // Preserve main, internalize all else.
107 ExternalNames.insert(MainFunc->getName());
108 }
109
110 bool Changed = false;
111
112 // Found a main function, mark all functions not named main as internal.
113 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000114 if (!I->isDeclaration() && // Function must be defined here
Chris Lattner8cdc7732006-01-03 19:13:17 +0000115 !I->hasInternalLinkage() && // Can't already have internal linkage
116 !ExternalNames.count(I->getName())) {// Not marked to keep external?
117 I->setLinkage(GlobalValue::InternalLinkage);
118 Changed = true;
119 ++NumFunctions;
Bill Wendling8f13b5c2006-11-26 10:02:32 +0000120 DOUT << "Internalizing func " << I->getName() << "\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +0000121 }
122
123 // Never internalize the llvm.used symbol. It is used to implement
124 // attribute((used)).
125 ExternalNames.insert("llvm.used");
Chris Lattnerd693b792006-01-19 00:40:39 +0000126
Jim Laskeyc56315c2007-01-26 21:22:28 +0000127 // Never internalize anchors used by the machine module info, else the info
128 // won't find them. (see MachineModuleInfo.)
Jim Laskey69effa22006-03-07 20:53:47 +0000129 ExternalNames.insert("llvm.dbg.compile_units");
130 ExternalNames.insert("llvm.dbg.global_variables");
131 ExternalNames.insert("llvm.dbg.subprograms");
Chris Lattner34404e32007-06-06 20:51:41 +0000132 ExternalNames.insert("llvm.global_ctors");
133 ExternalNames.insert("llvm.global_dtors");
Chris Lattnerd66e0cd2007-10-03 03:59:15 +0000134 ExternalNames.insert("llvm.noinline");
Tanya Lattner30f65fe2007-10-03 17:05:40 +0000135 ExternalNames.insert("llvm.global.annotations");
Chris Lattner8cdc7732006-01-03 19:13:17 +0000136
137 // Mark all global variables with initializers as internal as well.
138 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
139 I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000140 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
Chris Lattner8cdc7732006-01-03 19:13:17 +0000141 !ExternalNames.count(I->getName())) {
Chris Lattner8cdc7732006-01-03 19:13:17 +0000142 I->setLinkage(GlobalValue::InternalLinkage);
143 Changed = true;
144 ++NumGlobals;
Bill Wendling8f13b5c2006-11-26 10:02:32 +0000145 DOUT << "Internalized gvar " << I->getName() << "\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +0000146 }
147
148 return Changed;
149}
150
Chris Lattner45517ba2005-10-18 06:29:22 +0000151ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
152 return new InternalizePass(InternalizeEverything);
Chris Lattner1b94c002002-04-28 05:43:27 +0000153}
Devang Patel839d9262006-07-20 17:48:05 +0000154
Devang Pateledd2f992006-07-20 18:03:39 +0000155ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
156 return new InternalizePass(el);
Devang Patel839d9262006-07-20 17:48:05 +0000157}