vector_dce.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2018 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_VECTOR_DCE_H_
  15. #define SOURCE_OPT_VECTOR_DCE_H_
  16. #include <unordered_map>
  17. #include <vector>
  18. #include "source/opt/mem_pass.h"
  19. #include "source/util/bit_vector.h"
  20. namespace spvtools {
  21. namespace opt {
  22. class VectorDCE : public MemPass {
  23. private:
  24. using LiveComponentMap = std::unordered_map<uint32_t, utils::BitVector>;
  25. // According to the SPEC the maximum size for a vector is 16. See the data
  26. // rules in the universal validation rules (section 2.16.1).
  27. enum { kMaxVectorSize = 16 };
  28. struct WorkListItem {
  29. WorkListItem() : instruction(nullptr), components(kMaxVectorSize) {}
  30. Instruction* instruction;
  31. utils::BitVector components;
  32. };
  33. public:
  34. VectorDCE() : all_components_live_(kMaxVectorSize) {
  35. for (uint32_t i = 0; i < kMaxVectorSize; i++) {
  36. all_components_live_.Set(i);
  37. }
  38. }
  39. const char* name() const override { return "vector-dce"; }
  40. Status Process() override;
  41. IRContext::Analysis GetPreservedAnalyses() override {
  42. return IRContext::kAnalysisDefUse | IRContext::kAnalysisCFG |
  43. IRContext::kAnalysisInstrToBlockMapping |
  44. IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisDecorations |
  45. IRContext::kAnalysisDominatorAnalysis | IRContext::kAnalysisNameMap |
  46. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
  47. }
  48. private:
  49. // Runs the vector dce pass on |function|. Returns true if |function| was
  50. // modified.
  51. bool VectorDCEFunction(Function* function);
  52. // Identifies the live components of the vectors that are results of
  53. // instructions in |function|. The results are stored in |live_components|.
  54. void FindLiveComponents(Function* function,
  55. LiveComponentMap* live_components);
  56. // Rewrites instructions in |function| that are dead or partially dead. If an
  57. // instruction does not have an entry in |live_components|, then it is not
  58. // changed. Returns true if |function| was modified.
  59. bool RewriteInstructions(Function* function,
  60. const LiveComponentMap& live_components);
  61. // Makes all DebugValue instructions that use |composite| for their values as
  62. // dead instructions by putting them into |dead_dbg_value|.
  63. void MarkDebugValueUsesAsDead(Instruction* composite,
  64. std::vector<Instruction*>* dead_dbg_value);
  65. // Rewrites the OpCompositeInsert instruction |current_inst| to avoid
  66. // unnecessary computes given that the only components of the result that are
  67. // live are |live_components|.
  68. //
  69. // If the value being inserted is not live, then the result of |current_inst|
  70. // is replaced by the composite input to |current_inst|.
  71. //
  72. // If the composite input to |current_inst| is not live, then it is replaced
  73. // by and OpUndef in |current_inst|.
  74. bool RewriteInsertInstruction(Instruction* current_inst,
  75. const utils::BitVector& live_components,
  76. std::vector<Instruction*>* dead_dbg_value);
  77. // Returns true if the result of |inst| is a vector or a scalar.
  78. bool HasVectorOrScalarResult(const Instruction* inst) const;
  79. // Returns true if the result of |inst| is a vector.
  80. bool HasVectorResult(const Instruction* inst) const;
  81. // Returns true if the result of |inst| is a scalar.
  82. bool HasScalarResult(const Instruction* inst) const;
  83. // Returns the number of elements in the vector type with id |type_id|.
  84. uint32_t GetVectorComponentCount(uint32_t type_id);
  85. // Adds |work_item| to |work_list| if it is not already live according to
  86. // |live_components|. |live_components| is updated to indicate that
  87. // |work_item| is now live.
  88. void AddItemToWorkListIfNeeded(WorkListItem work_item,
  89. LiveComponentMap* live_components,
  90. std::vector<WorkListItem>* work_list);
  91. // Marks the components |live_elements| of the uses in |current_inst| as live
  92. // according to |live_components|. If they were not live before, then they are
  93. // added to |work_list|.
  94. void MarkUsesAsLive(Instruction* current_inst,
  95. const utils::BitVector& live_elements,
  96. LiveComponentMap* live_components,
  97. std::vector<WorkListItem>* work_list);
  98. // Marks the uses in the OpVectorShuffle instruction in |current_item| as live
  99. // based on the live components in |current_item|. If anything becomes live
  100. // they are added to |work_list| and |live_components| is updated
  101. // accordingly.
  102. void MarkVectorShuffleUsesAsLive(const WorkListItem& current_item,
  103. VectorDCE::LiveComponentMap* live_components,
  104. std::vector<WorkListItem>* work_list);
  105. // Marks the uses in the OpCompositeInsert instruction in |current_item| as
  106. // live based on the live components in |current_item|. If anything becomes
  107. // live they are added to |work_list| and |live_components| is updated
  108. // accordingly.
  109. void MarkInsertUsesAsLive(const WorkListItem& current_item,
  110. LiveComponentMap* live_components,
  111. std::vector<WorkListItem>* work_list);
  112. // Marks the uses in the OpCompositeExtract instruction |current_inst| as
  113. // live. If anything becomes live they are added to |work_list| and
  114. // |live_components| is updated accordingly.
  115. void MarkExtractUseAsLive(const Instruction* current_inst,
  116. const utils::BitVector& live_elements,
  117. LiveComponentMap* live_components,
  118. std::vector<WorkListItem>* work_list);
  119. // Marks the uses in the OpCompositeConstruct instruction |current_inst| as
  120. // live. If anything becomes live they are added to |work_list| and
  121. // |live_components| is updated accordingly.
  122. void MarkCompositeContructUsesAsLive(WorkListItem work_item,
  123. LiveComponentMap* live_components,
  124. std::vector<WorkListItem>* work_list);
  125. // A BitVector that can always be used to say that all components of a vector
  126. // are live.
  127. utils::BitVector all_components_live_;
  128. };
  129. } // namespace opt
  130. } // namespace spvtools
  131. #endif // SOURCE_OPT_VECTOR_DCE_H_