@@ -414,6 +414,11 @@ VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) {
414414 PrevBB->getParent (), CFG.ExitBB );
415415 LLVM_DEBUG (dbgs () << " LV: created " << NewBB->getName () << ' \n ' );
416416
417+ return NewBB;
418+ }
419+
420+ void VPBasicBlock::connectToPredecessors (VPTransformState::CFGState &CFG) {
421+ BasicBlock *NewBB = CFG.VPBB2IRBB [this ];
417422 // Hook up the new basic block to its predecessors.
418423 for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors ()) {
419424 VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock ();
@@ -438,19 +443,22 @@ VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) {
438443 // Set each forward successor here when it is created, excluding
439444 // backedges. A backward successor is set when the branch is created.
440445 unsigned idx = PredVPSuccessors.front () == this ? 0 : 1 ;
441- assert (!TermBr->getSuccessor (idx) &&
442- " Trying to reset an existing successor block." );
446+ assert (
447+ (!TermBr->getSuccessor (idx) ||
448+ (isa<VPIRBasicBlock>(this ) && TermBr->getSuccessor (idx) == NewBB)) &&
449+ " Trying to reset an existing successor block." );
443450 TermBr->setSuccessor (idx, NewBB);
444451 }
445452 CFG.DTU .applyUpdates ({{DominatorTree::Insert, PredBB, NewBB}});
446453 }
447- return NewBB;
448454}
449455
450456void VPIRBasicBlock::execute (VPTransformState *State) {
451457 assert (getHierarchicalSuccessors ().size () <= 2 &&
452458 " VPIRBasicBlock can have at most two successors at the moment!" );
453459 State->Builder .SetInsertPoint (IRBB->getTerminator ());
460+ State->CFG .PrevBB = IRBB;
461+ State->CFG .VPBB2IRBB [this ] = IRBB;
454462 executeRecipes (State, IRBB);
455463 // Create a branch instruction to terminate IRBB if one was not created yet
456464 // and is needed.
@@ -464,23 +472,7 @@ void VPIRBasicBlock::execute(VPTransformState *State) {
464472 " other blocks must be terminated by a branch" );
465473 }
466474
467- for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors ()) {
468- VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock ();
469- BasicBlock *PredBB = State->CFG .VPBB2IRBB [PredVPBB];
470- assert (PredBB && " Predecessor basic-block not found building successor." );
471- LLVM_DEBUG (dbgs () << " LV: draw edge from" << PredBB->getName () << ' \n ' );
472-
473- auto *PredBBTerminator = PredBB->getTerminator ();
474- auto *TermBr = cast<BranchInst>(PredBBTerminator);
475- // Set each forward successor here when it is created, excluding
476- // backedges. A backward successor is set when the branch is created.
477- const auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors ();
478- unsigned idx = PredVPSuccessors.front () == this ? 0 : 1 ;
479- assert ((!TermBr->getSuccessor (idx) || TermBr->getSuccessor (idx) == IRBB) &&
480- " Trying to reset an existing successor block." );
481- TermBr->setSuccessor (idx, IRBB);
482- State->CFG .DTU .applyUpdates ({{DominatorTree::Insert, PredBB, IRBB}});
483- }
475+ connectToPredecessors (State->CFG );
484476}
485477
486478void VPBasicBlock::execute (VPTransformState *State) {
@@ -512,6 +504,7 @@ void VPBasicBlock::execute(VPTransformState *State) {
512504 // predecessor of this region.
513505
514506 NewBB = createEmptyBasicBlock (State->CFG );
507+
515508 State->Builder .SetInsertPoint (NewBB);
516509 // Temporarily terminate with unreachable until CFG is rewired.
517510 UnreachableInst *Terminator = State->Builder .CreateUnreachable ();
@@ -520,7 +513,12 @@ void VPBasicBlock::execute(VPTransformState *State) {
520513 if (State->CurrentVectorLoop )
521514 State->CurrentVectorLoop ->addBasicBlockToLoop (NewBB, *State->LI );
522515 State->Builder .SetInsertPoint (Terminator);
516+
523517 State->CFG .PrevBB = NewBB;
518+ State->CFG .VPBB2IRBB [this ] = NewBB;
519+ connectToPredecessors (State->CFG );
520+ } else {
521+ State->CFG .VPBB2IRBB [this ] = NewBB;
524522 }
525523
526524 // 2. Fill the IR basic block with IR instructions.
@@ -541,7 +539,6 @@ void VPBasicBlock::executeRecipes(VPTransformState *State, BasicBlock *BB) {
541539 LLVM_DEBUG (dbgs () << " LV: vectorizing VPBB:" << getName ()
542540 << " in BB:" << BB->getName () << ' \n ' );
543541
544- State->CFG .VPBB2IRBB [this ] = BB;
545542 State->CFG .PrevVPBB = this ;
546543
547544 for (VPRecipeBase &Recipe : Recipes)
0 commit comments