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