Skip to content

Commit

Permalink
[KnownBits] Improve KnownBits::udiv
Browse files Browse the repository at this point in the history
We can more precisely determine the upper bits doing `MaxNum /
MinDenum` as opposed to only using the MSB.

As well, if the `exact` flag is set, we can sometimes determine some
of the low-bits.

Differential Revision: https://reviews.llvm.org/D150094
  • Loading branch information
goldsteinn committed May 16, 2023
1 parent 8c569c9 commit 7d05ab9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/KnownBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ struct KnownBits {
bool Exact = false);

/// Compute known bits for udiv(LHS, RHS).
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS);
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS,
bool Exact = false);

/// Compute known bits for urem(LHS, RHS).
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
Expand Down
30 changes: 21 additions & 9 deletions llvm/lib/Support/KnownBits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
bool Exact) {
// Equivilent of `udiv`. We must have caught this before it was folded.
if (LHS.isNonNegative() && RHS.isNonNegative())
return udiv(LHS, RHS);
return udiv(LHS, RHS, Exact);

unsigned BitWidth = LHS.getBitWidth();
assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
Expand Down Expand Up @@ -604,21 +604,33 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
return Known;
}

KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS) {
KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
bool Exact) {
unsigned BitWidth = LHS.getBitWidth();
assert(!LHS.hasConflict() && !RHS.hasConflict());
KnownBits Known(BitWidth);

// For the purposes of computing leading zeros we can conservatively
// treat a udiv as a logical right shift by the power of 2 known to
// be less than the denominator.
unsigned LeadZ = LHS.countMinLeadingZeros();
unsigned RHSMaxLeadingZeros = RHS.countMaxLeadingZeros();
// We can figure out the minimum number of upper zero bits by doing
// MaxNumerator / MinDenominator. If the Numerator gets smaller or Denominator
// gets larger, the number of upper zero bits increases.
APInt MinDenum = RHS.getMinValue();
APInt MaxNum = LHS.getMaxValue();
APInt MaxRes = MinDenum.isZero() ? MaxNum : MaxNum.udiv(MinDenum);

if (RHSMaxLeadingZeros != BitWidth)
LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
unsigned LeadZ = MaxRes.countLeadingZeros();

Known.Zero.setHighBits(LeadZ);
if (Exact) {
// Odd / Odd -> Odd
if (LHS.One[0] && RHS.One[0])
Known.One.setBit(0);
// Even / Odd -> Even
else if (LHS.Zero[0] && RHS.One[0])
Known.Zero.setBit(0);
// Odd / Even -> impossible
// Even / Even -> unknown
}

return Known;
}

Expand Down
7 changes: 1 addition & 6 deletions llvm/test/Analysis/ValueTracking/knownbits-div.ll
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,7 @@ define i1 @sdiv_exact_even_even_fail_unknown2(i8 %x, i8 %y) {

define i1 @udiv_high_bits(i8 %x, i8 %y) {
; CHECK-LABEL: @udiv_high_bits(
; CHECK-NEXT: [[NUM:%.*]] = and i8 [[X:%.*]], -127
; CHECK-NEXT: [[DENUM:%.*]] = or i8 [[Y:%.*]], 31
; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[NUM]], [[DENUM]]
; CHECK-NEXT: [[AND:%.*]] = and i8 [[DIV]], 8
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[AND]], 8
; CHECK-NEXT: ret i1 [[R]]
; CHECK-NEXT: ret i1 false
;
%num = and i8 %x, 129
%denum = or i8 %y, 31
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/Support/KnownBitsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ TEST(KnownBitsTest, BinaryExhaustive) {
return N1.udiv(N2);
},
checkCorrectnessOnlyBinary);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::udiv(Known1, Known2, /*Exact*/ true);
},
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
if (N2.isZero() || !N1.urem(N2).isZero())
return std::nullopt;
return N1.udiv(N2);
},
checkCorrectnessOnlyBinary);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::sdiv(Known1, Known2);
Expand Down

0 comments on commit 7d05ab9

Please sign in to comment.