[llvm] [GlobalISel] Add G_SUB for computeNumSignBits (PR #158384)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 2 04:00:58 PDT 2025


================
@@ -1959,6 +1959,41 @@ unsigned GISelValueTracking::computeNumSignBits(Register R,
 
     break;
   }
+  case TargetOpcode::G_SUB: {
+    Register Src2 = MI.getOperand(2).getReg();
+    unsigned Src2NumSignBits =
+        computeNumSignBits(Src2, DemandedElts, Depth + 1);
+    if (Src2NumSignBits == 1)
+      return 1; // Early out.
+
+    // Handle NEG.
+    Register Src1 = MI.getOperand(1).getReg();
+    KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
+    if (Known1.isZero()) {
+      KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
+      // If the input is known to be 0 or 1, the output is 0/-1, which is all
+      // sign bits set.
+      if ((Known2.Zero | 1).isAllOnes())
+        return TyBits;
+
+      // If the input is known to be positive (the sign bit is known clear),
+      // the output of the NEG has the same number of sign bits as the input.
----------------
jayfoad wrote:

```suggestion
      // the output of the NEG has at least as many sign bits as the input.
```

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


More information about the llvm-commits mailing list