construct.cpp 7.8 KB

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