@@ -2458,68 +2458,42 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan,
24582458 R->eraseFromParent ();
24592459}
24602460
2461- void VPlanTransforms::handleUncountableEarlyExit (VPlan &Plan,
2461+ void VPlanTransforms::handleUncountableEarlyExit (VPBasicBlock *EarlyExitingVPBB,
2462+ VPBasicBlock *EarlyExitVPBB,
2463+
2464+ VPlan &Plan,
24622465 VPBasicBlock *HeaderVPBB,
24632466 VPBasicBlock *LatchVPBB,
24642467 VFRange &Range) {
2465- // First find the uncountable early exiting block by looking at the
2466- // predecessors of the exit blocks.
2467- VPBlockBase *MiddleVPBB = LatchVPBB->getSuccessors ()[0 ];
2468- VPBasicBlock *EarlyExitingVPBB = nullptr ;
2469- VPIRBasicBlock *EarlyExitVPBB = nullptr ;
2470- for (auto *EB : Plan.getExitBlocks ()) {
2471- for (VPBlockBase *Pred : EB->getPredecessors ()) {
2472- if (Pred != MiddleVPBB) {
2473- EarlyExitingVPBB = cast<VPBasicBlock>(Pred);
2474- EarlyExitVPBB = EB;
2475- break ;
2476- }
2477- }
2478- }
2479- assert (EarlyExitVPBB && " Must have a early exiting block." );
2480- assert (all_of (Plan.getExitBlocks (),
2481- [EarlyExitingVPBB, MiddleVPBB](VPIRBasicBlock *EB) {
2482- return all_of (
2483- EB->getPredecessors (),
2484- [EarlyExitingVPBB, MiddleVPBB](VPBlockBase *Pred) {
2485- return Pred == EarlyExitingVPBB || Pred == MiddleVPBB;
2486- });
2487- }) &&
2488- " All exit blocks must only have EarlyExitingVPBB or MiddleVPBB as "
2489- " predecessors." );
2490-
2491- VPBuilder Builder (LatchVPBB->getTerminator ());
2492- VPBlockBase *TrueSucc = EarlyExitingVPBB->getSuccessors ()[0 ];
2493- VPValue *EarlyExitCond = EarlyExitingVPBB->getTerminator ()->getOperand (0 );
2494- auto *EarlyExitTakenCond = TrueSucc == EarlyExitVPBB
2495- ? EarlyExitCond
2496- : Builder.createNot (EarlyExitCond);
2468+ using namespace llvm ::VPlanPatternMatch;
24972469
2470+ VPBlockBase *MiddleVPBB = LatchVPBB->getSuccessors ()[0 ];
24982471 if (!EarlyExitVPBB->getSinglePredecessor () &&
24992472 EarlyExitVPBB->getPredecessors ()[0 ] != MiddleVPBB) {
2500- for (VPRecipeBase &R : EarlyExitVPBB->phis ()) {
2501- // Early exit operand should always be last, i.e., 0 if EarlyExitVPBB has
2502- // a single predecessor and 1 if it has two.
2503- // If EarlyExitVPBB has two predecessors, they are already ordered such
2504- // that early exit is second (and latch exit is first), by construction.
2505- // But its underlying IRBB (EarlyExitIRBB) may have its predecessors
2506- // ordered the other way around, and it is the order of the latter which
2507- // corresponds to the order of operands of EarlyExitVPBB's phi recipes.
2508- // Therefore, if early exit (UncountableExitingBlock) is the first
2509- // predecessor of EarlyExitIRBB, we swap the operands of phi recipes,
2510- // thereby bringing them to match EarlyExitVPBB's predecessor order,
2511- // with early exit being last (second). Otherwise they already match.
2473+ // Early exit operand should always be last phi operand. If EarlyExitVPBB
2474+ // has two predecessors and MiddleVPBB isn't the first, swap the operands of
2475+ // the phis.
2476+ for (VPRecipeBase &R : EarlyExitVPBB->phis ())
25122477 cast<VPIRPhi>(&R)->swapOperands ();
2513- }
25142478 }
25152479
2480+ VPBuilder Builder (LatchVPBB->getTerminator ());
2481+ VPBlockBase *TrueSucc = EarlyExitingVPBB->getSuccessors ()[0 ];
2482+ assert (
2483+ match (EarlyExitingVPBB->getTerminator (), m_BranchOnCond (m_VPValue ())) &&
2484+ " Terminator must be be BranchOnCond" );
2485+ VPValue *CondOfEarlyExitingVPBB =
2486+ EarlyExitingVPBB->getTerminator ()->getOperand (0 );
2487+ auto *CondToEarlyExit = TrueSucc == EarlyExitVPBB
2488+ ? CondOfEarlyExitingVPBB
2489+ : Builder.createNot (CondOfEarlyExitingVPBB);
25162490 EarlyExitingVPBB->getTerminator ()->eraseFromParent ();
25172491 VPBlockUtils::disconnectBlocks (EarlyExitingVPBB, EarlyExitVPBB);
25182492
25192493 // Split the middle block and have it conditionally branch to the early exit
25202494 // block if EarlyExitTaken.
25212495 VPValue *IsEarlyExitTaken =
2522- Builder.createNaryOp (VPInstruction::AnyOf, {EarlyExitTakenCond });
2496+ Builder.createNaryOp (VPInstruction::AnyOf, {CondToEarlyExit });
25232497 VPBasicBlock *NewMiddle = Plan.createVPBasicBlock (" middle.split" );
25242498 VPBasicBlock *VectorEarlyExitVPBB =
25252499 Plan.createVPBasicBlock (" vector.early.exit" );
@@ -2537,7 +2511,7 @@ void VPlanTransforms::handleUncountableEarlyExit(VPlan &Plan,
25372511 // Early exit operand should always be last, i.e., 0 if EarlyExitVPBB has
25382512 // a single predecessor and 1 if it has two.
25392513 unsigned EarlyExitIdx = ExitIRI->getNumOperands () - 1 ;
2540- if (!EarlyExitVPBB-> getSinglePredecessor () ) {
2514+ if (ExitIRI-> getNumOperands () != 1 ) {
25412515 // The first of two operands corresponds to the latch exit, via MiddleVPBB
25422516 // predecessor. Extract its last lane.
25432517 ExitIRI->extractLastLaneOfFirstOperand (MiddleBuilder);
@@ -2553,7 +2527,7 @@ void VPlanTransforms::handleUncountableEarlyExit(VPlan &Plan,
25532527 LoopVectorizationPlanner::getDecisionAndClampRange (IsVector, Range)) {
25542528 // Update the incoming value from the early exit.
25552529 VPValue *FirstActiveLane = EarlyExitB.createNaryOp (
2556- VPInstruction::FirstActiveLane, {EarlyExitTakenCond }, nullptr ,
2530+ VPInstruction::FirstActiveLane, {CondToEarlyExit }, nullptr ,
25572531 " first.active.lane" );
25582532 IncomingFromEarlyExit = EarlyExitB.createNaryOp (
25592533 Instruction::ExtractElement, {IncomingFromEarlyExit, FirstActiveLane},
0 commit comments