simplification_pass.cpp 6.1 KB

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