| 44 | } |
| 45 | |
| 46 | bool SimplificationPass::SimplifyFunction(Function* function) { |
| 47 | if (function->IsDeclaration()) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | bool modified = false; |
| 52 | // Phase 1: Traverse all instructions in dominance order. |
| 53 | // The second phase will only be on the instructions whose inputs have changed |
| 54 | // after being processed during phase 1. Since OpPhi instructions are the |
| 55 | // only instructions whose inputs do not necessarily dominate the use, we keep |
| 56 | // track of the OpPhi instructions already seen, and add them to the work list |
| 57 | // for phase 2 when needed. |
| 58 | std::vector<Instruction*> work_list; |
| 59 | std::unordered_set<Instruction*> process_phis; |
| 60 | std::unordered_set<Instruction*> inst_to_kill; |
| 61 | std::unordered_set<Instruction*> in_work_list; |
| 62 | std::unordered_set<Instruction*> inst_seen; |
| 63 | const InstructionFolder& folder = context()->get_instruction_folder(); |
| 64 | |
| 65 | cfg()->ForEachBlockInReversePostOrder( |
| 66 | function->entry().get(), |
| 67 | [&modified, &process_phis, &work_list, &in_work_list, &inst_to_kill, |
| 68 | &folder, &inst_seen, this](BasicBlock* bb) { |
| 69 | for (Instruction* inst = &*bb->begin(); inst; inst = inst->NextNode()) { |
| 70 | inst_seen.insert(inst); |
| 71 | if (inst->opcode() == spv::Op::OpPhi) { |
| 72 | process_phis.insert(inst); |
| 73 | } |
| 74 | |
| 75 | bool is_foldable_copy = |
| 76 | inst->opcode() == spv::Op::OpCopyObject && |
| 77 | context()->get_decoration_mgr()->HaveSubsetOfDecorations( |
| 78 | inst->result_id(), inst->GetSingleWordInOperand(0)); |
| 79 | |
| 80 | if (is_foldable_copy || folder.FoldInstruction(inst)) { |
| 81 | modified = true; |
| 82 | context()->AnalyzeUses(inst); |
| 83 | get_def_use_mgr()->ForEachUser(inst, [&work_list, &process_phis, |
| 84 | &in_work_list]( |
| 85 | Instruction* use) { |
| 86 | if (process_phis.count(use) && in_work_list.insert(use).second) { |
| 87 | work_list.push_back(use); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | AddNewOperands(inst, &inst_seen, &work_list); |
| 92 | |
| 93 | if (inst->opcode() == spv::Op::OpCopyObject) { |
| 94 | context()->ReplaceAllUsesWithPredicate( |
| 95 | inst->result_id(), inst->GetSingleWordInOperand(0), |
| 96 | [](Instruction* user) { |
| 97 | const auto opcode = user->opcode(); |
| 98 | if (!spvOpcodeIsDebug(opcode) && |
| 99 | !spvOpcodeIsDecoration(opcode)) { |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | }); |
nothing calls this directly
no test coverage detected