Skip to content

Optimize casts from constant byrefs #99130

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,14 @@ GenTree* Compiler::fgMorphExpandCast(GenTreeCast* tree)
// we fix this by copying the GC pointer to a non-gc pointer temp.
noway_assert(!varTypeIsGC(dstType) && "How can we have a cast to a GCRef here?");

// we can just retype byref constants
if (oper->IsCnsIntOrI() && oper->TypeIs(TYP_BYREF) && varTypeIsI(dstType))
{
assert(srcType == TYP_BYREF);
oper->gtType = dstType;
return fgMorphTree(oper);
}

// We generate a store to an int and then do the cast from an int. With this we avoid
// the gc problem and we allow casts to bytes, longs, etc...
unsigned lclNum = lvaGrabTemp(true DEBUGARG("Cast away GC"));
Expand Down
Loading