@@ -4407,6 +4407,13 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
44074407 LLVM_DEBUG (dbgs () << " LEV: Maximum Trip Count for Epilogue: "
44084408 << MaxTripCount << " \n " );
44094409 }
4410+ // Check if the RemainingIterations is scalable.
4411+ const SCEV *KnownMinRemIter = nullptr , *EstimatedRemIter = nullptr ;
4412+ bool ScalableRemIter = match (RemainingIterations, m_scev_c_Mul (m_SCEV (KnownMinRemIter), m_SCEVVScale ()));
4413+ if (ScalableRemIter)
4414+ EstimatedRemIter = SE.getMulExpr (
4415+ KnownMinRemIter,
4416+ SE.getConstant (TCType, CM.getVScaleForTuning ().value_or (1 )));
44104417
44114418 for (auto &NextVF : ProfitableVFs) {
44124419 // Skip candidate VFs without a corresponding VPlan.
@@ -4425,12 +4432,27 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
44254432
44264433 // If NextVF is greater than the number of remaining iterations, the
44274434 // epilogue loop would be dead. Skip such factors.
4428- ElementCount EstimatedRuntimeNextVF = ElementCount::getFixed (
4429- estimateElementCount (NextVF.Width , CM.getVScaleForTuning ()));
4430- if (SE.isKnownPredicate (CmpInst::ICMP_UGT,
4431- SE.getElementCount (TCType, EstimatedRuntimeNextVF),
4435+ if (ScalableRemIter == NextVF.Width .isScalable ()) {
4436+ if (SE.isKnownPredicate (CmpInst::ICMP_UGT,
4437+ SE.getElementCount (TCType, NextVF.Width ),
44324438 RemainingIterations))
4433- continue ;
4439+ continue ;
4440+ }
4441+ // Handle the case where NextVF and RemainingIterations are in different
4442+ // numerical spaces.
4443+ else if (NextVF.Width .isScalable ()) {
4444+ ElementCount EstimatedRuntimeNextVF = ElementCount::getFixed (
4445+ estimateElementCount (NextVF.Width , CM.getVScaleForTuning ()));
4446+ if (SE.isKnownPredicate (CmpInst::ICMP_UGT,
4447+ SE.getElementCount (TCType, EstimatedRuntimeNextVF),
4448+ RemainingIterations))
4449+ continue ;
4450+ } else {
4451+ if (SE.isKnownPredicate (CmpInst::ICMP_UGT,
4452+ SE.getElementCount (TCType, NextVF.Width ),
4453+ EstimatedRemIter))
4454+ continue ;
4455+ }
44344456
44354457 if (Result.Width .isScalar () ||
44364458 isMoreProfitable (NextVF, Result, MaxTripCount, !CM.foldTailByMasking (),
0 commit comments