Skip to content

Conversation

@yichi170
Copy link
Contributor

@yichi170 yichi170 commented Aug 5, 2025

This patch ports the ISD::SHL handling from SelectionDAG’s ComputeNumSignBits to GlobalISel.

Related to #150515.

@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2025

@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-backend-aarch64

Author: Yi-Chi Lee (yichi170)

Changes

This patch ports the ISD::SHL handling from SelectionDAG’s ComputeNumSignBits to GlobalISel.

Related to #150515.


Full diff: https://github.com/llvm/llvm-project/pull/152067.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp (+33)
  • (added) llvm/test/CodeGen/AArch64/GlobalISel/knownbits-shl.mir (+110)
diff --git a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
index 974fc40de6222..b27aa475e13d4 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
@@ -1892,6 +1892,38 @@ unsigned GISelValueTracking::computeNumSignBits(Register R,
       FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
     break;
   }
+  case TargetOpcode::G_SHL: {
+    Register Src1 = MI.getOperand(1).getReg();
+    Register Src2 = MI.getOperand(2).getReg();
+    if (std::optional<ConstantRange> ShAmtRange =
+            getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
+      uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
+      uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
+
+      MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
+      unsigned ExtOpc = ExtMI.getOpcode();
+
+      if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
+          ExtOpc == TargetOpcode::G_ANYEXT) {
+        LLT ExtTy = MRI.getType(Src1);
+        Register Extendee = ExtMI.getOperand(1).getReg();
+        LLT ExtendeeTy = MRI.getType(Extendee);
+        uint64_t SizeDiff =
+            ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
+
+        if (SizeDiff <= MinShAmt) {
+          unsigned Tmp =
+              SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
+          if (MaxShAmt < Tmp)
+            return Tmp - MaxShAmt;
+        }
+      }
+      unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
+      if (MaxShAmt < Tmp)
+        return Tmp - MaxShAmt;
+    }
+    break;
+  }
   case TargetOpcode::G_TRUNC: {
     Register Src = MI.getOperand(1).getReg();
     LLT SrcTy = MRI.getType(Src);
@@ -2051,6 +2083,7 @@ unsigned GISelValueTracking::computeNumSignBits(Register R,
   // Okay, we know that the sign bit in Mask is set.  Use CLO to determine
   // the number of identical bits in the top of the input value.
   Mask <<= Mask.getBitWidth() - TyBits;
+  LLVM_DEBUG(dbgs() << "Mask.countl_one(): " << Mask.countl_one() << "\n");
   return std::max(FirstAnswer, Mask.countl_one());
 }
 
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-shl.mir b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-shl.mir
new file mode 100644
index 0000000000000..9e9c1413259fa
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-shl.mir
@@ -0,0 +1,110 @@
+# NOTE: Assertions have been autogenerated by utils/update_givaluetracking_test_checks.py UTC_ARGS: --version 5
+# RUN: llc -mtriple aarch64 -passes="print<gisel-value-tracking>" %s -o - 2>&1 | FileCheck %s
+
+---
+name:            Cst
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @Cst
+  ; CHECK-NEXT: %0:_ KnownBits:11100000 SignBits:3
+  ; CHECK-NEXT: %1:_ KnownBits:00000010 SignBits:6
+  ; CHECK-NEXT: %2:_ KnownBits:10000000 SignBits:1
+    %0:_(s8) = G_CONSTANT i8 224
+    %1:_(s8) = G_CONSTANT i8 2
+    %2:_(s8) = G_SHL %0, %1
+...
+---
+name:            CstBig
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @CstBig
+  ; CHECK-NEXT: %0:_ KnownBits:11111001 SignBits:5
+  ; CHECK-NEXT: %1:_ KnownBits:00000110 SignBits:5
+  ; CHECK-NEXT: %2:_ KnownBits:01000000 SignBits:1
+    %0:_(s8) = G_CONSTANT i8 249
+    %1:_(s8) = G_CONSTANT i8 6
+    %2:_(s8) = G_SHL %0, %1
+...
+---
+name:            ScalarVar
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @ScalarVar
+  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:???????? SignBits:1
+  ; CHECK-NEXT: %2:_ KnownBits:???????? SignBits:1
+    %0:_(s8) = COPY $b0
+    %1:_(s8) = COPY $b1
+    %2:_(s8) = G_SHL %0, %1
+...
+---
+name:            ScalarCst
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @ScalarCst
+  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:00000011 SignBits:6
+  ; CHECK-NEXT: %2:_ KnownBits:?????000 SignBits:1
+    %0:_(s8) = COPY $b0
+    %1:_(s8) = G_CONSTANT i8 3
+    %2:_(s8) = G_SHL %0, %1
+...
+---
+name:            VectorVar
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @VectorVar
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:???????????????? SignBits:1
+  ; CHECK-NEXT: %2:_ KnownBits:???????????????? SignBits:1
+    %0:_(<4 x s16>) = COPY $d0
+    %1:_(<4 x s16>) = COPY $d1
+    %2:_(<4 x s16>) = G_SHL %0, %1
+...
+---
+name:            VectorCst
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @VectorCst
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:0000000000000011 SignBits:14
+  ; CHECK-NEXT: %2:_ KnownBits:0000000000000011 SignBits:14
+  ; CHECK-NEXT: %3:_ KnownBits:?????????????000 SignBits:1
+    %0:_(<4 x s16>) = COPY $d0
+    %1:_(s16) = G_CONSTANT i16 3
+    %2:_(<4 x s16>) = G_BUILD_VECTOR %1, %1, %1, %1
+    %3:_(<4 x s16>) = G_SHL %0, %2
+...
+---
+name:            VectorCst36
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @VectorCst36
+  ; CHECK-NEXT: %0:_ KnownBits:0000000000000011 SignBits:14
+  ; CHECK-NEXT: %1:_ KnownBits:0000000000000110 SignBits:13
+  ; CHECK-NEXT: %2:_ KnownBits:0000000000000?1? SignBits:13
+  ; CHECK-NEXT: %3:_ KnownBits:0000000000000?1? SignBits:13
+  ; CHECK-NEXT: %4:_ KnownBits:000000????????00 SignBits:7
+    %0:_(s16) = G_CONSTANT i16 3
+    %1:_(s16) = G_CONSTANT i16 6
+    %2:_(<4 x s16>) = G_BUILD_VECTOR %0, %1, %1, %0
+    %3:_(<4 x s16>) = G_BUILD_VECTOR %0, %1, %1, %0
+    %4:_(<4 x s16>) = G_SHL %2, %3
+...
+
+---
+name:            VectorCst3unknown
+body:             |
+  bb.1:
+  ; CHECK-LABEL: name: @VectorCst3unknown
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:???????????????? SignBits:1
+  ; CHECK-NEXT: %2:_ KnownBits:0000000000000011 SignBits:14
+  ; CHECK-NEXT: %3:_ KnownBits:???????????????? SignBits:1
+  ; CHECK-NEXT: %4:_ KnownBits:???????????????? SignBits:1
+    %0:_(<4 x s16>) = COPY $d0
+    %2:_(s16) = COPY $h0
+    %1:_(s16) = G_CONSTANT i16 3
+    %3:_(<4 x s16>) = G_BUILD_VECTOR %1, %2, %2, %1
+    %4:_(<4 x s16>) = G_SHL %0, %3
+...

Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests Look good - can we add these as some simple additions for when more sign bits are now know (and some edge cases).

---
name:            SHLless
body:             |
  bb.1:
  ; CHECK-LABEL: name: @SHLless
  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
  ; CHECK-NEXT: %1:_ KnownBits:???????????????? SignBits:9
  ; CHECK-NEXT: %2:_ KnownBits:0000000000000011 SignBits:14
  ; CHECK-NEXT: %3:_ KnownBits:?????????????000 SignBits:6
    %0:_(s8) = COPY $b0
    %1:_(s16) = G_SEXT %0(s8)
    %2:_(s16) = G_CONSTANT i16 3
    %3:_(s16) = G_SHL %1, %2
...
---
name:            SHLeq
body:             |
  bb.1:
  ; CHECK-LABEL: name: @SHLeq
  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
  ; CHECK-NEXT: %1:_ KnownBits:???????????????? SignBits:9
  ; CHECK-NEXT: %2:_ KnownBits:0000000000001000 SignBits:12
  ; CHECK-NEXT: %3:_ KnownBits:????????00000000 SignBits:1
    %0:_(s8) = COPY $b0
    %1:_(s16) = G_SEXT %0(s8)
    %2:_(s16) = G_CONSTANT i16 8
    %3:_(s16) = G_SHL %1, %2
...
---
name:            SHLmore
body:             |
  bb.1:
  ; CHECK-LABEL: name: @SHLmore
  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
  ; CHECK-NEXT: %1:_ KnownBits:???????????????? SignBits:9
  ; CHECK-NEXT: %2:_ KnownBits:0000000000001101 SignBits:12
  ; CHECK-NEXT: %3:_ KnownBits:???0000000000000 SignBits:1
    %0:_(s8) = COPY $b0
    %1:_(s16) = G_SEXT %0(s8)
    %2:_(s16) = G_CONSTANT i16 13
    %3:_(s16) = G_SHL %1, %2
...
---
name:            SignBitsThroughZext
body:             |
  bb.1:
  ; CHECK-LABEL: name: @SignBitsThroughZext
  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
  ; CHECK-NEXT: %1:_ KnownBits:0000000000000011 SignBits:14
  ; CHECK-NEXT: %2:_ KnownBits:???????? SignBits:4
  ; CHECK-NEXT: %3:_ KnownBits:00000000???????? SignBits:8
  ; CHECK-NEXT: %4:_ KnownBits:0000000000001000 SignBits:12
  ; CHECK-NEXT: %5:_ KnownBits:????????00000000 SignBits:4
    %0:_(s8) = COPY $b0
    %1:_(s16) = G_CONSTANT i16 3
    %2:_(s8) = G_ASHR %0, %1
    %3:_(s16) = G_ZEXT %2
    %4:_(s16) = G_CONSTANT i16 8
    %5:_(s16) = G_SHL %3, %4
...

Otherwise this LGTM - thanks.

@davemgreen
Copy link
Collaborator

@yichi170 did you get a chance to add a few extra test cases? This looked good to me otherwise and would be good to get it in.

@yichi170
Copy link
Contributor Author

@davemgreen Sorry for the late reply. I didn't notice your message about the additional tests. Will start working on it.

@yichi170
Copy link
Contributor Author

@davemgreen I updated the test! Let me know if there is anything that needs to be added.

Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed the update! LGTM, thanks for adding the tests.

Are you happy for us to hit submit?

@yichi170
Copy link
Contributor Author

Yes, I don't have the permission to merge the PR. Thank you so much!

@arsenm arsenm merged commit 0c6141a into llvm:main Sep 11, 2025
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 11, 2025

LLVM Buildbot has detected a new failure on builder cross-project-tests-sie-ubuntu running on doug-worker-1a while building llvm at step 6 "test-build-unified-tree-check-cross-project".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/181/builds/27670

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-cross-project) failure: test (failure)
******************** TEST 'cross-project-tests :: debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp' FAILED ********************
Exit Code: 2

Command Output (stdout):
--
optnone-vectors-and-functions.cpp: (1.0000)

## BEGIN ##
.   [0, "int vector[4] test1<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 30, 11, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [1, "int vector[4] test1<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 31, 12, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [2, "int vector[4] test1<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 32, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [3, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 59, 7, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [4, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 61, 9, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [5, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 62, 12, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [6, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 65, 14, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [7, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 66, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [8, "int vector[4] test3<int vector[4]>(int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 93, 23, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [9, "int vector[4] test3<int vector[4]>(int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 94, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [10, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [11, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.SAME", []]
.   [12, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.SAME", []]
.   [13, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.SAME", []]
.   [14, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 111, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
## END (15 steps) ##


  optnone-vectors-and-functions.cpp:110 [i] ExpectValue [0/21]
    expected encountered watches:
      0
      1
      2
      3

  optnone-vectors-and-functions.cpp:110-111 [x[0]] ExpectValue [0/14]
    expected encountered watches:
      1
      8

  optnone-vectors-and-functions.cpp:110-111 [x[1]] ExpectValue [0/14]
    expected encountered watches:
      2
      9

  optnone-vectors-and-functions.cpp:110-111 [x[2]] ExpectValue [0/14]
    expected encountered watches:
      10
      3

  optnone-vectors-and-functions.cpp:110-111 [x[3]] ExpectValue [0/14]
    expected encountered watches:
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants