[llvm] [SelectionDAG] Fold (icmp eq/ne (shift X, C), 0) -> (icmp eq/ne X, 0) (PR #88801)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 05:27:43 PDT 2024
================
@@ -4516,6 +4516,39 @@ SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
}
}
}
+
+ // Optimize
+ // (setcc (shift N00, N01C), 0, eq/ne) -> (setcc N00, 0, eq/ne)
+ // If all shifted out bits are known to be zero, then the zero'd ness
+ // doesn't change and we can omit the shift.
+ // If all shifted out bits are equal to at least one bit that isn't
+ // shifted out, then the zero'd ness doesn't change and we can omit the
+ // shift.
+ if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && C1.isZero() &&
+ N0.hasOneUse() &&
+ (N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
+ N0.getOpcode() == ISD::SRA)) {
+ bool IsRightShift = N0.getOpcode() != ISD::SHL;
+ SDValue N00 = N0.getOperand(0);
+ // Quick checks based on exact/nuw/nsw flags.
+ if (IsRightShift ? N0->getFlags().hasExact()
+ : (N0->getFlags().hasNoUnsignedWrap() ||
+ N0->getFlags().hasNoSignedWrap()))
+ return DAG.getSetCC(dl, VT, N00, N1, Cond);
+ // More expensive checks based on known bits.
+ APInt DemandedElts = VT.isFixedLengthVector()
+ ? APInt::getAllOnes(VT.getVectorMinNumElements())
+ : APInt(1, 1);
+ if (const APInt *ShAmt =
+ DAG.getValidMaximumShiftAmountConstant(N0, DemandedElts)) {
----------------
RKSimon wrote:
I've pushed 9f2a068bffad4a36db088673210f680bfd08b3d1 which should allow you to just call `getValidMaximumShiftAmountConstant(N0)`
https://github.com/llvm/llvm-project/pull/88801
More information about the llvm-commits
mailing list