@@ -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);
5864+
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;
58665871 break;
5872+ }
5873+ case ISD::SDIV: {
5874+ // div exact can only produce a zero if the dividend is zero.
5875+ if (Op->getFlags().hasExact())
5876+ return isKnownNeverZero(Op.getOperand(0), Depth + 1);
58675877
5878+ KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5879+ KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5880+ if (Op0.isStrictlyPositive() && Op1.isStrictlyPositive() &&
5881+ KnownBits::uge(Op0, Op1).value_or(false))
5882+ return true;
5883+ break;
5884+ }
58685885 case ISD::ADD:
58695886 if (Op->getFlags().hasNoUnsignedWrap())
58705887 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
0 commit comments