GlobalDCE.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/GlobalValue.h"
  26. #include "llvm/IR/PassManager.h"
  27. #include <unordered_map>
  28. namespace llvm {
  29. class Comdat;
  30. class Constant;
  31. class Function;
  32. class GlobalVariable;
  33. class Metadata;
  34. class Module;
  35. class Value;
  36. /// Pass to remove unused function declarations.
  37. class GlobalDCEPass : public PassInfoMixin<GlobalDCEPass> {
  38. public:
  39. PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
  40. private:
  41. SmallPtrSet<GlobalValue*, 32> AliveGlobals;
  42. /// Global -> Global that uses this global.
  43. DenseMap<GlobalValue *, SmallPtrSet<GlobalValue *, 4>> GVDependencies;
  44. /// Constant -> Globals that use this global cache.
  45. std::unordered_map<Constant *, SmallPtrSet<GlobalValue *, 8>>
  46. ConstantDependenciesCache;
  47. /// Comdat -> Globals in that Comdat section.
  48. std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
  49. /// !type metadata -> set of (vtable, offset) pairs
  50. DenseMap<Metadata *, SmallSet<std::pair<GlobalVariable *, uint64_t>, 4>>
  51. TypeIdMap;
  52. // Global variables which are vtables, and which we have enough information
  53. // about to safely do dead virtual function elimination.
  54. SmallPtrSet<GlobalValue *, 32> VFESafeVTables;
  55. void UpdateGVDependencies(GlobalValue &GV);
  56. void MarkLive(GlobalValue &GV,
  57. SmallVectorImpl<GlobalValue *> *Updates = nullptr);
  58. 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);
  64. void ComputeDependencies(Value *V, SmallPtrSetImpl<GlobalValue *> &U);
  65. };
  66. }
  67. #endif // LLVM_TRANSFORMS_IPO_GLOBALDCE_H
  68. #ifdef __GNUC__
  69. #pragma GCC diagnostic pop
  70. #endif