2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-05-05 07:19:08 +02:00
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "shader_recompiler/backend/glasm/reg_alloc.h"
|
|
|
|
#include "shader_recompiler/exception.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/value.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::GLASM {
|
2021-05-09 08:11:34 +02:00
|
|
|
|
|
|
|
Register RegAlloc::Define(IR::Inst& inst) {
|
2021-05-09 23:03:01 +02:00
|
|
|
return Define(inst, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Register RegAlloc::LongDefine(IR::Inst& inst) {
|
|
|
|
return Define(inst, true);
|
2021-05-07 11:31:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 08:47:31 +02:00
|
|
|
Value RegAlloc::Peek(const IR::Value& value) {
|
2021-05-25 07:22:21 +02:00
|
|
|
if (value.IsImmediate()) {
|
|
|
|
return MakeImm(value);
|
|
|
|
} else {
|
|
|
|
return PeekInst(*value.Inst());
|
|
|
|
}
|
2021-05-10 08:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
Value RegAlloc::Consume(const IR::Value& value) {
|
2021-05-25 07:22:21 +02:00
|
|
|
if (value.IsImmediate()) {
|
|
|
|
return MakeImm(value);
|
|
|
|
} else {
|
|
|
|
return ConsumeInst(*value.Inst());
|
|
|
|
}
|
2021-05-10 08:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::Unref(IR::Inst& inst) {
|
2021-05-18 00:24:09 +02:00
|
|
|
IR::Inst& value_inst{AliasInst(inst)};
|
|
|
|
value_inst.DestructiveRemoveUsage();
|
|
|
|
if (!value_inst.HasUses()) {
|
|
|
|
Free(value_inst.Definition<Id>());
|
2021-05-09 08:11:34 +02:00
|
|
|
}
|
2021-05-10 08:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Register RegAlloc::AllocReg() {
|
|
|
|
Register ret;
|
|
|
|
ret.type = Type::Register;
|
|
|
|
ret.id = Alloc(false);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register RegAlloc::AllocLongReg() {
|
|
|
|
Register ret;
|
|
|
|
ret.type = Type::Register;
|
|
|
|
ret.id = Alloc(true);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::FreeReg(Register reg) {
|
|
|
|
Free(reg.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value RegAlloc::MakeImm(const IR::Value& value) {
|
2021-05-09 08:11:34 +02:00
|
|
|
Value ret;
|
2021-05-07 11:31:30 +02:00
|
|
|
switch (value.Type()) {
|
2021-05-15 23:15:13 +02:00
|
|
|
case IR::Type::Void:
|
|
|
|
ret.type = Type::Void;
|
|
|
|
break;
|
2021-05-07 11:31:30 +02:00
|
|
|
case IR::Type::U1:
|
2021-05-09 08:11:34 +02:00
|
|
|
ret.type = Type::U32;
|
|
|
|
ret.imm_u32 = value.U1() ? 0xffffffff : 0;
|
|
|
|
break;
|
2021-05-07 11:31:30 +02:00
|
|
|
case IR::Type::U32:
|
2021-05-09 08:11:34 +02:00
|
|
|
ret.type = Type::U32;
|
|
|
|
ret.imm_u32 = value.U32();
|
|
|
|
break;
|
2021-05-07 11:31:30 +02:00
|
|
|
case IR::Type::F32:
|
2021-05-25 07:46:51 +02:00
|
|
|
ret.type = Type::U32;
|
|
|
|
ret.imm_u32 = Common::BitCast<u32>(value.F32());
|
2021-05-09 08:11:34 +02:00
|
|
|
break;
|
2021-05-10 03:43:29 +02:00
|
|
|
case IR::Type::U64:
|
|
|
|
ret.type = Type::U64;
|
|
|
|
ret.imm_u64 = value.U64();
|
|
|
|
break;
|
2021-05-09 23:03:01 +02:00
|
|
|
case IR::Type::F64:
|
2021-05-25 07:46:51 +02:00
|
|
|
ret.type = Type::U64;
|
|
|
|
ret.imm_u64 = Common::BitCast<u64>(value.F64());
|
2021-05-09 23:03:01 +02:00
|
|
|
break;
|
2021-05-07 11:31:30 +02:00
|
|
|
default:
|
2021-05-09 00:59:05 +02:00
|
|
|
throw NotImplementedException("Immediate type {}", value.Type());
|
2021-05-05 07:19:08 +02:00
|
|
|
}
|
2021-05-09 08:11:34 +02:00
|
|
|
return ret;
|
2021-05-05 07:19:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 08:47:31 +02:00
|
|
|
Register RegAlloc::Define(IR::Inst& inst, bool is_long) {
|
2021-05-25 07:22:21 +02:00
|
|
|
if (inst.HasUses()) {
|
|
|
|
inst.SetDefinition<Id>(Alloc(is_long));
|
|
|
|
} else {
|
|
|
|
Id id{};
|
|
|
|
id.is_long.Assign(is_long ? 1 : 0);
|
|
|
|
id.is_null.Assign(1);
|
|
|
|
inst.SetDefinition<Id>(id);
|
|
|
|
}
|
2021-05-10 08:47:31 +02:00
|
|
|
return Register{PeekInst(inst)};
|
2021-05-05 07:19:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 08:47:31 +02:00
|
|
|
Value RegAlloc::PeekInst(IR::Inst& inst) {
|
|
|
|
Value ret;
|
2021-05-09 23:03:01 +02:00
|
|
|
ret.type = Type::Register;
|
2021-05-10 08:47:31 +02:00
|
|
|
ret.id = inst.Definition<Id>();
|
2021-05-09 23:03:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-05-10 08:47:31 +02:00
|
|
|
Value RegAlloc::ConsumeInst(IR::Inst& inst) {
|
2021-05-18 00:24:09 +02:00
|
|
|
Unref(inst);
|
2021-05-10 08:47:31 +02:00
|
|
|
return PeekInst(inst);
|
2021-05-05 07:19:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 23:03:01 +02:00
|
|
|
Id RegAlloc::Alloc(bool is_long) {
|
|
|
|
size_t& num_regs{is_long ? num_used_long_registers : num_used_registers};
|
|
|
|
std::bitset<NUM_REGS>& use{is_long ? long_register_use : register_use};
|
|
|
|
if (num_used_registers + num_used_long_registers < NUM_REGS) {
|
|
|
|
for (size_t reg = 0; reg < NUM_REGS; ++reg) {
|
|
|
|
if (use[reg]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
num_regs = std::max(num_regs, reg + 1);
|
|
|
|
use[reg] = true;
|
|
|
|
Id ret{};
|
2021-05-25 07:22:21 +02:00
|
|
|
ret.is_valid.Assign(1);
|
2021-05-09 23:03:01 +02:00
|
|
|
ret.is_long.Assign(is_long ? 1 : 0);
|
|
|
|
ret.is_spill.Assign(0);
|
|
|
|
ret.is_condition_code.Assign(0);
|
2021-05-25 07:22:21 +02:00
|
|
|
ret.is_null.Assign(0);
|
|
|
|
ret.index.Assign(static_cast<u32>(reg));
|
2021-05-09 23:03:01 +02:00
|
|
|
return ret;
|
2021-05-05 07:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw NotImplementedException("Register spilling");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::Free(Id id) {
|
2021-05-25 07:22:21 +02:00
|
|
|
if (id.is_valid == 0) {
|
|
|
|
throw LogicError("Freeing invalid register");
|
|
|
|
}
|
2021-05-05 07:19:08 +02:00
|
|
|
if (id.is_spill != 0) {
|
|
|
|
throw NotImplementedException("Free spill");
|
|
|
|
}
|
2021-05-09 23:03:01 +02:00
|
|
|
if (id.is_long != 0) {
|
|
|
|
long_register_use[id.index] = false;
|
|
|
|
} else {
|
|
|
|
register_use[id.index] = false;
|
|
|
|
}
|
2021-05-05 07:19:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 00:24:09 +02:00
|
|
|
/*static*/ bool RegAlloc::IsAliased(const IR::Inst& inst) {
|
|
|
|
switch (inst.GetOpcode()) {
|
|
|
|
case IR::Opcode::Identity:
|
|
|
|
case IR::Opcode::BitCastU16F16:
|
|
|
|
case IR::Opcode::BitCastU32F32:
|
|
|
|
case IR::Opcode::BitCastU64F64:
|
|
|
|
case IR::Opcode::BitCastF16U16:
|
|
|
|
case IR::Opcode::BitCastF32U32:
|
|
|
|
case IR::Opcode::BitCastF64U64:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ IR::Inst& RegAlloc::AliasInst(IR::Inst& inst) {
|
|
|
|
IR::Inst* it{&inst};
|
|
|
|
while (IsAliased(*it)) {
|
|
|
|
const IR::Value arg{it->Arg(0)};
|
|
|
|
if (arg.IsImmediate()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it = arg.InstRecursive();
|
|
|
|
}
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
2021-05-05 07:19:08 +02:00
|
|
|
} // namespace Shader::Backend::GLASM
|