blob: 78117dbfe17f06243531e69fd9a78c4a6b332abf [file] [log] [blame]
Greg Fischer04fcc662016-11-10 10:11:50 -07001// Copyright (c) 2017 The Khronos Group Inc.
2// Copyright (c) 2017 Valve Corporation
3// Copyright (c) 2017 LunarG Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "inline_pass.h"
GregFe28bd392017-08-01 17:20:13 -060018
Greg Fischerbba812f2017-05-04 20:55:53 -060019#include "cfa.h"
Greg Fischer04fcc662016-11-10 10:11:50 -070020
21// Indices of operands in SPIR-V instructions
22
Greg Fischer04fcc662016-11-10 10:11:50 -070023static const int kSpvFunctionCallFunctionId = 2;
24static const int kSpvFunctionCallArgumentId = 3;
25static const int kSpvReturnValueId = 0;
26static const int kSpvTypePointerStorageClass = 1;
27static const int kSpvTypePointerTypeId = 2;
Greg Fischerbba812f2017-05-04 20:55:53 -060028static const int kSpvLoopMergeMergeBlockId = 0;
29static const int kSpvSelectionMergeMergeBlockId = 0;
Greg Fischer04fcc662016-11-10 10:11:50 -070030
31namespace spvtools {
32namespace opt {
33
34uint32_t InlinePass::FindPointerToType(uint32_t type_id,
35 SpvStorageClass storage_class) {
36 ir::Module::inst_iterator type_itr = module_->types_values_begin();
37 for (; type_itr != module_->types_values_end(); ++type_itr) {
38 const ir::Instruction* type_inst = &*type_itr;
39 if (type_inst->opcode() == SpvOpTypePointer &&
40 type_inst->GetSingleWordOperand(kSpvTypePointerTypeId) == type_id &&
41 type_inst->GetSingleWordOperand(kSpvTypePointerStorageClass) ==
42 storage_class)
43 return type_inst->result_id();
44 }
45 return 0;
46}
47
48uint32_t InlinePass::AddPointerToType(uint32_t type_id,
49 SpvStorageClass storage_class) {
50 uint32_t resultId = TakeNextId();
51 std::unique_ptr<ir::Instruction> type_inst(new ir::Instruction(
52 SpvOpTypePointer, 0, resultId,
53 {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
54 {uint32_t(storage_class)}},
55 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {type_id}}}));
56 module_->AddType(std::move(type_inst));
57 return resultId;
58}
59
60void InlinePass::AddBranch(uint32_t label_id,
Greg Fischerbba812f2017-05-04 20:55:53 -060061 std::unique_ptr<ir::BasicBlock>* block_ptr) {
Greg Fischer04fcc662016-11-10 10:11:50 -070062 std::unique_ptr<ir::Instruction> newBranch(new ir::Instruction(
Greg Fischerbba812f2017-05-04 20:55:53 -060063 SpvOpBranch, 0, 0,
64 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {label_id}}}));
Greg Fischer04fcc662016-11-10 10:11:50 -070065 (*block_ptr)->AddInstruction(std::move(newBranch));
66}
67
Greg Fischerbba812f2017-05-04 20:55:53 -060068void InlinePass::AddBranchCond(uint32_t cond_id, uint32_t true_id,
69 uint32_t false_id, std::unique_ptr<ir::BasicBlock>* block_ptr) {
70 std::unique_ptr<ir::Instruction> newBranch(new ir::Instruction(
71 SpvOpBranchConditional, 0, 0,
72 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cond_id}},
73 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {true_id}},
74 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {false_id}}}));
75 (*block_ptr)->AddInstruction(std::move(newBranch));
76}
77
78void InlinePass::AddLoopMerge(uint32_t merge_id, uint32_t continue_id,
79 std::unique_ptr<ir::BasicBlock>* block_ptr) {
80 std::unique_ptr<ir::Instruction> newLoopMerge(new ir::Instruction(
81 SpvOpLoopMerge, 0, 0,
82 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {merge_id}},
83 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {continue_id}},
84 {spv_operand_type_t::SPV_OPERAND_TYPE_LOOP_CONTROL, {0}}}));
85 (*block_ptr)->AddInstruction(std::move(newLoopMerge));
86}
87
Greg Fischer04fcc662016-11-10 10:11:50 -070088void InlinePass::AddStore(uint32_t ptr_id, uint32_t val_id,
89 std::unique_ptr<ir::BasicBlock>* block_ptr) {
90 std::unique_ptr<ir::Instruction> newStore(new ir::Instruction(
91 SpvOpStore, 0, 0, {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}},
92 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {val_id}}}));
93 (*block_ptr)->AddInstruction(std::move(newStore));
94}
95
96void InlinePass::AddLoad(uint32_t type_id, uint32_t resultId, uint32_t ptr_id,
97 std::unique_ptr<ir::BasicBlock>* block_ptr) {
98 std::unique_ptr<ir::Instruction> newLoad(new ir::Instruction(
99 SpvOpLoad, type_id, resultId,
100 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}}));
101 (*block_ptr)->AddInstruction(std::move(newLoad));
102}
103
104std::unique_ptr<ir::Instruction> InlinePass::NewLabel(uint32_t label_id) {
105 std::unique_ptr<ir::Instruction> newLabel(
106 new ir::Instruction(SpvOpLabel, 0, label_id, {}));
107 return newLabel;
108}
109
Greg Fischerbba812f2017-05-04 20:55:53 -0600110uint32_t InlinePass::GetFalseId() {
111 if (false_id_ != 0)
112 return false_id_;
113 false_id_ = module_->GetGlobalValue(SpvOpConstantFalse);
114 if (false_id_ != 0)
115 return false_id_;
116 uint32_t boolId = module_->GetGlobalValue(SpvOpTypeBool);
117 if (boolId == 0) {
118 boolId = TakeNextId();
119 module_->AddGlobalValue(SpvOpTypeBool, boolId, 0);
120 }
121 false_id_ = TakeNextId();
122 module_->AddGlobalValue(SpvOpConstantFalse, false_id_, boolId);
123 return false_id_;
124}
125
Greg Fischer04fcc662016-11-10 10:11:50 -0700126void InlinePass::MapParams(
127 ir::Function* calleeFn,
128 ir::UptrVectorIterator<ir::Instruction> call_inst_itr,
129 std::unordered_map<uint32_t, uint32_t>* callee2caller) {
130 int param_idx = 0;
131 calleeFn->ForEachParam(
132 [&call_inst_itr, &param_idx, &callee2caller](const ir::Instruction* cpi) {
133 const uint32_t pid = cpi->result_id();
134 (*callee2caller)[pid] = call_inst_itr->GetSingleWordOperand(
135 kSpvFunctionCallArgumentId + param_idx);
Greg Fischerbba812f2017-05-04 20:55:53 -0600136 ++param_idx;
Greg Fischer04fcc662016-11-10 10:11:50 -0700137 });
138}
139
140void InlinePass::CloneAndMapLocals(
141 ir::Function* calleeFn,
142 std::vector<std::unique_ptr<ir::Instruction>>* new_vars,
143 std::unordered_map<uint32_t, uint32_t>* callee2caller) {
144 auto callee_block_itr = calleeFn->begin();
145 auto callee_var_itr = callee_block_itr->begin();
146 while (callee_var_itr->opcode() == SpvOp::SpvOpVariable) {
147 std::unique_ptr<ir::Instruction> var_inst(
148 new ir::Instruction(*callee_var_itr));
149 uint32_t newId = TakeNextId();
150 var_inst->SetResultId(newId);
151 (*callee2caller)[callee_var_itr->result_id()] = newId;
152 new_vars->push_back(std::move(var_inst));
Greg Fischerbba812f2017-05-04 20:55:53 -0600153 ++callee_var_itr;
Greg Fischer04fcc662016-11-10 10:11:50 -0700154 }
155}
156
157uint32_t InlinePass::CreateReturnVar(
158 ir::Function* calleeFn,
159 std::vector<std::unique_ptr<ir::Instruction>>* new_vars) {
160 uint32_t returnVarId = 0;
161 const uint32_t calleeTypeId = calleeFn->type_id();
162 const ir::Instruction* calleeType =
163 def_use_mgr_->id_to_defs().find(calleeTypeId)->second;
164 if (calleeType->opcode() != SpvOpTypeVoid) {
165 // Find or create ptr to callee return type.
166 uint32_t returnVarTypeId =
167 FindPointerToType(calleeTypeId, SpvStorageClassFunction);
168 if (returnVarTypeId == 0)
169 returnVarTypeId = AddPointerToType(calleeTypeId, SpvStorageClassFunction);
170 // Add return var to new function scope variables.
171 returnVarId = TakeNextId();
172 std::unique_ptr<ir::Instruction> var_inst(new ir::Instruction(
173 SpvOpVariable, returnVarTypeId, returnVarId,
174 {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
175 {SpvStorageClassFunction}}}));
176 new_vars->push_back(std::move(var_inst));
177 }
178 return returnVarId;
179}
180
181bool InlinePass::IsSameBlockOp(const ir::Instruction* inst) const {
182 return inst->opcode() == SpvOpSampledImage || inst->opcode() == SpvOpImage;
183}
184
185void InlinePass::CloneSameBlockOps(
186 std::unique_ptr<ir::Instruction>* inst,
187 std::unordered_map<uint32_t, uint32_t>* postCallSB,
188 std::unordered_map<uint32_t, ir::Instruction*>* preCallSB,
189 std::unique_ptr<ir::BasicBlock>* block_ptr) {
190 (*inst)
191 ->ForEachInId([&postCallSB, &preCallSB, &block_ptr, this](uint32_t* iid) {
192 const auto mapItr = (*postCallSB).find(*iid);
193 if (mapItr == (*postCallSB).end()) {
194 const auto mapItr2 = (*preCallSB).find(*iid);
195 if (mapItr2 != (*preCallSB).end()) {
196 // Clone pre-call same-block ops, map result id.
197 const ir::Instruction* inInst = mapItr2->second;
198 std::unique_ptr<ir::Instruction> sb_inst(
199 new ir::Instruction(*inInst));
200 CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr);
201 const uint32_t rid = sb_inst->result_id();
202 const uint32_t nid = this->TakeNextId();
203 sb_inst->SetResultId(nid);
204 (*postCallSB)[rid] = nid;
205 *iid = nid;
206 (*block_ptr)->AddInstruction(std::move(sb_inst));
207 }
208 } else {
209 // Reset same-block op operand.
210 *iid = mapItr->second;
211 }
212 });
213}
214
215void InlinePass::GenInlineCode(
216 std::vector<std::unique_ptr<ir::BasicBlock>>* new_blocks,
217 std::vector<std::unique_ptr<ir::Instruction>>* new_vars,
218 ir::UptrVectorIterator<ir::Instruction> call_inst_itr,
219 ir::UptrVectorIterator<ir::BasicBlock> call_block_itr) {
220 // Map from all ids in the callee to their equivalent id in the caller
221 // as callee instructions are copied into caller.
222 std::unordered_map<uint32_t, uint32_t> callee2caller;
223 // Pre-call same-block insts
224 std::unordered_map<uint32_t, ir::Instruction*> preCallSB;
225 // Post-call same-block op ids
226 std::unordered_map<uint32_t, uint32_t> postCallSB;
227
228 ir::Function* calleeFn = id2function_[call_inst_itr->GetSingleWordOperand(
229 kSpvFunctionCallFunctionId)];
230
Greg Fischerbba812f2017-05-04 20:55:53 -0600231 // Check for early returns
232 auto fi = early_return_.find(calleeFn->result_id());
233 bool earlyReturn = fi != early_return_.end();
234
Greg Fischer04fcc662016-11-10 10:11:50 -0700235 // Map parameters to actual arguments.
236 MapParams(calleeFn, call_inst_itr, &callee2caller);
237
238 // Define caller local variables for all callee variables and create map to
239 // them.
240 CloneAndMapLocals(calleeFn, new_vars, &callee2caller);
241
242 // Create return var if needed.
243 uint32_t returnVarId = CreateReturnVar(calleeFn, new_vars);
244
245 // Clone and map callee code. Copy caller block code to beginning of
246 // first block and end of last block.
247 bool prevInstWasReturn = false;
Greg Fischerbba812f2017-05-04 20:55:53 -0600248 uint32_t singleTripLoopHeaderId = 0;
249 uint32_t singleTripLoopContinueId = 0;
Greg Fischer04fcc662016-11-10 10:11:50 -0700250 uint32_t returnLabelId = 0;
251 bool multiBlocks = false;
252 const uint32_t calleeTypeId = calleeFn->type_id();
253 std::unique_ptr<ir::BasicBlock> new_blk_ptr;
254 calleeFn->ForEachInst([&new_blocks, &callee2caller, &call_block_itr,
255 &call_inst_itr, &new_blk_ptr, &prevInstWasReturn,
256 &returnLabelId, &returnVarId, &calleeTypeId,
Greg Fischerbba812f2017-05-04 20:55:53 -0600257 &multiBlocks, &postCallSB, &preCallSB, &earlyReturn,
258 &singleTripLoopHeaderId, &singleTripLoopContinueId,
259 this](
Greg Fischer04fcc662016-11-10 10:11:50 -0700260 const ir::Instruction* cpi) {
261 switch (cpi->opcode()) {
262 case SpvOpFunction:
263 case SpvOpFunctionParameter:
264 case SpvOpVariable:
265 // Already processed
266 break;
267 case SpvOpLabel: {
268 // If previous instruction was early return, insert branch
269 // instruction to return block.
270 if (prevInstWasReturn) {
271 if (returnLabelId == 0) returnLabelId = this->TakeNextId();
272 AddBranch(returnLabelId, &new_blk_ptr);
273 prevInstWasReturn = false;
274 }
275 // Finish current block (if it exists) and get label for next block.
276 uint32_t labelId;
277 bool firstBlock = false;
278 if (new_blk_ptr != nullptr) {
279 new_blocks->push_back(std::move(new_blk_ptr));
280 // If result id is already mapped, use it, otherwise get a new
281 // one.
282 const uint32_t rid = cpi->result_id();
283 const auto mapItr = callee2caller.find(rid);
284 labelId = (mapItr != callee2caller.end()) ? mapItr->second
285 : this->TakeNextId();
286 } else {
287 // First block needs to use label of original block
288 // but map callee label in case of phi reference.
Greg Fischerbba812f2017-05-04 20:55:53 -0600289 labelId = call_block_itr->id();
Greg Fischer04fcc662016-11-10 10:11:50 -0700290 callee2caller[cpi->result_id()] = labelId;
291 firstBlock = true;
292 }
293 // Create first/next block.
294 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(labelId)));
295 if (firstBlock) {
296 // Copy contents of original caller block up to call instruction.
297 for (auto cii = call_block_itr->begin(); cii != call_inst_itr;
Greg Fischerbba812f2017-05-04 20:55:53 -0600298 ++cii) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700299 std::unique_ptr<ir::Instruction> cp_inst(new ir::Instruction(*cii));
300 // Remember same-block ops for possible regeneration.
301 if (IsSameBlockOp(&*cp_inst)) {
302 auto* sb_inst_ptr = cp_inst.get();
303 preCallSB[cp_inst->result_id()] = sb_inst_ptr;
304 }
305 new_blk_ptr->AddInstruction(std::move(cp_inst));
306 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600307 // If callee is early return function, insert header block for
308 // one-trip loop that will encompass callee code. Start postheader
309 // block.
310 if (earlyReturn) {
311 singleTripLoopHeaderId = this->TakeNextId();
312 AddBranch(singleTripLoopHeaderId, &new_blk_ptr);
313 new_blocks->push_back(std::move(new_blk_ptr));
314 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(
315 singleTripLoopHeaderId)));
316 returnLabelId = this->TakeNextId();
317 singleTripLoopContinueId = this->TakeNextId();
318 AddLoopMerge(returnLabelId, singleTripLoopContinueId, &new_blk_ptr);
319 uint32_t postHeaderId = this->TakeNextId();
320 AddBranch(postHeaderId, &new_blk_ptr);
321 new_blocks->push_back(std::move(new_blk_ptr));
322 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(postHeaderId)));
323 multiBlocks = true;
324 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700325 } else {
326 multiBlocks = true;
327 }
328 } break;
329 case SpvOpReturnValue: {
330 // Store return value to return variable.
331 assert(returnVarId != 0);
332 uint32_t valId = cpi->GetInOperand(kSpvReturnValueId).words[0];
333 const auto mapItr = callee2caller.find(valId);
334 if (mapItr != callee2caller.end()) {
335 valId = mapItr->second;
336 }
337 AddStore(returnVarId, valId, &new_blk_ptr);
338
339 // Remember we saw a return; if followed by a label, will need to
340 // insert branch.
341 prevInstWasReturn = true;
342 } break;
343 case SpvOpReturn: {
344 // Remember we saw a return; if followed by a label, will need to
345 // insert branch.
346 prevInstWasReturn = true;
347 } break;
348 case SpvOpFunctionEnd: {
Greg Fischerbba812f2017-05-04 20:55:53 -0600349 // If there was an early return, insert continue and return blocks.
Greg Fischer04fcc662016-11-10 10:11:50 -0700350 // If previous instruction was return, insert branch instruction
351 // to return block.
352 if (returnLabelId != 0) {
353 if (prevInstWasReturn) AddBranch(returnLabelId, &new_blk_ptr);
354 new_blocks->push_back(std::move(new_blk_ptr));
Greg Fischerbba812f2017-05-04 20:55:53 -0600355 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(
356 singleTripLoopContinueId)));
357 AddBranchCond(GetFalseId(), singleTripLoopHeaderId, returnLabelId,
358 &new_blk_ptr);
359 new_blocks->push_back(std::move(new_blk_ptr));
Greg Fischer04fcc662016-11-10 10:11:50 -0700360 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(returnLabelId)));
361 multiBlocks = true;
362 }
363 // Load return value into result id of call, if it exists.
364 if (returnVarId != 0) {
365 const uint32_t resId = call_inst_itr->result_id();
366 assert(resId != 0);
367 AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr);
368 }
369 // Copy remaining instructions from caller block.
370 auto cii = call_inst_itr;
Greg Fischerbba812f2017-05-04 20:55:53 -0600371 for (++cii; cii != call_block_itr->end(); ++cii) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700372 std::unique_ptr<ir::Instruction> cp_inst(new ir::Instruction(*cii));
373 // If multiple blocks generated, regenerate any same-block
374 // instruction that has not been seen in this last block.
375 if (multiBlocks) {
376 CloneSameBlockOps(&cp_inst, &postCallSB, &preCallSB, &new_blk_ptr);
377 // Remember same-block ops in this block.
378 if (IsSameBlockOp(&*cp_inst)) {
379 const uint32_t rid = cp_inst->result_id();
380 postCallSB[rid] = rid;
381 }
382 }
383 new_blk_ptr->AddInstruction(std::move(cp_inst));
384 }
385 // Finalize inline code.
386 new_blocks->push_back(std::move(new_blk_ptr));
387 } break;
388 default: {
389 // Copy callee instruction and remap all input Ids.
390 std::unique_ptr<ir::Instruction> cp_inst(new ir::Instruction(*cpi));
391 cp_inst->ForEachInId([&callee2caller, &cpi, this](uint32_t* iid) {
392 const auto mapItr = callee2caller.find(*iid);
393 if (mapItr != callee2caller.end()) {
394 *iid = mapItr->second;
Greg Fischerbba812f2017-05-04 20:55:53 -0600395 } else if (cpi->HasLabels()) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700396 const ir::Instruction* inst =
397 def_use_mgr_->id_to_defs().find(*iid)->second;
398 if (inst->opcode() == SpvOpLabel) {
399 // Forward label reference. Allocate a new label id, map it,
400 // use it and check for it at each label.
401 const uint32_t nid = this->TakeNextId();
402 callee2caller[*iid] = nid;
403 *iid = nid;
404 }
405 }
406 });
407 // Map and reset result id.
408 const uint32_t rid = cp_inst->result_id();
409 if (rid != 0) {
410 const uint32_t nid = this->TakeNextId();
411 callee2caller[rid] = nid;
412 cp_inst->SetResultId(nid);
413 }
414 new_blk_ptr->AddInstruction(std::move(cp_inst));
415 } break;
416 }
417 });
418 // Update block map given replacement blocks.
419 for (auto& blk : *new_blocks) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600420 id2block_[blk->id()] = &*blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700421 }
422}
423
David Netoceb1d4f2017-03-31 10:36:58 -0400424bool InlinePass::IsInlinableFunctionCall(const ir::Instruction* inst) {
425 if (inst->opcode() != SpvOp::SpvOpFunctionCall) return false;
GregFa107d342017-04-25 13:57:20 -0600426 const uint32_t calleeFnId =
427 inst->GetSingleWordOperand(kSpvFunctionCallFunctionId);
428 const auto ci = inlinable_.find(calleeFnId);
429 return ci != inlinable_.cend();
David Netoceb1d4f2017-03-31 10:36:58 -0400430}
431
GregFe28bd392017-08-01 17:20:13 -0600432void InlinePass::UpdateSucceedingPhis(
433 std::vector<std::unique_ptr<ir::BasicBlock>>& new_blocks) {
434 const auto firstBlk = new_blocks.begin();
435 const auto lastBlk = new_blocks.end() - 1;
436 const uint32_t firstId = (*firstBlk)->id();
437 const uint32_t lastId = (*lastBlk)->id();
438 (*lastBlk)->ForEachSuccessorLabel(
439 [&firstId, &lastId, this](uint32_t succ) {
440 ir::BasicBlock* sbp = this->id2block_[succ];
441 sbp->ForEachPhiInst([&firstId, &lastId](ir::Instruction* phi) {
442 phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
443 if (*id == firstId) *id = lastId;
444 });
445 });
446 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700447}
448
Greg Fischerbba812f2017-05-04 20:55:53 -0600449bool InlinePass::HasMultipleReturns(ir::Function* func) {
450 bool seenReturn = false;
451 bool multipleReturns = false;
452 for (auto& blk : *func) {
453 auto terminal_ii = blk.cend();
454 --terminal_ii;
455 if (terminal_ii->opcode() == SpvOpReturn ||
456 terminal_ii->opcode() == SpvOpReturnValue) {
457 if (seenReturn) {
458 multipleReturns = true;
459 break;
460 }
461 seenReturn = true;
462 }
463 }
464 return multipleReturns;
465}
466
467uint32_t InlinePass::MergeBlockIdIfAny(const ir::BasicBlock& blk) {
468 auto merge_ii = blk.cend();
469 --merge_ii;
470 uint32_t mbid = 0;
471 if (merge_ii != blk.cbegin()) {
472 --merge_ii;
473 if (merge_ii->opcode() == SpvOpLoopMerge)
474 mbid = merge_ii->GetSingleWordOperand(kSpvLoopMergeMergeBlockId);
475 else if (merge_ii->opcode() == SpvOpSelectionMerge)
476 mbid = merge_ii->GetSingleWordOperand(kSpvSelectionMergeMergeBlockId);
477 }
478 return mbid;
479}
480
481void InlinePass::ComputeStructuredSuccessors(ir::Function* func) {
482 // If header, make merge block first successor.
483 for (auto& blk : *func) {
484 uint32_t mbid = MergeBlockIdIfAny(blk);
485 if (mbid != 0)
486 block2structured_succs_[&blk].push_back(id2block_[mbid]);
487 // add true successors
488 blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
489 block2structured_succs_[&blk].push_back(id2block_[sbid]);
490 });
491 }
492}
493
494InlinePass::GetBlocksFunction InlinePass::StructuredSuccessorsFunction() {
495 return [this](const ir::BasicBlock* block) {
496 return &(block2structured_succs_[block]);
497 };
498}
499
500bool InlinePass::HasNoReturnInLoop(ir::Function* func) {
501 // If control not structured, do not do loop/return analysis
502 // TODO: Analyze returns in non-structured control flow
503 if (!module_->HasCapability(SpvCapabilityShader))
504 return false;
505 // Compute structured block order. This order has the property
506 // that dominators are before all blocks they dominate and merge blocks
507 // are after all blocks that are in the control constructs of their header.
508 ComputeStructuredSuccessors(func);
509 auto ignore_block = [](cbb_ptr) {};
510 auto ignore_edge = [](cbb_ptr, cbb_ptr) {};
511 std::list<const ir::BasicBlock*> structuredOrder;
512 spvtools::CFA<ir::BasicBlock>::DepthFirstTraversal(
513 &*func->begin(), StructuredSuccessorsFunction(), ignore_block,
514 [&](cbb_ptr b) { structuredOrder.push_front(b); }, ignore_edge);
515 // Search for returns in loops. Only need to track outermost loop
516 bool return_in_loop = false;
517 uint32_t outerLoopMergeId = 0;
518 for (auto& blk : structuredOrder) {
519 // Exiting current outer loop
520 if (blk->id() == outerLoopMergeId)
521 outerLoopMergeId = 0;
522 // Return block
523 auto terminal_ii = blk->cend();
524 --terminal_ii;
525 if (terminal_ii->opcode() == SpvOpReturn ||
526 terminal_ii->opcode() == SpvOpReturnValue) {
527 if (outerLoopMergeId != 0) {
528 return_in_loop = true;
529 break;
530 }
531 }
532 else if (terminal_ii != blk->cbegin()) {
533 auto merge_ii = terminal_ii;
534 --merge_ii;
535 // Entering outermost loop
536 if (merge_ii->opcode() == SpvOpLoopMerge && outerLoopMergeId == 0)
537 outerLoopMergeId = merge_ii->GetSingleWordOperand(
538 kSpvLoopMergeMergeBlockId);
539 }
540 }
541 return !return_in_loop;
542}
543
544void InlinePass::AnalyzeReturns(ir::Function* func) {
545 // Look for multiple returns
546 if (!HasMultipleReturns(func)) {
547 no_return_in_loop_.insert(func->result_id());
548 return;
549 }
550 early_return_.insert(func->result_id());
551 // If multiple returns, see if any are in a loop
552 if (HasNoReturnInLoop(func))
553 no_return_in_loop_.insert(func->result_id());
554}
555
556bool InlinePass::IsInlinableFunction(ir::Function* func) {
GregFa107d342017-04-25 13:57:20 -0600557 // We can only inline a function if it has blocks.
558 if (func->cbegin() == func->cend())
559 return false;
Greg Fischerbba812f2017-05-04 20:55:53 -0600560 // Do not inline functions with returns in loops. Currently early return
561 // functions are inlined by wrapping them in a one trip loop and implementing
562 // the returns as a branch to the loop's merge block. However, this can only
563 // done validly if the return was not in a loop in the original function.
564 // Also remember functions with multiple (early) returns.
565 AnalyzeReturns(func);
GregFe28bd392017-08-01 17:20:13 -0600566 return no_return_in_loop_.find(func->result_id()) !=
567 no_return_in_loop_.cend();
GregFa107d342017-04-25 13:57:20 -0600568}
569
GregFe28bd392017-08-01 17:20:13 -0600570void InlinePass::InitializeInline(ir::Module* module) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700571 def_use_mgr_.reset(new analysis::DefUseManager(consumer(), module));
572
573 // Initialize next unused Id.
574 next_id_ = module->id_bound();
575
576 // Save module.
577 module_ = module;
578
Greg Fischerbba812f2017-05-04 20:55:53 -0600579 false_id_ = 0;
580
GregFe28bd392017-08-01 17:20:13 -0600581 // clear collections
Greg Fischer04fcc662016-11-10 10:11:50 -0700582 id2function_.clear();
583 id2block_.clear();
Greg Fischerbba812f2017-05-04 20:55:53 -0600584 block2structured_succs_.clear();
GregFa107d342017-04-25 13:57:20 -0600585 inlinable_.clear();
GregFe28bd392017-08-01 17:20:13 -0600586 no_return_in_loop_.clear();
587 early_return_.clear();
588
Greg Fischer04fcc662016-11-10 10:11:50 -0700589 for (auto& fn : *module_) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600590 // Initialize function and block maps.
Greg Fischer04fcc662016-11-10 10:11:50 -0700591 id2function_[fn.result_id()] = &fn;
592 for (auto& blk : fn) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600593 id2block_[blk.id()] = &blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700594 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600595 // Compute inlinability
GregFa107d342017-04-25 13:57:20 -0600596 if (IsInlinableFunction(&fn))
597 inlinable_.insert(fn.result_id());
Greg Fischer04fcc662016-11-10 10:11:50 -0700598 }
599};
600
Greg Fischer04fcc662016-11-10 10:11:50 -0700601
602InlinePass::InlinePass()
603 : module_(nullptr), def_use_mgr_(nullptr), next_id_(0) {}
604
Greg Fischer04fcc662016-11-10 10:11:50 -0700605} // namespace opt
606} // namespace spvtools