@@ -432,6 +432,19 @@ BasicBlock *VPBasicBlock::createEmptyBasicBlock(VPTransformState &State) {
432432void  VPBasicBlock::connectToPredecessors (VPTransformState &State) {
433433  auto  &CFG = State.CFG ;
434434  BasicBlock *NewBB = CFG.VPBB2IRBB [this ];
435+ 
436+   //  Register NewBB in its loop. In innermost loops its the same for all
437+   //  BB's.
438+   Loop *ParentLoop = State.CurrentParentLoop ;
439+   //  If this block has a sole successor that is an exit block then it needs
440+   //  adding to the same parent loop as the exit block.
441+   VPBlockBase *SuccVPBB = getSingleSuccessor ();
442+   if  (SuccVPBB && State.Plan ->isExitBlock (SuccVPBB))
443+     ParentLoop =
444+         State.LI ->getLoopFor (cast<VPIRBasicBlock>(SuccVPBB)->getIRBasicBlock ());
445+   if  (ParentLoop && !State.LI ->getLoopFor (NewBB))
446+     ParentLoop->addBasicBlockToLoop (NewBB, *State.LI );
447+ 
435448  //  Hook up the new basic block to its predecessors.
436449  for  (VPBlockBase *PredVPBlock : getHierarchicalPredecessors ()) {
437450    VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock ();
@@ -517,17 +530,6 @@ void VPBasicBlock::execute(VPTransformState *State) {
517530    State->Builder .SetInsertPoint (NewBB);
518531    //  Temporarily terminate with unreachable until CFG is rewired.
519532    UnreachableInst *Terminator = State->Builder .CreateUnreachable ();
520-     //  Register NewBB in its loop. In innermost loops its the same for all
521-     //  BB's.
522-     Loop *ParentLoop = State->CurrentParentLoop ;
523-     //  If this block has a sole successor that is an exit block then it needs
524-     //  adding to the same parent loop as the exit block.
525-     VPBlockBase *SuccVPBB = getSingleSuccessor ();
526-     if  (SuccVPBB && State->Plan ->isExitBlock (SuccVPBB))
527-       ParentLoop = State->LI ->getLoopFor (
528-           cast<VPIRBasicBlock>(SuccVPBB)->getIRBasicBlock ());
529-     if  (ParentLoop)
530-       ParentLoop->addBasicBlockToLoop (NewBB, *State->LI );
531533    State->Builder .SetInsertPoint (Terminator);
532534
533535    State->CFG .PrevBB  = NewBB;
0 commit comments