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