diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1458,10 +1458,8 @@ /// uint64_t. The bitwidth must be <= 64 or the value must fit within a /// uint64_t. Otherwise an assertion will result. uint64_t getZExtValue() const { - if (isSingleWord()) { - assert(BitWidth && "zero width values not allowed"); + if (isSingleWord()) return U.VAL; - } assert(getActiveBits() <= 64 && "Too many bits for uint64_t"); return U.pVal[0]; } diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -757,34 +757,38 @@ } /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. -/// Requires 0 < B <= 32. +/// Requires 0 <= B <= 32. Sign extension of a zero-width value returns zero. template constexpr inline int32_t SignExtend32(uint32_t X) { - static_assert(B > 0, "Bit width can't be 0."); static_assert(B <= 32, "Bit width out of range."); + if (!B) + return 0; return int32_t(X << (32 - B)) >> (32 - B); } /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. -/// Requires 0 < B <= 32. +/// Requires 0 <= B <= 32. Sign extension of a zero-width value returns zero. inline int32_t SignExtend32(uint32_t X, unsigned B) { - assert(B > 0 && "Bit width can't be 0."); assert(B <= 32 && "Bit width out of range."); + if (!B) + return 0; return int32_t(X << (32 - B)) >> (32 - B); } /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. -/// Requires 0 < B <= 64. +/// Requires 0 <= B <= 64. Sign extension of a zero-width value returns zero. template constexpr inline int64_t SignExtend64(uint64_t x) { - static_assert(B > 0, "Bit width can't be 0."); static_assert(B <= 64, "Bit width out of range."); + if (!B) + return 0; return int64_t(x << (64 - B)) >> (64 - B); } /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. -/// Requires 0 < B <= 64. +/// Requires 0 <= B <= 64. Sign extension of a zero-width value returns zero. inline int64_t SignExtend64(uint64_t X, unsigned B) { - assert(B > 0 && "Bit width can't be 0."); assert(B <= 64 && "Bit width out of range."); + if (!B) + return 0; return int64_t(X << (64 - B)) >> (64 - B); }