[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
Wed Apr 26 15:10:54 PDT 2023
goldstein.w.n updated this revision to Diff 517350.
goldstein.w.n added a comment.
Rebase
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D149202/new/
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;
- break;
+
+ 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;
+ }
+ return false;
+ }
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.517350.patch
Type: text/x-patch
Size: 2003 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230426/1b04ddcb/attachment.bin>
More information about the llvm-commits
mailing list