[llvm] [DAG] SimplifyDemandedBits - ICMP_SLT(X, 0) - only sign mask of X is required (PR #164946)
Anikesh Parashar via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 25 05:51:48 PDT 2025
================
@@ -1754,24 +1754,31 @@ bool TargetLowering::SimplifyDemandedBits(
SDValue Op0 = Op.getOperand(0);
SDValue Op1 = Op.getOperand(1);
ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
- // If (1) we only need the sign-bit, (2) the setcc operands are the same
- // width as the setcc result, and (3) the result of a setcc conforms to 0 or
- // -1, we may be able to bypass the setcc.
- if (DemandedBits.isSignMask() &&
- Op0.getScalarValueSizeInBits() == BitWidth &&
- getBooleanContents(Op0.getValueType()) ==
- BooleanContent::ZeroOrNegativeOneBooleanContent) {
- // If we're testing X < 0, then this compare isn't needed - just use X!
- // FIXME: We're limiting to integer types here, but this should also work
- // if we don't care about FP signed-zero. The use of SETLT with FP means
- // that we don't care about NaNs.
- if (CC == ISD::SETLT && Op1.getValueType().isInteger() &&
- (isNullConstant(Op1) || ISD::isBuildVectorAllZeros(Op1.getNode())))
- return TLO.CombineTo(Op, Op0);
-
- // TODO: Should we check for other forms of sign-bit comparisons?
- // Examples: X <= -1, X >= 0
- }
+ // If we're testing X < 0 or X >= 0 then we only need the sign mask of the
+ // previous result
+ // FIXME: We're limiting to integer types here, but this should also work
+ // if we don't care about FP signed-zero. The use of SETLT with FP means
+ // that we don't care about NaNs.
+ if ((CC == ISD::SETLT || CC == ISD::SETGE) &&
+ Op1.getValueType().isInteger() && isNullOrNullSplat(Op1)) {
+ KnownBits KnownOp0;
+ bool Changed = false;
+ if (SimplifyDemandedBits(
+ Op0, APInt::getSignMask(Op0.getScalarValueSizeInBits()),
+ DemandedElts, KnownOp0, TLO, Depth + 1))
+ Changed = true;
+ // If (1) we only need the sign-bit, (2) the setcc operands are the same
+ // width as the setcc result, and (3) the result of a setcc conforms to 0
+ // or -1, we may be able to bypass the setcc.
+ if (DemandedBits.isSignMask() &&
+ Op0.getScalarValueSizeInBits() == BitWidth &&
+ getBooleanContents(Op0.getValueType()) ==
+ BooleanContent::ZeroOrNegativeOneBooleanContent)
+ Changed |= TLO.CombineTo(Op, Op0);
----------------
an1k3sh wrote:
Updated the code to add a NOT in this case
https://github.com/llvm/llvm-project/pull/164946
More information about the llvm-commits
mailing list