2021-02-02 18:07:00 -06:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
// This file implements the SSA rewriting algorithm proposed in
|
|
|
|
//
|
|
|
|
// Simple and Efficient Construction of Static Single Assignment Form.
|
2021-07-09 15:11:47 -05:00
|
|
|
// Braun M., Buchwald S., Hack S., Leiba R., Mallon C., Zwinkau A. (2013)
|
2021-02-02 18:07:00 -06:00
|
|
|
// In: Jhala R., De Bosschere K. (eds)
|
|
|
|
// Compiler Construction. CC 2013.
|
|
|
|
// Lecture Notes in Computer Science, vol 7791.
|
|
|
|
// Springer, Berlin, Heidelberg
|
|
|
|
//
|
|
|
|
// https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <boost/container/flat_map.hpp>
|
|
|
|
|
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/function.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/microinstruction.h"
|
2021-02-05 20:11:23 -06:00
|
|
|
#include "shader_recompiler/frontend/ir/opcodes.h"
|
2021-02-02 18:07:00 -06:00
|
|
|
#include "shader_recompiler/frontend/ir/pred.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/reg.h"
|
|
|
|
#include "shader_recompiler/ir_opt/passes.h"
|
|
|
|
|
|
|
|
namespace Shader::Optimization {
|
|
|
|
namespace {
|
|
|
|
using ValueMap = boost::container::flat_map<IR::Block*, IR::Value, std::less<IR::Block*>>;
|
|
|
|
|
2021-02-05 02:58:02 -06:00
|
|
|
struct FlagTag {};
|
|
|
|
struct ZeroFlagTag : FlagTag {};
|
|
|
|
struct SignFlagTag : FlagTag {};
|
|
|
|
struct CarryFlagTag : FlagTag {};
|
|
|
|
struct OverflowFlagTag : FlagTag {};
|
|
|
|
|
2021-02-11 13:39:06 -06:00
|
|
|
struct GotoVariable : FlagTag {
|
|
|
|
GotoVariable() = default;
|
|
|
|
explicit GotoVariable(u32 index_) : index{index_} {}
|
|
|
|
|
|
|
|
u32 index;
|
|
|
|
};
|
|
|
|
|
2021-02-02 18:07:00 -06:00
|
|
|
struct DefTable {
|
|
|
|
[[nodiscard]] ValueMap& operator[](IR::Reg variable) noexcept {
|
|
|
|
return regs[IR::RegIndex(variable)];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ValueMap& operator[](IR::Pred variable) noexcept {
|
|
|
|
return preds[IR::PredIndex(variable)];
|
|
|
|
}
|
|
|
|
|
2021-02-11 13:39:06 -06:00
|
|
|
[[nodiscard]] ValueMap& operator[](GotoVariable goto_variable) {
|
|
|
|
return goto_vars[goto_variable.index];
|
|
|
|
}
|
|
|
|
|
2021-02-05 02:58:02 -06:00
|
|
|
[[nodiscard]] ValueMap& operator[](ZeroFlagTag) noexcept {
|
|
|
|
return zero_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ValueMap& operator[](SignFlagTag) noexcept {
|
|
|
|
return sign_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ValueMap& operator[](CarryFlagTag) noexcept {
|
|
|
|
return carry_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ValueMap& operator[](OverflowFlagTag) noexcept {
|
|
|
|
return overflow_flag;
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:07:00 -06:00
|
|
|
std::array<ValueMap, IR::NUM_USER_REGS> regs;
|
|
|
|
std::array<ValueMap, IR::NUM_USER_PREDS> preds;
|
2021-02-11 13:39:06 -06:00
|
|
|
boost::container::flat_map<u32, ValueMap> goto_vars;
|
2021-02-05 02:58:02 -06:00
|
|
|
ValueMap zero_flag;
|
|
|
|
ValueMap sign_flag;
|
|
|
|
ValueMap carry_flag;
|
|
|
|
ValueMap overflow_flag;
|
2021-02-02 18:07:00 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
IR::Opcode UndefOpcode(IR::Reg) noexcept {
|
2021-02-11 13:39:06 -06:00
|
|
|
return IR::Opcode::UndefU32;
|
2021-02-02 18:07:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
IR::Opcode UndefOpcode(IR::Pred) noexcept {
|
2021-02-11 13:39:06 -06:00
|
|
|
return IR::Opcode::UndefU1;
|
2021-02-02 18:07:00 -06:00
|
|
|
}
|
|
|
|
|
2021-02-05 02:58:02 -06:00
|
|
|
IR::Opcode UndefOpcode(const FlagTag&) noexcept {
|
2021-02-11 13:39:06 -06:00
|
|
|
return IR::Opcode::UndefU1;
|
2021-02-05 02:58:02 -06:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:07:00 -06:00
|
|
|
[[nodiscard]] bool IsPhi(const IR::Inst& inst) noexcept {
|
|
|
|
return inst.Opcode() == IR::Opcode::Phi;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Pass {
|
|
|
|
public:
|
|
|
|
void WriteVariable(auto variable, IR::Block* block, const IR::Value& value) {
|
|
|
|
current_def[variable].insert_or_assign(block, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Value ReadVariable(auto variable, IR::Block* block) {
|
|
|
|
auto& def{current_def[variable]};
|
|
|
|
if (const auto it{def.find(block)}; it != def.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return ReadVariableRecursive(variable, block);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IR::Value ReadVariableRecursive(auto variable, IR::Block* block) {
|
|
|
|
IR::Value val;
|
|
|
|
if (const std::span preds{block->ImmediatePredecessors()}; preds.size() == 1) {
|
2021-02-13 22:24:32 -06:00
|
|
|
// Optimize the common case of one predecessor: no phi needed
|
2021-02-02 18:07:00 -06:00
|
|
|
val = ReadVariable(variable, preds.front());
|
|
|
|
} else {
|
|
|
|
// Break potential cycles with operandless phi
|
2021-02-05 23:38:22 -06:00
|
|
|
IR::Inst& phi_inst{*block->PrependNewInst(block->begin(), IR::Opcode::Phi)};
|
|
|
|
val = IR::Value{&phi_inst};
|
2021-02-02 18:07:00 -06:00
|
|
|
WriteVariable(variable, block, val);
|
2021-02-05 23:38:22 -06:00
|
|
|
val = AddPhiOperands(variable, phi_inst, block);
|
2021-02-02 18:07:00 -06:00
|
|
|
}
|
|
|
|
WriteVariable(variable, block, val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2021-02-05 23:38:22 -06:00
|
|
|
IR::Value AddPhiOperands(auto variable, IR::Inst& phi, IR::Block* block) {
|
2021-02-02 18:07:00 -06:00
|
|
|
for (IR::Block* const pred : block->ImmediatePredecessors()) {
|
2021-02-05 23:38:22 -06:00
|
|
|
phi.AddPhiOperand(pred, ReadVariable(variable, pred));
|
2021-02-02 18:07:00 -06:00
|
|
|
}
|
|
|
|
return TryRemoveTrivialPhi(phi, block, UndefOpcode(variable));
|
|
|
|
}
|
|
|
|
|
2021-02-05 23:38:22 -06:00
|
|
|
IR::Value TryRemoveTrivialPhi(IR::Inst& phi, IR::Block* block, IR::Opcode undef_opcode) {
|
2021-02-02 18:07:00 -06:00
|
|
|
IR::Value same;
|
2021-02-05 23:38:22 -06:00
|
|
|
const size_t num_args{phi.NumArgs()};
|
|
|
|
for (size_t arg_index = 0; arg_index < num_args; ++arg_index) {
|
|
|
|
const IR::Value& op{phi.Arg(arg_index)};
|
|
|
|
if (op == same || op == IR::Value{&phi}) {
|
2021-02-02 18:07:00 -06:00
|
|
|
// Unique value or self-reference
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!same.IsEmpty()) {
|
|
|
|
// The phi merges at least two values: not trivial
|
2021-02-05 23:38:22 -06:00
|
|
|
return IR::Value{&phi};
|
2021-02-02 18:07:00 -06:00
|
|
|
}
|
|
|
|
same = op;
|
|
|
|
}
|
|
|
|
if (same.IsEmpty()) {
|
|
|
|
// The phi is unreachable or in the start block
|
|
|
|
const auto first_not_phi{std::ranges::find_if_not(block->Instructions(), IsPhi)};
|
|
|
|
same = IR::Value{&*block->PrependNewInst(first_not_phi, undef_opcode)};
|
|
|
|
}
|
|
|
|
// Reroute all uses of phi to same and remove phi
|
2021-02-05 23:38:22 -06:00
|
|
|
phi.ReplaceUsesWith(same);
|
2021-02-02 18:07:00 -06:00
|
|
|
// TODO: Try to recursively remove all phi users, which might have become trivial
|
|
|
|
return same;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefTable current_def;
|
|
|
|
};
|
2021-02-13 22:24:32 -06:00
|
|
|
|
|
|
|
void VisitInst(Pass& pass, IR::Block* block, IR::Inst& inst) {
|
|
|
|
switch (inst.Opcode()) {
|
|
|
|
case IR::Opcode::SetRegister:
|
|
|
|
if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
|
|
|
|
pass.WriteVariable(reg, block, inst.Arg(1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::Opcode::SetPred:
|
|
|
|
if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
|
|
|
|
pass.WriteVariable(pred, block, inst.Arg(1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::Opcode::SetGotoVariable:
|
|
|
|
pass.WriteVariable(GotoVariable{inst.Arg(0).U32()}, block, inst.Arg(1));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::SetZFlag:
|
|
|
|
pass.WriteVariable(ZeroFlagTag{}, block, inst.Arg(0));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::SetSFlag:
|
|
|
|
pass.WriteVariable(SignFlagTag{}, block, inst.Arg(0));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::SetCFlag:
|
|
|
|
pass.WriteVariable(CarryFlagTag{}, block, inst.Arg(0));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::SetOFlag:
|
|
|
|
pass.WriteVariable(OverflowFlagTag{}, block, inst.Arg(0));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetRegister:
|
|
|
|
if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(reg, block));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetPred:
|
|
|
|
if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(pred, block));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetGotoVariable:
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(GotoVariable{inst.Arg(0).U32()}, block));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetZFlag:
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetSFlag:
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetCFlag:
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block));
|
|
|
|
break;
|
|
|
|
case IR::Opcode::GetOFlag:
|
|
|
|
inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 18:07:00 -06:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
void SsaRewritePass(IR::Function& function) {
|
|
|
|
Pass pass;
|
2021-02-05 20:11:23 -06:00
|
|
|
for (IR::Block* const block : function.blocks) {
|
2021-02-02 18:07:00 -06:00
|
|
|
for (IR::Inst& inst : block->Instructions()) {
|
2021-02-13 22:24:32 -06:00
|
|
|
VisitInst(pass, block, inst);
|
2021-02-02 18:07:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Optimization
|