Skip to content

[APInt] Add default-disabled assertion to APInt constructor #106524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions llvm/include/llvm/ADT/APInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,26 @@ class [[nodiscard]] APInt {
/// \param numBits the bit width of the constructed APInt
/// \param val the initial value of the APInt
/// \param isSigned how to treat signedness of val
APInt(unsigned numBits, uint64_t val, bool isSigned = false)
/// \param implicitTrunc allow implicit truncation of non-zero/sign bits of
/// val beyond the range of numBits
APInt(unsigned numBits, uint64_t val, bool isSigned = false,
bool implicitTrunc = true)
: BitWidth(numBits) {
if (!implicitTrunc) {
if (BitWidth == 0) {
assert(val == 0 && "Value must be zero for 0-bit APInt");
} else if (isSigned) {
assert(llvm::isIntN(BitWidth, val) &&
"Value is not an N-bit signed value");
} else {
assert(llvm::isUIntN(BitWidth, val) &&
"Value is not an N-bit unsigned value");
}
}
if (isSingleWord()) {
U.VAL = val;
clearUnusedBits();
if (implicitTrunc || isSigned)
clearUnusedBits();
} else {
initSlowCase(val, isSigned);
}
Expand Down
14 changes: 9 additions & 5 deletions llvm/lib/Support/APInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ APInt& APInt::operator-=(uint64_t RHS) {
APInt APInt::operator*(const APInt& RHS) const {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
if (isSingleWord())
return APInt(BitWidth, U.VAL * RHS.U.VAL);
return APInt(BitWidth, U.VAL * RHS.U.VAL, /*isSigned=*/false,
/*implicitTrunc=*/true);

APInt Result(getMemory(getNumWords()), getBitWidth());
tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords());
Expand Down Expand Up @@ -455,15 +456,17 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const {
"Illegal bit extraction");

if (isSingleWord())
return APInt(numBits, U.VAL >> bitPosition);
return APInt(numBits, U.VAL >> bitPosition, /*isSigned=*/false,
/*implicitTrunc=*/true);

unsigned loBit = whichBit(bitPosition);
unsigned loWord = whichWord(bitPosition);
unsigned hiWord = whichWord(bitPosition + numBits - 1);

// Single word result extracting bits from a single word source.
if (loWord == hiWord)
return APInt(numBits, U.pVal[loWord] >> loBit);
return APInt(numBits, U.pVal[loWord] >> loBit, /*isSigned=*/false,
/*implicitTrunc=*/true);

// Extracting bits that start on a source word boundary can be done
// as a fast memory copy.
Expand Down Expand Up @@ -907,7 +910,8 @@ APInt APInt::trunc(unsigned width) const {
assert(width <= BitWidth && "Invalid APInt Truncate request");

if (width <= APINT_BITS_PER_WORD)
return APInt(width, getRawData()[0]);
return APInt(width, getRawData()[0], /*isSigned=*/false,
/*implicitTrunc=*/true);

if (width == BitWidth)
return *this;
Expand Down Expand Up @@ -955,7 +959,7 @@ APInt APInt::sext(unsigned Width) const {
assert(Width >= BitWidth && "Invalid APInt SignExtend request");

if (Width <= APINT_BITS_PER_WORD)
return APInt(Width, SignExtend64(U.VAL, BitWidth));
return APInt(Width, SignExtend64(U.VAL, BitWidth), /*isSigned=*/true);

if (Width == BitWidth)
return *this;
Expand Down
5 changes: 3 additions & 2 deletions llvm/unittests/ADT/APIntTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ TEST(APIntTest, i256) {
}

TEST(APIntTest, i1) {
const APInt neg_two(1, static_cast<uint64_t>(-2), true);
const APInt neg_two(1, static_cast<uint64_t>(-2), true,
/*implicitTrunc=*/true);
const APInt neg_one(1, static_cast<uint64_t>(-1), true);
const APInt zero(1, 0);
const APInt one(1, 1);
const APInt two(1, 2);
const APInt two(1, 2, false, /*implicitTrunc=*/true);

EXPECT_EQ(0, neg_two.getSExtValue());
EXPECT_EQ(-1, neg_one.getSExtValue());
Expand Down
Loading