[PATCH] D149202: [ValueTracking] Add logic for `(sub x, y) != 0` if we know `KnownX != KnownY`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 14:30:20 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, spatel, StephenFan.
Herald added a subscriber: hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Alive2 Link:

  https://alive2.llvm.org/ce/z/TAFcjF


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149202

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Analysis/ValueTracking/known-non-zero.ll


Index: llvm/test/Analysis/ValueTracking/known-non-zero.ll
===================================================================
--- llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -442,12 +442,7 @@
 
 define i1 @sub_nonzero_ops_ne(i8 %xx, i8 %yy, i8 %z) {
 ; CHECK-LABEL: @sub_nonzero_ops_ne(
-; CHECK-NEXT:    [[X:%.*]] = and i8 [[XX:%.*]], -65
-; CHECK-NEXT:    [[Y:%.*]] = or i8 [[YY:%.*]], 64
-; CHECK-NEXT:    [[S:%.*]] = sub i8 [[X]], [[Y]]
-; CHECK-NEXT:    [[EXP:%.*]] = or i8 [[Z:%.*]], [[S]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[EXP]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %x = and i8 %xx, 191
   %y = or i8 %yy, 64
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2705,12 +2705,25 @@
             Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
       return isKnownNonZero(I->getOperand(0), Depth, Q);
     break;
-  case Instruction::Sub:
+  case Instruction::Sub: {
     if (auto *C = dyn_cast<Constant>(I->getOperand(0)))
       if (C->isNullValue() &&
           isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
         return true;
+
+    KnownBits XKnown =
+        computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
+    if (!XKnown.isUnknown()) {
+      KnownBits YKnown =
+          computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
+      // If X != Y then X - Y is non zero.
+      std::optional<bool> ne = KnownBits::ne(XKnown, YKnown);
+      // If we are unable to compute if X != Y, we won't be able to do anything
+      // computing the knownbits of the sub expression so just return here.
+      return ne && *ne;
+    }
     break;
+  }
   case Instruction::Or:
     // X | Y != 0 if X != 0 or Y != 0.
     return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149202.516919.patch
Type: text/x-patch
Size: 1984 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/0cf50f2a/attachment.bin>


More information about the llvm-commits mailing list