LLVM API Documentation

Internalize.cpp

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