blob: de7b98cb0259d51da5ddcb6fd723e5cadbc25dc2 [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"
Greg Fischerbba812f2017-05-04 20:55:53 -060018#include "cfa.h"
Greg Fischer04fcc662016-11-10 10:11:50 -070019
20// Indices of operands in SPIR-V instructions
21
22static const int kSpvEntryPointFunctionId = 1;
23static 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
Greg Fischer04fcc662016-11-10 10:11:50 -0700432bool InlinePass::Inline(ir::Function* func) {
433 bool modified = false;
434 // Using block iterators here because of block erasures and insertions.
Greg Fischerbba812f2017-05-04 20:55:53 -0600435 for (auto bi = func->begin(); bi != func->end(); ++bi) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700436 for (auto ii = bi->begin(); ii != bi->end();) {
David Netoceb1d4f2017-03-31 10:36:58 -0400437 if (IsInlinableFunctionCall(&*ii)) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700438 // Inline call.
439 std::vector<std::unique_ptr<ir::BasicBlock>> newBlocks;
440 std::vector<std::unique_ptr<ir::Instruction>> newVars;
441 GenInlineCode(&newBlocks, &newVars, ii, bi);
442 // Update phi functions in successor blocks if call block
443 // is replaced with more than one block.
444 if (newBlocks.size() > 1) {
445 const auto firstBlk = newBlocks.begin();
446 const auto lastBlk = newBlocks.end() - 1;
Greg Fischerbba812f2017-05-04 20:55:53 -0600447 const uint32_t firstId = (*firstBlk)->id();
448 const uint32_t lastId = (*lastBlk)->id();
David Netoceb1d4f2017-03-31 10:36:58 -0400449 (*lastBlk)->ForEachSuccessorLabel(
450 [&firstId, &lastId, this](uint32_t succ) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700451 ir::BasicBlock* sbp = this->id2block_[succ];
452 sbp->ForEachPhiInst([&firstId, &lastId](ir::Instruction* phi) {
453 phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
454 if (*id == firstId) *id = lastId;
455 });
456 });
457 });
458 }
459 // Replace old calling block with new block(s).
460 bi = bi.Erase();
461 bi = bi.InsertBefore(&newBlocks);
462 // Insert new function variables.
463 if (newVars.size() > 0) func->begin()->begin().InsertBefore(&newVars);
464 // Restart inlining at beginning of calling block.
465 ii = bi->begin();
466 modified = true;
467 } else {
Greg Fischerbba812f2017-05-04 20:55:53 -0600468 ++ii;
Greg Fischer04fcc662016-11-10 10:11:50 -0700469 }
470 }
471 }
472 return modified;
473}
474
Greg Fischerbba812f2017-05-04 20:55:53 -0600475bool InlinePass::HasMultipleReturns(ir::Function* func) {
476 bool seenReturn = false;
477 bool multipleReturns = false;
478 for (auto& blk : *func) {
479 auto terminal_ii = blk.cend();
480 --terminal_ii;
481 if (terminal_ii->opcode() == SpvOpReturn ||
482 terminal_ii->opcode() == SpvOpReturnValue) {
483 if (seenReturn) {
484 multipleReturns = true;
485 break;
486 }
487 seenReturn = true;
488 }
489 }
490 return multipleReturns;
491}
492
493uint32_t InlinePass::MergeBlockIdIfAny(const ir::BasicBlock& blk) {
494 auto merge_ii = blk.cend();
495 --merge_ii;
496 uint32_t mbid = 0;
497 if (merge_ii != blk.cbegin()) {
498 --merge_ii;
499 if (merge_ii->opcode() == SpvOpLoopMerge)
500 mbid = merge_ii->GetSingleWordOperand(kSpvLoopMergeMergeBlockId);
501 else if (merge_ii->opcode() == SpvOpSelectionMerge)
502 mbid = merge_ii->GetSingleWordOperand(kSpvSelectionMergeMergeBlockId);
503 }
504 return mbid;
505}
506
507void InlinePass::ComputeStructuredSuccessors(ir::Function* func) {
508 // If header, make merge block first successor.
509 for (auto& blk : *func) {
510 uint32_t mbid = MergeBlockIdIfAny(blk);
511 if (mbid != 0)
512 block2structured_succs_[&blk].push_back(id2block_[mbid]);
513 // add true successors
514 blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
515 block2structured_succs_[&blk].push_back(id2block_[sbid]);
516 });
517 }
518}
519
520InlinePass::GetBlocksFunction InlinePass::StructuredSuccessorsFunction() {
521 return [this](const ir::BasicBlock* block) {
522 return &(block2structured_succs_[block]);
523 };
524}
525
526bool InlinePass::HasNoReturnInLoop(ir::Function* func) {
527 // If control not structured, do not do loop/return analysis
528 // TODO: Analyze returns in non-structured control flow
529 if (!module_->HasCapability(SpvCapabilityShader))
530 return false;
531 // Compute structured block order. This order has the property
532 // that dominators are before all blocks they dominate and merge blocks
533 // are after all blocks that are in the control constructs of their header.
534 ComputeStructuredSuccessors(func);
535 auto ignore_block = [](cbb_ptr) {};
536 auto ignore_edge = [](cbb_ptr, cbb_ptr) {};
537 std::list<const ir::BasicBlock*> structuredOrder;
538 spvtools::CFA<ir::BasicBlock>::DepthFirstTraversal(
539 &*func->begin(), StructuredSuccessorsFunction(), ignore_block,
540 [&](cbb_ptr b) { structuredOrder.push_front(b); }, ignore_edge);
541 // Search for returns in loops. Only need to track outermost loop
542 bool return_in_loop = false;
543 uint32_t outerLoopMergeId = 0;
544 for (auto& blk : structuredOrder) {
545 // Exiting current outer loop
546 if (blk->id() == outerLoopMergeId)
547 outerLoopMergeId = 0;
548 // Return block
549 auto terminal_ii = blk->cend();
550 --terminal_ii;
551 if (terminal_ii->opcode() == SpvOpReturn ||
552 terminal_ii->opcode() == SpvOpReturnValue) {
553 if (outerLoopMergeId != 0) {
554 return_in_loop = true;
555 break;
556 }
557 }
558 else if (terminal_ii != blk->cbegin()) {
559 auto merge_ii = terminal_ii;
560 --merge_ii;
561 // Entering outermost loop
562 if (merge_ii->opcode() == SpvOpLoopMerge && outerLoopMergeId == 0)
563 outerLoopMergeId = merge_ii->GetSingleWordOperand(
564 kSpvLoopMergeMergeBlockId);
565 }
566 }
567 return !return_in_loop;
568}
569
570void InlinePass::AnalyzeReturns(ir::Function* func) {
571 // Look for multiple returns
572 if (!HasMultipleReturns(func)) {
573 no_return_in_loop_.insert(func->result_id());
574 return;
575 }
576 early_return_.insert(func->result_id());
577 // If multiple returns, see if any are in a loop
578 if (HasNoReturnInLoop(func))
579 no_return_in_loop_.insert(func->result_id());
580}
581
582bool InlinePass::IsInlinableFunction(ir::Function* func) {
GregFa107d342017-04-25 13:57:20 -0600583 // We can only inline a function if it has blocks.
584 if (func->cbegin() == func->cend())
585 return false;
Greg Fischerbba812f2017-05-04 20:55:53 -0600586 // Do not inline functions with returns in loops. Currently early return
587 // functions are inlined by wrapping them in a one trip loop and implementing
588 // the returns as a branch to the loop's merge block. However, this can only
589 // done validly if the return was not in a loop in the original function.
590 // Also remember functions with multiple (early) returns.
591 AnalyzeReturns(func);
592 const auto ci = no_return_in_loop_.find(func->result_id());
593 return ci != no_return_in_loop_.cend();
GregFa107d342017-04-25 13:57:20 -0600594}
595
Greg Fischer04fcc662016-11-10 10:11:50 -0700596void InlinePass::Initialize(ir::Module* module) {
597 def_use_mgr_.reset(new analysis::DefUseManager(consumer(), module));
598
599 // Initialize next unused Id.
600 next_id_ = module->id_bound();
601
602 // Save module.
603 module_ = module;
604
Greg Fischerbba812f2017-05-04 20:55:53 -0600605 false_id_ = 0;
606
Greg Fischer04fcc662016-11-10 10:11:50 -0700607 id2function_.clear();
608 id2block_.clear();
Greg Fischerbba812f2017-05-04 20:55:53 -0600609 block2structured_succs_.clear();
GregFa107d342017-04-25 13:57:20 -0600610 inlinable_.clear();
Greg Fischer04fcc662016-11-10 10:11:50 -0700611 for (auto& fn : *module_) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600612 // Initialize function and block maps.
Greg Fischer04fcc662016-11-10 10:11:50 -0700613 id2function_[fn.result_id()] = &fn;
614 for (auto& blk : fn) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600615 id2block_[blk.id()] = &blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700616 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600617 // Compute inlinability
GregFa107d342017-04-25 13:57:20 -0600618 if (IsInlinableFunction(&fn))
619 inlinable_.insert(fn.result_id());
Greg Fischer04fcc662016-11-10 10:11:50 -0700620 }
621};
622
623Pass::Status InlinePass::ProcessImpl() {
624 // Do exhaustive inlining on each entry point function in module
625 bool modified = false;
626 for (auto& e : module_->entry_points()) {
627 ir::Function* fn =
628 id2function_[e.GetSingleWordOperand(kSpvEntryPointFunctionId)];
David Neto760789f2017-06-20 15:57:47 -0400629 modified = Inline(fn) || modified;
Greg Fischer04fcc662016-11-10 10:11:50 -0700630 }
631
632 FinalizeNextId(module_);
633
634 return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
635}
636
637InlinePass::InlinePass()
638 : module_(nullptr), def_use_mgr_(nullptr), next_id_(0) {}
639
640Pass::Status InlinePass::Process(ir::Module* module) {
641 Initialize(module);
642 return ProcessImpl();
643}
644
645} // namespace opt
646} // namespace spvtools