struct_cfg_analysis.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. };
  40. // Set up a stack to keep track of currently active constructs.
  41. std::vector<TraversalInfo> state;
  42. state.emplace_back();
  43. state[0].cinfo.containing_construct = 0;
  44. state[0].cinfo.containing_loop = 0;
  45. state[0].cinfo.containing_switch = 0;
  46. state[0].merge_node = 0;
  47. for (BasicBlock* block : order) {
  48. if (context_->cfg()->IsPseudoEntryBlock(block) ||
  49. context_->cfg()->IsPseudoExitBlock(block)) {
  50. continue;
  51. }
  52. if (block->id() == state.back().merge_node) {
  53. state.pop_back();
  54. }
  55. bb_to_construct_.emplace(std::make_pair(block->id(), state.back().cinfo));
  56. if (Instruction* merge_inst = block->GetMergeInst()) {
  57. TraversalInfo new_state;
  58. new_state.merge_node =
  59. merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  60. new_state.cinfo.containing_construct = block->id();
  61. if (merge_inst->opcode() == SpvOpLoopMerge) {
  62. new_state.cinfo.containing_loop = block->id();
  63. new_state.cinfo.containing_switch = 0;
  64. } else {
  65. new_state.cinfo.containing_loop = state.back().cinfo.containing_loop;
  66. if (merge_inst->NextNode()->opcode() == SpvOpSwitch) {
  67. new_state.cinfo.containing_switch = block->id();
  68. } else {
  69. new_state.cinfo.containing_switch =
  70. state.back().cinfo.containing_switch;
  71. }
  72. }
  73. state.emplace_back(new_state);
  74. merge_blocks_.Set(new_state.merge_node);
  75. }
  76. }
  77. }
  78. uint32_t StructuredCFGAnalysis::ContainingConstruct(Instruction* inst) {
  79. uint32_t bb = context_->get_instr_block(inst)->id();
  80. return ContainingConstruct(bb);
  81. }
  82. uint32_t StructuredCFGAnalysis::MergeBlock(uint32_t bb_id) {
  83. uint32_t header_id = ContainingConstruct(bb_id);
  84. if (header_id == 0) {
  85. return 0;
  86. }
  87. BasicBlock* header = context_->cfg()->block(header_id);
  88. Instruction* merge_inst = header->GetMergeInst();
  89. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  90. }
  91. uint32_t StructuredCFGAnalysis::LoopMergeBlock(uint32_t bb_id) {
  92. uint32_t header_id = ContainingLoop(bb_id);
  93. if (header_id == 0) {
  94. return 0;
  95. }
  96. BasicBlock* header = context_->cfg()->block(header_id);
  97. Instruction* merge_inst = header->GetMergeInst();
  98. return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
  99. }
  100. uint32_t StructuredCFGAnalysis::LoopContinueBlock(uint32_t bb_id) {
  101. uint32_t header_id = ContainingLoop(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(kContinueNodeIndex);
  108. }
  109. uint32_t StructuredCFGAnalysis::SwitchMergeBlock(uint32_t bb_id) {
  110. uint32_t header_id = ContainingSwitch(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. bool StructuredCFGAnalysis::IsContinueBlock(uint32_t bb_id) {
  119. assert(bb_id != 0);
  120. return LoopContinueBlock(bb_id) == bb_id;
  121. }
  122. bool StructuredCFGAnalysis::IsMergeBlock(uint32_t bb_id) {
  123. return merge_blocks_.Get(bb_id);
  124. }
  125. } // namespace opt
  126. } // namespace spvtools