Skip to content

[SelectionDAG]: Deduce KnownNeverZero from SMIN and SMAX #85722

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

Merged
merged 2 commits into from
Mar 25, 2024
Merged
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
40 changes: 37 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5362,10 +5362,44 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
isKnownNeverZero(Op.getOperand(0), Depth + 1);

// TODO for smin/smax: If either operand is known negative/positive
// For smin/smax: If either operand is known negative/positive
// respectively we don't need the other to be known at all.
case ISD::SMAX:
case ISD::SMIN:
case ISD::SMAX: {
KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
if (Op1.isStrictlyPositive())
return true;

KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
if (Op0.isStrictlyPositive())
return true;

if (Op1.isNonZero() && Op0.isNonZero())
return true;

if (KnownBits::smax(Op0, Op1).isNonZero())
return true;

return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
isKnownNeverZero(Op.getOperand(0), Depth + 1);
}
case ISD::SMIN: {
KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
if (Op1.isNegative())
return true;

KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
if (Op0.isNegative())
return true;

if (Op1.isNonZero() && Op0.isNonZero())
return true;

if (KnownBits::smin(Op0, Op1).isNonZero())
return true;

return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
isKnownNeverZero(Op.getOperand(0), Depth + 1);
}
case ISD::UMIN:
return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
isKnownNeverZero(Op.getOperand(0), Depth + 1);
Expand Down
Loading