2019-06-25 20:15:40 -04:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
2019-06-24 19:46:49 -04:00
|
|
|
|
|
|
|
#include <list>
|
2019-06-25 13:03:51 -04:00
|
|
|
#include <map>
|
2019-06-27 00:39:40 -04:00
|
|
|
#include <set>
|
2019-06-26 12:56:03 -04:00
|
|
|
#include <stack>
|
2019-06-25 11:10:45 -04:00
|
|
|
#include <unordered_map>
|
2019-06-24 19:46:49 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
2019-06-27 00:39:40 -04:00
|
|
|
#include "video_core/shader/ast.h"
|
2019-06-24 19:46:49 -04:00
|
|
|
#include "video_core/shader/control_flow.h"
|
2020-04-24 00:44:14 -04:00
|
|
|
#include "video_core/shader/memory_util.h"
|
2020-02-28 18:53:10 -05:00
|
|
|
#include "video_core/shader/registry.h"
|
2019-06-24 19:46:49 -04:00
|
|
|
#include "video_core/shader/shader_ir.h"
|
|
|
|
|
|
|
|
namespace VideoCommon::Shader {
|
2019-10-27 01:24:48 -04:00
|
|
|
|
2019-07-16 11:32:35 -04:00
|
|
|
namespace {
|
2019-10-27 01:24:48 -04:00
|
|
|
|
2019-06-24 19:46:49 -04:00
|
|
|
using Tegra::Shader::Instruction;
|
|
|
|
using Tegra::Shader::OpCode;
|
|
|
|
|
|
|
|
constexpr s32 unassigned_branch = -2;
|
|
|
|
|
2019-06-25 11:10:45 -04:00
|
|
|
struct Query {
|
2019-06-25 20:15:40 -04:00
|
|
|
u32 address{};
|
2019-06-26 12:56:03 -04:00
|
|
|
std::stack<u32> ssy_stack{};
|
|
|
|
std::stack<u32> pbk_stack{};
|
2019-06-25 11:10:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BlockStack {
|
|
|
|
BlockStack() = default;
|
2019-07-16 11:52:08 -04:00
|
|
|
explicit BlockStack(const Query& q) : ssy_stack{q.ssy_stack}, pbk_stack{q.pbk_stack} {}
|
2019-06-26 12:56:03 -04:00
|
|
|
std::stack<u32> ssy_stack{};
|
|
|
|
std::stack<u32> pbk_stack{};
|
2019-06-25 11:10:45 -04:00
|
|
|
};
|
|
|
|
|
2019-09-23 22:55:25 -04:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
BlockBranchInfo MakeBranchInfo(Args&&... args) {
|
|
|
|
static_assert(std::is_convertible_v<T, BranchData>);
|
|
|
|
return std::make_shared<BranchData>(T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockBranchIsIgnored(BlockBranchInfo first) {
|
|
|
|
bool ignore = false;
|
|
|
|
if (std::holds_alternative<SingleBranch>(*first)) {
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto branch = std::get_if<SingleBranch>(first.get());
|
2019-09-23 22:55:25 -04:00
|
|
|
ignore = branch->ignore;
|
|
|
|
}
|
|
|
|
return ignore;
|
|
|
|
}
|
2019-06-24 19:46:49 -04:00
|
|
|
|
|
|
|
struct BlockInfo {
|
|
|
|
u32 start{};
|
|
|
|
u32 end{};
|
|
|
|
bool visited{};
|
|
|
|
BlockBranchInfo branch{};
|
|
|
|
|
|
|
|
bool IsInside(const u32 address) const {
|
|
|
|
return start <= address && address <= end;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CFGRebuildState {
|
2020-02-28 18:53:10 -05:00
|
|
|
explicit CFGRebuildState(const ProgramCode& program_code, u32 start, Registry& registry)
|
|
|
|
: program_code{program_code}, registry{registry}, start{start} {}
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-09-24 22:34:18 -04:00
|
|
|
const ProgramCode& program_code;
|
2020-02-28 18:53:10 -05:00
|
|
|
Registry& registry;
|
2019-06-26 12:19:43 -04:00
|
|
|
u32 start{};
|
2019-10-27 01:23:03 -04:00
|
|
|
std::vector<BlockInfo> block_info;
|
|
|
|
std::list<u32> inspect_queries;
|
|
|
|
std::list<Query> queries;
|
|
|
|
std::unordered_map<u32, u32> registered;
|
|
|
|
std::set<u32> labels;
|
|
|
|
std::map<u32, u32> ssy_labels;
|
|
|
|
std::map<u32, u32> pbk_labels;
|
|
|
|
std::unordered_map<u32, BlockStack> stacks;
|
|
|
|
ASTManager* manager{};
|
2019-06-24 19:46:49 -04:00
|
|
|
};
|
|
|
|
|
2019-06-26 12:19:43 -04:00
|
|
|
enum class BlockCollision : u32 { None, Found, Inside };
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-27 09:24:40 -04:00
|
|
|
std::pair<BlockCollision, u32> TryGetBlock(CFGRebuildState& state, u32 address) {
|
|
|
|
const auto& blocks = state.block_info;
|
|
|
|
for (u32 index = 0; index < blocks.size(); index++) {
|
|
|
|
if (blocks[index].start == address) {
|
|
|
|
return {BlockCollision::Found, index};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
2019-06-27 09:24:40 -04:00
|
|
|
if (blocks[index].IsInside(address)) {
|
|
|
|
return {BlockCollision::Inside, index};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 11:56:37 -04:00
|
|
|
return {BlockCollision::None, 0xFFFFFFFF};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ParseInfo {
|
|
|
|
BlockBranchInfo branch_info{};
|
|
|
|
u32 end_address{};
|
|
|
|
};
|
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
BlockInfo& CreateBlockInfo(CFGRebuildState& state, u32 start, u32 end) {
|
2019-06-24 19:46:49 -04:00
|
|
|
auto& it = state.block_info.emplace_back();
|
|
|
|
it.start = start;
|
|
|
|
it.end = end;
|
2019-06-25 20:15:40 -04:00
|
|
|
const u32 index = static_cast<u32>(state.block_info.size() - 1);
|
2019-06-25 11:10:45 -04:00
|
|
|
state.registered.insert({start, index});
|
2019-06-26 12:56:03 -04:00
|
|
|
return it;
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Pred GetPredicate(u32 index, bool negated) {
|
2019-10-27 01:23:34 -04:00
|
|
|
return static_cast<Pred>(static_cast<u64>(index) + (negated ? 8ULL : 0ULL));
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class ParseResult : u32 {
|
2019-06-26 12:19:43 -04:00
|
|
|
ControlCaught,
|
|
|
|
BlockEnd,
|
|
|
|
AbnormalFlow,
|
2019-06-24 19:46:49 -04:00
|
|
|
};
|
|
|
|
|
2019-09-23 11:15:09 -04:00
|
|
|
struct BranchIndirectInfo {
|
|
|
|
u32 buffer{};
|
|
|
|
u32 offset{};
|
|
|
|
u32 entries{};
|
|
|
|
s32 relative_position{};
|
|
|
|
};
|
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
struct BufferInfo {
|
|
|
|
u32 index;
|
|
|
|
u32 offset;
|
|
|
|
};
|
2019-09-23 11:15:09 -04:00
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
std::optional<std::pair<s32, u64>> GetBRXInfo(const CFGRebuildState& state, u32& pos) {
|
|
|
|
const Instruction instr = state.program_code[pos];
|
2019-09-23 11:15:09 -04:00
|
|
|
const auto opcode = OpCode::Decode(instr);
|
|
|
|
if (opcode->get().GetId() != OpCode::Id::BRX) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
|
|
|
if (instr.brx.constant_buffer != 0) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
--pos;
|
|
|
|
return std::make_pair(instr.brx.GetBranchExtend(), instr.gpr8.Value());
|
|
|
|
}
|
|
|
|
|
2019-10-27 02:58:37 -04:00
|
|
|
template <typename Result, typename TestCallable, typename PackCallable>
|
|
|
|
// requires std::predicate<TestCallable, Instruction, const OpCode::Matcher&>
|
|
|
|
// requires std::invocable<PackCallable, Instruction, const OpCode::Matcher&>
|
|
|
|
std::optional<Result> TrackInstruction(const CFGRebuildState& state, u32& pos, TestCallable test,
|
|
|
|
PackCallable pack) {
|
2019-10-27 01:24:48 -04:00
|
|
|
for (; pos >= state.start; --pos) {
|
|
|
|
if (IsSchedInstruction(pos, state.start)) {
|
2019-09-23 11:15:09 -04:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
const Instruction instr = state.program_code[pos];
|
2019-09-23 11:15:09 -04:00
|
|
|
const auto opcode = OpCode::Decode(instr);
|
2019-10-27 01:24:48 -04:00
|
|
|
if (!opcode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (test(instr, opcode->get())) {
|
|
|
|
--pos;
|
|
|
|
return std::make_optional(pack(instr, opcode->get()));
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2019-09-23 11:15:09 -04:00
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
std::optional<std::pair<BufferInfo, u64>> TrackLDC(const CFGRebuildState& state, u32& pos,
|
|
|
|
u64 brx_tracked_register) {
|
|
|
|
return TrackInstruction<std::pair<BufferInfo, u64>>(
|
|
|
|
state, pos,
|
2019-10-28 01:40:53 -04:00
|
|
|
[brx_tracked_register](auto instr, const auto& opcode) {
|
2019-10-27 01:24:48 -04:00
|
|
|
return opcode.GetId() == OpCode::Id::LD_C &&
|
|
|
|
instr.gpr0.Value() == brx_tracked_register &&
|
|
|
|
instr.ld_c.type.Value() == Tegra::Shader::UniformType::Single;
|
|
|
|
},
|
2019-10-28 01:40:53 -04:00
|
|
|
[](auto instr, const auto& opcode) {
|
2019-10-27 01:24:48 -04:00
|
|
|
const BufferInfo info = {static_cast<u32>(instr.cbuf36.index.Value()),
|
|
|
|
static_cast<u32>(instr.cbuf36.GetOffset())};
|
|
|
|
return std::make_pair(info, instr.gpr8.Value());
|
|
|
|
});
|
|
|
|
}
|
2019-09-23 11:15:09 -04:00
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
std::optional<u64> TrackSHLRegister(const CFGRebuildState& state, u32& pos,
|
|
|
|
u64 ldc_tracked_register) {
|
|
|
|
return TrackInstruction<u64>(state, pos,
|
2019-10-28 01:40:53 -04:00
|
|
|
[ldc_tracked_register](auto instr, const auto& opcode) {
|
2019-10-27 01:24:48 -04:00
|
|
|
return opcode.GetId() == OpCode::Id::SHL_IMM &&
|
|
|
|
instr.gpr0.Value() == ldc_tracked_register;
|
|
|
|
},
|
2019-10-28 01:40:53 -04:00
|
|
|
[](auto instr, const auto&) { return instr.gpr8.Value(); });
|
2019-10-27 01:24:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u32> TrackIMNMXValue(const CFGRebuildState& state, u32& pos,
|
|
|
|
u64 shl_tracked_register) {
|
2019-10-28 01:40:53 -04:00
|
|
|
return TrackInstruction<u32>(state, pos,
|
|
|
|
[shl_tracked_register](auto instr, const auto& opcode) {
|
|
|
|
return opcode.GetId() == OpCode::Id::IMNMX_IMM &&
|
|
|
|
instr.gpr0.Value() == shl_tracked_register;
|
|
|
|
},
|
|
|
|
[](auto instr, const auto&) {
|
|
|
|
return static_cast<u32>(instr.alu.GetSignedImm20_20() + 1);
|
|
|
|
});
|
2019-10-27 01:24:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<BranchIndirectInfo> TrackBranchIndirectInfo(const CFGRebuildState& state, u32 pos) {
|
|
|
|
const auto brx_info = GetBRXInfo(state, pos);
|
|
|
|
if (!brx_info) {
|
|
|
|
return std::nullopt;
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
const auto [relative_position, brx_tracked_register] = *brx_info;
|
2019-09-23 11:15:09 -04:00
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
const auto ldc_info = TrackLDC(state, pos, brx_tracked_register);
|
|
|
|
if (!ldc_info) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
const auto [buffer_info, ldc_tracked_register] = *ldc_info;
|
2019-09-23 11:15:09 -04:00
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
const auto shl_tracked_register = TrackSHLRegister(state, pos, ldc_tracked_register);
|
|
|
|
if (!shl_tracked_register) {
|
|
|
|
return std::nullopt;
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 01:24:48 -04:00
|
|
|
const auto entries = TrackIMNMXValue(state, pos, *shl_tracked_register);
|
|
|
|
if (!entries) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
|
|
|
|
return BranchIndirectInfo{buffer_info.index, buffer_info.offset, *entries, relative_position};
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
std::pair<ParseResult, ParseInfo> ParseCode(CFGRebuildState& state, u32 address) {
|
2019-06-24 19:46:49 -04:00
|
|
|
u32 offset = static_cast<u32>(address);
|
2019-09-24 22:34:18 -04:00
|
|
|
const u32 end_address = static_cast<u32>(state.program_code.size());
|
2019-06-26 12:56:03 -04:00
|
|
|
ParseInfo parse_info{};
|
2019-09-23 22:55:25 -04:00
|
|
|
SingleBranch single_branch{};
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
const auto insert_label = [](CFGRebuildState& state, u32 address) {
|
|
|
|
const auto pair = state.labels.emplace(address);
|
2019-06-24 19:46:49 -04:00
|
|
|
if (pair.second) {
|
|
|
|
state.inspect_queries.push_back(address);
|
|
|
|
}
|
2019-06-26 12:56:03 -04:00
|
|
|
};
|
2019-06-24 19:46:49 -04:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (offset >= end_address) {
|
2019-06-26 13:16:13 -04:00
|
|
|
// ASSERT_OR_EXECUTE can't be used, as it ignores the break
|
|
|
|
ASSERT_MSG(false, "Shader passed the current limit!");
|
2019-09-23 22:55:25 -04:00
|
|
|
|
|
|
|
single_branch.address = exit_branch;
|
|
|
|
single_branch.ignore = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
break;
|
|
|
|
}
|
2019-06-25 11:10:45 -04:00
|
|
|
if (state.registered.count(offset) != 0) {
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = offset;
|
|
|
|
single_branch.ignore = true;
|
2019-06-24 19:46:49 -04:00
|
|
|
break;
|
|
|
|
}
|
2019-06-26 12:19:43 -04:00
|
|
|
if (IsSchedInstruction(offset, state.start)) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-24 19:46:49 -04:00
|
|
|
const Instruction instr = {state.program_code[offset]};
|
|
|
|
const auto opcode = OpCode::Decode(instr);
|
|
|
|
if (!opcode || opcode->get().GetType() != OpCode::Type::Flow) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::EXIT: {
|
|
|
|
const auto pred_index = static_cast<u32>(instr.pred.pred_index);
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.predicate = GetPredicate(pred_index, instr.negate_pred != 0);
|
|
|
|
if (single_branch.condition.predicate == Pred::NeverExecute) {
|
2019-06-24 19:46:49 -04:00
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ConditionCode cc = instr.flow_condition_code;
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.cc = cc;
|
2019-06-24 19:46:49 -04:00
|
|
|
if (cc == ConditionCode::F) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = exit_branch;
|
|
|
|
single_branch.kill = false;
|
|
|
|
single_branch.is_sync = false;
|
|
|
|
single_branch.is_brk = false;
|
|
|
|
single_branch.ignore = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
parse_info.end_address = offset;
|
2019-09-23 22:55:25 -04:00
|
|
|
parse_info.branch_info = MakeBranchInfo<SingleBranch>(
|
|
|
|
single_branch.condition, single_branch.address, single_branch.kill,
|
|
|
|
single_branch.is_sync, single_branch.is_brk, single_branch.ignore);
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::ControlCaught, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
case OpCode::Id::BRA: {
|
|
|
|
if (instr.bra.constant_buffer != 0) {
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::AbnormalFlow, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
const auto pred_index = static_cast<u32>(instr.pred.pred_index);
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.predicate = GetPredicate(pred_index, instr.negate_pred != 0);
|
|
|
|
if (single_branch.condition.predicate == Pred::NeverExecute) {
|
2019-06-24 19:46:49 -04:00
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ConditionCode cc = instr.flow_condition_code;
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.cc = cc;
|
2019-06-24 19:46:49 -04:00
|
|
|
if (cc == ConditionCode::F) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-26 12:56:03 -04:00
|
|
|
const u32 branch_offset = offset + instr.bra.GetBranchTarget();
|
2019-06-24 19:46:49 -04:00
|
|
|
if (branch_offset == 0) {
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = exit_branch;
|
2019-06-24 19:46:49 -04:00
|
|
|
} else {
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = branch_offset;
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
insert_label(state, branch_offset);
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.kill = false;
|
|
|
|
single_branch.is_sync = false;
|
|
|
|
single_branch.is_brk = false;
|
|
|
|
single_branch.ignore = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
parse_info.end_address = offset;
|
2019-09-23 22:55:25 -04:00
|
|
|
parse_info.branch_info = MakeBranchInfo<SingleBranch>(
|
|
|
|
single_branch.condition, single_branch.address, single_branch.kill,
|
|
|
|
single_branch.is_sync, single_branch.is_brk, single_branch.ignore);
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::ControlCaught, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
case OpCode::Id::SYNC: {
|
|
|
|
const auto pred_index = static_cast<u32>(instr.pred.pred_index);
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.predicate = GetPredicate(pred_index, instr.negate_pred != 0);
|
|
|
|
if (single_branch.condition.predicate == Pred::NeverExecute) {
|
2019-06-24 19:46:49 -04:00
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ConditionCode cc = instr.flow_condition_code;
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.cc = cc;
|
2019-06-24 19:46:49 -04:00
|
|
|
if (cc == ConditionCode::F) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = unassigned_branch;
|
|
|
|
single_branch.kill = false;
|
|
|
|
single_branch.is_sync = true;
|
|
|
|
single_branch.is_brk = false;
|
|
|
|
single_branch.ignore = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
parse_info.end_address = offset;
|
2019-09-23 22:55:25 -04:00
|
|
|
parse_info.branch_info = MakeBranchInfo<SingleBranch>(
|
|
|
|
single_branch.condition, single_branch.address, single_branch.kill,
|
|
|
|
single_branch.is_sync, single_branch.is_brk, single_branch.ignore);
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::ControlCaught, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
case OpCode::Id::BRK: {
|
|
|
|
const auto pred_index = static_cast<u32>(instr.pred.pred_index);
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.predicate = GetPredicate(pred_index, instr.negate_pred != 0);
|
|
|
|
if (single_branch.condition.predicate == Pred::NeverExecute) {
|
2019-06-24 19:46:49 -04:00
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ConditionCode cc = instr.flow_condition_code;
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.cc = cc;
|
2019-06-24 19:46:49 -04:00
|
|
|
if (cc == ConditionCode::F) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = unassigned_branch;
|
|
|
|
single_branch.kill = false;
|
|
|
|
single_branch.is_sync = false;
|
|
|
|
single_branch.is_brk = true;
|
|
|
|
single_branch.ignore = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
parse_info.end_address = offset;
|
2019-09-23 22:55:25 -04:00
|
|
|
parse_info.branch_info = MakeBranchInfo<SingleBranch>(
|
|
|
|
single_branch.condition, single_branch.address, single_branch.kill,
|
|
|
|
single_branch.is_sync, single_branch.is_brk, single_branch.ignore);
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::ControlCaught, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
case OpCode::Id::KIL: {
|
|
|
|
const auto pred_index = static_cast<u32>(instr.pred.pred_index);
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.predicate = GetPredicate(pred_index, instr.negate_pred != 0);
|
|
|
|
if (single_branch.condition.predicate == Pred::NeverExecute) {
|
2019-06-24 19:46:49 -04:00
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ConditionCode cc = instr.flow_condition_code;
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.condition.cc = cc;
|
2019-06-24 19:46:49 -04:00
|
|
|
if (cc == ConditionCode::F) {
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.address = exit_branch;
|
|
|
|
single_branch.kill = true;
|
|
|
|
single_branch.is_sync = false;
|
|
|
|
single_branch.is_brk = false;
|
|
|
|
single_branch.ignore = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
parse_info.end_address = offset;
|
2019-09-23 22:55:25 -04:00
|
|
|
parse_info.branch_info = MakeBranchInfo<SingleBranch>(
|
|
|
|
single_branch.condition, single_branch.address, single_branch.kill,
|
|
|
|
single_branch.is_sync, single_branch.is_brk, single_branch.ignore);
|
2019-06-24 19:46:49 -04:00
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::ControlCaught, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
case OpCode::Id::SSY: {
|
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
|
|
|
insert_label(state, target);
|
|
|
|
state.ssy_labels.emplace(offset, target);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::PBK: {
|
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
|
|
|
insert_label(state, target);
|
|
|
|
state.pbk_labels.emplace(offset, target);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-24 21:25:38 -04:00
|
|
|
case OpCode::Id::BRX: {
|
2019-10-27 01:24:48 -04:00
|
|
|
const auto tmp = TrackBranchIndirectInfo(state, offset);
|
|
|
|
if (!tmp) {
|
2019-09-23 22:55:25 -04:00
|
|
|
LOG_WARNING(HW_GPU, "BRX Track Unsuccesful");
|
2019-10-27 01:24:48 -04:00
|
|
|
return {ParseResult::AbnormalFlow, parse_info};
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto result = *tmp;
|
|
|
|
const s32 pc_target = offset + result.relative_position;
|
|
|
|
std::vector<CaseBranch> branches;
|
|
|
|
for (u32 i = 0; i < result.entries; i++) {
|
2020-02-28 18:53:10 -05:00
|
|
|
auto key = state.registry.ObtainKey(result.buffer, result.offset + i * 4);
|
2019-10-27 01:24:48 -04:00
|
|
|
if (!key) {
|
|
|
|
return {ParseResult::AbnormalFlow, parse_info};
|
|
|
|
}
|
|
|
|
u32 value = *key;
|
|
|
|
u32 target = static_cast<u32>((value >> 3) + pc_target);
|
|
|
|
insert_label(state, target);
|
|
|
|
branches.emplace_back(value, target);
|
2019-09-23 11:15:09 -04:00
|
|
|
}
|
2019-10-27 01:24:48 -04:00
|
|
|
parse_info.end_address = offset;
|
|
|
|
parse_info.branch_info = MakeBranchInfo<MultiBranch>(
|
|
|
|
static_cast<u32>(instr.gpr8.Value()), std::move(branches));
|
|
|
|
|
|
|
|
return {ParseResult::ControlCaught, parse_info};
|
2019-06-24 21:25:38 -04:00
|
|
|
}
|
2019-06-24 19:46:49 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
single_branch.kill = false;
|
|
|
|
single_branch.is_sync = false;
|
|
|
|
single_branch.is_brk = false;
|
2019-06-24 19:46:49 -04:00
|
|
|
parse_info.end_address = offset - 1;
|
2019-09-23 22:55:25 -04:00
|
|
|
parse_info.branch_info = MakeBranchInfo<SingleBranch>(
|
|
|
|
single_branch.condition, single_branch.address, single_branch.kill, single_branch.is_sync,
|
|
|
|
single_branch.is_brk, single_branch.ignore);
|
2019-06-26 12:56:03 -04:00
|
|
|
return {ParseResult::BlockEnd, parse_info};
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TryInspectAddress(CFGRebuildState& state) {
|
|
|
|
if (state.inspect_queries.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-27 09:24:40 -04:00
|
|
|
|
2019-06-25 20:15:40 -04:00
|
|
|
const u32 address = state.inspect_queries.front();
|
2019-06-24 19:46:49 -04:00
|
|
|
state.inspect_queries.pop_front();
|
2019-06-27 09:24:40 -04:00
|
|
|
const auto [result, block_index] = TryGetBlock(state, address);
|
|
|
|
switch (result) {
|
2019-06-24 19:46:49 -04:00
|
|
|
case BlockCollision::Found: {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case BlockCollision::Inside: {
|
|
|
|
// This case is the tricky one:
|
2020-04-15 23:29:02 -04:00
|
|
|
// We need to split the block into 2 separate blocks
|
2019-06-27 09:24:40 -04:00
|
|
|
const u32 end = state.block_info[block_index].end;
|
|
|
|
BlockInfo& new_block = CreateBlockInfo(state, address, end);
|
|
|
|
BlockInfo& current_block = state.block_info[block_index];
|
|
|
|
current_block.end = address - 1;
|
2020-04-15 23:29:02 -04:00
|
|
|
new_block.branch = std::move(current_block.branch);
|
2019-09-23 22:55:25 -04:00
|
|
|
BlockBranchInfo forward_branch = MakeBranchInfo<SingleBranch>();
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto branch = std::get_if<SingleBranch>(forward_branch.get());
|
2019-09-23 22:55:25 -04:00
|
|
|
branch->address = address;
|
|
|
|
branch->ignore = true;
|
2020-04-15 23:29:02 -04:00
|
|
|
current_block.branch = std::move(forward_branch);
|
2019-06-24 19:46:49 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-06-26 12:56:03 -04:00
|
|
|
const auto [parse_result, parse_info] = ParseCode(state, address);
|
2019-06-24 19:46:49 -04:00
|
|
|
if (parse_result == ParseResult::AbnormalFlow) {
|
|
|
|
// if it's AbnormalFlow, we end it as false, ending the CFG reconstruction
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-26 12:56:03 -04:00
|
|
|
BlockInfo& block_info = CreateBlockInfo(state, address, parse_info.end_address);
|
|
|
|
block_info.branch = parse_info.branch_info;
|
2019-09-23 22:55:25 -04:00
|
|
|
if (std::holds_alternative<SingleBranch>(*block_info.branch)) {
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto branch = std::get_if<SingleBranch>(block_info.branch.get());
|
2019-09-23 22:55:25 -04:00
|
|
|
if (branch->condition.IsUnconditional()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const u32 fallthrough_address = parse_info.end_address + 1;
|
|
|
|
state.inspect_queries.push_front(fallthrough_address);
|
2019-06-24 19:46:49 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-25 11:10:45 -04:00
|
|
|
bool TryQuery(CFGRebuildState& state) {
|
2019-06-26 12:56:03 -04:00
|
|
|
const auto gather_labels = [](std::stack<u32>& cc, std::map<u32, u32>& labels,
|
|
|
|
BlockInfo& block) {
|
2019-06-25 13:03:51 -04:00
|
|
|
auto gather_start = labels.lower_bound(block.start);
|
2019-06-25 20:15:40 -04:00
|
|
|
const auto gather_end = labels.upper_bound(block.end);
|
2019-06-25 11:10:45 -04:00
|
|
|
while (gather_start != gather_end) {
|
2019-06-26 12:56:03 -04:00
|
|
|
cc.push(gather_start->second);
|
2019-07-16 11:42:05 -04:00
|
|
|
++gather_start;
|
2019-06-25 11:10:45 -04:00
|
|
|
}
|
2019-06-26 12:56:03 -04:00
|
|
|
};
|
2019-06-25 11:10:45 -04:00
|
|
|
if (state.queries.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-16 11:50:36 -04:00
|
|
|
|
2019-06-25 11:10:45 -04:00
|
|
|
Query& q = state.queries.front();
|
2019-06-25 20:15:40 -04:00
|
|
|
const u32 block_index = state.registered[q.address];
|
2019-06-25 11:10:45 -04:00
|
|
|
BlockInfo& block = state.block_info[block_index];
|
2019-07-16 11:50:36 -04:00
|
|
|
// If the block is visited, check if the stacks match, else gather the ssy/pbk
|
2019-06-25 13:03:51 -04:00
|
|
|
// labels into the current stack and look if the branch at the end of the block
|
|
|
|
// consumes a label. Schedule new queries accordingly
|
2019-06-25 11:10:45 -04:00
|
|
|
if (block.visited) {
|
|
|
|
BlockStack& stack = state.stacks[q.address];
|
2019-07-16 11:40:58 -04:00
|
|
|
const bool all_okay = (stack.ssy_stack.empty() || q.ssy_stack == stack.ssy_stack) &&
|
|
|
|
(stack.pbk_stack.empty() || q.pbk_stack == stack.pbk_stack);
|
2019-06-25 11:10:45 -04:00
|
|
|
state.queries.pop_front();
|
|
|
|
return all_okay;
|
|
|
|
}
|
|
|
|
block.visited = true;
|
2019-07-16 11:50:36 -04:00
|
|
|
state.stacks.insert_or_assign(q.address, BlockStack{q});
|
|
|
|
|
2019-06-25 11:10:45 -04:00
|
|
|
Query q2(q);
|
|
|
|
state.queries.pop_front();
|
|
|
|
gather_labels(q2.ssy_stack, state.ssy_labels, block);
|
|
|
|
gather_labels(q2.pbk_stack, state.pbk_labels, block);
|
2019-09-23 22:55:25 -04:00
|
|
|
if (std::holds_alternative<SingleBranch>(*block.branch)) {
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto branch = std::get_if<SingleBranch>(block.branch.get());
|
2019-09-23 22:55:25 -04:00
|
|
|
if (!branch->condition.IsUnconditional()) {
|
|
|
|
q2.address = block.end + 1;
|
|
|
|
state.queries.push_back(q2);
|
|
|
|
}
|
2019-07-16 11:50:36 -04:00
|
|
|
|
2019-09-23 22:55:25 -04:00
|
|
|
Query conditional_query{q2};
|
|
|
|
if (branch->is_sync) {
|
|
|
|
if (branch->address == unassigned_branch) {
|
|
|
|
branch->address = conditional_query.ssy_stack.top();
|
|
|
|
}
|
|
|
|
conditional_query.ssy_stack.pop();
|
2019-06-25 11:10:45 -04:00
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
if (branch->is_brk) {
|
|
|
|
if (branch->address == unassigned_branch) {
|
|
|
|
branch->address = conditional_query.pbk_stack.top();
|
|
|
|
}
|
|
|
|
conditional_query.pbk_stack.pop();
|
2019-06-25 11:10:45 -04:00
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
conditional_query.address = branch->address;
|
|
|
|
state.queries.push_back(std::move(conditional_query));
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto multi_branch = std::get_if<MultiBranch>(block.branch.get());
|
|
|
|
for (const auto& branch_case : multi_branch->branches) {
|
2019-09-23 22:55:25 -04:00
|
|
|
Query conditional_query{q2};
|
|
|
|
conditional_query.address = branch_case.address;
|
|
|
|
state.queries.push_back(std::move(conditional_query));
|
2019-06-25 11:10:45 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-24 22:34:18 -04:00
|
|
|
|
2019-09-23 22:55:25 -04:00
|
|
|
void InsertBranch(ASTManager& mm, const BlockBranchInfo& branch_info) {
|
2019-06-27 00:39:40 -04:00
|
|
|
const auto get_expr = ([&](const Condition& cond) -> Expr {
|
|
|
|
Expr result{};
|
|
|
|
if (cond.cc != ConditionCode::T) {
|
|
|
|
result = MakeExpr<ExprCondCode>(cond.cc);
|
|
|
|
}
|
|
|
|
if (cond.predicate != Pred::UnusedIndex) {
|
2019-06-28 20:54:21 -04:00
|
|
|
u32 pred = static_cast<u32>(cond.predicate);
|
2019-06-29 01:44:07 -04:00
|
|
|
bool negate = false;
|
2019-06-28 20:54:21 -04:00
|
|
|
if (pred > 7) {
|
|
|
|
negate = true;
|
|
|
|
pred -= 8;
|
|
|
|
}
|
|
|
|
Expr extra = MakeExpr<ExprPredicate>(pred);
|
|
|
|
if (negate) {
|
|
|
|
extra = MakeExpr<ExprNot>(extra);
|
|
|
|
}
|
2019-06-27 00:39:40 -04:00
|
|
|
if (result) {
|
|
|
|
return MakeExpr<ExprAnd>(extra, result);
|
|
|
|
}
|
|
|
|
return extra;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return MakeExpr<ExprBoolean>(true);
|
|
|
|
});
|
2019-09-23 22:55:25 -04:00
|
|
|
if (std::holds_alternative<SingleBranch>(*branch_info)) {
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto branch = std::get_if<SingleBranch>(branch_info.get());
|
2019-09-23 22:55:25 -04:00
|
|
|
if (branch->address < 0) {
|
|
|
|
if (branch->kill) {
|
|
|
|
mm.InsertReturn(get_expr(branch->condition), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mm.InsertReturn(get_expr(branch->condition), false);
|
2019-06-27 00:39:40 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
mm.InsertGoto(get_expr(branch->condition), branch->address);
|
2019-06-27 00:39:40 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-10-17 10:35:16 -04:00
|
|
|
const auto multi_branch = std::get_if<MultiBranch>(branch_info.get());
|
|
|
|
for (const auto& branch_case : multi_branch->branches) {
|
2019-09-23 22:55:25 -04:00
|
|
|
mm.InsertGoto(MakeExpr<ExprGprEqual>(multi_branch->gpr, branch_case.cmp_value),
|
|
|
|
branch_case.address);
|
|
|
|
}
|
2019-06-27 00:39:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecompileShader(CFGRebuildState& state) {
|
2019-08-16 16:25:02 -04:00
|
|
|
state.manager->Init();
|
2019-06-27 00:39:40 -04:00
|
|
|
for (auto label : state.labels) {
|
2019-08-16 16:25:02 -04:00
|
|
|
state.manager->DeclareLabel(label);
|
2019-06-27 00:39:40 -04:00
|
|
|
}
|
|
|
|
for (auto& block : state.block_info) {
|
|
|
|
if (state.labels.count(block.start) != 0) {
|
2019-08-16 16:25:02 -04:00
|
|
|
state.manager->InsertLabel(block.start);
|
2019-06-27 00:39:40 -04:00
|
|
|
}
|
2019-09-23 22:55:25 -04:00
|
|
|
const bool ignore = BlockBranchIsIgnored(block.branch);
|
|
|
|
u32 end = ignore ? block.end + 1 : block.end;
|
2019-08-16 16:25:02 -04:00
|
|
|
state.manager->InsertBlock(block.start, end);
|
2019-09-23 22:55:25 -04:00
|
|
|
if (!ignore) {
|
2019-08-16 16:25:02 -04:00
|
|
|
InsertBranch(*state.manager, block.branch);
|
2019-06-27 00:39:40 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 16:25:02 -04:00
|
|
|
state.manager->Decompile();
|
2019-06-27 00:39:40 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 22:43:33 -04:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2019-09-24 22:34:18 -04:00
|
|
|
std::unique_ptr<ShaderCharacteristics> ScanFlow(const ProgramCode& program_code, u32 start_address,
|
2019-09-23 15:40:58 -04:00
|
|
|
const CompilerSettings& settings,
|
2020-02-28 18:53:10 -05:00
|
|
|
Registry& registry) {
|
2019-08-16 16:25:02 -04:00
|
|
|
auto result_out = std::make_unique<ShaderCharacteristics>();
|
|
|
|
if (settings.depth == CompileDepth::BruteForce) {
|
|
|
|
result_out->settings.depth = CompileDepth::BruteForce;
|
2019-10-05 08:52:20 -04:00
|
|
|
return result_out;
|
2019-08-16 16:25:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
CFGRebuildState state{program_code, start_address, registry};
|
2019-06-24 19:46:49 -04:00
|
|
|
// Inspect Code and generate blocks
|
|
|
|
state.labels.clear();
|
|
|
|
state.labels.emplace(start_address);
|
2019-06-26 12:19:43 -04:00
|
|
|
state.inspect_queries.push_back(state.start);
|
2019-06-24 19:46:49 -04:00
|
|
|
while (!state.inspect_queries.empty()) {
|
|
|
|
if (!TryInspectAddress(state)) {
|
2019-08-16 16:25:02 -04:00
|
|
|
result_out->settings.depth = CompileDepth::BruteForce;
|
2019-10-05 08:52:20 -04:00
|
|
|
return result_out;
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 11:50:36 -04:00
|
|
|
|
2019-08-16 16:25:02 -04:00
|
|
|
bool use_flow_stack = true;
|
|
|
|
|
|
|
|
bool decompiled = false;
|
|
|
|
|
|
|
|
if (settings.depth != CompileDepth::FlowStack) {
|
|
|
|
// Decompile Stacks
|
|
|
|
state.queries.push_back(Query{state.start, {}, {}});
|
|
|
|
decompiled = true;
|
|
|
|
while (!state.queries.empty()) {
|
|
|
|
if (!TryQuery(state)) {
|
|
|
|
decompiled = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-06-25 11:10:45 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 11:50:36 -04:00
|
|
|
|
2019-08-16 16:25:02 -04:00
|
|
|
use_flow_stack = !decompiled;
|
|
|
|
|
2019-06-25 11:10:45 -04:00
|
|
|
// Sort and organize results
|
2019-06-24 19:46:49 -04:00
|
|
|
std::sort(state.block_info.begin(), state.block_info.end(),
|
2019-06-27 00:39:40 -04:00
|
|
|
[](const BlockInfo& a, const BlockInfo& b) -> bool { return a.start < b.start; });
|
2019-08-16 16:25:02 -04:00
|
|
|
if (decompiled && settings.depth != CompileDepth::NoFlowStack) {
|
2019-09-20 21:12:06 -04:00
|
|
|
ASTManager manager{settings.depth != CompileDepth::DecompileBackwards,
|
|
|
|
settings.disable_else_derivation};
|
2019-08-16 16:25:02 -04:00
|
|
|
state.manager = &manager;
|
2019-06-27 00:39:40 -04:00
|
|
|
DecompileShader(state);
|
2019-08-16 16:25:02 -04:00
|
|
|
decompiled = state.manager->IsFullyDecompiled();
|
2019-06-28 22:59:43 -04:00
|
|
|
if (!decompiled) {
|
2019-08-16 16:25:02 -04:00
|
|
|
if (settings.depth == CompileDepth::FullDecompile) {
|
|
|
|
LOG_CRITICAL(HW_GPU, "Failed to remove all the gotos!:");
|
|
|
|
} else {
|
|
|
|
LOG_CRITICAL(HW_GPU, "Failed to remove all backward gotos!:");
|
|
|
|
}
|
|
|
|
state.manager->ShowCurrentState("Of Shader");
|
|
|
|
state.manager->Clear();
|
|
|
|
} else {
|
2019-10-05 08:55:08 -04:00
|
|
|
auto characteristics = std::make_unique<ShaderCharacteristics>();
|
|
|
|
characteristics->start = start_address;
|
|
|
|
characteristics->settings.depth = settings.depth;
|
|
|
|
characteristics->manager = std::move(manager);
|
|
|
|
characteristics->end = state.block_info.back().end + 1;
|
|
|
|
return characteristics;
|
2019-06-28 22:59:43 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-05 08:52:20 -04:00
|
|
|
|
2019-06-28 22:59:43 -04:00
|
|
|
result_out->start = start_address;
|
2019-08-16 16:25:02 -04:00
|
|
|
result_out->settings.depth =
|
|
|
|
use_flow_stack ? CompileDepth::FlowStack : CompileDepth::NoFlowStack;
|
|
|
|
result_out->blocks.clear();
|
2019-06-28 22:59:43 -04:00
|
|
|
for (auto& block : state.block_info) {
|
2019-06-24 19:46:49 -04:00
|
|
|
ShaderBlock new_block{};
|
|
|
|
new_block.start = block.start;
|
|
|
|
new_block.end = block.end;
|
2019-09-23 22:55:25 -04:00
|
|
|
new_block.ignore_branch = BlockBranchIsIgnored(block.branch);
|
2019-06-25 11:10:45 -04:00
|
|
|
if (!new_block.ignore_branch) {
|
2019-09-23 22:55:25 -04:00
|
|
|
new_block.branch = block.branch;
|
2019-06-25 11:10:45 -04:00
|
|
|
}
|
2019-06-28 22:59:43 -04:00
|
|
|
result_out->end = std::max(result_out->end, block.end);
|
|
|
|
result_out->blocks.push_back(new_block);
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
2019-08-16 16:25:02 -04:00
|
|
|
if (!use_flow_stack) {
|
|
|
|
result_out->labels = std::move(state.labels);
|
2019-10-05 08:52:20 -04:00
|
|
|
return result_out;
|
2019-08-16 16:25:02 -04:00
|
|
|
}
|
2019-10-05 08:52:20 -04:00
|
|
|
|
2019-06-28 22:59:43 -04:00
|
|
|
auto back = result_out->blocks.begin();
|
2019-06-24 19:46:49 -04:00
|
|
|
auto next = std::next(back);
|
2019-06-28 22:59:43 -04:00
|
|
|
while (next != result_out->blocks.end()) {
|
2019-06-24 19:46:49 -04:00
|
|
|
if (state.labels.count(next->start) == 0 && next->start == back->end + 1) {
|
|
|
|
back->end = next->end;
|
2019-06-28 22:59:43 -04:00
|
|
|
next = result_out->blocks.erase(next);
|
2019-06-24 19:46:49 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
back = next;
|
2019-07-16 11:42:05 -04:00
|
|
|
++next;
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
2019-10-05 08:52:20 -04:00
|
|
|
|
|
|
|
return result_out;
|
2019-06-24 19:46:49 -04:00
|
|
|
}
|
|
|
|
} // namespace VideoCommon::Shader
|