Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 1 | //===-- Internalize.cpp - Mark functions internal -------------------------===// |
| 2 | // |
| 3 | // This pass loops over all of the functions in the input module, looking for a |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 4 | // main function. If a main function is found, all other functions and all |
| 5 | // global variables with initializers are marked as internal. |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | 99a53f6 | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
| 11 | #include "llvm/Module.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 12 | #include "Support/Statistic.h" |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 13 | #include "Support/CommandLine.h" |
| 14 | #include <fstream> |
| 15 | #include <set> |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 16 | |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 17 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 18 | Statistic<> NumFunctions("internalize", "Number of functions internalized"); |
| 19 | Statistic<> NumGlobals ("internalize", "Number of global vars internalized"); |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 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 | |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 27 | class InternalizePass : public Pass { |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 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 | } |
Chris Lattner | 17069b3 | 2002-11-08 20:34:21 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 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... |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 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) |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 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? |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 62 | I->setLinkage(GlobalValue::InternalLinkage); |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 63 | Changed = true; |
| 64 | ++NumFunctions; |
| 65 | DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n"); |
| 66 | } |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 68 | // Mark all global variables with initializers as internal as well... |
| 69 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 70 | if (!I->isExternal() && !I->hasInternalLinkage() && |
| 71 | !ExternalNames.count(I->getName())) { |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 72 | I->setLinkage(GlobalValue::InternalLinkage); |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 73 | Changed = true; |
| 74 | ++NumGlobals; |
| 75 | DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n"); |
| 76 | } |
| 77 | |
| 78 | return Changed; |
| 79 | } |
| 80 | }; |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame^] | 82 | RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols"); |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 83 | } // end anonymous namespace |
| 84 | |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 85 | Pass *createInternalizePass() { |
| 86 | return new InternalizePass(); |
| 87 | } |