reg_alloc.cpp 4.6 KB

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