struct_cfg_analysis.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef SOURCE_OPT_STRUCT_CFG_ANALYSIS_H_
  15. #define SOURCE_OPT_STRUCT_CFG_ANALYSIS_H_
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #include "source/opt/function.h"
  19. #include "source/util/bit_vector.h"
  20. namespace spvtools {
  21. namespace opt {
  22. class IRContext;
  23. // An analysis that, for each basic block, finds the constructs in which it is
  24. // contained, so we can easily get headers and merge nodes.
  25. class StructuredCFGAnalysis {
  26. public:
  27. explicit StructuredCFGAnalysis(IRContext* ctx);
  28. // Returns the id of the header of the innermost merge construct
  29. // that contains |bb_id|. Returns |0| if |bb_id| is not contained in any
  30. // merge construct.
  31. uint32_t ContainingConstruct(uint32_t bb_id) {
  32. auto it = bb_to_construct_.find(bb_id);
  33. if (it == bb_to_construct_.end()) {
  34. return 0;
  35. }
  36. return it->second.containing_construct;
  37. }
  38. // Returns the id of the header of the innermost merge construct
  39. // that contains |inst|. Returns |0| if |inst| is not contained in any
  40. // merge construct.
  41. uint32_t ContainingConstruct(Instruction* inst);
  42. // Returns the id of the merge block of the innermost merge construct
  43. // that contains |bb_id|. Returns |0| if |bb_id| is not contained in any
  44. // merge construct.
  45. uint32_t MergeBlock(uint32_t bb_id);
  46. // Returns the nesting depth of the given block, i.e. the number of merge
  47. // constructs containing it. Headers and merge blocks are not considered part
  48. // of the corresponding merge constructs.
  49. uint32_t NestingDepth(uint32_t block_id);
  50. // Returns the id of the header of the innermost loop construct
  51. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  52. // construct.
  53. uint32_t ContainingLoop(uint32_t bb_id) {
  54. auto it = bb_to_construct_.find(bb_id);
  55. if (it == bb_to_construct_.end()) {
  56. return 0;
  57. }
  58. return it->second.containing_loop;
  59. }
  60. // Returns the id of the merge block of the innermost loop construct
  61. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  62. // construct.
  63. uint32_t LoopMergeBlock(uint32_t bb_id);
  64. // Returns the id of the continue block of the innermost loop construct
  65. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  66. // construct.
  67. uint32_t LoopContinueBlock(uint32_t bb_id);
  68. // Returns the loop nesting depth of |bb_id| within its function, i.e. the
  69. // number of loop constructs in which |bb_id| is contained. As per other
  70. // functions in StructuredCFGAnalysis, a loop header is not regarded as being
  71. // part of the loop that it heads, so that e.g. the nesting depth of an
  72. // outer-most loop header is 0.
  73. uint32_t LoopNestingDepth(uint32_t bb_id);
  74. // Returns the id of the header of the innermost switch construct
  75. // that contains |bb_id| as long as there is no intervening loop. Returns |0|
  76. // if no such construct exists.
  77. uint32_t ContainingSwitch(uint32_t bb_id) {
  78. auto it = bb_to_construct_.find(bb_id);
  79. if (it == bb_to_construct_.end()) {
  80. return 0;
  81. }
  82. return it->second.containing_switch;
  83. }
  84. // Returns the id of the merge block of the innermost switch construct
  85. // that contains |bb_id| as long as there is no intervening loop. Return |0|
  86. // if no such block exists.
  87. uint32_t SwitchMergeBlock(uint32_t bb_id);
  88. // Returns true if |bb_id| is the continue block for a loop.
  89. bool IsContinueBlock(uint32_t bb_id);
  90. // Returns true if |bb_id| is in the continue construct for its inner most
  91. // containing loop.
  92. bool IsInContainingLoopsContinueConstruct(uint32_t bb_id);
  93. // Returns true if |bb_id| is in the continue construct for any loop in its
  94. // function.
  95. bool IsInContinueConstruct(uint32_t bb_id);
  96. // Return true if |bb_id| is the merge block for a construct.
  97. bool IsMergeBlock(uint32_t bb_id);
  98. // Returns the set of function ids that are called directly or indirectly from
  99. // a continue construct.
  100. std::unordered_set<uint32_t> FindFuncsCalledFromContinue();
  101. private:
  102. // Struct used to hold the information for a basic block.
  103. // |containing_construct| is the header for the innermost containing
  104. // construct, or 0 if no such construct exists. It could be a selection
  105. // construct or a loop construct.
  106. //
  107. // |containing_loop| is the innermost containing loop construct, or 0 if the
  108. // basic bloc is not in a loop. If the basic block is in a selection
  109. // construct that is contained in a loop construct, then these two values will
  110. // not be the same.
  111. //
  112. // |containing_switch| is the innermost contain selection construct with an
  113. // |OpSwitch| for the branch, as long as there is not intervening loop. This
  114. // is used to identify the selection construct from which it can break.
  115. //
  116. // |in_continue| is true of the block is in the continue construct for its
  117. // innermost containing loop.
  118. struct ConstructInfo {
  119. uint32_t containing_construct;
  120. uint32_t containing_loop;
  121. uint32_t containing_switch;
  122. bool in_continue;
  123. };
  124. // Populates |bb_to_construct_| with the innermost containing merge and loop
  125. // constructs for each basic block in |func|.
  126. void AddBlocksInFunction(Function* func);
  127. IRContext* context_;
  128. // A map from a basic block to the headers of its inner most containing
  129. // constructs.
  130. std::unordered_map<uint32_t, ConstructInfo> bb_to_construct_;
  131. utils::BitVector merge_blocks_;
  132. };
  133. } // namespace opt
  134. } // namespace spvtools
  135. #endif // SOURCE_OPT_STRUCT_CFG_ANALYSIS_H_