@@ -411,15 +411,17 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
411411 VPBlockBase *Succ = LatchVPBB->getSingleSuccessor ();
412412 assert (Succ && " Latch expected to be left with a single successor" );
413413
414+ // Use a temporary placeholder between LatchVPBB and its successor, to
415+ // preserve the original predecessor/successor order of the blocks.
414416 auto *PlaceHolder = Plan.createVPBasicBlock (" Region place holder" );
415417 VPBlockUtils::insertOnEdge (LatchVPBB, Succ, PlaceHolder);
416418 VPBlockUtils::disconnectBlocks (LatchVPBB, PlaceHolder);
417419 VPBlockUtils::connectBlocks (PreheaderVPBB, PlaceHolder);
418420
419421 auto *R = Plan.createVPRegionBlock (HeaderVPB, LatchVPBB, " " ,
420422 false /* isReplicator*/ );
421- // All VPBB's reachable shallowly from HeaderVPB belong to top level loop ,
422- // because VPlan is expected to end at top level latch disconnected above.
423+ // All VPBB's reachable shallowly from HeaderVPB belong to the current region ,
424+ // except the exit blocks reachable via non- latch exiting blocks,
423425 SmallPtrSet<VPBlockBase *, 2 > ExitBlocks (Plan.getExitBlocks ().begin (),
424426 Plan.getExitBlocks ().end ());
425427 for (VPBlockBase *VPBB : vp_depth_first_shallow (HeaderVPB))
@@ -428,6 +430,8 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
428430
429431 VPBlockUtils::insertBlockAfter (R, PreheaderVPBB);
430432 VPBlockUtils::insertOnEdge (PlaceHolder, Succ, R);
433+
434+ // Remove placeholder block.
431435 VPBlockUtils::disconnectBlocks (R, PlaceHolder);
432436 VPBlockUtils::disconnectBlocks (PlaceHolder, R);
433437}
@@ -481,9 +485,6 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
481485 VPBlockUtils::insertBlockAfter (VecPreheader, Plan.getEntry ());
482486
483487 VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock (" middle.block" );
484- VPBlockBase *LatchExitVPB = LatchVPB->getNumSuccessors () == 2
485- ? LatchVPB->getSuccessors ()[0 ]
486- : nullptr ;
487488 // Canonical LatchVPB has header block as last successor. If it has another
488489 // successor, the latter is an exit block - insert middle block on its edge.
489490 // Otherwise, add middle block as another successor retaining header as last.
@@ -498,8 +499,10 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
498499 addCanonicalIVRecipes (Plan, cast<VPBasicBlock>(HeaderVPB),
499500 cast<VPBasicBlock>(LatchVPB), InductionTy, IVDL);
500501
501- // Disconnect all edges between exit blocks other than from the latch.
502- // TODO: Uncountable exit blocks should be handled here.
502+ // Disconnect all edges to exit blocks other than from the middle block.
503+ // TODO: VPlans with early exits should be explicitly converted to a form only
504+ // exiting via the latch here, including adjusting the exit condition, instead
505+ // of simplify disconnecting the edges and adjusting the VPlan later.
503506 for (VPBlockBase *EB : to_vector (Plan.getExitBlocks ())) {
504507 for (VPBlockBase *Pred : to_vector (EB->getPredecessors ())) {
505508 if (Pred == MiddleVPBB)
@@ -533,8 +536,9 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
533536 // Thus if tail is to be folded, we know we don't need to run the
534537 // remainder and we can set the condition to true.
535538 // 3) Otherwise, construct a runtime check.
539+
536540 if (!RequiresScalarEpilogueCheck) {
537- if (LatchExitVPB)
541+ if (auto * LatchExitVPB = MiddleVPBB-> getSingleSuccessor () )
538542 VPBlockUtils::disconnectBlocks (MiddleVPBB, LatchExitVPB);
539543 VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
540544 // The exit blocks are unreachable, remove their recipes to make sure no
@@ -569,7 +573,8 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
569573void VPlanTransforms::createLoopRegions (VPlan &Plan) {
570574 VPDominatorTree VPDT;
571575 VPDT.recalculate (Plan);
572- for (VPBlockBase *HeaderVPB : vp_depth_first_shallow (Plan.getEntry ()))
576+ for (VPBlockBase *HeaderVPB : post_order (
577+ VPBlockShallowTraversalWrapper<VPBlockBase *>(Plan.getEntry ())))
573578 if (canonicalHeaderAndLatch (HeaderVPB, VPDT))
574579 createLoopRegion (Plan, HeaderVPB);
575580
0 commit comments