1 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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/Pass.h"
26 #include "llvm/Transforms/IPO.h"
27 #include "llvm/Transforms/Utils/CtorUtils.h"
28 #include "llvm/Transforms/Utils/GlobalStatus.h"
32 #define DEBUG_TYPE "globaldce"
35 ClEnableVFE("enable-vfe", cl::Hidden
, cl::init(true), cl::ZeroOrMore
,
36 cl::desc("Enable virtual function elimination"));
38 STATISTIC(NumAliases
, "Number of global aliases removed");
39 STATISTIC(NumFunctions
, "Number of functions removed");
40 STATISTIC(NumIFuncs
, "Number of indirect functions removed");
41 STATISTIC(NumVariables
, "Number of global variables removed");
42 STATISTIC(NumVFuncs
, "Number of virtual functions removed");
45 class GlobalDCELegacyPass
: public ModulePass
{
47 static char ID
; // Pass identification, replacement for typeid
48 GlobalDCELegacyPass() : ModulePass(ID
) {
49 initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
52 // run - Do the GlobalDCE pass on the specified module, optionally updating
53 // the specified callgraph to reflect the changes.
55 bool runOnModule(Module
&M
) override
{
59 // We need a minimally functional dummy module analysis manager. It needs
60 // to at least know about the possibility of proxying a function analysis
62 FunctionAnalysisManager DummyFAM
;
63 ModuleAnalysisManager DummyMAM
;
64 DummyMAM
.registerPass(
65 [&] { return FunctionAnalysisManagerModuleProxy(DummyFAM
); });
67 auto PA
= Impl
.run(M
, DummyMAM
);
68 return !PA
.areAllPreserved();
76 char GlobalDCELegacyPass::ID
= 0;
77 INITIALIZE_PASS(GlobalDCELegacyPass
, "globaldce",
78 "Dead Global Elimination", false, false)
80 // Public interface to the GlobalDCEPass.
81 ModulePass
*llvm::createGlobalDCEPass() {
82 return new GlobalDCELegacyPass();
85 /// Returns true if F is effectively empty.
86 static bool isEmptyFunction(Function
*F
) {
87 BasicBlock
&Entry
= F
->getEntryBlock();
88 for (auto &I
: Entry
) {
89 if (isa
<DbgInfoIntrinsic
>(I
))
91 if (auto *RI
= dyn_cast
<ReturnInst
>(&I
))
92 return !RI
->getReturnValue();
98 /// Compute the set of GlobalValue that depends from V.
99 /// The recursion stops as soon as a GlobalValue is met.
100 void GlobalDCEPass::ComputeDependencies(Value
*V
,
101 SmallPtrSetImpl
<GlobalValue
*> &Deps
) {
102 if (auto *I
= dyn_cast
<Instruction
>(V
)) {
103 Function
*Parent
= I
->getParent()->getParent();
105 } else if (auto *GV
= dyn_cast
<GlobalValue
>(V
)) {
107 } else if (auto *CE
= dyn_cast
<Constant
>(V
)) {
108 // Avoid walking the whole tree of a big ConstantExprs multiple times.
109 auto Where
= ConstantDependenciesCache
.find(CE
);
110 if (Where
!= ConstantDependenciesCache
.end()) {
111 auto const &K
= Where
->second
;
112 Deps
.insert(K
.begin(), K
.end());
114 SmallPtrSetImpl
<GlobalValue
*> &LocalDeps
= ConstantDependenciesCache
[CE
];
115 for (User
*CEUser
: CE
->users())
116 ComputeDependencies(CEUser
, LocalDeps
);
117 Deps
.insert(LocalDeps
.begin(), LocalDeps
.end());
122 void GlobalDCEPass::UpdateGVDependencies(GlobalValue
&GV
) {
123 SmallPtrSet
<GlobalValue
*, 8> Deps
;
124 for (User
*User
: GV
.users())
125 ComputeDependencies(User
, Deps
);
126 Deps
.erase(&GV
); // Remove self-reference.
127 for (GlobalValue
*GVU
: Deps
) {
128 // If this is a dep from a vtable to a virtual function, and we have
129 // complete information about all virtual call sites which could call
130 // though this vtable, then skip it, because the call site information will
132 if (VFESafeVTables
.count(GVU
) && isa
<Function
>(&GV
)) {
133 LLVM_DEBUG(dbgs() << "Ignoring dep " << GVU
->getName() << " -> "
134 << GV
.getName() << "\n");
137 GVDependencies
[GVU
].insert(&GV
);
141 /// Mark Global value as Live
142 void GlobalDCEPass::MarkLive(GlobalValue
&GV
,
143 SmallVectorImpl
<GlobalValue
*> *Updates
) {
144 auto const Ret
= AliveGlobals
.insert(&GV
);
149 Updates
->push_back(&GV
);
150 if (Comdat
*C
= GV
.getComdat()) {
151 for (auto &&CM
: make_range(ComdatMembers
.equal_range(C
))) {
152 MarkLive(*CM
.second
, Updates
); // Recursion depth is only two because only
153 // globals in the same comdat are visited.
158 void GlobalDCEPass::ScanVTables(Module
&M
) {
159 SmallVector
<MDNode
*, 2> Types
;
160 LLVM_DEBUG(dbgs() << "Building type info -> vtable map\n");
162 auto *LTOPostLinkMD
=
163 cast_or_null
<ConstantAsMetadata
>(M
.getModuleFlag("LTOPostLink"));
166 (cast
<ConstantInt
>(LTOPostLinkMD
->getValue())->getZExtValue() != 0);
168 for (GlobalVariable
&GV
: M
.globals()) {
170 GV
.getMetadata(LLVMContext::MD_type
, Types
);
171 if (GV
.isDeclaration() || Types
.empty())
174 // Use the typeid metadata on the vtable to build a mapping from typeids to
175 // the list of (GV, offset) pairs which are the possible vtables for that
177 for (MDNode
*Type
: Types
) {
178 Metadata
*TypeID
= Type
->getOperand(1).get();
182 cast
<ConstantAsMetadata
>(Type
->getOperand(0))->getValue())
185 TypeIdMap
[TypeID
].insert(std::make_pair(&GV
, Offset
));
188 // If the type corresponding to the vtable is private to this translation
189 // unit, we know that we can see all virtual functions which might use it,
191 if (auto GO
= dyn_cast
<GlobalObject
>(&GV
)) {
192 GlobalObject::VCallVisibility TypeVis
= GO
->getVCallVisibility();
193 if (TypeVis
== GlobalObject::VCallVisibilityTranslationUnit
||
195 TypeVis
== GlobalObject::VCallVisibilityLinkageUnit
)) {
196 LLVM_DEBUG(dbgs() << GV
.getName() << " is safe for VFE\n");
197 VFESafeVTables
.insert(&GV
);
203 void GlobalDCEPass::ScanVTableLoad(Function
*Caller
, Metadata
*TypeId
,
204 uint64_t CallOffset
) {
205 for (auto &VTableInfo
: TypeIdMap
[TypeId
]) {
206 GlobalVariable
*VTable
= VTableInfo
.first
;
207 uint64_t VTableOffset
= VTableInfo
.second
;
210 getPointerAtOffset(VTable
->getInitializer(), VTableOffset
+ CallOffset
,
211 *Caller
->getParent());
213 LLVM_DEBUG(dbgs() << "can't find pointer in vtable!\n");
214 VFESafeVTables
.erase(VTable
);
218 auto Callee
= dyn_cast
<Function
>(Ptr
->stripPointerCasts());
220 LLVM_DEBUG(dbgs() << "vtable entry is not function pointer!\n");
221 VFESafeVTables
.erase(VTable
);
225 LLVM_DEBUG(dbgs() << "vfunc dep " << Caller
->getName() << " -> "
226 << Callee
->getName() << "\n");
227 GVDependencies
[Caller
].insert(Callee
);
231 void GlobalDCEPass::ScanTypeCheckedLoadIntrinsics(Module
&M
) {
232 LLVM_DEBUG(dbgs() << "Scanning type.checked.load intrinsics\n");
233 Function
*TypeCheckedLoadFunc
=
234 M
.getFunction(Intrinsic::getName(Intrinsic::type_checked_load
));
236 if (!TypeCheckedLoadFunc
)
239 for (auto U
: TypeCheckedLoadFunc
->users()) {
240 auto CI
= dyn_cast
<CallInst
>(U
);
244 auto *Offset
= dyn_cast
<ConstantInt
>(CI
->getArgOperand(1));
245 Value
*TypeIdValue
= CI
->getArgOperand(2);
246 auto *TypeId
= cast
<MetadataAsValue
>(TypeIdValue
)->getMetadata();
249 ScanVTableLoad(CI
->getFunction(), TypeId
, Offset
->getZExtValue());
251 // type.checked.load with a non-constant offset, so assume every entry in
252 // every matching vtable is used.
253 for (auto &VTableInfo
: TypeIdMap
[TypeId
]) {
254 VFESafeVTables
.erase(VTableInfo
.first
);
260 void GlobalDCEPass::AddVirtualFunctionDependencies(Module
&M
) {
266 if (VFESafeVTables
.empty())
269 ScanTypeCheckedLoadIntrinsics(M
);
272 dbgs() << "VFE safe vtables:\n";
273 for (auto *VTable
: VFESafeVTables
)
274 dbgs() << " " << VTable
->getName() << "\n";
278 PreservedAnalyses
GlobalDCEPass::run(Module
&M
, ModuleAnalysisManager
&MAM
) {
279 bool Changed
= false;
281 // The algorithm first computes the set L of global variables that are
282 // trivially live. Then it walks the initialization of these variables to
283 // compute the globals used to initialize them, which effectively builds a
284 // directed graph where nodes are global variables, and an edge from A to B
285 // means B is used to initialize A. Finally, it propagates the liveness
286 // information through the graph starting from the nodes in L. Nodes note
287 // marked as alive are discarded.
289 // Remove empty functions from the global ctors list.
290 Changed
|= optimizeGlobalCtorsList(M
, isEmptyFunction
);
292 // Collect the set of members for each comdat.
293 for (Function
&F
: M
)
294 if (Comdat
*C
= F
.getComdat())
295 ComdatMembers
.insert(std::make_pair(C
, &F
));
296 for (GlobalVariable
&GV
: M
.globals())
297 if (Comdat
*C
= GV
.getComdat())
298 ComdatMembers
.insert(std::make_pair(C
, &GV
));
299 for (GlobalAlias
&GA
: M
.aliases())
300 if (Comdat
*C
= GA
.getComdat())
301 ComdatMembers
.insert(std::make_pair(C
, &GA
));
303 // Add dependencies between virtual call sites and the virtual functions they
304 // might call, if we have that information.
305 AddVirtualFunctionDependencies(M
);
307 // Loop over the module, adding globals which are obviously necessary.
308 for (GlobalObject
&GO
: M
.global_objects()) {
309 Changed
|= RemoveUnusedGlobalValue(GO
);
310 // Functions with external linkage are needed if they have a body.
311 // Externally visible & appending globals are needed, if they have an
313 if (!GO
.isDeclaration())
314 if (!GO
.isDiscardableIfUnused())
317 UpdateGVDependencies(GO
);
320 // Compute direct dependencies of aliases.
321 for (GlobalAlias
&GA
: M
.aliases()) {
322 Changed
|= RemoveUnusedGlobalValue(GA
);
323 // Externally visible aliases are needed.
324 if (!GA
.isDiscardableIfUnused())
327 UpdateGVDependencies(GA
);
330 // Compute direct dependencies of ifuncs.
331 for (GlobalIFunc
&GIF
: M
.ifuncs()) {
332 Changed
|= RemoveUnusedGlobalValue(GIF
);
333 // Externally visible ifuncs are needed.
334 if (!GIF
.isDiscardableIfUnused())
337 UpdateGVDependencies(GIF
);
340 // Propagate liveness from collected Global Values through the computed
342 SmallVector
<GlobalValue
*, 8> NewLiveGVs
{AliveGlobals
.begin(),
344 while (!NewLiveGVs
.empty()) {
345 GlobalValue
*LGV
= NewLiveGVs
.pop_back_val();
346 for (auto *GVD
: GVDependencies
[LGV
])
347 MarkLive(*GVD
, &NewLiveGVs
);
350 // Now that all globals which are needed are in the AliveGlobals set, we loop
351 // through the program, deleting those which are not alive.
354 // The first pass is to drop initializers of global variables which are dead.
355 std::vector
<GlobalVariable
*> DeadGlobalVars
; // Keep track of dead globals
356 for (GlobalVariable
&GV
: M
.globals())
357 if (!AliveGlobals
.count(&GV
)) {
358 DeadGlobalVars
.push_back(&GV
); // Keep track of dead globals
359 if (GV
.hasInitializer()) {
360 Constant
*Init
= GV
.getInitializer();
361 GV
.setInitializer(nullptr);
362 if (isSafeToDestroyConstant(Init
))
363 Init
->destroyConstant();
367 // The second pass drops the bodies of functions which are dead...
368 std::vector
<Function
*> DeadFunctions
;
369 for (Function
&F
: M
)
370 if (!AliveGlobals
.count(&F
)) {
371 DeadFunctions
.push_back(&F
); // Keep track of dead globals
372 if (!F
.isDeclaration())
376 // The third pass drops targets of aliases which are dead...
377 std::vector
<GlobalAlias
*> DeadAliases
;
378 for (GlobalAlias
&GA
: M
.aliases())
379 if (!AliveGlobals
.count(&GA
)) {
380 DeadAliases
.push_back(&GA
);
381 GA
.setAliasee(nullptr);
384 // The fourth pass drops targets of ifuncs which are dead...
385 std::vector
<GlobalIFunc
*> DeadIFuncs
;
386 for (GlobalIFunc
&GIF
: M
.ifuncs())
387 if (!AliveGlobals
.count(&GIF
)) {
388 DeadIFuncs
.push_back(&GIF
);
389 GIF
.setResolver(nullptr);
392 // Now that all interferences have been dropped, delete the actual objects
394 auto EraseUnusedGlobalValue
= [&](GlobalValue
*GV
) {
395 RemoveUnusedGlobalValue(*GV
);
396 GV
->eraseFromParent();
400 NumFunctions
+= DeadFunctions
.size();
401 for (Function
*F
: DeadFunctions
) {
402 if (!F
->use_empty()) {
403 // Virtual functions might still be referenced by one or more vtables,
404 // but if we've proven them to be unused then it's safe to replace the
405 // virtual function pointers with null, allowing us to remove the
408 F
->replaceNonMetadataUsesWith(ConstantPointerNull::get(F
->getType()));
410 EraseUnusedGlobalValue(F
);
413 NumVariables
+= DeadGlobalVars
.size();
414 for (GlobalVariable
*GV
: DeadGlobalVars
)
415 EraseUnusedGlobalValue(GV
);
417 NumAliases
+= DeadAliases
.size();
418 for (GlobalAlias
*GA
: DeadAliases
)
419 EraseUnusedGlobalValue(GA
);
421 NumIFuncs
+= DeadIFuncs
.size();
422 for (GlobalIFunc
*GIF
: DeadIFuncs
)
423 EraseUnusedGlobalValue(GIF
);
425 // Make sure that all memory is released
426 AliveGlobals
.clear();
427 ConstantDependenciesCache
.clear();
428 GVDependencies
.clear();
429 ComdatMembers
.clear();
431 VFESafeVTables
.clear();
434 return PreservedAnalyses::none();
435 return PreservedAnalyses::all();
438 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified
439 // GlobalValue, looking for the constant pointer ref that may be pointing to it.
440 // If found, check to see if the constant pointer ref is safe to destroy, and if
441 // so, nuke it. This will reduce the reference count on the global value, which
442 // might make it deader.
444 bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue
&GV
) {
447 GV
.removeDeadConstantUsers();
448 return GV
.use_empty();