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

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 28 09:35:32 PST 2024


================
@@ -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");
----------------
goldsteinn wrote:

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

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


More information about the llvm-commits mailing list