struct_cfg_analysis.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 {
  17. const uint32_t kMergeNodeIndex = 0;
  18. const uint32_t kContinueNodeIndex = 1;
  19. } // namespace
  20. namespace spvtools {
  21. namespace opt {
  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(SpvCapabilityShader)) {
  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() == SpvOpLoopMerge) {
  70. new_state.cinfo.containing_loop = block->id();
  71. new_state.cinfo.containing_switch = 0;
  72. new_state.cinfo.in_continue = false;
  73. new_state.continue_node =
  74. merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
  75. } else {
  76. new_state.cinfo.containing_loop = state.back().cinfo.containing_loop;
  77. new_state.cinfo.in_continue = state.back().cinfo.in_continue;
  78. new_state.continue_node = state.back().continue_node;
  79. if (merge_inst->NextNode()->opcode() == SpvOpSwitch) {
  80. new_state.cinfo.containing_switch = block->id();
  81. } else {
  82. new_state.cinfo.containing_switch =
  83. state.back().cinfo.containing_switch;
  84. }
  85. }
  86. state.emplace_back(new_state);
  87. merge_blocks_.Set(new_state.merge_node);
  88. }
  89. }
  90. }
  91. uint32_t StructuredCFGAnalysis::ContainingConstruct(Instruction* inst) {
  92. uint32_t bb = context_->get_instr_block(inst)->id();
  93. return ContainingConstruct(bb);
  94. }
  95. uint32_t StructuredCFGAnalysis::MergeBlock(uint32_t bb_id) {
  96. uint32_t header_id = ContainingConstruct(bb_id);
  97. if (header_id == 0) {
  98. return 0;
  99. }
  100. BasicBlock* header = context_->cfg()->block(header_id);
  101. Instruction* merge_inst = header->GetMergeInst();
  102. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  103. }
  104. uint32_t StructuredCFGAnalysis::LoopMergeBlock(uint32_t bb_id) {
  105. uint32_t header_id = ContainingLoop(bb_id);
  106. if (header_id == 0) {
  107. return 0;
  108. }
  109. BasicBlock* header = context_->cfg()->block(header_id);
  110. Instruction* merge_inst = header->GetMergeInst();
  111. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  112. }
  113. uint32_t StructuredCFGAnalysis::LoopContinueBlock(uint32_t bb_id) {
  114. uint32_t header_id = ContainingLoop(bb_id);
  115. if (header_id == 0) {
  116. return 0;
  117. }
  118. BasicBlock* header = context_->cfg()->block(header_id);
  119. Instruction* merge_inst = header->GetMergeInst();
  120. return merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
  121. }
  122. uint32_t StructuredCFGAnalysis::SwitchMergeBlock(uint32_t bb_id) {
  123. uint32_t header_id = ContainingSwitch(bb_id);
  124. if (header_id == 0) {
  125. return 0;
  126. }
  127. BasicBlock* header = context_->cfg()->block(header_id);
  128. Instruction* merge_inst = header->GetMergeInst();
  129. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  130. }
  131. bool StructuredCFGAnalysis::IsContinueBlock(uint32_t bb_id) {
  132. assert(bb_id != 0);
  133. return LoopContinueBlock(bb_id) == bb_id;
  134. }
  135. bool StructuredCFGAnalysis::IsInContainingLoopsContinueConstruct(
  136. uint32_t bb_id) {
  137. auto it = bb_to_construct_.find(bb_id);
  138. if (it == bb_to_construct_.end()) {
  139. return false;
  140. }
  141. return it->second.in_continue;
  142. }
  143. bool StructuredCFGAnalysis::IsInContinueConstruct(uint32_t bb_id) {
  144. while (bb_id != 0) {
  145. if (IsInContainingLoopsContinueConstruct(bb_id)) {
  146. return true;
  147. }
  148. bb_id = ContainingLoop(bb_id);
  149. }
  150. return false;
  151. }
  152. bool StructuredCFGAnalysis::IsMergeBlock(uint32_t bb_id) {
  153. return merge_blocks_.Get(bb_id);
  154. }
  155. std::unordered_set<uint32_t>
  156. StructuredCFGAnalysis::FindFuncsCalledFromContinue() {
  157. std::unordered_set<uint32_t> called_from_continue;
  158. std::queue<uint32_t> funcs_to_process;
  159. // First collect the functions that are called directly from a continue
  160. // construct.
  161. for (Function& func : *context_->module()) {
  162. for (auto& bb : func) {
  163. if (IsInContainingLoopsContinueConstruct(bb.id())) {
  164. for (const Instruction& inst : bb) {
  165. if (inst.opcode() == SpvOpFunctionCall) {
  166. funcs_to_process.push(inst.GetSingleWordInOperand(0));
  167. }
  168. }
  169. }
  170. }
  171. }
  172. // Now collect all of the functions that are indirectly called as well.
  173. while (!funcs_to_process.empty()) {
  174. uint32_t func_id = funcs_to_process.front();
  175. funcs_to_process.pop();
  176. Function* func = context_->GetFunction(func_id);
  177. if (called_from_continue.insert(func_id).second) {
  178. context_->AddCalls(func, &funcs_to_process);
  179. }
  180. }
  181. return called_from_continue;
  182. }
  183. } // namespace opt
  184. } // namespace spvtools