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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "internalize" |
Chris Lattner | 568ddab | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 24 | #include <fstream> |
| 25 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 28 | STATISTIC(NumFunctions, "Number of functions internalized"); |
| 29 | STATISTIC(NumGlobals , "Number of global vars internalized"); |
| 30 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 31 | namespace { |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 33 | // APIFile - A file which contains a list of symbols that should not be marked |
| 34 | // external. |
| 35 | cl::opt<std::string> |
| 36 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
Chris Lattner | 2345d71 | 2003-05-22 19:48:00 +0000 | [diff] [blame] | 37 | cl::desc("A file containing list of symbol names to preserve")); |
| 38 | |
| 39 | // APIList - A list of symbols that should not be marked internal. |
| 40 | cl::list<std::string> |
| 41 | APIList("internalize-public-api-list", cl::value_desc("list"), |
Chris Lattner | 88c7c32 | 2003-05-22 20:27:13 +0000 | [diff] [blame] | 42 | cl::desc("A list of symbol names to preserve"), |
| 43 | cl::CommaSeparated); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 44 | |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 45 | class VISIBILITY_HIDDEN InternalizePass : public ModulePass { |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 46 | std::set<std::string> ExternalNames; |
Chris Lattner | a27ea76 | 2005-10-18 06:29:22 +0000 | [diff] [blame] | 47 | bool DontInternalize; |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 48 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 49 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 50 | explicit InternalizePass(bool InternalizeEverything = true); |
| 51 | explicit InternalizePass(const std::vector <const char *>& exportList); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 52 | void LoadFile(const char *Filename); |
| 53 | virtual bool runOnModule(Module &M); |
Chris Lattner | 55e41ba | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 54 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 55 | char InternalizePass::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 56 | RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols"); |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 57 | } // end anonymous namespace |
| 58 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 59 | InternalizePass::InternalizePass(bool InternalizeEverything) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 60 | : ModulePass((intptr_t)&ID), DontInternalize(false){ |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 61 | if (!APIFile.empty()) // If a filename is specified, use it |
| 62 | LoadFile(APIFile.c_str()); |
| 63 | else if (!APIList.empty()) // Else, if a list is specified, use it. |
| 64 | ExternalNames.insert(APIList.begin(), APIList.end()); |
| 65 | else if (!InternalizeEverything) |
| 66 | // Finally, if we're allowed to, internalize all but main. |
| 67 | DontInternalize = true; |
| 68 | } |
| 69 | |
Devang Patel | 56eac5f | 2006-09-13 01:02:26 +0000 | [diff] [blame] | 70 | InternalizePass::InternalizePass(const std::vector<const char *>&exportList) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 71 | : ModulePass((intptr_t)&ID), DontInternalize(false){ |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 72 | for(std::vector<const char *>::const_iterator itr = exportList.begin(); |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 73 | itr != exportList.end(); itr++) { |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 74 | ExternalNames.insert(*itr); |
| 75 | } |
| 76 | } |
| 77 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 78 | void InternalizePass::LoadFile(const char *Filename) { |
| 79 | // Load the APIFile... |
| 80 | std::ifstream In(Filename); |
| 81 | if (!In.good()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 82 | cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n"; |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 83 | return; // Do not internalize anything... |
| 84 | } |
| 85 | while (In) { |
| 86 | std::string Symbol; |
| 87 | In >> Symbol; |
| 88 | if (!Symbol.empty()) |
| 89 | ExternalNames.insert(Symbol); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | bool InternalizePass::runOnModule(Module &M) { |
| 94 | if (DontInternalize) return false; |
| 95 | |
| 96 | // If no list or file of symbols was specified, check to see if there is a |
| 97 | // "main" symbol defined in the module. If so, use it, otherwise do not |
| 98 | // internalize the module, it must be a library or something. |
| 99 | // |
| 100 | if (ExternalNames.empty()) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 101 | Function *MainFunc = M.getFunction("main"); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 102 | if (MainFunc == 0 || MainFunc->isDeclaration()) |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 103 | return false; // No main found, must be a library... |
| 104 | |
| 105 | // Preserve main, internalize all else. |
| 106 | ExternalNames.insert(MainFunc->getName()); |
| 107 | } |
| 108 | |
| 109 | bool Changed = false; |
| 110 | |
| 111 | // Found a main function, mark all functions not named main as internal. |
| 112 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 113 | if (!I->isDeclaration() && // Function must be defined here |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 114 | !I->hasInternalLinkage() && // Can't already have internal linkage |
| 115 | !ExternalNames.count(I->getName())) {// Not marked to keep external? |
| 116 | I->setLinkage(GlobalValue::InternalLinkage); |
| 117 | Changed = true; |
| 118 | ++NumFunctions; |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 119 | DOUT << "Internalizing func " << I->getName() << "\n"; |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Never internalize the llvm.used symbol. It is used to implement |
| 123 | // attribute((used)). |
| 124 | ExternalNames.insert("llvm.used"); |
Chris Lattner | ee9e14c | 2006-01-19 00:40:39 +0000 | [diff] [blame] | 125 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 126 | // Never internalize anchors used by the machine module info, else the info |
| 127 | // won't find them. (see MachineModuleInfo.) |
Jim Laskey | e8c3e3b | 2006-03-07 20:53:47 +0000 | [diff] [blame] | 128 | ExternalNames.insert("llvm.dbg.compile_units"); |
| 129 | ExternalNames.insert("llvm.dbg.global_variables"); |
| 130 | ExternalNames.insert("llvm.dbg.subprograms"); |
Chris Lattner | 4e2288b | 2007-06-06 20:51:41 +0000 | [diff] [blame] | 131 | ExternalNames.insert("llvm.global_ctors"); |
| 132 | ExternalNames.insert("llvm.global_dtors"); |
Chris Lattner | bd14f58 | 2007-10-03 03:59:15 +0000 | [diff] [blame] | 133 | ExternalNames.insert("llvm.noinline"); |
Tanya Lattner | 088b591 | 2007-10-03 17:05:40 +0000 | [diff] [blame] | 134 | ExternalNames.insert("llvm.global.annotations"); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 135 | |
| 136 | // Mark all global variables with initializers as internal as well. |
| 137 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 138 | I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 139 | if (!I->isDeclaration() && !I->hasInternalLinkage() && |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 140 | !ExternalNames.count(I->getName())) { |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 141 | I->setLinkage(GlobalValue::InternalLinkage); |
| 142 | Changed = true; |
| 143 | ++NumGlobals; |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 144 | DOUT << "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 | } |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 153 | |
Devang Patel | d828a4b | 2006-07-20 18:03:39 +0000 | [diff] [blame] | 154 | ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { |
| 155 | return new InternalizePass(el); |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 156 | } |