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