LLVM  mainline
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 and variables in the input module.
00011 // If the function or variable is not in the list of external names given to
00012 // the pass it is marked as internal.
00013 //
00014 // This transformation would not be legal in a regular compilation, but it gets
00015 // extra information from the linker about what is safe.
00016 //
00017 // For example: Internalizing a function with external linkage. Only if we are
00018 // told it is only used from within this module, it is safe to do it.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "llvm/Transforms/IPO.h"
00023 #include "llvm/ADT/SmallPtrSet.h"
00024 #include "llvm/ADT/Statistic.h"
00025 #include "llvm/Analysis/CallGraph.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/Pass.h"
00028 #include "llvm/Support/CommandLine.h"
00029 #include "llvm/Support/Debug.h"
00030 #include "llvm/Support/raw_ostream.h"
00031 #include "llvm/Transforms/Utils/GlobalStatus.h"
00032 #include "llvm/Transforms/Utils/ModuleUtils.h"
00033 #include <fstream>
00034 #include <set>
00035 using namespace llvm;
00036 
00037 #define DEBUG_TYPE "internalize"
00038 
00039 STATISTIC(NumAliases  , "Number of aliases internalized");
00040 STATISTIC(NumFunctions, "Number of functions internalized");
00041 STATISTIC(NumGlobals  , "Number of global vars internalized");
00042 
00043 // APIFile - A file which contains a list of symbols that should not be marked
00044 // external.
00045 static cl::opt<std::string>
00046 APIFile("internalize-public-api-file", cl::value_desc("filename"),
00047         cl::desc("A file containing list of symbol names to preserve"));
00048 
00049 // APIList - A list of symbols that should not be marked internal.
00050 static cl::list<std::string>
00051 APIList("internalize-public-api-list", cl::value_desc("list"),
00052         cl::desc("A list of symbol names to preserve"),
00053         cl::CommaSeparated);
00054 
00055 namespace {
00056   class InternalizePass : public ModulePass {
00057     std::set<std::string> ExternalNames;
00058   public:
00059     static char ID; // Pass identification, replacement for typeid
00060     explicit InternalizePass();
00061     explicit InternalizePass(ArrayRef<const char *> ExportList);
00062     void LoadFile(const char *Filename);
00063     bool maybeInternalize(GlobalValue &GV,
00064                           const std::set<const Comdat *> &ExternalComdats);
00065     void checkComdatVisibility(GlobalValue &GV,
00066                                std::set<const Comdat *> &ExternalComdats);
00067     bool runOnModule(Module &M) override;
00068 
00069     void getAnalysisUsage(AnalysisUsage &AU) const override {
00070       AU.setPreservesCFG();
00071       AU.addPreserved<CallGraphWrapperPass>();
00072     }
00073   };
00074 } // end anonymous namespace
00075 
00076 char InternalizePass::ID = 0;
00077 INITIALIZE_PASS(InternalizePass, "internalize",
00078                 "Internalize Global Symbols", false, false)
00079 
00080 InternalizePass::InternalizePass() : ModulePass(ID) {
00081   initializeInternalizePassPass(*PassRegistry::getPassRegistry());
00082   if (!APIFile.empty())           // If a filename is specified, use it.
00083     LoadFile(APIFile.c_str());
00084   ExternalNames.insert(APIList.begin(), APIList.end());
00085 }
00086 
00087 InternalizePass::InternalizePass(ArrayRef<const char *> ExportList)
00088     : ModulePass(ID) {
00089   initializeInternalizePassPass(*PassRegistry::getPassRegistry());
00090   for(ArrayRef<const char *>::const_iterator itr = ExportList.begin();
00091         itr != ExportList.end(); itr++) {
00092     ExternalNames.insert(*itr);
00093   }
00094 }
00095 
00096 void InternalizePass::LoadFile(const char *Filename) {
00097   // Load the APIFile...
00098   std::ifstream In(Filename);
00099   if (!In.good()) {
00100     errs() << "WARNING: Internalize couldn't load file '" << Filename
00101          << "'! Continuing as if it's empty.\n";
00102     return; // Just continue as if the file were empty
00103   }
00104   while (In) {
00105     std::string Symbol;
00106     In >> Symbol;
00107     if (!Symbol.empty())
00108       ExternalNames.insert(Symbol);
00109   }
00110 }
00111 
00112 static bool isExternallyVisible(const GlobalValue &GV,
00113                                 const std::set<std::string> &ExternalNames) {
00114   // Function must be defined here
00115   if (GV.isDeclaration())
00116     return true;
00117 
00118   // Available externally is really just a "declaration with a body".
00119   if (GV.hasAvailableExternallyLinkage())
00120     return true;
00121 
00122   // Assume that dllexported symbols are referenced elsewhere
00123   if (GV.hasDLLExportStorageClass())
00124     return true;
00125 
00126   // Marked to keep external?
00127   if (!GV.hasLocalLinkage() && ExternalNames.count(GV.getName()))
00128     return true;
00129 
00130   return false;
00131 }
00132 
00133 // Internalize GV if it is possible to do so, i.e. it is not externally visible
00134 // and is not a member of an externally visible comdat.
00135 bool InternalizePass::maybeInternalize(
00136     GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) {
00137   if (Comdat *C = GV.getComdat()) {
00138     if (ExternalComdats.count(C))
00139       return false;
00140 
00141     // If a comdat is not externally visible we can drop it.
00142     if (auto GO = dyn_cast<GlobalObject>(&GV))
00143       GO->setComdat(nullptr);
00144 
00145     if (GV.hasLocalLinkage())
00146       return false;
00147   } else {
00148     if (GV.hasLocalLinkage())
00149       return false;
00150 
00151     if (isExternallyVisible(GV, ExternalNames))
00152       return false;
00153   }
00154 
00155   GV.setVisibility(GlobalValue::DefaultVisibility);
00156   GV.setLinkage(GlobalValue::InternalLinkage);
00157   return true;
00158 }
00159 
00160 // If GV is part of a comdat and is externally visible, keep track of its
00161 // comdat so that we don't internalize any of its members.
00162 void InternalizePass::checkComdatVisibility(
00163     GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
00164   Comdat *C = GV.getComdat();
00165   if (!C)
00166     return;
00167 
00168   if (isExternallyVisible(GV, ExternalNames))
00169     ExternalComdats.insert(C);
00170 }
00171 
00172 bool InternalizePass::runOnModule(Module &M) {
00173   CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>();
00174   CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
00175   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
00176 
00177   SmallPtrSet<GlobalValue *, 8> Used;
00178   collectUsedGlobalVariables(M, Used, false);
00179 
00180   // Collect comdat visiblity information for the module.
00181   std::set<const Comdat *> ExternalComdats;
00182   if (!M.getComdatSymbolTable().empty()) {
00183     for (Function &F : M)
00184       checkComdatVisibility(F, ExternalComdats);
00185     for (GlobalVariable &GV : M.globals())
00186       checkComdatVisibility(GV, ExternalComdats);
00187     for (GlobalAlias &GA : M.aliases())
00188       checkComdatVisibility(GA, ExternalComdats);
00189   }
00190 
00191   // We must assume that globals in llvm.used have a reference that not even
00192   // the linker can see, so we don't internalize them.
00193   // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
00194   // linker can drop those symbols. If this pass is running as part of LTO,
00195   // one might think that it could just drop llvm.compiler.used. The problem
00196   // is that even in LTO llvm doesn't see every reference. For example,
00197   // we don't see references from function local inline assembly. To be
00198   // conservative, we internalize symbols in llvm.compiler.used, but we
00199   // keep llvm.compiler.used so that the symbol is not deleted by llvm.
00200   for (GlobalValue *V : Used) {
00201     ExternalNames.insert(V->getName());
00202   }
00203 
00204   // Mark all functions not in the api as internal.
00205   for (Function &I : M) {
00206     if (!maybeInternalize(I, ExternalComdats))
00207       continue;
00208 
00209     if (ExternalNode)
00210       // Remove a callgraph edge from the external node to this function.
00211       ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]);
00212 
00213     ++NumFunctions;
00214     DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
00215   }
00216 
00217   // Never internalize the llvm.used symbol.  It is used to implement
00218   // attribute((used)).
00219   // FIXME: Shouldn't this just filter on llvm.metadata section??
00220   ExternalNames.insert("llvm.used");
00221   ExternalNames.insert("llvm.compiler.used");
00222 
00223   // Never internalize anchors used by the machine module info, else the info
00224   // won't find them.  (see MachineModuleInfo.)
00225   ExternalNames.insert("llvm.global_ctors");
00226   ExternalNames.insert("llvm.global_dtors");
00227   ExternalNames.insert("llvm.global.annotations");
00228 
00229   // Never internalize symbols code-gen inserts.
00230   // FIXME: We should probably add this (and the __stack_chk_guard) via some
00231   // type of call-back in CodeGen.
00232   ExternalNames.insert("__stack_chk_fail");
00233   ExternalNames.insert("__stack_chk_guard");
00234 
00235   // Mark all global variables with initializers that are not in the api as
00236   // internal as well.
00237   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00238        I != E; ++I) {
00239     if (!maybeInternalize(*I, ExternalComdats))
00240       continue;
00241 
00242     ++NumGlobals;
00243     DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
00244   }
00245 
00246   // Mark all aliases that are not in the api as internal as well.
00247   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
00248        I != E; ++I) {
00249     if (!maybeInternalize(*I, ExternalComdats))
00250       continue;
00251 
00252     ++NumAliases;
00253     DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
00254   }
00255 
00256   // We do not keep track of whether this pass changed the module because
00257   // it adds unnecessary complexity:
00258   // 1) This pass will generally be near the start of the pass pipeline, so
00259   //    there will be no analyses to invalidate.
00260   // 2) This pass will most likely end up changing the module and it isn't worth
00261   //    worrying about optimizing the case where the module is unchanged.
00262   return true;
00263 }
00264 
00265 ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
00266 
00267 ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) {
00268   return new InternalizePass(ExportList);
00269 }