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/Statistic.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/Analysis/CallGraph.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/InitializePasses.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/LineIterator.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Transforms/IPO.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   // As the name suggests, externally initialized variables need preserving as
102   // they would be initialized elsewhere externally.
103   if (const auto *G = dyn_cast<GlobalVariable>(&GV))
104     if (G->isExternallyInitialized())
105       return true;
106 
107   // Already local, has nothing to do.
108   if (GV.hasLocalLinkage())
109     return false;
110 
111   // Check some special cases
112   if (AlwaysPreserved.count(GV.getName()))
113     return true;
114 
115   return MustPreserveGV(GV);
116 }
117 
118 bool InternalizePass::maybeInternalize(
119     GlobalValue &GV, DenseMap<const Comdat *, ComdatInfo> &ComdatMap) {
120   SmallString<0> ComdatName;
121   if (Comdat *C = GV.getComdat()) {
122     // For GlobalAlias, C is the aliasee object's comdat which may have been
123     // redirected. So ComdatMap may not contain C.
124     if (ComdatMap.lookup(C).External)
125       return false;
126 
127     if (auto *GO = dyn_cast<GlobalObject>(&GV)) {
128       // If a comdat with one member is not externally visible, we can drop it.
129       // Otherwise, the comdat can be used to establish dependencies among the
130       // group of sections. Thus we have to keep the comdat but switch it to
131       // nodeduplicate.
132       // Note: nodeduplicate is not necessary for COFF. wasm doesn't support
133       // nodeduplicate.
134       ComdatInfo &Info = ComdatMap.find(C)->second;
135       if (Info.Size == 1)
136         GO->setComdat(nullptr);
137       else if (!IsWasm)
138         C->setSelectionKind(Comdat::NoDeduplicate);
139     }
140 
141     if (GV.hasLocalLinkage())
142       return false;
143   } else {
144     if (GV.hasLocalLinkage())
145       return false;
146 
147     if (shouldPreserveGV(GV))
148       return false;
149   }
150 
151   GV.setVisibility(GlobalValue::DefaultVisibility);
152   GV.setLinkage(GlobalValue::InternalLinkage);
153   return true;
154 }
155 
156 // If GV is part of a comdat and is externally visible, update the comdat size
157 // and keep track of its comdat so that we don't internalize any of its members.
158 void InternalizePass::checkComdat(
159     GlobalValue &GV, DenseMap<const Comdat *, ComdatInfo> &ComdatMap) {
160   Comdat *C = GV.getComdat();
161   if (!C)
162     return;
163 
164   ComdatInfo &Info = ComdatMap.try_emplace(C).first->second;
165   ++Info.Size;
166   if (shouldPreserveGV(GV))
167     Info.External = true;
168 }
169 
170 bool InternalizePass::internalizeModule(Module &M, CallGraph *CG) {
171   bool Changed = false;
172   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
173 
174   SmallVector<GlobalValue *, 4> Used;
175   collectUsedGlobalVariables(M, Used, false);
176 
177   // Collect comdat size and visiblity information for the module.
178   DenseMap<const Comdat *, ComdatInfo> ComdatMap;
179   if (!M.getComdatSymbolTable().empty()) {
180     for (Function &F : M)
181       checkComdat(F, ComdatMap);
182     for (GlobalVariable &GV : M.globals())
183       checkComdat(GV, ComdatMap);
184     for (GlobalAlias &GA : M.aliases())
185       checkComdat(GA, ComdatMap);
186   }
187 
188   // We must assume that globals in llvm.used have a reference that not even
189   // the linker can see, so we don't internalize them.
190   // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
191   // linker can drop those symbols. If this pass is running as part of LTO,
192   // one might think that it could just drop llvm.compiler.used. The problem
193   // is that even in LTO llvm doesn't see every reference. For example,
194   // we don't see references from function local inline assembly. To be
195   // conservative, we internalize symbols in llvm.compiler.used, but we
196   // keep llvm.compiler.used so that the symbol is not deleted by llvm.
197   for (GlobalValue *V : Used) {
198     AlwaysPreserved.insert(V->getName());
199   }
200 
201   // Never internalize the llvm.used symbol.  It is used to implement
202   // attribute((used)).
203   // FIXME: Shouldn't this just filter on llvm.metadata section??
204   AlwaysPreserved.insert("llvm.used");
205   AlwaysPreserved.insert("llvm.compiler.used");
206 
207   // Never internalize anchors used by the machine module info, else the info
208   // won't find them.  (see MachineModuleInfo.)
209   AlwaysPreserved.insert("llvm.global_ctors");
210   AlwaysPreserved.insert("llvm.global_dtors");
211   AlwaysPreserved.insert("llvm.global.annotations");
212 
213   // Never internalize symbols code-gen inserts.
214   // FIXME: We should probably add this (and the __stack_chk_guard) via some
215   // type of call-back in CodeGen.
216   AlwaysPreserved.insert("__stack_chk_fail");
217   if (Triple(M.getTargetTriple()).isOSAIX())
218     AlwaysPreserved.insert("__ssp_canary_word");
219   else
220     AlwaysPreserved.insert("__stack_chk_guard");
221 
222   // Mark all functions not in the api as internal.
223   IsWasm = Triple(M.getTargetTriple()).isOSBinFormatWasm();
224   for (Function &I : M) {
225     if (!maybeInternalize(I, ComdatMap))
226       continue;
227     Changed = true;
228 
229     if (ExternalNode)
230       // Remove a callgraph edge from the external node to this function.
231       ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]);
232 
233     ++NumFunctions;
234     LLVM_DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
235   }
236 
237   // Mark all global variables with initializers that are not in the api as
238   // internal as well.
239   for (auto &GV : M.globals()) {
240     if (!maybeInternalize(GV, ComdatMap))
241       continue;
242     Changed = true;
243 
244     ++NumGlobals;
245     LLVM_DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n");
246   }
247 
248   // Mark all aliases that are not in the api as internal as well.
249   for (auto &GA : M.aliases()) {
250     if (!maybeInternalize(GA, ComdatMap))
251       continue;
252     Changed = true;
253 
254     ++NumAliases;
255     LLVM_DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n");
256   }
257 
258   return Changed;
259 }
260 
261 InternalizePass::InternalizePass() : MustPreserveGV(PreserveAPIList()) {}
262 
263 PreservedAnalyses InternalizePass::run(Module &M, ModuleAnalysisManager &AM) {
264   if (!internalizeModule(M, AM.getCachedResult<CallGraphAnalysis>(M)))
265     return PreservedAnalyses::all();
266 
267   PreservedAnalyses PA;
268   PA.preserve<CallGraphAnalysis>();
269   return PA;
270 }
271 
272 namespace {
273 class InternalizeLegacyPass : public ModulePass {
274   // Client supplied callback to control wheter a symbol must be preserved.
275   std::function<bool(const GlobalValue &)> MustPreserveGV;
276 
277 public:
278   static char ID; // Pass identification, replacement for typeid
279 
280   InternalizeLegacyPass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
281 
282   InternalizeLegacyPass(std::function<bool(const GlobalValue &)> MustPreserveGV)
283       : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
284     initializeInternalizeLegacyPassPass(*PassRegistry::getPassRegistry());
285   }
286 
287   bool runOnModule(Module &M) override {
288     if (skipModule(M))
289       return false;
290 
291     CallGraphWrapperPass *CGPass =
292         getAnalysisIfAvailable<CallGraphWrapperPass>();
293     CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
294     return internalizeModule(M, MustPreserveGV, CG);
295   }
296 
297   void getAnalysisUsage(AnalysisUsage &AU) const override {
298     AU.setPreservesCFG();
299     AU.addPreserved<CallGraphWrapperPass>();
300   }
301 };
302 }
303 
304 char InternalizeLegacyPass::ID = 0;
305 INITIALIZE_PASS(InternalizeLegacyPass, "internalize",
306                 "Internalize Global Symbols", false, false)
307 
308 ModulePass *llvm::createInternalizePass() {
309   return new InternalizeLegacyPass();
310 }
311 
312 ModulePass *llvm::createInternalizePass(
313     std::function<bool(const GlobalValue &)> MustPreserveGV) {
314   return new InternalizeLegacyPass(std::move(MustPreserveGV));
315 }
316