struct_cfg_analysis.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.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() == SpvOpSwitch) {
  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::LoopMergeBlock(uint32_t bb_id) {
  110. uint32_t header_id = ContainingLoop(bb_id);
  111. if (header_id == 0) {
  112. return 0;
  113. }
  114. BasicBlock* header = context_->cfg()->block(header_id);
  115. Instruction* merge_inst = header->GetMergeInst();
  116. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  117. }
  118. uint32_t StructuredCFGAnalysis::LoopContinueBlock(uint32_t bb_id) {
  119. uint32_t header_id = ContainingLoop(bb_id);
  120. if (header_id == 0) {
  121. return 0;
  122. }
  123. BasicBlock* header = context_->cfg()->block(header_id);
  124. Instruction* merge_inst = header->GetMergeInst();
  125. return merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
  126. }
  127. uint32_t StructuredCFGAnalysis::SwitchMergeBlock(uint32_t bb_id) {
  128. uint32_t header_id = ContainingSwitch(bb_id);
  129. if (header_id == 0) {
  130. return 0;
  131. }
  132. BasicBlock* header = context_->cfg()->block(header_id);
  133. Instruction* merge_inst = header->GetMergeInst();
  134. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  135. }
  136. bool StructuredCFGAnalysis::IsContinueBlock(uint32_t bb_id) {
  137. assert(bb_id != 0);
  138. return LoopContinueBlock(bb_id) == bb_id;
  139. }
  140. bool StructuredCFGAnalysis::IsInContainingLoopsContinueConstruct(
  141. uint32_t bb_id) {
  142. auto it = bb_to_construct_.find(bb_id);
  143. if (it == bb_to_construct_.end()) {
  144. return false;
  145. }
  146. return it->second.in_continue;
  147. }
  148. bool StructuredCFGAnalysis::IsInContinueConstruct(uint32_t bb_id) {
  149. while (bb_id != 0) {
  150. if (IsInContainingLoopsContinueConstruct(bb_id)) {
  151. return true;
  152. }
  153. bb_id = ContainingLoop(bb_id);
  154. }
  155. return false;
  156. }
  157. bool StructuredCFGAnalysis::IsMergeBlock(uint32_t bb_id) {
  158. return merge_blocks_.Get(bb_id);
  159. }
  160. std::unordered_set<uint32_t>
  161. StructuredCFGAnalysis::FindFuncsCalledFromContinue() {
  162. std::unordered_set<uint32_t> called_from_continue;
  163. std::queue<uint32_t> funcs_to_process;
  164. // First collect the functions that are called directly from a continue
  165. // construct.
  166. for (Function& func : *context_->module()) {
  167. for (auto& bb : func) {
  168. if (IsInContainingLoopsContinueConstruct(bb.id())) {
  169. for (const Instruction& inst : bb) {
  170. if (inst.opcode() == SpvOpFunctionCall) {
  171. funcs_to_process.push(inst.GetSingleWordInOperand(0));
  172. }
  173. }
  174. }
  175. }
  176. }
  177. // Now collect all of the functions that are indirectly called as well.
  178. while (!funcs_to_process.empty()) {
  179. uint32_t func_id = funcs_to_process.front();
  180. funcs_to_process.pop();
  181. Function* func = context_->GetFunction(func_id);
  182. if (called_from_continue.insert(func_id).second) {
  183. context_->AddCalls(func, &funcs_to_process);
  184. }
  185. }
  186. return called_from_continue;
  187. }
  188. } // namespace opt
  189. } // namespace spvtools