| 243 | } |
| 244 | |
| 245 | BasicBlock* BasicBlock::SplitBasicBlock(IRContext* context, uint32_t label_id, |
| 246 | iterator iter) { |
| 247 | assert(!insts_.empty()); |
| 248 | |
| 249 | std::unique_ptr<BasicBlock> new_block_temp = MakeUnique<BasicBlock>( |
| 250 | MakeUnique<Instruction>(context, spv::Op::OpLabel, 0, label_id, |
| 251 | std::initializer_list<Operand>{})); |
| 252 | BasicBlock* new_block = new_block_temp.get(); |
| 253 | function_->InsertBasicBlockAfter(std::move(new_block_temp), this); |
| 254 | |
| 255 | new_block->insts_.Splice(new_block->end(), &insts_, iter, end()); |
| 256 | assert(new_block->GetParent() == GetParent() && |
| 257 | "The parent should already be set appropriately."); |
| 258 | |
| 259 | context->AnalyzeDefUse(new_block->GetLabelInst()); |
| 260 | |
| 261 | // Update the phi nodes in the successor blocks to reference the new block id. |
| 262 | const_cast<const BasicBlock*>(new_block)->ForEachSuccessorLabel( |
| 263 | [new_block, this, context](const uint32_t label) { |
| 264 | BasicBlock* target_bb = context->get_instr_block(label); |
| 265 | target_bb->ForEachPhiInst( |
| 266 | [this, new_block, context](Instruction* phi_inst) { |
| 267 | bool changed = false; |
| 268 | for (uint32_t i = 1; i < phi_inst->NumInOperands(); i += 2) { |
| 269 | if (phi_inst->GetSingleWordInOperand(i) == this->id()) { |
| 270 | changed = true; |
| 271 | phi_inst->SetInOperand(i, {new_block->id()}); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (changed) { |
| 276 | context->UpdateDefUse(phi_inst); |
| 277 | } |
| 278 | }); |
| 279 | }); |
| 280 | |
| 281 | if (context->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) { |
| 282 | context->set_instr_block(new_block->GetLabelInst(), new_block); |
| 283 | new_block->ForEachInst([new_block, context](Instruction* inst) { |
| 284 | context->set_instr_block(inst, new_block); |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | return new_block; |
| 289 | } |
| 290 | |
| 291 | } // namespace opt |
| 292 | } // namespace spvtools |
no test coverage detected