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

via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 5 01:59:20 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Mel Chen (Mel-Chen)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/170826.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp (+2-3) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanValue.h (+12-2) 


``````````diff
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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/170826


More information about the llvm-commits mailing list