diff --git a/lib/Support/KnownBits.cpp b/lib/Support/KnownBits.cpp index 0f36c6a9ef1..a46a90bb97d 100644 --- a/lib/Support/KnownBits.cpp +++ b/lib/Support/KnownBits.cpp @@ -85,7 +85,11 @@ KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const { unsigned BitWidth = getBitWidth(); - assert(BitWidth >= SrcBitWidth && "Illegal sext-in-register"); + assert(0 < SrcBitWidth && SrcBitWidth <= BitWidth && + "Illegal sext-in-register"); + + if (SrcBitWidth == BitWidth) + return *this; // Sign extension. Compute the demanded bits in the result that are not // present in the input. diff --git a/unittests/Support/KnownBitsTest.cpp b/unittests/Support/KnownBitsTest.cpp index 991096098b8..4e69df49837 100644 --- a/unittests/Support/KnownBitsTest.cpp +++ b/unittests/Support/KnownBitsTest.cpp @@ -427,7 +427,7 @@ TEST(KnownBitsTest, SExtOrTrunc) { TEST(KnownBitsTest, SExtInReg) { unsigned Bits = 4; - for (unsigned FromBits = 1; FromBits != Bits; ++FromBits) { + for (unsigned FromBits = 1; FromBits <= Bits; ++FromBits) { ForeachKnownBits(Bits, [&](const KnownBits &Known) { APInt CommonOne = APInt::getAllOnesValue(Bits); APInt CommonZero = APInt::getAllOnesValue(Bits);