summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/frontend/ir/value.cpp
blob: 1e7ffb86d56d6d9ec76b214b30965cae8eb2fb52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// 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"
#include "shader_recompiler/frontend/ir/opcodes.h"
#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} {}

Value::Value(f32 value) noexcept : type{Type::F32}, imm_f32{value} {}

Value::Value(u64 value) noexcept : type{Type::U64}, imm_u64{value} {}

Value::Value(f64 value) noexcept : type{Type::F64}, imm_f64{value} {}

bool Value::IsIdentity() const noexcept {
    return type == Type::Opaque && inst->GetOpcode() == Opcode::Identity;
}

bool Value::IsPhi() const noexcept {
    return type == Type::Opaque && inst->GetOpcode() == Opcode::Phi;
}

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 {
    if (IsPhi()) {
        // The type of a phi node is stored in its flags
        return inst->Flags<IR::Type>();
    }
    if (IsIdentity()) {
        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;
}

IR::Value Value::Resolve() const {
    if (IsIdentity()) {
        return inst->Arg(0).Resolve();
    }
    return *this;
}

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 {
    if (IsIdentity()) {
        return inst->Arg(0).U1();
    }
    ValidateAccess(Type::U1);
    return imm_u1;
}

u8 Value::U8() const {
    if (IsIdentity()) {
        return inst->Arg(0).U8();
    }
    ValidateAccess(Type::U8);
    return imm_u8;
}

u16 Value::U16() const {
    if (IsIdentity()) {
        return inst->Arg(0).U16();
    }
    ValidateAccess(Type::U16);
    return imm_u16;
}

u32 Value::U32() const {
    if (IsIdentity()) {
        return inst->Arg(0).U32();
    }
    ValidateAccess(Type::U32);
    return imm_u32;
}

f32 Value::F32() const {
    if (IsIdentity()) {
        return inst->Arg(0).F32();
    }
    ValidateAccess(Type::F32);
    return imm_f32;
}

u64 Value::U64() const {
    if (IsIdentity()) {
        return inst->Arg(0).U64();
    }
    ValidateAccess(Type::U64);
    return imm_u64;
}

f64 Value::F64() const {
    if (IsIdentity()) {
        return inst->Arg(0).F64();
    }
    ValidateAccess(Type::F64);
    return imm_f64;
}

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:
    case Type::F16:
        return imm_u16 == other.imm_u16;
    case Type::U32:
    case Type::F32:
        return imm_u32 == other.imm_u32;
    case Type::U64:
    case Type::F64:
        return imm_u64 == other.imm_u64;
    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;
    }
    throw LogicError("Invalid type {}", type);
}

bool Value::operator!=(const Value& other) const {
    return !operator==(other);
}

void Value::ValidateAccess(IR::Type expected) const {
    if (type != expected) {
        throw LogicError("Reading {} out of {}", expected, type);
    }
}

} // namespace Shader::IR