GlobalDCE.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- GlobalDCE.h - DCE unreachable internal functions ------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This transform is designed to eliminate unreachable internal globals from the
  15. // program. It uses an aggressive algorithm, searching out globals that are
  16. // known to be alive. After it finds all of the globals which are needed, it
  17. // deletes whatever is left over. This allows it to delete recursive chunks of
  18. // the program which are unreachable.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_TRANSFORMS_IPO_GLOBALDCE_H
  22. #define LLVM_TRANSFORMS_IPO_GLOBALDCE_H
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/SmallSet.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/IR/PassManager.h"
  27. #include <unordered_map>
  28. namespace llvm {
  29. /// Pass to remove unused function declarations.
  30. class GlobalDCEPass : public PassInfoMixin<GlobalDCEPass> {
  31. public:
  32. PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
  33. private:
  34. SmallPtrSet<GlobalValue*, 32> AliveGlobals;
  35. /// Global -> Global that uses this global.
  36. DenseMap<GlobalValue *, SmallPtrSet<GlobalValue *, 4>> GVDependencies;
  37. /// Constant -> Globals that use this global cache.
  38. std::unordered_map<Constant *, SmallPtrSet<GlobalValue *, 8>>
  39. ConstantDependenciesCache;
  40. /// Comdat -> Globals in that Comdat section.
  41. std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
  42. /// !type metadata -> set of (vtable, offset) pairs
  43. DenseMap<Metadata *, SmallSet<std::pair<GlobalVariable *, uint64_t>, 4>>
  44. TypeIdMap;
  45. // Global variables which are vtables, and which we have enough information
  46. // about to safely do dead virtual function elimination.
  47. SmallPtrSet<GlobalValue *, 32> VFESafeVTables;
  48. void UpdateGVDependencies(GlobalValue &GV);
  49. void MarkLive(GlobalValue &GV,
  50. SmallVectorImpl<GlobalValue *> *Updates = nullptr);
  51. bool RemoveUnusedGlobalValue(GlobalValue &GV);
  52. // Dead virtual function elimination.
  53. void AddVirtualFunctionDependencies(Module &M);
  54. void ScanVTables(Module &M);
  55. void ScanTypeCheckedLoadIntrinsics(Module &M);
  56. void ScanVTableLoad(Function *Caller, Metadata *TypeId, uint64_t CallOffset);
  57. void ComputeDependencies(Value *V, SmallPtrSetImpl<GlobalValue *> &U);
  58. };
  59. }
  60. #endif // LLVM_TRANSFORMS_IPO_GLOBALDCE_H
  61. #ifdef __GNUC__
  62. #pragma GCC diagnostic pop
  63. #endif