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 @@ -274,8 +274,6 @@ assert(this == LoopRegion->getSingleSuccessor() && "the current block must be the single successor of the region"); PredBBTerminator->setSuccessor(0, NewBB); - PredBBTerminator->setSuccessor( - 1, CFG.VPBB2IRBB[LoopRegion->getEntryBasicBlock()]); } } return NewBB; @@ -297,6 +295,13 @@ // ExitBB can be re-used for the exit block of the Plan. NewBB = State->CFG.ExitBB; State->CFG.PrevBB = NewBB; + + // Update the branch instruction in the predecessor to branch to ExitBB. + VPBlockBase *PredVPBB = getSingleHierarchicalPredecessor(); + VPBasicBlock *ExitingVPBB = PredVPBB->getExitingBasicBlock(); + assert(PredVPBB->getSuccessors()[0] == this); + BasicBlock *ExitingBB = State->CFG.VPBB2IRBB[ExitingVPBB]; + cast(ExitingBB->getTerminator())->setSuccessor(0, NewBB); } else if (PrevVPBB && /* A */ !((SingleHPred = getSingleHierarchicalPredecessor()) && SingleHPred->getExitingBasicBlock() == PrevVPBB && @@ -783,11 +788,13 @@ "empty entry block only expected in VPlanNativePath"); Header = cast(Header->getSingleSuccessor()); } - // TODO: Once the exit block is modeled in VPlan, use it instead of going - // through State.CFG.ExitBB. - BasicBlock *Exit = State.CFG.ExitBB; - Builder.CreateCondBr(Cond, Exit, State.CFG.VPBB2IRBB[Header]); + // Replace the temporary unreachable terminator with a new conditional + // branch, hooking it up to backward destination now and to forward + // destination(s) later when they are created. + BranchInst *CondBr = Builder.CreateCondBr(Cond, Builder.GetInsertBlock(), + State.CFG.VPBB2IRBB[Header]); + CondBr->setSuccessor(0, nullptr); Builder.GetInsertBlock()->getTerminator()->eraseFromParent(); break; }