value.h 11 KB

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