[llvm] IR: Remove null UseList checks in hasNUses methods (PR #165929)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 3 18:52:47 PST 2025


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/165929

>From ffa6cacbb0bc81941354676ac6870994c55dd098 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Fri, 31 Oct 2025 14:59:23 -0700
Subject: [PATCH 1/2] IR: Remove null UseList checks in hasNUses methods

There do not appear to be any cases where this is used.
This does introduce an odd asyemmtry where use_empty is not
equivalent to hasNUses(0).
---
 llvm/lib/IR/Value.cpp               |  8 -------
 llvm/unittests/IR/ConstantsTest.cpp | 36 +++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index b775cbb0c7920..95d61a987f6c1 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -148,18 +148,10 @@ void Value::destroyValueName() {
 }
 
 bool Value::hasNUses(unsigned N) const {
-  if (!UseList)
-    return N == 0;
-
-  // TODO: Disallow for ConstantData and remove !UseList check?
   return hasNItems(use_begin(), use_end(), N);
 }
 
 bool Value::hasNUsesOrMore(unsigned N) const {
-  // TODO: Disallow for ConstantData and remove !UseList check?
-  if (!UseList)
-    return N == 0;
-
   return hasNItemsOrMore(use_begin(), use_end(), N);
 }
 
diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index 6376165cbe766..d86e3d555c320 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -29,13 +29,8 @@ TEST(ConstantsTest, UseCounts) {
 
   EXPECT_TRUE(Zero->use_empty());
   EXPECT_EQ(Zero->getNumUses(), 0u);
-  EXPECT_TRUE(Zero->hasNUses(0));
   EXPECT_FALSE(Zero->hasOneUse());
   EXPECT_FALSE(Zero->hasOneUser());
-  EXPECT_FALSE(Zero->hasNUses(1));
-  EXPECT_FALSE(Zero->hasNUsesOrMore(1));
-  EXPECT_FALSE(Zero->hasNUses(2));
-  EXPECT_FALSE(Zero->hasNUsesOrMore(2));
 
   std::unique_ptr<Module> M(new Module("MyModule", Context));
 
@@ -50,15 +45,36 @@ TEST(ConstantsTest, UseCounts) {
   // Still looks like use_empty with uses.
   EXPECT_TRUE(Zero->use_empty());
   EXPECT_EQ(Zero->getNumUses(), 0u);
-  EXPECT_TRUE(Zero->hasNUses(0));
   EXPECT_FALSE(Zero->hasOneUse());
   EXPECT_FALSE(Zero->hasOneUser());
-  EXPECT_FALSE(Zero->hasNUses(1));
-  EXPECT_FALSE(Zero->hasNUsesOrMore(1));
-  EXPECT_FALSE(Zero->hasNUses(2));
-  EXPECT_FALSE(Zero->hasNUsesOrMore(2));
 }
 
+#ifdef GTEST_HAS_DEATH_TEST
+#ifndef NDEBUG
+
+TEST(ConstantsTest, hasNUsesInvalid) {
+  LLVMContext Context;
+  Type *Int32Ty = Type::getInt32Ty(Context);
+  Constant *Zero = ConstantInt::get(Int32Ty, 0);
+  std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+  // Introduce some uses
+  new GlobalVariable(*M, Int32Ty, /*isConstant=*/false,
+                     GlobalValue::ExternalLinkage, /*Initializer=*/Zero,
+                     "gv_user0");
+  new GlobalVariable(*M, Int32Ty, /*isConstant=*/false,
+                     GlobalValue::ExternalLinkage, /*Initializer=*/Zero,
+                     "gv_user1");
+
+  for (int I = 0; I != 3; ++I) {
+    EXPECT_DEATH(Zero->hasNUses(I), "hasUseList()");
+    EXPECT_DEATH(Zero->hasNUsesOrMore(I), "hasUseList()");
+  }
+}
+
+#endif
+#endif
+
 TEST(ConstantsTest, Integer_i1) {
   LLVMContext Context;
   IntegerType *Int1 = IntegerType::get(Context, 1);

>From 96e04afb2d50ca34462286b9d6474f44bc4da627 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 3 Nov 2025 18:52:39 -0800
Subject: [PATCH 2/2] Update llvm/unittests/IR/ConstantsTest.cpp

Co-authored-by: Nikita Popov <npopov at redhat.com>
---
 llvm/unittests/IR/ConstantsTest.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index d86e3d555c320..3d653e3ccd354 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -67,8 +67,8 @@ TEST(ConstantsTest, hasNUsesInvalid) {
                      "gv_user1");
 
   for (int I = 0; I != 3; ++I) {
-    EXPECT_DEATH(Zero->hasNUses(I), "hasUseList()");
-    EXPECT_DEATH(Zero->hasNUsesOrMore(I), "hasUseList()");
+    EXPECT_DEATH(Zero->hasNUses(I), "hasUseList\(\)");
+    EXPECT_DEATH(Zero->hasNUsesOrMore(I), "hasUseList\(\)");
   }
 }
 



More information about the llvm-commits mailing list