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" |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | 568ddab | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 25 | #include <fstream> |
| 26 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 29 | STATISTIC(NumAliases , "Number of aliases internalized"); |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 30 | STATISTIC(NumFunctions, "Number of functions internalized"); |
| 31 | STATISTIC(NumGlobals , "Number of global vars internalized"); |
| 32 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 33 | // APIFile - A file which contains a list of symbols that should not be marked |
| 34 | // external. |
| 35 | static cl::opt<std::string> |
| 36 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
| 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 | static cl::list<std::string> |
| 41 | APIList("internalize-public-api-list", cl::value_desc("list"), |
| 42 | cl::desc("A list of symbol names to preserve"), |
| 43 | cl::CommaSeparated); |
| 44 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 45 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 46 | class VISIBILITY_HIDDEN InternalizePass : public ModulePass { |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 47 | std::set<std::string> ExternalNames; |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 48 | /// If no api symbols were specified and a main function is defined, |
| 49 | /// assume the main function is the only API |
| 50 | bool AllButMain; |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 51 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 52 | static char ID; // Pass identification, replacement for typeid |
Matthijs Kooijman | 4e78908 | 2008-06-24 09:14:10 +0000 | [diff] [blame] | 53 | explicit InternalizePass(bool AllButMain = true); |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 54 | explicit InternalizePass(const std::vector <const char *>& exportList); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 55 | void LoadFile(const char *Filename); |
| 56 | virtual bool runOnModule(Module &M); |
Nuno Lopes | 0483d01 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 57 | |
| 58 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 59 | AU.setPreservesCFG(); |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 60 | AU.addPreserved<CallGraph>(); |
Nuno Lopes | 0483d01 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 55e41ba | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 62 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 63 | } // end anonymous namespace |
| 64 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | char InternalizePass::ID = 0; |
| 66 | static RegisterPass<InternalizePass> |
| 67 | X("internalize", "Internalize Global Symbols"); |
| 68 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 69 | InternalizePass::InternalizePass(bool AllButMain) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 70 | : ModulePass(&ID), AllButMain(AllButMain){ |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 71 | if (!APIFile.empty()) // If a filename is specified, use it. |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 72 | LoadFile(APIFile.c_str()); |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 73 | if (!APIList.empty()) // If a list is specified, use it as well. |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 74 | ExternalNames.insert(APIList.begin(), APIList.end()); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 77 | InternalizePass::InternalizePass(const std::vector<const char *>&exportList) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 78 | : ModulePass(&ID), AllButMain(false){ |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 79 | for(std::vector<const char *>::const_iterator itr = exportList.begin(); |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 80 | itr != exportList.end(); itr++) { |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 81 | ExternalNames.insert(*itr); |
| 82 | } |
| 83 | } |
| 84 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 85 | void InternalizePass::LoadFile(const char *Filename) { |
| 86 | // Load the APIFile... |
| 87 | std::ifstream In(Filename); |
| 88 | if (!In.good()) { |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 89 | cerr << "WARNING: Internalize couldn't load file '" << Filename |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 90 | << "'! Continuing as if it's empty.\n"; |
| 91 | return; // Just continue as if the file were empty |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 92 | } |
| 93 | while (In) { |
| 94 | std::string Symbol; |
| 95 | In >> Symbol; |
| 96 | if (!Symbol.empty()) |
| 97 | ExternalNames.insert(Symbol); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool InternalizePass::runOnModule(Module &M) { |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 102 | CallGraph *CG = getAnalysisIfAvailable<CallGraph>(); |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 103 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; |
| 104 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 105 | if (ExternalNames.empty()) { |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 106 | // Return if we're not in 'all but main' mode and have no external api |
| 107 | if (!AllButMain) |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 108 | return false; |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 109 | // If no list or file of symbols was specified, check to see if there is a |
| 110 | // "main" symbol defined in the module. If so, use it, otherwise do not |
| 111 | // internalize the module, it must be a library or something. |
| 112 | // |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 113 | Function *MainFunc = M.getFunction("main"); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 114 | if (MainFunc == 0 || MainFunc->isDeclaration()) |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 115 | return false; // No main found, must be a library... |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 117 | // Preserve main, internalize all else. |
| 118 | ExternalNames.insert(MainFunc->getName()); |
| 119 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 121 | bool Changed = false; |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 122 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 123 | // Mark all functions not in the api as internal. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 124 | // FIXME: maybe use private linkage? |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 125 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 126 | if (!I->isDeclaration() && // Function must be defined here |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 127 | !I->hasLocalLinkage() && // Can't already have internal linkage |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 128 | !ExternalNames.count(I->getName())) {// Not marked to keep external? |
| 129 | I->setLinkage(GlobalValue::InternalLinkage); |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 130 | // Remove a callgraph edge from the external node to this function. |
| 131 | if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 132 | Changed = true; |
| 133 | ++NumFunctions; |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 134 | DOUT << "Internalizing func " << I->getName() << "\n"; |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 135 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 137 | // Never internalize the llvm.used symbol. It is used to implement |
| 138 | // attribute((used)). |
| 139 | ExternalNames.insert("llvm.used"); |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 140 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 141 | // Never internalize anchors used by the machine module info, else the info |
| 142 | // won't find them. (see MachineModuleInfo.) |
Jim Laskey | e8c3e3b | 2006-03-07 20:53:47 +0000 | [diff] [blame] | 143 | ExternalNames.insert("llvm.dbg.compile_units"); |
| 144 | ExternalNames.insert("llvm.dbg.global_variables"); |
| 145 | ExternalNames.insert("llvm.dbg.subprograms"); |
Chris Lattner | 4e2288b | 2007-06-06 20:51:41 +0000 | [diff] [blame] | 146 | ExternalNames.insert("llvm.global_ctors"); |
| 147 | ExternalNames.insert("llvm.global_dtors"); |
Chris Lattner | bd14f58 | 2007-10-03 03:59:15 +0000 | [diff] [blame] | 148 | ExternalNames.insert("llvm.noinline"); |
Tanya Lattner | 088b591 | 2007-10-03 17:05:40 +0000 | [diff] [blame] | 149 | ExternalNames.insert("llvm.global.annotations"); |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 150 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 151 | // Mark all global variables with initializers that are not in the api as |
| 152 | // internal as well. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 153 | // FIXME: maybe use private linkage? |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 154 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 155 | I != E; ++I) |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 156 | if (!I->isDeclaration() && !I->hasLocalLinkage() && |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 157 | !ExternalNames.count(I->getName())) { |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 158 | I->setLinkage(GlobalValue::InternalLinkage); |
| 159 | Changed = true; |
| 160 | ++NumGlobals; |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 161 | DOUT << "Internalized gvar " << I->getName() << "\n"; |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 162 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 163 | |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 164 | // Mark all aliases that are not in the api as internal as well. |
| 165 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 166 | I != E; ++I) |
| 167 | if (!I->isDeclaration() && !I->hasInternalLinkage() && |
| 168 | !ExternalNames.count(I->getName())) { |
| 169 | I->setLinkage(GlobalValue::InternalLinkage); |
| 170 | Changed = true; |
| 171 | ++NumAliases; |
| 172 | DOUT << "Internalized alias " << I->getName() << "\n"; |
| 173 | } |
| 174 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 175 | return Changed; |
| 176 | } |
| 177 | |
Matthijs Kooijman | 4e78908 | 2008-06-24 09:14:10 +0000 | [diff] [blame] | 178 | ModulePass *llvm::createInternalizePass(bool AllButMain) { |
| 179 | return new InternalizePass(AllButMain); |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 180 | } |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 181 | |
Devang Patel | d828a4b | 2006-07-20 18:03:39 +0000 | [diff] [blame] | 182 | ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { |
| 183 | return new InternalizePass(el); |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 184 | } |