LLVM API Documentation
00001 //===-- Internalize.cpp - Mark functions internal -------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass loops over all of the functions in the input module, looking for a 00011 // main function. If a main function is found, all other functions and all 00012 // global variables with initializers are marked as internal. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #define DEBUG_TYPE "internalize" 00017 #include "llvm/Analysis/CallGraph.h" 00018 #include "llvm/Transforms/IPO.h" 00019 #include "llvm/Pass.h" 00020 #include "llvm/Module.h" 00021 #include "llvm/Support/CommandLine.h" 00022 #include "llvm/Support/Compiler.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/ADT/Statistic.h" 00025 #include <fstream> 00026 #include <set> 00027 using namespace llvm; 00028 00029 STATISTIC(NumAliases , "Number of aliases internalized"); 00030 STATISTIC(NumFunctions, "Number of functions internalized"); 00031 STATISTIC(NumGlobals , "Number of global vars internalized"); 00032 00033 // APIFile - A file which contains a list of symbols that should not be marked 00034 // external. 00035 static cl::opt<std::string> 00036 APIFile("internalize-public-api-file", cl::value_desc("filename"), 00037 cl::desc("A file containing list of symbol names to preserve")); 00038 00039 // APIList - A list of symbols that should not be marked internal. 00040 static cl::list<std::string> 00041 APIList("internalize-public-api-list", cl::value_desc("list"), 00042 cl::desc("A list of symbol names to preserve"), 00043 cl::CommaSeparated); 00044 00045 namespace { 00046 class VISIBILITY_HIDDEN InternalizePass : public ModulePass { 00047 std::set<std::string> ExternalNames; 00048 /// If no api symbols were specified and a main function is defined, 00049 /// assume the main function is the only API 00050 bool AllButMain; 00051 public: 00052 static char ID; // Pass identification, replacement for typeid 00053 explicit InternalizePass(bool AllButMain = true); 00054 explicit InternalizePass(const std::vector <const char *>& exportList); 00055 void LoadFile(const char *Filename); 00056 virtual bool runOnModule(Module &M); 00057 00058 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00059 AU.setPreservesCFG(); 00060 AU.addPreserved<CallGraph>(); 00061 } 00062 }; 00063 } // end anonymous namespace 00064 00065 char InternalizePass::ID = 0; 00066 static RegisterPass<InternalizePass> 00067 X("internalize", "Internalize Global Symbols"); 00068 00069 InternalizePass::InternalizePass(bool AllButMain) 00070 : ModulePass(&ID), AllButMain(AllButMain){ 00071 if (!APIFile.empty()) // If a filename is specified, use it. 00072 LoadFile(APIFile.c_str()); 00073 if (!APIList.empty()) // If a list is specified, use it as well. 00074 ExternalNames.insert(APIList.begin(), APIList.end()); 00075 } 00076 00077 InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 00078 : ModulePass(&ID), AllButMain(false){ 00079 for(std::vector<const char *>::const_iterator itr = exportList.begin(); 00080 itr != exportList.end(); itr++) { 00081 ExternalNames.insert(*itr); 00082 } 00083 } 00084 00085 void InternalizePass::LoadFile(const char *Filename) { 00086 // Load the APIFile... 00087 std::ifstream In(Filename); 00088 if (!In.good()) { 00089 cerr << "WARNING: Internalize couldn't load file '" << Filename 00090 << "'! Continuing as if it's empty.\n"; 00091 return; // Just continue as if the file were empty 00092 } 00093 while (In) { 00094 std::string Symbol; 00095 In >> Symbol; 00096 if (!Symbol.empty()) 00097 ExternalNames.insert(Symbol); 00098 } 00099 } 00100 00101 bool InternalizePass::runOnModule(Module &M) { 00102 CallGraph *CG = getAnalysisIfAvailable<CallGraph>(); 00103 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; 00104 00105 if (ExternalNames.empty()) { 00106 // Return if we're not in 'all but main' mode and have no external api 00107 if (!AllButMain) 00108 return false; 00109 // If no list or file of symbols was specified, check to see if there is a 00110 // "main" symbol defined in the module. If so, use it, otherwise do not 00111 // internalize the module, it must be a library or something. 00112 // 00113 Function *MainFunc = M.getFunction("main"); 00114 if (MainFunc == 0 || MainFunc->isDeclaration()) 00115 return false; // No main found, must be a library... 00116 00117 // Preserve main, internalize all else. 00118 ExternalNames.insert(MainFunc->getName()); 00119 } 00120 00121 bool Changed = false; 00122 00123 // Mark all functions not in the api as internal. 00124 // FIXME: maybe use private linkage? 00125 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00126 if (!I->isDeclaration() && // Function must be defined here 00127 !I->hasLocalLinkage() && // Can't already have internal linkage 00128 !ExternalNames.count(I->getName())) {// Not marked to keep external? 00129 I->setLinkage(GlobalValue::InternalLinkage); 00130 // Remove a callgraph edge from the external node to this function. 00131 if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); 00132 Changed = true; 00133 ++NumFunctions; 00134 DOUT << "Internalizing func " << I->getName() << "\n"; 00135 } 00136 00137 // Never internalize the llvm.used symbol. It is used to implement 00138 // attribute((used)). 00139 ExternalNames.insert("llvm.used"); 00140 00141 // Never internalize anchors used by the machine module info, else the info 00142 // won't find them. (see MachineModuleInfo.) 00143 ExternalNames.insert("llvm.dbg.compile_units"); 00144 ExternalNames.insert("llvm.dbg.global_variables"); 00145 ExternalNames.insert("llvm.dbg.subprograms"); 00146 ExternalNames.insert("llvm.global_ctors"); 00147 ExternalNames.insert("llvm.global_dtors"); 00148 ExternalNames.insert("llvm.noinline"); 00149 ExternalNames.insert("llvm.global.annotations"); 00150 00151 // Mark all global variables with initializers that are not in the api as 00152 // internal as well. 00153 // FIXME: maybe use private linkage? 00154 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 00155 I != E; ++I) 00156 if (!I->isDeclaration() && !I->hasLocalLinkage() && 00157 !ExternalNames.count(I->getName())) { 00158 I->setLinkage(GlobalValue::InternalLinkage); 00159 Changed = true; 00160 ++NumGlobals; 00161 DOUT << "Internalized gvar " << I->getName() << "\n"; 00162 } 00163 00164 // Mark all aliases that are not in the api as internal as well. 00165 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 00166 I != E; ++I) 00167 if (!I->isDeclaration() && !I->hasInternalLinkage() && 00168 !ExternalNames.count(I->getName())) { 00169 I->setLinkage(GlobalValue::InternalLinkage); 00170 Changed = true; 00171 ++NumAliases; 00172 DOUT << "Internalized alias " << I->getName() << "\n"; 00173 } 00174 00175 return Changed; 00176 } 00177 00178 ModulePass *llvm::createInternalizePass(bool AllButMain) { 00179 return new InternalizePass(AllButMain); 00180 } 00181 00182 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) { 00183 return new InternalizePass(el); 00184 }