[SCEVExpander] GetOptimalInsertionPointForCastOf(): gracefully handle Constant's
authorRoman Lebedev <lebedev.ri@gmail.com>
Mon, 19 Apr 2021 14:36:20 +0000 (17:36 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Mon, 19 Apr 2021 15:38:39 +0000 (18:38 +0300)
I guess this case hasn't come up thus far, and i'm not sure if it can
really happen for the existing usages, thus no test in *this* commit.

But, the following commit adds test coverage,
there we'd expirience a crash without this fix.

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

index 1af8804..3c0320e 100644 (file)
@@ -130,8 +130,17 @@ SCEVExpander::GetOptimalInsertionPointForCastOf(Value *V) const {
   }
 
   // Cast the instruction immediately after the instruction.
-  Instruction *I = cast<Instruction>(V);
-  return findInsertPointAfter(I, &*Builder.GetInsertPoint());
+  if (Instruction *I = dyn_cast<Instruction>(V))
+    return findInsertPointAfter(I, &*Builder.GetInsertPoint());
+
+  // Otherwise, this must be some kind of a constant,
+  // so let's plop this cast into the function's entry block.
+  assert(isa<Constant>(V) &&
+         "Expected the cast argument to be a global/constant");
+  return Builder.GetInsertBlock()
+      ->getParent()
+      ->getEntryBlock()
+      .getFirstInsertionPt();
 }
 
 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,