1 //===-- GlobalDCE.h - 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 #ifndef LLVM_TRANSFORMS_IPO_GLOBALDCE_H
18 #define LLVM_TRANSFORMS_IPO_GLOBALDCE_H
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PassManager.h"
24 #include <unordered_map>
28 /// Pass to remove unused function declarations.
29 class GlobalDCEPass
: public PassInfoMixin
<GlobalDCEPass
> {
31 PreservedAnalyses
run(Module
&M
, ModuleAnalysisManager
&);
34 SmallPtrSet
<GlobalValue
*, 32> AliveGlobals
;
36 /// Global -> Global that uses this global.
37 DenseMap
<GlobalValue
*, SmallPtrSet
<GlobalValue
*, 4>> GVDependencies
;
39 /// Constant -> Globals that use this global cache.
40 std::unordered_map
<Constant
*, SmallPtrSet
<GlobalValue
*, 8>>
41 ConstantDependenciesCache
;
43 /// Comdat -> Globals in that Comdat section.
44 std::unordered_multimap
<Comdat
*, GlobalValue
*> ComdatMembers
;
46 /// !type metadata -> set of (vtable, offset) pairs
47 DenseMap
<Metadata
*, SmallSet
<std::pair
<GlobalVariable
*, uint64_t>, 4>>
50 // Global variables which are vtables, and which we have enough information
51 // about to safely do dead virtual function elimination.
52 SmallPtrSet
<GlobalValue
*, 32> VFESafeVTables
;
54 void UpdateGVDependencies(GlobalValue
&GV
);
55 void MarkLive(GlobalValue
&GV
,
56 SmallVectorImpl
<GlobalValue
*> *Updates
= nullptr);
57 bool RemoveUnusedGlobalValue(GlobalValue
&GV
);
59 // Dead virtual function elimination.
60 void AddVirtualFunctionDependencies(Module
&M
);
61 void ScanVTables(Module
&M
);
62 void ScanTypeCheckedLoadIntrinsics(Module
&M
);
63 void ScanVTableLoad(Function
*Caller
, Metadata
*TypeId
, uint64_t CallOffset
);
65 void ComputeDependencies(Value
*V
, SmallPtrSetImpl
<GlobalValue
*> &U
);
70 #endif // LLVM_TRANSFORMS_IPO_GLOBALDCE_H