7bca45970a96e3133aab6fe5767c1fff59c4bfbc
[oota-llvm.git] / lib / Transforms / IPO / Internalize.cpp
1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
2 //
3 // This pass loops over all of the functions in the input module, looking for a
4 // main function.  If a main function is found, all other functions and all
5 // global variables with initializers are marked as internal.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/IPO.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Module.h"
12 #include "Support/CommandLine.h"
13 #include "Support/Debug.h"
14 #include "Support/Statistic.h"
15 #include <fstream>
16 #include <set>
17
18 namespace {
19   Statistic<> NumFunctions("internalize", "Number of functions internalized");
20   Statistic<> NumGlobals  ("internalize", "Number of global vars internalized");
21
22   // APIFile - A file which contains a list of symbols that should not be marked
23   // external.
24   cl::opt<std::string>
25   APIFile("internalize-public-api-file", cl::value_desc("filename"),
26           cl::desc("A file containing list of symbol names to preserve"));
27
28   // APIList - A list of symbols that should not be marked internal.
29   cl::list<std::string>
30   APIList("internalize-public-api-list", cl::value_desc("list"),
31           cl::desc("A list of symbol names to preserve"),
32           cl::CommaSeparated);
33  
34   class InternalizePass : public Pass {
35     std::set<std::string> ExternalNames;
36   public:
37     InternalizePass() {
38       if (!APIFile.empty())           // If a filename is specified, use it
39         LoadFile(APIFile.c_str());
40       else                            // Else, if a list is specified, use it.
41         ExternalNames.insert(APIList.begin(), APIList.end());
42     }
43
44     void LoadFile(const char *Filename) {
45       // Load the APIFile...
46       std::ifstream In(Filename);
47       if (!In.good()) {
48         std::cerr << "WARNING: Internalize couldn't load file '" << Filename
49                   << "'!\n";
50         return;   // Do not internalize anything...
51       }
52       while (In) {
53         std::string Symbol;
54         In >> Symbol;
55         if (!Symbol.empty())
56           ExternalNames.insert(Symbol);
57       }
58     }
59
60     virtual bool run(Module &M) {
61       // If no list or file of symbols was specified, check to see if there is a
62       // "main" symbol defined in the module.  If so, use it, otherwise do not
63       // internalize the module, it must be a library or something.
64       //
65       if (ExternalNames.empty()) {
66         Function *MainFunc = M.getMainFunction();
67         if (MainFunc == 0 || MainFunc->isExternal())
68           return false;  // No main found, must be a library...
69
70         // Preserve main, internalize all else.
71         ExternalNames.insert(MainFunc->getName());
72       }
73
74       bool Changed = false;
75       
76       // Found a main function, mark all functions not named main as internal.
77       for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
78         if (!I->isExternal() &&         // Function must be defined here
79             !I->hasInternalLinkage() &&  // Can't already have internal linkage
80             !ExternalNames.count(I->getName())) {// Not marked to keep external?
81           I->setLinkage(GlobalValue::InternalLinkage);
82           Changed = true;
83           ++NumFunctions;
84           DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
85         }
86
87       // Mark all global variables with initializers as internal as well...
88       for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
89         if (!I->isExternal() && !I->hasInternalLinkage() &&
90             !ExternalNames.count(I->getName())) {
91           // Special case handling of the global ctor and dtor list.  When we
92           // internalize it, we mark it constant, which allows elimination of
93           // the list if it's empty.
94           //
95           if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
96                                            I->getName() == "llvm.global_dtors"))
97             I->setConstant(true);
98
99           I->setLinkage(GlobalValue::InternalLinkage);
100           Changed = true;
101           ++NumGlobals;
102           DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
103         }
104       
105       return Changed;
106     }
107   };
108
109   RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
110 } // end anonymous namespace
111
112 Pass *createInternalizePass() {
113   return new InternalizePass();
114 }