Skip to content
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
23 changes: 20 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5857,14 +5857,31 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
return true;
break;
}
case ISD::UDIV:
case ISD::SDIV:
case ISD::UDIV: {
// div exact can only produce a zero if the dividend is zero.
// TODO: For udiv this is also true if Op1 u<= Op0
if (Op->getFlags().hasExact())
return isKnownNeverZero(Op.getOperand(0), Depth + 1);

// If Op0 >= Op1, then the result is at least 1, and therefore not 0.
KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
if (Op0.isStrictlyPositive() && Op1.isStrictlyPositive() &&
KnownBits::uge(Op0, Op1).value_or(false))
return true;
break;
}
case ISD::SDIV: {
// div exact can only produce a zero if the dividend is zero.
if (Op->getFlags().hasExact())
return isKnownNeverZero(Op.getOperand(0), Depth + 1);

KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
if (Op0.isStrictlyPositive() && Op1.isStrictlyPositive() &&
KnownBits::uge(Op0, Op1).value_or(false))
return true;
break;
}
case ISD::ADD:
if (Op->getFlags().hasNoUnsignedWrap())
if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
Expand Down
Loading
Loading