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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 #include "llvm/Transforms/IPO.h"
00017 #include "llvm/Pass.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/Support/CommandLine.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include <fstream>
00023 #include <iostream>
00024 #include <set>
00025 using namespace llvm;
00026 
00027 namespace {
00028   Statistic<> NumFunctions("internalize", "Number of functions internalized");
00029   Statistic<> NumGlobals  ("internalize", "Number of global vars internalized");
00030 
00031   // APIFile - A file which contains a list of symbols that should not be marked
00032   // external.
00033   cl::opt<std::string>
00034   APIFile("internalize-public-api-file", cl::value_desc("filename"),
00035           cl::desc("A file containing list of symbol names to preserve"));
00036 
00037   // APIList - A list of symbols that should not be marked internal.
00038   cl::list<std::string>
00039   APIList("internalize-public-api-list", cl::value_desc("list"),
00040           cl::desc("A list of symbol names to preserve"),
00041           cl::CommaSeparated);
00042 
00043   class InternalizePass : public ModulePass {
00044     std::set<std::string> ExternalNames;
00045     bool DontInternalize;
00046   public:
00047     InternalizePass(bool InternalizeEverything = true);
00048     void LoadFile(const char *Filename);
00049     virtual bool runOnModule(Module &M);
00050   };
00051   RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
00052 } // end anonymous namespace
00053 
00054 InternalizePass::InternalizePass(bool InternalizeEverything) 
00055   : DontInternalize(false){
00056   if (!APIFile.empty())           // If a filename is specified, use it
00057     LoadFile(APIFile.c_str());
00058   else if (!APIList.empty())      // Else, if a list is specified, use it.
00059     ExternalNames.insert(APIList.begin(), APIList.end());
00060   else if (!InternalizeEverything)
00061     // Finally, if we're allowed to, internalize all but main.
00062     DontInternalize = true;
00063 }
00064 
00065 void InternalizePass::LoadFile(const char *Filename) {
00066   // Load the APIFile...
00067   std::ifstream In(Filename);
00068   if (!In.good()) {
00069     std::cerr << "WARNING: Internalize couldn't load file '" << Filename
00070     << "'!\n";
00071     return;   // Do not internalize anything...
00072   }
00073   while (In) {
00074     std::string Symbol;
00075     In >> Symbol;
00076     if (!Symbol.empty())
00077       ExternalNames.insert(Symbol);
00078   }
00079 }
00080 
00081 bool InternalizePass::runOnModule(Module &M) {
00082   if (DontInternalize) return false;
00083   
00084   // If no list or file of symbols was specified, check to see if there is a
00085   // "main" symbol defined in the module.  If so, use it, otherwise do not
00086   // internalize the module, it must be a library or something.
00087   //
00088   if (ExternalNames.empty()) {
00089     Function *MainFunc = M.getMainFunction();
00090     if (MainFunc == 0 || MainFunc->isExternal())
00091       return false;  // No main found, must be a library...
00092     
00093     // Preserve main, internalize all else.
00094     ExternalNames.insert(MainFunc->getName());
00095   }
00096   
00097   bool Changed = false;
00098   
00099   // Found a main function, mark all functions not named main as internal.
00100   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00101     if (!I->isExternal() &&         // Function must be defined here
00102         !I->hasInternalLinkage() &&  // Can't already have internal linkage
00103         !ExternalNames.count(I->getName())) {// Not marked to keep external?
00104       I->setLinkage(GlobalValue::InternalLinkage);
00105       Changed = true;
00106       ++NumFunctions;
00107       DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
00108     }
00109   
00110   // Never internalize the llvm.used symbol.  It is used to implement
00111   // attribute((used)).
00112   ExternalNames.insert("llvm.used");
00113   
00114   // Never internalize anchors used by the debugger, else the debugger won't
00115   // find them.  (see MachineDebugInfo.)
00116   ExternalNames.insert("llvm.dbg.compile_units");
00117   ExternalNames.insert("llvm.dbg.global_variables");
00118   ExternalNames.insert("llvm.dbg.subprograms");
00119       
00120   // Mark all global variables with initializers as internal as well.
00121   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00122        I != E; ++I)
00123     if (!I->isExternal() && !I->hasInternalLinkage() &&
00124         !ExternalNames.count(I->getName())) {
00125       // Special case handling of the global ctor and dtor list.  When we
00126       // internalize it, we mark it constant, which allows elimination of
00127       // the list if it's empty.
00128       //
00129       if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
00130                                        I->getName() == "llvm.global_dtors")) {
00131         // If the global ctors/dtors list has no uses, do not internalize it, as
00132         // there is no __main in this program, so the asmprinter should handle
00133         // it.
00134         if (I->use_empty()) continue;
00135  
00136         // Otherwise, also mark the list constant, as we know that it will not
00137         // be mutated any longer, and the makes simple IPO xforms automatically
00138         // better.
00139         I->setConstant(true);
00140       }
00141       
00142       I->setLinkage(GlobalValue::InternalLinkage);
00143       Changed = true;
00144       ++NumGlobals;
00145       DEBUG(std::cerr << "Internalized gvar " << I->getName() << "\n");
00146     }
00147       
00148   return Changed;
00149 }
00150 
00151 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
00152   return new InternalizePass(InternalizeEverything);
00153 }