eliminate_dead_members_pass.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_ELIMINATE_DEAD_MEMBERS_PASS_H_
  15. #define SOURCE_OPT_ELIMINATE_DEAD_MEMBERS_PASS_H_
  16. #include "source/opt/def_use_manager.h"
  17. #include "source/opt/function.h"
  18. #include "source/opt/mem_pass.h"
  19. #include "source/opt/module.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // Remove unused members from structures. The remaining members will remain at
  23. // the same offset.
  24. class EliminateDeadMembersPass : public MemPass {
  25. public:
  26. const char* name() const override { return "eliminate-dead-members"; }
  27. Status Process() override;
  28. IRContext::Analysis GetPreservedAnalyses() override {
  29. return IRContext::kAnalysisDefUse |
  30. IRContext::kAnalysisInstrToBlockMapping |
  31. IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG |
  32. IRContext::kAnalysisDominatorAnalysis |
  33. IRContext::kAnalysisLoopAnalysis |
  34. IRContext::kAnalysisScalarEvolution |
  35. IRContext::kAnalysisRegisterPressure |
  36. IRContext::kAnalysisValueNumberTable |
  37. IRContext::kAnalysisStructuredCFG |
  38. IRContext::kAnalysisBuiltinVarId |
  39. IRContext::kAnalysisIdToFuncMapping;
  40. }
  41. private:
  42. // Populate |used_members_| with the member of structures that are live in the
  43. // current context.
  44. void FindLiveMembers();
  45. // Add to |used_members_| the member of structures that are live in
  46. // |function|.
  47. void FindLiveMembers(const Function& function);
  48. // Add to |used_members_| the member of structures that are live in |inst|.
  49. void FindLiveMembers(const Instruction* inst);
  50. // Add to |used_members_| the members that are live in the |OpStore|
  51. // instruction |inst|.
  52. void MarkMembersAsLiveForStore(const Instruction* inst);
  53. // Add to |used_members_| the members that are live in the |OpCopyMemory*|
  54. // instruction |inst|.
  55. void MarkMembersAsLiveForCopyMemory(const Instruction* inst);
  56. // Add to |used_members_| the members that are live in the
  57. // |OpCompositeExtract| instruction |inst|.
  58. void MarkMembersAsLiveForExtract(const Instruction* inst);
  59. // Add to |used_members_| the members that are live in the |Op*AccessChain|
  60. // instruction |inst|.
  61. void MarkMembersAsLiveForAccessChain(const Instruction* inst);
  62. // Add the member referenced by the OpArrayLength instruction |inst| to
  63. // |uses_members_|.
  64. void MarkMembersAsLiveForArrayLength(const Instruction* inst);
  65. // Remove dead members from structs and updates any instructions that need to
  66. // be updated as a consequence. Return true if something changed.
  67. bool RemoveDeadMembers();
  68. // Update |inst|, which must be an |OpMemberName| or |OpMemberDecorate|
  69. // instruction, so it references the correct member after the struct is
  70. // updated. Return true if something changed.
  71. bool UpdateOpMemberNameOrDecorate(Instruction* inst);
  72. // Update |inst|, which must be an |OpGroupMemberDecorate| instruction, so it
  73. // references the correct member after the struct is updated. Return true if
  74. // something changed.
  75. bool UpdateOpGroupMemberDecorate(Instruction* inst);
  76. // Update the |OpTypeStruct| instruction |inst| my removing the members that
  77. // are not live. Return true if something changed.
  78. bool UpdateOpTypeStruct(Instruction* inst);
  79. // Update the |OpConstantComposite| instruction |inst| to match the change
  80. // made to the type that was being generated. Return true if something
  81. // changed.
  82. bool UpdateConstantComposite(Instruction* inst);
  83. // Update the |Op*AccessChain| instruction |inst| to reference the correct
  84. // members. All members referenced in the access chain must be live. This
  85. // function must be called after the |OpTypeStruct| instruction for the type
  86. // has been updated. Return true if something changed.
  87. bool UpdateAccessChain(Instruction* inst);
  88. // Update the |OpCompositeExtract| instruction |inst| to reference the correct
  89. // members. All members referenced in the instruction must be live. This
  90. // function must be called after the |OpTypeStruct| instruction for the type
  91. // has been updated. Return true if something changed.
  92. bool UpdateCompsiteExtract(Instruction* inst);
  93. // Update the |OpCompositeInsert| instruction |inst| to reference the correct
  94. // members. If the member being inserted is not live, then |inst| is killed.
  95. // This function must be called after the |OpTypeStruct| instruction for the
  96. // type has been updated. Return true if something changed.
  97. bool UpdateCompositeInsert(Instruction* inst);
  98. // Update the |OpArrayLength| instruction |inst| to reference the correct
  99. // member. The member referenced in the instruction must be live. Return true
  100. // if something changed.
  101. bool UpdateOpArrayLength(Instruction* inst);
  102. // Add all of the members of type |type_id| and members of any subtypes to
  103. // |used_members_|.
  104. void MarkTypeAsFullyUsed(uint32_t type_id);
  105. // Add all of the members of the type of the operand |in_idx| in |inst| and
  106. // members of any subtypes to |uses_members_|.
  107. void MarkOperandTypeAsFullyUsed(const Instruction* inst, uint32_t in_idx);
  108. // Return the index of the member that use to be the |member_idx|th member of
  109. // |type_id|. If the member has been removed, |kRemovedMember| is returned.
  110. uint32_t GetNewMemberIndex(uint32_t type_id, uint32_t member_idx);
  111. // A map from a type id to a set of indices representing the members of the
  112. // type that are used, and must be kept.
  113. std::unordered_map<uint32_t, std::set<uint32_t>> used_members_;
  114. void MarkStructOperandsAsFullyUsed(const Instruction* inst);
  115. void MarkPointeeTypeAsFullUsed(uint32_t ptr_type_id);
  116. };
  117. } // namespace opt
  118. } // namespace spvtools
  119. #endif // SOURCE_OPT_ELIMINATE_DEAD_MEMBERS_PASS_H_