struct_cfg_analysis.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 id of the header of the innermost loop construct
  47. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  48. // construct.
  49. uint32_t ContainingLoop(uint32_t bb_id) {
  50. auto it = bb_to_construct_.find(bb_id);
  51. if (it == bb_to_construct_.end()) {
  52. return 0;
  53. }
  54. return it->second.containing_loop;
  55. }
  56. // Returns the id of the merge block of the innermost loop construct
  57. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  58. // construct.
  59. uint32_t LoopMergeBlock(uint32_t bb_id);
  60. // Returns the id of the continue 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 LoopContinueBlock(uint32_t bb_id);
  64. // Returns the id of the header of the innermost switch construct
  65. // that contains |bb_id| as long as there is no intervening loop. Returns |0|
  66. // if no such construct exists.
  67. uint32_t ContainingSwitch(uint32_t bb_id) {
  68. auto it = bb_to_construct_.find(bb_id);
  69. if (it == bb_to_construct_.end()) {
  70. return 0;
  71. }
  72. return it->second.containing_switch;
  73. }
  74. // Returns the id of the merge block of the innermost switch construct
  75. // that contains |bb_id| as long as there is no intervening loop. Return |0|
  76. // if no such block exists.
  77. uint32_t SwitchMergeBlock(uint32_t bb_id);
  78. // Returns true if |bb_id| is the continue block for a loop.
  79. bool IsContinueBlock(uint32_t bb_id);
  80. // Returns true if |bb_id| is in the continue construct for its inner most
  81. // containing loop.
  82. bool IsInContainingLoopsContinueConstruct(uint32_t bb_id);
  83. // Returns true if |bb_id| is in the continue construct for any loop in its
  84. // function.
  85. bool IsInContinueConstruct(uint32_t bb_id);
  86. // Return true if |bb_id| is the merge block for a construct.
  87. bool IsMergeBlock(uint32_t bb_id);
  88. // Returns the set of function ids that are called directly or indirectly from
  89. // a continue construct.
  90. std::unordered_set<uint32_t> FindFuncsCalledFromContinue();
  91. private:
  92. // Struct used to hold the information for a basic block.
  93. // |containing_construct| is the header for the innermost containing
  94. // construct, or 0 if no such construct exists. It could be a selection
  95. // construct or a loop construct.
  96. //
  97. // |containing_loop| is the innermost containing loop construct, or 0 if the
  98. // basic bloc is not in a loop. If the basic block is in a selection
  99. // construct that is contained in a loop construct, then these two values will
  100. // not be the same.
  101. //
  102. // |containing_switch| is the innermost contain selection construct with an
  103. // |OpSwitch| for the branch, as long as there is not intervening loop. This
  104. // is used to identify the selection construct from which it can break.
  105. //
  106. // |in_continue| is true of the block is in the continue construct for its
  107. // innermost containing loop.
  108. struct ConstructInfo {
  109. uint32_t containing_construct;
  110. uint32_t containing_loop;
  111. uint32_t containing_switch;
  112. bool in_continue;
  113. };
  114. // Populates |bb_to_construct_| with the innermost containing merge and loop
  115. // constructs for each basic block in |func|.
  116. void AddBlocksInFunction(Function* func);
  117. IRContext* context_;
  118. // A map from a basic block to the headers of its inner most containing
  119. // constructs.
  120. std::unordered_map<uint32_t, ConstructInfo> bb_to_construct_;
  121. utils::BitVector merge_blocks_;
  122. };
  123. } // namespace opt
  124. } // namespace spvtools
  125. #endif // SOURCE_OPT_STRUCT_CFG_ANALYSIS_H_