diff options
| -rw-r--r-- | src/shader_recompiler/frontend/ir/microinstruction.cpp | 5 | ||||
| -rw-r--r-- | src/shader_recompiler/frontend/ir/value.h | 4 | ||||
| -rw-r--r-- | src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp | 98 |
3 files changed, 98 insertions, 9 deletions
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.cpp b/src/shader_recompiler/frontend/ir/microinstruction.cpp index 631446cf7..4a2564f47 100644 --- a/src/shader_recompiler/frontend/ir/microinstruction.cpp +++ b/src/shader_recompiler/frontend/ir/microinstruction.cpp | |||
| @@ -326,6 +326,11 @@ void Inst::AddPhiOperand(Block* predecessor, const Value& value) { | |||
| 326 | phi_args.emplace_back(predecessor, value); | 326 | phi_args.emplace_back(predecessor, value); |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | void Inst::ErasePhiOperand(size_t index) { | ||
| 330 | const auto operand_it{phi_args.begin() + static_cast<ptrdiff_t>(index)}; | ||
| 331 | phi_args.erase(operand_it); | ||
| 332 | } | ||
| 333 | |||
| 329 | void Inst::OrderPhiArgs() { | 334 | void Inst::OrderPhiArgs() { |
| 330 | if (op != Opcode::Phi) { | 335 | if (op != Opcode::Phi) { |
| 331 | throw LogicError("{} is not a Phi instruction", op); | 336 | throw LogicError("{} is not a Phi instruction", op); |
diff --git a/src/shader_recompiler/frontend/ir/value.h b/src/shader_recompiler/frontend/ir/value.h index 947579852..14f6e55bc 100644 --- a/src/shader_recompiler/frontend/ir/value.h +++ b/src/shader_recompiler/frontend/ir/value.h | |||
| @@ -179,9 +179,13 @@ public: | |||
| 179 | 179 | ||
| 180 | /// Get a pointer to the block of a phi argument. | 180 | /// Get a pointer to the block of a phi argument. |
| 181 | [[nodiscard]] Block* PhiBlock(size_t index) const; | 181 | [[nodiscard]] Block* PhiBlock(size_t index) const; |
| 182 | |||
| 182 | /// Add phi operand to a phi instruction. | 183 | /// Add phi operand to a phi instruction. |
| 183 | void AddPhiOperand(Block* predecessor, const Value& value); | 184 | void AddPhiOperand(Block* predecessor, const Value& value); |
| 184 | 185 | ||
| 186 | // Erase the phi operand at the given index. | ||
| 187 | void ErasePhiOperand(size_t index); | ||
| 188 | |||
| 185 | /// Orders the Phi arguments from farthest away to nearest. | 189 | /// Orders the Phi arguments from farthest away to nearest. |
| 186 | void OrderPhiArgs(); | 190 | void OrderPhiArgs(); |
| 187 | 191 | ||
diff --git a/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp b/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp index 400836301..6697fde85 100644 --- a/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp +++ b/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp | |||
| @@ -2,24 +2,104 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | |||
| 7 | #include <boost/container/small_vector.hpp> | ||
| 8 | |||
| 5 | #include "shader_recompiler/frontend/ir/basic_block.h" | 9 | #include "shader_recompiler/frontend/ir/basic_block.h" |
| 6 | #include "shader_recompiler/frontend/ir/value.h" | 10 | #include "shader_recompiler/frontend/ir/value.h" |
| 7 | #include "shader_recompiler/ir_opt/passes.h" | 11 | #include "shader_recompiler/ir_opt/passes.h" |
| 8 | 12 | ||
| 9 | namespace Shader::Optimization { | 13 | namespace Shader::Optimization { |
| 10 | 14 | namespace { | |
| 11 | void DeadCodeEliminationPass(IR::Program& program) { | 15 | template <bool TEST_USES> |
| 16 | void DeadInstElimination(IR::Block* const block) { | ||
| 12 | // We iterate over the instructions in reverse order. | 17 | // We iterate over the instructions in reverse order. |
| 13 | // This is because removing an instruction reduces the number of uses for earlier instructions. | 18 | // This is because removing an instruction reduces the number of uses for earlier instructions. |
| 14 | for (IR::Block* const block : program.post_order_blocks) { | 19 | auto it{block->end()}; |
| 15 | auto it{block->end()}; | 20 | while (it != block->begin()) { |
| 16 | while (it != block->begin()) { | 21 | --it; |
| 17 | --it; | 22 | if constexpr (TEST_USES) { |
| 18 | if (!it->HasUses() && !it->MayHaveSideEffects()) { | 23 | if (it->HasUses() || it->MayHaveSideEffects()) { |
| 19 | it->Invalidate(); | 24 | continue; |
| 20 | it = block->Instructions().erase(it); | 25 | } |
| 26 | } | ||
| 27 | it->Invalidate(); | ||
| 28 | it = block->Instructions().erase(it); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | void DeletedPhiArgElimination(IR::Program& program, std::span<const IR::Block*> dead_blocks) { | ||
| 33 | for (IR::Block* const block : program.blocks) { | ||
| 34 | for (IR::Inst& phi : *block) { | ||
| 35 | if (!IR::IsPhi(phi)) { | ||
| 36 | continue; | ||
| 37 | } | ||
| 38 | for (size_t i = 0; i < phi.NumArgs(); ++i) { | ||
| 39 | if (std::ranges::find(dead_blocks, phi.PhiBlock(i)) == dead_blocks.end()) { | ||
| 40 | continue; | ||
| 41 | } | ||
| 42 | // Phi operand at this index is an unreachable block | ||
| 43 | phi.ErasePhiOperand(i); | ||
| 44 | --i; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | void DeadBranchElimination(IR::Program& program) { | ||
| 51 | boost::container::small_vector<const IR::Block*, 3> dead_blocks; | ||
| 52 | const auto begin_it{program.syntax_list.begin()}; | ||
| 53 | for (auto node_it = begin_it; node_it != program.syntax_list.end(); ++node_it) { | ||
| 54 | if (node_it->type != IR::AbstractSyntaxNode::Type::If) { | ||
| 55 | continue; | ||
| 56 | } | ||
| 57 | IR::Inst* const cond_ref{node_it->data.if_node.cond.Inst()}; | ||
| 58 | const IR::U1 cond{cond_ref->Arg(0)}; | ||
| 59 | if (!cond.IsImmediate()) { | ||
| 60 | continue; | ||
| 61 | } | ||
| 62 | if (cond.U1()) { | ||
| 63 | continue; | ||
| 64 | } | ||
| 65 | // False immediate condition. Remove condition ref, erase the entire branch. | ||
| 66 | cond_ref->Invalidate(); | ||
| 67 | // Account for nested if-statements within the if(false) branch | ||
| 68 | u32 nested_ifs{1u}; | ||
| 69 | while (node_it->type != IR::AbstractSyntaxNode::Type::EndIf || nested_ifs > 0) { | ||
| 70 | node_it = program.syntax_list.erase(node_it); | ||
| 71 | switch (node_it->type) { | ||
| 72 | case IR::AbstractSyntaxNode::Type::If: | ||
| 73 | ++nested_ifs; | ||
| 74 | break; | ||
| 75 | case IR::AbstractSyntaxNode::Type::EndIf: | ||
| 76 | --nested_ifs; | ||
| 77 | break; | ||
| 78 | case IR::AbstractSyntaxNode::Type::Block: { | ||
| 79 | IR::Block* const block{node_it->data.block}; | ||
| 80 | DeadInstElimination<false>(block); | ||
| 81 | dead_blocks.push_back(block); | ||
| 82 | break; | ||
| 83 | } | ||
| 84 | default: | ||
| 85 | break; | ||
| 21 | } | 86 | } |
| 22 | } | 87 | } |
| 88 | // Erase EndIf node of the if(false) branch | ||
| 89 | node_it = program.syntax_list.erase(node_it); | ||
| 90 | // Account for loop increment | ||
| 91 | --node_it; | ||
| 92 | } | ||
| 93 | if (!dead_blocks.empty()) { | ||
| 94 | DeletedPhiArgElimination(program, std::span(dead_blocks.data(), dead_blocks.size())); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } // namespace | ||
| 98 | |||
| 99 | void DeadCodeEliminationPass(IR::Program& program) { | ||
| 100 | DeadBranchElimination(program); | ||
| 101 | for (IR::Block* const block : program.post_order_blocks) { | ||
| 102 | DeadInstElimination<true>(block); | ||
| 23 | } | 103 | } |
| 24 | } | 104 | } |
| 25 | 105 | ||