Skip to content

[ValueTracking] Add support for overflow detection functions is isKnownNonZero #87701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,34 @@ static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
return ::isKnownNonEqual(X, Y, Depth, Q);
}

static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth,
const SimplifyQuery &Q, unsigned BitWidth, Value *X,
Value *Y, bool NSW, bool NUW) {
// If X and Y are non-zero then so is X * Y as long as the multiplication
// does not overflow.
if (NSW || NUW)
return isKnownNonZero(X, DemandedElts, Depth, Q) &&
isKnownNonZero(Y, DemandedElts, Depth, Q);

// If either X or Y is odd, then if the other is non-zero the result can't
// be zero.
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
if (XKnown.One[0])
return isKnownNonZero(Y, DemandedElts, Depth, Q);

KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
if (YKnown.One[0])
return XKnown.isNonZero() || isKnownNonZero(X, 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 lowest known One of X and Y. If they are non-zero, the result
// must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
// X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
BitWidth;
}

static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
unsigned Depth, const SimplifyQuery &Q,
const KnownBits &KnownVal) {
Expand Down Expand Up @@ -2658,33 +2686,10 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
Q.IIQ.hasNoUnsignedWrap(BO));
}
case Instruction::Mul: {
// If X and Y are non-zero then so is X * Y as long as the multiplication
// does not overflow.
const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
if (Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO))
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) &&
isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);

// If either X or Y is odd, then if the other is non-zero the result can't
// be zero.
KnownBits XKnown =
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
if (XKnown.One[0])
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);

KnownBits YKnown =
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
if (YKnown.One[0])
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 lowest known One of X and Y. If they are non-zero, the result
// must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
// X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
BitWidth;
return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
Q.IIQ.hasNoUnsignedWrap(BO));
}
case Instruction::Select: {
// (C ? X : Y) != 0 if X != 0 and Y != 0.
Expand Down Expand Up @@ -2785,6 +2790,29 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
// handled in isKnownNonZero.
return false;
}
case Instruction::ExtractValue: {
const WithOverflowInst *WO;
if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
switch (WO->getBinaryOp()) {
default:
break;
case Instruction::Add:
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
WO->getArgOperand(0), WO->getArgOperand(1),
/*NSW=*/false,
/*NUW=*/false);
case Instruction::Sub:
return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
WO->getArgOperand(0), WO->getArgOperand(1));
case Instruction::Mul:
return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
WO->getArgOperand(0), WO->getArgOperand(1),
/*NSW=*/false, /*NUW=*/false);
break;
}
}
break;
}
case Instruction::Call:
case Instruction::Invoke: {
const auto *Call = cast<CallBase>(I);
Expand Down
Loading