Skip to content

Commit

Permalink
[RyuJIT] static readonly fields can be Invariant (#44562)
Browse files Browse the repository at this point in the history
Co-authored-by: Andy Ayers <andya@microsoft.com>
  • Loading branch information
EgorBo and AndyAyersMS committed Mar 2, 2021
1 parent c003837 commit 94e163a
Showing 1 changed file with 19 additions and 1 deletion.
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)
{
JITDUMP("Marking initialized static read-only field '%s' as invariant.\n", eeGetFieldName(symHnd));
tree->gtFlags |= GTF_IND_INVARIANT;
tree->gtFlags &= ~GTF_ICON_INITCLASS;
addr->gtFlags = GTF_ICON_CONST_PTR;
}

return fgMorphSmpOp(tree);
}
else
Expand Down

0 comments on commit 94e163a

Please sign in to comment.