diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/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(I->getOperand(0))) if (C->isNullValue() && isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q)) return true; + + 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 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; + } break; + } case Instruction::Or: // X | Y != 0 if X != 0 or Y != 0. return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) || diff --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll --- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll +++ b/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