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

Commit 3800437

Browse files
authored
Backport the fix for dotnet/runtime issue 46529 to dotnet/coreclr 3.1 branch (#28146)
Fixes incorrect behavior a 64-bit long with shift of 32 or more combined with a cast to int
1 parent 13f6e4c commit 3800437

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/jit/morph.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,26 @@ GenTree* Compiler::fgMorphCast(GenTree* tree)
461461
// Don't try to optimize.
462462
assert(!canPushCast);
463463
}
464-
else if ((shiftAmountValue >= 32) && ((tree->gtFlags & GTF_ALL_EFFECT) == 0))
464+
else if (shiftAmountValue >= 32)
465465
{
466-
// Result of the shift is zero.
467-
DEBUG_DESTROY_NODE(tree);
468-
GenTree* zero = gtNewZeroConNode(TYP_INT);
469-
return fgMorphTree(zero);
466+
// We know that we have a narrowing cast ([u]long -> [u]int)
467+
// and that we are casting to a 32-bit value, which will result in zero.
468+
//
469+
// Check to see if we have any side-effects that we must keep
470+
//
471+
if ((tree->gtFlags & GTF_ALL_EFFECT) == 0)
472+
{
473+
// Result of the shift is zero.
474+
DEBUG_DESTROY_NODE(tree);
475+
GenTree* zero = gtNewZeroConNode(TYP_INT);
476+
return fgMorphTree(zero);
477+
}
478+
else // We do have a side-effect
479+
{
480+
// We could create a GT_COMMA node here to keep the side-effect and return a zero
481+
// Instead we just don't try to optimize this case.
482+
canPushCast = false;
483+
}
470484
}
471485
else
472486
{

0 commit comments

Comments
 (0)