[llvm] 7341123 - [GlobalISel][KnownBits] Early return on out of bound shift amounts

Konstantin Schwarz via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 12 09:39:30 PDT 2020


Author: Konstantin Schwarz
Date: 2020-10-12T18:39:19+02:00
New Revision: 734112343917a011676c2915c5e5d29803a51ba6

URL: https://github.com/llvm/llvm-project/commit/734112343917a011676c2915c5e5d29803a51ba6
DIFF: https://github.com/llvm/llvm-project/commit/734112343917a011676c2915c5e5d29803a51ba6.diff

LOG: [GlobalISel][KnownBits] Early return on out of bound shift amounts

If the known shift amount is bigger than or equal to the bitwidth of the type of the value to be shifted,
the result is target dependent, so don't try to infer any bits.

This fixes a crash we've seen in one of our internal test suites.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D89232

Added: 
    

Modified: 
    llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
    llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
index 3ebbac9fd659..81a89a6eb0b7 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
@@ -397,6 +397,10 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
     uint64_t Shift = RHSKnown.getConstant().getZExtValue();
     LLVM_DEBUG(dbgs() << '[' << Depth << "] Shift is " << Shift << '\n');
 
+    // Guard against oversized shift amounts
+    if (Shift >= MRI.getType(MI.getOperand(1).getReg()).getScalarSizeInBits())
+      break;
+
     computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
                          Depth + 1);
 

diff  --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
index faf6f7087ac0..5f1d24b1078b 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
@@ -725,3 +725,38 @@ TEST_F(AArch64GISelMITest, TestKnownBitsUMax) {
   EXPECT_EQ(0xffu, KnownUmax.Zero.getZExtValue());
   EXPECT_EQ(0xffffffffffffff00, KnownUmax.One.getZExtValue());
 }
+
+TEST_F(AArch64GISelMITest, TestInvalidQueries) {
+  StringRef MIRString = R"(
+   %src:_(s32) = COPY $w0
+   %thirty2:_(s32) = G_CONSTANT i32 32
+   %equalSized:_(s32) = G_SHL %src, %thirty2
+   %copy1:_(s32) = COPY %equalSized
+   %thirty3:_(s32) = G_CONSTANT i32 33
+   %biggerSized:_(s32) = G_SHL %src, %thirty3
+   %copy2:_(s32) = COPY %biggerSized
+)";
+  setUp(MIRString);
+  if (!TM)
+    return;
+
+  Register EqSizedCopyReg = Copies[Copies.size() - 2];
+  MachineInstr *EqSizedCopy = MRI->getVRegDef(EqSizedCopyReg);
+  Register EqSizedShl = EqSizedCopy->getOperand(1).getReg();
+
+  Register BiggerSizedCopyReg = Copies[Copies.size() - 1];
+  MachineInstr *BiggerSizedCopy = MRI->getVRegDef(BiggerSizedCopyReg);
+  Register BiggerSizedShl = BiggerSizedCopy->getOperand(1).getReg();
+
+  GISelKnownBits Info(*MF);
+  KnownBits EqSizeRes = Info.getKnownBits(EqSizedShl);
+  KnownBits BiggerSizeRes = Info.getKnownBits(BiggerSizedShl);
+
+
+  // We don't know what the result of the shift is, but we should not crash
+  EXPECT_TRUE(EqSizeRes.One.isNullValue());
+  EXPECT_TRUE(EqSizeRes.Zero.isNullValue());
+
+  EXPECT_TRUE(BiggerSizeRes.One.isNullValue());
+  EXPECT_TRUE(BiggerSizeRes.Zero.isNullValue());
+}


        


More information about the llvm-commits mailing list