From 84a65ca9d5bce40dc608e1857682b8513314f013 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Thu, 27 Jul 2023 22:31:18 +0200 Subject: [PATCH] Avoid boxing in Enum.HasFlag in Tier0 (#89348) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Co-authored-by: Michał Petryka <35800402+MichalPetryka@users.noreply.github.com> Co-authored-by: Jakob Botsch Nielsen --- src/coreclr/jit/importer.cpp | 13 ++++++++++++- src/coreclr/jit/importercalls.cpp | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 6e648ac..8f871b0 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -3160,7 +3160,18 @@ void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken) // and the other you get // *(temp+4) = expr - if (opts.OptimizationDisabled()) + // For minopts/debug code, try and minimize the total number + // of box temps by reusing an existing temp when possible. However, + bool shareBoxedTemps = opts.OptimizationDisabled(); + + // Avoid sharing in some tier 0 cases to, potentially, avoid boxing in Enum.HasFlag. + if (shareBoxedTemps && varTypeIsIntegral(exprToBox) && !lvaHaveManyLocals() && + (info.compCompHnd->isEnum(pResolvedToken->hClass, nullptr) != TypeCompareState::Must)) + { + shareBoxedTemps = false; + } + + if (shareBoxedTemps) { // For minopts/debug code, try and minimize the total number // of box temps by reusing an existing temp when possible. diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 23ed11f..3bb99bc 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -2580,6 +2580,10 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis, case NI_System_RuntimeType_get_TypeHandle: case NI_System_RuntimeTypeHandle_ToIntPtr: + // This one is not simple, but it will help us + // to avoid some unnecessary boxing + case NI_System_Enum_HasFlag: + // Most atomics are compiled to single instructions case NI_System_Threading_Interlocked_And: case NI_System_Threading_Interlocked_Or: -- 2.7.4