reg_alloc.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <bitset>
  6. #include "common/bit_field.h"
  7. #include "common/common_types.h"
  8. namespace Shader::IR {
  9. class Inst;
  10. class Value;
  11. } // namespace Shader::IR
  12. namespace Shader::Backend::GLSL {
  13. enum class Type : u32 {
  14. U1,
  15. S32,
  16. U32,
  17. F32,
  18. S64,
  19. U64,
  20. F64,
  21. U32x2,
  22. F32x2,
  23. Void,
  24. };
  25. struct Id {
  26. union {
  27. u32 raw;
  28. BitField<0, 29, u32> index;
  29. BitField<29, 1, u32> is_long;
  30. BitField<30, 1, u32> is_spill;
  31. BitField<31, 1, u32> is_condition_code;
  32. };
  33. bool operator==(Id rhs) const noexcept {
  34. return raw == rhs.raw;
  35. }
  36. bool operator!=(Id rhs) const noexcept {
  37. return !operator==(rhs);
  38. }
  39. };
  40. static_assert(sizeof(Id) == sizeof(u32));
  41. class RegAlloc {
  42. public:
  43. std::string Define(IR::Inst& inst, Type type = Type::Void);
  44. std::string Consume(const IR::Value& value);
  45. /// Returns true if the instruction is expected to be aliased to another
  46. static bool IsAliased(const IR::Inst& inst);
  47. /// Returns the underlying value out of an alias sequence
  48. static IR::Inst& AliasInst(IR::Inst& inst);
  49. private:
  50. static constexpr size_t NUM_REGS = 4096;
  51. static constexpr size_t NUM_ELEMENTS = 4;
  52. std::string Consume(IR::Inst& inst);
  53. std::string GetType(Type type, u32 index);
  54. Id Alloc();
  55. void Free(Id id);
  56. size_t num_used_registers{};
  57. std::bitset<NUM_REGS> register_use{};
  58. std::bitset<NUM_REGS> register_defined{};
  59. };
  60. } // namespace Shader::Backend::GLSL