Add option to internalize to allow it to read a file to determine which symbols
[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/Statistic.h"
13 #include "Support/CommandLine.h"
14 #include <fstream>
15 #include <set>
16
17 namespace {
18   Statistic<> NumFunctions("internalize", "Number of functions internalized");
19   Statistic<> NumGlobals  ("internalize", "Number of global vars internalized");
20
21   // APIFile - A file which contains a list of symbols that should not be marked
22   // external.
23   cl::opt<std::string>
24   APIFile("internalize-public-api-file", cl::value_desc("filename"),
25           cl::desc("A file containing list of globals to not internalize"));
26   
27   class InternalizePass : public Pass {
28     std::set<std::string> ExternalNames;
29   public:
30     InternalizePass() {
31       if (!APIFile.empty())
32         LoadFile(APIFile.c_str());
33       else
34         ExternalNames.insert("main");
35     }
36
37     void LoadFile(const char *Filename) {
38       // Load the APIFile...
39       std::ifstream In(Filename);
40       if (!In.good()) {
41         std::cerr << "WARNING: Internalize couldn't load file '" << Filename
42                   << "'!: Not internalizing.\n";
43         return;   // Do not internalize anything...
44       }
45       while (In) {
46         std::string Symbol;
47         In >> Symbol;
48         if (!Symbol.empty())
49           ExternalNames.insert(Symbol);
50       }
51     }
52
53     virtual bool run(Module &M) {
54       if (ExternalNames.empty()) return false;  // Error loading file...
55       bool Changed = false;
56       
57       // Found a main function, mark all functions not named main as internal.
58       for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
59         if (!I->isExternal() &&         // Function must be defined here
60             !I->hasInternalLinkage() &&  // Can't already have internal linkage
61             !ExternalNames.count(I->getName())) {// Not marked to keep external?
62           I->setLinkage(GlobalValue::InternalLinkage);
63           Changed = true;
64           ++NumFunctions;
65           DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
66         }
67
68       // Mark all global variables with initializers as internal as well...
69       for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
70         if (!I->isExternal() && !I->hasInternalLinkage() &&
71             !ExternalNames.count(I->getName())) {
72           I->setLinkage(GlobalValue::InternalLinkage);
73           Changed = true;
74           ++NumGlobals;
75           DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
76         }
77       
78       return Changed;
79     }
80   };
81
82   RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
83 } // end anonymous namespace
84
85 Pass *createInternalizePass() {
86   return new InternalizePass();
87 }