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 * function) const73 Construct::ConstructBlockSet Construct::blocks(Function* function) const {
74 auto header = entry_block();
75 auto merge = exit_block();
76 assert(header);
77 int header_depth = function->GetBlockDepth(const_cast<BasicBlock*>(header));
78 ConstructBlockSet construct_blocks;
79 std::unordered_set<BasicBlock*> corresponding_headers;
80 for (auto& other : corresponding_constructs()) {
81 // The corresponding header can be the same block as this construct's
82 // header for loops with no loop construct. In those cases, don't add the
83 // loop header as it prevents finding any blocks in the construct.
84 if (type() != ConstructType::kContinue || other->entry_block() != header) {
85 corresponding_headers.insert(other->entry_block());
86 }
87 }
88 std::vector<BasicBlock*> stack;
89 stack.push_back(const_cast<BasicBlock*>(header));
90 while (!stack.empty()) {
91 BasicBlock* block = stack.back();
92 stack.pop_back();
93
94 if (merge == block && ExitBlockIsMergeBlock()) {
95 // Merge block is not part of the construct.
96 continue;
97 }
98
99 if (corresponding_headers.count(block)) {
100 // Entered a corresponding construct.
101 continue;
102 }
103
104 int block_depth = function->GetBlockDepth(block);
105 if (block_depth < header_depth) {
106 // Broke to outer construct.
107 continue;
108 }
109
110 // In a loop, the continue target is at a depth of the loop construct + 1.
111 // A selection construct nested directly within the loop construct is also
112 // at the same depth. It is valid, however, to branch directly to the
113 // continue target from within the selection construct.
114 if (block != header && block_depth == header_depth &&
115 type() == ConstructType::kSelection &&
116 block->is_type(kBlockTypeContinue)) {
117 // Continued to outer construct.
118 continue;
119 }
120
121 if (!construct_blocks.insert(block).second) continue;
122
123 if (merge != block) {
124 for (auto succ : *block->successors()) {
125 // All blocks in the construct must be dominated by the header.
126 if (header->dominates(*succ)) {
127 stack.push_back(succ);
128 }
129 }
130 }
131 }
132
133 return construct_blocks;
134 }
135
IsStructuredExit(ValidationState_t & _,BasicBlock * dest) const136 bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
137 // Structured Exits:
138 // - Selection:
139 // - branch to its merge
140 // - branch to nearest enclosing loop merge or continue
141 // - branch to nearest enclosing switch selection merge
142 // - Loop:
143 // - branch to its merge
144 // - branch to its continue
145 // - Continue:
146 // - branch to loop header
147 // - branch to loop merge
148 //
149 // Note: we will never see a case construct here.
150 assert(type() != ConstructType::kCase);
151 if (type() == ConstructType::kLoop) {
152 auto header = entry_block();
153 auto terminator = header->terminator();
154 auto index = terminator - &_.ordered_instructions()[0];
155 auto merge_inst = &_.ordered_instructions()[index - 1];
156 auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
157 auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
158 if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
159 return true;
160 }
161 } else if (type() == ConstructType::kContinue) {
162 auto loop_construct = corresponding_constructs()[0];
163 auto header = loop_construct->entry_block();
164 auto terminator = header->terminator();
165 auto index = terminator - &_.ordered_instructions()[0];
166 auto merge_inst = &_.ordered_instructions()[index - 1];
167 auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
168 if (dest == header || dest->id() == merge_block_id) {
169 return true;
170 }
171 } else {
172 assert(type() == ConstructType::kSelection);
173 if (dest == exit_block()) {
174 return true;
175 }
176
177 // The next block in the traversal is either:
178 // i. The header block that declares |block| as its merge block.
179 // ii. The immediate dominator of |block|.
180 auto NextBlock = [](const BasicBlock* block) -> const BasicBlock* {
181 for (auto& use : block->label()->uses()) {
182 if ((use.first->opcode() == SpvOpLoopMerge ||
183 use.first->opcode() == SpvOpSelectionMerge) &&
184 use.second == 1)
185 return use.first->block();
186 }
187 return block->immediate_dominator();
188 };
189
190 bool seen_switch = false;
191 auto header = entry_block();
192 auto block = NextBlock(header);
193 while (block) {
194 auto terminator = block->terminator();
195 auto index = terminator - &_.ordered_instructions()[0];
196 auto merge_inst = &_.ordered_instructions()[index - 1];
197 if (merge_inst->opcode() == SpvOpLoopMerge ||
198 (header->terminator()->opcode() != SpvOpSwitch &&
199 merge_inst->opcode() == SpvOpSelectionMerge &&
200 terminator->opcode() == SpvOpSwitch)) {
201 auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
202 auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
203 if (merge_block->dominates(*header)) {
204 block = NextBlock(block);
205 continue;
206 }
207
208 if ((!seen_switch || merge_inst->opcode() == SpvOpLoopMerge) &&
209 dest->id() == merge_target) {
210 return true;
211 } else if (merge_inst->opcode() == SpvOpLoopMerge) {
212 auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
213 if (dest->id() == continue_target) {
214 return true;
215 }
216 }
217
218 if (terminator->opcode() == SpvOpSwitch) {
219 seen_switch = true;
220 }
221
222 // Hit an enclosing loop and didn't break or continue.
223 if (merge_inst->opcode() == SpvOpLoopMerge) return false;
224 }
225
226 block = NextBlock(block);
227 }
228 }
229
230 return false;
231 }
232
233 } // namespace val
234 } // namespace spvtools
235