diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 078a745f39f40..0ea01bc931627 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -13501,6 +13501,12 @@ void Compiler::impImportBlockCode(BasicBlock* block) { op1->gtFlags |= (GTF_OVERFLOW | GTF_EXCEPT); } + + if (op1->gtGetOp1()->OperIsConst() && opts.OptimizationEnabled()) + { + // Try and fold the introduced cast + op1 = gtFoldExprConst(op1); + } } impPushOnStack(op1, tiRetVal); diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index debbe3b85f01e..9816306ebb6c1 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -12212,7 +12212,12 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) tree = gtFoldExpr(tree); } - tree->AsOp()->CheckDivideByConstOptimized(this); + // We may fail to fold + if (!tree->OperIsConst()) + { + tree->AsOp()->CheckDivideByConstOptimized(this); + } + return tree; } }