From 92b125fc7671b82433f3adbd085b7e6b29b1a73d Mon Sep 17 00:00:00 2001 From: Sergey Andreenko Date: Thu, 17 Sep 2020 22:21:29 -0700 Subject: [PATCH] Allow more obj(addr(lcl_var) foldings. (#42343) * Allow `struct with no GCPointers <-> block` replacements. * Alllow local morph to fold `OBJ(ADDR(LCL_VAR))` when obj and lclVar loayouts are compatible. --- src/coreclr/src/jit/layout.cpp | 7 ++++--- src/coreclr/src/jit/lclmorph.cpp | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/coreclr/src/jit/layout.cpp b/src/coreclr/src/jit/layout.cpp index bafe17bb0b0ac..87b0bef89433a 100644 --- a/src/coreclr/src/jit/layout.cpp +++ b/src/coreclr/src/jit/layout.cpp @@ -430,10 +430,8 @@ bool ClassLayout::AreCompatible(const ClassLayout* layout1, const ClassLayout* l { CORINFO_CLASS_HANDLE clsHnd1 = layout1->GetClassHandle(); CORINFO_CLASS_HANDLE clsHnd2 = layout2->GetClassHandle(); - assert(clsHnd1 != NO_CLASS_HANDLE); - assert(clsHnd2 != NO_CLASS_HANDLE); - if (clsHnd1 == clsHnd2) + if ((clsHnd1 != NO_CLASS_HANDLE) && (clsHnd1 == clsHnd2)) { return true; } @@ -453,7 +451,10 @@ bool ClassLayout::AreCompatible(const ClassLayout* layout1, const ClassLayout* l return true; } + assert(clsHnd1 != NO_CLASS_HANDLE); + assert(clsHnd2 != NO_CLASS_HANDLE); assert(layout1->HasGCPtr() && layout2->HasGCPtr()); + if (layout1->GetGCPtrCount() != layout2->GetGCPtrCount()) { return false; diff --git a/src/coreclr/src/jit/lclmorph.cpp b/src/coreclr/src/jit/lclmorph.cpp index 1df49356c6ae3..811cfc038060e 100644 --- a/src/coreclr/src/jit/lclmorph.cpp +++ b/src/coreclr/src/jit/lclmorph.cpp @@ -1021,7 +1021,8 @@ class LocalAddressVisitor final : public GenTreeVisitor // otherwise the below layout equality check would be insufficient. assert(varDsc->GetLayout() != nullptr); - if ((val.Offset() == 0) && (structLayout == varDsc->GetLayout())) + if ((val.Offset() == 0) && (structLayout != nullptr) && + ClassLayout::AreCompatible(structLayout, varDsc->GetLayout())) { indir->ChangeOper(GT_LCL_VAR); indir->AsLclVar()->SetLclNum(val.LclNum());