[llvm] [VPlan] Avoid recursive-fan-out of usesFirstLaneOnly (PR #229130)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 04:00:44 PDT 2026


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/229130

>From 9c8fa8a3be664d12843dcc7937a103b85b67b2fd Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 5 Oct 2026 14:35:56 +0100
Subject: [PATCH 1/8] [VPlan] Avoid recursive-fan-out of usesFirstLaneOnly

This helps compile time.
---
 llvm/lib/Transforms/Vectorize/VPlan.h         | 92 +++++++++++++------
 .../Transforms/Vectorize/VPlanLowering.cpp    | 10 +-
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp | 43 ++++++---
 .../Transforms/Vectorize/VPlanTransforms.cpp  |  8 +-
 llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp |  7 +-
 llvm/lib/Transforms/Vectorize/VPlanUtils.cpp  | 25 ++++-
 llvm/lib/Transforms/Vectorize/VPlanUtils.h    |  5 +-
 llvm/lib/Transforms/Vectorize/VPlanValue.h    |  9 +-
 8 files changed, 140 insertions(+), 59 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index b1b6b6dd937b8..f3b7737e5627b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1571,12 +1571,10 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
   bool opcodeMayReadOrWriteFromMemory() const;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override;
+  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
 
   /// Returns true if the recipe only uses scalars of operand \p Op.
-  bool usesScalars(const VPValue *Op) const override {
-    return isSingleScalar() || usesFirstLaneOnly(Op);
-  }
+  bool usesScalars(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool usesFirstPartOnly(const VPValue *Op) const override;
@@ -1764,9 +1762,11 @@ class VPIRInstruction : public VPRecipeBase {
     return true;
   }
 
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return true;
   }
 
@@ -1870,9 +1870,11 @@ class LLVM_ABI_FOR_TEST VPWidenRecipe : public VPRecipeWithIRFlags,
 #endif
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return Opcode == Instruction::Select && Op == getOperand(0) &&
            isa<VPIRValue>(Op);
   }
@@ -2047,7 +2049,7 @@ class LLVM_ABI_FOR_TEST VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags,
   /// Returns true if the intrinsic may have side-effects.
   bool mayHaveSideEffects() const { return MayHaveSideEffects; }
 
-  bool usesFirstLaneOnly(const VPValue *Op) const override;
+  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2152,7 +2154,7 @@ class LLVM_ABI_FOR_TEST VPWidenCallRecipe : public VPRecipeWithIRFlags,
   const_operand_range args() const { return drop_end(operands()); }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override;
+  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2200,9 +2202,11 @@ class VPHistogramRecipe : public VPRecipeBase, public VPIRMetadata {
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return Op == getOperand(1);
   }
 
@@ -2260,7 +2264,7 @@ class LLVM_ABI_FOR_TEST VPWidenGEPRecipe : public VPRecipeWithIRFlags {
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override;
+  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2315,9 +2319,11 @@ class VPVectorEndPointerRecipe : public VPRecipeWithIRFlags {
 
   void execute(VPTransformState &State) override;
 
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return true;
   }
 
@@ -2387,9 +2393,11 @@ class VPVectorPointerRecipe : public VPRecipeWithIRFlags {
 
   Type *getSourceElementType() const { return SourceElementTy; }
 
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return true;
   }
 
@@ -2597,12 +2605,14 @@ class VPWidenInductionRecipe : public VPHeaderPHIRecipe {
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // The recipe creates its own wide start value, so it only requests the
     // first lane of the operand.
     // TODO: Remove once creating the start value is modeled separately.
+    ShouldRecurse = false;
     return Op == getStartValue() || Op == getStepValue();
   }
 };
@@ -2820,9 +2830,11 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
                               VPCostContext &Ctx) const override;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return Op == getStartValue();
   }
 
@@ -2949,9 +2961,11 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe, public VPIRFlags {
   bool isExpressionSunk() const { return ExpressionSunk; }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return isOrdered() || isInLoop();
   }
 
@@ -3038,7 +3052,7 @@ class LLVM_ABI_FOR_TEST VPBlendRecipe : public VPRecipeWithIRFlags {
                               VPCostContext &Ctx) const override;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override;
+  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -3131,7 +3145,8 @@ class LLVM_ABI_FOR_TEST VPInterleaveBase : public VPRecipeBase,
                               VPCostContext &Ctx) const override;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override = 0;
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override = 0;
 
   /// Returns the number of stored operands of this interleave group. Returns 0
   /// for load interleave groups.
@@ -3170,9 +3185,11 @@ class LLVM_ABI_FOR_TEST VPInterleaveRecipe final : public VPInterleaveBase {
   /// Generate the wide load or store, and shuffles.
   void execute(VPTransformState &State) override;
 
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return Op == getAddr() && !llvm::is_contained(getStoredValues(), Op);
   }
 
@@ -3219,9 +3236,11 @@ class LLVM_ABI_FOR_TEST VPInterleaveEVLRecipe final : public VPInterleaveBase {
   void execute(VPTransformState &State) override;
 
   /// The recipe only uses the first lane of the address, and EVL operand.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return (Op == getAddr() && !llvm::is_contained(getStoredValues(), Op)) ||
            Op == getEVL();
   }
@@ -3390,9 +3409,11 @@ class LLVM_ABI_FOR_TEST VPReductionEVLRecipe : public VPReductionRecipe {
   VPValue *getEVL() const { return getOperand(2); }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return Op == getEVL();
   }
 
@@ -3476,9 +3497,11 @@ class LLVM_ABI_FOR_TEST VPReplicateRecipe : public VPRecipeWithIRFlags,
   bool isPredicated() const { return IsPredicated; }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return isSingleScalar();
   }
 
@@ -3855,11 +3878,13 @@ struct LLVM_ABI_FOR_TEST VPWidenLoadRecipe final : public VPSingleDefRecipe,
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // Widened, consecutive loads operations only demand the first lane of
     // their address.
+    ShouldRecurse = false;
     return Op == getAddr() && isConsecutive();
   }
 
@@ -3909,11 +3934,13 @@ struct LLVM_ABI_FOR_TEST VPWidenLoadEVLRecipe final
                               VPCostContext &Ctx) const override;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // Widened loads only demand the first lane of EVL and consecutive loads
     // only demand the first lane of their address.
+    ShouldRecurse = false;
     return Op == getEVL() || (Op == getAddr() && isConsecutive());
   }
 
@@ -3961,11 +3988,13 @@ struct LLVM_ABI_FOR_TEST VPWidenStoreRecipe final : public VPRecipeBase,
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // Widened, consecutive stores only demand the first lane of their address,
     // unless the same operand is also stored.
+    ShouldRecurse = false;
     return Op == getAddr() && isConsecutive() && Op != getStoredValue();
   }
 
@@ -4014,9 +4043,11 @@ struct LLVM_ABI_FOR_TEST VPWidenStoreEVLRecipe final
                               VPCostContext &Ctx) const override;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     if (Op == getEVL()) {
       assert(getStoredValue() != Op && "unexpected store of EVL");
       return true;
@@ -4133,9 +4164,11 @@ class VPCurrentIterationPHIRecipe : public VPHeaderPHIRecipe {
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return true;
   }
 
@@ -4247,9 +4280,10 @@ class LLVM_ABI_FOR_TEST VPDerivedIVRecipe : public VPRecipeWithIRFlags {
   InductionDescriptor::InductionKind getInductionKind() const { return Kind; }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecuse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecuse = false;
     return true;
   }
 
@@ -4323,9 +4357,11 @@ class LLVM_ABI_FOR_TEST VPScalarIVStepsRecipe : public VPRecipeWithIRFlags {
   bool doesGeneratePerAllLanes() const;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op) const override {
+  bool usesFirstLaneOnly(const VPValue *Op,
+                         bool &ShouldRecurse) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return true;
   }
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanLowering.cpp b/llvm/lib/Transforms/Vectorize/VPlanLowering.cpp
index 42e3c30c30d88..11e8165bb38f9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanLowering.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanLowering.cpp
@@ -862,9 +862,8 @@ void VPlanTransforms::materializePacksAndUnpacks(VPlan &Plan) {
 
         // Only introduce an Unpack if some, but not all, users use the first
         // lane only.
-        unsigned NumFirstLaneUsers = count_if(Def->users(), [&Def](VPUser *U) {
-          return U->usesFirstLaneOnly(Def);
-        });
+        unsigned NumFirstLaneUsers =
+            count_if(Def->users(), bind_back<vputils::usesFirstLaneOnly>(Def));
         if (!NumFirstLaneUsers || NumFirstLaneUsers == Def->getNumUsers())
           continue;
 
@@ -873,8 +872,9 @@ void VPlanTransforms::materializePacksAndUnpacks(VPlan &Plan) {
           Unpack->insertBefore(*VPBB, VPBB->getFirstNonPhi());
         else
           Unpack->insertAfter(&R);
-        Def->replaceUsesWithIf(
-            Unpack, [&Def](VPUser &U) { return U.usesFirstLaneOnly(Def); });
+        Def->replaceUsesWithIf(Unpack, [&Def](VPUser &U) {
+          return vputils::usesFirstLaneOnly(&U, Def);
+        });
       }
     }
   }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 6d0eba2b5fa57..beae6a9500be7 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -1761,10 +1761,15 @@ bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
   }
 }
 
-bool VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
+bool VPInstruction::usesFirstLaneOnly(const VPValue *Op,
+                                      bool &ShouldRecurse) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  if (Instruction::isBinaryOp(getOpcode()) || Instruction::isCast(getOpcode()))
-    return vputils::onlyFirstLaneUsed(this);
+  ShouldRecurse = false;
+  if (Instruction::isBinaryOp(getOpcode()) ||
+      Instruction::isCast(getOpcode())) {
+    ShouldRecurse = true;
+    return false;
+  }
 
   switch (getOpcode()) {
   default:
@@ -1784,7 +1789,8 @@ bool VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
   case Instruction::Freeze:
   case VPInstruction::Not:
     // TODO: Cover additional opcodes.
-    return vputils::onlyFirstLaneUsed(this);
+    ShouldRecurse = true;
+    return false;
   case Instruction::Load:
   case VPInstruction::ActiveLaneMask:
   case VPInstruction::WideActiveLaneMask:
@@ -1805,7 +1811,8 @@ bool VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
     // Before replicating, it will have only a single operand.
     return getNumOperands() > 1;
   case VPInstruction::PtrAdd:
-    return Op == getOperand(0) || vputils::onlyFirstLaneUsed(this);
+    ShouldRecurse = true;
+    return Op == getOperand(0);
   case VPInstruction::WidePtrAdd:
     // WidePtrAdd supports scalar and vector base addresses.
     return false;
@@ -1816,6 +1823,10 @@ bool VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
   llvm_unreachable("switch should return");
 }
 
+bool VPInstruction::usesScalars(const VPValue *Op) const {
+  return isSingleScalar() || vputils::usesFirstLaneOnly(this, Op);
+}
+
 bool VPInstruction::usesFirstPartOnly(const VPValue *Op) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
   if (Instruction::isBinaryOp(getOpcode()))
@@ -2247,7 +2258,7 @@ void VPWidenCallRecipe::execute(VPTransformState &State) {
     if (!VFTy->getParamType(I.index())->isVectorTy())
       Arg = State.get(I.value(), VPLane(0));
     else
-      Arg = State.get(I.value(), usesFirstLaneOnly(I.value()));
+      Arg = State.get(I.value(), vputils::usesFirstLaneOnly(this, I.value()));
     Args.push_back(Arg);
   }
 
@@ -2279,9 +2290,11 @@ InstructionCost VPWidenCallRecipe::computeCallCost(Function *Variant,
                                   Ctx.CostKind);
 }
 
-bool VPWidenCallRecipe::usesFirstLaneOnly(const VPValue *Op) const {
+bool VPWidenCallRecipe::usesFirstLaneOnly(const VPValue *Op,
+                                          bool &ShouldRecurse) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
   assert(Variant && "Variant not set");
+  ShouldRecurse = false;
   FunctionType *VFTy = Variant->getFunctionType();
   return all_of(enumerate(args()), [VFTy, &Op](const auto &Arg) {
     auto [Idx, V] = Arg;
@@ -2343,7 +2356,7 @@ CallInst *VPWidenIntrinsicRecipe::createVectorCall(VPTransformState &State) {
                                            State.TTI))
       Arg = State.get(I.value(), VPLane(0));
     else
-      Arg = State.get(I.value(), usesFirstLaneOnly(I.value()));
+      Arg = State.get(I.value(), vputils::usesFirstLaneOnly(this, I.value()));
     if (isVectorIntrinsicWithOverloadTypeAtArg(VectorIntrinsicID, I.index(),
                                                State.TTI))
       TysForDecl.push_back(Arg->getType());
@@ -2436,8 +2449,10 @@ StringRef VPWidenIntrinsicRecipe::getIntrinsicName() const {
   return Intrinsic::getBaseName(VectorIntrinsicID);
 }
 
-bool VPWidenIntrinsicRecipe::usesFirstLaneOnly(const VPValue *Op) const {
+bool VPWidenIntrinsicRecipe::usesFirstLaneOnly(const VPValue *Op,
+                                               bool &ShouldRecurse) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
+  ShouldRecurse = false;
   return all_of(enumerate(operands()), [this, &Op](const auto &X) {
     auto [Idx, V] = X;
     return V != Op || isVectorIntrinsicWithScalarOpAtArg(getVectorIntrinsicID(),
@@ -3330,8 +3345,10 @@ void VPScalarIVStepsRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
 }
 #endif
 
-bool VPWidenGEPRecipe::usesFirstLaneOnly(const VPValue *Op) const {
+bool VPWidenGEPRecipe::usesFirstLaneOnly(const VPValue *Op,
+                                         bool &ShouldRecurse) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
+  ShouldRecurse = false;
   return vputils::isSingleScalar(Op);
 }
 
@@ -5161,9 +5178,11 @@ void VPReductionPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
 }
 #endif
 
-bool VPBlendRecipe::usesFirstLaneOnly(const VPValue *Op) const {
+bool VPBlendRecipe::usesFirstLaneOnly(const VPValue *Op,
+                                      bool &ShouldRecurse) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  return vputils::onlyFirstLaneUsed(this);
+  ShouldRecurse = true;
+  return false;
 }
 
 void VPWidenPHIRecipe::execute(VPTransformState &State) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index bada5e597ae48..943d2192decf2 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -380,7 +380,7 @@ static bool sinkScalarOperands(VPlan &Plan) {
           return cast<VPRecipeBase>(U)->getParent() != SinkTo;
         });
     if (any_of(UsersOutsideSinkTo, [SinkCandidate](VPUser *U) {
-          return !U->usesFirstLaneOnly(SinkCandidate);
+          return !vputils::usesFirstLaneOnly(U, SinkCandidate);
         }))
       continue;
     bool NeedsDuplicating = !UsersOutsideSinkTo.empty();
@@ -863,7 +863,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
       bool HasScalableVF = Plan.hasScalableVF();
       WideIV->replaceUsesWithIf(Steps, [WideIV, HasScalableVF](VPUser &U) {
         if (HasScalableVF)
-          return U.usesFirstLaneOnly(WideIV);
+          return vputils::usesFirstLaneOnly(&U, WideIV);
         return U.usesScalars(WideIV);
       });
     }
@@ -1663,8 +1663,8 @@ static VPSingleDefRecipe *combineRecipe(VPlan &Plan, VPSingleDefRecipe *Def,
   // Replace uses of a BuildVector by users that only use its first lane with
   // its first operand directly.
   if (match(Def, m_BuildVector())) {
-    Def->replaceUsesWithIf(Def->getOperand(0), [Def](VPUser &U) {
-      return U.usesFirstLaneOnly(Def);
+    Def->replaceUsesWithIf(Def->getOperand(0), [&Def](const VPUser &U) {
+      return vputils::usesFirstLaneOnly(&U, Def);
     });
     return Def;
   }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
index 43e3d08291bdc..3114f7ee7ef5a 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
@@ -124,6 +124,11 @@ class UnrollState {
 };
 } // namespace
 
+bool VPUser::usesScalars(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
+  return vputils::usesFirstLaneOnly(this, Op);
+}
+
 static void addStartIndexForScalarSteps(VPScalarIVStepsRecipe *Steps,
                                         unsigned Part, VPlan &Plan) {
   if (Part == 0)
@@ -992,7 +997,7 @@ void VPlanTransforms::replicateByVF(VPlan &Plan, ElementCount VF) {
       /// Users that only demand the first lane can use the definition for lane
       /// 0.
       DefR->replaceUsesWithIf(LaneDefs[0], [DefR](VPUser &U) {
-        if (U.usesFirstLaneOnly(DefR))
+        if (vputils::usesFirstLaneOnly(&U, DefR))
           return true;
         auto *VPI = dyn_cast<VPInstruction>(&U);
         return VPI && Instruction::isCast(VPI->getOpcode());
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 3b82a2294c5d8..3833e6e7150a6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -32,9 +32,30 @@ using namespace llvm;
 using namespace llvm::VPlanPatternMatch;
 using namespace llvm::SCEVPatternMatch;
 
+bool vputils::usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
+                                const VPValue *Def) {
+  SetVector<std::pair<const VPValue *, SmallVector<const VPUser *>>> Worklist;
+  Worklist.insert({Def, to_vector(Users)});
+  for (unsigned I = 0; I < Worklist.size(); ++I) {
+    auto [WorklistDef, WorklistUsers] = Worklist[I];
+    for (const VPUser *CurU : WorklistUsers) {
+      bool ShouldRecurse;
+      if (CurU->usesFirstLaneOnly(WorklistDef, ShouldRecurse))
+        continue;
+      if (!ShouldRecurse)
+        return false;
+
+      // We rely on the fact that the current implementations of
+      // usesFirstLaneOnly do not require recursion for non-SingleDefs.
+      const VPValue *CurDef = cast<VPSingleDefRecipe>(CurU);
+      Worklist.insert({CurDef, to_vector_of<const VPUser *>(CurDef->users())});
+    }
+  }
+  return true;
+}
+
 bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
-  return all_of(Def->users(),
-                [Def](const VPUser *U) { return U->usesFirstLaneOnly(Def); });
+  return usesFirstLaneOnly(Def->users(), Def);
 }
 
 bool vputils::onlyFirstPartUsed(const VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index c046239e2f983..ac99e7cd50801 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -25,7 +25,10 @@ class PredicatedScalarEvolution;
 namespace llvm {
 
 namespace vputils {
-/// Returns true if only the first lane of \p Def is used.
+/// Returns true if only the first lane of \p Def is used by any of \p Users.
+bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users, const VPValue *Def);
+
+/// Returns true if only the first lane of \p Def is used by all users.
 bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
diff --git a/llvm/lib/Transforms/Vectorize/VPlanValue.h b/llvm/lib/Transforms/Vectorize/VPlanValue.h
index 305a1a0f9a0e2..43b94eddb21f2 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanValue.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanValue.h
@@ -475,17 +475,14 @@ class LLVM_ABI_FOR_TEST VPUser {
 
   /// Returns true if the VPUser uses scalars of operand \p Op. Conservatively
   /// returns if only first (scalar) lane is used, as default.
-  virtual bool usesScalars(const VPValue *Op) const {
-    assert(is_contained(operands(), Op) &&
-           "Op must be an operand of the recipe");
-    return usesFirstLaneOnly(Op);
-  }
+  virtual bool usesScalars(const VPValue *Op) const;
 
   /// Returns true if the VPUser only uses the first lane of operand \p Op.
   /// Conservatively returns false.
-  virtual bool usesFirstLaneOnly(const VPValue *Op) const {
+  virtual bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
+    ShouldRecurse = false;
     return false;
   }
 

>From f87de391d91330ea7ac9dd0a028fee26d3550501 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 5 Oct 2026 18:11:47 +0100
Subject: [PATCH 2/8] [VPlan] Introduce VPRecurseResult

---
 llvm/lib/Transforms/Vectorize/VPlan.h         | 165 +++++++-----------
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  85 +++++----
 llvm/lib/Transforms/Vectorize/VPlanUtils.cpp  |   6 +-
 llvm/lib/Transforms/Vectorize/VPlanUtils.h    |   6 +-
 llvm/lib/Transforms/Vectorize/VPlanValue.h    |  17 +-
 5 files changed, 124 insertions(+), 155 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index f3b7737e5627b..d94f8d5f131f2 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1570,8 +1570,9 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
   /// Returns true if the underlying opcode may read from or write to memory.
   bool opcodeMayReadOrWriteFromMemory() const;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
+  /// Returns a VPRecurseResult to determine whether the recipe only uses the
+  /// first lane of operand \p Op.
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses scalars of operand \p Op.
   bool usesScalars(const VPValue *Op) const override;
@@ -1762,12 +1763,10 @@ class VPIRInstruction : public VPRecipeBase {
     return true;
   }
 
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return true;
+    return VPRecurseResult::True;
   }
 
 protected:
@@ -1869,14 +1868,13 @@ class LLVM_ABI_FOR_TEST VPWidenRecipe : public VPRecipeWithIRFlags,
                    VPSlotTracker &SlotTracker) const override;
 #endif
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
     return Opcode == Instruction::Select && Op == getOperand(0) &&
-           isa<VPIRValue>(Op);
+                   isa<VPIRValue>(Op)
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 };
 
@@ -2049,7 +2047,7 @@ class LLVM_ABI_FOR_TEST VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags,
   /// Returns true if the intrinsic may have side-effects.
   bool mayHaveSideEffects() const { return MayHaveSideEffects; }
 
-  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2153,8 +2151,7 @@ class LLVM_ABI_FOR_TEST VPWidenCallRecipe : public VPRecipeWithIRFlags,
   operand_range args() { return drop_end(operands()); }
   const_operand_range args() const { return drop_end(operands()); }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2201,13 +2198,10 @@ class VPHistogramRecipe : public VPRecipeBase, public VPIRMetadata {
     return getNumOperands() == 3 ? getOperand(2) : nullptr;
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return Op == getOperand(1);
+    return Op == getOperand(1) ? VPRecurseResult::True : VPRecurseResult::False;
   }
 
 protected:
@@ -2264,7 +2258,7 @@ class LLVM_ABI_FOR_TEST VPWidenGEPRecipe : public VPRecipeWithIRFlags {
   }
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2319,12 +2313,10 @@ class VPVectorEndPointerRecipe : public VPRecipeWithIRFlags {
 
   void execute(VPTransformState &State) override;
 
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return true;
+    return VPRecurseResult::True;
   }
 
   /// Return the cost of this VPVectorPointerRecipe.
@@ -2393,12 +2385,10 @@ class VPVectorPointerRecipe : public VPRecipeWithIRFlags {
 
   Type *getSourceElementType() const { return SourceElementTy; }
 
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return true;
+    return VPRecurseResult::True;
   }
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
@@ -2604,16 +2594,15 @@ class VPWidenInductionRecipe : public VPHeaderPHIRecipe {
         "VPWidenIntOrFpInductionRecipe generates its own backedge value");
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // The recipe creates its own wide start value, so it only requests the
     // first lane of the operand.
     // TODO: Remove once creating the start value is modeled separately.
-    ShouldRecurse = false;
-    return Op == getStartValue() || Op == getStepValue();
+    return Op == getStartValue() || Op == getStepValue()
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 };
 
@@ -2830,12 +2819,11 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
                               VPCostContext &Ctx) const override;
 
   /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return Op == getStartValue();
+    return Op == getStartValue() ? VPRecurseResult::True
+                                 : VPRecurseResult::False;
   }
 
 protected:
@@ -2960,13 +2948,11 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe, public VPIRFlags {
 
   bool isExpressionSunk() const { return ExpressionSunk; }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return isOrdered() || isInLoop();
+    return isOrdered() || isInLoop() ? VPRecurseResult::True
+                                     : VPRecurseResult::False;
   }
 
 protected:
@@ -3051,8 +3037,7 @@ class LLVM_ABI_FOR_TEST VPBlendRecipe : public VPRecipeWithIRFlags {
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const override;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -3144,9 +3129,7 @@ class LLVM_ABI_FOR_TEST VPInterleaveBase : public VPRecipeBase,
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override = 0;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override = 0;
 
   /// Returns the number of stored operands of this interleave group. Returns 0
   /// for load interleave groups.
@@ -3185,12 +3168,12 @@ class LLVM_ABI_FOR_TEST VPInterleaveRecipe final : public VPInterleaveBase {
   /// Generate the wide load or store, and shuffles.
   void execute(VPTransformState &State) override;
 
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return Op == getAddr() && !llvm::is_contained(getStoredValues(), Op);
+    return Op == getAddr() && !llvm::is_contained(getStoredValues(), Op)
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 
   unsigned getNumStoreOperands() const override {
@@ -3235,14 +3218,13 @@ class LLVM_ABI_FOR_TEST VPInterleaveEVLRecipe final : public VPInterleaveBase {
   /// Generate the wide load or store, and shuffles.
   void execute(VPTransformState &State) override;
 
-  /// The recipe only uses the first lane of the address, and EVL operand.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
     return (Op == getAddr() && !llvm::is_contained(getStoredValues(), Op)) ||
-           Op == getEVL();
+                   Op == getEVL()
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 
   unsigned getNumStoreOperands() const override {
@@ -3408,13 +3390,10 @@ class LLVM_ABI_FOR_TEST VPReductionEVLRecipe : public VPReductionRecipe {
   /// The VPValue of the explicit vector length.
   VPValue *getEVL() const { return getOperand(2); }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return Op == getEVL();
+    return Op == getEVL() ? VPRecurseResult::True : VPRecurseResult::False;
   }
 
 protected:
@@ -3496,13 +3475,10 @@ class LLVM_ABI_FOR_TEST VPReplicateRecipe : public VPRecipeWithIRFlags,
 
   bool isPredicated() const { return IsPredicated; }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return isSingleScalar();
+    return isSingleScalar() ? VPRecurseResult::True : VPRecurseResult::False;
   }
 
   /// Returns true if the recipe uses scalars of operand \p Op.
@@ -3877,15 +3853,13 @@ struct LLVM_ABI_FOR_TEST VPWidenLoadRecipe final : public VPSingleDefRecipe,
     return VPWidenMemoryRecipe::computeCost(VF, Ctx);
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // Widened, consecutive loads operations only demand the first lane of
     // their address.
-    ShouldRecurse = false;
-    return Op == getAddr() && isConsecutive();
+    return Op == getAddr() && isConsecutive() ? VPRecurseResult::True
+                                              : VPRecurseResult::False;
   }
 
 protected:
@@ -3933,15 +3907,14 @@ struct LLVM_ABI_FOR_TEST VPWidenLoadEVLRecipe final
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // Widened loads only demand the first lane of EVL and consecutive loads
     // only demand the first lane of their address.
-    ShouldRecurse = false;
-    return Op == getEVL() || (Op == getAddr() && isConsecutive());
+    return Op == getEVL() || (Op == getAddr() && isConsecutive())
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 
 protected:
@@ -3987,15 +3960,14 @@ struct LLVM_ABI_FOR_TEST VPWidenStoreRecipe final : public VPRecipeBase,
     return VPWidenMemoryRecipe::computeCost(VF, Ctx);
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
     // Widened, consecutive stores only demand the first lane of their address,
     // unless the same operand is also stored.
-    ShouldRecurse = false;
-    return Op == getAddr() && isConsecutive() && Op != getStoredValue();
+    return Op == getAddr() && isConsecutive() && Op != getStoredValue()
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 
 protected:
@@ -4042,20 +4014,19 @@ struct LLVM_ABI_FOR_TEST VPWidenStoreEVLRecipe final
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
     if (Op == getEVL()) {
       assert(getStoredValue() != Op && "unexpected store of EVL");
-      return true;
+      return VPRecurseResult::True;
     }
     // Widened, consecutive memory operations only demand the first lane of
     // their address, unless the same operand is also stored. That latter can
     // happen with opaque pointers.
-    return Op == getAddr() && isConsecutive() && Op != getStoredValue();
+    return Op == getAddr() && isConsecutive() && Op != getStoredValue()
+               ? VPRecurseResult::True
+               : VPRecurseResult::False;
   }
 
 protected:
@@ -4163,13 +4134,10 @@ class VPCurrentIterationPHIRecipe : public VPHeaderPHIRecipe {
     return 0;
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return true;
+    return VPRecurseResult::True;
   }
 
 protected:
@@ -4279,12 +4247,10 @@ class LLVM_ABI_FOR_TEST VPDerivedIVRecipe : public VPRecipeWithIRFlags {
   const FPMathOperator *getFPBinOp() const { return FPBinOp; }
   InductionDescriptor::InductionKind getInductionKind() const { return Kind; }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecuse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecuse = false;
-    return true;
+    return VPRecurseResult::True;
   }
 
 protected:
@@ -4356,13 +4322,10 @@ class LLVM_ABI_FOR_TEST VPScalarIVStepsRecipe : public VPRecipeWithIRFlags {
   /// Returns true if this recipe produces scalar values for all VF lanes.
   bool doesGeneratePerAllLanes() const;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool usesFirstLaneOnly(const VPValue *Op,
-                         bool &ShouldRecurse) const override {
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return true;
+    return VPRecurseResult::True;
   }
 
   Instruction::BinaryOps getInductionOpcode() const { return InductionOpcode; }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index beae6a9500be7..102569c76a56e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -1761,27 +1761,25 @@ bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
   }
 }
 
-bool VPInstruction::usesFirstLaneOnly(const VPValue *Op,
-                                      bool &ShouldRecurse) const {
+VPRecurseResult VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  ShouldRecurse = false;
   if (Instruction::isBinaryOp(getOpcode()) ||
       Instruction::isCast(getOpcode())) {
-    ShouldRecurse = true;
-    return false;
+    return VPRecurseResult::FalseRecurse;
   }
 
   switch (getOpcode()) {
   default:
-    return false;
+    return VPRecurseResult::False;
   case Instruction::ExtractElement:
-    return Op == getOperand(1);
+    return Op == getOperand(1) ? VPRecurseResult::True : VPRecurseResult::False;
   case Instruction::InsertElement:
-    return Op == getOperand(1) || Op == getOperand(2);
+    return Op == getOperand(1) || Op == getOperand(2) ? VPRecurseResult::True
+                                                      : VPRecurseResult::False;
   case VPInstruction::ExtractLastActive:
-    return Op == getOperand(0);
+    return Op == getOperand(0) ? VPRecurseResult::True : VPRecurseResult::False;
   case Instruction::PHI:
-    return true;
+    return VPRecurseResult::True;
   case Instruction::FCmp:
   case Instruction::ICmp:
   case Instruction::Select:
@@ -1789,8 +1787,7 @@ bool VPInstruction::usesFirstLaneOnly(const VPValue *Op,
   case Instruction::Freeze:
   case VPInstruction::Not:
     // TODO: Cover additional opcodes.
-    ShouldRecurse = true;
-    return false;
+    return VPRecurseResult::FalseRecurse;
   case Instruction::Load:
   case VPInstruction::ActiveLaneMask:
   case VPInstruction::WideActiveLaneMask:
@@ -1803,22 +1800,23 @@ bool VPInstruction::usesFirstLaneOnly(const VPValue *Op,
   case VPInstruction::Intrinsic:
   case VPInstruction::ReductionStartVector:
   case VPInstruction::ResumeForEpilogue:
-    return true;
+    return VPRecurseResult::True;
   case VPInstruction::BuildStructVector:
   case VPInstruction::BuildVector:
     // Before replicating by VF, Build(Struct)Vector uses all lanes of the
     // operand, after replicating its operands only the first lane is used.
     // Before replicating, it will have only a single operand.
-    return getNumOperands() > 1;
+    return getNumOperands() > 1 ? VPRecurseResult::True
+                                : VPRecurseResult::False;
   case VPInstruction::PtrAdd:
-    ShouldRecurse = true;
-    return Op == getOperand(0);
+    return Op == getOperand(0) ? VPRecurseResult::True
+                               : VPRecurseResult::FalseRecurse;
   case VPInstruction::WidePtrAdd:
     // WidePtrAdd supports scalar and vector base addresses.
-    return false;
+    return VPRecurseResult::False;
   case VPInstruction::ExitingIVValue:
   case VPInstruction::ExtractLane:
-    return Op == getOperand(0);
+    return Op == getOperand(0) ? VPRecurseResult::True : VPRecurseResult::False;
   };
   llvm_unreachable("switch should return");
 }
@@ -2290,18 +2288,20 @@ InstructionCost VPWidenCallRecipe::computeCallCost(Function *Variant,
                                   Ctx.CostKind);
 }
 
-bool VPWidenCallRecipe::usesFirstLaneOnly(const VPValue *Op,
-                                          bool &ShouldRecurse) const {
+VPRecurseResult VPWidenCallRecipe::usesFirstLaneOnly(const VPValue *Op) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
   assert(Variant && "Variant not set");
-  ShouldRecurse = false;
   FunctionType *VFTy = Variant->getFunctionType();
-  return all_of(enumerate(args()), [VFTy, &Op](const auto &Arg) {
-    auto [Idx, V] = Arg;
-    Type *ArgTy = VFTy->getParamType(Idx);
-    return V != Op || ArgTy->isIntegerTy() || ArgTy->isFloatingPointTy() ||
-           ArgTy->isPointerTy() || ArgTy->isByteTy();
-  });
+  return all_of(enumerate(args()),
+                [VFTy, &Op](const auto &Arg) {
+                  auto [Idx, V] = Arg;
+                  Type *ArgTy = VFTy->getParamType(Idx);
+                  return V != Op || ArgTy->isIntegerTy() ||
+                         ArgTy->isFloatingPointTy() || ArgTy->isPointerTy() ||
+                         ArgTy->isByteTy();
+                })
+             ? VPRecurseResult::True
+             : VPRecurseResult::False;
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2449,15 +2449,17 @@ StringRef VPWidenIntrinsicRecipe::getIntrinsicName() const {
   return Intrinsic::getBaseName(VectorIntrinsicID);
 }
 
-bool VPWidenIntrinsicRecipe::usesFirstLaneOnly(const VPValue *Op,
-                                               bool &ShouldRecurse) const {
+VPRecurseResult
+VPWidenIntrinsicRecipe::usesFirstLaneOnly(const VPValue *Op) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  ShouldRecurse = false;
-  return all_of(enumerate(operands()), [this, &Op](const auto &X) {
-    auto [Idx, V] = X;
-    return V != Op || isVectorIntrinsicWithScalarOpAtArg(getVectorIntrinsicID(),
-                                                         Idx, nullptr);
-  });
+  return all_of(enumerate(operands()),
+                [this, &Op](const auto &X) {
+                  auto [Idx, V] = X;
+                  return V != Op || isVectorIntrinsicWithScalarOpAtArg(
+                                        getVectorIntrinsicID(), Idx, nullptr);
+                })
+             ? VPRecurseResult::True
+             : VPRecurseResult::False;
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -3345,11 +3347,10 @@ void VPScalarIVStepsRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
 }
 #endif
 
-bool VPWidenGEPRecipe::usesFirstLaneOnly(const VPValue *Op,
-                                         bool &ShouldRecurse) const {
+VPRecurseResult VPWidenGEPRecipe::usesFirstLaneOnly(const VPValue *Op) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  ShouldRecurse = false;
-  return vputils::isSingleScalar(Op);
+  return vputils::isSingleScalar(Op) ? VPRecurseResult::True
+                                     : VPRecurseResult::False;
 }
 
 void VPWidenGEPRecipe::execute(VPTransformState &State) {
@@ -5178,11 +5179,9 @@ void VPReductionPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
 }
 #endif
 
-bool VPBlendRecipe::usesFirstLaneOnly(const VPValue *Op,
-                                      bool &ShouldRecurse) const {
+VPRecurseResult VPBlendRecipe::usesFirstLaneOnly(const VPValue *Op) const {
   assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  ShouldRecurse = true;
-  return false;
+  return VPRecurseResult::FalseRecurse;
 }
 
 void VPWidenPHIRecipe::execute(VPTransformState &State) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 3833e6e7150a6..43aacde3924c9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -39,10 +39,10 @@ bool vputils::usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
   for (unsigned I = 0; I < Worklist.size(); ++I) {
     auto [WorklistDef, WorklistUsers] = Worklist[I];
     for (const VPUser *CurU : WorklistUsers) {
-      bool ShouldRecurse;
-      if (CurU->usesFirstLaneOnly(WorklistDef, ShouldRecurse))
+      VPRecurseResult Res = CurU->usesFirstLaneOnly(WorklistDef);
+      if (Res == VPRecurseResult::True)
         continue;
-      if (!ShouldRecurse)
+      if (Res != VPRecurseResult::FalseRecurse)
         return false;
 
       // We rely on the fact that the current implementations of
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index ac99e7cd50801..a276265ca1339 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -25,16 +25,16 @@ class PredicatedScalarEvolution;
 namespace llvm {
 
 namespace vputils {
-/// Returns true if only the first lane of \p Def is used by any of \p Users.
+/// Returns true if only the first lane of \p Def is used by all of \p Users.
 bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users, const VPValue *Def);
 
-/// Returns true if only the first lane of \p Def is used by all users.
+/// Returns true if only the first lane of \p Def is used by all its users.
 bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(const VPValue *Def);
 
-/// Returns true if only scalar values of \p Def are used by all users.
+/// Returns true if only scalar values of \p Def are used by all its users.
 bool onlyScalarValuesUsed(const VPValue *Def);
 
 /// Get or create a VPValue that corresponds to the expansion of \p Expr. If \p
diff --git a/llvm/lib/Transforms/Vectorize/VPlanValue.h b/llvm/lib/Transforms/Vectorize/VPlanValue.h
index 43b94eddb21f2..9c18b41872587 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanValue.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanValue.h
@@ -44,6 +44,14 @@ class VPRegionValue;
 class VPRegionBlock;
 class VPSingleDefRecipe;
 
+/// An enumeration capturing a boolean non-recursive result, along with a state
+/// to recurse-on-false.
+enum class VPRecurseResult {
+  True = (1 << 0),
+  False = (1 << 1),
+  FalseRecurse = (1 << 2),
+};
+
 /// This is the base class of the VPlan Def/Use graph, used for modeling the
 /// data flow into, within and out of the VPlan. VPValues can stand for live-ins
 /// coming from the input IR, symbolic values and values defined by recipes.
@@ -477,13 +485,12 @@ class LLVM_ABI_FOR_TEST VPUser {
   /// returns if only first (scalar) lane is used, as default.
   virtual bool usesScalars(const VPValue *Op) const;
 
-  /// Returns true if the VPUser only uses the first lane of operand \p Op.
-  /// Conservatively returns false.
-  virtual bool usesFirstLaneOnly(const VPValue *Op, bool &ShouldRecurse) const {
+  /// Returns a VPRecurseResult indicating whether the VPUser only uses the
+  /// first lane of operand \p Op. Conservatively returns False.
+  virtual VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
-    ShouldRecurse = false;
-    return false;
+    return VPRecurseResult::False;
   }
 
   /// Returns true if the VPUser only uses the first part of operand \p Op.

>From ac1042019274e7d23579b9441ec0495c6b02fbda Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 5 Oct 2026 19:56:18 +0100
Subject: [PATCH 3/8] [VPlan] Optz for CT

---
 llvm/include/llvm/ADT/MapVector.h            |  3 +++
 llvm/lib/Transforms/Vectorize/VPlanUtils.cpp | 22 ----------------
 llvm/lib/Transforms/Vectorize/VPlanUtils.h   | 27 ++++++++++++++++++--
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 26b389756fd27..43f1be2126661 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -86,6 +86,9 @@ class MapVector {
   [[nodiscard]] const std::pair<KeyT, ValueT> &back() const {
     return Vector.back();
   }
+  [[nodiscard]] std::pair<KeyT, ValueT> at_idx(unsigned Idx) const { // NOLINT
+    return Vector[Idx];
+  }
 
   void clear() {
     Map.clear();
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 43aacde3924c9..ad2c65ba7ef4e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -32,28 +32,6 @@ using namespace llvm;
 using namespace llvm::VPlanPatternMatch;
 using namespace llvm::SCEVPatternMatch;
 
-bool vputils::usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
-                                const VPValue *Def) {
-  SetVector<std::pair<const VPValue *, SmallVector<const VPUser *>>> Worklist;
-  Worklist.insert({Def, to_vector(Users)});
-  for (unsigned I = 0; I < Worklist.size(); ++I) {
-    auto [WorklistDef, WorklistUsers] = Worklist[I];
-    for (const VPUser *CurU : WorklistUsers) {
-      VPRecurseResult Res = CurU->usesFirstLaneOnly(WorklistDef);
-      if (Res == VPRecurseResult::True)
-        continue;
-      if (Res != VPRecurseResult::FalseRecurse)
-        return false;
-
-      // We rely on the fact that the current implementations of
-      // usesFirstLaneOnly do not require recursion for non-SingleDefs.
-      const VPValue *CurDef = cast<VPSingleDefRecipe>(CurU);
-      Worklist.insert({CurDef, to_vector_of<const VPUser *>(CurDef->users())});
-    }
-  }
-  return true;
-}
-
 bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return usesFirstLaneOnly(Def->users(), Def);
 }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index a276265ca1339..bfd4257cf4743 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -25,8 +25,31 @@ class PredicatedScalarEvolution;
 namespace llvm {
 
 namespace vputils {
-/// Returns true if only the first lane of \p Def is used by all of \p Users.
-bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users, const VPValue *Def);
+/// Returns true if only the first lane of \p Def is used by all of \p Users
+/// recursively.
+inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
+                              const VPValue *Def) {
+  SmallMapVector<const VPValue *, SmallVector<const VPUser *>, 8> Worklist;
+  Worklist.insert({Def, to_vector(Users)});
+  for (unsigned I = 0; I < Worklist.size(); ++I) {
+    auto [WorklistDef, WorklistUsers] = Worklist.at_idx(I);
+    for (const VPUser *CurU : WorklistUsers) {
+      if (Worklist.contains(dyn_cast<VPSingleDefRecipe>(CurU)))
+        continue;
+      VPRecurseResult Res = CurU->usesFirstLaneOnly(WorklistDef);
+      if (Res == VPRecurseResult::True)
+        continue;
+      if (Res != VPRecurseResult::FalseRecurse)
+        return false;
+
+      // We rely on the fact that the current implementations of
+      // usesFirstLaneOnly do not require recursion for non-SingleDefs.
+      const VPValue *CurDef = cast<VPSingleDefRecipe>(CurU);
+      Worklist.insert({CurDef, to_vector_of<const VPUser *>(CurDef->users())});
+    }
+  }
+  return true;
+}
 
 /// Returns true if only the first lane of \p Def is used by all its users.
 bool onlyFirstLaneUsed(const VPValue *Def);

>From 8b1e4ab2862a9d92dfa2a75c8c54c510b95b4517 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 5 Oct 2026 21:43:35 +0100
Subject: [PATCH 4/8] [VPlan] More aggressive CT

---
 llvm/lib/Transforms/Vectorize/VPlan.h         | 71 ++++++++++++++++++-
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp | 65 -----------------
 llvm/lib/Transforms/Vectorize/VPlanUtils.cpp  |  6 --
 llvm/lib/Transforms/Vectorize/VPlanUtils.h    | 12 ++--
 4 files changed, 77 insertions(+), 77 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index d94f8d5f131f2..a825a1df3e2af 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1572,7 +1572,70 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
 
   /// Returns a VPRecurseResult to determine whether the recipe only uses the
   /// first lane of operand \p Op.
-  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
+    assert(is_contained(operands(), Op) &&
+           "Op must be an operand of the recipe");
+    if (Instruction::isBinaryOp(getOpcode()) ||
+        Instruction::isCast(getOpcode())) {
+      return VPRecurseResult::FalseRecurse;
+    }
+
+    switch (getOpcode()) {
+    default:
+      return VPRecurseResult::False;
+    case Instruction::ExtractElement:
+      return Op == getOperand(1) ? VPRecurseResult::True
+                                 : VPRecurseResult::False;
+    case Instruction::InsertElement:
+      return Op == getOperand(1) || Op == getOperand(2)
+                 ? VPRecurseResult::True
+                 : VPRecurseResult::False;
+    case VPInstruction::ExtractLastActive:
+      return Op == getOperand(0) ? VPRecurseResult::True
+                                 : VPRecurseResult::False;
+    case Instruction::PHI:
+      return VPRecurseResult::True;
+    case Instruction::FCmp:
+    case Instruction::ICmp:
+    case Instruction::Select:
+    case Instruction::Or:
+    case Instruction::Freeze:
+    case VPInstruction::Not:
+      // TODO: Cover additional opcodes.
+      return VPRecurseResult::FalseRecurse;
+    case Instruction::Load:
+    case VPInstruction::ActiveLaneMask:
+    case VPInstruction::WideActiveLaneMask:
+    case VPInstruction::ExplicitVectorLength:
+    case VPInstruction::CanonicalIVIncrementForPart:
+    case VPInstruction::BranchOnCount:
+    case VPInstruction::BranchOnCond:
+    case VPInstruction::BranchOnTwoConds:
+    case VPInstruction::Broadcast:
+    case VPInstruction::Intrinsic:
+    case VPInstruction::ReductionStartVector:
+    case VPInstruction::ResumeForEpilogue:
+      return VPRecurseResult::True;
+    case VPInstruction::BuildStructVector:
+    case VPInstruction::BuildVector:
+      // Before replicating by VF, Build(Struct)Vector uses all lanes of the
+      // operand, after replicating its operands only the first lane is used.
+      // Before replicating, it will have only a single operand.
+      return getNumOperands() > 1 ? VPRecurseResult::True
+                                  : VPRecurseResult::False;
+    case VPInstruction::PtrAdd:
+      return Op == getOperand(0) ? VPRecurseResult::True
+                                 : VPRecurseResult::FalseRecurse;
+    case VPInstruction::WidePtrAdd:
+      // WidePtrAdd supports scalar and vector base addresses.
+      return VPRecurseResult::False;
+    case VPInstruction::ExitingIVValue:
+    case VPInstruction::ExtractLane:
+      return Op == getOperand(0) ? VPRecurseResult::True
+                                 : VPRecurseResult::False;
+    };
+    llvm_unreachable("switch should return");
+  }
 
   /// Returns true if the recipe only uses scalars of operand \p Op.
   bool usesScalars(const VPValue *Op) const override;
@@ -3037,7 +3100,11 @@ class LLVM_ABI_FOR_TEST VPBlendRecipe : public VPRecipeWithIRFlags {
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
+    assert(is_contained(operands(), Op) &&
+           "Op must be an operand of the recipe");
+    return VPRecurseResult::FalseRecurse;
+  }
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 102569c76a56e..f7ffd1f34a2ab 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -1761,66 +1761,6 @@ bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
   }
 }
 
-VPRecurseResult VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
-  assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  if (Instruction::isBinaryOp(getOpcode()) ||
-      Instruction::isCast(getOpcode())) {
-    return VPRecurseResult::FalseRecurse;
-  }
-
-  switch (getOpcode()) {
-  default:
-    return VPRecurseResult::False;
-  case Instruction::ExtractElement:
-    return Op == getOperand(1) ? VPRecurseResult::True : VPRecurseResult::False;
-  case Instruction::InsertElement:
-    return Op == getOperand(1) || Op == getOperand(2) ? VPRecurseResult::True
-                                                      : VPRecurseResult::False;
-  case VPInstruction::ExtractLastActive:
-    return Op == getOperand(0) ? VPRecurseResult::True : VPRecurseResult::False;
-  case Instruction::PHI:
-    return VPRecurseResult::True;
-  case Instruction::FCmp:
-  case Instruction::ICmp:
-  case Instruction::Select:
-  case Instruction::Or:
-  case Instruction::Freeze:
-  case VPInstruction::Not:
-    // TODO: Cover additional opcodes.
-    return VPRecurseResult::FalseRecurse;
-  case Instruction::Load:
-  case VPInstruction::ActiveLaneMask:
-  case VPInstruction::WideActiveLaneMask:
-  case VPInstruction::ExplicitVectorLength:
-  case VPInstruction::CanonicalIVIncrementForPart:
-  case VPInstruction::BranchOnCount:
-  case VPInstruction::BranchOnCond:
-  case VPInstruction::BranchOnTwoConds:
-  case VPInstruction::Broadcast:
-  case VPInstruction::Intrinsic:
-  case VPInstruction::ReductionStartVector:
-  case VPInstruction::ResumeForEpilogue:
-    return VPRecurseResult::True;
-  case VPInstruction::BuildStructVector:
-  case VPInstruction::BuildVector:
-    // Before replicating by VF, Build(Struct)Vector uses all lanes of the
-    // operand, after replicating its operands only the first lane is used.
-    // Before replicating, it will have only a single operand.
-    return getNumOperands() > 1 ? VPRecurseResult::True
-                                : VPRecurseResult::False;
-  case VPInstruction::PtrAdd:
-    return Op == getOperand(0) ? VPRecurseResult::True
-                               : VPRecurseResult::FalseRecurse;
-  case VPInstruction::WidePtrAdd:
-    // WidePtrAdd supports scalar and vector base addresses.
-    return VPRecurseResult::False;
-  case VPInstruction::ExitingIVValue:
-  case VPInstruction::ExtractLane:
-    return Op == getOperand(0) ? VPRecurseResult::True : VPRecurseResult::False;
-  };
-  llvm_unreachable("switch should return");
-}
-
 bool VPInstruction::usesScalars(const VPValue *Op) const {
   return isSingleScalar() || vputils::usesFirstLaneOnly(this, Op);
 }
@@ -5179,11 +5119,6 @@ void VPReductionPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
 }
 #endif
 
-VPRecurseResult VPBlendRecipe::usesFirstLaneOnly(const VPValue *Op) const {
-  assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
-  return VPRecurseResult::FalseRecurse;
-}
-
 void VPWidenPHIRecipe::execute(VPTransformState &State) {
   executePhiRecipe(this, *this, State, /*IsScalar=*/false, Name);
 }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index ad2c65ba7ef4e..6f07e5f44d0fe 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -12,9 +12,7 @@
 #include "VPlanCFG.h"
 #include "VPlanDominatorTree.h"
 #include "VPlanPatternMatch.h"
-#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SetVector.h"
-#include "llvm/ADT/SmallVectorExtras.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
 #include "llvm/Analysis/BranchProbabilityInfo.h"
@@ -32,10 +30,6 @@ using namespace llvm;
 using namespace llvm::VPlanPatternMatch;
 using namespace llvm::SCEVPatternMatch;
 
-bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
-  return usesFirstLaneOnly(Def->users(), Def);
-}
-
 bool vputils::onlyFirstPartUsed(const VPValue *Def) {
   return all_of(Def->users(),
                 [Def](const VPUser *U) { return U->usesFirstPartOnly(Def); });
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index bfd4257cf4743..9d020b3ba8d31 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -10,6 +10,8 @@
 #define LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
 
 #include "VPlan.h"
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/BlockFrequency.h"
 #include "llvm/Support/BranchProbability.h"
 #include "llvm/Support/Compiler.h"
@@ -29,8 +31,8 @@ namespace vputils {
 /// recursively.
 inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
                               const VPValue *Def) {
-  SmallMapVector<const VPValue *, SmallVector<const VPUser *>, 8> Worklist;
-  Worklist.insert({Def, to_vector(Users)});
+  SmallMapVector<const VPValue *, SmallVector<const VPUser *>, 16> Worklist;
+  Worklist.try_emplace(Def, Users);
   for (unsigned I = 0; I < Worklist.size(); ++I) {
     auto [WorklistDef, WorklistUsers] = Worklist.at_idx(I);
     for (const VPUser *CurU : WorklistUsers) {
@@ -45,14 +47,16 @@ inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
       // We rely on the fact that the current implementations of
       // usesFirstLaneOnly do not require recursion for non-SingleDefs.
       const VPValue *CurDef = cast<VPSingleDefRecipe>(CurU);
-      Worklist.insert({CurDef, to_vector_of<const VPUser *>(CurDef->users())});
+      Worklist.try_emplace(CurDef, CurDef->users());
     }
   }
   return true;
 }
 
 /// Returns true if only the first lane of \p Def is used by all its users.
-bool onlyFirstLaneUsed(const VPValue *Def);
+inline bool onlyFirstLaneUsed(const VPValue *Def) {
+  return usesFirstLaneOnly(Def->users(), Def);
+}
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(const VPValue *Def);

>From 9bb296bac218682fb05fb685f594f5cbd31be9e4 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 6 Oct 2026 10:39:25 +0100
Subject: [PATCH 5/8] [VPlan] Drop unnecessary Users vector

---
 llvm/include/llvm/ADT/MapVector.h          |  3 ---
 llvm/lib/Transforms/Vectorize/VPlanUtils.h | 15 +++++++--------
 2 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 43f1be2126661..26b389756fd27 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -86,9 +86,6 @@ class MapVector {
   [[nodiscard]] const std::pair<KeyT, ValueT> &back() const {
     return Vector.back();
   }
-  [[nodiscard]] std::pair<KeyT, ValueT> at_idx(unsigned Idx) const { // NOLINT
-    return Vector[Idx];
-  }
 
   void clear() {
     Map.clear();
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 9d020b3ba8d31..bf180141049ff 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -31,14 +31,14 @@ namespace vputils {
 /// recursively.
 inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
                               const VPValue *Def) {
-  SmallMapVector<const VPValue *, SmallVector<const VPUser *>, 16> Worklist;
-  Worklist.try_emplace(Def, Users);
+  SmallSetVector<const VPValue *, 16> Worklist;
+  Worklist.insert(Def);
   for (unsigned I = 0; I < Worklist.size(); ++I) {
-    auto [WorklistDef, WorklistUsers] = Worklist.at_idx(I);
-    for (const VPUser *CurU : WorklistUsers) {
-      if (Worklist.contains(dyn_cast<VPSingleDefRecipe>(CurU)))
+    const VPValue *WorklistDef = Worklist[I];
+    for (const VPUser *U : WorklistDef == Def ? Users : WorklistDef->users()) {
+      if (Worklist.contains(dyn_cast<VPSingleDefRecipe>(U)))
         continue;
-      VPRecurseResult Res = CurU->usesFirstLaneOnly(WorklistDef);
+      VPRecurseResult Res = U->usesFirstLaneOnly(WorklistDef);
       if (Res == VPRecurseResult::True)
         continue;
       if (Res != VPRecurseResult::FalseRecurse)
@@ -46,8 +46,7 @@ inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
 
       // We rely on the fact that the current implementations of
       // usesFirstLaneOnly do not require recursion for non-SingleDefs.
-      const VPValue *CurDef = cast<VPSingleDefRecipe>(CurU);
-      Worklist.try_emplace(CurDef, CurDef->users());
+      Worklist.insert(cast<VPSingleDefRecipe>(U));
     }
   }
   return true;

>From 2ec8389359c14ff9a81305b8e5e79cc784d9f1ec Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 6 Oct 2026 10:51:42 +0100
Subject: [PATCH 6/8] [VPlan] Fix an include, strip some bad comments

---
 llvm/lib/Transforms/Vectorize/VPlan.h      | 2 --
 llvm/lib/Transforms/Vectorize/VPlanUtils.h | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index a825a1df3e2af..91a00d4679922 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2320,7 +2320,6 @@ class LLVM_ABI_FOR_TEST VPWidenGEPRecipe : public VPRecipeWithIRFlags {
     return 0;
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
   VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
 protected:
@@ -2881,7 +2880,6 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
   VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
     assert(is_contained(operands(), Op) &&
            "Op must be an operand of the recipe");
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index bf180141049ff..3458646ab6134 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -10,7 +10,7 @@
 #define LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
 
 #include "VPlan.h"
-#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/BlockFrequency.h"
 #include "llvm/Support/BranchProbability.h"

>From 4a71bead632a809f18f552c0dac69b9e11088ab1 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 6 Oct 2026 11:55:31 +0100
Subject: [PATCH 7/8] [VPlan] Put methods back into cpp files

---
 llvm/lib/Transforms/Vectorize/VPlan.h         | 71 +------------------
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp | 65 +++++++++++++++++
 llvm/lib/Transforms/Vectorize/VPlanUtils.cpp  |  4 ++
 llvm/lib/Transforms/Vectorize/VPlanUtils.h    |  4 +-
 4 files changed, 72 insertions(+), 72 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 91a00d4679922..28163e2707fa4 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1572,70 +1572,7 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
 
   /// Returns a VPRecurseResult to determine whether the recipe only uses the
   /// first lane of operand \p Op.
-  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
-    assert(is_contained(operands(), Op) &&
-           "Op must be an operand of the recipe");
-    if (Instruction::isBinaryOp(getOpcode()) ||
-        Instruction::isCast(getOpcode())) {
-      return VPRecurseResult::FalseRecurse;
-    }
-
-    switch (getOpcode()) {
-    default:
-      return VPRecurseResult::False;
-    case Instruction::ExtractElement:
-      return Op == getOperand(1) ? VPRecurseResult::True
-                                 : VPRecurseResult::False;
-    case Instruction::InsertElement:
-      return Op == getOperand(1) || Op == getOperand(2)
-                 ? VPRecurseResult::True
-                 : VPRecurseResult::False;
-    case VPInstruction::ExtractLastActive:
-      return Op == getOperand(0) ? VPRecurseResult::True
-                                 : VPRecurseResult::False;
-    case Instruction::PHI:
-      return VPRecurseResult::True;
-    case Instruction::FCmp:
-    case Instruction::ICmp:
-    case Instruction::Select:
-    case Instruction::Or:
-    case Instruction::Freeze:
-    case VPInstruction::Not:
-      // TODO: Cover additional opcodes.
-      return VPRecurseResult::FalseRecurse;
-    case Instruction::Load:
-    case VPInstruction::ActiveLaneMask:
-    case VPInstruction::WideActiveLaneMask:
-    case VPInstruction::ExplicitVectorLength:
-    case VPInstruction::CanonicalIVIncrementForPart:
-    case VPInstruction::BranchOnCount:
-    case VPInstruction::BranchOnCond:
-    case VPInstruction::BranchOnTwoConds:
-    case VPInstruction::Broadcast:
-    case VPInstruction::Intrinsic:
-    case VPInstruction::ReductionStartVector:
-    case VPInstruction::ResumeForEpilogue:
-      return VPRecurseResult::True;
-    case VPInstruction::BuildStructVector:
-    case VPInstruction::BuildVector:
-      // Before replicating by VF, Build(Struct)Vector uses all lanes of the
-      // operand, after replicating its operands only the first lane is used.
-      // Before replicating, it will have only a single operand.
-      return getNumOperands() > 1 ? VPRecurseResult::True
-                                  : VPRecurseResult::False;
-    case VPInstruction::PtrAdd:
-      return Op == getOperand(0) ? VPRecurseResult::True
-                                 : VPRecurseResult::FalseRecurse;
-    case VPInstruction::WidePtrAdd:
-      // WidePtrAdd supports scalar and vector base addresses.
-      return VPRecurseResult::False;
-    case VPInstruction::ExitingIVValue:
-    case VPInstruction::ExtractLane:
-      return Op == getOperand(0) ? VPRecurseResult::True
-                                 : VPRecurseResult::False;
-    };
-    llvm_unreachable("switch should return");
-  }
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses scalars of operand \p Op.
   bool usesScalars(const VPValue *Op) const override;
@@ -3098,11 +3035,7 @@ class LLVM_ABI_FOR_TEST VPBlendRecipe : public VPRecipeWithIRFlags {
   InstructionCost computeCost(ElementCount VF,
                               VPCostContext &Ctx) const override;
 
-  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override {
-    assert(is_contained(operands(), Op) &&
-           "Op must be an operand of the recipe");
-    return VPRecurseResult::FalseRecurse;
-  }
+  VPRecurseResult usesFirstLaneOnly(const VPValue *Op) const override;
 
 protected:
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index f7ffd1f34a2ab..102569c76a56e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -1761,6 +1761,66 @@ bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
   }
 }
 
+VPRecurseResult VPInstruction::usesFirstLaneOnly(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
+  if (Instruction::isBinaryOp(getOpcode()) ||
+      Instruction::isCast(getOpcode())) {
+    return VPRecurseResult::FalseRecurse;
+  }
+
+  switch (getOpcode()) {
+  default:
+    return VPRecurseResult::False;
+  case Instruction::ExtractElement:
+    return Op == getOperand(1) ? VPRecurseResult::True : VPRecurseResult::False;
+  case Instruction::InsertElement:
+    return Op == getOperand(1) || Op == getOperand(2) ? VPRecurseResult::True
+                                                      : VPRecurseResult::False;
+  case VPInstruction::ExtractLastActive:
+    return Op == getOperand(0) ? VPRecurseResult::True : VPRecurseResult::False;
+  case Instruction::PHI:
+    return VPRecurseResult::True;
+  case Instruction::FCmp:
+  case Instruction::ICmp:
+  case Instruction::Select:
+  case Instruction::Or:
+  case Instruction::Freeze:
+  case VPInstruction::Not:
+    // TODO: Cover additional opcodes.
+    return VPRecurseResult::FalseRecurse;
+  case Instruction::Load:
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::WideActiveLaneMask:
+  case VPInstruction::ExplicitVectorLength:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+  case VPInstruction::BranchOnCond:
+  case VPInstruction::BranchOnTwoConds:
+  case VPInstruction::Broadcast:
+  case VPInstruction::Intrinsic:
+  case VPInstruction::ReductionStartVector:
+  case VPInstruction::ResumeForEpilogue:
+    return VPRecurseResult::True;
+  case VPInstruction::BuildStructVector:
+  case VPInstruction::BuildVector:
+    // Before replicating by VF, Build(Struct)Vector uses all lanes of the
+    // operand, after replicating its operands only the first lane is used.
+    // Before replicating, it will have only a single operand.
+    return getNumOperands() > 1 ? VPRecurseResult::True
+                                : VPRecurseResult::False;
+  case VPInstruction::PtrAdd:
+    return Op == getOperand(0) ? VPRecurseResult::True
+                               : VPRecurseResult::FalseRecurse;
+  case VPInstruction::WidePtrAdd:
+    // WidePtrAdd supports scalar and vector base addresses.
+    return VPRecurseResult::False;
+  case VPInstruction::ExitingIVValue:
+  case VPInstruction::ExtractLane:
+    return Op == getOperand(0) ? VPRecurseResult::True : VPRecurseResult::False;
+  };
+  llvm_unreachable("switch should return");
+}
+
 bool VPInstruction::usesScalars(const VPValue *Op) const {
   return isSingleScalar() || vputils::usesFirstLaneOnly(this, Op);
 }
@@ -5119,6 +5179,11 @@ void VPReductionPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
 }
 #endif
 
+VPRecurseResult VPBlendRecipe::usesFirstLaneOnly(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the recipe");
+  return VPRecurseResult::FalseRecurse;
+}
+
 void VPWidenPHIRecipe::execute(VPTransformState &State) {
   executePhiRecipe(this, *this, State, /*IsScalar=*/false, Name);
 }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 6f07e5f44d0fe..586719c534faa 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -30,6 +30,10 @@ using namespace llvm;
 using namespace llvm::VPlanPatternMatch;
 using namespace llvm::SCEVPatternMatch;
 
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
+  return usesFirstLaneOnly(Def->users(), Def);
+}
+
 bool vputils::onlyFirstPartUsed(const VPValue *Def) {
   return all_of(Def->users(),
                 [Def](const VPUser *U) { return U->usesFirstPartOnly(Def); });
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 3458646ab6134..9950a038914c5 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -53,9 +53,7 @@ inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
 }
 
 /// Returns true if only the first lane of \p Def is used by all its users.
-inline bool onlyFirstLaneUsed(const VPValue *Def) {
-  return usesFirstLaneOnly(Def->users(), Def);
-}
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(const VPValue *Def);

>From 4462d832c2ab5310eeb05bb17750f4754e94630f Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 6 Oct 2026 11:58:32 +0100
Subject: [PATCH 8/8] [VPlan] Put usesFirstLaneOnly back into cpp

---
 llvm/lib/Transforms/Vectorize/VPlanUtils.cpp | 23 ++++++++++++++++++++
 llvm/lib/Transforms/Vectorize/VPlanUtils.h   | 23 +-------------------
 2 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 586719c534faa..188e69b20811f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -30,6 +30,29 @@ using namespace llvm;
 using namespace llvm::VPlanPatternMatch;
 using namespace llvm::SCEVPatternMatch;
 
+bool vputils::usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
+                                const VPValue *Def) {
+  SmallSetVector<const VPValue *, 16> Worklist;
+  Worklist.insert(Def);
+  for (unsigned I = 0; I < Worklist.size(); ++I) {
+    const VPValue *WorklistDef = Worklist[I];
+    for (const VPUser *U : WorklistDef == Def ? Users : WorklistDef->users()) {
+      if (Worklist.contains(dyn_cast<VPSingleDefRecipe>(U)))
+        continue;
+      VPRecurseResult Res = U->usesFirstLaneOnly(WorklistDef);
+      if (Res == VPRecurseResult::True)
+        continue;
+      if (Res != VPRecurseResult::FalseRecurse)
+        return false;
+
+      // We rely on the fact that the current implementations of
+      // usesFirstLaneOnly do not require recursion for non-SingleDefs.
+      Worklist.insert(cast<VPSingleDefRecipe>(U));
+    }
+  }
+  return true;
+}
+
 bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return usesFirstLaneOnly(Def->users(), Def);
 }
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 9950a038914c5..2405792730d8f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -29,28 +29,7 @@ namespace llvm {
 namespace vputils {
 /// Returns true if only the first lane of \p Def is used by all of \p Users
 /// recursively.
-inline bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users,
-                              const VPValue *Def) {
-  SmallSetVector<const VPValue *, 16> Worklist;
-  Worklist.insert(Def);
-  for (unsigned I = 0; I < Worklist.size(); ++I) {
-    const VPValue *WorklistDef = Worklist[I];
-    for (const VPUser *U : WorklistDef == Def ? Users : WorklistDef->users()) {
-      if (Worklist.contains(dyn_cast<VPSingleDefRecipe>(U)))
-        continue;
-      VPRecurseResult Res = U->usesFirstLaneOnly(WorklistDef);
-      if (Res == VPRecurseResult::True)
-        continue;
-      if (Res != VPRecurseResult::FalseRecurse)
-        return false;
-
-      // We rely on the fact that the current implementations of
-      // usesFirstLaneOnly do not require recursion for non-SingleDefs.
-      Worklist.insert(cast<VPSingleDefRecipe>(U));
-    }
-  }
-  return true;
-}
+bool usesFirstLaneOnly(ArrayRef<const VPUser *> Users, const VPValue *Def);
 
 /// Returns true if only the first lane of \p Def is used by all its users.
 bool onlyFirstLaneUsed(const VPValue *Def);



More information about the llvm-commits mailing list