2021-01-09 06:30:07 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
2021-02-03 19:43:04 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <type_traits>
|
2021-02-06 05:38:22 +00:00
|
|
|
#include <utility>
|
2021-02-03 00:07:00 +00:00
|
|
|
#include <vector>
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
#include <boost/intrusive/list.hpp>
|
|
|
|
|
2021-02-15 01:46:40 +00:00
|
|
|
#include "common/bit_cast.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "common/common_types.h"
|
2021-02-06 02:11:23 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/opcodes.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/type.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/value.h"
|
|
|
|
|
|
|
|
namespace Shader::IR {
|
|
|
|
|
2021-02-03 00:07:00 +00:00
|
|
|
class Block;
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
struct AssociatedInsts;
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
class Inst : public boost::intrusive::list_base_hook<> {
|
|
|
|
public:
|
2021-02-15 01:46:40 +00:00
|
|
|
explicit Inst(Opcode op_, u32 flags_) noexcept;
|
2021-02-06 05:38:22 +00:00
|
|
|
~Inst();
|
|
|
|
|
|
|
|
Inst& operator=(const Inst&) = delete;
|
|
|
|
Inst(const Inst&) = delete;
|
|
|
|
|
|
|
|
Inst& operator=(Inst&&) = delete;
|
|
|
|
Inst(Inst&&) = delete;
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
/// Get the number of uses this instruction has.
|
|
|
|
[[nodiscard]] int UseCount() const noexcept {
|
|
|
|
return use_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines whether this instruction has uses or not.
|
|
|
|
[[nodiscard]] bool HasUses() const noexcept {
|
|
|
|
return use_count > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the opcode this microinstruction represents.
|
2021-04-06 02:25:22 +00:00
|
|
|
[[nodiscard]] IR::Opcode GetOpcode() const noexcept {
|
2021-01-09 06:30:07 +00:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
/// Determines if there is a pseudo-operation associated with this instruction.
|
|
|
|
[[nodiscard]] bool HasAssociatedPseudoOperation() const noexcept {
|
|
|
|
return associated_insts != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-09 06:30:07 +00:00
|
|
|
/// Determines whether or not this instruction may have side effects.
|
|
|
|
[[nodiscard]] bool MayHaveSideEffects() const noexcept;
|
|
|
|
|
|
|
|
/// Determines whether or not this instruction is a pseudo-instruction.
|
|
|
|
/// Pseudo-instructions depend on their parent instructions for their semantics.
|
|
|
|
[[nodiscard]] bool IsPseudoInstruction() const noexcept;
|
|
|
|
|
2021-02-05 08:58:02 +00:00
|
|
|
/// Determines if all arguments of this instruction are immediates.
|
2021-02-06 05:38:22 +00:00
|
|
|
[[nodiscard]] bool AreAllArgsImmediates() const;
|
2021-02-05 08:58:02 +00:00
|
|
|
|
2021-01-09 06:30:07 +00:00
|
|
|
/// Gets a pseudo-operation associated with this instruction
|
|
|
|
[[nodiscard]] Inst* GetAssociatedPseudoOperation(IR::Opcode opcode);
|
|
|
|
|
|
|
|
/// Get the type this instruction returns.
|
|
|
|
[[nodiscard]] IR::Type Type() const;
|
|
|
|
|
2021-02-06 05:38:22 +00:00
|
|
|
/// Get the number of arguments this instruction has.
|
|
|
|
[[nodiscard]] size_t NumArgs() const;
|
|
|
|
|
2021-01-09 06:30:07 +00:00
|
|
|
/// Get the value of a given argument index.
|
|
|
|
[[nodiscard]] Value Arg(size_t index) const;
|
|
|
|
/// Set the value of a given argument index.
|
|
|
|
void SetArg(size_t index, Value value);
|
|
|
|
|
2021-02-06 05:38:22 +00:00
|
|
|
/// Get a pointer to the block of a phi argument.
|
|
|
|
[[nodiscard]] Block* PhiBlock(size_t index) const;
|
2021-02-03 00:07:00 +00:00
|
|
|
/// Add phi operand to a phi instruction.
|
|
|
|
void AddPhiOperand(Block* predecessor, const Value& value);
|
|
|
|
|
2021-01-09 06:30:07 +00:00
|
|
|
void Invalidate();
|
|
|
|
void ClearArgs();
|
|
|
|
|
|
|
|
void ReplaceUsesWith(Value replacement);
|
|
|
|
|
2021-02-19 21:10:18 +00:00
|
|
|
void ReplaceOpcode(IR::Opcode opcode);
|
|
|
|
|
2021-02-03 19:43:04 +00:00
|
|
|
template <typename FlagsType>
|
2021-02-15 01:46:40 +00:00
|
|
|
requires(sizeof(FlagsType) <= sizeof(u32) && std::is_trivially_copyable_v<FlagsType>)
|
2021-02-03 19:43:04 +00:00
|
|
|
[[nodiscard]] FlagsType Flags() const noexcept {
|
|
|
|
FlagsType ret;
|
2021-04-06 02:25:22 +00:00
|
|
|
std::memcpy(reinterpret_cast<char*>(&ret), &flags, sizeof(ret));
|
2021-02-03 19:43:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:02:04 +00:00
|
|
|
template <typename FlagsType>
|
|
|
|
requires(sizeof(FlagsType) <= sizeof(u32) && std::is_trivially_copyable_v<FlagsType>)
|
2021-03-26 19:46:07 +00:00
|
|
|
[[nodiscard]] void SetFlags(FlagsType value) noexcept {
|
|
|
|
std::memcpy(&flags, &value, sizeof(value));
|
2021-03-26 15:02:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 01:46:40 +00:00
|
|
|
/// Intrusively store the host definition of this instruction.
|
|
|
|
template <typename DefinitionType>
|
|
|
|
void SetDefinition(DefinitionType def) {
|
|
|
|
definition = Common::BitCast<u32>(def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the intrusively stored host definition of this instruction.
|
|
|
|
template <typename DefinitionType>
|
|
|
|
[[nodiscard]] DefinitionType Definition() const noexcept {
|
|
|
|
return Common::BitCast<DefinitionType>(definition);
|
|
|
|
}
|
|
|
|
|
2021-01-09 06:30:07 +00:00
|
|
|
private:
|
2021-02-06 05:38:22 +00:00
|
|
|
struct NonTriviallyDummy {
|
|
|
|
NonTriviallyDummy() noexcept {}
|
|
|
|
};
|
|
|
|
|
2021-01-09 06:30:07 +00:00
|
|
|
void Use(const Value& value);
|
|
|
|
void UndoUse(const Value& value);
|
|
|
|
|
|
|
|
IR::Opcode op{};
|
|
|
|
int use_count{};
|
2021-02-15 01:46:40 +00:00
|
|
|
u32 flags{};
|
|
|
|
u32 definition{};
|
2021-02-06 05:38:22 +00:00
|
|
|
union {
|
|
|
|
NonTriviallyDummy dummy{};
|
|
|
|
std::vector<std::pair<Block*, Value>> phi_args;
|
2021-03-08 21:31:53 +00:00
|
|
|
std::array<Value, 5> args;
|
|
|
|
};
|
|
|
|
std::unique_ptr<AssociatedInsts> associated_insts;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(Inst) <= 128, "Inst size unintentionally increased");
|
|
|
|
|
|
|
|
struct AssociatedInsts {
|
|
|
|
union {
|
2021-03-25 15:31:37 +00:00
|
|
|
Inst* in_bounds_inst;
|
2021-03-08 21:31:53 +00:00
|
|
|
Inst* sparse_inst;
|
|
|
|
Inst* zero_inst{};
|
2021-02-06 05:38:22 +00:00
|
|
|
};
|
2021-01-09 06:30:07 +00:00
|
|
|
Inst* sign_inst{};
|
|
|
|
Inst* carry_inst{};
|
|
|
|
Inst* overflow_inst{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Shader::IR
|