struct_cfg_analysis.cpp 3.7 KB

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