Skip to content

Commit fcdf818

Browse files
authored
[KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values (#82354)
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 1e8d3c3 commit fcdf818

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ 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 MinMaxDiff = computeForAddSub(false, false, UMaxValue, UMinValue);
184+
185+
// find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
186+
KnownBits Diff0 = computeForAddSub(false, false, LHS, RHS);
187+
KnownBits Diff1 = computeForAddSub(false, false, RHS, LHS);
188+
KnownBits SubDiff = Diff0.intersectWith(Diff1);
189+
190+
KnownBits KnownAbsDiff = MinMaxDiff.unionWith(SubDiff);
191+
assert(!KnownAbsDiff.hasConflict() && "Bad Output");
192+
return KnownAbsDiff;
193+
}
194+
179195
static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth) {
180196
if (isPowerOf2_32(BitWidth))
181197
return MaxValue.extractBitsAsZExtValue(Log2_32(BitWidth), 0);

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,32 @@ TEST(KnownBitsTest, SubBorrowExhaustive) {
244244
});
245245
}
246246

247+
TEST(KnownBitsTest, AbsDiffSpecialCase) {
248+
// There are 2 implementation of absdiff - both are currently needed to cover
249+
// extra cases.
250+
KnownBits LHS, RHS, Res;
251+
252+
// absdiff(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
253+
// Actual: false (Inputs = 1011, 101?, Computed = 000?, Exact = 000?)
254+
LHS.One = APInt(4, 0b1011);
255+
RHS.One = APInt(4, 0b1010);
256+
LHS.Zero = APInt(4, 0b0100);
257+
RHS.Zero = APInt(4, 0b0100);
258+
Res = KnownBits::absdiff(LHS, RHS);
259+
EXPECT_EQ(0b0000, Res.One.getZExtValue());
260+
EXPECT_EQ(0b1110, Res.Zero.getZExtValue());
261+
262+
// find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
263+
// Actual: false (Inputs = ???1, 1000, Computed = ???1, Exact = 0??1)
264+
LHS.One = APInt(4, 0b0001);
265+
RHS.One = APInt(4, 0b1000);
266+
LHS.Zero = APInt(4, 0b0000);
267+
RHS.Zero = APInt(4, 0b0111);
268+
Res = KnownBits::absdiff(LHS, RHS);
269+
EXPECT_EQ(0b0001, Res.One.getZExtValue());
270+
EXPECT_EQ(0b0000, Res.Zero.getZExtValue());
271+
}
272+
247273
TEST(KnownBitsTest, BinaryExhaustive) {
248274
testBinaryOpExhaustive(
249275
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -281,7 +307,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
281307
return KnownBits::smin(Known1, Known2);
282308
},
283309
[](const APInt &N1, const APInt &N2) { return APIntOps::smin(N1, N2); });
284-
310+
testBinaryOpExhaustive(
311+
[](const KnownBits &Known1, const KnownBits &Known2) {
312+
return KnownBits::absdiff(Known1, Known2);
313+
},
314+
[](const APInt &N1, const APInt &N2) {
315+
return APIntOps::absdiff(N1, N2);
316+
},
317+
checkCorrectnessOnlyBinary);
285318
testBinaryOpExhaustive(
286319
[](const KnownBits &Known1, const KnownBits &Known2) {
287320
return KnownBits::udiv(Known1, Known2);

0 commit comments

Comments
 (0)