2021-02-11 14:39:06 -05:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <ranges>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include <boost/intrusive/list.hpp>
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
#include "shader_recompiler/environment.h"
|
2021-02-11 14:39:06 -05:00
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
2021-03-14 01:41:05 -05:00
|
|
|
#include "shader_recompiler/frontend/maxwell/structured_control_flow.h"
|
|
|
|
#include "shader_recompiler/frontend/maxwell/translate/translate.h"
|
2021-02-11 14:39:06 -05:00
|
|
|
#include "shader_recompiler/object_pool.h"
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
namespace Shader::Maxwell {
|
2021-02-11 14:39:06 -05:00
|
|
|
namespace {
|
|
|
|
struct Statement;
|
|
|
|
|
|
|
|
// Use normal_link because we are not guaranteed to destroy the tree in order
|
|
|
|
using ListBaseHook =
|
|
|
|
boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>>;
|
|
|
|
|
|
|
|
using Tree = boost::intrusive::list<Statement,
|
|
|
|
// Allow using Statement without a definition
|
|
|
|
boost::intrusive::base_hook<ListBaseHook>,
|
|
|
|
// Avoid linear complexity on splice, size is never called
|
|
|
|
boost::intrusive::constant_time_size<false>>;
|
|
|
|
using Node = Tree::iterator;
|
|
|
|
using ConstNode = Tree::const_iterator;
|
|
|
|
|
|
|
|
enum class StatementType {
|
|
|
|
Code,
|
|
|
|
Goto,
|
|
|
|
Label,
|
|
|
|
If,
|
|
|
|
Loop,
|
|
|
|
Break,
|
|
|
|
Return,
|
2021-03-19 18:28:31 -04:00
|
|
|
Kill,
|
2021-02-11 14:39:06 -05:00
|
|
|
Function,
|
|
|
|
Identity,
|
|
|
|
Not,
|
|
|
|
Or,
|
|
|
|
SetVariable,
|
|
|
|
Variable,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool HasChildren(StatementType type) {
|
|
|
|
switch (type) {
|
|
|
|
case StatementType::If:
|
|
|
|
case StatementType::Loop:
|
|
|
|
case StatementType::Function:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Goto {};
|
|
|
|
struct Label {};
|
|
|
|
struct If {};
|
|
|
|
struct Loop {};
|
|
|
|
struct Break {};
|
|
|
|
struct Return {};
|
2021-03-19 18:28:31 -04:00
|
|
|
struct Kill {};
|
2021-02-11 14:39:06 -05:00
|
|
|
struct FunctionTag {};
|
|
|
|
struct Identity {};
|
|
|
|
struct Not {};
|
|
|
|
struct Or {};
|
|
|
|
struct SetVariable {};
|
|
|
|
struct Variable {};
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 26495) // Always initialize a member variable, expected in Statement
|
|
|
|
#endif
|
|
|
|
struct Statement : ListBaseHook {
|
2021-03-14 01:41:05 -05:00
|
|
|
Statement(IR::Block* code_, Statement* up_) : code{code_}, up{up_}, type{StatementType::Code} {}
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement(Goto, Statement* cond_, Node label_, Statement* up_)
|
|
|
|
: label{label_}, cond{cond_}, up{up_}, type{StatementType::Goto} {}
|
|
|
|
Statement(Label, u32 id_, Statement* up_) : id{id_}, up{up_}, type{StatementType::Label} {}
|
|
|
|
Statement(If, Statement* cond_, Tree&& children_, Statement* up_)
|
|
|
|
: children{std::move(children_)}, cond{cond_}, up{up_}, type{StatementType::If} {}
|
|
|
|
Statement(Loop, Statement* cond_, Tree&& children_, Statement* up_)
|
|
|
|
: children{std::move(children_)}, cond{cond_}, up{up_}, type{StatementType::Loop} {}
|
|
|
|
Statement(Break, Statement* cond_, Statement* up_)
|
|
|
|
: cond{cond_}, up{up_}, type{StatementType::Break} {}
|
|
|
|
Statement(Return) : type{StatementType::Return} {}
|
2021-03-19 18:28:31 -04:00
|
|
|
Statement(Kill) : type{StatementType::Kill} {}
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement(FunctionTag) : children{}, type{StatementType::Function} {}
|
2021-03-14 01:41:05 -05:00
|
|
|
Statement(Identity, IR::Condition cond_) : guest_cond{cond_}, type{StatementType::Identity} {}
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement(Not, Statement* op_) : op{op_}, type{StatementType::Not} {}
|
|
|
|
Statement(Or, Statement* op_a_, Statement* op_b_)
|
|
|
|
: op_a{op_a_}, op_b{op_b_}, type{StatementType::Or} {}
|
|
|
|
Statement(SetVariable, u32 id_, Statement* op_, Statement* up_)
|
|
|
|
: op{op_}, id{id_}, up{up_}, type{StatementType::SetVariable} {}
|
|
|
|
Statement(Variable, u32 id_) : id{id_}, type{StatementType::Variable} {}
|
|
|
|
|
|
|
|
~Statement() {
|
|
|
|
if (HasChildren(type)) {
|
|
|
|
std::destroy_at(&children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* code;
|
2021-02-11 14:39:06 -05:00
|
|
|
Node label;
|
|
|
|
Tree children;
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Condition guest_cond;
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement* op;
|
|
|
|
Statement* op_a;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
Statement* cond;
|
|
|
|
Statement* op_b;
|
|
|
|
u32 id;
|
|
|
|
};
|
|
|
|
Statement* up{};
|
|
|
|
StatementType type;
|
|
|
|
};
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string DumpExpr(const Statement* stmt) {
|
|
|
|
switch (stmt->type) {
|
|
|
|
case StatementType::Identity:
|
|
|
|
return fmt::format("{}", stmt->guest_cond);
|
|
|
|
case StatementType::Not:
|
|
|
|
return fmt::format("!{}", DumpExpr(stmt->op));
|
|
|
|
case StatementType::Or:
|
|
|
|
return fmt::format("{} || {}", DumpExpr(stmt->op_a), DumpExpr(stmt->op_b));
|
|
|
|
case StatementType::Variable:
|
|
|
|
return fmt::format("goto_L{}", stmt->id);
|
|
|
|
default:
|
|
|
|
return "<invalid type>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DumpTree(const Tree& tree, u32 indentation = 0) {
|
|
|
|
std::string ret;
|
|
|
|
std::string indent(indentation, ' ');
|
|
|
|
for (auto stmt = tree.begin(); stmt != tree.end(); ++stmt) {
|
|
|
|
switch (stmt->type) {
|
|
|
|
case StatementType::Code:
|
2021-03-27 01:54:32 -04:00
|
|
|
ret += fmt::format("{} Block {:04x} -> {:04x} (0x{:016x});\n", indent,
|
|
|
|
stmt->code->LocationBegin(), stmt->code->LocationEnd(),
|
|
|
|
reinterpret_cast<uintptr_t>(stmt->code));
|
2021-02-11 14:39:06 -05:00
|
|
|
break;
|
|
|
|
case StatementType::Goto:
|
|
|
|
ret += fmt::format("{} if ({}) goto L{};\n", indent, DumpExpr(stmt->cond),
|
|
|
|
stmt->label->id);
|
|
|
|
break;
|
|
|
|
case StatementType::Label:
|
|
|
|
ret += fmt::format("{}L{}:\n", indent, stmt->id);
|
|
|
|
break;
|
|
|
|
case StatementType::If:
|
|
|
|
ret += fmt::format("{} if ({}) {{\n", indent, DumpExpr(stmt->cond));
|
|
|
|
ret += DumpTree(stmt->children, indentation + 4);
|
|
|
|
ret += fmt::format("{} }}\n", indent);
|
|
|
|
break;
|
|
|
|
case StatementType::Loop:
|
|
|
|
ret += fmt::format("{} do {{\n", indent);
|
|
|
|
ret += DumpTree(stmt->children, indentation + 4);
|
|
|
|
ret += fmt::format("{} }} while ({});\n", indent, DumpExpr(stmt->cond));
|
|
|
|
break;
|
|
|
|
case StatementType::Break:
|
|
|
|
ret += fmt::format("{} if ({}) break;\n", indent, DumpExpr(stmt->cond));
|
|
|
|
break;
|
|
|
|
case StatementType::Return:
|
|
|
|
ret += fmt::format("{} return;\n", indent);
|
|
|
|
break;
|
2021-03-19 18:28:31 -04:00
|
|
|
case StatementType::Kill:
|
|
|
|
ret += fmt::format("{} kill;\n", indent);
|
|
|
|
break;
|
2021-02-11 14:39:06 -05:00
|
|
|
case StatementType::SetVariable:
|
|
|
|
ret += fmt::format("{} goto_L{} = {};\n", indent, stmt->id, DumpExpr(stmt->op));
|
|
|
|
break;
|
|
|
|
case StatementType::Function:
|
|
|
|
case StatementType::Identity:
|
|
|
|
case StatementType::Not:
|
|
|
|
case StatementType::Or:
|
|
|
|
case StatementType::Variable:
|
|
|
|
throw LogicError("Statement can't be printed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNode(const Tree& tree, ConstNode stmt) {
|
|
|
|
const auto end{tree.end()};
|
|
|
|
for (auto it = tree.begin(); it != end; ++it) {
|
|
|
|
if (it == stmt || (HasChildren(it->type) && HasNode(it->children, stmt))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node FindStatementWithLabel(Tree& tree, ConstNode goto_stmt) {
|
|
|
|
const ConstNode label_stmt{goto_stmt->label};
|
|
|
|
const ConstNode end{tree.end()};
|
|
|
|
for (auto it = tree.begin(); it != end; ++it) {
|
|
|
|
if (it == label_stmt || (HasChildren(it->type) && HasNode(it->children, label_stmt))) {
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw LogicError("Lift label not in tree");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SanitizeNoBreaks(const Tree& tree) {
|
|
|
|
if (std::ranges::find(tree, StatementType::Break, &Statement::type) != tree.end()) {
|
|
|
|
throw NotImplementedException("Capturing statement with break nodes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Level(Node stmt) {
|
|
|
|
size_t level{0};
|
|
|
|
Statement* node{stmt->up};
|
|
|
|
while (node) {
|
|
|
|
++level;
|
|
|
|
node = node->up;
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDirectlyRelated(Node goto_stmt, Node label_stmt) {
|
|
|
|
const size_t goto_level{Level(goto_stmt)};
|
|
|
|
const size_t label_level{Level(label_stmt)};
|
|
|
|
size_t min_level;
|
|
|
|
size_t max_level;
|
|
|
|
Node min;
|
|
|
|
Node max;
|
|
|
|
if (label_level < goto_level) {
|
|
|
|
min_level = label_level;
|
|
|
|
max_level = goto_level;
|
|
|
|
min = label_stmt;
|
|
|
|
max = goto_stmt;
|
|
|
|
} else { // goto_level < label_level
|
|
|
|
min_level = goto_level;
|
|
|
|
max_level = label_level;
|
|
|
|
min = goto_stmt;
|
|
|
|
max = label_stmt;
|
|
|
|
}
|
|
|
|
while (max_level > min_level) {
|
|
|
|
--max_level;
|
|
|
|
max = max->up;
|
|
|
|
}
|
|
|
|
return min->up == max->up;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsIndirectlyRelated(Node goto_stmt, Node label_stmt) {
|
|
|
|
return goto_stmt->up != label_stmt->up && !IsDirectlyRelated(goto_stmt, label_stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchNode(const Tree& tree, ConstNode stmt, size_t& offset) {
|
|
|
|
++offset;
|
|
|
|
|
|
|
|
const auto end = tree.end();
|
|
|
|
for (ConstNode it = tree.begin(); it != end; ++it) {
|
|
|
|
++offset;
|
|
|
|
if (stmt == it) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (HasChildren(it->type) && SearchNode(it->children, stmt, offset)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class GotoPass {
|
|
|
|
public:
|
2021-03-14 01:41:05 -05:00
|
|
|
explicit GotoPass(Flow::CFG& cfg, ObjectPool<IR::Inst>& inst_pool_,
|
|
|
|
ObjectPool<IR::Block>& block_pool_, ObjectPool<Statement>& stmt_pool)
|
|
|
|
: inst_pool{inst_pool_}, block_pool{block_pool_}, pool{stmt_pool} {
|
|
|
|
std::vector gotos{BuildTree(cfg)};
|
2021-02-11 14:39:06 -05:00
|
|
|
for (const Node& goto_stmt : gotos | std::views::reverse) {
|
|
|
|
RemoveGoto(goto_stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Statement& RootStatement() noexcept {
|
|
|
|
return root_stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RemoveGoto(Node goto_stmt) {
|
|
|
|
// Force goto_stmt and label_stmt to be directly related
|
|
|
|
const Node label_stmt{goto_stmt->label};
|
|
|
|
if (IsIndirectlyRelated(goto_stmt, label_stmt)) {
|
|
|
|
// Move goto_stmt out using outward-movement transformation until it becomes
|
|
|
|
// directly related to label_stmt
|
|
|
|
while (!IsDirectlyRelated(goto_stmt, label_stmt)) {
|
|
|
|
goto_stmt = MoveOutward(goto_stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Force goto_stmt and label_stmt to be siblings
|
|
|
|
if (IsDirectlyRelated(goto_stmt, label_stmt)) {
|
|
|
|
const size_t label_level{Level(label_stmt)};
|
|
|
|
size_t goto_level{Level(goto_stmt)};
|
|
|
|
if (goto_level > label_level) {
|
|
|
|
// Move goto_stmt out of its level using outward-movement transformations
|
|
|
|
while (goto_level > label_level) {
|
|
|
|
goto_stmt = MoveOutward(goto_stmt);
|
|
|
|
--goto_level;
|
|
|
|
}
|
|
|
|
} else { // Level(goto_stmt) < Level(label_stmt)
|
|
|
|
if (Offset(goto_stmt) > Offset(label_stmt)) {
|
|
|
|
// Lift goto_stmt to above stmt containing label_stmt using goto-lifting
|
|
|
|
// transformations
|
|
|
|
goto_stmt = Lift(goto_stmt);
|
|
|
|
}
|
|
|
|
// Move goto_stmt into label_stmt's level using inward-movement transformation
|
|
|
|
while (goto_level < label_level) {
|
|
|
|
goto_stmt = MoveInward(goto_stmt);
|
|
|
|
++goto_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Remove this
|
2021-03-14 01:41:05 -05:00
|
|
|
{
|
|
|
|
Node it{goto_stmt};
|
|
|
|
bool sibling{false};
|
|
|
|
do {
|
|
|
|
sibling |= it == label_stmt;
|
|
|
|
--it;
|
|
|
|
} while (it != goto_stmt->up->children.begin());
|
|
|
|
while (it != goto_stmt->up->children.end()) {
|
|
|
|
sibling |= it == label_stmt;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
if (!sibling) {
|
|
|
|
throw LogicError("Not siblings");
|
|
|
|
}
|
2021-02-11 14:39:06 -05:00
|
|
|
}
|
|
|
|
// goto_stmt and label_stmt are guaranteed to be siblings, eliminate
|
|
|
|
if (std::next(goto_stmt) == label_stmt) {
|
|
|
|
// Simply eliminate the goto if the label is next to it
|
|
|
|
goto_stmt->up->children.erase(goto_stmt);
|
|
|
|
} else if (Offset(goto_stmt) < Offset(label_stmt)) {
|
|
|
|
// Eliminate goto_stmt with a conditional
|
|
|
|
EliminateAsConditional(goto_stmt, label_stmt);
|
|
|
|
} else {
|
|
|
|
// Eliminate goto_stmt with a loop
|
|
|
|
EliminateAsLoop(goto_stmt, label_stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
std::vector<Node> BuildTree(Flow::CFG& cfg) {
|
|
|
|
u32 label_id{0};
|
2021-02-11 14:39:06 -05:00
|
|
|
std::vector<Node> gotos;
|
2021-03-14 01:41:05 -05:00
|
|
|
Flow::Function& first_function{cfg.Functions().front()};
|
|
|
|
BuildTree(cfg, first_function, label_id, gotos, root_stmt.children.end(), std::nullopt);
|
2021-02-11 14:39:06 -05:00
|
|
|
return gotos;
|
|
|
|
}
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
void BuildTree(Flow::CFG& cfg, Flow::Function& function, u32& label_id,
|
|
|
|
std::vector<Node>& gotos, Node function_insert_point,
|
|
|
|
std::optional<Node> return_label) {
|
|
|
|
Statement* const false_stmt{pool.Create(Identity{}, IR::Condition{false})};
|
2021-02-11 14:39:06 -05:00
|
|
|
Tree& root{root_stmt.children};
|
2021-03-14 01:41:05 -05:00
|
|
|
std::unordered_map<Flow::Block*, Node> local_labels;
|
|
|
|
local_labels.reserve(function.blocks.size());
|
|
|
|
|
|
|
|
for (Flow::Block& block : function.blocks) {
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement* const label{pool.Create(Label{}, label_id, &root_stmt)};
|
2021-03-14 01:41:05 -05:00
|
|
|
const Node label_it{root.insert(function_insert_point, *label)};
|
|
|
|
local_labels.emplace(&block, label_it);
|
2021-02-11 14:39:06 -05:00
|
|
|
++label_id;
|
|
|
|
}
|
2021-03-14 01:41:05 -05:00
|
|
|
for (Flow::Block& block : function.blocks) {
|
|
|
|
const Node label{local_labels.at(&block)};
|
|
|
|
// Insertion point
|
|
|
|
const Node ip{std::next(label)};
|
|
|
|
|
|
|
|
// Reset goto variables before the first block and after its respective label
|
|
|
|
const auto make_reset_variable{[&]() -> Statement& {
|
|
|
|
return *pool.Create(SetVariable{}, label->id, false_stmt, &root_stmt);
|
|
|
|
}};
|
|
|
|
root.push_front(make_reset_variable());
|
|
|
|
root.insert(ip, make_reset_variable());
|
|
|
|
|
|
|
|
const u32 begin_offset{block.begin.Offset()};
|
|
|
|
const u32 end_offset{block.end.Offset()};
|
|
|
|
IR::Block* const ir_block{block_pool.Create(inst_pool, begin_offset, end_offset)};
|
|
|
|
root.insert(ip, *pool.Create(ir_block, &root_stmt));
|
|
|
|
|
|
|
|
switch (block.end_class) {
|
|
|
|
case Flow::EndClass::Branch: {
|
|
|
|
Statement* const always_cond{pool.Create(Identity{}, IR::Condition{true})};
|
|
|
|
if (block.cond == IR::Condition{true}) {
|
|
|
|
const Node true_label{local_labels.at(block.branch_true)};
|
|
|
|
gotos.push_back(
|
|
|
|
root.insert(ip, *pool.Create(Goto{}, always_cond, true_label, &root_stmt)));
|
|
|
|
} else if (block.cond == IR::Condition{false}) {
|
|
|
|
const Node false_label{local_labels.at(block.branch_false)};
|
|
|
|
gotos.push_back(root.insert(
|
|
|
|
ip, *pool.Create(Goto{}, always_cond, false_label, &root_stmt)));
|
|
|
|
} else {
|
|
|
|
const Node true_label{local_labels.at(block.branch_true)};
|
|
|
|
const Node false_label{local_labels.at(block.branch_false)};
|
|
|
|
Statement* const true_cond{pool.Create(Identity{}, block.cond)};
|
|
|
|
gotos.push_back(
|
|
|
|
root.insert(ip, *pool.Create(Goto{}, true_cond, true_label, &root_stmt)));
|
|
|
|
gotos.push_back(root.insert(
|
|
|
|
ip, *pool.Create(Goto{}, always_cond, false_label, &root_stmt)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Flow::EndClass::Call: {
|
|
|
|
Flow::Function& call{cfg.Functions()[block.function_call]};
|
|
|
|
const Node call_return_label{local_labels.at(block.return_block)};
|
|
|
|
BuildTree(cfg, call, label_id, gotos, ip, call_return_label);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Flow::EndClass::Exit:
|
|
|
|
root.insert(ip, *pool.Create(Return{}));
|
|
|
|
break;
|
|
|
|
case Flow::EndClass::Return: {
|
|
|
|
Statement* const always_cond{pool.Create(Identity{}, block.cond)};
|
|
|
|
auto goto_stmt{pool.Create(Goto{}, always_cond, return_label.value(), &root_stmt)};
|
|
|
|
gotos.push_back(root.insert(ip, *goto_stmt));
|
|
|
|
break;
|
|
|
|
}
|
2021-03-19 18:28:31 -04:00
|
|
|
case Flow::EndClass::Kill:
|
|
|
|
root.insert(ip, *pool.Create(Kill{}));
|
|
|
|
break;
|
2021-03-14 01:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 14:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateTreeUp(Statement* tree) {
|
|
|
|
for (Statement& stmt : tree->children) {
|
|
|
|
stmt.up = tree;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EliminateAsConditional(Node goto_stmt, Node label_stmt) {
|
|
|
|
Tree& body{goto_stmt->up->children};
|
|
|
|
Tree if_body;
|
|
|
|
if_body.splice(if_body.begin(), body, std::next(goto_stmt), label_stmt);
|
|
|
|
Statement* const cond{pool.Create(Not{}, goto_stmt->cond)};
|
|
|
|
Statement* const if_stmt{pool.Create(If{}, cond, std::move(if_body), goto_stmt->up)};
|
|
|
|
UpdateTreeUp(if_stmt);
|
|
|
|
body.insert(goto_stmt, *if_stmt);
|
|
|
|
body.erase(goto_stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EliminateAsLoop(Node goto_stmt, Node label_stmt) {
|
|
|
|
Tree& body{goto_stmt->up->children};
|
|
|
|
Tree loop_body;
|
|
|
|
loop_body.splice(loop_body.begin(), body, label_stmt, goto_stmt);
|
|
|
|
Statement* const cond{goto_stmt->cond};
|
|
|
|
Statement* const loop{pool.Create(Loop{}, cond, std::move(loop_body), goto_stmt->up)};
|
|
|
|
UpdateTreeUp(loop);
|
|
|
|
body.insert(goto_stmt, *loop);
|
|
|
|
body.erase(goto_stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Node MoveOutward(Node goto_stmt) {
|
|
|
|
switch (goto_stmt->up->type) {
|
|
|
|
case StatementType::If:
|
|
|
|
return MoveOutwardIf(goto_stmt);
|
|
|
|
case StatementType::Loop:
|
|
|
|
return MoveOutwardLoop(goto_stmt);
|
|
|
|
default:
|
|
|
|
throw LogicError("Invalid outward movement");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Node MoveInward(Node goto_stmt) {
|
|
|
|
Statement* const parent{goto_stmt->up};
|
|
|
|
Tree& body{parent->children};
|
|
|
|
const Node label_nested_stmt{FindStatementWithLabel(body, goto_stmt)};
|
|
|
|
const Node label{goto_stmt->label};
|
|
|
|
const u32 label_id{label->id};
|
|
|
|
|
|
|
|
Statement* const goto_cond{goto_stmt->cond};
|
|
|
|
Statement* const set_var{pool.Create(SetVariable{}, label_id, goto_cond, parent)};
|
|
|
|
body.insert(goto_stmt, *set_var);
|
|
|
|
|
|
|
|
Tree if_body;
|
|
|
|
if_body.splice(if_body.begin(), body, std::next(goto_stmt), label_nested_stmt);
|
|
|
|
Statement* const variable{pool.Create(Variable{}, label_id)};
|
|
|
|
Statement* const neg_var{pool.Create(Not{}, variable)};
|
|
|
|
if (!if_body.empty()) {
|
|
|
|
Statement* const if_stmt{pool.Create(If{}, neg_var, std::move(if_body), parent)};
|
|
|
|
UpdateTreeUp(if_stmt);
|
|
|
|
body.insert(goto_stmt, *if_stmt);
|
|
|
|
}
|
|
|
|
body.erase(goto_stmt);
|
|
|
|
|
|
|
|
switch (label_nested_stmt->type) {
|
|
|
|
case StatementType::If:
|
2021-02-24 03:21:30 -05:00
|
|
|
// Update nested if condition
|
|
|
|
label_nested_stmt->cond = pool.Create(Or{}, variable, label_nested_stmt->cond);
|
2021-02-11 14:39:06 -05:00
|
|
|
break;
|
|
|
|
case StatementType::Loop:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw LogicError("Invalid inward movement");
|
|
|
|
}
|
|
|
|
Tree& nested_tree{label_nested_stmt->children};
|
|
|
|
Statement* const new_goto{pool.Create(Goto{}, variable, label, &*label_nested_stmt)};
|
|
|
|
return nested_tree.insert(nested_tree.begin(), *new_goto);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Node Lift(Node goto_stmt) {
|
|
|
|
Statement* const parent{goto_stmt->up};
|
|
|
|
Tree& body{parent->children};
|
|
|
|
const Node label{goto_stmt->label};
|
|
|
|
const u32 label_id{label->id};
|
|
|
|
const Node label_nested_stmt{FindStatementWithLabel(body, goto_stmt)};
|
|
|
|
const auto type{label_nested_stmt->type};
|
|
|
|
|
|
|
|
Tree loop_body;
|
|
|
|
loop_body.splice(loop_body.begin(), body, label_nested_stmt, goto_stmt);
|
|
|
|
SanitizeNoBreaks(loop_body);
|
|
|
|
Statement* const variable{pool.Create(Variable{}, label_id)};
|
|
|
|
Statement* const loop_stmt{pool.Create(Loop{}, variable, std::move(loop_body), parent)};
|
|
|
|
UpdateTreeUp(loop_stmt);
|
|
|
|
const Node loop_node{body.insert(goto_stmt, *loop_stmt)};
|
|
|
|
|
|
|
|
Statement* const new_goto{pool.Create(Goto{}, variable, label, loop_stmt)};
|
|
|
|
loop_stmt->children.push_front(*new_goto);
|
|
|
|
const Node new_goto_node{loop_stmt->children.begin()};
|
|
|
|
|
|
|
|
Statement* const set_var{pool.Create(SetVariable{}, label_id, goto_stmt->cond, loop_stmt)};
|
|
|
|
loop_stmt->children.push_back(*set_var);
|
|
|
|
|
|
|
|
body.erase(goto_stmt);
|
|
|
|
return new_goto_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node MoveOutwardIf(Node goto_stmt) {
|
|
|
|
const Node parent{Tree::s_iterator_to(*goto_stmt->up)};
|
|
|
|
Tree& body{parent->children};
|
|
|
|
const u32 label_id{goto_stmt->label->id};
|
|
|
|
Statement* const goto_cond{goto_stmt->cond};
|
|
|
|
Statement* const set_goto_var{pool.Create(SetVariable{}, label_id, goto_cond, &*parent)};
|
|
|
|
body.insert(goto_stmt, *set_goto_var);
|
|
|
|
|
|
|
|
Tree if_body;
|
|
|
|
if_body.splice(if_body.begin(), body, std::next(goto_stmt), body.end());
|
|
|
|
if_body.pop_front();
|
|
|
|
Statement* const cond{pool.Create(Variable{}, label_id)};
|
|
|
|
Statement* const neg_cond{pool.Create(Not{}, cond)};
|
|
|
|
Statement* const if_stmt{pool.Create(If{}, neg_cond, std::move(if_body), &*parent)};
|
|
|
|
UpdateTreeUp(if_stmt);
|
|
|
|
body.insert(goto_stmt, *if_stmt);
|
|
|
|
|
|
|
|
body.erase(goto_stmt);
|
|
|
|
|
|
|
|
Statement* const new_cond{pool.Create(Variable{}, label_id)};
|
|
|
|
Statement* const new_goto{pool.Create(Goto{}, new_cond, goto_stmt->label, parent->up)};
|
|
|
|
Tree& parent_tree{parent->up->children};
|
|
|
|
return parent_tree.insert(std::next(parent), *new_goto);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node MoveOutwardLoop(Node goto_stmt) {
|
|
|
|
Statement* const parent{goto_stmt->up};
|
|
|
|
Tree& body{parent->children};
|
|
|
|
const u32 label_id{goto_stmt->label->id};
|
|
|
|
Statement* const goto_cond{goto_stmt->cond};
|
|
|
|
Statement* const set_goto_var{pool.Create(SetVariable{}, label_id, goto_cond, parent)};
|
|
|
|
Statement* const cond{pool.Create(Variable{}, label_id)};
|
|
|
|
Statement* const break_stmt{pool.Create(Break{}, cond, parent)};
|
|
|
|
body.insert(goto_stmt, *set_goto_var);
|
|
|
|
body.insert(goto_stmt, *break_stmt);
|
|
|
|
body.erase(goto_stmt);
|
|
|
|
|
|
|
|
const Node loop{Tree::s_iterator_to(*goto_stmt->up)};
|
|
|
|
Statement* const new_goto_cond{pool.Create(Variable{}, label_id)};
|
|
|
|
Statement* const new_goto{pool.Create(Goto{}, new_goto_cond, goto_stmt->label, loop->up)};
|
|
|
|
Tree& parent_tree{loop->up->children};
|
|
|
|
return parent_tree.insert(std::next(loop), *new_goto);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Offset(ConstNode stmt) const {
|
|
|
|
size_t offset{0};
|
|
|
|
if (!SearchNode(root_stmt.children, stmt, offset)) {
|
|
|
|
throw LogicError("Node not found in tree");
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
ObjectPool<IR::Inst>& inst_pool;
|
|
|
|
ObjectPool<IR::Block>& block_pool;
|
2021-02-14 22:09:11 -05:00
|
|
|
ObjectPool<Statement>& pool;
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement root_stmt{FunctionTag{}};
|
|
|
|
};
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* TryFindForwardBlock(const Statement& stmt) {
|
2021-02-11 14:39:06 -05:00
|
|
|
const Tree& tree{stmt.up->children};
|
|
|
|
const ConstNode end{tree.cend()};
|
|
|
|
ConstNode forward_node{std::next(Tree::s_iterator_to(stmt))};
|
|
|
|
while (forward_node != end && !HasChildren(forward_node->type)) {
|
|
|
|
if (forward_node->type == StatementType::Code) {
|
|
|
|
return forward_node->code;
|
|
|
|
}
|
|
|
|
++forward_node;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
[[nodiscard]] IR::U1 VisitExpr(IR::IREmitter& ir, const Statement& stmt) {
|
2021-02-11 14:39:06 -05:00
|
|
|
switch (stmt.type) {
|
|
|
|
case StatementType::Identity:
|
|
|
|
return ir.Condition(stmt.guest_cond);
|
|
|
|
case StatementType::Not:
|
2021-03-14 01:41:05 -05:00
|
|
|
return ir.LogicalNot(IR::U1{VisitExpr(ir, *stmt.op)});
|
2021-02-11 14:39:06 -05:00
|
|
|
case StatementType::Or:
|
|
|
|
return ir.LogicalOr(VisitExpr(ir, *stmt.op_a), VisitExpr(ir, *stmt.op_b));
|
|
|
|
case StatementType::Variable:
|
|
|
|
return ir.GetGotoVariable(stmt.id);
|
|
|
|
default:
|
|
|
|
throw NotImplementedException("Statement type {}", stmt.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TranslatePass {
|
|
|
|
public:
|
2021-03-14 01:41:05 -05:00
|
|
|
TranslatePass(ObjectPool<IR::Inst>& inst_pool_, ObjectPool<IR::Block>& block_pool_,
|
|
|
|
ObjectPool<Statement>& stmt_pool_, Environment& env_, Statement& root_stmt,
|
|
|
|
IR::BlockList& block_list_)
|
|
|
|
: stmt_pool{stmt_pool_}, inst_pool{inst_pool_}, block_pool{block_pool_}, env{env_},
|
2021-02-11 14:39:06 -05:00
|
|
|
block_list{block_list_} {
|
|
|
|
Visit(root_stmt, nullptr, nullptr);
|
2021-03-24 00:33:45 -04:00
|
|
|
|
2021-03-24 18:37:44 -04:00
|
|
|
IR::Block& first_block{*block_list.front()};
|
|
|
|
IR::IREmitter ir{first_block, first_block.begin()};
|
2021-03-24 00:33:45 -04:00
|
|
|
ir.Prologue();
|
2021-02-11 14:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-03-14 01:41:05 -05:00
|
|
|
void Visit(Statement& parent, IR::Block* continue_block, IR::Block* break_block) {
|
2021-02-11 14:39:06 -05:00
|
|
|
Tree& tree{parent.children};
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* current_block{nullptr};
|
2021-02-11 14:39:06 -05:00
|
|
|
|
|
|
|
for (auto it = tree.begin(); it != tree.end(); ++it) {
|
|
|
|
Statement& stmt{*it};
|
|
|
|
switch (stmt.type) {
|
|
|
|
case StatementType::Label:
|
|
|
|
// Labels can be ignored
|
|
|
|
break;
|
|
|
|
case StatementType::Code: {
|
|
|
|
if (current_block && current_block != stmt.code) {
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::IREmitter{*current_block}.Branch(stmt.code);
|
2021-02-11 14:39:06 -05:00
|
|
|
}
|
|
|
|
current_block = stmt.code;
|
2021-03-14 01:41:05 -05:00
|
|
|
Translate(env, stmt.code);
|
2021-02-11 14:39:06 -05:00
|
|
|
block_list.push_back(stmt.code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StatementType::SetVariable: {
|
|
|
|
if (!current_block) {
|
|
|
|
current_block = MergeBlock(parent, stmt);
|
|
|
|
}
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::IREmitter ir{*current_block};
|
2021-02-11 14:39:06 -05:00
|
|
|
ir.SetGotoVariable(stmt.id, VisitExpr(ir, *stmt.op));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StatementType::If: {
|
|
|
|
if (!current_block) {
|
|
|
|
current_block = block_pool.Create(inst_pool);
|
|
|
|
block_list.push_back(current_block);
|
|
|
|
}
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const merge_block{MergeBlock(parent, stmt)};
|
2021-02-11 14:39:06 -05:00
|
|
|
|
|
|
|
// Visit children
|
|
|
|
const size_t first_block_index{block_list.size()};
|
|
|
|
Visit(stmt, merge_block, break_block);
|
|
|
|
|
|
|
|
// Implement if header block
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const first_if_block{block_list.at(first_block_index)};
|
|
|
|
IR::IREmitter ir{*current_block};
|
|
|
|
const IR::U1 cond{VisitExpr(ir, *stmt.cond)};
|
2021-02-11 14:39:06 -05:00
|
|
|
ir.SelectionMerge(merge_block);
|
|
|
|
ir.BranchConditional(cond, first_if_block, merge_block);
|
|
|
|
|
|
|
|
current_block = merge_block;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StatementType::Loop: {
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const loop_header_block{block_pool.Create(inst_pool)};
|
2021-02-11 14:39:06 -05:00
|
|
|
if (current_block) {
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::IREmitter{*current_block}.Branch(loop_header_block);
|
2021-02-11 14:39:06 -05:00
|
|
|
}
|
|
|
|
block_list.push_back(loop_header_block);
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const new_continue_block{block_pool.Create(inst_pool)};
|
|
|
|
IR::Block* const merge_block{MergeBlock(parent, stmt)};
|
2021-02-11 14:39:06 -05:00
|
|
|
|
|
|
|
// Visit children
|
|
|
|
const size_t first_block_index{block_list.size()};
|
|
|
|
Visit(stmt, new_continue_block, merge_block);
|
|
|
|
|
|
|
|
// The continue block is located at the end of the loop
|
|
|
|
block_list.push_back(new_continue_block);
|
|
|
|
|
|
|
|
// Implement loop header block
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const first_loop_block{block_list.at(first_block_index)};
|
|
|
|
IR::IREmitter ir{*loop_header_block};
|
2021-02-11 14:39:06 -05:00
|
|
|
ir.LoopMerge(merge_block, new_continue_block);
|
|
|
|
ir.Branch(first_loop_block);
|
|
|
|
|
|
|
|
// Implement continue block
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::IREmitter continue_ir{*new_continue_block};
|
|
|
|
const IR::U1 continue_cond{VisitExpr(continue_ir, *stmt.cond)};
|
2021-02-11 14:39:06 -05:00
|
|
|
continue_ir.BranchConditional(continue_cond, ir.block, merge_block);
|
|
|
|
|
|
|
|
current_block = merge_block;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StatementType::Break: {
|
|
|
|
if (!current_block) {
|
|
|
|
current_block = block_pool.Create(inst_pool);
|
|
|
|
block_list.push_back(current_block);
|
|
|
|
}
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const skip_block{MergeBlock(parent, stmt)};
|
2021-02-11 14:39:06 -05:00
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::IREmitter ir{*current_block};
|
2021-02-11 14:39:06 -05:00
|
|
|
ir.BranchConditional(VisitExpr(ir, *stmt.cond), break_block, skip_block);
|
|
|
|
|
|
|
|
current_block = skip_block;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StatementType::Return: {
|
|
|
|
if (!current_block) {
|
|
|
|
current_block = block_pool.Create(inst_pool);
|
|
|
|
block_list.push_back(current_block);
|
|
|
|
}
|
2021-03-24 00:33:45 -04:00
|
|
|
IR::IREmitter ir{*current_block};
|
|
|
|
ir.Epilogue();
|
|
|
|
ir.Return();
|
2021-02-11 14:39:06 -05:00
|
|
|
current_block = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-19 18:28:31 -04:00
|
|
|
case StatementType::Kill: {
|
|
|
|
if (!current_block) {
|
|
|
|
current_block = block_pool.Create(inst_pool);
|
|
|
|
block_list.push_back(current_block);
|
|
|
|
}
|
2021-03-27 01:54:32 -04:00
|
|
|
IR::Block* demote_block{MergeBlock(parent, stmt)};
|
|
|
|
IR::IREmitter{*current_block}.DemoteToHelperInvocation(demote_block);
|
|
|
|
current_block = demote_block;
|
2021-03-19 18:28:31 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-02-11 14:39:06 -05:00
|
|
|
default:
|
|
|
|
throw NotImplementedException("Statement type {}", stmt.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current_block && continue_block) {
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::IREmitter{*current_block}.Branch(continue_block);
|
2021-02-11 14:39:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* MergeBlock(Statement& parent, Statement& stmt) {
|
|
|
|
if (IR::Block* const block{TryFindForwardBlock(stmt)}) {
|
2021-02-11 14:39:06 -05:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
// Create a merge block we can visit later
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::Block* const block{block_pool.Create(inst_pool)};
|
2021-02-11 14:39:06 -05:00
|
|
|
Statement* const merge_stmt{stmt_pool.Create(block, &parent)};
|
|
|
|
parent.children.insert(std::next(Tree::s_iterator_to(stmt)), *merge_stmt);
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2021-02-14 22:09:11 -05:00
|
|
|
ObjectPool<Statement>& stmt_pool;
|
2021-03-14 01:41:05 -05:00
|
|
|
ObjectPool<IR::Inst>& inst_pool;
|
|
|
|
ObjectPool<IR::Block>& block_pool;
|
|
|
|
Environment& env;
|
|
|
|
IR::BlockList& block_list;
|
2021-02-11 14:39:06 -05:00
|
|
|
};
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
IR::BlockList VisitAST(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
|
|
|
|
Environment& env, Flow::CFG& cfg) {
|
2021-02-14 22:09:11 -05:00
|
|
|
ObjectPool<Statement> stmt_pool{64};
|
2021-03-14 01:41:05 -05:00
|
|
|
GotoPass goto_pass{cfg, inst_pool, block_pool, stmt_pool};
|
|
|
|
Statement& root{goto_pass.RootStatement()};
|
|
|
|
IR::BlockList block_list;
|
|
|
|
TranslatePass{inst_pool, block_pool, stmt_pool, env, root, block_list};
|
2021-02-11 14:39:06 -05:00
|
|
|
return block_list;
|
|
|
|
}
|
|
|
|
|
2021-03-14 01:41:05 -05:00
|
|
|
} // namespace Shader::Maxwell
|