eliminate_dead_constant_pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2016 Google Inc.
  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. #include "source/opt/eliminate_dead_constant_pass.h"
  15. #include <algorithm>
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #include <vector>
  19. #include "source/opt/def_use_manager.h"
  20. #include "source/opt/ir_context.h"
  21. #include "source/opt/log.h"
  22. #include "source/opt/reflect.h"
  23. namespace spvtools {
  24. namespace opt {
  25. Pass::Status EliminateDeadConstantPass::Process() {
  26. std::unordered_set<Instruction*> working_list;
  27. // Traverse all the instructions to get the initial set of dead constants as
  28. // working list and count number of real uses for constants. Uses in
  29. // annotation instructions do not count.
  30. std::unordered_map<Instruction*, size_t> use_counts;
  31. std::vector<Instruction*> constants = context()->GetConstants();
  32. for (auto* c : constants) {
  33. uint32_t const_id = c->result_id();
  34. size_t count = 0;
  35. context()->get_def_use_mgr()->ForEachUse(
  36. const_id, [&count](Instruction* user, uint32_t index) {
  37. (void)index;
  38. SpvOp op = user->opcode();
  39. if (!(IsAnnotationInst(op) || IsDebug1Inst(op) || IsDebug2Inst(op) ||
  40. IsDebug3Inst(op))) {
  41. ++count;
  42. }
  43. });
  44. use_counts[c] = count;
  45. if (!count) {
  46. working_list.insert(c);
  47. }
  48. }
  49. // Start from the constants with 0 uses, back trace through the def-use chain
  50. // to find all dead constants.
  51. std::unordered_set<Instruction*> dead_consts;
  52. while (!working_list.empty()) {
  53. Instruction* inst = *working_list.begin();
  54. // Back propagate if the instruction contains IDs in its operands.
  55. switch (inst->opcode()) {
  56. case SpvOp::SpvOpConstantComposite:
  57. case SpvOp::SpvOpSpecConstantComposite:
  58. case SpvOp::SpvOpSpecConstantOp:
  59. for (uint32_t i = 0; i < inst->NumInOperands(); i++) {
  60. // SpecConstantOp instruction contains 'opcode' as its operand. Need
  61. // to exclude such operands when decreasing uses.
  62. if (inst->GetInOperand(i).type != SPV_OPERAND_TYPE_ID) {
  63. continue;
  64. }
  65. uint32_t operand_id = inst->GetSingleWordInOperand(i);
  66. Instruction* def_inst =
  67. context()->get_def_use_mgr()->GetDef(operand_id);
  68. // If the use_count does not have any count for the def_inst,
  69. // def_inst must not be a constant, and should be ignored here.
  70. if (!use_counts.count(def_inst)) {
  71. continue;
  72. }
  73. // The number of uses should never be less then 0, so it can not be
  74. // less than 1 before it decreases.
  75. SPIRV_ASSERT(consumer(), use_counts[def_inst] > 0);
  76. --use_counts[def_inst];
  77. if (!use_counts[def_inst]) {
  78. working_list.insert(def_inst);
  79. }
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. dead_consts.insert(inst);
  86. working_list.erase(inst);
  87. }
  88. // Turn all dead instructions and uses of them to nop
  89. for (auto* dc : dead_consts) {
  90. context()->KillDef(dc->result_id());
  91. }
  92. return dead_consts.empty() ? Status::SuccessWithoutChange
  93. : Status::SuccessWithChange;
  94. }
  95. } // namespace opt
  96. } // namespace spvtools