AMDGPU/GlobalISel: Implement computeNumSignBitsForTargetInstr
diff --git a/llvm/unittests/CodeGen/GlobalISel/GISelMITest.cpp b/llvm/unittests/CodeGen/GlobalISel/GISelMITest.cpp
index 5b59ee7..ef50a0f 100644
--- a/llvm/unittests/CodeGen/GlobalISel/GISelMITest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/GISelMITest.cpp
@@ -66,3 +66,41 @@
    Twine(MIRFunc) + Twine("...\n"))
       .toNullTerminatedStringRef(S);
 }
+
+std::unique_ptr<LLVMTargetMachine>
+AMDGPUGISelMITest::createTargetMachine() const {
+  Triple TargetTriple("amdgcn-amd-amdhsa");
+  std::string Error;
+  const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);
+  if (!T)
+    return nullptr;
+
+  TargetOptions Options;
+  return std::unique_ptr<LLVMTargetMachine>(
+      static_cast<LLVMTargetMachine *>(T->createTargetMachine(
+          "amdgcn-amd-amdhsa", "gfx900", "", Options, None, None,
+          CodeGenOpt::Aggressive)));
+}
+
+void AMDGPUGISelMITest::getTargetTestModuleString(
+  SmallString<512> &S, StringRef MIRFunc) const {
+  (Twine(R"MIR(
+---
+...
+name: func
+tracksRegLiveness: true
+registers:
+  - { id: 0, class: _ }
+  - { id: 1, class: _ }
+  - { id: 2, class: _ }
+  - { id: 3, class: _ }
+body: |
+  bb.1:
+    liveins: $vgpr0, $vgpr1, $vgpr2
+
+    %0(s32) = COPY $vgpr0
+    %1(s32) = COPY $vgpr1
+    %2(s32) = COPY $vgpr2
+)MIR") + Twine(MIRFunc) + Twine("...\n"))
+                            .toNullTerminatedStringRef(S);
+}
diff --git a/llvm/unittests/CodeGen/GlobalISel/GISelMITest.h b/llvm/unittests/CodeGen/GlobalISel/GISelMITest.h
index 53f5a1c..db3f2b9 100644
--- a/llvm/unittests/CodeGen/GlobalISel/GISelMITest.h
+++ b/llvm/unittests/CodeGen/GlobalISel/GISelMITest.h
@@ -146,6 +146,12 @@
                                  StringRef MIRFunc) const override;
 };
 
+class AMDGPUGISelMITest : public GISelMITest {
+  std::unique_ptr<LLVMTargetMachine> createTargetMachine() const override;
+  void getTargetTestModuleString(SmallString<512> &S,
+                                 StringRef MIRFunc) const override;
+};
+
 #define DefineLegalizerInfo(Name, SettingUpActionsBlock)                       \
   class Name##Info : public LegalizerInfo {                                    \
   public:                                                                      \
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
index e2479b9..fe0c142 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
@@ -398,3 +398,36 @@
   EXPECT_EQ(8u, Info.computeNumSignBits(CopyTruncNeg1));
   EXPECT_EQ(5u, Info.computeNumSignBits(CopyTrunc7));
 }
+
+TEST_F(AMDGPUGISelMITest, TestNumSignBitsTrunc) {
+  StringRef MIRString =
+    "  %3:_(<4 x s32>) = G_IMPLICIT_DEF\n"
+    "  %4:_(s32) = G_IMPLICIT_DEF\n"
+    "  %5:_(s32) = G_AMDGPU_BUFFER_LOAD_UBYTE %3, %4, %4, %4, 0, 0, 0 :: (load 1)\n"
+    "  %6:_(s32) = COPY %5\n"
+
+    "  %7:_(s32) = G_AMDGPU_BUFFER_LOAD_SBYTE %3, %4, %4, %4, 0, 0, 0 :: (load 1)\n"
+    "  %8:_(s32) = COPY %7\n"
+
+    "  %9:_(s32) = G_AMDGPU_BUFFER_LOAD_USHORT %3, %4, %4, %4, 0, 0, 0 :: (load 2)\n"
+    "  %10:_(s32) = COPY %9\n"
+
+    "  %11:_(s32) = G_AMDGPU_BUFFER_LOAD_SSHORT %3, %4, %4, %4, 0, 0, 0 :: (load 2)\n"
+    "  %12:_(s32) = COPY %11\n";
+
+  setUp(MIRString);
+  if (!TM)
+    return;
+
+  Register CopyLoadUByte = Copies[Copies.size() - 4];
+  Register CopyLoadSByte = Copies[Copies.size() - 3];
+  Register CopyLoadUShort = Copies[Copies.size() - 2];
+  Register CopyLoadSShort = Copies[Copies.size() - 1];
+
+  GISelKnownBits Info(*MF);
+
+  EXPECT_EQ(24u, Info.computeNumSignBits(CopyLoadUByte));
+  EXPECT_EQ(25u, Info.computeNumSignBits(CopyLoadSByte));
+  EXPECT_EQ(16u, Info.computeNumSignBits(CopyLoadUShort));
+  EXPECT_EQ(17u, Info.computeNumSignBits(CopyLoadSShort));
+}