@@ -2503,23 +2503,26 @@ void VPlanTransforms::handleUncountableEarlyExit(
25032503 // Update the exit phis in the early exit block.
25042504 VPBuilder MiddleBuilder (NewMiddle);
25052505 VPBuilder EarlyExitB (VectorEarlyExitVPBB);
2506- for (VPRecipeBase &R : * VPEarlyExitBlock->phis ()) {
2506+ for (VPRecipeBase &R : VPEarlyExitBlock->phis ()) {
25072507 auto *ExitIRI = cast<VPIRPhi>(&R);
2508+ // By default, assume early exit operand is first, e.g., when the two exit
2509+ // blocks are distinct - VPEarlyExitBlock has a single predecessor.
25082510 unsigned EarlyExitIdx = 0 ;
25092511 if (OrigLoop->getUniqueExitBlock ()) {
2510- // After the transform, the first incoming value is coming from the
2511- // orignial loop latch, while the second operand is from the early exit.
2512- // Sawp the phi operands, if the first predecessor in the original IR is
2513- // not the loop latch.
2512+ // The incoming values currently correspond to the original IR
2513+ // predecessors. After the transform, the first incoming value is coming
2514+ // from the original loop latch, while the second operand is from the
2515+ // early exit. Swap the phi operands, if the first predecessor in the
2516+ // original IR is not the loop latch.
25142517 if (*pred_begin (VPEarlyExitBlock->getIRBasicBlock ()) !=
25152518 OrigLoop->getLoopLatch ())
25162519 ExitIRI->swapOperands ();
25172520
25182521 EarlyExitIdx = 1 ;
25192522 // If there's a unique exit block, VPEarlyExitBlock has 2 predecessors
2520- // (MiddleVPBB and NewMiddle). Add the incoming value from MiddleVPBB
2521- // which is coming from the original latch.
2522- ExitIRI->extractLastLaneOfOperand (MiddleBuilder);
2523+ // (MiddleVPBB and NewMiddle). Extract the last lane of the incoming value
2524+ // from MiddleVPBB which is coming from the original latch.
2525+ ExitIRI->extractLastLaneOfFirstOperand (MiddleBuilder);
25232526 }
25242527
25252528 VPValue *IncomingFromEarlyExit = ExitIRI->getOperand (EarlyExitIdx);
@@ -2530,15 +2533,14 @@ void VPlanTransforms::handleUncountableEarlyExit(
25302533 // and vector VFs.
25312534 if (!IncomingFromEarlyExit->isLiveIn () &&
25322535 LoopVectorizationPlanner::getDecisionAndClampRange (IsVector, Range)) {
2533- // Add the incoming value from the early exit.
2536+ // Update the incoming value from the early exit.
25342537 VPValue *FirstActiveLane = EarlyExitB.createNaryOp (
25352538 VPInstruction::FirstActiveLane, {EarlyExitTakenCond}, nullptr ,
25362539 " first.active.lane" );
2537- ExitIRI->setOperand (
2538- EarlyExitIdx,
2539- EarlyExitB.createNaryOp (Instruction::ExtractElement,
2540- {IncomingFromEarlyExit, FirstActiveLane},
2541- nullptr , " early.exit.value" ));
2540+ IncomingFromEarlyExit = EarlyExitB.createNaryOp (
2541+ Instruction::ExtractElement, {IncomingFromEarlyExit, FirstActiveLane},
2542+ nullptr , " early.exit.value" );
2543+ ExitIRI->setOperand (EarlyExitIdx, IncomingFromEarlyExit);
25422544 }
25432545 }
25442546 MiddleBuilder.createNaryOp (VPInstruction::BranchOnCond, {IsEarlyExitTaken});
0 commit comments