Make several variable declarations static.
[oota-llvm.git] / lib / Transforms / IPO / Internalize.cpp
1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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>
26 using namespace llvm;
27
28 STATISTIC(NumFunctions, "Number of functions internalized");
29 STATISTIC(NumGlobals  , "Number of global vars internalized");
30
31 namespace {
32
33   // APIFile - A file which contains a list of symbols that should not be marked
34   // external.
35   static cl::opt<std::string>
36   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.
40   static cl::list<std::string>
41   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
50     explicit InternalizePass(bool InternalizeEverything = true);
51     explicit InternalizePass(const std::vector <const char *>& exportList);
52     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
59 InternalizePass::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
70 InternalizePass::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
78 void 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
93 bool 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");
133   ExternalNames.insert("llvm.noinline");
134   ExternalNames.insert("llvm.global.annotations");
135       
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
150 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
151   return new InternalizePass(InternalizeEverything);
152 }
153
154 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
155   return new InternalizePass(el);
156 }