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 @@ -2772,6 +2772,14 @@ 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(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 --- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll +++ b/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)