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