simplification_pass.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "source/opt/simplification_pass.h"
  15. #include <set>
  16. #include <unordered_set>
  17. #include <vector>
  18. #include "source/opt/fold.h"
  19. namespace spvtools {
  20. namespace opt {
  21. Pass::Status SimplificationPass::Process() {
  22. bool modified = false;
  23. for (Function& function : *get_module()) {
  24. modified |= SimplifyFunction(&function);
  25. }
  26. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  27. }
  28. bool SimplificationPass::SimplifyFunction(Function* function) {
  29. bool modified = false;
  30. // Phase 1: Traverse all instructions in dominance order.
  31. // The second phase will only be on the instructions whose inputs have changed
  32. // after being processed during phase 1. Since OpPhi instructions are the
  33. // only instructions whose inputs do not necessarily dominate the use, we keep
  34. // track of the OpPhi instructions already seen, and add them to the work list
  35. // for phase 2 when needed.
  36. std::vector<Instruction*> work_list;
  37. std::unordered_set<Instruction*> process_phis;
  38. std::unordered_set<Instruction*> inst_to_kill;
  39. std::unordered_set<Instruction*> in_work_list;
  40. const InstructionFolder& folder = context()->get_instruction_folder();
  41. cfg()->ForEachBlockInReversePostOrder(
  42. function->entry().get(),
  43. [&modified, &process_phis, &work_list, &in_work_list, &inst_to_kill,
  44. folder, this](BasicBlock* bb) {
  45. for (Instruction* inst = &*bb->begin(); inst; inst = inst->NextNode()) {
  46. if (inst->opcode() == SpvOpPhi) {
  47. process_phis.insert(inst);
  48. }
  49. bool is_foldable_copy =
  50. inst->opcode() == SpvOpCopyObject &&
  51. context()->get_decoration_mgr()->HaveSubsetOfDecorations(
  52. inst->result_id(), inst->GetSingleWordInOperand(0));
  53. if (is_foldable_copy || folder.FoldInstruction(inst)) {
  54. modified = true;
  55. context()->AnalyzeUses(inst);
  56. get_def_use_mgr()->ForEachUser(inst, [&work_list, &process_phis,
  57. &in_work_list](
  58. Instruction* use) {
  59. if (process_phis.count(use) && in_work_list.insert(use).second) {
  60. work_list.push_back(use);
  61. }
  62. });
  63. if (inst->opcode() == SpvOpCopyObject) {
  64. context()->ReplaceAllUsesWithPredicate(
  65. inst->result_id(), inst->GetSingleWordInOperand(0),
  66. [](Instruction* user, uint32_t) {
  67. const auto opcode = user->opcode();
  68. if (!spvOpcodeIsDebug(opcode) &&
  69. !spvOpcodeIsDecoration(opcode)) {
  70. return true;
  71. }
  72. return false;
  73. });
  74. inst_to_kill.insert(inst);
  75. in_work_list.insert(inst);
  76. } else if (inst->opcode() == SpvOpNop) {
  77. inst_to_kill.insert(inst);
  78. in_work_list.insert(inst);
  79. }
  80. }
  81. }
  82. });
  83. // Phase 2: process the instructions in the work list until all of the work is
  84. // done. This time we add all users to the work list because phase 1
  85. // has already finished.
  86. for (size_t i = 0; i < work_list.size(); ++i) {
  87. Instruction* inst = work_list[i];
  88. in_work_list.erase(inst);
  89. bool is_foldable_copy =
  90. inst->opcode() == SpvOpCopyObject &&
  91. context()->get_decoration_mgr()->HaveSubsetOfDecorations(
  92. inst->result_id(), inst->GetSingleWordInOperand(0));
  93. if (is_foldable_copy || folder.FoldInstruction(inst)) {
  94. modified = true;
  95. context()->AnalyzeUses(inst);
  96. get_def_use_mgr()->ForEachUser(
  97. inst, [&work_list, &in_work_list](Instruction* use) {
  98. if (!use->IsDecoration() && use->opcode() != SpvOpName &&
  99. in_work_list.insert(use).second) {
  100. work_list.push_back(use);
  101. }
  102. });
  103. if (inst->opcode() == SpvOpCopyObject) {
  104. context()->ReplaceAllUsesWithPredicate(
  105. inst->result_id(), inst->GetSingleWordInOperand(0),
  106. [](Instruction* user, uint32_t) {
  107. const auto opcode = user->opcode();
  108. if (!spvOpcodeIsDebug(opcode) && !spvOpcodeIsDecoration(opcode)) {
  109. return true;
  110. }
  111. return false;
  112. });
  113. inst_to_kill.insert(inst);
  114. in_work_list.insert(inst);
  115. } else if (inst->opcode() == SpvOpNop) {
  116. inst_to_kill.insert(inst);
  117. in_work_list.insert(inst);
  118. }
  119. }
  120. }
  121. // Phase 3: Kill instructions we know are no longer needed.
  122. for (Instruction* inst : inst_to_kill) {
  123. context()->KillInst(inst);
  124. }
  125. return modified;
  126. }
  127. } // namespace opt
  128. } // namespace spvtools