Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- Internalize.cpp - Mark functions internal -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass loops over all of the functions in the input module, looking for a |
| 11 | // main function. If a main function is found, all other functions and all |
| 12 | // global variables with initializers are marked as internal. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "internalize" |
Duncan Sands | 349bf2e | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CallGraph.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include <fstream> |
| 26 | #include <set> |
| 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(NumFunctions, "Number of functions internalized"); |
| 30 | STATISTIC(NumGlobals , "Number of global vars internalized"); |
| 31 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 32 | // APIFile - A file which contains a list of symbols that should not be marked |
| 33 | // external. |
| 34 | static cl::opt<std::string> |
| 35 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
| 36 | cl::desc("A file containing list of symbol names to preserve")); |
| 37 | |
| 38 | // APIList - A list of symbols that should not be marked internal. |
| 39 | static cl::list<std::string> |
| 40 | APIList("internalize-public-api-list", cl::value_desc("list"), |
| 41 | cl::desc("A list of symbol names to preserve"), |
| 42 | cl::CommaSeparated); |
| 43 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 44 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 45 | class VISIBILITY_HIDDEN InternalizePass : public ModulePass { |
| 46 | std::set<std::string> ExternalNames; |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 47 | /// If no api symbols were specified and a main function is defined, |
| 48 | /// assume the main function is the only API |
| 49 | bool AllButMain; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | public: |
| 51 | static char ID; // Pass identification, replacement for typeid |
Matthijs Kooijman | 99feb6a | 2008-06-24 09:14:10 +0000 | [diff] [blame] | 52 | explicit InternalizePass(bool AllButMain = true); |
Dan Gohman | 34c280e | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 53 | explicit InternalizePass(const std::vector <const char *>& exportList); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | void LoadFile(const char *Filename); |
| 55 | virtual bool runOnModule(Module &M); |
Nuno Lopes | 5de9724 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 56 | |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.setPreservesCFG(); |
Duncan Sands | 349bf2e | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 59 | AU.addPreserved<CallGraph>(); |
Nuno Lopes | 5de9724 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 60 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | } // end anonymous namespace |
| 63 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 64 | char InternalizePass::ID = 0; |
| 65 | static RegisterPass<InternalizePass> |
| 66 | X("internalize", "Internalize Global Symbols"); |
| 67 | |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 68 | InternalizePass::InternalizePass(bool AllButMain) |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 69 | : ModulePass(&ID), AllButMain(AllButMain){ |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 70 | if (!APIFile.empty()) // If a filename is specified, use it. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | LoadFile(APIFile.c_str()); |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 72 | if (!APIList.empty()) // If a list is specified, use it as well. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | ExternalNames.insert(APIList.begin(), APIList.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | InternalizePass::InternalizePass(const std::vector<const char *>&exportList) |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 77 | : ModulePass(&ID), AllButMain(false){ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | for(std::vector<const char *>::const_iterator itr = exportList.begin(); |
| 79 | itr != exportList.end(); itr++) { |
| 80 | ExternalNames.insert(*itr); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void InternalizePass::LoadFile(const char *Filename) { |
| 85 | // Load the APIFile... |
| 86 | std::ifstream In(Filename); |
| 87 | if (!In.good()) { |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 88 | cerr << "WARNING: Internalize couldn't load file '" << Filename |
| 89 | << "'! Continuing as if it's empty.\n"; |
| 90 | return; // Just continue as if the file were empty |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 91 | } |
| 92 | while (In) { |
| 93 | std::string Symbol; |
| 94 | In >> Symbol; |
| 95 | if (!Symbol.empty()) |
| 96 | ExternalNames.insert(Symbol); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | bool InternalizePass::runOnModule(Module &M) { |
Duncan Sands | 349bf2e | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 101 | CallGraph *CG = getAnalysisToUpdate<CallGraph>(); |
| 102 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; |
| 103 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | if (ExternalNames.empty()) { |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 105 | // Return if we're not in 'all but main' mode and have no external api |
| 106 | if (!AllButMain) |
| 107 | return false; |
| 108 | // If no list or file of symbols was specified, check to see if there is a |
| 109 | // "main" symbol defined in the module. If so, use it, otherwise do not |
| 110 | // internalize the module, it must be a library or something. |
| 111 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | Function *MainFunc = M.getFunction("main"); |
| 113 | if (MainFunc == 0 || MainFunc->isDeclaration()) |
| 114 | return false; // No main found, must be a library... |
| 115 | |
| 116 | // Preserve main, internalize all else. |
| 117 | ExternalNames.insert(MainFunc->getName()); |
| 118 | } |
| 119 | |
| 120 | bool Changed = false; |
| 121 | |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 122 | // Mark all functions not in the api as internal. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 124 | if (!I->isDeclaration() && // Function must be defined here |
| 125 | !I->hasInternalLinkage() && // Can't already have internal linkage |
| 126 | !ExternalNames.count(I->getName())) {// Not marked to keep external? |
| 127 | I->setLinkage(GlobalValue::InternalLinkage); |
Duncan Sands | 349bf2e | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 128 | // Remove a callgraph edge from the external node to this function. |
| 129 | if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | Changed = true; |
| 131 | ++NumFunctions; |
| 132 | DOUT << "Internalizing func " << I->getName() << "\n"; |
| 133 | } |
| 134 | |
| 135 | // Never internalize the llvm.used symbol. It is used to implement |
| 136 | // attribute((used)). |
| 137 | ExternalNames.insert("llvm.used"); |
| 138 | |
| 139 | // Never internalize anchors used by the machine module info, else the info |
| 140 | // won't find them. (see MachineModuleInfo.) |
| 141 | ExternalNames.insert("llvm.dbg.compile_units"); |
| 142 | ExternalNames.insert("llvm.dbg.global_variables"); |
| 143 | ExternalNames.insert("llvm.dbg.subprograms"); |
| 144 | ExternalNames.insert("llvm.global_ctors"); |
| 145 | ExternalNames.insert("llvm.global_dtors"); |
Chris Lattner | 7ba70ae | 2007-10-03 03:59:15 +0000 | [diff] [blame] | 146 | ExternalNames.insert("llvm.noinline"); |
Tanya Lattner | aa42b9d | 2007-10-03 17:05:40 +0000 | [diff] [blame] | 147 | ExternalNames.insert("llvm.global.annotations"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | |
Devang Patel | 23c4b31 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 149 | // Mark all global variables with initializers that are not in the api as |
| 150 | // internal as well. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 152 | I != E; ++I) |
| 153 | if (!I->isDeclaration() && !I->hasInternalLinkage() && |
| 154 | !ExternalNames.count(I->getName())) { |
| 155 | I->setLinkage(GlobalValue::InternalLinkage); |
| 156 | Changed = true; |
| 157 | ++NumGlobals; |
| 158 | DOUT << "Internalized gvar " << I->getName() << "\n"; |
| 159 | } |
| 160 | |
| 161 | return Changed; |
| 162 | } |
| 163 | |
Matthijs Kooijman | 99feb6a | 2008-06-24 09:14:10 +0000 | [diff] [blame] | 164 | ModulePass *llvm::createInternalizePass(bool AllButMain) { |
| 165 | return new InternalizePass(AllButMain); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { |
| 169 | return new InternalizePass(el); |
| 170 | } |