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
LoopMergeBlock(uint32_t bb_id)131 uint32_t StructuredCFGAnalysis::LoopMergeBlock(uint32_t bb_id) {
132 uint32_t header_id = ContainingLoop(bb_id);
133 if (header_id == 0) {
134 return 0;
135 }
136
137 BasicBlock* header = context_->cfg()->block(header_id);
138 Instruction* merge_inst = header->GetMergeInst();
139 return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
140 }
141
LoopContinueBlock(uint32_t bb_id)142 uint32_t StructuredCFGAnalysis::LoopContinueBlock(uint32_t bb_id) {
143 uint32_t header_id = ContainingLoop(bb_id);
144 if (header_id == 0) {
145 return 0;
146 }
147
148 BasicBlock* header = context_->cfg()->block(header_id);
149 Instruction* merge_inst = header->GetMergeInst();
150 return merge_inst->GetSingleWordInOperand(kContinueNodeIndex);
151 }
152
SwitchMergeBlock(uint32_t bb_id)153 uint32_t StructuredCFGAnalysis::SwitchMergeBlock(uint32_t bb_id) {
154 uint32_t header_id = ContainingSwitch(bb_id);
155 if (header_id == 0) {
156 return 0;
157 }
158
159 BasicBlock* header = context_->cfg()->block(header_id);
160 Instruction* merge_inst = header->GetMergeInst();
161 return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
162 }
163
IsContinueBlock(uint32_t bb_id)164 bool StructuredCFGAnalysis::IsContinueBlock(uint32_t bb_id) {
165 assert(bb_id != 0);
166 return LoopContinueBlock(bb_id) == bb_id;
167 }
168
IsInContainingLoopsContinueConstruct(uint32_t bb_id)169 bool StructuredCFGAnalysis::IsInContainingLoopsContinueConstruct(
170 uint32_t bb_id) {
171 auto it = bb_to_construct_.find(bb_id);
172 if (it == bb_to_construct_.end()) {
173 return false;
174 }
175 return it->second.in_continue;
176 }
177
IsInContinueConstruct(uint32_t bb_id)178 bool StructuredCFGAnalysis::IsInContinueConstruct(uint32_t bb_id) {
179 while (bb_id != 0) {
180 if (IsInContainingLoopsContinueConstruct(bb_id)) {
181 return true;
182 }
183 bb_id = ContainingLoop(bb_id);
184 }
185 return false;
186 }
187
IsMergeBlock(uint32_t bb_id)188 bool StructuredCFGAnalysis::IsMergeBlock(uint32_t bb_id) {
189 return merge_blocks_.Get(bb_id);
190 }
191
192 std::unordered_set<uint32_t>
FindFuncsCalledFromContinue()193 StructuredCFGAnalysis::FindFuncsCalledFromContinue() {
194 std::unordered_set<uint32_t> called_from_continue;
195 std::queue<uint32_t> funcs_to_process;
196
197 // First collect the functions that are called directly from a continue
198 // construct.
199 for (Function& func : *context_->module()) {
200 for (auto& bb : func) {
201 if (IsInContainingLoopsContinueConstruct(bb.id())) {
202 for (const Instruction& inst : bb) {
203 if (inst.opcode() == SpvOpFunctionCall) {
204 funcs_to_process.push(inst.GetSingleWordInOperand(0));
205 }
206 }
207 }
208 }
209 }
210
211 // Now collect all of the functions that are indirectly called as well.
212 while (!funcs_to_process.empty()) {
213 uint32_t func_id = funcs_to_process.front();
214 funcs_to_process.pop();
215 Function* func = context_->GetFunction(func_id);
216 if (called_from_continue.insert(func_id).second) {
217 context_->AddCalls(func, &funcs_to_process);
218 }
219 }
220 return called_from_continue;
221 }
222
223 } // namespace opt
224 } // namespace spvtools
225