@@ -459,10 +459,10 @@ static void addCanonicalIVRecipes(VPlan &Plan, VPBasicBlock *HeaderVPBB,
459
459
LatchDL);
460
460
}
461
461
462
- void VPlanTransforms::prepareForVectorization (
463
- VPlan &Plan, Type *InductionTy, PredicatedScalarEvolution &PSE ,
464
- bool RequiresScalarEpilogueCheck, bool TailFolded, Loop *TheLoop ,
465
- DebugLoc IVDL, bool HasUncountableEarlyExit, VFRange &Range ) {
462
+ void VPlanTransforms::addInitialSkeleton (VPlan &Plan, Type *InductionTy,
463
+ DebugLoc IVDL ,
464
+ PredicatedScalarEvolution &PSE ,
465
+ Loop *TheLoop ) {
466
466
VPDominatorTree VPDT;
467
467
VPDT.recalculate (Plan);
468
468
@@ -488,12 +488,46 @@ void VPlanTransforms::prepareForVectorization(
488
488
489
489
addCanonicalIVRecipes (Plan, HeaderVPBB, LatchVPBB, InductionTy, IVDL);
490
490
491
- [[maybe_unused]] bool HandledUncountableEarlyExit = false ;
491
+ // Create SCEV and VPValue for the trip count.
492
+ // We use the symbolic max backedge-taken-count, which works also when
493
+ // vectorizing loops with uncountable early exits.
494
+ const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount ();
495
+ assert (!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) &&
496
+ " Invalid loop count" );
497
+ ScalarEvolution &SE = *PSE.getSE ();
498
+ const SCEV *TripCount = SE.getTripCountFromExitCount (BackedgeTakenCountSCEV,
499
+ InductionTy, TheLoop);
500
+ Plan.setTripCount (
501
+ vputils::getOrCreateVPValueForSCEVExpr (Plan, TripCount, SE));
502
+
503
+ VPBasicBlock *ScalarPH = Plan.createVPBasicBlock (" scalar.ph" );
504
+ VPBlockUtils::connectBlocks (ScalarPH, Plan.getScalarHeader ());
505
+
506
+ // The connection order corresponds to the operands of the conditional branch,
507
+ // with the middle block already connected to the exit block.
508
+ VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
509
+ // Also connect the entry block to the scalar preheader.
510
+ // TODO: Also introduce a branch recipe together with the minimum trip count
511
+ // check.
512
+ VPBlockUtils::connectBlocks (Plan.getEntry (), ScalarPH);
513
+ Plan.getEntry ()->swapSuccessors ();
514
+ }
515
+
516
+ void VPlanTransforms::handleEarlyExitsAndAddMiddleCheck (
517
+ VPlan &Plan, bool RequiresScalarEpilogueCheck, bool TailFolded,
518
+ bool HasUncountableEarlyExit, VFRange &Range) {
519
+ auto *MiddleVPBB = cast<VPBasicBlock>(
520
+ Plan.getScalarHeader ()->getSinglePredecessor ()->getPredecessors ()[0 ]);
521
+ VPBlockBase *HeaderVPB =
522
+ Plan.getEntry ()->getSuccessors ()[1 ]->getSingleSuccessor ();
523
+ auto *LatchVPBB = cast<VPBasicBlock>(HeaderVPB->getPredecessors ()[1 ]);
524
+
492
525
// Disconnect all early exits from the loop leaving it with a single exit from
493
526
// the latch. Early exits that are countable are left for a scalar epilog. The
494
527
// condition of uncountable early exits (currently at most one is supported)
495
528
// is fused into the latch exit, and used to branch from middle block to the
496
529
// early exit destination.
530
+ [[maybe_unused]] bool HandledUncountableEarlyExit = false ;
497
531
for (VPIRBasicBlock *EB : Plan.getExitBlocks ()) {
498
532
for (VPBlockBase *Pred : to_vector (EB->getPredecessors ())) {
499
533
if (Pred == MiddleVPBB)
@@ -502,7 +536,8 @@ void VPlanTransforms::prepareForVectorization(
502
536
assert (!HandledUncountableEarlyExit &&
503
537
" can handle exactly one uncountable early exit" );
504
538
handleUncountableEarlyExit (cast<VPBasicBlock>(Pred), EB, Plan,
505
- HeaderVPBB, LatchVPBB, Range);
539
+ cast<VPBasicBlock>(HeaderVPB), LatchVPBB,
540
+ Range);
506
541
HandledUncountableEarlyExit = true ;
507
542
} else {
508
543
for (VPRecipeBase &R : EB->phis ())
@@ -516,38 +551,11 @@ void VPlanTransforms::prepareForVectorization(
516
551
assert ((!HasUncountableEarlyExit || HandledUncountableEarlyExit) &&
517
552
" missed an uncountable exit that must be handled" );
518
553
519
- // Create SCEV and VPValue for the trip count.
520
- // We use the symbolic max backedge-taken-count, which works also when
521
- // vectorizing loops with uncountable early exits.
522
- const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount ();
523
- assert (!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) &&
524
- " Invalid loop count" );
525
- ScalarEvolution &SE = *PSE.getSE ();
526
- const SCEV *TripCount = SE.getTripCountFromExitCount (BackedgeTakenCountSCEV,
527
- InductionTy, TheLoop);
528
- Plan.setTripCount (
529
- vputils::getOrCreateVPValueForSCEVExpr (Plan, TripCount, SE));
530
-
531
- VPBasicBlock *ScalarPH = Plan.createVPBasicBlock (" scalar.ph" );
532
- VPBlockUtils::connectBlocks (ScalarPH, Plan.getScalarHeader ());
533
-
534
- // The connection order corresponds to the operands of the conditional branch,
535
- // with the middle block already connected to the exit block.
536
- VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
537
- // Also connect the entry block to the scalar preheader.
538
- // TODO: Also introduce a branch recipe together with the minimum trip count
539
- // check.
540
- VPBlockUtils::connectBlocks (Plan.getEntry (), ScalarPH);
541
- Plan.getEntry ()->swapSuccessors ();
542
-
543
554
// If MiddleVPBB has a single successor then the original loop does not exit
544
555
// via the latch and the single successor must be the scalar preheader.
545
556
// There's no need to add a runtime check to MiddleVPBB.
546
- if (MiddleVPBB->getNumSuccessors () == 1 ) {
547
- assert (MiddleVPBB->getSingleSuccessor () == ScalarPH &&
548
- " must have ScalarPH as single successor" );
557
+ if (MiddleVPBB->getNumSuccessors () == 1 )
549
558
return ;
550
- }
551
559
552
560
assert (MiddleVPBB->getNumSuccessors () == 2 && " must have 2 successors" );
553
561
0 commit comments