@@ -113,9 +113,6 @@ VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
113113 return VPBB;
114114 }
115115
116- if (!TheLoop->contains (BB))
117- return Plan->getExitBlock (BB);
118-
119116 // Create new VPBB.
120117 StringRef Name = BB->getName ();
121118 LLVM_DEBUG (dbgs () << " Creating VPBasicBlock for " << Name << " \n " );
@@ -249,6 +246,8 @@ std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
249246 DenseMap<VPBlockBase *, BasicBlock *> &VPB2IRBB) {
250247 VPIRBasicBlock *Entry = cast<VPIRBasicBlock>(Plan->getEntry ());
251248 BB2VPBB[Entry->getIRBasicBlock ()] = Entry;
249+ for (VPIRBasicBlock *ExitVPBB : Plan->getExitBlocks ())
250+ BB2VPBB[ExitVPBB->getIRBasicBlock ()] = ExitVPBB;
252251
253252 // 1. Scan the body of the loop in a topological order to visit each basic
254253 // block after having visited its predecessor basic blocks. Create a VPBB for
@@ -410,9 +409,12 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
410409 VPBlockUtils::disconnectBlocks (PreheaderVPBB, HeaderVPB);
411410 VPBlockUtils::disconnectBlocks (LatchVPBB, HeaderVPB);
412411 VPBlockBase *Succ = LatchVPBB->getSingleSuccessor ();
413- assert (LatchVPBB->getNumSuccessors () <= 1 &&
414- " Latch has more than one successor" );
415- LatchVPBB->removeSuccessor (Succ);
412+ assert (Succ && " Latch expected to be left with a single successor" );
413+
414+ auto *PlaceHolder = Plan.createVPBasicBlock (" Region place holder" );
415+ VPBlockUtils::insertOnEdge (LatchVPBB, Succ, PlaceHolder);
416+ VPBlockUtils::disconnectBlocks (LatchVPBB, PlaceHolder);
417+ VPBlockUtils::connectBlocks (PreheaderVPBB, PlaceHolder);
416418
417419 auto *R = Plan.createVPRegionBlock (HeaderVPB, LatchVPBB, " " ,
418420 false /* isReplicator*/ );
@@ -425,8 +427,9 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
425427 VPBB->setParent (R);
426428
427429 VPBlockUtils::insertBlockAfter (R, PreheaderVPBB);
428- R->setOneSuccessor (Succ);
429- Succ->replacePredecessor (LatchVPBB, R);
430+ VPBlockUtils::insertOnEdge (PlaceHolder, Succ, R);
431+ VPBlockUtils::disconnectBlocks (R, PlaceHolder);
432+ VPBlockUtils::disconnectBlocks (PlaceHolder, R);
430433}
431434
432435// Add the necessary canonical IV and branch recipes required to control the
@@ -481,11 +484,12 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
481484 VPBlockBase *LatchExitVPB = LatchVPB->getNumSuccessors () == 2
482485 ? LatchVPB->getSuccessors ()[0 ]
483486 : nullptr ;
484- if (LatchExitVPB) {
485- LatchVPB->getSuccessors ()[0 ] = MiddleVPBB;
486- MiddleVPBB->setPredecessors ({LatchVPB});
487- MiddleVPBB->setSuccessors ({LatchExitVPB});
488- LatchExitVPB->replacePredecessor (LatchVPB, MiddleVPBB);
487+ // Canonical LatchVPB has header block as last successor. If it has another
488+ // successor, the latter is an exit block - insert middle block on its edge.
489+ // Otherwise, add middle block as another successor retaining header as last.
490+ if (LatchVPB->getNumSuccessors () == 2 ) {
491+ VPBlockBase *LatchExitVPB = LatchVPB->getSuccessors ()[0 ];
492+ VPBlockUtils::insertOnEdge (LatchVPB, LatchExitVPB, MiddleVPBB);
489493 } else {
490494 VPBlockUtils::connectBlocks (LatchVPB, MiddleVPBB);
491495 LatchVPB->swapSuccessors ();
0 commit comments