[PATCH] D149203: [ValueTracking] Add logic for `udiv x, y != 0` iff `y u<= x` -> `x != 0`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 14:30:50 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/HBEJZv


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149203

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
@@ -501,12 +501,7 @@
 
 define i1 @udiv_y_le_x(i8 %xx, i8 %yy, i8 %z) {
 ; CHECK-LABEL: @udiv_y_le_x(
-; CHECK-NEXT:    [[X:%.*]] = or i8 [[XX:%.*]], 7
-; CHECK-NEXT:    [[Y:%.*]] = and i8 [[YY:%.*]], 7
-; CHECK-NEXT:    [[D:%.*]] = udiv i8 [[X]], [[Y]]
-; CHECK-NEXT:    [[O:%.*]] = or i8 [[D]], [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[O]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %x = or i8 %xx, 7
   %y = and i8 %yy, 7
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2769,6 +2769,17 @@
     // div exact can only produce a zero if the dividend is zero.
     if (cast<PossiblyExactOperator>(I)->isExact())
       return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
+    if (I->getOpcode() == Instruction::UDiv) {
+      KnownBits XKnown =
+          computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
+      KnownBits YKnown =
+          computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
+      std::optional<bool> YUleX = KnownBits::ule(YKnown, XKnown);
+
+      // If Y u<= X then div is only zero if X is zero.
+      if (YUleX && *YUleX)
+        return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
+    }
     break;
   case Instruction::Add: {
     // X + Y.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149203.516920.patch
Type: text/x-patch
Size: 1638 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/c831de21/attachment.bin>


More information about the llvm-commits mailing list