xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/IPO/GlobalDCE.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 //===-- GlobalDCE.h - 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 #ifndef LLVM_TRANSFORMS_IPO_GLOBALDCE_H
18 #define LLVM_TRANSFORMS_IPO_GLOBALDCE_H
19 
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/IR/PassManager.h"
24 #include <unordered_map>
25 
26 namespace llvm {
27 class Comdat;
28 class Constant;
29 class Function;
30 class GlobalVariable;
31 class Metadata;
32 class Module;
33 class Value;
34 
35 /// Pass to remove unused function declarations.
36 class GlobalDCEPass : public PassInfoMixin<GlobalDCEPass> {
37 public:
InLTOPostLink(InLTOPostLink)38   GlobalDCEPass(bool InLTOPostLink = false) : InLTOPostLink(InLTOPostLink) {}
39 
40   PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
41 
42   void printPipeline(raw_ostream &OS,
43                      function_ref<StringRef(StringRef)> MapClassName2PassName);
44 
45 private:
46   bool InLTOPostLink = false;
47 
48   SmallPtrSet<GlobalValue*, 32> AliveGlobals;
49 
50   /// Global -> Global that uses this global.
51   DenseMap<GlobalValue *, SmallPtrSet<GlobalValue *, 4>> GVDependencies;
52 
53   /// Constant -> Globals that use this global cache.
54   std::unordered_map<Constant *, SmallPtrSet<GlobalValue *, 8>>
55       ConstantDependenciesCache;
56 
57   /// Comdat -> Globals in that Comdat section.
58   std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
59 
60   /// !type metadata -> set of (vtable, offset) pairs
61   DenseMap<Metadata *, SmallSet<std::pair<GlobalVariable *, uint64_t>, 4>>
62       TypeIdMap;
63 
64   // Global variables which are vtables, and which we have enough information
65   // about to safely do dead virtual function elimination.
66   SmallPtrSet<GlobalValue *, 32> VFESafeVTables;
67 
68   void UpdateGVDependencies(GlobalValue &GV);
69   void MarkLive(GlobalValue &GV,
70                 SmallVectorImpl<GlobalValue *> *Updates = nullptr);
71 
72   // Dead virtual function elimination.
73   void AddVirtualFunctionDependencies(Module &M);
74   void ScanVTables(Module &M);
75   void ScanTypeCheckedLoadIntrinsics(Module &M);
76   void ScanVTableLoad(Function *Caller, Metadata *TypeId, uint64_t CallOffset);
77 
78   void ComputeDependencies(Value *V, SmallPtrSetImpl<GlobalValue *> &U);
79 };
80 
81 }
82 
83 #endif // LLVM_TRANSFORMS_IPO_GLOBALDCE_H
84