Don't generate a runtime lookup for static base if not needed (dotnet/coreclr#9833)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Mon, 27 Feb 2017 23:21:57 +0000 (15:21 -0800)
committerGitHub <noreply@github.com>
Mon, 27 Feb 2017 23:21:57 +0000 (15:21 -0800)
`fgInitThisClass` makes sure that the type initializer for the type that
owns the method being compiled has run. On CoreCLR, the fallthrough code
somehow ends up not doing a runtime lookup if the owning type is not
canonical. We need to avoid it on CoreRT as well.

Commit migrated from https://github.com/dotnet/coreclr/commit/4fb2f0ff574502894ab7657fe89e9f9f99eafe1a

src/coreclr/src/jit/morph.cpp

index 0035b9d..cac0ee8 100644 (file)
@@ -16292,6 +16292,15 @@ GenTreePtr Compiler::fgInitThisClass()
             CORINFO_RESOLVED_TOKEN resolvedToken;
             memset(&resolvedToken, 0, sizeof(resolvedToken));
 
+            // We are in a shared method body, but maybe we don't need a runtime lookup after all.
+            // This covers the case of a generic method on a non-generic type.
+            if (!(info.compClassAttr & CORINFO_FLG_SHAREDINST))
+            {
+                resolvedToken.hClass = info.compClassHnd;
+                return impReadyToRunHelperToTree(&resolvedToken, CORINFO_HELP_READYTORUN_STATIC_BASE, TYP_BYREF);
+            }
+
+            // We need a runtime lookup.
             GenTreePtr ctxTree = getRuntimeContextTree(kind.runtimeLookupKind);
 
             // CORINFO_HELP_READYTORUN_GENERIC_STATIC_BASE with a zeroed out resolvedToken means "get the static