Removed tabs everywhere except autogenerated & external files. Add make
[oota-llvm.git] / lib / Transforms / IPO / Internalize.cpp
1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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   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   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     InternalizePass(bool InternalizeEverything = true);
50     InternalizePass(const std::vector <const char *>& exportList);
51     void LoadFile(const char *Filename);
52     virtual bool runOnModule(Module &M);
53   };
54   RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
55 } // end anonymous namespace
56
57 InternalizePass::InternalizePass(bool InternalizeEverything) 
58   : DontInternalize(false){
59   if (!APIFile.empty())           // If a filename is specified, use it
60     LoadFile(APIFile.c_str());
61   else if (!APIList.empty())      // Else, if a list is specified, use it.
62     ExternalNames.insert(APIList.begin(), APIList.end());
63   else if (!InternalizeEverything)
64     // Finally, if we're allowed to, internalize all but main.
65     DontInternalize = true;
66 }
67
68 InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 
69   : DontInternalize(false){
70   for(std::vector<const char *>::const_iterator itr = exportList.begin();
71         itr != exportList.end(); itr++) {
72     ExternalNames.insert(*itr);
73   }
74 }
75
76 void InternalizePass::LoadFile(const char *Filename) {
77   // Load the APIFile...
78   std::ifstream In(Filename);
79   if (!In.good()) {
80     cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
81     return;   // Do not internalize anything...
82   }
83   while (In) {
84     std::string Symbol;
85     In >> Symbol;
86     if (!Symbol.empty())
87       ExternalNames.insert(Symbol);
88   }
89 }
90
91 bool InternalizePass::runOnModule(Module &M) {
92   if (DontInternalize) return false;
93   
94   // If no list or file of symbols was specified, check to see if there is a
95   // "main" symbol defined in the module.  If so, use it, otherwise do not
96   // internalize the module, it must be a library or something.
97   //
98   if (ExternalNames.empty()) {
99     Function *MainFunc = M.getFunction("main");
100     if (MainFunc == 0 || MainFunc->isDeclaration())
101       return false;  // No main found, must be a library...
102     
103     // Preserve main, internalize all else.
104     ExternalNames.insert(MainFunc->getName());
105   }
106   
107   bool Changed = false;
108   
109   // Found a main function, mark all functions not named main as internal.
110   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
111     if (!I->isDeclaration() &&         // Function must be defined here
112         !I->hasInternalLinkage() &&  // Can't already have internal linkage
113         !ExternalNames.count(I->getName())) {// Not marked to keep external?
114       I->setLinkage(GlobalValue::InternalLinkage);
115       Changed = true;
116       ++NumFunctions;
117       DOUT << "Internalizing func " << I->getName() << "\n";
118     }
119   
120   // Never internalize the llvm.used symbol.  It is used to implement
121   // attribute((used)).
122   ExternalNames.insert("llvm.used");
123   
124   // Never internalize anchors used by the machine module info, else the info
125   // won't find them.  (see MachineModuleInfo.)
126   ExternalNames.insert("llvm.dbg.compile_units");
127   ExternalNames.insert("llvm.dbg.global_variables");
128   ExternalNames.insert("llvm.dbg.subprograms");
129       
130   // Mark all global variables with initializers as internal as well.
131   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
132        I != E; ++I)
133     if (!I->isDeclaration() && !I->hasInternalLinkage() &&
134         !ExternalNames.count(I->getName())) {
135       // Special case handling of the global ctor and dtor list.  When we
136       // internalize it, we mark it constant, which allows elimination of
137       // the list if it's empty.
138       //
139       if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
140                                        I->getName() == "llvm.global_dtors")) {
141         // If the global ctors/dtors list has no uses, do not internalize it, as
142         // there is no __main in this program, so the asmprinter should handle
143         // it.
144         if (I->use_empty()) continue;
145  
146         // Otherwise, also mark the list constant, as we know that it will not
147         // be mutated any longer, and the makes simple IPO xforms automatically
148         // better.
149         I->setConstant(true);
150       }
151       
152       I->setLinkage(GlobalValue::InternalLinkage);
153       Changed = true;
154       ++NumGlobals;
155       DOUT << "Internalized gvar " << I->getName() << "\n";
156     }
157       
158   return Changed;
159 }
160
161 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
162   return new InternalizePass(InternalizeEverything);
163 }
164
165 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
166   return new InternalizePass(el);
167 }