diff --git a/src/coreclr/jit/assertionprop.cpp b/src/coreclr/jit/assertionprop.cpp index b140c8caf8aa8..430287284e6f1 100644 --- a/src/coreclr/jit/assertionprop.cpp +++ b/src/coreclr/jit/assertionprop.cpp @@ -3420,6 +3420,18 @@ GenTree* Compiler::optConstantAssertionProp(AssertionDsc* curAssertion, // Here we have to allocate a new 'large' node to replace the old one newTree = gtNewIconHandleNode(curAssertion->op2.u1.iconVal, curAssertion->op2.u1.iconFlags & GTF_ICON_HDL_MASK); + + // Make sure we don't retype const gc handles to TYP_I_IMPL + // Although, it's possible for e.g. GTF_ICON_STATIC_HDL + if (!newTree->IsIntegralConst(0) && newTree->IsIconHandle(GTF_ICON_STR_HDL)) + { + if (tree->TypeIs(TYP_BYREF)) + { + // Conservatively don't allow propagation of ICON TYP_REF into BYREF + return nullptr; + } + newTree->ChangeType(tree->TypeGet()); + } } else { diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 9bc2d82c268c9..288ab6a3b97e6 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -11121,8 +11121,15 @@ void Compiler::gtDispConst(GenTree* tree) if (tree->TypeGet() == TYP_REF) { - assert(tree->AsIntCon()->gtIconVal == 0); - printf(" null"); + if (tree->AsIntCon()->gtIconVal == 0) + { + printf(" null"); + } + else + { + assert(doesMethodHaveFrozenString()); + printf(" 0x%llx", dspIconVal); + } } else if ((tree->AsIntCon()->gtIconVal > -1000) && (tree->AsIntCon()->gtIconVal < 1000)) { diff --git a/src/coreclr/jit/optcse.cpp b/src/coreclr/jit/optcse.cpp index 76912c0e174ac..3d300d898e8bd 100644 --- a/src/coreclr/jit/optcse.cpp +++ b/src/coreclr/jit/optcse.cpp @@ -478,8 +478,9 @@ unsigned Compiler::optValnumCSE_Index(GenTree* tree, Statement* stmt) assert(vnStore->IsVNConstant(vnLibNorm)); // We don't share small offset constants when they require a reloc + // Also, we don't share non-null const gc handles // - if (!tree->AsIntConCommon()->ImmedValNeedsReloc(this)) + if (!tree->AsIntConCommon()->ImmedValNeedsReloc(this) && ((tree->IsIntegralConst(0)) || !varTypeIsGC(tree))) { // Here we make constants that have the same upper bits use the same key // diff --git a/src/coreclr/jit/valuenum.cpp b/src/coreclr/jit/valuenum.cpp index 2066b8ed208b6..2495cd4e90efa 100644 --- a/src/coreclr/jit/valuenum.cpp +++ b/src/coreclr/jit/valuenum.cpp @@ -8162,7 +8162,7 @@ void Compiler::fgValueNumberTreeConst(GenTree* tree) } else { - assert(tree->IsIconHandle(GTF_ICON_STR_HDL)); // Constant object can be only frozen string. + assert(doesMethodHaveFrozenString()); // Constant object can be only frozen string. tree->gtVNPair.SetBoth( vnStore->VNForHandle(ssize_t(tree->AsIntConCommon()->IconValue()), tree->GetIconHandleFlag())); }