microinstruction.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/exception.h"
  5. #include "shader_recompiler/frontend/ir/microinstruction.h"
  6. #include "shader_recompiler/frontend/ir/type.h"
  7. namespace Shader::IR {
  8. static void CheckPseudoInstruction(IR::Inst* inst, IR::Opcode opcode) {
  9. if (inst && inst->Opcode() != opcode) {
  10. throw LogicError("Invalid pseudo-instruction");
  11. }
  12. }
  13. static void SetPseudoInstruction(IR::Inst*& dest_inst, IR::Inst* pseudo_inst) {
  14. if (dest_inst) {
  15. throw LogicError("Only one of each type of pseudo-op allowed");
  16. }
  17. dest_inst = pseudo_inst;
  18. }
  19. static void RemovePseudoInstruction(IR::Inst*& inst, IR::Opcode expected_opcode) {
  20. if (inst->Opcode() != expected_opcode) {
  21. throw LogicError("Undoing use of invalid pseudo-op");
  22. }
  23. inst = nullptr;
  24. }
  25. bool Inst::MayHaveSideEffects() const noexcept {
  26. switch (op) {
  27. case Opcode::Branch:
  28. case Opcode::BranchConditional:
  29. case Opcode::Exit:
  30. case Opcode::Return:
  31. case Opcode::Unreachable:
  32. case Opcode::SetAttribute:
  33. case Opcode::SetAttributeIndexed:
  34. case Opcode::WriteGlobalU8:
  35. case Opcode::WriteGlobalS8:
  36. case Opcode::WriteGlobalU16:
  37. case Opcode::WriteGlobalS16:
  38. case Opcode::WriteGlobal32:
  39. case Opcode::WriteGlobal64:
  40. case Opcode::WriteGlobal128:
  41. return true;
  42. default:
  43. return false;
  44. }
  45. }
  46. bool Inst::IsPseudoInstruction() const noexcept {
  47. switch (op) {
  48. case Opcode::GetZeroFromOp:
  49. case Opcode::GetSignFromOp:
  50. case Opcode::GetCarryFromOp:
  51. case Opcode::GetOverflowFromOp:
  52. case Opcode::GetZSCOFromOp:
  53. return true;
  54. default:
  55. return false;
  56. }
  57. }
  58. bool Inst::HasAssociatedPseudoOperation() const noexcept {
  59. return zero_inst || sign_inst || carry_inst || overflow_inst || zsco_inst;
  60. }
  61. Inst* Inst::GetAssociatedPseudoOperation(IR::Opcode opcode) {
  62. // This is faster than doing a search through the block.
  63. switch (opcode) {
  64. case Opcode::GetZeroFromOp:
  65. CheckPseudoInstruction(zero_inst, Opcode::GetZeroFromOp);
  66. return zero_inst;
  67. case Opcode::GetSignFromOp:
  68. CheckPseudoInstruction(sign_inst, Opcode::GetSignFromOp);
  69. return sign_inst;
  70. case Opcode::GetCarryFromOp:
  71. CheckPseudoInstruction(carry_inst, Opcode::GetCarryFromOp);
  72. return carry_inst;
  73. case Opcode::GetOverflowFromOp:
  74. CheckPseudoInstruction(overflow_inst, Opcode::GetOverflowFromOp);
  75. return overflow_inst;
  76. case Opcode::GetZSCOFromOp:
  77. CheckPseudoInstruction(zsco_inst, Opcode::GetZSCOFromOp);
  78. return zsco_inst;
  79. default:
  80. throw InvalidArgument("{} is not a pseudo-instruction", opcode);
  81. }
  82. }
  83. size_t Inst::NumArgs() const {
  84. return NumArgsOf(op);
  85. }
  86. IR::Type Inst::Type() const {
  87. return TypeOf(op);
  88. }
  89. Value Inst::Arg(size_t index) const {
  90. if (index >= NumArgsOf(op)) {
  91. throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op);
  92. }
  93. return args[index];
  94. }
  95. void Inst::SetArg(size_t index, Value value) {
  96. if (index >= NumArgsOf(op)) {
  97. throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op);
  98. }
  99. if (!args[index].IsImmediate()) {
  100. UndoUse(args[index]);
  101. }
  102. if (!value.IsImmediate()) {
  103. Use(value);
  104. }
  105. args[index] = value;
  106. }
  107. std::span<const std::pair<Block*, Value>> Inst::PhiOperands() const noexcept {
  108. return phi_operands;
  109. }
  110. void Inst::AddPhiOperand(Block* predecessor, const Value& value) {
  111. if (!value.IsImmediate()) {
  112. Use(value);
  113. }
  114. phi_operands.emplace_back(predecessor, value);
  115. }
  116. void Inst::Invalidate() {
  117. ClearArgs();
  118. op = Opcode::Void;
  119. }
  120. void Inst::ClearArgs() {
  121. for (auto& value : args) {
  122. if (!value.IsImmediate()) {
  123. UndoUse(value);
  124. }
  125. value = {};
  126. }
  127. for (auto& [phi_block, phi_op] : phi_operands) {
  128. if (!phi_op.IsImmediate()) {
  129. UndoUse(phi_op);
  130. }
  131. }
  132. phi_operands.clear();
  133. }
  134. void Inst::ReplaceUsesWith(Value replacement) {
  135. Invalidate();
  136. op = Opcode::Identity;
  137. if (!replacement.IsImmediate()) {
  138. Use(replacement);
  139. }
  140. args[0] = replacement;
  141. }
  142. void Inst::Use(const Value& value) {
  143. ++value.Inst()->use_count;
  144. switch (op) {
  145. case Opcode::GetZeroFromOp:
  146. SetPseudoInstruction(value.Inst()->zero_inst, this);
  147. break;
  148. case Opcode::GetSignFromOp:
  149. SetPseudoInstruction(value.Inst()->sign_inst, this);
  150. break;
  151. case Opcode::GetCarryFromOp:
  152. SetPseudoInstruction(value.Inst()->carry_inst, this);
  153. break;
  154. case Opcode::GetOverflowFromOp:
  155. SetPseudoInstruction(value.Inst()->overflow_inst, this);
  156. break;
  157. case Opcode::GetZSCOFromOp:
  158. SetPseudoInstruction(value.Inst()->zsco_inst, this);
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. void Inst::UndoUse(const Value& value) {
  165. --value.Inst()->use_count;
  166. switch (op) {
  167. case Opcode::GetZeroFromOp:
  168. RemovePseudoInstruction(value.Inst()->zero_inst, Opcode::GetZeroFromOp);
  169. break;
  170. case Opcode::GetSignFromOp:
  171. RemovePseudoInstruction(value.Inst()->sign_inst, Opcode::GetSignFromOp);
  172. break;
  173. case Opcode::GetCarryFromOp:
  174. RemovePseudoInstruction(value.Inst()->carry_inst, Opcode::GetCarryFromOp);
  175. break;
  176. case Opcode::GetOverflowFromOp:
  177. RemovePseudoInstruction(value.Inst()->overflow_inst, Opcode::GetOverflowFromOp);
  178. break;
  179. case Opcode::GetZSCOFromOp:
  180. RemovePseudoInstruction(value.Inst()->zsco_inst, Opcode::GetZSCOFromOp);
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. } // namespace Shader::IR