[llvm] Removed redundant assert and condition in APInt::SetBits (PR #138038)

Liam Semeria via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 21:18:35 PDT 2025


https://github.com/liamsemeria updated https://github.com/llvm/llvm-project/pull/138038

>From b1749f17eaf530281ef0ac1180744ece2a3bbf33 Mon Sep 17 00:00:00 2001
From: Liam <liamsemeria at gmail.com>
Date: Wed, 30 Apr 2025 14:42:11 -0700
Subject: [PATCH 1/4] removed redundant assert and condition in APInt::SetBits

---
 llvm/include/llvm/ADT/APInt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 02d58d8c3d31c..4a1205cedf605 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1366,11 +1366,11 @@ class [[nodiscard]] APInt {
   /// This function handles case when \p loBit <= \p hiBit.
   void setBits(unsigned loBit, unsigned hiBit) {
     assert(hiBit <= BitWidth && "hiBit out of range");
-    assert(loBit <= BitWidth && "loBit out of range");
     assert(loBit <= hiBit && "loBit greater than hiBit");
     if (loBit == hiBit)
       return;
     if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
+    //if (hiBit <= APINT_BITS_PER_WORD) {
       uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
       mask <<= loBit;
       if (isSingleWord())

>From 85ccb4e96802027f1841bacda58c8d596af48c34 Mon Sep 17 00:00:00 2001
From: Liam <liamsemeria at gmail.com>
Date: Thu, 1 May 2025 14:02:45 -0700
Subject: [PATCH 2/4] reinserted assert for lowBits in APInt::SetBits

---
 llvm/include/llvm/ADT/APInt.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 4a1205cedf605..c682887fdd182 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1366,6 +1366,7 @@ class [[nodiscard]] APInt {
   /// This function handles case when \p loBit <= \p hiBit.
   void setBits(unsigned loBit, unsigned hiBit) {
     assert(hiBit <= BitWidth && "hiBit out of range");
+    assert(loBit <= BitWidth && "loBit out of range");
     assert(loBit <= hiBit && "loBit greater than hiBit");
     if (loBit == hiBit)
       return;

>From e9097c78a42803bd19759976df17946f45d5a685 Mon Sep 17 00:00:00 2001
From: Liam <liamsemeria at gmail.com>
Date: Fri, 2 May 2025 15:13:56 -0700
Subject: [PATCH 3/4] removed redundant check for lowBits in APInt::SetBits

---
 llvm/include/llvm/ADT/APInt.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index c682887fdd182..ebd474bba55ba 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1370,8 +1370,7 @@ class [[nodiscard]] APInt {
     assert(loBit <= hiBit && "loBit greater than hiBit");
     if (loBit == hiBit)
       return;
-    if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
-    //if (hiBit <= APINT_BITS_PER_WORD) {
+    if (hiBit <= APINT_BITS_PER_WORD) {
       uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
       mask <<= loBit;
       if (isSingleWord())

>From b36a066442be58e3006e4ac597673afebe4e94a7 Mon Sep 17 00:00:00 2001
From: Liam <liamsemeria at gmail.com>
Date: Tue, 6 May 2025 21:18:20 -0700
Subject: [PATCH 4/4] removed unneeded lowBits assert in APInt::SetBits

---
 llvm/include/llvm/ADT/APInt.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index ebd474bba55ba..aeb17a846f3c1 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1366,7 +1366,6 @@ class [[nodiscard]] APInt {
   /// This function handles case when \p loBit <= \p hiBit.
   void setBits(unsigned loBit, unsigned hiBit) {
     assert(hiBit <= BitWidth && "hiBit out of range");
-    assert(loBit <= BitWidth && "loBit out of range");
     assert(loBit <= hiBit && "loBit greater than hiBit");
     if (loBit == hiBit)
       return;



More information about the llvm-commits mailing list