diff options
author | ameerj <52414509+ameerj@users.noreply.github.com> | 2022-03-22 06:22:21 +0100 |
---|---|---|
committer | ameerj <52414509+ameerj@users.noreply.github.com> | 2022-03-22 07:39:31 +0100 |
commit | f10d40a0a25dc6709b8cbd0a6793175434db6472 (patch) | |
tree | 2f70f9d51b5ace18d8341cf65815d5c172fd7964 /src | |
parent | Merge pull request #8038 from liamwhite/exit-register-detection (diff) | |
download | yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.tar yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.tar.gz yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.tar.bz2 yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.tar.lz yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.tar.xz yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.tar.zst yuzu-f10d40a0a25dc6709b8cbd0a6793175434db6472.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp | 71 |
1 files changed, 62 insertions, 9 deletions
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..6c7c7b32d 100644 --- a/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp +++ b/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp @@ -7,19 +7,72 @@ #include "shader_recompiler/ir_opt/passes.h" namespace Shader::Optimization { - -void DeadCodeEliminationPass(IR::Program& program) { +namespace { +template <bool TEST_USES> +void DeadInstElimination(IR::Block* const block) { // We iterate over the instructions in reverse order. // This is because removing an instruction reduces the number of uses for earlier instructions. - for (IR::Block* const block : program.post_order_blocks) { - auto it{block->end()}; - while (it != block->begin()) { - --it; - if (!it->HasUses() && !it->MayHaveSideEffects()) { - it->Invalidate(); - it = block->Instructions().erase(it); + auto it{block->end()}; + while (it != block->begin()) { + --it; + if constexpr (TEST_USES) { + if (it->HasUses() || it->MayHaveSideEffects()) { + continue; } } + it->Invalidate(); + it = block->Instructions().erase(it); + } +} + +void DeadBranchElimination(IR::Program& program) { + const auto begin_it{program.syntax_list.begin()}; + for (auto node_it = begin_it; node_it != program.syntax_list.end(); ++node_it) { + if (node_it->type != IR::AbstractSyntaxNode::Type::If) { + continue; + } + IR::Inst* const cond_ref{node_it->data.if_node.cond.Inst()}; + const IR::U1 cond{cond_ref->Arg(0)}; + if (!cond.IsImmediate()) { + continue; + } + if (cond.U1()) { + continue; + } + // False immediate condition. Remove condition ref, erase the entire branch. + cond_ref->Invalidate(); + // Account for nested if-statements within the if(false) branch + u32 nested_ifs{1u}; + while (node_it->type != IR::AbstractSyntaxNode::Type::EndIf || nested_ifs > 0) { + node_it = program.syntax_list.erase(node_it); + switch (node_it->type) { + case IR::AbstractSyntaxNode::Type::If: + ++nested_ifs; + break; + case IR::AbstractSyntaxNode::Type::EndIf: + --nested_ifs; + break; + case IR::AbstractSyntaxNode::Type::Block: { + IR::Block* const block{node_it->data.block}; + DeadInstElimination<false>(block); + break; + } + default: + break; + } + } + // Erase EndIf node of the if(false) branch + node_it = program.syntax_list.erase(node_it); + // Account for loop increment + --node_it; + } +} +} // namespace + +void DeadCodeEliminationPass(IR::Program& program) { + DeadBranchElimination(program); + for (IR::Block* const block : program.post_order_blocks) { + DeadInstElimination<true>(block); } } |