reg_alloc.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string>
  5. #include <fmt/format.h>
  6. #include "shader_recompiler/backend/glasm/emit_context.h"
  7. #include "shader_recompiler/backend/glasm/reg_alloc.h"
  8. #include "shader_recompiler/exception.h"
  9. #include "shader_recompiler/frontend/ir/value.h"
  10. namespace Shader::Backend::GLASM {
  11. Register RegAlloc::Define(IR::Inst& inst) {
  12. return Define(inst, false);
  13. }
  14. Register RegAlloc::LongDefine(IR::Inst& inst) {
  15. return Define(inst, true);
  16. }
  17. Value RegAlloc::Peek(const IR::Value& value) {
  18. if (value.IsImmediate()) {
  19. return MakeImm(value);
  20. } else {
  21. return PeekInst(*value.Inst());
  22. }
  23. }
  24. Value RegAlloc::Consume(const IR::Value& value) {
  25. if (value.IsImmediate()) {
  26. return MakeImm(value);
  27. } else {
  28. return ConsumeInst(*value.Inst());
  29. }
  30. }
  31. void RegAlloc::Unref(IR::Inst& inst) {
  32. IR::Inst& value_inst{AliasInst(inst)};
  33. value_inst.DestructiveRemoveUsage();
  34. if (!value_inst.HasUses()) {
  35. Free(value_inst.Definition<Id>());
  36. }
  37. }
  38. Register RegAlloc::AllocReg() {
  39. Register ret;
  40. ret.type = Type::Register;
  41. ret.id = Alloc(false);
  42. return ret;
  43. }
  44. Register RegAlloc::AllocLongReg() {
  45. Register ret;
  46. ret.type = Type::Register;
  47. ret.id = Alloc(true);
  48. return ret;
  49. }
  50. void RegAlloc::FreeReg(Register reg) {
  51. Free(reg.id);
  52. }
  53. Value RegAlloc::MakeImm(const IR::Value& value) {
  54. Value ret;
  55. switch (value.Type()) {
  56. case IR::Type::Void:
  57. ret.type = Type::Void;
  58. break;
  59. case IR::Type::U1:
  60. ret.type = Type::U32;
  61. ret.imm_u32 = value.U1() ? 0xffffffff : 0;
  62. break;
  63. case IR::Type::U32:
  64. ret.type = Type::U32;
  65. ret.imm_u32 = value.U32();
  66. break;
  67. case IR::Type::F32:
  68. ret.type = Type::U32;
  69. ret.imm_u32 = Common::BitCast<u32>(value.F32());
  70. break;
  71. case IR::Type::U64:
  72. ret.type = Type::U64;
  73. ret.imm_u64 = value.U64();
  74. break;
  75. case IR::Type::F64:
  76. ret.type = Type::U64;
  77. ret.imm_u64 = Common::BitCast<u64>(value.F64());
  78. break;
  79. default:
  80. throw NotImplementedException("Immediate type {}", value.Type());
  81. }
  82. return ret;
  83. }
  84. Register RegAlloc::Define(IR::Inst& inst, bool is_long) {
  85. if (inst.HasUses()) {
  86. inst.SetDefinition<Id>(Alloc(is_long));
  87. } else {
  88. Id id{};
  89. id.is_long.Assign(is_long ? 1 : 0);
  90. id.is_null.Assign(1);
  91. inst.SetDefinition<Id>(id);
  92. }
  93. return Register{PeekInst(inst)};
  94. }
  95. Value RegAlloc::PeekInst(IR::Inst& inst) {
  96. Value ret;
  97. ret.type = Type::Register;
  98. ret.id = inst.Definition<Id>();
  99. return ret;
  100. }
  101. Value RegAlloc::ConsumeInst(IR::Inst& inst) {
  102. Unref(inst);
  103. return PeekInst(inst);
  104. }
  105. Id RegAlloc::Alloc(bool is_long) {
  106. size_t& num_regs{is_long ? num_used_long_registers : num_used_registers};
  107. std::bitset<NUM_REGS>& use{is_long ? long_register_use : register_use};
  108. if (num_used_registers + num_used_long_registers < NUM_REGS) {
  109. for (size_t reg = 0; reg < NUM_REGS; ++reg) {
  110. if (use[reg]) {
  111. continue;
  112. }
  113. num_regs = std::max(num_regs, reg + 1);
  114. use[reg] = true;
  115. Id ret{};
  116. ret.is_valid.Assign(1);
  117. ret.is_long.Assign(is_long ? 1 : 0);
  118. ret.is_spill.Assign(0);
  119. ret.is_condition_code.Assign(0);
  120. ret.is_null.Assign(0);
  121. ret.index.Assign(static_cast<u32>(reg));
  122. return ret;
  123. }
  124. }
  125. throw NotImplementedException("Register spilling");
  126. }
  127. void RegAlloc::Free(Id id) {
  128. if (id.is_valid == 0) {
  129. throw LogicError("Freeing invalid register");
  130. }
  131. if (id.is_spill != 0) {
  132. throw NotImplementedException("Free spill");
  133. }
  134. if (id.is_long != 0) {
  135. long_register_use[id.index] = false;
  136. } else {
  137. register_use[id.index] = false;
  138. }
  139. }
  140. /*static*/ bool RegAlloc::IsAliased(const IR::Inst& inst) {
  141. switch (inst.GetOpcode()) {
  142. case IR::Opcode::Identity:
  143. case IR::Opcode::BitCastU16F16:
  144. case IR::Opcode::BitCastU32F32:
  145. case IR::Opcode::BitCastU64F64:
  146. case IR::Opcode::BitCastF16U16:
  147. case IR::Opcode::BitCastF32U32:
  148. case IR::Opcode::BitCastF64U64:
  149. return true;
  150. default:
  151. return false;
  152. }
  153. }
  154. /*static*/ IR::Inst& RegAlloc::AliasInst(IR::Inst& inst) {
  155. IR::Inst* it{&inst};
  156. while (IsAliased(*it)) {
  157. const IR::Value arg{it->Arg(0)};
  158. if (arg.IsImmediate()) {
  159. break;
  160. }
  161. it = arg.InstRecursive();
  162. }
  163. return *it;
  164. }
  165. } // namespace Shader::Backend::GLASM