1 //===-- Internalize.cpp - Mark functions internal -------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass loops over all of the functions and variables in the input module. 10 // If the function or variable does not need to be preserved according to the 11 // client supplied callback, it is marked as internal. 12 // 13 // This transformation would not be legal in a regular compilation, but it gets 14 // extra information from the linker about what is safe. 15 // 16 // For example: Internalizing a function with external linkage. Only if we are 17 // told it is only used from within this module, it is safe to do it. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "llvm/Transforms/IPO/Internalize.h" 22 #include "llvm/ADT/SmallPtrSet.h" 23 #include "llvm/ADT/Statistic.h" 24 #include "llvm/ADT/StringSet.h" 25 #include "llvm/Analysis/CallGraph.h" 26 #include "llvm/IR/Module.h" 27 #include "llvm/Pass.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/LineIterator.h" 31 #include "llvm/Support/MemoryBuffer.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include "llvm/Transforms/IPO.h" 34 #include "llvm/Transforms/Utils/GlobalStatus.h" 35 using namespace llvm; 36 37 #define DEBUG_TYPE "internalize" 38 39 STATISTIC(NumAliases, "Number of aliases internalized"); 40 STATISTIC(NumFunctions, "Number of functions internalized"); 41 STATISTIC(NumGlobals, "Number of global vars internalized"); 42 43 // APIFile - A file which contains a list of symbols that should not be marked 44 // external. 45 static cl::opt<std::string> 46 APIFile("internalize-public-api-file", cl::value_desc("filename"), 47 cl::desc("A file containing list of symbol names to preserve")); 48 49 // APIList - A list of symbols that should not be marked internal. 50 static cl::list<std::string> 51 APIList("internalize-public-api-list", cl::value_desc("list"), 52 cl::desc("A list of symbol names to preserve"), cl::CommaSeparated); 53 54 namespace { 55 // Helper to load an API list to preserve from file and expose it as a functor 56 // for internalization. 57 class PreserveAPIList { 58 public: 59 PreserveAPIList() { 60 if (!APIFile.empty()) 61 LoadFile(APIFile); 62 ExternalNames.insert(APIList.begin(), APIList.end()); 63 } 64 65 bool operator()(const GlobalValue &GV) { 66 return ExternalNames.count(GV.getName()); 67 } 68 69 private: 70 // Contains the set of symbols loaded from file 71 StringSet<> ExternalNames; 72 73 void LoadFile(StringRef Filename) { 74 // Load the APIFile... 75 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = 76 MemoryBuffer::getFile(Filename); 77 if (!Buf) { 78 errs() << "WARNING: Internalize couldn't load file '" << Filename 79 << "'! Continuing as if it's empty.\n"; 80 return; // Just continue as if the file were empty 81 } 82 for (line_iterator I(*Buf->get(), true), E; I != E; ++I) 83 ExternalNames.insert(*I); 84 } 85 }; 86 } // end anonymous namespace 87 88 bool InternalizePass::shouldPreserveGV(const GlobalValue &GV) { 89 // Function must be defined here 90 if (GV.isDeclaration()) 91 return true; 92 93 // Available externally is really just a "declaration with a body". 94 if (GV.hasAvailableExternallyLinkage()) 95 return true; 96 97 // Assume that dllexported symbols are referenced elsewhere 98 if (GV.hasDLLExportStorageClass()) 99 return true; 100 101 // Already local, has nothing to do. 102 if (GV.hasLocalLinkage()) 103 return false; 104 105 // Check some special cases 106 if (AlwaysPreserved.count(GV.getName())) 107 return true; 108 109 return MustPreserveGV(GV); 110 } 111 112 bool InternalizePass::maybeInternalize( 113 GlobalValue &GV, const DenseSet<const Comdat *> &ExternalComdats) { 114 if (Comdat *C = GV.getComdat()) { 115 if (ExternalComdats.count(C)) 116 return false; 117 118 // If a comdat is not externally visible we can drop it. 119 if (auto GO = dyn_cast<GlobalObject>(&GV)) 120 GO->setComdat(nullptr); 121 122 if (GV.hasLocalLinkage()) 123 return false; 124 } else { 125 if (GV.hasLocalLinkage()) 126 return false; 127 128 if (shouldPreserveGV(GV)) 129 return false; 130 } 131 132 GV.setVisibility(GlobalValue::DefaultVisibility); 133 GV.setLinkage(GlobalValue::InternalLinkage); 134 return true; 135 } 136 137 // If GV is part of a comdat and is externally visible, keep track of its 138 // comdat so that we don't internalize any of its members. 139 void InternalizePass::checkComdatVisibility( 140 GlobalValue &GV, DenseSet<const Comdat *> &ExternalComdats) { 141 Comdat *C = GV.getComdat(); 142 if (!C) 143 return; 144 145 if (shouldPreserveGV(GV)) 146 ExternalComdats.insert(C); 147 } 148 149 bool InternalizePass::internalizeModule(Module &M, CallGraph *CG) { 150 bool Changed = false; 151 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr; 152 153 SmallPtrSet<GlobalValue *, 8> Used; 154 collectUsedGlobalVariables(M, Used, false); 155 156 // Collect comdat visiblity information for the module. 157 DenseSet<const Comdat *> ExternalComdats; 158 if (!M.getComdatSymbolTable().empty()) { 159 for (Function &F : M) 160 checkComdatVisibility(F, ExternalComdats); 161 for (GlobalVariable &GV : M.globals()) 162 checkComdatVisibility(GV, ExternalComdats); 163 for (GlobalAlias &GA : M.aliases()) 164 checkComdatVisibility(GA, ExternalComdats); 165 } 166 167 // We must assume that globals in llvm.used have a reference that not even 168 // the linker can see, so we don't internalize them. 169 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and 170 // linker can drop those symbols. If this pass is running as part of LTO, 171 // one might think that it could just drop llvm.compiler.used. The problem 172 // is that even in LTO llvm doesn't see every reference. For example, 173 // we don't see references from function local inline assembly. To be 174 // conservative, we internalize symbols in llvm.compiler.used, but we 175 // keep llvm.compiler.used so that the symbol is not deleted by llvm. 176 for (GlobalValue *V : Used) { 177 AlwaysPreserved.insert(V->getName()); 178 } 179 180 // Mark all functions not in the api as internal. 181 for (Function &I : M) { 182 if (!maybeInternalize(I, ExternalComdats)) 183 continue; 184 Changed = true; 185 186 if (ExternalNode) 187 // Remove a callgraph edge from the external node to this function. 188 ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]); 189 190 ++NumFunctions; 191 LLVM_DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n"); 192 } 193 194 // Never internalize the llvm.used symbol. It is used to implement 195 // attribute((used)). 196 // FIXME: Shouldn't this just filter on llvm.metadata section?? 197 AlwaysPreserved.insert("llvm.used"); 198 AlwaysPreserved.insert("llvm.compiler.used"); 199 200 // Never internalize anchors used by the machine module info, else the info 201 // won't find them. (see MachineModuleInfo.) 202 AlwaysPreserved.insert("llvm.global_ctors"); 203 AlwaysPreserved.insert("llvm.global_dtors"); 204 AlwaysPreserved.insert("llvm.global.annotations"); 205 206 // Never internalize symbols code-gen inserts. 207 // FIXME: We should probably add this (and the __stack_chk_guard) via some 208 // type of call-back in CodeGen. 209 AlwaysPreserved.insert("__stack_chk_fail"); 210 AlwaysPreserved.insert("__stack_chk_guard"); 211 212 // Mark all global variables with initializers that are not in the api as 213 // internal as well. 214 for (auto &GV : M.globals()) { 215 if (!maybeInternalize(GV, ExternalComdats)) 216 continue; 217 Changed = true; 218 219 ++NumGlobals; 220 LLVM_DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n"); 221 } 222 223 // Mark all aliases that are not in the api as internal as well. 224 for (auto &GA : M.aliases()) { 225 if (!maybeInternalize(GA, ExternalComdats)) 226 continue; 227 Changed = true; 228 229 ++NumAliases; 230 LLVM_DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n"); 231 } 232 233 return Changed; 234 } 235 236 InternalizePass::InternalizePass() : MustPreserveGV(PreserveAPIList()) {} 237 238 PreservedAnalyses InternalizePass::run(Module &M, ModuleAnalysisManager &AM) { 239 if (!internalizeModule(M, AM.getCachedResult<CallGraphAnalysis>(M))) 240 return PreservedAnalyses::all(); 241 242 PreservedAnalyses PA; 243 PA.preserve<CallGraphAnalysis>(); 244 return PA; 245 } 246 247 namespace { 248 class InternalizeLegacyPass : public ModulePass { 249 // Client supplied callback to control wheter a symbol must be preserved. 250 std::function<bool(const GlobalValue &)> MustPreserveGV; 251 252 public: 253 static char ID; // Pass identification, replacement for typeid 254 255 InternalizeLegacyPass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {} 256 257 InternalizeLegacyPass(std::function<bool(const GlobalValue &)> MustPreserveGV) 258 : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) { 259 initializeInternalizeLegacyPassPass(*PassRegistry::getPassRegistry()); 260 } 261 262 bool runOnModule(Module &M) override { 263 if (skipModule(M)) 264 return false; 265 266 CallGraphWrapperPass *CGPass = 267 getAnalysisIfAvailable<CallGraphWrapperPass>(); 268 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr; 269 return internalizeModule(M, MustPreserveGV, CG); 270 } 271 272 void getAnalysisUsage(AnalysisUsage &AU) const override { 273 AU.setPreservesCFG(); 274 AU.addPreserved<CallGraphWrapperPass>(); 275 } 276 }; 277 } 278 279 char InternalizeLegacyPass::ID = 0; 280 INITIALIZE_PASS(InternalizeLegacyPass, "internalize", 281 "Internalize Global Symbols", false, false) 282 283 ModulePass *llvm::createInternalizePass() { 284 return new InternalizeLegacyPass(); 285 } 286 287 ModulePass *llvm::createInternalizePass( 288 std::function<bool(const GlobalValue &)> MustPreserveGV) { 289 return new InternalizeLegacyPass(std::move(MustPreserveGV)); 290 } 291