Internalize.cpp revision 775079c227083be3fe22f6ae071d5b74a7ade745
1//===-- Internalize.cpp - Mark functions internal -------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This pass loops over all of the functions and variables in the input module. 11// If the function or variable is not in the list of external names given to 12// the pass it is marked as internal. 13// 14//===----------------------------------------------------------------------===// 15 16#define DEBUG_TYPE "internalize" 17#include "llvm/Transforms/IPO.h" 18#include "llvm/ADT/SmallPtrSet.h" 19#include "llvm/ADT/Statistic.h" 20#include "llvm/Analysis/CallGraph.h" 21#include "llvm/IR/Module.h" 22#include "llvm/Pass.h" 23#include "llvm/Support/CommandLine.h" 24#include "llvm/Support/Debug.h" 25#include "llvm/Support/raw_ostream.h" 26#include "llvm/Transforms/Utils/ModuleUtils.h" 27#include <fstream> 28#include <set> 29using namespace llvm; 30 31STATISTIC(NumAliases , "Number of aliases internalized"); 32STATISTIC(NumFunctions, "Number of functions internalized"); 33STATISTIC(NumGlobals , "Number of global vars internalized"); 34 35// APIFile - A file which contains a list of symbols that should not be marked 36// external. 37static cl::opt<std::string> 38APIFile("internalize-public-api-file", cl::value_desc("filename"), 39 cl::desc("A file containing list of symbol names to preserve")); 40 41// APIList - A list of symbols that should not be marked internal. 42static cl::list<std::string> 43APIList("internalize-public-api-list", cl::value_desc("list"), 44 cl::desc("A list of symbol names to preserve"), 45 cl::CommaSeparated); 46 47namespace { 48 class InternalizePass : public ModulePass { 49 std::set<std::string> ExternalNames; 50 public: 51 static char ID; // Pass identification, replacement for typeid 52 explicit InternalizePass(); 53 explicit InternalizePass(ArrayRef<const char *> ExportList); 54 void LoadFile(const char *Filename); 55 virtual bool runOnModule(Module &M); 56 57 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 58 AU.setPreservesCFG(); 59 AU.addPreserved<CallGraph>(); 60 } 61 }; 62} // end anonymous namespace 63 64char InternalizePass::ID = 0; 65INITIALIZE_PASS(InternalizePass, "internalize", 66 "Internalize Global Symbols", false, false) 67 68InternalizePass::InternalizePass() 69 : ModulePass(ID) { 70 initializeInternalizePassPass(*PassRegistry::getPassRegistry()); 71 if (!APIFile.empty()) // If a filename is specified, use it. 72 LoadFile(APIFile.c_str()); 73 ExternalNames.insert(APIList.begin(), APIList.end()); 74} 75 76InternalizePass::InternalizePass(ArrayRef<const char *> ExportList) 77 : ModulePass(ID){ 78 initializeInternalizePassPass(*PassRegistry::getPassRegistry()); 79 for(ArrayRef<const char *>::const_iterator itr = ExportList.begin(); 80 itr != ExportList.end(); itr++) { 81 ExternalNames.insert(*itr); 82 } 83} 84 85void InternalizePass::LoadFile(const char *Filename) { 86 // Load the APIFile... 87 std::ifstream In(Filename); 88 if (!In.good()) { 89 errs() << "WARNING: Internalize couldn't load file '" << Filename 90 << "'! Continuing as if it's empty.\n"; 91 return; // Just continue as if the file were empty 92 } 93 while (In) { 94 std::string Symbol; 95 In >> Symbol; 96 if (!Symbol.empty()) 97 ExternalNames.insert(Symbol); 98 } 99} 100 101static bool shouldInternalize(const GlobalValue &GV, 102 const std::set<std::string> &ExternalNames) { 103 // Function must be defined here 104 if (GV.isDeclaration()) 105 return false; 106 107 // Available externally is really just a "declaration with a body". 108 if (GV.hasAvailableExternallyLinkage()) 109 return false; 110 111 // Already has internal linkage 112 if (GV.hasLocalLinkage()) 113 return false; 114 115 // Marked to keep external? 116 if (ExternalNames.count(GV.getName())) 117 return false; 118 119 return true; 120} 121 122bool InternalizePass::runOnModule(Module &M) { 123 CallGraph *CG = getAnalysisIfAvailable<CallGraph>(); 124 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; 125 bool Changed = false; 126 127 SmallPtrSet<GlobalValue *, 8> Used; 128 collectUsedGlobalVariables(M, Used, false); 129 130 // We must assume that globals in llvm.used have a reference that not even 131 // the linker can see, so we don't internalize them. 132 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and 133 // linker can drop those symbols. If this pass is running as part of LTO, 134 // one might think that it could just drop llvm.compiler.used. The problem 135 // is that even in LTO llvm doesn't see every reference. For example, 136 // we don't see references from function local inline assembly. To be 137 // conservative, we internalize symbols in llvm.compiler.used, but we 138 // keep llvm.compiler.used so that the symbol is not deleted by llvm. 139 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end(); 140 I != E; ++I) { 141 GlobalValue *V = *I; 142 ExternalNames.insert(V->getName()); 143 } 144 145 // Mark all functions not in the api as internal. 146 // FIXME: maybe use private linkage? 147 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 148 if (!shouldInternalize(*I, ExternalNames)) 149 continue; 150 151 I->setLinkage(GlobalValue::InternalLinkage); 152 153 if (ExternalNode) 154 // Remove a callgraph edge from the external node to this function. 155 ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); 156 157 Changed = true; 158 ++NumFunctions; 159 DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n"); 160 } 161 162 // Never internalize the llvm.used symbol. It is used to implement 163 // attribute((used)). 164 // FIXME: Shouldn't this just filter on llvm.metadata section?? 165 ExternalNames.insert("llvm.used"); 166 ExternalNames.insert("llvm.compiler.used"); 167 168 // Never internalize anchors used by the machine module info, else the info 169 // won't find them. (see MachineModuleInfo.) 170 ExternalNames.insert("llvm.global_ctors"); 171 ExternalNames.insert("llvm.global_dtors"); 172 ExternalNames.insert("llvm.global.annotations"); 173 174 // Never internalize symbols code-gen inserts. 175 // FIXME: We should probably add this (and the __stack_chk_guard) via some 176 // type of call-back in CodeGen. 177 ExternalNames.insert("__stack_chk_fail"); 178 ExternalNames.insert("__stack_chk_guard"); 179 180 // Mark all global variables with initializers that are not in the api as 181 // internal as well. 182 // FIXME: maybe use private linkage? 183 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 184 I != E; ++I) { 185 if (!shouldInternalize(*I, ExternalNames)) 186 continue; 187 188 I->setLinkage(GlobalValue::InternalLinkage); 189 Changed = true; 190 ++NumGlobals; 191 DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n"); 192 } 193 194 // Mark all aliases that are not in the api as internal as well. 195 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 196 I != E; ++I) { 197 if (!shouldInternalize(*I, ExternalNames)) 198 continue; 199 200 I->setLinkage(GlobalValue::InternalLinkage); 201 Changed = true; 202 ++NumAliases; 203 DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n"); 204 } 205 206 return Changed; 207} 208 209ModulePass *llvm::createInternalizePass() { 210 return new InternalizePass(); 211} 212 213ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) { 214 return new InternalizePass(ExportList); 215} 216