construct.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. auto header = entry_block();
  62. auto merge = exit_block();
  63. assert(header);
  64. int header_depth = function->GetBlockDepth(const_cast<BasicBlock*>(header));
  65. ConstructBlockSet construct_blocks;
  66. std::unordered_set<BasicBlock*> corresponding_headers;
  67. for (auto& other : corresponding_constructs()) {
  68. corresponding_headers.insert(other->entry_block());
  69. }
  70. std::vector<BasicBlock*> stack;
  71. stack.push_back(const_cast<BasicBlock*>(header));
  72. while (!stack.empty()) {
  73. BasicBlock* block = stack.back();
  74. stack.pop_back();
  75. if (merge == block && ExitBlockIsMergeBlock()) {
  76. // Merge block is not part of the construct.
  77. continue;
  78. }
  79. if (corresponding_headers.count(block)) {
  80. // Entered a corresponding construct.
  81. continue;
  82. }
  83. int block_depth = function->GetBlockDepth(block);
  84. if (block_depth < header_depth) {
  85. // Broke to outer construct.
  86. continue;
  87. }
  88. // In a loop, the continue target is at a depth of the loop construct + 1.
  89. // A selection construct nested directly within the loop construct is also
  90. // at the same depth. It is valid, however, to branch directly to the
  91. // continue target from within the selection construct.
  92. if (block != header && block_depth == header_depth &&
  93. type() == ConstructType::kSelection &&
  94. block->is_type(kBlockTypeContinue)) {
  95. // Continued to outer construct.
  96. continue;
  97. }
  98. if (!construct_blocks.insert(block).second) continue;
  99. if (merge != block) {
  100. for (auto succ : *block->successors()) {
  101. // All blocks in the construct must be dominated by the header.
  102. if (header->dominates(*succ)) {
  103. stack.push_back(succ);
  104. }
  105. }
  106. }
  107. }
  108. return construct_blocks;
  109. }
  110. bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
  111. // Structured Exits:
  112. // - Selection:
  113. // - branch to its merge
  114. // - branch to nearest enclosing loop merge or continue
  115. // - branch to nearest enclosing switch selection merge
  116. // - Loop:
  117. // - branch to its merge
  118. // - branch to its continue
  119. // - Continue:
  120. // - branch to loop header
  121. // - branch to loop merge
  122. //
  123. // Note: we will never see a case construct here.
  124. assert(type() != ConstructType::kCase);
  125. if (type() == ConstructType::kLoop) {
  126. auto header = entry_block();
  127. auto terminator = header->terminator();
  128. auto index = terminator - &_.ordered_instructions()[0];
  129. auto merge_inst = &_.ordered_instructions()[index - 1];
  130. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  131. auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
  132. if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
  133. return true;
  134. }
  135. } else if (type() == ConstructType::kContinue) {
  136. auto loop_construct = corresponding_constructs()[0];
  137. auto header = loop_construct->entry_block();
  138. auto terminator = header->terminator();
  139. auto index = terminator - &_.ordered_instructions()[0];
  140. auto merge_inst = &_.ordered_instructions()[index - 1];
  141. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  142. if (dest == header || dest->id() == merge_block_id) {
  143. return true;
  144. }
  145. } else {
  146. assert(type() == ConstructType::kSelection);
  147. if (dest == exit_block()) {
  148. return true;
  149. }
  150. bool seen_switch = false;
  151. auto header = entry_block();
  152. auto block = header->immediate_dominator();
  153. while (block) {
  154. auto terminator = block->terminator();
  155. auto index = terminator - &_.ordered_instructions()[0];
  156. auto merge_inst = &_.ordered_instructions()[index - 1];
  157. if (merge_inst->opcode() == SpvOpLoopMerge ||
  158. (header->terminator()->opcode() != SpvOpSwitch &&
  159. merge_inst->opcode() == SpvOpSelectionMerge &&
  160. terminator->opcode() == SpvOpSwitch)) {
  161. auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
  162. auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
  163. if (merge_block->dominates(*header)) {
  164. block = block->immediate_dominator();
  165. continue;
  166. }
  167. if ((!seen_switch || merge_inst->opcode() == SpvOpLoopMerge) &&
  168. dest->id() == merge_target) {
  169. return true;
  170. } else if (merge_inst->opcode() == SpvOpLoopMerge) {
  171. auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
  172. if (dest->id() == continue_target) {
  173. return true;
  174. }
  175. }
  176. if (terminator->opcode() == SpvOpSwitch) seen_switch = true;
  177. // Hit an enclosing loop and didn't break or continue.
  178. if (merge_inst->opcode() == SpvOpLoopMerge) return false;
  179. }
  180. block = block->immediate_dominator();
  181. }
  182. }
  183. return false;
  184. }
  185. } // namespace val
  186. } // namespace spvtools