[llvm] 75b48b4 - [ValueTracking] Add logic for `add nuw x, y != 0` -> `x != 0 || y != 0`
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 26 21:48:56 PDT 2023
Author: Noah Goldstein
Date: 2023-04-26T23:48:20-05:00
New Revision: 75b48b40771ae8124f8624fff8f1fb422a5d1fc7
URL: https://github.com/llvm/llvm-project/commit/75b48b40771ae8124f8624fff8f1fb422a5d1fc7
DIFF: https://github.com/llvm/llvm-project/commit/75b48b40771ae8124f8624fff8f1fb422a5d1fc7.diff
LOG: [ValueTracking] Add logic for `add nuw x,y != 0` -> `x != 0 || y != 0`
Alive2 Link:
https://alive2.llvm.org/ce/z/TKpqxc
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D149204
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Analysis/ValueTracking/known-non-zero.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 76a9ef96b2b04..35f5bb7b51a01 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2772,6 +2772,14 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
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))
+ return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||
+ isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);
+
KnownBits XKnown =
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
KnownBits YKnown =
diff --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index a0a6f2e61b1fe..d913c6be5e36e 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -473,9 +473,7 @@ define i1 @add_nonzero_nuw(i8 %x, i8 %y) {
; 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)
More information about the llvm-commits
mailing list