blob: f3adb5692f17bbab73317b129d6a2097fa82e910 [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
31namespace {
32
33 // APIFile - A file which contains a list of symbols that should not be marked
34 // external.
Dan Gohman2c37da52008-05-06 01:53:16 +000035 static cl::opt<std::string>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 APIFile("internalize-public-api-file", cl::value_desc("filename"),
37 cl::desc("A file containing list of symbol names to preserve"));
38
39 // APIList - A list of symbols that should not be marked internal.
Dan Gohman2c37da52008-05-06 01:53:16 +000040 static cl::list<std::string>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 APIList("internalize-public-api-list", cl::value_desc("list"),
42 cl::desc("A list of symbol names to preserve"),
43 cl::CommaSeparated);
44
45 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
46 std::set<std::string> ExternalNames;
47 bool DontInternalize;
48 public:
49 static char ID; // Pass identification, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000050 explicit InternalizePass(bool InternalizeEverything = true);
51 explicit InternalizePass(const std::vector <const char *>& exportList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 void LoadFile(const char *Filename);
53 virtual bool runOnModule(Module &M);
54 };
55 char InternalizePass::ID = 0;
56 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
57} // end anonymous namespace
58
59InternalizePass::InternalizePass(bool InternalizeEverything)
60 : ModulePass((intptr_t)&ID), DontInternalize(false){
61 if (!APIFile.empty()) // If a filename is specified, use it
62 LoadFile(APIFile.c_str());
63 else if (!APIList.empty()) // Else, if a list is specified, use it.
64 ExternalNames.insert(APIList.begin(), APIList.end());
65 else if (!InternalizeEverything)
66 // Finally, if we're allowed to, internalize all but main.
67 DontInternalize = true;
68}
69
70InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
71 : ModulePass((intptr_t)&ID), DontInternalize(false){
72 for(std::vector<const char *>::const_iterator itr = exportList.begin();
73 itr != exportList.end(); itr++) {
74 ExternalNames.insert(*itr);
75 }
76}
77
78void InternalizePass::LoadFile(const char *Filename) {
79 // Load the APIFile...
80 std::ifstream In(Filename);
81 if (!In.good()) {
82 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
83 return; // Do not internalize anything...
84 }
85 while (In) {
86 std::string Symbol;
87 In >> Symbol;
88 if (!Symbol.empty())
89 ExternalNames.insert(Symbol);
90 }
91}
92
93bool InternalizePass::runOnModule(Module &M) {
94 if (DontInternalize) return false;
95
96 // If no list or file of symbols was specified, check to see if there is a
97 // "main" symbol defined in the module. If so, use it, otherwise do not
98 // internalize the module, it must be a library or something.
99 //
100 if (ExternalNames.empty()) {
101 Function *MainFunc = M.getFunction("main");
102 if (MainFunc == 0 || MainFunc->isDeclaration())
103 return false; // No main found, must be a library...
104
105 // Preserve main, internalize all else.
106 ExternalNames.insert(MainFunc->getName());
107 }
108
109 bool Changed = false;
110
111 // Found a main function, mark all functions not named main as internal.
112 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
113 if (!I->isDeclaration() && // Function must be defined here
114 !I->hasInternalLinkage() && // Can't already have internal linkage
115 !ExternalNames.count(I->getName())) {// Not marked to keep external?
116 I->setLinkage(GlobalValue::InternalLinkage);
117 Changed = true;
118 ++NumFunctions;
119 DOUT << "Internalizing func " << I->getName() << "\n";
120 }
121
122 // Never internalize the llvm.used symbol. It is used to implement
123 // attribute((used)).
124 ExternalNames.insert("llvm.used");
125
126 // Never internalize anchors used by the machine module info, else the info
127 // won't find them. (see MachineModuleInfo.)
128 ExternalNames.insert("llvm.dbg.compile_units");
129 ExternalNames.insert("llvm.dbg.global_variables");
130 ExternalNames.insert("llvm.dbg.subprograms");
131 ExternalNames.insert("llvm.global_ctors");
132 ExternalNames.insert("llvm.global_dtors");
Chris Lattner7ba70ae2007-10-03 03:59:15 +0000133 ExternalNames.insert("llvm.noinline");
Tanya Lattneraa42b9d2007-10-03 17:05:40 +0000134 ExternalNames.insert("llvm.global.annotations");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
136 // Mark all global variables with initializers as internal as well.
137 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
138 I != E; ++I)
139 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
140 !ExternalNames.count(I->getName())) {
141 I->setLinkage(GlobalValue::InternalLinkage);
142 Changed = true;
143 ++NumGlobals;
144 DOUT << "Internalized gvar " << I->getName() << "\n";
145 }
146
147 return Changed;
148}
149
150ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
151 return new InternalizePass(InternalizeEverything);
152}
153
154ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
155 return new InternalizePass(el);
156}