emit_glasm_integer.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/backend/glasm/emit_context.h"
  5. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  6. #include "shader_recompiler/frontend/ir/value.h"
  7. namespace Shader::Backend::GLASM {
  8. void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  9. const bool cc{inst.HasAssociatedPseudoOperation()};
  10. const std::string_view cc_mod{cc ? ".CC" : ""};
  11. if (cc) {
  12. ctx.reg_alloc.InvalidateConditionCodes();
  13. }
  14. const auto ret{ctx.reg_alloc.Define(inst)};
  15. ctx.Add("ADD.S{} {}.x,{},{};", cc_mod, ret, a, b);
  16. if (!cc) {
  17. return;
  18. }
  19. static constexpr std::array<std::string_view, 4> masks{"EQ", "SF", "CF", "OF"};
  20. const std::array flags{
  21. inst.GetAssociatedPseudoOperation(IR::Opcode::GetZeroFromOp),
  22. inst.GetAssociatedPseudoOperation(IR::Opcode::GetSignFromOp),
  23. inst.GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp),
  24. inst.GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp),
  25. };
  26. for (size_t i = 0; i < flags.size(); ++i) {
  27. if (flags[i]) {
  28. const auto flag_ret{ctx.reg_alloc.Define(*flags[i])};
  29. ctx.Add("MOV.S {},0;"
  30. "MOV.S {}({}.x),-1;",
  31. flag_ret, flag_ret, masks[i]);
  32. flags[i]->Invalidate();
  33. }
  34. }
  35. }
  36. void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, Register a, Register b) {
  37. ctx.LongAdd("ADD.S64 {}.x,{}.x,{}.x;", inst, a, b);
  38. }
  39. void EmitISub32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  40. ctx.Add("SUB.S {}.x,{},{};", inst, a, b);
  41. }
  42. void EmitISub64(EmitContext& ctx, IR::Inst& inst, Register a, Register b) {
  43. ctx.LongAdd("SUB.S64 {}.x,{}.x,{}.x;", inst, a, b);
  44. }
  45. void EmitIMul32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  46. ctx.Add("MUL.S {}.x,{},{};", inst, a, b);
  47. }
  48. void EmitINeg32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  49. ctx.Add("MOV.S {},-{};", inst, value);
  50. }
  51. void EmitINeg64(EmitContext& ctx, IR::Inst& inst, Register value) {
  52. ctx.LongAdd("MOV.S64 {},-{};", inst, value);
  53. }
  54. void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  55. ctx.Add("ABS.S {},{};", inst, value);
  56. }
  57. void EmitIAbs64(EmitContext& ctx, IR::Inst& inst, Register value) {
  58. ctx.LongAdd("MOV.S64 {},|{}|;", inst, value);
  59. }
  60. void EmitShiftLeftLogical32(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, ScalarU32 shift) {
  61. ctx.Add("SHL.U {}.x,{},{};", inst, base, shift);
  62. }
  63. void EmitShiftLeftLogical64(EmitContext& ctx, IR::Inst& inst, ScalarRegister base,
  64. ScalarU32 shift) {
  65. ctx.LongAdd("SHL.U64 {}.x,{},{};", inst, base, shift);
  66. }
  67. void EmitShiftRightLogical32(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, ScalarU32 shift) {
  68. ctx.Add("SHR.U {}.x,{},{};", inst, base, shift);
  69. }
  70. void EmitShiftRightLogical64(EmitContext& ctx, IR::Inst& inst, ScalarRegister base,
  71. ScalarU32 shift) {
  72. ctx.LongAdd("SHR.U64 {}.x,{},{};", inst, base, shift);
  73. }
  74. void EmitShiftRightArithmetic32(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, ScalarS32 shift) {
  75. ctx.Add("SHR.S {}.x,{},{};", inst, base, shift);
  76. }
  77. void EmitShiftRightArithmetic64(EmitContext& ctx, IR::Inst& inst, ScalarRegister base,
  78. ScalarS32 shift) {
  79. ctx.LongAdd("SHR.S64 {}.x,{},{};", inst, base, shift);
  80. }
  81. void EmitBitwiseAnd32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  82. ctx.Add("AND.S {}.x,{},{};", inst, a, b);
  83. }
  84. void EmitBitwiseOr32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  85. ctx.Add("OR.S {}.x,{},{};", inst, a, b);
  86. }
  87. void EmitBitwiseXor32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  88. ctx.Add("XOR.S {}.x,{},{};", inst, a, b);
  89. }
  90. void EmitBitFieldInsert(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, ScalarS32 insert,
  91. ScalarS32 offset, ScalarS32 count) {
  92. const Register ret{ctx.reg_alloc.Define(inst)};
  93. if (count.type != Type::Register && offset.type != Type::Register) {
  94. ctx.Add("BFI.S {},{{{},{},0,0}},{},{};", ret, count, offset, insert, base);
  95. } else {
  96. ctx.Add("MOV.S RC.x,{};"
  97. "MOV.S RC.y,{};"
  98. "BFI.S {},RC,{},{};",
  99. count, offset, ret, insert, base);
  100. }
  101. }
  102. void EmitBitFieldSExtract(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, ScalarS32 offset,
  103. ScalarS32 count) {
  104. const Register ret{ctx.reg_alloc.Define(inst)};
  105. if (count.type != Type::Register && offset.type != Type::Register) {
  106. ctx.Add("BFE.S {},{{{},{},0,0}},{};", ret, count, offset, base);
  107. } else {
  108. ctx.Add("MOV.S RC.x,{};"
  109. "MOV.S RC.y,{};"
  110. "BFE.S {},RC,{};",
  111. count, offset, ret, base);
  112. }
  113. }
  114. void EmitBitFieldUExtract(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, ScalarU32 offset,
  115. ScalarU32 count) {
  116. const Register ret{ctx.reg_alloc.Define(inst)};
  117. if (count.type != Type::Register && offset.type != Type::Register) {
  118. ctx.Add("BFE.U {},{{{},{},0,0}},{};", ret, count, offset, base);
  119. } else {
  120. ctx.Add("MOV.U RC.x,{};"
  121. "MOV.U RC.y,{};"
  122. "BFE.U {},RC,{};",
  123. count, offset, ret, base);
  124. }
  125. if (const auto zero = inst.GetAssociatedPseudoOperation(IR::Opcode::GetZeroFromOp)) {
  126. ctx.Add("SEQ.S {},{},0;", *zero, ret);
  127. zero->Invalidate();
  128. }
  129. if (const auto sign = inst.GetAssociatedPseudoOperation(IR::Opcode::GetSignFromOp)) {
  130. ctx.Add("SLT.S {},{},0;", *sign, ret);
  131. sign->Invalidate();
  132. }
  133. }
  134. void EmitBitReverse32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  135. ctx.Add("BFR {},{};", inst, value);
  136. }
  137. void EmitBitCount32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  138. ctx.Add("BTC {},{};", inst, value);
  139. }
  140. void EmitBitwiseNot32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  141. ctx.Add("NOT.S {},{};", inst, value);
  142. }
  143. void EmitFindSMsb32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  144. ctx.Add("BTFM.S {},{};", inst, value);
  145. }
  146. void EmitFindUMsb32(EmitContext& ctx, IR::Inst& inst, ScalarU32 value) {
  147. ctx.Add("BTFM.U {},{};", inst, value);
  148. }
  149. void EmitSMin32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  150. ctx.Add("MIN.S {},{},{};", inst, a, b);
  151. }
  152. void EmitUMin32(EmitContext& ctx, IR::Inst& inst, ScalarU32 a, ScalarU32 b) {
  153. ctx.Add("MIN.U {},{},{};", inst, a, b);
  154. }
  155. void EmitSMax32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  156. ctx.Add("MAX.S {},{},{};", inst, a, b);
  157. }
  158. void EmitUMax32(EmitContext& ctx, IR::Inst& inst, ScalarU32 a, ScalarU32 b) {
  159. ctx.Add("MAX.U {},{},{};", inst, a, b);
  160. }
  161. void EmitSClamp32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value, ScalarS32 min, ScalarS32 max) {
  162. const Register ret{ctx.reg_alloc.Define(inst)};
  163. ctx.Add("MIN.S RC.x,{},{};"
  164. "MAX.S {}.x,RC.x,{};",
  165. max, value, ret, min);
  166. }
  167. void EmitUClamp32(EmitContext& ctx, IR::Inst& inst, ScalarU32 value, ScalarU32 min, ScalarU32 max) {
  168. const Register ret{ctx.reg_alloc.Define(inst)};
  169. ctx.Add("MIN.U RC.x,{},{};"
  170. "MAX.U {}.x,RC.x,{};",
  171. max, value, ret, min);
  172. }
  173. void EmitSLessThan(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  174. ctx.Add("SLT.S {}.x,{},{};", inst, lhs, rhs);
  175. }
  176. void EmitULessThan(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  177. ctx.Add("SLT.U {}.x,{},{};", inst, lhs, rhs);
  178. }
  179. void EmitIEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  180. ctx.Add("SEQ.S {}.x,{},{};", inst, lhs, rhs);
  181. }
  182. void EmitSLessThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  183. ctx.Add("SLE.S {}.x,{},{};", inst, lhs, rhs);
  184. }
  185. void EmitULessThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  186. ctx.Add("SLE.U {}.x,{},{};", inst, lhs, rhs);
  187. }
  188. void EmitSGreaterThan(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  189. ctx.Add("SGT.S {}.x,{},{};", inst, lhs, rhs);
  190. }
  191. void EmitUGreaterThan(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  192. ctx.Add("SGT.U {}.x,{},{};", inst, lhs, rhs);
  193. }
  194. void EmitINotEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  195. ctx.Add("SNE.U {}.x,{},{};", inst, lhs, rhs);
  196. }
  197. void EmitSGreaterThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  198. ctx.Add("SGE.S {}.x,{},{};", inst, lhs, rhs);
  199. }
  200. void EmitUGreaterThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  201. ctx.Add("SGE.U {}.x,{},{};", inst, lhs, rhs);
  202. }
  203. } // namespace Shader::Backend::GLASM