basic_block.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <initializer_list>
  6. #include <map>
  7. #include <boost/intrusive/list.hpp>
  8. #include <boost/pool/pool_alloc.hpp>
  9. #include "shader_recompiler/frontend/ir/microinstruction.h"
  10. namespace Shader::IR {
  11. class Block {
  12. public:
  13. using InstructionList = boost::intrusive::list<Inst>;
  14. using size_type = InstructionList::size_type;
  15. using iterator = InstructionList::iterator;
  16. using const_iterator = InstructionList::const_iterator;
  17. using reverse_iterator = InstructionList::reverse_iterator;
  18. using const_reverse_iterator = InstructionList::const_reverse_iterator;
  19. explicit Block(u32 begin, u32 end);
  20. ~Block();
  21. Block(const Block&) = delete;
  22. Block& operator=(const Block&) = delete;
  23. Block(Block&&) = default;
  24. Block& operator=(Block&&) = default;
  25. /// Appends a new instruction to the end of this basic block.
  26. void AppendNewInst(Opcode op, std::initializer_list<Value> args);
  27. /// Prepends a new instruction to this basic block before the insertion point.
  28. iterator PrependNewInst(iterator insertion_point, Opcode op, std::initializer_list<Value> args);
  29. /// Gets the starting location of this basic block.
  30. [[nodiscard]] u32 LocationBegin() const noexcept;
  31. /// Gets the end location for this basic block.
  32. [[nodiscard]] u32 LocationEnd() const noexcept;
  33. /// Gets a mutable reference to the instruction list for this basic block.
  34. InstructionList& Instructions() noexcept;
  35. /// Gets an immutable reference to the instruction list for this basic block.
  36. const InstructionList& Instructions() const noexcept;
  37. [[nodiscard]] bool empty() const {
  38. return instructions.empty();
  39. }
  40. [[nodiscard]] size_type size() const {
  41. return instructions.size();
  42. }
  43. [[nodiscard]] Inst& front() {
  44. return instructions.front();
  45. }
  46. [[nodiscard]] const Inst& front() const {
  47. return instructions.front();
  48. }
  49. [[nodiscard]] Inst& back() {
  50. return instructions.back();
  51. }
  52. [[nodiscard]] const Inst& back() const {
  53. return instructions.back();
  54. }
  55. [[nodiscard]] iterator begin() {
  56. return instructions.begin();
  57. }
  58. [[nodiscard]] const_iterator begin() const {
  59. return instructions.begin();
  60. }
  61. [[nodiscard]] iterator end() {
  62. return instructions.end();
  63. }
  64. [[nodiscard]] const_iterator end() const {
  65. return instructions.end();
  66. }
  67. [[nodiscard]] reverse_iterator rbegin() {
  68. return instructions.rbegin();
  69. }
  70. [[nodiscard]] const_reverse_iterator rbegin() const {
  71. return instructions.rbegin();
  72. }
  73. [[nodiscard]] reverse_iterator rend() {
  74. return instructions.rend();
  75. }
  76. [[nodiscard]] const_reverse_iterator rend() const {
  77. return instructions.rend();
  78. }
  79. [[nodiscard]] const_iterator cbegin() const {
  80. return instructions.cbegin();
  81. }
  82. [[nodiscard]] const_iterator cend() const {
  83. return instructions.cend();
  84. }
  85. [[nodiscard]] const_reverse_iterator crbegin() const {
  86. return instructions.crbegin();
  87. }
  88. [[nodiscard]] const_reverse_iterator crend() const {
  89. return instructions.crend();
  90. }
  91. private:
  92. /// Starting location of this block
  93. u32 location_begin;
  94. /// End location of this block
  95. u32 location_end;
  96. /// List of instructions in this block.
  97. InstructionList instructions;
  98. /// Memory pool for instruction list
  99. boost::fast_pool_allocator<Inst, boost::default_user_allocator_malloc_free,
  100. boost::details::pool::null_mutex>
  101. instruction_alloc_pool;
  102. };
  103. [[nodiscard]] std::string DumpBlock(const Block& block);
  104. [[nodiscard]] std::string DumpBlock(const Block& block,
  105. const std::map<const Block*, size_t>& block_to_index,
  106. std::map<const Inst*, size_t>& inst_to_index,
  107. size_t& inst_index);
  108. } // namespace Shader::IR