Skip to content

Conversation

@RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented May 22, 2025

Fixes #141098

@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-llvm-adt

@llvm/pr-subscribers-llvm-support

Author: Simon Pilgrim (RKSimon)

Changes

Fixes #141098


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

2 Files Affected:

  • (modified) llvm/lib/Support/APInt.cpp (+2-2)
  • (modified) llvm/unittests/ADT/APIntTest.cpp (+9)
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 4743a039a9eb6..954af7fff92a8 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -349,9 +349,9 @@ void APInt::clearBitsSlowCase(unsigned LoBit, unsigned HiBit) {
     // Create a high mask with ones above HiBit.
     uint64_t HiMask = ~(WORDTYPE_MAX >> (APINT_BITS_PER_WORD - HiShiftAmt));
     // If LoWord and HiWord are equal, then we combine the masks. Otherwise,
-    // set the bits in HiWord.
+    // clear the bits in HiWord.
     if (HiWord == LoWord)
-      LoMask &= HiMask;
+      LoMask |= HiMask;
     else
       U.pVal[HiWord] &= HiMask;
   }
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index a58fbd6deffa5..4741c7bcc140f 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -2556,6 +2556,15 @@ TEST(APIntTest, clearBits) {
   EXPECT_EQ(6u, i256.countl_one());
   EXPECT_EQ(16u, i256.popcount());
 
+  APInt i299 = APInt::getAllOnes(299);
+  i299.clearBits(240, 250);
+  EXPECT_EQ(240u, i299.countr_one());
+  EXPECT_EQ(0u, i299.countr_zero());
+  EXPECT_EQ(299u, i299.getActiveBits());
+  EXPECT_EQ(0u, i299.countl_zero());
+  EXPECT_EQ(49u, i299.countl_one());
+  EXPECT_EQ(289u, i299.popcount());
+
   APInt i311 = APInt::getAllOnes(311);
   i311.clearBits(33, 99);
   EXPECT_EQ(33u, i311.countr_one());

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

@RKSimon RKSimon merged commit 23f0fbf into llvm:main May 22, 2025
14 checks passed
@RKSimon RKSimon deleted the apint-clearbits-141098 branch May 22, 2025 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RISC-V] Miscompile on -O3 with -flto

3 participants