simplification_pass.cpp 6.0 KB

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