spirv_cfg.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2016-2021 Arm Limited
  3. * SPDX-License-Identifier: Apache-2.0 OR MIT
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * At your option, you may choose to accept this material under either:
  19. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  20. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  21. */
  22. #ifndef SPIRV_CROSS_CFG_HPP
  23. #define SPIRV_CROSS_CFG_HPP
  24. #include "spirv_common.hpp"
  25. #include <assert.h>
  26. namespace SPIRV_CROSS_NAMESPACE
  27. {
  28. class Compiler;
  29. class CFG
  30. {
  31. public:
  32. CFG(Compiler &compiler, const SPIRFunction &function);
  33. Compiler &get_compiler()
  34. {
  35. return compiler;
  36. }
  37. const Compiler &get_compiler() const
  38. {
  39. return compiler;
  40. }
  41. const SPIRFunction &get_function() const
  42. {
  43. return func;
  44. }
  45. uint32_t get_immediate_dominator(uint32_t block) const
  46. {
  47. auto itr = immediate_dominators.find(block);
  48. if (itr != std::end(immediate_dominators))
  49. return itr->second;
  50. else
  51. return 0;
  52. }
  53. bool is_reachable(uint32_t block) const
  54. {
  55. return visit_order.count(block) != 0;
  56. }
  57. uint32_t get_visit_order(uint32_t block) const
  58. {
  59. auto itr = visit_order.find(block);
  60. assert(itr != std::end(visit_order));
  61. int v = itr->second.get();
  62. assert(v > 0);
  63. return uint32_t(v);
  64. }
  65. uint32_t find_common_dominator(uint32_t a, uint32_t b) const;
  66. const SmallVector<uint32_t> &get_preceding_edges(uint32_t block) const
  67. {
  68. auto itr = preceding_edges.find(block);
  69. if (itr != std::end(preceding_edges))
  70. return itr->second;
  71. else
  72. return empty_vector;
  73. }
  74. const SmallVector<uint32_t> &get_succeeding_edges(uint32_t block) const
  75. {
  76. auto itr = succeeding_edges.find(block);
  77. if (itr != std::end(succeeding_edges))
  78. return itr->second;
  79. else
  80. return empty_vector;
  81. }
  82. template <typename Op>
  83. void walk_from(std::unordered_set<uint32_t> &seen_blocks, uint32_t block, const Op &op) const
  84. {
  85. if (seen_blocks.count(block))
  86. return;
  87. seen_blocks.insert(block);
  88. if (op(block))
  89. {
  90. for (auto b : get_succeeding_edges(block))
  91. walk_from(seen_blocks, b, op);
  92. }
  93. }
  94. uint32_t find_loop_dominator(uint32_t block) const;
  95. bool node_terminates_control_flow_in_sub_graph(BlockID from, BlockID to) const;
  96. private:
  97. struct VisitOrder
  98. {
  99. int &get()
  100. {
  101. return v;
  102. }
  103. const int &get() const
  104. {
  105. return v;
  106. }
  107. int v = -1;
  108. };
  109. Compiler &compiler;
  110. const SPIRFunction &func;
  111. std::unordered_map<uint32_t, SmallVector<uint32_t>> preceding_edges;
  112. std::unordered_map<uint32_t, SmallVector<uint32_t>> succeeding_edges;
  113. std::unordered_map<uint32_t, uint32_t> immediate_dominators;
  114. std::unordered_map<uint32_t, VisitOrder> visit_order;
  115. SmallVector<uint32_t> post_order;
  116. SmallVector<uint32_t> empty_vector;
  117. void add_branch(uint32_t from, uint32_t to);
  118. void build_post_order_visit_order();
  119. void build_immediate_dominators();
  120. bool post_order_visit(uint32_t block);
  121. uint32_t visit_count = 0;
  122. bool is_back_edge(uint32_t to) const;
  123. bool has_visited_forward_edge(uint32_t to) const;
  124. };
  125. class DominatorBuilder
  126. {
  127. public:
  128. DominatorBuilder(const CFG &cfg);
  129. void add_block(uint32_t block);
  130. uint32_t get_dominator() const
  131. {
  132. return dominator;
  133. }
  134. void lift_continue_block_dominator();
  135. private:
  136. const CFG &cfg;
  137. uint32_t dominator = 0;
  138. };
  139. } // namespace SPIRV_CROSS_NAMESPACE
  140. #endif