blob: 453b4945e7306618758ea4bf5689c948c0872d84 [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;
46 bool DontInternalize;
47 public:
48 static char ID; // Pass identification, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000049 explicit InternalizePass(bool InternalizeEverything = true);
50 explicit InternalizePass(const std::vector <const char *>& exportList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 void LoadFile(const char *Filename);
52 virtual bool runOnModule(Module &M);
53 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054} // end anonymous namespace
55
Dan Gohman089efff2008-05-13 00:00:25 +000056char InternalizePass::ID = 0;
57static RegisterPass<InternalizePass>
58X("internalize", "Internalize Global Symbols");
59
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060InternalizePass::InternalizePass(bool InternalizeEverything)
61 : ModulePass((intptr_t)&ID), DontInternalize(false){
62 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
71InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
72 : ModulePass((intptr_t)&ID), DontInternalize(false){
73 for(std::vector<const char *>::const_iterator itr = exportList.begin();
74 itr != exportList.end(); itr++) {
75 ExternalNames.insert(*itr);
76 }
77}
78
79void InternalizePass::LoadFile(const char *Filename) {
80 // Load the APIFile...
81 std::ifstream In(Filename);
82 if (!In.good()) {
83 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
84 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()) {
102 Function *MainFunc = M.getFunction("main");
103 if (MainFunc == 0 || MainFunc->isDeclaration())
104 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)
114 if (!I->isDeclaration() && // Function must be defined here
115 !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;
120 DOUT << "Internalizing func " << I->getName() << "\n";
121 }
122
123 // Never internalize the llvm.used symbol. It is used to implement
124 // attribute((used)).
125 ExternalNames.insert("llvm.used");
126
127 // Never internalize anchors used by the machine module info, else the info
128 // won't find them. (see MachineModuleInfo.)
129 ExternalNames.insert("llvm.dbg.compile_units");
130 ExternalNames.insert("llvm.dbg.global_variables");
131 ExternalNames.insert("llvm.dbg.subprograms");
132 ExternalNames.insert("llvm.global_ctors");
133 ExternalNames.insert("llvm.global_dtors");
Chris Lattner7ba70ae2007-10-03 03:59:15 +0000134 ExternalNames.insert("llvm.noinline");
Tanya Lattneraa42b9d2007-10-03 17:05:40 +0000135 ExternalNames.insert("llvm.global.annotations");
Dan Gohmanf17a25c2007-07-18 16:29:46 +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)
140 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
141 !ExternalNames.count(I->getName())) {
142 I->setLinkage(GlobalValue::InternalLinkage);
143 Changed = true;
144 ++NumGlobals;
145 DOUT << "Internalized gvar " << I->getName() << "\n";
146 }
147
148 return Changed;
149}
150
151ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
152 return new InternalizePass(InternalizeEverything);
153}
154
155ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
156 return new InternalizePass(el);
157}