Skip to content

Conversation

@artagnon
Copy link
Contributor

@artagnon artagnon commented Jan 30, 2026

Introduce a SignedCompare parameter of isSameValue to use sext instead of zext.

Similar to isSameValue, it checks if two APInts have the same signed
value.
@llvmbot
Copy link
Member

llvmbot commented Jan 30, 2026

@llvm/pr-subscribers-llvm-adt

Author: Ramkumar Ramachandra (artagnon)

Changes

Similar to isSameValue, it checks if two APInts have the same signed value.


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/APInt.h (+12)
  • (modified) llvm/unittests/ADT/APIntTest.cpp (+22-1)
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 9193b5f8994e0..2676cb67005b0 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -561,6 +561,18 @@ class [[nodiscard]] APInt {
     return I1.zext(I2.getBitWidth()) == I2;
   }
 
+  /// Determine if two APInts have the same value, after sign-extending
+  /// one of them (if needed!) to ensure that the bit-widths match.
+  static bool isSameSignedValue(const APInt &I1, const APInt &I2) {
+    if (I1.getBitWidth() == I2.getBitWidth())
+      return I1 == I2;
+
+    if (I1.getBitWidth() > I2.getBitWidth())
+      return I1 == I2.sext(I1.getBitWidth());
+
+    return I1.sext(I2.getBitWidth()) == I2;
+  }
+
   /// Overload to compute a hash_code for an APInt value.
   LLVM_ABI friend hash_code hash_value(const APInt &Arg);
 
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 271d17cb29905..be5db038c904d 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -14,7 +14,6 @@
 #include "llvm/Support/Alignment.h"
 #include "gtest/gtest.h"
 #include <array>
-#include <climits>
 #include <limits>
 #include <optional>
 
@@ -29,6 +28,28 @@ TEST(APIntTest, ValueInit) {
   EXPECT_TRUE(!Zero.sext(64));
 }
 
+TEST(APIntTest, IsSameValueAndIsSameSignedValue) {
+  APInt One8(8, 1, /*isSigned=*/false);
+  APInt Three4(4, 3, /*isSigned=*/false);
+  EXPECT_FALSE(APInt::isSameValue(One8, Three4));
+  EXPECT_FALSE(APInt::isSameSignedValue(One8, Three4));
+
+  APInt Two8(8, 2, /*isSigned=*/false);
+  APInt Two4(4, 2, /*isSigned=*/false);
+  EXPECT_TRUE(APInt::isSameValue(Two8, Two4));
+  EXPECT_TRUE(APInt::isSameSignedValue(Two8, Two4));
+
+  APInt Seven8(8, 7, /*isSigned=*/false);
+  APInt Seven3(3, 7, /*isSigned=*/false);
+  EXPECT_TRUE(APInt::isSameValue(Seven8, Seven3));
+  EXPECT_FALSE(APInt::isSameSignedValue(Seven8, Seven3));
+
+  APInt Ones8 = APInt::getAllOnes(8);
+  APInt Ones4 = APInt::getAllOnes(4);
+  EXPECT_FALSE(APInt::isSameValue(Ones8, Ones4));
+  EXPECT_TRUE(APInt::isSameSignedValue(Ones8, Ones4));
+}
+
 // Test that 0^5 == 0
 TEST(APIntTest, PowZeroTo5) {
   APInt Zero = APInt::getZero(32);

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where will it be used?

@artagnon
Copy link
Contributor Author

Where will it be used?

Here: #172372

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered adding an extra flag to isSameValue, say signedCompare = false, so that we don't introduce too many functions? The implementation is identical in both cases modulo the extension used.

@artagnon artagnon changed the title [APInt] Introduce isSameSignedValue [APInt] Extend isSameValue to also do signed-compares Jan 30, 2026
Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@artagnon artagnon merged commit a34bfac into llvm:main Feb 1, 2026
10 checks passed
@artagnon artagnon deleted the apint-samesigned branch February 1, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants