Skip to content

Commit

Permalink
Avoid some unnecessary unsigned -> signed casts (#70884)
Browse files Browse the repository at this point in the history
We do not need a signed cast if we know the sign bit from a previous unsigned cast.
  • Loading branch information
jakobbotsch authored Jun 27, 2022
1 parent e8e1451 commit a052819
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,11 +1277,16 @@ bool Compiler::fgCastNeeded(GenTree* tree, var_types toType)
return false;
}
//
// If the sign-ness of the two types are different then a cast is necessary
// If the sign-ness of the two types are different then a cast is necessary, except for
// an unsigned -> signed cast where we already know the sign bit is zero.
//
if (varTypeIsUnsigned(toType) != varTypeIsUnsigned(fromType))
{
return true;
bool isZeroExtension = varTypeIsUnsigned(fromType) && (genTypeSize(fromType) < genTypeSize(toType));
if (!isZeroExtension)
{
return true;
}
}
//
// If the from type is the same size or smaller then an additional cast is not necessary
Expand Down

0 comments on commit a052819

Please sign in to comment.