Internalize.cpp revision 7ba70aeafb9594e1a369015c64fe541322b9ee3c
1//===-- Internalize.cpp - Mark functions internal -------------------------===// 2// 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. 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" 17#include "llvm/Transforms/IPO.h" 18#include "llvm/Pass.h" 19#include "llvm/Module.h" 20#include "llvm/Support/CommandLine.h" 21#include "llvm/Support/Compiler.h" 22#include "llvm/Support/Debug.h" 23#include "llvm/ADT/Statistic.h" 24#include <fstream> 25#include <set> 26using namespace llvm; 27 28STATISTIC(NumFunctions, "Number of functions internalized"); 29STATISTIC(NumGlobals , "Number of global vars internalized"); 30 31namespace { 32 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"), 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"), 42 cl::desc("A list of symbol names to preserve"), 43 cl::CommaSeparated); 44 45 class VISIBILITY_HIDDEN InternalizePass : public ModulePass { 46 std::set<std::string> ExternalNames; 47 bool DontInternalize; 48 public: 49 static char ID; // Pass identification, replacement for typeid 50 explicit InternalizePass(bool InternalizeEverything = true); 51 explicit InternalizePass(const std::vector <const char *>& exportList); 52 void LoadFile(const char *Filename); 53 virtual bool runOnModule(Module &M); 54 }; 55 char InternalizePass::ID = 0; 56 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols"); 57} // end anonymous namespace 58 59InternalizePass::InternalizePass(bool InternalizeEverything) 60 : ModulePass((intptr_t)&ID), DontInternalize(false){ 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 70InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 71 : ModulePass((intptr_t)&ID), DontInternalize(false){ 72 for(std::vector<const char *>::const_iterator itr = exportList.begin(); 73 itr != exportList.end(); itr++) { 74 ExternalNames.insert(*itr); 75 } 76} 77 78void InternalizePass::LoadFile(const char *Filename) { 79 // Load the APIFile... 80 std::ifstream In(Filename); 81 if (!In.good()) { 82 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n"; 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 93bool 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()) { 101 Function *MainFunc = M.getFunction("main"); 102 if (MainFunc == 0 || MainFunc->isDeclaration()) 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) 113 if (!I->isDeclaration() && // Function must be defined here 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; 119 DOUT << "Internalizing func " << I->getName() << "\n"; 120 } 121 122 // Never internalize the llvm.used symbol. It is used to implement 123 // attribute((used)). 124 ExternalNames.insert("llvm.used"); 125 126 // Never internalize anchors used by the machine module info, else the info 127 // won't find them. (see MachineModuleInfo.) 128 ExternalNames.insert("llvm.dbg.compile_units"); 129 ExternalNames.insert("llvm.dbg.global_variables"); 130 ExternalNames.insert("llvm.dbg.subprograms"); 131 ExternalNames.insert("llvm.global_ctors"); 132 ExternalNames.insert("llvm.global_dtors"); 133 ExternalNames.insert("llvm.noinline"); 134 135 // Mark all global variables with initializers as internal as well. 136 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 137 I != E; ++I) 138 if (!I->isDeclaration() && !I->hasInternalLinkage() && 139 !ExternalNames.count(I->getName())) { 140 I->setLinkage(GlobalValue::InternalLinkage); 141 Changed = true; 142 ++NumGlobals; 143 DOUT << "Internalized gvar " << I->getName() << "\n"; 144 } 145 146 return Changed; 147} 148 149ModulePass *llvm::createInternalizePass(bool InternalizeEverything) { 150 return new InternalizePass(InternalizeEverything); 151} 152 153ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { 154 return new InternalizePass(el); 155} 156