reg_alloc.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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);
  44. std::string Define(IR::Inst& inst, Type type);
  45. std::string Consume(const IR::Value& value);
  46. /// Returns true if the instruction is expected to be aliased to another
  47. static bool IsAliased(const IR::Inst& inst);
  48. /// Returns the underlying value out of an alias sequence
  49. static IR::Inst& AliasInst(IR::Inst& inst);
  50. private:
  51. static constexpr size_t NUM_REGS = 4096;
  52. static constexpr size_t NUM_ELEMENTS = 4;
  53. std::string Consume(IR::Inst& inst);
  54. std::string GetType(Type type, u32 index);
  55. Id Alloc();
  56. void Free(Id id);
  57. size_t num_used_registers{};
  58. std::bitset<NUM_REGS> register_use{};
  59. std::bitset<NUM_REGS> register_defined{};
  60. };
  61. } // namespace Shader::Backend::GLSL