diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h --- a/llvm/lib/Transforms/Vectorize/VPlan.h +++ b/llvm/lib/Transforms/Vectorize/VPlan.h @@ -307,9 +307,9 @@ /// header BasicBlock. BasicBlock *PrevBB = nullptr; - /// The last IR BasicBlock in the output IR. Set to the new latch - /// BasicBlock, used for placing the newly created BasicBlocks. - BasicBlock *LastBB = nullptr; + /// The last IR BasicBlock in the output IR. Set to the exit block of the + /// vector loop. + BasicBlock *ExitBB = nullptr; /// The IR BasicBlock that is the preheader of the vector loop in the output /// IR. diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp --- a/llvm/lib/Transforms/Vectorize/VPlan.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp @@ -252,7 +252,7 @@ // Pred stands for Predessor. Prev stands for Previous - last visited/created. BasicBlock *PrevBB = CFG.PrevBB; BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(), - PrevBB->getParent(), CFG.LastBB); + PrevBB->getParent(), CFG.ExitBB); LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n'); // Hook up the new basic block to its predecessors. @@ -319,7 +319,7 @@ UnreachableInst *Terminator = State->Builder.CreateUnreachable(); State->Builder.SetInsertPoint(Terminator); // Register NewBB in its loop. In innermost loops its the same for all BB's. - Loop *L = State->LI->getLoopFor(State->CFG.LastBB); + Loop *L = State->LI->getLoopFor(State->CFG.PrevBB); L->addBasicBlockToLoop(NewBB, *State->LI); State->CFG.PrevBB = NewBB; } @@ -756,9 +756,8 @@ Header = cast(Header->getSingleSuccessor()); } // TODO: Once the exit block is modeled in VPlan, use it instead of going - // through State.CFG.LastBB. - BasicBlock *Exit = - cast(State.CFG.LastBB->getTerminator())->getSuccessor(0); + // through State.CFG.ExitBB. + BasicBlock *Exit = State.CFG.ExitBB; Builder.CreateCondBr(Cond, Exit, State.CFG.VPBB2IRBB[Header]); Builder.GetInsertBlock()->getTerminator()->eraseFromParent(); @@ -909,11 +908,9 @@ BasicBlock *VectorHeaderBB = VectorPreHeaderBB->getSingleSuccessor(); assert(VectorHeaderBB && "Loop preheader does not have a single successor."); - // 1. Make room to generate basic-blocks inside loop body if needed. - BasicBlock *VectorLatchBB = VectorHeaderBB->splitBasicBlock( - VectorHeaderBB->getFirstInsertionPt(), "vector.body.latch"); Loop *L = State->LI->getLoopFor(VectorHeaderBB); - L->addBasicBlockToLoop(VectorLatchBB, *State->LI); + State->CFG.ExitBB = L->getExitBlock(); + // Remove the edge between Header and Latch to allow other connections. // Temporarily terminate with unreachable until CFG is rewired. // Note: this asserts the generated code's assumption that @@ -923,10 +920,9 @@ UnreachableInst *Terminator = State->Builder.CreateUnreachable(); State->Builder.SetInsertPoint(Terminator); - // 2. Generate code in loop body. + // Generate code in loop body. State->CFG.PrevVPBB = nullptr; State->CFG.PrevBB = VectorHeaderBB; - State->CFG.LastBB = VectorLatchBB; for (VPBlockBase *Block : depth_first(Entry)) Block->execute(State); @@ -949,28 +945,7 @@ } } - // 3. Merge the temporary latch created with the last basic-block filled. - BasicBlock *LastBB = State->CFG.PrevBB; - assert(isa(LastBB->getTerminator()) && - "Expected VPlan CFG to terminate with branch"); - - // Move both the branch and check from LastBB to VectorLatchBB. - auto *LastBranch = cast(LastBB->getTerminator()); - LastBranch->moveBefore(VectorLatchBB->getTerminator()); - VectorLatchBB->getTerminator()->eraseFromParent(); - // Move condition so it is guaranteed to be next to branch. This is only done - // to avoid excessive test updates. - // TODO: Remove special handling once the increments for all inductions are - // modeled explicitly in VPlan. - cast(LastBranch->getCondition())->moveBefore(LastBranch); - // Connect LastBB to VectorLatchBB to facilitate their merge. - BranchInst::Create(VectorLatchBB, LastBB); - - // Merge LastBB with Latch. - bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI); - (void)Merged; - assert(Merged && "Could not merge last basic block with latch."); - VectorLatchBB = LastBB; + BasicBlock *VectorLatchBB = State->CFG.PrevBB; // Fix the latch value of canonical, reduction and first-order recurrences // phis in the vector loop.