453b4945e7306618758ea4bf5689c948c0872d84
[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 // APIFile - A file which contains a list of symbols that should not be marked
32 // external.
33 static cl::opt<std::string>
34 APIFile("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.
38 static cl::list<std::string>
39 APIList("internalize-public-api-list", cl::value_desc("list"),
40         cl::desc("A list of symbol names to preserve"),
41         cl::CommaSeparated);
42
43 namespace {
44   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
49     explicit InternalizePass(bool InternalizeEverything = true);
50     explicit InternalizePass(const std::vector <const char *>& exportList);
51     void LoadFile(const char *Filename);
52     virtual bool runOnModule(Module &M);
53   };
54 } // end anonymous namespace
55
56 char InternalizePass::ID = 0;
57 static RegisterPass<InternalizePass>
58 X("internalize", "Internalize Global Symbols");
59
60 InternalizePass::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
71 InternalizePass::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
79 void 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
94 bool 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");
134   ExternalNames.insert("llvm.noinline");
135   ExternalNames.insert("llvm.global.annotations");
136       
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
151 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
152   return new InternalizePass(InternalizeEverything);
153 }
154
155 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
156   return new InternalizePass(el);
157 }