diff --git a/src/System.Private.CoreLib/shared/System/Char.cs b/src/System.Private.CoreLib/shared/System/Char.cs index 3835f8e99600..f69c63192c40 100644 --- a/src/System.Private.CoreLib/shared/System/Char.cs +++ b/src/System.Private.CoreLib/shared/System/Char.cs @@ -38,7 +38,7 @@ namespace System public const char MinValue = (char)0x00; // Unicode category values from Unicode U+0000 ~ U+00FF. Store them in byte[] array to save space. - private static ReadOnlySpan CategoryForLatin1 => new byte[] { // uses C# compiler's optimization for static byte[] data + private static ReadOnlySpan CategoryForLatin1 => new byte[256] { // uses C# compiler's optimization for static byte[] data (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, // 0000 - 0007 (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, // 0008 - 000F (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, (byte)UnicodeCategory.Control, // 0010 - 0017 @@ -89,7 +89,7 @@ private static bool IsAscii(char ch) private static UnicodeCategory GetLatin1UnicodeCategory(char ch) { Debug.Assert(IsLatin1(ch), "char.GetLatin1UnicodeCategory(): ch should be <= 007f"); - return (UnicodeCategory)CategoryForLatin1[(int)ch]; + return (UnicodeCategory)CategoryForLatin1[(byte)ch]; } // diff --git a/src/jit/rangecheck.cpp b/src/jit/rangecheck.cpp index 96fcaf9561ec..a307e04163c3 100644 --- a/src/jit/rangecheck.cpp +++ b/src/jit/rangecheck.cpp @@ -1050,6 +1050,16 @@ bool RangeCheck::ComputeDoesOverflow(BasicBlock* block, GenTree* expr) { overflows = DoesPhiOverflow(block, expr); } + else if (expr->OperGet() == GT_CAST) + { + GenTreeCast* castExpr = expr->AsCast(); + if ((castExpr->CastToType() == TYP_UBYTE || castExpr->CastToType() == TYP_USHORT) && + (castExpr->CastFromType() == TYP_INT)) + { + overflows = false; + } + } + GetOverflowMap()->Set(expr, overflows, OverflowMap::Overwrite); m_pSearchPath->Remove(expr); return overflows; @@ -1144,9 +1154,11 @@ Range RangeCheck::ComputeRange(BasicBlock* block, GenTree* expr, bool monotonic JITDUMP("%s\n", range.ToString(m_pCompiler->getAllocatorDebugOnly())); } } - else if (varTypeIsSmallInt(expr->TypeGet())) + else if (expr->OperIs(GT_CAST) || varTypeIsSmallInt(expr->TypeGet())) { - switch (expr->TypeGet()) + var_types exprType = expr->OperIs(GT_CAST) ? expr->AsCast()->CastToType() : expr->TypeGet(); + + switch (exprType) { case TYP_UBYTE: range = Range(Limit(Limit::keConstant, 0), Limit(Limit::keConstant, 255));