Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RyuJIT] static readonly fields can be Invariant #44562

Merged
merged 9 commits into from
Mar 2, 2021
20 changes: 19 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6490,7 +6490,17 @@ GenTree* Compiler::fgMorphField(GenTree* tree, MorphAddrContext* mac)
assert(pFldAddr == nullptr);

#ifdef TARGET_64BIT
if (IMAGE_REL_BASED_REL32 != eeGetRelocTypeHint(fldAddr))
bool isStaticReadOnlyInited = false;
bool plsSpeculative = true;
if (info.compCompHnd->getStaticFieldCurrentClass(symHnd, &plsSpeculative) != NO_CLASS_HANDLE)
{
isStaticReadOnlyInited = !plsSpeculative;
}

// even if RelocTypeHint is REL32 let's still prefer IND over GT_CLS_VAR
// for static readonly fields of statically initialized classes - thus we can
// apply GTF_IND_INVARIANT flag and make it hoistable/CSE-friendly
if (isStaticReadOnlyInited || (IMAGE_REL_BASED_REL32 != eeGetRelocTypeHint(fldAddr)))
{
// The address is not directly addressible, so force it into a
// constant, so we handle it properly
Expand All @@ -6510,6 +6520,14 @@ GenTree* Compiler::fgMorphField(GenTree* tree, MorphAddrContext* mac)
tree->SetOper(GT_IND);
tree->AsOp()->gtOp1 = addr;

if (isStaticReadOnlyInited)
{
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
JITDUMP("Marking initialized static read-only field '%s' as invariant.\n", eeGetFieldName(symHnd));
tree->gtFlags |= GTF_IND_INVARIANT;
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
tree->gtFlags &= ~GTF_ICON_INITCLASS;
addr->gtFlags = GTF_ICON_CONST_PTR;
}

return fgMorphSmpOp(tree);
}
else
Expand Down