dead_insert_elim_pass.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2018 The Khronos Group Inc.
  2. // Copyright (c) 2018 Valve Corporation
  3. // Copyright (c) 2018 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #ifndef SOURCE_OPT_DEAD_INSERT_ELIM_PASS_H_
  17. #define SOURCE_OPT_DEAD_INSERT_ELIM_PASS_H_
  18. #include <algorithm>
  19. #include <map>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <vector>
  24. #include "source/opt/basic_block.h"
  25. #include "source/opt/def_use_manager.h"
  26. #include "source/opt/ir_context.h"
  27. #include "source/opt/mem_pass.h"
  28. #include "source/opt/module.h"
  29. namespace spvtools {
  30. namespace opt {
  31. // See optimizer.hpp for documentation.
  32. class DeadInsertElimPass : public MemPass {
  33. public:
  34. DeadInsertElimPass() = default;
  35. const char* name() const override { return "eliminate-dead-inserts"; }
  36. Status Process() override;
  37. IRContext::Analysis GetPreservedAnalyses() override {
  38. return IRContext::kAnalysisDefUse |
  39. IRContext::kAnalysisInstrToBlockMapping |
  40. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  41. IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
  42. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  43. IRContext::kAnalysisTypes;
  44. }
  45. private:
  46. // Return the number of subcomponents in the composite type |typeId|.
  47. // Return 0 if not a composite type or number of components is not a
  48. // 32-bit constant.
  49. uint32_t NumComponents(Instruction* typeInst);
  50. // Mark all inserts in instruction chain ending at |insertChain| with
  51. // indices that intersect with extract indices |extIndices| starting with
  52. // index at |extOffset|. Chains are composed solely of Inserts and Phis.
  53. // Mark all inserts in chain if |extIndices| is nullptr.
  54. void MarkInsertChain(Instruction* insertChain,
  55. std::vector<uint32_t>* extIndices, uint32_t extOffset,
  56. std::unordered_set<uint32_t>* visited_phis);
  57. // Perform EliminateDeadInsertsOnePass(|func|) until no modification is
  58. // made. Return true if modified.
  59. bool EliminateDeadInserts(Function* func);
  60. // DCE all dead struct, matrix and vector inserts in |func|. An insert is
  61. // dead if the value it inserts is never used. Replace any reference to the
  62. // insert with its original composite. Return true if modified. Dead inserts
  63. // in dependence cycles are not currently eliminated. Dead inserts into
  64. // arrays are not currently eliminated.
  65. bool EliminateDeadInsertsOnePass(Function* func);
  66. // Return true if all extensions in this module are allowed by this pass.
  67. bool AllExtensionsSupported() const;
  68. // Live inserts
  69. std::unordered_set<uint32_t> liveInserts_;
  70. // Visited phis as insert chain is traversed; used to avoid infinite loop
  71. std::unordered_map<uint32_t, bool> visitedPhis_;
  72. };
  73. } // namespace opt
  74. } // namespace spvtools
  75. #endif // SOURCE_OPT_DEAD_INSERT_ELIM_PASS_H_