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

source code of llvm/lib/Transforms/IPO/GlobalDCE.cpp