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" |
| 22 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 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 { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 46 | class 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; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 66 | INITIALIZE_PASS(InternalizePass, "internalize", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 67 | "Internalize Global Symbols", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 69 | InternalizePass::InternalizePass(bool AllButMain) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 70 | : ModulePass(ID), AllButMain(AllButMain){ |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 71 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 72 | if (!APIFile.empty()) // If a filename is specified, use it. |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 73 | LoadFile(APIFile.c_str()); |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 74 | if (!APIList.empty()) // If a list is specified, use it as well. |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 75 | ExternalNames.insert(APIList.begin(), APIList.end()); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 78 | InternalizePass::InternalizePass(const std::vector<const char *>&exportList) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 79 | : ModulePass(ID), AllButMain(false){ |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 80 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 81 | for(std::vector<const char *>::const_iterator itr = exportList.begin(); |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 82 | itr != exportList.end(); itr++) { |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 83 | ExternalNames.insert(*itr); |
| 84 | } |
| 85 | } |
| 86 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 87 | void InternalizePass::LoadFile(const char *Filename) { |
| 88 | // Load the APIFile... |
| 89 | std::ifstream In(Filename); |
| 90 | if (!In.good()) { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 91 | errs() << "WARNING: Internalize couldn't load file '" << Filename |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 92 | << "'! Continuing as if it's empty.\n"; |
| 93 | return; // Just continue as if the file were empty |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 94 | } |
| 95 | while (In) { |
| 96 | std::string Symbol; |
| 97 | In >> Symbol; |
| 98 | if (!Symbol.empty()) |
| 99 | ExternalNames.insert(Symbol); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | bool InternalizePass::runOnModule(Module &M) { |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 104 | CallGraph *CG = getAnalysisIfAvailable<CallGraph>(); |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 105 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; |
Owen Anderson | 001dbfe | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 107 | if (ExternalNames.empty()) { |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 108 | // Return if we're not in 'all but main' mode and have no external api |
| 109 | if (!AllButMain) |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 110 | return false; |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 111 | // If no list or file of symbols was specified, check to see if there is a |
| 112 | // "main" symbol defined in the module. If so, use it, otherwise do not |
| 113 | // internalize the module, it must be a library or something. |
| 114 | // |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 115 | Function *MainFunc = M.getFunction("main"); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 116 | if (MainFunc == 0 || MainFunc->isDeclaration()) |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 117 | return false; // No main found, must be a library... |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 119 | // Preserve main, internalize all else. |
| 120 | ExternalNames.insert(MainFunc->getName()); |
| 121 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 123 | bool Changed = false; |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 124 | |
Bill Wendling | aab3c0c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 125 | // Never internalize functions which code-gen might insert. |
Bill Wendling | ab3a919 | 2012-04-16 04:23:52 +0000 | [diff] [blame^] | 126 | // FIXME: We should probably add this (and the __stack_chk_guard) via some |
| 127 | // type of call-back in CodeGen. |
Bill Wendling | aab3c0c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 128 | ExternalNames.insert("__stack_chk_fail"); |
| 129 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 130 | // Mark all functions not in the api as internal. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 131 | // FIXME: maybe use private linkage? |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 132 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 133 | if (!I->isDeclaration() && // Function must be defined here |
Rafael Espindola | 1b5ec06 | 2011-03-06 23:41:34 +0000 | [diff] [blame] | 134 | // Available externally is really just a "declaration with a body". |
| 135 | !I->hasAvailableExternallyLinkage() && |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 136 | !I->hasLocalLinkage() && // Can't already have internal linkage |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 137 | !ExternalNames.count(I->getName())) {// Not marked to keep external? |
| 138 | I->setLinkage(GlobalValue::InternalLinkage); |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 139 | // Remove a callgraph edge from the external node to this function. |
| 140 | if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 141 | Changed = true; |
| 142 | ++NumFunctions; |
David Greene | fd2ab9f | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 143 | DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n"); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 144 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 146 | // Never internalize the llvm.used symbol. It is used to implement |
| 147 | // attribute((used)). |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 148 | // FIXME: Shouldn't this just filter on llvm.metadata section?? |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 149 | ExternalNames.insert("llvm.used"); |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 150 | ExternalNames.insert("llvm.compiler.used"); |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 151 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 152 | // Never internalize anchors used by the machine module info, else the info |
| 153 | // won't find them. (see MachineModuleInfo.) |
Chris Lattner | 4e2288b | 2007-06-06 20:51:41 +0000 | [diff] [blame] | 154 | ExternalNames.insert("llvm.global_ctors"); |
| 155 | ExternalNames.insert("llvm.global_dtors"); |
Tanya Lattner | 088b591 | 2007-10-03 17:05:40 +0000 | [diff] [blame] | 156 | ExternalNames.insert("llvm.global.annotations"); |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 157 | |
Bill Wendling | aab3c0c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 158 | // Never internalize symbols code-gen inserts. |
| 159 | ExternalNames.insert("__stack_chk_guard"); |
| 160 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 161 | // Mark all global variables with initializers that are not in the api as |
| 162 | // internal as well. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 163 | // FIXME: maybe use private linkage? |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 164 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 165 | I != E; ++I) |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 166 | if (!I->isDeclaration() && !I->hasLocalLinkage() && |
Chris Lattner | 8e9c48a | 2010-04-03 05:24:50 +0000 | [diff] [blame] | 167 | // Available externally is really just a "declaration with a body". |
| 168 | !I->hasAvailableExternallyLinkage() && |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 169 | !ExternalNames.count(I->getName())) { |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 170 | I->setLinkage(GlobalValue::InternalLinkage); |
| 171 | Changed = true; |
| 172 | ++NumGlobals; |
David Greene | fd2ab9f | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 173 | DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n"); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 174 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 175 | |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 176 | // Mark all aliases that are not in the api as internal as well. |
| 177 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 178 | I != E; ++I) |
| 179 | if (!I->isDeclaration() && !I->hasInternalLinkage() && |
Chris Lattner | 8e9c48a | 2010-04-03 05:24:50 +0000 | [diff] [blame] | 180 | // Available externally is really just a "declaration with a body". |
| 181 | !I->hasAvailableExternallyLinkage() && |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 182 | !ExternalNames.count(I->getName())) { |
| 183 | I->setLinkage(GlobalValue::InternalLinkage); |
| 184 | Changed = true; |
| 185 | ++NumAliases; |
David Greene | fd2ab9f | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 186 | DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n"); |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 189 | return Changed; |
| 190 | } |
| 191 | |
Matthijs Kooijman | 4e78908 | 2008-06-24 09:14:10 +0000 | [diff] [blame] | 192 | ModulePass *llvm::createInternalizePass(bool AllButMain) { |
| 193 | return new InternalizePass(AllButMain); |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 194 | } |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 195 | |
Devang Patel | d828a4b | 2006-07-20 18:03:39 +0000 | [diff] [blame] | 196 | ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { |
| 197 | return new InternalizePass(el); |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 198 | } |