From 79f9415475a72623a8c51a942b229d8d6157feb1 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Tue, 18 May 2021 21:28:30 +0300 Subject: [PATCH] JIT: Optimize *x = dblCns to *x = intCns (#52298) --- src/coreclr/jit/lowerxarch.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/coreclr/jit/lowerxarch.cpp b/src/coreclr/jit/lowerxarch.cpp index c9b0d6ef7ae89..bbf7a72b6b5a4 100644 --- a/src/coreclr/jit/lowerxarch.cpp +++ b/src/coreclr/jit/lowerxarch.cpp @@ -130,6 +130,38 @@ void Lowering::LowerStoreIndir(GenTreeIndir* node) return; } } + else if (node->AsStoreInd()->Data()->OperIs(GT_CNS_DBL)) + { + // Optimize *x = DCON to *x = ICON which is slightly faster on xarch + GenTree* data = node->AsStoreInd()->Data(); + double dblCns = data->AsDblCon()->gtDconVal; + ssize_t intCns = 0; + var_types type = TYP_UNKNOWN; + + if (node->TypeIs(TYP_FLOAT)) + { + float fltCns = static_cast(dblCns); // should be a safe round-trip + intCns = static_cast(*reinterpret_cast(&fltCns)); + type = TYP_UINT; + } +#ifdef TARGET_AMD64 + else + { + assert(node->TypeIs(TYP_DOUBLE)); + intCns = static_cast(*reinterpret_cast(&dblCns)); + type = TYP_ULONG; + } +#endif + + if (type != TYP_UNKNOWN) + { + data->SetContained(); + data->ChangeOperConst(GT_CNS_INT); + data->AsIntCon()->SetIconValue(intCns); + data->ChangeType(type); + node->ChangeType(type); + } + } ContainCheckStoreIndir(node); }