Simplify internalize pass. Add test case.
[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     /// If no api symbols were specified and a main function is defined,
47     /// assume the main function is the only API
48     bool AllButMain;
49   public:
50     static char ID; // Pass identification, replacement for typeid
51     explicit InternalizePass(bool InternalizeEverything = true);
52     explicit InternalizePass(const std::vector <const char *>& exportList);
53     void LoadFile(const char *Filename);
54     virtual bool runOnModule(Module &M);
55   };
56 } // end anonymous namespace
57
58 char InternalizePass::ID = 0;
59 static RegisterPass<InternalizePass>
60 X("internalize", "Internalize Global Symbols");
61
62 InternalizePass::InternalizePass(bool AllButMain)
63   : ModulePass((intptr_t)&ID), AllButMain(AllButMain){
64   if (!APIFile.empty())           // If a filename is specified, use it.
65     LoadFile(APIFile.c_str());
66   if (!APIList.empty())           // If a list is specified, use it as well.
67     ExternalNames.insert(APIList.begin(), APIList.end());
68 }
69
70 InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 
71   : ModulePass((intptr_t)&ID), AllButMain(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 
83          << "'! Continuing as if it's empty.\n";
84     return; // Just continue as if the file were empty
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 (ExternalNames.empty()) {
96     // Return if we're not in 'all but main' mode and have no external api
97     if (!AllButMain)
98       return false; 
99     // If no list or file of symbols was specified, check to see if there is a
100     // "main" symbol defined in the module.  If so, use it, otherwise do not
101     // internalize the module, it must be a library or something.
102     //
103     Function *MainFunc = M.getFunction("main");
104     if (MainFunc == 0 || MainFunc->isDeclaration())
105       return false;  // No main found, must be a library...
106     
107     // Preserve main, internalize all else.
108     ExternalNames.insert(MainFunc->getName());
109   }
110   
111   bool Changed = false;
112   
113   // Mark all functions not in the api as internal.
114   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
115     if (!I->isDeclaration() &&         // Function must be defined here
116         !I->hasInternalLinkage() &&  // Can't already have internal linkage
117         !ExternalNames.count(I->getName())) {// Not marked to keep external?
118       I->setLinkage(GlobalValue::InternalLinkage);
119       Changed = true;
120       ++NumFunctions;
121       DOUT << "Internalizing func " << I->getName() << "\n";
122     }
123   
124   // Never internalize the llvm.used symbol.  It is used to implement
125   // attribute((used)).
126   ExternalNames.insert("llvm.used");
127   
128   // Never internalize anchors used by the machine module info, else the info
129   // won't find them.  (see MachineModuleInfo.)
130   ExternalNames.insert("llvm.dbg.compile_units");
131   ExternalNames.insert("llvm.dbg.global_variables");
132   ExternalNames.insert("llvm.dbg.subprograms");
133   ExternalNames.insert("llvm.global_ctors");
134   ExternalNames.insert("llvm.global_dtors");
135   ExternalNames.insert("llvm.noinline");
136   ExternalNames.insert("llvm.global.annotations");
137       
138   // Mark all global variables with initializers that are not in the api as
139   // internal as well.
140   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
141        I != E; ++I)
142     if (!I->isDeclaration() && !I->hasInternalLinkage() &&
143         !ExternalNames.count(I->getName())) {
144       I->setLinkage(GlobalValue::InternalLinkage);
145       Changed = true;
146       ++NumGlobals;
147       DOUT << "Internalized gvar " << I->getName() << "\n";
148     }
149       
150   return Changed;
151 }
152
153 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
154   return new InternalizePass(InternalizeEverything);
155 }
156
157 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
158   return new InternalizePass(el);
159 }