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

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 07:22:57 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);
+  }
----------------
RKSimon wrote:

It seems to help (in that the checkOptimalityBinary error messages seem to change).

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


More information about the llvm-commits mailing list