[llvm] [LV] Vectorize selecting last IV of min/max element. (PR #141431)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 25 02:58:51 PST 2025
================
@@ -1001,3 +1005,138 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
MiddleTerm->setOperand(0, NewCond);
return true;
}
+
+bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan) {
+ for (auto &PhiR : make_early_inc_range(
+ Plan.getVectorLoopRegion()->getEntryBasicBlock()->phis())) {
+ auto *MinMaxPhiR = dyn_cast<VPReductionPHIRecipe>(&PhiR);
+ // TODO: check for multi-uses in VPlan directly.
+ if (!MinMaxPhiR || !MinMaxPhiR->hasLoopUsesOutsideReductionChain())
+ continue;
+
+ // MinMaxPhiR has users outside the reduction cycle in the loop. Check if
+ // the only other user is a FindLastIV reduction. MinMaxPhiR must have
+ // exactly 3 users: 1) the min/max operation, the compare of a FindLastIV
+ // reduction and ComputeReductionResult. The comparisom must compare
+ // MinMaxPhiR against the min/max operand used for the min/max reduction
+ // and only be used by the select of the FindLastIV reduction.
+ RecurKind RdxKind = MinMaxPhiR->getRecurrenceKind();
+ assert(
+ RecurrenceDescriptor::isIntMinMaxRecurrenceKind(RdxKind) &&
+ "only min/max recurrences support users outside the reduction chain");
+
+ auto *MinMaxOp =
+ dyn_cast<VPRecipeWithIRFlags>(MinMaxPhiR->getBackedgeValue());
+ if (!MinMaxOp)
+ return false;
+
+ // MinMaxOp must have 2 users: 1) MinMaxPhiR and 2) ComputeReductionResult
+ // (asserted below).
+ assert(MinMaxOp->getNumUsers() == 2 &&
+ "MinMaxOp must have exactly 2 users");
+
+ assert((isa<VPWidenIntrinsicRecipe>(MinMaxOp) ||
+ (isa<VPReplicateRecipe>(MinMaxOp) &&
+ isa<IntrinsicInst>(
+ cast<VPReplicateRecipe>(MinMaxOp)->getUnderlyingValue()))) &&
+ "MinMaxOp must be a wide or scalar intrinsic");
+ VPValue *MinMaxOpA = MinMaxOp->getOperand(0);
+ VPValue *MinMaxOpB = MinMaxOp->getOperand(1);
+ if (MinMaxOpA != MinMaxPhiR)
+ std::swap(MinMaxOpA, MinMaxOpB);
+ assert(MinMaxOpA == MinMaxPhiR &&
+ "one of MinMaxOp's operands must be the phi");
+
+ VPValue *CmpOpA;
+ VPValue *CmpOpB;
+ CmpPredicate Pred;
+ auto *Cmp = dyn_cast_or_null<VPRecipeWithIRFlags>(findUserOf(
+ MinMaxPhiR, m_Cmp(Pred, m_VPValue(CmpOpA), m_VPValue(CmpOpB))));
+ if (!Cmp || Cmp->getNumUsers() != 1 ||
+ (CmpOpA != MinMaxOpB && CmpOpB != MinMaxOpB))
+ return false;
+
+ // MinMaxPhiR must have exactly 3 users:
+ // * MinMaxOp,
+ // * Cmp (that's part of a FindLastIV chain),
+ // * ComputeReductionResult.
+ if (MinMaxPhiR->getNumUsers() != 3)
+ return false;
+
+ VPInstruction *MinMaxResult =
+ findUserOf<VPInstruction::ComputeReductionResult>(MinMaxPhiR);
+ assert(is_contained(MinMaxPhiR->users(), MinMaxOp) &&
+ "one user must be MinMaxOp");
+ assert(is_contained(MinMaxPhiR->users(), Cmp) && "one user must be Cmp");
+ assert(is_contained(MinMaxPhiR->users(), MinMaxResult) &&
+ "one user must be MinMaxResult");
+ assert(is_contained(MinMaxOp->users(), MinMaxPhiR) &&
+ "one user must be MinMaxPhiR");
----------------
ayalz wrote:
redundant - by construction of MinMaxOp as feeding MinMaxPhiR along backedge.
https://github.com/llvm/llvm-project/pull/141431
More information about the llvm-commits
mailing list