Skip to content

Commit c180da9

Browse files
authored
[APInt] Fix getAllOnes() with zero width (#112227)
This makes sure that APInt::getAllOnes() keeps working after the APInt constructor assertions are enabled. I'm relaxing the requirement for the signed case to either an all zeros or all ones integer. This is basically saying that we can interpret the zero-width integer as either positive or negative.
1 parent 708b154 commit c180da9

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

llvm/include/llvm/ADT/APInt.h

+14-7
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,21 @@ class [[nodiscard]] APInt {
112112
bool implicitTrunc = true)
113113
: BitWidth(numBits) {
114114
if (!implicitTrunc) {
115-
if (BitWidth == 0) {
116-
assert(val == 0 && "Value must be zero for 0-bit APInt");
117-
} else if (isSigned) {
118-
assert(llvm::isIntN(BitWidth, val) &&
119-
"Value is not an N-bit signed value");
115+
if (isSigned) {
116+
if (BitWidth == 0) {
117+
assert((val == 0 || val == uint64_t(-1)) &&
118+
"Value must be 0 or -1 for signed 0-bit APInt");
119+
} else {
120+
assert(llvm::isIntN(BitWidth, val) &&
121+
"Value is not an N-bit signed value");
122+
}
120123
} else {
121-
assert(llvm::isUIntN(BitWidth, val) &&
122-
"Value is not an N-bit unsigned value");
124+
if (BitWidth == 0) {
125+
assert(val == 0 && "Value must be zero for unsigned 0-bit APInt");
126+
} else {
127+
assert(llvm::isUIntN(BitWidth, val) &&
128+
"Value is not an N-bit unsigned value");
129+
}
123130
}
124131
}
125132
if (isSingleWord()) {

llvm/unittests/ADT/APIntTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,7 @@ TEST(APIntTest, ZeroWidth) {
34233423
EXPECT_EQ(0U, ZW.getBitWidth());
34243424
EXPECT_EQ(0U, APInt(0, ArrayRef<uint64_t>({0, 1, 2})).getBitWidth());
34253425
EXPECT_EQ(0U, APInt(0, "0", 10).getBitWidth());
3426+
EXPECT_EQ(0U, APInt::getAllOnes(0).getBitWidth());
34263427

34273428
// Default constructor is single bit wide.
34283429
EXPECT_EQ(1U, APInt().getBitWidth());

0 commit comments

Comments
 (0)