Skip to content

Commit c97aade

Browse files
nikicakiramenai
authored andcommitted
[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 d8aae40 commit c97aade

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

llvm/include/llvm/ADT/APInt.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,21 @@ class [[nodiscard]] APInt {
114114
bool implicitTrunc = true)
115115
: BitWidth(numBits) {
116116
if (!implicitTrunc) {
117-
if (BitWidth == 0) {
118-
assert(val == 0 && "Value must be zero for 0-bit APInt");
119-
} else if (isSigned) {
120-
assert(llvm::isIntN(BitWidth, val) &&
121-
"Value is not an N-bit signed value");
117+
if (isSigned) {
118+
if (BitWidth == 0) {
119+
assert((val == 0 || val == uint64_t(-1)) &&
120+
"Value must be 0 or -1 for signed 0-bit APInt");
121+
} else {
122+
assert(llvm::isIntN(BitWidth, val) &&
123+
"Value is not an N-bit signed value");
124+
}
122125
} else {
123-
assert(llvm::isUIntN(BitWidth, val) &&
124-
"Value is not an N-bit unsigned value");
126+
if (BitWidth == 0) {
127+
assert(val == 0 && "Value must be zero for unsigned 0-bit APInt");
128+
} else {
129+
assert(llvm::isUIntN(BitWidth, val) &&
130+
"Value is not an N-bit unsigned value");
131+
}
125132
}
126133
}
127134
if (isSingleWord()) {

llvm/unittests/ADT/APIntTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,6 +3421,7 @@ TEST(APIntTest, ZeroWidth) {
34213421
EXPECT_EQ(0U, ZW.getBitWidth());
34223422
EXPECT_EQ(0U, APInt(0, ArrayRef<uint64_t>({0, 1, 2})).getBitWidth());
34233423
EXPECT_EQ(0U, APInt(0, "0", 10).getBitWidth());
3424+
EXPECT_EQ(0U, APInt::getAllOnes(0).getBitWidth());
34243425

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

0 commit comments

Comments
 (0)