summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/ir_opt
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/ir_opt')
-rw-r--r--src/shader_recompiler/ir_opt/constant_propagation_pass.cpp1
-rw-r--r--src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp98
-rw-r--r--src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp3
-rw-r--r--src/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp3
-rw-r--r--src/shader_recompiler/ir_opt/passes.h3
-rw-r--r--src/shader_recompiler/ir_opt/rescaling_pass.cpp1
-rw-r--r--src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp1
7 files changed, 89 insertions, 21 deletions
diff --git a/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp b/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp
index c134a12bc..2a14e7f12 100644
--- a/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp
+++ b/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp
@@ -8,7 +8,6 @@
#include <type_traits>
#include "common/bit_cast.h"
-#include "common/bit_util.h"
#include "shader_recompiler/exception.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/value.h"
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 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <algorithm>
+
+#include <boost/container/small_vector.hpp>
+
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/value.h"
#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 DeletedPhiArgElimination(IR::Program& program, std::span<const IR::Block*> dead_blocks) {
+ for (IR::Block* const block : program.blocks) {
+ for (IR::Inst& phi : *block) {
+ if (!IR::IsPhi(phi)) {
+ continue;
+ }
+ for (size_t i = 0; i < phi.NumArgs(); ++i) {
+ if (std::ranges::find(dead_blocks, phi.PhiBlock(i)) == dead_blocks.end()) {
+ continue;
+ }
+ // Phi operand at this index is an unreachable block
+ phi.ErasePhiOperand(i);
+ --i;
+ }
+ }
+ }
+}
+
+void DeadBranchElimination(IR::Program& program) {
+ boost::container::small_vector<const IR::Block*, 3> dead_blocks;
+ 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);
+ dead_blocks.push_back(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;
+ }
+ if (!dead_blocks.empty()) {
+ DeletedPhiArgElimination(program, std::span(dead_blocks.data(), dead_blocks.size()));
+ }
+}
+} // namespace
+
+void DeadCodeEliminationPass(IR::Program& program) {
+ DeadBranchElimination(program);
+ for (IR::Block* const block : program.post_order_blocks) {
+ DeadInstElimination<true>(block);
}
}
diff --git a/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp b/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
index ddf497e32..3cc1cc07a 100644
--- a/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
+++ b/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
@@ -2,10 +2,7 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include <algorithm>
-#include <compare>
#include <optional>
-#include <queue>
#include <boost/container/flat_set.hpp>
#include <boost/container/small_vector.hpp>
diff --git a/src/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp b/src/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp
index 773e1f961..622f94fc7 100644
--- a/src/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp
+++ b/src/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp
@@ -2,9 +2,6 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include <algorithm>
-
-#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
diff --git a/src/shader_recompiler/ir_opt/passes.h b/src/shader_recompiler/ir_opt/passes.h
index f877c7ba0..16ea3d80a 100644
--- a/src/shader_recompiler/ir_opt/passes.h
+++ b/src/shader_recompiler/ir_opt/passes.h
@@ -4,10 +4,7 @@
#pragma once
-#include <span>
-
#include "shader_recompiler/environment.h"
-#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/program.h"
namespace Shader::Optimization {
diff --git a/src/shader_recompiler/ir_opt/rescaling_pass.cpp b/src/shader_recompiler/ir_opt/rescaling_pass.cpp
index 496d4667e..75679c793 100644
--- a/src/shader_recompiler/ir_opt/rescaling_pass.cpp
+++ b/src/shader_recompiler/ir_opt/rescaling_pass.cpp
@@ -2,7 +2,6 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include "common/alignment.h"
#include "common/settings.h"
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
diff --git a/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp b/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
index 87aa09358..928557acb 100644
--- a/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
+++ b/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
@@ -20,7 +20,6 @@
#include <vector>
#include <boost/container/flat_map.hpp>
-#include <boost/container/flat_set.hpp>
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/opcodes.h"