struct_cfg_analysis.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "source/opt/function.h"
  18. #include "source/util/bit_vector.h"
  19. namespace spvtools {
  20. namespace opt {
  21. class IRContext;
  22. // An analysis that, for each basic block, finds the constructs in which it is
  23. // contained, so we can easily get headers and merge nodes.
  24. class StructuredCFGAnalysis {
  25. public:
  26. explicit StructuredCFGAnalysis(IRContext* ctx);
  27. // Returns the id of the header of the innermost merge construct
  28. // that contains |bb_id|. Returns |0| if |bb_id| is not contained in any
  29. // merge construct.
  30. uint32_t ContainingConstruct(uint32_t bb_id) {
  31. auto it = bb_to_construct_.find(bb_id);
  32. if (it == bb_to_construct_.end()) {
  33. return 0;
  34. }
  35. return it->second.containing_construct;
  36. }
  37. // Returns the id of the header of the innermost merge construct
  38. // that contains |inst|. Returns |0| if |inst| is not contained in any
  39. // merge construct.
  40. uint32_t ContainingConstruct(Instruction* inst);
  41. // Returns the id of the merge block of the innermost merge construct
  42. // that contains |bb_id|. Returns |0| if |bb_id| is not contained in any
  43. // merge construct.
  44. uint32_t MergeBlock(uint32_t bb_id);
  45. // Returns the id of the header of the innermost loop construct
  46. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  47. // construct.
  48. uint32_t ContainingLoop(uint32_t bb_id) {
  49. auto it = bb_to_construct_.find(bb_id);
  50. if (it == bb_to_construct_.end()) {
  51. return 0;
  52. }
  53. return it->second.containing_loop;
  54. }
  55. // Returns the id of the merge block of the innermost loop construct
  56. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  57. // construct.
  58. uint32_t LoopMergeBlock(uint32_t bb_id);
  59. // Returns the id of the continue block of the innermost loop construct
  60. // that contains |bb_id|. Return |0| if |bb_id| is not contained in any loop
  61. // construct.
  62. uint32_t LoopContinueBlock(uint32_t bb_id);
  63. // Returns the id of the header of the innermost switch construct
  64. // that contains |bb_id| as long as there is no intervening loop. Returns |0|
  65. // if no such construct exists.
  66. uint32_t ContainingSwitch(uint32_t bb_id) {
  67. auto it = bb_to_construct_.find(bb_id);
  68. if (it == bb_to_construct_.end()) {
  69. return 0;
  70. }
  71. return it->second.containing_switch;
  72. }
  73. // Returns the id of the merge block of the innermost switch construct
  74. // that contains |bb_id| as long as there is no intervening loop. Return |0|
  75. // if no such block exists.
  76. uint32_t SwitchMergeBlock(uint32_t bb_id);
  77. bool IsContinueBlock(uint32_t bb_id);
  78. bool IsMergeBlock(uint32_t bb_id);
  79. private:
  80. // Struct used to hold the information for a basic block.
  81. // |containing_construct| is the header for the innermost containing
  82. // construct, or 0 if no such construct exists. It could be a selection
  83. // construct or a loop construct. |containing_loop| is the innermost
  84. // containing loop construct, or 0 if the basic bloc is not in a loop. If the
  85. // basic block is in a selection construct that is contained in a loop
  86. // construct, then these two values will not be the same.
  87. struct ConstructInfo {
  88. uint32_t containing_construct;
  89. uint32_t containing_loop;
  90. uint32_t containing_switch;
  91. };
  92. // Populates |bb_to_construct_| with the innermost containing merge and loop
  93. // constructs for each basic block in |func|.
  94. void AddBlocksInFunction(Function* func);
  95. IRContext* context_;
  96. // A map from a basic block to the headers of its inner most containing
  97. // constructs.
  98. std::unordered_map<uint32_t, ConstructInfo> bb_to_construct_;
  99. utils::BitVector merge_blocks_;
  100. };
  101. } // namespace opt
  102. } // namespace spvtools
  103. #endif // SOURCE_OPT_STRUCT_CFG_ANALYSIS_H_