Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Backport the fix for dotnet/runtime issue 46529 to dotnet/coreclr 3.1…
Browse files Browse the repository at this point in the history
… branch (#28146)

Fixes incorrect behavior a 64-bit long with shift of 32 or more combined with a cast to int
  • Loading branch information
briansull authored Mar 10, 2021
1 parent 13f6e4c commit 3800437
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,26 @@ GenTree* Compiler::fgMorphCast(GenTree* tree)
// Don't try to optimize.
assert(!canPushCast);
}
else if ((shiftAmountValue >= 32) && ((tree->gtFlags & GTF_ALL_EFFECT) == 0))
else if (shiftAmountValue >= 32)
{
// Result of the shift is zero.
DEBUG_DESTROY_NODE(tree);
GenTree* zero = gtNewZeroConNode(TYP_INT);
return fgMorphTree(zero);
// We know that we have a narrowing cast ([u]long -> [u]int)
// and that we are casting to a 32-bit value, which will result in zero.
//
// Check to see if we have any side-effects that we must keep
//
if ((tree->gtFlags & GTF_ALL_EFFECT) == 0)
{
// Result of the shift is zero.
DEBUG_DESTROY_NODE(tree);
GenTree* zero = gtNewZeroConNode(TYP_INT);
return fgMorphTree(zero);
}
else // We do have a side-effect
{
// We could create a GT_COMMA node here to keep the side-effect and return a zero
// Instead we just don't try to optimize this case.
canPushCast = false;
}
}
else
{
Expand Down

0 comments on commit 3800437

Please sign in to comment.