Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 1 | //===-- Internalize.cpp - Mark functions internal -------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | dbb1735 | 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 | 55e41ba | 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 | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 568ddab | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 22 | #include <fstream> |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame^] | 23 | #include <iostream> |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 24 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 27 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 28 | Statistic<> NumFunctions("internalize", "Number of functions internalized"); |
| 29 | Statistic<> NumGlobals ("internalize", "Number of global vars internalized"); |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 30 | |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 31 | // APIFile - A file which contains a list of symbols that should not be marked |
| 32 | // external. |
| 33 | cl::opt<std::string> |
| 34 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
Chris Lattner | 2345d71 | 2003-05-22 19:48:00 +0000 | [diff] [blame] | 35 | cl::desc("A file containing list of symbol names to preserve")); |
| 36 | |
| 37 | // APIList - A list of symbols that should not be marked internal. |
| 38 | cl::list<std::string> |
| 39 | APIList("internalize-public-api-list", cl::value_desc("list"), |
Chris Lattner | 88c7c32 | 2003-05-22 20:27:13 +0000 | [diff] [blame] | 40 | cl::desc("A list of symbol names to preserve"), |
| 41 | cl::CommaSeparated); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 43 | class InternalizePass : public ModulePass { |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 44 | std::set<std::string> ExternalNames; |
Chris Lattner | a27ea76 | 2005-10-18 06:29:22 +0000 | [diff] [blame] | 45 | bool DontInternalize; |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 46 | public: |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 47 | InternalizePass(bool InternalizeEverything = true); |
| 48 | void LoadFile(const char *Filename); |
| 49 | virtual bool runOnModule(Module &M); |
Chris Lattner | 55e41ba | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 50 | }; |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 51 | RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols"); |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 52 | } // end anonymous namespace |
| 53 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 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 | void InternalizePass::LoadFile(const char *Filename) { |
| 66 | // Load the APIFile... |
| 67 | std::ifstream In(Filename); |
| 68 | if (!In.good()) { |
| 69 | std::cerr << "WARNING: Internalize couldn't load file '" << Filename |
| 70 | << "'!\n"; |
| 71 | return; // Do not internalize anything... |
| 72 | } |
| 73 | while (In) { |
| 74 | std::string Symbol; |
| 75 | In >> Symbol; |
| 76 | if (!Symbol.empty()) |
| 77 | ExternalNames.insert(Symbol); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool InternalizePass::runOnModule(Module &M) { |
| 82 | if (DontInternalize) return false; |
| 83 | |
| 84 | // If no list or file of symbols was specified, check to see if there is a |
| 85 | // "main" symbol defined in the module. If so, use it, otherwise do not |
| 86 | // internalize the module, it must be a library or something. |
| 87 | // |
| 88 | if (ExternalNames.empty()) { |
| 89 | Function *MainFunc = M.getMainFunction(); |
| 90 | if (MainFunc == 0 || MainFunc->isExternal()) |
| 91 | return false; // No main found, must be a library... |
| 92 | |
| 93 | // Preserve main, internalize all else. |
| 94 | ExternalNames.insert(MainFunc->getName()); |
| 95 | } |
| 96 | |
| 97 | bool Changed = false; |
| 98 | |
| 99 | // Found a main function, mark all functions not named main as internal. |
| 100 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 101 | if (!I->isExternal() && // Function must be defined here |
| 102 | !I->hasInternalLinkage() && // Can't already have internal linkage |
| 103 | !ExternalNames.count(I->getName())) {// Not marked to keep external? |
| 104 | I->setLinkage(GlobalValue::InternalLinkage); |
| 105 | Changed = true; |
| 106 | ++NumFunctions; |
| 107 | DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n"); |
| 108 | } |
| 109 | |
| 110 | // Never internalize the llvm.used symbol. It is used to implement |
| 111 | // attribute((used)). |
| 112 | ExternalNames.insert("llvm.used"); |
Chris Lattner | ee9e14c | 2006-01-19 00:40:39 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 114 | // Never internalize anchors used by the debugger, else the debugger won't |
| 115 | // find them. |
| 116 | ExternalNames.insert("llvm.dbg.translation_units"); |
| 117 | ExternalNames.insert("llvm.dbg.globals"); |
| 118 | |
| 119 | // Mark all global variables with initializers as internal as well. |
| 120 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 121 | I != E; ++I) |
| 122 | if (!I->isExternal() && !I->hasInternalLinkage() && |
| 123 | !ExternalNames.count(I->getName())) { |
| 124 | // Special case handling of the global ctor and dtor list. When we |
| 125 | // internalize it, we mark it constant, which allows elimination of |
| 126 | // the list if it's empty. |
| 127 | // |
| 128 | if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" || |
Chris Lattner | ee9e14c | 2006-01-19 00:40:39 +0000 | [diff] [blame] | 129 | I->getName() == "llvm.global_dtors")) { |
Chris Lattner | ee9e14c | 2006-01-19 00:40:39 +0000 | [diff] [blame] | 130 | // If the global ctors/dtors list has no uses, do not internalize it, as |
| 131 | // there is no __main in this program, so the asmprinter should handle |
| 132 | // it. |
| 133 | if (I->use_empty()) continue; |
Chris Lattner | 727552b | 2006-01-19 00:46:54 +0000 | [diff] [blame] | 134 | |
| 135 | // Otherwise, also mark the list constant, as we know that it will not |
| 136 | // be mutated any longer, and the makes simple IPO xforms automatically |
| 137 | // better. |
| 138 | I->setConstant(true); |
Chris Lattner | ee9e14c | 2006-01-19 00:40:39 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 140 | |
| 141 | I->setLinkage(GlobalValue::InternalLinkage); |
| 142 | Changed = true; |
| 143 | ++NumGlobals; |
Chris Lattner | ee9e14c | 2006-01-19 00:40:39 +0000 | [diff] [blame] | 144 | DEBUG(std::cerr << "Internalized gvar " << I->getName() << "\n"); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return Changed; |
| 148 | } |
| 149 | |
Chris Lattner | a27ea76 | 2005-10-18 06:29:22 +0000 | [diff] [blame] | 150 | ModulePass *llvm::createInternalizePass(bool InternalizeEverything) { |
| 151 | return new InternalizePass(InternalizeEverything); |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 152 | } |