/src/shaderc/third_party/spirv-tools/source/opt/function.cpp
Line | Count | Source |
1 | | // Copyright (c) 2016 Google 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/opt/function.h" |
16 | | |
17 | | #include <ostream> |
18 | | |
19 | | #include "ir_context.h" |
20 | | #include "source/util/bit_vector.h" |
21 | | |
22 | | namespace spvtools { |
23 | | namespace opt { |
24 | | |
25 | 0 | Function* Function::Clone(IRContext* ctx) const { |
26 | 0 | Function* clone = |
27 | 0 | new Function(std::unique_ptr<Instruction>(DefInst().Clone(ctx))); |
28 | 0 | clone->params_.reserve(params_.size()); |
29 | 0 | ForEachParam( |
30 | 0 | [clone, ctx](const Instruction* inst) { |
31 | 0 | clone->AddParameter(std::unique_ptr<Instruction>(inst->Clone(ctx))); |
32 | 0 | }, |
33 | 0 | true); |
34 | |
|
35 | 0 | for (const auto& i : debug_insts_in_header_) { |
36 | 0 | clone->AddDebugInstructionInHeader( |
37 | 0 | std::unique_ptr<Instruction>(i.Clone(ctx))); |
38 | 0 | } |
39 | |
|
40 | 0 | clone->blocks_.reserve(blocks_.size()); |
41 | 0 | for (const auto& b : blocks_) { |
42 | 0 | std::unique_ptr<BasicBlock> bb(b->Clone(ctx)); |
43 | 0 | if (!bb) { |
44 | 0 | delete clone; |
45 | 0 | return nullptr; |
46 | 0 | } |
47 | 0 | clone->AddBasicBlock(std::move(bb)); |
48 | 0 | } |
49 | | |
50 | 0 | clone->SetFunctionEnd(std::unique_ptr<Instruction>(EndInst()->Clone(ctx))); |
51 | |
|
52 | 0 | clone->non_semantic_.reserve(non_semantic_.size()); |
53 | 0 | for (auto& non_semantic : non_semantic_) { |
54 | 0 | clone->AddNonSemanticInstruction( |
55 | 0 | std::unique_ptr<Instruction>(non_semantic->Clone(ctx))); |
56 | 0 | } |
57 | 0 | return clone; |
58 | 0 | } |
59 | | |
60 | | void Function::ForEachInst(const std::function<void(Instruction*)>& f, |
61 | | bool run_on_debug_line_insts, |
62 | 2.66k | bool run_on_non_semantic_insts) { |
63 | 2.66k | WhileEachInst( |
64 | 435k | [&f](Instruction* inst) { |
65 | 435k | f(inst); |
66 | 435k | return true; |
67 | 435k | }, |
68 | 2.66k | run_on_debug_line_insts, run_on_non_semantic_insts); |
69 | 2.66k | } |
70 | | |
71 | | void Function::ForEachInst(const std::function<void(const Instruction*)>& f, |
72 | | bool run_on_debug_line_insts, |
73 | 332 | bool run_on_non_semantic_insts) const { |
74 | 332 | WhileEachInst( |
75 | 47.7k | [&f](const Instruction* inst) { |
76 | 47.7k | f(inst); |
77 | 47.7k | return true; |
78 | 47.7k | }, |
79 | 332 | run_on_debug_line_insts, run_on_non_semantic_insts); |
80 | 332 | } |
81 | | |
82 | | bool Function::WhileEachInst(const std::function<bool(Instruction*)>& f, |
83 | | bool run_on_debug_line_insts, |
84 | 3.74k | bool run_on_non_semantic_insts) { |
85 | 3.74k | if (def_inst_) { |
86 | 3.74k | if (!def_inst_->WhileEachInst(f, run_on_debug_line_insts)) { |
87 | 0 | return false; |
88 | 0 | } |
89 | 3.74k | } |
90 | | |
91 | 3.74k | for (auto& param : params_) { |
92 | 1.58k | if (!param->WhileEachInst(f, run_on_debug_line_insts)) { |
93 | 0 | return false; |
94 | 0 | } |
95 | 1.58k | } |
96 | | |
97 | 3.74k | if (!debug_insts_in_header_.empty()) { |
98 | 0 | Instruction* di = &debug_insts_in_header_.front(); |
99 | 0 | while (di != nullptr) { |
100 | 0 | Instruction* next_instruction = di->NextNode(); |
101 | 0 | if (!di->WhileEachInst(f, run_on_debug_line_insts)) return false; |
102 | 0 | di = next_instruction; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | 56.8k | for (auto& bb : blocks_) { |
107 | 56.8k | if (!bb->WhileEachInst(f, run_on_debug_line_insts)) { |
108 | 0 | return false; |
109 | 0 | } |
110 | 56.8k | } |
111 | | |
112 | 3.74k | if (end_inst_) { |
113 | 3.74k | if (!end_inst_->WhileEachInst(f, run_on_debug_line_insts)) { |
114 | 0 | return false; |
115 | 0 | } |
116 | 3.74k | } |
117 | | |
118 | 3.74k | if (run_on_non_semantic_insts) { |
119 | 2.18k | for (auto& non_semantic : non_semantic_) { |
120 | 0 | if (!non_semantic->WhileEachInst(f, run_on_debug_line_insts)) { |
121 | 0 | return false; |
122 | 0 | } |
123 | 0 | } |
124 | 2.18k | } |
125 | | |
126 | 3.74k | return true; |
127 | 3.74k | } |
128 | | |
129 | | bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f, |
130 | | bool run_on_debug_line_insts, |
131 | 332 | bool run_on_non_semantic_insts) const { |
132 | 332 | if (def_inst_) { |
133 | 332 | if (!static_cast<const Instruction*>(def_inst_.get()) |
134 | 332 | ->WhileEachInst(f, run_on_debug_line_insts)) { |
135 | 0 | return false; |
136 | 0 | } |
137 | 332 | } |
138 | | |
139 | 332 | for (const auto& param : params_) { |
140 | 0 | if (!static_cast<const Instruction*>(param.get()) |
141 | 0 | ->WhileEachInst(f, run_on_debug_line_insts)) { |
142 | 0 | return false; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | 332 | for (const auto& di : debug_insts_in_header_) { |
147 | 0 | if (!static_cast<const Instruction*>(&di)->WhileEachInst( |
148 | 0 | f, run_on_debug_line_insts)) |
149 | 0 | return false; |
150 | 0 | } |
151 | | |
152 | 6.25k | for (const auto& bb : blocks_) { |
153 | 6.25k | if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst( |
154 | 6.25k | f, run_on_debug_line_insts)) { |
155 | 0 | return false; |
156 | 0 | } |
157 | 6.25k | } |
158 | | |
159 | 332 | if (end_inst_) { |
160 | 332 | if (!static_cast<const Instruction*>(end_inst_.get()) |
161 | 332 | ->WhileEachInst(f, run_on_debug_line_insts)) { |
162 | 0 | return false; |
163 | 0 | } |
164 | 332 | } |
165 | | |
166 | 332 | if (run_on_non_semantic_insts) { |
167 | 292 | for (auto& non_semantic : non_semantic_) { |
168 | 0 | if (!static_cast<const Instruction*>(non_semantic.get()) |
169 | 0 | ->WhileEachInst(f, run_on_debug_line_insts)) { |
170 | 0 | return false; |
171 | 0 | } |
172 | 0 | } |
173 | 292 | } |
174 | | |
175 | 332 | return true; |
176 | 332 | } |
177 | | |
178 | | void Function::ForEachParam(const std::function<void(Instruction*)>& f, |
179 | 1.20k | bool run_on_debug_line_insts) { |
180 | 1.20k | for (auto& param : params_) |
181 | 352 | static_cast<Instruction*>(param.get()) |
182 | 352 | ->ForEachInst(f, run_on_debug_line_insts); |
183 | 1.20k | } |
184 | | |
185 | | void Function::ForEachParam(const std::function<void(const Instruction*)>& f, |
186 | 912 | bool run_on_debug_line_insts) const { |
187 | 912 | for (const auto& param : params_) |
188 | 0 | static_cast<const Instruction*>(param.get()) |
189 | 0 | ->ForEachInst(f, run_on_debug_line_insts); |
190 | 912 | } |
191 | | |
192 | | void Function::ForEachDebugInstructionsInHeader( |
193 | 1.05k | const std::function<void(Instruction*)>& f) { |
194 | 1.05k | if (debug_insts_in_header_.empty()) return; |
195 | | |
196 | 0 | Instruction* di = &debug_insts_in_header_.front(); |
197 | 0 | while (di != nullptr) { |
198 | 0 | Instruction* next_instruction = di->NextNode(); |
199 | 0 | di->ForEachInst(f); |
200 | 0 | di = next_instruction; |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | | BasicBlock* Function::InsertBasicBlockAfter( |
205 | 16 | std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) { |
206 | 16 | for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) { |
207 | 16 | if (&*bb_iter == position) { |
208 | 16 | new_block->SetParent(this); |
209 | 16 | ++bb_iter; |
210 | 16 | bb_iter = bb_iter.InsertBefore(std::move(new_block)); |
211 | 16 | return &*bb_iter; |
212 | 16 | } |
213 | 16 | } |
214 | 16 | assert(false && "Could not find insertion point."); |
215 | 0 | return nullptr; |
216 | 16 | } |
217 | | |
218 | | BasicBlock* Function::InsertBasicBlockBefore( |
219 | 0 | std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) { |
220 | 0 | for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) { |
221 | 0 | if (&*bb_iter == position) { |
222 | 0 | new_block->SetParent(this); |
223 | 0 | bb_iter = bb_iter.InsertBefore(std::move(new_block)); |
224 | 0 | return &*bb_iter; |
225 | 0 | } |
226 | 0 | } |
227 | 0 | assert(false && "Could not find insertion point."); |
228 | 0 | return nullptr; |
229 | 0 | } |
230 | | |
231 | 0 | bool Function::HasEarlyReturn() const { |
232 | 0 | auto post_dominator_analysis = |
233 | 0 | blocks_.front()->GetLabel()->context()->GetPostDominatorAnalysis(this); |
234 | 0 | for (auto& block : blocks_) { |
235 | 0 | if (spvOpcodeIsReturn(block->tail()->opcode()) && |
236 | 0 | !post_dominator_analysis->Dominates(block.get(), entry().get())) { |
237 | 0 | return true; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | return false; |
241 | 0 | } |
242 | | |
243 | 306 | bool Function::IsRecursive() const { |
244 | 306 | IRContext* ctx = blocks_.front()->GetLabel()->context(); |
245 | 306 | IRContext::ProcessFunction mark_visited = [this](Function* fp) { |
246 | 216 | return fp == this; |
247 | 216 | }; |
248 | | |
249 | | // Process the call tree from all of the function called by |this|. If it get |
250 | | // back to |this|, then we have a recursive function. |
251 | 306 | std::queue<uint32_t> roots; |
252 | 306 | ctx->AddCalls(this, &roots); |
253 | 306 | return ctx->ProcessCallTreeFromRoots(mark_visited, &roots); |
254 | 306 | } |
255 | | |
256 | 0 | std::ostream& operator<<(std::ostream& str, const Function& func) { |
257 | 0 | str << func.PrettyPrint(); |
258 | 0 | return str; |
259 | 0 | } |
260 | | |
261 | 0 | void Function::Dump() const { |
262 | 0 | std::cerr << "Function #" << result_id() << "\n" << *this << "\n"; |
263 | 0 | } |
264 | | |
265 | 0 | std::string Function::PrettyPrint(uint32_t options) const { |
266 | 0 | std::ostringstream str; |
267 | 0 | ForEachInst([&str, options](const Instruction* inst) { |
268 | 0 | str << inst->PrettyPrint(options); |
269 | 0 | if (inst->opcode() != spv::Op::OpFunctionEnd) { |
270 | 0 | str << std::endl; |
271 | 0 | } |
272 | 0 | }); |
273 | 0 | return str.str(); |
274 | 0 | } |
275 | | |
276 | 4 | void Function::ReorderBasicBlocksInStructuredOrder() { |
277 | 4 | std::list<BasicBlock*> order; |
278 | 4 | IRContext* context = this->def_inst_->context(); |
279 | 4 | context->cfg()->ComputeStructuredOrder(this, blocks_[0].get(), &order); |
280 | 4 | ReorderBasicBlocks(order.begin(), order.end()); |
281 | 4 | } |
282 | | |
283 | | } // namespace opt |
284 | | } // namespace spvtools |