2021-01-09 01:30:07 -05:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "shader_recompiler/frontend/ir/microinstruction.h"
|
2021-02-05 21:11:23 -05:00
|
|
|
#include "shader_recompiler/frontend/ir/opcodes.h"
|
2021-01-09 01:30:07 -05:00
|
|
|
#include "shader_recompiler/frontend/ir/value.h"
|
|
|
|
|
|
|
|
namespace Shader::IR {
|
|
|
|
|
|
|
|
Value::Value(IR::Inst* value) noexcept : type{Type::Opaque}, inst{value} {}
|
|
|
|
|
|
|
|
Value::Value(IR::Block* value) noexcept : type{Type::Label}, label{value} {}
|
|
|
|
|
|
|
|
Value::Value(IR::Reg value) noexcept : type{Type::Reg}, reg{value} {}
|
|
|
|
|
|
|
|
Value::Value(IR::Pred value) noexcept : type{Type::Pred}, pred{value} {}
|
|
|
|
|
|
|
|
Value::Value(IR::Attribute value) noexcept : type{Type::Attribute}, attribute{value} {}
|
|
|
|
|
|
|
|
Value::Value(bool value) noexcept : type{Type::U1}, imm_u1{value} {}
|
|
|
|
|
|
|
|
Value::Value(u8 value) noexcept : type{Type::U8}, imm_u8{value} {}
|
|
|
|
|
|
|
|
Value::Value(u16 value) noexcept : type{Type::U16}, imm_u16{value} {}
|
|
|
|
|
|
|
|
Value::Value(u32 value) noexcept : type{Type::U32}, imm_u32{value} {}
|
|
|
|
|
2021-02-05 17:19:36 -05:00
|
|
|
Value::Value(f32 value) noexcept : type{Type::F32}, imm_f32{value} {}
|
|
|
|
|
2021-01-09 01:30:07 -05:00
|
|
|
Value::Value(u64 value) noexcept : type{Type::U64}, imm_u64{value} {}
|
|
|
|
|
2021-02-05 17:19:36 -05:00
|
|
|
Value::Value(f64 value) noexcept : type{Type::F64}, imm_f64{value} {}
|
|
|
|
|
2021-01-09 01:30:07 -05:00
|
|
|
bool Value::IsIdentity() const noexcept {
|
2021-04-05 22:25:22 -04:00
|
|
|
return type == Type::Opaque && inst->GetOpcode() == Opcode::Identity;
|
2021-01-09 01:30:07 -05:00
|
|
|
}
|
|
|
|
|
2021-02-24 03:21:30 -05:00
|
|
|
bool Value::IsPhi() const noexcept {
|
2021-04-05 22:25:22 -04:00
|
|
|
return type == Type::Opaque && inst->GetOpcode() == Opcode::Phi;
|
2021-02-24 03:21:30 -05:00
|
|
|
}
|
|
|
|
|
2021-01-09 01:30:07 -05:00
|
|
|
bool Value::IsEmpty() const noexcept {
|
|
|
|
return type == Type::Void;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::IsImmediate() const noexcept {
|
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).IsImmediate();
|
|
|
|
}
|
|
|
|
return type != Type::Opaque;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::IsLabel() const noexcept {
|
|
|
|
return type == Type::Label;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Type Value::Type() const noexcept {
|
2021-03-30 02:19:50 -04:00
|
|
|
if (IsPhi()) {
|
|
|
|
// The type of a phi node is stored in its flags
|
|
|
|
return inst->Flags<IR::Type>();
|
|
|
|
}
|
|
|
|
if (IsIdentity()) {
|
2021-01-09 01:30:07 -05:00
|
|
|
return inst->Arg(0).Type();
|
|
|
|
}
|
|
|
|
if (type == Type::Opaque) {
|
|
|
|
return inst->Type();
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Inst* Value::Inst() const {
|
|
|
|
ValidateAccess(Type::Opaque);
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Block* Value::Label() const {
|
|
|
|
ValidateAccess(Type::Label);
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Inst* Value::InstRecursive() const {
|
|
|
|
ValidateAccess(Type::Opaque);
|
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).InstRecursive();
|
|
|
|
}
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2021-02-16 17:49:24 -05:00
|
|
|
IR::Value Value::Resolve() const {
|
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).Resolve();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-09 01:30:07 -05:00
|
|
|
IR::Reg Value::Reg() const {
|
|
|
|
ValidateAccess(Type::Reg);
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Pred Value::Pred() const {
|
|
|
|
ValidateAccess(Type::Pred);
|
|
|
|
return pred;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::Attribute Value::Attribute() const {
|
|
|
|
ValidateAccess(Type::Attribute);
|
|
|
|
return attribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::U1() const {
|
2021-02-05 03:58:02 -05:00
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).U1();
|
|
|
|
}
|
2021-01-09 01:30:07 -05:00
|
|
|
ValidateAccess(Type::U1);
|
|
|
|
return imm_u1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 Value::U8() const {
|
2021-02-05 03:58:02 -05:00
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).U8();
|
|
|
|
}
|
2021-01-09 01:30:07 -05:00
|
|
|
ValidateAccess(Type::U8);
|
|
|
|
return imm_u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 Value::U16() const {
|
2021-02-05 03:58:02 -05:00
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).U16();
|
|
|
|
}
|
2021-01-09 01:30:07 -05:00
|
|
|
ValidateAccess(Type::U16);
|
|
|
|
return imm_u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 Value::U32() const {
|
2021-02-05 03:58:02 -05:00
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).U32();
|
|
|
|
}
|
2021-01-09 01:30:07 -05:00
|
|
|
ValidateAccess(Type::U32);
|
|
|
|
return imm_u32;
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:19:36 -05:00
|
|
|
f32 Value::F32() const {
|
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).F32();
|
|
|
|
}
|
|
|
|
ValidateAccess(Type::F32);
|
|
|
|
return imm_f32;
|
|
|
|
}
|
|
|
|
|
2021-01-09 01:30:07 -05:00
|
|
|
u64 Value::U64() const {
|
2021-02-05 03:58:02 -05:00
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).U64();
|
|
|
|
}
|
2021-01-09 01:30:07 -05:00
|
|
|
ValidateAccess(Type::U64);
|
|
|
|
return imm_u64;
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:53:57 -04:00
|
|
|
f64 Value::F64() const {
|
|
|
|
if (IsIdentity()) {
|
|
|
|
return inst->Arg(0).F64();
|
|
|
|
}
|
|
|
|
ValidateAccess(Type::F64);
|
|
|
|
return imm_f64;
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:07:00 -05:00
|
|
|
bool Value::operator==(const Value& other) const {
|
|
|
|
if (type != other.type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case Type::Void:
|
|
|
|
return true;
|
|
|
|
case Type::Opaque:
|
|
|
|
return inst == other.inst;
|
|
|
|
case Type::Label:
|
|
|
|
return label == other.label;
|
|
|
|
case Type::Reg:
|
|
|
|
return reg == other.reg;
|
|
|
|
case Type::Pred:
|
|
|
|
return pred == other.pred;
|
|
|
|
case Type::Attribute:
|
|
|
|
return attribute == other.attribute;
|
|
|
|
case Type::U1:
|
|
|
|
return imm_u1 == other.imm_u1;
|
|
|
|
case Type::U8:
|
|
|
|
return imm_u8 == other.imm_u8;
|
|
|
|
case Type::U16:
|
2021-02-05 17:19:36 -05:00
|
|
|
case Type::F16:
|
2021-02-02 19:07:00 -05:00
|
|
|
return imm_u16 == other.imm_u16;
|
|
|
|
case Type::U32:
|
2021-02-05 17:19:36 -05:00
|
|
|
case Type::F32:
|
2021-02-02 19:07:00 -05:00
|
|
|
return imm_u32 == other.imm_u32;
|
|
|
|
case Type::U64:
|
2021-02-05 17:19:36 -05:00
|
|
|
case Type::F64:
|
2021-02-02 19:07:00 -05:00
|
|
|
return imm_u64 == other.imm_u64;
|
2021-02-05 17:19:36 -05:00
|
|
|
case Type::U32x2:
|
|
|
|
case Type::U32x3:
|
|
|
|
case Type::U32x4:
|
|
|
|
case Type::F16x2:
|
|
|
|
case Type::F16x3:
|
|
|
|
case Type::F16x4:
|
|
|
|
case Type::F32x2:
|
|
|
|
case Type::F32x3:
|
|
|
|
case Type::F32x4:
|
|
|
|
case Type::F64x2:
|
|
|
|
case Type::F64x3:
|
|
|
|
case Type::F64x4:
|
|
|
|
break;
|
2021-02-02 19:07:00 -05:00
|
|
|
}
|
|
|
|
throw LogicError("Invalid type {}", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(const Value& other) const {
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
|
2021-01-09 01:30:07 -05:00
|
|
|
void Value::ValidateAccess(IR::Type expected) const {
|
|
|
|
if (type != expected) {
|
|
|
|
throw LogicError("Reading {} out of {}", expected, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::IR
|