spirv_cfg.hpp 3.7 KB

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