Skip to content

Commit 453ef78

Browse files
committed
[KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values
Equivalent to "umax(A, B) - umin(A, B)" This is just an initial correctness implementation, hopefully we can make this optimal in the future.
1 parent 8bc0cbd commit 453ef78

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ struct KnownBits {
385385
/// Compute known bits for smin(LHS, RHS).
386386
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
387387

388+
/// Compute known bits for absdiff(LHS, RHS).
389+
static KnownBits absdiff(const KnownBits &LHS, const KnownBits &RHS);
390+
388391
/// Compute known bits for shl(LHS, RHS).
389392
/// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
390393
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS,

llvm/lib/Support/KnownBits.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
176176
return Flip(umax(Flip(LHS), Flip(RHS)));
177177
}
178178

179+
KnownBits KnownBits::absdiff(const KnownBits &LHS, const KnownBits &RHS) {
180+
// absdiff(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
181+
KnownBits UMaxValue = umax(LHS, RHS);
182+
KnownBits UMinValue = umin(LHS, RHS);
183+
KnownBits KnownAbsDiff = computeForAddSub(false, false, UMaxValue, UMinValue);
184+
185+
// fallback - find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
186+
if (KnownAbsDiff.isUnknown()) {
187+
KnownBits Diff0 = computeForAddSub(false, false, LHS, RHS);
188+
KnownBits Diff1 = computeForAddSub(false, false, RHS, LHS);
189+
KnownAbsDiff = Diff0.intersectWith(Diff1);
190+
}
191+
192+
assert(!KnownAbsDiff.hasConflict() && "Bad Output");
193+
return KnownAbsDiff;
194+
}
195+
179196
static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth) {
180197
if (isPowerOf2_32(BitWidth))
181198
return MaxValue.extractBitsAsZExtValue(Log2_32(BitWidth), 0);

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
281281
return KnownBits::smin(Known1, Known2);
282282
},
283283
[](const APInt &N1, const APInt &N2) { return APIntOps::smin(N1, N2); });
284-
284+
testBinaryOpExhaustive(
285+
[](const KnownBits &Known1, const KnownBits &Known2) {
286+
return KnownBits::absdiff(Known1, Known2);
287+
},
288+
[](const APInt &N1, const APInt &N2) {
289+
return APIntOps::absdiff(N1, N2);
290+
},
291+
checkCorrectnessOnlyBinary);
285292
testBinaryOpExhaustive(
286293
[](const KnownBits &Known1, const KnownBits &Known2) {
287294
return KnownBits::udiv(Known1, Known2);

0 commit comments

Comments
 (0)