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

Konstantin Schwarz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 12 04:22:37 PDT 2020


kschwarz created this revision.
kschwarz added reviewers: arsenm, aditya_nandakumar.
Herald added subscribers: hiraditya, rovka.
Herald added a project: LLVM.
kschwarz requested review of this revision.
Herald added a subscriber: wdng.

If the known shift amount is bigger than 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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89232

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


Index: llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
===================================================================
--- llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
+++ llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
@@ -725,3 +725,26 @@
   EXPECT_EQ(0xffu, KnownUmax.Zero.getZExtValue());
   EXPECT_EQ(0xffffffffffffff00, KnownUmax.One.getZExtValue());
 }
+
+TEST_F(AArch64GISelMITest, TestInvalidQueries) {
+  StringRef MIRString = R"(
+   %src:_(s32) = COPY $w0
+   %thirty3:_(s32) = G_CONSTANT i32 33
+   %shift:_(s32) = G_SHL %src, %thirty3
+   %final_copy:_(s32) = COPY %shift
+)";
+  setUp(MIRString);
+  if (!TM)
+    return;
+
+  Register CopyReg = Copies[Copies.size() - 1];
+  MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+  Register ShlRes = FinalCopy->getOperand(1).getReg();
+
+  GISelKnownBits Info(*MF);
+  KnownBits Res = Info.getKnownBits(ShlRes);
+
+  // We don't know what the result of the shift is, but we should not crash
+  EXPECT_TRUE(Res.One.isNullValue());
+  EXPECT_TRUE(Res.Zero.isNullValue());
+}
\ No newline at end of file
Index: llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
+++ llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
@@ -397,6 +397,10 @@
     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()).getSizeInBits())
+      break;
+
     computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
                          Depth + 1);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89232.297554.patch
Type: text/x-patch
Size: 1716 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201012/1497a79f/attachment.bin>


More information about the llvm-commits mailing list