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