1 // Copyright (c) 2018 Google LLC.
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/opt/struct_cfg_analysis.h"
16
17 #include "source/opt/ir_context.h"
18
19 namespace {
20 const uint32_t kMergeNodeIndex = 0;
21 const uint32_t kContinueNodeIndex = 1;
22 } // namespace
23
24 namespace spvtools {
25 namespace opt {
26
StructuredCFGAnalysis(IRContext * ctx)27 StructuredCFGAnalysis::StructuredCFGAnalysis(IRContext* ctx) : context_(ctx) {
28 // If this is not a shader, there are no merge instructions, and not
29 // structured CFG to analyze.
30 if (!context_->get_feature_mgr()->HasCapability(SpvCapabilityShader)) {
31 return;
32 }
33
34 for (auto& func : *context_->module()) {
35 AddBlocksInFunction(&func);
36 }
37 }
38
AddBlocksInFunction(Function * func)39 void StructuredCFGAnalysis::AddBlocksInFunction(Function* func) {
40 if (func->begin() == func->end()) return;
41
42 std::list<BasicBlock*> order;
43 context_->cfg()->ComputeStructuredOrder(func, &*func->begin(), &order);
44
45 struct TraversalInfo {
46 ConstructInfo cinfo;
47 uint32_t merge_node;
48 uint32_t continue_node;
49 };
50
51 // Set up a stack to keep track of currently active constructs.
52 std::vector<TraversalInfo> state;
53 state.emplace_back();
54 state[0].cinfo.containing_construct = 0;
55 state[0].cinfo.containing_loop = 0;
56 state[0].cinfo.containing_switch = 0;
57 state[0].cinfo.in_continue = false;
58 state[0].merge_node = 0;
59 state[0].continue_node = 0;
60
61 for (BasicBlock* block : order) {
62 if (context_->cfg()->IsPseudoEntryBlock(block) ||
63 context_->cfg()->IsPseudoExitBlock(block)) {
64 continue;
65 }
66
67 if (block->id() == state.back().merge_node) {
68 state.pop_back();
69 }
70
71 // This works because the structured order is designed to keep the blocks in
72 // the continue construct between the continue header and the merge node.
73 if (block->id() == state.back().continue_node) {
74 state.back().cinfo.in_continue = true;
75 }
76
77 bb_to_construct_.emplace(std::make_pair(block->id(), state.back().cinfo));
78
79 if (Instruction* merge_inst = block->GetMergeInst()) {
80 TraversalInfo new_state;
81 new_state.merge_node =
82 merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
83 new_state.cinfo.containing_construct = block->id();
84
85 if (merge_inst->opcode() == SpvOpLoopMerge) {
86 new_state.cinfo.containing_loop = block->id();
87 new_state.cinfo.containing_switch = 0;
88 new_state.continue_node =
89 merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
90 if (block->id() == new_state.continue_node) {
91 new_state.cinfo.in_continue = true;
92 bb_to_construct_[block->id()].in_continue = true;
93 } else {
94 new_state.cinfo.in_continue = false;
95 }
96 } else {
97 new_state.cinfo.containing_loop = state.back().cinfo.containing_loop;
98 new_state.cinfo.in_continue = state.back().cinfo.in_continue;
99 new_state.continue_node = state.back().continue_node;
100
101 if (merge_inst->NextNode()->opcode() == SpvOpSwitch) {
102 new_state.cinfo.containing_switch = block->id();
103 } else {
104 new_state.cinfo.containing_switch =
105 state.back().cinfo.containing_switch;
106 }
107 }
108
109 state.emplace_back(new_state);
110 merge_blocks_.Set(new_state.merge_node);
111 }
112 }
113 }
114
ContainingConstruct(Instruction * inst)115 uint32_t StructuredCFGAnalysis::ContainingConstruct(Instruction* inst) {
116 uint32_t bb = context_->get_instr_block(inst)->id();
117 return ContainingConstruct(bb);
118 }
119
MergeBlock(uint32_t bb_id)120 uint32_t StructuredCFGAnalysis::MergeBlock(uint32_t bb_id) {
121 uint32_t header_id = ContainingConstruct(bb_id);
122 if (header_id == 0) {
123 return 0;
124 }
125
126 BasicBlock* header = context_->cfg()->block(header_id);
127 Instruction* merge_inst = header->GetMergeInst();
128 return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
129 }
130
NestingDepth(uint32_t bb_id)131 uint32_t StructuredCFGAnalysis::NestingDepth(uint32_t bb_id) {
132 uint32_t result = 0;
133
134 // Find the merge block of the current merge construct as long as the block is
135 // inside a merge construct, exiting one for each iteration.
136 for (uint32_t merge_block_id = MergeBlock(bb_id); merge_block_id != 0;
137 merge_block_id = MergeBlock(merge_block_id)) {
138 result++;
139 }
140
141 return result;
142 }
143
LoopMergeBlock(uint32_t bb_id)144 uint32_t StructuredCFGAnalysis::LoopMergeBlock(uint32_t bb_id) {
145 uint32_t header_id = ContainingLoop(bb_id);
146 if (header_id == 0) {
147 return 0;
148 }
149
150 BasicBlock* header = context_->cfg()->block(header_id);
151 Instruction* merge_inst = header->GetMergeInst();
152 return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
153 }
154
LoopContinueBlock(uint32_t bb_id)155 uint32_t StructuredCFGAnalysis::LoopContinueBlock(uint32_t bb_id) {
156 uint32_t header_id = ContainingLoop(bb_id);
157 if (header_id == 0) {
158 return 0;
159 }
160
161 BasicBlock* header = context_->cfg()->block(header_id);
162 Instruction* merge_inst = header->GetMergeInst();
163 return merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
164 }
165
LoopNestingDepth(uint32_t bb_id)166 uint32_t StructuredCFGAnalysis::LoopNestingDepth(uint32_t bb_id) {
167 uint32_t result = 0;
168
169 // Find the merge block of the current loop as long as the block is inside a
170 // loop, exiting a loop for each iteration.
171 for (uint32_t merge_block_id = LoopMergeBlock(bb_id); merge_block_id != 0;
172 merge_block_id = LoopMergeBlock(merge_block_id)) {
173 result++;
174 }
175
176 return result;
177 }
178
SwitchMergeBlock(uint32_t bb_id)179 uint32_t StructuredCFGAnalysis::SwitchMergeBlock(uint32_t bb_id) {
180 uint32_t header_id = ContainingSwitch(bb_id);
181 if (header_id == 0) {
182 return 0;
183 }
184
185 BasicBlock* header = context_->cfg()->block(header_id);
186 Instruction* merge_inst = header->GetMergeInst();
187 return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
188 }
189
IsContinueBlock(uint32_t bb_id)190 bool StructuredCFGAnalysis::IsContinueBlock(uint32_t bb_id) {
191 assert(bb_id != 0);
192 return LoopContinueBlock(bb_id) == bb_id;
193 }
194
IsInContainingLoopsContinueConstruct(uint32_t bb_id)195 bool StructuredCFGAnalysis::IsInContainingLoopsContinueConstruct(
196 uint32_t bb_id) {
197 auto it = bb_to_construct_.find(bb_id);
198 if (it == bb_to_construct_.end()) {
199 return false;
200 }
201 return it->second.in_continue;
202 }
203
IsInContinueConstruct(uint32_t bb_id)204 bool StructuredCFGAnalysis::IsInContinueConstruct(uint32_t bb_id) {
205 while (bb_id != 0) {
206 if (IsInContainingLoopsContinueConstruct(bb_id)) {
207 return true;
208 }
209 bb_id = ContainingLoop(bb_id);
210 }
211 return false;
212 }
213
IsMergeBlock(uint32_t bb_id)214 bool StructuredCFGAnalysis::IsMergeBlock(uint32_t bb_id) {
215 return merge_blocks_.Get(bb_id);
216 }
217
218 std::unordered_set<uint32_t>
FindFuncsCalledFromContinue()219 StructuredCFGAnalysis::FindFuncsCalledFromContinue() {
220 std::unordered_set<uint32_t> called_from_continue;
221 std::queue<uint32_t> funcs_to_process;
222
223 // First collect the functions that are called directly from a continue
224 // construct.
225 for (Function& func : *context_->module()) {
226 for (auto& bb : func) {
227 if (IsInContainingLoopsContinueConstruct(bb.id())) {
228 for (const Instruction& inst : bb) {
229 if (inst.opcode() == SpvOpFunctionCall) {
230 funcs_to_process.push(inst.GetSingleWordInOperand(0));
231 }
232 }
233 }
234 }
235 }
236
237 // Now collect all of the functions that are indirectly called as well.
238 while (!funcs_to_process.empty()) {
239 uint32_t func_id = funcs_to_process.front();
240 funcs_to_process.pop();
241 Function* func = context_->GetFunction(func_id);
242 if (called_from_continue.insert(func_id).second) {
243 context_->AddCalls(func, &funcs_to_process);
244 }
245 }
246 return called_from_continue;
247 }
248
249 } // namespace opt
250 } // namespace spvtools
251