[llvm] [DAG] SimplifyDemandedBits - ICMP_SLT(X, 0) - only sign mask of X is required (PR #164946)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 25 09:03:34 PDT 2025


================
@@ -1754,24 +1754,45 @@ 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, X >= 0, X <= -1 (X is of integer type) or X > -1
+    // (X is of integer type) then we only need the sign mask of the previous
+    // result
+    // FIXME: We're limiting to integer types for X < 0 or X >= 0 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)) ||
+        ((CC == ISD::SETLE || CC == ISD::SETGT) &&
+         Op1.getValueType().isInteger() && isAllOnesOrAllOnesSplat(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) {
+        // If we remove a >= 0 or > -1 (for integers), we need to introduce a
+        // NOT Operation
+        if (CC == ISD::SETGE || CC == ISD::SETGT) {
+          SDLoc DL(Op);
+          EVT VT = Op0.getValueType();
+          SDValue NotOp0 = TLO.DAG.getNode(ISD::XOR, DL, VT, Op0,
----------------
topperc wrote:

DAG.getNOT

https://github.com/llvm/llvm-project/pull/164946


More information about the llvm-commits mailing list