2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2022-11-21 16:31:18 +00:00
|
|
|
#include "common/polyfill_ranges.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "shader_recompiler/exception.h"
|
|
|
|
#include "shader_recompiler/frontend/maxwell/control_flow.h"
|
|
|
|
#include "shader_recompiler/frontend/maxwell/decode.h"
|
2021-03-27 21:30:24 +00:00
|
|
|
#include "shader_recompiler/frontend/maxwell/indirect_branch_table_track.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "shader_recompiler/frontend/maxwell/location.h"
|
|
|
|
|
|
|
|
namespace Shader::Maxwell::Flow {
|
2021-02-11 19:39:06 +00:00
|
|
|
namespace {
|
|
|
|
struct Compare {
|
|
|
|
bool operator()(const Block& lhs, Location rhs) const noexcept {
|
|
|
|
return lhs.begin < rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(Location lhs, const Block& rhs) const noexcept {
|
|
|
|
return lhs < rhs.begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const Block& lhs, const Block& rhs) const noexcept {
|
|
|
|
return lhs.begin < rhs.begin;
|
|
|
|
}
|
|
|
|
};
|
2021-01-09 06:30:07 +00:00
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
u32 BranchOffset(Location pc, Instruction inst) {
|
2021-04-06 02:25:22 +00:00
|
|
|
return pc.Offset() + static_cast<u32>(inst.branch.Offset()) + 8u;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
void Split(Block* old_block, Block* new_block, Location pc) {
|
2021-02-11 19:39:06 +00:00
|
|
|
if (pc <= old_block->begin || pc >= old_block->end) {
|
2021-01-09 06:30:07 +00:00
|
|
|
throw InvalidArgument("Invalid address to split={}", pc);
|
|
|
|
}
|
2021-04-06 02:25:22 +00:00
|
|
|
*new_block = Block{};
|
|
|
|
new_block->begin = pc;
|
|
|
|
new_block->end = old_block->end;
|
2021-04-10 06:32:55 +00:00
|
|
|
new_block->end_class = old_block->end_class;
|
2021-04-06 02:25:22 +00:00
|
|
|
new_block->cond = old_block->cond;
|
|
|
|
new_block->stack = old_block->stack;
|
|
|
|
new_block->branch_true = old_block->branch_true;
|
|
|
|
new_block->branch_false = old_block->branch_false;
|
|
|
|
new_block->function_call = old_block->function_call;
|
|
|
|
new_block->return_block = old_block->return_block;
|
|
|
|
new_block->branch_reg = old_block->branch_reg;
|
|
|
|
new_block->branch_offset = old_block->branch_offset;
|
|
|
|
new_block->indirect_branches = std::move(old_block->indirect_branches);
|
|
|
|
|
|
|
|
const Location old_begin{old_block->begin};
|
|
|
|
Stack old_stack{std::move(old_block->stack)};
|
|
|
|
*old_block = Block{};
|
|
|
|
old_block->begin = old_begin;
|
|
|
|
old_block->end = pc;
|
|
|
|
old_block->end_class = EndClass::Branch;
|
|
|
|
old_block->cond = IR::Condition(true);
|
|
|
|
old_block->stack = old_stack;
|
|
|
|
old_block->branch_true = new_block;
|
|
|
|
old_block->branch_false = nullptr;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
Token OpcodeToken(Opcode opcode) {
|
2021-01-09 06:30:07 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case Opcode::PBK:
|
|
|
|
case Opcode::BRK:
|
|
|
|
return Token::PBK;
|
|
|
|
case Opcode::PCNT:
|
|
|
|
case Opcode::CONT:
|
2021-07-26 08:16:32 +00:00
|
|
|
return Token::PCNT;
|
2021-01-09 06:30:07 +00:00
|
|
|
case Opcode::PEXIT:
|
|
|
|
case Opcode::EXIT:
|
|
|
|
return Token::PEXIT;
|
|
|
|
case Opcode::PLONGJMP:
|
|
|
|
case Opcode::LONGJMP:
|
|
|
|
return Token::PLONGJMP;
|
|
|
|
case Opcode::PRET:
|
|
|
|
case Opcode::RET:
|
|
|
|
case Opcode::CAL:
|
|
|
|
return Token::PRET;
|
|
|
|
case Opcode::SSY:
|
|
|
|
case Opcode::SYNC:
|
|
|
|
return Token::SSY;
|
|
|
|
default:
|
|
|
|
throw InvalidArgument("{}", opcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
bool IsAbsoluteJump(Opcode opcode) {
|
2021-01-09 06:30:07 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case Opcode::JCAL:
|
|
|
|
case Opcode::JMP:
|
|
|
|
case Opcode::JMX:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
bool HasFlowTest(Opcode opcode) {
|
2021-01-09 06:30:07 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case Opcode::BRA:
|
|
|
|
case Opcode::BRX:
|
|
|
|
case Opcode::EXIT:
|
|
|
|
case Opcode::JMP:
|
|
|
|
case Opcode::JMX:
|
2021-03-19 22:28:31 +00:00
|
|
|
case Opcode::KIL:
|
2021-01-09 06:30:07 +00:00
|
|
|
case Opcode::BRK:
|
|
|
|
case Opcode::CONT:
|
|
|
|
case Opcode::LONGJMP:
|
|
|
|
case Opcode::RET:
|
|
|
|
case Opcode::SYNC:
|
|
|
|
return true;
|
|
|
|
case Opcode::CAL:
|
|
|
|
case Opcode::JCAL:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
throw InvalidArgument("Invalid branch {}", opcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
std::string NameOf(const Block& block) {
|
2021-01-09 06:30:07 +00:00
|
|
|
if (block.begin.IsVirtual()) {
|
2021-02-11 19:39:06 +00:00
|
|
|
return fmt::format("\"Virtual {}\"", block.begin);
|
2021-01-09 06:30:07 +00:00
|
|
|
} else {
|
|
|
|
return fmt::format("\"{}\"", block.begin);
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 06:41:05 +00:00
|
|
|
} // Anonymous namespace
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
void Stack::Push(Token token, Location target) {
|
|
|
|
entries.push_back({
|
2021-04-06 02:25:22 +00:00
|
|
|
.token = token,
|
2021-01-09 06:30:07 +00:00
|
|
|
.target{target},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Location, Stack> Stack::Pop(Token token) const {
|
|
|
|
const std::optional<Location> pc{Peek(token)};
|
|
|
|
if (!pc) {
|
|
|
|
throw LogicError("Token could not be found");
|
|
|
|
}
|
|
|
|
return {*pc, Remove(token)};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Location> Stack::Peek(Token token) const {
|
2021-07-12 08:22:01 +00:00
|
|
|
const auto it{std::find_if(entries.rbegin(), entries.rend(),
|
|
|
|
[token](const auto& entry) { return entry.token == token; })};
|
|
|
|
if (it == entries.rend()) {
|
2021-01-09 06:30:07 +00:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
return it->target;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stack Stack::Remove(Token token) const {
|
2021-07-12 08:22:01 +00:00
|
|
|
const auto it{std::find_if(entries.rbegin(), entries.rend(),
|
|
|
|
[token](const auto& entry) { return entry.token == token; })};
|
|
|
|
const auto pos{std::distance(entries.rbegin(), it)};
|
2021-01-09 06:30:07 +00:00
|
|
|
Stack result;
|
|
|
|
result.entries.insert(result.entries.end(), entries.begin(), entries.end() - pos - 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Block::Contains(Location pc) const noexcept {
|
|
|
|
return pc >= begin && pc < end;
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
Function::Function(ObjectPool<Block>& block_pool, Location start_address)
|
2021-04-06 02:25:22 +00:00
|
|
|
: entrypoint{start_address} {
|
|
|
|
Label& label{labels.emplace_back()};
|
|
|
|
label.address = start_address;
|
|
|
|
label.block = block_pool.Create(Block{});
|
|
|
|
label.block->begin = start_address;
|
|
|
|
label.block->end = start_address;
|
|
|
|
label.block->end_class = EndClass::Branch;
|
|
|
|
label.block->cond = IR::Condition(true);
|
|
|
|
label.block->branch_true = nullptr;
|
|
|
|
label.block->branch_false = nullptr;
|
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
|
2021-04-18 17:10:55 +00:00
|
|
|
CFG::CFG(Environment& env_, ObjectPool<Block>& block_pool_, Location start_address,
|
|
|
|
bool exits_to_dispatcher_)
|
|
|
|
: env{env_}, block_pool{block_pool_}, program_start{start_address}, exits_to_dispatcher{
|
|
|
|
exits_to_dispatcher_} {
|
|
|
|
if (exits_to_dispatcher) {
|
|
|
|
dispatch_block = block_pool.Create(Block{});
|
|
|
|
dispatch_block->begin = {};
|
|
|
|
dispatch_block->end = {};
|
|
|
|
dispatch_block->end_class = EndClass::Exit;
|
|
|
|
dispatch_block->cond = IR::Condition(true);
|
|
|
|
dispatch_block->stack = {};
|
|
|
|
dispatch_block->branch_true = nullptr;
|
|
|
|
dispatch_block->branch_false = nullptr;
|
|
|
|
}
|
2021-03-14 06:41:05 +00:00
|
|
|
functions.emplace_back(block_pool, start_address);
|
2021-01-09 06:30:07 +00:00
|
|
|
for (FunctionId function_id = 0; function_id < functions.size(); ++function_id) {
|
|
|
|
while (!functions[function_id].labels.empty()) {
|
|
|
|
Function& function{functions[function_id]};
|
|
|
|
Label label{function.labels.back()};
|
|
|
|
function.labels.pop_back();
|
|
|
|
AnalyzeLabel(function_id, label);
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 17:10:55 +00:00
|
|
|
if (exits_to_dispatcher) {
|
2021-05-01 12:56:25 +00:00
|
|
|
const auto last_block{functions[0].blocks.rbegin()};
|
|
|
|
dispatch_block->begin = last_block->end + 1;
|
|
|
|
dispatch_block->end = last_block->end + 1;
|
2021-04-18 17:10:55 +00:00
|
|
|
functions[0].blocks.insert(*dispatch_block);
|
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFG::AnalyzeLabel(FunctionId function_id, Label& label) {
|
|
|
|
if (InspectVisitedBlocks(function_id, label)) {
|
|
|
|
// Label address has been visited
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Try to find the next block
|
2021-02-11 19:39:06 +00:00
|
|
|
Function* const function{&functions[function_id]};
|
2021-01-09 06:30:07 +00:00
|
|
|
Location pc{label.address};
|
2021-02-11 19:39:06 +00:00
|
|
|
const auto next_it{function->blocks.upper_bound(pc, Compare{})};
|
|
|
|
const bool is_last{next_it == function->blocks.end()};
|
|
|
|
Block* const next{is_last ? nullptr : &*next_it};
|
2021-01-09 06:30:07 +00:00
|
|
|
// Insert before the next block
|
2021-02-11 19:39:06 +00:00
|
|
|
Block* const block{label.block};
|
2021-01-09 06:30:07 +00:00
|
|
|
// Analyze instructions until it reaches an already visited block or there's a branch
|
|
|
|
bool is_branch{false};
|
2021-02-11 19:39:06 +00:00
|
|
|
while (!next || pc < next->begin) {
|
2021-01-09 06:30:07 +00:00
|
|
|
is_branch = AnalyzeInst(block, function_id, pc) == AnalysisState::Branch;
|
|
|
|
if (is_branch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pc;
|
|
|
|
}
|
|
|
|
if (!is_branch) {
|
|
|
|
// If the block finished without a branch,
|
|
|
|
// it means that the next instruction is already visited, jump to it
|
2021-02-11 19:39:06 +00:00
|
|
|
block->end = pc;
|
|
|
|
block->cond = IR::Condition{true};
|
|
|
|
block->branch_true = next;
|
|
|
|
block->branch_false = nullptr;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
// Function's pointer might be invalid, resolve it again
|
2021-02-11 19:39:06 +00:00
|
|
|
// Insert the new block
|
|
|
|
functions[function_id].blocks.insert(*block);
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CFG::InspectVisitedBlocks(FunctionId function_id, const Label& label) {
|
|
|
|
const Location pc{label.address};
|
|
|
|
Function& function{functions[function_id]};
|
2021-02-11 19:39:06 +00:00
|
|
|
const auto it{
|
|
|
|
std::ranges::find_if(function.blocks, [pc](auto& block) { return block.Contains(pc); })};
|
2021-01-09 06:30:07 +00:00
|
|
|
if (it == function.blocks.end()) {
|
|
|
|
// Address has not been visited
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
Block* const visited_block{&*it};
|
|
|
|
if (visited_block->begin == pc) {
|
|
|
|
throw LogicError("Dangling block");
|
|
|
|
}
|
|
|
|
Block* const new_block{label.block};
|
|
|
|
Split(visited_block, new_block, pc);
|
|
|
|
function.blocks.insert(it, *new_block);
|
2021-01-09 06:30:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:39:06 +00:00
|
|
|
CFG::AnalysisState CFG::AnalyzeInst(Block* block, FunctionId function_id, Location pc) {
|
2021-01-09 06:30:07 +00:00
|
|
|
const Instruction inst{env.ReadInstruction(pc.Offset())};
|
|
|
|
const Opcode opcode{Decode(inst.raw)};
|
|
|
|
switch (opcode) {
|
|
|
|
case Opcode::BRA:
|
|
|
|
case Opcode::JMP:
|
|
|
|
case Opcode::RET:
|
|
|
|
if (!AnalyzeBranch(block, function_id, pc, inst, opcode)) {
|
|
|
|
return AnalysisState::Continue;
|
|
|
|
}
|
|
|
|
switch (opcode) {
|
|
|
|
case Opcode::BRA:
|
|
|
|
case Opcode::JMP:
|
|
|
|
AnalyzeBRA(block, function_id, pc, inst, IsAbsoluteJump(opcode));
|
|
|
|
break;
|
|
|
|
case Opcode::RET:
|
2021-02-11 19:39:06 +00:00
|
|
|
block->end_class = EndClass::Return;
|
2021-01-09 06:30:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
block->end = pc;
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Branch;
|
|
|
|
case Opcode::BRK:
|
|
|
|
case Opcode::CONT:
|
|
|
|
case Opcode::LONGJMP:
|
|
|
|
case Opcode::SYNC: {
|
|
|
|
if (!AnalyzeBranch(block, function_id, pc, inst, opcode)) {
|
|
|
|
return AnalysisState::Continue;
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
const auto [stack_pc, new_stack]{block->stack.Pop(OpcodeToken(opcode))};
|
|
|
|
block->branch_true = AddLabel(block, new_stack, stack_pc, function_id);
|
|
|
|
block->end = pc;
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
2021-03-19 22:28:31 +00:00
|
|
|
case Opcode::KIL: {
|
|
|
|
const Predicate pred{inst.Pred()};
|
|
|
|
const auto ir_pred{static_cast<IR::Pred>(pred.index)};
|
|
|
|
const IR::Condition cond{inst.branch.flow_test, ir_pred, pred.negated};
|
|
|
|
AnalyzeCondInst(block, function_id, pc, EndClass::Kill, cond);
|
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
case Opcode::PBK:
|
|
|
|
case Opcode::PCNT:
|
|
|
|
case Opcode::PEXIT:
|
|
|
|
case Opcode::PLONGJMP:
|
|
|
|
case Opcode::SSY:
|
2021-02-11 19:39:06 +00:00
|
|
|
block->stack.Push(OpcodeToken(opcode), BranchOffset(pc, inst));
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Continue;
|
2021-03-27 21:30:24 +00:00
|
|
|
case Opcode::BRX:
|
|
|
|
case Opcode::JMX:
|
|
|
|
return AnalyzeBRX(block, pc, inst, IsAbsoluteJump(opcode), function_id);
|
2021-01-09 06:30:07 +00:00
|
|
|
case Opcode::EXIT:
|
|
|
|
return AnalyzeEXIT(block, function_id, pc, inst);
|
|
|
|
case Opcode::PRET:
|
|
|
|
throw NotImplementedException("PRET flow analysis");
|
|
|
|
case Opcode::CAL:
|
|
|
|
case Opcode::JCAL: {
|
|
|
|
const bool is_absolute{IsAbsoluteJump(opcode)};
|
|
|
|
const Location cal_pc{is_absolute ? inst.branch.Absolute() : BranchOffset(pc, inst)};
|
|
|
|
// Technically CAL pushes into PRET, but that's implicit in the function call for us
|
|
|
|
// Insert the function into the list if it doesn't exist
|
2021-03-14 06:41:05 +00:00
|
|
|
const auto it{std::ranges::find(functions, cal_pc, &Function::entrypoint)};
|
|
|
|
const bool exists{it != functions.end()};
|
2021-04-06 02:25:22 +00:00
|
|
|
const FunctionId call_id{exists ? static_cast<size_t>(std::distance(functions.begin(), it))
|
|
|
|
: functions.size()};
|
2021-03-14 06:41:05 +00:00
|
|
|
if (!exists) {
|
|
|
|
functions.emplace_back(block_pool, cal_pc);
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
2021-03-14 06:41:05 +00:00
|
|
|
block->end_class = EndClass::Call;
|
|
|
|
block->function_call = call_id;
|
|
|
|
block->return_block = AddLabel(block, block->stack, pc + 1, function_id);
|
|
|
|
block->end = pc;
|
|
|
|
return AnalysisState::Branch;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const Predicate pred{inst.Pred()};
|
|
|
|
if (pred == Predicate{true} || pred == Predicate{false}) {
|
|
|
|
return AnalysisState::Continue;
|
|
|
|
}
|
|
|
|
const IR::Condition cond{static_cast<IR::Pred>(pred.index), pred.negated};
|
2021-03-19 22:28:31 +00:00
|
|
|
AnalyzeCondInst(block, function_id, pc, EndClass::Branch, cond);
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:39:06 +00:00
|
|
|
void CFG::AnalyzeCondInst(Block* block, FunctionId function_id, Location pc,
|
2021-03-19 22:28:31 +00:00
|
|
|
EndClass insn_end_class, IR::Condition cond) {
|
2021-02-11 19:39:06 +00:00
|
|
|
if (block->begin != pc) {
|
2021-01-09 06:30:07 +00:00
|
|
|
// If the block doesn't start in the conditional instruction
|
|
|
|
// mark it as a label to visit it later
|
2021-02-11 19:39:06 +00:00
|
|
|
block->end = pc;
|
|
|
|
block->cond = IR::Condition{true};
|
|
|
|
block->branch_true = AddLabel(block, block->stack, pc, function_id);
|
|
|
|
block->branch_false = nullptr;
|
2021-01-09 06:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
// Create a virtual block and a conditional block
|
|
|
|
Block* const conditional_block{block_pool.Create()};
|
2021-04-06 02:25:22 +00:00
|
|
|
Block virtual_block{};
|
|
|
|
virtual_block.begin = block->begin.Virtual();
|
|
|
|
virtual_block.end = block->begin.Virtual();
|
|
|
|
virtual_block.end_class = EndClass::Branch;
|
|
|
|
virtual_block.stack = block->stack;
|
|
|
|
virtual_block.cond = cond;
|
|
|
|
virtual_block.branch_true = conditional_block;
|
|
|
|
virtual_block.branch_false = nullptr;
|
2021-02-11 19:39:06 +00:00
|
|
|
// Save the contents of the visited block in the conditional block
|
|
|
|
*conditional_block = std::move(*block);
|
|
|
|
// Impersonate the visited block with a virtual block
|
|
|
|
*block = std::move(virtual_block);
|
|
|
|
// Set the end properties of the conditional instruction
|
2021-03-19 22:28:31 +00:00
|
|
|
conditional_block->end = pc + 1;
|
2021-02-11 19:39:06 +00:00
|
|
|
conditional_block->end_class = insn_end_class;
|
2021-01-09 06:30:07 +00:00
|
|
|
// Add a label to the instruction after the conditional instruction
|
2021-02-11 19:39:06 +00:00
|
|
|
Block* const endif_block{AddLabel(conditional_block, block->stack, pc + 1, function_id)};
|
2021-01-09 06:30:07 +00:00
|
|
|
// Branch to the next instruction from the virtual block
|
2021-02-11 19:39:06 +00:00
|
|
|
block->branch_false = endif_block;
|
2021-03-19 22:28:31 +00:00
|
|
|
// And branch to it from the conditional instruction if it is a branch or a kill instruction
|
|
|
|
// Kill instructions are considered a branch because they demote to a helper invocation and
|
|
|
|
// execution may continue.
|
|
|
|
if (insn_end_class == EndClass::Branch || insn_end_class == EndClass::Kill) {
|
2021-02-11 19:39:06 +00:00
|
|
|
conditional_block->cond = IR::Condition{true};
|
|
|
|
conditional_block->branch_true = endif_block;
|
|
|
|
conditional_block->branch_false = nullptr;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
// Finally insert the condition block into the list of blocks
|
|
|
|
functions[function_id].blocks.insert(*conditional_block);
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 19:39:06 +00:00
|
|
|
bool CFG::AnalyzeBranch(Block* block, FunctionId function_id, Location pc, Instruction inst,
|
2021-01-09 06:30:07 +00:00
|
|
|
Opcode opcode) {
|
|
|
|
if (inst.branch.is_cbuf) {
|
|
|
|
throw NotImplementedException("Branch with constant buffer offset");
|
|
|
|
}
|
|
|
|
const Predicate pred{inst.Pred()};
|
|
|
|
if (pred == Predicate{false}) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const bool has_flow_test{HasFlowTest(opcode)};
|
|
|
|
const IR::FlowTest flow_test{has_flow_test ? inst.branch.flow_test.Value() : IR::FlowTest::T};
|
|
|
|
if (pred != Predicate{true} || flow_test != IR::FlowTest::T) {
|
2021-02-11 19:39:06 +00:00
|
|
|
block->cond = IR::Condition(flow_test, static_cast<IR::Pred>(pred.index), pred.negated);
|
|
|
|
block->branch_false = AddLabel(block, block->stack, pc + 1, function_id);
|
2021-01-09 06:30:07 +00:00
|
|
|
} else {
|
2021-02-11 19:39:06 +00:00
|
|
|
block->cond = IR::Condition{true};
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:39:06 +00:00
|
|
|
void CFG::AnalyzeBRA(Block* block, FunctionId function_id, Location pc, Instruction inst,
|
2021-01-09 06:30:07 +00:00
|
|
|
bool is_absolute) {
|
|
|
|
const Location bra_pc{is_absolute ? inst.branch.Absolute() : BranchOffset(pc, inst)};
|
2021-02-11 19:39:06 +00:00
|
|
|
block->branch_true = AddLabel(block, block->stack, bra_pc, function_id);
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 21:30:24 +00:00
|
|
|
CFG::AnalysisState CFG::AnalyzeBRX(Block* block, Location pc, Instruction inst, bool is_absolute,
|
|
|
|
FunctionId function_id) {
|
2021-04-06 00:01:01 +00:00
|
|
|
const std::optional brx_table{TrackIndirectBranchTable(env, pc, program_start)};
|
2021-03-27 21:30:24 +00:00
|
|
|
if (!brx_table) {
|
2021-04-06 00:01:01 +00:00
|
|
|
TrackIndirectBranchTable(env, pc, program_start);
|
2021-03-27 21:30:24 +00:00
|
|
|
throw NotImplementedException("Failed to track indirect branch");
|
|
|
|
}
|
|
|
|
const IR::FlowTest flow_test{inst.branch.flow_test};
|
|
|
|
const Predicate pred{inst.Pred()};
|
|
|
|
if (flow_test != IR::FlowTest::T || pred != Predicate{true}) {
|
|
|
|
throw NotImplementedException("Conditional indirect branch");
|
|
|
|
}
|
|
|
|
std::vector<u32> targets;
|
|
|
|
targets.reserve(brx_table->num_entries);
|
|
|
|
for (u32 i = 0; i < brx_table->num_entries; ++i) {
|
|
|
|
u32 target{env.ReadCbufValue(brx_table->cbuf_index, brx_table->cbuf_offset + i * 4)};
|
|
|
|
if (!is_absolute) {
|
|
|
|
target += pc.Offset();
|
|
|
|
}
|
2021-04-10 06:32:55 +00:00
|
|
|
target += static_cast<u32>(brx_table->branch_offset);
|
2021-03-27 21:30:24 +00:00
|
|
|
target += 8;
|
|
|
|
targets.push_back(target);
|
|
|
|
}
|
|
|
|
std::ranges::sort(targets);
|
|
|
|
targets.erase(std::unique(targets.begin(), targets.end()), targets.end());
|
|
|
|
|
|
|
|
block->indirect_branches.reserve(targets.size());
|
|
|
|
for (const u32 target : targets) {
|
|
|
|
Block* const branch{AddLabel(block, block->stack, target, function_id)};
|
2021-03-30 01:13:37 +00:00
|
|
|
block->indirect_branches.push_back({
|
2021-04-06 02:25:22 +00:00
|
|
|
.block = branch,
|
|
|
|
.address = target,
|
2021-03-30 01:13:37 +00:00
|
|
|
});
|
2021-03-27 21:30:24 +00:00
|
|
|
}
|
|
|
|
block->cond = IR::Condition{true};
|
|
|
|
block->end = pc + 1;
|
|
|
|
block->end_class = EndClass::IndirectBranch;
|
|
|
|
block->branch_reg = brx_table->branch_reg;
|
|
|
|
block->branch_offset = brx_table->branch_offset + 8;
|
|
|
|
if (!is_absolute) {
|
|
|
|
block->branch_offset += pc.Offset();
|
|
|
|
}
|
|
|
|
return AnalysisState::Branch;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 19:39:06 +00:00
|
|
|
CFG::AnalysisState CFG::AnalyzeEXIT(Block* block, FunctionId function_id, Location pc,
|
2021-01-09 06:30:07 +00:00
|
|
|
Instruction inst) {
|
|
|
|
const IR::FlowTest flow_test{inst.branch.flow_test};
|
|
|
|
const Predicate pred{inst.Pred()};
|
|
|
|
if (pred == Predicate{false} || flow_test == IR::FlowTest::F) {
|
|
|
|
// EXIT will never be taken
|
|
|
|
return AnalysisState::Continue;
|
|
|
|
}
|
2021-04-18 17:10:55 +00:00
|
|
|
if (exits_to_dispatcher && function_id != 0) {
|
2021-05-01 12:56:25 +00:00
|
|
|
throw NotImplementedException("Dispatch EXIT on external function");
|
2021-04-18 17:10:55 +00:00
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
if (pred != Predicate{true} || flow_test != IR::FlowTest::T) {
|
2021-02-11 19:39:06 +00:00
|
|
|
if (block->stack.Peek(Token::PEXIT).has_value()) {
|
2021-01-09 06:30:07 +00:00
|
|
|
throw NotImplementedException("Conditional EXIT with PEXIT token");
|
|
|
|
}
|
|
|
|
const IR::Condition cond{flow_test, static_cast<IR::Pred>(pred.index), pred.negated};
|
2021-04-18 17:10:55 +00:00
|
|
|
if (exits_to_dispatcher) {
|
|
|
|
block->end = pc;
|
|
|
|
block->end_class = EndClass::Branch;
|
|
|
|
block->cond = cond;
|
2021-05-01 12:56:25 +00:00
|
|
|
block->branch_true = dispatch_block;
|
2021-04-18 17:10:55 +00:00
|
|
|
block->branch_false = AddLabel(block, block->stack, pc + 1, function_id);
|
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
2021-03-19 22:28:31 +00:00
|
|
|
AnalyzeCondInst(block, function_id, pc, EndClass::Exit, cond);
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
if (const std::optional<Location> exit_pc{block->stack.Peek(Token::PEXIT)}) {
|
|
|
|
const Stack popped_stack{block->stack.Remove(Token::PEXIT)};
|
|
|
|
block->cond = IR::Condition{true};
|
|
|
|
block->branch_true = AddLabel(block, popped_stack, *exit_pc, function_id);
|
|
|
|
block->branch_false = nullptr;
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
2021-04-18 17:10:55 +00:00
|
|
|
if (exits_to_dispatcher) {
|
|
|
|
block->cond = IR::Condition{true};
|
|
|
|
block->end = pc;
|
|
|
|
block->end_class = EndClass::Branch;
|
|
|
|
block->branch_true = dispatch_block;
|
|
|
|
block->branch_false = nullptr;
|
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
2021-03-19 22:28:31 +00:00
|
|
|
block->end = pc + 1;
|
2021-02-11 19:39:06 +00:00
|
|
|
block->end_class = EndClass::Exit;
|
2021-01-09 06:30:07 +00:00
|
|
|
return AnalysisState::Branch;
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:39:06 +00:00
|
|
|
Block* CFG::AddLabel(Block* block, Stack stack, Location pc, FunctionId function_id) {
|
2021-01-09 06:30:07 +00:00
|
|
|
Function& function{functions[function_id]};
|
2021-02-11 19:39:06 +00:00
|
|
|
if (block->begin == pc) {
|
|
|
|
// Jumps to itself
|
|
|
|
return block;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
if (const auto it{function.blocks.find(pc, Compare{})}; it != function.blocks.end()) {
|
|
|
|
// Block already exists and it has been visited
|
2021-04-02 04:17:47 +00:00
|
|
|
if (function.blocks.begin() != it) {
|
|
|
|
// Check if the previous node is the virtual variant of the label
|
|
|
|
// This won't exist if a virtual node is not needed or it hasn't been visited
|
|
|
|
// If it hasn't been visited and a virtual node is needed, this will still behave as
|
|
|
|
// expected because the node impersonated with its virtual node.
|
|
|
|
const auto prev{std::prev(it)};
|
|
|
|
if (it->begin.Virtual() == prev->begin) {
|
|
|
|
return &*prev;
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
return &*it;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
2021-04-07 19:48:39 +00:00
|
|
|
// Make sure we don't insert the same layer twice
|
|
|
|
const auto label_it{std::ranges::find(function.labels, pc, &Label::address)};
|
|
|
|
if (label_it != function.labels.end()) {
|
|
|
|
return label_it->block;
|
|
|
|
}
|
2021-04-06 02:25:22 +00:00
|
|
|
Block* const new_block{block_pool.Create()};
|
|
|
|
new_block->begin = pc;
|
|
|
|
new_block->end = pc;
|
|
|
|
new_block->end_class = EndClass::Branch;
|
|
|
|
new_block->cond = IR::Condition(true);
|
|
|
|
new_block->stack = stack;
|
|
|
|
new_block->branch_true = nullptr;
|
|
|
|
new_block->branch_false = nullptr;
|
2021-01-09 06:30:07 +00:00
|
|
|
function.labels.push_back(Label{
|
|
|
|
.address{pc},
|
2021-04-06 02:25:22 +00:00
|
|
|
.block = new_block,
|
2021-01-09 06:30:07 +00:00
|
|
|
.stack{std::move(stack)},
|
|
|
|
});
|
2021-02-11 19:39:06 +00:00
|
|
|
return new_block;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CFG::Dot() const {
|
|
|
|
int node_uid{0};
|
|
|
|
|
|
|
|
std::string dot{"digraph shader {\n"};
|
|
|
|
for (const Function& function : functions) {
|
|
|
|
dot += fmt::format("\tsubgraph cluster_{} {{\n", function.entrypoint);
|
|
|
|
dot += fmt::format("\t\tnode [style=filled];\n");
|
2021-02-11 19:39:06 +00:00
|
|
|
for (const Block& block : function.blocks) {
|
2021-02-03 00:07:00 +00:00
|
|
|
const std::string name{NameOf(block)};
|
2021-02-11 19:39:06 +00:00
|
|
|
const auto add_branch = [&](Block* branch, bool add_label) {
|
|
|
|
dot += fmt::format("\t\t{}->{}", name, NameOf(*branch));
|
|
|
|
if (add_label && block.cond != IR::Condition{true} &&
|
|
|
|
block.cond != IR::Condition{false}) {
|
2021-01-09 06:30:07 +00:00
|
|
|
dot += fmt::format(" [label=\"{}\"]", block.cond);
|
|
|
|
}
|
|
|
|
dot += '\n';
|
|
|
|
};
|
|
|
|
dot += fmt::format("\t\t{};\n", name);
|
|
|
|
switch (block.end_class) {
|
|
|
|
case EndClass::Branch:
|
2021-02-11 19:39:06 +00:00
|
|
|
if (block.cond != IR::Condition{false}) {
|
2021-01-09 06:30:07 +00:00
|
|
|
add_branch(block.branch_true, true);
|
|
|
|
}
|
2021-02-11 19:39:06 +00:00
|
|
|
if (block.cond != IR::Condition{true}) {
|
2021-01-09 06:30:07 +00:00
|
|
|
add_branch(block.branch_false, false);
|
|
|
|
}
|
|
|
|
break;
|
2021-03-27 21:30:24 +00:00
|
|
|
case EndClass::IndirectBranch:
|
2021-03-30 01:13:37 +00:00
|
|
|
for (const IndirectBranch& branch : block.indirect_branches) {
|
|
|
|
add_branch(branch.block, false);
|
2021-03-27 21:30:24 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-03-14 06:41:05 +00:00
|
|
|
case EndClass::Call:
|
|
|
|
dot += fmt::format("\t\t{}->N{};\n", name, node_uid);
|
|
|
|
dot += fmt::format("\t\tN{}->{};\n", node_uid, NameOf(*block.return_block));
|
|
|
|
dot += fmt::format("\t\tN{} [label=\"Call {}\"][shape=square][style=stripped];\n",
|
|
|
|
node_uid, block.function_call);
|
|
|
|
dot += '\n';
|
|
|
|
++node_uid;
|
|
|
|
break;
|
2021-01-09 06:30:07 +00:00
|
|
|
case EndClass::Exit:
|
|
|
|
dot += fmt::format("\t\t{}->N{};\n", name, node_uid);
|
|
|
|
dot += fmt::format("\t\tN{} [label=\"Exit\"][shape=square][style=stripped];\n",
|
|
|
|
node_uid);
|
|
|
|
++node_uid;
|
|
|
|
break;
|
|
|
|
case EndClass::Return:
|
|
|
|
dot += fmt::format("\t\t{}->N{};\n", name, node_uid);
|
|
|
|
dot += fmt::format("\t\tN{} [label=\"Return\"][shape=square][style=stripped];\n",
|
|
|
|
node_uid);
|
|
|
|
++node_uid;
|
|
|
|
break;
|
2021-03-19 22:28:31 +00:00
|
|
|
case EndClass::Kill:
|
|
|
|
dot += fmt::format("\t\t{}->N{};\n", name, node_uid);
|
|
|
|
dot += fmt::format("\t\tN{} [label=\"Kill\"][shape=square][style=stripped];\n",
|
|
|
|
node_uid);
|
|
|
|
++node_uid;
|
|
|
|
break;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (function.entrypoint == 8) {
|
|
|
|
dot += fmt::format("\t\tlabel = \"main\";\n");
|
|
|
|
} else {
|
|
|
|
dot += fmt::format("\t\tlabel = \"Function {}\";\n", function.entrypoint);
|
|
|
|
}
|
|
|
|
dot += "\t}\n";
|
|
|
|
}
|
|
|
|
if (!functions.empty()) {
|
2021-02-11 19:39:06 +00:00
|
|
|
auto& function{functions.front()};
|
|
|
|
if (function.blocks.empty()) {
|
2021-01-09 06:30:07 +00:00
|
|
|
dot += "Start;\n";
|
|
|
|
} else {
|
2021-02-11 19:39:06 +00:00
|
|
|
dot += fmt::format("\tStart -> {};\n", NameOf(*function.blocks.begin()));
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
dot += fmt::format("\tStart [shape=diamond];\n");
|
|
|
|
}
|
|
|
|
dot += "}\n";
|
|
|
|
return dot;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Maxwell::Flow
|