From 69e7635972e5f3acf286816049abb020395db2fd Mon Sep 17 00:00:00 2001 From: Christian Kissig Date: Tue, 26 Sep 2023 12:18:59 +0000 Subject: [PATCH 1/3] [Support] Add KnownBits::computeForSubBorrow Implements computeForSubBorrow as alias for computeforAddCarry. Borrow is expected to be 1-bit wide. Adds exhaustive unit test. --- llvm/include/llvm/Support/KnownBits.h | 4 +++ llvm/lib/Support/KnownBits.cpp | 12 +++++++++ llvm/unittests/Support/KnownBitsTest.cpp | 31 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h index 8462aa11202d5..b9a996f664c89 100644 --- a/llvm/include/llvm/Support/KnownBits.h +++ b/llvm/include/llvm/Support/KnownBits.h @@ -332,6 +332,10 @@ struct KnownBits { static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS, KnownBits RHS); + /// Compute known bits results from subtracting RHS from LHS with 1-bit Borrow. + static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, + const KnownBits &Borrow); + /// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS) static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS); diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp index 097c22d33dd12..99ac50a34666f 100644 --- a/llvm/lib/Support/KnownBits.cpp +++ b/llvm/lib/Support/KnownBits.cpp @@ -85,6 +85,18 @@ KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, return KnownOut; } +KnownBits KnownBits::computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, + const KnownBits &Borrow) { + assert(Borrow.getBitWidth() == 1 && "Borrow must be 1-bit"); + + // LHS - RHS = LHS + ~RHS + 1 + // Carry 1 - Borrow in ::computeForAddCarry + std::swap(RHS.Zero, RHS.One); + return ::computeForAddCarry(LHS, RHS, + /*CarryZero*/ Borrow.One.getBoolValue(), + /*CarryOne*/ Borrow.Zero.getBoolValue()); +} + KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const { unsigned BitWidth = getBitWidth(); assert(0 < SrcBitWidth && SrcBitWidth <= BitWidth && diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp index 9d184beea3ba9..5597d69ab248d 100644 --- a/llvm/unittests/Support/KnownBitsTest.cpp +++ b/llvm/unittests/Support/KnownBitsTest.cpp @@ -213,6 +213,37 @@ TEST(KnownBitsTest, AddSubExhaustive) { TestAddSubExhaustive(false); } +TEST(KnownBitsTest, SubBorrowExhaustive) { + unsigned Bits = 4; + ForeachKnownBits(Bits, [&](const KnownBits &Known1) { + ForeachKnownBits(Bits, [&](const KnownBits &Known2) { + ForeachKnownBits(1, [&](const KnownBits &KnownBorrow) { + // Explicitly compute known bits of the addition by trying all + // possibilities. + KnownBits Known(Bits); + Known.Zero.setAllBits(); + Known.One.setAllBits(); + ForeachNumInKnownBits(Known1, [&](const APInt &N1) { + ForeachNumInKnownBits(Known2, [&](const APInt &N2) { + ForeachNumInKnownBits(KnownBorrow, [&](const APInt &Borrow) { + APInt Sub = N1 - N2; + if (Borrow.getBoolValue()) + --Sub; + + Known.One &= Sub; + Known.Zero &= ~Sub; + }); + }); + }); + + KnownBits KnownComputed = + KnownBits::computeForSubBorrow(Known1, Known2, KnownBorrow); + EXPECT_EQ(Known, KnownComputed); + }); + }); + }); +} + TEST(KnownBitsTest, BinaryExhaustive) { testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { From 6089f4f90d43e21daf95f35c7875b27268fe2bd9 Mon Sep 17 00:00:00 2001 From: Christian Kissig Date: Tue, 26 Sep 2023 18:02:49 +0000 Subject: [PATCH 2/3] [CodeGen] Implement USUBC, USUBO_CARRY, and SSUBO_CARRY with KnownBits::computeForSubBorrow --- .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 12 ++++++---- .../CodeGen/AArch64SelectionDAGTest.cpp | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 8c275bfcfbd27..2fd00caa5e4ee 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -3741,14 +3741,18 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts, assert(Op.getResNo() == 0 && "We only compute knownbits for the difference here."); - // TODO: Compute influence of the carry operand. + // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in. + KnownBits Borrow(1); if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) - break; + // TODO: Compute known bits for the carry operand. Creates + // parity with UADDO_CARRY And SADDO_CARRY as of now. + Borrow.resetAll(); + else + Borrow.setAllZero(); Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); - Known = KnownBits::computeForAddSub(/* Add */ false, /* NSW */ false, - Known, Known2); + Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow); break; } case ISD::UADDO: diff --git a/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp b/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp index 0e1f2736907ff..303ee50a763fb 100644 --- a/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp +++ b/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp @@ -273,6 +273,30 @@ TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_SUB) { EXPECT_EQ(Known.One, APInt(8, 0x1)); } +// Piggy-backing on the AArch64 tests to verify SelectionDAG::computeKnownBits. +TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_USUBO_CARRY) { + SDLoc Loc; + auto IntVT = EVT::getIntegerVT(Context, 8); + auto N0 = DAG->getConstant(0x5a, Loc, IntVT); + auto UnknownOp1 = DAG->getRegister(0, IntVT); // ???????? + auto Mask1_Zero = DAG->getConstant(0x8, Loc, IntVT); // 00001000 + auto Mask1_One = DAG->getConstant(0x20, Loc, IntVT); // 00100000 + // N1 = (???????? & 00001000) | 00100000 = 0010?000 + auto N1 = DAG->getNode(ISD::AND, Loc, IntVT, Mask1_Zero, UnknownOp1); + N1 = DAG->getNode(ISD::OR, Loc, IntVT, Mask1_One, N1); + auto UnknownOpC = DAG->getRegister(1, IntVT); + auto Op = DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, UnknownOpC); + // N0 = 01011010 + // N1 = 0010?000 + // C = ? + // => + // Known.Zero = 11000100 (0xc4) + // Known.One = 00110000 (0x30) + KnownBits Known = DAG->computeKnownBits(Op); + EXPECT_EQ(Known.Zero, APInt(8, 0xc4)); + EXPECT_EQ(Known.One, APInt(8, 0x30)); +} + TEST_F(AArch64SelectionDAGTest, isSplatValue_Fixed_BUILD_VECTOR) { TargetLowering TL(*TM); From 30eaac5871fe485b5a244ccb9f0bb0dbbe7dcf32 Mon Sep 17 00:00:00 2001 From: Christian Kissig Date: Wed, 27 Sep 2023 12:35:59 +0000 Subject: [PATCH 3/3] [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 --- llvm/include/llvm/Support/KnownBits.h | 3 +- .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 25 +++-- llvm/lib/Support/KnownBits.cpp | 4 +- .../CodeGen/AArch64SelectionDAGTest.cpp | 95 +++++++++++++++++-- llvm/unittests/Support/KnownBitsTest.cpp | 2 +- 5 files changed, 106 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h index b9a996f664c89..fb034e0b9e3ba 100644 --- a/llvm/include/llvm/Support/KnownBits.h +++ b/llvm/include/llvm/Support/KnownBits.h @@ -332,7 +332,8 @@ struct KnownBits { static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS, KnownBits RHS); - /// Compute known bits results from subtracting RHS from LHS with 1-bit Borrow. + /// Compute known bits results from subtracting RHS from LHS with 1-bit + /// Borrow. static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 2fd00caa5e4ee..3c131d9247d72 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -3743,12 +3743,13 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts, // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in. KnownBits Borrow(1); - if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) - // TODO: Compute known bits for the carry operand. Creates - // parity with UADDO_CARRY And SADDO_CARRY as of now. - Borrow.resetAll(); - else + if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) { + Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1); + // Borrow has bit width 1 + Borrow = Borrow.trunc(1); + } else { Borrow.setAllZero(); + } Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); @@ -3777,15 +3778,13 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts, if (Opcode == ISD::ADDE) // Can't track carry from glue, set carry to unknown. Carry.resetAll(); - else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) - // TODO: Compute known bits for the carry operand. Not sure if it is worth - // the trouble (how often will we find a known carry bit). And I haven't - // tested this very much yet, but something like this might work: - // Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1); - // Carry = Carry.zextOrTrunc(1, false); - Carry.resetAll(); - else + else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) { + Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1); + // Carry has bit width 1 + Carry = Carry.trunc(1); + } else { Carry.setAllZero(); + } Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp index 99ac50a34666f..770e4051ca3ff 100644 --- a/llvm/lib/Support/KnownBits.cpp +++ b/llvm/lib/Support/KnownBits.cpp @@ -93,8 +93,8 @@ KnownBits KnownBits::computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, // Carry 1 - Borrow in ::computeForAddCarry std::swap(RHS.Zero, RHS.One); return ::computeForAddCarry(LHS, RHS, - /*CarryZero*/ Borrow.One.getBoolValue(), - /*CarryOne*/ Borrow.Zero.getBoolValue()); + /*CarryZero=*/Borrow.One.getBoolValue(), + /*CarryOne=*/Borrow.Zero.getBoolValue()); } KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const { diff --git a/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp b/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp index 303ee50a763fb..bb8e76a2eeb8b 100644 --- a/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp +++ b/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp @@ -254,6 +254,59 @@ TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_ADD) { EXPECT_EQ(Known.One, APInt(8, 0x55)); } +// Piggy-backing on the AArch64 tests to verify SelectionDAG::computeKnownBits. +TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_UADDO_CARRY) { + SDLoc Loc; + auto IntVT = EVT::getIntegerVT(Context, 8); + auto UnknownOp = DAG->getRegister(0, IntVT); + auto Mask_Zero = DAG->getConstant(0x28, Loc, IntVT); + auto Mask_One = DAG->getConstant(0x20, Loc, IntVT); + auto N0 = DAG->getNode(ISD::AND, Loc, IntVT, Mask_Zero, UnknownOp); + N0 = DAG->getNode(ISD::OR, Loc, IntVT, Mask_One, N0); + auto N1 = DAG->getConstant(0x65, Loc, IntVT); + + KnownBits Known; + + auto UnknownBorrow = DAG->getRegister(1, IntVT); + auto OpUnknownBorrow = + DAG->getNode(ISD::UADDO_CARRY, Loc, IntVT, N0, N1, UnknownBorrow); + // N0 = 0010?000 + // N1 = 01100101 + // B = ? + // => + // Known.Zero = 01110000 (0x70) + // Known.One = 10000100 (0x84) + Known = DAG->computeKnownBits(OpUnknownBorrow); + EXPECT_EQ(Known.Zero, APInt(8, 0x70)); + EXPECT_EQ(Known.One, APInt(8, 0x84)); + + auto ZeroBorrow = DAG->getConstant(0x0, Loc, IntVT); + auto OpZeroBorrow = + DAG->getNode(ISD::UADDO_CARRY, Loc, IntVT, N0, N1, ZeroBorrow); + // N0 = 0010?000 + // N1 = 01100101 + // B = 0 + // => + // Known.Zero = 01110010 (0x72) + // Known.One = 10000101 (0x85) + Known = DAG->computeKnownBits(OpZeroBorrow); + EXPECT_EQ(Known.Zero, APInt(8, 0x72)); + EXPECT_EQ(Known.One, APInt(8, 0x85)); + + auto OneBorrow = DAG->getConstant(0x1, Loc, IntVT); + auto OpOneBorrow = + DAG->getNode(ISD::UADDO_CARRY, Loc, IntVT, N0, N1, OneBorrow); + // N0 = 0010?000 + // N1 = 01100101 + // B = 1 + // => + // Known.Zero = 01110001 (0x71) + // Known.One = 10000110 (0x86) + Known = DAG->computeKnownBits(OpOneBorrow); + EXPECT_EQ(Known.Zero, APInt(8, 0x71)); + EXPECT_EQ(Known.One, APInt(8, 0x86)); +} + // Piggy-backing on the AArch64 tests to verify SelectionDAG::computeKnownBits. TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_SUB) { SDLoc Loc; @@ -278,23 +331,53 @@ TEST_F(AArch64SelectionDAGTest, ComputeKnownBits_USUBO_CARRY) { SDLoc Loc; auto IntVT = EVT::getIntegerVT(Context, 8); auto N0 = DAG->getConstant(0x5a, Loc, IntVT); - auto UnknownOp1 = DAG->getRegister(0, IntVT); // ???????? + auto UnknownOp = DAG->getRegister(0, IntVT); // ???????? auto Mask1_Zero = DAG->getConstant(0x8, Loc, IntVT); // 00001000 auto Mask1_One = DAG->getConstant(0x20, Loc, IntVT); // 00100000 // N1 = (???????? & 00001000) | 00100000 = 0010?000 - auto N1 = DAG->getNode(ISD::AND, Loc, IntVT, Mask1_Zero, UnknownOp1); + auto N1 = DAG->getNode(ISD::AND, Loc, IntVT, Mask1_Zero, UnknownOp); N1 = DAG->getNode(ISD::OR, Loc, IntVT, Mask1_One, N1); - auto UnknownOpC = DAG->getRegister(1, IntVT); - auto Op = DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, UnknownOpC); + + KnownBits Known; + + auto UnknownBorrow = DAG->getRegister(1, IntVT); + auto OpUnknownBorrow = + DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, UnknownBorrow); // N0 = 01011010 // N1 = 0010?000 - // C = ? + // B = ? // => // Known.Zero = 11000100 (0xc4) // Known.One = 00110000 (0x30) - KnownBits Known = DAG->computeKnownBits(Op); + Known = DAG->computeKnownBits(OpUnknownBorrow); EXPECT_EQ(Known.Zero, APInt(8, 0xc4)); EXPECT_EQ(Known.One, APInt(8, 0x30)); + + auto ZeroBorrow = DAG->getConstant(0x0, Loc, IntVT); + auto OpZeroBorrow = + DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, ZeroBorrow); + // N0 = 01011010 + // N1 = 0010?000 + // B = 0 + // => + // Known.Zero = 11000101 (0xc5) + // Known.One = 00110010 (0x32) + Known = DAG->computeKnownBits(OpZeroBorrow); + EXPECT_EQ(Known.Zero, APInt(8, 0xc5)); + EXPECT_EQ(Known.One, APInt(8, 0x32)); + + auto OneBorrow = DAG->getConstant(0x1, Loc, IntVT); + auto OpOneBorrow = + DAG->getNode(ISD::USUBO_CARRY, Loc, IntVT, N0, N1, OneBorrow); + // N0 = 01011010 + // N1 = 0010?000 + // B = 1 + // => + // Known.Zero = 11000110 (0xc6) + // Known.One = 00110001 (0x31) + Known = DAG->computeKnownBits(OpOneBorrow); + EXPECT_EQ(Known.Zero, APInt(8, 0xc6)); + EXPECT_EQ(Known.One, APInt(8, 0x31)); } TEST_F(AArch64SelectionDAGTest, isSplatValue_Fixed_BUILD_VECTOR) { diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp index 5597d69ab248d..c0377d45c303a 100644 --- a/llvm/unittests/Support/KnownBitsTest.cpp +++ b/llvm/unittests/Support/KnownBitsTest.cpp @@ -218,7 +218,7 @@ TEST(KnownBitsTest, SubBorrowExhaustive) { ForeachKnownBits(Bits, [&](const KnownBits &Known1) { ForeachKnownBits(Bits, [&](const KnownBits &Known2) { ForeachKnownBits(1, [&](const KnownBits &KnownBorrow) { - // Explicitly compute known bits of the addition by trying all + // Explicitly compute known bits of the subtraction by trying all // possibilities. KnownBits Known(Bits); Known.Zero.setAllBits();