blob: 6d0379f4ea2ee09845390f0375ae320ad2827d60 [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
David Neto2a1014b2017-08-09 14:59:04 -0400231 // Check for multiple returns in the callee.
232 auto fi = multi_return_funcs_.find(calleeFn->result_id());
233 const bool multiReturn = fi != multi_return_funcs_.end();
Greg Fischerbba812f2017-05-04 20:55:53 -0600234
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();
David Neto2a1014b2017-08-09 14:59:04 -0400253 // new_blk_ptr is a new basic block in the caller. New instructions are
254 // written to it. It is created when we encounter the OpLabel
255 // of the first callee block.
Greg Fischer04fcc662016-11-10 10:11:50 -0700256 std::unique_ptr<ir::BasicBlock> new_blk_ptr;
257 calleeFn->ForEachInst([&new_blocks, &callee2caller, &call_block_itr,
258 &call_inst_itr, &new_blk_ptr, &prevInstWasReturn,
259 &returnLabelId, &returnVarId, &calleeTypeId,
David Neto2a1014b2017-08-09 14:59:04 -0400260 &multiBlocks, &postCallSB, &preCallSB, multiReturn,
Greg Fischerbba812f2017-05-04 20:55:53 -0600261 &singleTripLoopHeaderId, &singleTripLoopContinueId,
262 this](
Greg Fischer04fcc662016-11-10 10:11:50 -0700263 const ir::Instruction* cpi) {
264 switch (cpi->opcode()) {
265 case SpvOpFunction:
266 case SpvOpFunctionParameter:
267 case SpvOpVariable:
268 // Already processed
269 break;
270 case SpvOpLabel: {
271 // If previous instruction was early return, insert branch
272 // instruction to return block.
273 if (prevInstWasReturn) {
274 if (returnLabelId == 0) returnLabelId = this->TakeNextId();
275 AddBranch(returnLabelId, &new_blk_ptr);
276 prevInstWasReturn = false;
277 }
278 // Finish current block (if it exists) and get label for next block.
279 uint32_t labelId;
280 bool firstBlock = false;
281 if (new_blk_ptr != nullptr) {
282 new_blocks->push_back(std::move(new_blk_ptr));
283 // If result id is already mapped, use it, otherwise get a new
284 // one.
285 const uint32_t rid = cpi->result_id();
286 const auto mapItr = callee2caller.find(rid);
287 labelId = (mapItr != callee2caller.end()) ? mapItr->second
288 : this->TakeNextId();
289 } else {
290 // First block needs to use label of original block
291 // but map callee label in case of phi reference.
Greg Fischerbba812f2017-05-04 20:55:53 -0600292 labelId = call_block_itr->id();
Greg Fischer04fcc662016-11-10 10:11:50 -0700293 callee2caller[cpi->result_id()] = labelId;
294 firstBlock = true;
295 }
296 // Create first/next block.
297 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(labelId)));
298 if (firstBlock) {
299 // Copy contents of original caller block up to call instruction.
300 for (auto cii = call_block_itr->begin(); cii != call_inst_itr;
Greg Fischerbba812f2017-05-04 20:55:53 -0600301 ++cii) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700302 std::unique_ptr<ir::Instruction> cp_inst(new ir::Instruction(*cii));
303 // Remember same-block ops for possible regeneration.
304 if (IsSameBlockOp(&*cp_inst)) {
305 auto* sb_inst_ptr = cp_inst.get();
306 preCallSB[cp_inst->result_id()] = sb_inst_ptr;
307 }
308 new_blk_ptr->AddInstruction(std::move(cp_inst));
309 }
David Neto2a1014b2017-08-09 14:59:04 -0400310 // If callee has multiple returns, insert header block for
311 // single-trip loop that will encompass callee code. Start postheader
Greg Fischerbba812f2017-05-04 20:55:53 -0600312 // block.
David Neto2a1014b2017-08-09 14:59:04 -0400313 if (multiReturn) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600314 singleTripLoopHeaderId = this->TakeNextId();
315 AddBranch(singleTripLoopHeaderId, &new_blk_ptr);
316 new_blocks->push_back(std::move(new_blk_ptr));
317 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(
318 singleTripLoopHeaderId)));
319 returnLabelId = this->TakeNextId();
320 singleTripLoopContinueId = this->TakeNextId();
321 AddLoopMerge(returnLabelId, singleTripLoopContinueId, &new_blk_ptr);
322 uint32_t postHeaderId = this->TakeNextId();
323 AddBranch(postHeaderId, &new_blk_ptr);
324 new_blocks->push_back(std::move(new_blk_ptr));
325 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(postHeaderId)));
326 multiBlocks = true;
327 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700328 } else {
329 multiBlocks = true;
330 }
331 } break;
332 case SpvOpReturnValue: {
333 // Store return value to return variable.
334 assert(returnVarId != 0);
335 uint32_t valId = cpi->GetInOperand(kSpvReturnValueId).words[0];
336 const auto mapItr = callee2caller.find(valId);
337 if (mapItr != callee2caller.end()) {
338 valId = mapItr->second;
339 }
340 AddStore(returnVarId, valId, &new_blk_ptr);
341
342 // Remember we saw a return; if followed by a label, will need to
343 // insert branch.
344 prevInstWasReturn = true;
345 } break;
346 case SpvOpReturn: {
347 // Remember we saw a return; if followed by a label, will need to
348 // insert branch.
349 prevInstWasReturn = true;
350 } break;
351 case SpvOpFunctionEnd: {
David Neto2a1014b2017-08-09 14:59:04 -0400352 // If there was an early return, we generated a return label id
353 // for it. Now we have to generate the return block with that Id.
Greg Fischer04fcc662016-11-10 10:11:50 -0700354 if (returnLabelId != 0) {
David Neto2a1014b2017-08-09 14:59:04 -0400355 // If previous instruction was return, insert branch instruction
356 // to return block.
Greg Fischer04fcc662016-11-10 10:11:50 -0700357 if (prevInstWasReturn) AddBranch(returnLabelId, &new_blk_ptr);
David Neto2a1014b2017-08-09 14:59:04 -0400358 if (multiReturn) {
359 // If we generated a loop header to for the single-trip loop
360 // to accommodate multiple returns, insert the continue
361 // target block now, with a false branch back to the loop header.
362 new_blocks->push_back(std::move(new_blk_ptr));
363 new_blk_ptr.reset(
364 new ir::BasicBlock(NewLabel(singleTripLoopContinueId)));
365 AddBranchCond(GetFalseId(), singleTripLoopHeaderId, returnLabelId,
366 &new_blk_ptr);
367 }
368 // Generate the return block.
Greg Fischerbba812f2017-05-04 20:55:53 -0600369 new_blocks->push_back(std::move(new_blk_ptr));
Greg Fischer04fcc662016-11-10 10:11:50 -0700370 new_blk_ptr.reset(new ir::BasicBlock(NewLabel(returnLabelId)));
371 multiBlocks = true;
372 }
373 // Load return value into result id of call, if it exists.
374 if (returnVarId != 0) {
375 const uint32_t resId = call_inst_itr->result_id();
376 assert(resId != 0);
377 AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr);
378 }
379 // Copy remaining instructions from caller block.
380 auto cii = call_inst_itr;
Greg Fischerbba812f2017-05-04 20:55:53 -0600381 for (++cii; cii != call_block_itr->end(); ++cii) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700382 std::unique_ptr<ir::Instruction> cp_inst(new ir::Instruction(*cii));
383 // If multiple blocks generated, regenerate any same-block
384 // instruction that has not been seen in this last block.
385 if (multiBlocks) {
386 CloneSameBlockOps(&cp_inst, &postCallSB, &preCallSB, &new_blk_ptr);
387 // Remember same-block ops in this block.
388 if (IsSameBlockOp(&*cp_inst)) {
389 const uint32_t rid = cp_inst->result_id();
390 postCallSB[rid] = rid;
391 }
392 }
393 new_blk_ptr->AddInstruction(std::move(cp_inst));
394 }
395 // Finalize inline code.
396 new_blocks->push_back(std::move(new_blk_ptr));
397 } break;
398 default: {
399 // Copy callee instruction and remap all input Ids.
400 std::unique_ptr<ir::Instruction> cp_inst(new ir::Instruction(*cpi));
401 cp_inst->ForEachInId([&callee2caller, &cpi, this](uint32_t* iid) {
402 const auto mapItr = callee2caller.find(*iid);
403 if (mapItr != callee2caller.end()) {
404 *iid = mapItr->second;
Greg Fischerbba812f2017-05-04 20:55:53 -0600405 } else if (cpi->HasLabels()) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700406 const ir::Instruction* inst =
407 def_use_mgr_->id_to_defs().find(*iid)->second;
408 if (inst->opcode() == SpvOpLabel) {
409 // Forward label reference. Allocate a new label id, map it,
410 // use it and check for it at each label.
411 const uint32_t nid = this->TakeNextId();
412 callee2caller[*iid] = nid;
413 *iid = nid;
414 }
415 }
416 });
417 // Map and reset result id.
418 const uint32_t rid = cp_inst->result_id();
419 if (rid != 0) {
420 const uint32_t nid = this->TakeNextId();
421 callee2caller[rid] = nid;
422 cp_inst->SetResultId(nid);
423 }
424 new_blk_ptr->AddInstruction(std::move(cp_inst));
425 } break;
426 }
427 });
428 // Update block map given replacement blocks.
429 for (auto& blk : *new_blocks) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600430 id2block_[blk->id()] = &*blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700431 }
432}
433
David Netoceb1d4f2017-03-31 10:36:58 -0400434bool InlinePass::IsInlinableFunctionCall(const ir::Instruction* inst) {
435 if (inst->opcode() != SpvOp::SpvOpFunctionCall) return false;
GregFa107d342017-04-25 13:57:20 -0600436 const uint32_t calleeFnId =
437 inst->GetSingleWordOperand(kSpvFunctionCallFunctionId);
438 const auto ci = inlinable_.find(calleeFnId);
439 return ci != inlinable_.cend();
David Netoceb1d4f2017-03-31 10:36:58 -0400440}
441
GregFe28bd392017-08-01 17:20:13 -0600442void InlinePass::UpdateSucceedingPhis(
443 std::vector<std::unique_ptr<ir::BasicBlock>>& new_blocks) {
444 const auto firstBlk = new_blocks.begin();
445 const auto lastBlk = new_blocks.end() - 1;
446 const uint32_t firstId = (*firstBlk)->id();
447 const uint32_t lastId = (*lastBlk)->id();
448 (*lastBlk)->ForEachSuccessorLabel(
449 [&firstId, &lastId, this](uint32_t succ) {
450 ir::BasicBlock* sbp = this->id2block_[succ];
451 sbp->ForEachPhiInst([&firstId, &lastId](ir::Instruction* phi) {
452 phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
453 if (*id == firstId) *id = lastId;
454 });
455 });
456 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700457}
458
Greg Fischerbba812f2017-05-04 20:55:53 -0600459bool InlinePass::HasMultipleReturns(ir::Function* func) {
460 bool seenReturn = false;
461 bool multipleReturns = false;
462 for (auto& blk : *func) {
463 auto terminal_ii = blk.cend();
464 --terminal_ii;
465 if (terminal_ii->opcode() == SpvOpReturn ||
466 terminal_ii->opcode() == SpvOpReturnValue) {
467 if (seenReturn) {
468 multipleReturns = true;
469 break;
470 }
471 seenReturn = true;
472 }
473 }
474 return multipleReturns;
475}
476
477uint32_t InlinePass::MergeBlockIdIfAny(const ir::BasicBlock& blk) {
478 auto merge_ii = blk.cend();
479 --merge_ii;
480 uint32_t mbid = 0;
481 if (merge_ii != blk.cbegin()) {
482 --merge_ii;
483 if (merge_ii->opcode() == SpvOpLoopMerge)
484 mbid = merge_ii->GetSingleWordOperand(kSpvLoopMergeMergeBlockId);
485 else if (merge_ii->opcode() == SpvOpSelectionMerge)
486 mbid = merge_ii->GetSingleWordOperand(kSpvSelectionMergeMergeBlockId);
487 }
488 return mbid;
489}
490
491void InlinePass::ComputeStructuredSuccessors(ir::Function* func) {
492 // If header, make merge block first successor.
493 for (auto& blk : *func) {
494 uint32_t mbid = MergeBlockIdIfAny(blk);
495 if (mbid != 0)
496 block2structured_succs_[&blk].push_back(id2block_[mbid]);
497 // add true successors
498 blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
499 block2structured_succs_[&blk].push_back(id2block_[sbid]);
500 });
501 }
502}
503
504InlinePass::GetBlocksFunction InlinePass::StructuredSuccessorsFunction() {
505 return [this](const ir::BasicBlock* block) {
506 return &(block2structured_succs_[block]);
507 };
508}
509
510bool InlinePass::HasNoReturnInLoop(ir::Function* func) {
511 // If control not structured, do not do loop/return analysis
512 // TODO: Analyze returns in non-structured control flow
513 if (!module_->HasCapability(SpvCapabilityShader))
514 return false;
515 // Compute structured block order. This order has the property
516 // that dominators are before all blocks they dominate and merge blocks
517 // are after all blocks that are in the control constructs of their header.
518 ComputeStructuredSuccessors(func);
519 auto ignore_block = [](cbb_ptr) {};
520 auto ignore_edge = [](cbb_ptr, cbb_ptr) {};
521 std::list<const ir::BasicBlock*> structuredOrder;
522 spvtools::CFA<ir::BasicBlock>::DepthFirstTraversal(
523 &*func->begin(), StructuredSuccessorsFunction(), ignore_block,
524 [&](cbb_ptr b) { structuredOrder.push_front(b); }, ignore_edge);
525 // Search for returns in loops. Only need to track outermost loop
526 bool return_in_loop = false;
527 uint32_t outerLoopMergeId = 0;
528 for (auto& blk : structuredOrder) {
529 // Exiting current outer loop
530 if (blk->id() == outerLoopMergeId)
531 outerLoopMergeId = 0;
532 // Return block
533 auto terminal_ii = blk->cend();
534 --terminal_ii;
535 if (terminal_ii->opcode() == SpvOpReturn ||
536 terminal_ii->opcode() == SpvOpReturnValue) {
537 if (outerLoopMergeId != 0) {
538 return_in_loop = true;
539 break;
540 }
541 }
542 else if (terminal_ii != blk->cbegin()) {
543 auto merge_ii = terminal_ii;
544 --merge_ii;
545 // Entering outermost loop
546 if (merge_ii->opcode() == SpvOpLoopMerge && outerLoopMergeId == 0)
547 outerLoopMergeId = merge_ii->GetSingleWordOperand(
548 kSpvLoopMergeMergeBlockId);
549 }
550 }
551 return !return_in_loop;
552}
553
554void InlinePass::AnalyzeReturns(ir::Function* func) {
555 // Look for multiple returns
556 if (!HasMultipleReturns(func)) {
557 no_return_in_loop_.insert(func->result_id());
558 return;
559 }
David Neto2a1014b2017-08-09 14:59:04 -0400560 multi_return_funcs_.insert(func->result_id());
Greg Fischerbba812f2017-05-04 20:55:53 -0600561 // If multiple returns, see if any are in a loop
562 if (HasNoReturnInLoop(func))
563 no_return_in_loop_.insert(func->result_id());
564}
565
566bool InlinePass::IsInlinableFunction(ir::Function* func) {
GregFa107d342017-04-25 13:57:20 -0600567 // We can only inline a function if it has blocks.
568 if (func->cbegin() == func->cend())
569 return false;
Greg Fischerbba812f2017-05-04 20:55:53 -0600570 // Do not inline functions with returns in loops. Currently early return
571 // functions are inlined by wrapping them in a one trip loop and implementing
572 // the returns as a branch to the loop's merge block. However, this can only
573 // done validly if the return was not in a loop in the original function.
574 // Also remember functions with multiple (early) returns.
575 AnalyzeReturns(func);
GregFe28bd392017-08-01 17:20:13 -0600576 return no_return_in_loop_.find(func->result_id()) !=
577 no_return_in_loop_.cend();
GregFa107d342017-04-25 13:57:20 -0600578}
579
GregFe28bd392017-08-01 17:20:13 -0600580void InlinePass::InitializeInline(ir::Module* module) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700581 def_use_mgr_.reset(new analysis::DefUseManager(consumer(), module));
582
583 // Initialize next unused Id.
584 next_id_ = module->id_bound();
585
586 // Save module.
587 module_ = module;
588
Greg Fischerbba812f2017-05-04 20:55:53 -0600589 false_id_ = 0;
590
GregFe28bd392017-08-01 17:20:13 -0600591 // clear collections
Greg Fischer04fcc662016-11-10 10:11:50 -0700592 id2function_.clear();
593 id2block_.clear();
Greg Fischerbba812f2017-05-04 20:55:53 -0600594 block2structured_succs_.clear();
GregFa107d342017-04-25 13:57:20 -0600595 inlinable_.clear();
GregFe28bd392017-08-01 17:20:13 -0600596 no_return_in_loop_.clear();
David Neto2a1014b2017-08-09 14:59:04 -0400597 multi_return_funcs_.clear();
GregFe28bd392017-08-01 17:20:13 -0600598
Greg Fischer04fcc662016-11-10 10:11:50 -0700599 for (auto& fn : *module_) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600600 // Initialize function and block maps.
Greg Fischer04fcc662016-11-10 10:11:50 -0700601 id2function_[fn.result_id()] = &fn;
602 for (auto& blk : fn) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600603 id2block_[blk.id()] = &blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700604 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600605 // Compute inlinability
GregFa107d342017-04-25 13:57:20 -0600606 if (IsInlinableFunction(&fn))
607 inlinable_.insert(fn.result_id());
Greg Fischer04fcc662016-11-10 10:11:50 -0700608 }
609};
610
Greg Fischer04fcc662016-11-10 10:11:50 -0700611
612InlinePass::InlinePass()
613 : module_(nullptr), def_use_mgr_(nullptr), next_id_(0) {}
614
Greg Fischer04fcc662016-11-10 10:11:50 -0700615} // namespace opt
616} // namespace spvtools