@@ -5871,6 +5871,55 @@ bool SelectionDAG::isKnownNeverZeroFloat(SDValue Op) const {
58715871 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
58725872}
58735873
5874+ static bool isNonZeroShift(const SelectionDAG &DAG, const SDValue I,
5875+ const KnownBits &KnownVal, unsigned Depth) {
5876+ auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
5877+ switch (I.getOpcode()) {
5878+ case ISD::SHL:
5879+ return Lhs.shl(Rhs);
5880+ case ISD::SRL:
5881+ return Lhs.lshr(Rhs);
5882+ case ISD::SRA:
5883+ return Lhs.ashr(Rhs);
5884+ default:
5885+ llvm_unreachable("Unknown Shift Opcode");
5886+ }
5887+ };
5888+
5889+ auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
5890+ switch (I.getOpcode()) {
5891+ case ISD::SHL:
5892+ return Lhs.lshr(Rhs);
5893+ case ISD::SRA:
5894+ case ISD::SRL:
5895+ return Lhs.shl(Rhs);
5896+ default:
5897+ llvm_unreachable("Unknown Shift Opcode");
5898+ }
5899+ };
5900+
5901+ if (KnownVal.isUnknown())
5902+ return false;
5903+
5904+ KnownBits KnownCnt = DAG.computeKnownBits(I.getOperand(1), Depth + 1);
5905+ APInt MaxShift = KnownCnt.getMaxValue();
5906+ unsigned NumBits = KnownVal.getBitWidth();
5907+ if (MaxShift.uge(NumBits))
5908+ return false;
5909+
5910+ if (!ShiftOp(KnownVal.One, MaxShift).isZero())
5911+ return true;
5912+
5913+ // If all of the bits shifted out are known to be zero, and Val is known
5914+ // non-zero then at least one non-zero bit must remain.
5915+ if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
5916+ .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
5917+ DAG.isKnownNeverZero(I.getOperand(0), Depth + 1))
5918+ return true;
5919+
5920+ return false;
5921+ }
5922+
58745923bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
58755924 if (Depth >= MaxRecursionDepth)
58765925 return false; // Limit search depth.
@@ -5906,9 +5955,7 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
59065955 if (ValKnown.One[0])
59075956 return true;
59085957 // If max shift cnt of known ones is non-zero, result is non-zero.
5909- APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5910- if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5911- !ValKnown.One.shl(MaxCnt).isZero())
5958+ if (isNonZeroShift(*this, Op, ValKnown, Depth))
59125959 return true;
59135960 break;
59145961 }
@@ -5968,10 +6015,8 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
59686015 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
59696016 if (ValKnown.isNegative())
59706017 return true;
5971- // If max shift cnt of known ones is non-zero, result is non-zero.
5972- APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5973- if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5974- !ValKnown.One.lshr(MaxCnt).isZero())
6018+
6019+ if (isNonZeroShift(*this, Op, ValKnown, Depth))
59756020 return true;
59766021 break;
59776022 }
0 commit comments