[PATCH] D157302: [ValueTracking] Compute `sdiv` as non-zero if `abs(num) u>= abs(denum)`
Noah Goldstein via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 7 10:46:36 PDT 2023
goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, Allen.
Herald added subscribers: StephenFan, 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.
Just covering an additional case.
Proof: https://alive2.llvm.org/ce/z/MJz9fT
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D157302
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
@@ -1125,11 +1125,7 @@
define i1 @sdiv_known_non_zero(i8 %x, i8 %y) {
; CHECK-LABEL: @sdiv_known_non_zero(
-; CHECK-NEXT: [[XX0:%.*]] = or i8 [[X:%.*]], -121
-; CHECK-NEXT: [[XX:%.*]] = and i8 [[XX0]], -2
-; CHECK-NEXT: [[XY:%.*]] = sdiv i8 [[XX]], -2
-; CHECK-NEXT: [[NZ:%.*]] = icmp ne i8 [[XY]], 0
-; CHECK-NEXT: ret i1 [[NZ]]
+; CHECK-NEXT: ret i1 true
;
%xx0 = or i8 %x, 135
%xx = and i8 %xx0, -2
@@ -1140,12 +1136,7 @@
define i1 @sdiv_known_non_zero2(i8 %x, i8 %y) {
; CHECK-LABEL: @sdiv_known_non_zero2(
-; CHECK-NEXT: [[XX0:%.*]] = or i8 [[X:%.*]], 15
-; CHECK-NEXT: [[XX:%.*]] = and i8 [[XX0]], -4
-; CHECK-NEXT: [[YY:%.*]] = and i8 [[Y:%.*]], 3
-; CHECK-NEXT: [[XY:%.*]] = sdiv i8 [[XX]], [[YY]]
-; CHECK-NEXT: [[NZ:%.*]] = icmp ne i8 [[XY]], 0
-; CHECK-NEXT: ret i1 [[NZ]]
+; CHECK-NEXT: ret i1 true
;
%xx0 = or i8 %x, 15
%xx = and i8 %xx0, -4
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2557,26 +2557,32 @@
return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
}
case Instruction::UDiv:
- case Instruction::SDiv:
+ case Instruction::SDiv: {
// X / Y
// 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) {
- std::optional<bool> XUgeY;
- KnownBits XKnown =
- computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
- if (!XKnown.isUnknown()) {
- KnownBits YKnown =
- computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
- // If X u>= Y then div is non zero (0/0 is UB).
- XUgeY = KnownBits::uge(XKnown, YKnown);
+
+ std::optional<bool> XUgeY;
+ KnownBits XKnown =
+ computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
+ // If X is fully unknown we won't be able to figure anything out so don't
+ // both compuring knownbits for Y.
+ if (!XKnown.isUnknown()) {
+ KnownBits YKnown =
+ computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
+ if (I->getOpcode() == Instruction::SDiv) {
+ // For signed division need to compare abs value of the operands.
+ XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
+ YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
}
- // If X is total unknown or X u< Y we won't be able to prove non-zero
- // with compute known bits so just return early.
- return XUgeY && *XUgeY;
+ // If X u>= Y then div is non zero (0/0 is UB).
+ XUgeY = KnownBits::uge(XKnown, YKnown);
}
- break;
+ // If X is total unknown or X u< Y we won't be able to prove non-zero
+ // with compute known bits so just return early.
+ return XUgeY && *XUgeY;
+ }
case Instruction::Add: {
// X + Y.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157302.547848.patch
Type: text/x-patch
Size: 3256 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230807/819232ea/attachment.bin>
More information about the llvm-commits
mailing list