| 358 | } |
| 359 | |
| 360 | Loop* LoopFissionImpl::SplitLoop() { |
| 361 | // Clone the loop. |
| 362 | LoopUtils util{context_, loop_}; |
| 363 | LoopUtils::LoopCloningResult clone_results; |
| 364 | Loop* cloned_loop = util.CloneAndAttachLoopToHeader(&clone_results); |
| 365 | if (!cloned_loop) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
| 369 | // Update the OpLoopMerge in the cloned loop. |
| 370 | cloned_loop->UpdateLoopMergeInst(); |
| 371 | |
| 372 | // Add the loop_ to the module. |
| 373 | BasicBlock* pre_header = loop_->GetOrCreatePreHeaderBlock(); |
| 374 | if (!pre_header) { |
| 375 | return nullptr; |
| 376 | } |
| 377 | Function::iterator it = util.GetFunction()->FindBlock(pre_header->id()); |
| 378 | util.GetFunction()->AddBasicBlocks(clone_results.cloned_bb_.begin(), |
| 379 | clone_results.cloned_bb_.end(), ++it); |
| 380 | loop_->SetPreHeaderBlock(cloned_loop->GetMergeBlock()); |
| 381 | |
| 382 | std::vector<Instruction*> instructions_to_kill{}; |
| 383 | |
| 384 | // Kill all the instructions which should appear in the cloned loop but not in |
| 385 | // the original loop. |
| 386 | for (uint32_t id : loop_->GetBlocks()) { |
| 387 | BasicBlock* block = context_->cfg()->block(id); |
| 388 | |
| 389 | for (Instruction& inst : *block) { |
| 390 | // If the instruction appears in the cloned loop instruction group, kill |
| 391 | // it. |
| 392 | if (cloned_loop_instructions_.count(&inst) == 1 && |
| 393 | original_loop_instructions_.count(&inst) == 0) { |
| 394 | instructions_to_kill.push_back(&inst); |
| 395 | if (inst.opcode() == spv::Op::OpPhi) { |
| 396 | context_->ReplaceAllUsesWith( |
| 397 | inst.result_id(), clone_results.value_map_[inst.result_id()]); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Kill all instructions which should appear in the original loop and not in |
| 404 | // the cloned loop. |
| 405 | for (uint32_t id : cloned_loop->GetBlocks()) { |
| 406 | BasicBlock* block = context_->cfg()->block(id); |
| 407 | for (Instruction& inst : *block) { |
| 408 | Instruction* old_inst = clone_results.ptr_map_[&inst]; |
| 409 | // If the instruction belongs to the original loop instruction group, kill |
| 410 | // it. |
| 411 | if (cloned_loop_instructions_.count(old_inst) == 0 && |
| 412 | original_loop_instructions_.count(old_inst) == 1) { |
| 413 | instructions_to_kill.push_back(&inst); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 |
no test coverage detected