simplification_pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (inst->opcode() == SpvOpCopyObject ||
  50. folder.FoldInstruction(inst)) {
  51. modified = true;
  52. context()->AnalyzeUses(inst);
  53. get_def_use_mgr()->ForEachUser(inst, [&work_list, &process_phis,
  54. &in_work_list](
  55. Instruction* use) {
  56. if (process_phis.count(use) && in_work_list.insert(use).second) {
  57. work_list.push_back(use);
  58. }
  59. });
  60. if (inst->opcode() == SpvOpCopyObject) {
  61. context()->ReplaceAllUsesWith(inst->result_id(),
  62. inst->GetSingleWordInOperand(0));
  63. inst_to_kill.insert(inst);
  64. in_work_list.insert(inst);
  65. } else if (inst->opcode() == SpvOpNop) {
  66. inst_to_kill.insert(inst);
  67. in_work_list.insert(inst);
  68. }
  69. }
  70. }
  71. });
  72. // Phase 2: process the instructions in the work list until all of the work is
  73. // done. This time we add all users to the work list because phase 1
  74. // has already finished.
  75. for (size_t i = 0; i < work_list.size(); ++i) {
  76. Instruction* inst = work_list[i];
  77. in_work_list.erase(inst);
  78. if (inst->opcode() == SpvOpCopyObject || folder.FoldInstruction(inst)) {
  79. modified = true;
  80. context()->AnalyzeUses(inst);
  81. get_def_use_mgr()->ForEachUser(
  82. inst, [&work_list, &in_work_list](Instruction* use) {
  83. if (!use->IsDecoration() && use->opcode() != SpvOpName &&
  84. in_work_list.insert(use).second) {
  85. work_list.push_back(use);
  86. }
  87. });
  88. if (inst->opcode() == SpvOpCopyObject) {
  89. context()->ReplaceAllUsesWith(inst->result_id(),
  90. inst->GetSingleWordInOperand(0));
  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. // Phase 3: Kill instructions we know are no longer needed.
  100. for (Instruction* inst : inst_to_kill) {
  101. context()->KillInst(inst);
  102. }
  103. return modified;
  104. }
  105. } // namespace opt
  106. } // namespace spvtools