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

Yatao Wang via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 16 11:42:26 PDT 2025


================
@@ -1976,6 +1976,42 @@ unsigned GISelValueTracking::computeNumSignBits(Register R,
 
     break;
   }
+  case TargetOpcode::G_SUB: {
+    Register Src1 = MI.getOperand(1).getReg();
+    unsigned Src1NumSignBits =
+        computeNumSignBits(Src1, DemandedElts, Depth + 1);
+    if (Src1NumSignBits == 1)
+      return 1; // Early Out.
----------------
ningxinr wrote:

On a second thought, I think it might be better that we move both the early out checks before we handle the neg case because they seem to be cheaper checks. I changed both `SelectionDAG` and `GlobalISel`, so that they match each other. So far the change passes `check-all` and the test cases I added. 

Before:
If number of sign bits of RHS = 1, return 1;

If LHS is zero,
    If RHS is zero or one, return width
    If RHS is positive (we already checked zero case in the previous if condition), return sign bits of RHS
      
If number of sign bits of LHS = 1, return 1;
Return minimum of sign bits of LHS and that of RHS minus 1;

After:
If number of sign bits of LHS = 1, return 1;
If number of sign bits of RHS = 1, return 1;

If LHS is zero,
    If RHS is zero or one, return width
    If RHS is positive (we already checked zero case in the previous if condition), return sign bits of RHS
  
Return minimum of sign bits of LHS and that of RHS minus 1;

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


More information about the llvm-commits mailing list