[llvm] [VPlanValue] Introduce hasOneUser(). nfc (PR #170826)

Mel Chen via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 5 01:58:44 PST 2025


https://github.com/Mel-Chen created https://github.com/llvm/llvm-project/pull/170826

SmallVector Users may contain duplicate users, so relying on hasOneUse() does not guarantee a single unique user. This patch introduces hasOneUser() to check for exactly only one user, and updates getSingleUser() and VPWidenCastRecipe::computeCost() to use it.

>From d76bed623e596a746792ecc7b3b266d20585db4f Mon Sep 17 00:00:00 2001
From: Mel Chen <mel.chen at sifive.com>
Date: Fri, 5 Dec 2025 01:04:36 -0800
Subject: [PATCH] Intro hasOneUser

---
 llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp |  5 ++---
 llvm/lib/Transforms/Vectorize/VPlanValue.h     | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 1b1308c78c76e..17adcafd18044 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -2259,9 +2259,8 @@ InstructionCost VPWidenCastRecipe::computeCost(ElementCount VF,
   VPValue *Operand = getOperand(0);
   TTI::CastContextHint CCH = TTI::CastContextHint::None;
   // For Trunc/FPTrunc, get the context from the only user.
-  if ((Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) &&
-      !hasMoreThanOneUniqueUser() && getNumUsers() > 0) {
-    if (auto *StoreRecipe = dyn_cast<VPRecipeBase>(*user_begin()))
+  if (Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) {
+    if (auto *StoreRecipe = dyn_cast_or_null<VPRecipeBase>(getSingleUser()))
       CCH = ComputeCCH(StoreRecipe);
   }
   // For Z/Sext, get the context from the operand.
diff --git a/llvm/lib/Transforms/Vectorize/VPlanValue.h b/llvm/lib/Transforms/Vectorize/VPlanValue.h
index b9f5847ec731c..db48fe5ed73bf 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanValue.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanValue.h
@@ -150,11 +150,21 @@ class LLVM_ABI_FOR_TEST VPValue {
 
   bool hasOneUse() const { return getNumUsers() == 1; }
 
+  /// Returns true if the value has exactly one unique user, ignoring multiple
+  /// uses by the same user.
+  bool hasOneUser() const {
+    if (getNumUsers() == 0)
+      return false;
+    if (hasOneUse())
+      return true;
+    return std::equal(std::next(user_begin()), user_end(), user_begin());
+  }
+
   /// Return the single user of this value, or nullptr if there is not exactly
   /// one user.
-  VPUser *getSingleUser() { return hasOneUse() ? *user_begin() : nullptr; }
+  VPUser *getSingleUser() { return hasOneUser() ? *user_begin() : nullptr; }
   const VPUser *getSingleUser() const {
-    return hasOneUse() ? *user_begin() : nullptr;
+    return hasOneUser() ? *user_begin() : nullptr;
   }
 
   void replaceAllUsesWith(VPValue *New);



More information about the llvm-commits mailing list