Index: llvm/include/llvm/ADT/APInt.h =================================================================== --- llvm/include/llvm/ADT/APInt.h +++ llvm/include/llvm/ADT/APInt.h @@ -1417,9 +1417,13 @@ 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 > hiBit) { + setLowBits(hiBit); + setHighBits(BitWidth - loBit); + return; + } if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); mask <<= loBit; Index: llvm/unittests/ADT/APIntTest.cpp =================================================================== --- llvm/unittests/ADT/APIntTest.cpp +++ llvm/unittests/ADT/APIntTest.cpp @@ -1907,6 +1907,22 @@ EXPECT_EQ(1u, i127hi1lo1.countTrailingZeros()); EXPECT_EQ(0u, i127hi1lo1.countTrailingOnes()); EXPECT_EQ(125u, i127hi1lo1.countPopulation()); + + APInt i64hi1lo1wrap = APInt::getBitsSet(64, 63, 1); + EXPECT_EQ(1u, i64hi1lo1wrap.countLeadingOnes()); + EXPECT_EQ(0u, i64hi1lo1wrap.countLeadingZeros()); + EXPECT_EQ(64u, i64hi1lo1wrap.getActiveBits()); + EXPECT_EQ(0u, i64hi1lo1wrap.countTrailingZeros()); + EXPECT_EQ(1u, i64hi1lo1wrap.countTrailingOnes()); + EXPECT_EQ(2u, i64hi1lo1wrap.countPopulation()); + + APInt i127hi1lo1wrap = APInt::getBitsSet(127, 126, 1); + EXPECT_EQ(1u, i127hi1lo1wrap.countLeadingOnes()); + EXPECT_EQ(0u, i127hi1lo1wrap.countLeadingZeros()); + EXPECT_EQ(127u, i127hi1lo1wrap.getActiveBits()); + EXPECT_EQ(0u, i127hi1lo1wrap.countTrailingZeros()); + EXPECT_EQ(1u, i127hi1lo1wrap.countTrailingOnes()); + EXPECT_EQ(2u, i127hi1lo1wrap.countPopulation()); } TEST(APIntTest, getHighBitsSet) {