From 80cd4422b38ebbcecf83d4b69bd79416851f68f1 Mon Sep 17 00:00:00 2001 From: Bruce Forstall Date: Thu, 28 Mar 2024 14:07:33 -0700 Subject: [PATCH] Fix optAssertionProp_Update for IND of handle constant replacement --- src/coreclr/jit/assertionprop.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/coreclr/jit/assertionprop.cpp b/src/coreclr/jit/assertionprop.cpp index 68a01a1ab7fcd1..f26e751c67cd9e 100644 --- a/src/coreclr/jit/assertionprop.cpp +++ b/src/coreclr/jit/assertionprop.cpp @@ -5428,6 +5428,25 @@ GenTree* Compiler::optAssertionProp_Update(GenTree* newTree, GenTree* tree, Stat if (parent != nullptr) { parent->ReplaceOperand(useEdge, newTree); + + // If the parent is a GT_IND and we replaced the child with a handle constant, we might need + // to mark the GT_IND as invariant. This is the same as what gtNewIndOfIconHandleNode() does. + // Review: should some kind of more general morphing take care of this? + // Should this share code with gtNewIndOfIconHandleNode()? + + if (parent->OperIs(GT_IND) && newTree->IsIconHandle()) + { + GenTreeFlags iconFlags = newTree->GetIconHandleFlag(); + if (GenTree::HandleKindDataIsInvariant(iconFlags)) + { + parent->gtFlags |= GTF_IND_INVARIANT; + if (iconFlags == GTF_ICON_STR_HDL) + { + // String literals are never null + parent->gtFlags |= GTF_IND_NONNULL; + } + } + } } else {