spirv_cfg.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.order;
  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 order = -1;
  100. bool visited_resolve = false;
  101. bool visited_branches = false;
  102. };
  103. Compiler &compiler;
  104. const SPIRFunction &func;
  105. std::unordered_map<uint32_t, SmallVector<uint32_t>> preceding_edges;
  106. std::unordered_map<uint32_t, SmallVector<uint32_t>> succeeding_edges;
  107. std::unordered_map<uint32_t, uint32_t> immediate_dominators;
  108. std::unordered_map<uint32_t, VisitOrder> visit_order;
  109. SmallVector<uint32_t> post_order;
  110. SmallVector<uint32_t> empty_vector;
  111. void add_branch(uint32_t from, uint32_t to);
  112. void build_post_order_visit_order();
  113. void build_immediate_dominators();
  114. void post_order_visit_branches(uint32_t block);
  115. void post_order_visit_resolve(uint32_t block);
  116. void post_order_visit_entry(uint32_t block);
  117. uint32_t visit_count = 0;
  118. bool is_back_edge(uint32_t to) const;
  119. bool has_visited_branch(uint32_t to) const;
  120. void visit_branch(uint32_t block_id);
  121. SmallVector<uint32_t> visit_stack;
  122. size_t last_visited_size = 0;
  123. };
  124. class DominatorBuilder
  125. {
  126. public:
  127. DominatorBuilder(const CFG &cfg);
  128. void add_block(uint32_t block);
  129. uint32_t get_dominator() const
  130. {
  131. return dominator;
  132. }
  133. void lift_continue_block_dominator();
  134. private:
  135. const CFG &cfg;
  136. uint32_t dominator = 0;
  137. };
  138. } // namespace SPIRV_CROSS_NAMESPACE
  139. #endif