Skip to content

Commit 30eaac5

Browse files
[CodeGen] Compute known bits of Carry/Borrow for UADDO, SADDO, USUBO, and SSUBO
Computes known bits for carry/borrow for UADDO_CARRY and USUBO_CARRY Operations. Carry/borrow are truncated to bit width 1. Adds computeKnownBits for Carry/Borrow for UADDO, SADDO, USUBO, and SSUBO. Carry over is expected to be of bit width 1. Adds unit tests for UADDO_CARRY and USUBO_CARRY. Adds a unit test for UADDO_CARRY and USUBO_CARRY. Co-authored-by: Shafik Yaghmour <[email protected]>
1 parent 6089f4f commit 30eaac5

File tree

5 files changed

+106
-23
lines changed

5 files changed

+106
-23
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ struct KnownBits {
332332
static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS,
333333
KnownBits RHS);
334334

335-
/// Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
335+
/// Compute known bits results from subtracting RHS from LHS with 1-bit
336+
/// Borrow.
336337
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
337338
const KnownBits &Borrow);
338339

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3743,12 +3743,13 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
37433743

37443744
// With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
37453745
KnownBits Borrow(1);
3746-
if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY)
3747-
// TODO: Compute known bits for the carry operand. Creates
3748-
// parity with UADDO_CARRY And SADDO_CARRY as of now.
3749-
Borrow.resetAll();
3750-
else
3746+
if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3747+
Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3748+
// Borrow has bit width 1
3749+
Borrow = Borrow.trunc(1);
3750+
} else {
37513751
Borrow.setAllZero();
3752+
}
37523753

37533754
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
37543755
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
@@ -3777,15 +3778,13 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
37773778
if (Opcode == ISD::ADDE)
37783779
// Can't track carry from glue, set carry to unknown.
37793780
Carry.resetAll();
3780-
else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY)
3781-
// TODO: Compute known bits for the carry operand. Not sure if it is worth
3782-
// the trouble (how often will we find a known carry bit). And I haven't
3783-
// tested this very much yet, but something like this might work:
3784-
// Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3785-
// Carry = Carry.zextOrTrunc(1, false);
3786-
Carry.resetAll();
3787-
else
3781+
else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3782+
Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3783+
// Carry has bit width 1
3784+
Carry = Carry.trunc(1);
3785+
} else {
37883786
Carry.setAllZero();
3787+
}
37893788

37903789
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
37913790
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);

llvm/lib/Support/KnownBits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ KnownBits KnownBits::computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
9393
// Carry 1 - Borrow in ::computeForAddCarry
9494
std::swap(RHS.Zero, RHS.One);
9595
return ::computeForAddCarry(LHS, RHS,
96-
/*CarryZero*/ Borrow.One.getBoolValue(),
97-
/*CarryOne*/ Borrow.Zero.getBoolValue());
96+
/*CarryZero=*/Borrow.One.getBoolValue(),
97+
/*CarryOne=*/Borrow.Zero.getBoolValue());
9898
}
9999

100100
KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const {

llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,59 @@ TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_ADD) {
254254
EXPECT_EQ(Known.One, APInt(8, 0x55));
255255
}
256256

257+
// Piggy-backing on the AArch64 tests to verify SelectionDAG::computeKnownBits.
258+
TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_UADDO_CARRY) {
259+
SDLoc Loc;
260+
auto IntVT = EVT::getIntegerVT(Context, 8);
261+
auto UnknownOp = DAG->getRegister(0, IntVT);
262+
auto Mask_Zero = DAG->getConstant(0x28, Loc, IntVT);
263+
auto Mask_One = DAG->getConstant(0x20, Loc, IntVT);
264+
auto N0 = DAG->getNode(ISD::AND, Loc, IntVT, Mask_Zero, UnknownOp);
265+
N0 = DAG->getNode(ISD::OR, Loc, IntVT, Mask_One, N0);
266+
auto N1 = DAG->getConstant(0x65, Loc, IntVT);
267+
268+
KnownBits Known;
269+
270+
auto UnknownBorrow = DAG->getRegister(1, IntVT);
271+
auto OpUnknownBorrow =
272+
DAG->getNode(ISD::UADDO_CARRY, Loc, IntVT, N0, N1, UnknownBorrow);
273+
// N0 = 0010?000
274+
// N1 = 01100101
275+
// B = ?
276+
// =>
277+
// Known.Zero = 01110000 (0x70)
278+
// Known.One = 10000100 (0x84)
279+
Known = DAG->computeKnownBits(OpUnknownBorrow);
280+
EXPECT_EQ(Known.Zero, APInt(8, 0x70));
281+
EXPECT_EQ(Known.One, APInt(8, 0x84));
282+
283+
auto ZeroBorrow = DAG->getConstant(0x0, Loc, IntVT);
284+
auto OpZeroBorrow =
285+
DAG->getNode(ISD::UADDO_CARRY, Loc, IntVT, N0, N1, ZeroBorrow);
286+
// N0 = 0010?000
287+
// N1 = 01100101
288+
// B = 0
289+
// =>
290+
// Known.Zero = 01110010 (0x72)
291+
// Known.One = 10000101 (0x85)
292+
Known = DAG->computeKnownBits(OpZeroBorrow);
293+
EXPECT_EQ(Known.Zero, APInt(8, 0x72));
294+
EXPECT_EQ(Known.One, APInt(8, 0x85));
295+
296+
auto OneBorrow = DAG->getConstant(0x1, Loc, IntVT);
297+
auto OpOneBorrow =
298+
DAG->getNode(ISD::UADDO_CARRY, Loc, IntVT, N0, N1, OneBorrow);
299+
// N0 = 0010?000
300+
// N1 = 01100101
301+
// B = 1
302+
// =>
303+
// Known.Zero = 01110001 (0x71)
304+
// Known.One = 10000110 (0x86)
305+
Known = DAG->computeKnownBits(OpOneBorrow);
306+
EXPECT_EQ(Known.Zero, APInt(8, 0x71));
307+
EXPECT_EQ(Known.One, APInt(8, 0x86));
308+
}
309+
257310
// Piggy-backing on the AArch64 tests to verify SelectionDAG::computeKnownBits.
258311
TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_SUB) {
259312
SDLoc Loc;
@@ -278,23 +331,53 @@ TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_USUBO_CARRY) {
278331
SDLoc Loc;
279332
auto IntVT = EVT::getIntegerVT(Context, 8);
280333
auto N0 = DAG->getConstant(0x5a, Loc, IntVT);
281-
auto UnknownOp1 = DAG->getRegister(0, IntVT); // ????????
334+
auto UnknownOp = DAG->getRegister(0, IntVT); // ????????
282335
auto Mask1_Zero = DAG->getConstant(0x8, Loc, IntVT); // 00001000
283336
auto Mask1_One = DAG->getConstant(0x20, Loc, IntVT); // 00100000
284337
// N1 = (???????? & 00001000) | 00100000 = 0010?000
285-
auto N1 = DAG->getNode(ISD::AND, Loc, IntVT, Mask1_Zero, UnknownOp1);
338+
auto N1 = DAG->getNode(ISD::AND, Loc, IntVT, Mask1_Zero, UnknownOp);
286339
N1 = DAG->getNode(ISD::OR, Loc, IntVT, Mask1_One, N1);
287-
auto UnknownOpC = DAG->getRegister(1, IntVT);
288-
auto Op = DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, UnknownOpC);
340+
341+
KnownBits Known;
342+
343+
auto UnknownBorrow = DAG->getRegister(1, IntVT);
344+
auto OpUnknownBorrow =
345+
DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, UnknownBorrow);
289346
// N0 = 01011010
290347
// N1 = 0010?000
291-
// C = ?
348+
// B = ?
292349
// =>
293350
// Known.Zero = 11000100 (0xc4)
294351
// Known.One = 00110000 (0x30)
295-
KnownBits Known = DAG->computeKnownBits(Op);
352+
Known = DAG->computeKnownBits(OpUnknownBorrow);
296353
EXPECT_EQ(Known.Zero, APInt(8, 0xc4));
297354
EXPECT_EQ(Known.One, APInt(8, 0x30));
355+
356+
auto ZeroBorrow = DAG->getConstant(0x0, Loc, IntVT);
357+
auto OpZeroBorrow =
358+
DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, ZeroBorrow);
359+
// N0 = 01011010
360+
// N1 = 0010?000
361+
// B = 0
362+
// =>
363+
// Known.Zero = 11000101 (0xc5)
364+
// Known.One = 00110010 (0x32)
365+
Known = DAG->computeKnownBits(OpZeroBorrow);
366+
EXPECT_EQ(Known.Zero, APInt(8, 0xc5));
367+
EXPECT_EQ(Known.One, APInt(8, 0x32));
368+
369+
auto OneBorrow = DAG->getConstant(0x1, Loc, IntVT);
370+
auto OpOneBorrow =
371+
DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, OneBorrow);
372+
// N0 = 01011010
373+
// N1 = 0010?000
374+
// B = 1
375+
// =>
376+
// Known.Zero = 11000110 (0xc6)
377+
// Known.One = 00110001 (0x31)
378+
Known = DAG->computeKnownBits(OpOneBorrow);
379+
EXPECT_EQ(Known.Zero, APInt(8, 0xc6));
380+
EXPECT_EQ(Known.One, APInt(8, 0x31));
298381
}
299382

300383
TEST_F(AArch64SelectionDAGTest, isSplatValue_Fixed_BUILD_VECTOR) {

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ TEST(KnownBitsTest, SubBorrowExhaustive) {
218218
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
219219
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
220220
ForeachKnownBits(1, [&](const KnownBits &KnownBorrow) {
221-
// Explicitly compute known bits of the addition by trying all
221+
// Explicitly compute known bits of the subtraction by trying all
222222
// possibilities.
223223
KnownBits Known(Bits);
224224
Known.Zero.setAllBits();

0 commit comments

Comments
 (0)