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     InternalizePass(const std::vector <const char *>& exportList);
00049     void LoadFile(const char *Filename);
00050     virtual bool runOnModule(Module &M);
00051   };
00052   RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
00053 } // end anonymous namespace
00054 
00055 InternalizePass::InternalizePass(bool InternalizeEverything) 
00056   : DontInternalize(false){
00057   if (!APIFile.empty())           // If a filename is specified, use it
00058     LoadFile(APIFile.c_str());
00059   else if (!APIList.empty())      // Else, if a list is specified, use it.
00060     ExternalNames.insert(APIList.begin(), APIList.end());
00061   else if (!InternalizeEverything)
00062     // Finally, if we're allowed to, internalize all but main.
00063     DontInternalize = true;
00064 }
00065 
00066 InternalizePass::InternalizePass(const std::vector<const char *>&exportList) {
00067   for(std::vector<const char *>::const_iterator itr = exportList.begin();
00068   itr != exportList.end(); itr++) {
00069     ExternalNames.insert(*itr);
00070   }
00071 }
00072 
00073 void InternalizePass::LoadFile(const char *Filename) {
00074   // Load the APIFile...
00075   std::ifstream In(Filename);
00076   if (!In.good()) {
00077     std::cerr << "WARNING: Internalize couldn't load file '" << Filename
00078     << "'!\n";
00079     return;   // Do not internalize anything...
00080   }
00081   while (In) {
00082     std::string Symbol;
00083     In >> Symbol;
00084     if (!Symbol.empty())
00085       ExternalNames.insert(Symbol);
00086   }
00087 }
00088 
00089 bool InternalizePass::runOnModule(Module &M) {
00090   if (DontInternalize) return false;
00091   
00092   // If no list or file of symbols was specified, check to see if there is a
00093   // "main" symbol defined in the module.  If so, use it, otherwise do not
00094   // internalize the module, it must be a library or something.
00095   //
00096   if (ExternalNames.empty()) {
00097     Function *MainFunc = M.getMainFunction();
00098     if (MainFunc == 0 || MainFunc->isExternal())
00099       return false;  // No main found, must be a library...
00100     
00101     // Preserve main, internalize all else.
00102     ExternalNames.insert(MainFunc->getName());
00103   }
00104   
00105   bool Changed = false;
00106   
00107   // Found a main function, mark all functions not named main as internal.
00108   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00109     if (!I->isExternal() &&         // Function must be defined here
00110         !I->hasInternalLinkage() &&  // Can't already have internal linkage
00111         !ExternalNames.count(I->getName())) {// Not marked to keep external?
00112       I->setLinkage(GlobalValue::InternalLinkage);
00113       Changed = true;
00114       ++NumFunctions;
00115       DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
00116     }
00117   
00118   // Never internalize the llvm.used symbol.  It is used to implement
00119   // attribute((used)).
00120   ExternalNames.insert("llvm.used");
00121   
00122   // Never internalize anchors used by the debugger, else the debugger won't
00123   // find them.  (see MachineDebugInfo.)
00124   ExternalNames.insert("llvm.dbg.compile_units");
00125   ExternalNames.insert("llvm.dbg.global_variables");
00126   ExternalNames.insert("llvm.dbg.subprograms");
00127       
00128   // Mark all global variables with initializers as internal as well.
00129   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00130        I != E; ++I)
00131     if (!I->isExternal() && !I->hasInternalLinkage() &&
00132         !ExternalNames.count(I->getName())) {
00133       // Special case handling of the global ctor and dtor list.  When we
00134       // internalize it, we mark it constant, which allows elimination of
00135       // the list if it's empty.
00136       //
00137       if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
00138                                        I->getName() == "llvm.global_dtors")) {
00139         // If the global ctors/dtors list has no uses, do not internalize it, as
00140         // there is no __main in this program, so the asmprinter should handle
00141         // it.
00142         if (I->use_empty()) continue;
00143  
00144         // Otherwise, also mark the list constant, as we know that it will not
00145         // be mutated any longer, and the makes simple IPO xforms automatically
00146         // better.
00147         I->setConstant(true);
00148       }
00149       
00150       I->setLinkage(GlobalValue::InternalLinkage);
00151       Changed = true;
00152       ++NumGlobals;
00153       DEBUG(std::cerr << "Internalized gvar " << I->getName() << "\n");
00154     }
00155       
00156   return Changed;
00157 }
00158 
00159 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
00160   return new InternalizePass(InternalizeEverything);
00161 }
00162 
00163 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
00164   return new InternalizePass(el);
00165 }