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 @@ -2887,6 +2887,17 @@ return XKnown.isNonZero() || isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); + // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is + // non-zero, then X * Y is non-zero. We can find sX and sY by just taking + // the the lowest known One of X and Y. If they are non-zero, the result + // must be non-zero. + if (XKnown.isNonZero() && YKnown.isNonZero()) { + APInt XLowBit = XKnown.One & (-XKnown.One); + APInt YLowBit = YKnown.One & (-YKnown.One); + if (!(XLowBit * YLowBit).isZero()) + return true; + } + return KnownBits::mul(XKnown, YKnown).isNonZero(); } case Instruction::Select: 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 @@ -1099,11 +1099,7 @@ define i1 @mul_nonzero_contains_nonzero_mul(i8 %x, i8 %y) { ; CHECK-LABEL: @mul_nonzero_contains_nonzero_mul( -; CHECK-NEXT: [[XX:%.*]] = or i8 [[X:%.*]], 16 -; CHECK-NEXT: [[YY:%.*]] = or i8 [[X]], 8 -; CHECK-NEXT: [[XY:%.*]] = mul i8 [[XX]], [[YY]] -; CHECK-NEXT: [[NZ:%.*]] = icmp ne i8 [[XY]], 0 -; CHECK-NEXT: ret i1 [[NZ]] +; CHECK-NEXT: ret i1 true ; %xx = or i8 %x, 16 %yy = or i8 %x, 8