Remove VISIBILITY_HIDDEN from class/struct found inside anonymous namespaces.
[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/Analysis/CallGraph.h"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Module.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/ADT/Statistic.h"
26 #include <fstream>
27 #include <set>
28 using namespace llvm;
29
30 STATISTIC(NumAliases  , "Number of aliases internalized");
31 STATISTIC(NumFunctions, "Number of functions internalized");
32 STATISTIC(NumGlobals  , "Number of global vars internalized");
33
34 // APIFile - A file which contains a list of symbols that should not be marked
35 // external.
36 static cl::opt<std::string>
37 APIFile("internalize-public-api-file", cl::value_desc("filename"),
38         cl::desc("A file containing list of symbol names to preserve"));
39
40 // APIList - A list of symbols that should not be marked internal.
41 static cl::list<std::string>
42 APIList("internalize-public-api-list", cl::value_desc("list"),
43         cl::desc("A list of symbol names to preserve"),
44         cl::CommaSeparated);
45
46 namespace {
47   class InternalizePass : public ModulePass {
48     std::set<std::string> ExternalNames;
49     /// If no api symbols were specified and a main function is defined,
50     /// assume the main function is the only API
51     bool AllButMain;
52   public:
53     static char ID; // Pass identification, replacement for typeid
54     explicit InternalizePass(bool AllButMain = true);
55     explicit InternalizePass(const std::vector <const char *>& exportList);
56     void LoadFile(const char *Filename);
57     virtual bool runOnModule(Module &M);
58
59     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60       AU.setPreservesCFG();
61       AU.addPreserved<CallGraph>();
62     }
63   };
64 } // end anonymous namespace
65
66 char InternalizePass::ID = 0;
67 static RegisterPass<InternalizePass>
68 X("internalize", "Internalize Global Symbols");
69
70 InternalizePass::InternalizePass(bool AllButMain)
71   : ModulePass(&ID), AllButMain(AllButMain){
72   if (!APIFile.empty())           // If a filename is specified, use it.
73     LoadFile(APIFile.c_str());
74   if (!APIList.empty())           // If a list is specified, use it as well.
75     ExternalNames.insert(APIList.begin(), APIList.end());
76 }
77
78 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
79   : ModulePass(&ID), AllButMain(false){
80   for(std::vector<const char *>::const_iterator itr = exportList.begin();
81         itr != exportList.end(); itr++) {
82     ExternalNames.insert(*itr);
83   }
84 }
85
86 void InternalizePass::LoadFile(const char *Filename) {
87   // Load the APIFile...
88   std::ifstream In(Filename);
89   if (!In.good()) {
90     errs() << "WARNING: Internalize couldn't load file '" << Filename
91          << "'! Continuing as if it's empty.\n";
92     return; // Just continue as if the file were empty
93   }
94   while (In) {
95     std::string Symbol;
96     In >> Symbol;
97     if (!Symbol.empty())
98       ExternalNames.insert(Symbol);
99   }
100 }
101
102 bool InternalizePass::runOnModule(Module &M) {
103   CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
104   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
105   
106   if (ExternalNames.empty()) {
107     // Return if we're not in 'all but main' mode and have no external api
108     if (!AllButMain)
109       return false;
110     // If no list or file of symbols was specified, check to see if there is a
111     // "main" symbol defined in the module.  If so, use it, otherwise do not
112     // internalize the module, it must be a library or something.
113     //
114     Function *MainFunc = M.getFunction("main");
115     if (MainFunc == 0 || MainFunc->isDeclaration())
116       return false;  // No main found, must be a library...
117
118     // Preserve main, internalize all else.
119     ExternalNames.insert(MainFunc->getName());
120   }
121
122   bool Changed = false;
123
124   // Mark all functions not in the api as internal.
125   // FIXME: maybe use private linkage?
126   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
127     if (!I->isDeclaration() &&         // Function must be defined here
128         !I->hasLocalLinkage() &&  // Can't already have internal linkage
129         !ExternalNames.count(I->getName())) {// Not marked to keep external?
130       I->setLinkage(GlobalValue::InternalLinkage);
131       // Remove a callgraph edge from the external node to this function.
132       if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
133       Changed = true;
134       ++NumFunctions;
135       DEBUG(errs() << "Internalizing func " << I->getName() << "\n");
136     }
137
138   // Never internalize the llvm.used symbol.  It is used to implement
139   // attribute((used)).
140   // FIXME: Shouldn't this just filter on llvm.metadata section??
141   ExternalNames.insert("llvm.used");
142   ExternalNames.insert("llvm.compiler.used");
143
144   // Never internalize anchors used by the machine module info, else the info
145   // won't find them.  (see MachineModuleInfo.)
146   ExternalNames.insert("llvm.dbg.compile_units");
147   ExternalNames.insert("llvm.dbg.global_variables");
148   ExternalNames.insert("llvm.dbg.subprograms");
149   ExternalNames.insert("llvm.global_ctors");
150   ExternalNames.insert("llvm.global_dtors");
151   ExternalNames.insert("llvm.noinline");
152   ExternalNames.insert("llvm.global.annotations");
153
154   // Mark all global variables with initializers that are not in the api as
155   // internal as well.
156   // FIXME: maybe use private linkage?
157   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
158        I != E; ++I)
159     if (!I->isDeclaration() && !I->hasLocalLinkage() &&
160         !ExternalNames.count(I->getName())) {
161       I->setLinkage(GlobalValue::InternalLinkage);
162       Changed = true;
163       ++NumGlobals;
164       DEBUG(errs() << "Internalized gvar " << I->getName() << "\n");
165     }
166
167   // Mark all aliases that are not in the api as internal as well.
168   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
169        I != E; ++I)
170     if (!I->isDeclaration() && !I->hasInternalLinkage() &&
171         !ExternalNames.count(I->getName())) {
172       I->setLinkage(GlobalValue::InternalLinkage);
173       Changed = true;
174       ++NumAliases;
175       DEBUG(errs() << "Internalized alias " << I->getName() << "\n");
176     }
177
178   return Changed;
179 }
180
181 ModulePass *llvm::createInternalizePass(bool AllButMain) {
182   return new InternalizePass(AllButMain);
183 }
184
185 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
186   return new InternalizePass(el);
187 }