Skip to content
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

[APInt] Fix getAllOnes() with zero width #112227

Merged
merged 1 commit into from
Oct 15, 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
21 changes: 14 additions & 7 deletions llvm/include/llvm/ADT/APInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,21 @@ class [[nodiscard]] APInt {
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");
if (isSigned) {
if (BitWidth == 0) {
assert((val == 0 || val == uint64_t(-1)) &&
"Value must be 0 or -1 for signed 0-bit APInt");
} else {
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 (BitWidth == 0) {
assert(val == 0 && "Value must be zero for unsigned 0-bit APInt");
} else {
assert(llvm::isUIntN(BitWidth, val) &&
"Value is not an N-bit unsigned value");
}
}
}
if (isSingleWord()) {
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/ADT/APIntTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3423,6 +3423,7 @@ TEST(APIntTest, ZeroWidth) {
EXPECT_EQ(0U, ZW.getBitWidth());
EXPECT_EQ(0U, APInt(0, ArrayRef<uint64_t>({0, 1, 2})).getBitWidth());
EXPECT_EQ(0U, APInt(0, "0", 10).getBitWidth());
EXPECT_EQ(0U, APInt::getAllOnes(0).getBitWidth());

// Default constructor is single bit wide.
EXPECT_EQ(1U, APInt().getBitWidth());
Expand Down
Loading