Skip to content

[llvm] Add KnownBits implementations for avgFloor and avgCeil #86445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e447f4c
initial attempt
changkhothuychung Mar 24, 2024
8b6e8bb
add definitions to header file
changkhothuychung Mar 24, 2024
f7eff4e
Merge branch 'main' into knownbits_impl
changkhothuychung Apr 4, 2024
b0d8100
fix comments
changkhothuychung Apr 5, 2024
ba43d22
add tests
changkhothuychung Apr 5, 2024
af3a08c
remove opcode check
changkhothuychung Apr 5, 2024
d5ded0e
fix test
changkhothuychung Apr 6, 2024
e9c8c6c
fix or operation
changkhothuychung Apr 8, 2024
529ebe7
fix variable name
changkhothuychung Apr 8, 2024
cecb3c9
fix test
changkhothuychung Apr 10, 2024
0ad452f
try not touch zeros
changkhothuychung Apr 12, 2024
87367c0
revert back
changkhothuychung Apr 12, 2024
6505a12
change impl
changkhothuychung Apr 12, 2024
1390e86
change impl to use APInt
changkhothuychung Apr 15, 2024
1dffaa4
fix syntax
changkhothuychung Apr 15, 2024
afd2456
update from main
changkhothuychung Apr 24, 2024
61c7db7
clang format
changkhothuychung Apr 24, 2024
857f57a
try new impl
changkhothuychung May 12, 2024
1403ad2
Merge branch 'main' into knownbits_impl
changkhothuychung May 12, 2024
672566a
remove unused code
changkhothuychung May 12, 2024
91e6eb2
missing header
changkhothuychung May 12, 2024
94ce6cb
revert
changkhothuychung May 12, 2024
7825ad7
typo
changkhothuychung May 14, 2024
0b91c85
add helper function
changkhothuychung May 17, 2024
489a371
change function name
changkhothuychung May 17, 2024
10de0a1
syntax fix
changkhothuychung May 17, 2024
69c1a37
syntax fix
changkhothuychung May 17, 2024
99e179a
syntax fix
changkhothuychung May 17, 2024
ab00768
some fix
changkhothuychung May 17, 2024
d230997
some fix
changkhothuychung May 17, 2024
eca6944
revert
changkhothuychung May 17, 2024
2cd048c
make test optimal
changkhothuychung May 17, 2024
3ae62ff
add avgCeilU back
changkhothuychung May 17, 2024
667c7af
remove spurious reformat
changkhothuychung May 17, 2024
44c0b92
remove spurious reformat
changkhothuychung May 17, 2024
22b2aa9
disable optimality check
changkhothuychung May 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions llvm/include/llvm/Support/KnownBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ struct KnownBits {
/// Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS);

/// Compute knownbits resulting from APIntOps::avgFloorS
static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS);

/// Compute knownbits resulting from APIntOps::avgFloorU
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS);

/// Compute knownbits resulting from APIntOps::avgCeilS
static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS);

/// Compute knownbits resulting from APIntOps::avgCeilU
static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);

/// Compute known bits resulting from multiplying LHS and RHS.
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
bool NoUndefSelfMultiply = false);
Expand Down
29 changes: 19 additions & 10 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3468,19 +3468,28 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
Known = KnownBits::mulhs(Known, Known2);
break;
}
case ISD::AVGFLOORU:
case ISD::AVGCEILU:
case ISD::AVGFLOORS:
case ISD::AVGFLOORU: {
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
Known = KnownBits::avgFloorU(Known, Known2);
break;
}
case ISD::AVGCEILU: {
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
Known = KnownBits::avgCeilU(Known, Known2);
break;
}
case ISD::AVGFLOORS: {
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
Known = KnownBits::avgFloorS(Known, Known2);
break;
}
case ISD::AVGCEILS: {
bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
Known = Known.extractBits(BitWidth, 1);
Known = KnownBits::avgCeilS(Known, Known2);
break;
}
case ISD::SELECT:
Expand Down
31 changes: 31 additions & 0 deletions llvm/lib/Support/KnownBits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,37 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
}

static KnownBits avgCompute(KnownBits LHS, KnownBits RHS, bool IsCeil,
bool IsSigned) {
unsigned BitWidth = LHS.getBitWidth();
LHS = IsSigned ? LHS.sext(BitWidth + 1) : LHS.zext(BitWidth + 1);
RHS = IsSigned ? RHS.sext(BitWidth + 1) : RHS.zext(BitWidth + 1);
KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
LHS = KnownBits::computeForAddCarry(LHS, RHS, Carry);
LHS = LHS.extractBits(BitWidth, 1);
return LHS;
}

KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
return avgCompute(LHS, RHS, /* IsCeil */ false,
/* IsSigned */ true);
}

KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
return avgCompute(LHS, RHS, /* IsCeil */ false,
/* IsSigned */ false);
}

KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
return avgCompute(LHS, RHS, /* IsCeil */ true,
/* IsSigned */ true);
}

KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
return avgCompute(LHS, RHS, /* IsCeil */ true,
/* IsSigned */ false);
}

KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
bool NoUndefSelfMultiply) {
unsigned BitWidth = LHS.getBitWidth();
Expand Down
12 changes: 12 additions & 0 deletions llvm/unittests/Support/KnownBitsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,18 @@ TEST(KnownBitsTest, BinaryExhaustive) {
"mulhu", KnownBits::mulhu,
[](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
/*CheckOptimality=*/false);

testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS, APIntOps::avgFloorS,
false);

testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU, APIntOps::avgFloorU,
false);

testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU,
false);

testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS,
false);
}

TEST(KnownBitsTest, UnaryExhaustive) {
Expand Down
Loading