[PATCH] D149204: [ValueTracking] Add logic for `add nuw x, y != 0` -> `x != 0 || y != 0`
Noah Goldstein via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 25 14:31:07 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/TKpqxc
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D149204
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
@@ -473,9 +473,7 @@
; CHECK-LABEL: @add_nonzero_nuw(
; CHECK-NEXT: [[X_NZ:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[X_NZ]])
-; CHECK-NEXT: [[A:%.*]] = add nuw i8 [[X]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[A]], 0
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%x_nz = icmp ne i8 %x, 0
call void @llvm.assume(i1 %x_nz)
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2783,6 +2783,15 @@
break;
case Instruction::Add: {
// X + Y.
+
+ // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
+ // non-zero.
+ auto *BO = cast<OverflowingBinaryOperator>(V);
+ if (Q.IIQ.hasNoUnsignedWrap(BO))
+ if (isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||
+ isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
+ return true;
+
KnownBits XKnown =
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
KnownBits YKnown =
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149204.516921.patch
Type: text/x-patch
Size: 1380 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/52ff7a30/attachment.bin>
More information about the llvm-commits
mailing list