Change inferred getCast into specific getCast. Passes all tests.
[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 #include "llvm/Transforms/IPO.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/Statistic.h"
22 #include <fstream>
23 #include <set>
24 using namespace llvm;
25
26 namespace {
27   Statistic NumFunctions("internalize", "Number of functions internalized");
28   Statistic NumGlobals  ("internalize", "Number of global vars internalized");
29
30   // APIFile - A file which contains a list of symbols that should not be marked
31   // external.
32   cl::opt<std::string>
33   APIFile("internalize-public-api-file", cl::value_desc("filename"),
34           cl::desc("A file containing list of symbol names to preserve"));
35
36   // APIList - A list of symbols that should not be marked internal.
37   cl::list<std::string>
38   APIList("internalize-public-api-list", cl::value_desc("list"),
39           cl::desc("A list of symbol names to preserve"),
40           cl::CommaSeparated);
41
42   class InternalizePass : public ModulePass {
43     std::set<std::string> ExternalNames;
44     bool DontInternalize;
45   public:
46     InternalizePass(bool InternalizeEverything = true);
47     InternalizePass(const std::vector <const char *>& exportList);
48     void LoadFile(const char *Filename);
49     virtual bool runOnModule(Module &M);
50   };
51   RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
52 } // end anonymous namespace
53
54 InternalizePass::InternalizePass(bool InternalizeEverything) 
55   : DontInternalize(false){
56   if (!APIFile.empty())           // If a filename is specified, use it
57     LoadFile(APIFile.c_str());
58   else if (!APIList.empty())      // Else, if a list is specified, use it.
59     ExternalNames.insert(APIList.begin(), APIList.end());
60   else if (!InternalizeEverything)
61     // Finally, if we're allowed to, internalize all but main.
62     DontInternalize = true;
63 }
64
65 InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 
66   : DontInternalize(false){
67   for(std::vector<const char *>::const_iterator itr = exportList.begin();
68         itr != exportList.end(); itr++) {
69     ExternalNames.insert(*itr);
70   }
71 }
72
73 void InternalizePass::LoadFile(const char *Filename) {
74   // Load the APIFile...
75   std::ifstream In(Filename);
76   if (!In.good()) {
77     cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
78     return;   // Do not internalize anything...
79   }
80   while (In) {
81     std::string Symbol;
82     In >> Symbol;
83     if (!Symbol.empty())
84       ExternalNames.insert(Symbol);
85   }
86 }
87
88 bool InternalizePass::runOnModule(Module &M) {
89   if (DontInternalize) return false;
90   
91   // If no list or file of symbols was specified, check to see if there is a
92   // "main" symbol defined in the module.  If so, use it, otherwise do not
93   // internalize the module, it must be a library or something.
94   //
95   if (ExternalNames.empty()) {
96     Function *MainFunc = M.getMainFunction();
97     if (MainFunc == 0 || MainFunc->isExternal())
98       return false;  // No main found, must be a library...
99     
100     // Preserve main, internalize all else.
101     ExternalNames.insert(MainFunc->getName());
102   }
103   
104   bool Changed = false;
105   
106   // Found a main function, mark all functions not named main as internal.
107   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
108     if (!I->isExternal() &&         // Function must be defined here
109         !I->hasInternalLinkage() &&  // Can't already have internal linkage
110         !ExternalNames.count(I->getName())) {// Not marked to keep external?
111       I->setLinkage(GlobalValue::InternalLinkage);
112       Changed = true;
113       ++NumFunctions;
114       DOUT << "Internalizing func " << I->getName() << "\n";
115     }
116   
117   // Never internalize the llvm.used symbol.  It is used to implement
118   // attribute((used)).
119   ExternalNames.insert("llvm.used");
120   
121   // Never internalize anchors used by the debugger, else the debugger won't
122   // find them.  (see MachineDebugInfo.)
123   ExternalNames.insert("llvm.dbg.compile_units");
124   ExternalNames.insert("llvm.dbg.global_variables");
125   ExternalNames.insert("llvm.dbg.subprograms");
126       
127   // Mark all global variables with initializers as internal as well.
128   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
129        I != E; ++I)
130     if (!I->isExternal() && !I->hasInternalLinkage() &&
131         !ExternalNames.count(I->getName())) {
132       // Special case handling of the global ctor and dtor list.  When we
133       // internalize it, we mark it constant, which allows elimination of
134       // the list if it's empty.
135       //
136       if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
137                                        I->getName() == "llvm.global_dtors")) {
138         // If the global ctors/dtors list has no uses, do not internalize it, as
139         // there is no __main in this program, so the asmprinter should handle
140         // it.
141         if (I->use_empty()) continue;
142  
143         // Otherwise, also mark the list constant, as we know that it will not
144         // be mutated any longer, and the makes simple IPO xforms automatically
145         // better.
146         I->setConstant(true);
147       }
148       
149       I->setLinkage(GlobalValue::InternalLinkage);
150       Changed = true;
151       ++NumGlobals;
152       DOUT << "Internalized gvar " << I->getName() << "\n";
153     }
154       
155   return Changed;
156 }
157
158 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
159   return new InternalizePass(InternalizeEverything);
160 }
161
162 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
163   return new InternalizePass(el);
164 }