Internalize.cpp revision f4d48e15f7af90a5b0b056cea6a21bbfa266779e
1//===-- Internalize.cpp - Mark functions internal -------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 31// APIFile - A file which contains a list of symbols that should not be marked 32// external. 33static cl::opt<std::string> 34APIFile("internalize-public-api-file", cl::value_desc("filename"), 35 cl::desc("A file containing list of symbol names to preserve")); 36 37// APIList - A list of symbols that should not be marked internal. 38static cl::list<std::string> 39APIList("internalize-public-api-list", cl::value_desc("list"), 40 cl::desc("A list of symbol names to preserve"), 41 cl::CommaSeparated); 42 43namespace { 44 class VISIBILITY_HIDDEN InternalizePass : public ModulePass { 45 std::set<std::string> ExternalNames; 46 /// If no api symbols were specified and a main function is defined, 47 /// assume the main function is the only API 48 bool AllButMain; 49 public: 50 static char ID; // Pass identification, replacement for typeid 51 explicit InternalizePass(bool AllButMain = true); 52 explicit InternalizePass(const std::vector <const char *>& exportList); 53 void LoadFile(const char *Filename); 54 virtual bool runOnModule(Module &M); 55 56 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 57 AU.setPreservesCFG(); 58 } 59 }; 60} // end anonymous namespace 61 62char InternalizePass::ID = 0; 63static RegisterPass<InternalizePass> 64X("internalize", "Internalize Global Symbols"); 65 66InternalizePass::InternalizePass(bool AllButMain) 67 : ModulePass(&ID), AllButMain(AllButMain){ 68 if (!APIFile.empty()) // If a filename is specified, use it. 69 LoadFile(APIFile.c_str()); 70 if (!APIList.empty()) // If a list is specified, use it as well. 71 ExternalNames.insert(APIList.begin(), APIList.end()); 72} 73 74InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 75 : ModulePass(&ID), AllButMain(false){ 76 for(std::vector<const char *>::const_iterator itr = exportList.begin(); 77 itr != exportList.end(); itr++) { 78 ExternalNames.insert(*itr); 79 } 80} 81 82void InternalizePass::LoadFile(const char *Filename) { 83 // Load the APIFile... 84 std::ifstream In(Filename); 85 if (!In.good()) { 86 cerr << "WARNING: Internalize couldn't load file '" << Filename 87 << "'! Continuing as if it's empty.\n"; 88 return; // Just continue as if the file were empty 89 } 90 while (In) { 91 std::string Symbol; 92 In >> Symbol; 93 if (!Symbol.empty()) 94 ExternalNames.insert(Symbol); 95 } 96} 97 98bool InternalizePass::runOnModule(Module &M) { 99 if (ExternalNames.empty()) { 100 // Return if we're not in 'all but main' mode and have no external api 101 if (!AllButMain) 102 return false; 103 // If no list or file of symbols was specified, check to see if there is a 104 // "main" symbol defined in the module. If so, use it, otherwise do not 105 // internalize the module, it must be a library or something. 106 // 107 Function *MainFunc = M.getFunction("main"); 108 if (MainFunc == 0 || MainFunc->isDeclaration()) 109 return false; // No main found, must be a library... 110 111 // Preserve main, internalize all else. 112 ExternalNames.insert(MainFunc->getName()); 113 } 114 115 bool Changed = false; 116 117 // Mark all functions not in the api as internal. 118 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 119 if (!I->isDeclaration() && // Function must be defined here 120 !I->hasInternalLinkage() && // Can't already have internal linkage 121 !ExternalNames.count(I->getName())) {// Not marked to keep external? 122 I->setLinkage(GlobalValue::InternalLinkage); 123 Changed = true; 124 ++NumFunctions; 125 DOUT << "Internalizing func " << I->getName() << "\n"; 126 } 127 128 // Never internalize the llvm.used symbol. It is used to implement 129 // attribute((used)). 130 ExternalNames.insert("llvm.used"); 131 132 // Never internalize anchors used by the machine module info, else the info 133 // won't find them. (see MachineModuleInfo.) 134 ExternalNames.insert("llvm.dbg.compile_units"); 135 ExternalNames.insert("llvm.dbg.global_variables"); 136 ExternalNames.insert("llvm.dbg.subprograms"); 137 ExternalNames.insert("llvm.global_ctors"); 138 ExternalNames.insert("llvm.global_dtors"); 139 ExternalNames.insert("llvm.noinline"); 140 ExternalNames.insert("llvm.global.annotations"); 141 142 // Mark all global variables with initializers that are not in the api as 143 // internal as well. 144 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 145 I != E; ++I) 146 if (!I->isDeclaration() && !I->hasInternalLinkage() && 147 !ExternalNames.count(I->getName())) { 148 I->setLinkage(GlobalValue::InternalLinkage); 149 Changed = true; 150 ++NumGlobals; 151 DOUT << "Internalized gvar " << I->getName() << "\n"; 152 } 153 154 return Changed; 155} 156 157ModulePass *llvm::createInternalizePass(bool AllButMain) { 158 return new InternalizePass(AllButMain); 159} 160 161ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { 162 return new InternalizePass(el); 163} 164