eliminate_dead_constant_pass.cpp 3.6 KB

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