struct_cfg_analysis.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/struct_cfg_analysis.h"
  15. #include "source/opt/ir_context.h"
  16. namespace spvtools {
  17. namespace opt {
  18. namespace {
  19. constexpr uint32_t kMergeNodeIndex = 0;
  20. constexpr uint32_t kContinueNodeIndex = 1;
  21. } // namespace
  22. StructuredCFGAnalysis::StructuredCFGAnalysis(IRContext* ctx) : context_(ctx) {
  23. // If this is not a shader, there are no merge instructions, and not
  24. // structured CFG to analyze.
  25. if (!context_->get_feature_mgr()->HasCapability(spv::Capability::Shader)) {
  26. return;
  27. }
  28. for (auto& func : *context_->module()) {
  29. AddBlocksInFunction(&func);
  30. }
  31. }
  32. void StructuredCFGAnalysis::AddBlocksInFunction(Function* func) {
  33. if (func->begin() == func->end()) return;
  34. std::list<BasicBlock*> order;
  35. context_->cfg()->ComputeStructuredOrder(func, &*func->begin(), &order);
  36. struct TraversalInfo {
  37. ConstructInfo cinfo;
  38. uint32_t merge_node;
  39. uint32_t continue_node;
  40. };
  41. // Set up a stack to keep track of currently active constructs.
  42. std::vector<TraversalInfo> state;
  43. state.emplace_back();
  44. state[0].cinfo.containing_construct = 0;
  45. state[0].cinfo.containing_loop = 0;
  46. state[0].cinfo.containing_switch = 0;
  47. state[0].cinfo.in_continue = false;
  48. state[0].merge_node = 0;
  49. state[0].continue_node = 0;
  50. for (BasicBlock* block : order) {
  51. if (context_->cfg()->IsPseudoEntryBlock(block) ||
  52. context_->cfg()->IsPseudoExitBlock(block)) {
  53. continue;
  54. }
  55. if (block->id() == state.back().merge_node) {
  56. state.pop_back();
  57. }
  58. // This works because the structured order is designed to keep the blocks in
  59. // the continue construct between the continue header and the merge node.
  60. if (block->id() == state.back().continue_node) {
  61. state.back().cinfo.in_continue = true;
  62. }
  63. bb_to_construct_.emplace(std::make_pair(block->id(), state.back().cinfo));
  64. if (Instruction* merge_inst = block->GetMergeInst()) {
  65. TraversalInfo new_state;
  66. new_state.merge_node =
  67. merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  68. new_state.cinfo.containing_construct = block->id();
  69. if (merge_inst->opcode() == spv::Op::OpLoopMerge) {
  70. new_state.cinfo.containing_loop = block->id();
  71. new_state.cinfo.containing_switch = 0;
  72. new_state.continue_node =
  73. merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
  74. if (block->id() == new_state.continue_node) {
  75. new_state.cinfo.in_continue = true;
  76. bb_to_construct_[block->id()].in_continue = true;
  77. } else {
  78. new_state.cinfo.in_continue = false;
  79. }
  80. } else {
  81. new_state.cinfo.containing_loop = state.back().cinfo.containing_loop;
  82. new_state.cinfo.in_continue = state.back().cinfo.in_continue;
  83. new_state.continue_node = state.back().continue_node;
  84. if (merge_inst->NextNode()->opcode() == spv::Op::OpSwitch) {
  85. new_state.cinfo.containing_switch = block->id();
  86. } else {
  87. new_state.cinfo.containing_switch =
  88. state.back().cinfo.containing_switch;
  89. }
  90. }
  91. state.emplace_back(new_state);
  92. merge_blocks_.Set(new_state.merge_node);
  93. }
  94. }
  95. }
  96. uint32_t StructuredCFGAnalysis::ContainingConstruct(Instruction* inst) {
  97. uint32_t bb = context_->get_instr_block(inst)->id();
  98. return ContainingConstruct(bb);
  99. }
  100. uint32_t StructuredCFGAnalysis::MergeBlock(uint32_t bb_id) {
  101. uint32_t header_id = ContainingConstruct(bb_id);
  102. if (header_id == 0) {
  103. return 0;
  104. }
  105. BasicBlock* header = context_->cfg()->block(header_id);
  106. Instruction* merge_inst = header->GetMergeInst();
  107. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  108. }
  109. uint32_t StructuredCFGAnalysis::NestingDepth(uint32_t bb_id) {
  110. uint32_t result = 0;
  111. // Find the merge block of the current merge construct as long as the block is
  112. // inside a merge construct, exiting one for each iteration.
  113. for (uint32_t merge_block_id = MergeBlock(bb_id); merge_block_id != 0;
  114. merge_block_id = MergeBlock(merge_block_id)) {
  115. result++;
  116. }
  117. return result;
  118. }
  119. uint32_t StructuredCFGAnalysis::LoopMergeBlock(uint32_t bb_id) {
  120. uint32_t header_id = ContainingLoop(bb_id);
  121. if (header_id == 0) {
  122. return 0;
  123. }
  124. BasicBlock* header = context_->cfg()->block(header_id);
  125. Instruction* merge_inst = header->GetMergeInst();
  126. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  127. }
  128. uint32_t StructuredCFGAnalysis::LoopContinueBlock(uint32_t bb_id) {
  129. uint32_t header_id = ContainingLoop(bb_id);
  130. if (header_id == 0) {
  131. return 0;
  132. }
  133. BasicBlock* header = context_->cfg()->block(header_id);
  134. Instruction* merge_inst = header->GetMergeInst();
  135. return merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
  136. }
  137. uint32_t StructuredCFGAnalysis::LoopNestingDepth(uint32_t bb_id) {
  138. uint32_t result = 0;
  139. // Find the merge block of the current loop as long as the block is inside a
  140. // loop, exiting a loop for each iteration.
  141. for (uint32_t merge_block_id = LoopMergeBlock(bb_id); merge_block_id != 0;
  142. merge_block_id = LoopMergeBlock(merge_block_id)) {
  143. result++;
  144. }
  145. return result;
  146. }
  147. uint32_t StructuredCFGAnalysis::SwitchMergeBlock(uint32_t bb_id) {
  148. uint32_t header_id = ContainingSwitch(bb_id);
  149. if (header_id == 0) {
  150. return 0;
  151. }
  152. BasicBlock* header = context_->cfg()->block(header_id);
  153. Instruction* merge_inst = header->GetMergeInst();
  154. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  155. }
  156. bool StructuredCFGAnalysis::IsContinueBlock(uint32_t bb_id) {
  157. assert(bb_id != 0);
  158. return LoopContinueBlock(bb_id) == bb_id;
  159. }
  160. bool StructuredCFGAnalysis::IsInContainingLoopsContinueConstruct(
  161. uint32_t bb_id) {
  162. auto it = bb_to_construct_.find(bb_id);
  163. if (it == bb_to_construct_.end()) {
  164. return false;
  165. }
  166. return it->second.in_continue;
  167. }
  168. bool StructuredCFGAnalysis::IsInContinueConstruct(uint32_t bb_id) {
  169. while (bb_id != 0) {
  170. if (IsInContainingLoopsContinueConstruct(bb_id)) {
  171. return true;
  172. }
  173. bb_id = ContainingLoop(bb_id);
  174. }
  175. return false;
  176. }
  177. bool StructuredCFGAnalysis::IsMergeBlock(uint32_t bb_id) {
  178. return merge_blocks_.Get(bb_id);
  179. }
  180. std::unordered_set<uint32_t>
  181. StructuredCFGAnalysis::FindFuncsCalledFromContinue() {
  182. std::unordered_set<uint32_t> called_from_continue;
  183. std::queue<uint32_t> funcs_to_process;
  184. // First collect the functions that are called directly from a continue
  185. // construct.
  186. for (Function& func : *context_->module()) {
  187. for (auto& bb : func) {
  188. if (IsInContainingLoopsContinueConstruct(bb.id())) {
  189. for (const Instruction& inst : bb) {
  190. if (inst.opcode() == spv::Op::OpFunctionCall) {
  191. funcs_to_process.push(inst.GetSingleWordInOperand(0));
  192. }
  193. }
  194. }
  195. }
  196. }
  197. // Now collect all of the functions that are indirectly called as well.
  198. while (!funcs_to_process.empty()) {
  199. uint32_t func_id = funcs_to_process.front();
  200. funcs_to_process.pop();
  201. Function* func = context_->GetFunction(func_id);
  202. if (called_from_continue.insert(func_id).second) {
  203. context_->AddCalls(func, &funcs_to_process);
  204. }
  205. }
  206. return called_from_continue;
  207. }
  208. } // namespace opt
  209. } // namespace spvtools