Skip to content

Commit dd03d7b

Browse files
committed
[SelectionDAG] Add more cases for UDIV and SDIV
Ported from ValueTracking.
1 parent aea7403 commit dd03d7b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5857,14 +5857,31 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
58575857
return true;
58585858
break;
58595859
}
5860-
case ISD::UDIV:
5861-
case ISD::SDIV:
5860+
case ISD::UDIV: {
58625861
// div exact can only produce a zero if the dividend is zero.
5863-
// TODO: For udiv this is also true if Op1 u<= Op0
58645862
if (Op->getFlags().hasExact())
58655863
return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5866-
break;
58675864

5865+
// If Op0 >= Op1, then the result is at least 1, and therefore not 0.
5866+
KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5867+
KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5868+
if (Op0.isStrictlyPositive() && Op1.isStrictlyPositive() &&
5869+
KnownBits::uge(Op0, Op1).value_or(false))
5870+
return true;
5871+
}
5872+
case ISD::SDIV: {
5873+
// div exact can only produce a zero if the dividend is zero.
5874+
if (Op->getFlags().hasExact())
5875+
return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5876+
5877+
KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5878+
KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5879+
Op0 = Op0.abs(/*IntMinIsPoison*/ false);
5880+
Op1 = Op1.abs(/*IntMinIsPoison*/ false);
5881+
if (Op0.isStrictlyPositive() && Op1.isStrictlyPositive() &&
5882+
KnownBits::uge(Op0, Op1).value_or(false))
5883+
return true;
5884+
}
58685885
case ISD::ADD:
58695886
if (Op->getFlags().hasNoUnsignedWrap())
58705887
if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||

0 commit comments

Comments
 (0)