blob: eaf29aa05348f000b428f2f3581629bb5329f61b [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
dan sinclaireda2cfb2018-08-03 15:06:09 -040017#include "source/opt/inline_pass.h"
GregFe28bd392017-08-01 17:20:13 -060018
dan sinclaireda2cfb2018-08-03 15:06:09 -040019#include <unordered_set>
20#include <utility>
21
22#include "source/cfa.h"
Steven Perronbd0a2da2020-05-14 10:55:47 -040023#include "source/opt/reflect.h"
dan sinclair1963a2d2018-08-14 15:01:50 -040024#include "source/util/make_unique.h"
Greg Fischer04fcc662016-11-10 10:11:50 -070025
26// Indices of operands in SPIR-V instructions
27
Greg Fischer04fcc662016-11-10 10:11:50 -070028static const int kSpvFunctionCallFunctionId = 2;
29static const int kSpvFunctionCallArgumentId = 3;
30static const int kSpvReturnValueId = 0;
Greg Fischer04fcc662016-11-10 10:11:50 -070031
32namespace spvtools {
33namespace opt {
34
Greg Fischer04fcc662016-11-10 10:11:50 -070035uint32_t InlinePass::AddPointerToType(uint32_t type_id,
36 SpvStorageClass storage_class) {
Steven Perronacd27812018-12-18 19:34:03 +000037 uint32_t resultId = context()->TakeNextId();
38 if (resultId == 0) {
39 return resultId;
40 }
41
dan sinclairc7da51a2018-07-12 15:14:43 -040042 std::unique_ptr<Instruction> type_inst(
43 new Instruction(context(), SpvOpTypePointer, 0, resultId,
44 {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
45 {uint32_t(storage_class)}},
46 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {type_id}}}));
Steven Perron476cae62017-10-30 11:13:24 -040047 context()->AddType(std::move(type_inst));
Alan Baker61690852017-12-08 15:33:19 -050048 analysis::Type* pointeeTy;
49 std::unique_ptr<analysis::Pointer> pointerTy;
50 std::tie(pointeeTy, pointerTy) =
51 context()->get_type_mgr()->GetTypeAndPointerType(type_id,
52 SpvStorageClassFunction);
53 context()->get_type_mgr()->RegisterType(resultId, *pointerTy);
Greg Fischer04fcc662016-11-10 10:11:50 -070054 return resultId;
55}
56
57void InlinePass::AddBranch(uint32_t label_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040058 std::unique_ptr<BasicBlock>* block_ptr) {
59 std::unique_ptr<Instruction> newBranch(
60 new Instruction(context(), SpvOpBranch, 0, 0,
61 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {label_id}}}));
Greg Fischer04fcc662016-11-10 10:11:50 -070062 (*block_ptr)->AddInstruction(std::move(newBranch));
63}
64
Greg Fischerbba812f2017-05-04 20:55:53 -060065void InlinePass::AddBranchCond(uint32_t cond_id, uint32_t true_id,
Diego Novillod2938e42017-11-08 12:40:02 -050066 uint32_t false_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040067 std::unique_ptr<BasicBlock>* block_ptr) {
68 std::unique_ptr<Instruction> newBranch(
69 new Instruction(context(), SpvOpBranchConditional, 0, 0,
70 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cond_id}},
71 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {true_id}},
72 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {false_id}}}));
Greg Fischerbba812f2017-05-04 20:55:53 -060073 (*block_ptr)->AddInstruction(std::move(newBranch));
74}
75
76void InlinePass::AddLoopMerge(uint32_t merge_id, uint32_t continue_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040077 std::unique_ptr<BasicBlock>* block_ptr) {
78 std::unique_ptr<Instruction> newLoopMerge(new Instruction(
Alan Bakera7717132017-11-14 14:11:50 -050079 context(), SpvOpLoopMerge, 0, 0,
Greg Fischerbba812f2017-05-04 20:55:53 -060080 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {merge_id}},
81 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {continue_id}},
82 {spv_operand_type_t::SPV_OPERAND_TYPE_LOOP_CONTROL, {0}}}));
83 (*block_ptr)->AddInstruction(std::move(newLoopMerge));
84}
85
Greg Fischer04fcc662016-11-10 10:11:50 -070086void InlinePass::AddStore(uint32_t ptr_id, uint32_t val_id,
Jaebaek Seo50b15572020-05-21 13:09:43 -040087 std::unique_ptr<BasicBlock>* block_ptr,
88 const Instruction* line_inst,
89 const DebugScope& dbg_scope) {
dan sinclairc7da51a2018-07-12 15:14:43 -040090 std::unique_ptr<Instruction> newStore(
91 new Instruction(context(), SpvOpStore, 0, 0,
92 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}},
93 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {val_id}}}));
Jaebaek Seo50b15572020-05-21 13:09:43 -040094 if (line_inst != nullptr) {
95 newStore->dbg_line_insts().push_back(*line_inst);
96 }
97 newStore->SetDebugScope(dbg_scope);
Greg Fischer04fcc662016-11-10 10:11:50 -070098 (*block_ptr)->AddInstruction(std::move(newStore));
99}
100
101void InlinePass::AddLoad(uint32_t type_id, uint32_t resultId, uint32_t ptr_id,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400102 std::unique_ptr<BasicBlock>* block_ptr,
103 const Instruction* line_inst,
104 const DebugScope& dbg_scope) {
dan sinclairc7da51a2018-07-12 15:14:43 -0400105 std::unique_ptr<Instruction> newLoad(
106 new Instruction(context(), SpvOpLoad, type_id, resultId,
107 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}}));
Jaebaek Seo50b15572020-05-21 13:09:43 -0400108 if (line_inst != nullptr) {
109 newLoad->dbg_line_insts().push_back(*line_inst);
110 }
111 newLoad->SetDebugScope(dbg_scope);
Greg Fischer04fcc662016-11-10 10:11:50 -0700112 (*block_ptr)->AddInstruction(std::move(newLoad));
113}
114
dan sinclairc7da51a2018-07-12 15:14:43 -0400115std::unique_ptr<Instruction> InlinePass::NewLabel(uint32_t label_id) {
116 std::unique_ptr<Instruction> newLabel(
117 new Instruction(context(), SpvOpLabel, 0, label_id, {}));
Greg Fischer04fcc662016-11-10 10:11:50 -0700118 return newLabel;
119}
120
Greg Fischerbba812f2017-05-04 20:55:53 -0600121uint32_t InlinePass::GetFalseId() {
Diego Novillod2938e42017-11-08 12:40:02 -0500122 if (false_id_ != 0) return false_id_;
Diego Novillo1040a952017-10-25 13:26:25 -0400123 false_id_ = get_module()->GetGlobalValue(SpvOpConstantFalse);
Diego Novillod2938e42017-11-08 12:40:02 -0500124 if (false_id_ != 0) return false_id_;
Diego Novillo1040a952017-10-25 13:26:25 -0400125 uint32_t boolId = get_module()->GetGlobalValue(SpvOpTypeBool);
Greg Fischerbba812f2017-05-04 20:55:53 -0600126 if (boolId == 0) {
Steven Perronacd27812018-12-18 19:34:03 +0000127 boolId = context()->TakeNextId();
128 if (boolId == 0) {
129 return 0;
130 }
Diego Novillo1040a952017-10-25 13:26:25 -0400131 get_module()->AddGlobalValue(SpvOpTypeBool, boolId, 0);
Greg Fischerbba812f2017-05-04 20:55:53 -0600132 }
Steven Perronacd27812018-12-18 19:34:03 +0000133 false_id_ = context()->TakeNextId();
134 if (false_id_ == 0) {
135 return 0;
136 }
Diego Novillo1040a952017-10-25 13:26:25 -0400137 get_module()->AddGlobalValue(SpvOpConstantFalse, false_id_, boolId);
Greg Fischerbba812f2017-05-04 20:55:53 -0600138 return false_id_;
139}
140
Greg Fischer04fcc662016-11-10 10:11:50 -0700141void InlinePass::MapParams(
dan sinclairc7da51a2018-07-12 15:14:43 -0400142 Function* calleeFn, BasicBlock::iterator call_inst_itr,
Greg Fischer04fcc662016-11-10 10:11:50 -0700143 std::unordered_map<uint32_t, uint32_t>* callee2caller) {
144 int param_idx = 0;
Steven Perronacd27812018-12-18 19:34:03 +0000145 calleeFn->ForEachParam(
146 [&call_inst_itr, &param_idx, &callee2caller](const Instruction* cpi) {
147 const uint32_t pid = cpi->result_id();
148 (*callee2caller)[pid] = call_inst_itr->GetSingleWordOperand(
149 kSpvFunctionCallArgumentId + param_idx);
150 ++param_idx;
151 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700152}
153
Steven Perronacd27812018-12-18 19:34:03 +0000154bool InlinePass::CloneAndMapLocals(
dan sinclairc7da51a2018-07-12 15:14:43 -0400155 Function* calleeFn, std::vector<std::unique_ptr<Instruction>>* new_vars,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400156 std::unordered_map<uint32_t, uint32_t>* callee2caller,
157 analysis::DebugInlinedAtContext* inlined_at_ctx) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700158 auto callee_block_itr = calleeFn->begin();
159 auto callee_var_itr = callee_block_itr->begin();
Jaebaek Seo50b15572020-05-21 13:09:43 -0400160 while (callee_var_itr->opcode() == SpvOp::SpvOpVariable ||
161 callee_var_itr->GetOpenCL100DebugOpcode() ==
162 OpenCLDebugInfo100DebugDeclare) {
163 if (callee_var_itr->opcode() != SpvOp::SpvOpVariable) {
164 ++callee_var_itr;
165 continue;
166 }
167
dan sinclairc7da51a2018-07-12 15:14:43 -0400168 std::unique_ptr<Instruction> var_inst(callee_var_itr->Clone(context()));
Steven Perronacd27812018-12-18 19:34:03 +0000169 uint32_t newId = context()->TakeNextId();
170 if (newId == 0) {
171 return false;
172 }
Pierre Moreau5bd55f12018-02-20 19:19:57 +0100173 get_decoration_mgr()->CloneDecorations(callee_var_itr->result_id(), newId);
Greg Fischer04fcc662016-11-10 10:11:50 -0700174 var_inst->SetResultId(newId);
Jaebaek Seo50b15572020-05-21 13:09:43 -0400175 var_inst->UpdateDebugInlinedAt(
176 context()->get_debug_info_mgr()->BuildDebugInlinedAtChain(
177 callee_var_itr->GetDebugInlinedAt(), inlined_at_ctx));
Greg Fischer04fcc662016-11-10 10:11:50 -0700178 (*callee2caller)[callee_var_itr->result_id()] = newId;
179 new_vars->push_back(std::move(var_inst));
Greg Fischerbba812f2017-05-04 20:55:53 -0600180 ++callee_var_itr;
Greg Fischer04fcc662016-11-10 10:11:50 -0700181 }
Steven Perronacd27812018-12-18 19:34:03 +0000182 return true;
Greg Fischer04fcc662016-11-10 10:11:50 -0700183}
184
185uint32_t InlinePass::CreateReturnVar(
dan sinclairc7da51a2018-07-12 15:14:43 -0400186 Function* calleeFn, std::vector<std::unique_ptr<Instruction>>* new_vars) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700187 uint32_t returnVarId = 0;
188 const uint32_t calleeTypeId = calleeFn->type_id();
Steven Perronacd27812018-12-18 19:34:03 +0000189 analysis::TypeManager* type_mgr = context()->get_type_mgr();
190 assert(type_mgr->GetType(calleeTypeId)->AsVoid() == nullptr &&
191 "Cannot create a return variable of type void.");
192 // Find or create ptr to callee return type.
193 uint32_t returnVarTypeId =
194 type_mgr->FindPointerToType(calleeTypeId, SpvStorageClassFunction);
195
196 if (returnVarTypeId == 0) {
197 returnVarTypeId = AddPointerToType(calleeTypeId, SpvStorageClassFunction);
198 if (returnVarTypeId == 0) {
199 return 0;
200 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700201 }
Steven Perronacd27812018-12-18 19:34:03 +0000202
203 // Add return var to new function scope variables.
204 returnVarId = context()->TakeNextId();
205 if (returnVarId == 0) {
206 return 0;
207 }
208
209 std::unique_ptr<Instruction> var_inst(
210 new Instruction(context(), SpvOpVariable, returnVarTypeId, returnVarId,
211 {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
212 {SpvStorageClassFunction}}}));
213 new_vars->push_back(std::move(var_inst));
Pierre Moreau5bd55f12018-02-20 19:19:57 +0100214 get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId);
Greg Fischer04fcc662016-11-10 10:11:50 -0700215 return returnVarId;
216}
217
dan sinclairc7da51a2018-07-12 15:14:43 -0400218bool InlinePass::IsSameBlockOp(const Instruction* inst) const {
Greg Fischer04fcc662016-11-10 10:11:50 -0700219 return inst->opcode() == SpvOpSampledImage || inst->opcode() == SpvOpImage;
220}
221
Steven Perronacd27812018-12-18 19:34:03 +0000222bool InlinePass::CloneSameBlockOps(
dan sinclairc7da51a2018-07-12 15:14:43 -0400223 std::unique_ptr<Instruction>* inst,
Greg Fischer04fcc662016-11-10 10:11:50 -0700224 std::unordered_map<uint32_t, uint32_t>* postCallSB,
dan sinclairc7da51a2018-07-12 15:14:43 -0400225 std::unordered_map<uint32_t, Instruction*>* preCallSB,
226 std::unique_ptr<BasicBlock>* block_ptr) {
Steven Perronacd27812018-12-18 19:34:03 +0000227 return (*inst)->WhileEachInId([&postCallSB, &preCallSB, &block_ptr,
228 this](uint32_t* iid) {
229 const auto mapItr = (*postCallSB).find(*iid);
230 if (mapItr == (*postCallSB).end()) {
231 const auto mapItr2 = (*preCallSB).find(*iid);
232 if (mapItr2 != (*preCallSB).end()) {
233 // Clone pre-call same-block ops, map result id.
234 const Instruction* inInst = mapItr2->second;
235 std::unique_ptr<Instruction> sb_inst(inInst->Clone(context()));
236 if (!CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr)) {
237 return false;
Pierre Moreau5bd55f12018-02-20 19:19:57 +0100238 }
Steven Perronacd27812018-12-18 19:34:03 +0000239
240 const uint32_t rid = sb_inst->result_id();
241 const uint32_t nid = context()->TakeNextId();
242 if (nid == 0) {
243 return false;
244 }
245 get_decoration_mgr()->CloneDecorations(rid, nid);
246 sb_inst->SetResultId(nid);
247 (*postCallSB)[rid] = nid;
248 *iid = nid;
249 (*block_ptr)->AddInstruction(std::move(sb_inst));
250 }
251 } else {
252 // Reset same-block op operand.
253 *iid = mapItr->second;
254 }
255 return true;
256 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700257}
258
Steven Perronbd0a2da2020-05-14 10:55:47 -0400259void InlinePass::MoveInstsBeforeEntryBlock(
260 std::unordered_map<uint32_t, Instruction*>* preCallSB,
261 BasicBlock* new_blk_ptr, BasicBlock::iterator call_inst_itr,
262 UptrVectorIterator<BasicBlock> call_block_itr) {
263 for (auto cii = call_block_itr->begin(); cii != call_inst_itr;
264 cii = call_block_itr->begin()) {
265 Instruction* inst = &*cii;
266 inst->RemoveFromList();
267 std::unique_ptr<Instruction> cp_inst(inst);
268 // Remember same-block ops for possible regeneration.
269 if (IsSameBlockOp(&*cp_inst)) {
270 auto* sb_inst_ptr = cp_inst.get();
271 (*preCallSB)[cp_inst->result_id()] = sb_inst_ptr;
272 }
273 new_blk_ptr->AddInstruction(std::move(cp_inst));
274 }
275}
276
277std::unique_ptr<BasicBlock> InlinePass::AddGuardBlock(
278 std::vector<std::unique_ptr<BasicBlock>>* new_blocks,
279 std::unordered_map<uint32_t, uint32_t>* callee2caller,
280 std::unique_ptr<BasicBlock> new_blk_ptr, uint32_t entry_blk_label_id) {
281 const auto guard_block_id = context()->TakeNextId();
282 if (guard_block_id == 0) {
283 return nullptr;
284 }
285 AddBranch(guard_block_id, &new_blk_ptr);
286 new_blocks->push_back(std::move(new_blk_ptr));
287 // Start the next block.
288 new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(guard_block_id));
289 // Reset the mapping of the callee's entry block to point to
290 // the guard block. Do this so we can fix up phis later on to
291 // satisfy dominance.
292 (*callee2caller)[entry_blk_label_id] = guard_block_id;
293 return new_blk_ptr;
294}
295
296InstructionList::iterator InlinePass::AddStoresForVariableInitializers(
297 const std::unordered_map<uint32_t, uint32_t>& callee2caller,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400298 analysis::DebugInlinedAtContext* inlined_at_ctx,
Steven Perronbd0a2da2020-05-14 10:55:47 -0400299 std::unique_ptr<BasicBlock>* new_blk_ptr,
300 UptrVectorIterator<BasicBlock> callee_first_block_itr) {
Jaebaek Seo50b15572020-05-21 13:09:43 -0400301 auto callee_itr = callee_first_block_itr->begin();
302 while (callee_itr->opcode() == SpvOp::SpvOpVariable ||
303 callee_itr->GetOpenCL100DebugOpcode() ==
304 OpenCLDebugInfo100DebugDeclare) {
305 if (callee_itr->opcode() == SpvOp::SpvOpVariable &&
306 callee_itr->NumInOperands() == 2) {
307 assert(callee2caller.count(callee_itr->result_id()) &&
Steven Perronbd0a2da2020-05-14 10:55:47 -0400308 "Expected the variable to have already been mapped.");
Jaebaek Seo50b15572020-05-21 13:09:43 -0400309 uint32_t new_var_id = callee2caller.at(callee_itr->result_id());
Steven Perronbd0a2da2020-05-14 10:55:47 -0400310
311 // The initializer must be a constant or global value. No mapped
312 // should be used.
Jaebaek Seo50b15572020-05-21 13:09:43 -0400313 uint32_t val_id = callee_itr->GetSingleWordInOperand(1);
314 AddStore(new_var_id, val_id, new_blk_ptr, callee_itr->dbg_line_inst(),
315 context()->get_debug_info_mgr()->BuildDebugScope(
316 callee_itr->GetDebugScope(), inlined_at_ctx));
Steven Perronbd0a2da2020-05-14 10:55:47 -0400317 }
Jaebaek Seo50b15572020-05-21 13:09:43 -0400318 if (callee_itr->GetOpenCL100DebugOpcode() ==
319 OpenCLDebugInfo100DebugDeclare) {
320 InlineSingleInstruction(
321 callee2caller, new_blk_ptr->get(), &*callee_itr,
322 context()->get_debug_info_mgr()->BuildDebugInlinedAtChain(
323 callee_itr->GetDebugScope().GetInlinedAt(), inlined_at_ctx));
324 }
325 ++callee_itr;
Steven Perronbd0a2da2020-05-14 10:55:47 -0400326 }
Jaebaek Seo50b15572020-05-21 13:09:43 -0400327 return callee_itr;
Steven Perronbd0a2da2020-05-14 10:55:47 -0400328}
329
Jaebaek Seo50b15572020-05-21 13:09:43 -0400330bool InlinePass::InlineSingleInstruction(
Steven Perronbd0a2da2020-05-14 10:55:47 -0400331 const std::unordered_map<uint32_t, uint32_t>& callee2caller,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400332 BasicBlock* new_blk_ptr, const Instruction* inst, uint32_t dbg_inlined_at) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400333 // If we have return, it must be at the end of the callee. We will handle
334 // it at the end.
335 if (inst->opcode() == SpvOpReturnValue || inst->opcode() == SpvOpReturn)
336 return true;
337
338 // Copy callee instruction and remap all input Ids.
339 std::unique_ptr<Instruction> cp_inst(inst->Clone(context()));
340 cp_inst->ForEachInId([&callee2caller](uint32_t* iid) {
341 const auto mapItr = callee2caller.find(*iid);
342 if (mapItr != callee2caller.end()) {
343 *iid = mapItr->second;
344 }
345 });
Jaebaek Seo50b15572020-05-21 13:09:43 -0400346
Steven Perronbd0a2da2020-05-14 10:55:47 -0400347 // If result id is non-zero, remap it.
348 const uint32_t rid = cp_inst->result_id();
349 if (rid != 0) {
350 const auto mapItr = callee2caller.find(rid);
Jaebaek Seo50b15572020-05-21 13:09:43 -0400351 if (mapItr == callee2caller.end()) {
352 return false;
353 }
Steven Perronbd0a2da2020-05-14 10:55:47 -0400354 uint32_t nid = mapItr->second;
355 cp_inst->SetResultId(nid);
356 get_decoration_mgr()->CloneDecorations(rid, nid);
357 }
Jaebaek Seo50b15572020-05-21 13:09:43 -0400358
359 cp_inst->UpdateDebugInlinedAt(dbg_inlined_at);
Steven Perronbd0a2da2020-05-14 10:55:47 -0400360 new_blk_ptr->AddInstruction(std::move(cp_inst));
361 return true;
362}
363
364std::unique_ptr<BasicBlock> InlinePass::InlineReturn(
365 const std::unordered_map<uint32_t, uint32_t>& callee2caller,
366 std::vector<std::unique_ptr<BasicBlock>>* new_blocks,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400367 std::unique_ptr<BasicBlock> new_blk_ptr,
368 analysis::DebugInlinedAtContext* inlined_at_ctx, Function* calleeFn,
Steven Perronbd0a2da2020-05-14 10:55:47 -0400369 const Instruction* inst, uint32_t returnVarId) {
370 // Store return value to return variable.
371 if (inst->opcode() == SpvOpReturnValue) {
372 assert(returnVarId != 0);
373 uint32_t valId = inst->GetInOperand(kSpvReturnValueId).words[0];
374 const auto mapItr = callee2caller.find(valId);
375 if (mapItr != callee2caller.end()) {
376 valId = mapItr->second;
377 }
Jaebaek Seo50b15572020-05-21 13:09:43 -0400378 AddStore(returnVarId, valId, &new_blk_ptr, inst->dbg_line_inst(),
379 context()->get_debug_info_mgr()->BuildDebugScope(
380 inst->GetDebugScope(), inlined_at_ctx));
Steven Perronbd0a2da2020-05-14 10:55:47 -0400381 }
382
383 uint32_t returnLabelId = 0;
384 for (auto callee_block_itr = calleeFn->begin();
385 callee_block_itr != calleeFn->end(); ++callee_block_itr) {
386 if (callee_block_itr->tail()->opcode() == SpvOpUnreachable ||
alan-bakerf3cec932020-07-22 11:45:02 -0400387 callee_block_itr->tail()->opcode() == SpvOpKill ||
388 callee_block_itr->tail()->opcode() == SpvOpTerminateInvocation) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400389 returnLabelId = context()->TakeNextId();
390 break;
391 }
392 }
393 if (returnLabelId == 0) return new_blk_ptr;
394
395 if (inst->opcode() == SpvOpReturn || inst->opcode() == SpvOpReturnValue)
396 AddBranch(returnLabelId, &new_blk_ptr);
397 new_blocks->push_back(std::move(new_blk_ptr));
398 return MakeUnique<BasicBlock>(NewLabel(returnLabelId));
399}
400
401bool InlinePass::InlineEntryBlock(
402 const std::unordered_map<uint32_t, uint32_t>& callee2caller,
403 std::unique_ptr<BasicBlock>* new_blk_ptr,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400404 UptrVectorIterator<BasicBlock> callee_first_block,
405 analysis::DebugInlinedAtContext* inlined_at_ctx) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400406 auto callee_inst_itr = AddStoresForVariableInitializers(
Jaebaek Seo50b15572020-05-21 13:09:43 -0400407 callee2caller, inlined_at_ctx, new_blk_ptr, callee_first_block);
Steven Perronbd0a2da2020-05-14 10:55:47 -0400408
409 while (callee_inst_itr != callee_first_block->end()) {
Jaebaek Seo50b15572020-05-21 13:09:43 -0400410 if (!InlineSingleInstruction(
411 callee2caller, new_blk_ptr->get(), &*callee_inst_itr,
412 context()->get_debug_info_mgr()->BuildDebugInlinedAtChain(
413 callee_inst_itr->GetDebugScope().GetInlinedAt(),
414 inlined_at_ctx))) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400415 return false;
416 }
417 ++callee_inst_itr;
418 }
419 return true;
420}
421
422std::unique_ptr<BasicBlock> InlinePass::InlineBasicBlocks(
423 std::vector<std::unique_ptr<BasicBlock>>* new_blocks,
424 const std::unordered_map<uint32_t, uint32_t>& callee2caller,
Jaebaek Seo50b15572020-05-21 13:09:43 -0400425 std::unique_ptr<BasicBlock> new_blk_ptr,
426 analysis::DebugInlinedAtContext* inlined_at_ctx, Function* calleeFn) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400427 auto callee_block_itr = calleeFn->begin();
428 ++callee_block_itr;
429
430 while (callee_block_itr != calleeFn->end()) {
431 new_blocks->push_back(std::move(new_blk_ptr));
432 const auto mapItr =
433 callee2caller.find(callee_block_itr->GetLabelInst()->result_id());
434 if (mapItr == callee2caller.end()) return nullptr;
435 new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(mapItr->second));
436
437 auto tail_inst_itr = callee_block_itr->end();
438 for (auto inst_itr = callee_block_itr->begin(); inst_itr != tail_inst_itr;
439 ++inst_itr) {
Jaebaek Seo50b15572020-05-21 13:09:43 -0400440 if (!InlineSingleInstruction(
441 callee2caller, new_blk_ptr.get(), &*inst_itr,
442 context()->get_debug_info_mgr()->BuildDebugInlinedAtChain(
443 inst_itr->GetDebugScope().GetInlinedAt(), inlined_at_ctx))) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400444 return nullptr;
445 }
446 }
447
448 ++callee_block_itr;
449 }
450 return new_blk_ptr;
451}
452
453bool InlinePass::MoveCallerInstsAfterFunctionCall(
454 std::unordered_map<uint32_t, Instruction*>* preCallSB,
455 std::unordered_map<uint32_t, uint32_t>* postCallSB,
456 std::unique_ptr<BasicBlock>* new_blk_ptr,
457 BasicBlock::iterator call_inst_itr, bool multiBlocks) {
458 // Copy remaining instructions from caller block.
459 for (Instruction* inst = call_inst_itr->NextNode(); inst;
460 inst = call_inst_itr->NextNode()) {
461 inst->RemoveFromList();
462 std::unique_ptr<Instruction> cp_inst(inst);
463 // If multiple blocks generated, regenerate any same-block
464 // instruction that has not been seen in this last block.
465 if (multiBlocks) {
466 if (!CloneSameBlockOps(&cp_inst, postCallSB, preCallSB, new_blk_ptr)) {
467 return false;
468 }
469
470 // Remember same-block ops in this block.
471 if (IsSameBlockOp(&*cp_inst)) {
472 const uint32_t rid = cp_inst->result_id();
473 (*postCallSB)[rid] = rid;
474 }
475 }
476 new_blk_ptr->get()->AddInstruction(std::move(cp_inst));
477 }
478
479 return true;
480}
481
482void InlinePass::MoveLoopMergeInstToFirstBlock(
483 std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
484 // Move the OpLoopMerge from the last block back to the first, where
485 // it belongs.
486 auto& first = new_blocks->front();
487 auto& last = new_blocks->back();
488 assert(first != last);
489
490 // Insert a modified copy of the loop merge into the first block.
491 auto loop_merge_itr = last->tail();
492 --loop_merge_itr;
493 assert(loop_merge_itr->opcode() == SpvOpLoopMerge);
494 std::unique_ptr<Instruction> cp_inst(loop_merge_itr->Clone(context()));
495 first->tail().InsertBefore(std::move(cp_inst));
496
497 // Remove the loop merge from the last block.
498 loop_merge_itr->RemoveFromList();
499 delete &*loop_merge_itr;
500}
501
Steven Perronacd27812018-12-18 19:34:03 +0000502bool InlinePass::GenInlineCode(
dan sinclairc7da51a2018-07-12 15:14:43 -0400503 std::vector<std::unique_ptr<BasicBlock>>* new_blocks,
504 std::vector<std::unique_ptr<Instruction>>* new_vars,
505 BasicBlock::iterator call_inst_itr,
506 UptrVectorIterator<BasicBlock> call_block_itr) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700507 // Map from all ids in the callee to their equivalent id in the caller
508 // as callee instructions are copied into caller.
509 std::unordered_map<uint32_t, uint32_t> callee2caller;
510 // Pre-call same-block insts
dan sinclairc7da51a2018-07-12 15:14:43 -0400511 std::unordered_map<uint32_t, Instruction*> preCallSB;
Greg Fischer04fcc662016-11-10 10:11:50 -0700512 // Post-call same-block op ids
513 std::unordered_map<uint32_t, uint32_t> postCallSB;
514
Jaebaek Seo50b15572020-05-21 13:09:43 -0400515 analysis::DebugInlinedAtContext inlined_at_ctx(&*call_inst_itr);
516
Steven Perronb3daa932018-03-06 11:20:28 -0500517 // Invalidate the def-use chains. They are not kept up to date while
518 // inlining. However, certain calls try to keep them up-to-date if they are
519 // valid. These operations can fail.
dan sinclairc7da51a2018-07-12 15:14:43 -0400520 context()->InvalidateAnalyses(IRContext::kAnalysisDefUse);
Steven Perronb3daa932018-03-06 11:20:28 -0500521
Steven Perronbd0a2da2020-05-14 10:55:47 -0400522 // If the caller is a loop header and the callee has multiple blocks, then the
523 // normal inlining logic will place the OpLoopMerge in the last of several
524 // blocks in the loop. Instead, it should be placed at the end of the first
525 // block. We'll wait to move the OpLoopMerge until the end of the regular
526 // inlining logic, and only if necessary.
527 bool caller_is_loop_header = call_block_itr->GetLoopMergeInst() != nullptr;
528
529 // Single-trip loop continue block
530 std::unique_ptr<BasicBlock> single_trip_loop_cont_blk;
531
dan sinclairc7da51a2018-07-12 15:14:43 -0400532 Function* calleeFn = id2function_[call_inst_itr->GetSingleWordOperand(
Greg Fischer04fcc662016-11-10 10:11:50 -0700533 kSpvFunctionCallFunctionId)];
534
535 // Map parameters to actual arguments.
536 MapParams(calleeFn, call_inst_itr, &callee2caller);
537
538 // Define caller local variables for all callee variables and create map to
539 // them.
Jaebaek Seo50b15572020-05-21 13:09:43 -0400540 if (!CloneAndMapLocals(calleeFn, new_vars, &callee2caller, &inlined_at_ctx)) {
Steven Perronacd27812018-12-18 19:34:03 +0000541 return false;
542 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700543
Steven Perronbd0a2da2020-05-14 10:55:47 -0400544 // First block needs to use label of original block
545 // but map callee label in case of phi reference.
546 uint32_t entry_blk_label_id = calleeFn->begin()->GetLabelInst()->result_id();
547 callee2caller[entry_blk_label_id] = call_block_itr->id();
548 std::unique_ptr<BasicBlock> new_blk_ptr =
549 MakeUnique<BasicBlock>(NewLabel(call_block_itr->id()));
550
551 // Move instructions of original caller block up to call instruction.
552 MoveInstsBeforeEntryBlock(&preCallSB, new_blk_ptr.get(), call_inst_itr,
553 call_block_itr);
554
555 if (caller_is_loop_header &&
556 (*(calleeFn->begin())).GetMergeInst() != nullptr) {
557 // We can't place both the caller's merge instruction and
558 // another merge instruction in the same block. So split the
559 // calling block. Insert an unconditional branch to a new guard
560 // block. Later, once we know the ID of the last block, we
561 // will move the caller's OpLoopMerge from the last generated
562 // block into the first block. We also wait to avoid
563 // invalidating various iterators.
564 new_blk_ptr = AddGuardBlock(new_blocks, &callee2caller,
565 std::move(new_blk_ptr), entry_blk_label_id);
566 if (new_blk_ptr == nullptr) return false;
567 }
568
Greg Fischer04fcc662016-11-10 10:11:50 -0700569 // Create return var if needed.
Steven Perronacd27812018-12-18 19:34:03 +0000570 const uint32_t calleeTypeId = calleeFn->type_id();
571 uint32_t returnVarId = 0;
572 analysis::Type* calleeType = context()->get_type_mgr()->GetType(calleeTypeId);
573 if (calleeType->AsVoid() == nullptr) {
574 returnVarId = CreateReturnVar(calleeFn, new_vars);
575 if (returnVarId == 0) {
576 return false;
577 }
578 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700579
Steven Perronbd0a2da2020-05-14 10:55:47 -0400580 calleeFn->WhileEachInst([&callee2caller, this](const Instruction* cpi) {
581 // Create set of callee result ids. Used to detect forward references
GregFa699d1a2017-08-29 18:35:05 -0600582 const uint32_t rid = cpi->result_id();
Steven Perronbd0a2da2020-05-14 10:55:47 -0400583 if (rid != 0 && callee2caller.find(rid) == callee2caller.end()) {
584 const uint32_t nid = context()->TakeNextId();
585 if (nid == 0) return false;
586 callee2caller[rid] = nid;
587 }
588 return true;
GregFa699d1a2017-08-29 18:35:05 -0600589 });
590
Jaebaek Seo50b15572020-05-21 13:09:43 -0400591 // Inline DebugClare instructions in the callee's header.
592 calleeFn->ForEachDebugInstructionsInHeader(
593 [&new_blk_ptr, &callee2caller, &inlined_at_ctx, this](Instruction* inst) {
594 InlineSingleInstruction(
595 callee2caller, new_blk_ptr.get(), inst,
596 context()->get_debug_info_mgr()->BuildDebugInlinedAtChain(
597 inst->GetDebugScope().GetInlinedAt(), &inlined_at_ctx));
598 });
599
Steven Perronbd0a2da2020-05-14 10:55:47 -0400600 // Inline the entry block of the callee function.
Jaebaek Seo50b15572020-05-21 13:09:43 -0400601 if (!InlineEntryBlock(callee2caller, &new_blk_ptr, calleeFn->begin(),
602 &inlined_at_ctx)) {
Steven Perronacd27812018-12-18 19:34:03 +0000603 return false;
604 }
David Netoefff5fa2017-08-31 15:47:31 -0400605
Steven Perronbd0a2da2020-05-14 10:55:47 -0400606 // Inline blocks of the callee function other than the entry block.
Jaebaek Seo50b15572020-05-21 13:09:43 -0400607 new_blk_ptr =
608 InlineBasicBlocks(new_blocks, callee2caller, std::move(new_blk_ptr),
609 &inlined_at_ctx, calleeFn);
Steven Perronbd0a2da2020-05-14 10:55:47 -0400610 if (new_blk_ptr == nullptr) return false;
David Netoefff5fa2017-08-31 15:47:31 -0400611
Jaebaek Seo50b15572020-05-21 13:09:43 -0400612 new_blk_ptr = InlineReturn(callee2caller, new_blocks, std::move(new_blk_ptr),
613 &inlined_at_ctx, calleeFn,
614 &*(calleeFn->tail()->tail()), returnVarId);
David Netoefff5fa2017-08-31 15:47:31 -0400615
Steven Perronbd0a2da2020-05-14 10:55:47 -0400616 // Load return value into result id of call, if it exists.
617 if (returnVarId != 0) {
618 const uint32_t resId = call_inst_itr->result_id();
619 assert(resId != 0);
Jaebaek Seo50b15572020-05-21 13:09:43 -0400620 AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr,
621 call_inst_itr->dbg_line_inst(), call_inst_itr->GetDebugScope());
Steven Perron3f8501d2020-08-24 15:08:55 -0400622 } else {
623 // Even though it is very unlikely, it is possible that the result id of
624 // the void-function call is used, so we need to generate an instruction
625 // with that result id.
626 std::unique_ptr<Instruction> undef_inst(
627 new Instruction(context(), SpvOpUndef, call_inst_itr->type_id(),
628 call_inst_itr->result_id(), {}));
629 context()->AddGlobalValue(std::move(undef_inst));
David Netoefff5fa2017-08-31 15:47:31 -0400630 }
631
Steven Perronbd0a2da2020-05-14 10:55:47 -0400632 // Move instructions of original caller block after call instruction.
633 if (!MoveCallerInstsAfterFunctionCall(&preCallSB, &postCallSB, &new_blk_ptr,
634 call_inst_itr,
635 calleeFn->begin() != calleeFn->end()))
636 return false;
637
638 // Finalize inline code.
639 new_blocks->push_back(std::move(new_blk_ptr));
640
641 if (caller_is_loop_header && (new_blocks->size() > 1))
642 MoveLoopMergeInstToFirstBlock(new_blocks);
643
Greg Fischer04fcc662016-11-10 10:11:50 -0700644 // Update block map given replacement blocks.
645 for (auto& blk : *new_blocks) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600646 id2block_[blk->id()] = &*blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700647 }
Steven Perronacd27812018-12-18 19:34:03 +0000648 return true;
Greg Fischer04fcc662016-11-10 10:11:50 -0700649}
650
Steven Perronaa9e8f52019-07-17 14:59:05 -0400651bool InlinePass::IsInlinableFunctionCall(const Instruction* inst) {
David Netoceb1d4f2017-03-31 10:36:58 -0400652 if (inst->opcode() != SpvOp::SpvOpFunctionCall) return false;
GregFa107d342017-04-25 13:57:20 -0600653 const uint32_t calleeFnId =
654 inst->GetSingleWordOperand(kSpvFunctionCallFunctionId);
655 const auto ci = inlinable_.find(calleeFnId);
Steven Perronbd0a2da2020-05-14 10:55:47 -0400656 if (ci == inlinable_.cend()) return false;
657
658 if (early_return_funcs_.find(calleeFnId) != early_return_funcs_.end()) {
659 // We rely on the merge-return pass to handle the early return case
660 // in advance.
661 std::string message =
662 "The function '" + id2function_[calleeFnId]->DefInst().PrettyPrint() +
663 "' could not be inlined because the return instruction "
664 "is not at the end of the function. This could be fixed by "
665 "running merge-return before inlining.";
666 consumer()(SPV_MSG_WARNING, "", {0, 0, 0}, message.c_str());
667 return false;
668 }
669
670 return true;
David Netoceb1d4f2017-03-31 10:36:58 -0400671}
672
GregFe28bd392017-08-01 17:20:13 -0600673void InlinePass::UpdateSucceedingPhis(
dan sinclairc7da51a2018-07-12 15:14:43 -0400674 std::vector<std::unique_ptr<BasicBlock>>& new_blocks) {
GregFe28bd392017-08-01 17:20:13 -0600675 const auto firstBlk = new_blocks.begin();
676 const auto lastBlk = new_blocks.end() - 1;
677 const uint32_t firstId = (*firstBlk)->id();
678 const uint32_t lastId = (*lastBlk)->id();
dan sinclairc7da51a2018-07-12 15:14:43 -0400679 const BasicBlock& const_last_block = *lastBlk->get();
David Neto87f9cfa2018-02-02 14:17:42 -0800680 const_last_block.ForEachSuccessorLabel(
681 [&firstId, &lastId, this](const uint32_t succ) {
dan sinclairc7da51a2018-07-12 15:14:43 -0400682 BasicBlock* sbp = this->id2block_[succ];
683 sbp->ForEachPhiInst([&firstId, &lastId](Instruction* phi) {
David Neto87f9cfa2018-02-02 14:17:42 -0800684 phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
685 if (*id == firstId) *id = lastId;
686 });
687 });
GregFe28bd392017-08-01 17:20:13 -0600688 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700689}
690
dan sinclairc7da51a2018-07-12 15:14:43 -0400691bool InlinePass::HasNoReturnInLoop(Function* func) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600692 // If control not structured, do not do loop/return analysis
693 // TODO: Analyze returns in non-structured control flow
Steven Perron756b2772017-12-19 14:18:13 -0500694 if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
695 return false;
greg-lunarg67214782018-11-08 07:11:20 -0700696 const auto structured_analysis = context()->GetStructuredCFGAnalysis();
697 // Search for returns in structured construct.
Greg Fischerbba812f2017-05-04 20:55:53 -0600698 bool return_in_loop = false;
greg-lunarg67214782018-11-08 07:11:20 -0700699 for (auto& blk : *func) {
700 auto terminal_ii = blk.cend();
Greg Fischerbba812f2017-05-04 20:55:53 -0600701 --terminal_ii;
greg-lunarg67214782018-11-08 07:11:20 -0700702 if (spvOpcodeIsReturn(terminal_ii->opcode()) &&
703 structured_analysis->ContainingLoop(blk.id()) != 0) {
704 return_in_loop = true;
705 break;
Greg Fischerbba812f2017-05-04 20:55:53 -0600706 }
707 }
708 return !return_in_loop;
709}
710
dan sinclairc7da51a2018-07-12 15:14:43 -0400711void InlinePass::AnalyzeReturns(Function* func) {
Steven Perronbd0a2da2020-05-14 10:55:47 -0400712 // Analyze functions without a return in loop.
greg-lunarg67214782018-11-08 07:11:20 -0700713 if (HasNoReturnInLoop(func)) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600714 no_return_in_loop_.insert(func->result_id());
Steven Perronbd0a2da2020-05-14 10:55:47 -0400715 }
716 // Analyze functions with a return before its tail basic block.
717 for (auto& blk : *func) {
718 auto terminal_ii = blk.cend();
719 --terminal_ii;
720 if (spvOpcodeIsReturn(terminal_ii->opcode()) && &blk != func->tail()) {
greg-lunarg67214782018-11-08 07:11:20 -0700721 early_return_funcs_.insert(func->result_id());
Steven Perronbd0a2da2020-05-14 10:55:47 -0400722 break;
723 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600724 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600725}
726
dan sinclairc7da51a2018-07-12 15:14:43 -0400727bool InlinePass::IsInlinableFunction(Function* func) {
GregFa107d342017-04-25 13:57:20 -0600728 // We can only inline a function if it has blocks.
Diego Novillod2938e42017-11-08 12:40:02 -0500729 if (func->cbegin() == func->cend()) return false;
Junda Liu82b378d2020-10-30 04:38:56 +0800730
731 // Do not inline functions with DontInline flag.
732 if (func->control_mask() & SpvFunctionControlDontInlineMask) {
733 return false;
734 }
735
Greg Fischerbba812f2017-05-04 20:55:53 -0600736 // Do not inline functions with returns in loops. Currently early return
737 // functions are inlined by wrapping them in a one trip loop and implementing
738 // the returns as a branch to the loop's merge block. However, this can only
739 // done validly if the return was not in a loop in the original function.
740 // Also remember functions with multiple (early) returns.
741 AnalyzeReturns(func);
Steven Perron2d2a5122018-11-29 14:24:58 -0500742 if (no_return_in_loop_.find(func->result_id()) == no_return_in_loop_.cend()) {
743 return false;
744 }
745
746 if (func->IsRecursive()) {
747 return false;
748 }
749
Steven Perronc18c9ff2019-10-04 13:05:32 -0400750 // Do not inline functions with an OpKill if they are called from a continue
751 // construct. If it is inlined into a continue construct it will generate
752 // invalid code.
753 bool func_is_called_from_continue =
754 funcs_called_from_continue_.count(func->result_id()) != 0;
Steven Perronc7a39bc2019-09-11 13:26:55 -0400755
alan-bakerf3cec932020-07-22 11:45:02 -0400756 if (func_is_called_from_continue && ContainsKillOrTerminateInvocation(func)) {
Steven Perronc7a39bc2019-09-11 13:26:55 -0400757 return false;
758 }
759
Steven Perron2d2a5122018-11-29 14:24:58 -0500760 return true;
GregFa107d342017-04-25 13:57:20 -0600761}
762
alan-bakerf3cec932020-07-22 11:45:02 -0400763bool InlinePass::ContainsKillOrTerminateInvocation(Function* func) const {
764 return !func->WhileEachInst([](Instruction* inst) {
765 const auto opcode = inst->opcode();
766 return (opcode != SpvOpKill) && (opcode != SpvOpTerminateInvocation);
767 });
Steven Perronc18c9ff2019-10-04 13:05:32 -0400768}
769
dan sinclairf96b7f12018-07-12 09:08:45 -0400770void InlinePass::InitializeInline() {
Greg Fischerbba812f2017-05-04 20:55:53 -0600771 false_id_ = 0;
772
GregFe28bd392017-08-01 17:20:13 -0600773 // clear collections
Greg Fischer04fcc662016-11-10 10:11:50 -0700774 id2function_.clear();
775 id2block_.clear();
GregFa107d342017-04-25 13:57:20 -0600776 inlinable_.clear();
GregFe28bd392017-08-01 17:20:13 -0600777 no_return_in_loop_.clear();
greg-lunarg67214782018-11-08 07:11:20 -0700778 early_return_funcs_.clear();
Steven Perronc18c9ff2019-10-04 13:05:32 -0400779 funcs_called_from_continue_ =
780 context()->GetStructuredCFGAnalysis()->FindFuncsCalledFromContinue();
GregFe28bd392017-08-01 17:20:13 -0600781
Diego Novillo1040a952017-10-25 13:26:25 -0400782 for (auto& fn : *get_module()) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600783 // Initialize function and block maps.
Greg Fischer04fcc662016-11-10 10:11:50 -0700784 id2function_[fn.result_id()] = &fn;
785 for (auto& blk : fn) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600786 id2block_[blk.id()] = &blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700787 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600788 // Compute inlinability
Diego Novillod2938e42017-11-08 12:40:02 -0500789 if (IsInlinableFunction(&fn)) inlinable_.insert(fn.result_id());
Greg Fischer04fcc662016-11-10 10:11:50 -0700790 }
Eleni Maria Stea045cc8f2018-03-21 11:15:56 +0200791}
Greg Fischer04fcc662016-11-10 10:11:50 -0700792
Diego Novillo1040a952017-10-25 13:26:25 -0400793InlinePass::InlinePass() {}
Greg Fischer04fcc662016-11-10 10:11:50 -0700794
Greg Fischer04fcc662016-11-10 10:11:50 -0700795} // namespace opt
796} // namespace spvtools