Expand CORINFO_HELP_CHKCASTANY cast (#86728)
authorEgor Bogatov <egorbo@gmail.com>
Fri, 26 May 2023 09:08:58 +0000 (11:08 +0200)
committerGitHub <noreply@github.com>
Fri, 26 May 2023 09:08:58 +0000 (11:08 +0200)
src/coreclr/jit/importer.cpp

index 680ee0f..3cfa6bd 100644 (file)
@@ -5487,6 +5487,14 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
         {
             // Jit can only inline expand CHKCASTCLASS and CHKCASTARRAY helpers.
             canExpandInline = (helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY);
+
+            // For ChkCastAny we ignore cases where the class is known to be abstract or is an interface.
+            if (helper == CORINFO_HELP_CHKCASTANY)
+            {
+                const bool isAbstract = (info.compCompHnd->getClassAttribs(pResolvedToken->hClass) &
+                                         (CORINFO_FLG_INTERFACE | CORINFO_FLG_ABSTRACT)) != 0;
+                canExpandInline = !isAbstract;
+            }
         }
         else if ((helper == CORINFO_HELP_ISINSTANCEOFCLASS) || (helper == CORINFO_HELP_ISINSTANCEOFARRAY))
         {
@@ -5625,7 +5633,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
     if (isCastClass)
     {
         assert((helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY) ||
-               (helper == CORINFO_HELP_CHKCASTINTERFACE));
+               (helper == CORINFO_HELP_CHKCASTANY) || (helper == CORINFO_HELP_CHKCASTINTERFACE));
 
         CorInfoHelpFunc specialHelper = helper;
         if ((helper == CORINFO_HELP_CHKCASTCLASS) &&