@@ -2035,6 +2035,43 @@ static bool isConditionTrueViaVFAndUF(VPValue *Cond, VPlan &Plan,
20352035 return SE .isKnownPredicate (CmpInst::ICMP_EQ , VectorTripCount, C);
20362036}
20372037
2038+ static bool replaceMaskWithCompare (VPlan &Plan, ElementCount BestVF) {
2039+ if (!BestVF.isScalar ())
2040+ return false ;
2041+
2042+ bool MadeChange = false ;
2043+ VPBuilder Builder;
2044+ VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion ();
2045+ VPBasicBlock *PreheaderVPBB = Plan.getVectorPreheader ();
2046+ VPBasicBlock *ExitingVPBB = VectorRegion->getExitingBasicBlock ();
2047+
2048+ VPValue *Start, *TC ;
2049+ uint64_t Idx;
2050+ for (VPBasicBlock *VPBB : {PreheaderVPBB, ExitingVPBB}) {
2051+ for (VPRecipeBase &R : make_early_inc_range (*VPBB )) {
2052+ if (!match (&R, m_ExtractVectorForPart (
2053+ m_WideActiveLaneMask (m_VPValue (Start), m_VPValue (TC ),
2054+ m_VPValue ()),
2055+ m_ConstantInt (Idx))))
2056+ continue ;
2057+
2058+ auto *Extract = cast<VPInstruction>(&R);
2059+ Builder.setInsertPoint (Extract);
2060+
2061+ if (Idx > 0 )
2062+ Start = Builder.createAdd (
2063+ Start, Plan.getConstantInt (Start->getScalarType (), Idx));
2064+
2065+ VPValue *ICmp = Builder.createICmp (CmpInst::ICMP_ULT , Start, TC );
2066+ Extract->replaceAllUsesWith (ICmp);
2067+ Extract->eraseFromParent ();
2068+ MadeChange = true ;
2069+ }
2070+ }
2071+
2072+ return MadeChange;
2073+ }
2074+
20382075// / Try to simplify the branch condition of \p Plan. This may restrict the
20392076// / resulting plan to \p BestVF and \p BestUF.
20402077static bool simplifyBranchConditionForVFAndUF (VPlan &Plan, ElementCount BestVF,
@@ -2050,16 +2087,13 @@ static bool simplifyBranchConditionForVFAndUF(VPlan &Plan, ElementCount BestVF,
20502087 if (match (Term, m_BranchOnCount (
20512088 m_CombineOr (m_CanIVInc, m_c_Add (m_CanIVInc, m_LiveIn ())),
20522089 m_VPValue ())) ||
2053- match (Term, m_BranchOnCond (m_Not (m_ActiveLaneMask (
2054- m_VPValue (), m_VPValue (), m_VPValue ())))) ||
20552090 match (Term,
20562091 m_BranchOnCond (m_Not (m_ExtractVectorForPart (
20572092 m_WideActiveLaneMask (m_VPValue (), m_VPValue (), m_VPValue ()),
20582093 m_ZeroInt ()))))) {
20592094 // Try to simplify the branch condition if VectorTC <= VF * UF when the
2060- // latch terminator is BranchOnCount, BranchOnCond(Not(ActiveLaneMask)) or
2061- // BranchOnCond(Not(ExtractVectorForPart(WideActiveLaneMask),
2062- // 0))
2095+ // latch terminator is BranchOnCount or
2096+ // BranchOnCond(Not(ExtractVectorForPart(WideActiveLaneMask), 0))
20632097 const SCEV *VectorTripCount =
20642098 vputils::getSCEVExprForVPValue (&Plan.getVectorTripCount (), PSE );
20652099 if (isa<SCEVCouldNotCompute>(VectorTripCount))
@@ -2103,8 +2137,8 @@ void VPlanTransforms::optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF,
21032137 assert (Plan.hasVF (BestVF) && " BestVF is not available in Plan" );
21042138 assert (Plan.hasUF (BestUF) && " BestUF is not available in Plan" );
21052139
2106- bool MadeChange =
2107- simplifyBranchConditionForVFAndUF (Plan, BestVF, BestUF, PSE );
2140+ bool MadeChange = replaceMaskWithCompare (Plan, BestVF);
2141+ MadeChange |= simplifyBranchConditionForVFAndUF (Plan, BestVF, BestUF, PSE );
21082142 MadeChange |= optimizeVectorInductionWidthForTCAndVFUF (Plan, BestVF, BestUF);
21092143
21102144 if (MadeChange) {
@@ -3102,13 +3136,12 @@ static bool handleUncountableExitsWithSideEffects(
31023136 VPBuilder MaskBuilder (HeaderVPBB, InsertIt);
31033137 VPValue *FirstActive = MaskBuilder.createFirstActiveLane (*Cond);
31043138 Type *IVScalarTy = IV ->getScalarType ();
3105- VPValue *ALMMultiplier = Plan.getConstantInt (IVScalarTy, 1 );
31063139 VPValue *Zero = Plan.getZero (IVScalarTy);
31073140 FirstActive =
31083141 MaskBuilder.createScalarZExtOrTrunc (FirstActive, IVScalarTy, DebugLoc ());
31093142 VPValue *Mask = MaskBuilder.createNaryOp (VPInstruction::ActiveLaneMask,
3110- {Zero, FirstActive, ALMMultiplier} ,
3111- DebugLoc (), " uncountable.exit.mask" );
3143+ {Zero, FirstActive}, DebugLoc () ,
3144+ " uncountable.exit.mask" );
31123145
31133146 // Convert all other memory operations to use the mask.
31143147 for (VPBasicBlock *VPBB : vp_rpo_plain_cfg_loop_body (HeaderVPBB))
0 commit comments