1 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
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 transform is designed to eliminate unreachable internal globals from the
10 // program.  It uses an aggressive algorithm, searching out globals that are
11 // known to be alive.  After it finds all of the globals which are needed, it
12 // deletes whatever is left over.  This allows it to delete recursive chunks of
13 // the program which are unreachable.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Transforms/IPO/GlobalDCE.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/TypeMetadataUtils.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/InitializePasses.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Transforms/IPO.h"
29 #include "llvm/Transforms/Utils/CtorUtils.h"
30 #include "llvm/Transforms/Utils/GlobalStatus.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "globaldce"
35 
36 static cl::opt<bool>
37     ClEnableVFE("enable-vfe", cl::Hidden, cl::init(true), cl::ZeroOrMore,
38                 cl::desc("Enable virtual function elimination"));
39 
40 STATISTIC(NumAliases  , "Number of global aliases removed");
41 STATISTIC(NumFunctions, "Number of functions removed");
42 STATISTIC(NumIFuncs,    "Number of indirect functions removed");
43 STATISTIC(NumVariables, "Number of global variables removed");
44 STATISTIC(NumVFuncs,    "Number of virtual functions removed");
45 
46 namespace {
47   class GlobalDCELegacyPass : public ModulePass {
48   public:
49     static char ID; // Pass identification, replacement for typeid
50     GlobalDCELegacyPass() : ModulePass(ID) {
51       initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
52     }
53 
54     // run - Do the GlobalDCE pass on the specified module, optionally updating
55     // the specified callgraph to reflect the changes.
56     //
57     bool runOnModule(Module &M) override {
58       if (skipModule(M))
59         return false;
60 
61       // We need a minimally functional dummy module analysis manager. It needs
62       // to at least know about the possibility of proxying a function analysis
63       // manager.
64       FunctionAnalysisManager DummyFAM;
65       ModuleAnalysisManager DummyMAM;
66       DummyMAM.registerPass(
67           [&] { return FunctionAnalysisManagerModuleProxy(DummyFAM); });
68 
69       auto PA = Impl.run(M, DummyMAM);
70       return !PA.areAllPreserved();
71     }
72 
73   private:
74     GlobalDCEPass Impl;
75   };
76 }
77 
78 char GlobalDCELegacyPass::ID = 0;
79 INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce",
80                 "Dead Global Elimination", false, false)
81 
82 // Public interface to the GlobalDCEPass.
83 ModulePass *llvm::createGlobalDCEPass() {
84   return new GlobalDCELegacyPass();
85 }
86 
87 /// Returns true if F is effectively empty.
88 static bool isEmptyFunction(Function *F) {
89   BasicBlock &Entry = F->getEntryBlock();
90   for (auto &I : Entry) {
91     if (isa<DbgInfoIntrinsic>(I))
92       continue;
93     if (auto *RI = dyn_cast<ReturnInst>(&I))
94       return !RI->getReturnValue();
95     break;
96   }
97   return false;
98 }
99 
100 /// Compute the set of GlobalValue that depends from V.
101 /// The recursion stops as soon as a GlobalValue is met.
102 void GlobalDCEPass::ComputeDependencies(Value *V,
103                                         SmallPtrSetImpl<GlobalValue *> &Deps) {
104   if (auto *I = dyn_cast<Instruction>(V)) {
105     Function *Parent = I->getParent()->getParent();
106     Deps.insert(Parent);
107   } else if (auto *GV = dyn_cast<GlobalValue>(V)) {
108     Deps.insert(GV);
109   } else if (auto *CE = dyn_cast<Constant>(V)) {
110     // Avoid walking the whole tree of a big ConstantExprs multiple times.
111     auto Where = ConstantDependenciesCache.find(CE);
112     if (Where != ConstantDependenciesCache.end()) {
113       auto const &K = Where->second;
114       Deps.insert(K.begin(), K.end());
115     } else {
116       SmallPtrSetImpl<GlobalValue *> &LocalDeps = ConstantDependenciesCache[CE];
117       for (User *CEUser : CE->users())
118         ComputeDependencies(CEUser, LocalDeps);
119       Deps.insert(LocalDeps.begin(), LocalDeps.end());
120     }
121   }
122 }
123 
124 void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) {
125   SmallPtrSet<GlobalValue *, 8> Deps;
126   for (User *User : GV.users())
127     ComputeDependencies(User, Deps);
128   Deps.erase(&GV); // Remove self-reference.
129   for (GlobalValue *GVU : Deps) {
130     // If this is a dep from a vtable to a virtual function, and we have
131     // complete information about all virtual call sites which could call
132     // though this vtable, then skip it, because the call site information will
133     // be more precise.
134     if (VFESafeVTables.count(GVU) && isa<Function>(&GV)) {
135       LLVM_DEBUG(dbgs() << "Ignoring dep " << GVU->getName() << " -> "
136                         << GV.getName() << "\n");
137       continue;
138     }
139     GVDependencies[GVU].insert(&GV);
140   }
141 }
142 
143 /// Mark Global value as Live
144 void GlobalDCEPass::MarkLive(GlobalValue &GV,
145                              SmallVectorImpl<GlobalValue *> *Updates) {
146   auto const Ret = AliveGlobals.insert(&GV);
147   if (!Ret.second)
148     return;
149 
150   if (Updates)
151     Updates->push_back(&GV);
152   if (Comdat *C = GV.getComdat()) {
153     for (auto &&CM : make_range(ComdatMembers.equal_range(C))) {
154       MarkLive(*CM.second, Updates); // Recursion depth is only two because only
155                                      // globals in the same comdat are visited.
156     }
157   }
158 }
159 
160 void GlobalDCEPass::ScanVTables(Module &M) {
161   SmallVector<MDNode *, 2> Types;
162   LLVM_DEBUG(dbgs() << "Building type info -> vtable map\n");
163 
164   auto *LTOPostLinkMD =
165       cast_or_null<ConstantAsMetadata>(M.getModuleFlag("LTOPostLink"));
166   bool LTOPostLink =
167       LTOPostLinkMD &&
168       (cast<ConstantInt>(LTOPostLinkMD->getValue())->getZExtValue() != 0);
169 
170   for (GlobalVariable &GV : M.globals()) {
171     Types.clear();
172     GV.getMetadata(LLVMContext::MD_type, Types);
173     if (GV.isDeclaration() || Types.empty())
174       continue;
175 
176     // Use the typeid metadata on the vtable to build a mapping from typeids to
177     // the list of (GV, offset) pairs which are the possible vtables for that
178     // typeid.
179     for (MDNode *Type : Types) {
180       Metadata *TypeID = Type->getOperand(1).get();
181 
182       uint64_t Offset =
183           cast<ConstantInt>(
184               cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
185               ->getZExtValue();
186 
187       TypeIdMap[TypeID].insert(std::make_pair(&GV, Offset));
188     }
189 
190     // If the type corresponding to the vtable is private to this translation
191     // unit, we know that we can see all virtual functions which might use it,
192     // so VFE is safe.
193     if (auto GO = dyn_cast<GlobalObject>(&GV)) {
194       GlobalObject::VCallVisibility TypeVis = GO->getVCallVisibility();
195       if (TypeVis == GlobalObject::VCallVisibilityTranslationUnit ||
196           (LTOPostLink &&
197            TypeVis == GlobalObject::VCallVisibilityLinkageUnit)) {
198         LLVM_DEBUG(dbgs() << GV.getName() << " is safe for VFE\n");
199         VFESafeVTables.insert(&GV);
200       }
201     }
202   }
203 }
204 
205 void GlobalDCEPass::ScanVTableLoad(Function *Caller, Metadata *TypeId,
206                                    uint64_t CallOffset) {
207   for (auto &VTableInfo : TypeIdMap[TypeId]) {
208     GlobalVariable *VTable = VTableInfo.first;
209     uint64_t VTableOffset = VTableInfo.second;
210 
211     Constant *Ptr =
212         getPointerAtOffset(VTable->getInitializer(), VTableOffset + CallOffset,
213                            *Caller->getParent());
214     if (!Ptr) {
215       LLVM_DEBUG(dbgs() << "can't find pointer in vtable!\n");
216       VFESafeVTables.erase(VTable);
217       return;
218     }
219 
220     auto Callee = dyn_cast<Function>(Ptr->stripPointerCasts());
221     if (!Callee) {
222       LLVM_DEBUG(dbgs() << "vtable entry is not function pointer!\n");
223       VFESafeVTables.erase(VTable);
224       return;
225     }
226 
227     LLVM_DEBUG(dbgs() << "vfunc dep " << Caller->getName() << " -> "
228                       << Callee->getName() << "\n");
229     GVDependencies[Caller].insert(Callee);
230   }
231 }
232 
233 void GlobalDCEPass::ScanTypeCheckedLoadIntrinsics(Module &M) {
234   LLVM_DEBUG(dbgs() << "Scanning type.checked.load intrinsics\n");
235   Function *TypeCheckedLoadFunc =
236       M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
237 
238   if (!TypeCheckedLoadFunc)
239     return;
240 
241   for (auto U : TypeCheckedLoadFunc->users()) {
242     auto CI = dyn_cast<CallInst>(U);
243     if (!CI)
244       continue;
245 
246     auto *Offset = dyn_cast<ConstantInt>(CI->getArgOperand(1));
247     Value *TypeIdValue = CI->getArgOperand(2);
248     auto *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata();
249 
250     if (Offset) {
251       ScanVTableLoad(CI->getFunction(), TypeId, Offset->getZExtValue());
252     } else {
253       // type.checked.load with a non-constant offset, so assume every entry in
254       // every matching vtable is used.
255       for (auto &VTableInfo : TypeIdMap[TypeId]) {
256         VFESafeVTables.erase(VTableInfo.first);
257       }
258     }
259   }
260 }
261 
262 void GlobalDCEPass::AddVirtualFunctionDependencies(Module &M) {
263   if (!ClEnableVFE)
264     return;
265 
266   ScanVTables(M);
267 
268   if (VFESafeVTables.empty())
269     return;
270 
271   ScanTypeCheckedLoadIntrinsics(M);
272 
273   LLVM_DEBUG(
274     dbgs() << "VFE safe vtables:\n";
275     for (auto *VTable : VFESafeVTables)
276       dbgs() << "  " << VTable->getName() << "\n";
277   );
278 }
279 
280 PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &MAM) {
281   bool Changed = false;
282 
283   // The algorithm first computes the set L of global variables that are
284   // trivially live.  Then it walks the initialization of these variables to
285   // compute the globals used to initialize them, which effectively builds a
286   // directed graph where nodes are global variables, and an edge from A to B
287   // means B is used to initialize A.  Finally, it propagates the liveness
288   // information through the graph starting from the nodes in L. Nodes note
289   // marked as alive are discarded.
290 
291   // Remove empty functions from the global ctors list.
292   Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
293 
294   // Collect the set of members for each comdat.
295   for (Function &F : M)
296     if (Comdat *C = F.getComdat())
297       ComdatMembers.insert(std::make_pair(C, &F));
298   for (GlobalVariable &GV : M.globals())
299     if (Comdat *C = GV.getComdat())
300       ComdatMembers.insert(std::make_pair(C, &GV));
301   for (GlobalAlias &GA : M.aliases())
302     if (Comdat *C = GA.getComdat())
303       ComdatMembers.insert(std::make_pair(C, &GA));
304 
305   // Add dependencies between virtual call sites and the virtual functions they
306   // might call, if we have that information.
307   AddVirtualFunctionDependencies(M);
308 
309   // Loop over the module, adding globals which are obviously necessary.
310   for (GlobalObject &GO : M.global_objects()) {
311     Changed |= RemoveUnusedGlobalValue(GO);
312     // Functions with external linkage are needed if they have a body.
313     // Externally visible & appending globals are needed, if they have an
314     // initializer.
315     if (!GO.isDeclaration())
316       if (!GO.isDiscardableIfUnused())
317         MarkLive(GO);
318 
319     UpdateGVDependencies(GO);
320   }
321 
322   // Compute direct dependencies of aliases.
323   for (GlobalAlias &GA : M.aliases()) {
324     Changed |= RemoveUnusedGlobalValue(GA);
325     // Externally visible aliases are needed.
326     if (!GA.isDiscardableIfUnused())
327       MarkLive(GA);
328 
329     UpdateGVDependencies(GA);
330   }
331 
332   // Compute direct dependencies of ifuncs.
333   for (GlobalIFunc &GIF : M.ifuncs()) {
334     Changed |= RemoveUnusedGlobalValue(GIF);
335     // Externally visible ifuncs are needed.
336     if (!GIF.isDiscardableIfUnused())
337       MarkLive(GIF);
338 
339     UpdateGVDependencies(GIF);
340   }
341 
342   // Propagate liveness from collected Global Values through the computed
343   // dependencies.
344   SmallVector<GlobalValue *, 8> NewLiveGVs{AliveGlobals.begin(),
345                                            AliveGlobals.end()};
346   while (!NewLiveGVs.empty()) {
347     GlobalValue *LGV = NewLiveGVs.pop_back_val();
348     for (auto *GVD : GVDependencies[LGV])
349       MarkLive(*GVD, &NewLiveGVs);
350   }
351 
352   // Now that all globals which are needed are in the AliveGlobals set, we loop
353   // through the program, deleting those which are not alive.
354   //
355 
356   // The first pass is to drop initializers of global variables which are dead.
357   std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals
358   for (GlobalVariable &GV : M.globals())
359     if (!AliveGlobals.count(&GV)) {
360       DeadGlobalVars.push_back(&GV);         // Keep track of dead globals
361       if (GV.hasInitializer()) {
362         Constant *Init = GV.getInitializer();
363         GV.setInitializer(nullptr);
364         if (isSafeToDestroyConstant(Init))
365           Init->destroyConstant();
366       }
367     }
368 
369   // The second pass drops the bodies of functions which are dead...
370   std::vector<Function *> DeadFunctions;
371   for (Function &F : M)
372     if (!AliveGlobals.count(&F)) {
373       DeadFunctions.push_back(&F);         // Keep track of dead globals
374       if (!F.isDeclaration())
375         F.deleteBody();
376     }
377 
378   // The third pass drops targets of aliases which are dead...
379   std::vector<GlobalAlias*> DeadAliases;
380   for (GlobalAlias &GA : M.aliases())
381     if (!AliveGlobals.count(&GA)) {
382       DeadAliases.push_back(&GA);
383       GA.setAliasee(nullptr);
384     }
385 
386   // The fourth pass drops targets of ifuncs which are dead...
387   std::vector<GlobalIFunc*> DeadIFuncs;
388   for (GlobalIFunc &GIF : M.ifuncs())
389     if (!AliveGlobals.count(&GIF)) {
390       DeadIFuncs.push_back(&GIF);
391       GIF.setResolver(nullptr);
392     }
393 
394   // Now that all interferences have been dropped, delete the actual objects
395   // themselves.
396   auto EraseUnusedGlobalValue = [&](GlobalValue *GV) {
397     RemoveUnusedGlobalValue(*GV);
398     GV->eraseFromParent();
399     Changed = true;
400   };
401 
402   NumFunctions += DeadFunctions.size();
403   for (Function *F : DeadFunctions) {
404     if (!F->use_empty()) {
405       // Virtual functions might still be referenced by one or more vtables,
406       // but if we've proven them to be unused then it's safe to replace the
407       // virtual function pointers with null, allowing us to remove the
408       // function itself.
409       ++NumVFuncs;
410       F->replaceNonMetadataUsesWith(ConstantPointerNull::get(F->getType()));
411     }
412     EraseUnusedGlobalValue(F);
413   }
414 
415   NumVariables += DeadGlobalVars.size();
416   for (GlobalVariable *GV : DeadGlobalVars)
417     EraseUnusedGlobalValue(GV);
418 
419   NumAliases += DeadAliases.size();
420   for (GlobalAlias *GA : DeadAliases)
421     EraseUnusedGlobalValue(GA);
422 
423   NumIFuncs += DeadIFuncs.size();
424   for (GlobalIFunc *GIF : DeadIFuncs)
425     EraseUnusedGlobalValue(GIF);
426 
427   // Make sure that all memory is released
428   AliveGlobals.clear();
429   ConstantDependenciesCache.clear();
430   GVDependencies.clear();
431   ComdatMembers.clear();
432   TypeIdMap.clear();
433   VFESafeVTables.clear();
434 
435   if (Changed)
436     return PreservedAnalyses::none();
437   return PreservedAnalyses::all();
438 }
439 
440 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified
441 // GlobalValue, looking for the constant pointer ref that may be pointing to it.
442 // If found, check to see if the constant pointer ref is safe to destroy, and if
443 // so, nuke it.  This will reduce the reference count on the global value, which
444 // might make it deader.
445 //
446 bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) {
447   if (GV.use_empty())
448     return false;
449   GV.removeDeadConstantUsers();
450   return GV.use_empty();
451 }
452