[Analysis] Fix another issue when querying vscale attributes on functions
authorDavid Sherwood <david.sherwood@arm.com>
Fri, 24 Sep 2021 12:31:38 +0000 (13:31 +0100)
committerDavid Sherwood <david.sherwood@arm.com>
Fri, 24 Sep 2021 12:37:23 +0000 (13:37 +0100)
There are several places in the code that are currently broken where
we assume an Instruction is always a member of a BasicBlock that
lives in a Function. This is a problem specifically when
attempting to get the vscale_range attribute. This patch adds checks
that an Instruction's parent also has a parent!

I've added a test for a function-less @llvm.vscale intrinsic call here:

  unittests/Analysis/ValueTrackingTest.cpp

llvm/lib/Analysis/ValueTracking.cpp
llvm/unittests/Analysis/ValueTrackingTest.cpp

index ab5c388..35178f8 100644 (file)
@@ -1685,7 +1685,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
           Known.Zero.setBitsFrom(31);
         break;
       case Intrinsic::vscale: {
-        if (!II->getParent() ||
+        if (!II->getParent() || !II->getFunction() ||
             !II->getFunction()->hasFnAttribute(Attribute::VScaleRange))
           break;
 
index 8d9df97..593100c 100644 (file)
@@ -1606,12 +1606,22 @@ TEST_F(ComputeKnownBitsTest, ComputeKnownBitsUnknownVScale) {
   CallInst *CI = Builder.CreateCall(TheFn, {}, {}, "");
 
   KnownBits Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0);
-  delete CI;
+  // There is no parent function so we cannot look up the vscale_range
+  // attribute to determine the number of bits.
+  EXPECT_EQ(Known.One.getZExtValue(), 0u);
+  EXPECT_EQ(Known.Zero.getZExtValue(), 0u);
 
+  BasicBlock *BB = BasicBlock::Create(Context);
+  BB->getInstList().push_back(CI);
+  Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0);
   // There is no parent function so we cannot look up the vscale_range
   // attribute to determine the number of bits.
   EXPECT_EQ(Known.One.getZExtValue(), 0u);
   EXPECT_EQ(Known.Zero.getZExtValue(), 0u);
+
+  CI->removeFromParent();
+  delete CI;
+  delete BB;
 }
 
 // 512 + [32, 64) doesn't produce overlapping bits.