[jit] Optimize constrained calls to Enum.GetHasCode () to avoid boxing. Fixes #51330...
[mono.git] / mono / mini / method-to-ir.c
index 8c046edac8d3889feea2ece0de6fd32fa69f5bf3..f5ee5fa74e42e9cd038eca80e073927f82445faf 100644 (file)
@@ -8582,6 +8582,15 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                CHECK_CFG_ERROR;
                                        }
                                }
+
+                               if (constrained_class->enumtype && !strcmp (cmethod->name, "GetHashCode")) {
+                                       /* Use the corresponding method from the base type to avoid boxing */
+                                       MonoType *base_type = mono_class_enum_basetype (constrained_class);
+                                       g_assert (base_type);
+                                       constrained_class = mono_class_from_mono_type (base_type);
+                                       cmethod = mono_class_get_method_from_name (constrained_class, cmethod->name, 0);
+                                       g_assert (cmethod);
+                               }
                        }
                                        
                        if (!dont_verify && !cfg->skip_visibility) {