value.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <cstring>
  6. #include <memory>
  7. #include <type_traits>
  8. #include <utility>
  9. #include <vector>
  10. #include <boost/container/small_vector.hpp>
  11. #include <boost/intrusive/list.hpp>
  12. #include "common/assert.h"
  13. #include "common/bit_cast.h"
  14. #include "common/common_types.h"
  15. #include "shader_recompiler/exception.h"
  16. #include "shader_recompiler/frontend/ir/attribute.h"
  17. #include "shader_recompiler/frontend/ir/opcodes.h"
  18. #include "shader_recompiler/frontend/ir/patch.h"
  19. #include "shader_recompiler/frontend/ir/pred.h"
  20. #include "shader_recompiler/frontend/ir/reg.h"
  21. #include "shader_recompiler/frontend/ir/type.h"
  22. namespace Shader::IR {
  23. class Block;
  24. class Inst;
  25. struct AssociatedInsts;
  26. class Value {
  27. public:
  28. Value() noexcept = default;
  29. explicit Value(IR::Inst* value) noexcept;
  30. explicit Value(IR::Reg value) noexcept;
  31. explicit Value(IR::Pred value) noexcept;
  32. explicit Value(IR::Attribute value) noexcept;
  33. explicit Value(IR::Patch value) noexcept;
  34. explicit Value(bool value) noexcept;
  35. explicit Value(u8 value) noexcept;
  36. explicit Value(u16 value) noexcept;
  37. explicit Value(u32 value) noexcept;
  38. explicit Value(s32 value) noexcept;
  39. explicit Value(f32 value) noexcept;
  40. explicit Value(u64 value) noexcept;
  41. explicit Value(f64 value) noexcept;
  42. [[nodiscard]] bool IsIdentity() const noexcept;
  43. [[nodiscard]] bool IsPhi() const noexcept;
  44. [[nodiscard]] bool IsEmpty() const noexcept;
  45. [[nodiscard]] bool IsImmediate() const noexcept;
  46. [[nodiscard]] IR::Type Type() const noexcept;
  47. [[nodiscard]] IR::Inst* Inst() const;
  48. [[nodiscard]] IR::Inst* InstRecursive() const;
  49. [[nodiscard]] IR::Inst* TryInstRecursive() const;
  50. [[nodiscard]] IR::Value Resolve() const;
  51. [[nodiscard]] IR::Reg Reg() const;
  52. [[nodiscard]] IR::Pred Pred() const;
  53. [[nodiscard]] IR::Attribute Attribute() const;
  54. [[nodiscard]] IR::Patch Patch() const;
  55. [[nodiscard]] bool U1() const;
  56. [[nodiscard]] u8 U8() const;
  57. [[nodiscard]] u16 U16() const;
  58. [[nodiscard]] u32 U32() const;
  59. [[nodiscard]] s32 S32() const;
  60. [[nodiscard]] f32 F32() const;
  61. [[nodiscard]] u64 U64() const;
  62. [[nodiscard]] f64 F64() const;
  63. [[nodiscard]] bool operator==(const Value& other) const;
  64. [[nodiscard]] bool operator!=(const Value& other) const;
  65. private:
  66. IR::Type type{};
  67. union {
  68. IR::Inst* inst{};
  69. IR::Reg reg;
  70. IR::Pred pred;
  71. IR::Attribute attribute;
  72. IR::Patch patch;
  73. bool imm_u1;
  74. u8 imm_u8;
  75. u16 imm_u16;
  76. u32 imm_u32;
  77. s32 imm_s32;
  78. f32 imm_f32;
  79. u64 imm_u64;
  80. f64 imm_f64;
  81. };
  82. };
  83. static_assert(static_cast<u32>(IR::Type::Void) == 0, "memset relies on IR::Type being zero");
  84. static_assert(std::is_trivially_copyable_v<Value>);
  85. template <IR::Type type_>
  86. class TypedValue : public Value {
  87. public:
  88. TypedValue() = default;
  89. template <IR::Type other_type>
  90. requires((other_type & type_) != IR::Type::Void) explicit(false)
  91. TypedValue(const TypedValue<other_type>& value)
  92. : Value(value) {}
  93. explicit TypedValue(const Value& value) : Value(value) {
  94. if ((value.Type() & type_) == IR::Type::Void) {
  95. throw InvalidArgument("Incompatible types {} and {}", type_, value.Type());
  96. }
  97. }
  98. explicit TypedValue(IR::Inst* inst_) : TypedValue(Value(inst_)) {}
  99. };
  100. class Inst : public boost::intrusive::list_base_hook<> {
  101. public:
  102. explicit Inst(IR::Opcode op_, u32 flags_) noexcept;
  103. explicit Inst(const Inst& base);
  104. ~Inst();
  105. Inst& operator=(const Inst&) = delete;
  106. Inst& operator=(Inst&&) = delete;
  107. Inst(Inst&&) = delete;
  108. /// Get the number of uses this instruction has.
  109. [[nodiscard]] int UseCount() const noexcept {
  110. return use_count;
  111. }
  112. /// Determines whether this instruction has uses or not.
  113. [[nodiscard]] bool HasUses() const noexcept {
  114. return use_count > 0;
  115. }
  116. /// Get the opcode this microinstruction represents.
  117. [[nodiscard]] IR::Opcode GetOpcode() const noexcept {
  118. return op;
  119. }
  120. /// Determines if there is a pseudo-operation associated with this instruction.
  121. [[nodiscard]] bool HasAssociatedPseudoOperation() const noexcept {
  122. return associated_insts != nullptr;
  123. }
  124. /// Determines whether or not this instruction may have side effects.
  125. [[nodiscard]] bool MayHaveSideEffects() const noexcept;
  126. /// Determines whether or not this instruction is a pseudo-instruction.
  127. /// Pseudo-instructions depend on their parent instructions for their semantics.
  128. [[nodiscard]] bool IsPseudoInstruction() const noexcept;
  129. /// Determines if all arguments of this instruction are immediates.
  130. [[nodiscard]] bool AreAllArgsImmediates() const;
  131. /// Gets a pseudo-operation associated with this instruction
  132. [[nodiscard]] Inst* GetAssociatedPseudoOperation(IR::Opcode opcode);
  133. /// Get the type this instruction returns.
  134. [[nodiscard]] IR::Type Type() const;
  135. /// Get the number of arguments this instruction has.
  136. [[nodiscard]] size_t NumArgs() const {
  137. return op == IR::Opcode::Phi ? phi_args.size() : NumArgsOf(op);
  138. }
  139. /// Get the value of a given argument index.
  140. [[nodiscard]] Value Arg(size_t index) const noexcept {
  141. if (op == IR::Opcode::Phi) {
  142. return phi_args[index].second;
  143. } else {
  144. return args[index];
  145. }
  146. }
  147. /// Set the value of a given argument index.
  148. void SetArg(size_t index, Value value);
  149. /// Get a pointer to the block of a phi argument.
  150. [[nodiscard]] Block* PhiBlock(size_t index) const;
  151. /// Add phi operand to a phi instruction.
  152. void AddPhiOperand(Block* predecessor, const Value& value);
  153. /// Orders the Phi arguments from farthest away to nearest.
  154. void OrderPhiArgs();
  155. void Invalidate();
  156. void ClearArgs();
  157. void ReplaceUsesWith(Value replacement);
  158. void ReplaceOpcode(IR::Opcode opcode);
  159. template <typename FlagsType>
  160. requires(sizeof(FlagsType) <= sizeof(u32) && std::is_trivially_copyable_v<FlagsType>)
  161. [[nodiscard]] FlagsType Flags() const noexcept {
  162. FlagsType ret;
  163. std::memcpy(reinterpret_cast<char*>(&ret), &flags, sizeof(ret));
  164. return ret;
  165. }
  166. template <typename FlagsType>
  167. requires(sizeof(FlagsType) <= sizeof(u32) &&
  168. std::is_trivially_copyable_v<FlagsType>) void SetFlags(FlagsType value) noexcept {
  169. std::memcpy(&flags, &value, sizeof(value));
  170. }
  171. /// Intrusively store the host definition of this instruction.
  172. template <typename DefinitionType>
  173. void SetDefinition(DefinitionType def) {
  174. definition = Common::BitCast<u32>(def);
  175. }
  176. /// Return the intrusively stored host definition of this instruction.
  177. template <typename DefinitionType>
  178. [[nodiscard]] DefinitionType Definition() const noexcept {
  179. return Common::BitCast<DefinitionType>(definition);
  180. }
  181. /// Destructively remove one reference count from the instruction
  182. /// Useful for register allocation
  183. void DestructiveRemoveUsage() {
  184. --use_count;
  185. }
  186. /// Destructively add usages to the instruction
  187. /// Useful for register allocation
  188. void DestructiveAddUsage(int count) {
  189. use_count += count;
  190. }
  191. private:
  192. struct NonTriviallyDummy {
  193. NonTriviallyDummy() noexcept {}
  194. };
  195. void Use(const Value& value);
  196. void UndoUse(const Value& value);
  197. IR::Opcode op{};
  198. int use_count{};
  199. u32 flags{};
  200. u32 definition{};
  201. union {
  202. NonTriviallyDummy dummy{};
  203. boost::container::small_vector<std::pair<Block*, Value>, 2> phi_args;
  204. std::array<Value, 5> args;
  205. };
  206. std::unique_ptr<AssociatedInsts> associated_insts;
  207. };
  208. static_assert(sizeof(Inst) <= 128, "Inst size unintentionally increased");
  209. struct AssociatedInsts {
  210. union {
  211. Inst* in_bounds_inst;
  212. Inst* sparse_inst;
  213. Inst* zero_inst{};
  214. };
  215. Inst* sign_inst{};
  216. Inst* carry_inst{};
  217. Inst* overflow_inst{};
  218. };
  219. using U1 = TypedValue<Type::U1>;
  220. using U8 = TypedValue<Type::U8>;
  221. using U16 = TypedValue<Type::U16>;
  222. using U32 = TypedValue<Type::U32>;
  223. using U64 = TypedValue<Type::U64>;
  224. using S32 = TypedValue<Type::S32>;
  225. using F16 = TypedValue<Type::F16>;
  226. using F32 = TypedValue<Type::F32>;
  227. using F64 = TypedValue<Type::F64>;
  228. using U32U64 = TypedValue<Type::U32 | Type::U64>;
  229. using F32F64 = TypedValue<Type::F32 | Type::F64>;
  230. using U16U32U64 = TypedValue<Type::U16 | Type::U32 | Type::U64>;
  231. using F16F32F64 = TypedValue<Type::F16 | Type::F32 | Type::F64>;
  232. using UAny = TypedValue<Type::U8 | Type::U16 | Type::U32 | Type::U64>;
  233. inline bool Value::IsIdentity() const noexcept {
  234. return type == Type::Opaque && inst->GetOpcode() == Opcode::Identity;
  235. }
  236. inline bool Value::IsPhi() const noexcept {
  237. return type == Type::Opaque && inst->GetOpcode() == Opcode::Phi;
  238. }
  239. inline bool Value::IsEmpty() const noexcept {
  240. return type == Type::Void;
  241. }
  242. inline bool Value::IsImmediate() const noexcept {
  243. IR::Type current_type{type};
  244. const IR::Inst* current_inst{inst};
  245. while (current_type == Type::Opaque && current_inst->GetOpcode() == Opcode::Identity) {
  246. const Value& arg{current_inst->Arg(0)};
  247. current_type = arg.type;
  248. current_inst = arg.inst;
  249. }
  250. return current_type != Type::Opaque;
  251. }
  252. inline IR::Inst* Value::Inst() const {
  253. DEBUG_ASSERT(type == Type::Opaque);
  254. return inst;
  255. }
  256. inline IR::Inst* Value::InstRecursive() const {
  257. DEBUG_ASSERT(type == Type::Opaque);
  258. if (IsIdentity()) {
  259. return inst->Arg(0).InstRecursive();
  260. }
  261. return inst;
  262. }
  263. inline IR::Inst* Value::TryInstRecursive() const {
  264. if (IsIdentity()) {
  265. return inst->Arg(0).TryInstRecursive();
  266. }
  267. return type == Type::Opaque ? inst : nullptr;
  268. }
  269. inline IR::Value Value::Resolve() const {
  270. if (IsIdentity()) {
  271. return inst->Arg(0).Resolve();
  272. }
  273. return *this;
  274. }
  275. inline IR::Reg Value::Reg() const {
  276. DEBUG_ASSERT(type == Type::Reg);
  277. return reg;
  278. }
  279. inline IR::Pred Value::Pred() const {
  280. DEBUG_ASSERT(type == Type::Pred);
  281. return pred;
  282. }
  283. inline IR::Attribute Value::Attribute() const {
  284. DEBUG_ASSERT(type == Type::Attribute);
  285. return attribute;
  286. }
  287. inline IR::Patch Value::Patch() const {
  288. DEBUG_ASSERT(type == Type::Patch);
  289. return patch;
  290. }
  291. inline bool Value::U1() const {
  292. if (IsIdentity()) {
  293. return inst->Arg(0).U1();
  294. }
  295. DEBUG_ASSERT(type == Type::U1);
  296. return imm_u1;
  297. }
  298. inline u8 Value::U8() const {
  299. if (IsIdentity()) {
  300. return inst->Arg(0).U8();
  301. }
  302. DEBUG_ASSERT(type == Type::U8);
  303. return imm_u8;
  304. }
  305. inline u16 Value::U16() const {
  306. if (IsIdentity()) {
  307. return inst->Arg(0).U16();
  308. }
  309. DEBUG_ASSERT(type == Type::U16);
  310. return imm_u16;
  311. }
  312. inline u32 Value::U32() const {
  313. if (IsIdentity()) {
  314. return inst->Arg(0).U32();
  315. }
  316. DEBUG_ASSERT(type == Type::U32);
  317. return imm_u32;
  318. }
  319. inline s32 Value::S32() const {
  320. if (IsIdentity()) {
  321. return inst->Arg(0).S32();
  322. }
  323. DEBUG_ASSERT(type == Type::S32);
  324. return imm_s32;
  325. }
  326. inline f32 Value::F32() const {
  327. if (IsIdentity()) {
  328. return inst->Arg(0).F32();
  329. }
  330. DEBUG_ASSERT(type == Type::F32);
  331. return imm_f32;
  332. }
  333. inline u64 Value::U64() const {
  334. if (IsIdentity()) {
  335. return inst->Arg(0).U64();
  336. }
  337. DEBUG_ASSERT(type == Type::U64);
  338. return imm_u64;
  339. }
  340. inline f64 Value::F64() const {
  341. if (IsIdentity()) {
  342. return inst->Arg(0).F64();
  343. }
  344. DEBUG_ASSERT(type == Type::F64);
  345. return imm_f64;
  346. }
  347. [[nodiscard]] inline bool IsPhi(const Inst& inst) {
  348. return inst.GetOpcode() == Opcode::Phi;
  349. }
  350. } // namespace Shader::IR