2
0

construct.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  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/val/construct.h"
  15. #include <cassert>
  16. #include <cstddef>
  17. #include <unordered_set>
  18. #include "source/val/function.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. Construct::Construct(ConstructType construct_type, BasicBlock* entry,
  23. BasicBlock* exit, std::vector<Construct*> constructs)
  24. : type_(construct_type),
  25. corresponding_constructs_(constructs),
  26. entry_block_(entry),
  27. exit_block_(exit) {}
  28. ConstructType Construct::type() const { return type_; }
  29. const std::vector<Construct*>& Construct::corresponding_constructs() const {
  30. return corresponding_constructs_;
  31. }
  32. std::vector<Construct*>& Construct::corresponding_constructs() {
  33. return corresponding_constructs_;
  34. }
  35. bool ValidateConstructSize(ConstructType type, size_t size) {
  36. switch (type) {
  37. case ConstructType::kSelection:
  38. return size == 0;
  39. case ConstructType::kContinue:
  40. return size == 1;
  41. case ConstructType::kLoop:
  42. return size == 1;
  43. case ConstructType::kCase:
  44. return size >= 1;
  45. default:
  46. assert(1 == 0 && "Type not defined");
  47. }
  48. return false;
  49. }
  50. void Construct::set_corresponding_constructs(
  51. std::vector<Construct*> constructs) {
  52. assert(ValidateConstructSize(type_, constructs.size()));
  53. corresponding_constructs_ = constructs;
  54. }
  55. const BasicBlock* Construct::entry_block() const { return entry_block_; }
  56. BasicBlock* Construct::entry_block() { return entry_block_; }
  57. const BasicBlock* Construct::exit_block() const { return exit_block_; }
  58. BasicBlock* Construct::exit_block() { return exit_block_; }
  59. void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
  60. Construct::ConstructBlockSet Construct::blocks(Function* /*function*/) const {
  61. const auto header = entry_block();
  62. const auto exit = exit_block();
  63. const bool is_continue = type() == ConstructType::kContinue;
  64. const bool is_loop = type() == ConstructType::kLoop;
  65. const BasicBlock* continue_header = nullptr;
  66. if (is_loop) {
  67. // The only corresponding construct for a loop is the continue.
  68. continue_header = (*corresponding_constructs().begin())->entry_block();
  69. }
  70. std::vector<BasicBlock*> stack;
  71. stack.push_back(const_cast<BasicBlock*>(header));
  72. ConstructBlockSet construct_blocks;
  73. while (!stack.empty()) {
  74. auto* block = stack.back();
  75. stack.pop_back();
  76. if (header->structurally_dominates(*block)) {
  77. bool include = false;
  78. if (is_continue && exit->structurally_postdominates(*block)) {
  79. // Continue construct include blocks dominated by the continue target
  80. // and post-dominated by the back-edge block.
  81. include = true;
  82. } else if (!exit->structurally_dominates(*block)) {
  83. // Selection and loop constructs include blocks dominated by the header
  84. // and not dominated by the merge.
  85. include = true;
  86. if (is_loop && continue_header->structurally_dominates(*block)) {
  87. // Loop constructs have an additional constraint that they do not
  88. // include blocks dominated by the continue construct. Since all
  89. // blocks in the continue construct are dominated by the continue
  90. // target, we just test for dominance by continue target.
  91. include = false;
  92. }
  93. }
  94. if (include) {
  95. if (!construct_blocks.insert(block).second) continue;
  96. for (auto succ : *block->structural_successors()) {
  97. stack.push_back(succ);
  98. }
  99. }
  100. }
  101. }
  102. return construct_blocks;
  103. }
  104. bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
  105. // Structured Exits:
  106. // - Selection:
  107. // - branch to its merge
  108. // - branch to nearest enclosing loop merge or continue
  109. // - branch to nearest enclosing switch selection merge
  110. // - Loop:
  111. // - branch to its merge
  112. // - branch to its continue
  113. // - Continue:
  114. // - branch to loop header
  115. // - branch to loop merge
  116. //
  117. // Note: we will never see a case construct here.
  118. assert(type() != ConstructType::kCase);
  119. if (type() == ConstructType::kLoop) {
  120. auto header = entry_block();
  121. auto terminator = header->terminator();
  122. auto index = terminator - &_.ordered_instructions()[0];
  123. auto merge_inst = &_.ordered_instructions()[index - 1];
  124. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  125. auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
  126. if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
  127. return true;
  128. }
  129. } else if (type() == ConstructType::kContinue) {
  130. auto loop_construct = corresponding_constructs()[0];
  131. auto header = loop_construct->entry_block();
  132. auto terminator = header->terminator();
  133. auto index = terminator - &_.ordered_instructions()[0];
  134. auto merge_inst = &_.ordered_instructions()[index - 1];
  135. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  136. if (dest == header || dest->id() == merge_block_id) {
  137. return true;
  138. }
  139. } else {
  140. assert(type() == ConstructType::kSelection);
  141. if (dest == exit_block()) {
  142. return true;
  143. }
  144. // The next block in the traversal is either:
  145. // i. The header block that declares |block| as its merge block.
  146. // ii. The immediate dominator of |block|.
  147. auto NextBlock = [](const BasicBlock* block) -> const BasicBlock* {
  148. for (auto& use : block->label()->uses()) {
  149. if ((use.first->opcode() == SpvOpLoopMerge ||
  150. use.first->opcode() == SpvOpSelectionMerge) &&
  151. use.second == 1 &&
  152. use.first->block()->structurally_dominates(*block)) {
  153. return use.first->block();
  154. }
  155. }
  156. return block->immediate_structural_dominator();
  157. };
  158. bool seen_switch = false;
  159. auto header = entry_block();
  160. auto block = NextBlock(header);
  161. while (block) {
  162. auto terminator = block->terminator();
  163. auto index = terminator - &_.ordered_instructions()[0];
  164. auto merge_inst = &_.ordered_instructions()[index - 1];
  165. if (merge_inst->opcode() == SpvOpLoopMerge ||
  166. (header->terminator()->opcode() != SpvOpSwitch &&
  167. merge_inst->opcode() == SpvOpSelectionMerge &&
  168. terminator->opcode() == SpvOpSwitch)) {
  169. auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
  170. auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
  171. if (merge_block->structurally_dominates(*header)) {
  172. block = NextBlock(block);
  173. continue;
  174. }
  175. if ((!seen_switch || merge_inst->opcode() == SpvOpLoopMerge) &&
  176. dest->id() == merge_target) {
  177. return true;
  178. } else if (merge_inst->opcode() == SpvOpLoopMerge) {
  179. auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
  180. if (dest->id() == continue_target) {
  181. return true;
  182. }
  183. }
  184. if (terminator->opcode() == SpvOpSwitch) {
  185. seen_switch = true;
  186. }
  187. // Hit an enclosing loop and didn't break or continue.
  188. if (merge_inst->opcode() == SpvOpLoopMerge) return false;
  189. }
  190. block = NextBlock(block);
  191. }
  192. }
  193. return false;
  194. }
  195. } // namespace val
  196. } // namespace spvtools