[llvm] [Support][APint] Add fast path for APInt::urem if RHS is power of 2 (PR #189245)

Max Graey via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 29 06:59:14 PDT 2026


https://github.com/MaxGraey updated https://github.com/llvm/llvm-project/pull/189245

>From a1cb047f0eec2681595fc7ba25e10b9bbc8e54ca Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 29 Mar 2026 16:23:58 +0300
Subject: [PATCH 1/4] add fast path for APInt::urem if RHS is power of 2

---
 llvm/lib/Support/APInt.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 0a9cda5299fc8..55c7e4f004132 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -1705,6 +1705,9 @@ APInt APInt::urem(const APInt &RHS) const {
   if (lhsWords == 1)
     // All high words are zero, just use native remainder
     return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]);
+  if (RHS.isPowerOf2())
+    // X % 2^w ===> X & (2^w - 1)
+    return trunc(RHS.logBase2());
 
   // We have to compute it the hard way. Invoke the Knuth divide algorithm.
   APInt Remainder(BitWidth, 0);
@@ -1737,6 +1740,9 @@ uint64_t APInt::urem(uint64_t RHS) const {
   if (lhsWords == 1)
     // All high words are zero, just use native remainder
     return U.pVal[0] % RHS;
+  if (llvm::isPowerOf2_64(RHS))
+    // X % 2^w ===> X & (2^w - 1)
+    return getZExtValue() & (RHS - 1);
 
   // We have to compute it the hard way. Invoke the Knuth divide algorithm.
   uint64_t Remainder;

>From e2f2f088c1571e44f84d1ee1065c16e99407cbe1 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 29 Mar 2026 16:49:37 +0300
Subject: [PATCH 2/4] add fix & tests

---
 llvm/lib/Support/APInt.cpp       |  2 +-
 llvm/unittests/ADT/APIntTest.cpp | 22 +++++++++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 55c7e4f004132..a5ca06065cf5d 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -1707,7 +1707,7 @@ APInt APInt::urem(const APInt &RHS) const {
     return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]);
   if (RHS.isPowerOf2())
     // X % 2^w ===> X & (2^w - 1)
-    return trunc(RHS.logBase2());
+    return *this & (RHS - 1);
 
   // We have to compute it the hard way. Invoke the Knuth divide algorithm.
   APInt Remainder(BitWidth, 0);
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 9bd283953f733..07e18582b0e4a 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1069,7 +1069,7 @@ TEST(APIntTest, divrem_big3) {
 }
 
 TEST(APIntTest, divrem_big4) {
-  // Tests heap allocation in divide() enfoced by huge numbers
+  // Tests heap allocation in divide() enforced by huge numbers
   testDiv(APInt{4096, 5}.shl(2001),
           APInt{4096, 1}.shl(2000),
           APInt{4096, 4219*13});
@@ -1096,6 +1096,16 @@ TEST(APIntTest, divrem_big7) {
           {224, "80000000800000010000000f", 16});
 }
 
+TEST(APIntTest, divrem_big_pow2) {
+  // Tests fast path for power-of-two divisor.
+  APInt LHS{512, "ffffffffffffffffffffffffffffffffffffffffffffffff", 16};
+  for (unsigned k : {1, 63, 64, 65, 127, 128, 200, 255, 256}) {
+    APInt RHS = APInt{512, 1}.shl(k);
+    APInt RES = LHS.trunc(k);
+    testDiv(LHS, RHS, RES);
+  }
+}
+
 void testDiv(APInt a, uint64_t b, APInt c) {
   auto p = a * b + c;
 
@@ -1154,6 +1164,16 @@ TEST(APIntTest, divremuint) {
           APInt{1024, 1});
 }
 
+TEST(APIntTest, divremuint_pow2) {
+  // Tests fast path for power-of-two uint64_t divisor.
+  APInt LHS{512, "ffffffffffffffffffffffffffffffffffffffffffffffff", 16};
+  for (unsigned k : {1, 2, 3, 7, 15, 31, 32, 33, 63}) {
+    uint64_t RHS = 1ULL << k;
+    APInt RES = LHS.trunc(k);
+    testDiv(LHS, RHS, RES);
+  }
+}
+
 TEST(APIntTest, divrem_simple) {
   // Test simple cases.
   APInt A(65, 2), B(65, 2);

>From 00eca72671ec4bd91171e3453ad249f26b76f0af Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 29 Mar 2026 16:53:58 +0300
Subject: [PATCH 3/4] fix unit tests

---
 llvm/unittests/ADT/APIntTest.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 07e18582b0e4a..508338a19d543 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1101,7 +1101,7 @@ TEST(APIntTest, divrem_big_pow2) {
   APInt LHS{512, "ffffffffffffffffffffffffffffffffffffffffffffffff", 16};
   for (unsigned k : {1, 63, 64, 65, 127, 128, 200, 255, 256}) {
     APInt RHS = APInt{512, 1}.shl(k);
-    APInt RES = LHS.trunc(k);
+    APInt RES = LHS & (RHS - 1);
     testDiv(LHS, RHS, RES);
   }
 }
@@ -1169,7 +1169,7 @@ TEST(APIntTest, divremuint_pow2) {
   APInt LHS{512, "ffffffffffffffffffffffffffffffffffffffffffffffff", 16};
   for (unsigned k : {1, 2, 3, 7, 15, 31, 32, 33, 63}) {
     uint64_t RHS = 1ULL << k;
-    APInt RES = LHS.trunc(k);
+    APInt RES = LHS.urem(APInt{512, RHS});
     testDiv(LHS, RHS, RES);
   }
 }

>From 17a4c721c15a0c50781b8b445bffccaf37d45c6c Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 29 Mar 2026 16:58:44 +0300
Subject: [PATCH 4/4] remove tests

---
 llvm/unittests/ADT/APIntTest.cpp | 20 --------------------
 1 file changed, 20 deletions(-)

diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 508338a19d543..34efc4ff8ccf5 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1096,16 +1096,6 @@ TEST(APIntTest, divrem_big7) {
           {224, "80000000800000010000000f", 16});
 }
 
-TEST(APIntTest, divrem_big_pow2) {
-  // Tests fast path for power-of-two divisor.
-  APInt LHS{512, "ffffffffffffffffffffffffffffffffffffffffffffffff", 16};
-  for (unsigned k : {1, 63, 64, 65, 127, 128, 200, 255, 256}) {
-    APInt RHS = APInt{512, 1}.shl(k);
-    APInt RES = LHS & (RHS - 1);
-    testDiv(LHS, RHS, RES);
-  }
-}
-
 void testDiv(APInt a, uint64_t b, APInt c) {
   auto p = a * b + c;
 
@@ -1164,16 +1154,6 @@ TEST(APIntTest, divremuint) {
           APInt{1024, 1});
 }
 
-TEST(APIntTest, divremuint_pow2) {
-  // Tests fast path for power-of-two uint64_t divisor.
-  APInt LHS{512, "ffffffffffffffffffffffffffffffffffffffffffffffff", 16};
-  for (unsigned k : {1, 2, 3, 7, 15, 31, 32, 33, 63}) {
-    uint64_t RHS = 1ULL << k;
-    APInt RES = LHS.urem(APInt{512, RHS});
-    testDiv(LHS, RHS, RES);
-  }
-}
-
 TEST(APIntTest, divrem_simple) {
   // Test simple cases.
   APInt A(65, 2), B(65, 2);



More information about the llvm-commits mailing list