early-access version 2592

This commit is contained in:
pineappleEA 2022-03-23 01:39:43 +01:00
parent a771dc9b88
commit c94418145e
4 changed files with 96 additions and 10 deletions

View file

@ -1,7 +1,7 @@
yuzu emulator early access yuzu emulator early access
============= =============
This is the source code for early-access 2591. This is the source code for early-access 2592.
## Legal Notice ## Legal Notice

View file

@ -326,6 +326,11 @@ void Inst::AddPhiOperand(Block* predecessor, const Value& value) {
phi_args.emplace_back(predecessor, value); phi_args.emplace_back(predecessor, value);
} }
void Inst::ErasePhiOperand(size_t index) {
const auto operand_it{phi_args.begin() + static_cast<u32>(index)};
phi_args.erase(operand_it);
}
void Inst::OrderPhiArgs() { void Inst::OrderPhiArgs() {
if (op != Opcode::Phi) { if (op != Opcode::Phi) {
throw LogicError("{} is not a Phi instruction", op); throw LogicError("{} is not a Phi instruction", op);

View file

@ -179,9 +179,13 @@ public:
/// Get a pointer to the block of a phi argument. /// Get a pointer to the block of a phi argument.
[[nodiscard]] Block* PhiBlock(size_t index) const; [[nodiscard]] Block* PhiBlock(size_t index) const;
/// Add phi operand to a phi instruction. /// Add phi operand to a phi instruction.
void AddPhiOperand(Block* predecessor, const Value& value); void AddPhiOperand(Block* predecessor, const Value& value);
// Erase the phi operand at the given index.
void ErasePhiOperand(size_t index);
/// Orders the Phi arguments from farthest away to nearest. /// Orders the Phi arguments from farthest away to nearest.
void OrderPhiArgs(); void OrderPhiArgs();

View file

@ -7,20 +7,97 @@
#include "shader_recompiler/ir_opt/passes.h" #include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization { namespace Shader::Optimization {
namespace {
void DeadCodeEliminationPass(IR::Program& program) { template <bool TEST_USES>
void DeadInstElimination(IR::Block* const block) {
// We iterate over the instructions in reverse order. // We iterate over the instructions in reverse order.
// This is because removing an instruction reduces the number of uses for earlier instructions. // 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()};
auto it{block->end()}; while (it != block->begin()) {
while (it != block->begin()) { --it;
--it; if constexpr (TEST_USES) {
if (!it->HasUses() && !it->MayHaveSideEffects()) { if (it->HasUses() || it->MayHaveSideEffects()) {
it->Invalidate(); continue;
it = block->Instructions().erase(it); }
}
it->Invalidate();
it = block->Instructions().erase(it);
}
}
void DeletedPhiArgElimination(IR::Program& program, const std::vector<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) {
const auto it{std::find(dead_blocks.begin(), dead_blocks.end(), phi.PhiBlock(i))};
if (it == dead_blocks.end()) {
continue;
}
// Phi operand at this index is an unreachable block
phi.ErasePhiOperand(i);
--i;
} }
} }
} }
} }
void DeadBranchElimination(IR::Program& program) {
std::vector<IR::Block*> 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, dead_blocks);
}
}
} // namespace
void DeadCodeEliminationPass(IR::Program& program) {
DeadBranchElimination(program);
for (IR::Block* const block : program.post_order_blocks) {
DeadInstElimination<true>(block);
}
}
} // namespace Shader::Optimization } // namespace Shader::Optimization