[llvm] b615ebd - [APInt] Extend APIntOps::gcd to take IsSigned argument (#217269)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 23 22:38:47 PDT 2026


Author: Ramkumar Ramachandra
Date: 2026-08-24T06:38:42+01:00
New Revision: b615ebdd35cc1c13d722c81ecf52af990ad29c6a

URL: https://github.com/llvm/llvm-project/commit/b615ebdd35cc1c13d722c81ecf52af990ad29c6a
DIFF: https://github.com/llvm/llvm-project/commit/b615ebdd35cc1c13d722c81ecf52af990ad29c6a.diff

LOG: [APInt] Extend APIntOps::gcd to take IsSigned argument (#217269)

This clarifies the semantics of APIntOps::GreatestCommonDivisor for
signed arguments.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/APInt.h
    llvm/lib/Support/APInt.cpp
    llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/unittests/ADT/APIntTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index c3a18de7d89ba..454025b024ae6 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2331,13 +2331,15 @@ LLVM_ABI APInt muluExtended(const APInt &C1, const APInt &C2);
 /// 0^0 is supported and returns 1.
 LLVM_ABI APInt pow(const APInt &X, int64_t N);
 
-/// Compute GCD of two unsigned APInt values.
+/// Compute GCD of two APInt values.
 ///
 /// This function returns the greatest common divisor of the two APInt values
 /// using Stein's algorithm.
 ///
-/// \returns the greatest common divisor of A and B.
-LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B);
+/// \returns the greatest common divisor of A and B. If \p Signed is true, it
+/// takes the absolute value of the both arguments, and returns the unsigned
+/// greatest common divisor.
+LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B, bool IsSigned = false);
 
 /// Converts the given APInt to a double value.
 ///

diff  --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 943b3b6777b05..d9413415b348b 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -822,7 +822,13 @@ APInt APInt::reverseBits() const {
   return Result;
 }
 
-APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B) {
+APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B, bool IsSigned) {
+  // Take absolute value if IsSigned.
+  if (IsSigned) {
+    A = A.abs();
+    B = B.abs();
+  }
+
   // Fast-path a common case.
   if (A == B) return A;
 

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 4e1aa36230550..128e3e3dcdfe8 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1405,9 +1405,7 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
 
       // (X * C1) / C2 -> (X * (C1/D)) / (C2/D) if D = gcd(C1, C2) > 1.
       if (Op0->hasOneUse()) {
-        APInt GCD = IsSigned
-                        ? APIntOps::GreatestCommonDivisor(C1->abs(), C2->abs())
-                        : APIntOps::GreatestCommonDivisor(*C1, *C2);
+        APInt GCD = APIntOps::GreatestCommonDivisor(*C1, *C2, IsSigned);
         if (GCD.ugt(1)) {
           APInt NewC1 = IsSigned ? C1->sdiv(GCD) : C1->udiv(GCD);
           APInt NewC2 = IsSigned ? C2->sdiv(GCD) : C2->udiv(GCD);

diff  --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 9a0ff612c4985..cd728e089719a 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -2889,18 +2889,25 @@ TEST(APIntTest, GCD) {
 
   for (unsigned Bits : {1, 2, 32, 63, 64, 65}) {
     // Test some corner cases near zero.
-    APInt Zero(Bits, 0), One(Bits, 1);
+    APInt Zero(Bits, 0), One(Bits, 1), MinusOne(Bits, -1, true);
     EXPECT_EQ(GreatestCommonDivisor(Zero, Zero), Zero);
     EXPECT_EQ(GreatestCommonDivisor(Zero, One), One);
     EXPECT_EQ(GreatestCommonDivisor(One, Zero), One);
     EXPECT_EQ(GreatestCommonDivisor(One, One), One);
 
     if (Bits > 1) {
-      APInt Two(Bits, 2);
+      APInt Two(Bits, 2), MinusTwo(Bits, -2, true);
       EXPECT_EQ(GreatestCommonDivisor(Zero, Two), Two);
       EXPECT_EQ(GreatestCommonDivisor(One, Two), One);
       EXPECT_EQ(GreatestCommonDivisor(Two, Two), Two);
 
+      EXPECT_EQ(GreatestCommonDivisor(Zero, MinusTwo, /*IsSigned=*/true), Two);
+      EXPECT_EQ(GreatestCommonDivisor(MinusOne, MinusTwo, /*IsSigned=*/true),
+                One);
+      EXPECT_EQ(GreatestCommonDivisor(One, MinusTwo, /*IsSigned=*/true), One);
+      EXPECT_EQ(GreatestCommonDivisor(MinusTwo, MinusTwo, /*IsSigned=*/true),
+                Two);
+
       // Test some corner cases near the highest representable value.
       APInt Max(Bits, 0);
       Max.setAllBits();
@@ -2909,6 +2916,20 @@ TEST(APIntTest, GCD) {
       EXPECT_EQ(GreatestCommonDivisor(Two, Max), One);
       EXPECT_EQ(GreatestCommonDivisor(Max, Max), Max);
 
+      EXPECT_EQ(GreatestCommonDivisor(Zero, Max, /*IsSigned=*/true), One);
+      EXPECT_EQ(GreatestCommonDivisor(MinusOne, Max, /*IsSigned=*/true), One);
+      EXPECT_EQ(GreatestCommonDivisor(MinusTwo, Max, /*IsSigned=*/true), One);
+      EXPECT_EQ(GreatestCommonDivisor(Max, Max, /*IsSigned=*/true), One);
+
+      // Test some corner cases near the minimum signed value.
+      APInt SMin = APInt::getSignedMinValue(Bits);
+      EXPECT_EQ(GreatestCommonDivisor(Zero, SMin, /*IsSigned=*/true), SMin);
+      EXPECT_EQ(GreatestCommonDivisor(MinusOne, SMin, /*IsSigned=*/true), One);
+      EXPECT_EQ(GreatestCommonDivisor(MinusTwo, SMin, /*IsSigned=*/true), Two);
+      EXPECT_EQ(GreatestCommonDivisor(One, SMin, /*IsSigned=*/true), One);
+      EXPECT_EQ(GreatestCommonDivisor(Two, SMin, /*IsSigned=*/true), Two);
+      EXPECT_EQ(GreatestCommonDivisor(SMin, SMin, /*IsSigned=*/true), SMin);
+
       APInt MaxOver2 = Max.udiv(Two);
       EXPECT_EQ(GreatestCommonDivisor(MaxOver2, Max), One);
       // Max - 1 == Max / 2 * 2, because Max is odd.
@@ -2923,8 +2944,12 @@ TEST(APIntTest, GCD) {
   // 9931 and 123456 are coprime.
   APInt A = HugePrime * APInt(BitWidth, 9931);
   APInt B = HugePrime * APInt(BitWidth, 123456);
+  APInt SA = HugePrime * APInt(BitWidth, -9931, true);
+  APInt SB = HugePrime * APInt(BitWidth, -123456, true);
   APInt C = GreatestCommonDivisor(A, B);
   EXPECT_EQ(C, HugePrime);
+  APInt SC = GreatestCommonDivisor(SA, SB, /*IsSigned=*/true);
+  EXPECT_EQ(SC, HugePrime);
 }
 
 TEST(APIntTest, LogicalRightShift) {


        


More information about the llvm-commits mailing list