Skip to content

Commit 6654235

Browse files
authored
[SelectionDAG] implement computeKnownBits for add AVG* instructions (#86754)
knownBits calculation for **AVGFLOORU** / **AVGFLOORS** / **AVGCEILU** / **AVGCEILS** instructions Prerequisite for #76644
1 parent 6cce67a commit 6654235

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -3419,13 +3419,18 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34193419
Known = KnownBits::mulhs(Known, Known2);
34203420
break;
34213421
}
3422-
case ISD::AVGCEILU: {
3422+
case ISD::AVGFLOORU:
3423+
case ISD::AVGCEILU:
3424+
case ISD::AVGFLOORS:
3425+
case ISD::AVGCEILS: {
3426+
bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
3427+
bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
34233428
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
34243429
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3425-
Known = Known.zext(BitWidth + 1);
3426-
Known2 = Known2.zext(BitWidth + 1);
3427-
KnownBits One = KnownBits::makeConstant(APInt(1, 1));
3428-
Known = KnownBits::computeForAddCarry(Known, Known2, One);
3430+
Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
3431+
Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
3432+
KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
3433+
Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
34293434
Known = Known.extractBits(BitWidth, 1);
34303435
break;
34313436
}

llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -796,4 +796,52 @@ TEST_F(AArch64SelectionDAGTest, computeKnownBits_extload_knownnegative) {
796796
EXPECT_EQ(Known.One, APInt(32, 0xfffffff0));
797797
}
798798

799+
TEST_F(AArch64SelectionDAGTest,
800+
computeKnownBits_AVGFLOORU_AVGFLOORS_AVGCEILU_AVGCEILS) {
801+
SDLoc Loc;
802+
auto Int8VT = EVT::getIntegerVT(Context, 8);
803+
auto Int16VT = EVT::getIntegerVT(Context, 16);
804+
auto Int8Vec8VT = EVT::getVectorVT(Context, Int8VT, 8);
805+
auto Int16Vec8VT = EVT::getVectorVT(Context, Int16VT, 8);
806+
807+
SDValue UnknownOp0 = DAG->getRegister(0, Int8Vec8VT);
808+
SDValue UnknownOp1 = DAG->getRegister(1, Int8Vec8VT);
809+
810+
SDValue ZextOp0 =
811+
DAG->getNode(ISD::ZERO_EXTEND, Loc, Int16Vec8VT, UnknownOp0);
812+
SDValue ZextOp1 =
813+
DAG->getNode(ISD::ZERO_EXTEND, Loc, Int16Vec8VT, UnknownOp1);
814+
// ZextOp0 = 00000000????????
815+
// ZextOp1 = 00000000????????
816+
// => (for all AVG* instructions)
817+
// Known.Zero = 1111111100000000 (0xFF00)
818+
// Known.One = 0000000000000000 (0x0000)
819+
auto Zeroes = APInt(16, 0xFF00);
820+
auto Ones = APInt(16, 0x0000);
821+
822+
SDValue AVGFLOORU =
823+
DAG->getNode(ISD::AVGFLOORU, Loc, Int16Vec8VT, ZextOp0, ZextOp1);
824+
KnownBits KnownAVGFLOORU = DAG->computeKnownBits(AVGFLOORU);
825+
EXPECT_EQ(KnownAVGFLOORU.Zero, Zeroes);
826+
EXPECT_EQ(KnownAVGFLOORU.One, Ones);
827+
828+
SDValue AVGFLOORS =
829+
DAG->getNode(ISD::AVGFLOORU, Loc, Int16Vec8VT, ZextOp0, ZextOp1);
830+
KnownBits KnownAVGFLOORS = DAG->computeKnownBits(AVGFLOORS);
831+
EXPECT_EQ(KnownAVGFLOORS.Zero, Zeroes);
832+
EXPECT_EQ(KnownAVGFLOORS.One, Ones);
833+
834+
SDValue AVGCEILU =
835+
DAG->getNode(ISD::AVGCEILU, Loc, Int16Vec8VT, ZextOp0, ZextOp1);
836+
KnownBits KnownAVGCEILU = DAG->computeKnownBits(AVGCEILU);
837+
EXPECT_EQ(KnownAVGCEILU.Zero, Zeroes);
838+
EXPECT_EQ(KnownAVGCEILU.One, Ones);
839+
840+
SDValue AVGCEILS =
841+
DAG->getNode(ISD::AVGCEILS, Loc, Int16Vec8VT, ZextOp0, ZextOp1);
842+
KnownBits KnownAVGCEILS = DAG->computeKnownBits(AVGCEILS);
843+
EXPECT_EQ(KnownAVGCEILS.Zero, Zeroes);
844+
EXPECT_EQ(KnownAVGCEILS.One, Ones);
845+
}
846+
799847
} // end namespace llvm

0 commit comments

Comments
 (0)