Skip to content
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

[KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values #82354

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented Feb 20, 2024

Equivalent to "umax(A, B) - umin(A, B)"

This is just an initial correctness implementation, hopefully we can make this optimal in the future.

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 20, 2024

@llvm/pr-subscribers-llvm-support

Author: Simon Pilgrim (RKSimon)

Changes

Equivalent to "umax(A, B) - umin(A, B)"

This is just an initial correctness implementation, hopefully we can make this optimal in the future.


Full diff: https://github.com/llvm/llvm-project/pull/82354.diff

3 Files Affected:

  • (modified) llvm/include/llvm/Support/KnownBits.h (+3)
  • (modified) llvm/lib/Support/KnownBits.cpp (+17)
  • (modified) llvm/unittests/Support/KnownBitsTest.cpp (+8-1)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index fb034e0b9e3baf..69c569b97ccae1 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -385,6 +385,9 @@ struct KnownBits {
   /// Compute known bits for smin(LHS, RHS).
   static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
 
+  /// Compute known bits for absdiff(LHS, RHS).
+  static KnownBits absdiff(const KnownBits &LHS, const KnownBits &RHS);
+
   /// Compute known bits for shl(LHS, RHS).
   /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
   static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS,
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 770e4051ca3ffa..e0ecdd56711959 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -176,6 +176,23 @@ KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
   return Flip(umax(Flip(LHS), Flip(RHS)));
 }
 
+KnownBits KnownBits::absdiff(const KnownBits &LHS, const KnownBits &RHS) {
+  // absdiff(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
+  KnownBits UMaxValue = umax(LHS, RHS);
+  KnownBits UMinValue = umin(LHS, RHS);
+  KnownBits KnownAbsDiff = computeForAddSub(false, false, UMaxValue, UMinValue);
+
+  // fallback - find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
+  if (KnownAbsDiff.isUnknown()) {
+    KnownBits Diff0 = computeForAddSub(false, false, LHS, RHS);
+    KnownBits Diff1 = computeForAddSub(false, false, RHS, LHS);
+    KnownAbsDiff = Diff0.intersectWith(Diff1);
+  }
+
+  assert(!KnownAbsDiff.hasConflict() && "Bad Output");
+  return KnownAbsDiff;
+}
+
 static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth) {
   if (isPowerOf2_32(BitWidth))
     return MaxValue.extractBitsAsZExtValue(Log2_32(BitWidth), 0);
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index c0377d45c303a1..dfa3fe06a32647 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -281,7 +281,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
         return KnownBits::smin(Known1, Known2);
       },
       [](const APInt &N1, const APInt &N2) { return APIntOps::smin(N1, N2); });
-
+  testBinaryOpExhaustive(
+      [](const KnownBits &Known1, const KnownBits &Known2) {
+        return KnownBits::absdiff(Known1, Known2);
+      },
+      [](const APInt &N1, const APInt &N2) {
+        return APIntOps::absdiff(N1, N2);
+      },
+      checkCorrectnessOnlyBinary);
   testBinaryOpExhaustive(
       [](const KnownBits &Known1, const KnownBits &Known2) {
         return KnownBits::udiv(Known1, Known2);

KnownAbsDiff = Diff0.intersectWith(Diff1);
}

assert(!KnownAbsDiff.hasConflict() && "Bad Output");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you can get away with special casing high bits. Basically all high bits min(LHS.numSignBits(), RHS.numSignBits()) will always be zero.

edit: thats not right! The 1s/0s need to match. So a bit more complicate but like:

KnownBits Common = commonBits(LHS, RHS);
unsigned NumZerodHiBits = Common.countMinSignBits();
...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to work on "checkOptimalityBinary" cases in a future patch - the handling of leading bits still needs a bit of work - I think adding NUW handling to KnownBits::computeForAddSub might be the way to go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, please leave it as a todo then. I agree it makes sense to add nuw flag in computeForAddSub

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im working on a patch to support NUW for add/sub

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: PR #83382

… 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.
@RKSimon
Copy link
Collaborator Author

RKSimon commented Mar 1, 2024

ping?

Res = KnownBits::absdiff(LHS, RHS);
EXPECT_EQ(0b0001, Res.One.getZExtValue());
EXPECT_EQ(0b0000, Res.Zero.getZExtValue());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test provide anything that the exhaustive test doing already cover?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was requested by @nikic - if you disable either of the computations then one of the tests will fail

@goldsteinn
Copy link
Contributor

LGTM.

@RKSimon RKSimon merged commit fcdf818 into llvm:main Mar 1, 2024
3 of 4 checks passed
@RKSimon RKSimon deleted the knownbits-absdiff branch March 1, 2024 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants