[llvm] 442c408 - [SCEVExpander] GetOptimalInsertionPointForCastOf(): gracefully handle Constant's

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 19 08:39:32 PDT 2021


Author: Roman Lebedev
Date: 2021-04-19T18:38:39+03:00
New Revision: 442c408e0e4a4d794752e710a362a5449d585432

URL: https://github.com/llvm/llvm-project/commit/442c408e0e4a4d794752e710a362a5449d585432
DIFF: https://github.com/llvm/llvm-project/commit/442c408e0e4a4d794752e710a362a5449d585432.diff

LOG: [SCEVExpander] GetOptimalInsertionPointForCastOf(): gracefully handle Constant's

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.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index 1af8804da46d..3c0320ee0442 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -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,


        


More information about the llvm-commits mailing list